blob: f2e907833612e0132bf7c047e4d1a60ad22ba519 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016package android.net;
17
Felipe Leme1b103232016-01-22 09:44:57 -080018import android.annotation.IntDef;
Robin Lee244ce8e2016-01-05 18:03:46 +000019import android.annotation.Nullable;
Jeff Sharkey30e06bb2017-04-24 11:18:03 -060020import android.annotation.RequiresPermission;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.annotation.SdkConstant;
22import android.annotation.SdkConstant.SdkConstantType;
Udam Sainib7c24872016-01-04 12:16:14 -080023import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060024import android.annotation.SystemService;
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010025import android.annotation.UnsupportedAppUsage;
Robert Greenwalt9258c642014-03-26 16:47:06 -070026import android.app.PendingIntent;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070027import android.content.Context;
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070028import android.content.Intent;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +090029import android.content.pm.PackageManager;
Robert Greenwalt42acef32009-08-12 16:08:25 -070030import android.os.Binder;
Jeff Sharkey3a844fc2011-08-16 14:37:57 -070031import android.os.Build.VERSION_CODES;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080032import android.os.Bundle;
Robert Greenwalt9258c642014-03-26 16:47:06 -070033import android.os.Handler;
34import android.os.HandlerThread;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080035import android.os.IBinder;
36import android.os.INetworkActivityListener;
37import android.os.INetworkManagementService;
Robert Greenwalt9258c642014-03-26 16:47:06 -070038import android.os.Looper;
39import android.os.Message;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -070040import android.os.Messenger;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +090041import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.os.RemoteException;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080043import android.os.ResultReceiver;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080044import android.os.ServiceManager;
Hugo Benichicb883232017-05-11 13:16:17 +090045import android.os.ServiceSpecificException;
Jeff Sharkey961e3042011-08-29 16:02:57 -070046import android.provider.Settings;
Wink Saville36ffb042014-12-05 11:10:30 -080047import android.telephony.SubscriptionManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080048import android.util.ArrayMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -070049import android.util.Log;
Erik Kline35bf06c2017-01-26 18:08:28 +090050import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
Robert Greenwaltafa05c02014-05-21 20:04:36 -070052import com.android.internal.telephony.ITelephony;
Robert Greenwalt562cc542014-05-15 18:07:26 -070053import com.android.internal.telephony.PhoneConstants;
Hugo Benichidafed3d2017-03-06 09:17:06 +090054import com.android.internal.util.Preconditions;
55import com.android.internal.util.Protocol;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070056
Paul Jensenc91b5342014-08-27 12:38:45 -040057import libcore.net.event.NetworkEventDispatcher;
58
Felipe Leme1b103232016-01-22 09:44:57 -080059import java.lang.annotation.Retention;
60import java.lang.annotation.RetentionPolicy;
Jeremy Kleind42209d2015-12-28 15:11:58 -080061import java.net.InetAddress;
Jeff Vander Stoep0ac2c092018-07-23 10:57:53 -070062import java.net.InetSocketAddress;
Hugo Benichidafed3d2017-03-06 09:17:06 +090063import java.util.ArrayList;
Jeremy Kleind42209d2015-12-28 15:11:58 -080064import java.util.HashMap;
Hugo Benichidafed3d2017-03-06 09:17:06 +090065import java.util.List;
66import java.util.Map;
Jeremy Kleind42209d2015-12-28 15:11:58 -080067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068/**
69 * Class that answers queries about the state of network connectivity. It also
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060070 * notifies applications when network connectivity changes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 * <p>
72 * The primary responsibilities of this class are to:
73 * <ol>
74 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
75 * <li>Send broadcast intents when network connectivity changes</li>
76 * <li>Attempt to "fail over" to another network when connectivity to a network
77 * is lost</li>
78 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
79 * state of the available networks</li>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070080 * <li>Provide an API that allows applications to request and select networks for their data
81 * traffic</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 * </ol>
83 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060084@SystemService(Context.CONNECTIVITY_SERVICE)
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070085public class ConnectivityManager {
86 private static final String TAG = "ConnectivityManager";
87
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070089 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 * been established or lost. The NetworkInfo for the affected network is
91 * sent as an extra; it should be consulted to see what kind of
92 * connectivity event occurred.
93 * <p/>
Mark Lu33ec1062016-12-05 10:57:55 -080094 * Apps targeting Android 7.0 (API level 24) and higher do not receive this
95 * broadcast if they declare the broadcast receiver in their manifest. Apps
96 * will still receive broadcasts if they register their
97 * {@link android.content.BroadcastReceiver} with
98 * {@link android.content.Context#registerReceiver Context.registerReceiver()}
99 * and that context is still valid.
100 * <p/>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 * If this is a connection that was the result of failing over from a
102 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
103 * set to true.
104 * <p/>
105 * For a loss of connectivity, if the connectivity manager is attempting
106 * to connect (or has already connected) to another network, the
107 * NetworkInfo for the new network is also passed as an extra. This lets
108 * any receivers of the broadcast know that they should not necessarily
109 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800110 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 * the failover attempt succeeded (and so there is still overall data
112 * connectivity), or that the failover attempt failed, meaning that all
113 * connectivity has been lost.
114 * <p/>
115 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
116 * is set to {@code true} if there are no connected networks at all.
Chalard Jean054cd162018-02-10 05:33:50 +0900117 *
118 * @deprecated apps should use the more versatile {@link #requestNetwork},
119 * {@link #registerNetworkCallback} or {@link #registerDefaultNetworkCallback}
120 * functions instead for faster and more detailed updates about the network
121 * changes they care about.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800123 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jean054cd162018-02-10 05:33:50 +0900124 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 /**
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700128 * A temporary hack until SUPL system can get off the legacy APIS.
129 * They do too many network requests and the long list of apps listening
Chalard Jean4d660112018-06-04 16:52:49 +0900130 * and waking due to the CONNECTIVITY_ACTION broadcast makes it expensive.
131 * Use this broadcast intent instead for SUPL requests.
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700132 * @hide
133 */
134 public static final String CONNECTIVITY_ACTION_SUPL =
135 "android.net.conn.CONNECTIVITY_CHANGE_SUPL";
136
137 /**
Paul Jensen25a217c2015-02-27 22:55:47 -0500138 * The device has connected to a network that has presented a captive
139 * portal, which is blocking Internet connectivity. The user was presented
140 * with a notification that network sign in is required,
141 * and the user invoked the notification's action indicating they
Paul Jensen49e3edf2015-05-22 10:50:39 -0400142 * desire to sign in to the network. Apps handling this activity should
Paul Jensen25a217c2015-02-27 22:55:47 -0500143 * facilitate signing in to the network. This action includes a
144 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents
145 * the network presenting the captive portal; all communication with the
146 * captive portal must be done using this {@code Network} object.
147 * <p/>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400148 * This activity includes a {@link CaptivePortal} extra named
149 * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different
150 * outcomes of the captive portal sign in to the system:
151 * <ul>
152 * <li> When the app handling this action believes the user has signed in to
153 * the network and the captive portal has been dismissed, the app should
154 * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can
155 * reevaluate the network. If reevaluation finds the network no longer
156 * subject to a captive portal, the network may become the default active
Chalard Jean4d660112018-06-04 16:52:49 +0900157 * data network.</li>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400158 * <li> When the app handling this action believes the user explicitly wants
Paul Jensen25a217c2015-02-27 22:55:47 -0500159 * to ignore the captive portal and the network, the app should call
Paul Jensen49e3edf2015-05-22 10:50:39 -0400160 * {@link CaptivePortal#ignoreNetwork}. </li>
161 * </ul>
Paul Jensen25a217c2015-02-27 22:55:47 -0500162 */
163 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
164 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL";
165
166 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 * The lookup key for a {@link NetworkInfo} object. Retrieve with
168 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700169 *
170 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
171 * should always obtain network information through
Paul Jensen3541e9f2015-03-18 12:23:02 -0400172 * {@link #getActiveNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700173 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700175 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700179 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700180 *
181 * @see android.content.Intent#getIntExtra(String, int)
182 */
183 public static final String EXTRA_NETWORK_TYPE = "networkType";
184
185 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 * The lookup key for a boolean that indicates whether a connect event
187 * is for a network to which the connectivity manager was failing over
188 * following a disconnect on another network.
189 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
190 */
191 public static final String EXTRA_IS_FAILOVER = "isFailover";
192 /**
193 * The lookup key for a {@link NetworkInfo} object. This is supplied when
194 * there is another network that it may be possible to connect to. Retrieve with
195 * {@link android.content.Intent#getParcelableExtra(String)}.
196 */
197 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
198 /**
199 * The lookup key for a boolean that indicates whether there is a
200 * complete lack of connectivity, i.e., no network is available.
201 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
202 */
203 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
204 /**
205 * The lookup key for a string that indicates why an attempt to connect
206 * to a network failed. The string has no particular structure. It is
207 * intended to be used in notifications presented to users. Retrieve
208 * it with {@link android.content.Intent#getStringExtra(String)}.
209 */
210 public static final String EXTRA_REASON = "reason";
211 /**
212 * The lookup key for a string that provides optionally supplied
213 * extra information about the network state. The information
214 * may be passed up from the lower networking layers, and its
215 * meaning may be specific to a particular network type. Retrieve
216 * it with {@link android.content.Intent#getStringExtra(String)}.
217 */
218 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700219 /**
220 * The lookup key for an int that provides information about
221 * our connection to the internet at large. 0 indicates no connection,
222 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700223 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700224 * {@hide}
225 */
226 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 /**
Paul Jensen49e3edf2015-05-22 10:50:39 -0400228 * The lookup key for a {@link CaptivePortal} object included with the
229 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal}
230 * object can be used to either indicate to the system that the captive
231 * portal has been dismissed or that the user does not want to pursue
232 * signing in to captive portal. Retrieve it with
233 * {@link android.content.Intent#getParcelableExtra(String)}.
Paul Jensen25a217c2015-02-27 22:55:47 -0500234 */
Paul Jensen49e3edf2015-05-22 10:50:39 -0400235 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL";
Jan Nordqvist52eb29f2015-09-22 15:54:32 -0700236
237 /**
238 * Key for passing a URL to the captive portal login activity.
239 */
240 public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL";
241
Paul Jensen25a217c2015-02-27 22:55:47 -0500242 /**
Remi NGUYEN VAN8255c2d2018-05-22 10:01:53 +0900243 * Key for passing a {@link android.net.captiveportal.CaptivePortalProbeSpec} to the captive
244 * portal login activity.
245 * {@hide}
246 */
247 public static final String EXTRA_CAPTIVE_PORTAL_PROBE_SPEC =
248 "android.net.extra.CAPTIVE_PORTAL_PROBE_SPEC";
249
250 /**
Hugo Benichicdf3ba42016-12-14 08:23:40 +0900251 * Key for passing a user agent string to the captive portal login activity.
252 * {@hide}
253 */
254 public static final String EXTRA_CAPTIVE_PORTAL_USER_AGENT =
255 "android.net.extra.CAPTIVE_PORTAL_USER_AGENT";
256
257 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700258 * Broadcast action to indicate the change of data activity status
259 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800260 * The network becomes active when data transmission is started, or
261 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700262 * {@hide}
263 */
264 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jean4d660112018-06-04 16:52:49 +0900265 public static final String ACTION_DATA_ACTIVITY_CHANGE =
266 "android.net.conn.DATA_ACTIVITY_CHANGE";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700267 /**
268 * The lookup key for an enum that indicates the network device type on which this data activity
269 * change happens.
270 * {@hide}
271 */
272 public static final String EXTRA_DEVICE_TYPE = "deviceType";
273 /**
274 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
275 * it is actively sending or receiving data and {@code false} means it is idle.
276 * {@hide}
277 */
278 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700279 /**
280 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
281 * {@hide}
282 */
283 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700284
285 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800286 * Broadcast Action: The setting for background data usage has changed
287 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
288 * <p>
289 * If an application uses the network in the background, it should listen
290 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700291 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800292 * <p>
293 *
294 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
295 * of background data depends on several combined factors, and
296 * this broadcast is no longer sent. Instead, when background
297 * data is unavailable, {@link #getActiveNetworkInfo()} will now
298 * appear disconnected. During first boot after a platform
299 * upgrade, this broadcast will be sent once if
300 * {@link #getBackgroundDataSetting()} was {@code false} before
301 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800302 */
303 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800304 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800305 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
306 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
307
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700308 /**
309 * Broadcast Action: The network connection may not be good
310 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
311 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
312 * the network and it's condition.
313 * @hide
314 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800315 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100316 @UnsupportedAppUsage
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700317 public static final String INET_CONDITION_ACTION =
318 "android.net.conn.INET_CONDITION_ACTION";
319
Robert Greenwalt42acef32009-08-12 16:08:25 -0700320 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800321 * Broadcast Action: A tetherable connection has come or gone.
322 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
Erik Kline8351faa2017-04-17 16:47:23 +0900323 * {@code ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY},
324 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER}, and
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800325 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
326 * the current state of tethering. Each include a list of
327 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800328 * @hide
329 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800330 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100331 @UnsupportedAppUsage
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800332 public static final String ACTION_TETHER_STATE_CHANGED =
333 "android.net.conn.TETHER_STATE_CHANGED";
334
335 /**
336 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800337 * gives a String[] listing all the interfaces configured for
338 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800339 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100340 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800341 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800342
343 /**
344 * @hide
Erik Kline8351faa2017-04-17 16:47:23 +0900345 * gives a String[] listing all the interfaces currently in local-only
346 * mode (ie, has DHCPv4+IPv6-ULA support and no packet forwarding)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800347 */
Erik Kline8351faa2017-04-17 16:47:23 +0900348 public static final String EXTRA_ACTIVE_LOCAL_ONLY = "localOnlyArray";
349
350 /**
351 * @hide
352 * gives a String[] listing all the interfaces currently tethered
353 * (ie, has DHCPv4 support and packets potentially forwarded/NATed)
354 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100355 @UnsupportedAppUsage
Erik Kline8351faa2017-04-17 16:47:23 +0900356 public static final String EXTRA_ACTIVE_TETHER = "tetherArray";
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800357
358 /**
359 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800360 * gives a String[] listing all the interfaces we tried to tether and
361 * failed. Use {@link #getLastTetherError} to find the error code
362 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800363 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100364 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800365 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800366
367 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800368 * Broadcast Action: The captive portal tracker has finished its test.
369 * Sent only while running Setup Wizard, in lieu of showing a user
370 * notification.
371 * @hide
372 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800373 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800374 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
375 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
376 /**
377 * The lookup key for a boolean that indicates whether a captive portal was detected.
378 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
379 * @hide
380 */
381 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
382
383 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900384 * Action used to display a dialog that asks the user whether to connect to a network that is
385 * not validated. This intent is used to start the dialog in settings via startActivity.
386 *
387 * @hide
388 */
389 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED";
390
391 /**
Lorenzo Colitti9be58c52016-09-15 14:02:29 +0900392 * Action used to display a dialog that asks the user whether to avoid a network that is no
393 * longer validated. This intent is used to start the dialog in settings via startActivity.
394 *
395 * @hide
396 */
397 public static final String ACTION_PROMPT_LOST_VALIDATION =
398 "android.net.conn.PROMPT_LOST_VALIDATION";
399
400 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800401 * Invalid tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900402 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800403 * @hide
404 */
405 public static final int TETHERING_INVALID = -1;
406
407 /**
408 * Wifi tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900409 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800410 * @hide
411 */
412 @SystemApi
413 public static final int TETHERING_WIFI = 0;
414
415 /**
416 * USB tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900417 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800418 * @hide
419 */
420 @SystemApi
421 public static final int TETHERING_USB = 1;
422
423 /**
424 * Bluetooth tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900425 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800426 * @hide
427 */
428 @SystemApi
429 public static final int TETHERING_BLUETOOTH = 2;
430
431 /**
432 * Extra used for communicating with the TetherService. Includes the type of tethering to
433 * enable if any.
434 * @hide
435 */
436 public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType";
437
438 /**
439 * Extra used for communicating with the TetherService. Includes the type of tethering for
440 * which to cancel provisioning.
441 * @hide
442 */
443 public static final String EXTRA_REM_TETHER_TYPE = "extraRemTetherType";
444
445 /**
446 * Extra used for communicating with the TetherService. True to schedule a recheck of tether
447 * provisioning.
448 * @hide
449 */
450 public static final String EXTRA_SET_ALARM = "extraSetAlarm";
451
452 /**
453 * Tells the TetherService to run a provision check now.
454 * @hide
455 */
456 public static final String EXTRA_RUN_PROVISION = "extraRunProvision";
457
458 /**
459 * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver}
460 * which will receive provisioning results. Can be left empty.
461 * @hide
462 */
463 public static final String EXTRA_PROVISION_CALLBACK = "extraProvisionCallback";
464
465 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800466 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700467 * @hide
468 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100469 @UnsupportedAppUsage
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700470 public static final int TYPE_NONE = -1;
471
472 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900473 * A Mobile data connection. Devices may support more than one.
474 *
475 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
476 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
477 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700478 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900479 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700480 public static final int TYPE_MOBILE = 0;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900481
Robert Greenwalt42acef32009-08-12 16:08:25 -0700482 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900483 * A WIFI data connection. Devices may support more than one.
484 *
485 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
486 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
487 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700488 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900489 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700490 public static final int TYPE_WIFI = 1;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900491
Robert Greenwalt42acef32009-08-12 16:08:25 -0700492 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800493 * An MMS-specific Mobile data connection. This network type may use the
494 * same network interface as {@link #TYPE_MOBILE} or it may use a different
495 * one. This is used by applications needing to talk to the carrier's
496 * Multimedia Messaging Service servers.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900497 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900498 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900499 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900500 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700501 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700502 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700503 public static final int TYPE_MOBILE_MMS = 2;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900504
Robert Greenwalt42acef32009-08-12 16:08:25 -0700505 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800506 * A SUPL-specific Mobile data connection. This network type may use the
507 * same network interface as {@link #TYPE_MOBILE} or it may use a different
508 * one. This is used by applications needing to talk to the carrier's
509 * Secure User Plane Location servers for help locating the device.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900510 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900511 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900512 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900513 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700514 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700515 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700516 public static final int TYPE_MOBILE_SUPL = 3;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900517
Robert Greenwalt42acef32009-08-12 16:08:25 -0700518 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800519 * A DUN-specific Mobile data connection. This network type may use the
520 * same network interface as {@link #TYPE_MOBILE} or it may use a different
521 * one. This is sometimes by the system when setting up an upstream connection
522 * for tethering so that the carrier is aware of DUN traffic.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900523 *
524 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
525 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
526 * provides the {@link NetworkCapabilities#NET_CAPABILITY_DUN} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700527 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900528 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700529 public static final int TYPE_MOBILE_DUN = 4;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900530
Robert Greenwalt42acef32009-08-12 16:08:25 -0700531 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800532 * A High Priority Mobile data connection. This network type uses the
533 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colittie285b432015-04-23 15:32:42 +0900534 * is different.
535 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900536 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
537 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
538 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700539 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700540 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700541 public static final int TYPE_MOBILE_HIPRI = 5;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900542
jsh8214deb2010-03-11 15:04:43 -0800543 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900544 * A WiMAX data connection.
545 *
546 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
547 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
548 * appropriate network. {@see NetworkCapabilities} for supported transports.
jsh8214deb2010-03-11 15:04:43 -0800549 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900550 @Deprecated
jsh8214deb2010-03-11 15:04:43 -0800551 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800552
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800553 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900554 * A Bluetooth data connection.
555 *
556 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
557 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
558 * appropriate network. {@see NetworkCapabilities} for supported transports.
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800559 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900560 @Deprecated
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800561 public static final int TYPE_BLUETOOTH = 7;
562
Robert Greenwalt60810842011-04-22 15:28:18 -0700563 /**
564 * Dummy data connection. This should not be used on shipping devices.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900565 * @deprecated This is not used any more.
Robert Greenwalt60810842011-04-22 15:28:18 -0700566 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900567 @Deprecated
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800568 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800569
Robert Greenwalt60810842011-04-22 15:28:18 -0700570 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900571 * An Ethernet data connection.
572 *
573 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
574 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
575 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt60810842011-04-22 15:28:18 -0700576 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900577 @Deprecated
Robert Greenwalte12aec92011-01-28 14:48:37 -0800578 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700579
Wink Saville9d7d6282011-03-12 14:52:01 -0800580 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800581 * Over the air Administration.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900582 * @deprecated Use {@link NetworkCapabilities} instead.
Wink Saville9d7d6282011-03-12 14:52:01 -0800583 * {@hide}
584 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900585 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100586 @UnsupportedAppUsage
Wink Saville9d7d6282011-03-12 14:52:01 -0800587 public static final int TYPE_MOBILE_FOTA = 10;
588
589 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800590 * IP Multimedia Subsystem.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900591 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IMS} instead.
Wink Saville9d7d6282011-03-12 14:52:01 -0800592 * {@hide}
593 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900594 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100595 @UnsupportedAppUsage
Wink Saville9d7d6282011-03-12 14:52:01 -0800596 public static final int TYPE_MOBILE_IMS = 11;
597
598 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800599 * Carrier Branded Services.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900600 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_CBS} instead.
Wink Saville9d7d6282011-03-12 14:52:01 -0800601 * {@hide}
602 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900603 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100604 @UnsupportedAppUsage
Wink Saville9d7d6282011-03-12 14:52:01 -0800605 public static final int TYPE_MOBILE_CBS = 12;
606
repo syncaea743a2011-07-29 23:55:49 -0700607 /**
608 * A Wi-Fi p2p connection. Only requesting processes will have access to
609 * the peers connected.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900610 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_WIFI_P2P} instead.
repo syncaea743a2011-07-29 23:55:49 -0700611 * {@hide}
612 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900613 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100614 @UnsupportedAppUsage
repo syncaea743a2011-07-29 23:55:49 -0700615 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800616
Wink Saville5e56bc52013-07-29 15:00:57 -0700617 /**
618 * The network to use for initially attaching to the network
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900619 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IA} instead.
Wink Saville5e56bc52013-07-29 15:00:57 -0700620 * {@hide}
621 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900622 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100623 @UnsupportedAppUsage
Wink Saville5e56bc52013-07-29 15:00:57 -0700624 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700625
Lorenzo Colittie285b432015-04-23 15:32:42 +0900626 /**
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700627 * Emergency PDN connection for emergency services. This
628 * may include IMS and MMS in emergency situations.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900629 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_EIMS} instead.
Ram3e0e3bc2014-06-26 11:03:44 -0700630 * {@hide}
631 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900632 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100633 @UnsupportedAppUsage
Ram3e0e3bc2014-06-26 11:03:44 -0700634 public static final int TYPE_MOBILE_EMERGENCY = 15;
635
Hui Lu1c5624a2014-01-15 11:05:36 -0500636 /**
637 * The network that uses proxy to achieve connectivity.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900638 * @deprecated Use {@link NetworkCapabilities} instead.
Hui Lu1c5624a2014-01-15 11:05:36 -0500639 * {@hide}
640 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900641 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100642 @UnsupportedAppUsage
Hui Lu1c5624a2014-01-15 11:05:36 -0500643 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700644
Robert Greenwalt8283f882014-07-07 17:09:01 -0700645 /**
646 * A virtual network using one or more native bearers.
647 * It may or may not be providing security services.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900648 * @deprecated Applications should use {@link NetworkCapabilities#TRANSPORT_VPN} instead.
Robert Greenwalt8283f882014-07-07 17:09:01 -0700649 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900650 @Deprecated
Robert Greenwalt8283f882014-07-07 17:09:01 -0700651 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500652
653 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700654 public static final int MAX_RADIO_TYPE = TYPE_VPN;
655
656 /** {@hide} */
657 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658
Hugo Benichi16f0a942017-06-20 14:07:59 +0900659 private static final int MIN_NETWORK_TYPE = TYPE_MOBILE;
660
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800661 /**
662 * If you want to set the default network preference,you can directly
663 * change the networkAttributes array in framework's config.xml.
664 *
665 * @deprecated Since we support so many more networks now, the single
666 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800667 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800668 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800669 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800670 * from an App.
671 */
672 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800673 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
674
Jeff Sharkey625239a2012-09-26 22:03:49 -0700675 /**
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700676 * @hide
677 */
Hugo Benichia5c1f7f2017-06-20 14:10:14 +0900678 public static final int REQUEST_ID_UNSET = 0;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700679
Paul Jensen5d59e782014-07-11 12:28:19 -0400680 /**
Hugo Benichi31c176d2017-06-17 13:14:12 +0900681 * Static unique request used as a tombstone for NetworkCallbacks that have been unregistered.
682 * This allows to distinguish when unregistering NetworkCallbacks those that were never
Chalard Jean4d660112018-06-04 16:52:49 +0900683 * registered from those that were already unregistered.
Hugo Benichi31c176d2017-06-17 13:14:12 +0900684 * @hide
685 */
Hugo Benichia5c1f7f2017-06-20 14:10:14 +0900686 private static final NetworkRequest ALREADY_UNREGISTERED =
Hugo Benichi31c176d2017-06-17 13:14:12 +0900687 new NetworkRequest.Builder().clearCapabilities().build();
688
689 /**
Paul Jensen5d59e782014-07-11 12:28:19 -0400690 * A NetID indicating no Network is selected.
691 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
692 * @hide
693 */
694 public static final int NETID_UNSET = 0;
695
Erik Kline4d092232017-10-30 15:29:44 +0900696 /**
697 * Private DNS Mode values.
698 *
699 * The "private_dns_mode" global setting stores a String value which is
700 * expected to be one of the following.
701 */
702
703 /**
704 * @hide
705 */
706 public static final String PRIVATE_DNS_MODE_OFF = "off";
707 /**
708 * @hide
709 */
710 public static final String PRIVATE_DNS_MODE_OPPORTUNISTIC = "opportunistic";
711 /**
712 * @hide
713 */
714 public static final String PRIVATE_DNS_MODE_PROVIDER_HOSTNAME = "hostname";
715 /**
716 * The default Private DNS mode.
717 *
718 * This may change from release to release or may become dependent upon
719 * the capabilities of the underlying platform.
720 *
721 * @hide
722 */
Erik Kline19841792018-05-16 16:41:57 +0900723 public static final String PRIVATE_DNS_DEFAULT_MODE_FALLBACK = PRIVATE_DNS_MODE_OPPORTUNISTIC;
Erik Kline4d092232017-10-30 15:29:44 +0900724
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100725 @UnsupportedAppUsage
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700726 private final IConnectivityManager mService;
Paul Jensene0bef712014-12-10 15:12:18 -0500727 /**
728 * A kludge to facilitate static access where a Context pointer isn't available, like in the
729 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
730 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
731 * methods that take a Context argument.
732 */
733 private static ConnectivityManager sInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900735 private final Context mContext;
736
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800737 private INetworkManagementService mNMService;
Felipe Leme1b103232016-01-22 09:44:57 -0800738 private INetworkPolicyManager mNPManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800739
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800740 /**
741 * Tests if a given integer represents a valid network type.
742 * @param networkType the type to be tested
743 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensen9e59e122015-05-06 10:42:25 -0400744 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
745 * validate a network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800746 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700747 @Deprecated
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700748 public static boolean isNetworkTypeValid(int networkType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900749 return MIN_NETWORK_TYPE <= networkType && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800750 }
751
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800752 /**
753 * Returns a non-localized string representing a given network type.
754 * ONLY used for debugging output.
755 * @param type the type needing naming
756 * @return a String for the given type, or a string version of the type ("87")
757 * if no name is known.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900758 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800759 * {@hide}
760 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900761 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100762 @UnsupportedAppUsage
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700763 public static String getNetworkTypeName(int type) {
764 switch (type) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900765 case TYPE_NONE:
766 return "NONE";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700767 case TYPE_MOBILE:
768 return "MOBILE";
769 case TYPE_WIFI:
770 return "WIFI";
771 case TYPE_MOBILE_MMS:
772 return "MOBILE_MMS";
773 case TYPE_MOBILE_SUPL:
774 return "MOBILE_SUPL";
775 case TYPE_MOBILE_DUN:
776 return "MOBILE_DUN";
777 case TYPE_MOBILE_HIPRI:
778 return "MOBILE_HIPRI";
779 case TYPE_WIMAX:
780 return "WIMAX";
781 case TYPE_BLUETOOTH:
782 return "BLUETOOTH";
783 case TYPE_DUMMY:
784 return "DUMMY";
785 case TYPE_ETHERNET:
786 return "ETHERNET";
787 case TYPE_MOBILE_FOTA:
788 return "MOBILE_FOTA";
789 case TYPE_MOBILE_IMS:
790 return "MOBILE_IMS";
791 case TYPE_MOBILE_CBS:
792 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700793 case TYPE_WIFI_P2P:
794 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700795 case TYPE_MOBILE_IA:
796 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700797 case TYPE_MOBILE_EMERGENCY:
798 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500799 case TYPE_PROXY:
800 return "PROXY";
Erik Kline37fbfa12014-11-19 17:23:41 +0900801 case TYPE_VPN:
802 return "VPN";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700803 default:
804 return Integer.toString(type);
805 }
806 }
807
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800808 /**
809 * Checks if a given type uses the cellular data connection.
810 * This should be replaced in the future by a network property.
811 * @param networkType the type to check
812 * @return a boolean - {@code true} if uses cellular network, else {@code false}
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900813 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800814 * {@hide}
815 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900816 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100817 @UnsupportedAppUsage
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700818 public static boolean isNetworkTypeMobile(int networkType) {
819 switch (networkType) {
820 case TYPE_MOBILE:
821 case TYPE_MOBILE_MMS:
822 case TYPE_MOBILE_SUPL:
823 case TYPE_MOBILE_DUN:
824 case TYPE_MOBILE_HIPRI:
825 case TYPE_MOBILE_FOTA:
826 case TYPE_MOBILE_IMS:
827 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700828 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700829 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700830 return true;
831 default:
832 return false;
833 }
834 }
835
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800836 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700837 * Checks if the given network type is backed by a Wi-Fi radio.
838 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900839 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700840 * @hide
841 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900842 @Deprecated
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700843 public static boolean isNetworkTypeWifi(int networkType) {
844 switch (networkType) {
845 case TYPE_WIFI:
846 case TYPE_WIFI_P2P:
847 return true;
848 default:
849 return false;
850 }
851 }
852
853 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800854 * Specifies the preferred network type. When the device has more
855 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800856 *
857 * @param preference the network type to prefer over all others. It is
858 * unspecified what happens to the old preferred network in the
859 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700860 * @deprecated Functionality has been removed as it no longer makes sense,
861 * with many more than two networks - we'd need an array to express
862 * preference. Instead we use dynamic network properties of
863 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800864 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700865 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 }
868
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800869 /**
870 * Retrieves the current preferred network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800871 *
872 * @return an integer representing the preferred network type
873 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700874 * @deprecated Functionality has been removed as it no longer makes sense,
875 * with many more than two networks - we'd need an array to express
876 * preference. Instead we use dynamic network properties of
877 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800878 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700879 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600880 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700882 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 }
884
Scott Main671644c2011-10-06 19:02:28 -0700885 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800886 * Returns details about the currently active default data network. When
887 * connected, this network is the default route for outgoing connections.
888 * You should always check {@link NetworkInfo#isConnected()} before initiating
889 * network traffic. This may return {@code null} when there is no default
890 * network.
Chalard Jean5a041d12018-03-29 17:45:24 +0900891 * Note that if the default network is a VPN, this method will return the
892 * NetworkInfo for one of its underlying networks instead, or null if the
893 * VPN agent did not specify any. Apps interested in learning about VPNs
894 * should use {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800895 *
896 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500897 * or {@code null} if no default network is currently active
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700898 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600899 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 public NetworkInfo getActiveNetworkInfo() {
901 try {
902 return mService.getActiveNetworkInfo();
903 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700904 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800905 }
906 }
907
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800908 /**
Paul Jensen31a94f42015-02-13 14:18:39 -0500909 * Returns a {@link Network} object corresponding to the currently active
910 * default data network. In the event that the current active default data
911 * network disconnects, the returned {@code Network} object will no longer
912 * be usable. This will return {@code null} when there is no default
913 * network.
914 *
915 * @return a {@link Network} object for the current default network or
916 * {@code null} if no default network is currently active
Paul Jensen31a94f42015-02-13 14:18:39 -0500917 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600918 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Paul Jensen31a94f42015-02-13 14:18:39 -0500919 public Network getActiveNetwork() {
920 try {
921 return mService.getActiveNetwork();
922 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700923 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -0500924 }
925 }
926
927 /**
Robin Leed2baf792016-03-24 12:07:00 +0000928 * Returns a {@link Network} object corresponding to the currently active
929 * default data network for a specific UID. In the event that the default data
930 * network disconnects, the returned {@code Network} object will no longer
931 * be usable. This will return {@code null} when there is no default
932 * network for the UID.
Robin Leed2baf792016-03-24 12:07:00 +0000933 *
934 * @return a {@link Network} object for the current default network for the
935 * given UID or {@code null} if no default network is currently active
936 *
937 * @hide
938 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600939 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Robin Leed2baf792016-03-24 12:07:00 +0000940 public Network getActiveNetworkForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600941 return getActiveNetworkForUid(uid, false);
942 }
943
944 /** {@hide} */
945 public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) {
Robin Leed2baf792016-03-24 12:07:00 +0000946 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600947 return mService.getActiveNetworkForUid(uid, ignoreBlocked);
Robin Leed2baf792016-03-24 12:07:00 +0000948 } catch (RemoteException e) {
949 throw e.rethrowFromSystemServer();
950 }
951 }
952
953 /**
Charles He36738632017-05-15 17:07:18 +0100954 * Checks if a VPN app supports always-on mode.
955 *
956 * In order to support the always-on feature, an app has to
957 * <ul>
958 * <li>target {@link VERSION_CODES#N API 24} or above, and
Charles Hec57a01c2017-08-15 15:30:22 +0100959 * <li>not opt out through the {@link VpnService#SERVICE_META_DATA_SUPPORTS_ALWAYS_ON}
960 * meta-data field.
Charles He36738632017-05-15 17:07:18 +0100961 * </ul>
962 *
963 * @param userId The identifier of the user for whom the VPN app is installed.
964 * @param vpnPackage The canonical package name of the VPN app.
965 * @return {@code true} if and only if the VPN app exists and supports always-on mode.
966 * @hide
967 */
968 public boolean isAlwaysOnVpnPackageSupportedForUser(int userId, @Nullable String vpnPackage) {
969 try {
970 return mService.isAlwaysOnVpnPackageSupported(userId, vpnPackage);
971 } catch (RemoteException e) {
972 throw e.rethrowFromSystemServer();
973 }
974 }
975
976 /**
Robin Lee244ce8e2016-01-05 18:03:46 +0000977 * Configures an always-on VPN connection through a specific application.
978 * This connection is automatically granted and persisted after a reboot.
979 *
980 * <p>The designated package should declare a {@link VpnService} in its
981 * manifest guarded by {@link android.Manifest.permission.BIND_VPN_SERVICE},
982 * otherwise the call will fail.
983 *
984 * @param userId The identifier of the user to set an always-on VPN for.
985 * @param vpnPackage The package name for an installed VPN app on the device, or {@code null}
986 * to remove an existing always-on VPN configuration.
Robin Leedc679712016-05-03 13:23:03 +0100987 * @param lockdownEnabled {@code true} to disallow networking when the VPN is not connected or
988 * {@code false} otherwise.
Robin Lee244ce8e2016-01-05 18:03:46 +0000989 * @return {@code true} if the package is set as always-on VPN controller;
990 * {@code false} otherwise.
991 * @hide
992 */
Robin Leedc679712016-05-03 13:23:03 +0100993 public boolean setAlwaysOnVpnPackageForUser(int userId, @Nullable String vpnPackage,
994 boolean lockdownEnabled) {
Robin Lee244ce8e2016-01-05 18:03:46 +0000995 try {
Robin Leedc679712016-05-03 13:23:03 +0100996 return mService.setAlwaysOnVpnPackage(userId, vpnPackage, lockdownEnabled);
Robin Lee244ce8e2016-01-05 18:03:46 +0000997 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700998 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +0000999 }
1000 }
1001
1002 /**
1003 * Returns the package name of the currently set always-on VPN application.
1004 * If there is no always-on VPN set, or the VPN is provided by the system instead
1005 * of by an app, {@code null} will be returned.
1006 *
1007 * @return Package name of VPN controller responsible for always-on VPN,
1008 * or {@code null} if none is set.
1009 * @hide
1010 */
1011 public String getAlwaysOnVpnPackageForUser(int userId) {
1012 try {
1013 return mService.getAlwaysOnVpnPackage(userId);
1014 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001015 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +00001016 }
1017 }
1018
1019 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001020 * Returns details about the currently active default data network
1021 * for a given uid. This is for internal use only to avoid spying
1022 * other apps.
1023 *
1024 * @return a {@link NetworkInfo} object for the current default network
1025 * for the given uid or {@code null} if no default network is
1026 * available for the specified uid.
1027 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001028 * {@hide}
1029 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001030 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001031 @UnsupportedAppUsage
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001032 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001033 return getActiveNetworkInfoForUid(uid, false);
1034 }
1035
1036 /** {@hide} */
1037 public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001038 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001039 return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001040 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001041 throw e.rethrowFromSystemServer();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001042 }
1043 }
1044
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001045 /**
1046 * Returns connection status information about a particular
1047 * network type.
1048 *
1049 * @param networkType integer specifying which networkType in
1050 * which you're interested.
1051 * @return a {@link NetworkInfo} object for the requested
1052 * network type or {@code null} if the type is not
Chalard Jean5a041d12018-03-29 17:45:24 +09001053 * supported by the device. If {@code networkType} is
1054 * TYPE_VPN and a VPN is active for the calling app,
1055 * then this method will try to return one of the
1056 * underlying networks for the VPN or null if the
1057 * VPN agent didn't specify any.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001058 *
Paul Jensen3541e9f2015-03-18 12:23:02 -04001059 * @deprecated This method does not support multiple connected networks
1060 * of the same type. Use {@link #getAllNetworks} and
1061 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001062 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001063 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001064 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 public NetworkInfo getNetworkInfo(int networkType) {
1066 try {
1067 return mService.getNetworkInfo(networkType);
1068 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001069 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001070 }
1071 }
1072
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001073 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001074 * Returns connection status information about a particular
1075 * Network.
1076 *
1077 * @param network {@link Network} specifying which network
1078 * in which you're interested.
1079 * @return a {@link NetworkInfo} object for the requested
1080 * network or {@code null} if the {@code Network}
1081 * is not valid.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001082 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001083 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001084 public NetworkInfo getNetworkInfo(Network network) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001085 return getNetworkInfoForUid(network, Process.myUid(), false);
1086 }
1087
1088 /** {@hide} */
1089 public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001090 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001091 return mService.getNetworkInfoForUid(network, uid, ignoreBlocked);
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001092 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001093 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001094 }
1095 }
1096
1097 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001098 * Returns connection status information about all network
1099 * types supported by the device.
1100 *
1101 * @return an array of {@link NetworkInfo} objects. Check each
1102 * {@link NetworkInfo#getType} for which type each applies.
1103 *
Paul Jensen3541e9f2015-03-18 12:23:02 -04001104 * @deprecated This method does not support multiple connected networks
1105 * of the same type. Use {@link #getAllNetworks} and
1106 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001107 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001108 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001109 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001110 public NetworkInfo[] getAllNetworkInfo() {
1111 try {
1112 return mService.getAllNetworkInfo();
1113 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001114 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 }
1116 }
1117
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001118 /**
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001119 * Returns the {@link Network} object currently serving a given type, or
1120 * null if the given type is not connected.
1121 *
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001122 * @hide
Paul Jensen3541e9f2015-03-18 12:23:02 -04001123 * @deprecated This method does not support multiple connected networks
1124 * of the same type. Use {@link #getAllNetworks} and
1125 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001126 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001127 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001128 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001129 @UnsupportedAppUsage
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001130 public Network getNetworkForType(int networkType) {
1131 try {
1132 return mService.getNetworkForType(networkType);
1133 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001134 throw e.rethrowFromSystemServer();
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001135 }
1136 }
1137
1138 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001139 * Returns an array of all {@link Network} currently tracked by the
1140 * framework.
Paul Jensenb2748922015-05-06 11:10:18 -04001141 *
1142 * @return an array of {@link Network} objects.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001143 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001144 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001145 public Network[] getAllNetworks() {
1146 try {
1147 return mService.getAllNetworks();
1148 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001149 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001150 }
1151 }
1152
1153 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001154 * Returns an array of {@link android.net.NetworkCapabilities} objects, representing
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001155 * the Networks that applications run by the given user will use by default.
1156 * @hide
1157 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001158 @UnsupportedAppUsage
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001159 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
1160 try {
1161 return mService.getDefaultNetworkCapabilitiesForUser(userId);
1162 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001163 throw e.rethrowFromSystemServer();
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001164 }
1165 }
1166
1167 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001168 * Returns the IP information for the current default network.
1169 *
1170 * @return a {@link LinkProperties} object describing the IP info
1171 * for the current default network, or {@code null} if there
1172 * is no current default network.
1173 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001174 * {@hide}
1175 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001176 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001177 @UnsupportedAppUsage
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001178 public LinkProperties getActiveLinkProperties() {
1179 try {
1180 return mService.getActiveLinkProperties();
1181 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001182 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001183 }
1184 }
1185
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001186 /**
1187 * Returns the IP information for a given network type.
1188 *
1189 * @param networkType the network type of interest.
1190 * @return a {@link LinkProperties} object describing the IP info
1191 * for the given networkType, or {@code null} if there is
1192 * no current default network.
1193 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001194 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04001195 * @deprecated This method does not support multiple connected networks
1196 * of the same type. Use {@link #getAllNetworks},
1197 * {@link #getNetworkInfo(android.net.Network)}, and
1198 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001199 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001200 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001201 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001202 @UnsupportedAppUsage
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001203 public LinkProperties getLinkProperties(int networkType) {
1204 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001205 return mService.getLinkPropertiesForType(networkType);
1206 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001207 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001208 }
1209 }
1210
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001211 /**
1212 * Get the {@link LinkProperties} for the given {@link Network}. This
1213 * will return {@code null} if the network is unknown.
1214 *
1215 * @param network The {@link Network} object identifying the network in question.
1216 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensenb2748922015-05-06 11:10:18 -04001217 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001218 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt9258c642014-03-26 16:47:06 -07001219 public LinkProperties getLinkProperties(Network network) {
1220 try {
1221 return mService.getLinkProperties(network);
1222 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001223 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001224 }
1225 }
1226
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001227 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001228 * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001229 * will return {@code null} if the network is unknown.
1230 *
1231 * @param network The {@link Network} object identifying the network in question.
Lorenzo Colittie285b432015-04-23 15:32:42 +09001232 * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001233 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001234 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt9258c642014-03-26 16:47:06 -07001235 public NetworkCapabilities getNetworkCapabilities(Network network) {
1236 try {
1237 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001238 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001239 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001240 }
1241 }
1242
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001243 /**
Udam Sainib7c24872016-01-04 12:16:14 -08001244 * Gets the URL that should be used for resolving whether a captive portal is present.
1245 * 1. This URL should respond with a 204 response to a GET request to indicate no captive
1246 * portal is present.
1247 * 2. This URL must be HTTP as redirect responses are used to find captive portal
1248 * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects.
1249 *
1250 * @hide
1251 */
1252 @SystemApi
Udam Saini0e94c362017-06-07 12:06:28 -07001253 @RequiresPermission(android.Manifest.permission.LOCAL_MAC_ADDRESS)
Udam Sainib7c24872016-01-04 12:16:14 -08001254 public String getCaptivePortalServerUrl() {
1255 try {
1256 return mService.getCaptivePortalServerUrl();
1257 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001258 throw e.rethrowFromSystemServer();
Udam Sainib7c24872016-01-04 12:16:14 -08001259 }
1260 }
1261
1262 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 * Tells the underlying networking system that the caller wants to
1264 * begin using the named feature. The interpretation of {@code feature}
1265 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001266 *
1267 * <p>This method requires the caller to hold either the
1268 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1269 * or the ability to modify system settings as determined by
1270 * {@link android.provider.Settings.System#canWrite}.</p>
1271 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 * @param networkType specifies which network the request pertains to
1273 * @param feature the name of the feature to be used
1274 * @return an integer value representing the outcome of the request.
1275 * The interpretation of this value is specific to each networking
1276 * implementation+feature combination, except that the value {@code -1}
1277 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001278 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001279 * @deprecated Deprecated in favor of the cleaner
1280 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001281 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001282 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001283 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001285 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001287 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001288 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1289 if (netCap == null) {
1290 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
1291 feature);
1292 return PhoneConstants.APN_REQUEST_FAILED;
1293 }
1294
1295 NetworkRequest request = null;
1296 synchronized (sLegacyRequests) {
1297 LegacyRequest l = sLegacyRequests.get(netCap);
1298 if (l != null) {
1299 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
1300 renewRequestLocked(l);
1301 if (l.currentNetwork != null) {
1302 return PhoneConstants.APN_ALREADY_ACTIVE;
1303 } else {
1304 return PhoneConstants.APN_REQUEST_STARTED;
1305 }
1306 }
1307
1308 request = requestNetworkForFeatureLocked(netCap);
1309 }
1310 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -07001311 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001312 return PhoneConstants.APN_REQUEST_STARTED;
1313 } else {
1314 Log.d(TAG, " request Failed");
1315 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001316 }
1317 }
1318
1319 /**
1320 * Tells the underlying networking system that the caller is finished
1321 * using the named feature. The interpretation of {@code feature}
1322 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001323 *
1324 * <p>This method requires the caller to hold either the
1325 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1326 * or the ability to modify system settings as determined by
1327 * {@link android.provider.Settings.System#canWrite}.</p>
1328 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 * @param networkType specifies which network the request pertains to
1330 * @param feature the name of the feature that is no longer needed
1331 * @return an integer value representing the outcome of the request.
1332 * The interpretation of this value is specific to each networking
1333 * implementation+feature combination, except that the value {@code -1}
1334 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001335 *
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09001336 * @deprecated Deprecated in favor of the cleaner
1337 * {@link #unregisterNetworkCallback(NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001338 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001339 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001340 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001341 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001342 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001343 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001344 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001345 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1346 if (netCap == null) {
1347 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
1348 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001349 return -1;
1350 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001351
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001352 if (removeRequestForFeature(netCap)) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001353 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001354 }
1355 return 1;
1356 }
1357
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001358 @UnsupportedAppUsage
Robert Greenwalt562cc542014-05-15 18:07:26 -07001359 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1360 if (networkType == TYPE_MOBILE) {
Erik Kline35bf06c2017-01-26 18:08:28 +09001361 switch (feature) {
1362 case "enableCBS":
1363 return networkCapabilitiesForType(TYPE_MOBILE_CBS);
1364 case "enableDUN":
1365 case "enableDUNAlways":
1366 return networkCapabilitiesForType(TYPE_MOBILE_DUN);
1367 case "enableFOTA":
1368 return networkCapabilitiesForType(TYPE_MOBILE_FOTA);
1369 case "enableHIPRI":
1370 return networkCapabilitiesForType(TYPE_MOBILE_HIPRI);
1371 case "enableIMS":
1372 return networkCapabilitiesForType(TYPE_MOBILE_IMS);
1373 case "enableMMS":
1374 return networkCapabilitiesForType(TYPE_MOBILE_MMS);
1375 case "enableSUPL":
1376 return networkCapabilitiesForType(TYPE_MOBILE_SUPL);
1377 default:
1378 return null;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001379 }
Erik Kline35bf06c2017-01-26 18:08:28 +09001380 } else if (networkType == TYPE_WIFI && "p2p".equals(feature)) {
1381 return networkCapabilitiesForType(TYPE_WIFI_P2P);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001382 }
1383 return null;
1384 }
1385
Robert Greenwalt06314e42014-10-29 14:04:06 -07001386 /**
1387 * Guess what the network request was trying to say so that the resulting
1388 * network is accessible via the legacy (deprecated) API such as
1389 * requestRouteToHost.
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001390 *
1391 * This means we should try to be fairly precise about transport and
Robert Greenwalt06314e42014-10-29 14:04:06 -07001392 * capability but ignore things such as networkSpecifier.
1393 * If the request has more than one transport or capability it doesn't
1394 * match the old legacy requests (they selected only single transport/capability)
1395 * so this function cannot map the request to a single legacy type and
1396 * the resulting network will not be available to the legacy APIs.
1397 *
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001398 * This code is only called from the requestNetwork API (L and above).
1399 *
1400 * Setting a legacy type causes CONNECTIVITY_ACTION broadcasts, which are expensive
1401 * because they wake up lots of apps - see http://b/23350688 . So we currently only
1402 * do this for SUPL requests, which are the only ones that we know need it. If
1403 * omitting these broadcasts causes unacceptable app breakage, then for backwards
1404 * compatibility we can send them:
1405 *
1406 * if (targetSdkVersion < Build.VERSION_CODES.M) && // legacy API unsupported >= M
1407 * targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP)) // requestNetwork not present < L
1408 *
Robert Greenwalt06314e42014-10-29 14:04:06 -07001409 * TODO - This should be removed when the legacy APIs are removed.
1410 */
Ye Wenb87875e2014-07-21 14:19:01 -07001411 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1412 if (netCap == null) {
1413 return TYPE_NONE;
1414 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001415
Ye Wenb87875e2014-07-21 14:19:01 -07001416 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1417 return TYPE_NONE;
1418 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001419
Lifu Tang30f95a72016-01-07 23:20:38 -08001420 // Do this only for SUPL, until GnssLocationProvider is fixed. http://b/25876485 .
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001421 if (!netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1422 // NOTE: if this causes app breakage, we should not just comment out this early return;
1423 // instead, we should make this early return conditional on the requesting app's target
1424 // SDK version, as described in the comment above.
1425 return TYPE_NONE;
1426 }
1427
Robert Greenwalt06314e42014-10-29 14:04:06 -07001428 String type = null;
1429 int result = TYPE_NONE;
1430
Ye Wenb87875e2014-07-21 14:19:01 -07001431 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
Robert Greenwalt06314e42014-10-29 14:04:06 -07001432 type = "enableCBS";
1433 result = TYPE_MOBILE_CBS;
1434 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1435 type = "enableIMS";
1436 result = TYPE_MOBILE_IMS;
1437 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1438 type = "enableFOTA";
1439 result = TYPE_MOBILE_FOTA;
1440 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1441 type = "enableDUN";
1442 result = TYPE_MOBILE_DUN;
1443 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001444 type = "enableSUPL";
Robert Greenwalt06314e42014-10-29 14:04:06 -07001445 result = TYPE_MOBILE_SUPL;
Robert Greenwalt74ab4fa2015-08-28 12:37:54 -07001446 // back out this hack for mms as they no longer need this and it's causing
1447 // device slowdowns - b/23350688 (note, supl still needs this)
1448 //} else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1449 // type = "enableMMS";
1450 // result = TYPE_MOBILE_MMS;
Robert Greenwalt06314e42014-10-29 14:04:06 -07001451 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1452 type = "enableHIPRI";
1453 result = TYPE_MOBILE_HIPRI;
Ye Wenb87875e2014-07-21 14:19:01 -07001454 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001455 if (type != null) {
1456 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1457 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1458 return result;
Ye Wenb87875e2014-07-21 14:19:01 -07001459 }
1460 }
1461 return TYPE_NONE;
1462 }
1463
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001464 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001465 if (netCap == null) return TYPE_NONE;
1466 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1467 return TYPE_MOBILE_CBS;
1468 }
1469 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1470 return TYPE_MOBILE_IMS;
1471 }
1472 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1473 return TYPE_MOBILE_FOTA;
1474 }
1475 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1476 return TYPE_MOBILE_DUN;
1477 }
1478 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1479 return TYPE_MOBILE_SUPL;
1480 }
1481 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1482 return TYPE_MOBILE_MMS;
1483 }
1484 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1485 return TYPE_MOBILE_HIPRI;
1486 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001487 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1488 return TYPE_WIFI_P2P;
1489 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001490 return TYPE_NONE;
1491 }
1492
1493 private static class LegacyRequest {
1494 NetworkCapabilities networkCapabilities;
1495 NetworkRequest networkRequest;
1496 int expireSequenceNumber;
1497 Network currentNetwork;
1498 int delay = -1;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001499
1500 private void clearDnsBinding() {
1501 if (currentNetwork != null) {
1502 currentNetwork = null;
1503 setProcessDefaultNetworkForHostResolution(null);
1504 }
1505 }
1506
Robert Greenwalt6078b502014-06-11 16:05:07 -07001507 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001508 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001509 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001510 currentNetwork = network;
1511 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001512 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001513 }
1514 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001515 public void onLost(Network network) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001516 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001517 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1518 }
1519 };
1520 }
1521
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001522 @UnsupportedAppUsage
Chalard Jean4d660112018-06-04 16:52:49 +09001523 private static final HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
1524 new HashMap<>();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001525
1526 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1527 synchronized (sLegacyRequests) {
1528 LegacyRequest l = sLegacyRequests.get(netCap);
1529 if (l != null) return l.networkRequest;
1530 }
1531 return null;
1532 }
1533
1534 private void renewRequestLocked(LegacyRequest l) {
1535 l.expireSequenceNumber++;
1536 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1537 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1538 }
1539
1540 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1541 int ourSeqNum = -1;
1542 synchronized (sLegacyRequests) {
1543 LegacyRequest l = sLegacyRequests.get(netCap);
1544 if (l == null) return;
1545 ourSeqNum = l.expireSequenceNumber;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001546 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001547 }
1548 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1549 }
1550
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001551 @UnsupportedAppUsage
Robert Greenwalt562cc542014-05-15 18:07:26 -07001552 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1553 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001554 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001555 try {
1556 delay = mService.getRestoreDefaultNetworkDelay(type);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001557 } catch (RemoteException e) {
1558 throw e.rethrowFromSystemServer();
1559 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001560 LegacyRequest l = new LegacyRequest();
1561 l.networkCapabilities = netCap;
1562 l.delay = delay;
1563 l.expireSequenceNumber = 0;
Hugo Benichi2583ef02017-02-02 17:02:36 +09001564 l.networkRequest = sendRequestForNetwork(
1565 netCap, l.networkCallback, 0, REQUEST, type, getDefaultHandler());
Robert Greenwalt562cc542014-05-15 18:07:26 -07001566 if (l.networkRequest == null) return null;
1567 sLegacyRequests.put(netCap, l);
1568 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1569 return l.networkRequest;
1570 }
1571
1572 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1573 if (delay >= 0) {
1574 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
Hugo Benichi2583ef02017-02-02 17:02:36 +09001575 CallbackHandler handler = getDefaultHandler();
Hugo Benichi6f260f32017-02-03 14:18:44 +09001576 Message msg = handler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1577 handler.sendMessageDelayed(msg, delay);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001578 }
1579 }
1580
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001581 @UnsupportedAppUsage
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001582 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1583 final LegacyRequest l;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001584 synchronized (sLegacyRequests) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001585 l = sLegacyRequests.remove(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001586 }
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001587 if (l == null) return false;
1588 unregisterNetworkCallback(l.networkCallback);
1589 l.clearDnsBinding();
1590 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001591 }
1592
Erik Kline35bf06c2017-01-26 18:08:28 +09001593 private static final SparseIntArray sLegacyTypeToTransport = new SparseIntArray();
1594 static {
1595 sLegacyTypeToTransport.put(TYPE_MOBILE, NetworkCapabilities.TRANSPORT_CELLULAR);
1596 sLegacyTypeToTransport.put(TYPE_MOBILE_CBS, NetworkCapabilities.TRANSPORT_CELLULAR);
1597 sLegacyTypeToTransport.put(TYPE_MOBILE_DUN, NetworkCapabilities.TRANSPORT_CELLULAR);
1598 sLegacyTypeToTransport.put(TYPE_MOBILE_FOTA, NetworkCapabilities.TRANSPORT_CELLULAR);
1599 sLegacyTypeToTransport.put(TYPE_MOBILE_HIPRI, NetworkCapabilities.TRANSPORT_CELLULAR);
1600 sLegacyTypeToTransport.put(TYPE_MOBILE_IMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1601 sLegacyTypeToTransport.put(TYPE_MOBILE_MMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1602 sLegacyTypeToTransport.put(TYPE_MOBILE_SUPL, NetworkCapabilities.TRANSPORT_CELLULAR);
1603 sLegacyTypeToTransport.put(TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI);
1604 sLegacyTypeToTransport.put(TYPE_WIFI_P2P, NetworkCapabilities.TRANSPORT_WIFI);
1605 sLegacyTypeToTransport.put(TYPE_BLUETOOTH, NetworkCapabilities.TRANSPORT_BLUETOOTH);
1606 sLegacyTypeToTransport.put(TYPE_ETHERNET, NetworkCapabilities.TRANSPORT_ETHERNET);
1607 }
1608
1609 private static final SparseIntArray sLegacyTypeToCapability = new SparseIntArray();
1610 static {
1611 sLegacyTypeToCapability.put(TYPE_MOBILE_CBS, NetworkCapabilities.NET_CAPABILITY_CBS);
1612 sLegacyTypeToCapability.put(TYPE_MOBILE_DUN, NetworkCapabilities.NET_CAPABILITY_DUN);
1613 sLegacyTypeToCapability.put(TYPE_MOBILE_FOTA, NetworkCapabilities.NET_CAPABILITY_FOTA);
1614 sLegacyTypeToCapability.put(TYPE_MOBILE_IMS, NetworkCapabilities.NET_CAPABILITY_IMS);
1615 sLegacyTypeToCapability.put(TYPE_MOBILE_MMS, NetworkCapabilities.NET_CAPABILITY_MMS);
1616 sLegacyTypeToCapability.put(TYPE_MOBILE_SUPL, NetworkCapabilities.NET_CAPABILITY_SUPL);
1617 sLegacyTypeToCapability.put(TYPE_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
1618 }
1619
1620 /**
1621 * Given a legacy type (TYPE_WIFI, ...) returns a NetworkCapabilities
1622 * instance suitable for registering a request or callback. Throws an
1623 * IllegalArgumentException if no mapping from the legacy type to
1624 * NetworkCapabilities is known.
1625 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +09001626 * @deprecated Types are deprecated. Use {@link NetworkCallback} or {@link NetworkRequest}
1627 * to find the network instead.
Erik Kline35bf06c2017-01-26 18:08:28 +09001628 * @hide
1629 */
1630 public static NetworkCapabilities networkCapabilitiesForType(int type) {
1631 final NetworkCapabilities nc = new NetworkCapabilities();
1632
1633 // Map from type to transports.
1634 final int NOT_FOUND = -1;
1635 final int transport = sLegacyTypeToTransport.get(type, NOT_FOUND);
Hugo Benichie7678512017-05-09 15:19:01 +09001636 Preconditions.checkArgument(transport != NOT_FOUND, "unknown legacy type: " + type);
Erik Kline35bf06c2017-01-26 18:08:28 +09001637 nc.addTransportType(transport);
1638
1639 // Map from type to capabilities.
1640 nc.addCapability(sLegacyTypeToCapability.get(
1641 type, NetworkCapabilities.NET_CAPABILITY_INTERNET));
1642 nc.maybeMarkCapabilitiesRestricted();
1643 return nc;
1644 }
1645
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001646 /** @hide */
1647 public static class PacketKeepaliveCallback {
1648 /** The requested keepalive was successfully started. */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001649 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001650 public void onStarted() {}
1651 /** The keepalive was successfully stopped. */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001652 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001653 public void onStopped() {}
1654 /** An error occurred. */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001655 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001656 public void onError(int error) {}
1657 }
1658
1659 /**
1660 * Allows applications to request that the system periodically send specific packets on their
1661 * behalf, using hardware offload to save battery power.
1662 *
1663 * To request that the system send keepalives, call one of the methods that return a
1664 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1665 * passing in a non-null callback. If the callback is successfully started, the callback's
1666 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1667 * specifying one of the {@code ERROR_*} constants in this class.
1668 *
Chalard Jean4d660112018-06-04 16:52:49 +09001669 * To stop an existing keepalive, call {@link PacketKeepalive#stop}. The system will call
1670 * {@link PacketKeepaliveCallback#onStopped} if the operation was successful or
1671 * {@link PacketKeepaliveCallback#onError} if an error occurred.
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001672 *
1673 * @hide
1674 */
1675 public class PacketKeepalive {
1676
1677 private static final String TAG = "PacketKeepalive";
1678
1679 /** @hide */
1680 public static final int SUCCESS = 0;
1681
1682 /** @hide */
1683 public static final int NO_KEEPALIVE = -1;
1684
1685 /** @hide */
1686 public static final int BINDER_DIED = -10;
1687
1688 /** The specified {@code Network} is not connected. */
1689 public static final int ERROR_INVALID_NETWORK = -20;
1690 /** The specified IP addresses are invalid. For example, the specified source IP address is
1691 * not configured on the specified {@code Network}. */
1692 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1693 /** The requested port is invalid. */
1694 public static final int ERROR_INVALID_PORT = -22;
1695 /** The packet length is invalid (e.g., too long). */
1696 public static final int ERROR_INVALID_LENGTH = -23;
1697 /** The packet transmission interval is invalid (e.g., too short). */
1698 public static final int ERROR_INVALID_INTERVAL = -24;
1699
1700 /** The hardware does not support this request. */
1701 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti9d1284e2015-09-08 16:46:36 +09001702 /** The hardware returned an error. */
1703 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001704
Nathan Harold63dd8132018-02-14 13:09:45 -08001705 /** The NAT-T destination port for IPsec */
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001706 public static final int NATT_PORT = 4500;
1707
Nathan Harold63dd8132018-02-14 13:09:45 -08001708 /** The minimum interval in seconds between keepalive packet transmissions */
1709 public static final int MIN_INTERVAL = 10;
1710
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001711 private final Network mNetwork;
1712 private final PacketKeepaliveCallback mCallback;
1713 private final Looper mLooper;
1714 private final Messenger mMessenger;
1715
1716 private volatile Integer mSlot;
1717
1718 void stopLooper() {
1719 mLooper.quit();
1720 }
1721
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001722 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001723 public void stop() {
1724 try {
1725 mService.stopKeepalive(mNetwork, mSlot);
1726 } catch (RemoteException e) {
1727 Log.e(TAG, "Error stopping packet keepalive: ", e);
1728 stopLooper();
1729 }
1730 }
1731
1732 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09001733 Preconditions.checkNotNull(network, "network cannot be null");
1734 Preconditions.checkNotNull(callback, "callback cannot be null");
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001735 mNetwork = network;
1736 mCallback = callback;
1737 HandlerThread thread = new HandlerThread(TAG);
1738 thread.start();
1739 mLooper = thread.getLooper();
1740 mMessenger = new Messenger(new Handler(mLooper) {
1741 @Override
1742 public void handleMessage(Message message) {
1743 switch (message.what) {
1744 case NetworkAgent.EVENT_PACKET_KEEPALIVE:
1745 int error = message.arg2;
1746 try {
1747 if (error == SUCCESS) {
1748 if (mSlot == null) {
1749 mSlot = message.arg1;
1750 mCallback.onStarted();
1751 } else {
1752 mSlot = null;
1753 stopLooper();
1754 mCallback.onStopped();
1755 }
1756 } else {
1757 stopLooper();
1758 mCallback.onError(error);
1759 }
1760 } catch (Exception e) {
1761 Log.e(TAG, "Exception in keepalive callback(" + error + ")", e);
1762 }
1763 break;
1764 default:
1765 Log.e(TAG, "Unhandled message " + Integer.toHexString(message.what));
1766 break;
1767 }
1768 }
1769 });
1770 }
1771 }
1772
1773 /**
1774 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
1775 *
1776 * @hide
1777 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001778 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001779 public PacketKeepalive startNattKeepalive(
1780 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
1781 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
1782 final PacketKeepalive k = new PacketKeepalive(network, callback);
1783 try {
1784 mService.startNattKeepalive(network, intervalSeconds, k.mMessenger, new Binder(),
1785 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
1786 } catch (RemoteException e) {
1787 Log.e(TAG, "Error starting packet keepalive: ", e);
1788 k.stopLooper();
1789 return null;
1790 }
1791 return k;
1792 }
1793
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001794 /**
1795 * Ensure that a network route exists to deliver traffic to the specified
1796 * host via the specified network interface. An attempt to add a route that
1797 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001798 *
1799 * <p>This method requires the caller to hold either the
1800 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1801 * or the ability to modify system settings as determined by
1802 * {@link android.provider.Settings.System#canWrite}.</p>
1803 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 * @param networkType the type of the network over which traffic to the specified
1805 * host is to be routed
1806 * @param hostAddress the IP address of the host to which the route is desired
1807 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001808 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001809 * @deprecated Deprecated in favor of the
1810 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
1811 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001812 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001813 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001814 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001816 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001818 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001819 }
1820
1821 /**
1822 * Ensure that a network route exists to deliver traffic to the specified
1823 * host via the specified network interface. An attempt to add a route that
1824 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001825 *
1826 * <p>This method requires the caller to hold either the
1827 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1828 * or the ability to modify system settings as determined by
1829 * {@link android.provider.Settings.System#canWrite}.</p>
1830 *
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001831 * @param networkType the type of the network over which traffic to the specified
1832 * host is to be routed
1833 * @param hostAddress the IP address of the host to which the route is desired
1834 * @return {@code true} on success, {@code false} on failure
1835 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001836 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001837 * {@link #bindProcessToNetwork} API.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001838 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001839 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001840 @UnsupportedAppUsage
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001841 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001842 checkLegacyRoutingApiAccess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 try {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001844 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001845 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001846 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001847 }
1848 }
1849
1850 /**
1851 * Returns the value of the setting for background data usage. If false,
1852 * applications should not use the network if the application is not in the
1853 * foreground. Developers should respect this setting, and check the value
1854 * of this before performing any background data operations.
1855 * <p>
1856 * All applications that have background services that use the network
1857 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001858 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001859 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001860 * background data depends on several combined factors, and this method will
1861 * always return {@code true}. Instead, when background data is unavailable,
1862 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001863 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001864 * @return Whether background data usage is allowed.
1865 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001866 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001867 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001868 // assume that background data is allowed; final authority is
1869 // NetworkInfo which may be blocked.
1870 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001871 }
1872
1873 /**
1874 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001875 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001876 * @param allowBackgroundData Whether an application should use data while
1877 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001878 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1880 * @see #getBackgroundDataSetting()
1881 * @hide
1882 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001883 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001884 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001885 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001886 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001887 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001888
Jeff Sharkey43d2a172017-07-12 10:50:42 -06001889 /** {@hide} */
1890 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001891 @UnsupportedAppUsage
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001892 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1893 try {
1894 return mService.getActiveNetworkQuotaInfo();
1895 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001896 throw e.rethrowFromSystemServer();
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001897 }
1898 }
1899
1900 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001901 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001902 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001903 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001904 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001905 @UnsupportedAppUsage
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001906 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001907 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1908 if (b != null) {
1909 try {
1910 ITelephony it = ITelephony.Stub.asInterface(b);
Shishir Agrawal7ea3e8b2016-01-25 13:03:07 -08001911 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Wink Saville36ffb042014-12-05 11:10:30 -08001912 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
Malcolm Chenb455e722017-11-28 15:57:14 -08001913 boolean retVal = it.isUserDataEnabled(subId);
Wink Saville36ffb042014-12-05 11:10:30 -08001914 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1915 + " retVal=" + retVal);
1916 return retVal;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001917 } catch (RemoteException e) {
1918 throw e.rethrowFromSystemServer();
1919 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001920 }
Wink Saville36ffb042014-12-05 11:10:30 -08001921 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001922 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001923 }
1924
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001925 /**
Robert Greenwaltb2489872014-09-04 16:44:35 -07001926 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt6078b502014-06-11 16:05:07 -07001927 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001928 */
1929 public interface OnNetworkActiveListener {
1930 /**
1931 * Called on the main thread of the process to report that the current data network
1932 * has become active, and it is now a good time to perform any pending network
1933 * operations. Note that this listener only tells you when the network becomes
1934 * active; if at any other time you want to know whether it is active (and thus okay
1935 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001936 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001937 */
Chalard Jean4d660112018-06-04 16:52:49 +09001938 void onNetworkActive();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001939 }
1940
1941 private INetworkManagementService getNetworkManagementService() {
1942 synchronized (this) {
1943 if (mNMService != null) {
1944 return mNMService;
1945 }
1946 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1947 mNMService = INetworkManagementService.Stub.asInterface(b);
1948 return mNMService;
1949 }
1950 }
1951
1952 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
Chalard Jean4d660112018-06-04 16:52:49 +09001953 mNetworkActivityListeners = new ArrayMap<>();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001954
1955 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001956 * Start listening to reports when the system's default data network is active, meaning it is
1957 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1958 * to determine the current state of the system's default network after registering the
1959 * listener.
1960 * <p>
1961 * If the process default network has been set with
Paul Jensen72db88e2015-03-10 10:54:12 -04001962 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt6078b502014-06-11 16:05:07 -07001963 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001964 *
1965 * @param l The listener to be told when the network is active.
1966 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001967 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001968 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1969 @Override
1970 public void onNetworkActive() throws RemoteException {
1971 l.onNetworkActive();
1972 }
1973 };
1974
1975 try {
1976 getNetworkManagementService().registerNetworkActivityListener(rl);
1977 mNetworkActivityListeners.put(l, rl);
1978 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001979 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001980 }
1981 }
1982
1983 /**
1984 * Remove network active listener previously registered with
Robert Greenwaltb2489872014-09-04 16:44:35 -07001985 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001986 *
1987 * @param l Previously registered listener.
1988 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001989 public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001990 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
Hugo Benichie7678512017-05-09 15:19:01 +09001991 Preconditions.checkArgument(rl != null, "Listener was not registered.");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001992 try {
1993 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1994 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001995 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001996 }
1997 }
1998
1999 /**
2000 * Return whether the data network is currently active. An active network means that
2001 * it is currently in a high power state for performing data transmission. On some
2002 * types of networks, it may be expensive to move and stay in such a state, so it is
2003 * more power efficient to batch network traffic together when the radio is already in
2004 * this state. This method tells you whether right now is currently a good time to
2005 * initiate network traffic, as the network is already active.
2006 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002007 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002008 try {
2009 return getNetworkManagementService().isNetworkActive();
2010 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002011 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002012 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002013 }
2014
2015 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002016 * {@hide}
2017 */
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09002018 public ConnectivityManager(Context context, IConnectivityManager service) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09002019 mContext = Preconditions.checkNotNull(context, "missing context");
2020 mService = Preconditions.checkNotNull(service, "missing IConnectivityManager");
Paul Jensene0bef712014-12-10 15:12:18 -05002021 sInstance = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002022 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002023
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07002024 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002025 @UnsupportedAppUsage
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07002026 public static ConnectivityManager from(Context context) {
2027 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
2028 }
2029
Lorenzo Colittifbe9b1a2016-07-28 17:14:11 +09002030 /* TODO: These permissions checks don't belong in client-side code. Move them to
2031 * services.jar, possibly in com.android.server.net. */
2032
2033 /** {@hide} */
Lorenzo Colittid5427052015-10-15 16:29:00 +09002034 public static final void enforceChangePermission(Context context) {
2035 int uid = Binder.getCallingUid();
2036 Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
2037 .getPackageNameForUid(context, uid), true /* throwException */);
2038 }
2039
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002040 /** {@hide} */
2041 public static final void enforceTetherChangePermission(Context context, String callingPkg) {
Hugo Benichie7678512017-05-09 15:19:01 +09002042 Preconditions.checkNotNull(context, "Context cannot be null");
2043 Preconditions.checkNotNull(callingPkg, "callingPkg cannot be null");
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002044
Robert Greenwaltedb47662014-09-16 17:54:19 -07002045 if (context.getResources().getStringArray(
2046 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
2047 // Have a provisioning app - must only let system apps (which check this app)
2048 // turn on tethering
2049 context.enforceCallingOrSelfPermission(
Jeremy Kleind42209d2015-12-28 15:11:58 -08002050 android.Manifest.permission.TETHER_PRIVILEGED, "ConnectivityService");
Robert Greenwaltedb47662014-09-16 17:54:19 -07002051 } else {
Billy Laua7238a32015-08-01 12:45:02 +01002052 int uid = Binder.getCallingUid();
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002053 // If callingPkg's uid is not same as Binder.getCallingUid(),
2054 // AppOpsService throws SecurityException.
2055 Settings.checkAndNoteWriteSettingsOperation(context, uid, callingPkg,
2056 true /* throwException */);
Robert Greenwaltedb47662014-09-16 17:54:19 -07002057 }
2058 }
2059
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002060 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002061 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2062 * situations where a Context pointer is unavailable.
2063 * @hide
2064 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002065 @Deprecated
Paul Jensen72db88e2015-03-10 10:54:12 -04002066 static ConnectivityManager getInstanceOrNull() {
2067 return sInstance;
2068 }
2069
2070 /**
2071 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2072 * situations where a Context pointer is unavailable.
2073 * @hide
2074 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002075 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002076 @UnsupportedAppUsage
Paul Jensen72db88e2015-03-10 10:54:12 -04002077 private static ConnectivityManager getInstance() {
2078 if (getInstanceOrNull() == null) {
Paul Jensene0bef712014-12-10 15:12:18 -05002079 throw new IllegalStateException("No ConnectivityManager yet constructed");
2080 }
Paul Jensen72db88e2015-03-10 10:54:12 -04002081 return getInstanceOrNull();
Paul Jensene0bef712014-12-10 15:12:18 -05002082 }
2083
2084 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002085 * Get the set of tetherable, available interfaces. This list is limited by
2086 * device configuration and current interface existence.
2087 *
2088 * @return an array of 0 or more Strings of tetherable interface names.
2089 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002090 * {@hide}
2091 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002092 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002093 @UnsupportedAppUsage
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002094 public String[] getTetherableIfaces() {
2095 try {
2096 return mService.getTetherableIfaces();
2097 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002098 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002099 }
2100 }
2101
2102 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002103 * Get the set of tethered interfaces.
2104 *
2105 * @return an array of 0 or more String of currently tethered interface names.
2106 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002107 * {@hide}
2108 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002109 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002110 @UnsupportedAppUsage
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002111 public String[] getTetheredIfaces() {
2112 try {
2113 return mService.getTetheredIfaces();
2114 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002115 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002116 }
2117 }
2118
2119 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002120 * Get the set of interface names which attempted to tether but
2121 * failed. Re-attempting to tether may cause them to reset to the Tethered
2122 * state. Alternatively, causing the interface to be destroyed and recreated
2123 * may cause them to reset to the available state.
2124 * {@link ConnectivityManager#getLastTetherError} can be used to get more
2125 * information on the cause of the errors.
2126 *
2127 * @return an array of 0 or more String indicating the interface names
2128 * which failed to tether.
2129 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002130 * {@hide}
2131 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002132 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002133 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002134 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002135 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08002136 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002137 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002138 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002139 }
2140 }
2141
2142 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07002143 * Get the set of tethered dhcp ranges.
2144 *
2145 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
2146 * {@hide}
2147 */
2148 public String[] getTetheredDhcpRanges() {
2149 try {
2150 return mService.getTetheredDhcpRanges();
2151 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002152 throw e.rethrowFromSystemServer();
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07002153 }
2154 }
2155
2156 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002157 * Attempt to tether the named interface. This will setup a dhcp server
2158 * on the interface, forward and NAT IP packets and forward DNS requests
2159 * to the best active upstream network interface. Note that if no upstream
2160 * IP network interface is available, dhcp will still run and traffic will be
2161 * allowed between the tethered devices and this device, though upstream net
2162 * access will of course fail until an upstream network interface becomes
2163 * active.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002164 *
2165 * <p>This method requires the caller to hold either the
2166 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2167 * or the ability to modify system settings as determined by
2168 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002169 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002170 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2171 * and WifiStateMachine which need direct access. All other clients should use
2172 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2173 * logic.</p>
2174 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002175 * @param iface the interface name to tether.
2176 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2177 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002178 * {@hide}
2179 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002180 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002181 public int tether(String iface) {
2182 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002183 String pkgName = mContext.getOpPackageName();
2184 Log.i(TAG, "tether caller:" + pkgName);
2185 return mService.tether(iface, pkgName);
Robert Greenwalt5a735062010-03-02 17:25:02 -08002186 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002187 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002188 }
2189 }
2190
2191 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002192 * Stop tethering the named interface.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002193 *
2194 * <p>This method requires the caller to hold either the
2195 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2196 * or the ability to modify system settings as determined by
2197 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002198 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002199 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2200 * and WifiStateMachine which need direct access. All other clients should use
2201 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2202 * logic.</p>
2203 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002204 * @param iface the interface name to untether.
2205 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2206 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002207 * {@hide}
2208 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002209 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002210 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002211 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002212 String pkgName = mContext.getOpPackageName();
2213 Log.i(TAG, "untether caller:" + pkgName);
2214 return mService.untether(iface, pkgName);
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002215 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002216 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002217 }
2218 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002219
2220 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002221 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002222 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002223 * due to device configuration.
2224 *
Chalard Jean8f76fc32017-09-26 15:45:18 +09002225 * <p>If this app does not have permission to use this API, it will always
2226 * return false rather than throw an exception.</p>
2227 *
2228 * <p>If the device has a hotspot provisioning app, the caller is required to hold the
2229 * {@link android.Manifest.permission.TETHER_PRIVILEGED} permission.</p>
2230 *
2231 * <p>Otherwise, this method requires the caller to hold the ability to modify system
2232 * settings as determined by {@link android.provider.Settings.System#canWrite}.</p>
2233 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002234 * @return a boolean - {@code true} indicating Tethering is supported.
2235 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002236 * {@hide}
2237 */
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002238 @SystemApi
Chalard Jean8f76fc32017-09-26 15:45:18 +09002239 @RequiresPermission(anyOf = {android.Manifest.permission.TETHER_PRIVILEGED,
2240 android.Manifest.permission.WRITE_SETTINGS})
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002241 public boolean isTetheringSupported() {
Chalard Jean8f76fc32017-09-26 15:45:18 +09002242 String pkgName = mContext.getOpPackageName();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002243 try {
Udam Saini0e94c362017-06-07 12:06:28 -07002244 return mService.isTetheringSupported(pkgName);
Chalard Jean8f76fc32017-09-26 15:45:18 +09002245 } catch (SecurityException e) {
2246 // This API is not available to this caller, but for backward-compatibility
2247 // this will just return false instead of throwing.
2248 return false;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002249 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002250 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002251 }
2252 }
2253
2254 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002255 * Callback for use with {@link #startTethering} to find out whether tethering succeeded.
2256 * @hide
2257 */
2258 @SystemApi
2259 public static abstract class OnStartTetheringCallback {
2260 /**
2261 * Called when tethering has been successfully started.
2262 */
Chalard Jean4d660112018-06-04 16:52:49 +09002263 public void onTetheringStarted() {}
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002264
2265 /**
2266 * Called when starting tethering failed.
2267 */
Chalard Jean4d660112018-06-04 16:52:49 +09002268 public void onTetheringFailed() {}
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002269 }
2270
2271 /**
2272 * Convenient overload for
2273 * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null
2274 * handler to run on the current thread's {@link Looper}.
2275 * @hide
2276 */
2277 @SystemApi
Udam Saini0e94c362017-06-07 12:06:28 -07002278 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002279 public void startTethering(int type, boolean showProvisioningUi,
2280 final OnStartTetheringCallback callback) {
2281 startTethering(type, showProvisioningUi, callback, null);
2282 }
2283
2284 /**
2285 * Runs tether provisioning for the given type if needed and then starts tethering if
2286 * the check succeeds. If no carrier provisioning is required for tethering, tethering is
2287 * enabled immediately. If provisioning fails, tethering will not be enabled. It also
2288 * schedules tether provisioning re-checks if appropriate.
2289 *
2290 * @param type The type of tethering to start. Must be one of
2291 * {@link ConnectivityManager.TETHERING_WIFI},
2292 * {@link ConnectivityManager.TETHERING_USB}, or
2293 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2294 * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there
2295 * is one. This should be true the first time this function is called and also any time
2296 * the user can see this UI. It gives users information from their carrier about the
2297 * check failing and how they can sign up for tethering if possible.
2298 * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller
2299 * of the result of trying to tether.
2300 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
2301 * @hide
2302 */
2303 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002304 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002305 public void startTethering(int type, boolean showProvisioningUi,
2306 final OnStartTetheringCallback callback, Handler handler) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09002307 Preconditions.checkNotNull(callback, "OnStartTetheringCallback cannot be null.");
Jeremy Klein5f277e12016-03-12 16:29:54 -08002308
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002309 ResultReceiver wrappedCallback = new ResultReceiver(handler) {
2310 @Override
2311 protected void onReceiveResult(int resultCode, Bundle resultData) {
2312 if (resultCode == TETHER_ERROR_NO_ERROR) {
2313 callback.onTetheringStarted();
2314 } else {
2315 callback.onTetheringFailed();
2316 }
2317 }
2318 };
Jeremy Klein5f277e12016-03-12 16:29:54 -08002319
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002320 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002321 String pkgName = mContext.getOpPackageName();
2322 Log.i(TAG, "startTethering caller:" + pkgName);
2323 mService.startTethering(type, wrappedCallback, showProvisioningUi, pkgName);
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002324 } catch (RemoteException e) {
2325 Log.e(TAG, "Exception trying to start tethering.", e);
2326 wrappedCallback.send(TETHER_ERROR_SERVICE_UNAVAIL, null);
2327 }
2328 }
2329
2330 /**
2331 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if
2332 * applicable.
2333 *
2334 * @param type The type of tethering to stop. Must be one of
2335 * {@link ConnectivityManager.TETHERING_WIFI},
2336 * {@link ConnectivityManager.TETHERING_USB}, or
2337 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2338 * @hide
2339 */
2340 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002341 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002342 public void stopTethering(int type) {
2343 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002344 String pkgName = mContext.getOpPackageName();
2345 Log.i(TAG, "stopTethering caller:" + pkgName);
2346 mService.stopTethering(type, pkgName);
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002347 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002348 throw e.rethrowFromSystemServer();
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002349 }
2350 }
2351
2352 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002353 * Get the list of regular expressions that define any tetherable
2354 * USB network interfaces. If USB tethering is not supported by the
2355 * device, this list should be empty.
2356 *
2357 * @return an array of 0 or more regular expression Strings defining
2358 * what interfaces are considered tetherable usb interfaces.
2359 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002360 * {@hide}
2361 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002362 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002363 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002364 public String[] getTetherableUsbRegexs() {
2365 try {
2366 return mService.getTetherableUsbRegexs();
2367 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002368 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002369 }
2370 }
2371
2372 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002373 * Get the list of regular expressions that define any tetherable
2374 * Wifi network interfaces. If Wifi tethering is not supported by the
2375 * device, this list should be empty.
2376 *
2377 * @return an array of 0 or more regular expression Strings defining
2378 * what interfaces are considered tetherable wifi interfaces.
2379 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002380 * {@hide}
2381 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002382 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002383 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002384 public String[] getTetherableWifiRegexs() {
2385 try {
2386 return mService.getTetherableWifiRegexs();
2387 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002388 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002389 }
2390 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08002391
Danica Chang6fdd0c62010-08-11 14:54:43 -07002392 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002393 * Get the list of regular expressions that define any tetherable
2394 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
2395 * device, this list should be empty.
2396 *
2397 * @return an array of 0 or more regular expression Strings defining
2398 * what interfaces are considered tetherable bluetooth interfaces.
2399 *
Danica Chang6fdd0c62010-08-11 14:54:43 -07002400 * {@hide}
2401 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002402 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002403 @UnsupportedAppUsage
Danica Chang6fdd0c62010-08-11 14:54:43 -07002404 public String[] getTetherableBluetoothRegexs() {
2405 try {
2406 return mService.getTetherableBluetoothRegexs();
2407 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002408 throw e.rethrowFromSystemServer();
Danica Chang6fdd0c62010-08-11 14:54:43 -07002409 }
2410 }
2411
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002412 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002413 * Attempt to both alter the mode of USB and Tethering of USB. A
2414 * utility method to deal with some of the complexity of USB - will
2415 * attempt to switch to Rndis and subsequently tether the resulting
2416 * interface on {@code true} or turn off tethering and switch off
2417 * Rndis on {@code false}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002418 *
2419 * <p>This method requires the caller to hold either the
2420 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2421 * or the ability to modify system settings as determined by
2422 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002423 *
2424 * @param enable a boolean - {@code true} to enable tethering
2425 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2426 *
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002427 * {@hide}
2428 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002429 @UnsupportedAppUsage
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002430 public int setUsbTethering(boolean enable) {
2431 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002432 String pkgName = mContext.getOpPackageName();
2433 Log.i(TAG, "setUsbTethering caller:" + pkgName);
2434 return mService.setUsbTethering(enable, pkgName);
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002435 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002436 throw e.rethrowFromSystemServer();
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002437 }
2438 }
2439
Robert Greenwalt5a735062010-03-02 17:25:02 -08002440 /** {@hide} */
2441 public static final int TETHER_ERROR_NO_ERROR = 0;
2442 /** {@hide} */
2443 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
2444 /** {@hide} */
2445 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
2446 /** {@hide} */
2447 public static final int TETHER_ERROR_UNSUPPORTED = 3;
2448 /** {@hide} */
2449 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
2450 /** {@hide} */
2451 public static final int TETHER_ERROR_MASTER_ERROR = 5;
2452 /** {@hide} */
2453 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
2454 /** {@hide} */
2455 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
2456 /** {@hide} */
2457 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
2458 /** {@hide} */
2459 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
2460 /** {@hide} */
2461 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002462 /** {@hide} */
2463 public static final int TETHER_ERROR_PROVISION_FAILED = 11;
Robert Greenwalt5a735062010-03-02 17:25:02 -08002464
2465 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002466 * Get a more detailed error code after a Tethering or Untethering
2467 * request asynchronously failed.
2468 *
2469 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08002470 * @return error The error code of the last error tethering or untethering the named
2471 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002472 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002473 * {@hide}
2474 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002475 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002476 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002477 public int getLastTetherError(String iface) {
2478 try {
2479 return mService.getLastTetherError(iface);
2480 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002481 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002482 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002483 }
2484
2485 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002486 * Report network connectivity status. This is currently used only
2487 * to alter status bar UI.
Paul Jensenb2748922015-05-06 11:10:18 -04002488 * <p>This method requires the caller to hold the permission
2489 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002490 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002491 * @param networkType The type of network you want to report on
2492 * @param percentage The quality of the connection 0 is bad, 100 is good
Chalard Jean6b1da6e2018-03-08 13:54:53 +09002493 * @deprecated Types are deprecated. Use {@link #reportNetworkConnectivity} instead.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002494 * {@hide}
2495 */
2496 public void reportInetCondition(int networkType, int percentage) {
2497 try {
2498 mService.reportInetCondition(networkType, percentage);
2499 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002500 throw e.rethrowFromSystemServer();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002501 }
2502 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002503
2504 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002505 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07002506 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002507 * the framework to re-evaluate network connectivity and/or switch to another
2508 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002509 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002510 * @param network The {@link Network} the application was attempting to use
2511 * or {@code null} to indicate the current default network.
Paul Jensenbfd17b72015-04-07 12:43:13 -04002512 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
2513 * working and non-working connectivity.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002514 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002515 @Deprecated
Robert Greenwalt9258c642014-03-26 16:47:06 -07002516 public void reportBadNetwork(Network network) {
2517 try {
Paul Jensenbfd17b72015-04-07 12:43:13 -04002518 // One of these will be ignored because it matches system's current state.
2519 // The other will trigger the necessary reevaluation.
2520 mService.reportNetworkConnectivity(network, true);
2521 mService.reportNetworkConnectivity(network, false);
2522 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002523 throw e.rethrowFromSystemServer();
Paul Jensenbfd17b72015-04-07 12:43:13 -04002524 }
2525 }
2526
2527 /**
2528 * Report to the framework whether a network has working connectivity.
2529 * This provides a hint to the system that a particular network is providing
2530 * working connectivity or not. In response the framework may re-evaluate
2531 * the network's connectivity and might take further action thereafter.
2532 *
2533 * @param network The {@link Network} the application was attempting to use
2534 * or {@code null} to indicate the current default network.
2535 * @param hasConnectivity {@code true} if the application was able to successfully access the
2536 * Internet using {@code network} or {@code false} if not.
2537 */
2538 public void reportNetworkConnectivity(Network network, boolean hasConnectivity) {
2539 try {
2540 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002541 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002542 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002543 }
2544 }
2545
2546 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002547 * Set a network-independent global http proxy. This is not normally what you want
2548 * for typical HTTP proxies - they are general network dependent. However if you're
2549 * doing something unusual like general internal filtering this may be useful. On
2550 * a private network where the proxy is not accessible, you may break HTTP using this.
Paul Jensenb2748922015-05-06 11:10:18 -04002551 *
2552 * @param p A {@link ProxyInfo} object defining the new global
2553 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002554 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002555 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002556 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Jason Monk207900c2014-04-25 15:00:09 -04002557 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002558 try {
2559 mService.setGlobalProxy(p);
2560 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002561 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002562 }
2563 }
2564
2565 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002566 * Retrieve any network-independent global HTTP proxy.
2567 *
Jason Monk207900c2014-04-25 15:00:09 -04002568 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002569 * if no global HTTP proxy is set.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002570 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002571 */
Jason Monk207900c2014-04-25 15:00:09 -04002572 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002573 try {
2574 return mService.getGlobalProxy();
2575 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002576 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002577 }
2578 }
2579
2580 /**
Paul Jensencee9b512015-05-06 07:32:40 -04002581 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
2582 * network-specific HTTP proxy. If {@code network} is null, the
2583 * network-specific proxy returned is the proxy of the default active
2584 * network.
2585 *
2586 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
2587 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
2588 * or when {@code network} is {@code null},
2589 * the {@code ProxyInfo} for the default active network. Returns
2590 * {@code null} when no proxy applies or the caller doesn't have
2591 * permission to use {@code network}.
2592 * @hide
2593 */
2594 public ProxyInfo getProxyForNetwork(Network network) {
2595 try {
2596 return mService.getProxyForNetwork(network);
2597 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002598 throw e.rethrowFromSystemServer();
Paul Jensencee9b512015-05-06 07:32:40 -04002599 }
2600 }
2601
2602 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002603 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
2604 * otherwise if this process is bound to a {@link Network} using
Paul Jensen72db88e2015-03-10 10:54:12 -04002605 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensene0bef712014-12-10 15:12:18 -05002606 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002607 *
Jason Monk207900c2014-04-25 15:00:09 -04002608 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002609 * HTTP proxy is active.
Robert Greenwalt434203a2010-10-11 16:00:27 -07002610 */
Paul Jensene0bef712014-12-10 15:12:18 -05002611 public ProxyInfo getDefaultProxy() {
Paul Jensencee9b512015-05-06 07:32:40 -04002612 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwalt434203a2010-10-11 16:00:27 -07002613 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07002614
2615 /**
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002616 * Returns true if the hardware supports the given network type
2617 * else it returns false. This doesn't indicate we have coverage
2618 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002619 * hardware supports it. For example a GSM phone without a SIM
2620 * should still return {@code true} for mobile data, but a wifi only
2621 * tablet would return {@code false}.
2622 *
2623 * @param networkType The network type we'd like to check
2624 * @return {@code true} if supported, else {@code false}
Chalard Jean6b1da6e2018-03-08 13:54:53 +09002625 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002626 * @hide
2627 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +09002628 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002629 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002630 @UnsupportedAppUsage
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002631 public boolean isNetworkSupported(int networkType) {
2632 try {
2633 return mService.isNetworkSupported(networkType);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002634 } catch (RemoteException e) {
2635 throw e.rethrowFromSystemServer();
2636 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002637 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002638
2639 /**
2640 * Returns if the currently active data network is metered. A network is
2641 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002642 * that connection due to monetary costs, data limitations or
2643 * battery/performance issues. You should check this before doing large
2644 * data transfers, and warn the user or delay the operation until another
2645 * network is available.
2646 *
2647 * @return {@code true} if large transfers should be avoided, otherwise
2648 * {@code false}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002649 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002650 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002651 public boolean isActiveNetworkMetered() {
2652 try {
2653 return mService.isActiveNetworkMetered();
2654 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002655 throw e.rethrowFromSystemServer();
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002656 }
2657 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002658
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002659 /**
2660 * If the LockdownVpn mechanism is enabled, updates the vpn
2661 * with a reload of its profile.
2662 *
2663 * @return a boolean with {@code} indicating success
2664 *
2665 * <p>This method can only be called by the system UID
2666 * {@hide}
2667 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002668 public boolean updateLockdownVpn() {
2669 try {
2670 return mService.updateLockdownVpn();
2671 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002672 throw e.rethrowFromSystemServer();
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002673 }
2674 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07002675
2676 /**
Wink Saville948282b2013-08-29 08:55:16 -07002677 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07002678 *
Wink Savilleab9321d2013-06-29 21:10:57 -07002679 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07002680 *
2681 * @return time out that will be used, maybe less that suggestedTimeOutMs
2682 * -1 if an error.
2683 *
2684 * {@hide}
2685 */
Wink Saville948282b2013-08-29 08:55:16 -07002686 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07002687 int timeOutMs = -1;
2688 try {
Wink Saville948282b2013-08-29 08:55:16 -07002689 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07002690 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002691 throw e.rethrowFromSystemServer();
Wink Savilleab9321d2013-06-29 21:10:57 -07002692 }
2693 return timeOutMs;
2694 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002695
2696 /**
Wink Saville42d4f082013-07-20 20:31:59 -07002697 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002698 * {@hide}
2699 */
2700 public String getMobileProvisioningUrl() {
2701 try {
2702 return mService.getMobileProvisioningUrl();
2703 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002704 throw e.rethrowFromSystemServer();
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002705 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002706 }
Wink Saville42d4f082013-07-20 20:31:59 -07002707
2708 /**
Wink Saville948282b2013-08-29 08:55:16 -07002709 * Set sign in error notification to visible or in visible
2710 *
Wink Saville948282b2013-08-29 08:55:16 -07002711 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04002712 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville948282b2013-08-29 08:55:16 -07002713 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002714 @Deprecated
Wink Saville948282b2013-08-29 08:55:16 -07002715 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04002716 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07002717 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04002718 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07002719 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002720 throw e.rethrowFromSystemServer();
Wink Saville948282b2013-08-29 08:55:16 -07002721 }
2722 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002723
2724 /**
2725 * Set the value for enabling/disabling airplane mode
2726 *
2727 * @param enable whether to enable airplane mode or not
2728 *
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002729 * @hide
2730 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002731 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002732 @UnsupportedAppUsage
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002733 public void setAirplaneMode(boolean enable) {
2734 try {
2735 mService.setAirplaneMode(enable);
2736 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002737 throw e.rethrowFromSystemServer();
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002738 }
2739 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002740
2741 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002742 @UnsupportedAppUsage
Robert Greenwalta67be032014-05-16 15:49:14 -07002743 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07002744 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07002745 mService.registerNetworkFactory(messenger, name);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002746 } catch (RemoteException e) {
2747 throw e.rethrowFromSystemServer();
2748 }
Robert Greenwalta67be032014-05-16 15:49:14 -07002749 }
2750
2751 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002752 @UnsupportedAppUsage
Robert Greenwalta67be032014-05-16 15:49:14 -07002753 public void unregisterNetworkFactory(Messenger messenger) {
2754 try {
2755 mService.unregisterNetworkFactory(messenger);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002756 } catch (RemoteException e) {
2757 throw e.rethrowFromSystemServer();
2758 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002759 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002760
Paul Jensen31a94f42015-02-13 14:18:39 -05002761 /**
2762 * @hide
2763 * Register a NetworkAgent with ConnectivityService.
2764 * @return NetID corresponding to NetworkAgent.
2765 */
2766 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002767 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002768 try {
Paul Jensen31a94f42015-02-13 14:18:39 -05002769 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
2770 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002771 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -05002772 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002773 }
2774
Robert Greenwalt9258c642014-03-26 16:47:06 -07002775 /**
Hugo Benichidafed3d2017-03-06 09:17:06 +09002776 * Base class for {@code NetworkRequest} callbacks. Used for notifications about network
2777 * changes. Should be extended by applications wanting notifications.
2778 *
2779 * A {@code NetworkCallback} is registered by calling
2780 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
2781 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)},
Hugo Benichica867dc2018-02-07 21:17:43 +09002782 * or {@link #registerDefaultNetworkCallback(NetworkCallback)}. A {@code NetworkCallback} is
Hugo Benichidafed3d2017-03-06 09:17:06 +09002783 * unregistered by calling {@link #unregisterNetworkCallback(NetworkCallback)}.
2784 * A {@code NetworkCallback} should be registered at most once at any time.
2785 * A {@code NetworkCallback} that has been unregistered can be registered again.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002786 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002787 public static class NetworkCallback {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002788 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002789 * Called when the framework connects to a new network to evaluate whether it satisfies this
2790 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
2791 * callback. There is no guarantee that this new network will satisfy any requests, or that
2792 * the network will stay connected for longer than the time necessary to evaluate it.
2793 * <p>
2794 * Most applications <b>should not</b> act on this callback, and should instead use
2795 * {@link #onAvailable}. This callback is intended for use by applications that can assist
2796 * the framework in properly evaluating the network &mdash; for example, an application that
2797 * can automatically log in to a captive portal without user intervention.
2798 *
2799 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti66276122015-06-11 14:27:17 +09002800 *
2801 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002802 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002803 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002804
2805 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002806 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002807 * This callback may be called more than once if the {@link Network} that is
2808 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002809 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002810 * @param network The {@link Network} of the satisfying network.
Chalard Jean804b8fb2018-01-30 22:41:41 +09002811 * @param networkCapabilities The {@link NetworkCapabilities} of the satisfying network.
2812 * @param linkProperties The {@link LinkProperties} of the satisfying network.
2813 * @hide
2814 */
2815 public void onAvailable(Network network, NetworkCapabilities networkCapabilities,
2816 LinkProperties linkProperties) {
2817 // Internally only this method is called when a new network is available, and
2818 // it calls the callback in the same way and order that older versions used
2819 // to call so as not to change the behavior.
2820 onAvailable(network);
2821 if (!networkCapabilities.hasCapability(
2822 NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)) {
2823 onNetworkSuspended(network);
2824 }
2825 onCapabilitiesChanged(network, networkCapabilities);
2826 onLinkPropertiesChanged(network, linkProperties);
2827 }
2828
2829 /**
2830 * Called when the framework connects and has declared a new network ready for use.
2831 * This callback may be called more than once if the {@link Network} that is
2832 * satisfying the request changes. This will always immediately be followed by a
2833 * call to {@link #onCapabilitiesChanged(Network, NetworkCapabilities)} then by a
2834 * call to {@link #onLinkPropertiesChanged(Network, LinkProperties)}.
2835 *
2836 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002837 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002838 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002839
2840 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002841 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002842 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002843 * for graceful handover. This may not be called if we have a hard loss
2844 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002845 * {@link NetworkCallback#onLost} call or a
2846 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002847 * on whether we lose or regain it.
2848 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002849 * @param network The {@link Network} that is about to be disconnected.
2850 * @param maxMsToLive The time in ms the framework will attempt to keep the
2851 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002852 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002853 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002854 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002855
2856 /**
2857 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002858 * graceful failure ends.
2859 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002860 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002861 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002862 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002863
2864 /**
Etan Cohenaebf17e2017-03-01 12:47:28 -08002865 * Called if no network is found in the timeout time specified in
Hugo Benichi0eec03f2017-05-15 15:15:33 +09002866 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} call. This callback is not
Etan Cohenaebf17e2017-03-01 12:47:28 -08002867 * called for the version of {@link #requestNetwork(NetworkRequest, NetworkCallback)}
2868 * without timeout. When this callback is invoked the associated
2869 * {@link NetworkRequest} will have already been removed and released, as if
2870 * {@link #unregisterNetworkCallback(NetworkCallback)} had been called.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002871 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002872 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002873
2874 /**
2875 * Called when the network the framework connected to for this request
2876 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002877 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002878 * @param network The {@link Network} whose capabilities have changed.
Chalard Jean804b8fb2018-01-30 22:41:41 +09002879 * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this
2880 * network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002881 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002882 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002883 NetworkCapabilities networkCapabilities) {}
2884
2885 /**
2886 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002887 * changes {@link LinkProperties}.
2888 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002889 * @param network The {@link Network} whose link properties have changed.
2890 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002891 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002892 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002893
Robert Greenwalt8d482522015-06-24 13:23:42 -07002894 /**
2895 * Called when the network the framework connected to for this request
Chalard Jean804b8fb2018-01-30 22:41:41 +09002896 * goes into {@link NetworkInfo.State#SUSPENDED}.
Robert Greenwalt8d482522015-06-24 13:23:42 -07002897 * This generally means that while the TCP connections are still live,
2898 * temporarily network data fails to transfer. Specifically this is used
2899 * on cellular networks to mask temporary outages when driving through
2900 * a tunnel, etc.
2901 * @hide
2902 */
2903 public void onNetworkSuspended(Network network) {}
2904
2905 /**
2906 * Called when the network the framework connected to for this request
Chalard Jean804b8fb2018-01-30 22:41:41 +09002907 * returns from a {@link NetworkInfo.State#SUSPENDED} state. This should always be
2908 * preceded by a matching {@link NetworkCallback#onNetworkSuspended} call.
Robert Greenwalt8d482522015-06-24 13:23:42 -07002909 * @hide
2910 */
2911 public void onNetworkResumed(Network network) {}
2912
Robert Greenwalt6078b502014-06-11 16:05:07 -07002913 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002914 }
2915
Hugo Benichicb883232017-05-11 13:16:17 +09002916 /**
2917 * Constant error codes used by ConnectivityService to communicate about failures and errors
2918 * across a Binder boundary.
2919 * @hide
2920 */
2921 public interface Errors {
Chalard Jean4d660112018-06-04 16:52:49 +09002922 int TOO_MANY_REQUESTS = 1;
Hugo Benichicb883232017-05-11 13:16:17 +09002923 }
2924
2925 /** @hide */
2926 public static class TooManyRequestsException extends RuntimeException {}
2927
2928 private static RuntimeException convertServiceException(ServiceSpecificException e) {
2929 switch (e.errorCode) {
2930 case Errors.TOO_MANY_REQUESTS:
2931 return new TooManyRequestsException();
2932 default:
2933 Log.w(TAG, "Unknown service error code " + e.errorCode);
2934 return new RuntimeException(e);
2935 }
2936 }
2937
Robert Greenwalt9258c642014-03-26 16:47:06 -07002938 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002939 /** @hide */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002940 public static final int CALLBACK_PRECHECK = BASE + 1;
2941 /** @hide */
2942 public static final int CALLBACK_AVAILABLE = BASE + 2;
2943 /** @hide arg1 = TTL */
2944 public static final int CALLBACK_LOSING = BASE + 3;
2945 /** @hide */
2946 public static final int CALLBACK_LOST = BASE + 4;
2947 /** @hide */
2948 public static final int CALLBACK_UNAVAIL = BASE + 5;
2949 /** @hide */
2950 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2951 /** @hide */
2952 public static final int CALLBACK_IP_CHANGED = BASE + 7;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002953 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Hugo Benichidba33db2017-03-23 22:40:44 +09002954 private static final int EXPIRE_LEGACY_REQUEST = BASE + 8;
Robert Greenwalt8d482522015-06-24 13:23:42 -07002955 /** @hide */
Hugo Benichidba33db2017-03-23 22:40:44 +09002956 public static final int CALLBACK_SUSPENDED = BASE + 9;
Robert Greenwalt8d482522015-06-24 13:23:42 -07002957 /** @hide */
Hugo Benichidba33db2017-03-23 22:40:44 +09002958 public static final int CALLBACK_RESUMED = BASE + 10;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002959
Erik Kline57faba92015-11-25 12:49:38 +09002960 /** @hide */
2961 public static String getCallbackName(int whichCallback) {
2962 switch (whichCallback) {
2963 case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK";
2964 case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE";
2965 case CALLBACK_LOSING: return "CALLBACK_LOSING";
2966 case CALLBACK_LOST: return "CALLBACK_LOST";
2967 case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL";
2968 case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED";
2969 case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED";
Erik Kline57faba92015-11-25 12:49:38 +09002970 case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST";
2971 case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED";
2972 case CALLBACK_RESUMED: return "CALLBACK_RESUMED";
2973 default:
2974 return Integer.toString(whichCallback);
2975 }
2976 }
2977
Robert Greenwalt562cc542014-05-15 18:07:26 -07002978 private class CallbackHandler extends Handler {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002979 private static final String TAG = "ConnectivityManager.CallbackHandler";
Robert Greenwalta9ebeef2015-09-03 16:41:45 -07002980 private static final boolean DBG = false;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002981
Hugo Benichid42650f2016-07-06 22:53:17 +09002982 CallbackHandler(Looper looper) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002983 super(looper);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002984 }
2985
Hugo Benichi2583ef02017-02-02 17:02:36 +09002986 CallbackHandler(Handler handler) {
Hugo Benichie7678512017-05-09 15:19:01 +09002987 this(Preconditions.checkNotNull(handler, "Handler cannot be null.").getLooper());
Hugo Benichi2583ef02017-02-02 17:02:36 +09002988 }
2989
Robert Greenwalt9258c642014-03-26 16:47:06 -07002990 @Override
2991 public void handleMessage(Message message) {
Hugo Benichi2c684522017-05-09 14:36:02 +09002992 if (message.what == EXPIRE_LEGACY_REQUEST) {
2993 expireRequest((NetworkCapabilities) message.obj, message.arg1);
2994 return;
2995 }
2996
2997 final NetworkRequest request = getObject(message, NetworkRequest.class);
2998 final Network network = getObject(message, Network.class);
2999 final NetworkCallback callback;
3000 synchronized (sCallbacks) {
3001 callback = sCallbacks.get(request);
3002 }
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09003003 if (DBG) {
Hugo Benichia0385682017-03-22 17:07:57 +09003004 Log.d(TAG, getCallbackName(message.what) + " for network " + network);
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09003005 }
Hugo Benichi2c684522017-05-09 14:36:02 +09003006 if (callback == null) {
3007 Log.w(TAG, "callback not found for " + getCallbackName(message.what) + " message");
3008 return;
3009 }
3010
Robert Greenwalt9258c642014-03-26 16:47:06 -07003011 switch (message.what) {
3012 case CALLBACK_PRECHECK: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003013 callback.onPreCheck(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003014 break;
3015 }
3016 case CALLBACK_AVAILABLE: {
Chalard Jean804b8fb2018-01-30 22:41:41 +09003017 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
3018 LinkProperties lp = getObject(message, LinkProperties.class);
3019 callback.onAvailable(network, cap, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003020 break;
3021 }
3022 case CALLBACK_LOSING: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003023 callback.onLosing(network, message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003024 break;
3025 }
3026 case CALLBACK_LOST: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003027 callback.onLost(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003028 break;
3029 }
3030 case CALLBACK_UNAVAIL: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003031 callback.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07003032 break;
3033 }
3034 case CALLBACK_CAP_CHANGED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003035 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
3036 callback.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003037 break;
3038 }
3039 case CALLBACK_IP_CHANGED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003040 LinkProperties lp = getObject(message, LinkProperties.class);
3041 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003042 break;
3043 }
Robert Greenwalt8d482522015-06-24 13:23:42 -07003044 case CALLBACK_SUSPENDED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003045 callback.onNetworkSuspended(network);
Robert Greenwalt8d482522015-06-24 13:23:42 -07003046 break;
3047 }
3048 case CALLBACK_RESUMED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003049 callback.onNetworkResumed(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07003050 break;
3051 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003052 }
3053 }
3054
Hugo Benichid42650f2016-07-06 22:53:17 +09003055 private <T> T getObject(Message msg, Class<T> c) {
3056 return (T) msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07003057 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003058 }
3059
Hugo Benichi2583ef02017-02-02 17:02:36 +09003060 private CallbackHandler getDefaultHandler() {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003061 synchronized (sCallbacks) {
3062 if (sCallbackHandler == null) {
3063 sCallbackHandler = new CallbackHandler(ConnectivityThread.getInstanceLooper());
Robert Greenwalt9258c642014-03-26 16:47:06 -07003064 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003065 return sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003066 }
3067 }
3068
Hugo Benichi6f260f32017-02-03 14:18:44 +09003069 private static final HashMap<NetworkRequest, NetworkCallback> sCallbacks = new HashMap<>();
3070 private static CallbackHandler sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003071
Hugo Benichi6f260f32017-02-03 14:18:44 +09003072 private static final int LISTEN = 1;
3073 private static final int REQUEST = 2;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003074
Hugo Benichi6f260f32017-02-03 14:18:44 +09003075 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, NetworkCallback callback,
3076 int timeoutMs, int action, int legacyType, CallbackHandler handler) {
Hugo Benichie7678512017-05-09 15:19:01 +09003077 checkCallbackNotNull(callback);
Hugo Benichidafed3d2017-03-06 09:17:06 +09003078 Preconditions.checkArgument(action == REQUEST || need != null, "null NetworkCapabilities");
Hugo Benichid42650f2016-07-06 22:53:17 +09003079 final NetworkRequest request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003080 try {
Hugo Benichid42650f2016-07-06 22:53:17 +09003081 synchronized(sCallbacks) {
Hugo Benichi31c176d2017-06-17 13:14:12 +09003082 if (callback.networkRequest != null
3083 && callback.networkRequest != ALREADY_UNREGISTERED) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09003084 // TODO: throw exception instead and enforce 1:1 mapping of callbacks
3085 // and requests (http://b/20701525).
3086 Log.e(TAG, "NetworkCallback was already registered");
3087 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003088 Messenger messenger = new Messenger(handler);
Hugo Benichid42650f2016-07-06 22:53:17 +09003089 Binder binder = new Binder();
Paul Jensen7221cc32014-06-27 11:05:32 -04003090 if (action == LISTEN) {
Hugo Benichid42650f2016-07-06 22:53:17 +09003091 request = mService.listenForNetwork(need, messenger, binder);
Paul Jensen7221cc32014-06-27 11:05:32 -04003092 } else {
Hugo Benichid42650f2016-07-06 22:53:17 +09003093 request = mService.requestNetwork(
3094 need, messenger, timeoutMs, binder, legacyType);
Paul Jensen7221cc32014-06-27 11:05:32 -04003095 }
Hugo Benichid42650f2016-07-06 22:53:17 +09003096 if (request != null) {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003097 sCallbacks.put(request, callback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003098 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003099 callback.networkRequest = request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003100 }
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003101 } catch (RemoteException e) {
3102 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003103 } catch (ServiceSpecificException e) {
3104 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003105 }
Hugo Benichid42650f2016-07-06 22:53:17 +09003106 return request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003107 }
3108
3109 /**
Erik Klinea2d29402016-03-16 15:31:39 +09003110 * Helper function to request a network with a particular legacy type.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09003111 *
3112 * This is temporarily public @hide so it can be called by system code that uses the
3113 * NetworkRequest API to request networks but relies on CONNECTIVITY_ACTION broadcasts for
3114 * instead network notifications.
3115 *
3116 * TODO: update said system code to rely on NetworkCallbacks and make this method private.
3117 *
3118 * @hide
3119 */
Lorenzo Colittid1179462015-11-25 15:47:14 +09003120 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
Hugo Benichi2583ef02017-02-02 17:02:36 +09003121 int timeoutMs, int legacyType, Handler handler) {
3122 CallbackHandler cbHandler = new CallbackHandler(handler);
3123 NetworkCapabilities nc = request.networkCapabilities;
3124 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, legacyType, cbHandler);
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09003125 }
3126
3127 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09003128 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003129 *
3130 * This {@link NetworkRequest} will live until released via
Etan Cohenaebf17e2017-03-01 12:47:28 -08003131 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A
3132 * version of the method which takes a timeout is
Hugo Benichi0eec03f2017-05-15 15:15:33 +09003133 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003134 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07003135 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003136 * can be used to direct traffic to the network.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003137 * <p>It is presently unsupported to request a network with mutable
3138 * {@link NetworkCapabilities} such as
3139 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3140 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3141 * as these {@code NetworkCapabilities} represent states that a particular
3142 * network may never attain, and whether a network will attain these states
3143 * is unknown prior to bringing up the network so the framework does not
3144 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003145 *
3146 * <p>This method requires the caller to hold either the
3147 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3148 * or the ability to modify system settings as determined by
3149 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt9258c642014-03-26 16:47:06 -07003150 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003151 * @param request {@link NetworkRequest} describing this request.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003152 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3153 * the callback must not be shared - it uniquely specifies this request.
3154 * The callback is invoked on the default internal Handler.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003155 * @throws IllegalArgumentException if {@code request} specifies any mutable
3156 * {@code NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003157 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003158 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003159 requestNetwork(request, networkCallback, getDefaultHandler());
3160 }
3161
3162 /**
3163 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
3164 *
3165 * This {@link NetworkRequest} will live until released via
Etan Cohenaebf17e2017-03-01 12:47:28 -08003166 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A
3167 * version of the method which takes a timeout is
Hugo Benichi0eec03f2017-05-15 15:15:33 +09003168 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003169 * Status of the request can be followed by listening to the various
3170 * callbacks described in {@link NetworkCallback}. The {@link Network}
3171 * can be used to direct traffic to the network.
3172 * <p>It is presently unsupported to request a network with mutable
3173 * {@link NetworkCapabilities} such as
3174 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3175 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3176 * as these {@code NetworkCapabilities} represent states that a particular
3177 * network may never attain, and whether a network will attain these states
3178 * is unknown prior to bringing up the network so the framework does not
Chalard Jean4d660112018-06-04 16:52:49 +09003179 * know how to go about satisfying a request with these capabilities.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003180 *
3181 * <p>This method requires the caller to hold either the
3182 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3183 * or the ability to modify system settings as determined by
3184 * {@link android.provider.Settings.System#canWrite}.</p>
3185 *
3186 * @param request {@link NetworkRequest} describing this request.
3187 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3188 * the callback must not be shared - it uniquely specifies this request.
3189 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
3190 * @throws IllegalArgumentException if {@code request} specifies any mutable
3191 * {@code NetworkCapabilities}.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003192 */
3193 public void requestNetwork(
3194 NetworkRequest request, NetworkCallback networkCallback, Handler handler) {
3195 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
3196 CallbackHandler cbHandler = new CallbackHandler(handler);
3197 requestNetwork(request, networkCallback, 0, legacyType, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003198 }
3199
3200 /**
Etan Cohenaebf17e2017-03-01 12:47:28 -08003201 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
3202 * by a timeout.
3203 *
3204 * This function behaves identically to the non-timed-out version
3205 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network
3206 * is not found within the given time (in milliseconds) the
3207 * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be
3208 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3209 * not have to be released if timed-out (it is automatically released). Unregistering a
3210 * request that timed out is not an error.
3211 *
3212 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3213 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3214 * for that purpose. Calling this method will attempt to bring up the requested network.
3215 *
3216 * <p>This method requires the caller to hold either the
3217 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3218 * or the ability to modify system settings as determined by
3219 * {@link android.provider.Settings.System#canWrite}.</p>
3220 *
3221 * @param request {@link NetworkRequest} describing this request.
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003222 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3223 * the callback must not be shared - it uniquely specifies this request.
Etan Cohenaebf17e2017-03-01 12:47:28 -08003224 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3225 * before {@link NetworkCallback#onUnavailable()} is called. The timeout must
3226 * be a positive value (i.e. >0).
Etan Cohenaebf17e2017-03-01 12:47:28 -08003227 */
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003228 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
3229 int timeoutMs) {
Hugo Benichie7678512017-05-09 15:19:01 +09003230 checkTimeout(timeoutMs);
Hugo Benichi2583ef02017-02-02 17:02:36 +09003231 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
Erik Klineb583b032017-02-22 12:58:24 +09003232 requestNetwork(request, networkCallback, timeoutMs, legacyType, getDefaultHandler());
Hugo Benichi2583ef02017-02-02 17:02:36 +09003233 }
3234
Hugo Benichi2583ef02017-02-02 17:02:36 +09003235 /**
3236 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
3237 * by a timeout.
3238 *
Chalard Jean4d660112018-06-04 16:52:49 +09003239 * This function behaves identically to the version without timeout, but if a suitable
Hugo Benichi2583ef02017-02-02 17:02:36 +09003240 * network is not found within the given time (in milliseconds) the
Etan Cohenaebf17e2017-03-01 12:47:28 -08003241 * {@link NetworkCallback#onUnavailable} callback is called. The request can still be
3242 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3243 * not have to be released if timed-out (it is automatically released). Unregistering a
3244 * request that timed out is not an error.
3245 *
3246 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3247 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3248 * for that purpose. Calling this method will attempt to bring up the requested network.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003249 *
3250 * <p>This method requires the caller to hold either the
3251 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3252 * or the ability to modify system settings as determined by
3253 * {@link android.provider.Settings.System#canWrite}.</p>
3254 *
3255 * @param request {@link NetworkRequest} describing this request.
Etan Cohenaebf17e2017-03-01 12:47:28 -08003256 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3257 * the callback must not be shared - it uniquely specifies this request.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003258 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003259 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3260 * before {@link NetworkCallback#onUnavailable} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003261 */
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003262 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
3263 Handler handler, int timeoutMs) {
Hugo Benichie7678512017-05-09 15:19:01 +09003264 checkTimeout(timeoutMs);
Hugo Benichi2583ef02017-02-02 17:02:36 +09003265 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
3266 CallbackHandler cbHandler = new CallbackHandler(handler);
3267 requestNetwork(request, networkCallback, timeoutMs, legacyType, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003268 }
3269
3270 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003271 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003272 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003273 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfcde58f2015-02-11 16:51:13 -08003274 * <p>
Paul Jensen72db88e2015-03-10 10:54:12 -04003275 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
3276 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003277 */
Erik Kline90e93072014-11-19 12:12:24 +09003278 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003279
3280 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07003281 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003282 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003283 * {@link android.content.Intent#getParcelableExtra(String)}.
3284 */
Erik Kline90e93072014-11-19 12:12:24 +09003285 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003286
3287
3288 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09003289 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003290 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003291 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07003292 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003293 * the request may outlive the calling application and get called back when a suitable
3294 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003295 * <p>
3296 * The operation is an Intent broadcast that goes to a broadcast receiver that
3297 * you registered with {@link Context#registerReceiver} or through the
3298 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3299 * <p>
3300 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09003301 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3302 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07003303 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07003304 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07003305 * Intent to reserve the network or it will be released shortly after the Intent
3306 * is processed.
3307 * <p>
Paul Jensen694f2b82015-06-17 14:15:39 -04003308 * If there is already a request for this Intent registered (with the equality of
Robert Greenwalt9258c642014-03-26 16:47:06 -07003309 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003310 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003311 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003312 * The request may be released normally by calling
3313 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003314 * <p>It is presently unsupported to request a network with either
3315 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3316 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3317 * as these {@code NetworkCapabilities} represent states that a particular
3318 * network may never attain, and whether a network will attain these states
3319 * is unknown prior to bringing up the network so the framework does not
Chalard Jean4d660112018-06-04 16:52:49 +09003320 * know how to go about satisfying a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003321 *
3322 * <p>This method requires the caller to hold either the
3323 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3324 * or the ability to modify system settings as determined by
3325 * {@link android.provider.Settings.System#canWrite}.</p>
3326 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003327 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003328 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07003329 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003330 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003331 * @throws IllegalArgumentException if {@code request} contains either
3332 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3333 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003334 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003335 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003336 checkPendingIntentNotNull(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003337 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07003338 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003339 } catch (RemoteException e) {
3340 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003341 } catch (ServiceSpecificException e) {
3342 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003343 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003344 }
3345
3346 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003347 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
3348 * <p>
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003349 * This method has the same behavior as
3350 * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003351 * releasing network resources and disconnecting.
3352 *
3353 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3354 * PendingIntent passed to
3355 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
3356 * corresponding NetworkRequest you'd like to remove. Cannot be null.
3357 */
3358 public void releaseNetworkRequest(PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003359 checkPendingIntentNotNull(operation);
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003360 try {
3361 mService.releasePendingNetworkRequest(operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003362 } catch (RemoteException e) {
3363 throw e.rethrowFromSystemServer();
3364 }
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003365 }
3366
Hugo Benichie7678512017-05-09 15:19:01 +09003367 private static void checkPendingIntentNotNull(PendingIntent intent) {
3368 Preconditions.checkNotNull(intent, "PendingIntent cannot be null.");
3369 }
3370
3371 private static void checkCallbackNotNull(NetworkCallback callback) {
3372 Preconditions.checkNotNull(callback, "null NetworkCallback");
3373 }
3374
3375 private static void checkTimeout(int timeoutMs) {
3376 Preconditions.checkArgumentPositive(timeoutMs, "timeoutMs must be strictly positive.");
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003377 }
3378
3379 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07003380 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07003381 * {@link NetworkRequest}. The callbacks will continue to be called until
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003382 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003383 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003384 * @param request {@link NetworkRequest} describing this request.
3385 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3386 * networks change state.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003387 * The callback is invoked on the default internal Handler.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003388 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003389 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt6078b502014-06-11 16:05:07 -07003390 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003391 registerNetworkCallback(request, networkCallback, getDefaultHandler());
3392 }
3393
3394 /**
3395 * Registers to receive notifications about all networks which satisfy the given
3396 * {@link NetworkRequest}. The callbacks will continue to be called until
3397 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003398 *
3399 * @param request {@link NetworkRequest} describing this request.
3400 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3401 * networks change state.
3402 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003403 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003404 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Hugo Benichi2583ef02017-02-02 17:02:36 +09003405 public void registerNetworkCallback(
3406 NetworkRequest request, NetworkCallback networkCallback, Handler handler) {
3407 CallbackHandler cbHandler = new CallbackHandler(handler);
3408 NetworkCapabilities nc = request.networkCapabilities;
3409 sendRequestForNetwork(nc, networkCallback, 0, LISTEN, TYPE_NONE, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003410 }
3411
3412 /**
Paul Jensen694f2b82015-06-17 14:15:39 -04003413 * Registers a PendingIntent to be sent when a network is available which satisfies the given
3414 * {@link NetworkRequest}.
3415 *
3416 * This function behaves identically to the version that takes a NetworkCallback, but instead
3417 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
3418 * the request may outlive the calling application and get called back when a suitable
3419 * network is found.
3420 * <p>
3421 * The operation is an Intent broadcast that goes to a broadcast receiver that
3422 * you registered with {@link Context#registerReceiver} or through the
3423 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3424 * <p>
3425 * The operation Intent is delivered with two extras, a {@link Network} typed
3426 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3427 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
3428 * the original requests parameters.
3429 * <p>
3430 * If there is already a request for this Intent registered (with the equality of
3431 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
3432 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
3433 * <p>
3434 * The request may be released normally by calling
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003435 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Paul Jensen694f2b82015-06-17 14:15:39 -04003436 * @param request {@link NetworkRequest} describing this request.
3437 * @param operation Action to perform when the network is available (corresponds
3438 * to the {@link NetworkCallback#onAvailable} call. Typically
3439 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
3440 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003441 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Paul Jensen694f2b82015-06-17 14:15:39 -04003442 public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003443 checkPendingIntentNotNull(operation);
Paul Jensen694f2b82015-06-17 14:15:39 -04003444 try {
3445 mService.pendingListenForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003446 } catch (RemoteException e) {
3447 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003448 } catch (ServiceSpecificException e) {
3449 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003450 }
Paul Jensen694f2b82015-06-17 14:15:39 -04003451 }
3452
3453 /**
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003454 * Registers to receive notifications about changes in the system default network. The callbacks
3455 * will continue to be called until either the application exits or
3456 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Erik Klinea2d29402016-03-16 15:31:39 +09003457 *
3458 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3459 * system default network changes.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003460 * The callback is invoked on the default internal Handler.
Erik Klinea2d29402016-03-16 15:31:39 +09003461 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003462 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Erik Klinea2d29402016-03-16 15:31:39 +09003463 public void registerDefaultNetworkCallback(NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003464 registerDefaultNetworkCallback(networkCallback, getDefaultHandler());
3465 }
3466
3467 /**
3468 * Registers to receive notifications about changes in the system default network. The callbacks
3469 * will continue to be called until either the application exits or
3470 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003471 *
3472 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3473 * system default network changes.
3474 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003475 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003476 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Hugo Benichi2583ef02017-02-02 17:02:36 +09003477 public void registerDefaultNetworkCallback(NetworkCallback networkCallback, Handler handler) {
Erik Klinea2d29402016-03-16 15:31:39 +09003478 // This works because if the NetworkCapabilities are null,
3479 // ConnectivityService takes them from the default request.
3480 //
3481 // Since the capabilities are exactly the same as the default request's
3482 // capabilities, this request is guaranteed, at all times, to be
3483 // satisfied by the same network, if any, that satisfies the default
3484 // request, i.e., the system default network.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003485 CallbackHandler cbHandler = new CallbackHandler(handler);
Chalard Jean4d660112018-06-04 16:52:49 +09003486 sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0,
3487 REQUEST, TYPE_NONE, cbHandler);
Erik Klinea2d29402016-03-16 15:31:39 +09003488 }
3489
3490 /**
fengludb571472015-04-21 17:12:05 -07003491 * Requests bandwidth update for a given {@link Network} and returns whether the update request
3492 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
3493 * network connection for updated bandwidth information. The caller will be notified via
3494 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003495 * method assumes that the caller has previously called
3496 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network
3497 * changes.
fenglub15e72b2015-03-20 11:29:56 -07003498 *
fengluae519192015-04-27 14:28:04 -07003499 * @param network {@link Network} specifying which network you're interested.
fengludb571472015-04-21 17:12:05 -07003500 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglub15e72b2015-03-20 11:29:56 -07003501 */
fengludb571472015-04-21 17:12:05 -07003502 public boolean requestBandwidthUpdate(Network network) {
fenglub15e72b2015-03-20 11:29:56 -07003503 try {
fengludb571472015-04-21 17:12:05 -07003504 return mService.requestBandwidthUpdate(network);
fenglub15e72b2015-03-20 11:29:56 -07003505 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003506 throw e.rethrowFromSystemServer();
fenglub15e72b2015-03-20 11:29:56 -07003507 }
3508 }
3509
3510 /**
Hugo Benichidafed3d2017-03-06 09:17:06 +09003511 * Unregisters a {@code NetworkCallback} and possibly releases networks originating from
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003512 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and
3513 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls.
3514 * If the given {@code NetworkCallback} had previously been used with
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09003515 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
3516 * will be disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003517 *
Hugo Benichidafed3d2017-03-06 09:17:06 +09003518 * Notifications that would have triggered that {@code NetworkCallback} will immediately stop
3519 * triggering it as soon as this call returns.
3520 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003521 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003522 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003523 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
Hugo Benichie7678512017-05-09 15:19:01 +09003524 checkCallbackNotNull(networkCallback);
Hugo Benichidafed3d2017-03-06 09:17:06 +09003525 final List<NetworkRequest> reqs = new ArrayList<>();
3526 // Find all requests associated to this callback and stop callback triggers immediately.
3527 // Callback is reusable immediately. http://b/20701525, http://b/35921499.
3528 synchronized (sCallbacks) {
Hugo Benichi31c176d2017-06-17 13:14:12 +09003529 Preconditions.checkArgument(networkCallback.networkRequest != null,
3530 "NetworkCallback was not registered");
3531 Preconditions.checkArgument(networkCallback.networkRequest != ALREADY_UNREGISTERED,
3532 "NetworkCallback was already unregistered");
Hugo Benichidafed3d2017-03-06 09:17:06 +09003533 for (Map.Entry<NetworkRequest, NetworkCallback> e : sCallbacks.entrySet()) {
3534 if (e.getValue() == networkCallback) {
3535 reqs.add(e.getKey());
3536 }
3537 }
3538 // TODO: throw exception if callback was registered more than once (http://b/20701525).
3539 for (NetworkRequest r : reqs) {
3540 try {
3541 mService.releaseNetworkRequest(r);
3542 } catch (RemoteException e) {
3543 throw e.rethrowFromSystemServer();
3544 }
3545 // Only remove mapping if rpc was successful.
3546 sCallbacks.remove(r);
3547 }
Hugo Benichi31c176d2017-06-17 13:14:12 +09003548 networkCallback.networkRequest = ALREADY_UNREGISTERED;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003549 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003550 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003551
3552 /**
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003553 * Unregisters a callback previously registered via
3554 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3555 *
3556 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3557 * PendingIntent passed to
3558 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3559 * Cannot be null.
3560 */
3561 public void unregisterNetworkCallback(PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003562 checkPendingIntentNotNull(operation);
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003563 releaseNetworkRequest(operation);
3564 }
3565
3566 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003567 * Informs the system whether it should switch to {@code network} regardless of whether it is
3568 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
3569 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
3570 * the system default network regardless of any other network that's currently connected. If
3571 * {@code always} is true, then the choice is remembered, so that the next time the user
3572 * connects to this network, the system will switch to it.
3573 *
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003574 * @param network The network to accept.
3575 * @param accept Whether to accept the network even if unvalidated.
3576 * @param always Whether to remember this choice in the future.
3577 *
3578 * @hide
3579 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003580 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003581 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
3582 try {
3583 mService.setAcceptUnvalidated(network, accept, always);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003584 } catch (RemoteException e) {
3585 throw e.rethrowFromSystemServer();
3586 }
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003587 }
3588
3589 /**
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003590 * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is
3591 * only meaningful if the system is configured not to penalize such networks, e.g., if the
3592 * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code
3593 * NETWORK_AVOID_BAD_WIFI setting is unset}.
3594 *
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003595 * @param network The network to accept.
3596 *
3597 * @hide
3598 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003599 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003600 public void setAvoidUnvalidated(Network network) {
3601 try {
3602 mService.setAvoidUnvalidated(network);
3603 } catch (RemoteException e) {
3604 throw e.rethrowFromSystemServer();
3605 }
3606 }
3607
3608 /**
Lorenzo Colitti4734cdb2017-04-27 14:30:21 +09003609 * Requests that the system open the captive portal app on the specified network.
3610 *
3611 * @param network The network to log into.
3612 *
3613 * @hide
3614 */
3615 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
3616 public void startCaptivePortalApp(Network network) {
3617 try {
3618 mService.startCaptivePortalApp(network);
3619 } catch (RemoteException e) {
3620 throw e.rethrowFromSystemServer();
3621 }
3622 }
3623
3624 /**
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003625 * It is acceptable to briefly use multipath data to provide seamless connectivity for
3626 * time-sensitive user-facing operations when the system default network is temporarily
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003627 * unresponsive. The amount of data should be limited (less than one megabyte for every call to
3628 * this method), and the operation should be infrequent to ensure that data usage is limited.
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003629 *
3630 * An example of such an operation might be a time-sensitive foreground activity, such as a
3631 * voice command, that the user is performing while walking out of range of a Wi-Fi network.
3632 */
3633 public static final int MULTIPATH_PREFERENCE_HANDOVER = 1 << 0;
3634
3635 /**
3636 * It is acceptable to use small amounts of multipath data on an ongoing basis to provide
3637 * a backup channel for traffic that is primarily going over another network.
3638 *
3639 * An example might be maintaining backup connections to peers or servers for the purpose of
3640 * fast fallback if the default network is temporarily unresponsive or disconnects. The traffic
3641 * on backup paths should be negligible compared to the traffic on the main path.
3642 */
3643 public static final int MULTIPATH_PREFERENCE_RELIABILITY = 1 << 1;
3644
3645 /**
3646 * It is acceptable to use metered data to improve network latency and performance.
3647 */
3648 public static final int MULTIPATH_PREFERENCE_PERFORMANCE = 1 << 2;
3649
3650 /**
3651 * Return value to use for unmetered networks. On such networks we currently set all the flags
3652 * to true.
3653 * @hide
3654 */
3655 public static final int MULTIPATH_PREFERENCE_UNMETERED =
3656 MULTIPATH_PREFERENCE_HANDOVER |
3657 MULTIPATH_PREFERENCE_RELIABILITY |
3658 MULTIPATH_PREFERENCE_PERFORMANCE;
3659
3660 /** @hide */
3661 @Retention(RetentionPolicy.SOURCE)
3662 @IntDef(flag = true, value = {
3663 MULTIPATH_PREFERENCE_HANDOVER,
3664 MULTIPATH_PREFERENCE_RELIABILITY,
3665 MULTIPATH_PREFERENCE_PERFORMANCE,
3666 })
3667 public @interface MultipathPreference {
3668 }
3669
3670 /**
3671 * Provides a hint to the calling application on whether it is desirable to use the
3672 * multinetwork APIs (e.g., {@link Network#openConnection}, {@link Network#bindSocket}, etc.)
3673 * for multipath data transfer on this network when it is not the system default network.
3674 * Applications desiring to use multipath network protocols should call this method before
3675 * each such operation.
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003676 *
3677 * @param network The network on which the application desires to use multipath data.
3678 * If {@code null}, this method will return the a preference that will generally
3679 * apply to metered networks.
3680 * @return a bitwise OR of zero or more of the {@code MULTIPATH_PREFERENCE_*} constants.
3681 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003682 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003683 public @MultipathPreference int getMultipathPreference(Network network) {
3684 try {
3685 return mService.getMultipathPreference(network);
3686 } catch (RemoteException e) {
3687 throw e.rethrowFromSystemServer();
3688 }
3689 }
3690
3691 /**
Stuart Scott984dc852015-03-30 13:17:11 -07003692 * Resets all connectivity manager settings back to factory defaults.
3693 * @hide
3694 */
3695 public void factoryReset() {
Stuart Scott984dc852015-03-30 13:17:11 -07003696 try {
Stuart Scottf1fb3972015-04-02 18:00:02 -07003697 mService.factoryReset();
Stuart Scott984dc852015-03-30 13:17:11 -07003698 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003699 throw e.rethrowFromSystemServer();
Stuart Scott984dc852015-03-30 13:17:11 -07003700 }
3701 }
3702
3703 /**
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003704 * Binds the current process to {@code network}. All Sockets created in the future
3705 * (and not explicitly bound via a bound SocketFactory from
3706 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3707 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3708 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3709 * work and all host name resolutions will fail. This is by design so an application doesn't
3710 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3711 * To clear binding pass {@code null} for {@code network}. Using individually bound
3712 * Sockets created by Network.getSocketFactory().createSocket() and
3713 * performing network-specific host name resolutions via
3714 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen72db88e2015-03-10 10:54:12 -04003715 * {@code bindProcessToNetwork}.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003716 *
3717 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3718 * the current binding.
3719 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3720 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003721 public boolean bindProcessToNetwork(Network network) {
Chalard Jean4d660112018-06-04 16:52:49 +09003722 // Forcing callers to call through non-static function ensures ConnectivityManager
Paul Jensen72db88e2015-03-10 10:54:12 -04003723 // instantiated.
3724 return setProcessDefaultNetwork(network);
3725 }
3726
3727 /**
3728 * Binds the current process to {@code network}. All Sockets created in the future
3729 * (and not explicitly bound via a bound SocketFactory from
3730 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3731 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3732 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3733 * work and all host name resolutions will fail. This is by design so an application doesn't
3734 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3735 * To clear binding pass {@code null} for {@code network}. Using individually bound
3736 * Sockets created by Network.getSocketFactory().createSocket() and
3737 * performing network-specific host name resolutions via
3738 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
3739 * {@code setProcessDefaultNetwork}.
3740 *
3741 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3742 * the current binding.
3743 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3744 * @deprecated This function can throw {@link IllegalStateException}. Use
3745 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
3746 * is a direct replacement.
3747 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003748 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003749 public static boolean setProcessDefaultNetwork(Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003750 int netId = (network == null) ? NETID_UNSET : network.netId;
Paul Jensen72db88e2015-03-10 10:54:12 -04003751 if (netId == NetworkUtils.getBoundNetworkForProcess()) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003752 return true;
3753 }
3754 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05003755 // Set HTTP proxy system properties to match network.
3756 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colittiec4c5552015-04-22 11:52:48 +09003757 try {
3758 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
3759 } catch (SecurityException e) {
3760 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
3761 Log.e(TAG, "Can't set proxy properties", e);
3762 }
Paul Jensenc91b5342014-08-27 12:38:45 -04003763 // Must flush DNS cache as new network may have different DNS resolutions.
3764 InetAddress.clearDnsCache();
3765 // Must flush socket pool as idle sockets will be bound to previous network and may
3766 // cause subsequent fetches to be performed on old network.
3767 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
3768 return true;
3769 } else {
3770 return false;
3771 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003772 }
3773
3774 /**
3775 * Returns the {@link Network} currently bound to this process via
Paul Jensen72db88e2015-03-10 10:54:12 -04003776 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003777 *
3778 * @return {@code Network} to which this process is bound, or {@code null}.
3779 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003780 public Network getBoundNetworkForProcess() {
3781 // Forcing callers to call thru non-static function ensures ConnectivityManager
3782 // instantiated.
3783 return getProcessDefaultNetwork();
3784 }
3785
3786 /**
3787 * Returns the {@link Network} currently bound to this process via
3788 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
3789 *
3790 * @return {@code Network} to which this process is bound, or {@code null}.
3791 * @deprecated Using this function can lead to other functions throwing
3792 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
3793 * {@code getBoundNetworkForProcess} is a direct replacement.
3794 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003795 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003796 public static Network getProcessDefaultNetwork() {
Paul Jensen72db88e2015-03-10 10:54:12 -04003797 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04003798 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003799 return new Network(netId);
3800 }
3801
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003802 private void unsupportedStartingFrom(int version) {
3803 if (Process.myUid() == Process.SYSTEM_UID) {
3804 // The getApplicationInfo() call we make below is not supported in system context, and
3805 // we want to allow the system to use these APIs anyway.
3806 return;
3807 }
3808
3809 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
3810 throw new UnsupportedOperationException(
3811 "This method is not supported in target SDK version " + version + " and above");
3812 }
3813 }
3814
3815 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
3816 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
Lifu Tang30f95a72016-01-07 23:20:38 -08003817 // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003818 // remove these exemptions. Note that this check is not secure, and apps can still access these
3819 // functions by accessing ConnectivityService directly. However, it should be clear that doing
3820 // so is unsupported and may break in the future. http://b/22728205
3821 private void checkLegacyRoutingApiAccess() {
3822 if (mContext.checkCallingOrSelfPermission("com.android.permission.INJECT_OMADM_SETTINGS")
3823 == PackageManager.PERMISSION_GRANTED) {
3824 return;
3825 }
3826
Dianne Hackborn692a2442015-07-31 10:35:34 -07003827 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003828 }
3829
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003830 /**
3831 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen72db88e2015-03-10 10:54:12 -04003832 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003833 *
3834 * @param network The {@link Network} to bind host resolutions from the current process to, or
3835 * {@code null} to clear the current binding.
3836 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3837 * @hide
3838 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
3839 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003840 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01003841 @UnsupportedAppUsage
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003842 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04003843 return NetworkUtils.bindProcessToNetworkForHostResolution(
Erik Klinef4fa9822018-04-27 22:48:33 +09003844 (network == null) ? NETID_UNSET : network.getNetIdForResolv());
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003845 }
Felipe Leme1b103232016-01-22 09:44:57 -08003846
3847 /**
3848 * Device is not restricting metered network activity while application is running on
3849 * background.
3850 */
3851 public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1;
3852
3853 /**
3854 * Device is restricting metered network activity while application is running on background,
3855 * but application is allowed to bypass it.
3856 * <p>
3857 * In this state, application should take action to mitigate metered network access.
3858 * For example, a music streaming application should switch to a low-bandwidth bitrate.
3859 */
3860 public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2;
3861
3862 /**
3863 * Device is restricting metered network activity while application is running on background.
Felipe Leme9778f762016-01-27 14:46:39 -08003864 * <p>
Felipe Leme1b103232016-01-22 09:44:57 -08003865 * In this state, application should not try to use the network while running on background,
3866 * because it would be denied.
3867 */
3868 public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3;
3869
Felipe Leme9778f762016-01-27 14:46:39 -08003870 /**
3871 * A change in the background metered network activity restriction has occurred.
3872 * <p>
3873 * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction
3874 * applies to them.
3875 * <p>
3876 * This is only sent to registered receivers, not manifest receivers.
3877 */
3878 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
3879 public static final String ACTION_RESTRICT_BACKGROUND_CHANGED =
3880 "android.net.conn.RESTRICT_BACKGROUND_CHANGED";
3881
Felipe Lemeecfccea2016-01-25 11:48:04 -08003882 /** @hide */
3883 @Retention(RetentionPolicy.SOURCE)
Felipe Leme1b103232016-01-22 09:44:57 -08003884 @IntDef(flag = false, value = {
3885 RESTRICT_BACKGROUND_STATUS_DISABLED,
3886 RESTRICT_BACKGROUND_STATUS_WHITELISTED,
3887 RESTRICT_BACKGROUND_STATUS_ENABLED,
3888 })
Felipe Leme1b103232016-01-22 09:44:57 -08003889 public @interface RestrictBackgroundStatus {
3890 }
3891
3892 private INetworkPolicyManager getNetworkPolicyManager() {
3893 synchronized (this) {
3894 if (mNPManager != null) {
3895 return mNPManager;
3896 }
3897 mNPManager = INetworkPolicyManager.Stub.asInterface(ServiceManager
3898 .getService(Context.NETWORK_POLICY_SERVICE));
3899 return mNPManager;
3900 }
3901 }
3902
3903 /**
3904 * Determines if the calling application is subject to metered network restrictions while
3905 * running on background.
Felipe Lemec9c7be52016-05-16 13:57:19 -07003906 *
3907 * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED},
3908 * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED},
3909 * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED}
Felipe Leme1b103232016-01-22 09:44:57 -08003910 */
3911 public @RestrictBackgroundStatus int getRestrictBackgroundStatus() {
3912 try {
3913 return getNetworkPolicyManager().getRestrictBackgroundByCaller();
3914 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003915 throw e.rethrowFromSystemServer();
Felipe Leme1b103232016-01-22 09:44:57 -08003916 }
3917 }
Ricky Wai44dcbde2018-01-23 04:09:45 +00003918
3919 /**
3920 * The network watchlist is a list of domains and IP addresses that are associated with
Ricky Waia86d5d52018-03-20 14:20:54 +00003921 * potentially harmful apps. This method returns the SHA-256 of the watchlist config file
3922 * currently used by the system for validation purposes.
Ricky Wai44dcbde2018-01-23 04:09:45 +00003923 *
3924 * @return Hash of network watchlist config file. Null if config does not exist.
3925 */
3926 public byte[] getNetworkWatchlistConfigHash() {
3927 try {
3928 return mService.getNetworkWatchlistConfigHash();
3929 } catch (RemoteException e) {
3930 Log.e(TAG, "Unable to get watchlist config hash");
3931 throw e.rethrowFromSystemServer();
3932 }
3933 }
Jeff Vander Stoep0ac2c092018-07-23 10:57:53 -07003934
3935 /**
3936 * Returns the {@code uid} of the owner of a network connection.
3937 *
3938 * @param protocol The protocol of the connection. Only {@code IPPROTO_TCP} and
3939 * {@code IPPROTO_UDP} currently supported.
3940 * @param local The local {@link InetSocketAddress} of a connection.
3941 * @param remote The remote {@link InetSocketAddress} of a connection.
3942 *
3943 * @return {@code uid} if the connection is found and the app has permission to observe it
3944 * (e.g., if it is associated with the calling VPN app's tunnel) or
3945 * {@link android.os.Process#INVALID_UID} if the connection is not found.
3946 */
3947 public int getConnectionOwnerUid(int protocol, InetSocketAddress local,
3948 InetSocketAddress remote) {
3949 ConnectionInfo connectionInfo = new ConnectionInfo(protocol, local, remote);
3950 try {
3951 return mService.getConnectionOwnerUid(connectionInfo);
3952 } catch (RemoteException e) {
3953 throw e.rethrowFromSystemServer();
3954 }
3955 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003956}