blob: 90eb13e5ee6fbb81ebb9fec437ece9507357d61a [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
junyulai48eac1d42018-12-27 17:25:29 +080018import android.annotation.CallbackExecutor;
Felipe Leme1b103232016-01-22 09:44:57 -080019import android.annotation.IntDef;
Chalard Jean50bea3d2019-01-07 19:26:34 +090020import android.annotation.NonNull;
Robin Lee244ce8e2016-01-05 18:03:46 +000021import android.annotation.Nullable;
Jeff Sharkey30e06bb2017-04-24 11:18:03 -060022import android.annotation.RequiresPermission;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.annotation.SdkConstant;
24import android.annotation.SdkConstant.SdkConstantType;
Udam Sainib7c24872016-01-04 12:16:14 -080025import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060026import android.annotation.SystemService;
Remi NGUYEN VAN7731c5b2019-01-17 14:38:31 +090027import android.annotation.TestApi;
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010028import android.annotation.UnsupportedAppUsage;
Robert Greenwalt9258c642014-03-26 16:47:06 -070029import android.app.PendingIntent;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070030import android.content.Context;
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070031import android.content.Intent;
junyulai48eac1d42018-12-27 17:25:29 +080032import android.net.IpSecManager.UdpEncapsulationSocket;
33import android.net.SocketKeepalive.Callback;
Robert Greenwalt42acef32009-08-12 16:08:25 -070034import android.os.Binder;
Mathew Inwood55418ea2018-12-20 15:30:45 +000035import android.os.Build;
Jeff Sharkey3a844fc2011-08-16 14:37:57 -070036import android.os.Build.VERSION_CODES;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080037import android.os.Bundle;
Robert Greenwalt9258c642014-03-26 16:47:06 -070038import android.os.Handler;
39import android.os.HandlerThread;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080040import android.os.IBinder;
41import android.os.INetworkActivityListener;
42import android.os.INetworkManagementService;
Robert Greenwalt9258c642014-03-26 16:47:06 -070043import android.os.Looper;
44import android.os.Message;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -070045import android.os.Messenger;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +090046import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047import android.os.RemoteException;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080048import android.os.ResultReceiver;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080049import android.os.ServiceManager;
Hugo Benichicb883232017-05-11 13:16:17 +090050import android.os.ServiceSpecificException;
Jeff Sharkey961e3042011-08-29 16:02:57 -070051import android.provider.Settings;
Wink Saville36ffb042014-12-05 11:10:30 -080052import android.telephony.SubscriptionManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080053import android.util.ArrayMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -070054import android.util.Log;
Erik Kline35bf06c2017-01-26 18:08:28 +090055import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080056
Robert Greenwaltafa05c02014-05-21 20:04:36 -070057import com.android.internal.telephony.ITelephony;
Robert Greenwalt562cc542014-05-15 18:07:26 -070058import com.android.internal.telephony.PhoneConstants;
Hugo Benichidafed3d2017-03-06 09:17:06 +090059import com.android.internal.util.Preconditions;
60import com.android.internal.util.Protocol;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070061
Paul Jensenc91b5342014-08-27 12:38:45 -040062import libcore.net.event.NetworkEventDispatcher;
63
Felipe Leme1b103232016-01-22 09:44:57 -080064import java.lang.annotation.Retention;
65import java.lang.annotation.RetentionPolicy;
Jeremy Kleind42209d2015-12-28 15:11:58 -080066import java.net.InetAddress;
Jeff Vander Stoep0ac2c092018-07-23 10:57:53 -070067import java.net.InetSocketAddress;
Hugo Benichidafed3d2017-03-06 09:17:06 +090068import java.util.ArrayList;
Jeremy Kleind42209d2015-12-28 15:11:58 -080069import java.util.HashMap;
Hugo Benichidafed3d2017-03-06 09:17:06 +090070import java.util.List;
71import java.util.Map;
junyulai48eac1d42018-12-27 17:25:29 +080072import java.util.concurrent.Executor;
Jeremy Kleind42209d2015-12-28 15:11:58 -080073
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074/**
75 * Class that answers queries about the state of network connectivity. It also
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060076 * notifies applications when network connectivity changes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080077 * <p>
78 * The primary responsibilities of this class are to:
79 * <ol>
80 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
81 * <li>Send broadcast intents when network connectivity changes</li>
82 * <li>Attempt to "fail over" to another network when connectivity to a network
83 * is lost</li>
84 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
85 * state of the available networks</li>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070086 * <li>Provide an API that allows applications to request and select networks for their data
87 * traffic</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 * </ol>
89 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060090@SystemService(Context.CONNECTIVITY_SERVICE)
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070091public class ConnectivityManager {
92 private static final String TAG = "ConnectivityManager";
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +090093 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070094
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070096 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 * been established or lost. The NetworkInfo for the affected network is
98 * sent as an extra; it should be consulted to see what kind of
99 * connectivity event occurred.
100 * <p/>
Mark Lu33ec1062016-12-05 10:57:55 -0800101 * Apps targeting Android 7.0 (API level 24) and higher do not receive this
102 * broadcast if they declare the broadcast receiver in their manifest. Apps
103 * will still receive broadcasts if they register their
104 * {@link android.content.BroadcastReceiver} with
105 * {@link android.content.Context#registerReceiver Context.registerReceiver()}
106 * and that context is still valid.
107 * <p/>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 * If this is a connection that was the result of failing over from a
109 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
110 * set to true.
111 * <p/>
112 * For a loss of connectivity, if the connectivity manager is attempting
113 * to connect (or has already connected) to another network, the
114 * NetworkInfo for the new network is also passed as an extra. This lets
115 * any receivers of the broadcast know that they should not necessarily
116 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800117 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 * the failover attempt succeeded (and so there is still overall data
119 * connectivity), or that the failover attempt failed, meaning that all
120 * connectivity has been lost.
121 * <p/>
122 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
123 * is set to {@code true} if there are no connected networks at all.
Chalard Jean054cd162018-02-10 05:33:50 +0900124 *
125 * @deprecated apps should use the more versatile {@link #requestNetwork},
126 * {@link #registerNetworkCallback} or {@link #registerDefaultNetworkCallback}
127 * functions instead for faster and more detailed updates about the network
128 * changes they care about.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800129 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800130 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jean054cd162018-02-10 05:33:50 +0900131 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700133
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800134 /**
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700135 * A temporary hack until SUPL system can get off the legacy APIS.
136 * They do too many network requests and the long list of apps listening
Chalard Jean4d660112018-06-04 16:52:49 +0900137 * and waking due to the CONNECTIVITY_ACTION broadcast makes it expensive.
138 * Use this broadcast intent instead for SUPL requests.
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700139 * @hide
140 */
141 public static final String CONNECTIVITY_ACTION_SUPL =
142 "android.net.conn.CONNECTIVITY_CHANGE_SUPL";
143
144 /**
Paul Jensen25a217c2015-02-27 22:55:47 -0500145 * The device has connected to a network that has presented a captive
146 * portal, which is blocking Internet connectivity. The user was presented
147 * with a notification that network sign in is required,
148 * and the user invoked the notification's action indicating they
Paul Jensen49e3edf2015-05-22 10:50:39 -0400149 * desire to sign in to the network. Apps handling this activity should
Paul Jensen25a217c2015-02-27 22:55:47 -0500150 * facilitate signing in to the network. This action includes a
151 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents
152 * the network presenting the captive portal; all communication with the
153 * captive portal must be done using this {@code Network} object.
154 * <p/>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400155 * This activity includes a {@link CaptivePortal} extra named
156 * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different
157 * outcomes of the captive portal sign in to the system:
158 * <ul>
159 * <li> When the app handling this action believes the user has signed in to
160 * the network and the captive portal has been dismissed, the app should
161 * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can
162 * reevaluate the network. If reevaluation finds the network no longer
163 * subject to a captive portal, the network may become the default active
Chalard Jean4d660112018-06-04 16:52:49 +0900164 * data network.</li>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400165 * <li> When the app handling this action believes the user explicitly wants
Paul Jensen25a217c2015-02-27 22:55:47 -0500166 * to ignore the captive portal and the network, the app should call
Paul Jensen49e3edf2015-05-22 10:50:39 -0400167 * {@link CaptivePortal#ignoreNetwork}. </li>
168 * </ul>
Paul Jensen25a217c2015-02-27 22:55:47 -0500169 */
170 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
171 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL";
172
173 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 * The lookup key for a {@link NetworkInfo} object. Retrieve with
175 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700176 *
177 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
178 * should always obtain network information through
Paul Jensen3541e9f2015-03-18 12:23:02 -0400179 * {@link #getActiveNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700180 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800181 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700182 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700184
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700186 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700187 *
188 * @see android.content.Intent#getIntExtra(String, int)
189 */
190 public static final String EXTRA_NETWORK_TYPE = "networkType";
191
192 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * The lookup key for a boolean that indicates whether a connect event
194 * is for a network to which the connectivity manager was failing over
195 * following a disconnect on another network.
196 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
junyulai3822c8a2018-12-13 12:47:51 +0800197 *
198 * @deprecated See {@link NetworkInfo}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800199 */
junyulai3822c8a2018-12-13 12:47:51 +0800200 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 public static final String EXTRA_IS_FAILOVER = "isFailover";
202 /**
203 * The lookup key for a {@link NetworkInfo} object. This is supplied when
204 * there is another network that it may be possible to connect to. Retrieve with
205 * {@link android.content.Intent#getParcelableExtra(String)}.
junyulai3822c8a2018-12-13 12:47:51 +0800206 *
207 * @deprecated See {@link NetworkInfo}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800208 */
junyulai3822c8a2018-12-13 12:47:51 +0800209 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
211 /**
212 * The lookup key for a boolean that indicates whether there is a
213 * complete lack of connectivity, i.e., no network is available.
214 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
215 */
216 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
217 /**
218 * The lookup key for a string that indicates why an attempt to connect
219 * to a network failed. The string has no particular structure. It is
220 * intended to be used in notifications presented to users. Retrieve
221 * it with {@link android.content.Intent#getStringExtra(String)}.
222 */
223 public static final String EXTRA_REASON = "reason";
224 /**
225 * The lookup key for a string that provides optionally supplied
226 * extra information about the network state. The information
227 * may be passed up from the lower networking layers, and its
228 * meaning may be specific to a particular network type. Retrieve
229 * it with {@link android.content.Intent#getStringExtra(String)}.
junyulai3822c8a2018-12-13 12:47:51 +0800230 *
231 * @deprecated See {@link NetworkInfo#getExtraInfo()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800232 */
junyulai3822c8a2018-12-13 12:47:51 +0800233 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800234 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700235 /**
236 * The lookup key for an int that provides information about
237 * our connection to the internet at large. 0 indicates no connection,
238 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700239 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700240 * {@hide}
241 */
242 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 /**
Paul Jensen49e3edf2015-05-22 10:50:39 -0400244 * The lookup key for a {@link CaptivePortal} object included with the
245 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal}
246 * object can be used to either indicate to the system that the captive
247 * portal has been dismissed or that the user does not want to pursue
248 * signing in to captive portal. Retrieve it with
249 * {@link android.content.Intent#getParcelableExtra(String)}.
Paul Jensen25a217c2015-02-27 22:55:47 -0500250 */
Paul Jensen49e3edf2015-05-22 10:50:39 -0400251 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL";
Jan Nordqvist52eb29f2015-09-22 15:54:32 -0700252
253 /**
254 * Key for passing a URL to the captive portal login activity.
255 */
256 public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL";
257
Paul Jensen25a217c2015-02-27 22:55:47 -0500258 /**
Remi NGUYEN VAN8255c2d2018-05-22 10:01:53 +0900259 * Key for passing a {@link android.net.captiveportal.CaptivePortalProbeSpec} to the captive
260 * portal login activity.
261 * {@hide}
262 */
Remi NGUYEN VAN7731c5b2019-01-17 14:38:31 +0900263 @SystemApi
264 @TestApi
Remi NGUYEN VAN8255c2d2018-05-22 10:01:53 +0900265 public static final String EXTRA_CAPTIVE_PORTAL_PROBE_SPEC =
266 "android.net.extra.CAPTIVE_PORTAL_PROBE_SPEC";
267
268 /**
Hugo Benichicdf3ba42016-12-14 08:23:40 +0900269 * Key for passing a user agent string to the captive portal login activity.
270 * {@hide}
271 */
Remi NGUYEN VAN7731c5b2019-01-17 14:38:31 +0900272 @SystemApi
273 @TestApi
Hugo Benichicdf3ba42016-12-14 08:23:40 +0900274 public static final String EXTRA_CAPTIVE_PORTAL_USER_AGENT =
275 "android.net.extra.CAPTIVE_PORTAL_USER_AGENT";
276
277 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700278 * Broadcast action to indicate the change of data activity status
279 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800280 * The network becomes active when data transmission is started, or
281 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700282 * {@hide}
283 */
284 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jean4d660112018-06-04 16:52:49 +0900285 public static final String ACTION_DATA_ACTIVITY_CHANGE =
286 "android.net.conn.DATA_ACTIVITY_CHANGE";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700287 /**
288 * The lookup key for an enum that indicates the network device type on which this data activity
289 * change happens.
290 * {@hide}
291 */
292 public static final String EXTRA_DEVICE_TYPE = "deviceType";
293 /**
294 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
295 * it is actively sending or receiving data and {@code false} means it is idle.
296 * {@hide}
297 */
298 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700299 /**
300 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
301 * {@hide}
302 */
303 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700304
305 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 * Broadcast Action: The setting for background data usage has changed
307 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
308 * <p>
309 * If an application uses the network in the background, it should listen
310 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700311 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800312 * <p>
313 *
314 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
315 * of background data depends on several combined factors, and
316 * this broadcast is no longer sent. Instead, when background
317 * data is unavailable, {@link #getActiveNetworkInfo()} will now
318 * appear disconnected. During first boot after a platform
319 * upgrade, this broadcast will be sent once if
320 * {@link #getBackgroundDataSetting()} was {@code false} before
321 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800322 */
323 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800324 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800325 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
326 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
327
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700328 /**
329 * Broadcast Action: The network connection may not be good
330 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
331 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
332 * the network and it's condition.
333 * @hide
334 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800335 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100336 @UnsupportedAppUsage
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700337 public static final String INET_CONDITION_ACTION =
338 "android.net.conn.INET_CONDITION_ACTION";
339
Robert Greenwalt42acef32009-08-12 16:08:25 -0700340 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800341 * Broadcast Action: A tetherable connection has come or gone.
342 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
Erik Kline8351faa2017-04-17 16:47:23 +0900343 * {@code ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY},
344 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER}, and
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800345 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
346 * the current state of tethering. Each include a list of
347 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800348 * @hide
349 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800350 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100351 @UnsupportedAppUsage
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800352 public static final String ACTION_TETHER_STATE_CHANGED =
353 "android.net.conn.TETHER_STATE_CHANGED";
354
355 /**
356 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800357 * gives a String[] listing all the interfaces configured for
358 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800359 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100360 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800361 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800362
363 /**
364 * @hide
Erik Kline8351faa2017-04-17 16:47:23 +0900365 * gives a String[] listing all the interfaces currently in local-only
366 * mode (ie, has DHCPv4+IPv6-ULA support and no packet forwarding)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800367 */
Erik Kline8351faa2017-04-17 16:47:23 +0900368 public static final String EXTRA_ACTIVE_LOCAL_ONLY = "localOnlyArray";
369
370 /**
371 * @hide
372 * gives a String[] listing all the interfaces currently tethered
373 * (ie, has DHCPv4 support and packets potentially forwarded/NATed)
374 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100375 @UnsupportedAppUsage
Erik Kline8351faa2017-04-17 16:47:23 +0900376 public static final String EXTRA_ACTIVE_TETHER = "tetherArray";
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800377
378 /**
379 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800380 * gives a String[] listing all the interfaces we tried to tether and
381 * failed. Use {@link #getLastTetherError} to find the error code
382 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800383 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100384 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800385 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800386
387 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800388 * Broadcast Action: The captive portal tracker has finished its test.
389 * Sent only while running Setup Wizard, in lieu of showing a user
390 * notification.
391 * @hide
392 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800393 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800394 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
395 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
396 /**
397 * The lookup key for a boolean that indicates whether a captive portal was detected.
398 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
399 * @hide
400 */
401 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
402
403 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900404 * Action used to display a dialog that asks the user whether to connect to a network that is
405 * not validated. This intent is used to start the dialog in settings via startActivity.
406 *
407 * @hide
408 */
409 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED";
410
411 /**
Lorenzo Colitti9be58c52016-09-15 14:02:29 +0900412 * Action used to display a dialog that asks the user whether to avoid a network that is no
413 * longer validated. This intent is used to start the dialog in settings via startActivity.
414 *
415 * @hide
416 */
417 public static final String ACTION_PROMPT_LOST_VALIDATION =
418 "android.net.conn.PROMPT_LOST_VALIDATION";
419
420 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800421 * Invalid tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900422 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800423 * @hide
424 */
425 public static final int TETHERING_INVALID = -1;
426
427 /**
428 * Wifi tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900429 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800430 * @hide
431 */
432 @SystemApi
433 public static final int TETHERING_WIFI = 0;
434
435 /**
436 * USB tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900437 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800438 * @hide
439 */
440 @SystemApi
441 public static final int TETHERING_USB = 1;
442
443 /**
444 * Bluetooth tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900445 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800446 * @hide
447 */
448 @SystemApi
449 public static final int TETHERING_BLUETOOTH = 2;
450
451 /**
452 * Extra used for communicating with the TetherService. Includes the type of tethering to
453 * enable if any.
454 * @hide
455 */
456 public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType";
457
458 /**
459 * Extra used for communicating with the TetherService. Includes the type of tethering for
460 * which to cancel provisioning.
461 * @hide
462 */
463 public static final String EXTRA_REM_TETHER_TYPE = "extraRemTetherType";
464
465 /**
466 * Extra used for communicating with the TetherService. True to schedule a recheck of tether
467 * provisioning.
468 * @hide
469 */
470 public static final String EXTRA_SET_ALARM = "extraSetAlarm";
471
472 /**
473 * Tells the TetherService to run a provision check now.
474 * @hide
475 */
476 public static final String EXTRA_RUN_PROVISION = "extraRunProvision";
477
478 /**
479 * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver}
480 * which will receive provisioning results. Can be left empty.
481 * @hide
482 */
483 public static final String EXTRA_PROVISION_CALLBACK = "extraProvisionCallback";
484
485 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800486 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700487 * @hide
488 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100489 @UnsupportedAppUsage
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700490 public static final int TYPE_NONE = -1;
491
492 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900493 * A Mobile data connection. Devices may support more than one.
494 *
495 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
496 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
497 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700498 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900499 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700500 public static final int TYPE_MOBILE = 0;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900501
Robert Greenwalt42acef32009-08-12 16:08:25 -0700502 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900503 * A WIFI data connection. Devices may support more than one.
504 *
505 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
506 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
507 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700508 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900509 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700510 public static final int TYPE_WIFI = 1;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900511
Robert Greenwalt42acef32009-08-12 16:08:25 -0700512 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800513 * An MMS-specific Mobile data connection. This network type may use the
514 * same network interface as {@link #TYPE_MOBILE} or it may use a different
515 * one. This is used by applications needing to talk to the carrier's
516 * Multimedia Messaging Service servers.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900517 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900518 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900519 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900520 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700521 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700522 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700523 public static final int TYPE_MOBILE_MMS = 2;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900524
Robert Greenwalt42acef32009-08-12 16:08:25 -0700525 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800526 * A SUPL-specific Mobile data connection. This network type may use the
527 * same network interface as {@link #TYPE_MOBILE} or it may use a different
528 * one. This is used by applications needing to talk to the carrier's
529 * Secure User Plane Location servers for help locating the device.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900530 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900531 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900532 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900533 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700534 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700535 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700536 public static final int TYPE_MOBILE_SUPL = 3;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900537
Robert Greenwalt42acef32009-08-12 16:08:25 -0700538 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800539 * A DUN-specific Mobile data connection. This network type may use the
540 * same network interface as {@link #TYPE_MOBILE} or it may use a different
541 * one. This is sometimes by the system when setting up an upstream connection
542 * for tethering so that the carrier is aware of DUN traffic.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900543 *
544 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
545 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
546 * provides the {@link NetworkCapabilities#NET_CAPABILITY_DUN} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700547 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900548 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700549 public static final int TYPE_MOBILE_DUN = 4;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900550
Robert Greenwalt42acef32009-08-12 16:08:25 -0700551 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800552 * A High Priority Mobile data connection. This network type uses the
553 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colittie285b432015-04-23 15:32:42 +0900554 * is different.
555 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900556 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
557 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
558 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700559 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700560 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700561 public static final int TYPE_MOBILE_HIPRI = 5;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900562
jsh8214deb2010-03-11 15:04:43 -0800563 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900564 * A WiMAX data connection.
565 *
566 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
567 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
568 * appropriate network. {@see NetworkCapabilities} for supported transports.
jsh8214deb2010-03-11 15:04:43 -0800569 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900570 @Deprecated
jsh8214deb2010-03-11 15:04:43 -0800571 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800572
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800573 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900574 * A Bluetooth data connection.
575 *
576 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
577 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
578 * appropriate network. {@see NetworkCapabilities} for supported transports.
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800579 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900580 @Deprecated
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800581 public static final int TYPE_BLUETOOTH = 7;
582
Robert Greenwalt60810842011-04-22 15:28:18 -0700583 /**
584 * Dummy data connection. This should not be used on shipping devices.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900585 * @deprecated This is not used any more.
Robert Greenwalt60810842011-04-22 15:28:18 -0700586 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900587 @Deprecated
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800588 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800589
Robert Greenwalt60810842011-04-22 15:28:18 -0700590 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900591 * An Ethernet data connection.
592 *
593 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
594 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
595 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt60810842011-04-22 15:28:18 -0700596 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900597 @Deprecated
Robert Greenwalte12aec92011-01-28 14:48:37 -0800598 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700599
Wink Saville9d7d6282011-03-12 14:52:01 -0800600 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800601 * Over the air Administration.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900602 * @deprecated Use {@link NetworkCapabilities} 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_FOTA = 10;
608
609 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800610 * IP Multimedia Subsystem.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900611 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IMS} 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_IMS = 11;
617
618 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800619 * Carrier Branded Services.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900620 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_CBS} instead.
Wink Saville9d7d6282011-03-12 14:52:01 -0800621 * {@hide}
622 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900623 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100624 @UnsupportedAppUsage
Wink Saville9d7d6282011-03-12 14:52:01 -0800625 public static final int TYPE_MOBILE_CBS = 12;
626
repo syncaea743a2011-07-29 23:55:49 -0700627 /**
628 * A Wi-Fi p2p connection. Only requesting processes will have access to
629 * the peers connected.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900630 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_WIFI_P2P} instead.
repo syncaea743a2011-07-29 23:55:49 -0700631 * {@hide}
632 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900633 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100634 @UnsupportedAppUsage
repo syncaea743a2011-07-29 23:55:49 -0700635 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800636
Wink Saville5e56bc52013-07-29 15:00:57 -0700637 /**
638 * The network to use for initially attaching to the network
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900639 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IA} instead.
Wink Saville5e56bc52013-07-29 15:00:57 -0700640 * {@hide}
641 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900642 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100643 @UnsupportedAppUsage
Wink Saville5e56bc52013-07-29 15:00:57 -0700644 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700645
Lorenzo Colittie285b432015-04-23 15:32:42 +0900646 /**
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700647 * Emergency PDN connection for emergency services. This
648 * may include IMS and MMS in emergency situations.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900649 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_EIMS} instead.
Ram3e0e3bc2014-06-26 11:03:44 -0700650 * {@hide}
651 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900652 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100653 @UnsupportedAppUsage
Ram3e0e3bc2014-06-26 11:03:44 -0700654 public static final int TYPE_MOBILE_EMERGENCY = 15;
655
Hui Lu1c5624a2014-01-15 11:05:36 -0500656 /**
657 * The network that uses proxy to achieve connectivity.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900658 * @deprecated Use {@link NetworkCapabilities} instead.
Hui Lu1c5624a2014-01-15 11:05:36 -0500659 * {@hide}
660 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900661 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100662 @UnsupportedAppUsage
Hui Lu1c5624a2014-01-15 11:05:36 -0500663 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700664
Robert Greenwalt8283f882014-07-07 17:09:01 -0700665 /**
666 * A virtual network using one or more native bearers.
667 * It may or may not be providing security services.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900668 * @deprecated Applications should use {@link NetworkCapabilities#TRANSPORT_VPN} instead.
Robert Greenwalt8283f882014-07-07 17:09:01 -0700669 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900670 @Deprecated
Robert Greenwalt8283f882014-07-07 17:09:01 -0700671 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500672
673 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700674 public static final int MAX_RADIO_TYPE = TYPE_VPN;
675
676 /** {@hide} */
677 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678
Hugo Benichi16f0a942017-06-20 14:07:59 +0900679 private static final int MIN_NETWORK_TYPE = TYPE_MOBILE;
680
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800681 /**
682 * If you want to set the default network preference,you can directly
683 * change the networkAttributes array in framework's config.xml.
684 *
685 * @deprecated Since we support so many more networks now, the single
686 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800687 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800688 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800689 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800690 * from an App.
691 */
692 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800693 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
694
Jeff Sharkey625239a2012-09-26 22:03:49 -0700695 /**
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700696 * @hide
697 */
Hugo Benichia5c1f7f2017-06-20 14:10:14 +0900698 public static final int REQUEST_ID_UNSET = 0;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700699
Paul Jensen5d59e782014-07-11 12:28:19 -0400700 /**
Hugo Benichi31c176d2017-06-17 13:14:12 +0900701 * Static unique request used as a tombstone for NetworkCallbacks that have been unregistered.
702 * This allows to distinguish when unregistering NetworkCallbacks those that were never
Chalard Jean4d660112018-06-04 16:52:49 +0900703 * registered from those that were already unregistered.
Hugo Benichi31c176d2017-06-17 13:14:12 +0900704 * @hide
705 */
Hugo Benichia5c1f7f2017-06-20 14:10:14 +0900706 private static final NetworkRequest ALREADY_UNREGISTERED =
Hugo Benichi31c176d2017-06-17 13:14:12 +0900707 new NetworkRequest.Builder().clearCapabilities().build();
708
709 /**
Paul Jensen5d59e782014-07-11 12:28:19 -0400710 * A NetID indicating no Network is selected.
711 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
712 * @hide
713 */
714 public static final int NETID_UNSET = 0;
715
Erik Kline4d092232017-10-30 15:29:44 +0900716 /**
717 * Private DNS Mode values.
718 *
719 * The "private_dns_mode" global setting stores a String value which is
720 * expected to be one of the following.
721 */
722
723 /**
724 * @hide
725 */
726 public static final String PRIVATE_DNS_MODE_OFF = "off";
727 /**
728 * @hide
729 */
730 public static final String PRIVATE_DNS_MODE_OPPORTUNISTIC = "opportunistic";
731 /**
732 * @hide
733 */
734 public static final String PRIVATE_DNS_MODE_PROVIDER_HOSTNAME = "hostname";
735 /**
736 * The default Private DNS mode.
737 *
738 * This may change from release to release or may become dependent upon
739 * the capabilities of the underlying platform.
740 *
741 * @hide
742 */
Erik Kline19841792018-05-16 16:41:57 +0900743 public static final String PRIVATE_DNS_DEFAULT_MODE_FALLBACK = PRIVATE_DNS_MODE_OPPORTUNISTIC;
Erik Kline4d092232017-10-30 15:29:44 +0900744
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100745 @UnsupportedAppUsage
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700746 private final IConnectivityManager mService;
Paul Jensene0bef712014-12-10 15:12:18 -0500747 /**
748 * A kludge to facilitate static access where a Context pointer isn't available, like in the
749 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
750 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
751 * methods that take a Context argument.
752 */
753 private static ConnectivityManager sInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800754
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900755 private final Context mContext;
756
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800757 private INetworkManagementService mNMService;
Felipe Leme1b103232016-01-22 09:44:57 -0800758 private INetworkPolicyManager mNPManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800759
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800760 /**
761 * Tests if a given integer represents a valid network type.
762 * @param networkType the type to be tested
763 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensen9e59e122015-05-06 10:42:25 -0400764 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
765 * validate a network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800766 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700767 @Deprecated
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700768 public static boolean isNetworkTypeValid(int networkType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900769 return MIN_NETWORK_TYPE <= networkType && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770 }
771
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800772 /**
773 * Returns a non-localized string representing a given network type.
774 * ONLY used for debugging output.
775 * @param type the type needing naming
776 * @return a String for the given type, or a string version of the type ("87")
777 * if no name is known.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900778 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800779 * {@hide}
780 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900781 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100782 @UnsupportedAppUsage
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700783 public static String getNetworkTypeName(int type) {
784 switch (type) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900785 case TYPE_NONE:
786 return "NONE";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700787 case TYPE_MOBILE:
788 return "MOBILE";
789 case TYPE_WIFI:
790 return "WIFI";
791 case TYPE_MOBILE_MMS:
792 return "MOBILE_MMS";
793 case TYPE_MOBILE_SUPL:
794 return "MOBILE_SUPL";
795 case TYPE_MOBILE_DUN:
796 return "MOBILE_DUN";
797 case TYPE_MOBILE_HIPRI:
798 return "MOBILE_HIPRI";
799 case TYPE_WIMAX:
800 return "WIMAX";
801 case TYPE_BLUETOOTH:
802 return "BLUETOOTH";
803 case TYPE_DUMMY:
804 return "DUMMY";
805 case TYPE_ETHERNET:
806 return "ETHERNET";
807 case TYPE_MOBILE_FOTA:
808 return "MOBILE_FOTA";
809 case TYPE_MOBILE_IMS:
810 return "MOBILE_IMS";
811 case TYPE_MOBILE_CBS:
812 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700813 case TYPE_WIFI_P2P:
814 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700815 case TYPE_MOBILE_IA:
816 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700817 case TYPE_MOBILE_EMERGENCY:
818 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500819 case TYPE_PROXY:
820 return "PROXY";
Erik Kline37fbfa12014-11-19 17:23:41 +0900821 case TYPE_VPN:
822 return "VPN";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700823 default:
824 return Integer.toString(type);
825 }
826 }
827
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800828 /**
829 * Checks if a given type uses the cellular data connection.
830 * This should be replaced in the future by a network property.
831 * @param networkType the type to check
832 * @return a boolean - {@code true} if uses cellular network, else {@code false}
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900833 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800834 * {@hide}
835 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900836 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100837 @UnsupportedAppUsage
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700838 public static boolean isNetworkTypeMobile(int networkType) {
839 switch (networkType) {
840 case TYPE_MOBILE:
841 case TYPE_MOBILE_MMS:
842 case TYPE_MOBILE_SUPL:
843 case TYPE_MOBILE_DUN:
844 case TYPE_MOBILE_HIPRI:
845 case TYPE_MOBILE_FOTA:
846 case TYPE_MOBILE_IMS:
847 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700848 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700849 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700850 return true;
851 default:
852 return false;
853 }
854 }
855
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800856 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700857 * Checks if the given network type is backed by a Wi-Fi radio.
858 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900859 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700860 * @hide
861 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900862 @Deprecated
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700863 public static boolean isNetworkTypeWifi(int networkType) {
864 switch (networkType) {
865 case TYPE_WIFI:
866 case TYPE_WIFI_P2P:
867 return true;
868 default:
869 return false;
870 }
871 }
872
873 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800874 * Specifies the preferred network type. When the device has more
875 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800876 *
877 * @param preference the network type to prefer over all others. It is
878 * unspecified what happens to the old preferred network in the
879 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700880 * @deprecated Functionality has been removed as it no longer makes sense,
881 * with many more than two networks - we'd need an array to express
882 * preference. Instead we use dynamic network properties of
883 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800884 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700885 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 }
888
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800889 /**
890 * Retrieves the current preferred network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800891 *
892 * @return an integer representing the preferred network type
893 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700894 * @deprecated Functionality has been removed as it no longer makes sense,
895 * with many more than two networks - we'd need an array to express
896 * preference. Instead we use dynamic network properties of
897 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800898 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700899 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600900 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700902 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 }
904
Scott Main671644c2011-10-06 19:02:28 -0700905 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800906 * Returns details about the currently active default data network. When
907 * connected, this network is the default route for outgoing connections.
908 * You should always check {@link NetworkInfo#isConnected()} before initiating
909 * network traffic. This may return {@code null} when there is no default
910 * network.
Chalard Jean5a041d12018-03-29 17:45:24 +0900911 * Note that if the default network is a VPN, this method will return the
912 * NetworkInfo for one of its underlying networks instead, or null if the
913 * VPN agent did not specify any. Apps interested in learning about VPNs
914 * should use {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800915 *
916 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500917 * or {@code null} if no default network is currently active
junyulai3822c8a2018-12-13 12:47:51 +0800918 * @deprecated See {@link NetworkInfo}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700919 */
junyulai3822c8a2018-12-13 12:47:51 +0800920 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600921 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +0900922 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800923 public NetworkInfo getActiveNetworkInfo() {
924 try {
925 return mService.getActiveNetworkInfo();
926 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700927 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 }
929 }
930
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800931 /**
Paul Jensen31a94f42015-02-13 14:18:39 -0500932 * Returns a {@link Network} object corresponding to the currently active
933 * default data network. In the event that the current active default data
934 * network disconnects, the returned {@code Network} object will no longer
935 * be usable. This will return {@code null} when there is no default
936 * network.
937 *
938 * @return a {@link Network} object for the current default network or
939 * {@code null} if no default network is currently active
Paul Jensen31a94f42015-02-13 14:18:39 -0500940 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600941 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +0900942 @Nullable
Paul Jensen31a94f42015-02-13 14:18:39 -0500943 public Network getActiveNetwork() {
944 try {
945 return mService.getActiveNetwork();
946 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700947 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -0500948 }
949 }
950
951 /**
Robin Leed2baf792016-03-24 12:07:00 +0000952 * Returns a {@link Network} object corresponding to the currently active
953 * default data network for a specific UID. In the event that the default data
954 * network disconnects, the returned {@code Network} object will no longer
955 * be usable. This will return {@code null} when there is no default
956 * network for the UID.
Robin Leed2baf792016-03-24 12:07:00 +0000957 *
958 * @return a {@link Network} object for the current default network for the
959 * given UID or {@code null} if no default network is currently active
960 *
961 * @hide
962 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600963 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Chalard Jean50bea3d2019-01-07 19:26:34 +0900964 @Nullable
Robin Leed2baf792016-03-24 12:07:00 +0000965 public Network getActiveNetworkForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600966 return getActiveNetworkForUid(uid, false);
967 }
968
969 /** {@hide} */
970 public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) {
Robin Leed2baf792016-03-24 12:07:00 +0000971 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600972 return mService.getActiveNetworkForUid(uid, ignoreBlocked);
Robin Leed2baf792016-03-24 12:07:00 +0000973 } catch (RemoteException e) {
974 throw e.rethrowFromSystemServer();
975 }
976 }
977
978 /**
Charles He36738632017-05-15 17:07:18 +0100979 * Checks if a VPN app supports always-on mode.
980 *
981 * In order to support the always-on feature, an app has to
982 * <ul>
983 * <li>target {@link VERSION_CODES#N API 24} or above, and
Charles Hec57a01c2017-08-15 15:30:22 +0100984 * <li>not opt out through the {@link VpnService#SERVICE_META_DATA_SUPPORTS_ALWAYS_ON}
985 * meta-data field.
Charles He36738632017-05-15 17:07:18 +0100986 * </ul>
987 *
988 * @param userId The identifier of the user for whom the VPN app is installed.
989 * @param vpnPackage The canonical package name of the VPN app.
990 * @return {@code true} if and only if the VPN app exists and supports always-on mode.
991 * @hide
992 */
993 public boolean isAlwaysOnVpnPackageSupportedForUser(int userId, @Nullable String vpnPackage) {
994 try {
995 return mService.isAlwaysOnVpnPackageSupported(userId, vpnPackage);
996 } catch (RemoteException e) {
997 throw e.rethrowFromSystemServer();
998 }
999 }
1000
1001 /**
Robin Lee244ce8e2016-01-05 18:03:46 +00001002 * Configures an always-on VPN connection through a specific application.
1003 * This connection is automatically granted and persisted after a reboot.
1004 *
1005 * <p>The designated package should declare a {@link VpnService} in its
1006 * manifest guarded by {@link android.Manifest.permission.BIND_VPN_SERVICE},
1007 * otherwise the call will fail.
1008 *
1009 * @param userId The identifier of the user to set an always-on VPN for.
1010 * @param vpnPackage The package name for an installed VPN app on the device, or {@code null}
1011 * to remove an existing always-on VPN configuration.
Robin Leedc679712016-05-03 13:23:03 +01001012 * @param lockdownEnabled {@code true} to disallow networking when the VPN is not connected or
1013 * {@code false} otherwise.
Robin Lee244ce8e2016-01-05 18:03:46 +00001014 * @return {@code true} if the package is set as always-on VPN controller;
1015 * {@code false} otherwise.
1016 * @hide
1017 */
Robin Leedc679712016-05-03 13:23:03 +01001018 public boolean setAlwaysOnVpnPackageForUser(int userId, @Nullable String vpnPackage,
1019 boolean lockdownEnabled) {
Robin Lee244ce8e2016-01-05 18:03:46 +00001020 try {
Robin Leedc679712016-05-03 13:23:03 +01001021 return mService.setAlwaysOnVpnPackage(userId, vpnPackage, lockdownEnabled);
Robin Lee244ce8e2016-01-05 18:03:46 +00001022 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001023 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +00001024 }
1025 }
1026
1027 /**
1028 * Returns the package name of the currently set always-on VPN application.
1029 * If there is no always-on VPN set, or the VPN is provided by the system instead
1030 * of by an app, {@code null} will be returned.
1031 *
1032 * @return Package name of VPN controller responsible for always-on VPN,
1033 * or {@code null} if none is set.
1034 * @hide
1035 */
1036 public String getAlwaysOnVpnPackageForUser(int userId) {
1037 try {
1038 return mService.getAlwaysOnVpnPackage(userId);
1039 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001040 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +00001041 }
1042 }
1043
1044 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001045 * Returns details about the currently active default data network
1046 * for a given uid. This is for internal use only to avoid spying
1047 * other apps.
1048 *
1049 * @return a {@link NetworkInfo} object for the current default network
1050 * for the given uid or {@code null} if no default network is
1051 * available for the specified uid.
1052 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001053 * {@hide}
1054 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001055 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001056 @UnsupportedAppUsage
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001057 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001058 return getActiveNetworkInfoForUid(uid, false);
1059 }
1060
1061 /** {@hide} */
1062 public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001063 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001064 return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001065 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001066 throw e.rethrowFromSystemServer();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001067 }
1068 }
1069
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001070 /**
1071 * Returns connection status information about a particular
1072 * network type.
1073 *
1074 * @param networkType integer specifying which networkType in
1075 * which you're interested.
1076 * @return a {@link NetworkInfo} object for the requested
1077 * network type or {@code null} if the type is not
Chalard Jean5a041d12018-03-29 17:45:24 +09001078 * supported by the device. If {@code networkType} is
1079 * TYPE_VPN and a VPN is active for the calling app,
1080 * then this method will try to return one of the
1081 * underlying networks for the VPN or null if the
1082 * VPN agent didn't specify any.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001083 *
Paul Jensen3541e9f2015-03-18 12:23:02 -04001084 * @deprecated This method does not support multiple connected networks
1085 * of the same type. Use {@link #getAllNetworks} and
1086 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001087 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001088 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001089 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09001090 @Nullable
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 public NetworkInfo getNetworkInfo(int networkType) {
1092 try {
1093 return mService.getNetworkInfo(networkType);
1094 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001095 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 }
1097 }
1098
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001099 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001100 * Returns connection status information about a particular
1101 * Network.
1102 *
1103 * @param network {@link Network} specifying which network
1104 * in which you're interested.
1105 * @return a {@link NetworkInfo} object for the requested
1106 * network or {@code null} if the {@code Network}
1107 * is not valid.
junyulai3822c8a2018-12-13 12:47:51 +08001108 * @deprecated See {@link NetworkInfo}.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001109 */
junyulai3822c8a2018-12-13 12:47:51 +08001110 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001111 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09001112 @Nullable
1113 public NetworkInfo getNetworkInfo(@Nullable Network network) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001114 return getNetworkInfoForUid(network, Process.myUid(), false);
1115 }
1116
1117 /** {@hide} */
1118 public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001119 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001120 return mService.getNetworkInfoForUid(network, uid, ignoreBlocked);
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001121 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001122 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001123 }
1124 }
1125
1126 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001127 * Returns connection status information about all network
1128 * types supported by the device.
1129 *
1130 * @return an array of {@link NetworkInfo} objects. Check each
1131 * {@link NetworkInfo#getType} for which type each applies.
1132 *
Paul Jensen3541e9f2015-03-18 12:23:02 -04001133 * @deprecated This method does not support multiple connected networks
1134 * of the same type. Use {@link #getAllNetworks} and
1135 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001136 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001137 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001138 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09001139 @NonNull
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001140 public NetworkInfo[] getAllNetworkInfo() {
1141 try {
1142 return mService.getAllNetworkInfo();
1143 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001144 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 }
1146 }
1147
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001148 /**
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001149 * Returns the {@link Network} object currently serving a given type, or
1150 * null if the given type is not connected.
1151 *
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001152 * @hide
Paul Jensen3541e9f2015-03-18 12:23:02 -04001153 * @deprecated This method does not support multiple connected networks
1154 * of the same type. Use {@link #getAllNetworks} and
1155 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001156 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001157 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001158 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001159 @UnsupportedAppUsage
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001160 public Network getNetworkForType(int networkType) {
1161 try {
1162 return mService.getNetworkForType(networkType);
1163 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001164 throw e.rethrowFromSystemServer();
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001165 }
1166 }
1167
1168 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001169 * Returns an array of all {@link Network} currently tracked by the
1170 * framework.
Paul Jensenb2748922015-05-06 11:10:18 -04001171 *
1172 * @return an array of {@link Network} objects.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001173 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001174 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09001175 @NonNull
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001176 public Network[] getAllNetworks() {
1177 try {
1178 return mService.getAllNetworks();
1179 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001180 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001181 }
1182 }
1183
1184 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001185 * Returns an array of {@link android.net.NetworkCapabilities} objects, representing
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001186 * the Networks that applications run by the given user will use by default.
1187 * @hide
1188 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001189 @UnsupportedAppUsage
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001190 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
1191 try {
1192 return mService.getDefaultNetworkCapabilitiesForUser(userId);
1193 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001194 throw e.rethrowFromSystemServer();
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001195 }
1196 }
1197
1198 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001199 * Returns the IP information for the current default network.
1200 *
1201 * @return a {@link LinkProperties} object describing the IP info
1202 * for the current default network, or {@code null} if there
1203 * is no current default network.
1204 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001205 * {@hide}
1206 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001207 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001208 @UnsupportedAppUsage
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001209 public LinkProperties getActiveLinkProperties() {
1210 try {
1211 return mService.getActiveLinkProperties();
1212 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001213 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001214 }
1215 }
1216
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001217 /**
1218 * Returns the IP information for a given network type.
1219 *
1220 * @param networkType the network type of interest.
1221 * @return a {@link LinkProperties} object describing the IP info
1222 * for the given networkType, or {@code null} if there is
1223 * no current default network.
1224 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001225 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04001226 * @deprecated This method does not support multiple connected networks
1227 * of the same type. Use {@link #getAllNetworks},
1228 * {@link #getNetworkInfo(android.net.Network)}, and
1229 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001230 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001231 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001232 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001233 @UnsupportedAppUsage
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001234 public LinkProperties getLinkProperties(int networkType) {
1235 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001236 return mService.getLinkPropertiesForType(networkType);
1237 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001238 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001239 }
1240 }
1241
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001242 /**
1243 * Get the {@link LinkProperties} for the given {@link Network}. This
1244 * will return {@code null} if the network is unknown.
1245 *
1246 * @param network The {@link Network} object identifying the network in question.
1247 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensenb2748922015-05-06 11:10:18 -04001248 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001249 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09001250 @Nullable
1251 public LinkProperties getLinkProperties(@Nullable Network network) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001252 try {
1253 return mService.getLinkProperties(network);
1254 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001255 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001256 }
1257 }
1258
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001259 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001260 * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001261 * will return {@code null} if the network is unknown.
1262 *
1263 * @param network The {@link Network} object identifying the network in question.
Lorenzo Colittie285b432015-04-23 15:32:42 +09001264 * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001265 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001266 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09001267 @Nullable
1268 public NetworkCapabilities getNetworkCapabilities(@Nullable Network network) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001269 try {
1270 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001271 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001272 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001273 }
1274 }
1275
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001276 /**
Udam Sainib7c24872016-01-04 12:16:14 -08001277 * Gets the URL that should be used for resolving whether a captive portal is present.
1278 * 1. This URL should respond with a 204 response to a GET request to indicate no captive
1279 * portal is present.
1280 * 2. This URL must be HTTP as redirect responses are used to find captive portal
1281 * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects.
1282 *
1283 * @hide
1284 */
1285 @SystemApi
Udam Saini0e94c362017-06-07 12:06:28 -07001286 @RequiresPermission(android.Manifest.permission.LOCAL_MAC_ADDRESS)
Udam Sainib7c24872016-01-04 12:16:14 -08001287 public String getCaptivePortalServerUrl() {
1288 try {
1289 return mService.getCaptivePortalServerUrl();
1290 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001291 throw e.rethrowFromSystemServer();
Udam Sainib7c24872016-01-04 12:16:14 -08001292 }
1293 }
1294
1295 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001296 * Tells the underlying networking system that the caller wants to
1297 * begin using the named feature. The interpretation of {@code feature}
1298 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001299 *
1300 * <p>This method requires the caller to hold either the
1301 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1302 * or the ability to modify system settings as determined by
1303 * {@link android.provider.Settings.System#canWrite}.</p>
1304 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 * @param networkType specifies which network the request pertains to
1306 * @param feature the name of the feature to be used
1307 * @return an integer value representing the outcome of the request.
1308 * The interpretation of this value is specific to each networking
1309 * implementation+feature combination, except that the value {@code -1}
1310 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001311 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001312 * @deprecated Deprecated in favor of the cleaner
1313 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001314 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001315 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001316 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001318 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001319 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001320 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001321 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1322 if (netCap == null) {
1323 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
1324 feature);
1325 return PhoneConstants.APN_REQUEST_FAILED;
1326 }
1327
1328 NetworkRequest request = null;
1329 synchronized (sLegacyRequests) {
1330 LegacyRequest l = sLegacyRequests.get(netCap);
1331 if (l != null) {
1332 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
1333 renewRequestLocked(l);
1334 if (l.currentNetwork != null) {
1335 return PhoneConstants.APN_ALREADY_ACTIVE;
1336 } else {
1337 return PhoneConstants.APN_REQUEST_STARTED;
1338 }
1339 }
1340
1341 request = requestNetworkForFeatureLocked(netCap);
1342 }
1343 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -07001344 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001345 return PhoneConstants.APN_REQUEST_STARTED;
1346 } else {
1347 Log.d(TAG, " request Failed");
1348 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 }
1350 }
1351
1352 /**
1353 * Tells the underlying networking system that the caller is finished
1354 * using the named feature. The interpretation of {@code feature}
1355 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001356 *
1357 * <p>This method requires the caller to hold either the
1358 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1359 * or the ability to modify system settings as determined by
1360 * {@link android.provider.Settings.System#canWrite}.</p>
1361 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 * @param networkType specifies which network the request pertains to
1363 * @param feature the name of the feature that is no longer needed
1364 * @return an integer value representing the outcome of the request.
1365 * The interpretation of this value is specific to each networking
1366 * implementation+feature combination, except that the value {@code -1}
1367 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001368 *
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09001369 * @deprecated Deprecated in favor of the cleaner
1370 * {@link #unregisterNetworkCallback(NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001371 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001372 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001373 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001374 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001375 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001376 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001377 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001378 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1379 if (netCap == null) {
1380 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
1381 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001382 return -1;
1383 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001384
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001385 if (removeRequestForFeature(netCap)) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001386 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001387 }
1388 return 1;
1389 }
1390
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001391 @UnsupportedAppUsage
Robert Greenwalt562cc542014-05-15 18:07:26 -07001392 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1393 if (networkType == TYPE_MOBILE) {
Erik Kline35bf06c2017-01-26 18:08:28 +09001394 switch (feature) {
1395 case "enableCBS":
1396 return networkCapabilitiesForType(TYPE_MOBILE_CBS);
1397 case "enableDUN":
1398 case "enableDUNAlways":
1399 return networkCapabilitiesForType(TYPE_MOBILE_DUN);
1400 case "enableFOTA":
1401 return networkCapabilitiesForType(TYPE_MOBILE_FOTA);
1402 case "enableHIPRI":
1403 return networkCapabilitiesForType(TYPE_MOBILE_HIPRI);
1404 case "enableIMS":
1405 return networkCapabilitiesForType(TYPE_MOBILE_IMS);
1406 case "enableMMS":
1407 return networkCapabilitiesForType(TYPE_MOBILE_MMS);
1408 case "enableSUPL":
1409 return networkCapabilitiesForType(TYPE_MOBILE_SUPL);
1410 default:
1411 return null;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001412 }
Erik Kline35bf06c2017-01-26 18:08:28 +09001413 } else if (networkType == TYPE_WIFI && "p2p".equals(feature)) {
1414 return networkCapabilitiesForType(TYPE_WIFI_P2P);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001415 }
1416 return null;
1417 }
1418
Robert Greenwalt06314e42014-10-29 14:04:06 -07001419 /**
1420 * Guess what the network request was trying to say so that the resulting
1421 * network is accessible via the legacy (deprecated) API such as
1422 * requestRouteToHost.
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001423 *
1424 * This means we should try to be fairly precise about transport and
Robert Greenwalt06314e42014-10-29 14:04:06 -07001425 * capability but ignore things such as networkSpecifier.
1426 * If the request has more than one transport or capability it doesn't
1427 * match the old legacy requests (they selected only single transport/capability)
1428 * so this function cannot map the request to a single legacy type and
1429 * the resulting network will not be available to the legacy APIs.
1430 *
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001431 * This code is only called from the requestNetwork API (L and above).
1432 *
1433 * Setting a legacy type causes CONNECTIVITY_ACTION broadcasts, which are expensive
1434 * because they wake up lots of apps - see http://b/23350688 . So we currently only
1435 * do this for SUPL requests, which are the only ones that we know need it. If
1436 * omitting these broadcasts causes unacceptable app breakage, then for backwards
1437 * compatibility we can send them:
1438 *
1439 * if (targetSdkVersion < Build.VERSION_CODES.M) && // legacy API unsupported >= M
1440 * targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP)) // requestNetwork not present < L
1441 *
Robert Greenwalt06314e42014-10-29 14:04:06 -07001442 * TODO - This should be removed when the legacy APIs are removed.
1443 */
Ye Wenb87875e2014-07-21 14:19:01 -07001444 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1445 if (netCap == null) {
1446 return TYPE_NONE;
1447 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001448
Ye Wenb87875e2014-07-21 14:19:01 -07001449 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1450 return TYPE_NONE;
1451 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001452
Lifu Tang30f95a72016-01-07 23:20:38 -08001453 // Do this only for SUPL, until GnssLocationProvider is fixed. http://b/25876485 .
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001454 if (!netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1455 // NOTE: if this causes app breakage, we should not just comment out this early return;
1456 // instead, we should make this early return conditional on the requesting app's target
1457 // SDK version, as described in the comment above.
1458 return TYPE_NONE;
1459 }
1460
Robert Greenwalt06314e42014-10-29 14:04:06 -07001461 String type = null;
1462 int result = TYPE_NONE;
1463
Ye Wenb87875e2014-07-21 14:19:01 -07001464 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
Robert Greenwalt06314e42014-10-29 14:04:06 -07001465 type = "enableCBS";
1466 result = TYPE_MOBILE_CBS;
1467 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1468 type = "enableIMS";
1469 result = TYPE_MOBILE_IMS;
1470 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1471 type = "enableFOTA";
1472 result = TYPE_MOBILE_FOTA;
1473 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1474 type = "enableDUN";
1475 result = TYPE_MOBILE_DUN;
1476 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001477 type = "enableSUPL";
Robert Greenwalt06314e42014-10-29 14:04:06 -07001478 result = TYPE_MOBILE_SUPL;
Robert Greenwalt74ab4fa2015-08-28 12:37:54 -07001479 // back out this hack for mms as they no longer need this and it's causing
1480 // device slowdowns - b/23350688 (note, supl still needs this)
1481 //} else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1482 // type = "enableMMS";
1483 // result = TYPE_MOBILE_MMS;
Robert Greenwalt06314e42014-10-29 14:04:06 -07001484 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1485 type = "enableHIPRI";
1486 result = TYPE_MOBILE_HIPRI;
Ye Wenb87875e2014-07-21 14:19:01 -07001487 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001488 if (type != null) {
1489 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1490 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1491 return result;
Ye Wenb87875e2014-07-21 14:19:01 -07001492 }
1493 }
1494 return TYPE_NONE;
1495 }
1496
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001497 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001498 if (netCap == null) return TYPE_NONE;
1499 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1500 return TYPE_MOBILE_CBS;
1501 }
1502 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1503 return TYPE_MOBILE_IMS;
1504 }
1505 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1506 return TYPE_MOBILE_FOTA;
1507 }
1508 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1509 return TYPE_MOBILE_DUN;
1510 }
1511 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1512 return TYPE_MOBILE_SUPL;
1513 }
1514 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1515 return TYPE_MOBILE_MMS;
1516 }
1517 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1518 return TYPE_MOBILE_HIPRI;
1519 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001520 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1521 return TYPE_WIFI_P2P;
1522 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001523 return TYPE_NONE;
1524 }
1525
1526 private static class LegacyRequest {
1527 NetworkCapabilities networkCapabilities;
1528 NetworkRequest networkRequest;
1529 int expireSequenceNumber;
1530 Network currentNetwork;
1531 int delay = -1;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001532
1533 private void clearDnsBinding() {
1534 if (currentNetwork != null) {
1535 currentNetwork = null;
1536 setProcessDefaultNetworkForHostResolution(null);
1537 }
1538 }
1539
Robert Greenwalt6078b502014-06-11 16:05:07 -07001540 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001541 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001542 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001543 currentNetwork = network;
1544 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001545 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001546 }
1547 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001548 public void onLost(Network network) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001549 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001550 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1551 }
1552 };
1553 }
1554
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001555 @UnsupportedAppUsage
Chalard Jean4d660112018-06-04 16:52:49 +09001556 private static final HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
1557 new HashMap<>();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001558
1559 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1560 synchronized (sLegacyRequests) {
1561 LegacyRequest l = sLegacyRequests.get(netCap);
1562 if (l != null) return l.networkRequest;
1563 }
1564 return null;
1565 }
1566
1567 private void renewRequestLocked(LegacyRequest l) {
1568 l.expireSequenceNumber++;
1569 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1570 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1571 }
1572
1573 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1574 int ourSeqNum = -1;
1575 synchronized (sLegacyRequests) {
1576 LegacyRequest l = sLegacyRequests.get(netCap);
1577 if (l == null) return;
1578 ourSeqNum = l.expireSequenceNumber;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001579 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001580 }
1581 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1582 }
1583
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001584 @UnsupportedAppUsage
Robert Greenwalt562cc542014-05-15 18:07:26 -07001585 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1586 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001587 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001588 try {
1589 delay = mService.getRestoreDefaultNetworkDelay(type);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001590 } catch (RemoteException e) {
1591 throw e.rethrowFromSystemServer();
1592 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001593 LegacyRequest l = new LegacyRequest();
1594 l.networkCapabilities = netCap;
1595 l.delay = delay;
1596 l.expireSequenceNumber = 0;
Hugo Benichi2583ef02017-02-02 17:02:36 +09001597 l.networkRequest = sendRequestForNetwork(
1598 netCap, l.networkCallback, 0, REQUEST, type, getDefaultHandler());
Robert Greenwalt562cc542014-05-15 18:07:26 -07001599 if (l.networkRequest == null) return null;
1600 sLegacyRequests.put(netCap, l);
1601 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1602 return l.networkRequest;
1603 }
1604
1605 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1606 if (delay >= 0) {
1607 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
Hugo Benichi2583ef02017-02-02 17:02:36 +09001608 CallbackHandler handler = getDefaultHandler();
Hugo Benichi6f260f32017-02-03 14:18:44 +09001609 Message msg = handler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1610 handler.sendMessageDelayed(msg, delay);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001611 }
1612 }
1613
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001614 @UnsupportedAppUsage
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001615 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1616 final LegacyRequest l;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001617 synchronized (sLegacyRequests) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001618 l = sLegacyRequests.remove(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001619 }
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001620 if (l == null) return false;
1621 unregisterNetworkCallback(l.networkCallback);
1622 l.clearDnsBinding();
1623 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001624 }
1625
Erik Kline35bf06c2017-01-26 18:08:28 +09001626 private static final SparseIntArray sLegacyTypeToTransport = new SparseIntArray();
1627 static {
1628 sLegacyTypeToTransport.put(TYPE_MOBILE, NetworkCapabilities.TRANSPORT_CELLULAR);
1629 sLegacyTypeToTransport.put(TYPE_MOBILE_CBS, NetworkCapabilities.TRANSPORT_CELLULAR);
1630 sLegacyTypeToTransport.put(TYPE_MOBILE_DUN, NetworkCapabilities.TRANSPORT_CELLULAR);
1631 sLegacyTypeToTransport.put(TYPE_MOBILE_FOTA, NetworkCapabilities.TRANSPORT_CELLULAR);
1632 sLegacyTypeToTransport.put(TYPE_MOBILE_HIPRI, NetworkCapabilities.TRANSPORT_CELLULAR);
1633 sLegacyTypeToTransport.put(TYPE_MOBILE_IMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1634 sLegacyTypeToTransport.put(TYPE_MOBILE_MMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1635 sLegacyTypeToTransport.put(TYPE_MOBILE_SUPL, NetworkCapabilities.TRANSPORT_CELLULAR);
1636 sLegacyTypeToTransport.put(TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI);
1637 sLegacyTypeToTransport.put(TYPE_WIFI_P2P, NetworkCapabilities.TRANSPORT_WIFI);
1638 sLegacyTypeToTransport.put(TYPE_BLUETOOTH, NetworkCapabilities.TRANSPORT_BLUETOOTH);
1639 sLegacyTypeToTransport.put(TYPE_ETHERNET, NetworkCapabilities.TRANSPORT_ETHERNET);
1640 }
1641
1642 private static final SparseIntArray sLegacyTypeToCapability = new SparseIntArray();
1643 static {
1644 sLegacyTypeToCapability.put(TYPE_MOBILE_CBS, NetworkCapabilities.NET_CAPABILITY_CBS);
1645 sLegacyTypeToCapability.put(TYPE_MOBILE_DUN, NetworkCapabilities.NET_CAPABILITY_DUN);
1646 sLegacyTypeToCapability.put(TYPE_MOBILE_FOTA, NetworkCapabilities.NET_CAPABILITY_FOTA);
1647 sLegacyTypeToCapability.put(TYPE_MOBILE_IMS, NetworkCapabilities.NET_CAPABILITY_IMS);
1648 sLegacyTypeToCapability.put(TYPE_MOBILE_MMS, NetworkCapabilities.NET_CAPABILITY_MMS);
1649 sLegacyTypeToCapability.put(TYPE_MOBILE_SUPL, NetworkCapabilities.NET_CAPABILITY_SUPL);
1650 sLegacyTypeToCapability.put(TYPE_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
1651 }
1652
1653 /**
1654 * Given a legacy type (TYPE_WIFI, ...) returns a NetworkCapabilities
1655 * instance suitable for registering a request or callback. Throws an
1656 * IllegalArgumentException if no mapping from the legacy type to
1657 * NetworkCapabilities is known.
1658 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +09001659 * @deprecated Types are deprecated. Use {@link NetworkCallback} or {@link NetworkRequest}
1660 * to find the network instead.
Erik Kline35bf06c2017-01-26 18:08:28 +09001661 * @hide
1662 */
1663 public static NetworkCapabilities networkCapabilitiesForType(int type) {
1664 final NetworkCapabilities nc = new NetworkCapabilities();
1665
1666 // Map from type to transports.
1667 final int NOT_FOUND = -1;
1668 final int transport = sLegacyTypeToTransport.get(type, NOT_FOUND);
Hugo Benichie7678512017-05-09 15:19:01 +09001669 Preconditions.checkArgument(transport != NOT_FOUND, "unknown legacy type: " + type);
Erik Kline35bf06c2017-01-26 18:08:28 +09001670 nc.addTransportType(transport);
1671
1672 // Map from type to capabilities.
1673 nc.addCapability(sLegacyTypeToCapability.get(
1674 type, NetworkCapabilities.NET_CAPABILITY_INTERNET));
1675 nc.maybeMarkCapabilitiesRestricted();
1676 return nc;
1677 }
1678
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001679 /** @hide */
1680 public static class PacketKeepaliveCallback {
1681 /** The requested keepalive was successfully started. */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001682 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001683 public void onStarted() {}
1684 /** The keepalive was successfully stopped. */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001685 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001686 public void onStopped() {}
1687 /** An error occurred. */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001688 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001689 public void onError(int error) {}
1690 }
1691
1692 /**
1693 * Allows applications to request that the system periodically send specific packets on their
1694 * behalf, using hardware offload to save battery power.
1695 *
1696 * To request that the system send keepalives, call one of the methods that return a
1697 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1698 * passing in a non-null callback. If the callback is successfully started, the callback's
1699 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1700 * specifying one of the {@code ERROR_*} constants in this class.
1701 *
Chalard Jean4d660112018-06-04 16:52:49 +09001702 * To stop an existing keepalive, call {@link PacketKeepalive#stop}. The system will call
1703 * {@link PacketKeepaliveCallback#onStopped} if the operation was successful or
1704 * {@link PacketKeepaliveCallback#onError} if an error occurred.
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001705 *
junyulai48eac1d42018-12-27 17:25:29 +08001706 * @deprecated Use {@link SocketKeepalive} instead.
1707 *
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001708 * @hide
1709 */
1710 public class PacketKeepalive {
1711
1712 private static final String TAG = "PacketKeepalive";
1713
1714 /** @hide */
1715 public static final int SUCCESS = 0;
1716
1717 /** @hide */
1718 public static final int NO_KEEPALIVE = -1;
1719
1720 /** @hide */
1721 public static final int BINDER_DIED = -10;
1722
1723 /** The specified {@code Network} is not connected. */
1724 public static final int ERROR_INVALID_NETWORK = -20;
1725 /** The specified IP addresses are invalid. For example, the specified source IP address is
1726 * not configured on the specified {@code Network}. */
1727 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1728 /** The requested port is invalid. */
1729 public static final int ERROR_INVALID_PORT = -22;
1730 /** The packet length is invalid (e.g., too long). */
1731 public static final int ERROR_INVALID_LENGTH = -23;
1732 /** The packet transmission interval is invalid (e.g., too short). */
1733 public static final int ERROR_INVALID_INTERVAL = -24;
1734
1735 /** The hardware does not support this request. */
1736 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti9d1284e2015-09-08 16:46:36 +09001737 /** The hardware returned an error. */
1738 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001739
Nathan Harold63dd8132018-02-14 13:09:45 -08001740 /** The NAT-T destination port for IPsec */
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001741 public static final int NATT_PORT = 4500;
1742
Nathan Harold63dd8132018-02-14 13:09:45 -08001743 /** The minimum interval in seconds between keepalive packet transmissions */
1744 public static final int MIN_INTERVAL = 10;
1745
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001746 private final Network mNetwork;
1747 private final PacketKeepaliveCallback mCallback;
1748 private final Looper mLooper;
1749 private final Messenger mMessenger;
1750
1751 private volatile Integer mSlot;
1752
1753 void stopLooper() {
1754 mLooper.quit();
1755 }
1756
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001757 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001758 public void stop() {
1759 try {
1760 mService.stopKeepalive(mNetwork, mSlot);
1761 } catch (RemoteException e) {
1762 Log.e(TAG, "Error stopping packet keepalive: ", e);
1763 stopLooper();
1764 }
1765 }
1766
1767 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09001768 Preconditions.checkNotNull(network, "network cannot be null");
1769 Preconditions.checkNotNull(callback, "callback cannot be null");
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001770 mNetwork = network;
1771 mCallback = callback;
1772 HandlerThread thread = new HandlerThread(TAG);
1773 thread.start();
1774 mLooper = thread.getLooper();
1775 mMessenger = new Messenger(new Handler(mLooper) {
1776 @Override
1777 public void handleMessage(Message message) {
1778 switch (message.what) {
1779 case NetworkAgent.EVENT_PACKET_KEEPALIVE:
1780 int error = message.arg2;
1781 try {
1782 if (error == SUCCESS) {
1783 if (mSlot == null) {
1784 mSlot = message.arg1;
1785 mCallback.onStarted();
1786 } else {
1787 mSlot = null;
1788 stopLooper();
1789 mCallback.onStopped();
1790 }
1791 } else {
1792 stopLooper();
1793 mCallback.onError(error);
1794 }
1795 } catch (Exception e) {
1796 Log.e(TAG, "Exception in keepalive callback(" + error + ")", e);
1797 }
1798 break;
1799 default:
1800 Log.e(TAG, "Unhandled message " + Integer.toHexString(message.what));
1801 break;
1802 }
1803 }
1804 });
1805 }
1806 }
1807
1808 /**
1809 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
1810 *
junyulai48eac1d42018-12-27 17:25:29 +08001811 * @deprecated Use {@link #createSocketKeepalive} instead.
1812 *
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001813 * @hide
1814 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001815 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001816 public PacketKeepalive startNattKeepalive(
1817 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
1818 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
1819 final PacketKeepalive k = new PacketKeepalive(network, callback);
1820 try {
1821 mService.startNattKeepalive(network, intervalSeconds, k.mMessenger, new Binder(),
1822 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
1823 } catch (RemoteException e) {
1824 Log.e(TAG, "Error starting packet keepalive: ", e);
1825 k.stopLooper();
1826 return null;
1827 }
1828 return k;
1829 }
1830
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 /**
junyulai48eac1d42018-12-27 17:25:29 +08001832 * Request that keepalives be started on a IPsec NAT-T socket.
1833 *
1834 * @param network The {@link Network} the socket is on.
1835 * @param socket The socket that needs to be kept alive.
1836 * @param source The source address of the {@link UdpEncapsulationSocket}.
1837 * @param destination The destination address of the {@link UdpEncapsulationSocket}.
1838 * @param executor The executor on which callback will be invoked. The provided {@link Executor}
1839 * must run callback sequentially, otherwise the order of callbacks cannot be
1840 * guaranteed.
1841 * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive
1842 * changes. Must be extended by applications that use this API.
1843 *
1844 * @return A {@link SocketKeepalive} object, which can be used to control this keepalive object.
1845 **/
1846 public SocketKeepalive createSocketKeepalive(@NonNull Network network,
1847 @NonNull UdpEncapsulationSocket socket,
1848 @NonNull InetAddress source,
1849 @NonNull InetAddress destination,
1850 @NonNull @CallbackExecutor Executor executor,
1851 @NonNull Callback callback) {
1852 return new NattSocketKeepalive(mService, network, socket, source, destination, executor,
1853 callback);
1854 }
1855
1856 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001857 * Ensure that a network route exists to deliver traffic to the specified
1858 * host via the specified network interface. An attempt to add a route that
1859 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001860 *
1861 * <p>This method requires the caller to hold either the
1862 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1863 * or the ability to modify system settings as determined by
1864 * {@link android.provider.Settings.System#canWrite}.</p>
1865 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 * @param networkType the type of the network over which traffic to the specified
1867 * host is to be routed
1868 * @param hostAddress the IP address of the host to which the route is desired
1869 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001870 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001871 * @deprecated Deprecated in favor of the
1872 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
1873 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001874 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001875 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001876 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001878 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001880 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001881 }
1882
1883 /**
1884 * Ensure that a network route exists to deliver traffic to the specified
1885 * host via the specified network interface. An attempt to add a route that
1886 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001887 *
1888 * <p>This method requires the caller to hold either the
1889 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1890 * or the ability to modify system settings as determined by
1891 * {@link android.provider.Settings.System#canWrite}.</p>
1892 *
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001893 * @param networkType the type of the network over which traffic to the specified
1894 * host is to be routed
1895 * @param hostAddress the IP address of the host to which the route is desired
1896 * @return {@code true} on success, {@code false} on failure
1897 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001898 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001899 * {@link #bindProcessToNetwork} API.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001900 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001901 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001902 @UnsupportedAppUsage
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001903 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001904 checkLegacyRoutingApiAccess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001905 try {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001906 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001907 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001908 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001909 }
1910 }
1911
1912 /**
1913 * Returns the value of the setting for background data usage. If false,
1914 * applications should not use the network if the application is not in the
1915 * foreground. Developers should respect this setting, and check the value
1916 * of this before performing any background data operations.
1917 * <p>
1918 * All applications that have background services that use the network
1919 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001920 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001921 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001922 * background data depends on several combined factors, and this method will
1923 * always return {@code true}. Instead, when background data is unavailable,
1924 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001925 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 * @return Whether background data usage is allowed.
1927 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001928 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001929 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001930 // assume that background data is allowed; final authority is
1931 // NetworkInfo which may be blocked.
1932 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001933 }
1934
1935 /**
1936 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001937 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 * @param allowBackgroundData Whether an application should use data while
1939 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001940 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001941 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1942 * @see #getBackgroundDataSetting()
1943 * @hide
1944 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001945 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001946 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001947 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001948 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001949 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001950
Jeff Sharkey43d2a172017-07-12 10:50:42 -06001951 /** {@hide} */
1952 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001953 @UnsupportedAppUsage
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001954 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1955 try {
1956 return mService.getActiveNetworkQuotaInfo();
1957 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001958 throw e.rethrowFromSystemServer();
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001959 }
1960 }
1961
1962 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001963 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001964 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001965 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001966 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001967 @UnsupportedAppUsage
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001968 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001969 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1970 if (b != null) {
1971 try {
1972 ITelephony it = ITelephony.Stub.asInterface(b);
Shishir Agrawal7ea3e8b2016-01-25 13:03:07 -08001973 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Wink Saville36ffb042014-12-05 11:10:30 -08001974 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
Malcolm Chenb455e722017-11-28 15:57:14 -08001975 boolean retVal = it.isUserDataEnabled(subId);
Wink Saville36ffb042014-12-05 11:10:30 -08001976 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1977 + " retVal=" + retVal);
1978 return retVal;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001979 } catch (RemoteException e) {
1980 throw e.rethrowFromSystemServer();
1981 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001982 }
Wink Saville36ffb042014-12-05 11:10:30 -08001983 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001984 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001985 }
1986
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001987 /**
Robert Greenwaltb2489872014-09-04 16:44:35 -07001988 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt6078b502014-06-11 16:05:07 -07001989 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001990 */
1991 public interface OnNetworkActiveListener {
1992 /**
1993 * Called on the main thread of the process to report that the current data network
1994 * has become active, and it is now a good time to perform any pending network
1995 * operations. Note that this listener only tells you when the network becomes
1996 * active; if at any other time you want to know whether it is active (and thus okay
1997 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001998 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001999 */
Chalard Jean4d660112018-06-04 16:52:49 +09002000 void onNetworkActive();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002001 }
2002
2003 private INetworkManagementService getNetworkManagementService() {
2004 synchronized (this) {
2005 if (mNMService != null) {
2006 return mNMService;
2007 }
2008 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
2009 mNMService = INetworkManagementService.Stub.asInterface(b);
2010 return mNMService;
2011 }
2012 }
2013
2014 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
Chalard Jean4d660112018-06-04 16:52:49 +09002015 mNetworkActivityListeners = new ArrayMap<>();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002016
2017 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002018 * Start listening to reports when the system's default data network is active, meaning it is
2019 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
2020 * to determine the current state of the system's default network after registering the
2021 * listener.
2022 * <p>
2023 * If the process default network has been set with
Paul Jensen72db88e2015-03-10 10:54:12 -04002024 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt6078b502014-06-11 16:05:07 -07002025 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002026 *
2027 * @param l The listener to be told when the network is active.
2028 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07002029 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002030 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
2031 @Override
2032 public void onNetworkActive() throws RemoteException {
2033 l.onNetworkActive();
2034 }
2035 };
2036
2037 try {
2038 getNetworkManagementService().registerNetworkActivityListener(rl);
2039 mNetworkActivityListeners.put(l, rl);
2040 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002041 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002042 }
2043 }
2044
2045 /**
2046 * Remove network active listener previously registered with
Robert Greenwaltb2489872014-09-04 16:44:35 -07002047 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002048 *
2049 * @param l Previously registered listener.
2050 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09002051 public void removeDefaultNetworkActiveListener(@NonNull OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002052 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
Hugo Benichie7678512017-05-09 15:19:01 +09002053 Preconditions.checkArgument(rl != null, "Listener was not registered.");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002054 try {
2055 getNetworkManagementService().unregisterNetworkActivityListener(rl);
2056 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002057 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002058 }
2059 }
2060
2061 /**
2062 * Return whether the data network is currently active. An active network means that
2063 * it is currently in a high power state for performing data transmission. On some
2064 * types of networks, it may be expensive to move and stay in such a state, so it is
2065 * more power efficient to batch network traffic together when the radio is already in
2066 * this state. This method tells you whether right now is currently a good time to
2067 * initiate network traffic, as the network is already active.
2068 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002069 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002070 try {
2071 return getNetworkManagementService().isNetworkActive();
2072 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002073 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002074 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002075 }
2076
2077 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002078 * {@hide}
2079 */
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09002080 public ConnectivityManager(Context context, IConnectivityManager service) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09002081 mContext = Preconditions.checkNotNull(context, "missing context");
2082 mService = Preconditions.checkNotNull(service, "missing IConnectivityManager");
Paul Jensene0bef712014-12-10 15:12:18 -05002083 sInstance = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002084 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002085
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07002086 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002087 @UnsupportedAppUsage
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07002088 public static ConnectivityManager from(Context context) {
2089 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
2090 }
2091
Remi NGUYEN VANe67b0c32018-12-27 16:43:56 +09002092 /** @hide */
2093 public NetworkRequest getDefaultRequest() {
2094 try {
2095 // This is not racy as the default request is final in ConnectivityService.
2096 return mService.getDefaultRequest();
2097 } catch (RemoteException e) {
2098 throw e.rethrowFromSystemServer();
2099 }
2100 }
2101
Lorenzo Colittifbe9b1a2016-07-28 17:14:11 +09002102 /* TODO: These permissions checks don't belong in client-side code. Move them to
2103 * services.jar, possibly in com.android.server.net. */
2104
2105 /** {@hide} */
Lorenzo Colittid5427052015-10-15 16:29:00 +09002106 public static final void enforceChangePermission(Context context) {
2107 int uid = Binder.getCallingUid();
2108 Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
2109 .getPackageNameForUid(context, uid), true /* throwException */);
2110 }
2111
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002112 /** {@hide} */
2113 public static final void enforceTetherChangePermission(Context context, String callingPkg) {
Hugo Benichie7678512017-05-09 15:19:01 +09002114 Preconditions.checkNotNull(context, "Context cannot be null");
2115 Preconditions.checkNotNull(callingPkg, "callingPkg cannot be null");
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002116
Robert Greenwaltedb47662014-09-16 17:54:19 -07002117 if (context.getResources().getStringArray(
2118 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
2119 // Have a provisioning app - must only let system apps (which check this app)
2120 // turn on tethering
2121 context.enforceCallingOrSelfPermission(
Jeremy Kleind42209d2015-12-28 15:11:58 -08002122 android.Manifest.permission.TETHER_PRIVILEGED, "ConnectivityService");
Robert Greenwaltedb47662014-09-16 17:54:19 -07002123 } else {
Billy Laua7238a32015-08-01 12:45:02 +01002124 int uid = Binder.getCallingUid();
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002125 // If callingPkg's uid is not same as Binder.getCallingUid(),
2126 // AppOpsService throws SecurityException.
2127 Settings.checkAndNoteWriteSettingsOperation(context, uid, callingPkg,
2128 true /* throwException */);
Robert Greenwaltedb47662014-09-16 17:54:19 -07002129 }
2130 }
2131
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002132 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002133 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2134 * situations where a Context pointer is unavailable.
2135 * @hide
2136 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002137 @Deprecated
Paul Jensen72db88e2015-03-10 10:54:12 -04002138 static ConnectivityManager getInstanceOrNull() {
2139 return sInstance;
2140 }
2141
2142 /**
2143 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2144 * situations where a Context pointer is unavailable.
2145 * @hide
2146 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002147 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002148 @UnsupportedAppUsage
Paul Jensen72db88e2015-03-10 10:54:12 -04002149 private static ConnectivityManager getInstance() {
2150 if (getInstanceOrNull() == null) {
Paul Jensene0bef712014-12-10 15:12:18 -05002151 throw new IllegalStateException("No ConnectivityManager yet constructed");
2152 }
Paul Jensen72db88e2015-03-10 10:54:12 -04002153 return getInstanceOrNull();
Paul Jensene0bef712014-12-10 15:12:18 -05002154 }
2155
2156 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002157 * Get the set of tetherable, available interfaces. This list is limited by
2158 * device configuration and current interface existence.
2159 *
2160 * @return an array of 0 or more Strings of tetherable interface names.
2161 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002162 * {@hide}
2163 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002164 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002165 @UnsupportedAppUsage
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002166 public String[] getTetherableIfaces() {
2167 try {
2168 return mService.getTetherableIfaces();
2169 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002170 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002171 }
2172 }
2173
2174 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002175 * Get the set of tethered interfaces.
2176 *
2177 * @return an array of 0 or more String of currently tethered interface names.
2178 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002179 * {@hide}
2180 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002181 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002182 @UnsupportedAppUsage
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002183 public String[] getTetheredIfaces() {
2184 try {
2185 return mService.getTetheredIfaces();
2186 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002187 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002188 }
2189 }
2190
2191 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002192 * Get the set of interface names which attempted to tether but
2193 * failed. Re-attempting to tether may cause them to reset to the Tethered
2194 * state. Alternatively, causing the interface to be destroyed and recreated
2195 * may cause them to reset to the available state.
2196 * {@link ConnectivityManager#getLastTetherError} can be used to get more
2197 * information on the cause of the errors.
2198 *
2199 * @return an array of 0 or more String indicating the interface names
2200 * which failed to tether.
2201 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002202 * {@hide}
2203 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002204 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002205 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002206 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002207 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08002208 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002209 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002210 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002211 }
2212 }
2213
2214 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07002215 * Get the set of tethered dhcp ranges.
2216 *
2217 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
2218 * {@hide}
2219 */
2220 public String[] getTetheredDhcpRanges() {
2221 try {
2222 return mService.getTetheredDhcpRanges();
2223 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002224 throw e.rethrowFromSystemServer();
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07002225 }
2226 }
2227
2228 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002229 * Attempt to tether the named interface. This will setup a dhcp server
2230 * on the interface, forward and NAT IP packets and forward DNS requests
2231 * to the best active upstream network interface. Note that if no upstream
2232 * IP network interface is available, dhcp will still run and traffic will be
2233 * allowed between the tethered devices and this device, though upstream net
2234 * access will of course fail until an upstream network interface becomes
2235 * active.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002236 *
2237 * <p>This method requires the caller to hold either the
2238 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2239 * or the ability to modify system settings as determined by
2240 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002241 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002242 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2243 * and WifiStateMachine which need direct access. All other clients should use
2244 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2245 * logic.</p>
2246 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002247 * @param iface the interface name to tether.
2248 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2249 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002250 * {@hide}
2251 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002252 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002253 public int tether(String iface) {
2254 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002255 String pkgName = mContext.getOpPackageName();
2256 Log.i(TAG, "tether caller:" + pkgName);
2257 return mService.tether(iface, pkgName);
Robert Greenwalt5a735062010-03-02 17:25:02 -08002258 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002259 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002260 }
2261 }
2262
2263 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002264 * Stop tethering the named interface.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002265 *
2266 * <p>This method requires the caller to hold either the
2267 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2268 * or the ability to modify system settings as determined by
2269 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002270 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002271 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2272 * and WifiStateMachine which need direct access. All other clients should use
2273 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2274 * logic.</p>
2275 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002276 * @param iface the interface name to untether.
2277 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2278 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002279 * {@hide}
2280 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002281 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002282 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002283 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002284 String pkgName = mContext.getOpPackageName();
2285 Log.i(TAG, "untether caller:" + pkgName);
2286 return mService.untether(iface, pkgName);
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002287 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002288 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002289 }
2290 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002291
2292 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002293 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002294 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002295 * due to device configuration.
2296 *
Chalard Jean8f76fc32017-09-26 15:45:18 +09002297 * <p>If this app does not have permission to use this API, it will always
2298 * return false rather than throw an exception.</p>
2299 *
2300 * <p>If the device has a hotspot provisioning app, the caller is required to hold the
2301 * {@link android.Manifest.permission.TETHER_PRIVILEGED} permission.</p>
2302 *
2303 * <p>Otherwise, this method requires the caller to hold the ability to modify system
2304 * settings as determined by {@link android.provider.Settings.System#canWrite}.</p>
2305 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002306 * @return a boolean - {@code true} indicating Tethering is supported.
2307 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002308 * {@hide}
2309 */
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002310 @SystemApi
Chalard Jean8f76fc32017-09-26 15:45:18 +09002311 @RequiresPermission(anyOf = {android.Manifest.permission.TETHER_PRIVILEGED,
2312 android.Manifest.permission.WRITE_SETTINGS})
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002313 public boolean isTetheringSupported() {
Chalard Jean8f76fc32017-09-26 15:45:18 +09002314 String pkgName = mContext.getOpPackageName();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002315 try {
Udam Saini0e94c362017-06-07 12:06:28 -07002316 return mService.isTetheringSupported(pkgName);
Chalard Jean8f76fc32017-09-26 15:45:18 +09002317 } catch (SecurityException e) {
2318 // This API is not available to this caller, but for backward-compatibility
2319 // this will just return false instead of throwing.
2320 return false;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002321 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002322 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002323 }
2324 }
2325
2326 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002327 * Callback for use with {@link #startTethering} to find out whether tethering succeeded.
2328 * @hide
2329 */
2330 @SystemApi
2331 public static abstract class OnStartTetheringCallback {
2332 /**
2333 * Called when tethering has been successfully started.
2334 */
Chalard Jean4d660112018-06-04 16:52:49 +09002335 public void onTetheringStarted() {}
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002336
2337 /**
2338 * Called when starting tethering failed.
2339 */
Chalard Jean4d660112018-06-04 16:52:49 +09002340 public void onTetheringFailed() {}
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002341 }
2342
2343 /**
2344 * Convenient overload for
2345 * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null
2346 * handler to run on the current thread's {@link Looper}.
2347 * @hide
2348 */
2349 @SystemApi
Udam Saini0e94c362017-06-07 12:06:28 -07002350 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002351 public void startTethering(int type, boolean showProvisioningUi,
2352 final OnStartTetheringCallback callback) {
2353 startTethering(type, showProvisioningUi, callback, null);
2354 }
2355
2356 /**
2357 * Runs tether provisioning for the given type if needed and then starts tethering if
2358 * the check succeeds. If no carrier provisioning is required for tethering, tethering is
2359 * enabled immediately. If provisioning fails, tethering will not be enabled. It also
2360 * schedules tether provisioning re-checks if appropriate.
2361 *
2362 * @param type The type of tethering to start. Must be one of
2363 * {@link ConnectivityManager.TETHERING_WIFI},
2364 * {@link ConnectivityManager.TETHERING_USB}, or
2365 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2366 * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there
2367 * is one. This should be true the first time this function is called and also any time
2368 * the user can see this UI. It gives users information from their carrier about the
2369 * check failing and how they can sign up for tethering if possible.
2370 * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller
2371 * of the result of trying to tether.
2372 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
2373 * @hide
2374 */
2375 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002376 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002377 public void startTethering(int type, boolean showProvisioningUi,
2378 final OnStartTetheringCallback callback, Handler handler) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09002379 Preconditions.checkNotNull(callback, "OnStartTetheringCallback cannot be null.");
Jeremy Klein5f277e12016-03-12 16:29:54 -08002380
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002381 ResultReceiver wrappedCallback = new ResultReceiver(handler) {
2382 @Override
2383 protected void onReceiveResult(int resultCode, Bundle resultData) {
2384 if (resultCode == TETHER_ERROR_NO_ERROR) {
2385 callback.onTetheringStarted();
2386 } else {
2387 callback.onTetheringFailed();
2388 }
2389 }
2390 };
Jeremy Klein5f277e12016-03-12 16:29:54 -08002391
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002392 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002393 String pkgName = mContext.getOpPackageName();
2394 Log.i(TAG, "startTethering caller:" + pkgName);
2395 mService.startTethering(type, wrappedCallback, showProvisioningUi, pkgName);
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002396 } catch (RemoteException e) {
2397 Log.e(TAG, "Exception trying to start tethering.", e);
2398 wrappedCallback.send(TETHER_ERROR_SERVICE_UNAVAIL, null);
2399 }
2400 }
2401
2402 /**
2403 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if
2404 * applicable.
2405 *
2406 * @param type The type of tethering to stop. Must be one of
2407 * {@link ConnectivityManager.TETHERING_WIFI},
2408 * {@link ConnectivityManager.TETHERING_USB}, or
2409 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2410 * @hide
2411 */
2412 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002413 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002414 public void stopTethering(int type) {
2415 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002416 String pkgName = mContext.getOpPackageName();
2417 Log.i(TAG, "stopTethering caller:" + pkgName);
2418 mService.stopTethering(type, pkgName);
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002419 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002420 throw e.rethrowFromSystemServer();
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002421 }
2422 }
2423
2424 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002425 * Get the list of regular expressions that define any tetherable
2426 * USB network interfaces. If USB tethering is not supported by the
2427 * device, this list should be empty.
2428 *
2429 * @return an array of 0 or more regular expression Strings defining
2430 * what interfaces are considered tetherable usb interfaces.
2431 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002432 * {@hide}
2433 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002434 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002435 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002436 public String[] getTetherableUsbRegexs() {
2437 try {
2438 return mService.getTetherableUsbRegexs();
2439 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002440 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002441 }
2442 }
2443
2444 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002445 * Get the list of regular expressions that define any tetherable
2446 * Wifi network interfaces. If Wifi tethering is not supported by the
2447 * device, this list should be empty.
2448 *
2449 * @return an array of 0 or more regular expression Strings defining
2450 * what interfaces are considered tetherable wifi interfaces.
2451 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002452 * {@hide}
2453 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002454 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002455 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002456 public String[] getTetherableWifiRegexs() {
2457 try {
2458 return mService.getTetherableWifiRegexs();
2459 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002460 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002461 }
2462 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08002463
Danica Chang6fdd0c62010-08-11 14:54:43 -07002464 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002465 * Get the list of regular expressions that define any tetherable
2466 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
2467 * device, this list should be empty.
2468 *
2469 * @return an array of 0 or more regular expression Strings defining
2470 * what interfaces are considered tetherable bluetooth interfaces.
2471 *
Danica Chang6fdd0c62010-08-11 14:54:43 -07002472 * {@hide}
2473 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002474 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002475 @UnsupportedAppUsage
Danica Chang6fdd0c62010-08-11 14:54:43 -07002476 public String[] getTetherableBluetoothRegexs() {
2477 try {
2478 return mService.getTetherableBluetoothRegexs();
2479 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002480 throw e.rethrowFromSystemServer();
Danica Chang6fdd0c62010-08-11 14:54:43 -07002481 }
2482 }
2483
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002484 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002485 * Attempt to both alter the mode of USB and Tethering of USB. A
2486 * utility method to deal with some of the complexity of USB - will
2487 * attempt to switch to Rndis and subsequently tether the resulting
2488 * interface on {@code true} or turn off tethering and switch off
2489 * Rndis on {@code false}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002490 *
2491 * <p>This method requires the caller to hold either the
2492 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2493 * or the ability to modify system settings as determined by
2494 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002495 *
2496 * @param enable a boolean - {@code true} to enable tethering
2497 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2498 *
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002499 * {@hide}
2500 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002501 @UnsupportedAppUsage
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002502 public int setUsbTethering(boolean enable) {
2503 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002504 String pkgName = mContext.getOpPackageName();
2505 Log.i(TAG, "setUsbTethering caller:" + pkgName);
2506 return mService.setUsbTethering(enable, pkgName);
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002507 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002508 throw e.rethrowFromSystemServer();
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002509 }
2510 }
2511
Robert Greenwalt5a735062010-03-02 17:25:02 -08002512 /** {@hide} */
2513 public static final int TETHER_ERROR_NO_ERROR = 0;
2514 /** {@hide} */
2515 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
2516 /** {@hide} */
2517 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
2518 /** {@hide} */
2519 public static final int TETHER_ERROR_UNSUPPORTED = 3;
2520 /** {@hide} */
2521 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
2522 /** {@hide} */
2523 public static final int TETHER_ERROR_MASTER_ERROR = 5;
2524 /** {@hide} */
2525 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
2526 /** {@hide} */
2527 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
2528 /** {@hide} */
2529 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
2530 /** {@hide} */
2531 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
2532 /** {@hide} */
2533 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002534 /** {@hide} */
2535 public static final int TETHER_ERROR_PROVISION_FAILED = 11;
Remi NGUYEN VAN0e3d09232018-12-04 12:13:09 +09002536 /** {@hide} */
2537 public static final int TETHER_ERROR_DHCPSERVER_ERROR = 12;
Robert Greenwalt5a735062010-03-02 17:25:02 -08002538
2539 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002540 * Get a more detailed error code after a Tethering or Untethering
2541 * request asynchronously failed.
2542 *
2543 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08002544 * @return error The error code of the last error tethering or untethering the named
2545 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002546 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002547 * {@hide}
2548 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002549 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002550 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002551 public int getLastTetherError(String iface) {
2552 try {
2553 return mService.getLastTetherError(iface);
2554 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002555 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002556 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002557 }
2558
2559 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002560 * Report network connectivity status. This is currently used only
2561 * to alter status bar UI.
Paul Jensenb2748922015-05-06 11:10:18 -04002562 * <p>This method requires the caller to hold the permission
2563 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002564 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002565 * @param networkType The type of network you want to report on
2566 * @param percentage The quality of the connection 0 is bad, 100 is good
Chalard Jean6b1da6e2018-03-08 13:54:53 +09002567 * @deprecated Types are deprecated. Use {@link #reportNetworkConnectivity} instead.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002568 * {@hide}
2569 */
2570 public void reportInetCondition(int networkType, int percentage) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09002571 printStackTrace();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002572 try {
2573 mService.reportInetCondition(networkType, percentage);
2574 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002575 throw e.rethrowFromSystemServer();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002576 }
2577 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002578
2579 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002580 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07002581 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002582 * the framework to re-evaluate network connectivity and/or switch to another
2583 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002584 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002585 * @param network The {@link Network} the application was attempting to use
2586 * or {@code null} to indicate the current default network.
Paul Jensenbfd17b72015-04-07 12:43:13 -04002587 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
2588 * working and non-working connectivity.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002589 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002590 @Deprecated
Chalard Jean50bea3d2019-01-07 19:26:34 +09002591 public void reportBadNetwork(@Nullable Network network) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09002592 printStackTrace();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002593 try {
Paul Jensenbfd17b72015-04-07 12:43:13 -04002594 // One of these will be ignored because it matches system's current state.
2595 // The other will trigger the necessary reevaluation.
2596 mService.reportNetworkConnectivity(network, true);
2597 mService.reportNetworkConnectivity(network, false);
2598 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002599 throw e.rethrowFromSystemServer();
Paul Jensenbfd17b72015-04-07 12:43:13 -04002600 }
2601 }
2602
2603 /**
2604 * Report to the framework whether a network has working connectivity.
2605 * This provides a hint to the system that a particular network is providing
2606 * working connectivity or not. In response the framework may re-evaluate
2607 * the network's connectivity and might take further action thereafter.
2608 *
2609 * @param network The {@link Network} the application was attempting to use
2610 * or {@code null} to indicate the current default network.
2611 * @param hasConnectivity {@code true} if the application was able to successfully access the
2612 * Internet using {@code network} or {@code false} if not.
2613 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09002614 public void reportNetworkConnectivity(@Nullable Network network, boolean hasConnectivity) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09002615 printStackTrace();
Paul Jensenbfd17b72015-04-07 12:43:13 -04002616 try {
2617 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002618 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002619 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002620 }
2621 }
2622
2623 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002624 * Set a network-independent global http proxy. This is not normally what you want
2625 * for typical HTTP proxies - they are general network dependent. However if you're
2626 * doing something unusual like general internal filtering this may be useful. On
2627 * a private network where the proxy is not accessible, you may break HTTP using this.
Paul Jensenb2748922015-05-06 11:10:18 -04002628 *
2629 * @param p A {@link ProxyInfo} object defining the new global
2630 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002631 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002632 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002633 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Jason Monk207900c2014-04-25 15:00:09 -04002634 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002635 try {
2636 mService.setGlobalProxy(p);
2637 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002638 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002639 }
2640 }
2641
2642 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002643 * Retrieve any network-independent global HTTP proxy.
2644 *
Jason Monk207900c2014-04-25 15:00:09 -04002645 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002646 * if no global HTTP proxy is set.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002647 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002648 */
Jason Monk207900c2014-04-25 15:00:09 -04002649 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002650 try {
2651 return mService.getGlobalProxy();
2652 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002653 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002654 }
2655 }
2656
2657 /**
Paul Jensencee9b512015-05-06 07:32:40 -04002658 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
2659 * network-specific HTTP proxy. If {@code network} is null, the
2660 * network-specific proxy returned is the proxy of the default active
2661 * network.
2662 *
2663 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
2664 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
2665 * or when {@code network} is {@code null},
2666 * the {@code ProxyInfo} for the default active network. Returns
2667 * {@code null} when no proxy applies or the caller doesn't have
2668 * permission to use {@code network}.
2669 * @hide
2670 */
2671 public ProxyInfo getProxyForNetwork(Network network) {
2672 try {
2673 return mService.getProxyForNetwork(network);
2674 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002675 throw e.rethrowFromSystemServer();
Paul Jensencee9b512015-05-06 07:32:40 -04002676 }
2677 }
2678
2679 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002680 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
2681 * otherwise if this process is bound to a {@link Network} using
Paul Jensen72db88e2015-03-10 10:54:12 -04002682 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensene0bef712014-12-10 15:12:18 -05002683 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002684 *
Jason Monk207900c2014-04-25 15:00:09 -04002685 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002686 * HTTP proxy is active.
Robert Greenwalt434203a2010-10-11 16:00:27 -07002687 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09002688 @Nullable
Paul Jensene0bef712014-12-10 15:12:18 -05002689 public ProxyInfo getDefaultProxy() {
Paul Jensencee9b512015-05-06 07:32:40 -04002690 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwalt434203a2010-10-11 16:00:27 -07002691 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07002692
2693 /**
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002694 * Returns true if the hardware supports the given network type
2695 * else it returns false. This doesn't indicate we have coverage
2696 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002697 * hardware supports it. For example a GSM phone without a SIM
2698 * should still return {@code true} for mobile data, but a wifi only
2699 * tablet would return {@code false}.
2700 *
2701 * @param networkType The network type we'd like to check
2702 * @return {@code true} if supported, else {@code false}
Chalard Jean6b1da6e2018-03-08 13:54:53 +09002703 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002704 * @hide
2705 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +09002706 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002707 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002708 @UnsupportedAppUsage
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002709 public boolean isNetworkSupported(int networkType) {
2710 try {
2711 return mService.isNetworkSupported(networkType);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002712 } catch (RemoteException e) {
2713 throw e.rethrowFromSystemServer();
2714 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002715 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002716
2717 /**
2718 * Returns if the currently active data network is metered. A network is
2719 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002720 * that connection due to monetary costs, data limitations or
2721 * battery/performance issues. You should check this before doing large
2722 * data transfers, and warn the user or delay the operation until another
2723 * network is available.
2724 *
2725 * @return {@code true} if large transfers should be avoided, otherwise
2726 * {@code false}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002727 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002728 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002729 public boolean isActiveNetworkMetered() {
2730 try {
2731 return mService.isActiveNetworkMetered();
2732 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002733 throw e.rethrowFromSystemServer();
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002734 }
2735 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002736
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002737 /**
2738 * If the LockdownVpn mechanism is enabled, updates the vpn
2739 * with a reload of its profile.
2740 *
2741 * @return a boolean with {@code} indicating success
2742 *
2743 * <p>This method can only be called by the system UID
2744 * {@hide}
2745 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002746 public boolean updateLockdownVpn() {
2747 try {
2748 return mService.updateLockdownVpn();
2749 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002750 throw e.rethrowFromSystemServer();
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002751 }
2752 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07002753
2754 /**
Wink Saville948282b2013-08-29 08:55:16 -07002755 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07002756 *
Wink Savilleab9321d2013-06-29 21:10:57 -07002757 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07002758 *
2759 * @return time out that will be used, maybe less that suggestedTimeOutMs
2760 * -1 if an error.
2761 *
2762 * {@hide}
2763 */
Wink Saville948282b2013-08-29 08:55:16 -07002764 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07002765 int timeOutMs = -1;
2766 try {
Wink Saville948282b2013-08-29 08:55:16 -07002767 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07002768 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002769 throw e.rethrowFromSystemServer();
Wink Savilleab9321d2013-06-29 21:10:57 -07002770 }
2771 return timeOutMs;
2772 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002773
2774 /**
Wink Saville42d4f082013-07-20 20:31:59 -07002775 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002776 * {@hide}
2777 */
2778 public String getMobileProvisioningUrl() {
2779 try {
2780 return mService.getMobileProvisioningUrl();
2781 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002782 throw e.rethrowFromSystemServer();
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002783 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002784 }
Wink Saville42d4f082013-07-20 20:31:59 -07002785
2786 /**
Wink Saville948282b2013-08-29 08:55:16 -07002787 * Set sign in error notification to visible or in visible
2788 *
Wink Saville948282b2013-08-29 08:55:16 -07002789 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04002790 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville948282b2013-08-29 08:55:16 -07002791 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002792 @Deprecated
Wink Saville948282b2013-08-29 08:55:16 -07002793 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04002794 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07002795 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04002796 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07002797 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002798 throw e.rethrowFromSystemServer();
Wink Saville948282b2013-08-29 08:55:16 -07002799 }
2800 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002801
2802 /**
2803 * Set the value for enabling/disabling airplane mode
2804 *
2805 * @param enable whether to enable airplane mode or not
2806 *
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002807 * @hide
2808 */
Lorenzo Colittif5845d12018-10-09 18:55:11 +09002809 @RequiresPermission(anyOf = {
2810 android.Manifest.permission.NETWORK_SETTINGS,
2811 android.Manifest.permission.NETWORK_SETUP_WIZARD,
2812 android.Manifest.permission.NETWORK_STACK})
Lorenzo Colitti85eca482018-10-09 18:50:32 +09002813 @SystemApi
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002814 public void setAirplaneMode(boolean enable) {
2815 try {
2816 mService.setAirplaneMode(enable);
2817 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002818 throw e.rethrowFromSystemServer();
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002819 }
2820 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002821
2822 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002823 @UnsupportedAppUsage
Robert Greenwalta67be032014-05-16 15:49:14 -07002824 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07002825 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07002826 mService.registerNetworkFactory(messenger, name);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002827 } catch (RemoteException e) {
2828 throw e.rethrowFromSystemServer();
2829 }
Robert Greenwalta67be032014-05-16 15:49:14 -07002830 }
2831
2832 /** {@hide} */
Mathew Inwood55418ea2018-12-20 15:30:45 +00002833 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
Robert Greenwalta67be032014-05-16 15:49:14 -07002834 public void unregisterNetworkFactory(Messenger messenger) {
2835 try {
2836 mService.unregisterNetworkFactory(messenger);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002837 } catch (RemoteException e) {
2838 throw e.rethrowFromSystemServer();
2839 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002840 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002841
Paul Jensen31a94f42015-02-13 14:18:39 -05002842 /**
2843 * @hide
2844 * Register a NetworkAgent with ConnectivityService.
2845 * @return NetID corresponding to NetworkAgent.
2846 */
2847 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002848 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002849 try {
Paul Jensen31a94f42015-02-13 14:18:39 -05002850 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
2851 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002852 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -05002853 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002854 }
2855
Robert Greenwalt9258c642014-03-26 16:47:06 -07002856 /**
Hugo Benichidafed3d2017-03-06 09:17:06 +09002857 * Base class for {@code NetworkRequest} callbacks. Used for notifications about network
2858 * changes. Should be extended by applications wanting notifications.
2859 *
2860 * A {@code NetworkCallback} is registered by calling
2861 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
2862 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)},
Hugo Benichica867dc2018-02-07 21:17:43 +09002863 * or {@link #registerDefaultNetworkCallback(NetworkCallback)}. A {@code NetworkCallback} is
Hugo Benichidafed3d2017-03-06 09:17:06 +09002864 * unregistered by calling {@link #unregisterNetworkCallback(NetworkCallback)}.
2865 * A {@code NetworkCallback} should be registered at most once at any time.
2866 * A {@code NetworkCallback} that has been unregistered can be registered again.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002867 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002868 public static class NetworkCallback {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002869 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002870 * Called when the framework connects to a new network to evaluate whether it satisfies this
2871 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
2872 * callback. There is no guarantee that this new network will satisfy any requests, or that
2873 * the network will stay connected for longer than the time necessary to evaluate it.
2874 * <p>
2875 * Most applications <b>should not</b> act on this callback, and should instead use
2876 * {@link #onAvailable}. This callback is intended for use by applications that can assist
2877 * the framework in properly evaluating the network &mdash; for example, an application that
2878 * can automatically log in to a captive portal without user intervention.
2879 *
2880 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti66276122015-06-11 14:27:17 +09002881 *
2882 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002883 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002884 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002885
2886 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002887 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002888 * This callback may be called more than once if the {@link Network} that is
2889 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002890 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002891 * @param network The {@link Network} of the satisfying network.
Chalard Jean804b8fb2018-01-30 22:41:41 +09002892 * @param networkCapabilities The {@link NetworkCapabilities} of the satisfying network.
2893 * @param linkProperties The {@link LinkProperties} of the satisfying network.
junyulai05986c62018-08-07 19:50:45 +08002894 * @param blocked Whether access to the {@link Network} is blocked due to system policy.
Chalard Jean804b8fb2018-01-30 22:41:41 +09002895 * @hide
2896 */
2897 public void onAvailable(Network network, NetworkCapabilities networkCapabilities,
junyulai05986c62018-08-07 19:50:45 +08002898 LinkProperties linkProperties, boolean blocked) {
Chalard Jean804b8fb2018-01-30 22:41:41 +09002899 // Internally only this method is called when a new network is available, and
2900 // it calls the callback in the same way and order that older versions used
2901 // to call so as not to change the behavior.
2902 onAvailable(network);
2903 if (!networkCapabilities.hasCapability(
2904 NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)) {
2905 onNetworkSuspended(network);
2906 }
2907 onCapabilitiesChanged(network, networkCapabilities);
2908 onLinkPropertiesChanged(network, linkProperties);
junyulai05986c62018-08-07 19:50:45 +08002909 onBlockedStatusChanged(network, blocked);
Chalard Jean804b8fb2018-01-30 22:41:41 +09002910 }
2911
2912 /**
2913 * Called when the framework connects and has declared a new network ready for use.
2914 * This callback may be called more than once if the {@link Network} that is
2915 * satisfying the request changes. This will always immediately be followed by a
2916 * call to {@link #onCapabilitiesChanged(Network, NetworkCapabilities)} then by a
junyulai05986c62018-08-07 19:50:45 +08002917 * call to {@link #onLinkPropertiesChanged(Network, LinkProperties)}, and a call to
2918 * {@link #onBlockedStatusChanged(Network, boolean)}.
Chalard Jean804b8fb2018-01-30 22:41:41 +09002919 *
2920 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002921 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002922 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002923
2924 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002925 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002926 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002927 * for graceful handover. This may not be called if we have a hard loss
2928 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002929 * {@link NetworkCallback#onLost} call or a
2930 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002931 * on whether we lose or regain it.
2932 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002933 * @param network The {@link Network} that is about to be disconnected.
2934 * @param maxMsToLive The time in ms the framework will attempt to keep the
2935 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002936 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002937 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002938 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002939
2940 /**
2941 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002942 * graceful failure ends.
2943 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002944 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002945 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002946 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002947
2948 /**
Etan Cohenaebf17e2017-03-01 12:47:28 -08002949 * Called if no network is found in the timeout time specified in
Hugo Benichi0eec03f2017-05-15 15:15:33 +09002950 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} call. This callback is not
Etan Cohenaebf17e2017-03-01 12:47:28 -08002951 * called for the version of {@link #requestNetwork(NetworkRequest, NetworkCallback)}
2952 * without timeout. When this callback is invoked the associated
2953 * {@link NetworkRequest} will have already been removed and released, as if
2954 * {@link #unregisterNetworkCallback(NetworkCallback)} had been called.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002955 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002956 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002957
2958 /**
2959 * Called when the network the framework connected to for this request
2960 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002961 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002962 * @param network The {@link Network} whose capabilities have changed.
Chalard Jean804b8fb2018-01-30 22:41:41 +09002963 * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this
2964 * network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002965 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002966 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002967 NetworkCapabilities networkCapabilities) {}
2968
2969 /**
2970 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002971 * changes {@link LinkProperties}.
2972 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002973 * @param network The {@link Network} whose link properties have changed.
2974 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002975 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002976 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002977
Robert Greenwalt8d482522015-06-24 13:23:42 -07002978 /**
2979 * Called when the network the framework connected to for this request
Chalard Jean804b8fb2018-01-30 22:41:41 +09002980 * goes into {@link NetworkInfo.State#SUSPENDED}.
Robert Greenwalt8d482522015-06-24 13:23:42 -07002981 * This generally means that while the TCP connections are still live,
2982 * temporarily network data fails to transfer. Specifically this is used
2983 * on cellular networks to mask temporary outages when driving through
2984 * a tunnel, etc.
2985 * @hide
2986 */
2987 public void onNetworkSuspended(Network network) {}
2988
2989 /**
2990 * Called when the network the framework connected to for this request
Chalard Jean804b8fb2018-01-30 22:41:41 +09002991 * returns from a {@link NetworkInfo.State#SUSPENDED} state. This should always be
2992 * preceded by a matching {@link NetworkCallback#onNetworkSuspended} call.
Robert Greenwalt8d482522015-06-24 13:23:42 -07002993 * @hide
2994 */
2995 public void onNetworkResumed(Network network) {}
2996
junyulai05986c62018-08-07 19:50:45 +08002997 /**
2998 * Called when access to the specified network is blocked or unblocked.
2999 *
3000 * @param network The {@link Network} whose blocked status has changed.
3001 * @param blocked The blocked status of this {@link Network}.
3002 */
3003 public void onBlockedStatusChanged(Network network, boolean blocked) {}
3004
Robert Greenwalt6078b502014-06-11 16:05:07 -07003005 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07003006 }
3007
Hugo Benichicb883232017-05-11 13:16:17 +09003008 /**
3009 * Constant error codes used by ConnectivityService to communicate about failures and errors
3010 * across a Binder boundary.
3011 * @hide
3012 */
3013 public interface Errors {
Chalard Jean4d660112018-06-04 16:52:49 +09003014 int TOO_MANY_REQUESTS = 1;
Hugo Benichicb883232017-05-11 13:16:17 +09003015 }
3016
3017 /** @hide */
3018 public static class TooManyRequestsException extends RuntimeException {}
3019
3020 private static RuntimeException convertServiceException(ServiceSpecificException e) {
3021 switch (e.errorCode) {
3022 case Errors.TOO_MANY_REQUESTS:
3023 return new TooManyRequestsException();
3024 default:
3025 Log.w(TAG, "Unknown service error code " + e.errorCode);
3026 return new RuntimeException(e);
3027 }
3028 }
3029
Robert Greenwalt9258c642014-03-26 16:47:06 -07003030 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003031 /** @hide */
Robert Greenwalt8d482522015-06-24 13:23:42 -07003032 public static final int CALLBACK_PRECHECK = BASE + 1;
3033 /** @hide */
3034 public static final int CALLBACK_AVAILABLE = BASE + 2;
3035 /** @hide arg1 = TTL */
3036 public static final int CALLBACK_LOSING = BASE + 3;
3037 /** @hide */
3038 public static final int CALLBACK_LOST = BASE + 4;
3039 /** @hide */
3040 public static final int CALLBACK_UNAVAIL = BASE + 5;
3041 /** @hide */
3042 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
3043 /** @hide */
3044 public static final int CALLBACK_IP_CHANGED = BASE + 7;
Robert Greenwalt562cc542014-05-15 18:07:26 -07003045 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Hugo Benichidba33db2017-03-23 22:40:44 +09003046 private static final int EXPIRE_LEGACY_REQUEST = BASE + 8;
Robert Greenwalt8d482522015-06-24 13:23:42 -07003047 /** @hide */
Hugo Benichidba33db2017-03-23 22:40:44 +09003048 public static final int CALLBACK_SUSPENDED = BASE + 9;
Robert Greenwalt8d482522015-06-24 13:23:42 -07003049 /** @hide */
Hugo Benichidba33db2017-03-23 22:40:44 +09003050 public static final int CALLBACK_RESUMED = BASE + 10;
junyulai05986c62018-08-07 19:50:45 +08003051 /** @hide */
3052 public static final int CALLBACK_BLK_CHANGED = BASE + 11;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003053
Erik Kline57faba92015-11-25 12:49:38 +09003054 /** @hide */
3055 public static String getCallbackName(int whichCallback) {
3056 switch (whichCallback) {
3057 case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK";
3058 case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE";
3059 case CALLBACK_LOSING: return "CALLBACK_LOSING";
3060 case CALLBACK_LOST: return "CALLBACK_LOST";
3061 case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL";
3062 case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED";
3063 case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED";
Erik Kline57faba92015-11-25 12:49:38 +09003064 case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST";
3065 case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED";
3066 case CALLBACK_RESUMED: return "CALLBACK_RESUMED";
junyulai05986c62018-08-07 19:50:45 +08003067 case CALLBACK_BLK_CHANGED: return "CALLBACK_BLK_CHANGED";
Erik Kline57faba92015-11-25 12:49:38 +09003068 default:
3069 return Integer.toString(whichCallback);
3070 }
3071 }
3072
Robert Greenwalt562cc542014-05-15 18:07:26 -07003073 private class CallbackHandler extends Handler {
Robert Greenwalt9258c642014-03-26 16:47:06 -07003074 private static final String TAG = "ConnectivityManager.CallbackHandler";
Robert Greenwalta9ebeef2015-09-03 16:41:45 -07003075 private static final boolean DBG = false;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003076
Hugo Benichid42650f2016-07-06 22:53:17 +09003077 CallbackHandler(Looper looper) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07003078 super(looper);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003079 }
3080
Hugo Benichi2583ef02017-02-02 17:02:36 +09003081 CallbackHandler(Handler handler) {
Hugo Benichie7678512017-05-09 15:19:01 +09003082 this(Preconditions.checkNotNull(handler, "Handler cannot be null.").getLooper());
Hugo Benichi2583ef02017-02-02 17:02:36 +09003083 }
3084
Robert Greenwalt9258c642014-03-26 16:47:06 -07003085 @Override
3086 public void handleMessage(Message message) {
Hugo Benichi2c684522017-05-09 14:36:02 +09003087 if (message.what == EXPIRE_LEGACY_REQUEST) {
3088 expireRequest((NetworkCapabilities) message.obj, message.arg1);
3089 return;
3090 }
3091
3092 final NetworkRequest request = getObject(message, NetworkRequest.class);
3093 final Network network = getObject(message, Network.class);
3094 final NetworkCallback callback;
3095 synchronized (sCallbacks) {
3096 callback = sCallbacks.get(request);
3097 }
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09003098 if (DBG) {
Hugo Benichia0385682017-03-22 17:07:57 +09003099 Log.d(TAG, getCallbackName(message.what) + " for network " + network);
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09003100 }
Hugo Benichi2c684522017-05-09 14:36:02 +09003101 if (callback == null) {
3102 Log.w(TAG, "callback not found for " + getCallbackName(message.what) + " message");
3103 return;
3104 }
3105
Robert Greenwalt9258c642014-03-26 16:47:06 -07003106 switch (message.what) {
3107 case CALLBACK_PRECHECK: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003108 callback.onPreCheck(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003109 break;
3110 }
3111 case CALLBACK_AVAILABLE: {
Chalard Jean804b8fb2018-01-30 22:41:41 +09003112 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
3113 LinkProperties lp = getObject(message, LinkProperties.class);
junyulai05986c62018-08-07 19:50:45 +08003114 callback.onAvailable(network, cap, lp, message.arg1 != 0);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003115 break;
3116 }
3117 case CALLBACK_LOSING: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003118 callback.onLosing(network, message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003119 break;
3120 }
3121 case CALLBACK_LOST: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003122 callback.onLost(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003123 break;
3124 }
3125 case CALLBACK_UNAVAIL: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003126 callback.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07003127 break;
3128 }
3129 case CALLBACK_CAP_CHANGED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003130 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
3131 callback.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003132 break;
3133 }
3134 case CALLBACK_IP_CHANGED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003135 LinkProperties lp = getObject(message, LinkProperties.class);
3136 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003137 break;
3138 }
Robert Greenwalt8d482522015-06-24 13:23:42 -07003139 case CALLBACK_SUSPENDED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003140 callback.onNetworkSuspended(network);
Robert Greenwalt8d482522015-06-24 13:23:42 -07003141 break;
3142 }
3143 case CALLBACK_RESUMED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003144 callback.onNetworkResumed(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07003145 break;
3146 }
junyulai05986c62018-08-07 19:50:45 +08003147 case CALLBACK_BLK_CHANGED: {
3148 boolean blocked = message.arg1 != 0;
3149 callback.onBlockedStatusChanged(network, blocked);
3150 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003151 }
3152 }
3153
Hugo Benichid42650f2016-07-06 22:53:17 +09003154 private <T> T getObject(Message msg, Class<T> c) {
3155 return (T) msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07003156 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003157 }
3158
Hugo Benichi2583ef02017-02-02 17:02:36 +09003159 private CallbackHandler getDefaultHandler() {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003160 synchronized (sCallbacks) {
3161 if (sCallbackHandler == null) {
3162 sCallbackHandler = new CallbackHandler(ConnectivityThread.getInstanceLooper());
Robert Greenwalt9258c642014-03-26 16:47:06 -07003163 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003164 return sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003165 }
3166 }
3167
Hugo Benichi6f260f32017-02-03 14:18:44 +09003168 private static final HashMap<NetworkRequest, NetworkCallback> sCallbacks = new HashMap<>();
3169 private static CallbackHandler sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003170
Hugo Benichi6f260f32017-02-03 14:18:44 +09003171 private static final int LISTEN = 1;
3172 private static final int REQUEST = 2;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003173
Hugo Benichi6f260f32017-02-03 14:18:44 +09003174 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, NetworkCallback callback,
3175 int timeoutMs, int action, int legacyType, CallbackHandler handler) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09003176 printStackTrace();
Hugo Benichie7678512017-05-09 15:19:01 +09003177 checkCallbackNotNull(callback);
Hugo Benichidafed3d2017-03-06 09:17:06 +09003178 Preconditions.checkArgument(action == REQUEST || need != null, "null NetworkCapabilities");
Hugo Benichid42650f2016-07-06 22:53:17 +09003179 final NetworkRequest request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003180 try {
Hugo Benichid42650f2016-07-06 22:53:17 +09003181 synchronized(sCallbacks) {
Hugo Benichi31c176d2017-06-17 13:14:12 +09003182 if (callback.networkRequest != null
3183 && callback.networkRequest != ALREADY_UNREGISTERED) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09003184 // TODO: throw exception instead and enforce 1:1 mapping of callbacks
3185 // and requests (http://b/20701525).
3186 Log.e(TAG, "NetworkCallback was already registered");
3187 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003188 Messenger messenger = new Messenger(handler);
Hugo Benichid42650f2016-07-06 22:53:17 +09003189 Binder binder = new Binder();
Paul Jensen7221cc32014-06-27 11:05:32 -04003190 if (action == LISTEN) {
Hugo Benichid42650f2016-07-06 22:53:17 +09003191 request = mService.listenForNetwork(need, messenger, binder);
Paul Jensen7221cc32014-06-27 11:05:32 -04003192 } else {
Hugo Benichid42650f2016-07-06 22:53:17 +09003193 request = mService.requestNetwork(
3194 need, messenger, timeoutMs, binder, legacyType);
Paul Jensen7221cc32014-06-27 11:05:32 -04003195 }
Hugo Benichid42650f2016-07-06 22:53:17 +09003196 if (request != null) {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003197 sCallbacks.put(request, callback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003198 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003199 callback.networkRequest = request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003200 }
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003201 } catch (RemoteException e) {
3202 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003203 } catch (ServiceSpecificException e) {
3204 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003205 }
Hugo Benichid42650f2016-07-06 22:53:17 +09003206 return request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003207 }
3208
3209 /**
Erik Klinea2d29402016-03-16 15:31:39 +09003210 * Helper function to request a network with a particular legacy type.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09003211 *
3212 * This is temporarily public @hide so it can be called by system code that uses the
3213 * NetworkRequest API to request networks but relies on CONNECTIVITY_ACTION broadcasts for
3214 * instead network notifications.
3215 *
3216 * TODO: update said system code to rely on NetworkCallbacks and make this method private.
3217 *
3218 * @hide
3219 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003220 public void requestNetwork(@NonNull NetworkRequest request,
3221 @NonNull NetworkCallback networkCallback, int timeoutMs, int legacyType,
3222 @NonNull Handler handler) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003223 CallbackHandler cbHandler = new CallbackHandler(handler);
3224 NetworkCapabilities nc = request.networkCapabilities;
3225 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, legacyType, cbHandler);
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09003226 }
3227
3228 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09003229 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003230 *
3231 * This {@link NetworkRequest} will live until released via
Etan Cohenaebf17e2017-03-01 12:47:28 -08003232 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A
3233 * version of the method which takes a timeout is
Hugo Benichi0eec03f2017-05-15 15:15:33 +09003234 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003235 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07003236 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003237 * can be used to direct traffic to the network.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003238 * <p>It is presently unsupported to request a network with mutable
3239 * {@link NetworkCapabilities} such as
3240 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3241 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3242 * as these {@code NetworkCapabilities} represent states that a particular
3243 * network may never attain, and whether a network will attain these states
3244 * is unknown prior to bringing up the network so the framework does not
3245 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003246 *
3247 * <p>This method requires the caller to hold either the
3248 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3249 * or the ability to modify system settings as determined by
3250 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt9258c642014-03-26 16:47:06 -07003251 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003252 * @param request {@link NetworkRequest} describing this request.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003253 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3254 * the callback must not be shared - it uniquely specifies this request.
3255 * The callback is invoked on the default internal Handler.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003256 * @throws IllegalArgumentException if {@code request} specifies any mutable
3257 * {@code NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003258 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003259 public void requestNetwork(@NonNull NetworkRequest request,
3260 @NonNull NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003261 requestNetwork(request, networkCallback, getDefaultHandler());
3262 }
3263
3264 /**
3265 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
3266 *
3267 * This {@link NetworkRequest} will live until released via
Etan Cohenaebf17e2017-03-01 12:47:28 -08003268 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A
3269 * version of the method which takes a timeout is
Hugo Benichi0eec03f2017-05-15 15:15:33 +09003270 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003271 * Status of the request can be followed by listening to the various
3272 * callbacks described in {@link NetworkCallback}. The {@link Network}
3273 * can be used to direct traffic to the network.
3274 * <p>It is presently unsupported to request a network with mutable
3275 * {@link NetworkCapabilities} such as
3276 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3277 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3278 * as these {@code NetworkCapabilities} represent states that a particular
3279 * network may never attain, and whether a network will attain these states
3280 * is unknown prior to bringing up the network so the framework does not
Chalard Jean4d660112018-06-04 16:52:49 +09003281 * know how to go about satisfying a request with these capabilities.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003282 *
3283 * <p>This method requires the caller to hold either the
3284 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3285 * or the ability to modify system settings as determined by
3286 * {@link android.provider.Settings.System#canWrite}.</p>
3287 *
3288 * @param request {@link NetworkRequest} describing this request.
3289 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3290 * the callback must not be shared - it uniquely specifies this request.
3291 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
3292 * @throws IllegalArgumentException if {@code request} specifies any mutable
3293 * {@code NetworkCapabilities}.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003294 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003295 public void requestNetwork(@NonNull NetworkRequest request,
3296 @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003297 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
3298 CallbackHandler cbHandler = new CallbackHandler(handler);
3299 requestNetwork(request, networkCallback, 0, legacyType, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003300 }
3301
3302 /**
Etan Cohenaebf17e2017-03-01 12:47:28 -08003303 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
3304 * by a timeout.
3305 *
3306 * This function behaves identically to the non-timed-out version
3307 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network
3308 * is not found within the given time (in milliseconds) the
3309 * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be
3310 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3311 * not have to be released if timed-out (it is automatically released). Unregistering a
3312 * request that timed out is not an error.
3313 *
3314 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3315 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3316 * for that purpose. Calling this method will attempt to bring up the requested network.
3317 *
3318 * <p>This method requires the caller to hold either the
3319 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3320 * or the ability to modify system settings as determined by
3321 * {@link android.provider.Settings.System#canWrite}.</p>
3322 *
3323 * @param request {@link NetworkRequest} describing this request.
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003324 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3325 * the callback must not be shared - it uniquely specifies this request.
Etan Cohenaebf17e2017-03-01 12:47:28 -08003326 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3327 * before {@link NetworkCallback#onUnavailable()} is called. The timeout must
3328 * be a positive value (i.e. >0).
Etan Cohenaebf17e2017-03-01 12:47:28 -08003329 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003330 public void requestNetwork(@NonNull NetworkRequest request,
3331 @NonNull NetworkCallback networkCallback, int timeoutMs) {
Hugo Benichie7678512017-05-09 15:19:01 +09003332 checkTimeout(timeoutMs);
Hugo Benichi2583ef02017-02-02 17:02:36 +09003333 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
Erik Klineb583b032017-02-22 12:58:24 +09003334 requestNetwork(request, networkCallback, timeoutMs, legacyType, getDefaultHandler());
Hugo Benichi2583ef02017-02-02 17:02:36 +09003335 }
3336
Hugo Benichi2583ef02017-02-02 17:02:36 +09003337 /**
3338 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
3339 * by a timeout.
3340 *
Chalard Jean4d660112018-06-04 16:52:49 +09003341 * This function behaves identically to the version without timeout, but if a suitable
Hugo Benichi2583ef02017-02-02 17:02:36 +09003342 * network is not found within the given time (in milliseconds) the
Etan Cohenaebf17e2017-03-01 12:47:28 -08003343 * {@link NetworkCallback#onUnavailable} callback is called. The request can still be
3344 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3345 * not have to be released if timed-out (it is automatically released). Unregistering a
3346 * request that timed out is not an error.
3347 *
3348 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3349 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3350 * for that purpose. Calling this method will attempt to bring up the requested network.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003351 *
3352 * <p>This method requires the caller to hold either the
3353 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3354 * or the ability to modify system settings as determined by
3355 * {@link android.provider.Settings.System#canWrite}.</p>
3356 *
3357 * @param request {@link NetworkRequest} describing this request.
Etan Cohenaebf17e2017-03-01 12:47:28 -08003358 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3359 * the callback must not be shared - it uniquely specifies this request.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003360 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003361 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3362 * before {@link NetworkCallback#onUnavailable} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003363 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003364 public void requestNetwork(@NonNull NetworkRequest request,
3365 @NonNull NetworkCallback networkCallback, @NonNull Handler handler, int timeoutMs) {
Hugo Benichie7678512017-05-09 15:19:01 +09003366 checkTimeout(timeoutMs);
Hugo Benichi2583ef02017-02-02 17:02:36 +09003367 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
3368 CallbackHandler cbHandler = new CallbackHandler(handler);
3369 requestNetwork(request, networkCallback, timeoutMs, legacyType, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003370 }
3371
3372 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003373 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003374 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003375 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfcde58f2015-02-11 16:51:13 -08003376 * <p>
Paul Jensen72db88e2015-03-10 10:54:12 -04003377 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
3378 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003379 */
Erik Kline90e93072014-11-19 12:12:24 +09003380 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003381
3382 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07003383 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003384 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003385 * {@link android.content.Intent#getParcelableExtra(String)}.
3386 */
Erik Kline90e93072014-11-19 12:12:24 +09003387 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003388
3389
3390 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09003391 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003392 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003393 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07003394 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003395 * the request may outlive the calling application and get called back when a suitable
3396 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003397 * <p>
3398 * The operation is an Intent broadcast that goes to a broadcast receiver that
3399 * you registered with {@link Context#registerReceiver} or through the
3400 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3401 * <p>
3402 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09003403 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3404 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07003405 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07003406 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07003407 * Intent to reserve the network or it will be released shortly after the Intent
3408 * is processed.
3409 * <p>
Paul Jensen694f2b82015-06-17 14:15:39 -04003410 * If there is already a request for this Intent registered (with the equality of
Robert Greenwalt9258c642014-03-26 16:47:06 -07003411 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003412 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003413 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003414 * The request may be released normally by calling
3415 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003416 * <p>It is presently unsupported to request a network with either
3417 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3418 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3419 * as these {@code NetworkCapabilities} represent states that a particular
3420 * network may never attain, and whether a network will attain these states
3421 * is unknown prior to bringing up the network so the framework does not
Chalard Jean4d660112018-06-04 16:52:49 +09003422 * know how to go about satisfying a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003423 *
3424 * <p>This method requires the caller to hold either the
3425 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3426 * or the ability to modify system settings as determined by
3427 * {@link android.provider.Settings.System#canWrite}.</p>
3428 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003429 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003430 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07003431 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003432 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003433 * @throws IllegalArgumentException if {@code request} contains either
3434 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3435 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003436 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003437 public void requestNetwork(@NonNull NetworkRequest request,
3438 @NonNull PendingIntent operation) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09003439 printStackTrace();
Hugo Benichie7678512017-05-09 15:19:01 +09003440 checkPendingIntentNotNull(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003441 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07003442 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003443 } catch (RemoteException e) {
3444 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003445 } catch (ServiceSpecificException e) {
3446 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003447 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003448 }
3449
3450 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003451 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
3452 * <p>
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003453 * This method has the same behavior as
3454 * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003455 * releasing network resources and disconnecting.
3456 *
3457 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3458 * PendingIntent passed to
3459 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
3460 * corresponding NetworkRequest you'd like to remove. Cannot be null.
3461 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003462 public void releaseNetworkRequest(@NonNull PendingIntent operation) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09003463 printStackTrace();
Hugo Benichie7678512017-05-09 15:19:01 +09003464 checkPendingIntentNotNull(operation);
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003465 try {
3466 mService.releasePendingNetworkRequest(operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003467 } catch (RemoteException e) {
3468 throw e.rethrowFromSystemServer();
3469 }
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003470 }
3471
Hugo Benichie7678512017-05-09 15:19:01 +09003472 private static void checkPendingIntentNotNull(PendingIntent intent) {
3473 Preconditions.checkNotNull(intent, "PendingIntent cannot be null.");
3474 }
3475
3476 private static void checkCallbackNotNull(NetworkCallback callback) {
3477 Preconditions.checkNotNull(callback, "null NetworkCallback");
3478 }
3479
3480 private static void checkTimeout(int timeoutMs) {
3481 Preconditions.checkArgumentPositive(timeoutMs, "timeoutMs must be strictly positive.");
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003482 }
3483
3484 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07003485 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07003486 * {@link NetworkRequest}. The callbacks will continue to be called until
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003487 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003488 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003489 * @param request {@link NetworkRequest} describing this request.
3490 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3491 * networks change state.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003492 * The callback is invoked on the default internal Handler.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003493 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003494 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09003495 public void registerNetworkCallback(@NonNull NetworkRequest request,
3496 @NonNull NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003497 registerNetworkCallback(request, networkCallback, getDefaultHandler());
3498 }
3499
3500 /**
3501 * Registers to receive notifications about all networks which satisfy the given
3502 * {@link NetworkRequest}. The callbacks will continue to be called until
3503 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003504 *
3505 * @param request {@link NetworkRequest} describing this request.
3506 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3507 * networks change state.
3508 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003509 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003510 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09003511 public void registerNetworkCallback(@NonNull NetworkRequest request,
3512 @NonNull NetworkCallback networkCallback, @NonNull Handler handler) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003513 CallbackHandler cbHandler = new CallbackHandler(handler);
3514 NetworkCapabilities nc = request.networkCapabilities;
3515 sendRequestForNetwork(nc, networkCallback, 0, LISTEN, TYPE_NONE, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003516 }
3517
3518 /**
Paul Jensen694f2b82015-06-17 14:15:39 -04003519 * Registers a PendingIntent to be sent when a network is available which satisfies the given
3520 * {@link NetworkRequest}.
3521 *
3522 * This function behaves identically to the version that takes a NetworkCallback, but instead
3523 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
3524 * the request may outlive the calling application and get called back when a suitable
3525 * network is found.
3526 * <p>
3527 * The operation is an Intent broadcast that goes to a broadcast receiver that
3528 * you registered with {@link Context#registerReceiver} or through the
3529 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3530 * <p>
3531 * The operation Intent is delivered with two extras, a {@link Network} typed
3532 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3533 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
3534 * the original requests parameters.
3535 * <p>
3536 * If there is already a request for this Intent registered (with the equality of
3537 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
3538 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
3539 * <p>
3540 * The request may be released normally by calling
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003541 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Paul Jensen694f2b82015-06-17 14:15:39 -04003542 * @param request {@link NetworkRequest} describing this request.
3543 * @param operation Action to perform when the network is available (corresponds
3544 * to the {@link NetworkCallback#onAvailable} call. Typically
3545 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
3546 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003547 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09003548 public void registerNetworkCallback(@NonNull NetworkRequest request,
3549 @NonNull PendingIntent operation) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09003550 printStackTrace();
Hugo Benichie7678512017-05-09 15:19:01 +09003551 checkPendingIntentNotNull(operation);
Paul Jensen694f2b82015-06-17 14:15:39 -04003552 try {
3553 mService.pendingListenForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003554 } catch (RemoteException e) {
3555 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003556 } catch (ServiceSpecificException e) {
3557 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003558 }
Paul Jensen694f2b82015-06-17 14:15:39 -04003559 }
3560
3561 /**
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003562 * Registers to receive notifications about changes in the system default network. The callbacks
3563 * will continue to be called until either the application exits or
3564 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Erik Klinea2d29402016-03-16 15:31:39 +09003565 *
3566 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3567 * system default network changes.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003568 * The callback is invoked on the default internal Handler.
Erik Klinea2d29402016-03-16 15:31:39 +09003569 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003570 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09003571 public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003572 registerDefaultNetworkCallback(networkCallback, getDefaultHandler());
3573 }
3574
3575 /**
3576 * Registers to receive notifications about changes in the system default network. The callbacks
3577 * will continue to be called until either the application exits or
3578 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003579 *
3580 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3581 * system default network changes.
3582 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003583 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003584 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09003585 public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback,
3586 @NonNull Handler handler) {
Erik Klinea2d29402016-03-16 15:31:39 +09003587 // This works because if the NetworkCapabilities are null,
3588 // ConnectivityService takes them from the default request.
3589 //
3590 // Since the capabilities are exactly the same as the default request's
3591 // capabilities, this request is guaranteed, at all times, to be
3592 // satisfied by the same network, if any, that satisfies the default
3593 // request, i.e., the system default network.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003594 CallbackHandler cbHandler = new CallbackHandler(handler);
Chalard Jean4d660112018-06-04 16:52:49 +09003595 sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0,
3596 REQUEST, TYPE_NONE, cbHandler);
Erik Klinea2d29402016-03-16 15:31:39 +09003597 }
3598
3599 /**
fengludb571472015-04-21 17:12:05 -07003600 * Requests bandwidth update for a given {@link Network} and returns whether the update request
3601 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
3602 * network connection for updated bandwidth information. The caller will be notified via
3603 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003604 * method assumes that the caller has previously called
3605 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network
3606 * changes.
fenglub15e72b2015-03-20 11:29:56 -07003607 *
fengluae519192015-04-27 14:28:04 -07003608 * @param network {@link Network} specifying which network you're interested.
fengludb571472015-04-21 17:12:05 -07003609 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglub15e72b2015-03-20 11:29:56 -07003610 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003611 public boolean requestBandwidthUpdate(@NonNull Network network) {
fenglub15e72b2015-03-20 11:29:56 -07003612 try {
fengludb571472015-04-21 17:12:05 -07003613 return mService.requestBandwidthUpdate(network);
fenglub15e72b2015-03-20 11:29:56 -07003614 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003615 throw e.rethrowFromSystemServer();
fenglub15e72b2015-03-20 11:29:56 -07003616 }
3617 }
3618
3619 /**
Hugo Benichidafed3d2017-03-06 09:17:06 +09003620 * Unregisters a {@code NetworkCallback} and possibly releases networks originating from
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003621 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and
3622 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls.
3623 * If the given {@code NetworkCallback} had previously been used with
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09003624 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
3625 * will be disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003626 *
Hugo Benichidafed3d2017-03-06 09:17:06 +09003627 * Notifications that would have triggered that {@code NetworkCallback} will immediately stop
3628 * triggering it as soon as this call returns.
3629 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003630 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003631 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003632 public void unregisterNetworkCallback(@NonNull NetworkCallback networkCallback) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09003633 printStackTrace();
Hugo Benichie7678512017-05-09 15:19:01 +09003634 checkCallbackNotNull(networkCallback);
Hugo Benichidafed3d2017-03-06 09:17:06 +09003635 final List<NetworkRequest> reqs = new ArrayList<>();
3636 // Find all requests associated to this callback and stop callback triggers immediately.
3637 // Callback is reusable immediately. http://b/20701525, http://b/35921499.
3638 synchronized (sCallbacks) {
Hugo Benichi31c176d2017-06-17 13:14:12 +09003639 Preconditions.checkArgument(networkCallback.networkRequest != null,
3640 "NetworkCallback was not registered");
3641 Preconditions.checkArgument(networkCallback.networkRequest != ALREADY_UNREGISTERED,
3642 "NetworkCallback was already unregistered");
Hugo Benichidafed3d2017-03-06 09:17:06 +09003643 for (Map.Entry<NetworkRequest, NetworkCallback> e : sCallbacks.entrySet()) {
3644 if (e.getValue() == networkCallback) {
3645 reqs.add(e.getKey());
3646 }
3647 }
3648 // TODO: throw exception if callback was registered more than once (http://b/20701525).
3649 for (NetworkRequest r : reqs) {
3650 try {
3651 mService.releaseNetworkRequest(r);
3652 } catch (RemoteException e) {
3653 throw e.rethrowFromSystemServer();
3654 }
3655 // Only remove mapping if rpc was successful.
3656 sCallbacks.remove(r);
3657 }
Hugo Benichi31c176d2017-06-17 13:14:12 +09003658 networkCallback.networkRequest = ALREADY_UNREGISTERED;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003659 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003660 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003661
3662 /**
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003663 * Unregisters a callback previously registered via
3664 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3665 *
3666 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3667 * PendingIntent passed to
3668 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3669 * Cannot be null.
3670 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003671 public void unregisterNetworkCallback(@NonNull PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003672 checkPendingIntentNotNull(operation);
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003673 releaseNetworkRequest(operation);
3674 }
3675
3676 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003677 * Informs the system whether it should switch to {@code network} regardless of whether it is
3678 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
3679 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
3680 * the system default network regardless of any other network that's currently connected. If
3681 * {@code always} is true, then the choice is remembered, so that the next time the user
3682 * connects to this network, the system will switch to it.
3683 *
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003684 * @param network The network to accept.
3685 * @param accept Whether to accept the network even if unvalidated.
3686 * @param always Whether to remember this choice in the future.
3687 *
3688 * @hide
3689 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003690 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003691 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
3692 try {
3693 mService.setAcceptUnvalidated(network, accept, always);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003694 } catch (RemoteException e) {
3695 throw e.rethrowFromSystemServer();
3696 }
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003697 }
3698
3699 /**
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003700 * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is
3701 * only meaningful if the system is configured not to penalize such networks, e.g., if the
3702 * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code
3703 * NETWORK_AVOID_BAD_WIFI setting is unset}.
3704 *
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003705 * @param network The network to accept.
3706 *
3707 * @hide
3708 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003709 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003710 public void setAvoidUnvalidated(Network network) {
3711 try {
3712 mService.setAvoidUnvalidated(network);
3713 } catch (RemoteException e) {
3714 throw e.rethrowFromSystemServer();
3715 }
3716 }
3717
3718 /**
Lorenzo Colitti4734cdb2017-04-27 14:30:21 +09003719 * Requests that the system open the captive portal app on the specified network.
3720 *
3721 * @param network The network to log into.
3722 *
3723 * @hide
3724 */
3725 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
3726 public void startCaptivePortalApp(Network network) {
3727 try {
3728 mService.startCaptivePortalApp(network);
3729 } catch (RemoteException e) {
3730 throw e.rethrowFromSystemServer();
3731 }
3732 }
3733
3734 /**
Remi NGUYEN VAN97f69c22019-01-20 20:35:06 +09003735 * Determine whether the device is configured to avoid bad wifi.
3736 * @hide
3737 */
3738 @SystemApi
3739 public boolean getAvoidBadWifi() {
3740 try {
3741 return mService.getAvoidBadWifi();
3742 } catch (RemoteException e) {
3743 throw e.rethrowFromSystemServer();
3744 }
3745 }
3746
3747 /**
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003748 * It is acceptable to briefly use multipath data to provide seamless connectivity for
3749 * time-sensitive user-facing operations when the system default network is temporarily
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003750 * unresponsive. The amount of data should be limited (less than one megabyte for every call to
3751 * this method), and the operation should be infrequent to ensure that data usage is limited.
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003752 *
3753 * An example of such an operation might be a time-sensitive foreground activity, such as a
3754 * voice command, that the user is performing while walking out of range of a Wi-Fi network.
3755 */
3756 public static final int MULTIPATH_PREFERENCE_HANDOVER = 1 << 0;
3757
3758 /**
3759 * It is acceptable to use small amounts of multipath data on an ongoing basis to provide
3760 * a backup channel for traffic that is primarily going over another network.
3761 *
3762 * An example might be maintaining backup connections to peers or servers for the purpose of
3763 * fast fallback if the default network is temporarily unresponsive or disconnects. The traffic
3764 * on backup paths should be negligible compared to the traffic on the main path.
3765 */
3766 public static final int MULTIPATH_PREFERENCE_RELIABILITY = 1 << 1;
3767
3768 /**
3769 * It is acceptable to use metered data to improve network latency and performance.
3770 */
3771 public static final int MULTIPATH_PREFERENCE_PERFORMANCE = 1 << 2;
3772
3773 /**
3774 * Return value to use for unmetered networks. On such networks we currently set all the flags
3775 * to true.
3776 * @hide
3777 */
3778 public static final int MULTIPATH_PREFERENCE_UNMETERED =
3779 MULTIPATH_PREFERENCE_HANDOVER |
3780 MULTIPATH_PREFERENCE_RELIABILITY |
3781 MULTIPATH_PREFERENCE_PERFORMANCE;
3782
3783 /** @hide */
3784 @Retention(RetentionPolicy.SOURCE)
3785 @IntDef(flag = true, value = {
3786 MULTIPATH_PREFERENCE_HANDOVER,
3787 MULTIPATH_PREFERENCE_RELIABILITY,
3788 MULTIPATH_PREFERENCE_PERFORMANCE,
3789 })
3790 public @interface MultipathPreference {
3791 }
3792
3793 /**
3794 * Provides a hint to the calling application on whether it is desirable to use the
3795 * multinetwork APIs (e.g., {@link Network#openConnection}, {@link Network#bindSocket}, etc.)
3796 * for multipath data transfer on this network when it is not the system default network.
3797 * Applications desiring to use multipath network protocols should call this method before
3798 * each such operation.
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003799 *
3800 * @param network The network on which the application desires to use multipath data.
3801 * If {@code null}, this method will return the a preference that will generally
3802 * apply to metered networks.
3803 * @return a bitwise OR of zero or more of the {@code MULTIPATH_PREFERENCE_*} constants.
3804 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003805 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Chalard Jean50bea3d2019-01-07 19:26:34 +09003806 public @MultipathPreference int getMultipathPreference(@Nullable Network network) {
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003807 try {
3808 return mService.getMultipathPreference(network);
3809 } catch (RemoteException e) {
3810 throw e.rethrowFromSystemServer();
3811 }
3812 }
3813
3814 /**
Stuart Scott984dc852015-03-30 13:17:11 -07003815 * Resets all connectivity manager settings back to factory defaults.
3816 * @hide
3817 */
3818 public void factoryReset() {
Stuart Scott984dc852015-03-30 13:17:11 -07003819 try {
Stuart Scottf1fb3972015-04-02 18:00:02 -07003820 mService.factoryReset();
Stuart Scott984dc852015-03-30 13:17:11 -07003821 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003822 throw e.rethrowFromSystemServer();
Stuart Scott984dc852015-03-30 13:17:11 -07003823 }
3824 }
3825
3826 /**
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003827 * Binds the current process to {@code network}. All Sockets created in the future
3828 * (and not explicitly bound via a bound SocketFactory from
3829 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3830 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3831 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3832 * work and all host name resolutions will fail. This is by design so an application doesn't
3833 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3834 * To clear binding pass {@code null} for {@code network}. Using individually bound
3835 * Sockets created by Network.getSocketFactory().createSocket() and
3836 * performing network-specific host name resolutions via
3837 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen72db88e2015-03-10 10:54:12 -04003838 * {@code bindProcessToNetwork}.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003839 *
3840 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3841 * the current binding.
3842 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3843 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003844 public boolean bindProcessToNetwork(@Nullable Network network) {
Chalard Jean4d660112018-06-04 16:52:49 +09003845 // Forcing callers to call through non-static function ensures ConnectivityManager
Paul Jensen72db88e2015-03-10 10:54:12 -04003846 // instantiated.
3847 return setProcessDefaultNetwork(network);
3848 }
3849
3850 /**
3851 * Binds the current process to {@code network}. All Sockets created in the future
3852 * (and not explicitly bound via a bound SocketFactory from
3853 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3854 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3855 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3856 * work and all host name resolutions will fail. This is by design so an application doesn't
3857 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3858 * To clear binding pass {@code null} for {@code network}. Using individually bound
3859 * Sockets created by Network.getSocketFactory().createSocket() and
3860 * performing network-specific host name resolutions via
3861 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
3862 * {@code setProcessDefaultNetwork}.
3863 *
3864 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3865 * the current binding.
3866 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3867 * @deprecated This function can throw {@link IllegalStateException}. Use
3868 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
3869 * is a direct replacement.
3870 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003871 @Deprecated
Chalard Jean50bea3d2019-01-07 19:26:34 +09003872 public static boolean setProcessDefaultNetwork(@Nullable Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003873 int netId = (network == null) ? NETID_UNSET : network.netId;
Paul Jensen72db88e2015-03-10 10:54:12 -04003874 if (netId == NetworkUtils.getBoundNetworkForProcess()) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003875 return true;
3876 }
3877 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05003878 // Set HTTP proxy system properties to match network.
3879 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colittiec4c5552015-04-22 11:52:48 +09003880 try {
3881 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
3882 } catch (SecurityException e) {
3883 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
3884 Log.e(TAG, "Can't set proxy properties", e);
3885 }
Paul Jensenc91b5342014-08-27 12:38:45 -04003886 // Must flush DNS cache as new network may have different DNS resolutions.
3887 InetAddress.clearDnsCache();
3888 // Must flush socket pool as idle sockets will be bound to previous network and may
3889 // cause subsequent fetches to be performed on old network.
3890 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
3891 return true;
3892 } else {
3893 return false;
3894 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003895 }
3896
3897 /**
3898 * Returns the {@link Network} currently bound to this process via
Paul Jensen72db88e2015-03-10 10:54:12 -04003899 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003900 *
3901 * @return {@code Network} to which this process is bound, or {@code null}.
3902 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09003903 @Nullable
Paul Jensen72db88e2015-03-10 10:54:12 -04003904 public Network getBoundNetworkForProcess() {
3905 // Forcing callers to call thru non-static function ensures ConnectivityManager
3906 // instantiated.
3907 return getProcessDefaultNetwork();
3908 }
3909
3910 /**
3911 * Returns the {@link Network} currently bound to this process via
3912 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
3913 *
3914 * @return {@code Network} to which this process is bound, or {@code null}.
3915 * @deprecated Using this function can lead to other functions throwing
3916 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
3917 * {@code getBoundNetworkForProcess} is a direct replacement.
3918 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003919 @Deprecated
Chalard Jean50bea3d2019-01-07 19:26:34 +09003920 @Nullable
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003921 public static Network getProcessDefaultNetwork() {
Paul Jensen72db88e2015-03-10 10:54:12 -04003922 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04003923 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003924 return new Network(netId);
3925 }
3926
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003927 private void unsupportedStartingFrom(int version) {
3928 if (Process.myUid() == Process.SYSTEM_UID) {
Lorenzo Colitti2e31a7c2018-09-28 11:31:55 +09003929 // The getApplicationInfo() call we make below is not supported in system context. Let
3930 // the call through here, and rely on the fact that ConnectivityService will refuse to
3931 // allow the system to use these APIs anyway.
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003932 return;
3933 }
3934
3935 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
3936 throw new UnsupportedOperationException(
3937 "This method is not supported in target SDK version " + version + " and above");
3938 }
3939 }
3940
3941 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
3942 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
Lifu Tang30f95a72016-01-07 23:20:38 -08003943 // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003944 // remove these exemptions. Note that this check is not secure, and apps can still access these
3945 // functions by accessing ConnectivityService directly. However, it should be clear that doing
3946 // so is unsupported and may break in the future. http://b/22728205
3947 private void checkLegacyRoutingApiAccess() {
Dianne Hackborn692a2442015-07-31 10:35:34 -07003948 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003949 }
3950
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003951 /**
3952 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen72db88e2015-03-10 10:54:12 -04003953 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003954 *
3955 * @param network The {@link Network} to bind host resolutions from the current process to, or
3956 * {@code null} to clear the current binding.
3957 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3958 * @hide
3959 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
3960 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003961 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01003962 @UnsupportedAppUsage
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003963 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04003964 return NetworkUtils.bindProcessToNetworkForHostResolution(
Erik Klinef4fa9822018-04-27 22:48:33 +09003965 (network == null) ? NETID_UNSET : network.getNetIdForResolv());
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003966 }
Felipe Leme1b103232016-01-22 09:44:57 -08003967
3968 /**
3969 * Device is not restricting metered network activity while application is running on
3970 * background.
3971 */
3972 public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1;
3973
3974 /**
3975 * Device is restricting metered network activity while application is running on background,
3976 * but application is allowed to bypass it.
3977 * <p>
3978 * In this state, application should take action to mitigate metered network access.
3979 * For example, a music streaming application should switch to a low-bandwidth bitrate.
3980 */
3981 public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2;
3982
3983 /**
3984 * Device is restricting metered network activity while application is running on background.
Felipe Leme9778f762016-01-27 14:46:39 -08003985 * <p>
Felipe Leme1b103232016-01-22 09:44:57 -08003986 * In this state, application should not try to use the network while running on background,
3987 * because it would be denied.
3988 */
3989 public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3;
3990
Felipe Leme9778f762016-01-27 14:46:39 -08003991 /**
3992 * A change in the background metered network activity restriction has occurred.
3993 * <p>
3994 * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction
3995 * applies to them.
3996 * <p>
3997 * This is only sent to registered receivers, not manifest receivers.
3998 */
3999 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
4000 public static final String ACTION_RESTRICT_BACKGROUND_CHANGED =
4001 "android.net.conn.RESTRICT_BACKGROUND_CHANGED";
4002
Felipe Lemeecfccea2016-01-25 11:48:04 -08004003 /** @hide */
4004 @Retention(RetentionPolicy.SOURCE)
Felipe Leme1b103232016-01-22 09:44:57 -08004005 @IntDef(flag = false, value = {
4006 RESTRICT_BACKGROUND_STATUS_DISABLED,
4007 RESTRICT_BACKGROUND_STATUS_WHITELISTED,
4008 RESTRICT_BACKGROUND_STATUS_ENABLED,
4009 })
Felipe Leme1b103232016-01-22 09:44:57 -08004010 public @interface RestrictBackgroundStatus {
4011 }
4012
4013 private INetworkPolicyManager getNetworkPolicyManager() {
4014 synchronized (this) {
4015 if (mNPManager != null) {
4016 return mNPManager;
4017 }
4018 mNPManager = INetworkPolicyManager.Stub.asInterface(ServiceManager
4019 .getService(Context.NETWORK_POLICY_SERVICE));
4020 return mNPManager;
4021 }
4022 }
4023
4024 /**
4025 * Determines if the calling application is subject to metered network restrictions while
4026 * running on background.
Felipe Lemec9c7be52016-05-16 13:57:19 -07004027 *
4028 * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED},
4029 * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED},
4030 * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED}
Felipe Leme1b103232016-01-22 09:44:57 -08004031 */
4032 public @RestrictBackgroundStatus int getRestrictBackgroundStatus() {
4033 try {
4034 return getNetworkPolicyManager().getRestrictBackgroundByCaller();
4035 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07004036 throw e.rethrowFromSystemServer();
Felipe Leme1b103232016-01-22 09:44:57 -08004037 }
4038 }
Ricky Wai44dcbde2018-01-23 04:09:45 +00004039
4040 /**
4041 * The network watchlist is a list of domains and IP addresses that are associated with
Ricky Waia86d5d52018-03-20 14:20:54 +00004042 * potentially harmful apps. This method returns the SHA-256 of the watchlist config file
4043 * currently used by the system for validation purposes.
Ricky Wai44dcbde2018-01-23 04:09:45 +00004044 *
4045 * @return Hash of network watchlist config file. Null if config does not exist.
4046 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09004047 @Nullable
Ricky Wai44dcbde2018-01-23 04:09:45 +00004048 public byte[] getNetworkWatchlistConfigHash() {
4049 try {
4050 return mService.getNetworkWatchlistConfigHash();
4051 } catch (RemoteException e) {
4052 Log.e(TAG, "Unable to get watchlist config hash");
4053 throw e.rethrowFromSystemServer();
4054 }
4055 }
Jeff Vander Stoep0ac2c092018-07-23 10:57:53 -07004056
4057 /**
4058 * Returns the {@code uid} of the owner of a network connection.
4059 *
4060 * @param protocol The protocol of the connection. Only {@code IPPROTO_TCP} and
4061 * {@code IPPROTO_UDP} currently supported.
4062 * @param local The local {@link InetSocketAddress} of a connection.
4063 * @param remote The remote {@link InetSocketAddress} of a connection.
4064 *
4065 * @return {@code uid} if the connection is found and the app has permission to observe it
4066 * (e.g., if it is associated with the calling VPN app's tunnel) or
4067 * {@link android.os.Process#INVALID_UID} if the connection is not found.
4068 */
Chalard Jean50bea3d2019-01-07 19:26:34 +09004069 public int getConnectionOwnerUid(int protocol, @NonNull InetSocketAddress local,
4070 @NonNull InetSocketAddress remote) {
Jeff Vander Stoep0ac2c092018-07-23 10:57:53 -07004071 ConnectionInfo connectionInfo = new ConnectionInfo(protocol, local, remote);
4072 try {
4073 return mService.getConnectionOwnerUid(connectionInfo);
4074 } catch (RemoteException e) {
4075 throw e.rethrowFromSystemServer();
4076 }
4077 }
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09004078
4079 private void printStackTrace() {
4080 if (DEBUG) {
4081 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
4082 final StringBuffer sb = new StringBuffer();
4083 for (int i = 3; i < callStack.length; i++) {
4084 final String stackTrace = callStack[i].toString();
4085 if (stackTrace == null || stackTrace.contains("android.os")) {
4086 break;
4087 }
4088 sb.append(" [").append(stackTrace).append("]");
4089 }
4090 Log.d(TAG, "StackLog:" + sb.toString());
4091 }
4092 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004093}