blob: 23e4ec0567f221c94767947aaf56c14578adae7e [file] [log] [blame]
The Android Open Source Project9066cfe2009-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 Project9066cfe2009-03-03 19:31:44 -080016package android.net;
17
Felipe Leme1b103232016-01-22 09:44:57 -080018import android.annotation.IntDef;
Chalard Jean50bea3d2019-01-07 19:26:34 +090019import android.annotation.NonNull;
Robin Lee244ce8e2016-01-05 18:03:46 +000020import android.annotation.Nullable;
Jeff Sharkey30e06bb2017-04-24 11:18:03 -060021import android.annotation.RequiresPermission;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.annotation.SdkConstant;
23import android.annotation.SdkConstant.SdkConstantType;
Udam Sainib7c24872016-01-04 12:16:14 -080024import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060025import android.annotation.SystemService;
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010026import android.annotation.UnsupportedAppUsage;
Robert Greenwalt9258c642014-03-26 16:47:06 -070027import android.app.PendingIntent;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070028import android.content.Context;
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070029import android.content.Intent;
Robert Greenwalt42acef32009-08-12 16:08:25 -070030import android.os.Binder;
Mathew Inwood55418ea2018-12-20 15:30:45 +000031import android.os.Build;
Jeff Sharkey3a844fc2011-08-16 14:37:57 -070032import android.os.Build.VERSION_CODES;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080033import android.os.Bundle;
Robert Greenwalt9258c642014-03-26 16:47:06 -070034import android.os.Handler;
35import android.os.HandlerThread;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080036import android.os.IBinder;
37import android.os.INetworkActivityListener;
38import android.os.INetworkManagementService;
Robert Greenwalt9258c642014-03-26 16:47:06 -070039import android.os.Looper;
40import android.os.Message;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -070041import android.os.Messenger;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +090042import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080043import android.os.RemoteException;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080044import android.os.ResultReceiver;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080045import android.os.ServiceManager;
Hugo Benichicb883232017-05-11 13:16:17 +090046import android.os.ServiceSpecificException;
Jeff Sharkey961e3042011-08-29 16:02:57 -070047import android.provider.Settings;
Wink Saville36ffb042014-12-05 11:10:30 -080048import android.telephony.SubscriptionManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080049import android.util.ArrayMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -070050import android.util.Log;
Erik Kline35bf06c2017-01-26 18:08:28 +090051import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
Robert Greenwaltafa05c02014-05-21 20:04:36 -070053import com.android.internal.telephony.ITelephony;
Robert Greenwalt562cc542014-05-15 18:07:26 -070054import com.android.internal.telephony.PhoneConstants;
Hugo Benichidafed3d2017-03-06 09:17:06 +090055import com.android.internal.util.Preconditions;
56import com.android.internal.util.Protocol;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070057
Paul Jensenc91b5342014-08-27 12:38:45 -040058import libcore.net.event.NetworkEventDispatcher;
59
Felipe Leme1b103232016-01-22 09:44:57 -080060import java.lang.annotation.Retention;
61import java.lang.annotation.RetentionPolicy;
Jeremy Kleind42209d2015-12-28 15:11:58 -080062import java.net.InetAddress;
Jeff Vander Stoep0ac2c092018-07-23 10:57:53 -070063import java.net.InetSocketAddress;
Hugo Benichidafed3d2017-03-06 09:17:06 +090064import java.util.ArrayList;
Jeremy Kleind42209d2015-12-28 15:11:58 -080065import java.util.HashMap;
Hugo Benichidafed3d2017-03-06 09:17:06 +090066import java.util.List;
67import java.util.Map;
Jeremy Kleind42209d2015-12-28 15:11:58 -080068
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069/**
70 * Class that answers queries about the state of network connectivity. It also
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060071 * notifies applications when network connectivity changes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080072 * <p>
73 * The primary responsibilities of this class are to:
74 * <ol>
75 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
76 * <li>Send broadcast intents when network connectivity changes</li>
77 * <li>Attempt to "fail over" to another network when connectivity to a network
78 * is lost</li>
79 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
80 * state of the available networks</li>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070081 * <li>Provide an API that allows applications to request and select networks for their data
82 * traffic</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080083 * </ol>
84 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060085@SystemService(Context.CONNECTIVITY_SERVICE)
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070086public class ConnectivityManager {
87 private static final String TAG = "ConnectivityManager";
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +090088 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070089
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070091 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 * been established or lost. The NetworkInfo for the affected network is
93 * sent as an extra; it should be consulted to see what kind of
94 * connectivity event occurred.
95 * <p/>
Mark Lu33ec1062016-12-05 10:57:55 -080096 * Apps targeting Android 7.0 (API level 24) and higher do not receive this
97 * broadcast if they declare the broadcast receiver in their manifest. Apps
98 * will still receive broadcasts if they register their
99 * {@link android.content.BroadcastReceiver} with
100 * {@link android.content.Context#registerReceiver Context.registerReceiver()}
101 * and that context is still valid.
102 * <p/>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800103 * If this is a connection that was the result of failing over from a
104 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
105 * set to true.
106 * <p/>
107 * For a loss of connectivity, if the connectivity manager is attempting
108 * to connect (or has already connected) to another network, the
109 * NetworkInfo for the new network is also passed as an extra. This lets
110 * any receivers of the broadcast know that they should not necessarily
111 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800112 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 * the failover attempt succeeded (and so there is still overall data
114 * connectivity), or that the failover attempt failed, meaning that all
115 * connectivity has been lost.
116 * <p/>
117 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
118 * is set to {@code true} if there are no connected networks at all.
Chalard Jean054cd162018-02-10 05:33:50 +0900119 *
120 * @deprecated apps should use the more versatile {@link #requestNetwork},
121 * {@link #registerNetworkCallback} or {@link #registerDefaultNetworkCallback}
122 * functions instead for faster and more detailed updates about the network
123 * changes they care about.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800125 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jean054cd162018-02-10 05:33:50 +0900126 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700128
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 /**
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700130 * A temporary hack until SUPL system can get off the legacy APIS.
131 * They do too many network requests and the long list of apps listening
Chalard Jean4d660112018-06-04 16:52:49 +0900132 * and waking due to the CONNECTIVITY_ACTION broadcast makes it expensive.
133 * Use this broadcast intent instead for SUPL requests.
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700134 * @hide
135 */
136 public static final String CONNECTIVITY_ACTION_SUPL =
137 "android.net.conn.CONNECTIVITY_CHANGE_SUPL";
138
139 /**
Paul Jensen25a217c2015-02-27 22:55:47 -0500140 * The device has connected to a network that has presented a captive
141 * portal, which is blocking Internet connectivity. The user was presented
142 * with a notification that network sign in is required,
143 * and the user invoked the notification's action indicating they
Paul Jensen49e3edf2015-05-22 10:50:39 -0400144 * desire to sign in to the network. Apps handling this activity should
Paul Jensen25a217c2015-02-27 22:55:47 -0500145 * facilitate signing in to the network. This action includes a
146 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents
147 * the network presenting the captive portal; all communication with the
148 * captive portal must be done using this {@code Network} object.
149 * <p/>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400150 * This activity includes a {@link CaptivePortal} extra named
151 * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different
152 * outcomes of the captive portal sign in to the system:
153 * <ul>
154 * <li> When the app handling this action believes the user has signed in to
155 * the network and the captive portal has been dismissed, the app should
156 * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can
157 * reevaluate the network. If reevaluation finds the network no longer
158 * subject to a captive portal, the network may become the default active
Chalard Jean4d660112018-06-04 16:52:49 +0900159 * data network.</li>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400160 * <li> When the app handling this action believes the user explicitly wants
Paul Jensen25a217c2015-02-27 22:55:47 -0500161 * to ignore the captive portal and the network, the app should call
Paul Jensen49e3edf2015-05-22 10:50:39 -0400162 * {@link CaptivePortal#ignoreNetwork}. </li>
163 * </ul>
Paul Jensen25a217c2015-02-27 22:55:47 -0500164 */
165 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
166 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL";
167
168 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 * The lookup key for a {@link NetworkInfo} object. Retrieve with
170 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700171 *
172 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
173 * should always obtain network information through
Paul Jensen3541e9f2015-03-18 12:23:02 -0400174 * {@link #getActiveNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700175 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700177 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800180 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700181 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700182 *
183 * @see android.content.Intent#getIntExtra(String, int)
184 */
185 public static final String EXTRA_NETWORK_TYPE = "networkType";
186
187 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800188 * The lookup key for a boolean that indicates whether a connect event
189 * is for a network to which the connectivity manager was failing over
190 * following a disconnect on another network.
191 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
junyulai3822c8a2018-12-13 12:47:51 +0800192 *
193 * @deprecated See {@link NetworkInfo}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 */
junyulai3822c8a2018-12-13 12:47:51 +0800195 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800196 public static final String EXTRA_IS_FAILOVER = "isFailover";
197 /**
198 * The lookup key for a {@link NetworkInfo} object. This is supplied when
199 * there is another network that it may be possible to connect to. Retrieve with
200 * {@link android.content.Intent#getParcelableExtra(String)}.
junyulai3822c8a2018-12-13 12:47:51 +0800201 *
202 * @deprecated See {@link NetworkInfo}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 */
junyulai3822c8a2018-12-13 12:47:51 +0800204 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
206 /**
207 * The lookup key for a boolean that indicates whether there is a
208 * complete lack of connectivity, i.e., no network is available.
209 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
210 */
211 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
212 /**
213 * The lookup key for a string that indicates why an attempt to connect
214 * to a network failed. The string has no particular structure. It is
215 * intended to be used in notifications presented to users. Retrieve
216 * it with {@link android.content.Intent#getStringExtra(String)}.
217 */
218 public static final String EXTRA_REASON = "reason";
219 /**
220 * The lookup key for a string that provides optionally supplied
221 * extra information about the network state. The information
222 * may be passed up from the lower networking layers, and its
223 * meaning may be specific to a particular network type. Retrieve
224 * it with {@link android.content.Intent#getStringExtra(String)}.
junyulai3822c8a2018-12-13 12:47:51 +0800225 *
226 * @deprecated See {@link NetworkInfo#getExtraInfo()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 */
junyulai3822c8a2018-12-13 12:47:51 +0800228 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800229 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700230 /**
231 * The lookup key for an int that provides information about
232 * our connection to the internet at large. 0 indicates no connection,
233 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700234 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700235 * {@hide}
236 */
237 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800238 /**
Paul Jensen49e3edf2015-05-22 10:50:39 -0400239 * The lookup key for a {@link CaptivePortal} object included with the
240 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal}
241 * object can be used to either indicate to the system that the captive
242 * portal has been dismissed or that the user does not want to pursue
243 * signing in to captive portal. Retrieve it with
244 * {@link android.content.Intent#getParcelableExtra(String)}.
Paul Jensen25a217c2015-02-27 22:55:47 -0500245 */
Paul Jensen49e3edf2015-05-22 10:50:39 -0400246 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL";
Jan Nordqvist52eb29f2015-09-22 15:54:32 -0700247
248 /**
249 * Key for passing a URL to the captive portal login activity.
250 */
251 public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL";
252
Paul Jensen25a217c2015-02-27 22:55:47 -0500253 /**
Remi NGUYEN VAN8255c2d2018-05-22 10:01:53 +0900254 * Key for passing a {@link android.net.captiveportal.CaptivePortalProbeSpec} to the captive
255 * portal login activity.
256 * {@hide}
257 */
258 public static final String EXTRA_CAPTIVE_PORTAL_PROBE_SPEC =
259 "android.net.extra.CAPTIVE_PORTAL_PROBE_SPEC";
260
261 /**
Hugo Benichicdf3ba42016-12-14 08:23:40 +0900262 * Key for passing a user agent string to the captive portal login activity.
263 * {@hide}
264 */
265 public static final String EXTRA_CAPTIVE_PORTAL_USER_AGENT =
266 "android.net.extra.CAPTIVE_PORTAL_USER_AGENT";
267
268 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700269 * Broadcast action to indicate the change of data activity status
270 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800271 * The network becomes active when data transmission is started, or
272 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700273 * {@hide}
274 */
275 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jean4d660112018-06-04 16:52:49 +0900276 public static final String ACTION_DATA_ACTIVITY_CHANGE =
277 "android.net.conn.DATA_ACTIVITY_CHANGE";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700278 /**
279 * The lookup key for an enum that indicates the network device type on which this data activity
280 * change happens.
281 * {@hide}
282 */
283 public static final String EXTRA_DEVICE_TYPE = "deviceType";
284 /**
285 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
286 * it is actively sending or receiving data and {@code false} means it is idle.
287 * {@hide}
288 */
289 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700290 /**
291 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
292 * {@hide}
293 */
294 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700295
296 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800297 * Broadcast Action: The setting for background data usage has changed
298 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
299 * <p>
300 * If an application uses the network in the background, it should listen
301 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700302 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800303 * <p>
304 *
305 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
306 * of background data depends on several combined factors, and
307 * this broadcast is no longer sent. Instead, when background
308 * data is unavailable, {@link #getActiveNetworkInfo()} will now
309 * appear disconnected. During first boot after a platform
310 * upgrade, this broadcast will be sent once if
311 * {@link #getBackgroundDataSetting()} was {@code false} before
312 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800313 */
314 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800315 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800316 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
317 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
318
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700319 /**
320 * Broadcast Action: The network connection may not be good
321 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
322 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
323 * the network and it's condition.
324 * @hide
325 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800326 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100327 @UnsupportedAppUsage
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700328 public static final String INET_CONDITION_ACTION =
329 "android.net.conn.INET_CONDITION_ACTION";
330
Robert Greenwalt42acef32009-08-12 16:08:25 -0700331 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800332 * Broadcast Action: A tetherable connection has come or gone.
333 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
Erik Kline8351faa2017-04-17 16:47:23 +0900334 * {@code ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY},
335 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER}, and
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800336 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
337 * the current state of tethering. Each include a list of
338 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800339 * @hide
340 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800341 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100342 @UnsupportedAppUsage
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800343 public static final String ACTION_TETHER_STATE_CHANGED =
344 "android.net.conn.TETHER_STATE_CHANGED";
345
346 /**
347 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800348 * gives a String[] listing all the interfaces configured for
349 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800350 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100351 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800352 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800353
354 /**
355 * @hide
Erik Kline8351faa2017-04-17 16:47:23 +0900356 * gives a String[] listing all the interfaces currently in local-only
357 * mode (ie, has DHCPv4+IPv6-ULA support and no packet forwarding)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800358 */
Erik Kline8351faa2017-04-17 16:47:23 +0900359 public static final String EXTRA_ACTIVE_LOCAL_ONLY = "localOnlyArray";
360
361 /**
362 * @hide
363 * gives a String[] listing all the interfaces currently tethered
364 * (ie, has DHCPv4 support and packets potentially forwarded/NATed)
365 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100366 @UnsupportedAppUsage
Erik Kline8351faa2017-04-17 16:47:23 +0900367 public static final String EXTRA_ACTIVE_TETHER = "tetherArray";
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800368
369 /**
370 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800371 * gives a String[] listing all the interfaces we tried to tether and
372 * failed. Use {@link #getLastTetherError} to find the error code
373 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800374 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100375 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800376 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800377
378 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800379 * Broadcast Action: The captive portal tracker has finished its test.
380 * Sent only while running Setup Wizard, in lieu of showing a user
381 * notification.
382 * @hide
383 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800384 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800385 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
386 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
387 /**
388 * The lookup key for a boolean that indicates whether a captive portal was detected.
389 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
390 * @hide
391 */
392 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
393
394 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900395 * Action used to display a dialog that asks the user whether to connect to a network that is
396 * not validated. This intent is used to start the dialog in settings via startActivity.
397 *
398 * @hide
399 */
400 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED";
401
402 /**
Lorenzo Colitti9be58c52016-09-15 14:02:29 +0900403 * Action used to display a dialog that asks the user whether to avoid a network that is no
404 * longer validated. This intent is used to start the dialog in settings via startActivity.
405 *
406 * @hide
407 */
408 public static final String ACTION_PROMPT_LOST_VALIDATION =
409 "android.net.conn.PROMPT_LOST_VALIDATION";
410
411 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800412 * Invalid tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900413 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800414 * @hide
415 */
416 public static final int TETHERING_INVALID = -1;
417
418 /**
419 * Wifi tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900420 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800421 * @hide
422 */
423 @SystemApi
424 public static final int TETHERING_WIFI = 0;
425
426 /**
427 * USB tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900428 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800429 * @hide
430 */
431 @SystemApi
432 public static final int TETHERING_USB = 1;
433
434 /**
435 * Bluetooth tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900436 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800437 * @hide
438 */
439 @SystemApi
440 public static final int TETHERING_BLUETOOTH = 2;
441
442 /**
443 * Extra used for communicating with the TetherService. Includes the type of tethering to
444 * enable if any.
445 * @hide
446 */
447 public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType";
448
449 /**
450 * Extra used for communicating with the TetherService. Includes the type of tethering for
451 * which to cancel provisioning.
452 * @hide
453 */
454 public static final String EXTRA_REM_TETHER_TYPE = "extraRemTetherType";
455
456 /**
457 * Extra used for communicating with the TetherService. True to schedule a recheck of tether
458 * provisioning.
459 * @hide
460 */
461 public static final String EXTRA_SET_ALARM = "extraSetAlarm";
462
463 /**
464 * Tells the TetherService to run a provision check now.
465 * @hide
466 */
467 public static final String EXTRA_RUN_PROVISION = "extraRunProvision";
468
469 /**
470 * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver}
471 * which will receive provisioning results. Can be left empty.
472 * @hide
473 */
474 public static final String EXTRA_PROVISION_CALLBACK = "extraProvisionCallback";
475
476 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800477 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700478 * @hide
479 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100480 @UnsupportedAppUsage
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700481 public static final int TYPE_NONE = -1;
482
483 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900484 * A Mobile data connection. Devices may support more than one.
485 *
486 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
487 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
488 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700489 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900490 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700491 public static final int TYPE_MOBILE = 0;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900492
Robert Greenwalt42acef32009-08-12 16:08:25 -0700493 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900494 * A WIFI data connection. Devices may support more than one.
495 *
496 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
497 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
498 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700499 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900500 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700501 public static final int TYPE_WIFI = 1;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900502
Robert Greenwalt42acef32009-08-12 16:08:25 -0700503 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800504 * An MMS-specific Mobile data connection. This network type may use the
505 * same network interface as {@link #TYPE_MOBILE} or it may use a different
506 * one. This is used by applications needing to talk to the carrier's
507 * Multimedia Messaging Service servers.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900508 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900509 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900510 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900511 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700512 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700513 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700514 public static final int TYPE_MOBILE_MMS = 2;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900515
Robert Greenwalt42acef32009-08-12 16:08:25 -0700516 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800517 * A SUPL-specific Mobile data connection. This network type may use the
518 * same network interface as {@link #TYPE_MOBILE} or it may use a different
519 * one. This is used by applications needing to talk to the carrier's
520 * Secure User Plane Location servers for help locating the device.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900521 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900522 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900523 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900524 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700525 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700526 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700527 public static final int TYPE_MOBILE_SUPL = 3;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900528
Robert Greenwalt42acef32009-08-12 16:08:25 -0700529 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800530 * A DUN-specific Mobile data connection. This network type may use the
531 * same network interface as {@link #TYPE_MOBILE} or it may use a different
532 * one. This is sometimes by the system when setting up an upstream connection
533 * for tethering so that the carrier is aware of DUN traffic.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900534 *
535 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
536 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
537 * provides the {@link NetworkCapabilities#NET_CAPABILITY_DUN} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700538 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900539 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700540 public static final int TYPE_MOBILE_DUN = 4;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900541
Robert Greenwalt42acef32009-08-12 16:08:25 -0700542 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800543 * A High Priority Mobile data connection. This network type uses the
544 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colittie285b432015-04-23 15:32:42 +0900545 * is different.
546 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900547 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
548 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
549 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700550 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700551 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700552 public static final int TYPE_MOBILE_HIPRI = 5;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900553
jsh8214deb2010-03-11 15:04:43 -0800554 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900555 * A WiMAX data connection.
556 *
557 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
558 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
559 * appropriate network. {@see NetworkCapabilities} for supported transports.
jsh8214deb2010-03-11 15:04:43 -0800560 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900561 @Deprecated
jsh8214deb2010-03-11 15:04:43 -0800562 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800563
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800564 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900565 * A Bluetooth data connection.
566 *
567 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
568 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
569 * appropriate network. {@see NetworkCapabilities} for supported transports.
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800570 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900571 @Deprecated
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800572 public static final int TYPE_BLUETOOTH = 7;
573
Robert Greenwalt60810842011-04-22 15:28:18 -0700574 /**
575 * Dummy data connection. This should not be used on shipping devices.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900576 * @deprecated This is not used any more.
Robert Greenwalt60810842011-04-22 15:28:18 -0700577 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900578 @Deprecated
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800579 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800580
Robert Greenwalt60810842011-04-22 15:28:18 -0700581 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900582 * An Ethernet data connection.
583 *
584 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
585 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
586 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt60810842011-04-22 15:28:18 -0700587 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900588 @Deprecated
Robert Greenwalte12aec92011-01-28 14:48:37 -0800589 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700590
Wink Saville9d7d6282011-03-12 14:52:01 -0800591 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800592 * Over the air Administration.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900593 * @deprecated Use {@link NetworkCapabilities} instead.
Wink Saville9d7d6282011-03-12 14:52:01 -0800594 * {@hide}
595 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900596 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100597 @UnsupportedAppUsage
Wink Saville9d7d6282011-03-12 14:52:01 -0800598 public static final int TYPE_MOBILE_FOTA = 10;
599
600 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800601 * IP Multimedia Subsystem.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900602 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IMS} instead.
Wink Saville9d7d6282011-03-12 14:52:01 -0800603 * {@hide}
604 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900605 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100606 @UnsupportedAppUsage
Wink Saville9d7d6282011-03-12 14:52:01 -0800607 public static final int TYPE_MOBILE_IMS = 11;
608
609 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800610 * Carrier Branded Services.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900611 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_CBS} instead.
Wink Saville9d7d6282011-03-12 14:52:01 -0800612 * {@hide}
613 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900614 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100615 @UnsupportedAppUsage
Wink Saville9d7d6282011-03-12 14:52:01 -0800616 public static final int TYPE_MOBILE_CBS = 12;
617
repo syncaea743a2011-07-29 23:55:49 -0700618 /**
619 * A Wi-Fi p2p connection. Only requesting processes will have access to
620 * the peers connected.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900621 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_WIFI_P2P} instead.
repo syncaea743a2011-07-29 23:55:49 -0700622 * {@hide}
623 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900624 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100625 @UnsupportedAppUsage
repo syncaea743a2011-07-29 23:55:49 -0700626 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800627
Wink Saville5e56bc52013-07-29 15:00:57 -0700628 /**
629 * The network to use for initially attaching to the network
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900630 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IA} instead.
Wink Saville5e56bc52013-07-29 15:00:57 -0700631 * {@hide}
632 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900633 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100634 @UnsupportedAppUsage
Wink Saville5e56bc52013-07-29 15:00:57 -0700635 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700636
Lorenzo Colittie285b432015-04-23 15:32:42 +0900637 /**
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700638 * Emergency PDN connection for emergency services. This
639 * may include IMS and MMS in emergency situations.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900640 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_EIMS} instead.
Ram3e0e3bc2014-06-26 11:03:44 -0700641 * {@hide}
642 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900643 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100644 @UnsupportedAppUsage
Ram3e0e3bc2014-06-26 11:03:44 -0700645 public static final int TYPE_MOBILE_EMERGENCY = 15;
646
Hui Lu1c5624a2014-01-15 11:05:36 -0500647 /**
648 * The network that uses proxy to achieve connectivity.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900649 * @deprecated Use {@link NetworkCapabilities} instead.
Hui Lu1c5624a2014-01-15 11:05:36 -0500650 * {@hide}
651 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900652 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100653 @UnsupportedAppUsage
Hui Lu1c5624a2014-01-15 11:05:36 -0500654 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700655
Robert Greenwalt8283f882014-07-07 17:09:01 -0700656 /**
657 * A virtual network using one or more native bearers.
658 * It may or may not be providing security services.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900659 * @deprecated Applications should use {@link NetworkCapabilities#TRANSPORT_VPN} instead.
Robert Greenwalt8283f882014-07-07 17:09:01 -0700660 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900661 @Deprecated
Robert Greenwalt8283f882014-07-07 17:09:01 -0700662 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500663
664 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700665 public static final int MAX_RADIO_TYPE = TYPE_VPN;
666
667 /** {@hide} */
668 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800669
Hugo Benichi16f0a942017-06-20 14:07:59 +0900670 private static final int MIN_NETWORK_TYPE = TYPE_MOBILE;
671
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800672 /**
673 * If you want to set the default network preference,you can directly
674 * change the networkAttributes array in framework's config.xml.
675 *
676 * @deprecated Since we support so many more networks now, the single
677 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800678 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800679 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800680 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800681 * from an App.
682 */
683 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800684 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
685
Jeff Sharkey625239a2012-09-26 22:03:49 -0700686 /**
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700687 * @hide
688 */
Hugo Benichia5c1f7f2017-06-20 14:10:14 +0900689 public static final int REQUEST_ID_UNSET = 0;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700690
Paul Jensen5d59e782014-07-11 12:28:19 -0400691 /**
Hugo Benichi31c176d2017-06-17 13:14:12 +0900692 * Static unique request used as a tombstone for NetworkCallbacks that have been unregistered.
693 * This allows to distinguish when unregistering NetworkCallbacks those that were never
Chalard Jean4d660112018-06-04 16:52:49 +0900694 * registered from those that were already unregistered.
Hugo Benichi31c176d2017-06-17 13:14:12 +0900695 * @hide
696 */
Hugo Benichia5c1f7f2017-06-20 14:10:14 +0900697 private static final NetworkRequest ALREADY_UNREGISTERED =
Hugo Benichi31c176d2017-06-17 13:14:12 +0900698 new NetworkRequest.Builder().clearCapabilities().build();
699
700 /**
Paul Jensen5d59e782014-07-11 12:28:19 -0400701 * A NetID indicating no Network is selected.
702 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
703 * @hide
704 */
705 public static final int NETID_UNSET = 0;
706
Erik Kline4d092232017-10-30 15:29:44 +0900707 /**
708 * Private DNS Mode values.
709 *
710 * The "private_dns_mode" global setting stores a String value which is
711 * expected to be one of the following.
712 */
713
714 /**
715 * @hide
716 */
717 public static final String PRIVATE_DNS_MODE_OFF = "off";
718 /**
719 * @hide
720 */
721 public static final String PRIVATE_DNS_MODE_OPPORTUNISTIC = "opportunistic";
722 /**
723 * @hide
724 */
725 public static final String PRIVATE_DNS_MODE_PROVIDER_HOSTNAME = "hostname";
726 /**
727 * The default Private DNS mode.
728 *
729 * This may change from release to release or may become dependent upon
730 * the capabilities of the underlying platform.
731 *
732 * @hide
733 */
Erik Kline19841792018-05-16 16:41:57 +0900734 public static final String PRIVATE_DNS_DEFAULT_MODE_FALLBACK = PRIVATE_DNS_MODE_OPPORTUNISTIC;
Erik Kline4d092232017-10-30 15:29:44 +0900735
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100736 @UnsupportedAppUsage
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700737 private final IConnectivityManager mService;
Paul Jensene0bef712014-12-10 15:12:18 -0500738 /**
739 * A kludge to facilitate static access where a Context pointer isn't available, like in the
740 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
741 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
742 * methods that take a Context argument.
743 */
744 private static ConnectivityManager sInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900746 private final Context mContext;
747
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800748 private INetworkManagementService mNMService;
Felipe Leme1b103232016-01-22 09:44:57 -0800749 private INetworkPolicyManager mNPManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800750
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800751 /**
752 * Tests if a given integer represents a valid network type.
753 * @param networkType the type to be tested
754 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensen9e59e122015-05-06 10:42:25 -0400755 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
756 * validate a network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800757 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700758 @Deprecated
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700759 public static boolean isNetworkTypeValid(int networkType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900760 return MIN_NETWORK_TYPE <= networkType && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800761 }
762
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800763 /**
764 * Returns a non-localized string representing a given network type.
765 * ONLY used for debugging output.
766 * @param type the type needing naming
767 * @return a String for the given type, or a string version of the type ("87")
768 * if no name is known.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900769 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800770 * {@hide}
771 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900772 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100773 @UnsupportedAppUsage
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700774 public static String getNetworkTypeName(int type) {
775 switch (type) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900776 case TYPE_NONE:
777 return "NONE";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700778 case TYPE_MOBILE:
779 return "MOBILE";
780 case TYPE_WIFI:
781 return "WIFI";
782 case TYPE_MOBILE_MMS:
783 return "MOBILE_MMS";
784 case TYPE_MOBILE_SUPL:
785 return "MOBILE_SUPL";
786 case TYPE_MOBILE_DUN:
787 return "MOBILE_DUN";
788 case TYPE_MOBILE_HIPRI:
789 return "MOBILE_HIPRI";
790 case TYPE_WIMAX:
791 return "WIMAX";
792 case TYPE_BLUETOOTH:
793 return "BLUETOOTH";
794 case TYPE_DUMMY:
795 return "DUMMY";
796 case TYPE_ETHERNET:
797 return "ETHERNET";
798 case TYPE_MOBILE_FOTA:
799 return "MOBILE_FOTA";
800 case TYPE_MOBILE_IMS:
801 return "MOBILE_IMS";
802 case TYPE_MOBILE_CBS:
803 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700804 case TYPE_WIFI_P2P:
805 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700806 case TYPE_MOBILE_IA:
807 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700808 case TYPE_MOBILE_EMERGENCY:
809 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500810 case TYPE_PROXY:
811 return "PROXY";
Erik Kline37fbfa12014-11-19 17:23:41 +0900812 case TYPE_VPN:
813 return "VPN";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700814 default:
815 return Integer.toString(type);
816 }
817 }
818
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800819 /**
820 * Checks if a given type uses the cellular data connection.
821 * This should be replaced in the future by a network property.
822 * @param networkType the type to check
823 * @return a boolean - {@code true} if uses cellular network, else {@code false}
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900824 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800825 * {@hide}
826 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900827 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100828 @UnsupportedAppUsage
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700829 public static boolean isNetworkTypeMobile(int networkType) {
830 switch (networkType) {
831 case TYPE_MOBILE:
832 case TYPE_MOBILE_MMS:
833 case TYPE_MOBILE_SUPL:
834 case TYPE_MOBILE_DUN:
835 case TYPE_MOBILE_HIPRI:
836 case TYPE_MOBILE_FOTA:
837 case TYPE_MOBILE_IMS:
838 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700839 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700840 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700841 return true;
842 default:
843 return false;
844 }
845 }
846
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800847 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700848 * Checks if the given network type is backed by a Wi-Fi radio.
849 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900850 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700851 * @hide
852 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900853 @Deprecated
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700854 public static boolean isNetworkTypeWifi(int networkType) {
855 switch (networkType) {
856 case TYPE_WIFI:
857 case TYPE_WIFI_P2P:
858 return true;
859 default:
860 return false;
861 }
862 }
863
864 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800865 * Specifies the preferred network type. When the device has more
866 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800867 *
868 * @param preference the network type to prefer over all others. It is
869 * unspecified what happens to the old preferred network in the
870 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700871 * @deprecated Functionality has been removed as it no longer makes sense,
872 * with many more than two networks - we'd need an array to express
873 * preference. Instead we use dynamic network properties of
874 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800875 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700876 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 }
879
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800880 /**
881 * Retrieves the current preferred network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800882 *
883 * @return an integer representing the preferred network type
884 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700885 * @deprecated Functionality has been removed as it no longer makes sense,
886 * with many more than two networks - we'd need an array to express
887 * preference. Instead we use dynamic network properties of
888 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800889 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700890 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600891 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700893 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800894 }
895
Scott Main671644c2011-10-06 19:02:28 -0700896 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800897 * Returns details about the currently active default data network. When
898 * connected, this network is the default route for outgoing connections.
899 * You should always check {@link NetworkInfo#isConnected()} before initiating
900 * network traffic. This may return {@code null} when there is no default
901 * network.
Chalard Jean5a041d12018-03-29 17:45:24 +0900902 * Note that if the default network is a VPN, this method will return the
903 * NetworkInfo for one of its underlying networks instead, or null if the
904 * VPN agent did not specify any. Apps interested in learning about VPNs
905 * should use {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800906 *
907 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500908 * or {@code null} if no default network is currently active
junyulai3822c8a2018-12-13 12:47:51 +0800909 * @deprecated See {@link NetworkInfo}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700910 */
junyulai3822c8a2018-12-13 12:47:51 +0800911 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600912 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +0900913 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800914 public NetworkInfo getActiveNetworkInfo() {
915 try {
916 return mService.getActiveNetworkInfo();
917 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700918 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800919 }
920 }
921
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800922 /**
Paul Jensen31a94f42015-02-13 14:18:39 -0500923 * Returns a {@link Network} object corresponding to the currently active
924 * default data network. In the event that the current active default data
925 * network disconnects, the returned {@code Network} object will no longer
926 * be usable. This will return {@code null} when there is no default
927 * network.
928 *
929 * @return a {@link Network} object for the current default network or
930 * {@code null} if no default network is currently active
Paul Jensen31a94f42015-02-13 14:18:39 -0500931 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600932 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +0900933 @Nullable
Paul Jensen31a94f42015-02-13 14:18:39 -0500934 public Network getActiveNetwork() {
935 try {
936 return mService.getActiveNetwork();
937 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700938 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -0500939 }
940 }
941
942 /**
Robin Leed2baf792016-03-24 12:07:00 +0000943 * Returns a {@link Network} object corresponding to the currently active
944 * default data network for a specific UID. In the event that the default data
945 * network disconnects, the returned {@code Network} object will no longer
946 * be usable. This will return {@code null} when there is no default
947 * network for the UID.
Robin Leed2baf792016-03-24 12:07:00 +0000948 *
949 * @return a {@link Network} object for the current default network for the
950 * given UID or {@code null} if no default network is currently active
951 *
952 * @hide
953 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600954 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Chalard Jean50bea3d2019-01-07 19:26:34 +0900955 @Nullable
Robin Leed2baf792016-03-24 12:07:00 +0000956 public Network getActiveNetworkForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600957 return getActiveNetworkForUid(uid, false);
958 }
959
960 /** {@hide} */
961 public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) {
Robin Leed2baf792016-03-24 12:07:00 +0000962 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600963 return mService.getActiveNetworkForUid(uid, ignoreBlocked);
Robin Leed2baf792016-03-24 12:07:00 +0000964 } catch (RemoteException e) {
965 throw e.rethrowFromSystemServer();
966 }
967 }
968
969 /**
Charles He36738632017-05-15 17:07:18 +0100970 * Checks if a VPN app supports always-on mode.
971 *
972 * In order to support the always-on feature, an app has to
973 * <ul>
974 * <li>target {@link VERSION_CODES#N API 24} or above, and
Charles Hec57a01c2017-08-15 15:30:22 +0100975 * <li>not opt out through the {@link VpnService#SERVICE_META_DATA_SUPPORTS_ALWAYS_ON}
976 * meta-data field.
Charles He36738632017-05-15 17:07:18 +0100977 * </ul>
978 *
979 * @param userId The identifier of the user for whom the VPN app is installed.
980 * @param vpnPackage The canonical package name of the VPN app.
981 * @return {@code true} if and only if the VPN app exists and supports always-on mode.
982 * @hide
983 */
984 public boolean isAlwaysOnVpnPackageSupportedForUser(int userId, @Nullable String vpnPackage) {
985 try {
986 return mService.isAlwaysOnVpnPackageSupported(userId, vpnPackage);
987 } catch (RemoteException e) {
988 throw e.rethrowFromSystemServer();
989 }
990 }
991
992 /**
Robin Lee244ce8e2016-01-05 18:03:46 +0000993 * Configures an always-on VPN connection through a specific application.
994 * This connection is automatically granted and persisted after a reboot.
995 *
996 * <p>The designated package should declare a {@link VpnService} in its
997 * manifest guarded by {@link android.Manifest.permission.BIND_VPN_SERVICE},
998 * otherwise the call will fail.
999 *
1000 * @param userId The identifier of the user to set an always-on VPN for.
1001 * @param vpnPackage The package name for an installed VPN app on the device, or {@code null}
1002 * to remove an existing always-on VPN configuration.
Robin Leedc679712016-05-03 13:23:03 +01001003 * @param lockdownEnabled {@code true} to disallow networking when the VPN is not connected or
1004 * {@code false} otherwise.
Robin Lee244ce8e2016-01-05 18:03:46 +00001005 * @return {@code true} if the package is set as always-on VPN controller;
1006 * {@code false} otherwise.
1007 * @hide
1008 */
Robin Leedc679712016-05-03 13:23:03 +01001009 public boolean setAlwaysOnVpnPackageForUser(int userId, @Nullable String vpnPackage,
1010 boolean lockdownEnabled) {
Robin Lee244ce8e2016-01-05 18:03:46 +00001011 try {
Robin Leedc679712016-05-03 13:23:03 +01001012 return mService.setAlwaysOnVpnPackage(userId, vpnPackage, lockdownEnabled);
Robin Lee244ce8e2016-01-05 18:03:46 +00001013 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001014 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +00001015 }
1016 }
1017
1018 /**
1019 * Returns the package name of the currently set always-on VPN application.
1020 * If there is no always-on VPN set, or the VPN is provided by the system instead
1021 * of by an app, {@code null} will be returned.
1022 *
1023 * @return Package name of VPN controller responsible for always-on VPN,
1024 * or {@code null} if none is set.
1025 * @hide
1026 */
1027 public String getAlwaysOnVpnPackageForUser(int userId) {
1028 try {
1029 return mService.getAlwaysOnVpnPackage(userId);
1030 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001031 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +00001032 }
1033 }
1034
1035 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001036 * Returns details about the currently active default data network
1037 * for a given uid. This is for internal use only to avoid spying
1038 * other apps.
1039 *
1040 * @return a {@link NetworkInfo} object for the current default network
1041 * for the given uid or {@code null} if no default network is
1042 * available for the specified uid.
1043 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001044 * {@hide}
1045 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001046 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001047 @UnsupportedAppUsage
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001048 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001049 return getActiveNetworkInfoForUid(uid, false);
1050 }
1051
1052 /** {@hide} */
1053 public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001054 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001055 return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001056 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001057 throw e.rethrowFromSystemServer();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001058 }
1059 }
1060
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001061 /**
1062 * Returns connection status information about a particular
1063 * network type.
1064 *
1065 * @param networkType integer specifying which networkType in
1066 * which you're interested.
1067 * @return a {@link NetworkInfo} object for the requested
1068 * network type or {@code null} if the type is not
Chalard Jean5a041d12018-03-29 17:45:24 +09001069 * supported by the device. If {@code networkType} is
1070 * TYPE_VPN and a VPN is active for the calling app,
1071 * then this method will try to return one of the
1072 * underlying networks for the VPN or null if the
1073 * VPN agent didn't specify any.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001074 *
Paul Jensen3541e9f2015-03-18 12:23:02 -04001075 * @deprecated This method does not support multiple connected networks
1076 * of the same type. Use {@link #getAllNetworks} and
1077 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001078 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001079 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001080 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09001081 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 public NetworkInfo getNetworkInfo(int networkType) {
1083 try {
1084 return mService.getNetworkInfo(networkType);
1085 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001086 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 }
1088 }
1089
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001090 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001091 * Returns connection status information about a particular
1092 * Network.
1093 *
1094 * @param network {@link Network} specifying which network
1095 * in which you're interested.
1096 * @return a {@link NetworkInfo} object for the requested
1097 * network or {@code null} if the {@code Network}
1098 * is not valid.
junyulai3822c8a2018-12-13 12:47:51 +08001099 * @deprecated See {@link NetworkInfo}.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001100 */
junyulai3822c8a2018-12-13 12:47:51 +08001101 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001102 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09001103 @Nullable
1104 public NetworkInfo getNetworkInfo(@Nullable Network network) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001105 return getNetworkInfoForUid(network, Process.myUid(), false);
1106 }
1107
1108 /** {@hide} */
1109 public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001110 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001111 return mService.getNetworkInfoForUid(network, uid, ignoreBlocked);
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001112 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001113 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001114 }
1115 }
1116
1117 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001118 * Returns connection status information about all network
1119 * types supported by the device.
1120 *
1121 * @return an array of {@link NetworkInfo} objects. Check each
1122 * {@link NetworkInfo#getType} for which type each applies.
1123 *
Paul Jensen3541e9f2015-03-18 12:23:02 -04001124 * @deprecated This method does not support multiple connected networks
1125 * of the same type. Use {@link #getAllNetworks} and
1126 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001127 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001128 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001129 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09001130 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 public NetworkInfo[] getAllNetworkInfo() {
1132 try {
1133 return mService.getAllNetworkInfo();
1134 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001135 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001136 }
1137 }
1138
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001139 /**
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001140 * Returns the {@link Network} object currently serving a given type, or
1141 * null if the given type is not connected.
1142 *
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001143 * @hide
Paul Jensen3541e9f2015-03-18 12:23:02 -04001144 * @deprecated This method does not support multiple connected networks
1145 * of the same type. Use {@link #getAllNetworks} and
1146 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001147 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001148 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001149 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001150 @UnsupportedAppUsage
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001151 public Network getNetworkForType(int networkType) {
1152 try {
1153 return mService.getNetworkForType(networkType);
1154 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001155 throw e.rethrowFromSystemServer();
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001156 }
1157 }
1158
1159 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001160 * Returns an array of all {@link Network} currently tracked by the
1161 * framework.
Paul Jensenb2748922015-05-06 11:10:18 -04001162 *
1163 * @return an array of {@link Network} objects.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001164 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001165 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09001166 @NonNull
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001167 public Network[] getAllNetworks() {
1168 try {
1169 return mService.getAllNetworks();
1170 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001171 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001172 }
1173 }
1174
1175 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001176 * Returns an array of {@link android.net.NetworkCapabilities} objects, representing
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001177 * the Networks that applications run by the given user will use by default.
1178 * @hide
1179 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001180 @UnsupportedAppUsage
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001181 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
1182 try {
1183 return mService.getDefaultNetworkCapabilitiesForUser(userId);
1184 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001185 throw e.rethrowFromSystemServer();
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001186 }
1187 }
1188
1189 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001190 * Returns the IP information for the current default network.
1191 *
1192 * @return a {@link LinkProperties} object describing the IP info
1193 * for the current default network, or {@code null} if there
1194 * is no current default network.
1195 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001196 * {@hide}
1197 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001198 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001199 @UnsupportedAppUsage
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001200 public LinkProperties getActiveLinkProperties() {
1201 try {
1202 return mService.getActiveLinkProperties();
1203 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001204 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001205 }
1206 }
1207
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001208 /**
1209 * Returns the IP information for a given network type.
1210 *
1211 * @param networkType the network type of interest.
1212 * @return a {@link LinkProperties} object describing the IP info
1213 * for the given networkType, or {@code null} if there is
1214 * no current default network.
1215 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001216 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04001217 * @deprecated This method does not support multiple connected networks
1218 * of the same type. Use {@link #getAllNetworks},
1219 * {@link #getNetworkInfo(android.net.Network)}, and
1220 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001221 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001222 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001223 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001224 @UnsupportedAppUsage
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001225 public LinkProperties getLinkProperties(int networkType) {
1226 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001227 return mService.getLinkPropertiesForType(networkType);
1228 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001229 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001230 }
1231 }
1232
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001233 /**
1234 * Get the {@link LinkProperties} for the given {@link Network}. This
1235 * will return {@code null} if the network is unknown.
1236 *
1237 * @param network The {@link Network} object identifying the network in question.
1238 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensenb2748922015-05-06 11:10:18 -04001239 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001240 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09001241 @Nullable
1242 public LinkProperties getLinkProperties(@Nullable Network network) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001243 try {
1244 return mService.getLinkProperties(network);
1245 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001246 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001247 }
1248 }
1249
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001250 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001251 * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001252 * will return {@code null} if the network is unknown.
1253 *
1254 * @param network The {@link Network} object identifying the network in question.
Lorenzo Colittie285b432015-04-23 15:32:42 +09001255 * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001256 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001257 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09001258 @Nullable
1259 public NetworkCapabilities getNetworkCapabilities(@Nullable Network network) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001260 try {
1261 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001262 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001263 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001264 }
1265 }
1266
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001267 /**
Udam Sainib7c24872016-01-04 12:16:14 -08001268 * Gets the URL that should be used for resolving whether a captive portal is present.
1269 * 1. This URL should respond with a 204 response to a GET request to indicate no captive
1270 * portal is present.
1271 * 2. This URL must be HTTP as redirect responses are used to find captive portal
1272 * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects.
1273 *
1274 * @hide
1275 */
1276 @SystemApi
Udam Saini0e94c362017-06-07 12:06:28 -07001277 @RequiresPermission(android.Manifest.permission.LOCAL_MAC_ADDRESS)
Udam Sainib7c24872016-01-04 12:16:14 -08001278 public String getCaptivePortalServerUrl() {
1279 try {
1280 return mService.getCaptivePortalServerUrl();
1281 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001282 throw e.rethrowFromSystemServer();
Udam Sainib7c24872016-01-04 12:16:14 -08001283 }
1284 }
1285
1286 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 * Tells the underlying networking system that the caller wants to
1288 * begin using the named feature. The interpretation of {@code feature}
1289 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001290 *
1291 * <p>This method requires the caller to hold either the
1292 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1293 * or the ability to modify system settings as determined by
1294 * {@link android.provider.Settings.System#canWrite}.</p>
1295 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 * @param networkType specifies which network the request pertains to
1297 * @param feature the name of the feature to be used
1298 * @return an integer value representing the outcome of the request.
1299 * The interpretation of this value is specific to each networking
1300 * implementation+feature combination, except that the value {@code -1}
1301 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001302 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001303 * @deprecated Deprecated in favor of the cleaner
1304 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001305 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001306 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001307 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001308 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001309 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001310 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001311 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001312 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1313 if (netCap == null) {
1314 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
1315 feature);
1316 return PhoneConstants.APN_REQUEST_FAILED;
1317 }
1318
1319 NetworkRequest request = null;
1320 synchronized (sLegacyRequests) {
1321 LegacyRequest l = sLegacyRequests.get(netCap);
1322 if (l != null) {
1323 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
1324 renewRequestLocked(l);
1325 if (l.currentNetwork != null) {
1326 return PhoneConstants.APN_ALREADY_ACTIVE;
1327 } else {
1328 return PhoneConstants.APN_REQUEST_STARTED;
1329 }
1330 }
1331
1332 request = requestNetworkForFeatureLocked(netCap);
1333 }
1334 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -07001335 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001336 return PhoneConstants.APN_REQUEST_STARTED;
1337 } else {
1338 Log.d(TAG, " request Failed");
1339 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 }
1341 }
1342
1343 /**
1344 * Tells the underlying networking system that the caller is finished
1345 * using the named feature. The interpretation of {@code feature}
1346 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001347 *
1348 * <p>This method requires the caller to hold either the
1349 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1350 * or the ability to modify system settings as determined by
1351 * {@link android.provider.Settings.System#canWrite}.</p>
1352 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001353 * @param networkType specifies which network the request pertains to
1354 * @param feature the name of the feature that is no longer needed
1355 * @return an integer value representing the outcome of the request.
1356 * The interpretation of this value is specific to each networking
1357 * implementation+feature combination, except that the value {@code -1}
1358 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001359 *
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09001360 * @deprecated Deprecated in favor of the cleaner
1361 * {@link #unregisterNetworkCallback(NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001362 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001363 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001364 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001365 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001366 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001367 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001368 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001369 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1370 if (netCap == null) {
1371 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
1372 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001373 return -1;
1374 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001375
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001376 if (removeRequestForFeature(netCap)) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001377 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001378 }
1379 return 1;
1380 }
1381
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001382 @UnsupportedAppUsage
Robert Greenwalt562cc542014-05-15 18:07:26 -07001383 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1384 if (networkType == TYPE_MOBILE) {
Erik Kline35bf06c2017-01-26 18:08:28 +09001385 switch (feature) {
1386 case "enableCBS":
1387 return networkCapabilitiesForType(TYPE_MOBILE_CBS);
1388 case "enableDUN":
1389 case "enableDUNAlways":
1390 return networkCapabilitiesForType(TYPE_MOBILE_DUN);
1391 case "enableFOTA":
1392 return networkCapabilitiesForType(TYPE_MOBILE_FOTA);
1393 case "enableHIPRI":
1394 return networkCapabilitiesForType(TYPE_MOBILE_HIPRI);
1395 case "enableIMS":
1396 return networkCapabilitiesForType(TYPE_MOBILE_IMS);
1397 case "enableMMS":
1398 return networkCapabilitiesForType(TYPE_MOBILE_MMS);
1399 case "enableSUPL":
1400 return networkCapabilitiesForType(TYPE_MOBILE_SUPL);
1401 default:
1402 return null;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001403 }
Erik Kline35bf06c2017-01-26 18:08:28 +09001404 } else if (networkType == TYPE_WIFI && "p2p".equals(feature)) {
1405 return networkCapabilitiesForType(TYPE_WIFI_P2P);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001406 }
1407 return null;
1408 }
1409
Robert Greenwalt06314e42014-10-29 14:04:06 -07001410 /**
1411 * Guess what the network request was trying to say so that the resulting
1412 * network is accessible via the legacy (deprecated) API such as
1413 * requestRouteToHost.
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001414 *
1415 * This means we should try to be fairly precise about transport and
Robert Greenwalt06314e42014-10-29 14:04:06 -07001416 * capability but ignore things such as networkSpecifier.
1417 * If the request has more than one transport or capability it doesn't
1418 * match the old legacy requests (they selected only single transport/capability)
1419 * so this function cannot map the request to a single legacy type and
1420 * the resulting network will not be available to the legacy APIs.
1421 *
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001422 * This code is only called from the requestNetwork API (L and above).
1423 *
1424 * Setting a legacy type causes CONNECTIVITY_ACTION broadcasts, which are expensive
1425 * because they wake up lots of apps - see http://b/23350688 . So we currently only
1426 * do this for SUPL requests, which are the only ones that we know need it. If
1427 * omitting these broadcasts causes unacceptable app breakage, then for backwards
1428 * compatibility we can send them:
1429 *
1430 * if (targetSdkVersion < Build.VERSION_CODES.M) && // legacy API unsupported >= M
1431 * targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP)) // requestNetwork not present < L
1432 *
Robert Greenwalt06314e42014-10-29 14:04:06 -07001433 * TODO - This should be removed when the legacy APIs are removed.
1434 */
Ye Wenb87875e2014-07-21 14:19:01 -07001435 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1436 if (netCap == null) {
1437 return TYPE_NONE;
1438 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001439
Ye Wenb87875e2014-07-21 14:19:01 -07001440 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1441 return TYPE_NONE;
1442 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001443
Lifu Tang30f95a72016-01-07 23:20:38 -08001444 // Do this only for SUPL, until GnssLocationProvider is fixed. http://b/25876485 .
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001445 if (!netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1446 // NOTE: if this causes app breakage, we should not just comment out this early return;
1447 // instead, we should make this early return conditional on the requesting app's target
1448 // SDK version, as described in the comment above.
1449 return TYPE_NONE;
1450 }
1451
Robert Greenwalt06314e42014-10-29 14:04:06 -07001452 String type = null;
1453 int result = TYPE_NONE;
1454
Ye Wenb87875e2014-07-21 14:19:01 -07001455 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
Robert Greenwalt06314e42014-10-29 14:04:06 -07001456 type = "enableCBS";
1457 result = TYPE_MOBILE_CBS;
1458 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1459 type = "enableIMS";
1460 result = TYPE_MOBILE_IMS;
1461 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1462 type = "enableFOTA";
1463 result = TYPE_MOBILE_FOTA;
1464 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1465 type = "enableDUN";
1466 result = TYPE_MOBILE_DUN;
1467 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001468 type = "enableSUPL";
Robert Greenwalt06314e42014-10-29 14:04:06 -07001469 result = TYPE_MOBILE_SUPL;
Robert Greenwalt74ab4fa2015-08-28 12:37:54 -07001470 // back out this hack for mms as they no longer need this and it's causing
1471 // device slowdowns - b/23350688 (note, supl still needs this)
1472 //} else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1473 // type = "enableMMS";
1474 // result = TYPE_MOBILE_MMS;
Robert Greenwalt06314e42014-10-29 14:04:06 -07001475 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1476 type = "enableHIPRI";
1477 result = TYPE_MOBILE_HIPRI;
Ye Wenb87875e2014-07-21 14:19:01 -07001478 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001479 if (type != null) {
1480 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1481 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1482 return result;
Ye Wenb87875e2014-07-21 14:19:01 -07001483 }
1484 }
1485 return TYPE_NONE;
1486 }
1487
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001488 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001489 if (netCap == null) return TYPE_NONE;
1490 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1491 return TYPE_MOBILE_CBS;
1492 }
1493 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1494 return TYPE_MOBILE_IMS;
1495 }
1496 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1497 return TYPE_MOBILE_FOTA;
1498 }
1499 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1500 return TYPE_MOBILE_DUN;
1501 }
1502 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1503 return TYPE_MOBILE_SUPL;
1504 }
1505 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1506 return TYPE_MOBILE_MMS;
1507 }
1508 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1509 return TYPE_MOBILE_HIPRI;
1510 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001511 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1512 return TYPE_WIFI_P2P;
1513 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001514 return TYPE_NONE;
1515 }
1516
1517 private static class LegacyRequest {
1518 NetworkCapabilities networkCapabilities;
1519 NetworkRequest networkRequest;
1520 int expireSequenceNumber;
1521 Network currentNetwork;
1522 int delay = -1;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001523
1524 private void clearDnsBinding() {
1525 if (currentNetwork != null) {
1526 currentNetwork = null;
1527 setProcessDefaultNetworkForHostResolution(null);
1528 }
1529 }
1530
Robert Greenwalt6078b502014-06-11 16:05:07 -07001531 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001532 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001533 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001534 currentNetwork = network;
1535 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001536 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001537 }
1538 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001539 public void onLost(Network network) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001540 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001541 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1542 }
1543 };
1544 }
1545
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001546 @UnsupportedAppUsage
Chalard Jean4d660112018-06-04 16:52:49 +09001547 private static final HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
1548 new HashMap<>();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001549
1550 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1551 synchronized (sLegacyRequests) {
1552 LegacyRequest l = sLegacyRequests.get(netCap);
1553 if (l != null) return l.networkRequest;
1554 }
1555 return null;
1556 }
1557
1558 private void renewRequestLocked(LegacyRequest l) {
1559 l.expireSequenceNumber++;
1560 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1561 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1562 }
1563
1564 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1565 int ourSeqNum = -1;
1566 synchronized (sLegacyRequests) {
1567 LegacyRequest l = sLegacyRequests.get(netCap);
1568 if (l == null) return;
1569 ourSeqNum = l.expireSequenceNumber;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001570 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001571 }
1572 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1573 }
1574
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001575 @UnsupportedAppUsage
Robert Greenwalt562cc542014-05-15 18:07:26 -07001576 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1577 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001578 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001579 try {
1580 delay = mService.getRestoreDefaultNetworkDelay(type);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001581 } catch (RemoteException e) {
1582 throw e.rethrowFromSystemServer();
1583 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001584 LegacyRequest l = new LegacyRequest();
1585 l.networkCapabilities = netCap;
1586 l.delay = delay;
1587 l.expireSequenceNumber = 0;
Hugo Benichi2583ef02017-02-02 17:02:36 +09001588 l.networkRequest = sendRequestForNetwork(
1589 netCap, l.networkCallback, 0, REQUEST, type, getDefaultHandler());
Robert Greenwalt562cc542014-05-15 18:07:26 -07001590 if (l.networkRequest == null) return null;
1591 sLegacyRequests.put(netCap, l);
1592 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1593 return l.networkRequest;
1594 }
1595
1596 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1597 if (delay >= 0) {
1598 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
Hugo Benichi2583ef02017-02-02 17:02:36 +09001599 CallbackHandler handler = getDefaultHandler();
Hugo Benichi6f260f32017-02-03 14:18:44 +09001600 Message msg = handler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1601 handler.sendMessageDelayed(msg, delay);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001602 }
1603 }
1604
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001605 @UnsupportedAppUsage
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001606 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1607 final LegacyRequest l;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001608 synchronized (sLegacyRequests) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001609 l = sLegacyRequests.remove(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001610 }
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001611 if (l == null) return false;
1612 unregisterNetworkCallback(l.networkCallback);
1613 l.clearDnsBinding();
1614 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 }
1616
Erik Kline35bf06c2017-01-26 18:08:28 +09001617 private static final SparseIntArray sLegacyTypeToTransport = new SparseIntArray();
1618 static {
1619 sLegacyTypeToTransport.put(TYPE_MOBILE, NetworkCapabilities.TRANSPORT_CELLULAR);
1620 sLegacyTypeToTransport.put(TYPE_MOBILE_CBS, NetworkCapabilities.TRANSPORT_CELLULAR);
1621 sLegacyTypeToTransport.put(TYPE_MOBILE_DUN, NetworkCapabilities.TRANSPORT_CELLULAR);
1622 sLegacyTypeToTransport.put(TYPE_MOBILE_FOTA, NetworkCapabilities.TRANSPORT_CELLULAR);
1623 sLegacyTypeToTransport.put(TYPE_MOBILE_HIPRI, NetworkCapabilities.TRANSPORT_CELLULAR);
1624 sLegacyTypeToTransport.put(TYPE_MOBILE_IMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1625 sLegacyTypeToTransport.put(TYPE_MOBILE_MMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1626 sLegacyTypeToTransport.put(TYPE_MOBILE_SUPL, NetworkCapabilities.TRANSPORT_CELLULAR);
1627 sLegacyTypeToTransport.put(TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI);
1628 sLegacyTypeToTransport.put(TYPE_WIFI_P2P, NetworkCapabilities.TRANSPORT_WIFI);
1629 sLegacyTypeToTransport.put(TYPE_BLUETOOTH, NetworkCapabilities.TRANSPORT_BLUETOOTH);
1630 sLegacyTypeToTransport.put(TYPE_ETHERNET, NetworkCapabilities.TRANSPORT_ETHERNET);
1631 }
1632
1633 private static final SparseIntArray sLegacyTypeToCapability = new SparseIntArray();
1634 static {
1635 sLegacyTypeToCapability.put(TYPE_MOBILE_CBS, NetworkCapabilities.NET_CAPABILITY_CBS);
1636 sLegacyTypeToCapability.put(TYPE_MOBILE_DUN, NetworkCapabilities.NET_CAPABILITY_DUN);
1637 sLegacyTypeToCapability.put(TYPE_MOBILE_FOTA, NetworkCapabilities.NET_CAPABILITY_FOTA);
1638 sLegacyTypeToCapability.put(TYPE_MOBILE_IMS, NetworkCapabilities.NET_CAPABILITY_IMS);
1639 sLegacyTypeToCapability.put(TYPE_MOBILE_MMS, NetworkCapabilities.NET_CAPABILITY_MMS);
1640 sLegacyTypeToCapability.put(TYPE_MOBILE_SUPL, NetworkCapabilities.NET_CAPABILITY_SUPL);
1641 sLegacyTypeToCapability.put(TYPE_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
1642 }
1643
1644 /**
1645 * Given a legacy type (TYPE_WIFI, ...) returns a NetworkCapabilities
1646 * instance suitable for registering a request or callback. Throws an
1647 * IllegalArgumentException if no mapping from the legacy type to
1648 * NetworkCapabilities is known.
1649 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +09001650 * @deprecated Types are deprecated. Use {@link NetworkCallback} or {@link NetworkRequest}
1651 * to find the network instead.
Erik Kline35bf06c2017-01-26 18:08:28 +09001652 * @hide
1653 */
1654 public static NetworkCapabilities networkCapabilitiesForType(int type) {
1655 final NetworkCapabilities nc = new NetworkCapabilities();
1656
1657 // Map from type to transports.
1658 final int NOT_FOUND = -1;
1659 final int transport = sLegacyTypeToTransport.get(type, NOT_FOUND);
Hugo Benichie7678512017-05-09 15:19:01 +09001660 Preconditions.checkArgument(transport != NOT_FOUND, "unknown legacy type: " + type);
Erik Kline35bf06c2017-01-26 18:08:28 +09001661 nc.addTransportType(transport);
1662
1663 // Map from type to capabilities.
1664 nc.addCapability(sLegacyTypeToCapability.get(
1665 type, NetworkCapabilities.NET_CAPABILITY_INTERNET));
1666 nc.maybeMarkCapabilitiesRestricted();
1667 return nc;
1668 }
1669
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001670 /** @hide */
1671 public static class PacketKeepaliveCallback {
1672 /** The requested keepalive was successfully started. */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001673 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001674 public void onStarted() {}
1675 /** The keepalive was successfully stopped. */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001676 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001677 public void onStopped() {}
1678 /** An error occurred. */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001679 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001680 public void onError(int error) {}
1681 }
1682
1683 /**
1684 * Allows applications to request that the system periodically send specific packets on their
1685 * behalf, using hardware offload to save battery power.
1686 *
1687 * To request that the system send keepalives, call one of the methods that return a
1688 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1689 * passing in a non-null callback. If the callback is successfully started, the callback's
1690 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1691 * specifying one of the {@code ERROR_*} constants in this class.
1692 *
Chalard Jean4d660112018-06-04 16:52:49 +09001693 * To stop an existing keepalive, call {@link PacketKeepalive#stop}. The system will call
1694 * {@link PacketKeepaliveCallback#onStopped} if the operation was successful or
1695 * {@link PacketKeepaliveCallback#onError} if an error occurred.
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001696 *
1697 * @hide
1698 */
1699 public class PacketKeepalive {
1700
1701 private static final String TAG = "PacketKeepalive";
1702
1703 /** @hide */
1704 public static final int SUCCESS = 0;
1705
1706 /** @hide */
1707 public static final int NO_KEEPALIVE = -1;
1708
1709 /** @hide */
1710 public static final int BINDER_DIED = -10;
1711
1712 /** The specified {@code Network} is not connected. */
1713 public static final int ERROR_INVALID_NETWORK = -20;
1714 /** The specified IP addresses are invalid. For example, the specified source IP address is
1715 * not configured on the specified {@code Network}. */
1716 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1717 /** The requested port is invalid. */
1718 public static final int ERROR_INVALID_PORT = -22;
1719 /** The packet length is invalid (e.g., too long). */
1720 public static final int ERROR_INVALID_LENGTH = -23;
1721 /** The packet transmission interval is invalid (e.g., too short). */
1722 public static final int ERROR_INVALID_INTERVAL = -24;
1723
1724 /** The hardware does not support this request. */
1725 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti9d1284e2015-09-08 16:46:36 +09001726 /** The hardware returned an error. */
1727 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001728
Nathan Harold63dd8132018-02-14 13:09:45 -08001729 /** The NAT-T destination port for IPsec */
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001730 public static final int NATT_PORT = 4500;
1731
Nathan Harold63dd8132018-02-14 13:09:45 -08001732 /** The minimum interval in seconds between keepalive packet transmissions */
1733 public static final int MIN_INTERVAL = 10;
1734
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001735 private final Network mNetwork;
1736 private final PacketKeepaliveCallback mCallback;
1737 private final Looper mLooper;
1738 private final Messenger mMessenger;
1739
1740 private volatile Integer mSlot;
1741
1742 void stopLooper() {
1743 mLooper.quit();
1744 }
1745
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001746 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001747 public void stop() {
1748 try {
1749 mService.stopKeepalive(mNetwork, mSlot);
1750 } catch (RemoteException e) {
1751 Log.e(TAG, "Error stopping packet keepalive: ", e);
1752 stopLooper();
1753 }
1754 }
1755
1756 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09001757 Preconditions.checkNotNull(network, "network cannot be null");
1758 Preconditions.checkNotNull(callback, "callback cannot be null");
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001759 mNetwork = network;
1760 mCallback = callback;
1761 HandlerThread thread = new HandlerThread(TAG);
1762 thread.start();
1763 mLooper = thread.getLooper();
1764 mMessenger = new Messenger(new Handler(mLooper) {
1765 @Override
1766 public void handleMessage(Message message) {
1767 switch (message.what) {
1768 case NetworkAgent.EVENT_PACKET_KEEPALIVE:
1769 int error = message.arg2;
1770 try {
1771 if (error == SUCCESS) {
1772 if (mSlot == null) {
1773 mSlot = message.arg1;
1774 mCallback.onStarted();
1775 } else {
1776 mSlot = null;
1777 stopLooper();
1778 mCallback.onStopped();
1779 }
1780 } else {
1781 stopLooper();
1782 mCallback.onError(error);
1783 }
1784 } catch (Exception e) {
1785 Log.e(TAG, "Exception in keepalive callback(" + error + ")", e);
1786 }
1787 break;
1788 default:
1789 Log.e(TAG, "Unhandled message " + Integer.toHexString(message.what));
1790 break;
1791 }
1792 }
1793 });
1794 }
1795 }
1796
1797 /**
1798 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
1799 *
1800 * @hide
1801 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001802 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001803 public PacketKeepalive startNattKeepalive(
1804 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
1805 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
1806 final PacketKeepalive k = new PacketKeepalive(network, callback);
1807 try {
1808 mService.startNattKeepalive(network, intervalSeconds, k.mMessenger, new Binder(),
1809 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
1810 } catch (RemoteException e) {
1811 Log.e(TAG, "Error starting packet keepalive: ", e);
1812 k.stopLooper();
1813 return null;
1814 }
1815 return k;
1816 }
1817
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 /**
1819 * Ensure that a network route exists to deliver traffic to the specified
1820 * host via the specified network interface. An attempt to add a route that
1821 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001822 *
1823 * <p>This method requires the caller to hold either the
1824 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1825 * or the ability to modify system settings as determined by
1826 * {@link android.provider.Settings.System#canWrite}.</p>
1827 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 * @param networkType the type of the network over which traffic to the specified
1829 * host is to be routed
1830 * @param hostAddress the IP address of the host to which the route is desired
1831 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001832 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001833 * @deprecated Deprecated in favor of the
1834 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
1835 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001836 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001837 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001838 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001839 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001840 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001842 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001843 }
1844
1845 /**
1846 * Ensure that a network route exists to deliver traffic to the specified
1847 * host via the specified network interface. An attempt to add a route that
1848 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001849 *
1850 * <p>This method requires the caller to hold either the
1851 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1852 * or the ability to modify system settings as determined by
1853 * {@link android.provider.Settings.System#canWrite}.</p>
1854 *
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001855 * @param networkType the type of the network over which traffic to the specified
1856 * host is to be routed
1857 * @param hostAddress the IP address of the host to which the route is desired
1858 * @return {@code true} on success, {@code false} on failure
1859 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001860 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001861 * {@link #bindProcessToNetwork} API.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001862 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001863 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001864 @UnsupportedAppUsage
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001865 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001866 checkLegacyRoutingApiAccess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 try {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001868 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001869 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001870 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 }
1872 }
1873
1874 /**
1875 * Returns the value of the setting for background data usage. If false,
1876 * applications should not use the network if the application is not in the
1877 * foreground. Developers should respect this setting, and check the value
1878 * of this before performing any background data operations.
1879 * <p>
1880 * All applications that have background services that use the network
1881 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001882 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001883 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001884 * background data depends on several combined factors, and this method will
1885 * always return {@code true}. Instead, when background data is unavailable,
1886 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001887 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 * @return Whether background data usage is allowed.
1889 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001890 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001891 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001892 // assume that background data is allowed; final authority is
1893 // NetworkInfo which may be blocked.
1894 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001895 }
1896
1897 /**
1898 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001899 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 * @param allowBackgroundData Whether an application should use data while
1901 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001902 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001903 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1904 * @see #getBackgroundDataSetting()
1905 * @hide
1906 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001907 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001908 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001910 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001911 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001912
Jeff Sharkey43d2a172017-07-12 10:50:42 -06001913 /** {@hide} */
1914 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001915 @UnsupportedAppUsage
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001916 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1917 try {
1918 return mService.getActiveNetworkQuotaInfo();
1919 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001920 throw e.rethrowFromSystemServer();
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001921 }
1922 }
1923
1924 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001925 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001926 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001927 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001928 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001929 @UnsupportedAppUsage
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001930 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001931 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1932 if (b != null) {
1933 try {
1934 ITelephony it = ITelephony.Stub.asInterface(b);
Shishir Agrawal7ea3e8b2016-01-25 13:03:07 -08001935 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Wink Saville36ffb042014-12-05 11:10:30 -08001936 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
Malcolm Chenb455e722017-11-28 15:57:14 -08001937 boolean retVal = it.isUserDataEnabled(subId);
Wink Saville36ffb042014-12-05 11:10:30 -08001938 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1939 + " retVal=" + retVal);
1940 return retVal;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001941 } catch (RemoteException e) {
1942 throw e.rethrowFromSystemServer();
1943 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001944 }
Wink Saville36ffb042014-12-05 11:10:30 -08001945 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001946 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001947 }
1948
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 /**
Robert Greenwaltb2489872014-09-04 16:44:35 -07001950 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt6078b502014-06-11 16:05:07 -07001951 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001952 */
1953 public interface OnNetworkActiveListener {
1954 /**
1955 * Called on the main thread of the process to report that the current data network
1956 * has become active, and it is now a good time to perform any pending network
1957 * operations. Note that this listener only tells you when the network becomes
1958 * active; if at any other time you want to know whether it is active (and thus okay
1959 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001960 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001961 */
Chalard Jean4d660112018-06-04 16:52:49 +09001962 void onNetworkActive();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001963 }
1964
1965 private INetworkManagementService getNetworkManagementService() {
1966 synchronized (this) {
1967 if (mNMService != null) {
1968 return mNMService;
1969 }
1970 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1971 mNMService = INetworkManagementService.Stub.asInterface(b);
1972 return mNMService;
1973 }
1974 }
1975
1976 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
Chalard Jean4d660112018-06-04 16:52:49 +09001977 mNetworkActivityListeners = new ArrayMap<>();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001978
1979 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001980 * Start listening to reports when the system's default data network is active, meaning it is
1981 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1982 * to determine the current state of the system's default network after registering the
1983 * listener.
1984 * <p>
1985 * If the process default network has been set with
Paul Jensen72db88e2015-03-10 10:54:12 -04001986 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt6078b502014-06-11 16:05:07 -07001987 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001988 *
1989 * @param l The listener to be told when the network is active.
1990 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001991 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001992 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1993 @Override
1994 public void onNetworkActive() throws RemoteException {
1995 l.onNetworkActive();
1996 }
1997 };
1998
1999 try {
2000 getNetworkManagementService().registerNetworkActivityListener(rl);
2001 mNetworkActivityListeners.put(l, rl);
2002 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002003 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002004 }
2005 }
2006
2007 /**
2008 * Remove network active listener previously registered with
Robert Greenwaltb2489872014-09-04 16:44:35 -07002009 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002010 *
2011 * @param l Previously registered listener.
2012 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09002013 public void removeDefaultNetworkActiveListener(@NonNull OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002014 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
Hugo Benichie7678512017-05-09 15:19:01 +09002015 Preconditions.checkArgument(rl != null, "Listener was not registered.");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002016 try {
2017 getNetworkManagementService().unregisterNetworkActivityListener(rl);
2018 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002019 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002020 }
2021 }
2022
2023 /**
2024 * Return whether the data network is currently active. An active network means that
2025 * it is currently in a high power state for performing data transmission. On some
2026 * types of networks, it may be expensive to move and stay in such a state, so it is
2027 * more power efficient to batch network traffic together when the radio is already in
2028 * this state. This method tells you whether right now is currently a good time to
2029 * initiate network traffic, as the network is already active.
2030 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002031 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002032 try {
2033 return getNetworkManagementService().isNetworkActive();
2034 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002035 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002036 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002037 }
2038
2039 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002040 * {@hide}
2041 */
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09002042 public ConnectivityManager(Context context, IConnectivityManager service) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09002043 mContext = Preconditions.checkNotNull(context, "missing context");
2044 mService = Preconditions.checkNotNull(service, "missing IConnectivityManager");
Paul Jensene0bef712014-12-10 15:12:18 -05002045 sInstance = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002046 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002047
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07002048 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002049 @UnsupportedAppUsage
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07002050 public static ConnectivityManager from(Context context) {
2051 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
2052 }
2053
Lorenzo Colittifbe9b1a2016-07-28 17:14:11 +09002054 /* TODO: These permissions checks don't belong in client-side code. Move them to
2055 * services.jar, possibly in com.android.server.net. */
2056
2057 /** {@hide} */
Lorenzo Colittid5427052015-10-15 16:29:00 +09002058 public static final void enforceChangePermission(Context context) {
2059 int uid = Binder.getCallingUid();
2060 Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
2061 .getPackageNameForUid(context, uid), true /* throwException */);
2062 }
2063
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002064 /** {@hide} */
2065 public static final void enforceTetherChangePermission(Context context, String callingPkg) {
Hugo Benichie7678512017-05-09 15:19:01 +09002066 Preconditions.checkNotNull(context, "Context cannot be null");
2067 Preconditions.checkNotNull(callingPkg, "callingPkg cannot be null");
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002068
Robert Greenwaltedb47662014-09-16 17:54:19 -07002069 if (context.getResources().getStringArray(
2070 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
2071 // Have a provisioning app - must only let system apps (which check this app)
2072 // turn on tethering
2073 context.enforceCallingOrSelfPermission(
Jeremy Kleind42209d2015-12-28 15:11:58 -08002074 android.Manifest.permission.TETHER_PRIVILEGED, "ConnectivityService");
Robert Greenwaltedb47662014-09-16 17:54:19 -07002075 } else {
Billy Laua7238a32015-08-01 12:45:02 +01002076 int uid = Binder.getCallingUid();
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002077 // If callingPkg's uid is not same as Binder.getCallingUid(),
2078 // AppOpsService throws SecurityException.
2079 Settings.checkAndNoteWriteSettingsOperation(context, uid, callingPkg,
2080 true /* throwException */);
Robert Greenwaltedb47662014-09-16 17:54:19 -07002081 }
2082 }
2083
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002084 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002085 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2086 * situations where a Context pointer is unavailable.
2087 * @hide
2088 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002089 @Deprecated
Paul Jensen72db88e2015-03-10 10:54:12 -04002090 static ConnectivityManager getInstanceOrNull() {
2091 return sInstance;
2092 }
2093
2094 /**
2095 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2096 * situations where a Context pointer is unavailable.
2097 * @hide
2098 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002099 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002100 @UnsupportedAppUsage
Paul Jensen72db88e2015-03-10 10:54:12 -04002101 private static ConnectivityManager getInstance() {
2102 if (getInstanceOrNull() == null) {
Paul Jensene0bef712014-12-10 15:12:18 -05002103 throw new IllegalStateException("No ConnectivityManager yet constructed");
2104 }
Paul Jensen72db88e2015-03-10 10:54:12 -04002105 return getInstanceOrNull();
Paul Jensene0bef712014-12-10 15:12:18 -05002106 }
2107
2108 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002109 * Get the set of tetherable, available interfaces. This list is limited by
2110 * device configuration and current interface existence.
2111 *
2112 * @return an array of 0 or more Strings of tetherable interface names.
2113 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002114 * {@hide}
2115 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002116 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002117 @UnsupportedAppUsage
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002118 public String[] getTetherableIfaces() {
2119 try {
2120 return mService.getTetherableIfaces();
2121 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002122 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002123 }
2124 }
2125
2126 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002127 * Get the set of tethered interfaces.
2128 *
2129 * @return an array of 0 or more String of currently tethered interface names.
2130 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002131 * {@hide}
2132 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002133 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002134 @UnsupportedAppUsage
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002135 public String[] getTetheredIfaces() {
2136 try {
2137 return mService.getTetheredIfaces();
2138 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002139 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002140 }
2141 }
2142
2143 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002144 * Get the set of interface names which attempted to tether but
2145 * failed. Re-attempting to tether may cause them to reset to the Tethered
2146 * state. Alternatively, causing the interface to be destroyed and recreated
2147 * may cause them to reset to the available state.
2148 * {@link ConnectivityManager#getLastTetherError} can be used to get more
2149 * information on the cause of the errors.
2150 *
2151 * @return an array of 0 or more String indicating the interface names
2152 * which failed to tether.
2153 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002154 * {@hide}
2155 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002156 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002157 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002158 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002159 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08002160 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002161 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002162 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002163 }
2164 }
2165
2166 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07002167 * Get the set of tethered dhcp ranges.
2168 *
2169 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
2170 * {@hide}
2171 */
2172 public String[] getTetheredDhcpRanges() {
2173 try {
2174 return mService.getTetheredDhcpRanges();
2175 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002176 throw e.rethrowFromSystemServer();
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07002177 }
2178 }
2179
2180 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002181 * Attempt to tether the named interface. This will setup a dhcp server
2182 * on the interface, forward and NAT IP packets and forward DNS requests
2183 * to the best active upstream network interface. Note that if no upstream
2184 * IP network interface is available, dhcp will still run and traffic will be
2185 * allowed between the tethered devices and this device, though upstream net
2186 * access will of course fail until an upstream network interface becomes
2187 * active.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002188 *
2189 * <p>This method requires the caller to hold either the
2190 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2191 * or the ability to modify system settings as determined by
2192 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002193 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002194 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2195 * and WifiStateMachine which need direct access. All other clients should use
2196 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2197 * logic.</p>
2198 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002199 * @param iface the interface name to tether.
2200 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2201 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002202 * {@hide}
2203 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002204 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002205 public int tether(String iface) {
2206 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002207 String pkgName = mContext.getOpPackageName();
2208 Log.i(TAG, "tether caller:" + pkgName);
2209 return mService.tether(iface, pkgName);
Robert Greenwalt5a735062010-03-02 17:25:02 -08002210 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002211 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002212 }
2213 }
2214
2215 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002216 * Stop tethering the named interface.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002217 *
2218 * <p>This method requires the caller to hold either the
2219 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2220 * or the ability to modify system settings as determined by
2221 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002222 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002223 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2224 * and WifiStateMachine which need direct access. All other clients should use
2225 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2226 * logic.</p>
2227 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002228 * @param iface the interface name to untether.
2229 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2230 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002231 * {@hide}
2232 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002233 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002234 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002235 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002236 String pkgName = mContext.getOpPackageName();
2237 Log.i(TAG, "untether caller:" + pkgName);
2238 return mService.untether(iface, pkgName);
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002239 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002240 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002241 }
2242 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002243
2244 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002245 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002246 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002247 * due to device configuration.
2248 *
Chalard Jean8f76fc32017-09-26 15:45:18 +09002249 * <p>If this app does not have permission to use this API, it will always
2250 * return false rather than throw an exception.</p>
2251 *
2252 * <p>If the device has a hotspot provisioning app, the caller is required to hold the
2253 * {@link android.Manifest.permission.TETHER_PRIVILEGED} permission.</p>
2254 *
2255 * <p>Otherwise, this method requires the caller to hold the ability to modify system
2256 * settings as determined by {@link android.provider.Settings.System#canWrite}.</p>
2257 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002258 * @return a boolean - {@code true} indicating Tethering is supported.
2259 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002260 * {@hide}
2261 */
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002262 @SystemApi
Chalard Jean8f76fc32017-09-26 15:45:18 +09002263 @RequiresPermission(anyOf = {android.Manifest.permission.TETHER_PRIVILEGED,
2264 android.Manifest.permission.WRITE_SETTINGS})
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002265 public boolean isTetheringSupported() {
Chalard Jean8f76fc32017-09-26 15:45:18 +09002266 String pkgName = mContext.getOpPackageName();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002267 try {
Udam Saini0e94c362017-06-07 12:06:28 -07002268 return mService.isTetheringSupported(pkgName);
Chalard Jean8f76fc32017-09-26 15:45:18 +09002269 } catch (SecurityException e) {
2270 // This API is not available to this caller, but for backward-compatibility
2271 // this will just return false instead of throwing.
2272 return false;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002273 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002274 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002275 }
2276 }
2277
2278 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002279 * Callback for use with {@link #startTethering} to find out whether tethering succeeded.
2280 * @hide
2281 */
2282 @SystemApi
2283 public static abstract class OnStartTetheringCallback {
2284 /**
2285 * Called when tethering has been successfully started.
2286 */
Chalard Jean4d660112018-06-04 16:52:49 +09002287 public void onTetheringStarted() {}
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002288
2289 /**
2290 * Called when starting tethering failed.
2291 */
Chalard Jean4d660112018-06-04 16:52:49 +09002292 public void onTetheringFailed() {}
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002293 }
2294
2295 /**
2296 * Convenient overload for
2297 * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null
2298 * handler to run on the current thread's {@link Looper}.
2299 * @hide
2300 */
2301 @SystemApi
Udam Saini0e94c362017-06-07 12:06:28 -07002302 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002303 public void startTethering(int type, boolean showProvisioningUi,
2304 final OnStartTetheringCallback callback) {
2305 startTethering(type, showProvisioningUi, callback, null);
2306 }
2307
2308 /**
2309 * Runs tether provisioning for the given type if needed and then starts tethering if
2310 * the check succeeds. If no carrier provisioning is required for tethering, tethering is
2311 * enabled immediately. If provisioning fails, tethering will not be enabled. It also
2312 * schedules tether provisioning re-checks if appropriate.
2313 *
2314 * @param type The type of tethering to start. Must be one of
2315 * {@link ConnectivityManager.TETHERING_WIFI},
2316 * {@link ConnectivityManager.TETHERING_USB}, or
2317 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2318 * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there
2319 * is one. This should be true the first time this function is called and also any time
2320 * the user can see this UI. It gives users information from their carrier about the
2321 * check failing and how they can sign up for tethering if possible.
2322 * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller
2323 * of the result of trying to tether.
2324 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
2325 * @hide
2326 */
2327 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002328 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002329 public void startTethering(int type, boolean showProvisioningUi,
2330 final OnStartTetheringCallback callback, Handler handler) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09002331 Preconditions.checkNotNull(callback, "OnStartTetheringCallback cannot be null.");
Jeremy Klein5f277e12016-03-12 16:29:54 -08002332
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002333 ResultReceiver wrappedCallback = new ResultReceiver(handler) {
2334 @Override
2335 protected void onReceiveResult(int resultCode, Bundle resultData) {
2336 if (resultCode == TETHER_ERROR_NO_ERROR) {
2337 callback.onTetheringStarted();
2338 } else {
2339 callback.onTetheringFailed();
2340 }
2341 }
2342 };
Jeremy Klein5f277e12016-03-12 16:29:54 -08002343
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002344 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002345 String pkgName = mContext.getOpPackageName();
2346 Log.i(TAG, "startTethering caller:" + pkgName);
2347 mService.startTethering(type, wrappedCallback, showProvisioningUi, pkgName);
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002348 } catch (RemoteException e) {
2349 Log.e(TAG, "Exception trying to start tethering.", e);
2350 wrappedCallback.send(TETHER_ERROR_SERVICE_UNAVAIL, null);
2351 }
2352 }
2353
2354 /**
2355 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if
2356 * applicable.
2357 *
2358 * @param type The type of tethering to stop. Must be one of
2359 * {@link ConnectivityManager.TETHERING_WIFI},
2360 * {@link ConnectivityManager.TETHERING_USB}, or
2361 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2362 * @hide
2363 */
2364 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002365 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002366 public void stopTethering(int type) {
2367 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002368 String pkgName = mContext.getOpPackageName();
2369 Log.i(TAG, "stopTethering caller:" + pkgName);
2370 mService.stopTethering(type, pkgName);
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002371 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002372 throw e.rethrowFromSystemServer();
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002373 }
2374 }
2375
2376 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002377 * Get the list of regular expressions that define any tetherable
2378 * USB network interfaces. If USB tethering is not supported by the
2379 * device, this list should be empty.
2380 *
2381 * @return an array of 0 or more regular expression Strings defining
2382 * what interfaces are considered tetherable usb interfaces.
2383 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002384 * {@hide}
2385 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002386 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002387 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002388 public String[] getTetherableUsbRegexs() {
2389 try {
2390 return mService.getTetherableUsbRegexs();
2391 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002392 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002393 }
2394 }
2395
2396 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002397 * Get the list of regular expressions that define any tetherable
2398 * Wifi network interfaces. If Wifi tethering is not supported by the
2399 * device, this list should be empty.
2400 *
2401 * @return an array of 0 or more regular expression Strings defining
2402 * what interfaces are considered tetherable wifi interfaces.
2403 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002404 * {@hide}
2405 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002406 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002407 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002408 public String[] getTetherableWifiRegexs() {
2409 try {
2410 return mService.getTetherableWifiRegexs();
2411 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002412 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002413 }
2414 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08002415
Danica Chang6fdd0c62010-08-11 14:54:43 -07002416 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002417 * Get the list of regular expressions that define any tetherable
2418 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
2419 * device, this list should be empty.
2420 *
2421 * @return an array of 0 or more regular expression Strings defining
2422 * what interfaces are considered tetherable bluetooth interfaces.
2423 *
Danica Chang6fdd0c62010-08-11 14:54:43 -07002424 * {@hide}
2425 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002426 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002427 @UnsupportedAppUsage
Danica Chang6fdd0c62010-08-11 14:54:43 -07002428 public String[] getTetherableBluetoothRegexs() {
2429 try {
2430 return mService.getTetherableBluetoothRegexs();
2431 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002432 throw e.rethrowFromSystemServer();
Danica Chang6fdd0c62010-08-11 14:54:43 -07002433 }
2434 }
2435
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002436 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002437 * Attempt to both alter the mode of USB and Tethering of USB. A
2438 * utility method to deal with some of the complexity of USB - will
2439 * attempt to switch to Rndis and subsequently tether the resulting
2440 * interface on {@code true} or turn off tethering and switch off
2441 * Rndis on {@code false}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002442 *
2443 * <p>This method requires the caller to hold either the
2444 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2445 * or the ability to modify system settings as determined by
2446 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002447 *
2448 * @param enable a boolean - {@code true} to enable tethering
2449 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2450 *
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002451 * {@hide}
2452 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002453 @UnsupportedAppUsage
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002454 public int setUsbTethering(boolean enable) {
2455 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002456 String pkgName = mContext.getOpPackageName();
2457 Log.i(TAG, "setUsbTethering caller:" + pkgName);
2458 return mService.setUsbTethering(enable, pkgName);
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002459 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002460 throw e.rethrowFromSystemServer();
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002461 }
2462 }
2463
Robert Greenwalt5a735062010-03-02 17:25:02 -08002464 /** {@hide} */
2465 public static final int TETHER_ERROR_NO_ERROR = 0;
2466 /** {@hide} */
2467 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
2468 /** {@hide} */
2469 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
2470 /** {@hide} */
2471 public static final int TETHER_ERROR_UNSUPPORTED = 3;
2472 /** {@hide} */
2473 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
2474 /** {@hide} */
2475 public static final int TETHER_ERROR_MASTER_ERROR = 5;
2476 /** {@hide} */
2477 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
2478 /** {@hide} */
2479 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
2480 /** {@hide} */
2481 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
2482 /** {@hide} */
2483 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
2484 /** {@hide} */
2485 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002486 /** {@hide} */
2487 public static final int TETHER_ERROR_PROVISION_FAILED = 11;
Robert Greenwalt5a735062010-03-02 17:25:02 -08002488
2489 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002490 * Get a more detailed error code after a Tethering or Untethering
2491 * request asynchronously failed.
2492 *
2493 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08002494 * @return error The error code of the last error tethering or untethering the named
2495 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002496 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002497 * {@hide}
2498 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002499 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002500 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002501 public int getLastTetherError(String iface) {
2502 try {
2503 return mService.getLastTetherError(iface);
2504 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002505 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002506 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002507 }
2508
2509 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002510 * Report network connectivity status. This is currently used only
2511 * to alter status bar UI.
Paul Jensenb2748922015-05-06 11:10:18 -04002512 * <p>This method requires the caller to hold the permission
2513 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002514 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002515 * @param networkType The type of network you want to report on
2516 * @param percentage The quality of the connection 0 is bad, 100 is good
Chalard Jean6b1da6e2018-03-08 13:54:53 +09002517 * @deprecated Types are deprecated. Use {@link #reportNetworkConnectivity} instead.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002518 * {@hide}
2519 */
2520 public void reportInetCondition(int networkType, int percentage) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09002521 printStackTrace();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002522 try {
2523 mService.reportInetCondition(networkType, percentage);
2524 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002525 throw e.rethrowFromSystemServer();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002526 }
2527 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002528
2529 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002530 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07002531 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002532 * the framework to re-evaluate network connectivity and/or switch to another
2533 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002534 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002535 * @param network The {@link Network} the application was attempting to use
2536 * or {@code null} to indicate the current default network.
Paul Jensenbfd17b72015-04-07 12:43:13 -04002537 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
2538 * working and non-working connectivity.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002539 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002540 @Deprecated
Chalard Jean50bea3d2019-01-07 19:26:34 +09002541 public void reportBadNetwork(@Nullable Network network) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09002542 printStackTrace();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002543 try {
Paul Jensenbfd17b72015-04-07 12:43:13 -04002544 // One of these will be ignored because it matches system's current state.
2545 // The other will trigger the necessary reevaluation.
2546 mService.reportNetworkConnectivity(network, true);
2547 mService.reportNetworkConnectivity(network, false);
2548 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002549 throw e.rethrowFromSystemServer();
Paul Jensenbfd17b72015-04-07 12:43:13 -04002550 }
2551 }
2552
2553 /**
2554 * Report to the framework whether a network has working connectivity.
2555 * This provides a hint to the system that a particular network is providing
2556 * working connectivity or not. In response the framework may re-evaluate
2557 * the network's connectivity and might take further action thereafter.
2558 *
2559 * @param network The {@link Network} the application was attempting to use
2560 * or {@code null} to indicate the current default network.
2561 * @param hasConnectivity {@code true} if the application was able to successfully access the
2562 * Internet using {@code network} or {@code false} if not.
2563 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09002564 public void reportNetworkConnectivity(@Nullable Network network, boolean hasConnectivity) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09002565 printStackTrace();
Paul Jensenbfd17b72015-04-07 12:43:13 -04002566 try {
2567 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002568 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002569 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002570 }
2571 }
2572
2573 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002574 * Set a network-independent global http proxy. This is not normally what you want
2575 * for typical HTTP proxies - they are general network dependent. However if you're
2576 * doing something unusual like general internal filtering this may be useful. On
2577 * a private network where the proxy is not accessible, you may break HTTP using this.
Paul Jensenb2748922015-05-06 11:10:18 -04002578 *
2579 * @param p A {@link ProxyInfo} object defining the new global
2580 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002581 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002582 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002583 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Jason Monk207900c2014-04-25 15:00:09 -04002584 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002585 try {
2586 mService.setGlobalProxy(p);
2587 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002588 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002589 }
2590 }
2591
2592 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002593 * Retrieve any network-independent global HTTP proxy.
2594 *
Jason Monk207900c2014-04-25 15:00:09 -04002595 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002596 * if no global HTTP proxy is set.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002597 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002598 */
Jason Monk207900c2014-04-25 15:00:09 -04002599 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002600 try {
2601 return mService.getGlobalProxy();
2602 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002603 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002604 }
2605 }
2606
2607 /**
Paul Jensencee9b512015-05-06 07:32:40 -04002608 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
2609 * network-specific HTTP proxy. If {@code network} is null, the
2610 * network-specific proxy returned is the proxy of the default active
2611 * network.
2612 *
2613 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
2614 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
2615 * or when {@code network} is {@code null},
2616 * the {@code ProxyInfo} for the default active network. Returns
2617 * {@code null} when no proxy applies or the caller doesn't have
2618 * permission to use {@code network}.
2619 * @hide
2620 */
2621 public ProxyInfo getProxyForNetwork(Network network) {
2622 try {
2623 return mService.getProxyForNetwork(network);
2624 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002625 throw e.rethrowFromSystemServer();
Paul Jensencee9b512015-05-06 07:32:40 -04002626 }
2627 }
2628
2629 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002630 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
2631 * otherwise if this process is bound to a {@link Network} using
Paul Jensen72db88e2015-03-10 10:54:12 -04002632 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensene0bef712014-12-10 15:12:18 -05002633 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002634 *
Jason Monk207900c2014-04-25 15:00:09 -04002635 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002636 * HTTP proxy is active.
Robert Greenwalt434203a2010-10-11 16:00:27 -07002637 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09002638 @Nullable
Paul Jensene0bef712014-12-10 15:12:18 -05002639 public ProxyInfo getDefaultProxy() {
Paul Jensencee9b512015-05-06 07:32:40 -04002640 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwalt434203a2010-10-11 16:00:27 -07002641 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07002642
2643 /**
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002644 * Returns true if the hardware supports the given network type
2645 * else it returns false. This doesn't indicate we have coverage
2646 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002647 * hardware supports it. For example a GSM phone without a SIM
2648 * should still return {@code true} for mobile data, but a wifi only
2649 * tablet would return {@code false}.
2650 *
2651 * @param networkType The network type we'd like to check
2652 * @return {@code true} if supported, else {@code false}
Chalard Jean6b1da6e2018-03-08 13:54:53 +09002653 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002654 * @hide
2655 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +09002656 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002657 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002658 @UnsupportedAppUsage
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002659 public boolean isNetworkSupported(int networkType) {
2660 try {
2661 return mService.isNetworkSupported(networkType);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002662 } catch (RemoteException e) {
2663 throw e.rethrowFromSystemServer();
2664 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002665 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002666
2667 /**
2668 * Returns if the currently active data network is metered. A network is
2669 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002670 * that connection due to monetary costs, data limitations or
2671 * battery/performance issues. You should check this before doing large
2672 * data transfers, and warn the user or delay the operation until another
2673 * network is available.
2674 *
2675 * @return {@code true} if large transfers should be avoided, otherwise
2676 * {@code false}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002677 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002678 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002679 public boolean isActiveNetworkMetered() {
2680 try {
2681 return mService.isActiveNetworkMetered();
2682 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002683 throw e.rethrowFromSystemServer();
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002684 }
2685 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002686
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002687 /**
2688 * If the LockdownVpn mechanism is enabled, updates the vpn
2689 * with a reload of its profile.
2690 *
2691 * @return a boolean with {@code} indicating success
2692 *
2693 * <p>This method can only be called by the system UID
2694 * {@hide}
2695 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002696 public boolean updateLockdownVpn() {
2697 try {
2698 return mService.updateLockdownVpn();
2699 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002700 throw e.rethrowFromSystemServer();
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002701 }
2702 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07002703
2704 /**
Wink Saville948282b2013-08-29 08:55:16 -07002705 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07002706 *
Wink Savilleab9321d2013-06-29 21:10:57 -07002707 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07002708 *
2709 * @return time out that will be used, maybe less that suggestedTimeOutMs
2710 * -1 if an error.
2711 *
2712 * {@hide}
2713 */
Wink Saville948282b2013-08-29 08:55:16 -07002714 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07002715 int timeOutMs = -1;
2716 try {
Wink Saville948282b2013-08-29 08:55:16 -07002717 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07002718 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002719 throw e.rethrowFromSystemServer();
Wink Savilleab9321d2013-06-29 21:10:57 -07002720 }
2721 return timeOutMs;
2722 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002723
2724 /**
Wink Saville42d4f082013-07-20 20:31:59 -07002725 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002726 * {@hide}
2727 */
2728 public String getMobileProvisioningUrl() {
2729 try {
2730 return mService.getMobileProvisioningUrl();
2731 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002732 throw e.rethrowFromSystemServer();
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002733 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002734 }
Wink Saville42d4f082013-07-20 20:31:59 -07002735
2736 /**
Wink Saville948282b2013-08-29 08:55:16 -07002737 * Set sign in error notification to visible or in visible
2738 *
Wink Saville948282b2013-08-29 08:55:16 -07002739 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04002740 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville948282b2013-08-29 08:55:16 -07002741 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002742 @Deprecated
Wink Saville948282b2013-08-29 08:55:16 -07002743 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04002744 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07002745 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04002746 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07002747 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002748 throw e.rethrowFromSystemServer();
Wink Saville948282b2013-08-29 08:55:16 -07002749 }
2750 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002751
2752 /**
2753 * Set the value for enabling/disabling airplane mode
2754 *
2755 * @param enable whether to enable airplane mode or not
2756 *
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002757 * @hide
2758 */
Lorenzo Colittif5845d12018-10-09 18:55:11 +09002759 @RequiresPermission(anyOf = {
2760 android.Manifest.permission.NETWORK_SETTINGS,
2761 android.Manifest.permission.NETWORK_SETUP_WIZARD,
2762 android.Manifest.permission.NETWORK_STACK})
Lorenzo Colitti85eca482018-10-09 18:50:32 +09002763 @SystemApi
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002764 public void setAirplaneMode(boolean enable) {
2765 try {
2766 mService.setAirplaneMode(enable);
2767 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002768 throw e.rethrowFromSystemServer();
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002769 }
2770 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002771
2772 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002773 @UnsupportedAppUsage
Robert Greenwalta67be032014-05-16 15:49:14 -07002774 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07002775 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07002776 mService.registerNetworkFactory(messenger, name);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002777 } catch (RemoteException e) {
2778 throw e.rethrowFromSystemServer();
2779 }
Robert Greenwalta67be032014-05-16 15:49:14 -07002780 }
2781
2782 /** {@hide} */
Mathew Inwood55418ea2018-12-20 15:30:45 +00002783 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Robert Greenwalta67be032014-05-16 15:49:14 -07002784 public void unregisterNetworkFactory(Messenger messenger) {
2785 try {
2786 mService.unregisterNetworkFactory(messenger);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002787 } catch (RemoteException e) {
2788 throw e.rethrowFromSystemServer();
2789 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002790 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002791
Paul Jensen31a94f42015-02-13 14:18:39 -05002792 /**
2793 * @hide
2794 * Register a NetworkAgent with ConnectivityService.
2795 * @return NetID corresponding to NetworkAgent.
2796 */
2797 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002798 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002799 try {
Paul Jensen31a94f42015-02-13 14:18:39 -05002800 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
2801 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002802 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -05002803 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002804 }
2805
Robert Greenwalt9258c642014-03-26 16:47:06 -07002806 /**
Hugo Benichidafed3d2017-03-06 09:17:06 +09002807 * Base class for {@code NetworkRequest} callbacks. Used for notifications about network
2808 * changes. Should be extended by applications wanting notifications.
2809 *
2810 * A {@code NetworkCallback} is registered by calling
2811 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
2812 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)},
Hugo Benichica867dc2018-02-07 21:17:43 +09002813 * or {@link #registerDefaultNetworkCallback(NetworkCallback)}. A {@code NetworkCallback} is
Hugo Benichidafed3d2017-03-06 09:17:06 +09002814 * unregistered by calling {@link #unregisterNetworkCallback(NetworkCallback)}.
2815 * A {@code NetworkCallback} should be registered at most once at any time.
2816 * A {@code NetworkCallback} that has been unregistered can be registered again.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002817 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002818 public static class NetworkCallback {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002819 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002820 * Called when the framework connects to a new network to evaluate whether it satisfies this
2821 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
2822 * callback. There is no guarantee that this new network will satisfy any requests, or that
2823 * the network will stay connected for longer than the time necessary to evaluate it.
2824 * <p>
2825 * Most applications <b>should not</b> act on this callback, and should instead use
2826 * {@link #onAvailable}. This callback is intended for use by applications that can assist
2827 * the framework in properly evaluating the network &mdash; for example, an application that
2828 * can automatically log in to a captive portal without user intervention.
2829 *
2830 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti66276122015-06-11 14:27:17 +09002831 *
2832 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002833 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002834 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002835
2836 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002837 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002838 * This callback may be called more than once if the {@link Network} that is
2839 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002840 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002841 * @param network The {@link Network} of the satisfying network.
Chalard Jean804b8fb2018-01-30 22:41:41 +09002842 * @param networkCapabilities The {@link NetworkCapabilities} of the satisfying network.
2843 * @param linkProperties The {@link LinkProperties} of the satisfying network.
junyulai05986c62018-08-07 19:50:45 +08002844 * @param blocked Whether access to the {@link Network} is blocked due to system policy.
Chalard Jean804b8fb2018-01-30 22:41:41 +09002845 * @hide
2846 */
2847 public void onAvailable(Network network, NetworkCapabilities networkCapabilities,
junyulai05986c62018-08-07 19:50:45 +08002848 LinkProperties linkProperties, boolean blocked) {
Chalard Jean804b8fb2018-01-30 22:41:41 +09002849 // Internally only this method is called when a new network is available, and
2850 // it calls the callback in the same way and order that older versions used
2851 // to call so as not to change the behavior.
2852 onAvailable(network);
2853 if (!networkCapabilities.hasCapability(
2854 NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)) {
2855 onNetworkSuspended(network);
2856 }
2857 onCapabilitiesChanged(network, networkCapabilities);
2858 onLinkPropertiesChanged(network, linkProperties);
junyulai05986c62018-08-07 19:50:45 +08002859 onBlockedStatusChanged(network, blocked);
Chalard Jean804b8fb2018-01-30 22:41:41 +09002860 }
2861
2862 /**
2863 * Called when the framework connects and has declared a new network ready for use.
2864 * This callback may be called more than once if the {@link Network} that is
2865 * satisfying the request changes. This will always immediately be followed by a
2866 * call to {@link #onCapabilitiesChanged(Network, NetworkCapabilities)} then by a
junyulai05986c62018-08-07 19:50:45 +08002867 * call to {@link #onLinkPropertiesChanged(Network, LinkProperties)}, and a call to
2868 * {@link #onBlockedStatusChanged(Network, boolean)}.
Chalard Jean804b8fb2018-01-30 22:41:41 +09002869 *
2870 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002871 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002872 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002873
2874 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002875 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002876 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002877 * for graceful handover. This may not be called if we have a hard loss
2878 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002879 * {@link NetworkCallback#onLost} call or a
2880 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002881 * on whether we lose or regain it.
2882 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002883 * @param network The {@link Network} that is about to be disconnected.
2884 * @param maxMsToLive The time in ms the framework will attempt to keep the
2885 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002886 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002887 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002888 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002889
2890 /**
2891 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002892 * graceful failure ends.
2893 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002894 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002895 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002896 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002897
2898 /**
Etan Cohenaebf17e2017-03-01 12:47:28 -08002899 * Called if no network is found in the timeout time specified in
Hugo Benichi0eec03f2017-05-15 15:15:33 +09002900 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} call. This callback is not
Etan Cohenaebf17e2017-03-01 12:47:28 -08002901 * called for the version of {@link #requestNetwork(NetworkRequest, NetworkCallback)}
2902 * without timeout. When this callback is invoked the associated
2903 * {@link NetworkRequest} will have already been removed and released, as if
2904 * {@link #unregisterNetworkCallback(NetworkCallback)} had been called.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002905 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002906 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002907
2908 /**
2909 * Called when the network the framework connected to for this request
2910 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002911 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002912 * @param network The {@link Network} whose capabilities have changed.
Chalard Jean804b8fb2018-01-30 22:41:41 +09002913 * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this
2914 * network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002915 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002916 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002917 NetworkCapabilities networkCapabilities) {}
2918
2919 /**
2920 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002921 * changes {@link LinkProperties}.
2922 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002923 * @param network The {@link Network} whose link properties have changed.
2924 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002925 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002926 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002927
Robert Greenwalt8d482522015-06-24 13:23:42 -07002928 /**
2929 * Called when the network the framework connected to for this request
Chalard Jean804b8fb2018-01-30 22:41:41 +09002930 * goes into {@link NetworkInfo.State#SUSPENDED}.
Robert Greenwalt8d482522015-06-24 13:23:42 -07002931 * This generally means that while the TCP connections are still live,
2932 * temporarily network data fails to transfer. Specifically this is used
2933 * on cellular networks to mask temporary outages when driving through
2934 * a tunnel, etc.
2935 * @hide
2936 */
2937 public void onNetworkSuspended(Network network) {}
2938
2939 /**
2940 * Called when the network the framework connected to for this request
Chalard Jean804b8fb2018-01-30 22:41:41 +09002941 * returns from a {@link NetworkInfo.State#SUSPENDED} state. This should always be
2942 * preceded by a matching {@link NetworkCallback#onNetworkSuspended} call.
Robert Greenwalt8d482522015-06-24 13:23:42 -07002943 * @hide
2944 */
2945 public void onNetworkResumed(Network network) {}
2946
junyulai05986c62018-08-07 19:50:45 +08002947 /**
2948 * Called when access to the specified network is blocked or unblocked.
2949 *
2950 * @param network The {@link Network} whose blocked status has changed.
2951 * @param blocked The blocked status of this {@link Network}.
2952 */
2953 public void onBlockedStatusChanged(Network network, boolean blocked) {}
2954
Robert Greenwalt6078b502014-06-11 16:05:07 -07002955 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002956 }
2957
Hugo Benichicb883232017-05-11 13:16:17 +09002958 /**
2959 * Constant error codes used by ConnectivityService to communicate about failures and errors
2960 * across a Binder boundary.
2961 * @hide
2962 */
2963 public interface Errors {
Chalard Jean4d660112018-06-04 16:52:49 +09002964 int TOO_MANY_REQUESTS = 1;
Hugo Benichicb883232017-05-11 13:16:17 +09002965 }
2966
2967 /** @hide */
2968 public static class TooManyRequestsException extends RuntimeException {}
2969
2970 private static RuntimeException convertServiceException(ServiceSpecificException e) {
2971 switch (e.errorCode) {
2972 case Errors.TOO_MANY_REQUESTS:
2973 return new TooManyRequestsException();
2974 default:
2975 Log.w(TAG, "Unknown service error code " + e.errorCode);
2976 return new RuntimeException(e);
2977 }
2978 }
2979
Robert Greenwalt9258c642014-03-26 16:47:06 -07002980 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002981 /** @hide */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002982 public static final int CALLBACK_PRECHECK = BASE + 1;
2983 /** @hide */
2984 public static final int CALLBACK_AVAILABLE = BASE + 2;
2985 /** @hide arg1 = TTL */
2986 public static final int CALLBACK_LOSING = BASE + 3;
2987 /** @hide */
2988 public static final int CALLBACK_LOST = BASE + 4;
2989 /** @hide */
2990 public static final int CALLBACK_UNAVAIL = BASE + 5;
2991 /** @hide */
2992 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2993 /** @hide */
2994 public static final int CALLBACK_IP_CHANGED = BASE + 7;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002995 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Hugo Benichidba33db2017-03-23 22:40:44 +09002996 private static final int EXPIRE_LEGACY_REQUEST = BASE + 8;
Robert Greenwalt8d482522015-06-24 13:23:42 -07002997 /** @hide */
Hugo Benichidba33db2017-03-23 22:40:44 +09002998 public static final int CALLBACK_SUSPENDED = BASE + 9;
Robert Greenwalt8d482522015-06-24 13:23:42 -07002999 /** @hide */
Hugo Benichidba33db2017-03-23 22:40:44 +09003000 public static final int CALLBACK_RESUMED = BASE + 10;
junyulai05986c62018-08-07 19:50:45 +08003001 /** @hide */
3002 public static final int CALLBACK_BLK_CHANGED = BASE + 11;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003003
Erik Kline57faba92015-11-25 12:49:38 +09003004 /** @hide */
3005 public static String getCallbackName(int whichCallback) {
3006 switch (whichCallback) {
3007 case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK";
3008 case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE";
3009 case CALLBACK_LOSING: return "CALLBACK_LOSING";
3010 case CALLBACK_LOST: return "CALLBACK_LOST";
3011 case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL";
3012 case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED";
3013 case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED";
Erik Kline57faba92015-11-25 12:49:38 +09003014 case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST";
3015 case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED";
3016 case CALLBACK_RESUMED: return "CALLBACK_RESUMED";
junyulai05986c62018-08-07 19:50:45 +08003017 case CALLBACK_BLK_CHANGED: return "CALLBACK_BLK_CHANGED";
Erik Kline57faba92015-11-25 12:49:38 +09003018 default:
3019 return Integer.toString(whichCallback);
3020 }
3021 }
3022
Robert Greenwalt562cc542014-05-15 18:07:26 -07003023 private class CallbackHandler extends Handler {
Robert Greenwalt9258c642014-03-26 16:47:06 -07003024 private static final String TAG = "ConnectivityManager.CallbackHandler";
Robert Greenwalta9ebeef2015-09-03 16:41:45 -07003025 private static final boolean DBG = false;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003026
Hugo Benichid42650f2016-07-06 22:53:17 +09003027 CallbackHandler(Looper looper) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07003028 super(looper);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003029 }
3030
Hugo Benichi2583ef02017-02-02 17:02:36 +09003031 CallbackHandler(Handler handler) {
Hugo Benichie7678512017-05-09 15:19:01 +09003032 this(Preconditions.checkNotNull(handler, "Handler cannot be null.").getLooper());
Hugo Benichi2583ef02017-02-02 17:02:36 +09003033 }
3034
Robert Greenwalt9258c642014-03-26 16:47:06 -07003035 @Override
3036 public void handleMessage(Message message) {
Hugo Benichi2c684522017-05-09 14:36:02 +09003037 if (message.what == EXPIRE_LEGACY_REQUEST) {
3038 expireRequest((NetworkCapabilities) message.obj, message.arg1);
3039 return;
3040 }
3041
3042 final NetworkRequest request = getObject(message, NetworkRequest.class);
3043 final Network network = getObject(message, Network.class);
3044 final NetworkCallback callback;
3045 synchronized (sCallbacks) {
3046 callback = sCallbacks.get(request);
3047 }
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09003048 if (DBG) {
Hugo Benichia0385682017-03-22 17:07:57 +09003049 Log.d(TAG, getCallbackName(message.what) + " for network " + network);
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09003050 }
Hugo Benichi2c684522017-05-09 14:36:02 +09003051 if (callback == null) {
3052 Log.w(TAG, "callback not found for " + getCallbackName(message.what) + " message");
3053 return;
3054 }
3055
Robert Greenwalt9258c642014-03-26 16:47:06 -07003056 switch (message.what) {
3057 case CALLBACK_PRECHECK: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003058 callback.onPreCheck(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003059 break;
3060 }
3061 case CALLBACK_AVAILABLE: {
Chalard Jean804b8fb2018-01-30 22:41:41 +09003062 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
3063 LinkProperties lp = getObject(message, LinkProperties.class);
junyulai05986c62018-08-07 19:50:45 +08003064 callback.onAvailable(network, cap, lp, message.arg1 != 0);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003065 break;
3066 }
3067 case CALLBACK_LOSING: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003068 callback.onLosing(network, message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003069 break;
3070 }
3071 case CALLBACK_LOST: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003072 callback.onLost(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003073 break;
3074 }
3075 case CALLBACK_UNAVAIL: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003076 callback.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07003077 break;
3078 }
3079 case CALLBACK_CAP_CHANGED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003080 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
3081 callback.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003082 break;
3083 }
3084 case CALLBACK_IP_CHANGED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003085 LinkProperties lp = getObject(message, LinkProperties.class);
3086 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003087 break;
3088 }
Robert Greenwalt8d482522015-06-24 13:23:42 -07003089 case CALLBACK_SUSPENDED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003090 callback.onNetworkSuspended(network);
Robert Greenwalt8d482522015-06-24 13:23:42 -07003091 break;
3092 }
3093 case CALLBACK_RESUMED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003094 callback.onNetworkResumed(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07003095 break;
3096 }
junyulai05986c62018-08-07 19:50:45 +08003097 case CALLBACK_BLK_CHANGED: {
3098 boolean blocked = message.arg1 != 0;
3099 callback.onBlockedStatusChanged(network, blocked);
3100 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003101 }
3102 }
3103
Hugo Benichid42650f2016-07-06 22:53:17 +09003104 private <T> T getObject(Message msg, Class<T> c) {
3105 return (T) msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07003106 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003107 }
3108
Hugo Benichi2583ef02017-02-02 17:02:36 +09003109 private CallbackHandler getDefaultHandler() {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003110 synchronized (sCallbacks) {
3111 if (sCallbackHandler == null) {
3112 sCallbackHandler = new CallbackHandler(ConnectivityThread.getInstanceLooper());
Robert Greenwalt9258c642014-03-26 16:47:06 -07003113 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003114 return sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003115 }
3116 }
3117
Hugo Benichi6f260f32017-02-03 14:18:44 +09003118 private static final HashMap<NetworkRequest, NetworkCallback> sCallbacks = new HashMap<>();
3119 private static CallbackHandler sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003120
Hugo Benichi6f260f32017-02-03 14:18:44 +09003121 private static final int LISTEN = 1;
3122 private static final int REQUEST = 2;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003123
Hugo Benichi6f260f32017-02-03 14:18:44 +09003124 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, NetworkCallback callback,
3125 int timeoutMs, int action, int legacyType, CallbackHandler handler) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09003126 printStackTrace();
Hugo Benichie7678512017-05-09 15:19:01 +09003127 checkCallbackNotNull(callback);
Hugo Benichidafed3d2017-03-06 09:17:06 +09003128 Preconditions.checkArgument(action == REQUEST || need != null, "null NetworkCapabilities");
Hugo Benichid42650f2016-07-06 22:53:17 +09003129 final NetworkRequest request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003130 try {
Hugo Benichid42650f2016-07-06 22:53:17 +09003131 synchronized(sCallbacks) {
Hugo Benichi31c176d2017-06-17 13:14:12 +09003132 if (callback.networkRequest != null
3133 && callback.networkRequest != ALREADY_UNREGISTERED) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09003134 // TODO: throw exception instead and enforce 1:1 mapping of callbacks
3135 // and requests (http://b/20701525).
3136 Log.e(TAG, "NetworkCallback was already registered");
3137 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003138 Messenger messenger = new Messenger(handler);
Hugo Benichid42650f2016-07-06 22:53:17 +09003139 Binder binder = new Binder();
Paul Jensen7221cc32014-06-27 11:05:32 -04003140 if (action == LISTEN) {
Hugo Benichid42650f2016-07-06 22:53:17 +09003141 request = mService.listenForNetwork(need, messenger, binder);
Paul Jensen7221cc32014-06-27 11:05:32 -04003142 } else {
Hugo Benichid42650f2016-07-06 22:53:17 +09003143 request = mService.requestNetwork(
3144 need, messenger, timeoutMs, binder, legacyType);
Paul Jensen7221cc32014-06-27 11:05:32 -04003145 }
Hugo Benichid42650f2016-07-06 22:53:17 +09003146 if (request != null) {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003147 sCallbacks.put(request, callback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003148 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003149 callback.networkRequest = request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003150 }
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003151 } catch (RemoteException e) {
3152 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003153 } catch (ServiceSpecificException e) {
3154 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003155 }
Hugo Benichid42650f2016-07-06 22:53:17 +09003156 return request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003157 }
3158
3159 /**
Erik Klinea2d29402016-03-16 15:31:39 +09003160 * Helper function to request a network with a particular legacy type.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09003161 *
3162 * This is temporarily public @hide so it can be called by system code that uses the
3163 * NetworkRequest API to request networks but relies on CONNECTIVITY_ACTION broadcasts for
3164 * instead network notifications.
3165 *
3166 * TODO: update said system code to rely on NetworkCallbacks and make this method private.
3167 *
3168 * @hide
3169 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003170 public void requestNetwork(@NonNull NetworkRequest request,
3171 @NonNull NetworkCallback networkCallback, int timeoutMs, int legacyType,
3172 @NonNull Handler handler) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003173 CallbackHandler cbHandler = new CallbackHandler(handler);
3174 NetworkCapabilities nc = request.networkCapabilities;
3175 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, legacyType, cbHandler);
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09003176 }
3177
3178 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09003179 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003180 *
3181 * This {@link NetworkRequest} will live until released via
Etan Cohenaebf17e2017-03-01 12:47:28 -08003182 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A
3183 * version of the method which takes a timeout is
Hugo Benichi0eec03f2017-05-15 15:15:33 +09003184 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003185 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07003186 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003187 * can be used to direct traffic to the network.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003188 * <p>It is presently unsupported to request a network with mutable
3189 * {@link NetworkCapabilities} such as
3190 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3191 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3192 * as these {@code NetworkCapabilities} represent states that a particular
3193 * network may never attain, and whether a network will attain these states
3194 * is unknown prior to bringing up the network so the framework does not
3195 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003196 *
3197 * <p>This method requires the caller to hold either the
3198 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3199 * or the ability to modify system settings as determined by
3200 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt9258c642014-03-26 16:47:06 -07003201 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003202 * @param request {@link NetworkRequest} describing this request.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003203 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3204 * the callback must not be shared - it uniquely specifies this request.
3205 * The callback is invoked on the default internal Handler.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003206 * @throws IllegalArgumentException if {@code request} specifies any mutable
3207 * {@code NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003208 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003209 public void requestNetwork(@NonNull NetworkRequest request,
3210 @NonNull NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003211 requestNetwork(request, networkCallback, getDefaultHandler());
3212 }
3213
3214 /**
3215 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
3216 *
3217 * This {@link NetworkRequest} will live until released via
Etan Cohenaebf17e2017-03-01 12:47:28 -08003218 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A
3219 * version of the method which takes a timeout is
Hugo Benichi0eec03f2017-05-15 15:15:33 +09003220 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003221 * Status of the request can be followed by listening to the various
3222 * callbacks described in {@link NetworkCallback}. The {@link Network}
3223 * can be used to direct traffic to the network.
3224 * <p>It is presently unsupported to request a network with mutable
3225 * {@link NetworkCapabilities} such as
3226 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3227 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3228 * as these {@code NetworkCapabilities} represent states that a particular
3229 * network may never attain, and whether a network will attain these states
3230 * is unknown prior to bringing up the network so the framework does not
Chalard Jean4d660112018-06-04 16:52:49 +09003231 * know how to go about satisfying a request with these capabilities.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003232 *
3233 * <p>This method requires the caller to hold either the
3234 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3235 * or the ability to modify system settings as determined by
3236 * {@link android.provider.Settings.System#canWrite}.</p>
3237 *
3238 * @param request {@link NetworkRequest} describing this request.
3239 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3240 * the callback must not be shared - it uniquely specifies this request.
3241 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
3242 * @throws IllegalArgumentException if {@code request} specifies any mutable
3243 * {@code NetworkCapabilities}.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003244 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003245 public void requestNetwork(@NonNull NetworkRequest request,
3246 @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003247 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
3248 CallbackHandler cbHandler = new CallbackHandler(handler);
3249 requestNetwork(request, networkCallback, 0, legacyType, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003250 }
3251
3252 /**
Etan Cohenaebf17e2017-03-01 12:47:28 -08003253 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
3254 * by a timeout.
3255 *
3256 * This function behaves identically to the non-timed-out version
3257 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network
3258 * is not found within the given time (in milliseconds) the
3259 * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be
3260 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3261 * not have to be released if timed-out (it is automatically released). Unregistering a
3262 * request that timed out is not an error.
3263 *
3264 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3265 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3266 * for that purpose. Calling this method will attempt to bring up the requested network.
3267 *
3268 * <p>This method requires the caller to hold either the
3269 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3270 * or the ability to modify system settings as determined by
3271 * {@link android.provider.Settings.System#canWrite}.</p>
3272 *
3273 * @param request {@link NetworkRequest} describing this request.
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003274 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3275 * the callback must not be shared - it uniquely specifies this request.
Etan Cohenaebf17e2017-03-01 12:47:28 -08003276 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3277 * before {@link NetworkCallback#onUnavailable()} is called. The timeout must
3278 * be a positive value (i.e. >0).
Etan Cohenaebf17e2017-03-01 12:47:28 -08003279 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003280 public void requestNetwork(@NonNull NetworkRequest request,
3281 @NonNull NetworkCallback networkCallback, int timeoutMs) {
Hugo Benichie7678512017-05-09 15:19:01 +09003282 checkTimeout(timeoutMs);
Hugo Benichi2583ef02017-02-02 17:02:36 +09003283 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
Erik Klineb583b032017-02-22 12:58:24 +09003284 requestNetwork(request, networkCallback, timeoutMs, legacyType, getDefaultHandler());
Hugo Benichi2583ef02017-02-02 17:02:36 +09003285 }
3286
Hugo Benichi2583ef02017-02-02 17:02:36 +09003287 /**
3288 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
3289 * by a timeout.
3290 *
Chalard Jean4d660112018-06-04 16:52:49 +09003291 * This function behaves identically to the version without timeout, but if a suitable
Hugo Benichi2583ef02017-02-02 17:02:36 +09003292 * network is not found within the given time (in milliseconds) the
Etan Cohenaebf17e2017-03-01 12:47:28 -08003293 * {@link NetworkCallback#onUnavailable} callback is called. The request can still be
3294 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3295 * not have to be released if timed-out (it is automatically released). Unregistering a
3296 * request that timed out is not an error.
3297 *
3298 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3299 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3300 * for that purpose. Calling this method will attempt to bring up the requested network.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003301 *
3302 * <p>This method requires the caller to hold either the
3303 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3304 * or the ability to modify system settings as determined by
3305 * {@link android.provider.Settings.System#canWrite}.</p>
3306 *
3307 * @param request {@link NetworkRequest} describing this request.
Etan Cohenaebf17e2017-03-01 12:47:28 -08003308 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3309 * the callback must not be shared - it uniquely specifies this request.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003310 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003311 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3312 * before {@link NetworkCallback#onUnavailable} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003313 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003314 public void requestNetwork(@NonNull NetworkRequest request,
3315 @NonNull NetworkCallback networkCallback, @NonNull Handler handler, int timeoutMs) {
Hugo Benichie7678512017-05-09 15:19:01 +09003316 checkTimeout(timeoutMs);
Hugo Benichi2583ef02017-02-02 17:02:36 +09003317 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
3318 CallbackHandler cbHandler = new CallbackHandler(handler);
3319 requestNetwork(request, networkCallback, timeoutMs, legacyType, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003320 }
3321
3322 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003323 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003324 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003325 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfcde58f2015-02-11 16:51:13 -08003326 * <p>
Paul Jensen72db88e2015-03-10 10:54:12 -04003327 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
3328 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003329 */
Erik Kline90e93072014-11-19 12:12:24 +09003330 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003331
3332 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07003333 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003334 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003335 * {@link android.content.Intent#getParcelableExtra(String)}.
3336 */
Erik Kline90e93072014-11-19 12:12:24 +09003337 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003338
3339
3340 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09003341 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003342 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003343 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07003344 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003345 * the request may outlive the calling application and get called back when a suitable
3346 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003347 * <p>
3348 * The operation is an Intent broadcast that goes to a broadcast receiver that
3349 * you registered with {@link Context#registerReceiver} or through the
3350 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3351 * <p>
3352 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09003353 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3354 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07003355 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07003356 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07003357 * Intent to reserve the network or it will be released shortly after the Intent
3358 * is processed.
3359 * <p>
Paul Jensen694f2b82015-06-17 14:15:39 -04003360 * If there is already a request for this Intent registered (with the equality of
Robert Greenwalt9258c642014-03-26 16:47:06 -07003361 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003362 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003363 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003364 * The request may be released normally by calling
3365 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003366 * <p>It is presently unsupported to request a network with either
3367 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3368 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3369 * as these {@code NetworkCapabilities} represent states that a particular
3370 * network may never attain, and whether a network will attain these states
3371 * is unknown prior to bringing up the network so the framework does not
Chalard Jean4d660112018-06-04 16:52:49 +09003372 * know how to go about satisfying a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003373 *
3374 * <p>This method requires the caller to hold either the
3375 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3376 * or the ability to modify system settings as determined by
3377 * {@link android.provider.Settings.System#canWrite}.</p>
3378 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003379 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003380 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07003381 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003382 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003383 * @throws IllegalArgumentException if {@code request} contains either
3384 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3385 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003386 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003387 public void requestNetwork(@NonNull NetworkRequest request,
3388 @NonNull PendingIntent operation) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09003389 printStackTrace();
Hugo Benichie7678512017-05-09 15:19:01 +09003390 checkPendingIntentNotNull(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003391 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07003392 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003393 } catch (RemoteException e) {
3394 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003395 } catch (ServiceSpecificException e) {
3396 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003397 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003398 }
3399
3400 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003401 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
3402 * <p>
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003403 * This method has the same behavior as
3404 * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003405 * releasing network resources and disconnecting.
3406 *
3407 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3408 * PendingIntent passed to
3409 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
3410 * corresponding NetworkRequest you'd like to remove. Cannot be null.
3411 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003412 public void releaseNetworkRequest(@NonNull PendingIntent operation) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09003413 printStackTrace();
Hugo Benichie7678512017-05-09 15:19:01 +09003414 checkPendingIntentNotNull(operation);
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003415 try {
3416 mService.releasePendingNetworkRequest(operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003417 } catch (RemoteException e) {
3418 throw e.rethrowFromSystemServer();
3419 }
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003420 }
3421
Hugo Benichie7678512017-05-09 15:19:01 +09003422 private static void checkPendingIntentNotNull(PendingIntent intent) {
3423 Preconditions.checkNotNull(intent, "PendingIntent cannot be null.");
3424 }
3425
3426 private static void checkCallbackNotNull(NetworkCallback callback) {
3427 Preconditions.checkNotNull(callback, "null NetworkCallback");
3428 }
3429
3430 private static void checkTimeout(int timeoutMs) {
3431 Preconditions.checkArgumentPositive(timeoutMs, "timeoutMs must be strictly positive.");
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003432 }
3433
3434 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07003435 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07003436 * {@link NetworkRequest}. The callbacks will continue to be called until
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003437 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003438 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003439 * @param request {@link NetworkRequest} describing this request.
3440 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3441 * networks change state.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003442 * The callback is invoked on the default internal Handler.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003443 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003444 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09003445 public void registerNetworkCallback(@NonNull NetworkRequest request,
3446 @NonNull NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003447 registerNetworkCallback(request, networkCallback, getDefaultHandler());
3448 }
3449
3450 /**
3451 * Registers to receive notifications about all networks which satisfy the given
3452 * {@link NetworkRequest}. The callbacks will continue to be called until
3453 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003454 *
3455 * @param request {@link NetworkRequest} describing this request.
3456 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3457 * networks change state.
3458 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003459 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003460 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09003461 public void registerNetworkCallback(@NonNull NetworkRequest request,
3462 @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003463 CallbackHandler cbHandler = new CallbackHandler(handler);
3464 NetworkCapabilities nc = request.networkCapabilities;
3465 sendRequestForNetwork(nc, networkCallback, 0, LISTEN, TYPE_NONE, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003466 }
3467
3468 /**
Paul Jensen694f2b82015-06-17 14:15:39 -04003469 * Registers a PendingIntent to be sent when a network is available which satisfies the given
3470 * {@link NetworkRequest}.
3471 *
3472 * This function behaves identically to the version that takes a NetworkCallback, but instead
3473 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
3474 * the request may outlive the calling application and get called back when a suitable
3475 * network is found.
3476 * <p>
3477 * The operation is an Intent broadcast that goes to a broadcast receiver that
3478 * you registered with {@link Context#registerReceiver} or through the
3479 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3480 * <p>
3481 * The operation Intent is delivered with two extras, a {@link Network} typed
3482 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3483 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
3484 * the original requests parameters.
3485 * <p>
3486 * If there is already a request for this Intent registered (with the equality of
3487 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
3488 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
3489 * <p>
3490 * The request may be released normally by calling
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003491 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Paul Jensen694f2b82015-06-17 14:15:39 -04003492 * @param request {@link NetworkRequest} describing this request.
3493 * @param operation Action to perform when the network is available (corresponds
3494 * to the {@link NetworkCallback#onAvailable} call. Typically
3495 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
3496 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003497 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09003498 public void registerNetworkCallback(@NonNull NetworkRequest request,
3499 @NonNull PendingIntent operation) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09003500 printStackTrace();
Hugo Benichie7678512017-05-09 15:19:01 +09003501 checkPendingIntentNotNull(operation);
Paul Jensen694f2b82015-06-17 14:15:39 -04003502 try {
3503 mService.pendingListenForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003504 } catch (RemoteException e) {
3505 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003506 } catch (ServiceSpecificException e) {
3507 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003508 }
Paul Jensen694f2b82015-06-17 14:15:39 -04003509 }
3510
3511 /**
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003512 * Registers to receive notifications about changes in the system default network. The callbacks
3513 * will continue to be called until either the application exits or
3514 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Erik Klinea2d29402016-03-16 15:31:39 +09003515 *
3516 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3517 * system default network changes.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003518 * The callback is invoked on the default internal Handler.
Erik Klinea2d29402016-03-16 15:31:39 +09003519 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003520 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09003521 public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003522 registerDefaultNetworkCallback(networkCallback, getDefaultHandler());
3523 }
3524
3525 /**
3526 * Registers to receive notifications about changes in the system default network. The callbacks
3527 * will continue to be called until either the application exits or
3528 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003529 *
3530 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3531 * system default network changes.
3532 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003533 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003534 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09003535 public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback,
3536 @NonNull Handler handler) {
Erik Klinea2d29402016-03-16 15:31:39 +09003537 // This works because if the NetworkCapabilities are null,
3538 // ConnectivityService takes them from the default request.
3539 //
3540 // Since the capabilities are exactly the same as the default request's
3541 // capabilities, this request is guaranteed, at all times, to be
3542 // satisfied by the same network, if any, that satisfies the default
3543 // request, i.e., the system default network.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003544 CallbackHandler cbHandler = new CallbackHandler(handler);
Chalard Jean4d660112018-06-04 16:52:49 +09003545 sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0,
3546 REQUEST, TYPE_NONE, cbHandler);
Erik Klinea2d29402016-03-16 15:31:39 +09003547 }
3548
3549 /**
fengludb571472015-04-21 17:12:05 -07003550 * Requests bandwidth update for a given {@link Network} and returns whether the update request
3551 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
3552 * network connection for updated bandwidth information. The caller will be notified via
3553 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003554 * method assumes that the caller has previously called
3555 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network
3556 * changes.
fenglub15e72b2015-03-20 11:29:56 -07003557 *
fengluae519192015-04-27 14:28:04 -07003558 * @param network {@link Network} specifying which network you're interested.
fengludb571472015-04-21 17:12:05 -07003559 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglub15e72b2015-03-20 11:29:56 -07003560 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003561 public boolean requestBandwidthUpdate(@NonNull Network network) {
fenglub15e72b2015-03-20 11:29:56 -07003562 try {
fengludb571472015-04-21 17:12:05 -07003563 return mService.requestBandwidthUpdate(network);
fenglub15e72b2015-03-20 11:29:56 -07003564 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003565 throw e.rethrowFromSystemServer();
fenglub15e72b2015-03-20 11:29:56 -07003566 }
3567 }
3568
3569 /**
Hugo Benichidafed3d2017-03-06 09:17:06 +09003570 * Unregisters a {@code NetworkCallback} and possibly releases networks originating from
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003571 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and
3572 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls.
3573 * If the given {@code NetworkCallback} had previously been used with
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09003574 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
3575 * will be disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003576 *
Hugo Benichidafed3d2017-03-06 09:17:06 +09003577 * Notifications that would have triggered that {@code NetworkCallback} will immediately stop
3578 * triggering it as soon as this call returns.
3579 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003580 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003581 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003582 public void unregisterNetworkCallback(@NonNull NetworkCallback networkCallback) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09003583 printStackTrace();
Hugo Benichie7678512017-05-09 15:19:01 +09003584 checkCallbackNotNull(networkCallback);
Hugo Benichidafed3d2017-03-06 09:17:06 +09003585 final List<NetworkRequest> reqs = new ArrayList<>();
3586 // Find all requests associated to this callback and stop callback triggers immediately.
3587 // Callback is reusable immediately. http://b/20701525, http://b/35921499.
3588 synchronized (sCallbacks) {
Hugo Benichi31c176d2017-06-17 13:14:12 +09003589 Preconditions.checkArgument(networkCallback.networkRequest != null,
3590 "NetworkCallback was not registered");
3591 Preconditions.checkArgument(networkCallback.networkRequest != ALREADY_UNREGISTERED,
3592 "NetworkCallback was already unregistered");
Hugo Benichidafed3d2017-03-06 09:17:06 +09003593 for (Map.Entry<NetworkRequest, NetworkCallback> e : sCallbacks.entrySet()) {
3594 if (e.getValue() == networkCallback) {
3595 reqs.add(e.getKey());
3596 }
3597 }
3598 // TODO: throw exception if callback was registered more than once (http://b/20701525).
3599 for (NetworkRequest r : reqs) {
3600 try {
3601 mService.releaseNetworkRequest(r);
3602 } catch (RemoteException e) {
3603 throw e.rethrowFromSystemServer();
3604 }
3605 // Only remove mapping if rpc was successful.
3606 sCallbacks.remove(r);
3607 }
Hugo Benichi31c176d2017-06-17 13:14:12 +09003608 networkCallback.networkRequest = ALREADY_UNREGISTERED;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003609 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003610 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003611
3612 /**
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003613 * Unregisters a callback previously registered via
3614 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3615 *
3616 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3617 * PendingIntent passed to
3618 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3619 * Cannot be null.
3620 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003621 public void unregisterNetworkCallback(@NonNull PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003622 checkPendingIntentNotNull(operation);
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003623 releaseNetworkRequest(operation);
3624 }
3625
3626 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003627 * Informs the system whether it should switch to {@code network} regardless of whether it is
3628 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
3629 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
3630 * the system default network regardless of any other network that's currently connected. If
3631 * {@code always} is true, then the choice is remembered, so that the next time the user
3632 * connects to this network, the system will switch to it.
3633 *
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003634 * @param network The network to accept.
3635 * @param accept Whether to accept the network even if unvalidated.
3636 * @param always Whether to remember this choice in the future.
3637 *
3638 * @hide
3639 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003640 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003641 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
3642 try {
3643 mService.setAcceptUnvalidated(network, accept, always);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003644 } catch (RemoteException e) {
3645 throw e.rethrowFromSystemServer();
3646 }
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003647 }
3648
3649 /**
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003650 * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is
3651 * only meaningful if the system is configured not to penalize such networks, e.g., if the
3652 * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code
3653 * NETWORK_AVOID_BAD_WIFI setting is unset}.
3654 *
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003655 * @param network The network to accept.
3656 *
3657 * @hide
3658 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003659 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003660 public void setAvoidUnvalidated(Network network) {
3661 try {
3662 mService.setAvoidUnvalidated(network);
3663 } catch (RemoteException e) {
3664 throw e.rethrowFromSystemServer();
3665 }
3666 }
3667
3668 /**
Lorenzo Colitti4734cdb2017-04-27 14:30:21 +09003669 * Requests that the system open the captive portal app on the specified network.
3670 *
3671 * @param network The network to log into.
3672 *
3673 * @hide
3674 */
3675 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
3676 public void startCaptivePortalApp(Network network) {
3677 try {
3678 mService.startCaptivePortalApp(network);
3679 } catch (RemoteException e) {
3680 throw e.rethrowFromSystemServer();
3681 }
3682 }
3683
3684 /**
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003685 * It is acceptable to briefly use multipath data to provide seamless connectivity for
3686 * time-sensitive user-facing operations when the system default network is temporarily
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003687 * unresponsive. The amount of data should be limited (less than one megabyte for every call to
3688 * this method), and the operation should be infrequent to ensure that data usage is limited.
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003689 *
3690 * An example of such an operation might be a time-sensitive foreground activity, such as a
3691 * voice command, that the user is performing while walking out of range of a Wi-Fi network.
3692 */
3693 public static final int MULTIPATH_PREFERENCE_HANDOVER = 1 << 0;
3694
3695 /**
3696 * It is acceptable to use small amounts of multipath data on an ongoing basis to provide
3697 * a backup channel for traffic that is primarily going over another network.
3698 *
3699 * An example might be maintaining backup connections to peers or servers for the purpose of
3700 * fast fallback if the default network is temporarily unresponsive or disconnects. The traffic
3701 * on backup paths should be negligible compared to the traffic on the main path.
3702 */
3703 public static final int MULTIPATH_PREFERENCE_RELIABILITY = 1 << 1;
3704
3705 /**
3706 * It is acceptable to use metered data to improve network latency and performance.
3707 */
3708 public static final int MULTIPATH_PREFERENCE_PERFORMANCE = 1 << 2;
3709
3710 /**
3711 * Return value to use for unmetered networks. On such networks we currently set all the flags
3712 * to true.
3713 * @hide
3714 */
3715 public static final int MULTIPATH_PREFERENCE_UNMETERED =
3716 MULTIPATH_PREFERENCE_HANDOVER |
3717 MULTIPATH_PREFERENCE_RELIABILITY |
3718 MULTIPATH_PREFERENCE_PERFORMANCE;
3719
3720 /** @hide */
3721 @Retention(RetentionPolicy.SOURCE)
3722 @IntDef(flag = true, value = {
3723 MULTIPATH_PREFERENCE_HANDOVER,
3724 MULTIPATH_PREFERENCE_RELIABILITY,
3725 MULTIPATH_PREFERENCE_PERFORMANCE,
3726 })
3727 public @interface MultipathPreference {
3728 }
3729
3730 /**
3731 * Provides a hint to the calling application on whether it is desirable to use the
3732 * multinetwork APIs (e.g., {@link Network#openConnection}, {@link Network#bindSocket}, etc.)
3733 * for multipath data transfer on this network when it is not the system default network.
3734 * Applications desiring to use multipath network protocols should call this method before
3735 * each such operation.
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003736 *
3737 * @param network The network on which the application desires to use multipath data.
3738 * If {@code null}, this method will return the a preference that will generally
3739 * apply to metered networks.
3740 * @return a bitwise OR of zero or more of the {@code MULTIPATH_PREFERENCE_*} constants.
3741 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003742 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09003743 public @MultipathPreference int getMultipathPreference(@Nullable Network network) {
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003744 try {
3745 return mService.getMultipathPreference(network);
3746 } catch (RemoteException e) {
3747 throw e.rethrowFromSystemServer();
3748 }
3749 }
3750
3751 /**
Stuart Scott984dc852015-03-30 13:17:11 -07003752 * Resets all connectivity manager settings back to factory defaults.
3753 * @hide
3754 */
3755 public void factoryReset() {
Stuart Scott984dc852015-03-30 13:17:11 -07003756 try {
Stuart Scottf1fb3972015-04-02 18:00:02 -07003757 mService.factoryReset();
Stuart Scott984dc852015-03-30 13:17:11 -07003758 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003759 throw e.rethrowFromSystemServer();
Stuart Scott984dc852015-03-30 13:17:11 -07003760 }
3761 }
3762
3763 /**
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003764 * Binds the current process to {@code network}. All Sockets created in the future
3765 * (and not explicitly bound via a bound SocketFactory from
3766 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3767 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3768 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3769 * work and all host name resolutions will fail. This is by design so an application doesn't
3770 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3771 * To clear binding pass {@code null} for {@code network}. Using individually bound
3772 * Sockets created by Network.getSocketFactory().createSocket() and
3773 * performing network-specific host name resolutions via
3774 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen72db88e2015-03-10 10:54:12 -04003775 * {@code bindProcessToNetwork}.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003776 *
3777 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3778 * the current binding.
3779 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3780 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003781 public boolean bindProcessToNetwork(@Nullable Network network) {
Chalard Jean4d660112018-06-04 16:52:49 +09003782 // Forcing callers to call through non-static function ensures ConnectivityManager
Paul Jensen72db88e2015-03-10 10:54:12 -04003783 // instantiated.
3784 return setProcessDefaultNetwork(network);
3785 }
3786
3787 /**
3788 * Binds the current process to {@code network}. All Sockets created in the future
3789 * (and not explicitly bound via a bound SocketFactory from
3790 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3791 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3792 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3793 * work and all host name resolutions will fail. This is by design so an application doesn't
3794 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3795 * To clear binding pass {@code null} for {@code network}. Using individually bound
3796 * Sockets created by Network.getSocketFactory().createSocket() and
3797 * performing network-specific host name resolutions via
3798 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
3799 * {@code setProcessDefaultNetwork}.
3800 *
3801 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3802 * the current binding.
3803 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3804 * @deprecated This function can throw {@link IllegalStateException}. Use
3805 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
3806 * is a direct replacement.
3807 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003808 @Deprecated
Chalard Jean50bea3d2019-01-07 19:26:34 +09003809 public static boolean setProcessDefaultNetwork(@Nullable Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003810 int netId = (network == null) ? NETID_UNSET : network.netId;
Paul Jensen72db88e2015-03-10 10:54:12 -04003811 if (netId == NetworkUtils.getBoundNetworkForProcess()) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003812 return true;
3813 }
3814 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05003815 // Set HTTP proxy system properties to match network.
3816 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colittiec4c5552015-04-22 11:52:48 +09003817 try {
3818 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
3819 } catch (SecurityException e) {
3820 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
3821 Log.e(TAG, "Can't set proxy properties", e);
3822 }
Paul Jensenc91b5342014-08-27 12:38:45 -04003823 // Must flush DNS cache as new network may have different DNS resolutions.
3824 InetAddress.clearDnsCache();
3825 // Must flush socket pool as idle sockets will be bound to previous network and may
3826 // cause subsequent fetches to be performed on old network.
3827 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
3828 return true;
3829 } else {
3830 return false;
3831 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003832 }
3833
3834 /**
3835 * Returns the {@link Network} currently bound to this process via
Paul Jensen72db88e2015-03-10 10:54:12 -04003836 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003837 *
3838 * @return {@code Network} to which this process is bound, or {@code null}.
3839 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003840 @Nullable
Paul Jensen72db88e2015-03-10 10:54:12 -04003841 public Network getBoundNetworkForProcess() {
3842 // Forcing callers to call thru non-static function ensures ConnectivityManager
3843 // instantiated.
3844 return getProcessDefaultNetwork();
3845 }
3846
3847 /**
3848 * Returns the {@link Network} currently bound to this process via
3849 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
3850 *
3851 * @return {@code Network} to which this process is bound, or {@code null}.
3852 * @deprecated Using this function can lead to other functions throwing
3853 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
3854 * {@code getBoundNetworkForProcess} is a direct replacement.
3855 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003856 @Deprecated
Chalard Jean50bea3d2019-01-07 19:26:34 +09003857 @Nullable
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003858 public static Network getProcessDefaultNetwork() {
Paul Jensen72db88e2015-03-10 10:54:12 -04003859 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04003860 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003861 return new Network(netId);
3862 }
3863
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003864 private void unsupportedStartingFrom(int version) {
3865 if (Process.myUid() == Process.SYSTEM_UID) {
Lorenzo Colitti2e31a7c2018-09-28 11:31:55 +09003866 // The getApplicationInfo() call we make below is not supported in system context. Let
3867 // the call through here, and rely on the fact that ConnectivityService will refuse to
3868 // allow the system to use these APIs anyway.
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003869 return;
3870 }
3871
3872 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
3873 throw new UnsupportedOperationException(
3874 "This method is not supported in target SDK version " + version + " and above");
3875 }
3876 }
3877
3878 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
3879 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
Lifu Tang30f95a72016-01-07 23:20:38 -08003880 // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003881 // remove these exemptions. Note that this check is not secure, and apps can still access these
3882 // functions by accessing ConnectivityService directly. However, it should be clear that doing
3883 // so is unsupported and may break in the future. http://b/22728205
3884 private void checkLegacyRoutingApiAccess() {
Dianne Hackborn692a2442015-07-31 10:35:34 -07003885 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003886 }
3887
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003888 /**
3889 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen72db88e2015-03-10 10:54:12 -04003890 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003891 *
3892 * @param network The {@link Network} to bind host resolutions from the current process to, or
3893 * {@code null} to clear the current binding.
3894 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3895 * @hide
3896 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
3897 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003898 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01003899 @UnsupportedAppUsage
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003900 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04003901 return NetworkUtils.bindProcessToNetworkForHostResolution(
Erik Klinef4fa9822018-04-27 22:48:33 +09003902 (network == null) ? NETID_UNSET : network.getNetIdForResolv());
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003903 }
Felipe Leme1b103232016-01-22 09:44:57 -08003904
3905 /**
3906 * Device is not restricting metered network activity while application is running on
3907 * background.
3908 */
3909 public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1;
3910
3911 /**
3912 * Device is restricting metered network activity while application is running on background,
3913 * but application is allowed to bypass it.
3914 * <p>
3915 * In this state, application should take action to mitigate metered network access.
3916 * For example, a music streaming application should switch to a low-bandwidth bitrate.
3917 */
3918 public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2;
3919
3920 /**
3921 * Device is restricting metered network activity while application is running on background.
Felipe Leme9778f762016-01-27 14:46:39 -08003922 * <p>
Felipe Leme1b103232016-01-22 09:44:57 -08003923 * In this state, application should not try to use the network while running on background,
3924 * because it would be denied.
3925 */
3926 public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3;
3927
Felipe Leme9778f762016-01-27 14:46:39 -08003928 /**
3929 * A change in the background metered network activity restriction has occurred.
3930 * <p>
3931 * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction
3932 * applies to them.
3933 * <p>
3934 * This is only sent to registered receivers, not manifest receivers.
3935 */
3936 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
3937 public static final String ACTION_RESTRICT_BACKGROUND_CHANGED =
3938 "android.net.conn.RESTRICT_BACKGROUND_CHANGED";
3939
Felipe Lemeecfccea2016-01-25 11:48:04 -08003940 /** @hide */
3941 @Retention(RetentionPolicy.SOURCE)
Felipe Leme1b103232016-01-22 09:44:57 -08003942 @IntDef(flag = false, value = {
3943 RESTRICT_BACKGROUND_STATUS_DISABLED,
3944 RESTRICT_BACKGROUND_STATUS_WHITELISTED,
3945 RESTRICT_BACKGROUND_STATUS_ENABLED,
3946 })
Felipe Leme1b103232016-01-22 09:44:57 -08003947 public @interface RestrictBackgroundStatus {
3948 }
3949
3950 private INetworkPolicyManager getNetworkPolicyManager() {
3951 synchronized (this) {
3952 if (mNPManager != null) {
3953 return mNPManager;
3954 }
3955 mNPManager = INetworkPolicyManager.Stub.asInterface(ServiceManager
3956 .getService(Context.NETWORK_POLICY_SERVICE));
3957 return mNPManager;
3958 }
3959 }
3960
3961 /**
3962 * Determines if the calling application is subject to metered network restrictions while
3963 * running on background.
Felipe Lemec9c7be52016-05-16 13:57:19 -07003964 *
3965 * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED},
3966 * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED},
3967 * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED}
Felipe Leme1b103232016-01-22 09:44:57 -08003968 */
3969 public @RestrictBackgroundStatus int getRestrictBackgroundStatus() {
3970 try {
3971 return getNetworkPolicyManager().getRestrictBackgroundByCaller();
3972 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003973 throw e.rethrowFromSystemServer();
Felipe Leme1b103232016-01-22 09:44:57 -08003974 }
3975 }
Ricky Wai44dcbde2018-01-23 04:09:45 +00003976
3977 /**
3978 * The network watchlist is a list of domains and IP addresses that are associated with
Ricky Waia86d5d52018-03-20 14:20:54 +00003979 * potentially harmful apps. This method returns the SHA-256 of the watchlist config file
3980 * currently used by the system for validation purposes.
Ricky Wai44dcbde2018-01-23 04:09:45 +00003981 *
3982 * @return Hash of network watchlist config file. Null if config does not exist.
3983 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003984 @Nullable
Ricky Wai44dcbde2018-01-23 04:09:45 +00003985 public byte[] getNetworkWatchlistConfigHash() {
3986 try {
3987 return mService.getNetworkWatchlistConfigHash();
3988 } catch (RemoteException e) {
3989 Log.e(TAG, "Unable to get watchlist config hash");
3990 throw e.rethrowFromSystemServer();
3991 }
3992 }
Jeff Vander Stoep0ac2c092018-07-23 10:57:53 -07003993
3994 /**
3995 * Returns the {@code uid} of the owner of a network connection.
3996 *
3997 * @param protocol The protocol of the connection. Only {@code IPPROTO_TCP} and
3998 * {@code IPPROTO_UDP} currently supported.
3999 * @param local The local {@link InetSocketAddress} of a connection.
4000 * @param remote The remote {@link InetSocketAddress} of a connection.
4001 *
4002 * @return {@code uid} if the connection is found and the app has permission to observe it
4003 * (e.g., if it is associated with the calling VPN app's tunnel) or
4004 * {@link android.os.Process#INVALID_UID} if the connection is not found.
4005 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09004006 public int getConnectionOwnerUid(int protocol, @NonNull InetSocketAddress local,
4007 @NonNull InetSocketAddress remote) {
Jeff Vander Stoep0ac2c092018-07-23 10:57:53 -07004008 ConnectionInfo connectionInfo = new ConnectionInfo(protocol, local, remote);
4009 try {
4010 return mService.getConnectionOwnerUid(connectionInfo);
4011 } catch (RemoteException e) {
4012 throw e.rethrowFromSystemServer();
4013 }
4014 }
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09004015
4016 private void printStackTrace() {
4017 if (DEBUG) {
4018 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
4019 final StringBuffer sb = new StringBuffer();
4020 for (int i = 3; i < callStack.length; i++) {
4021 final String stackTrace = callStack[i].toString();
4022 if (stackTrace == null || stackTrace.contains("android.os")) {
4023 break;
4024 }
4025 sb.append(" [").append(stackTrace).append("]");
4026 }
4027 Log.d(TAG, "StackLog:" + sb.toString());
4028 }
4029 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004030}