blob: ee386127bd4d536fd63f64fdc156e5663f7b5f3d [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;
Robert Greenwalt42acef32009-08-12 16:08:25 -070029import android.os.Binder;
Jeff Sharkey3a844fc2011-08-16 14:37:57 -070030import android.os.Build.VERSION_CODES;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080031import android.os.Bundle;
Robert Greenwalt9258c642014-03-26 16:47:06 -070032import android.os.Handler;
33import android.os.HandlerThread;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080034import android.os.IBinder;
35import android.os.INetworkActivityListener;
36import android.os.INetworkManagementService;
Robert Greenwalt9258c642014-03-26 16:47:06 -070037import android.os.Looper;
38import android.os.Message;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -070039import android.os.Messenger;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +090040import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.RemoteException;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080042import android.os.ResultReceiver;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080043import android.os.ServiceManager;
Hugo Benichicb883232017-05-11 13:16:17 +090044import android.os.ServiceSpecificException;
Jeff Sharkey961e3042011-08-29 16:02:57 -070045import android.provider.Settings;
Wink Saville36ffb042014-12-05 11:10:30 -080046import android.telephony.SubscriptionManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080047import android.util.ArrayMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -070048import android.util.Log;
Erik Kline35bf06c2017-01-26 18:08:28 +090049import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
Robert Greenwaltafa05c02014-05-21 20:04:36 -070051import com.android.internal.telephony.ITelephony;
Robert Greenwalt562cc542014-05-15 18:07:26 -070052import com.android.internal.telephony.PhoneConstants;
Hugo Benichidafed3d2017-03-06 09:17:06 +090053import com.android.internal.util.Preconditions;
54import com.android.internal.util.Protocol;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070055
Paul Jensenc91b5342014-08-27 12:38:45 -040056import libcore.net.event.NetworkEventDispatcher;
57
Felipe Leme1b103232016-01-22 09:44:57 -080058import java.lang.annotation.Retention;
59import java.lang.annotation.RetentionPolicy;
Jeremy Kleind42209d2015-12-28 15:11:58 -080060import java.net.InetAddress;
Jeff Vander Stoep0ac2c092018-07-23 10:57:53 -070061import java.net.InetSocketAddress;
Hugo Benichidafed3d2017-03-06 09:17:06 +090062import java.util.ArrayList;
Jeremy Kleind42209d2015-12-28 15:11:58 -080063import java.util.HashMap;
Hugo Benichidafed3d2017-03-06 09:17:06 +090064import java.util.List;
65import java.util.Map;
Jeremy Kleind42209d2015-12-28 15:11:58 -080066
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067/**
68 * Class that answers queries about the state of network connectivity. It also
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060069 * notifies applications when network connectivity changes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 * <p>
71 * The primary responsibilities of this class are to:
72 * <ol>
73 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
74 * <li>Send broadcast intents when network connectivity changes</li>
75 * <li>Attempt to "fail over" to another network when connectivity to a network
76 * is lost</li>
77 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
78 * state of the available networks</li>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070079 * <li>Provide an API that allows applications to request and select networks for their data
80 * traffic</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081 * </ol>
82 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060083@SystemService(Context.CONNECTIVITY_SERVICE)
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070084public class ConnectivityManager {
85 private static final String TAG = "ConnectivityManager";
86
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070088 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 * been established or lost. The NetworkInfo for the affected network is
90 * sent as an extra; it should be consulted to see what kind of
91 * connectivity event occurred.
92 * <p/>
Mark Lu33ec1062016-12-05 10:57:55 -080093 * Apps targeting Android 7.0 (API level 24) and higher do not receive this
94 * broadcast if they declare the broadcast receiver in their manifest. Apps
95 * will still receive broadcasts if they register their
96 * {@link android.content.BroadcastReceiver} with
97 * {@link android.content.Context#registerReceiver Context.registerReceiver()}
98 * and that context is still valid.
99 * <p/>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * If this is a connection that was the result of failing over from a
101 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
102 * set to true.
103 * <p/>
104 * For a loss of connectivity, if the connectivity manager is attempting
105 * to connect (or has already connected) to another network, the
106 * NetworkInfo for the new network is also passed as an extra. This lets
107 * any receivers of the broadcast know that they should not necessarily
108 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800109 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 * the failover attempt succeeded (and so there is still overall data
111 * connectivity), or that the failover attempt failed, meaning that all
112 * connectivity has been lost.
113 * <p/>
114 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
115 * is set to {@code true} if there are no connected networks at all.
Chalard Jean054cd162018-02-10 05:33:50 +0900116 *
117 * @deprecated apps should use the more versatile {@link #requestNetwork},
118 * {@link #registerNetworkCallback} or {@link #registerDefaultNetworkCallback}
119 * functions instead for faster and more detailed updates about the network
120 * changes they care about.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800122 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jean054cd162018-02-10 05:33:50 +0900123 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800124 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700125
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 /**
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700127 * A temporary hack until SUPL system can get off the legacy APIS.
128 * They do too many network requests and the long list of apps listening
Chalard Jean4d660112018-06-04 16:52:49 +0900129 * and waking due to the CONNECTIVITY_ACTION broadcast makes it expensive.
130 * Use this broadcast intent instead for SUPL requests.
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700131 * @hide
132 */
133 public static final String CONNECTIVITY_ACTION_SUPL =
134 "android.net.conn.CONNECTIVITY_CHANGE_SUPL";
135
136 /**
Paul Jensen25a217c2015-02-27 22:55:47 -0500137 * The device has connected to a network that has presented a captive
138 * portal, which is blocking Internet connectivity. The user was presented
139 * with a notification that network sign in is required,
140 * and the user invoked the notification's action indicating they
Paul Jensen49e3edf2015-05-22 10:50:39 -0400141 * desire to sign in to the network. Apps handling this activity should
Paul Jensen25a217c2015-02-27 22:55:47 -0500142 * facilitate signing in to the network. This action includes a
143 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents
144 * the network presenting the captive portal; all communication with the
145 * captive portal must be done using this {@code Network} object.
146 * <p/>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400147 * This activity includes a {@link CaptivePortal} extra named
148 * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different
149 * outcomes of the captive portal sign in to the system:
150 * <ul>
151 * <li> When the app handling this action believes the user has signed in to
152 * the network and the captive portal has been dismissed, the app should
153 * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can
154 * reevaluate the network. If reevaluation finds the network no longer
155 * subject to a captive portal, the network may become the default active
Chalard Jean4d660112018-06-04 16:52:49 +0900156 * data network.</li>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400157 * <li> When the app handling this action believes the user explicitly wants
Paul Jensen25a217c2015-02-27 22:55:47 -0500158 * to ignore the captive portal and the network, the app should call
Paul Jensen49e3edf2015-05-22 10:50:39 -0400159 * {@link CaptivePortal#ignoreNetwork}. </li>
160 * </ul>
Paul Jensen25a217c2015-02-27 22:55:47 -0500161 */
162 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
163 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL";
164
165 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 * The lookup key for a {@link NetworkInfo} object. Retrieve with
167 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700168 *
169 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
170 * should always obtain network information through
Paul Jensen3541e9f2015-03-18 12:23:02 -0400171 * {@link #getActiveNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700172 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700174 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700176
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700178 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700179 *
180 * @see android.content.Intent#getIntExtra(String, int)
181 */
182 public static final String EXTRA_NETWORK_TYPE = "networkType";
183
184 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800185 * The lookup key for a boolean that indicates whether a connect event
186 * is for a network to which the connectivity manager was failing over
187 * following a disconnect on another network.
188 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
189 */
190 public static final String EXTRA_IS_FAILOVER = "isFailover";
191 /**
192 * The lookup key for a {@link NetworkInfo} object. This is supplied when
193 * there is another network that it may be possible to connect to. Retrieve with
194 * {@link android.content.Intent#getParcelableExtra(String)}.
195 */
196 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
197 /**
198 * The lookup key for a boolean that indicates whether there is a
199 * complete lack of connectivity, i.e., no network is available.
200 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
201 */
202 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
203 /**
204 * The lookup key for a string that indicates why an attempt to connect
205 * to a network failed. The string has no particular structure. It is
206 * intended to be used in notifications presented to users. Retrieve
207 * it with {@link android.content.Intent#getStringExtra(String)}.
208 */
209 public static final String EXTRA_REASON = "reason";
210 /**
211 * The lookup key for a string that provides optionally supplied
212 * extra information about the network state. The information
213 * may be passed up from the lower networking layers, and its
214 * meaning may be specific to a particular network type. Retrieve
215 * it with {@link android.content.Intent#getStringExtra(String)}.
216 */
217 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700218 /**
219 * The lookup key for an int that provides information about
220 * our connection to the internet at large. 0 indicates no connection,
221 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700222 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700223 * {@hide}
224 */
225 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800226 /**
Paul Jensen49e3edf2015-05-22 10:50:39 -0400227 * The lookup key for a {@link CaptivePortal} object included with the
228 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal}
229 * object can be used to either indicate to the system that the captive
230 * portal has been dismissed or that the user does not want to pursue
231 * signing in to captive portal. Retrieve it with
232 * {@link android.content.Intent#getParcelableExtra(String)}.
Paul Jensen25a217c2015-02-27 22:55:47 -0500233 */
Paul Jensen49e3edf2015-05-22 10:50:39 -0400234 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL";
Jan Nordqvist52eb29f2015-09-22 15:54:32 -0700235
236 /**
237 * Key for passing a URL to the captive portal login activity.
238 */
239 public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL";
240
Paul Jensen25a217c2015-02-27 22:55:47 -0500241 /**
Remi NGUYEN VAN8255c2d2018-05-22 10:01:53 +0900242 * Key for passing a {@link android.net.captiveportal.CaptivePortalProbeSpec} to the captive
243 * portal login activity.
244 * {@hide}
245 */
246 public static final String EXTRA_CAPTIVE_PORTAL_PROBE_SPEC =
247 "android.net.extra.CAPTIVE_PORTAL_PROBE_SPEC";
248
249 /**
Hugo Benichicdf3ba42016-12-14 08:23:40 +0900250 * Key for passing a user agent string to the captive portal login activity.
251 * {@hide}
252 */
253 public static final String EXTRA_CAPTIVE_PORTAL_USER_AGENT =
254 "android.net.extra.CAPTIVE_PORTAL_USER_AGENT";
255
256 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700257 * Broadcast action to indicate the change of data activity status
258 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800259 * The network becomes active when data transmission is started, or
260 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700261 * {@hide}
262 */
263 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jean4d660112018-06-04 16:52:49 +0900264 public static final String ACTION_DATA_ACTIVITY_CHANGE =
265 "android.net.conn.DATA_ACTIVITY_CHANGE";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700266 /**
267 * The lookup key for an enum that indicates the network device type on which this data activity
268 * change happens.
269 * {@hide}
270 */
271 public static final String EXTRA_DEVICE_TYPE = "deviceType";
272 /**
273 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
274 * it is actively sending or receiving data and {@code false} means it is idle.
275 * {@hide}
276 */
277 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700278 /**
279 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
280 * {@hide}
281 */
282 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700283
284 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800285 * Broadcast Action: The setting for background data usage has changed
286 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
287 * <p>
288 * If an application uses the network in the background, it should listen
289 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700290 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800291 * <p>
292 *
293 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
294 * of background data depends on several combined factors, and
295 * this broadcast is no longer sent. Instead, when background
296 * data is unavailable, {@link #getActiveNetworkInfo()} will now
297 * appear disconnected. During first boot after a platform
298 * upgrade, this broadcast will be sent once if
299 * {@link #getBackgroundDataSetting()} was {@code false} before
300 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800301 */
302 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800303 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800304 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
305 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
306
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700307 /**
308 * Broadcast Action: The network connection may not be good
309 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
310 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
311 * the network and it's condition.
312 * @hide
313 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800314 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100315 @UnsupportedAppUsage
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700316 public static final String INET_CONDITION_ACTION =
317 "android.net.conn.INET_CONDITION_ACTION";
318
Robert Greenwalt42acef32009-08-12 16:08:25 -0700319 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800320 * Broadcast Action: A tetherable connection has come or gone.
321 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
Erik Kline8351faa2017-04-17 16:47:23 +0900322 * {@code ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY},
323 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER}, and
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800324 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
325 * the current state of tethering. Each include a list of
326 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800327 * @hide
328 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800329 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100330 @UnsupportedAppUsage
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800331 public static final String ACTION_TETHER_STATE_CHANGED =
332 "android.net.conn.TETHER_STATE_CHANGED";
333
334 /**
335 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800336 * gives a String[] listing all the interfaces configured for
337 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800338 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100339 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800340 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800341
342 /**
343 * @hide
Erik Kline8351faa2017-04-17 16:47:23 +0900344 * gives a String[] listing all the interfaces currently in local-only
345 * mode (ie, has DHCPv4+IPv6-ULA support and no packet forwarding)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800346 */
Erik Kline8351faa2017-04-17 16:47:23 +0900347 public static final String EXTRA_ACTIVE_LOCAL_ONLY = "localOnlyArray";
348
349 /**
350 * @hide
351 * gives a String[] listing all the interfaces currently tethered
352 * (ie, has DHCPv4 support and packets potentially forwarded/NATed)
353 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100354 @UnsupportedAppUsage
Erik Kline8351faa2017-04-17 16:47:23 +0900355 public static final String EXTRA_ACTIVE_TETHER = "tetherArray";
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800356
357 /**
358 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800359 * gives a String[] listing all the interfaces we tried to tether and
360 * failed. Use {@link #getLastTetherError} to find the error code
361 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800362 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100363 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800364 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800365
366 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800367 * Broadcast Action: The captive portal tracker has finished its test.
368 * Sent only while running Setup Wizard, in lieu of showing a user
369 * notification.
370 * @hide
371 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800372 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800373 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
374 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
375 /**
376 * The lookup key for a boolean that indicates whether a captive portal was detected.
377 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
378 * @hide
379 */
380 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
381
382 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900383 * Action used to display a dialog that asks the user whether to connect to a network that is
384 * not validated. This intent is used to start the dialog in settings via startActivity.
385 *
386 * @hide
387 */
388 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED";
389
390 /**
Lorenzo Colitti9be58c52016-09-15 14:02:29 +0900391 * Action used to display a dialog that asks the user whether to avoid a network that is no
392 * longer validated. This intent is used to start the dialog in settings via startActivity.
393 *
394 * @hide
395 */
396 public static final String ACTION_PROMPT_LOST_VALIDATION =
397 "android.net.conn.PROMPT_LOST_VALIDATION";
398
399 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800400 * Invalid tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900401 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800402 * @hide
403 */
404 public static final int TETHERING_INVALID = -1;
405
406 /**
407 * Wifi tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900408 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800409 * @hide
410 */
411 @SystemApi
412 public static final int TETHERING_WIFI = 0;
413
414 /**
415 * USB tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900416 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800417 * @hide
418 */
419 @SystemApi
420 public static final int TETHERING_USB = 1;
421
422 /**
423 * Bluetooth tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900424 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800425 * @hide
426 */
427 @SystemApi
428 public static final int TETHERING_BLUETOOTH = 2;
429
430 /**
431 * Extra used for communicating with the TetherService. Includes the type of tethering to
432 * enable if any.
433 * @hide
434 */
435 public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType";
436
437 /**
438 * Extra used for communicating with the TetherService. Includes the type of tethering for
439 * which to cancel provisioning.
440 * @hide
441 */
442 public static final String EXTRA_REM_TETHER_TYPE = "extraRemTetherType";
443
444 /**
445 * Extra used for communicating with the TetherService. True to schedule a recheck of tether
446 * provisioning.
447 * @hide
448 */
449 public static final String EXTRA_SET_ALARM = "extraSetAlarm";
450
451 /**
452 * Tells the TetherService to run a provision check now.
453 * @hide
454 */
455 public static final String EXTRA_RUN_PROVISION = "extraRunProvision";
456
457 /**
458 * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver}
459 * which will receive provisioning results. Can be left empty.
460 * @hide
461 */
462 public static final String EXTRA_PROVISION_CALLBACK = "extraProvisionCallback";
463
464 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800465 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700466 * @hide
467 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100468 @UnsupportedAppUsage
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700469 public static final int TYPE_NONE = -1;
470
471 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900472 * A Mobile data connection. Devices may support more than one.
473 *
474 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
475 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
476 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700477 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900478 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700479 public static final int TYPE_MOBILE = 0;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900480
Robert Greenwalt42acef32009-08-12 16:08:25 -0700481 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900482 * A WIFI data connection. Devices may support more than one.
483 *
484 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
485 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
486 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700487 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900488 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700489 public static final int TYPE_WIFI = 1;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900490
Robert Greenwalt42acef32009-08-12 16:08:25 -0700491 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800492 * An MMS-specific Mobile data connection. This network type may use the
493 * same network interface as {@link #TYPE_MOBILE} or it may use a different
494 * one. This is used by applications needing to talk to the carrier's
495 * Multimedia Messaging Service servers.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900496 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900497 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900498 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900499 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700500 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700501 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700502 public static final int TYPE_MOBILE_MMS = 2;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900503
Robert Greenwalt42acef32009-08-12 16:08:25 -0700504 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800505 * A SUPL-specific Mobile data connection. This network type may use the
506 * same network interface as {@link #TYPE_MOBILE} or it may use a different
507 * one. This is used by applications needing to talk to the carrier's
508 * Secure User Plane Location servers for help locating the device.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900509 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900510 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900511 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900512 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700513 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700514 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700515 public static final int TYPE_MOBILE_SUPL = 3;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900516
Robert Greenwalt42acef32009-08-12 16:08:25 -0700517 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800518 * A DUN-specific Mobile data connection. This network type may use the
519 * same network interface as {@link #TYPE_MOBILE} or it may use a different
520 * one. This is sometimes by the system when setting up an upstream connection
521 * for tethering so that the carrier is aware of DUN traffic.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900522 *
523 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
524 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
525 * provides the {@link NetworkCapabilities#NET_CAPABILITY_DUN} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700526 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900527 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700528 public static final int TYPE_MOBILE_DUN = 4;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900529
Robert Greenwalt42acef32009-08-12 16:08:25 -0700530 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800531 * A High Priority Mobile data connection. This network type uses the
532 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colittie285b432015-04-23 15:32:42 +0900533 * is different.
534 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900535 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
536 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
537 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700538 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700539 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700540 public static final int TYPE_MOBILE_HIPRI = 5;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900541
jsh8214deb2010-03-11 15:04:43 -0800542 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900543 * A WiMAX data connection.
544 *
545 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
546 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
547 * appropriate network. {@see NetworkCapabilities} for supported transports.
jsh8214deb2010-03-11 15:04:43 -0800548 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900549 @Deprecated
jsh8214deb2010-03-11 15:04:43 -0800550 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800551
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800552 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900553 * A Bluetooth data connection.
554 *
555 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
556 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
557 * appropriate network. {@see NetworkCapabilities} for supported transports.
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800558 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900559 @Deprecated
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800560 public static final int TYPE_BLUETOOTH = 7;
561
Robert Greenwalt60810842011-04-22 15:28:18 -0700562 /**
563 * Dummy data connection. This should not be used on shipping devices.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900564 * @deprecated This is not used any more.
Robert Greenwalt60810842011-04-22 15:28:18 -0700565 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900566 @Deprecated
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800567 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800568
Robert Greenwalt60810842011-04-22 15:28:18 -0700569 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900570 * An Ethernet data connection.
571 *
572 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
573 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
574 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt60810842011-04-22 15:28:18 -0700575 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900576 @Deprecated
Robert Greenwalte12aec92011-01-28 14:48:37 -0800577 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700578
Wink Saville9d7d6282011-03-12 14:52:01 -0800579 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800580 * Over the air Administration.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900581 * @deprecated Use {@link NetworkCapabilities} instead.
Wink Saville9d7d6282011-03-12 14:52:01 -0800582 * {@hide}
583 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900584 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100585 @UnsupportedAppUsage
Wink Saville9d7d6282011-03-12 14:52:01 -0800586 public static final int TYPE_MOBILE_FOTA = 10;
587
588 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800589 * IP Multimedia Subsystem.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900590 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IMS} instead.
Wink Saville9d7d6282011-03-12 14:52:01 -0800591 * {@hide}
592 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900593 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100594 @UnsupportedAppUsage
Wink Saville9d7d6282011-03-12 14:52:01 -0800595 public static final int TYPE_MOBILE_IMS = 11;
596
597 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800598 * Carrier Branded Services.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900599 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_CBS} instead.
Wink Saville9d7d6282011-03-12 14:52:01 -0800600 * {@hide}
601 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900602 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100603 @UnsupportedAppUsage
Wink Saville9d7d6282011-03-12 14:52:01 -0800604 public static final int TYPE_MOBILE_CBS = 12;
605
repo syncaea743a2011-07-29 23:55:49 -0700606 /**
607 * A Wi-Fi p2p connection. Only requesting processes will have access to
608 * the peers connected.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900609 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_WIFI_P2P} instead.
repo syncaea743a2011-07-29 23:55:49 -0700610 * {@hide}
611 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900612 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100613 @UnsupportedAppUsage
repo syncaea743a2011-07-29 23:55:49 -0700614 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800615
Wink Saville5e56bc52013-07-29 15:00:57 -0700616 /**
617 * The network to use for initially attaching to the network
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900618 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IA} instead.
Wink Saville5e56bc52013-07-29 15:00:57 -0700619 * {@hide}
620 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900621 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100622 @UnsupportedAppUsage
Wink Saville5e56bc52013-07-29 15:00:57 -0700623 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700624
Lorenzo Colittie285b432015-04-23 15:32:42 +0900625 /**
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700626 * Emergency PDN connection for emergency services. This
627 * may include IMS and MMS in emergency situations.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900628 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_EIMS} instead.
Ram3e0e3bc2014-06-26 11:03:44 -0700629 * {@hide}
630 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900631 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100632 @UnsupportedAppUsage
Ram3e0e3bc2014-06-26 11:03:44 -0700633 public static final int TYPE_MOBILE_EMERGENCY = 15;
634
Hui Lu1c5624a2014-01-15 11:05:36 -0500635 /**
636 * The network that uses proxy to achieve connectivity.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900637 * @deprecated Use {@link NetworkCapabilities} instead.
Hui Lu1c5624a2014-01-15 11:05:36 -0500638 * {@hide}
639 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900640 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100641 @UnsupportedAppUsage
Hui Lu1c5624a2014-01-15 11:05:36 -0500642 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700643
Robert Greenwalt8283f882014-07-07 17:09:01 -0700644 /**
645 * A virtual network using one or more native bearers.
646 * It may or may not be providing security services.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900647 * @deprecated Applications should use {@link NetworkCapabilities#TRANSPORT_VPN} instead.
Robert Greenwalt8283f882014-07-07 17:09:01 -0700648 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900649 @Deprecated
Robert Greenwalt8283f882014-07-07 17:09:01 -0700650 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500651
652 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700653 public static final int MAX_RADIO_TYPE = TYPE_VPN;
654
655 /** {@hide} */
656 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657
Hugo Benichi16f0a942017-06-20 14:07:59 +0900658 private static final int MIN_NETWORK_TYPE = TYPE_MOBILE;
659
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800660 /**
661 * If you want to set the default network preference,you can directly
662 * change the networkAttributes array in framework's config.xml.
663 *
664 * @deprecated Since we support so many more networks now, the single
665 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800666 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800667 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800668 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800669 * from an App.
670 */
671 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800672 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
673
Jeff Sharkey625239a2012-09-26 22:03:49 -0700674 /**
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700675 * @hide
676 */
Hugo Benichia5c1f7f2017-06-20 14:10:14 +0900677 public static final int REQUEST_ID_UNSET = 0;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700678
Paul Jensen5d59e782014-07-11 12:28:19 -0400679 /**
Hugo Benichi31c176d2017-06-17 13:14:12 +0900680 * Static unique request used as a tombstone for NetworkCallbacks that have been unregistered.
681 * This allows to distinguish when unregistering NetworkCallbacks those that were never
Chalard Jean4d660112018-06-04 16:52:49 +0900682 * registered from those that were already unregistered.
Hugo Benichi31c176d2017-06-17 13:14:12 +0900683 * @hide
684 */
Hugo Benichia5c1f7f2017-06-20 14:10:14 +0900685 private static final NetworkRequest ALREADY_UNREGISTERED =
Hugo Benichi31c176d2017-06-17 13:14:12 +0900686 new NetworkRequest.Builder().clearCapabilities().build();
687
688 /**
Paul Jensen5d59e782014-07-11 12:28:19 -0400689 * A NetID indicating no Network is selected.
690 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
691 * @hide
692 */
693 public static final int NETID_UNSET = 0;
694
Erik Kline4d092232017-10-30 15:29:44 +0900695 /**
696 * Private DNS Mode values.
697 *
698 * The "private_dns_mode" global setting stores a String value which is
699 * expected to be one of the following.
700 */
701
702 /**
703 * @hide
704 */
705 public static final String PRIVATE_DNS_MODE_OFF = "off";
706 /**
707 * @hide
708 */
709 public static final String PRIVATE_DNS_MODE_OPPORTUNISTIC = "opportunistic";
710 /**
711 * @hide
712 */
713 public static final String PRIVATE_DNS_MODE_PROVIDER_HOSTNAME = "hostname";
714 /**
715 * The default Private DNS mode.
716 *
717 * This may change from release to release or may become dependent upon
718 * the capabilities of the underlying platform.
719 *
720 * @hide
721 */
Erik Kline19841792018-05-16 16:41:57 +0900722 public static final String PRIVATE_DNS_DEFAULT_MODE_FALLBACK = PRIVATE_DNS_MODE_OPPORTUNISTIC;
Erik Kline4d092232017-10-30 15:29:44 +0900723
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100724 @UnsupportedAppUsage
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700725 private final IConnectivityManager mService;
Paul Jensene0bef712014-12-10 15:12:18 -0500726 /**
727 * A kludge to facilitate static access where a Context pointer isn't available, like in the
728 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
729 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
730 * methods that take a Context argument.
731 */
732 private static ConnectivityManager sInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800733
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900734 private final Context mContext;
735
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800736 private INetworkManagementService mNMService;
Felipe Leme1b103232016-01-22 09:44:57 -0800737 private INetworkPolicyManager mNPManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800738
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800739 /**
740 * Tests if a given integer represents a valid network type.
741 * @param networkType the type to be tested
742 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensen9e59e122015-05-06 10:42:25 -0400743 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
744 * validate a network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800745 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700746 @Deprecated
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700747 public static boolean isNetworkTypeValid(int networkType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900748 return MIN_NETWORK_TYPE <= networkType && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800749 }
750
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800751 /**
752 * Returns a non-localized string representing a given network type.
753 * ONLY used for debugging output.
754 * @param type the type needing naming
755 * @return a String for the given type, or a string version of the type ("87")
756 * if no name is known.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900757 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800758 * {@hide}
759 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900760 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100761 @UnsupportedAppUsage
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700762 public static String getNetworkTypeName(int type) {
763 switch (type) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900764 case TYPE_NONE:
765 return "NONE";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700766 case TYPE_MOBILE:
767 return "MOBILE";
768 case TYPE_WIFI:
769 return "WIFI";
770 case TYPE_MOBILE_MMS:
771 return "MOBILE_MMS";
772 case TYPE_MOBILE_SUPL:
773 return "MOBILE_SUPL";
774 case TYPE_MOBILE_DUN:
775 return "MOBILE_DUN";
776 case TYPE_MOBILE_HIPRI:
777 return "MOBILE_HIPRI";
778 case TYPE_WIMAX:
779 return "WIMAX";
780 case TYPE_BLUETOOTH:
781 return "BLUETOOTH";
782 case TYPE_DUMMY:
783 return "DUMMY";
784 case TYPE_ETHERNET:
785 return "ETHERNET";
786 case TYPE_MOBILE_FOTA:
787 return "MOBILE_FOTA";
788 case TYPE_MOBILE_IMS:
789 return "MOBILE_IMS";
790 case TYPE_MOBILE_CBS:
791 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700792 case TYPE_WIFI_P2P:
793 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700794 case TYPE_MOBILE_IA:
795 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700796 case TYPE_MOBILE_EMERGENCY:
797 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500798 case TYPE_PROXY:
799 return "PROXY";
Erik Kline37fbfa12014-11-19 17:23:41 +0900800 case TYPE_VPN:
801 return "VPN";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700802 default:
803 return Integer.toString(type);
804 }
805 }
806
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800807 /**
808 * Checks if a given type uses the cellular data connection.
809 * This should be replaced in the future by a network property.
810 * @param networkType the type to check
811 * @return a boolean - {@code true} if uses cellular network, else {@code false}
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900812 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800813 * {@hide}
814 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900815 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100816 @UnsupportedAppUsage
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700817 public static boolean isNetworkTypeMobile(int networkType) {
818 switch (networkType) {
819 case TYPE_MOBILE:
820 case TYPE_MOBILE_MMS:
821 case TYPE_MOBILE_SUPL:
822 case TYPE_MOBILE_DUN:
823 case TYPE_MOBILE_HIPRI:
824 case TYPE_MOBILE_FOTA:
825 case TYPE_MOBILE_IMS:
826 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700827 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700828 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700829 return true;
830 default:
831 return false;
832 }
833 }
834
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800835 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700836 * Checks if the given network type is backed by a Wi-Fi radio.
837 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900838 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700839 * @hide
840 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900841 @Deprecated
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700842 public static boolean isNetworkTypeWifi(int networkType) {
843 switch (networkType) {
844 case TYPE_WIFI:
845 case TYPE_WIFI_P2P:
846 return true;
847 default:
848 return false;
849 }
850 }
851
852 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800853 * Specifies the preferred network type. When the device has more
854 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800855 *
856 * @param preference the network type to prefer over all others. It is
857 * unspecified what happens to the old preferred network in the
858 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700859 * @deprecated Functionality has been removed as it no longer makes sense,
860 * with many more than two networks - we'd need an array to express
861 * preference. Instead we use dynamic network properties of
862 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800863 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700864 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800866 }
867
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800868 /**
869 * Retrieves the current preferred network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800870 *
871 * @return an integer representing the preferred network type
872 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700873 * @deprecated Functionality has been removed as it no longer makes sense,
874 * with many more than two networks - we'd need an array to express
875 * preference. Instead we use dynamic network properties of
876 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800877 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700878 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600879 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800880 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700881 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 }
883
Scott Main671644c2011-10-06 19:02:28 -0700884 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800885 * Returns details about the currently active default data network. When
886 * connected, this network is the default route for outgoing connections.
887 * You should always check {@link NetworkInfo#isConnected()} before initiating
888 * network traffic. This may return {@code null} when there is no default
889 * network.
Chalard Jean5a041d12018-03-29 17:45:24 +0900890 * Note that if the default network is a VPN, this method will return the
891 * NetworkInfo for one of its underlying networks instead, or null if the
892 * VPN agent did not specify any. Apps interested in learning about VPNs
893 * should use {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800894 *
895 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500896 * or {@code null} if no default network is currently active
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700897 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600898 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800899 public NetworkInfo getActiveNetworkInfo() {
900 try {
901 return mService.getActiveNetworkInfo();
902 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700903 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 }
905 }
906
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800907 /**
Paul Jensen31a94f42015-02-13 14:18:39 -0500908 * Returns a {@link Network} object corresponding to the currently active
909 * default data network. In the event that the current active default data
910 * network disconnects, the returned {@code Network} object will no longer
911 * be usable. This will return {@code null} when there is no default
912 * network.
913 *
914 * @return a {@link Network} object for the current default network or
915 * {@code null} if no default network is currently active
Paul Jensen31a94f42015-02-13 14:18:39 -0500916 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600917 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Paul Jensen31a94f42015-02-13 14:18:39 -0500918 public Network getActiveNetwork() {
919 try {
920 return mService.getActiveNetwork();
921 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700922 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -0500923 }
924 }
925
926 /**
Robin Leed2baf792016-03-24 12:07:00 +0000927 * Returns a {@link Network} object corresponding to the currently active
928 * default data network for a specific UID. In the event that the default data
929 * network disconnects, the returned {@code Network} object will no longer
930 * be usable. This will return {@code null} when there is no default
931 * network for the UID.
Robin Leed2baf792016-03-24 12:07:00 +0000932 *
933 * @return a {@link Network} object for the current default network for the
934 * given UID or {@code null} if no default network is currently active
935 *
936 * @hide
937 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600938 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Robin Leed2baf792016-03-24 12:07:00 +0000939 public Network getActiveNetworkForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600940 return getActiveNetworkForUid(uid, false);
941 }
942
943 /** {@hide} */
944 public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) {
Robin Leed2baf792016-03-24 12:07:00 +0000945 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600946 return mService.getActiveNetworkForUid(uid, ignoreBlocked);
Robin Leed2baf792016-03-24 12:07:00 +0000947 } catch (RemoteException e) {
948 throw e.rethrowFromSystemServer();
949 }
950 }
951
952 /**
Charles He36738632017-05-15 17:07:18 +0100953 * Checks if a VPN app supports always-on mode.
954 *
955 * In order to support the always-on feature, an app has to
956 * <ul>
957 * <li>target {@link VERSION_CODES#N API 24} or above, and
Charles Hec57a01c2017-08-15 15:30:22 +0100958 * <li>not opt out through the {@link VpnService#SERVICE_META_DATA_SUPPORTS_ALWAYS_ON}
959 * meta-data field.
Charles He36738632017-05-15 17:07:18 +0100960 * </ul>
961 *
962 * @param userId The identifier of the user for whom the VPN app is installed.
963 * @param vpnPackage The canonical package name of the VPN app.
964 * @return {@code true} if and only if the VPN app exists and supports always-on mode.
965 * @hide
966 */
967 public boolean isAlwaysOnVpnPackageSupportedForUser(int userId, @Nullable String vpnPackage) {
968 try {
969 return mService.isAlwaysOnVpnPackageSupported(userId, vpnPackage);
970 } catch (RemoteException e) {
971 throw e.rethrowFromSystemServer();
972 }
973 }
974
975 /**
Robin Lee244ce8e2016-01-05 18:03:46 +0000976 * Configures an always-on VPN connection through a specific application.
977 * This connection is automatically granted and persisted after a reboot.
978 *
979 * <p>The designated package should declare a {@link VpnService} in its
980 * manifest guarded by {@link android.Manifest.permission.BIND_VPN_SERVICE},
981 * otherwise the call will fail.
982 *
983 * @param userId The identifier of the user to set an always-on VPN for.
984 * @param vpnPackage The package name for an installed VPN app on the device, or {@code null}
985 * to remove an existing always-on VPN configuration.
Robin Leedc679712016-05-03 13:23:03 +0100986 * @param lockdownEnabled {@code true} to disallow networking when the VPN is not connected or
987 * {@code false} otherwise.
Robin Lee244ce8e2016-01-05 18:03:46 +0000988 * @return {@code true} if the package is set as always-on VPN controller;
989 * {@code false} otherwise.
990 * @hide
991 */
Robin Leedc679712016-05-03 13:23:03 +0100992 public boolean setAlwaysOnVpnPackageForUser(int userId, @Nullable String vpnPackage,
993 boolean lockdownEnabled) {
Robin Lee244ce8e2016-01-05 18:03:46 +0000994 try {
Robin Leedc679712016-05-03 13:23:03 +0100995 return mService.setAlwaysOnVpnPackage(userId, vpnPackage, lockdownEnabled);
Robin Lee244ce8e2016-01-05 18:03:46 +0000996 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700997 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +0000998 }
999 }
1000
1001 /**
1002 * Returns the package name of the currently set always-on VPN application.
1003 * If there is no always-on VPN set, or the VPN is provided by the system instead
1004 * of by an app, {@code null} will be returned.
1005 *
1006 * @return Package name of VPN controller responsible for always-on VPN,
1007 * or {@code null} if none is set.
1008 * @hide
1009 */
1010 public String getAlwaysOnVpnPackageForUser(int userId) {
1011 try {
1012 return mService.getAlwaysOnVpnPackage(userId);
1013 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001014 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +00001015 }
1016 }
1017
1018 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001019 * Returns details about the currently active default data network
1020 * for a given uid. This is for internal use only to avoid spying
1021 * other apps.
1022 *
1023 * @return a {@link NetworkInfo} object for the current default network
1024 * for the given uid or {@code null} if no default network is
1025 * available for the specified uid.
1026 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001027 * {@hide}
1028 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001029 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001030 @UnsupportedAppUsage
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001031 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001032 return getActiveNetworkInfoForUid(uid, false);
1033 }
1034
1035 /** {@hide} */
1036 public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001037 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001038 return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001039 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001040 throw e.rethrowFromSystemServer();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001041 }
1042 }
1043
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001044 /**
1045 * Returns connection status information about a particular
1046 * network type.
1047 *
1048 * @param networkType integer specifying which networkType in
1049 * which you're interested.
1050 * @return a {@link NetworkInfo} object for the requested
1051 * network type or {@code null} if the type is not
Chalard Jean5a041d12018-03-29 17:45:24 +09001052 * supported by the device. If {@code networkType} is
1053 * TYPE_VPN and a VPN is active for the calling app,
1054 * then this method will try to return one of the
1055 * underlying networks for the VPN or null if the
1056 * VPN agent didn't specify any.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001057 *
Paul Jensen3541e9f2015-03-18 12:23:02 -04001058 * @deprecated This method does not support multiple connected networks
1059 * of the same type. Use {@link #getAllNetworks} and
1060 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001061 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001062 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001063 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001064 public NetworkInfo getNetworkInfo(int networkType) {
1065 try {
1066 return mService.getNetworkInfo(networkType);
1067 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001068 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001069 }
1070 }
1071
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001072 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001073 * Returns connection status information about a particular
1074 * Network.
1075 *
1076 * @param network {@link Network} specifying which network
1077 * in which you're interested.
1078 * @return a {@link NetworkInfo} object for the requested
1079 * network or {@code null} if the {@code Network}
1080 * is not valid.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001081 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001082 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001083 public NetworkInfo getNetworkInfo(Network network) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001084 return getNetworkInfoForUid(network, Process.myUid(), false);
1085 }
1086
1087 /** {@hide} */
1088 public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001089 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001090 return mService.getNetworkInfoForUid(network, uid, ignoreBlocked);
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001091 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001092 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001093 }
1094 }
1095
1096 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001097 * Returns connection status information about all network
1098 * types supported by the device.
1099 *
1100 * @return an array of {@link NetworkInfo} objects. Check each
1101 * {@link NetworkInfo#getType} for which type each applies.
1102 *
Paul Jensen3541e9f2015-03-18 12:23:02 -04001103 * @deprecated This method does not support multiple connected networks
1104 * of the same type. Use {@link #getAllNetworks} and
1105 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001106 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001107 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001108 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001109 public NetworkInfo[] getAllNetworkInfo() {
1110 try {
1111 return mService.getAllNetworkInfo();
1112 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001113 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001114 }
1115 }
1116
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001117 /**
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001118 * Returns the {@link Network} object currently serving a given type, or
1119 * null if the given type is not connected.
1120 *
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001121 * @hide
Paul Jensen3541e9f2015-03-18 12:23:02 -04001122 * @deprecated This method does not support multiple connected networks
1123 * of the same type. Use {@link #getAllNetworks} and
1124 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001125 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001126 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001127 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001128 @UnsupportedAppUsage
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001129 public Network getNetworkForType(int networkType) {
1130 try {
1131 return mService.getNetworkForType(networkType);
1132 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001133 throw e.rethrowFromSystemServer();
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001134 }
1135 }
1136
1137 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001138 * Returns an array of all {@link Network} currently tracked by the
1139 * framework.
Paul Jensenb2748922015-05-06 11:10:18 -04001140 *
1141 * @return an array of {@link Network} objects.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001142 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001143 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001144 public Network[] getAllNetworks() {
1145 try {
1146 return mService.getAllNetworks();
1147 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001148 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001149 }
1150 }
1151
1152 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001153 * Returns an array of {@link android.net.NetworkCapabilities} objects, representing
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001154 * the Networks that applications run by the given user will use by default.
1155 * @hide
1156 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001157 @UnsupportedAppUsage
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001158 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
1159 try {
1160 return mService.getDefaultNetworkCapabilitiesForUser(userId);
1161 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001162 throw e.rethrowFromSystemServer();
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001163 }
1164 }
1165
1166 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001167 * Returns the IP information for the current default network.
1168 *
1169 * @return a {@link LinkProperties} object describing the IP info
1170 * for the current default network, or {@code null} if there
1171 * is no current default network.
1172 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001173 * {@hide}
1174 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001175 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001176 @UnsupportedAppUsage
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001177 public LinkProperties getActiveLinkProperties() {
1178 try {
1179 return mService.getActiveLinkProperties();
1180 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001181 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001182 }
1183 }
1184
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001185 /**
1186 * Returns the IP information for a given network type.
1187 *
1188 * @param networkType the network type of interest.
1189 * @return a {@link LinkProperties} object describing the IP info
1190 * for the given networkType, or {@code null} if there is
1191 * no current default network.
1192 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001193 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04001194 * @deprecated This method does not support multiple connected networks
1195 * of the same type. Use {@link #getAllNetworks},
1196 * {@link #getNetworkInfo(android.net.Network)}, and
1197 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001198 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001199 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001200 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001201 @UnsupportedAppUsage
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001202 public LinkProperties getLinkProperties(int networkType) {
1203 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001204 return mService.getLinkPropertiesForType(networkType);
1205 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001206 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001207 }
1208 }
1209
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001210 /**
1211 * Get the {@link LinkProperties} for the given {@link Network}. This
1212 * will return {@code null} if the network is unknown.
1213 *
1214 * @param network The {@link Network} object identifying the network in question.
1215 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensenb2748922015-05-06 11:10:18 -04001216 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001217 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt9258c642014-03-26 16:47:06 -07001218 public LinkProperties getLinkProperties(Network network) {
1219 try {
1220 return mService.getLinkProperties(network);
1221 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001222 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001223 }
1224 }
1225
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001226 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001227 * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001228 * will return {@code null} if the network is unknown.
1229 *
1230 * @param network The {@link Network} object identifying the network in question.
Lorenzo Colittie285b432015-04-23 15:32:42 +09001231 * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001232 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001233 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt9258c642014-03-26 16:47:06 -07001234 public NetworkCapabilities getNetworkCapabilities(Network network) {
1235 try {
1236 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001237 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001238 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001239 }
1240 }
1241
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001242 /**
Udam Sainib7c24872016-01-04 12:16:14 -08001243 * Gets the URL that should be used for resolving whether a captive portal is present.
1244 * 1. This URL should respond with a 204 response to a GET request to indicate no captive
1245 * portal is present.
1246 * 2. This URL must be HTTP as redirect responses are used to find captive portal
1247 * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects.
1248 *
1249 * @hide
1250 */
1251 @SystemApi
Udam Saini0e94c362017-06-07 12:06:28 -07001252 @RequiresPermission(android.Manifest.permission.LOCAL_MAC_ADDRESS)
Udam Sainib7c24872016-01-04 12:16:14 -08001253 public String getCaptivePortalServerUrl() {
1254 try {
1255 return mService.getCaptivePortalServerUrl();
1256 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001257 throw e.rethrowFromSystemServer();
Udam Sainib7c24872016-01-04 12:16:14 -08001258 }
1259 }
1260
1261 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 * Tells the underlying networking system that the caller wants to
1263 * begin using the named feature. The interpretation of {@code feature}
1264 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001265 *
1266 * <p>This method requires the caller to hold either the
1267 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1268 * or the ability to modify system settings as determined by
1269 * {@link android.provider.Settings.System#canWrite}.</p>
1270 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001271 * @param networkType specifies which network the request pertains to
1272 * @param feature the name of the feature to be used
1273 * @return an integer value representing the outcome of the request.
1274 * The interpretation of this value is specific to each networking
1275 * implementation+feature combination, except that the value {@code -1}
1276 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001277 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001278 * @deprecated Deprecated in favor of the cleaner
1279 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001280 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001281 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001282 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001283 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001284 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001286 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001287 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1288 if (netCap == null) {
1289 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
1290 feature);
1291 return PhoneConstants.APN_REQUEST_FAILED;
1292 }
1293
1294 NetworkRequest request = null;
1295 synchronized (sLegacyRequests) {
1296 LegacyRequest l = sLegacyRequests.get(netCap);
1297 if (l != null) {
1298 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
1299 renewRequestLocked(l);
1300 if (l.currentNetwork != null) {
1301 return PhoneConstants.APN_ALREADY_ACTIVE;
1302 } else {
1303 return PhoneConstants.APN_REQUEST_STARTED;
1304 }
1305 }
1306
1307 request = requestNetworkForFeatureLocked(netCap);
1308 }
1309 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -07001310 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001311 return PhoneConstants.APN_REQUEST_STARTED;
1312 } else {
1313 Log.d(TAG, " request Failed");
1314 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001315 }
1316 }
1317
1318 /**
1319 * Tells the underlying networking system that the caller is finished
1320 * using the named feature. The interpretation of {@code feature}
1321 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001322 *
1323 * <p>This method requires the caller to hold either the
1324 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1325 * or the ability to modify system settings as determined by
1326 * {@link android.provider.Settings.System#canWrite}.</p>
1327 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001328 * @param networkType specifies which network the request pertains to
1329 * @param feature the name of the feature that is no longer needed
1330 * @return an integer value representing the outcome of the request.
1331 * The interpretation of this value is specific to each networking
1332 * implementation+feature combination, except that the value {@code -1}
1333 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001334 *
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09001335 * @deprecated Deprecated in favor of the cleaner
1336 * {@link #unregisterNetworkCallback(NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001337 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001338 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001339 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001340 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001341 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001343 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001344 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1345 if (netCap == null) {
1346 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
1347 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001348 return -1;
1349 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001350
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001351 if (removeRequestForFeature(netCap)) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001352 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001353 }
1354 return 1;
1355 }
1356
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001357 @UnsupportedAppUsage
Robert Greenwalt562cc542014-05-15 18:07:26 -07001358 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1359 if (networkType == TYPE_MOBILE) {
Erik Kline35bf06c2017-01-26 18:08:28 +09001360 switch (feature) {
1361 case "enableCBS":
1362 return networkCapabilitiesForType(TYPE_MOBILE_CBS);
1363 case "enableDUN":
1364 case "enableDUNAlways":
1365 return networkCapabilitiesForType(TYPE_MOBILE_DUN);
1366 case "enableFOTA":
1367 return networkCapabilitiesForType(TYPE_MOBILE_FOTA);
1368 case "enableHIPRI":
1369 return networkCapabilitiesForType(TYPE_MOBILE_HIPRI);
1370 case "enableIMS":
1371 return networkCapabilitiesForType(TYPE_MOBILE_IMS);
1372 case "enableMMS":
1373 return networkCapabilitiesForType(TYPE_MOBILE_MMS);
1374 case "enableSUPL":
1375 return networkCapabilitiesForType(TYPE_MOBILE_SUPL);
1376 default:
1377 return null;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001378 }
Erik Kline35bf06c2017-01-26 18:08:28 +09001379 } else if (networkType == TYPE_WIFI && "p2p".equals(feature)) {
1380 return networkCapabilitiesForType(TYPE_WIFI_P2P);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001381 }
1382 return null;
1383 }
1384
Robert Greenwalt06314e42014-10-29 14:04:06 -07001385 /**
1386 * Guess what the network request was trying to say so that the resulting
1387 * network is accessible via the legacy (deprecated) API such as
1388 * requestRouteToHost.
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001389 *
1390 * This means we should try to be fairly precise about transport and
Robert Greenwalt06314e42014-10-29 14:04:06 -07001391 * capability but ignore things such as networkSpecifier.
1392 * If the request has more than one transport or capability it doesn't
1393 * match the old legacy requests (they selected only single transport/capability)
1394 * so this function cannot map the request to a single legacy type and
1395 * the resulting network will not be available to the legacy APIs.
1396 *
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001397 * This code is only called from the requestNetwork API (L and above).
1398 *
1399 * Setting a legacy type causes CONNECTIVITY_ACTION broadcasts, which are expensive
1400 * because they wake up lots of apps - see http://b/23350688 . So we currently only
1401 * do this for SUPL requests, which are the only ones that we know need it. If
1402 * omitting these broadcasts causes unacceptable app breakage, then for backwards
1403 * compatibility we can send them:
1404 *
1405 * if (targetSdkVersion < Build.VERSION_CODES.M) && // legacy API unsupported >= M
1406 * targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP)) // requestNetwork not present < L
1407 *
Robert Greenwalt06314e42014-10-29 14:04:06 -07001408 * TODO - This should be removed when the legacy APIs are removed.
1409 */
Ye Wenb87875e2014-07-21 14:19:01 -07001410 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1411 if (netCap == null) {
1412 return TYPE_NONE;
1413 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001414
Ye Wenb87875e2014-07-21 14:19:01 -07001415 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1416 return TYPE_NONE;
1417 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001418
Lifu Tang30f95a72016-01-07 23:20:38 -08001419 // Do this only for SUPL, until GnssLocationProvider is fixed. http://b/25876485 .
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001420 if (!netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1421 // NOTE: if this causes app breakage, we should not just comment out this early return;
1422 // instead, we should make this early return conditional on the requesting app's target
1423 // SDK version, as described in the comment above.
1424 return TYPE_NONE;
1425 }
1426
Robert Greenwalt06314e42014-10-29 14:04:06 -07001427 String type = null;
1428 int result = TYPE_NONE;
1429
Ye Wenb87875e2014-07-21 14:19:01 -07001430 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
Robert Greenwalt06314e42014-10-29 14:04:06 -07001431 type = "enableCBS";
1432 result = TYPE_MOBILE_CBS;
1433 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1434 type = "enableIMS";
1435 result = TYPE_MOBILE_IMS;
1436 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1437 type = "enableFOTA";
1438 result = TYPE_MOBILE_FOTA;
1439 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1440 type = "enableDUN";
1441 result = TYPE_MOBILE_DUN;
1442 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001443 type = "enableSUPL";
Robert Greenwalt06314e42014-10-29 14:04:06 -07001444 result = TYPE_MOBILE_SUPL;
Robert Greenwalt74ab4fa2015-08-28 12:37:54 -07001445 // back out this hack for mms as they no longer need this and it's causing
1446 // device slowdowns - b/23350688 (note, supl still needs this)
1447 //} else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1448 // type = "enableMMS";
1449 // result = TYPE_MOBILE_MMS;
Robert Greenwalt06314e42014-10-29 14:04:06 -07001450 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1451 type = "enableHIPRI";
1452 result = TYPE_MOBILE_HIPRI;
Ye Wenb87875e2014-07-21 14:19:01 -07001453 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001454 if (type != null) {
1455 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1456 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1457 return result;
Ye Wenb87875e2014-07-21 14:19:01 -07001458 }
1459 }
1460 return TYPE_NONE;
1461 }
1462
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001463 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001464 if (netCap == null) return TYPE_NONE;
1465 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1466 return TYPE_MOBILE_CBS;
1467 }
1468 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1469 return TYPE_MOBILE_IMS;
1470 }
1471 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1472 return TYPE_MOBILE_FOTA;
1473 }
1474 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1475 return TYPE_MOBILE_DUN;
1476 }
1477 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1478 return TYPE_MOBILE_SUPL;
1479 }
1480 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1481 return TYPE_MOBILE_MMS;
1482 }
1483 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1484 return TYPE_MOBILE_HIPRI;
1485 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001486 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1487 return TYPE_WIFI_P2P;
1488 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001489 return TYPE_NONE;
1490 }
1491
1492 private static class LegacyRequest {
1493 NetworkCapabilities networkCapabilities;
1494 NetworkRequest networkRequest;
1495 int expireSequenceNumber;
1496 Network currentNetwork;
1497 int delay = -1;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001498
1499 private void clearDnsBinding() {
1500 if (currentNetwork != null) {
1501 currentNetwork = null;
1502 setProcessDefaultNetworkForHostResolution(null);
1503 }
1504 }
1505
Robert Greenwalt6078b502014-06-11 16:05:07 -07001506 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001507 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001508 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001509 currentNetwork = network;
1510 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001511 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001512 }
1513 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001514 public void onLost(Network network) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001515 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001516 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1517 }
1518 };
1519 }
1520
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001521 @UnsupportedAppUsage
Chalard Jean4d660112018-06-04 16:52:49 +09001522 private static final HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
1523 new HashMap<>();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001524
1525 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1526 synchronized (sLegacyRequests) {
1527 LegacyRequest l = sLegacyRequests.get(netCap);
1528 if (l != null) return l.networkRequest;
1529 }
1530 return null;
1531 }
1532
1533 private void renewRequestLocked(LegacyRequest l) {
1534 l.expireSequenceNumber++;
1535 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1536 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1537 }
1538
1539 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1540 int ourSeqNum = -1;
1541 synchronized (sLegacyRequests) {
1542 LegacyRequest l = sLegacyRequests.get(netCap);
1543 if (l == null) return;
1544 ourSeqNum = l.expireSequenceNumber;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001545 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001546 }
1547 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1548 }
1549
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001550 @UnsupportedAppUsage
Robert Greenwalt562cc542014-05-15 18:07:26 -07001551 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1552 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001553 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001554 try {
1555 delay = mService.getRestoreDefaultNetworkDelay(type);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001556 } catch (RemoteException e) {
1557 throw e.rethrowFromSystemServer();
1558 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001559 LegacyRequest l = new LegacyRequest();
1560 l.networkCapabilities = netCap;
1561 l.delay = delay;
1562 l.expireSequenceNumber = 0;
Hugo Benichi2583ef02017-02-02 17:02:36 +09001563 l.networkRequest = sendRequestForNetwork(
1564 netCap, l.networkCallback, 0, REQUEST, type, getDefaultHandler());
Robert Greenwalt562cc542014-05-15 18:07:26 -07001565 if (l.networkRequest == null) return null;
1566 sLegacyRequests.put(netCap, l);
1567 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1568 return l.networkRequest;
1569 }
1570
1571 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1572 if (delay >= 0) {
1573 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
Hugo Benichi2583ef02017-02-02 17:02:36 +09001574 CallbackHandler handler = getDefaultHandler();
Hugo Benichi6f260f32017-02-03 14:18:44 +09001575 Message msg = handler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1576 handler.sendMessageDelayed(msg, delay);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001577 }
1578 }
1579
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001580 @UnsupportedAppUsage
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001581 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1582 final LegacyRequest l;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001583 synchronized (sLegacyRequests) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001584 l = sLegacyRequests.remove(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001585 }
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001586 if (l == null) return false;
1587 unregisterNetworkCallback(l.networkCallback);
1588 l.clearDnsBinding();
1589 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 }
1591
Erik Kline35bf06c2017-01-26 18:08:28 +09001592 private static final SparseIntArray sLegacyTypeToTransport = new SparseIntArray();
1593 static {
1594 sLegacyTypeToTransport.put(TYPE_MOBILE, NetworkCapabilities.TRANSPORT_CELLULAR);
1595 sLegacyTypeToTransport.put(TYPE_MOBILE_CBS, NetworkCapabilities.TRANSPORT_CELLULAR);
1596 sLegacyTypeToTransport.put(TYPE_MOBILE_DUN, NetworkCapabilities.TRANSPORT_CELLULAR);
1597 sLegacyTypeToTransport.put(TYPE_MOBILE_FOTA, NetworkCapabilities.TRANSPORT_CELLULAR);
1598 sLegacyTypeToTransport.put(TYPE_MOBILE_HIPRI, NetworkCapabilities.TRANSPORT_CELLULAR);
1599 sLegacyTypeToTransport.put(TYPE_MOBILE_IMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1600 sLegacyTypeToTransport.put(TYPE_MOBILE_MMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1601 sLegacyTypeToTransport.put(TYPE_MOBILE_SUPL, NetworkCapabilities.TRANSPORT_CELLULAR);
1602 sLegacyTypeToTransport.put(TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI);
1603 sLegacyTypeToTransport.put(TYPE_WIFI_P2P, NetworkCapabilities.TRANSPORT_WIFI);
1604 sLegacyTypeToTransport.put(TYPE_BLUETOOTH, NetworkCapabilities.TRANSPORT_BLUETOOTH);
1605 sLegacyTypeToTransport.put(TYPE_ETHERNET, NetworkCapabilities.TRANSPORT_ETHERNET);
1606 }
1607
1608 private static final SparseIntArray sLegacyTypeToCapability = new SparseIntArray();
1609 static {
1610 sLegacyTypeToCapability.put(TYPE_MOBILE_CBS, NetworkCapabilities.NET_CAPABILITY_CBS);
1611 sLegacyTypeToCapability.put(TYPE_MOBILE_DUN, NetworkCapabilities.NET_CAPABILITY_DUN);
1612 sLegacyTypeToCapability.put(TYPE_MOBILE_FOTA, NetworkCapabilities.NET_CAPABILITY_FOTA);
1613 sLegacyTypeToCapability.put(TYPE_MOBILE_IMS, NetworkCapabilities.NET_CAPABILITY_IMS);
1614 sLegacyTypeToCapability.put(TYPE_MOBILE_MMS, NetworkCapabilities.NET_CAPABILITY_MMS);
1615 sLegacyTypeToCapability.put(TYPE_MOBILE_SUPL, NetworkCapabilities.NET_CAPABILITY_SUPL);
1616 sLegacyTypeToCapability.put(TYPE_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
1617 }
1618
1619 /**
1620 * Given a legacy type (TYPE_WIFI, ...) returns a NetworkCapabilities
1621 * instance suitable for registering a request or callback. Throws an
1622 * IllegalArgumentException if no mapping from the legacy type to
1623 * NetworkCapabilities is known.
1624 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +09001625 * @deprecated Types are deprecated. Use {@link NetworkCallback} or {@link NetworkRequest}
1626 * to find the network instead.
Erik Kline35bf06c2017-01-26 18:08:28 +09001627 * @hide
1628 */
1629 public static NetworkCapabilities networkCapabilitiesForType(int type) {
1630 final NetworkCapabilities nc = new NetworkCapabilities();
1631
1632 // Map from type to transports.
1633 final int NOT_FOUND = -1;
1634 final int transport = sLegacyTypeToTransport.get(type, NOT_FOUND);
Hugo Benichie7678512017-05-09 15:19:01 +09001635 Preconditions.checkArgument(transport != NOT_FOUND, "unknown legacy type: " + type);
Erik Kline35bf06c2017-01-26 18:08:28 +09001636 nc.addTransportType(transport);
1637
1638 // Map from type to capabilities.
1639 nc.addCapability(sLegacyTypeToCapability.get(
1640 type, NetworkCapabilities.NET_CAPABILITY_INTERNET));
1641 nc.maybeMarkCapabilitiesRestricted();
1642 return nc;
1643 }
1644
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001645 /** @hide */
1646 public static class PacketKeepaliveCallback {
1647 /** The requested keepalive was successfully started. */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001648 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001649 public void onStarted() {}
1650 /** The keepalive was successfully stopped. */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001651 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001652 public void onStopped() {}
1653 /** An error occurred. */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001654 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001655 public void onError(int error) {}
1656 }
1657
1658 /**
1659 * Allows applications to request that the system periodically send specific packets on their
1660 * behalf, using hardware offload to save battery power.
1661 *
1662 * To request that the system send keepalives, call one of the methods that return a
1663 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1664 * passing in a non-null callback. If the callback is successfully started, the callback's
1665 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1666 * specifying one of the {@code ERROR_*} constants in this class.
1667 *
Chalard Jean4d660112018-06-04 16:52:49 +09001668 * To stop an existing keepalive, call {@link PacketKeepalive#stop}. The system will call
1669 * {@link PacketKeepaliveCallback#onStopped} if the operation was successful or
1670 * {@link PacketKeepaliveCallback#onError} if an error occurred.
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001671 *
1672 * @hide
1673 */
1674 public class PacketKeepalive {
1675
1676 private static final String TAG = "PacketKeepalive";
1677
1678 /** @hide */
1679 public static final int SUCCESS = 0;
1680
1681 /** @hide */
1682 public static final int NO_KEEPALIVE = -1;
1683
1684 /** @hide */
1685 public static final int BINDER_DIED = -10;
1686
1687 /** The specified {@code Network} is not connected. */
1688 public static final int ERROR_INVALID_NETWORK = -20;
1689 /** The specified IP addresses are invalid. For example, the specified source IP address is
1690 * not configured on the specified {@code Network}. */
1691 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1692 /** The requested port is invalid. */
1693 public static final int ERROR_INVALID_PORT = -22;
1694 /** The packet length is invalid (e.g., too long). */
1695 public static final int ERROR_INVALID_LENGTH = -23;
1696 /** The packet transmission interval is invalid (e.g., too short). */
1697 public static final int ERROR_INVALID_INTERVAL = -24;
1698
1699 /** The hardware does not support this request. */
1700 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti9d1284e2015-09-08 16:46:36 +09001701 /** The hardware returned an error. */
1702 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001703
Nathan Harold63dd8132018-02-14 13:09:45 -08001704 /** The NAT-T destination port for IPsec */
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001705 public static final int NATT_PORT = 4500;
1706
Nathan Harold63dd8132018-02-14 13:09:45 -08001707 /** The minimum interval in seconds between keepalive packet transmissions */
1708 public static final int MIN_INTERVAL = 10;
1709
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001710 private final Network mNetwork;
1711 private final PacketKeepaliveCallback mCallback;
1712 private final Looper mLooper;
1713 private final Messenger mMessenger;
1714
1715 private volatile Integer mSlot;
1716
1717 void stopLooper() {
1718 mLooper.quit();
1719 }
1720
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001721 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001722 public void stop() {
1723 try {
1724 mService.stopKeepalive(mNetwork, mSlot);
1725 } catch (RemoteException e) {
1726 Log.e(TAG, "Error stopping packet keepalive: ", e);
1727 stopLooper();
1728 }
1729 }
1730
1731 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09001732 Preconditions.checkNotNull(network, "network cannot be null");
1733 Preconditions.checkNotNull(callback, "callback cannot be null");
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001734 mNetwork = network;
1735 mCallback = callback;
1736 HandlerThread thread = new HandlerThread(TAG);
1737 thread.start();
1738 mLooper = thread.getLooper();
1739 mMessenger = new Messenger(new Handler(mLooper) {
1740 @Override
1741 public void handleMessage(Message message) {
1742 switch (message.what) {
1743 case NetworkAgent.EVENT_PACKET_KEEPALIVE:
1744 int error = message.arg2;
1745 try {
1746 if (error == SUCCESS) {
1747 if (mSlot == null) {
1748 mSlot = message.arg1;
1749 mCallback.onStarted();
1750 } else {
1751 mSlot = null;
1752 stopLooper();
1753 mCallback.onStopped();
1754 }
1755 } else {
1756 stopLooper();
1757 mCallback.onError(error);
1758 }
1759 } catch (Exception e) {
1760 Log.e(TAG, "Exception in keepalive callback(" + error + ")", e);
1761 }
1762 break;
1763 default:
1764 Log.e(TAG, "Unhandled message " + Integer.toHexString(message.what));
1765 break;
1766 }
1767 }
1768 });
1769 }
1770 }
1771
1772 /**
1773 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
1774 *
1775 * @hide
1776 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001777 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001778 public PacketKeepalive startNattKeepalive(
1779 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
1780 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
1781 final PacketKeepalive k = new PacketKeepalive(network, callback);
1782 try {
1783 mService.startNattKeepalive(network, intervalSeconds, k.mMessenger, new Binder(),
1784 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
1785 } catch (RemoteException e) {
1786 Log.e(TAG, "Error starting packet keepalive: ", e);
1787 k.stopLooper();
1788 return null;
1789 }
1790 return k;
1791 }
1792
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001793 /**
1794 * Ensure that a network route exists to deliver traffic to the specified
1795 * host via the specified network interface. An attempt to add a route that
1796 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001797 *
1798 * <p>This method requires the caller to hold either the
1799 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1800 * or the ability to modify system settings as determined by
1801 * {@link android.provider.Settings.System#canWrite}.</p>
1802 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001803 * @param networkType the type of the network over which traffic to the specified
1804 * host is to be routed
1805 * @param hostAddress the IP address of the host to which the route is desired
1806 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001807 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001808 * @deprecated Deprecated in favor of the
1809 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
1810 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001811 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001812 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001813 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001815 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001817 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001818 }
1819
1820 /**
1821 * Ensure that a network route exists to deliver traffic to the specified
1822 * host via the specified network interface. An attempt to add a route that
1823 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001824 *
1825 * <p>This method requires the caller to hold either the
1826 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1827 * or the ability to modify system settings as determined by
1828 * {@link android.provider.Settings.System#canWrite}.</p>
1829 *
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001830 * @param networkType the type of the network over which traffic to the specified
1831 * host is to be routed
1832 * @param hostAddress the IP address of the host to which the route is desired
1833 * @return {@code true} on success, {@code false} on failure
1834 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001835 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001836 * {@link #bindProcessToNetwork} API.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001837 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001838 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001839 @UnsupportedAppUsage
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001840 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001841 checkLegacyRoutingApiAccess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001842 try {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001843 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001845 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 }
1847 }
1848
1849 /**
1850 * Returns the value of the setting for background data usage. If false,
1851 * applications should not use the network if the application is not in the
1852 * foreground. Developers should respect this setting, and check the value
1853 * of this before performing any background data operations.
1854 * <p>
1855 * All applications that have background services that use the network
1856 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001857 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001858 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001859 * background data depends on several combined factors, and this method will
1860 * always return {@code true}. Instead, when background data is unavailable,
1861 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001862 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001863 * @return Whether background data usage is allowed.
1864 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001865 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001866 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001867 // assume that background data is allowed; final authority is
1868 // NetworkInfo which may be blocked.
1869 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001870 }
1871
1872 /**
1873 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001874 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001875 * @param allowBackgroundData Whether an application should use data while
1876 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001877 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001878 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1879 * @see #getBackgroundDataSetting()
1880 * @hide
1881 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001882 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001883 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001885 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001887
Jeff Sharkey43d2a172017-07-12 10:50:42 -06001888 /** {@hide} */
1889 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001890 @UnsupportedAppUsage
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001891 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1892 try {
1893 return mService.getActiveNetworkQuotaInfo();
1894 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001895 throw e.rethrowFromSystemServer();
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001896 }
1897 }
1898
1899 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001900 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001901 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001902 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001903 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001904 @UnsupportedAppUsage
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001905 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001906 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1907 if (b != null) {
1908 try {
1909 ITelephony it = ITelephony.Stub.asInterface(b);
Shishir Agrawal7ea3e8b2016-01-25 13:03:07 -08001910 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Wink Saville36ffb042014-12-05 11:10:30 -08001911 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
Malcolm Chenb455e722017-11-28 15:57:14 -08001912 boolean retVal = it.isUserDataEnabled(subId);
Wink Saville36ffb042014-12-05 11:10:30 -08001913 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1914 + " retVal=" + retVal);
1915 return retVal;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001916 } catch (RemoteException e) {
1917 throw e.rethrowFromSystemServer();
1918 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001919 }
Wink Saville36ffb042014-12-05 11:10:30 -08001920 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001921 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001922 }
1923
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001924 /**
Robert Greenwaltb2489872014-09-04 16:44:35 -07001925 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt6078b502014-06-11 16:05:07 -07001926 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001927 */
1928 public interface OnNetworkActiveListener {
1929 /**
1930 * Called on the main thread of the process to report that the current data network
1931 * has become active, and it is now a good time to perform any pending network
1932 * operations. Note that this listener only tells you when the network becomes
1933 * active; if at any other time you want to know whether it is active (and thus okay
1934 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001935 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001936 */
Chalard Jean4d660112018-06-04 16:52:49 +09001937 void onNetworkActive();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001938 }
1939
1940 private INetworkManagementService getNetworkManagementService() {
1941 synchronized (this) {
1942 if (mNMService != null) {
1943 return mNMService;
1944 }
1945 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1946 mNMService = INetworkManagementService.Stub.asInterface(b);
1947 return mNMService;
1948 }
1949 }
1950
1951 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
Chalard Jean4d660112018-06-04 16:52:49 +09001952 mNetworkActivityListeners = new ArrayMap<>();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001953
1954 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001955 * Start listening to reports when the system's default data network is active, meaning it is
1956 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1957 * to determine the current state of the system's default network after registering the
1958 * listener.
1959 * <p>
1960 * If the process default network has been set with
Paul Jensen72db88e2015-03-10 10:54:12 -04001961 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt6078b502014-06-11 16:05:07 -07001962 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001963 *
1964 * @param l The listener to be told when the network is active.
1965 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001966 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001967 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1968 @Override
1969 public void onNetworkActive() throws RemoteException {
1970 l.onNetworkActive();
1971 }
1972 };
1973
1974 try {
1975 getNetworkManagementService().registerNetworkActivityListener(rl);
1976 mNetworkActivityListeners.put(l, rl);
1977 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001978 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001979 }
1980 }
1981
1982 /**
1983 * Remove network active listener previously registered with
Robert Greenwaltb2489872014-09-04 16:44:35 -07001984 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001985 *
1986 * @param l Previously registered listener.
1987 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001988 public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001989 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
Hugo Benichie7678512017-05-09 15:19:01 +09001990 Preconditions.checkArgument(rl != null, "Listener was not registered.");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001991 try {
1992 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1993 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001994 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001995 }
1996 }
1997
1998 /**
1999 * Return whether the data network is currently active. An active network means that
2000 * it is currently in a high power state for performing data transmission. On some
2001 * types of networks, it may be expensive to move and stay in such a state, so it is
2002 * more power efficient to batch network traffic together when the radio is already in
2003 * this state. This method tells you whether right now is currently a good time to
2004 * initiate network traffic, as the network is already active.
2005 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002006 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002007 try {
2008 return getNetworkManagementService().isNetworkActive();
2009 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002010 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002011 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002012 }
2013
2014 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002015 * {@hide}
2016 */
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09002017 public ConnectivityManager(Context context, IConnectivityManager service) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09002018 mContext = Preconditions.checkNotNull(context, "missing context");
2019 mService = Preconditions.checkNotNull(service, "missing IConnectivityManager");
Paul Jensene0bef712014-12-10 15:12:18 -05002020 sInstance = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002021 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002022
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07002023 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002024 @UnsupportedAppUsage
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07002025 public static ConnectivityManager from(Context context) {
2026 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
2027 }
2028
Lorenzo Colittifbe9b1a2016-07-28 17:14:11 +09002029 /* TODO: These permissions checks don't belong in client-side code. Move them to
2030 * services.jar, possibly in com.android.server.net. */
2031
2032 /** {@hide} */
Lorenzo Colittid5427052015-10-15 16:29:00 +09002033 public static final void enforceChangePermission(Context context) {
2034 int uid = Binder.getCallingUid();
2035 Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
2036 .getPackageNameForUid(context, uid), true /* throwException */);
2037 }
2038
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002039 /** {@hide} */
2040 public static final void enforceTetherChangePermission(Context context, String callingPkg) {
Hugo Benichie7678512017-05-09 15:19:01 +09002041 Preconditions.checkNotNull(context, "Context cannot be null");
2042 Preconditions.checkNotNull(callingPkg, "callingPkg cannot be null");
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002043
Robert Greenwaltedb47662014-09-16 17:54:19 -07002044 if (context.getResources().getStringArray(
2045 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
2046 // Have a provisioning app - must only let system apps (which check this app)
2047 // turn on tethering
2048 context.enforceCallingOrSelfPermission(
Jeremy Kleind42209d2015-12-28 15:11:58 -08002049 android.Manifest.permission.TETHER_PRIVILEGED, "ConnectivityService");
Robert Greenwaltedb47662014-09-16 17:54:19 -07002050 } else {
Billy Laua7238a32015-08-01 12:45:02 +01002051 int uid = Binder.getCallingUid();
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002052 // If callingPkg's uid is not same as Binder.getCallingUid(),
2053 // AppOpsService throws SecurityException.
2054 Settings.checkAndNoteWriteSettingsOperation(context, uid, callingPkg,
2055 true /* throwException */);
Robert Greenwaltedb47662014-09-16 17:54:19 -07002056 }
2057 }
2058
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002059 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002060 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2061 * situations where a Context pointer is unavailable.
2062 * @hide
2063 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002064 @Deprecated
Paul Jensen72db88e2015-03-10 10:54:12 -04002065 static ConnectivityManager getInstanceOrNull() {
2066 return sInstance;
2067 }
2068
2069 /**
2070 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2071 * situations where a Context pointer is unavailable.
2072 * @hide
2073 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002074 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002075 @UnsupportedAppUsage
Paul Jensen72db88e2015-03-10 10:54:12 -04002076 private static ConnectivityManager getInstance() {
2077 if (getInstanceOrNull() == null) {
Paul Jensene0bef712014-12-10 15:12:18 -05002078 throw new IllegalStateException("No ConnectivityManager yet constructed");
2079 }
Paul Jensen72db88e2015-03-10 10:54:12 -04002080 return getInstanceOrNull();
Paul Jensene0bef712014-12-10 15:12:18 -05002081 }
2082
2083 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002084 * Get the set of tetherable, available interfaces. This list is limited by
2085 * device configuration and current interface existence.
2086 *
2087 * @return an array of 0 or more Strings of tetherable interface names.
2088 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002089 * {@hide}
2090 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002091 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002092 @UnsupportedAppUsage
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002093 public String[] getTetherableIfaces() {
2094 try {
2095 return mService.getTetherableIfaces();
2096 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002097 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002098 }
2099 }
2100
2101 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002102 * Get the set of tethered interfaces.
2103 *
2104 * @return an array of 0 or more String of currently tethered interface names.
2105 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002106 * {@hide}
2107 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002108 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002109 @UnsupportedAppUsage
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002110 public String[] getTetheredIfaces() {
2111 try {
2112 return mService.getTetheredIfaces();
2113 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002114 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002115 }
2116 }
2117
2118 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002119 * Get the set of interface names which attempted to tether but
2120 * failed. Re-attempting to tether may cause them to reset to the Tethered
2121 * state. Alternatively, causing the interface to be destroyed and recreated
2122 * may cause them to reset to the available state.
2123 * {@link ConnectivityManager#getLastTetherError} can be used to get more
2124 * information on the cause of the errors.
2125 *
2126 * @return an array of 0 or more String indicating the interface names
2127 * which failed to tether.
2128 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002129 * {@hide}
2130 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002131 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002132 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002133 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002134 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08002135 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002136 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002137 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002138 }
2139 }
2140
2141 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07002142 * Get the set of tethered dhcp ranges.
2143 *
2144 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
2145 * {@hide}
2146 */
2147 public String[] getTetheredDhcpRanges() {
2148 try {
2149 return mService.getTetheredDhcpRanges();
2150 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002151 throw e.rethrowFromSystemServer();
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07002152 }
2153 }
2154
2155 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002156 * Attempt to tether the named interface. This will setup a dhcp server
2157 * on the interface, forward and NAT IP packets and forward DNS requests
2158 * to the best active upstream network interface. Note that if no upstream
2159 * IP network interface is available, dhcp will still run and traffic will be
2160 * allowed between the tethered devices and this device, though upstream net
2161 * access will of course fail until an upstream network interface becomes
2162 * active.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002163 *
2164 * <p>This method requires the caller to hold either the
2165 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2166 * or the ability to modify system settings as determined by
2167 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002168 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002169 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2170 * and WifiStateMachine which need direct access. All other clients should use
2171 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2172 * logic.</p>
2173 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002174 * @param iface the interface name to tether.
2175 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2176 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002177 * {@hide}
2178 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002179 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002180 public int tether(String iface) {
2181 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002182 String pkgName = mContext.getOpPackageName();
2183 Log.i(TAG, "tether caller:" + pkgName);
2184 return mService.tether(iface, pkgName);
Robert Greenwalt5a735062010-03-02 17:25:02 -08002185 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002186 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002187 }
2188 }
2189
2190 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002191 * Stop tethering the named interface.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002192 *
2193 * <p>This method requires the caller to hold either the
2194 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2195 * or the ability to modify system settings as determined by
2196 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002197 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002198 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2199 * and WifiStateMachine which need direct access. All other clients should use
2200 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2201 * logic.</p>
2202 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002203 * @param iface the interface name to untether.
2204 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2205 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002206 * {@hide}
2207 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002208 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002209 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002210 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002211 String pkgName = mContext.getOpPackageName();
2212 Log.i(TAG, "untether caller:" + pkgName);
2213 return mService.untether(iface, pkgName);
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002214 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002215 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002216 }
2217 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002218
2219 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002220 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002221 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002222 * due to device configuration.
2223 *
Chalard Jean8f76fc32017-09-26 15:45:18 +09002224 * <p>If this app does not have permission to use this API, it will always
2225 * return false rather than throw an exception.</p>
2226 *
2227 * <p>If the device has a hotspot provisioning app, the caller is required to hold the
2228 * {@link android.Manifest.permission.TETHER_PRIVILEGED} permission.</p>
2229 *
2230 * <p>Otherwise, this method requires the caller to hold the ability to modify system
2231 * settings as determined by {@link android.provider.Settings.System#canWrite}.</p>
2232 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002233 * @return a boolean - {@code true} indicating Tethering is supported.
2234 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002235 * {@hide}
2236 */
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002237 @SystemApi
Chalard Jean8f76fc32017-09-26 15:45:18 +09002238 @RequiresPermission(anyOf = {android.Manifest.permission.TETHER_PRIVILEGED,
2239 android.Manifest.permission.WRITE_SETTINGS})
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002240 public boolean isTetheringSupported() {
Chalard Jean8f76fc32017-09-26 15:45:18 +09002241 String pkgName = mContext.getOpPackageName();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002242 try {
Udam Saini0e94c362017-06-07 12:06:28 -07002243 return mService.isTetheringSupported(pkgName);
Chalard Jean8f76fc32017-09-26 15:45:18 +09002244 } catch (SecurityException e) {
2245 // This API is not available to this caller, but for backward-compatibility
2246 // this will just return false instead of throwing.
2247 return false;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002248 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002249 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002250 }
2251 }
2252
2253 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002254 * Callback for use with {@link #startTethering} to find out whether tethering succeeded.
2255 * @hide
2256 */
2257 @SystemApi
2258 public static abstract class OnStartTetheringCallback {
2259 /**
2260 * Called when tethering has been successfully started.
2261 */
Chalard Jean4d660112018-06-04 16:52:49 +09002262 public void onTetheringStarted() {}
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002263
2264 /**
2265 * Called when starting tethering failed.
2266 */
Chalard Jean4d660112018-06-04 16:52:49 +09002267 public void onTetheringFailed() {}
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002268 }
2269
2270 /**
2271 * Convenient overload for
2272 * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null
2273 * handler to run on the current thread's {@link Looper}.
2274 * @hide
2275 */
2276 @SystemApi
Udam Saini0e94c362017-06-07 12:06:28 -07002277 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002278 public void startTethering(int type, boolean showProvisioningUi,
2279 final OnStartTetheringCallback callback) {
2280 startTethering(type, showProvisioningUi, callback, null);
2281 }
2282
2283 /**
2284 * Runs tether provisioning for the given type if needed and then starts tethering if
2285 * the check succeeds. If no carrier provisioning is required for tethering, tethering is
2286 * enabled immediately. If provisioning fails, tethering will not be enabled. It also
2287 * schedules tether provisioning re-checks if appropriate.
2288 *
2289 * @param type The type of tethering to start. Must be one of
2290 * {@link ConnectivityManager.TETHERING_WIFI},
2291 * {@link ConnectivityManager.TETHERING_USB}, or
2292 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2293 * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there
2294 * is one. This should be true the first time this function is called and also any time
2295 * the user can see this UI. It gives users information from their carrier about the
2296 * check failing and how they can sign up for tethering if possible.
2297 * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller
2298 * of the result of trying to tether.
2299 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
2300 * @hide
2301 */
2302 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002303 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002304 public void startTethering(int type, boolean showProvisioningUi,
2305 final OnStartTetheringCallback callback, Handler handler) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09002306 Preconditions.checkNotNull(callback, "OnStartTetheringCallback cannot be null.");
Jeremy Klein5f277e12016-03-12 16:29:54 -08002307
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002308 ResultReceiver wrappedCallback = new ResultReceiver(handler) {
2309 @Override
2310 protected void onReceiveResult(int resultCode, Bundle resultData) {
2311 if (resultCode == TETHER_ERROR_NO_ERROR) {
2312 callback.onTetheringStarted();
2313 } else {
2314 callback.onTetheringFailed();
2315 }
2316 }
2317 };
Jeremy Klein5f277e12016-03-12 16:29:54 -08002318
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002319 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002320 String pkgName = mContext.getOpPackageName();
2321 Log.i(TAG, "startTethering caller:" + pkgName);
2322 mService.startTethering(type, wrappedCallback, showProvisioningUi, pkgName);
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002323 } catch (RemoteException e) {
2324 Log.e(TAG, "Exception trying to start tethering.", e);
2325 wrappedCallback.send(TETHER_ERROR_SERVICE_UNAVAIL, null);
2326 }
2327 }
2328
2329 /**
2330 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if
2331 * applicable.
2332 *
2333 * @param type The type of tethering to stop. Must be one of
2334 * {@link ConnectivityManager.TETHERING_WIFI},
2335 * {@link ConnectivityManager.TETHERING_USB}, or
2336 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2337 * @hide
2338 */
2339 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002340 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002341 public void stopTethering(int type) {
2342 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002343 String pkgName = mContext.getOpPackageName();
2344 Log.i(TAG, "stopTethering caller:" + pkgName);
2345 mService.stopTethering(type, pkgName);
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002346 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002347 throw e.rethrowFromSystemServer();
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002348 }
2349 }
2350
2351 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002352 * Get the list of regular expressions that define any tetherable
2353 * USB network interfaces. If USB tethering is not supported by the
2354 * device, this list should be empty.
2355 *
2356 * @return an array of 0 or more regular expression Strings defining
2357 * what interfaces are considered tetherable usb interfaces.
2358 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002359 * {@hide}
2360 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002361 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002362 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002363 public String[] getTetherableUsbRegexs() {
2364 try {
2365 return mService.getTetherableUsbRegexs();
2366 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002367 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002368 }
2369 }
2370
2371 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002372 * Get the list of regular expressions that define any tetherable
2373 * Wifi network interfaces. If Wifi tethering is not supported by the
2374 * device, this list should be empty.
2375 *
2376 * @return an array of 0 or more regular expression Strings defining
2377 * what interfaces are considered tetherable wifi interfaces.
2378 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002379 * {@hide}
2380 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002381 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002382 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002383 public String[] getTetherableWifiRegexs() {
2384 try {
2385 return mService.getTetherableWifiRegexs();
2386 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002387 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002388 }
2389 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08002390
Danica Chang6fdd0c62010-08-11 14:54:43 -07002391 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002392 * Get the list of regular expressions that define any tetherable
2393 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
2394 * device, this list should be empty.
2395 *
2396 * @return an array of 0 or more regular expression Strings defining
2397 * what interfaces are considered tetherable bluetooth interfaces.
2398 *
Danica Chang6fdd0c62010-08-11 14:54:43 -07002399 * {@hide}
2400 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002401 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002402 @UnsupportedAppUsage
Danica Chang6fdd0c62010-08-11 14:54:43 -07002403 public String[] getTetherableBluetoothRegexs() {
2404 try {
2405 return mService.getTetherableBluetoothRegexs();
2406 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002407 throw e.rethrowFromSystemServer();
Danica Chang6fdd0c62010-08-11 14:54:43 -07002408 }
2409 }
2410
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002411 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002412 * Attempt to both alter the mode of USB and Tethering of USB. A
2413 * utility method to deal with some of the complexity of USB - will
2414 * attempt to switch to Rndis and subsequently tether the resulting
2415 * interface on {@code true} or turn off tethering and switch off
2416 * Rndis on {@code false}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002417 *
2418 * <p>This method requires the caller to hold either the
2419 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2420 * or the ability to modify system settings as determined by
2421 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002422 *
2423 * @param enable a boolean - {@code true} to enable tethering
2424 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2425 *
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002426 * {@hide}
2427 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002428 @UnsupportedAppUsage
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002429 public int setUsbTethering(boolean enable) {
2430 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002431 String pkgName = mContext.getOpPackageName();
2432 Log.i(TAG, "setUsbTethering caller:" + pkgName);
2433 return mService.setUsbTethering(enable, pkgName);
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002434 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002435 throw e.rethrowFromSystemServer();
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002436 }
2437 }
2438
Robert Greenwalt5a735062010-03-02 17:25:02 -08002439 /** {@hide} */
2440 public static final int TETHER_ERROR_NO_ERROR = 0;
2441 /** {@hide} */
2442 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
2443 /** {@hide} */
2444 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
2445 /** {@hide} */
2446 public static final int TETHER_ERROR_UNSUPPORTED = 3;
2447 /** {@hide} */
2448 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
2449 /** {@hide} */
2450 public static final int TETHER_ERROR_MASTER_ERROR = 5;
2451 /** {@hide} */
2452 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
2453 /** {@hide} */
2454 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
2455 /** {@hide} */
2456 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
2457 /** {@hide} */
2458 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
2459 /** {@hide} */
2460 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002461 /** {@hide} */
2462 public static final int TETHER_ERROR_PROVISION_FAILED = 11;
Robert Greenwalt5a735062010-03-02 17:25:02 -08002463
2464 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002465 * Get a more detailed error code after a Tethering or Untethering
2466 * request asynchronously failed.
2467 *
2468 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08002469 * @return error The error code of the last error tethering or untethering the named
2470 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002471 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002472 * {@hide}
2473 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002474 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002475 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002476 public int getLastTetherError(String iface) {
2477 try {
2478 return mService.getLastTetherError(iface);
2479 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002480 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002481 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002482 }
2483
2484 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002485 * Report network connectivity status. This is currently used only
2486 * to alter status bar UI.
Paul Jensenb2748922015-05-06 11:10:18 -04002487 * <p>This method requires the caller to hold the permission
2488 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002489 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002490 * @param networkType The type of network you want to report on
2491 * @param percentage The quality of the connection 0 is bad, 100 is good
Chalard Jean6b1da6e2018-03-08 13:54:53 +09002492 * @deprecated Types are deprecated. Use {@link #reportNetworkConnectivity} instead.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002493 * {@hide}
2494 */
2495 public void reportInetCondition(int networkType, int percentage) {
2496 try {
2497 mService.reportInetCondition(networkType, percentage);
2498 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002499 throw e.rethrowFromSystemServer();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002500 }
2501 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002502
2503 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002504 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07002505 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002506 * the framework to re-evaluate network connectivity and/or switch to another
2507 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002508 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002509 * @param network The {@link Network} the application was attempting to use
2510 * or {@code null} to indicate the current default network.
Paul Jensenbfd17b72015-04-07 12:43:13 -04002511 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
2512 * working and non-working connectivity.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002513 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002514 @Deprecated
Robert Greenwalt9258c642014-03-26 16:47:06 -07002515 public void reportBadNetwork(Network network) {
2516 try {
Paul Jensenbfd17b72015-04-07 12:43:13 -04002517 // One of these will be ignored because it matches system's current state.
2518 // The other will trigger the necessary reevaluation.
2519 mService.reportNetworkConnectivity(network, true);
2520 mService.reportNetworkConnectivity(network, false);
2521 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002522 throw e.rethrowFromSystemServer();
Paul Jensenbfd17b72015-04-07 12:43:13 -04002523 }
2524 }
2525
2526 /**
2527 * Report to the framework whether a network has working connectivity.
2528 * This provides a hint to the system that a particular network is providing
2529 * working connectivity or not. In response the framework may re-evaluate
2530 * the network's connectivity and might take further action thereafter.
2531 *
2532 * @param network The {@link Network} the application was attempting to use
2533 * or {@code null} to indicate the current default network.
2534 * @param hasConnectivity {@code true} if the application was able to successfully access the
2535 * Internet using {@code network} or {@code false} if not.
2536 */
2537 public void reportNetworkConnectivity(Network network, boolean hasConnectivity) {
2538 try {
2539 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002540 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002541 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002542 }
2543 }
2544
2545 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002546 * Set a network-independent global http proxy. This is not normally what you want
2547 * for typical HTTP proxies - they are general network dependent. However if you're
2548 * doing something unusual like general internal filtering this may be useful. On
2549 * a private network where the proxy is not accessible, you may break HTTP using this.
Paul Jensenb2748922015-05-06 11:10:18 -04002550 *
2551 * @param p A {@link ProxyInfo} object defining the new global
2552 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002553 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002554 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002555 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Jason Monk207900c2014-04-25 15:00:09 -04002556 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002557 try {
2558 mService.setGlobalProxy(p);
2559 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002560 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002561 }
2562 }
2563
2564 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002565 * Retrieve any network-independent global HTTP proxy.
2566 *
Jason Monk207900c2014-04-25 15:00:09 -04002567 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002568 * if no global HTTP proxy is set.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002569 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002570 */
Jason Monk207900c2014-04-25 15:00:09 -04002571 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002572 try {
2573 return mService.getGlobalProxy();
2574 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002575 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002576 }
2577 }
2578
2579 /**
Paul Jensencee9b512015-05-06 07:32:40 -04002580 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
2581 * network-specific HTTP proxy. If {@code network} is null, the
2582 * network-specific proxy returned is the proxy of the default active
2583 * network.
2584 *
2585 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
2586 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
2587 * or when {@code network} is {@code null},
2588 * the {@code ProxyInfo} for the default active network. Returns
2589 * {@code null} when no proxy applies or the caller doesn't have
2590 * permission to use {@code network}.
2591 * @hide
2592 */
2593 public ProxyInfo getProxyForNetwork(Network network) {
2594 try {
2595 return mService.getProxyForNetwork(network);
2596 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002597 throw e.rethrowFromSystemServer();
Paul Jensencee9b512015-05-06 07:32:40 -04002598 }
2599 }
2600
2601 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002602 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
2603 * otherwise if this process is bound to a {@link Network} using
Paul Jensen72db88e2015-03-10 10:54:12 -04002604 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensene0bef712014-12-10 15:12:18 -05002605 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002606 *
Jason Monk207900c2014-04-25 15:00:09 -04002607 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002608 * HTTP proxy is active.
Robert Greenwalt434203a2010-10-11 16:00:27 -07002609 */
Paul Jensene0bef712014-12-10 15:12:18 -05002610 public ProxyInfo getDefaultProxy() {
Paul Jensencee9b512015-05-06 07:32:40 -04002611 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwalt434203a2010-10-11 16:00:27 -07002612 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07002613
2614 /**
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002615 * Returns true if the hardware supports the given network type
2616 * else it returns false. This doesn't indicate we have coverage
2617 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002618 * hardware supports it. For example a GSM phone without a SIM
2619 * should still return {@code true} for mobile data, but a wifi only
2620 * tablet would return {@code false}.
2621 *
2622 * @param networkType The network type we'd like to check
2623 * @return {@code true} if supported, else {@code false}
Chalard Jean6b1da6e2018-03-08 13:54:53 +09002624 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002625 * @hide
2626 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +09002627 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002628 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002629 @UnsupportedAppUsage
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002630 public boolean isNetworkSupported(int networkType) {
2631 try {
2632 return mService.isNetworkSupported(networkType);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002633 } catch (RemoteException e) {
2634 throw e.rethrowFromSystemServer();
2635 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002636 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002637
2638 /**
2639 * Returns if the currently active data network is metered. A network is
2640 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002641 * that connection due to monetary costs, data limitations or
2642 * battery/performance issues. You should check this before doing large
2643 * data transfers, and warn the user or delay the operation until another
2644 * network is available.
2645 *
2646 * @return {@code true} if large transfers should be avoided, otherwise
2647 * {@code false}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002648 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002649 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002650 public boolean isActiveNetworkMetered() {
2651 try {
2652 return mService.isActiveNetworkMetered();
2653 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002654 throw e.rethrowFromSystemServer();
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002655 }
2656 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002657
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002658 /**
2659 * If the LockdownVpn mechanism is enabled, updates the vpn
2660 * with a reload of its profile.
2661 *
2662 * @return a boolean with {@code} indicating success
2663 *
2664 * <p>This method can only be called by the system UID
2665 * {@hide}
2666 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002667 public boolean updateLockdownVpn() {
2668 try {
2669 return mService.updateLockdownVpn();
2670 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002671 throw e.rethrowFromSystemServer();
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002672 }
2673 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07002674
2675 /**
Wink Saville948282b2013-08-29 08:55:16 -07002676 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07002677 *
Wink Savilleab9321d2013-06-29 21:10:57 -07002678 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07002679 *
2680 * @return time out that will be used, maybe less that suggestedTimeOutMs
2681 * -1 if an error.
2682 *
2683 * {@hide}
2684 */
Wink Saville948282b2013-08-29 08:55:16 -07002685 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07002686 int timeOutMs = -1;
2687 try {
Wink Saville948282b2013-08-29 08:55:16 -07002688 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07002689 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002690 throw e.rethrowFromSystemServer();
Wink Savilleab9321d2013-06-29 21:10:57 -07002691 }
2692 return timeOutMs;
2693 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002694
2695 /**
Wink Saville42d4f082013-07-20 20:31:59 -07002696 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002697 * {@hide}
2698 */
2699 public String getMobileProvisioningUrl() {
2700 try {
2701 return mService.getMobileProvisioningUrl();
2702 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002703 throw e.rethrowFromSystemServer();
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002704 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002705 }
Wink Saville42d4f082013-07-20 20:31:59 -07002706
2707 /**
Wink Saville948282b2013-08-29 08:55:16 -07002708 * Set sign in error notification to visible or in visible
2709 *
Wink Saville948282b2013-08-29 08:55:16 -07002710 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04002711 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville948282b2013-08-29 08:55:16 -07002712 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002713 @Deprecated
Wink Saville948282b2013-08-29 08:55:16 -07002714 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04002715 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07002716 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04002717 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07002718 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002719 throw e.rethrowFromSystemServer();
Wink Saville948282b2013-08-29 08:55:16 -07002720 }
2721 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002722
2723 /**
2724 * Set the value for enabling/disabling airplane mode
2725 *
2726 * @param enable whether to enable airplane mode or not
2727 *
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002728 * @hide
2729 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002730 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Lorenzo Colitti85eca482018-10-09 18:50:32 +09002731 @SystemApi
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002732 public void setAirplaneMode(boolean enable) {
2733 try {
2734 mService.setAirplaneMode(enable);
2735 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002736 throw e.rethrowFromSystemServer();
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002737 }
2738 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002739
2740 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002741 @UnsupportedAppUsage
Robert Greenwalta67be032014-05-16 15:49:14 -07002742 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07002743 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07002744 mService.registerNetworkFactory(messenger, name);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002745 } catch (RemoteException e) {
2746 throw e.rethrowFromSystemServer();
2747 }
Robert Greenwalta67be032014-05-16 15:49:14 -07002748 }
2749
2750 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002751 @UnsupportedAppUsage
Robert Greenwalta67be032014-05-16 15:49:14 -07002752 public void unregisterNetworkFactory(Messenger messenger) {
2753 try {
2754 mService.unregisterNetworkFactory(messenger);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002755 } catch (RemoteException e) {
2756 throw e.rethrowFromSystemServer();
2757 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002758 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002759
Paul Jensen31a94f42015-02-13 14:18:39 -05002760 /**
2761 * @hide
2762 * Register a NetworkAgent with ConnectivityService.
2763 * @return NetID corresponding to NetworkAgent.
2764 */
2765 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002766 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002767 try {
Paul Jensen31a94f42015-02-13 14:18:39 -05002768 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
2769 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002770 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -05002771 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002772 }
2773
Robert Greenwalt9258c642014-03-26 16:47:06 -07002774 /**
Hugo Benichidafed3d2017-03-06 09:17:06 +09002775 * Base class for {@code NetworkRequest} callbacks. Used for notifications about network
2776 * changes. Should be extended by applications wanting notifications.
2777 *
2778 * A {@code NetworkCallback} is registered by calling
2779 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
2780 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)},
Hugo Benichica867dc2018-02-07 21:17:43 +09002781 * or {@link #registerDefaultNetworkCallback(NetworkCallback)}. A {@code NetworkCallback} is
Hugo Benichidafed3d2017-03-06 09:17:06 +09002782 * unregistered by calling {@link #unregisterNetworkCallback(NetworkCallback)}.
2783 * A {@code NetworkCallback} should be registered at most once at any time.
2784 * A {@code NetworkCallback} that has been unregistered can be registered again.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002785 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002786 public static class NetworkCallback {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002787 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002788 * Called when the framework connects to a new network to evaluate whether it satisfies this
2789 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
2790 * callback. There is no guarantee that this new network will satisfy any requests, or that
2791 * the network will stay connected for longer than the time necessary to evaluate it.
2792 * <p>
2793 * Most applications <b>should not</b> act on this callback, and should instead use
2794 * {@link #onAvailable}. This callback is intended for use by applications that can assist
2795 * the framework in properly evaluating the network &mdash; for example, an application that
2796 * can automatically log in to a captive portal without user intervention.
2797 *
2798 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti66276122015-06-11 14:27:17 +09002799 *
2800 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002801 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002802 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002803
2804 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002805 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002806 * This callback may be called more than once if the {@link Network} that is
2807 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002808 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002809 * @param network The {@link Network} of the satisfying network.
Chalard Jean804b8fb2018-01-30 22:41:41 +09002810 * @param networkCapabilities The {@link NetworkCapabilities} of the satisfying network.
2811 * @param linkProperties The {@link LinkProperties} of the satisfying network.
2812 * @hide
2813 */
2814 public void onAvailable(Network network, NetworkCapabilities networkCapabilities,
2815 LinkProperties linkProperties) {
2816 // Internally only this method is called when a new network is available, and
2817 // it calls the callback in the same way and order that older versions used
2818 // to call so as not to change the behavior.
2819 onAvailable(network);
2820 if (!networkCapabilities.hasCapability(
2821 NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)) {
2822 onNetworkSuspended(network);
2823 }
2824 onCapabilitiesChanged(network, networkCapabilities);
2825 onLinkPropertiesChanged(network, linkProperties);
2826 }
2827
2828 /**
2829 * Called when the framework connects and has declared a new network ready for use.
2830 * This callback may be called more than once if the {@link Network} that is
2831 * satisfying the request changes. This will always immediately be followed by a
2832 * call to {@link #onCapabilitiesChanged(Network, NetworkCapabilities)} then by a
2833 * call to {@link #onLinkPropertiesChanged(Network, LinkProperties)}.
2834 *
2835 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002836 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002837 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002838
2839 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002840 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002841 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002842 * for graceful handover. This may not be called if we have a hard loss
2843 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002844 * {@link NetworkCallback#onLost} call or a
2845 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002846 * on whether we lose or regain it.
2847 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002848 * @param network The {@link Network} that is about to be disconnected.
2849 * @param maxMsToLive The time in ms the framework will attempt to keep the
2850 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002851 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002852 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002853 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002854
2855 /**
2856 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002857 * graceful failure ends.
2858 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002859 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002860 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002861 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002862
2863 /**
Etan Cohenaebf17e2017-03-01 12:47:28 -08002864 * Called if no network is found in the timeout time specified in
Hugo Benichi0eec03f2017-05-15 15:15:33 +09002865 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} call. This callback is not
Etan Cohenaebf17e2017-03-01 12:47:28 -08002866 * called for the version of {@link #requestNetwork(NetworkRequest, NetworkCallback)}
2867 * without timeout. When this callback is invoked the associated
2868 * {@link NetworkRequest} will have already been removed and released, as if
2869 * {@link #unregisterNetworkCallback(NetworkCallback)} had been called.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002870 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002871 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002872
2873 /**
2874 * Called when the network the framework connected to for this request
2875 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002876 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002877 * @param network The {@link Network} whose capabilities have changed.
Chalard Jean804b8fb2018-01-30 22:41:41 +09002878 * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this
2879 * network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002880 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002881 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002882 NetworkCapabilities networkCapabilities) {}
2883
2884 /**
2885 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002886 * changes {@link LinkProperties}.
2887 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002888 * @param network The {@link Network} whose link properties have changed.
2889 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002890 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002891 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002892
Robert Greenwalt8d482522015-06-24 13:23:42 -07002893 /**
2894 * Called when the network the framework connected to for this request
Chalard Jean804b8fb2018-01-30 22:41:41 +09002895 * goes into {@link NetworkInfo.State#SUSPENDED}.
Robert Greenwalt8d482522015-06-24 13:23:42 -07002896 * This generally means that while the TCP connections are still live,
2897 * temporarily network data fails to transfer. Specifically this is used
2898 * on cellular networks to mask temporary outages when driving through
2899 * a tunnel, etc.
2900 * @hide
2901 */
2902 public void onNetworkSuspended(Network network) {}
2903
2904 /**
2905 * Called when the network the framework connected to for this request
Chalard Jean804b8fb2018-01-30 22:41:41 +09002906 * returns from a {@link NetworkInfo.State#SUSPENDED} state. This should always be
2907 * preceded by a matching {@link NetworkCallback#onNetworkSuspended} call.
Robert Greenwalt8d482522015-06-24 13:23:42 -07002908 * @hide
2909 */
2910 public void onNetworkResumed(Network network) {}
2911
Robert Greenwalt6078b502014-06-11 16:05:07 -07002912 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002913 }
2914
Hugo Benichicb883232017-05-11 13:16:17 +09002915 /**
2916 * Constant error codes used by ConnectivityService to communicate about failures and errors
2917 * across a Binder boundary.
2918 * @hide
2919 */
2920 public interface Errors {
Chalard Jean4d660112018-06-04 16:52:49 +09002921 int TOO_MANY_REQUESTS = 1;
Hugo Benichicb883232017-05-11 13:16:17 +09002922 }
2923
2924 /** @hide */
2925 public static class TooManyRequestsException extends RuntimeException {}
2926
2927 private static RuntimeException convertServiceException(ServiceSpecificException e) {
2928 switch (e.errorCode) {
2929 case Errors.TOO_MANY_REQUESTS:
2930 return new TooManyRequestsException();
2931 default:
2932 Log.w(TAG, "Unknown service error code " + e.errorCode);
2933 return new RuntimeException(e);
2934 }
2935 }
2936
Robert Greenwalt9258c642014-03-26 16:47:06 -07002937 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002938 /** @hide */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002939 public static final int CALLBACK_PRECHECK = BASE + 1;
2940 /** @hide */
2941 public static final int CALLBACK_AVAILABLE = BASE + 2;
2942 /** @hide arg1 = TTL */
2943 public static final int CALLBACK_LOSING = BASE + 3;
2944 /** @hide */
2945 public static final int CALLBACK_LOST = BASE + 4;
2946 /** @hide */
2947 public static final int CALLBACK_UNAVAIL = BASE + 5;
2948 /** @hide */
2949 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2950 /** @hide */
2951 public static final int CALLBACK_IP_CHANGED = BASE + 7;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002952 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Hugo Benichidba33db2017-03-23 22:40:44 +09002953 private static final int EXPIRE_LEGACY_REQUEST = BASE + 8;
Robert Greenwalt8d482522015-06-24 13:23:42 -07002954 /** @hide */
Hugo Benichidba33db2017-03-23 22:40:44 +09002955 public static final int CALLBACK_SUSPENDED = BASE + 9;
Robert Greenwalt8d482522015-06-24 13:23:42 -07002956 /** @hide */
Hugo Benichidba33db2017-03-23 22:40:44 +09002957 public static final int CALLBACK_RESUMED = BASE + 10;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002958
Erik Kline57faba92015-11-25 12:49:38 +09002959 /** @hide */
2960 public static String getCallbackName(int whichCallback) {
2961 switch (whichCallback) {
2962 case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK";
2963 case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE";
2964 case CALLBACK_LOSING: return "CALLBACK_LOSING";
2965 case CALLBACK_LOST: return "CALLBACK_LOST";
2966 case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL";
2967 case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED";
2968 case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED";
Erik Kline57faba92015-11-25 12:49:38 +09002969 case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST";
2970 case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED";
2971 case CALLBACK_RESUMED: return "CALLBACK_RESUMED";
2972 default:
2973 return Integer.toString(whichCallback);
2974 }
2975 }
2976
Robert Greenwalt562cc542014-05-15 18:07:26 -07002977 private class CallbackHandler extends Handler {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002978 private static final String TAG = "ConnectivityManager.CallbackHandler";
Robert Greenwalta9ebeef2015-09-03 16:41:45 -07002979 private static final boolean DBG = false;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002980
Hugo Benichid42650f2016-07-06 22:53:17 +09002981 CallbackHandler(Looper looper) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002982 super(looper);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002983 }
2984
Hugo Benichi2583ef02017-02-02 17:02:36 +09002985 CallbackHandler(Handler handler) {
Hugo Benichie7678512017-05-09 15:19:01 +09002986 this(Preconditions.checkNotNull(handler, "Handler cannot be null.").getLooper());
Hugo Benichi2583ef02017-02-02 17:02:36 +09002987 }
2988
Robert Greenwalt9258c642014-03-26 16:47:06 -07002989 @Override
2990 public void handleMessage(Message message) {
Hugo Benichi2c684522017-05-09 14:36:02 +09002991 if (message.what == EXPIRE_LEGACY_REQUEST) {
2992 expireRequest((NetworkCapabilities) message.obj, message.arg1);
2993 return;
2994 }
2995
2996 final NetworkRequest request = getObject(message, NetworkRequest.class);
2997 final Network network = getObject(message, Network.class);
2998 final NetworkCallback callback;
2999 synchronized (sCallbacks) {
3000 callback = sCallbacks.get(request);
3001 }
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09003002 if (DBG) {
Hugo Benichia0385682017-03-22 17:07:57 +09003003 Log.d(TAG, getCallbackName(message.what) + " for network " + network);
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09003004 }
Hugo Benichi2c684522017-05-09 14:36:02 +09003005 if (callback == null) {
3006 Log.w(TAG, "callback not found for " + getCallbackName(message.what) + " message");
3007 return;
3008 }
3009
Robert Greenwalt9258c642014-03-26 16:47:06 -07003010 switch (message.what) {
3011 case CALLBACK_PRECHECK: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003012 callback.onPreCheck(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003013 break;
3014 }
3015 case CALLBACK_AVAILABLE: {
Chalard Jean804b8fb2018-01-30 22:41:41 +09003016 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
3017 LinkProperties lp = getObject(message, LinkProperties.class);
3018 callback.onAvailable(network, cap, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003019 break;
3020 }
3021 case CALLBACK_LOSING: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003022 callback.onLosing(network, message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003023 break;
3024 }
3025 case CALLBACK_LOST: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003026 callback.onLost(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003027 break;
3028 }
3029 case CALLBACK_UNAVAIL: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003030 callback.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07003031 break;
3032 }
3033 case CALLBACK_CAP_CHANGED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003034 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
3035 callback.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003036 break;
3037 }
3038 case CALLBACK_IP_CHANGED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003039 LinkProperties lp = getObject(message, LinkProperties.class);
3040 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003041 break;
3042 }
Robert Greenwalt8d482522015-06-24 13:23:42 -07003043 case CALLBACK_SUSPENDED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003044 callback.onNetworkSuspended(network);
Robert Greenwalt8d482522015-06-24 13:23:42 -07003045 break;
3046 }
3047 case CALLBACK_RESUMED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003048 callback.onNetworkResumed(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07003049 break;
3050 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003051 }
3052 }
3053
Hugo Benichid42650f2016-07-06 22:53:17 +09003054 private <T> T getObject(Message msg, Class<T> c) {
3055 return (T) msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07003056 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003057 }
3058
Hugo Benichi2583ef02017-02-02 17:02:36 +09003059 private CallbackHandler getDefaultHandler() {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003060 synchronized (sCallbacks) {
3061 if (sCallbackHandler == null) {
3062 sCallbackHandler = new CallbackHandler(ConnectivityThread.getInstanceLooper());
Robert Greenwalt9258c642014-03-26 16:47:06 -07003063 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003064 return sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003065 }
3066 }
3067
Hugo Benichi6f260f32017-02-03 14:18:44 +09003068 private static final HashMap<NetworkRequest, NetworkCallback> sCallbacks = new HashMap<>();
3069 private static CallbackHandler sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003070
Hugo Benichi6f260f32017-02-03 14:18:44 +09003071 private static final int LISTEN = 1;
3072 private static final int REQUEST = 2;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003073
Hugo Benichi6f260f32017-02-03 14:18:44 +09003074 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, NetworkCallback callback,
3075 int timeoutMs, int action, int legacyType, CallbackHandler handler) {
Hugo Benichie7678512017-05-09 15:19:01 +09003076 checkCallbackNotNull(callback);
Hugo Benichidafed3d2017-03-06 09:17:06 +09003077 Preconditions.checkArgument(action == REQUEST || need != null, "null NetworkCapabilities");
Hugo Benichid42650f2016-07-06 22:53:17 +09003078 final NetworkRequest request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003079 try {
Hugo Benichid42650f2016-07-06 22:53:17 +09003080 synchronized(sCallbacks) {
Hugo Benichi31c176d2017-06-17 13:14:12 +09003081 if (callback.networkRequest != null
3082 && callback.networkRequest != ALREADY_UNREGISTERED) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09003083 // TODO: throw exception instead and enforce 1:1 mapping of callbacks
3084 // and requests (http://b/20701525).
3085 Log.e(TAG, "NetworkCallback was already registered");
3086 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003087 Messenger messenger = new Messenger(handler);
Hugo Benichid42650f2016-07-06 22:53:17 +09003088 Binder binder = new Binder();
Paul Jensen7221cc32014-06-27 11:05:32 -04003089 if (action == LISTEN) {
Hugo Benichid42650f2016-07-06 22:53:17 +09003090 request = mService.listenForNetwork(need, messenger, binder);
Paul Jensen7221cc32014-06-27 11:05:32 -04003091 } else {
Hugo Benichid42650f2016-07-06 22:53:17 +09003092 request = mService.requestNetwork(
3093 need, messenger, timeoutMs, binder, legacyType);
Paul Jensen7221cc32014-06-27 11:05:32 -04003094 }
Hugo Benichid42650f2016-07-06 22:53:17 +09003095 if (request != null) {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003096 sCallbacks.put(request, callback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003097 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003098 callback.networkRequest = request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003099 }
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003100 } catch (RemoteException e) {
3101 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003102 } catch (ServiceSpecificException e) {
3103 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003104 }
Hugo Benichid42650f2016-07-06 22:53:17 +09003105 return request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003106 }
3107
3108 /**
Erik Klinea2d29402016-03-16 15:31:39 +09003109 * Helper function to request a network with a particular legacy type.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09003110 *
3111 * This is temporarily public @hide so it can be called by system code that uses the
3112 * NetworkRequest API to request networks but relies on CONNECTIVITY_ACTION broadcasts for
3113 * instead network notifications.
3114 *
3115 * TODO: update said system code to rely on NetworkCallbacks and make this method private.
3116 *
3117 * @hide
3118 */
Lorenzo Colittid1179462015-11-25 15:47:14 +09003119 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
Hugo Benichi2583ef02017-02-02 17:02:36 +09003120 int timeoutMs, int legacyType, Handler handler) {
3121 CallbackHandler cbHandler = new CallbackHandler(handler);
3122 NetworkCapabilities nc = request.networkCapabilities;
3123 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, legacyType, cbHandler);
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09003124 }
3125
3126 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09003127 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003128 *
3129 * This {@link NetworkRequest} will live until released via
Etan Cohenaebf17e2017-03-01 12:47:28 -08003130 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A
3131 * version of the method which takes a timeout is
Hugo Benichi0eec03f2017-05-15 15:15:33 +09003132 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003133 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07003134 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003135 * can be used to direct traffic to the network.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003136 * <p>It is presently unsupported to request a network with mutable
3137 * {@link NetworkCapabilities} such as
3138 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3139 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3140 * as these {@code NetworkCapabilities} represent states that a particular
3141 * network may never attain, and whether a network will attain these states
3142 * is unknown prior to bringing up the network so the framework does not
3143 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003144 *
3145 * <p>This method requires the caller to hold either the
3146 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3147 * or the ability to modify system settings as determined by
3148 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt9258c642014-03-26 16:47:06 -07003149 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003150 * @param request {@link NetworkRequest} describing this request.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003151 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3152 * the callback must not be shared - it uniquely specifies this request.
3153 * The callback is invoked on the default internal Handler.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003154 * @throws IllegalArgumentException if {@code request} specifies any mutable
3155 * {@code NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003156 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003157 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003158 requestNetwork(request, networkCallback, getDefaultHandler());
3159 }
3160
3161 /**
3162 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
3163 *
3164 * This {@link NetworkRequest} will live until released via
Etan Cohenaebf17e2017-03-01 12:47:28 -08003165 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A
3166 * version of the method which takes a timeout is
Hugo Benichi0eec03f2017-05-15 15:15:33 +09003167 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003168 * Status of the request can be followed by listening to the various
3169 * callbacks described in {@link NetworkCallback}. The {@link Network}
3170 * can be used to direct traffic to the network.
3171 * <p>It is presently unsupported to request a network with mutable
3172 * {@link NetworkCapabilities} such as
3173 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3174 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3175 * as these {@code NetworkCapabilities} represent states that a particular
3176 * network may never attain, and whether a network will attain these states
3177 * is unknown prior to bringing up the network so the framework does not
Chalard Jean4d660112018-06-04 16:52:49 +09003178 * know how to go about satisfying a request with these capabilities.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003179 *
3180 * <p>This method requires the caller to hold either the
3181 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3182 * or the ability to modify system settings as determined by
3183 * {@link android.provider.Settings.System#canWrite}.</p>
3184 *
3185 * @param request {@link NetworkRequest} describing this request.
3186 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3187 * the callback must not be shared - it uniquely specifies this request.
3188 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
3189 * @throws IllegalArgumentException if {@code request} specifies any mutable
3190 * {@code NetworkCapabilities}.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003191 */
3192 public void requestNetwork(
3193 NetworkRequest request, NetworkCallback networkCallback, Handler handler) {
3194 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
3195 CallbackHandler cbHandler = new CallbackHandler(handler);
3196 requestNetwork(request, networkCallback, 0, legacyType, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003197 }
3198
3199 /**
Etan Cohenaebf17e2017-03-01 12:47:28 -08003200 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
3201 * by a timeout.
3202 *
3203 * This function behaves identically to the non-timed-out version
3204 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network
3205 * is not found within the given time (in milliseconds) the
3206 * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be
3207 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3208 * not have to be released if timed-out (it is automatically released). Unregistering a
3209 * request that timed out is not an error.
3210 *
3211 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3212 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3213 * for that purpose. Calling this method will attempt to bring up the requested network.
3214 *
3215 * <p>This method requires the caller to hold either the
3216 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3217 * or the ability to modify system settings as determined by
3218 * {@link android.provider.Settings.System#canWrite}.</p>
3219 *
3220 * @param request {@link NetworkRequest} describing this request.
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003221 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3222 * the callback must not be shared - it uniquely specifies this request.
Etan Cohenaebf17e2017-03-01 12:47:28 -08003223 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3224 * before {@link NetworkCallback#onUnavailable()} is called. The timeout must
3225 * be a positive value (i.e. >0).
Etan Cohenaebf17e2017-03-01 12:47:28 -08003226 */
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003227 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
3228 int timeoutMs) {
Hugo Benichie7678512017-05-09 15:19:01 +09003229 checkTimeout(timeoutMs);
Hugo Benichi2583ef02017-02-02 17:02:36 +09003230 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
Erik Klineb583b032017-02-22 12:58:24 +09003231 requestNetwork(request, networkCallback, timeoutMs, legacyType, getDefaultHandler());
Hugo Benichi2583ef02017-02-02 17:02:36 +09003232 }
3233
Hugo Benichi2583ef02017-02-02 17:02:36 +09003234 /**
3235 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
3236 * by a timeout.
3237 *
Chalard Jean4d660112018-06-04 16:52:49 +09003238 * This function behaves identically to the version without timeout, but if a suitable
Hugo Benichi2583ef02017-02-02 17:02:36 +09003239 * network is not found within the given time (in milliseconds) the
Etan Cohenaebf17e2017-03-01 12:47:28 -08003240 * {@link NetworkCallback#onUnavailable} callback is called. The request can still be
3241 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3242 * not have to be released if timed-out (it is automatically released). Unregistering a
3243 * request that timed out is not an error.
3244 *
3245 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3246 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3247 * for that purpose. Calling this method will attempt to bring up the requested network.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003248 *
3249 * <p>This method requires the caller to hold either the
3250 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3251 * or the ability to modify system settings as determined by
3252 * {@link android.provider.Settings.System#canWrite}.</p>
3253 *
3254 * @param request {@link NetworkRequest} describing this request.
Etan Cohenaebf17e2017-03-01 12:47:28 -08003255 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3256 * the callback must not be shared - it uniquely specifies this request.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003257 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003258 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3259 * before {@link NetworkCallback#onUnavailable} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003260 */
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003261 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
3262 Handler handler, int timeoutMs) {
Hugo Benichie7678512017-05-09 15:19:01 +09003263 checkTimeout(timeoutMs);
Hugo Benichi2583ef02017-02-02 17:02:36 +09003264 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
3265 CallbackHandler cbHandler = new CallbackHandler(handler);
3266 requestNetwork(request, networkCallback, timeoutMs, legacyType, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003267 }
3268
3269 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003270 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003271 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003272 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfcde58f2015-02-11 16:51:13 -08003273 * <p>
Paul Jensen72db88e2015-03-10 10:54:12 -04003274 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
3275 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003276 */
Erik Kline90e93072014-11-19 12:12:24 +09003277 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003278
3279 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07003280 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003281 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003282 * {@link android.content.Intent#getParcelableExtra(String)}.
3283 */
Erik Kline90e93072014-11-19 12:12:24 +09003284 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003285
3286
3287 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09003288 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003289 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003290 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07003291 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003292 * the request may outlive the calling application and get called back when a suitable
3293 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003294 * <p>
3295 * The operation is an Intent broadcast that goes to a broadcast receiver that
3296 * you registered with {@link Context#registerReceiver} or through the
3297 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3298 * <p>
3299 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09003300 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3301 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07003302 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07003303 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07003304 * Intent to reserve the network or it will be released shortly after the Intent
3305 * is processed.
3306 * <p>
Paul Jensen694f2b82015-06-17 14:15:39 -04003307 * If there is already a request for this Intent registered (with the equality of
Robert Greenwalt9258c642014-03-26 16:47:06 -07003308 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003309 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003310 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003311 * The request may be released normally by calling
3312 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003313 * <p>It is presently unsupported to request a network with either
3314 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3315 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3316 * as these {@code NetworkCapabilities} represent states that a particular
3317 * network may never attain, and whether a network will attain these states
3318 * is unknown prior to bringing up the network so the framework does not
Chalard Jean4d660112018-06-04 16:52:49 +09003319 * know how to go about satisfying a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003320 *
3321 * <p>This method requires the caller to hold either the
3322 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3323 * or the ability to modify system settings as determined by
3324 * {@link android.provider.Settings.System#canWrite}.</p>
3325 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003326 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003327 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07003328 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003329 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003330 * @throws IllegalArgumentException if {@code request} contains either
3331 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3332 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003333 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003334 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003335 checkPendingIntentNotNull(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003336 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07003337 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003338 } catch (RemoteException e) {
3339 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003340 } catch (ServiceSpecificException e) {
3341 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003342 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003343 }
3344
3345 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003346 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
3347 * <p>
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003348 * This method has the same behavior as
3349 * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003350 * releasing network resources and disconnecting.
3351 *
3352 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3353 * PendingIntent passed to
3354 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
3355 * corresponding NetworkRequest you'd like to remove. Cannot be null.
3356 */
3357 public void releaseNetworkRequest(PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003358 checkPendingIntentNotNull(operation);
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003359 try {
3360 mService.releasePendingNetworkRequest(operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003361 } catch (RemoteException e) {
3362 throw e.rethrowFromSystemServer();
3363 }
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003364 }
3365
Hugo Benichie7678512017-05-09 15:19:01 +09003366 private static void checkPendingIntentNotNull(PendingIntent intent) {
3367 Preconditions.checkNotNull(intent, "PendingIntent cannot be null.");
3368 }
3369
3370 private static void checkCallbackNotNull(NetworkCallback callback) {
3371 Preconditions.checkNotNull(callback, "null NetworkCallback");
3372 }
3373
3374 private static void checkTimeout(int timeoutMs) {
3375 Preconditions.checkArgumentPositive(timeoutMs, "timeoutMs must be strictly positive.");
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003376 }
3377
3378 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07003379 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07003380 * {@link NetworkRequest}. The callbacks will continue to be called until
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003381 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003382 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003383 * @param request {@link NetworkRequest} describing this request.
3384 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3385 * networks change state.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003386 * The callback is invoked on the default internal Handler.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003387 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003388 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt6078b502014-06-11 16:05:07 -07003389 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003390 registerNetworkCallback(request, networkCallback, getDefaultHandler());
3391 }
3392
3393 /**
3394 * Registers to receive notifications about all networks which satisfy the given
3395 * {@link NetworkRequest}. The callbacks will continue to be called until
3396 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003397 *
3398 * @param request {@link NetworkRequest} describing this request.
3399 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3400 * networks change state.
3401 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003402 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003403 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Hugo Benichi2583ef02017-02-02 17:02:36 +09003404 public void registerNetworkCallback(
3405 NetworkRequest request, NetworkCallback networkCallback, Handler handler) {
3406 CallbackHandler cbHandler = new CallbackHandler(handler);
3407 NetworkCapabilities nc = request.networkCapabilities;
3408 sendRequestForNetwork(nc, networkCallback, 0, LISTEN, TYPE_NONE, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003409 }
3410
3411 /**
Paul Jensen694f2b82015-06-17 14:15:39 -04003412 * Registers a PendingIntent to be sent when a network is available which satisfies the given
3413 * {@link NetworkRequest}.
3414 *
3415 * This function behaves identically to the version that takes a NetworkCallback, but instead
3416 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
3417 * the request may outlive the calling application and get called back when a suitable
3418 * network is found.
3419 * <p>
3420 * The operation is an Intent broadcast that goes to a broadcast receiver that
3421 * you registered with {@link Context#registerReceiver} or through the
3422 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3423 * <p>
3424 * The operation Intent is delivered with two extras, a {@link Network} typed
3425 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3426 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
3427 * the original requests parameters.
3428 * <p>
3429 * If there is already a request for this Intent registered (with the equality of
3430 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
3431 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
3432 * <p>
3433 * The request may be released normally by calling
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003434 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Paul Jensen694f2b82015-06-17 14:15:39 -04003435 * @param request {@link NetworkRequest} describing this request.
3436 * @param operation Action to perform when the network is available (corresponds
3437 * to the {@link NetworkCallback#onAvailable} call. Typically
3438 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
3439 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003440 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Paul Jensen694f2b82015-06-17 14:15:39 -04003441 public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003442 checkPendingIntentNotNull(operation);
Paul Jensen694f2b82015-06-17 14:15:39 -04003443 try {
3444 mService.pendingListenForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003445 } catch (RemoteException e) {
3446 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003447 } catch (ServiceSpecificException e) {
3448 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003449 }
Paul Jensen694f2b82015-06-17 14:15:39 -04003450 }
3451
3452 /**
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003453 * Registers to receive notifications about changes in the system default network. The callbacks
3454 * will continue to be called until either the application exits or
3455 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Erik Klinea2d29402016-03-16 15:31:39 +09003456 *
3457 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3458 * system default network changes.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003459 * The callback is invoked on the default internal Handler.
Erik Klinea2d29402016-03-16 15:31:39 +09003460 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003461 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Erik Klinea2d29402016-03-16 15:31:39 +09003462 public void registerDefaultNetworkCallback(NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003463 registerDefaultNetworkCallback(networkCallback, getDefaultHandler());
3464 }
3465
3466 /**
3467 * Registers to receive notifications about changes in the system default network. The callbacks
3468 * will continue to be called until either the application exits or
3469 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003470 *
3471 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3472 * system default network changes.
3473 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003474 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003475 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Hugo Benichi2583ef02017-02-02 17:02:36 +09003476 public void registerDefaultNetworkCallback(NetworkCallback networkCallback, Handler handler) {
Erik Klinea2d29402016-03-16 15:31:39 +09003477 // This works because if the NetworkCapabilities are null,
3478 // ConnectivityService takes them from the default request.
3479 //
3480 // Since the capabilities are exactly the same as the default request's
3481 // capabilities, this request is guaranteed, at all times, to be
3482 // satisfied by the same network, if any, that satisfies the default
3483 // request, i.e., the system default network.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003484 CallbackHandler cbHandler = new CallbackHandler(handler);
Chalard Jean4d660112018-06-04 16:52:49 +09003485 sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0,
3486 REQUEST, TYPE_NONE, cbHandler);
Erik Klinea2d29402016-03-16 15:31:39 +09003487 }
3488
3489 /**
fengludb571472015-04-21 17:12:05 -07003490 * Requests bandwidth update for a given {@link Network} and returns whether the update request
3491 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
3492 * network connection for updated bandwidth information. The caller will be notified via
3493 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003494 * method assumes that the caller has previously called
3495 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network
3496 * changes.
fenglub15e72b2015-03-20 11:29:56 -07003497 *
fengluae519192015-04-27 14:28:04 -07003498 * @param network {@link Network} specifying which network you're interested.
fengludb571472015-04-21 17:12:05 -07003499 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglub15e72b2015-03-20 11:29:56 -07003500 */
fengludb571472015-04-21 17:12:05 -07003501 public boolean requestBandwidthUpdate(Network network) {
fenglub15e72b2015-03-20 11:29:56 -07003502 try {
fengludb571472015-04-21 17:12:05 -07003503 return mService.requestBandwidthUpdate(network);
fenglub15e72b2015-03-20 11:29:56 -07003504 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003505 throw e.rethrowFromSystemServer();
fenglub15e72b2015-03-20 11:29:56 -07003506 }
3507 }
3508
3509 /**
Hugo Benichidafed3d2017-03-06 09:17:06 +09003510 * Unregisters a {@code NetworkCallback} and possibly releases networks originating from
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003511 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and
3512 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls.
3513 * If the given {@code NetworkCallback} had previously been used with
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09003514 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
3515 * will be disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003516 *
Hugo Benichidafed3d2017-03-06 09:17:06 +09003517 * Notifications that would have triggered that {@code NetworkCallback} will immediately stop
3518 * triggering it as soon as this call returns.
3519 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003520 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003521 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003522 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
Hugo Benichie7678512017-05-09 15:19:01 +09003523 checkCallbackNotNull(networkCallback);
Hugo Benichidafed3d2017-03-06 09:17:06 +09003524 final List<NetworkRequest> reqs = new ArrayList<>();
3525 // Find all requests associated to this callback and stop callback triggers immediately.
3526 // Callback is reusable immediately. http://b/20701525, http://b/35921499.
3527 synchronized (sCallbacks) {
Hugo Benichi31c176d2017-06-17 13:14:12 +09003528 Preconditions.checkArgument(networkCallback.networkRequest != null,
3529 "NetworkCallback was not registered");
3530 Preconditions.checkArgument(networkCallback.networkRequest != ALREADY_UNREGISTERED,
3531 "NetworkCallback was already unregistered");
Hugo Benichidafed3d2017-03-06 09:17:06 +09003532 for (Map.Entry<NetworkRequest, NetworkCallback> e : sCallbacks.entrySet()) {
3533 if (e.getValue() == networkCallback) {
3534 reqs.add(e.getKey());
3535 }
3536 }
3537 // TODO: throw exception if callback was registered more than once (http://b/20701525).
3538 for (NetworkRequest r : reqs) {
3539 try {
3540 mService.releaseNetworkRequest(r);
3541 } catch (RemoteException e) {
3542 throw e.rethrowFromSystemServer();
3543 }
3544 // Only remove mapping if rpc was successful.
3545 sCallbacks.remove(r);
3546 }
Hugo Benichi31c176d2017-06-17 13:14:12 +09003547 networkCallback.networkRequest = ALREADY_UNREGISTERED;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003548 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003549 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003550
3551 /**
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003552 * Unregisters a callback previously registered via
3553 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3554 *
3555 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3556 * PendingIntent passed to
3557 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3558 * Cannot be null.
3559 */
3560 public void unregisterNetworkCallback(PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003561 checkPendingIntentNotNull(operation);
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003562 releaseNetworkRequest(operation);
3563 }
3564
3565 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003566 * Informs the system whether it should switch to {@code network} regardless of whether it is
3567 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
3568 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
3569 * the system default network regardless of any other network that's currently connected. If
3570 * {@code always} is true, then the choice is remembered, so that the next time the user
3571 * connects to this network, the system will switch to it.
3572 *
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003573 * @param network The network to accept.
3574 * @param accept Whether to accept the network even if unvalidated.
3575 * @param always Whether to remember this choice in the future.
3576 *
3577 * @hide
3578 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003579 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003580 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
3581 try {
3582 mService.setAcceptUnvalidated(network, accept, always);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003583 } catch (RemoteException e) {
3584 throw e.rethrowFromSystemServer();
3585 }
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003586 }
3587
3588 /**
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003589 * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is
3590 * only meaningful if the system is configured not to penalize such networks, e.g., if the
3591 * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code
3592 * NETWORK_AVOID_BAD_WIFI setting is unset}.
3593 *
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003594 * @param network The network to accept.
3595 *
3596 * @hide
3597 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003598 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003599 public void setAvoidUnvalidated(Network network) {
3600 try {
3601 mService.setAvoidUnvalidated(network);
3602 } catch (RemoteException e) {
3603 throw e.rethrowFromSystemServer();
3604 }
3605 }
3606
3607 /**
Lorenzo Colitti4734cdb2017-04-27 14:30:21 +09003608 * Requests that the system open the captive portal app on the specified network.
3609 *
3610 * @param network The network to log into.
3611 *
3612 * @hide
3613 */
3614 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
3615 public void startCaptivePortalApp(Network network) {
3616 try {
3617 mService.startCaptivePortalApp(network);
3618 } catch (RemoteException e) {
3619 throw e.rethrowFromSystemServer();
3620 }
3621 }
3622
3623 /**
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003624 * It is acceptable to briefly use multipath data to provide seamless connectivity for
3625 * time-sensitive user-facing operations when the system default network is temporarily
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003626 * unresponsive. The amount of data should be limited (less than one megabyte for every call to
3627 * this method), and the operation should be infrequent to ensure that data usage is limited.
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003628 *
3629 * An example of such an operation might be a time-sensitive foreground activity, such as a
3630 * voice command, that the user is performing while walking out of range of a Wi-Fi network.
3631 */
3632 public static final int MULTIPATH_PREFERENCE_HANDOVER = 1 << 0;
3633
3634 /**
3635 * It is acceptable to use small amounts of multipath data on an ongoing basis to provide
3636 * a backup channel for traffic that is primarily going over another network.
3637 *
3638 * An example might be maintaining backup connections to peers or servers for the purpose of
3639 * fast fallback if the default network is temporarily unresponsive or disconnects. The traffic
3640 * on backup paths should be negligible compared to the traffic on the main path.
3641 */
3642 public static final int MULTIPATH_PREFERENCE_RELIABILITY = 1 << 1;
3643
3644 /**
3645 * It is acceptable to use metered data to improve network latency and performance.
3646 */
3647 public static final int MULTIPATH_PREFERENCE_PERFORMANCE = 1 << 2;
3648
3649 /**
3650 * Return value to use for unmetered networks. On such networks we currently set all the flags
3651 * to true.
3652 * @hide
3653 */
3654 public static final int MULTIPATH_PREFERENCE_UNMETERED =
3655 MULTIPATH_PREFERENCE_HANDOVER |
3656 MULTIPATH_PREFERENCE_RELIABILITY |
3657 MULTIPATH_PREFERENCE_PERFORMANCE;
3658
3659 /** @hide */
3660 @Retention(RetentionPolicy.SOURCE)
3661 @IntDef(flag = true, value = {
3662 MULTIPATH_PREFERENCE_HANDOVER,
3663 MULTIPATH_PREFERENCE_RELIABILITY,
3664 MULTIPATH_PREFERENCE_PERFORMANCE,
3665 })
3666 public @interface MultipathPreference {
3667 }
3668
3669 /**
3670 * Provides a hint to the calling application on whether it is desirable to use the
3671 * multinetwork APIs (e.g., {@link Network#openConnection}, {@link Network#bindSocket}, etc.)
3672 * for multipath data transfer on this network when it is not the system default network.
3673 * Applications desiring to use multipath network protocols should call this method before
3674 * each such operation.
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003675 *
3676 * @param network The network on which the application desires to use multipath data.
3677 * If {@code null}, this method will return the a preference that will generally
3678 * apply to metered networks.
3679 * @return a bitwise OR of zero or more of the {@code MULTIPATH_PREFERENCE_*} constants.
3680 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003681 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003682 public @MultipathPreference int getMultipathPreference(Network network) {
3683 try {
3684 return mService.getMultipathPreference(network);
3685 } catch (RemoteException e) {
3686 throw e.rethrowFromSystemServer();
3687 }
3688 }
3689
3690 /**
Stuart Scott984dc852015-03-30 13:17:11 -07003691 * Resets all connectivity manager settings back to factory defaults.
3692 * @hide
3693 */
3694 public void factoryReset() {
Stuart Scott984dc852015-03-30 13:17:11 -07003695 try {
Stuart Scottf1fb3972015-04-02 18:00:02 -07003696 mService.factoryReset();
Stuart Scott984dc852015-03-30 13:17:11 -07003697 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003698 throw e.rethrowFromSystemServer();
Stuart Scott984dc852015-03-30 13:17:11 -07003699 }
3700 }
3701
3702 /**
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003703 * Binds the current process to {@code network}. All Sockets created in the future
3704 * (and not explicitly bound via a bound SocketFactory from
3705 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3706 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3707 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3708 * work and all host name resolutions will fail. This is by design so an application doesn't
3709 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3710 * To clear binding pass {@code null} for {@code network}. Using individually bound
3711 * Sockets created by Network.getSocketFactory().createSocket() and
3712 * performing network-specific host name resolutions via
3713 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen72db88e2015-03-10 10:54:12 -04003714 * {@code bindProcessToNetwork}.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003715 *
3716 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3717 * the current binding.
3718 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3719 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003720 public boolean bindProcessToNetwork(Network network) {
Chalard Jean4d660112018-06-04 16:52:49 +09003721 // Forcing callers to call through non-static function ensures ConnectivityManager
Paul Jensen72db88e2015-03-10 10:54:12 -04003722 // instantiated.
3723 return setProcessDefaultNetwork(network);
3724 }
3725
3726 /**
3727 * Binds the current process to {@code network}. All Sockets created in the future
3728 * (and not explicitly bound via a bound SocketFactory from
3729 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3730 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3731 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3732 * work and all host name resolutions will fail. This is by design so an application doesn't
3733 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3734 * To clear binding pass {@code null} for {@code network}. Using individually bound
3735 * Sockets created by Network.getSocketFactory().createSocket() and
3736 * performing network-specific host name resolutions via
3737 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
3738 * {@code setProcessDefaultNetwork}.
3739 *
3740 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3741 * the current binding.
3742 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3743 * @deprecated This function can throw {@link IllegalStateException}. Use
3744 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
3745 * is a direct replacement.
3746 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003747 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003748 public static boolean setProcessDefaultNetwork(Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003749 int netId = (network == null) ? NETID_UNSET : network.netId;
Paul Jensen72db88e2015-03-10 10:54:12 -04003750 if (netId == NetworkUtils.getBoundNetworkForProcess()) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003751 return true;
3752 }
3753 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05003754 // Set HTTP proxy system properties to match network.
3755 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colittiec4c5552015-04-22 11:52:48 +09003756 try {
3757 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
3758 } catch (SecurityException e) {
3759 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
3760 Log.e(TAG, "Can't set proxy properties", e);
3761 }
Paul Jensenc91b5342014-08-27 12:38:45 -04003762 // Must flush DNS cache as new network may have different DNS resolutions.
3763 InetAddress.clearDnsCache();
3764 // Must flush socket pool as idle sockets will be bound to previous network and may
3765 // cause subsequent fetches to be performed on old network.
3766 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
3767 return true;
3768 } else {
3769 return false;
3770 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003771 }
3772
3773 /**
3774 * Returns the {@link Network} currently bound to this process via
Paul Jensen72db88e2015-03-10 10:54:12 -04003775 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003776 *
3777 * @return {@code Network} to which this process is bound, or {@code null}.
3778 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003779 public Network getBoundNetworkForProcess() {
3780 // Forcing callers to call thru non-static function ensures ConnectivityManager
3781 // instantiated.
3782 return getProcessDefaultNetwork();
3783 }
3784
3785 /**
3786 * Returns the {@link Network} currently bound to this process via
3787 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
3788 *
3789 * @return {@code Network} to which this process is bound, or {@code null}.
3790 * @deprecated Using this function can lead to other functions throwing
3791 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
3792 * {@code getBoundNetworkForProcess} is a direct replacement.
3793 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003794 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003795 public static Network getProcessDefaultNetwork() {
Paul Jensen72db88e2015-03-10 10:54:12 -04003796 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04003797 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003798 return new Network(netId);
3799 }
3800
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003801 private void unsupportedStartingFrom(int version) {
3802 if (Process.myUid() == Process.SYSTEM_UID) {
Lorenzo Colitti2e31a7c2018-09-28 11:31:55 +09003803 // The getApplicationInfo() call we make below is not supported in system context. Let
3804 // the call through here, and rely on the fact that ConnectivityService will refuse to
3805 // allow the system to use these APIs anyway.
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003806 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() {
Dianne Hackborn692a2442015-07-31 10:35:34 -07003822 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003823 }
3824
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003825 /**
3826 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen72db88e2015-03-10 10:54:12 -04003827 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003828 *
3829 * @param network The {@link Network} to bind host resolutions from the current process to, or
3830 * {@code null} to clear the current binding.
3831 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3832 * @hide
3833 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
3834 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003835 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01003836 @UnsupportedAppUsage
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003837 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04003838 return NetworkUtils.bindProcessToNetworkForHostResolution(
Erik Klinef4fa9822018-04-27 22:48:33 +09003839 (network == null) ? NETID_UNSET : network.getNetIdForResolv());
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003840 }
Felipe Leme1b103232016-01-22 09:44:57 -08003841
3842 /**
3843 * Device is not restricting metered network activity while application is running on
3844 * background.
3845 */
3846 public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1;
3847
3848 /**
3849 * Device is restricting metered network activity while application is running on background,
3850 * but application is allowed to bypass it.
3851 * <p>
3852 * In this state, application should take action to mitigate metered network access.
3853 * For example, a music streaming application should switch to a low-bandwidth bitrate.
3854 */
3855 public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2;
3856
3857 /**
3858 * Device is restricting metered network activity while application is running on background.
Felipe Leme9778f762016-01-27 14:46:39 -08003859 * <p>
Felipe Leme1b103232016-01-22 09:44:57 -08003860 * In this state, application should not try to use the network while running on background,
3861 * because it would be denied.
3862 */
3863 public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3;
3864
Felipe Leme9778f762016-01-27 14:46:39 -08003865 /**
3866 * A change in the background metered network activity restriction has occurred.
3867 * <p>
3868 * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction
3869 * applies to them.
3870 * <p>
3871 * This is only sent to registered receivers, not manifest receivers.
3872 */
3873 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
3874 public static final String ACTION_RESTRICT_BACKGROUND_CHANGED =
3875 "android.net.conn.RESTRICT_BACKGROUND_CHANGED";
3876
Felipe Lemeecfccea2016-01-25 11:48:04 -08003877 /** @hide */
3878 @Retention(RetentionPolicy.SOURCE)
Felipe Leme1b103232016-01-22 09:44:57 -08003879 @IntDef(flag = false, value = {
3880 RESTRICT_BACKGROUND_STATUS_DISABLED,
3881 RESTRICT_BACKGROUND_STATUS_WHITELISTED,
3882 RESTRICT_BACKGROUND_STATUS_ENABLED,
3883 })
Felipe Leme1b103232016-01-22 09:44:57 -08003884 public @interface RestrictBackgroundStatus {
3885 }
3886
3887 private INetworkPolicyManager getNetworkPolicyManager() {
3888 synchronized (this) {
3889 if (mNPManager != null) {
3890 return mNPManager;
3891 }
3892 mNPManager = INetworkPolicyManager.Stub.asInterface(ServiceManager
3893 .getService(Context.NETWORK_POLICY_SERVICE));
3894 return mNPManager;
3895 }
3896 }
3897
3898 /**
3899 * Determines if the calling application is subject to metered network restrictions while
3900 * running on background.
Felipe Lemec9c7be52016-05-16 13:57:19 -07003901 *
3902 * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED},
3903 * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED},
3904 * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED}
Felipe Leme1b103232016-01-22 09:44:57 -08003905 */
3906 public @RestrictBackgroundStatus int getRestrictBackgroundStatus() {
3907 try {
3908 return getNetworkPolicyManager().getRestrictBackgroundByCaller();
3909 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003910 throw e.rethrowFromSystemServer();
Felipe Leme1b103232016-01-22 09:44:57 -08003911 }
3912 }
Ricky Wai44dcbde2018-01-23 04:09:45 +00003913
3914 /**
3915 * The network watchlist is a list of domains and IP addresses that are associated with
Ricky Waia86d5d52018-03-20 14:20:54 +00003916 * potentially harmful apps. This method returns the SHA-256 of the watchlist config file
3917 * currently used by the system for validation purposes.
Ricky Wai44dcbde2018-01-23 04:09:45 +00003918 *
3919 * @return Hash of network watchlist config file. Null if config does not exist.
3920 */
3921 public byte[] getNetworkWatchlistConfigHash() {
3922 try {
3923 return mService.getNetworkWatchlistConfigHash();
3924 } catch (RemoteException e) {
3925 Log.e(TAG, "Unable to get watchlist config hash");
3926 throw e.rethrowFromSystemServer();
3927 }
3928 }
Jeff Vander Stoep0ac2c092018-07-23 10:57:53 -07003929
3930 /**
3931 * Returns the {@code uid} of the owner of a network connection.
3932 *
3933 * @param protocol The protocol of the connection. Only {@code IPPROTO_TCP} and
3934 * {@code IPPROTO_UDP} currently supported.
3935 * @param local The local {@link InetSocketAddress} of a connection.
3936 * @param remote The remote {@link InetSocketAddress} of a connection.
3937 *
3938 * @return {@code uid} if the connection is found and the app has permission to observe it
3939 * (e.g., if it is associated with the calling VPN app's tunnel) or
3940 * {@link android.os.Process#INVALID_UID} if the connection is not found.
3941 */
3942 public int getConnectionOwnerUid(int protocol, InetSocketAddress local,
3943 InetSocketAddress remote) {
3944 ConnectionInfo connectionInfo = new ConnectionInfo(protocol, local, remote);
3945 try {
3946 return mService.getConnectionOwnerUid(connectionInfo);
3947 } catch (RemoteException e) {
3948 throw e.rethrowFromSystemServer();
3949 }
3950 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003951}