blob: 49c3dc63e15135da3f98e19f2749cbe6b509d525 [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";
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +090086 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070087
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070089 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 * been established or lost. The NetworkInfo for the affected network is
91 * sent as an extra; it should be consulted to see what kind of
92 * connectivity event occurred.
93 * <p/>
Mark Lu33ec1062016-12-05 10:57:55 -080094 * Apps targeting Android 7.0 (API level 24) and higher do not receive this
95 * broadcast if they declare the broadcast receiver in their manifest. Apps
96 * will still receive broadcasts if they register their
97 * {@link android.content.BroadcastReceiver} with
98 * {@link android.content.Context#registerReceiver Context.registerReceiver()}
99 * and that context is still valid.
100 * <p/>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800101 * If this is a connection that was the result of failing over from a
102 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
103 * set to true.
104 * <p/>
105 * For a loss of connectivity, if the connectivity manager is attempting
106 * to connect (or has already connected) to another network, the
107 * NetworkInfo for the new network is also passed as an extra. This lets
108 * any receivers of the broadcast know that they should not necessarily
109 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800110 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 * the failover attempt succeeded (and so there is still overall data
112 * connectivity), or that the failover attempt failed, meaning that all
113 * connectivity has been lost.
114 * <p/>
115 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
116 * is set to {@code true} if there are no connected networks at all.
Chalard Jean054cd162018-02-10 05:33:50 +0900117 *
118 * @deprecated apps should use the more versatile {@link #requestNetwork},
119 * {@link #registerNetworkCallback} or {@link #registerDefaultNetworkCallback}
120 * functions instead for faster and more detailed updates about the network
121 * changes they care about.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800123 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jean054cd162018-02-10 05:33:50 +0900124 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700126
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 /**
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700128 * A temporary hack until SUPL system can get off the legacy APIS.
129 * They do too many network requests and the long list of apps listening
Chalard Jean4d660112018-06-04 16:52:49 +0900130 * and waking due to the CONNECTIVITY_ACTION broadcast makes it expensive.
131 * Use this broadcast intent instead for SUPL requests.
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700132 * @hide
133 */
134 public static final String CONNECTIVITY_ACTION_SUPL =
135 "android.net.conn.CONNECTIVITY_CHANGE_SUPL";
136
137 /**
Paul Jensen25a217c2015-02-27 22:55:47 -0500138 * The device has connected to a network that has presented a captive
139 * portal, which is blocking Internet connectivity. The user was presented
140 * with a notification that network sign in is required,
141 * and the user invoked the notification's action indicating they
Paul Jensen49e3edf2015-05-22 10:50:39 -0400142 * desire to sign in to the network. Apps handling this activity should
Paul Jensen25a217c2015-02-27 22:55:47 -0500143 * facilitate signing in to the network. This action includes a
144 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents
145 * the network presenting the captive portal; all communication with the
146 * captive portal must be done using this {@code Network} object.
147 * <p/>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400148 * This activity includes a {@link CaptivePortal} extra named
149 * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different
150 * outcomes of the captive portal sign in to the system:
151 * <ul>
152 * <li> When the app handling this action believes the user has signed in to
153 * the network and the captive portal has been dismissed, the app should
154 * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can
155 * reevaluate the network. If reevaluation finds the network no longer
156 * subject to a captive portal, the network may become the default active
Chalard Jean4d660112018-06-04 16:52:49 +0900157 * data network.</li>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400158 * <li> When the app handling this action believes the user explicitly wants
Paul Jensen25a217c2015-02-27 22:55:47 -0500159 * to ignore the captive portal and the network, the app should call
Paul Jensen49e3edf2015-05-22 10:50:39 -0400160 * {@link CaptivePortal#ignoreNetwork}. </li>
161 * </ul>
Paul Jensen25a217c2015-02-27 22:55:47 -0500162 */
163 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
164 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL";
165
166 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800167 * The lookup key for a {@link NetworkInfo} object. Retrieve with
168 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700169 *
170 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
171 * should always obtain network information through
Paul Jensen3541e9f2015-03-18 12:23:02 -0400172 * {@link #getActiveNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700173 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700175 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700177
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700179 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700180 *
181 * @see android.content.Intent#getIntExtra(String, int)
182 */
183 public static final String EXTRA_NETWORK_TYPE = "networkType";
184
185 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 * The lookup key for a boolean that indicates whether a connect event
187 * is for a network to which the connectivity manager was failing over
188 * following a disconnect on another network.
189 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
junyulai3822c8a2018-12-13 12:47:51 +0800190 *
191 * @deprecated See {@link NetworkInfo}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 */
junyulai3822c8a2018-12-13 12:47:51 +0800193 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 public static final String EXTRA_IS_FAILOVER = "isFailover";
195 /**
196 * The lookup key for a {@link NetworkInfo} object. This is supplied when
197 * there is another network that it may be possible to connect to. Retrieve with
198 * {@link android.content.Intent#getParcelableExtra(String)}.
junyulai3822c8a2018-12-13 12:47:51 +0800199 *
200 * @deprecated See {@link NetworkInfo}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201 */
junyulai3822c8a2018-12-13 12:47:51 +0800202 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800203 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
204 /**
205 * The lookup key for a boolean that indicates whether there is a
206 * complete lack of connectivity, i.e., no network is available.
207 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
208 */
209 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
210 /**
211 * The lookup key for a string that indicates why an attempt to connect
212 * to a network failed. The string has no particular structure. It is
213 * intended to be used in notifications presented to users. Retrieve
214 * it with {@link android.content.Intent#getStringExtra(String)}.
215 */
216 public static final String EXTRA_REASON = "reason";
217 /**
218 * The lookup key for a string that provides optionally supplied
219 * extra information about the network state. The information
220 * may be passed up from the lower networking layers, and its
221 * meaning may be specific to a particular network type. Retrieve
222 * it with {@link android.content.Intent#getStringExtra(String)}.
junyulai3822c8a2018-12-13 12:47:51 +0800223 *
224 * @deprecated See {@link NetworkInfo#getExtraInfo()}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 */
junyulai3822c8a2018-12-13 12:47:51 +0800226 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700228 /**
229 * The lookup key for an int that provides information about
230 * our connection to the internet at large. 0 indicates no connection,
231 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700232 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700233 * {@hide}
234 */
235 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800236 /**
Paul Jensen49e3edf2015-05-22 10:50:39 -0400237 * The lookup key for a {@link CaptivePortal} object included with the
238 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal}
239 * object can be used to either indicate to the system that the captive
240 * portal has been dismissed or that the user does not want to pursue
241 * signing in to captive portal. Retrieve it with
242 * {@link android.content.Intent#getParcelableExtra(String)}.
Paul Jensen25a217c2015-02-27 22:55:47 -0500243 */
Paul Jensen49e3edf2015-05-22 10:50:39 -0400244 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL";
Jan Nordqvist52eb29f2015-09-22 15:54:32 -0700245
246 /**
247 * Key for passing a URL to the captive portal login activity.
248 */
249 public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL";
250
Paul Jensen25a217c2015-02-27 22:55:47 -0500251 /**
Remi NGUYEN VAN8255c2d2018-05-22 10:01:53 +0900252 * Key for passing a {@link android.net.captiveportal.CaptivePortalProbeSpec} to the captive
253 * portal login activity.
254 * {@hide}
255 */
256 public static final String EXTRA_CAPTIVE_PORTAL_PROBE_SPEC =
257 "android.net.extra.CAPTIVE_PORTAL_PROBE_SPEC";
258
259 /**
Hugo Benichicdf3ba42016-12-14 08:23:40 +0900260 * Key for passing a user agent string to the captive portal login activity.
261 * {@hide}
262 */
263 public static final String EXTRA_CAPTIVE_PORTAL_USER_AGENT =
264 "android.net.extra.CAPTIVE_PORTAL_USER_AGENT";
265
266 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700267 * Broadcast action to indicate the change of data activity status
268 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800269 * The network becomes active when data transmission is started, or
270 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700271 * {@hide}
272 */
273 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jean4d660112018-06-04 16:52:49 +0900274 public static final String ACTION_DATA_ACTIVITY_CHANGE =
275 "android.net.conn.DATA_ACTIVITY_CHANGE";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700276 /**
277 * The lookup key for an enum that indicates the network device type on which this data activity
278 * change happens.
279 * {@hide}
280 */
281 public static final String EXTRA_DEVICE_TYPE = "deviceType";
282 /**
283 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
284 * it is actively sending or receiving data and {@code false} means it is idle.
285 * {@hide}
286 */
287 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700288 /**
289 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
290 * {@hide}
291 */
292 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700293
294 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800295 * Broadcast Action: The setting for background data usage has changed
296 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
297 * <p>
298 * If an application uses the network in the background, it should listen
299 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700300 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800301 * <p>
302 *
303 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
304 * of background data depends on several combined factors, and
305 * this broadcast is no longer sent. Instead, when background
306 * data is unavailable, {@link #getActiveNetworkInfo()} will now
307 * appear disconnected. During first boot after a platform
308 * upgrade, this broadcast will be sent once if
309 * {@link #getBackgroundDataSetting()} was {@code false} before
310 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800311 */
312 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800313 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800314 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
315 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
316
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700317 /**
318 * Broadcast Action: The network connection may not be good
319 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
320 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
321 * the network and it's condition.
322 * @hide
323 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800324 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100325 @UnsupportedAppUsage
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700326 public static final String INET_CONDITION_ACTION =
327 "android.net.conn.INET_CONDITION_ACTION";
328
Robert Greenwalt42acef32009-08-12 16:08:25 -0700329 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800330 * Broadcast Action: A tetherable connection has come or gone.
331 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
Erik Kline8351faa2017-04-17 16:47:23 +0900332 * {@code ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY},
333 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER}, and
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800334 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
335 * the current state of tethering. Each include a list of
336 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800337 * @hide
338 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800339 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100340 @UnsupportedAppUsage
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800341 public static final String ACTION_TETHER_STATE_CHANGED =
342 "android.net.conn.TETHER_STATE_CHANGED";
343
344 /**
345 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800346 * gives a String[] listing all the interfaces configured for
347 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800348 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100349 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800350 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800351
352 /**
353 * @hide
Erik Kline8351faa2017-04-17 16:47:23 +0900354 * gives a String[] listing all the interfaces currently in local-only
355 * mode (ie, has DHCPv4+IPv6-ULA support and no packet forwarding)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800356 */
Erik Kline8351faa2017-04-17 16:47:23 +0900357 public static final String EXTRA_ACTIVE_LOCAL_ONLY = "localOnlyArray";
358
359 /**
360 * @hide
361 * gives a String[] listing all the interfaces currently tethered
362 * (ie, has DHCPv4 support and packets potentially forwarded/NATed)
363 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100364 @UnsupportedAppUsage
Erik Kline8351faa2017-04-17 16:47:23 +0900365 public static final String EXTRA_ACTIVE_TETHER = "tetherArray";
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800366
367 /**
368 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800369 * gives a String[] listing all the interfaces we tried to tether and
370 * failed. Use {@link #getLastTetherError} to find the error code
371 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800372 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100373 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800374 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800375
376 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800377 * Broadcast Action: The captive portal tracker has finished its test.
378 * Sent only while running Setup Wizard, in lieu of showing a user
379 * notification.
380 * @hide
381 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800382 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800383 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
384 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
385 /**
386 * The lookup key for a boolean that indicates whether a captive portal was detected.
387 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
388 * @hide
389 */
390 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
391
392 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900393 * Action used to display a dialog that asks the user whether to connect to a network that is
394 * not validated. This intent is used to start the dialog in settings via startActivity.
395 *
396 * @hide
397 */
398 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED";
399
400 /**
Lorenzo Colitti9be58c52016-09-15 14:02:29 +0900401 * Action used to display a dialog that asks the user whether to avoid a network that is no
402 * longer validated. This intent is used to start the dialog in settings via startActivity.
403 *
404 * @hide
405 */
406 public static final String ACTION_PROMPT_LOST_VALIDATION =
407 "android.net.conn.PROMPT_LOST_VALIDATION";
408
409 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800410 * Invalid tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900411 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800412 * @hide
413 */
414 public static final int TETHERING_INVALID = -1;
415
416 /**
417 * Wifi tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900418 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800419 * @hide
420 */
421 @SystemApi
422 public static final int TETHERING_WIFI = 0;
423
424 /**
425 * USB tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900426 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800427 * @hide
428 */
429 @SystemApi
430 public static final int TETHERING_USB = 1;
431
432 /**
433 * Bluetooth tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900434 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800435 * @hide
436 */
437 @SystemApi
438 public static final int TETHERING_BLUETOOTH = 2;
439
440 /**
441 * Extra used for communicating with the TetherService. Includes the type of tethering to
442 * enable if any.
443 * @hide
444 */
445 public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType";
446
447 /**
448 * Extra used for communicating with the TetherService. Includes the type of tethering for
449 * which to cancel provisioning.
450 * @hide
451 */
452 public static final String EXTRA_REM_TETHER_TYPE = "extraRemTetherType";
453
454 /**
455 * Extra used for communicating with the TetherService. True to schedule a recheck of tether
456 * provisioning.
457 * @hide
458 */
459 public static final String EXTRA_SET_ALARM = "extraSetAlarm";
460
461 /**
462 * Tells the TetherService to run a provision check now.
463 * @hide
464 */
465 public static final String EXTRA_RUN_PROVISION = "extraRunProvision";
466
467 /**
468 * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver}
469 * which will receive provisioning results. Can be left empty.
470 * @hide
471 */
472 public static final String EXTRA_PROVISION_CALLBACK = "extraProvisionCallback";
473
474 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800475 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700476 * @hide
477 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100478 @UnsupportedAppUsage
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700479 public static final int TYPE_NONE = -1;
480
481 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900482 * A Mobile 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_MOBILE = 0;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900490
Robert Greenwalt42acef32009-08-12 16:08:25 -0700491 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900492 * A WIFI data connection. Devices may support more than one.
493 *
494 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
495 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
496 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700497 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900498 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700499 public static final int TYPE_WIFI = 1;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900500
Robert Greenwalt42acef32009-08-12 16:08:25 -0700501 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800502 * An MMS-specific Mobile data connection. This network type may use the
503 * same network interface as {@link #TYPE_MOBILE} or it may use a different
504 * one. This is used by applications needing to talk to the carrier's
505 * Multimedia Messaging Service servers.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900506 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900507 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900508 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900509 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700510 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700511 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700512 public static final int TYPE_MOBILE_MMS = 2;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900513
Robert Greenwalt42acef32009-08-12 16:08:25 -0700514 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800515 * A SUPL-specific Mobile data connection. This network type may use the
516 * same network interface as {@link #TYPE_MOBILE} or it may use a different
517 * one. This is used by applications needing to talk to the carrier's
518 * Secure User Plane Location servers for help locating the device.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900519 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900520 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900521 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900522 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700523 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700524 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700525 public static final int TYPE_MOBILE_SUPL = 3;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900526
Robert Greenwalt42acef32009-08-12 16:08:25 -0700527 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800528 * A DUN-specific Mobile data connection. This network type may use the
529 * same network interface as {@link #TYPE_MOBILE} or it may use a different
530 * one. This is sometimes by the system when setting up an upstream connection
531 * for tethering so that the carrier is aware of DUN traffic.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900532 *
533 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
534 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
535 * provides the {@link NetworkCapabilities#NET_CAPABILITY_DUN} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700536 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900537 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700538 public static final int TYPE_MOBILE_DUN = 4;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900539
Robert Greenwalt42acef32009-08-12 16:08:25 -0700540 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800541 * A High Priority Mobile data connection. This network type uses the
542 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colittie285b432015-04-23 15:32:42 +0900543 * is different.
544 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900545 * @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.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700548 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700549 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700550 public static final int TYPE_MOBILE_HIPRI = 5;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900551
jsh8214deb2010-03-11 15:04:43 -0800552 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900553 * A WiMAX 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.
jsh8214deb2010-03-11 15:04:43 -0800558 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900559 @Deprecated
jsh8214deb2010-03-11 15:04:43 -0800560 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800561
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800562 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900563 * A Bluetooth data connection.
564 *
565 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
566 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
567 * appropriate network. {@see NetworkCapabilities} for supported transports.
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800568 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900569 @Deprecated
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800570 public static final int TYPE_BLUETOOTH = 7;
571
Robert Greenwalt60810842011-04-22 15:28:18 -0700572 /**
573 * Dummy data connection. This should not be used on shipping devices.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900574 * @deprecated This is not used any more.
Robert Greenwalt60810842011-04-22 15:28:18 -0700575 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900576 @Deprecated
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800577 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800578
Robert Greenwalt60810842011-04-22 15:28:18 -0700579 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900580 * An Ethernet data connection.
581 *
582 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
583 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
584 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt60810842011-04-22 15:28:18 -0700585 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900586 @Deprecated
Robert Greenwalte12aec92011-01-28 14:48:37 -0800587 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700588
Wink Saville9d7d6282011-03-12 14:52:01 -0800589 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800590 * Over the air Administration.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900591 * @deprecated Use {@link NetworkCapabilities} instead.
Wink Saville9d7d6282011-03-12 14:52:01 -0800592 * {@hide}
593 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900594 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100595 @UnsupportedAppUsage
Wink Saville9d7d6282011-03-12 14:52:01 -0800596 public static final int TYPE_MOBILE_FOTA = 10;
597
598 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800599 * IP Multimedia Subsystem.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900600 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IMS} instead.
Wink Saville9d7d6282011-03-12 14:52:01 -0800601 * {@hide}
602 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900603 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100604 @UnsupportedAppUsage
Wink Saville9d7d6282011-03-12 14:52:01 -0800605 public static final int TYPE_MOBILE_IMS = 11;
606
607 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800608 * Carrier Branded Services.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900609 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_CBS} instead.
Wink Saville9d7d6282011-03-12 14:52:01 -0800610 * {@hide}
611 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900612 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100613 @UnsupportedAppUsage
Wink Saville9d7d6282011-03-12 14:52:01 -0800614 public static final int TYPE_MOBILE_CBS = 12;
615
repo syncaea743a2011-07-29 23:55:49 -0700616 /**
617 * A Wi-Fi p2p connection. Only requesting processes will have access to
618 * the peers connected.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900619 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_WIFI_P2P} instead.
repo syncaea743a2011-07-29 23:55:49 -0700620 * {@hide}
621 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900622 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100623 @UnsupportedAppUsage
repo syncaea743a2011-07-29 23:55:49 -0700624 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800625
Wink Saville5e56bc52013-07-29 15:00:57 -0700626 /**
627 * The network to use for initially attaching to the network
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900628 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IA} instead.
Wink Saville5e56bc52013-07-29 15:00:57 -0700629 * {@hide}
630 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900631 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100632 @UnsupportedAppUsage
Wink Saville5e56bc52013-07-29 15:00:57 -0700633 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700634
Lorenzo Colittie285b432015-04-23 15:32:42 +0900635 /**
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700636 * Emergency PDN connection for emergency services. This
637 * may include IMS and MMS in emergency situations.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900638 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_EIMS} instead.
Ram3e0e3bc2014-06-26 11:03:44 -0700639 * {@hide}
640 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900641 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100642 @UnsupportedAppUsage
Ram3e0e3bc2014-06-26 11:03:44 -0700643 public static final int TYPE_MOBILE_EMERGENCY = 15;
644
Hui Lu1c5624a2014-01-15 11:05:36 -0500645 /**
646 * The network that uses proxy to achieve connectivity.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900647 * @deprecated Use {@link NetworkCapabilities} instead.
Hui Lu1c5624a2014-01-15 11:05:36 -0500648 * {@hide}
649 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900650 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100651 @UnsupportedAppUsage
Hui Lu1c5624a2014-01-15 11:05:36 -0500652 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700653
Robert Greenwalt8283f882014-07-07 17:09:01 -0700654 /**
655 * A virtual network using one or more native bearers.
656 * It may or may not be providing security services.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900657 * @deprecated Applications should use {@link NetworkCapabilities#TRANSPORT_VPN} instead.
Robert Greenwalt8283f882014-07-07 17:09:01 -0700658 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900659 @Deprecated
Robert Greenwalt8283f882014-07-07 17:09:01 -0700660 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500661
662 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700663 public static final int MAX_RADIO_TYPE = TYPE_VPN;
664
665 /** {@hide} */
666 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800667
Hugo Benichi16f0a942017-06-20 14:07:59 +0900668 private static final int MIN_NETWORK_TYPE = TYPE_MOBILE;
669
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800670 /**
671 * If you want to set the default network preference,you can directly
672 * change the networkAttributes array in framework's config.xml.
673 *
674 * @deprecated Since we support so many more networks now, the single
675 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800676 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800677 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800678 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800679 * from an App.
680 */
681 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800682 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
683
Jeff Sharkey625239a2012-09-26 22:03:49 -0700684 /**
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700685 * @hide
686 */
Hugo Benichia5c1f7f2017-06-20 14:10:14 +0900687 public static final int REQUEST_ID_UNSET = 0;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700688
Paul Jensen5d59e782014-07-11 12:28:19 -0400689 /**
Hugo Benichi31c176d2017-06-17 13:14:12 +0900690 * Static unique request used as a tombstone for NetworkCallbacks that have been unregistered.
691 * This allows to distinguish when unregistering NetworkCallbacks those that were never
Chalard Jean4d660112018-06-04 16:52:49 +0900692 * registered from those that were already unregistered.
Hugo Benichi31c176d2017-06-17 13:14:12 +0900693 * @hide
694 */
Hugo Benichia5c1f7f2017-06-20 14:10:14 +0900695 private static final NetworkRequest ALREADY_UNREGISTERED =
Hugo Benichi31c176d2017-06-17 13:14:12 +0900696 new NetworkRequest.Builder().clearCapabilities().build();
697
698 /**
Paul Jensen5d59e782014-07-11 12:28:19 -0400699 * A NetID indicating no Network is selected.
700 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
701 * @hide
702 */
703 public static final int NETID_UNSET = 0;
704
Erik Kline4d092232017-10-30 15:29:44 +0900705 /**
706 * Private DNS Mode values.
707 *
708 * The "private_dns_mode" global setting stores a String value which is
709 * expected to be one of the following.
710 */
711
712 /**
713 * @hide
714 */
715 public static final String PRIVATE_DNS_MODE_OFF = "off";
716 /**
717 * @hide
718 */
719 public static final String PRIVATE_DNS_MODE_OPPORTUNISTIC = "opportunistic";
720 /**
721 * @hide
722 */
723 public static final String PRIVATE_DNS_MODE_PROVIDER_HOSTNAME = "hostname";
724 /**
725 * The default Private DNS mode.
726 *
727 * This may change from release to release or may become dependent upon
728 * the capabilities of the underlying platform.
729 *
730 * @hide
731 */
Erik Kline19841792018-05-16 16:41:57 +0900732 public static final String PRIVATE_DNS_DEFAULT_MODE_FALLBACK = PRIVATE_DNS_MODE_OPPORTUNISTIC;
Erik Kline4d092232017-10-30 15:29:44 +0900733
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100734 @UnsupportedAppUsage
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700735 private final IConnectivityManager mService;
Paul Jensene0bef712014-12-10 15:12:18 -0500736 /**
737 * A kludge to facilitate static access where a Context pointer isn't available, like in the
738 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
739 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
740 * methods that take a Context argument.
741 */
742 private static ConnectivityManager sInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900744 private final Context mContext;
745
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800746 private INetworkManagementService mNMService;
Felipe Leme1b103232016-01-22 09:44:57 -0800747 private INetworkPolicyManager mNPManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800748
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800749 /**
750 * Tests if a given integer represents a valid network type.
751 * @param networkType the type to be tested
752 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensen9e59e122015-05-06 10:42:25 -0400753 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
754 * validate a network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800755 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700756 @Deprecated
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700757 public static boolean isNetworkTypeValid(int networkType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900758 return MIN_NETWORK_TYPE <= networkType && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800759 }
760
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800761 /**
762 * Returns a non-localized string representing a given network type.
763 * ONLY used for debugging output.
764 * @param type the type needing naming
765 * @return a String for the given type, or a string version of the type ("87")
766 * if no name is known.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900767 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800768 * {@hide}
769 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900770 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100771 @UnsupportedAppUsage
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700772 public static String getNetworkTypeName(int type) {
773 switch (type) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900774 case TYPE_NONE:
775 return "NONE";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700776 case TYPE_MOBILE:
777 return "MOBILE";
778 case TYPE_WIFI:
779 return "WIFI";
780 case TYPE_MOBILE_MMS:
781 return "MOBILE_MMS";
782 case TYPE_MOBILE_SUPL:
783 return "MOBILE_SUPL";
784 case TYPE_MOBILE_DUN:
785 return "MOBILE_DUN";
786 case TYPE_MOBILE_HIPRI:
787 return "MOBILE_HIPRI";
788 case TYPE_WIMAX:
789 return "WIMAX";
790 case TYPE_BLUETOOTH:
791 return "BLUETOOTH";
792 case TYPE_DUMMY:
793 return "DUMMY";
794 case TYPE_ETHERNET:
795 return "ETHERNET";
796 case TYPE_MOBILE_FOTA:
797 return "MOBILE_FOTA";
798 case TYPE_MOBILE_IMS:
799 return "MOBILE_IMS";
800 case TYPE_MOBILE_CBS:
801 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700802 case TYPE_WIFI_P2P:
803 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700804 case TYPE_MOBILE_IA:
805 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700806 case TYPE_MOBILE_EMERGENCY:
807 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500808 case TYPE_PROXY:
809 return "PROXY";
Erik Kline37fbfa12014-11-19 17:23:41 +0900810 case TYPE_VPN:
811 return "VPN";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700812 default:
813 return Integer.toString(type);
814 }
815 }
816
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800817 /**
818 * Checks if a given type uses the cellular data connection.
819 * This should be replaced in the future by a network property.
820 * @param networkType the type to check
821 * @return a boolean - {@code true} if uses cellular network, else {@code false}
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900822 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800823 * {@hide}
824 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900825 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100826 @UnsupportedAppUsage
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700827 public static boolean isNetworkTypeMobile(int networkType) {
828 switch (networkType) {
829 case TYPE_MOBILE:
830 case TYPE_MOBILE_MMS:
831 case TYPE_MOBILE_SUPL:
832 case TYPE_MOBILE_DUN:
833 case TYPE_MOBILE_HIPRI:
834 case TYPE_MOBILE_FOTA:
835 case TYPE_MOBILE_IMS:
836 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700837 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700838 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700839 return true;
840 default:
841 return false;
842 }
843 }
844
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800845 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700846 * Checks if the given network type is backed by a Wi-Fi radio.
847 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900848 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700849 * @hide
850 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900851 @Deprecated
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700852 public static boolean isNetworkTypeWifi(int networkType) {
853 switch (networkType) {
854 case TYPE_WIFI:
855 case TYPE_WIFI_P2P:
856 return true;
857 default:
858 return false;
859 }
860 }
861
862 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800863 * Specifies the preferred network type. When the device has more
864 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800865 *
866 * @param preference the network type to prefer over all others. It is
867 * unspecified what happens to the old preferred network in the
868 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700869 * @deprecated Functionality has been removed as it no longer makes sense,
870 * with many more than two networks - we'd need an array to express
871 * preference. Instead we use dynamic network properties of
872 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800873 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700874 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800875 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800876 }
877
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800878 /**
879 * Retrieves the current preferred network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800880 *
881 * @return an integer representing the preferred network type
882 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700883 * @deprecated Functionality has been removed as it no longer makes sense,
884 * with many more than two networks - we'd need an array to express
885 * preference. Instead we use dynamic network properties of
886 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800887 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700888 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600889 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800890 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700891 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800892 }
893
Scott Main671644c2011-10-06 19:02:28 -0700894 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800895 * Returns details about the currently active default data network. When
896 * connected, this network is the default route for outgoing connections.
897 * You should always check {@link NetworkInfo#isConnected()} before initiating
898 * network traffic. This may return {@code null} when there is no default
899 * network.
Chalard Jean5a041d12018-03-29 17:45:24 +0900900 * Note that if the default network is a VPN, this method will return the
901 * NetworkInfo for one of its underlying networks instead, or null if the
902 * VPN agent did not specify any. Apps interested in learning about VPNs
903 * should use {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800904 *
905 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500906 * or {@code null} if no default network is currently active
junyulai3822c8a2018-12-13 12:47:51 +0800907 * @deprecated See {@link NetworkInfo}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700908 */
junyulai3822c8a2018-12-13 12:47:51 +0800909 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600910 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800911 public NetworkInfo getActiveNetworkInfo() {
912 try {
913 return mService.getActiveNetworkInfo();
914 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700915 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800916 }
917 }
918
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800919 /**
Paul Jensen31a94f42015-02-13 14:18:39 -0500920 * Returns a {@link Network} object corresponding to the currently active
921 * default data network. In the event that the current active default data
922 * network disconnects, the returned {@code Network} object will no longer
923 * be usable. This will return {@code null} when there is no default
924 * network.
925 *
926 * @return a {@link Network} object for the current default network or
927 * {@code null} if no default network is currently active
Paul Jensen31a94f42015-02-13 14:18:39 -0500928 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600929 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Paul Jensen31a94f42015-02-13 14:18:39 -0500930 public Network getActiveNetwork() {
931 try {
932 return mService.getActiveNetwork();
933 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700934 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -0500935 }
936 }
937
938 /**
Robin Leed2baf792016-03-24 12:07:00 +0000939 * Returns a {@link Network} object corresponding to the currently active
940 * default data network for a specific UID. In the event that the default data
941 * network disconnects, the returned {@code Network} object will no longer
942 * be usable. This will return {@code null} when there is no default
943 * network for the UID.
Robin Leed2baf792016-03-24 12:07:00 +0000944 *
945 * @return a {@link Network} object for the current default network for the
946 * given UID or {@code null} if no default network is currently active
947 *
948 * @hide
949 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600950 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Robin Leed2baf792016-03-24 12:07:00 +0000951 public Network getActiveNetworkForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600952 return getActiveNetworkForUid(uid, false);
953 }
954
955 /** {@hide} */
956 public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) {
Robin Leed2baf792016-03-24 12:07:00 +0000957 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600958 return mService.getActiveNetworkForUid(uid, ignoreBlocked);
Robin Leed2baf792016-03-24 12:07:00 +0000959 } catch (RemoteException e) {
960 throw e.rethrowFromSystemServer();
961 }
962 }
963
964 /**
Charles He36738632017-05-15 17:07:18 +0100965 * Checks if a VPN app supports always-on mode.
966 *
967 * In order to support the always-on feature, an app has to
968 * <ul>
969 * <li>target {@link VERSION_CODES#N API 24} or above, and
Charles Hec57a01c2017-08-15 15:30:22 +0100970 * <li>not opt out through the {@link VpnService#SERVICE_META_DATA_SUPPORTS_ALWAYS_ON}
971 * meta-data field.
Charles He36738632017-05-15 17:07:18 +0100972 * </ul>
973 *
974 * @param userId The identifier of the user for whom the VPN app is installed.
975 * @param vpnPackage The canonical package name of the VPN app.
976 * @return {@code true} if and only if the VPN app exists and supports always-on mode.
977 * @hide
978 */
979 public boolean isAlwaysOnVpnPackageSupportedForUser(int userId, @Nullable String vpnPackage) {
980 try {
981 return mService.isAlwaysOnVpnPackageSupported(userId, vpnPackage);
982 } catch (RemoteException e) {
983 throw e.rethrowFromSystemServer();
984 }
985 }
986
987 /**
Robin Lee244ce8e2016-01-05 18:03:46 +0000988 * Configures an always-on VPN connection through a specific application.
989 * This connection is automatically granted and persisted after a reboot.
990 *
991 * <p>The designated package should declare a {@link VpnService} in its
992 * manifest guarded by {@link android.Manifest.permission.BIND_VPN_SERVICE},
993 * otherwise the call will fail.
994 *
995 * @param userId The identifier of the user to set an always-on VPN for.
996 * @param vpnPackage The package name for an installed VPN app on the device, or {@code null}
997 * to remove an existing always-on VPN configuration.
Robin Leedc679712016-05-03 13:23:03 +0100998 * @param lockdownEnabled {@code true} to disallow networking when the VPN is not connected or
999 * {@code false} otherwise.
Robin Lee244ce8e2016-01-05 18:03:46 +00001000 * @return {@code true} if the package is set as always-on VPN controller;
1001 * {@code false} otherwise.
1002 * @hide
1003 */
Robin Leedc679712016-05-03 13:23:03 +01001004 public boolean setAlwaysOnVpnPackageForUser(int userId, @Nullable String vpnPackage,
1005 boolean lockdownEnabled) {
Robin Lee244ce8e2016-01-05 18:03:46 +00001006 try {
Robin Leedc679712016-05-03 13:23:03 +01001007 return mService.setAlwaysOnVpnPackage(userId, vpnPackage, lockdownEnabled);
Robin Lee244ce8e2016-01-05 18:03:46 +00001008 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001009 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +00001010 }
1011 }
1012
1013 /**
1014 * Returns the package name of the currently set always-on VPN application.
1015 * If there is no always-on VPN set, or the VPN is provided by the system instead
1016 * of by an app, {@code null} will be returned.
1017 *
1018 * @return Package name of VPN controller responsible for always-on VPN,
1019 * or {@code null} if none is set.
1020 * @hide
1021 */
1022 public String getAlwaysOnVpnPackageForUser(int userId) {
1023 try {
1024 return mService.getAlwaysOnVpnPackage(userId);
1025 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001026 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +00001027 }
1028 }
1029
1030 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001031 * Returns details about the currently active default data network
1032 * for a given uid. This is for internal use only to avoid spying
1033 * other apps.
1034 *
1035 * @return a {@link NetworkInfo} object for the current default network
1036 * for the given uid or {@code null} if no default network is
1037 * available for the specified uid.
1038 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001039 * {@hide}
1040 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001041 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001042 @UnsupportedAppUsage
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001043 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001044 return getActiveNetworkInfoForUid(uid, false);
1045 }
1046
1047 /** {@hide} */
1048 public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001049 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001050 return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001051 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001052 throw e.rethrowFromSystemServer();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001053 }
1054 }
1055
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001056 /**
1057 * Returns connection status information about a particular
1058 * network type.
1059 *
1060 * @param networkType integer specifying which networkType in
1061 * which you're interested.
1062 * @return a {@link NetworkInfo} object for the requested
1063 * network type or {@code null} if the type is not
Chalard Jean5a041d12018-03-29 17:45:24 +09001064 * supported by the device. If {@code networkType} is
1065 * TYPE_VPN and a VPN is active for the calling app,
1066 * then this method will try to return one of the
1067 * underlying networks for the VPN or null if the
1068 * VPN agent didn't specify any.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001069 *
Paul Jensen3541e9f2015-03-18 12:23:02 -04001070 * @deprecated This method does not support multiple connected networks
1071 * of the same type. Use {@link #getAllNetworks} and
1072 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001073 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001074 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001075 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001076 public NetworkInfo getNetworkInfo(int networkType) {
1077 try {
1078 return mService.getNetworkInfo(networkType);
1079 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001080 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001081 }
1082 }
1083
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001084 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001085 * Returns connection status information about a particular
1086 * Network.
1087 *
1088 * @param network {@link Network} specifying which network
1089 * in which you're interested.
1090 * @return a {@link NetworkInfo} object for the requested
1091 * network or {@code null} if the {@code Network}
1092 * is not valid.
junyulai3822c8a2018-12-13 12:47:51 +08001093 * @deprecated See {@link NetworkInfo}.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001094 */
junyulai3822c8a2018-12-13 12:47:51 +08001095 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001096 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001097 public NetworkInfo getNetworkInfo(Network network) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001098 return getNetworkInfoForUid(network, Process.myUid(), false);
1099 }
1100
1101 /** {@hide} */
1102 public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001103 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001104 return mService.getNetworkInfoForUid(network, uid, ignoreBlocked);
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001105 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001106 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001107 }
1108 }
1109
1110 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001111 * Returns connection status information about all network
1112 * types supported by the device.
1113 *
1114 * @return an array of {@link NetworkInfo} objects. Check each
1115 * {@link NetworkInfo#getType} for which type each applies.
1116 *
Paul Jensen3541e9f2015-03-18 12:23:02 -04001117 * @deprecated This method does not support multiple connected networks
1118 * of the same type. Use {@link #getAllNetworks} and
1119 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001120 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001121 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001122 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 public NetworkInfo[] getAllNetworkInfo() {
1124 try {
1125 return mService.getAllNetworkInfo();
1126 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001127 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001128 }
1129 }
1130
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001131 /**
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001132 * Returns the {@link Network} object currently serving a given type, or
1133 * null if the given type is not connected.
1134 *
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001135 * @hide
Paul Jensen3541e9f2015-03-18 12:23:02 -04001136 * @deprecated This method does not support multiple connected networks
1137 * of the same type. Use {@link #getAllNetworks} and
1138 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001139 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001140 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001141 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001142 @UnsupportedAppUsage
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001143 public Network getNetworkForType(int networkType) {
1144 try {
1145 return mService.getNetworkForType(networkType);
1146 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001147 throw e.rethrowFromSystemServer();
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001148 }
1149 }
1150
1151 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001152 * Returns an array of all {@link Network} currently tracked by the
1153 * framework.
Paul Jensenb2748922015-05-06 11:10:18 -04001154 *
1155 * @return an array of {@link Network} objects.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001156 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001157 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001158 public Network[] getAllNetworks() {
1159 try {
1160 return mService.getAllNetworks();
1161 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001162 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001163 }
1164 }
1165
1166 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001167 * Returns an array of {@link android.net.NetworkCapabilities} objects, representing
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001168 * the Networks that applications run by the given user will use by default.
1169 * @hide
1170 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001171 @UnsupportedAppUsage
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001172 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
1173 try {
1174 return mService.getDefaultNetworkCapabilitiesForUser(userId);
1175 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001176 throw e.rethrowFromSystemServer();
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001177 }
1178 }
1179
1180 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001181 * Returns the IP information for the current default network.
1182 *
1183 * @return a {@link LinkProperties} object describing the IP info
1184 * for the current default network, or {@code null} if there
1185 * is no current default network.
1186 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001187 * {@hide}
1188 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001189 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001190 @UnsupportedAppUsage
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001191 public LinkProperties getActiveLinkProperties() {
1192 try {
1193 return mService.getActiveLinkProperties();
1194 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001195 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001196 }
1197 }
1198
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001199 /**
1200 * Returns the IP information for a given network type.
1201 *
1202 * @param networkType the network type of interest.
1203 * @return a {@link LinkProperties} object describing the IP info
1204 * for the given networkType, or {@code null} if there is
1205 * no current default network.
1206 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001207 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04001208 * @deprecated This method does not support multiple connected networks
1209 * of the same type. Use {@link #getAllNetworks},
1210 * {@link #getNetworkInfo(android.net.Network)}, and
1211 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001212 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001213 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001214 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001215 @UnsupportedAppUsage
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001216 public LinkProperties getLinkProperties(int networkType) {
1217 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001218 return mService.getLinkPropertiesForType(networkType);
1219 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001220 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001221 }
1222 }
1223
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001224 /**
1225 * Get the {@link LinkProperties} for the given {@link Network}. This
1226 * will return {@code null} if the network is unknown.
1227 *
1228 * @param network The {@link Network} object identifying the network in question.
1229 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensenb2748922015-05-06 11:10:18 -04001230 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001231 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt9258c642014-03-26 16:47:06 -07001232 public LinkProperties getLinkProperties(Network network) {
1233 try {
1234 return mService.getLinkProperties(network);
1235 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001236 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001237 }
1238 }
1239
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001240 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001241 * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001242 * will return {@code null} if the network is unknown.
1243 *
1244 * @param network The {@link Network} object identifying the network in question.
Lorenzo Colittie285b432015-04-23 15:32:42 +09001245 * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001246 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001247 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt9258c642014-03-26 16:47:06 -07001248 public NetworkCapabilities getNetworkCapabilities(Network network) {
1249 try {
1250 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001251 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001252 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001253 }
1254 }
1255
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001256 /**
Udam Sainib7c24872016-01-04 12:16:14 -08001257 * Gets the URL that should be used for resolving whether a captive portal is present.
1258 * 1. This URL should respond with a 204 response to a GET request to indicate no captive
1259 * portal is present.
1260 * 2. This URL must be HTTP as redirect responses are used to find captive portal
1261 * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects.
1262 *
1263 * @hide
1264 */
1265 @SystemApi
Udam Saini0e94c362017-06-07 12:06:28 -07001266 @RequiresPermission(android.Manifest.permission.LOCAL_MAC_ADDRESS)
Udam Sainib7c24872016-01-04 12:16:14 -08001267 public String getCaptivePortalServerUrl() {
1268 try {
1269 return mService.getCaptivePortalServerUrl();
1270 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001271 throw e.rethrowFromSystemServer();
Udam Sainib7c24872016-01-04 12:16:14 -08001272 }
1273 }
1274
1275 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001276 * Tells the underlying networking system that the caller wants to
1277 * begin using the named feature. The interpretation of {@code feature}
1278 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001279 *
1280 * <p>This method requires the caller to hold either the
1281 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1282 * or the ability to modify system settings as determined by
1283 * {@link android.provider.Settings.System#canWrite}.</p>
1284 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 * @param networkType specifies which network the request pertains to
1286 * @param feature the name of the feature to be used
1287 * @return an integer value representing the outcome of the request.
1288 * The interpretation of this value is specific to each networking
1289 * implementation+feature combination, except that the value {@code -1}
1290 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001291 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001292 * @deprecated Deprecated in favor of the cleaner
1293 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001294 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001295 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001296 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001298 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001299 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001300 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001301 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1302 if (netCap == null) {
1303 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
1304 feature);
1305 return PhoneConstants.APN_REQUEST_FAILED;
1306 }
1307
1308 NetworkRequest request = null;
1309 synchronized (sLegacyRequests) {
1310 LegacyRequest l = sLegacyRequests.get(netCap);
1311 if (l != null) {
1312 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
1313 renewRequestLocked(l);
1314 if (l.currentNetwork != null) {
1315 return PhoneConstants.APN_ALREADY_ACTIVE;
1316 } else {
1317 return PhoneConstants.APN_REQUEST_STARTED;
1318 }
1319 }
1320
1321 request = requestNetworkForFeatureLocked(netCap);
1322 }
1323 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -07001324 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001325 return PhoneConstants.APN_REQUEST_STARTED;
1326 } else {
1327 Log.d(TAG, " request Failed");
1328 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001329 }
1330 }
1331
1332 /**
1333 * Tells the underlying networking system that the caller is finished
1334 * using the named feature. The interpretation of {@code feature}
1335 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001336 *
1337 * <p>This method requires the caller to hold either the
1338 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1339 * or the ability to modify system settings as determined by
1340 * {@link android.provider.Settings.System#canWrite}.</p>
1341 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 * @param networkType specifies which network the request pertains to
1343 * @param feature the name of the feature that is no longer needed
1344 * @return an integer value representing the outcome of the request.
1345 * The interpretation of this value is specific to each networking
1346 * implementation+feature combination, except that the value {@code -1}
1347 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001348 *
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09001349 * @deprecated Deprecated in favor of the cleaner
1350 * {@link #unregisterNetworkCallback(NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001351 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001352 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001353 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001354 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001355 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001356 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001357 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001358 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1359 if (netCap == null) {
1360 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
1361 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001362 return -1;
1363 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001364
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001365 if (removeRequestForFeature(netCap)) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001366 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001367 }
1368 return 1;
1369 }
1370
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001371 @UnsupportedAppUsage
Robert Greenwalt562cc542014-05-15 18:07:26 -07001372 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1373 if (networkType == TYPE_MOBILE) {
Erik Kline35bf06c2017-01-26 18:08:28 +09001374 switch (feature) {
1375 case "enableCBS":
1376 return networkCapabilitiesForType(TYPE_MOBILE_CBS);
1377 case "enableDUN":
1378 case "enableDUNAlways":
1379 return networkCapabilitiesForType(TYPE_MOBILE_DUN);
1380 case "enableFOTA":
1381 return networkCapabilitiesForType(TYPE_MOBILE_FOTA);
1382 case "enableHIPRI":
1383 return networkCapabilitiesForType(TYPE_MOBILE_HIPRI);
1384 case "enableIMS":
1385 return networkCapabilitiesForType(TYPE_MOBILE_IMS);
1386 case "enableMMS":
1387 return networkCapabilitiesForType(TYPE_MOBILE_MMS);
1388 case "enableSUPL":
1389 return networkCapabilitiesForType(TYPE_MOBILE_SUPL);
1390 default:
1391 return null;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001392 }
Erik Kline35bf06c2017-01-26 18:08:28 +09001393 } else if (networkType == TYPE_WIFI && "p2p".equals(feature)) {
1394 return networkCapabilitiesForType(TYPE_WIFI_P2P);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001395 }
1396 return null;
1397 }
1398
Robert Greenwalt06314e42014-10-29 14:04:06 -07001399 /**
1400 * Guess what the network request was trying to say so that the resulting
1401 * network is accessible via the legacy (deprecated) API such as
1402 * requestRouteToHost.
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001403 *
1404 * This means we should try to be fairly precise about transport and
Robert Greenwalt06314e42014-10-29 14:04:06 -07001405 * capability but ignore things such as networkSpecifier.
1406 * If the request has more than one transport or capability it doesn't
1407 * match the old legacy requests (they selected only single transport/capability)
1408 * so this function cannot map the request to a single legacy type and
1409 * the resulting network will not be available to the legacy APIs.
1410 *
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001411 * This code is only called from the requestNetwork API (L and above).
1412 *
1413 * Setting a legacy type causes CONNECTIVITY_ACTION broadcasts, which are expensive
1414 * because they wake up lots of apps - see http://b/23350688 . So we currently only
1415 * do this for SUPL requests, which are the only ones that we know need it. If
1416 * omitting these broadcasts causes unacceptable app breakage, then for backwards
1417 * compatibility we can send them:
1418 *
1419 * if (targetSdkVersion < Build.VERSION_CODES.M) && // legacy API unsupported >= M
1420 * targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP)) // requestNetwork not present < L
1421 *
Robert Greenwalt06314e42014-10-29 14:04:06 -07001422 * TODO - This should be removed when the legacy APIs are removed.
1423 */
Ye Wenb87875e2014-07-21 14:19:01 -07001424 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1425 if (netCap == null) {
1426 return TYPE_NONE;
1427 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001428
Ye Wenb87875e2014-07-21 14:19:01 -07001429 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1430 return TYPE_NONE;
1431 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001432
Lifu Tang30f95a72016-01-07 23:20:38 -08001433 // Do this only for SUPL, until GnssLocationProvider is fixed. http://b/25876485 .
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001434 if (!netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1435 // NOTE: if this causes app breakage, we should not just comment out this early return;
1436 // instead, we should make this early return conditional on the requesting app's target
1437 // SDK version, as described in the comment above.
1438 return TYPE_NONE;
1439 }
1440
Robert Greenwalt06314e42014-10-29 14:04:06 -07001441 String type = null;
1442 int result = TYPE_NONE;
1443
Ye Wenb87875e2014-07-21 14:19:01 -07001444 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
Robert Greenwalt06314e42014-10-29 14:04:06 -07001445 type = "enableCBS";
1446 result = TYPE_MOBILE_CBS;
1447 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1448 type = "enableIMS";
1449 result = TYPE_MOBILE_IMS;
1450 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1451 type = "enableFOTA";
1452 result = TYPE_MOBILE_FOTA;
1453 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1454 type = "enableDUN";
1455 result = TYPE_MOBILE_DUN;
1456 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001457 type = "enableSUPL";
Robert Greenwalt06314e42014-10-29 14:04:06 -07001458 result = TYPE_MOBILE_SUPL;
Robert Greenwalt74ab4fa2015-08-28 12:37:54 -07001459 // back out this hack for mms as they no longer need this and it's causing
1460 // device slowdowns - b/23350688 (note, supl still needs this)
1461 //} else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1462 // type = "enableMMS";
1463 // result = TYPE_MOBILE_MMS;
Robert Greenwalt06314e42014-10-29 14:04:06 -07001464 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1465 type = "enableHIPRI";
1466 result = TYPE_MOBILE_HIPRI;
Ye Wenb87875e2014-07-21 14:19:01 -07001467 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001468 if (type != null) {
1469 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1470 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1471 return result;
Ye Wenb87875e2014-07-21 14:19:01 -07001472 }
1473 }
1474 return TYPE_NONE;
1475 }
1476
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001477 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001478 if (netCap == null) return TYPE_NONE;
1479 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1480 return TYPE_MOBILE_CBS;
1481 }
1482 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1483 return TYPE_MOBILE_IMS;
1484 }
1485 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1486 return TYPE_MOBILE_FOTA;
1487 }
1488 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1489 return TYPE_MOBILE_DUN;
1490 }
1491 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1492 return TYPE_MOBILE_SUPL;
1493 }
1494 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1495 return TYPE_MOBILE_MMS;
1496 }
1497 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1498 return TYPE_MOBILE_HIPRI;
1499 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001500 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1501 return TYPE_WIFI_P2P;
1502 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001503 return TYPE_NONE;
1504 }
1505
1506 private static class LegacyRequest {
1507 NetworkCapabilities networkCapabilities;
1508 NetworkRequest networkRequest;
1509 int expireSequenceNumber;
1510 Network currentNetwork;
1511 int delay = -1;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001512
1513 private void clearDnsBinding() {
1514 if (currentNetwork != null) {
1515 currentNetwork = null;
1516 setProcessDefaultNetworkForHostResolution(null);
1517 }
1518 }
1519
Robert Greenwalt6078b502014-06-11 16:05:07 -07001520 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001521 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001522 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001523 currentNetwork = network;
1524 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001525 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001526 }
1527 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001528 public void onLost(Network network) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001529 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001530 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1531 }
1532 };
1533 }
1534
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001535 @UnsupportedAppUsage
Chalard Jean4d660112018-06-04 16:52:49 +09001536 private static final HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
1537 new HashMap<>();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001538
1539 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1540 synchronized (sLegacyRequests) {
1541 LegacyRequest l = sLegacyRequests.get(netCap);
1542 if (l != null) return l.networkRequest;
1543 }
1544 return null;
1545 }
1546
1547 private void renewRequestLocked(LegacyRequest l) {
1548 l.expireSequenceNumber++;
1549 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1550 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1551 }
1552
1553 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1554 int ourSeqNum = -1;
1555 synchronized (sLegacyRequests) {
1556 LegacyRequest l = sLegacyRequests.get(netCap);
1557 if (l == null) return;
1558 ourSeqNum = l.expireSequenceNumber;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001559 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001560 }
1561 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1562 }
1563
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001564 @UnsupportedAppUsage
Robert Greenwalt562cc542014-05-15 18:07:26 -07001565 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1566 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001567 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001568 try {
1569 delay = mService.getRestoreDefaultNetworkDelay(type);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001570 } catch (RemoteException e) {
1571 throw e.rethrowFromSystemServer();
1572 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001573 LegacyRequest l = new LegacyRequest();
1574 l.networkCapabilities = netCap;
1575 l.delay = delay;
1576 l.expireSequenceNumber = 0;
Hugo Benichi2583ef02017-02-02 17:02:36 +09001577 l.networkRequest = sendRequestForNetwork(
1578 netCap, l.networkCallback, 0, REQUEST, type, getDefaultHandler());
Robert Greenwalt562cc542014-05-15 18:07:26 -07001579 if (l.networkRequest == null) return null;
1580 sLegacyRequests.put(netCap, l);
1581 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1582 return l.networkRequest;
1583 }
1584
1585 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1586 if (delay >= 0) {
1587 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
Hugo Benichi2583ef02017-02-02 17:02:36 +09001588 CallbackHandler handler = getDefaultHandler();
Hugo Benichi6f260f32017-02-03 14:18:44 +09001589 Message msg = handler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1590 handler.sendMessageDelayed(msg, delay);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001591 }
1592 }
1593
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001594 @UnsupportedAppUsage
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001595 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1596 final LegacyRequest l;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001597 synchronized (sLegacyRequests) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001598 l = sLegacyRequests.remove(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001599 }
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001600 if (l == null) return false;
1601 unregisterNetworkCallback(l.networkCallback);
1602 l.clearDnsBinding();
1603 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001604 }
1605
Erik Kline35bf06c2017-01-26 18:08:28 +09001606 private static final SparseIntArray sLegacyTypeToTransport = new SparseIntArray();
1607 static {
1608 sLegacyTypeToTransport.put(TYPE_MOBILE, NetworkCapabilities.TRANSPORT_CELLULAR);
1609 sLegacyTypeToTransport.put(TYPE_MOBILE_CBS, NetworkCapabilities.TRANSPORT_CELLULAR);
1610 sLegacyTypeToTransport.put(TYPE_MOBILE_DUN, NetworkCapabilities.TRANSPORT_CELLULAR);
1611 sLegacyTypeToTransport.put(TYPE_MOBILE_FOTA, NetworkCapabilities.TRANSPORT_CELLULAR);
1612 sLegacyTypeToTransport.put(TYPE_MOBILE_HIPRI, NetworkCapabilities.TRANSPORT_CELLULAR);
1613 sLegacyTypeToTransport.put(TYPE_MOBILE_IMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1614 sLegacyTypeToTransport.put(TYPE_MOBILE_MMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1615 sLegacyTypeToTransport.put(TYPE_MOBILE_SUPL, NetworkCapabilities.TRANSPORT_CELLULAR);
1616 sLegacyTypeToTransport.put(TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI);
1617 sLegacyTypeToTransport.put(TYPE_WIFI_P2P, NetworkCapabilities.TRANSPORT_WIFI);
1618 sLegacyTypeToTransport.put(TYPE_BLUETOOTH, NetworkCapabilities.TRANSPORT_BLUETOOTH);
1619 sLegacyTypeToTransport.put(TYPE_ETHERNET, NetworkCapabilities.TRANSPORT_ETHERNET);
1620 }
1621
1622 private static final SparseIntArray sLegacyTypeToCapability = new SparseIntArray();
1623 static {
1624 sLegacyTypeToCapability.put(TYPE_MOBILE_CBS, NetworkCapabilities.NET_CAPABILITY_CBS);
1625 sLegacyTypeToCapability.put(TYPE_MOBILE_DUN, NetworkCapabilities.NET_CAPABILITY_DUN);
1626 sLegacyTypeToCapability.put(TYPE_MOBILE_FOTA, NetworkCapabilities.NET_CAPABILITY_FOTA);
1627 sLegacyTypeToCapability.put(TYPE_MOBILE_IMS, NetworkCapabilities.NET_CAPABILITY_IMS);
1628 sLegacyTypeToCapability.put(TYPE_MOBILE_MMS, NetworkCapabilities.NET_CAPABILITY_MMS);
1629 sLegacyTypeToCapability.put(TYPE_MOBILE_SUPL, NetworkCapabilities.NET_CAPABILITY_SUPL);
1630 sLegacyTypeToCapability.put(TYPE_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
1631 }
1632
1633 /**
1634 * Given a legacy type (TYPE_WIFI, ...) returns a NetworkCapabilities
1635 * instance suitable for registering a request or callback. Throws an
1636 * IllegalArgumentException if no mapping from the legacy type to
1637 * NetworkCapabilities is known.
1638 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +09001639 * @deprecated Types are deprecated. Use {@link NetworkCallback} or {@link NetworkRequest}
1640 * to find the network instead.
Erik Kline35bf06c2017-01-26 18:08:28 +09001641 * @hide
1642 */
1643 public static NetworkCapabilities networkCapabilitiesForType(int type) {
1644 final NetworkCapabilities nc = new NetworkCapabilities();
1645
1646 // Map from type to transports.
1647 final int NOT_FOUND = -1;
1648 final int transport = sLegacyTypeToTransport.get(type, NOT_FOUND);
Hugo Benichie7678512017-05-09 15:19:01 +09001649 Preconditions.checkArgument(transport != NOT_FOUND, "unknown legacy type: " + type);
Erik Kline35bf06c2017-01-26 18:08:28 +09001650 nc.addTransportType(transport);
1651
1652 // Map from type to capabilities.
1653 nc.addCapability(sLegacyTypeToCapability.get(
1654 type, NetworkCapabilities.NET_CAPABILITY_INTERNET));
1655 nc.maybeMarkCapabilitiesRestricted();
1656 return nc;
1657 }
1658
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001659 /** @hide */
1660 public static class PacketKeepaliveCallback {
1661 /** The requested keepalive was successfully started. */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001662 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001663 public void onStarted() {}
1664 /** The keepalive was successfully stopped. */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001665 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001666 public void onStopped() {}
1667 /** An error occurred. */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001668 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001669 public void onError(int error) {}
1670 }
1671
1672 /**
1673 * Allows applications to request that the system periodically send specific packets on their
1674 * behalf, using hardware offload to save battery power.
1675 *
1676 * To request that the system send keepalives, call one of the methods that return a
1677 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1678 * passing in a non-null callback. If the callback is successfully started, the callback's
1679 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1680 * specifying one of the {@code ERROR_*} constants in this class.
1681 *
Chalard Jean4d660112018-06-04 16:52:49 +09001682 * To stop an existing keepalive, call {@link PacketKeepalive#stop}. The system will call
1683 * {@link PacketKeepaliveCallback#onStopped} if the operation was successful or
1684 * {@link PacketKeepaliveCallback#onError} if an error occurred.
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001685 *
1686 * @hide
1687 */
1688 public class PacketKeepalive {
1689
1690 private static final String TAG = "PacketKeepalive";
1691
1692 /** @hide */
1693 public static final int SUCCESS = 0;
1694
1695 /** @hide */
1696 public static final int NO_KEEPALIVE = -1;
1697
1698 /** @hide */
1699 public static final int BINDER_DIED = -10;
1700
1701 /** The specified {@code Network} is not connected. */
1702 public static final int ERROR_INVALID_NETWORK = -20;
1703 /** The specified IP addresses are invalid. For example, the specified source IP address is
1704 * not configured on the specified {@code Network}. */
1705 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1706 /** The requested port is invalid. */
1707 public static final int ERROR_INVALID_PORT = -22;
1708 /** The packet length is invalid (e.g., too long). */
1709 public static final int ERROR_INVALID_LENGTH = -23;
1710 /** The packet transmission interval is invalid (e.g., too short). */
1711 public static final int ERROR_INVALID_INTERVAL = -24;
1712
1713 /** The hardware does not support this request. */
1714 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti9d1284e2015-09-08 16:46:36 +09001715 /** The hardware returned an error. */
1716 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001717
Nathan Harold63dd8132018-02-14 13:09:45 -08001718 /** The NAT-T destination port for IPsec */
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001719 public static final int NATT_PORT = 4500;
1720
Nathan Harold63dd8132018-02-14 13:09:45 -08001721 /** The minimum interval in seconds between keepalive packet transmissions */
1722 public static final int MIN_INTERVAL = 10;
1723
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001724 private final Network mNetwork;
1725 private final PacketKeepaliveCallback mCallback;
1726 private final Looper mLooper;
1727 private final Messenger mMessenger;
1728
1729 private volatile Integer mSlot;
1730
1731 void stopLooper() {
1732 mLooper.quit();
1733 }
1734
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001735 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001736 public void stop() {
1737 try {
1738 mService.stopKeepalive(mNetwork, mSlot);
1739 } catch (RemoteException e) {
1740 Log.e(TAG, "Error stopping packet keepalive: ", e);
1741 stopLooper();
1742 }
1743 }
1744
1745 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09001746 Preconditions.checkNotNull(network, "network cannot be null");
1747 Preconditions.checkNotNull(callback, "callback cannot be null");
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001748 mNetwork = network;
1749 mCallback = callback;
1750 HandlerThread thread = new HandlerThread(TAG);
1751 thread.start();
1752 mLooper = thread.getLooper();
1753 mMessenger = new Messenger(new Handler(mLooper) {
1754 @Override
1755 public void handleMessage(Message message) {
1756 switch (message.what) {
1757 case NetworkAgent.EVENT_PACKET_KEEPALIVE:
1758 int error = message.arg2;
1759 try {
1760 if (error == SUCCESS) {
1761 if (mSlot == null) {
1762 mSlot = message.arg1;
1763 mCallback.onStarted();
1764 } else {
1765 mSlot = null;
1766 stopLooper();
1767 mCallback.onStopped();
1768 }
1769 } else {
1770 stopLooper();
1771 mCallback.onError(error);
1772 }
1773 } catch (Exception e) {
1774 Log.e(TAG, "Exception in keepalive callback(" + error + ")", e);
1775 }
1776 break;
1777 default:
1778 Log.e(TAG, "Unhandled message " + Integer.toHexString(message.what));
1779 break;
1780 }
1781 }
1782 });
1783 }
1784 }
1785
1786 /**
1787 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
1788 *
1789 * @hide
1790 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001791 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001792 public PacketKeepalive startNattKeepalive(
1793 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
1794 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
1795 final PacketKeepalive k = new PacketKeepalive(network, callback);
1796 try {
1797 mService.startNattKeepalive(network, intervalSeconds, k.mMessenger, new Binder(),
1798 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
1799 } catch (RemoteException e) {
1800 Log.e(TAG, "Error starting packet keepalive: ", e);
1801 k.stopLooper();
1802 return null;
1803 }
1804 return k;
1805 }
1806
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001807 /**
1808 * Ensure that a network route exists to deliver traffic to the specified
1809 * host via the specified network interface. An attempt to add a route that
1810 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001811 *
1812 * <p>This method requires the caller to hold either the
1813 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1814 * or the ability to modify system settings as determined by
1815 * {@link android.provider.Settings.System#canWrite}.</p>
1816 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001817 * @param networkType the type of the network over which traffic to the specified
1818 * host is to be routed
1819 * @param hostAddress the IP address of the host to which the route is desired
1820 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001821 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001822 * @deprecated Deprecated in favor of the
1823 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
1824 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001825 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001826 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001827 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001829 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001830 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001831 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001832 }
1833
1834 /**
1835 * Ensure that a network route exists to deliver traffic to the specified
1836 * host via the specified network interface. An attempt to add a route that
1837 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001838 *
1839 * <p>This method requires the caller to hold either the
1840 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1841 * or the ability to modify system settings as determined by
1842 * {@link android.provider.Settings.System#canWrite}.</p>
1843 *
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001844 * @param networkType the type of the network over which traffic to the specified
1845 * host is to be routed
1846 * @param hostAddress the IP address of the host to which the route is desired
1847 * @return {@code true} on success, {@code false} on failure
1848 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001849 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001850 * {@link #bindProcessToNetwork} API.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001851 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001852 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001853 @UnsupportedAppUsage
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001854 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001855 checkLegacyRoutingApiAccess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001856 try {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001857 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001858 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001859 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001860 }
1861 }
1862
1863 /**
1864 * Returns the value of the setting for background data usage. If false,
1865 * applications should not use the network if the application is not in the
1866 * foreground. Developers should respect this setting, and check the value
1867 * of this before performing any background data operations.
1868 * <p>
1869 * All applications that have background services that use the network
1870 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001871 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001872 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001873 * background data depends on several combined factors, and this method will
1874 * always return {@code true}. Instead, when background data is unavailable,
1875 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001876 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 * @return Whether background data usage is allowed.
1878 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001879 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001881 // assume that background data is allowed; final authority is
1882 // NetworkInfo which may be blocked.
1883 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001884 }
1885
1886 /**
1887 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001888 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 * @param allowBackgroundData Whether an application should use data while
1890 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001891 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001892 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1893 * @see #getBackgroundDataSetting()
1894 * @hide
1895 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001896 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001897 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001898 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001899 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001900 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001901
Jeff Sharkey43d2a172017-07-12 10:50:42 -06001902 /** {@hide} */
1903 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001904 @UnsupportedAppUsage
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001905 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1906 try {
1907 return mService.getActiveNetworkQuotaInfo();
1908 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001909 throw e.rethrowFromSystemServer();
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001910 }
1911 }
1912
1913 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001914 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001915 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001916 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001917 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001918 @UnsupportedAppUsage
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001919 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001920 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1921 if (b != null) {
1922 try {
1923 ITelephony it = ITelephony.Stub.asInterface(b);
Shishir Agrawal7ea3e8b2016-01-25 13:03:07 -08001924 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Wink Saville36ffb042014-12-05 11:10:30 -08001925 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
Malcolm Chenb455e722017-11-28 15:57:14 -08001926 boolean retVal = it.isUserDataEnabled(subId);
Wink Saville36ffb042014-12-05 11:10:30 -08001927 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1928 + " retVal=" + retVal);
1929 return retVal;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001930 } catch (RemoteException e) {
1931 throw e.rethrowFromSystemServer();
1932 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001933 }
Wink Saville36ffb042014-12-05 11:10:30 -08001934 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001935 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001936 }
1937
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001938 /**
Robert Greenwaltb2489872014-09-04 16:44:35 -07001939 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt6078b502014-06-11 16:05:07 -07001940 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001941 */
1942 public interface OnNetworkActiveListener {
1943 /**
1944 * Called on the main thread of the process to report that the current data network
1945 * has become active, and it is now a good time to perform any pending network
1946 * operations. Note that this listener only tells you when the network becomes
1947 * active; if at any other time you want to know whether it is active (and thus okay
1948 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001949 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001950 */
Chalard Jean4d660112018-06-04 16:52:49 +09001951 void onNetworkActive();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001952 }
1953
1954 private INetworkManagementService getNetworkManagementService() {
1955 synchronized (this) {
1956 if (mNMService != null) {
1957 return mNMService;
1958 }
1959 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1960 mNMService = INetworkManagementService.Stub.asInterface(b);
1961 return mNMService;
1962 }
1963 }
1964
1965 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
Chalard Jean4d660112018-06-04 16:52:49 +09001966 mNetworkActivityListeners = new ArrayMap<>();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001967
1968 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001969 * Start listening to reports when the system's default data network is active, meaning it is
1970 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1971 * to determine the current state of the system's default network after registering the
1972 * listener.
1973 * <p>
1974 * If the process default network has been set with
Paul Jensen72db88e2015-03-10 10:54:12 -04001975 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt6078b502014-06-11 16:05:07 -07001976 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001977 *
1978 * @param l The listener to be told when the network is active.
1979 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001980 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001981 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1982 @Override
1983 public void onNetworkActive() throws RemoteException {
1984 l.onNetworkActive();
1985 }
1986 };
1987
1988 try {
1989 getNetworkManagementService().registerNetworkActivityListener(rl);
1990 mNetworkActivityListeners.put(l, rl);
1991 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001992 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001993 }
1994 }
1995
1996 /**
1997 * Remove network active listener previously registered with
Robert Greenwaltb2489872014-09-04 16:44:35 -07001998 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001999 *
2000 * @param l Previously registered listener.
2001 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07002002 public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002003 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
Hugo Benichie7678512017-05-09 15:19:01 +09002004 Preconditions.checkArgument(rl != null, "Listener was not registered.");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002005 try {
2006 getNetworkManagementService().unregisterNetworkActivityListener(rl);
2007 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002008 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002009 }
2010 }
2011
2012 /**
2013 * Return whether the data network is currently active. An active network means that
2014 * it is currently in a high power state for performing data transmission. On some
2015 * types of networks, it may be expensive to move and stay in such a state, so it is
2016 * more power efficient to batch network traffic together when the radio is already in
2017 * this state. This method tells you whether right now is currently a good time to
2018 * initiate network traffic, as the network is already active.
2019 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002020 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002021 try {
2022 return getNetworkManagementService().isNetworkActive();
2023 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002024 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002025 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002026 }
2027
2028 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002029 * {@hide}
2030 */
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09002031 public ConnectivityManager(Context context, IConnectivityManager service) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09002032 mContext = Preconditions.checkNotNull(context, "missing context");
2033 mService = Preconditions.checkNotNull(service, "missing IConnectivityManager");
Paul Jensene0bef712014-12-10 15:12:18 -05002034 sInstance = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002035 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002036
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07002037 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002038 @UnsupportedAppUsage
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07002039 public static ConnectivityManager from(Context context) {
2040 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
2041 }
2042
Lorenzo Colittifbe9b1a2016-07-28 17:14:11 +09002043 /* TODO: These permissions checks don't belong in client-side code. Move them to
2044 * services.jar, possibly in com.android.server.net. */
2045
2046 /** {@hide} */
Lorenzo Colittid5427052015-10-15 16:29:00 +09002047 public static final void enforceChangePermission(Context context) {
2048 int uid = Binder.getCallingUid();
2049 Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
2050 .getPackageNameForUid(context, uid), true /* throwException */);
2051 }
2052
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002053 /** {@hide} */
2054 public static final void enforceTetherChangePermission(Context context, String callingPkg) {
Hugo Benichie7678512017-05-09 15:19:01 +09002055 Preconditions.checkNotNull(context, "Context cannot be null");
2056 Preconditions.checkNotNull(callingPkg, "callingPkg cannot be null");
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002057
Robert Greenwaltedb47662014-09-16 17:54:19 -07002058 if (context.getResources().getStringArray(
2059 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
2060 // Have a provisioning app - must only let system apps (which check this app)
2061 // turn on tethering
2062 context.enforceCallingOrSelfPermission(
Jeremy Kleind42209d2015-12-28 15:11:58 -08002063 android.Manifest.permission.TETHER_PRIVILEGED, "ConnectivityService");
Robert Greenwaltedb47662014-09-16 17:54:19 -07002064 } else {
Billy Laua7238a32015-08-01 12:45:02 +01002065 int uid = Binder.getCallingUid();
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002066 // If callingPkg's uid is not same as Binder.getCallingUid(),
2067 // AppOpsService throws SecurityException.
2068 Settings.checkAndNoteWriteSettingsOperation(context, uid, callingPkg,
2069 true /* throwException */);
Robert Greenwaltedb47662014-09-16 17:54:19 -07002070 }
2071 }
2072
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002073 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002074 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2075 * situations where a Context pointer is unavailable.
2076 * @hide
2077 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002078 @Deprecated
Paul Jensen72db88e2015-03-10 10:54:12 -04002079 static ConnectivityManager getInstanceOrNull() {
2080 return sInstance;
2081 }
2082
2083 /**
2084 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2085 * situations where a Context pointer is unavailable.
2086 * @hide
2087 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002088 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002089 @UnsupportedAppUsage
Paul Jensen72db88e2015-03-10 10:54:12 -04002090 private static ConnectivityManager getInstance() {
2091 if (getInstanceOrNull() == null) {
Paul Jensene0bef712014-12-10 15:12:18 -05002092 throw new IllegalStateException("No ConnectivityManager yet constructed");
2093 }
Paul Jensen72db88e2015-03-10 10:54:12 -04002094 return getInstanceOrNull();
Paul Jensene0bef712014-12-10 15:12:18 -05002095 }
2096
2097 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002098 * Get the set of tetherable, available interfaces. This list is limited by
2099 * device configuration and current interface existence.
2100 *
2101 * @return an array of 0 or more Strings of tetherable interface names.
2102 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002103 * {@hide}
2104 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002105 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002106 @UnsupportedAppUsage
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002107 public String[] getTetherableIfaces() {
2108 try {
2109 return mService.getTetherableIfaces();
2110 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002111 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002112 }
2113 }
2114
2115 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002116 * Get the set of tethered interfaces.
2117 *
2118 * @return an array of 0 or more String of currently tethered interface names.
2119 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002120 * {@hide}
2121 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002122 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002123 @UnsupportedAppUsage
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002124 public String[] getTetheredIfaces() {
2125 try {
2126 return mService.getTetheredIfaces();
2127 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002128 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002129 }
2130 }
2131
2132 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002133 * Get the set of interface names which attempted to tether but
2134 * failed. Re-attempting to tether may cause them to reset to the Tethered
2135 * state. Alternatively, causing the interface to be destroyed and recreated
2136 * may cause them to reset to the available state.
2137 * {@link ConnectivityManager#getLastTetherError} can be used to get more
2138 * information on the cause of the errors.
2139 *
2140 * @return an array of 0 or more String indicating the interface names
2141 * which failed to tether.
2142 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002143 * {@hide}
2144 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002145 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002146 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002147 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002148 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08002149 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002150 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002151 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002152 }
2153 }
2154
2155 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07002156 * Get the set of tethered dhcp ranges.
2157 *
2158 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
2159 * {@hide}
2160 */
2161 public String[] getTetheredDhcpRanges() {
2162 try {
2163 return mService.getTetheredDhcpRanges();
2164 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002165 throw e.rethrowFromSystemServer();
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07002166 }
2167 }
2168
2169 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002170 * Attempt to tether the named interface. This will setup a dhcp server
2171 * on the interface, forward and NAT IP packets and forward DNS requests
2172 * to the best active upstream network interface. Note that if no upstream
2173 * IP network interface is available, dhcp will still run and traffic will be
2174 * allowed between the tethered devices and this device, though upstream net
2175 * access will of course fail until an upstream network interface becomes
2176 * active.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002177 *
2178 * <p>This method requires the caller to hold either the
2179 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2180 * or the ability to modify system settings as determined by
2181 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002182 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002183 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2184 * and WifiStateMachine which need direct access. All other clients should use
2185 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2186 * logic.</p>
2187 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002188 * @param iface the interface name to tether.
2189 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2190 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002191 * {@hide}
2192 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002193 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002194 public int tether(String iface) {
2195 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002196 String pkgName = mContext.getOpPackageName();
2197 Log.i(TAG, "tether caller:" + pkgName);
2198 return mService.tether(iface, pkgName);
Robert Greenwalt5a735062010-03-02 17:25:02 -08002199 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002200 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002201 }
2202 }
2203
2204 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002205 * Stop tethering the named interface.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002206 *
2207 * <p>This method requires the caller to hold either the
2208 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2209 * or the ability to modify system settings as determined by
2210 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002211 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002212 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2213 * and WifiStateMachine which need direct access. All other clients should use
2214 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2215 * logic.</p>
2216 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002217 * @param iface the interface name to untether.
2218 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2219 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002220 * {@hide}
2221 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002222 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002223 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002224 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002225 String pkgName = mContext.getOpPackageName();
2226 Log.i(TAG, "untether caller:" + pkgName);
2227 return mService.untether(iface, pkgName);
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002228 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002229 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002230 }
2231 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002232
2233 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002234 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002235 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002236 * due to device configuration.
2237 *
Chalard Jean8f76fc32017-09-26 15:45:18 +09002238 * <p>If this app does not have permission to use this API, it will always
2239 * return false rather than throw an exception.</p>
2240 *
2241 * <p>If the device has a hotspot provisioning app, the caller is required to hold the
2242 * {@link android.Manifest.permission.TETHER_PRIVILEGED} permission.</p>
2243 *
2244 * <p>Otherwise, this method requires the caller to hold the ability to modify system
2245 * settings as determined by {@link android.provider.Settings.System#canWrite}.</p>
2246 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002247 * @return a boolean - {@code true} indicating Tethering is supported.
2248 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002249 * {@hide}
2250 */
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002251 @SystemApi
Chalard Jean8f76fc32017-09-26 15:45:18 +09002252 @RequiresPermission(anyOf = {android.Manifest.permission.TETHER_PRIVILEGED,
2253 android.Manifest.permission.WRITE_SETTINGS})
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002254 public boolean isTetheringSupported() {
Chalard Jean8f76fc32017-09-26 15:45:18 +09002255 String pkgName = mContext.getOpPackageName();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002256 try {
Udam Saini0e94c362017-06-07 12:06:28 -07002257 return mService.isTetheringSupported(pkgName);
Chalard Jean8f76fc32017-09-26 15:45:18 +09002258 } catch (SecurityException e) {
2259 // This API is not available to this caller, but for backward-compatibility
2260 // this will just return false instead of throwing.
2261 return false;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002262 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002263 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002264 }
2265 }
2266
2267 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002268 * Callback for use with {@link #startTethering} to find out whether tethering succeeded.
2269 * @hide
2270 */
2271 @SystemApi
2272 public static abstract class OnStartTetheringCallback {
2273 /**
2274 * Called when tethering has been successfully started.
2275 */
Chalard Jean4d660112018-06-04 16:52:49 +09002276 public void onTetheringStarted() {}
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002277
2278 /**
2279 * Called when starting tethering failed.
2280 */
Chalard Jean4d660112018-06-04 16:52:49 +09002281 public void onTetheringFailed() {}
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002282 }
2283
2284 /**
2285 * Convenient overload for
2286 * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null
2287 * handler to run on the current thread's {@link Looper}.
2288 * @hide
2289 */
2290 @SystemApi
Udam Saini0e94c362017-06-07 12:06:28 -07002291 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002292 public void startTethering(int type, boolean showProvisioningUi,
2293 final OnStartTetheringCallback callback) {
2294 startTethering(type, showProvisioningUi, callback, null);
2295 }
2296
2297 /**
2298 * Runs tether provisioning for the given type if needed and then starts tethering if
2299 * the check succeeds. If no carrier provisioning is required for tethering, tethering is
2300 * enabled immediately. If provisioning fails, tethering will not be enabled. It also
2301 * schedules tether provisioning re-checks if appropriate.
2302 *
2303 * @param type The type of tethering to start. Must be one of
2304 * {@link ConnectivityManager.TETHERING_WIFI},
2305 * {@link ConnectivityManager.TETHERING_USB}, or
2306 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2307 * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there
2308 * is one. This should be true the first time this function is called and also any time
2309 * the user can see this UI. It gives users information from their carrier about the
2310 * check failing and how they can sign up for tethering if possible.
2311 * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller
2312 * of the result of trying to tether.
2313 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
2314 * @hide
2315 */
2316 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002317 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002318 public void startTethering(int type, boolean showProvisioningUi,
2319 final OnStartTetheringCallback callback, Handler handler) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09002320 Preconditions.checkNotNull(callback, "OnStartTetheringCallback cannot be null.");
Jeremy Klein5f277e12016-03-12 16:29:54 -08002321
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002322 ResultReceiver wrappedCallback = new ResultReceiver(handler) {
2323 @Override
2324 protected void onReceiveResult(int resultCode, Bundle resultData) {
2325 if (resultCode == TETHER_ERROR_NO_ERROR) {
2326 callback.onTetheringStarted();
2327 } else {
2328 callback.onTetheringFailed();
2329 }
2330 }
2331 };
Jeremy Klein5f277e12016-03-12 16:29:54 -08002332
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002333 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002334 String pkgName = mContext.getOpPackageName();
2335 Log.i(TAG, "startTethering caller:" + pkgName);
2336 mService.startTethering(type, wrappedCallback, showProvisioningUi, pkgName);
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002337 } catch (RemoteException e) {
2338 Log.e(TAG, "Exception trying to start tethering.", e);
2339 wrappedCallback.send(TETHER_ERROR_SERVICE_UNAVAIL, null);
2340 }
2341 }
2342
2343 /**
2344 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if
2345 * applicable.
2346 *
2347 * @param type The type of tethering to stop. Must be one of
2348 * {@link ConnectivityManager.TETHERING_WIFI},
2349 * {@link ConnectivityManager.TETHERING_USB}, or
2350 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2351 * @hide
2352 */
2353 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002354 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002355 public void stopTethering(int type) {
2356 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002357 String pkgName = mContext.getOpPackageName();
2358 Log.i(TAG, "stopTethering caller:" + pkgName);
2359 mService.stopTethering(type, pkgName);
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002360 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002361 throw e.rethrowFromSystemServer();
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002362 }
2363 }
2364
2365 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002366 * Get the list of regular expressions that define any tetherable
2367 * USB network interfaces. If USB tethering is not supported by the
2368 * device, this list should be empty.
2369 *
2370 * @return an array of 0 or more regular expression Strings defining
2371 * what interfaces are considered tetherable usb interfaces.
2372 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002373 * {@hide}
2374 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002375 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002376 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002377 public String[] getTetherableUsbRegexs() {
2378 try {
2379 return mService.getTetherableUsbRegexs();
2380 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002381 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002382 }
2383 }
2384
2385 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002386 * Get the list of regular expressions that define any tetherable
2387 * Wifi network interfaces. If Wifi tethering is not supported by the
2388 * device, this list should be empty.
2389 *
2390 * @return an array of 0 or more regular expression Strings defining
2391 * what interfaces are considered tetherable wifi interfaces.
2392 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002393 * {@hide}
2394 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002395 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002396 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002397 public String[] getTetherableWifiRegexs() {
2398 try {
2399 return mService.getTetherableWifiRegexs();
2400 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002401 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002402 }
2403 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08002404
Danica Chang6fdd0c62010-08-11 14:54:43 -07002405 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002406 * Get the list of regular expressions that define any tetherable
2407 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
2408 * device, this list should be empty.
2409 *
2410 * @return an array of 0 or more regular expression Strings defining
2411 * what interfaces are considered tetherable bluetooth interfaces.
2412 *
Danica Chang6fdd0c62010-08-11 14:54:43 -07002413 * {@hide}
2414 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002415 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002416 @UnsupportedAppUsage
Danica Chang6fdd0c62010-08-11 14:54:43 -07002417 public String[] getTetherableBluetoothRegexs() {
2418 try {
2419 return mService.getTetherableBluetoothRegexs();
2420 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002421 throw e.rethrowFromSystemServer();
Danica Chang6fdd0c62010-08-11 14:54:43 -07002422 }
2423 }
2424
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002425 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002426 * Attempt to both alter the mode of USB and Tethering of USB. A
2427 * utility method to deal with some of the complexity of USB - will
2428 * attempt to switch to Rndis and subsequently tether the resulting
2429 * interface on {@code true} or turn off tethering and switch off
2430 * Rndis on {@code false}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002431 *
2432 * <p>This method requires the caller to hold either the
2433 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2434 * or the ability to modify system settings as determined by
2435 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002436 *
2437 * @param enable a boolean - {@code true} to enable tethering
2438 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2439 *
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002440 * {@hide}
2441 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002442 @UnsupportedAppUsage
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002443 public int setUsbTethering(boolean enable) {
2444 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002445 String pkgName = mContext.getOpPackageName();
2446 Log.i(TAG, "setUsbTethering caller:" + pkgName);
2447 return mService.setUsbTethering(enable, pkgName);
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002448 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002449 throw e.rethrowFromSystemServer();
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002450 }
2451 }
2452
Robert Greenwalt5a735062010-03-02 17:25:02 -08002453 /** {@hide} */
2454 public static final int TETHER_ERROR_NO_ERROR = 0;
2455 /** {@hide} */
2456 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
2457 /** {@hide} */
2458 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
2459 /** {@hide} */
2460 public static final int TETHER_ERROR_UNSUPPORTED = 3;
2461 /** {@hide} */
2462 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
2463 /** {@hide} */
2464 public static final int TETHER_ERROR_MASTER_ERROR = 5;
2465 /** {@hide} */
2466 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
2467 /** {@hide} */
2468 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
2469 /** {@hide} */
2470 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
2471 /** {@hide} */
2472 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
2473 /** {@hide} */
2474 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002475 /** {@hide} */
2476 public static final int TETHER_ERROR_PROVISION_FAILED = 11;
Robert Greenwalt5a735062010-03-02 17:25:02 -08002477
2478 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002479 * Get a more detailed error code after a Tethering or Untethering
2480 * request asynchronously failed.
2481 *
2482 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08002483 * @return error The error code of the last error tethering or untethering the named
2484 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002485 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002486 * {@hide}
2487 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002488 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002489 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002490 public int getLastTetherError(String iface) {
2491 try {
2492 return mService.getLastTetherError(iface);
2493 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002494 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002495 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002496 }
2497
2498 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002499 * Report network connectivity status. This is currently used only
2500 * to alter status bar UI.
Paul Jensenb2748922015-05-06 11:10:18 -04002501 * <p>This method requires the caller to hold the permission
2502 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002503 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002504 * @param networkType The type of network you want to report on
2505 * @param percentage The quality of the connection 0 is bad, 100 is good
Chalard Jean6b1da6e2018-03-08 13:54:53 +09002506 * @deprecated Types are deprecated. Use {@link #reportNetworkConnectivity} instead.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002507 * {@hide}
2508 */
2509 public void reportInetCondition(int networkType, int percentage) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09002510 printStackTrace();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002511 try {
2512 mService.reportInetCondition(networkType, percentage);
2513 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002514 throw e.rethrowFromSystemServer();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002515 }
2516 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002517
2518 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002519 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07002520 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002521 * the framework to re-evaluate network connectivity and/or switch to another
2522 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002523 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002524 * @param network The {@link Network} the application was attempting to use
2525 * or {@code null} to indicate the current default network.
Paul Jensenbfd17b72015-04-07 12:43:13 -04002526 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
2527 * working and non-working connectivity.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002528 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002529 @Deprecated
Robert Greenwalt9258c642014-03-26 16:47:06 -07002530 public void reportBadNetwork(Network network) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09002531 printStackTrace();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002532 try {
Paul Jensenbfd17b72015-04-07 12:43:13 -04002533 // One of these will be ignored because it matches system's current state.
2534 // The other will trigger the necessary reevaluation.
2535 mService.reportNetworkConnectivity(network, true);
2536 mService.reportNetworkConnectivity(network, false);
2537 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002538 throw e.rethrowFromSystemServer();
Paul Jensenbfd17b72015-04-07 12:43:13 -04002539 }
2540 }
2541
2542 /**
2543 * Report to the framework whether a network has working connectivity.
2544 * This provides a hint to the system that a particular network is providing
2545 * working connectivity or not. In response the framework may re-evaluate
2546 * the network's connectivity and might take further action thereafter.
2547 *
2548 * @param network The {@link Network} the application was attempting to use
2549 * or {@code null} to indicate the current default network.
2550 * @param hasConnectivity {@code true} if the application was able to successfully access the
2551 * Internet using {@code network} or {@code false} if not.
2552 */
2553 public void reportNetworkConnectivity(Network network, boolean hasConnectivity) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09002554 printStackTrace();
Paul Jensenbfd17b72015-04-07 12:43:13 -04002555 try {
2556 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002557 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002558 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002559 }
2560 }
2561
2562 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002563 * Set a network-independent global http proxy. This is not normally what you want
2564 * for typical HTTP proxies - they are general network dependent. However if you're
2565 * doing something unusual like general internal filtering this may be useful. On
2566 * a private network where the proxy is not accessible, you may break HTTP using this.
Paul Jensenb2748922015-05-06 11:10:18 -04002567 *
2568 * @param p A {@link ProxyInfo} object defining the new global
2569 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002570 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002571 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002572 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Jason Monk207900c2014-04-25 15:00:09 -04002573 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002574 try {
2575 mService.setGlobalProxy(p);
2576 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002577 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002578 }
2579 }
2580
2581 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002582 * Retrieve any network-independent global HTTP proxy.
2583 *
Jason Monk207900c2014-04-25 15:00:09 -04002584 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002585 * if no global HTTP proxy is set.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002586 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002587 */
Jason Monk207900c2014-04-25 15:00:09 -04002588 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002589 try {
2590 return mService.getGlobalProxy();
2591 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002592 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002593 }
2594 }
2595
2596 /**
Paul Jensencee9b512015-05-06 07:32:40 -04002597 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
2598 * network-specific HTTP proxy. If {@code network} is null, the
2599 * network-specific proxy returned is the proxy of the default active
2600 * network.
2601 *
2602 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
2603 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
2604 * or when {@code network} is {@code null},
2605 * the {@code ProxyInfo} for the default active network. Returns
2606 * {@code null} when no proxy applies or the caller doesn't have
2607 * permission to use {@code network}.
2608 * @hide
2609 */
2610 public ProxyInfo getProxyForNetwork(Network network) {
2611 try {
2612 return mService.getProxyForNetwork(network);
2613 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002614 throw e.rethrowFromSystemServer();
Paul Jensencee9b512015-05-06 07:32:40 -04002615 }
2616 }
2617
2618 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002619 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
2620 * otherwise if this process is bound to a {@link Network} using
Paul Jensen72db88e2015-03-10 10:54:12 -04002621 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensene0bef712014-12-10 15:12:18 -05002622 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002623 *
Jason Monk207900c2014-04-25 15:00:09 -04002624 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002625 * HTTP proxy is active.
Robert Greenwalt434203a2010-10-11 16:00:27 -07002626 */
Paul Jensene0bef712014-12-10 15:12:18 -05002627 public ProxyInfo getDefaultProxy() {
Paul Jensencee9b512015-05-06 07:32:40 -04002628 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwalt434203a2010-10-11 16:00:27 -07002629 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07002630
2631 /**
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002632 * Returns true if the hardware supports the given network type
2633 * else it returns false. This doesn't indicate we have coverage
2634 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002635 * hardware supports it. For example a GSM phone without a SIM
2636 * should still return {@code true} for mobile data, but a wifi only
2637 * tablet would return {@code false}.
2638 *
2639 * @param networkType The network type we'd like to check
2640 * @return {@code true} if supported, else {@code false}
Chalard Jean6b1da6e2018-03-08 13:54:53 +09002641 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002642 * @hide
2643 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +09002644 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002645 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002646 @UnsupportedAppUsage
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002647 public boolean isNetworkSupported(int networkType) {
2648 try {
2649 return mService.isNetworkSupported(networkType);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002650 } catch (RemoteException e) {
2651 throw e.rethrowFromSystemServer();
2652 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002653 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002654
2655 /**
2656 * Returns if the currently active data network is metered. A network is
2657 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002658 * that connection due to monetary costs, data limitations or
2659 * battery/performance issues. You should check this before doing large
2660 * data transfers, and warn the user or delay the operation until another
2661 * network is available.
2662 *
2663 * @return {@code true} if large transfers should be avoided, otherwise
2664 * {@code false}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002665 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002666 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002667 public boolean isActiveNetworkMetered() {
2668 try {
2669 return mService.isActiveNetworkMetered();
2670 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002671 throw e.rethrowFromSystemServer();
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002672 }
2673 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002674
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002675 /**
2676 * If the LockdownVpn mechanism is enabled, updates the vpn
2677 * with a reload of its profile.
2678 *
2679 * @return a boolean with {@code} indicating success
2680 *
2681 * <p>This method can only be called by the system UID
2682 * {@hide}
2683 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002684 public boolean updateLockdownVpn() {
2685 try {
2686 return mService.updateLockdownVpn();
2687 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002688 throw e.rethrowFromSystemServer();
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002689 }
2690 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07002691
2692 /**
Wink Saville948282b2013-08-29 08:55:16 -07002693 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07002694 *
Wink Savilleab9321d2013-06-29 21:10:57 -07002695 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07002696 *
2697 * @return time out that will be used, maybe less that suggestedTimeOutMs
2698 * -1 if an error.
2699 *
2700 * {@hide}
2701 */
Wink Saville948282b2013-08-29 08:55:16 -07002702 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07002703 int timeOutMs = -1;
2704 try {
Wink Saville948282b2013-08-29 08:55:16 -07002705 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07002706 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002707 throw e.rethrowFromSystemServer();
Wink Savilleab9321d2013-06-29 21:10:57 -07002708 }
2709 return timeOutMs;
2710 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002711
2712 /**
Wink Saville42d4f082013-07-20 20:31:59 -07002713 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002714 * {@hide}
2715 */
2716 public String getMobileProvisioningUrl() {
2717 try {
2718 return mService.getMobileProvisioningUrl();
2719 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002720 throw e.rethrowFromSystemServer();
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002721 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002722 }
Wink Saville42d4f082013-07-20 20:31:59 -07002723
2724 /**
Wink Saville948282b2013-08-29 08:55:16 -07002725 * Set sign in error notification to visible or in visible
2726 *
Wink Saville948282b2013-08-29 08:55:16 -07002727 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04002728 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville948282b2013-08-29 08:55:16 -07002729 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002730 @Deprecated
Wink Saville948282b2013-08-29 08:55:16 -07002731 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04002732 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07002733 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04002734 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07002735 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002736 throw e.rethrowFromSystemServer();
Wink Saville948282b2013-08-29 08:55:16 -07002737 }
2738 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002739
2740 /**
2741 * Set the value for enabling/disabling airplane mode
2742 *
2743 * @param enable whether to enable airplane mode or not
2744 *
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002745 * @hide
2746 */
Lorenzo Colittif5845d12018-10-09 18:55:11 +09002747 @RequiresPermission(anyOf = {
2748 android.Manifest.permission.NETWORK_SETTINGS,
2749 android.Manifest.permission.NETWORK_SETUP_WIZARD,
2750 android.Manifest.permission.NETWORK_STACK})
Lorenzo Colitti85eca482018-10-09 18:50:32 +09002751 @SystemApi
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002752 public void setAirplaneMode(boolean enable) {
2753 try {
2754 mService.setAirplaneMode(enable);
2755 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002756 throw e.rethrowFromSystemServer();
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002757 }
2758 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002759
2760 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002761 @UnsupportedAppUsage
Robert Greenwalta67be032014-05-16 15:49:14 -07002762 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07002763 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07002764 mService.registerNetworkFactory(messenger, name);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002765 } catch (RemoteException e) {
2766 throw e.rethrowFromSystemServer();
2767 }
Robert Greenwalta67be032014-05-16 15:49:14 -07002768 }
2769
2770 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002771 @UnsupportedAppUsage
Robert Greenwalta67be032014-05-16 15:49:14 -07002772 public void unregisterNetworkFactory(Messenger messenger) {
2773 try {
2774 mService.unregisterNetworkFactory(messenger);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002775 } catch (RemoteException e) {
2776 throw e.rethrowFromSystemServer();
2777 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002778 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002779
Paul Jensen31a94f42015-02-13 14:18:39 -05002780 /**
2781 * @hide
2782 * Register a NetworkAgent with ConnectivityService.
2783 * @return NetID corresponding to NetworkAgent.
2784 */
2785 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002786 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002787 try {
Paul Jensen31a94f42015-02-13 14:18:39 -05002788 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
2789 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002790 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -05002791 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002792 }
2793
Robert Greenwalt9258c642014-03-26 16:47:06 -07002794 /**
Hugo Benichidafed3d2017-03-06 09:17:06 +09002795 * Base class for {@code NetworkRequest} callbacks. Used for notifications about network
2796 * changes. Should be extended by applications wanting notifications.
2797 *
2798 * A {@code NetworkCallback} is registered by calling
2799 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
2800 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)},
Hugo Benichica867dc2018-02-07 21:17:43 +09002801 * or {@link #registerDefaultNetworkCallback(NetworkCallback)}. A {@code NetworkCallback} is
Hugo Benichidafed3d2017-03-06 09:17:06 +09002802 * unregistered by calling {@link #unregisterNetworkCallback(NetworkCallback)}.
2803 * A {@code NetworkCallback} should be registered at most once at any time.
2804 * A {@code NetworkCallback} that has been unregistered can be registered again.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002805 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002806 public static class NetworkCallback {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002807 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002808 * Called when the framework connects to a new network to evaluate whether it satisfies this
2809 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
2810 * callback. There is no guarantee that this new network will satisfy any requests, or that
2811 * the network will stay connected for longer than the time necessary to evaluate it.
2812 * <p>
2813 * Most applications <b>should not</b> act on this callback, and should instead use
2814 * {@link #onAvailable}. This callback is intended for use by applications that can assist
2815 * the framework in properly evaluating the network &mdash; for example, an application that
2816 * can automatically log in to a captive portal without user intervention.
2817 *
2818 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti66276122015-06-11 14:27:17 +09002819 *
2820 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002821 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002822 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002823
2824 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002825 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002826 * This callback may be called more than once if the {@link Network} that is
2827 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002828 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002829 * @param network The {@link Network} of the satisfying network.
Chalard Jean804b8fb2018-01-30 22:41:41 +09002830 * @param networkCapabilities The {@link NetworkCapabilities} of the satisfying network.
2831 * @param linkProperties The {@link LinkProperties} of the satisfying network.
junyulai05986c62018-08-07 19:50:45 +08002832 * @param blocked Whether access to the {@link Network} is blocked due to system policy.
Chalard Jean804b8fb2018-01-30 22:41:41 +09002833 * @hide
2834 */
2835 public void onAvailable(Network network, NetworkCapabilities networkCapabilities,
junyulai05986c62018-08-07 19:50:45 +08002836 LinkProperties linkProperties, boolean blocked) {
Chalard Jean804b8fb2018-01-30 22:41:41 +09002837 // Internally only this method is called when a new network is available, and
2838 // it calls the callback in the same way and order that older versions used
2839 // to call so as not to change the behavior.
2840 onAvailable(network);
2841 if (!networkCapabilities.hasCapability(
2842 NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)) {
2843 onNetworkSuspended(network);
2844 }
2845 onCapabilitiesChanged(network, networkCapabilities);
2846 onLinkPropertiesChanged(network, linkProperties);
junyulai05986c62018-08-07 19:50:45 +08002847 onBlockedStatusChanged(network, blocked);
Chalard Jean804b8fb2018-01-30 22:41:41 +09002848 }
2849
2850 /**
2851 * Called when the framework connects and has declared a new network ready for use.
2852 * This callback may be called more than once if the {@link Network} that is
2853 * satisfying the request changes. This will always immediately be followed by a
2854 * call to {@link #onCapabilitiesChanged(Network, NetworkCapabilities)} then by a
junyulai05986c62018-08-07 19:50:45 +08002855 * call to {@link #onLinkPropertiesChanged(Network, LinkProperties)}, and a call to
2856 * {@link #onBlockedStatusChanged(Network, boolean)}.
Chalard Jean804b8fb2018-01-30 22:41:41 +09002857 *
2858 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002859 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002860 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002861
2862 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002863 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002864 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002865 * for graceful handover. This may not be called if we have a hard loss
2866 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002867 * {@link NetworkCallback#onLost} call or a
2868 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002869 * on whether we lose or regain it.
2870 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002871 * @param network The {@link Network} that is about to be disconnected.
2872 * @param maxMsToLive The time in ms the framework will attempt to keep the
2873 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002874 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002875 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002876 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002877
2878 /**
2879 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002880 * graceful failure ends.
2881 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002882 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002883 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002884 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002885
2886 /**
Etan Cohenaebf17e2017-03-01 12:47:28 -08002887 * Called if no network is found in the timeout time specified in
Hugo Benichi0eec03f2017-05-15 15:15:33 +09002888 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} call. This callback is not
Etan Cohenaebf17e2017-03-01 12:47:28 -08002889 * called for the version of {@link #requestNetwork(NetworkRequest, NetworkCallback)}
2890 * without timeout. When this callback is invoked the associated
2891 * {@link NetworkRequest} will have already been removed and released, as if
2892 * {@link #unregisterNetworkCallback(NetworkCallback)} had been called.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002893 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002894 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002895
2896 /**
2897 * Called when the network the framework connected to for this request
2898 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002899 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002900 * @param network The {@link Network} whose capabilities have changed.
Chalard Jean804b8fb2018-01-30 22:41:41 +09002901 * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this
2902 * network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002903 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002904 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002905 NetworkCapabilities networkCapabilities) {}
2906
2907 /**
2908 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002909 * changes {@link LinkProperties}.
2910 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002911 * @param network The {@link Network} whose link properties have changed.
2912 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002913 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002914 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002915
Robert Greenwalt8d482522015-06-24 13:23:42 -07002916 /**
2917 * Called when the network the framework connected to for this request
Chalard Jean804b8fb2018-01-30 22:41:41 +09002918 * goes into {@link NetworkInfo.State#SUSPENDED}.
Robert Greenwalt8d482522015-06-24 13:23:42 -07002919 * This generally means that while the TCP connections are still live,
2920 * temporarily network data fails to transfer. Specifically this is used
2921 * on cellular networks to mask temporary outages when driving through
2922 * a tunnel, etc.
2923 * @hide
2924 */
2925 public void onNetworkSuspended(Network network) {}
2926
2927 /**
2928 * Called when the network the framework connected to for this request
Chalard Jean804b8fb2018-01-30 22:41:41 +09002929 * returns from a {@link NetworkInfo.State#SUSPENDED} state. This should always be
2930 * preceded by a matching {@link NetworkCallback#onNetworkSuspended} call.
Robert Greenwalt8d482522015-06-24 13:23:42 -07002931 * @hide
2932 */
2933 public void onNetworkResumed(Network network) {}
2934
junyulai05986c62018-08-07 19:50:45 +08002935 /**
2936 * Called when access to the specified network is blocked or unblocked.
2937 *
2938 * @param network The {@link Network} whose blocked status has changed.
2939 * @param blocked The blocked status of this {@link Network}.
2940 */
2941 public void onBlockedStatusChanged(Network network, boolean blocked) {}
2942
Robert Greenwalt6078b502014-06-11 16:05:07 -07002943 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002944 }
2945
Hugo Benichicb883232017-05-11 13:16:17 +09002946 /**
2947 * Constant error codes used by ConnectivityService to communicate about failures and errors
2948 * across a Binder boundary.
2949 * @hide
2950 */
2951 public interface Errors {
Chalard Jean4d660112018-06-04 16:52:49 +09002952 int TOO_MANY_REQUESTS = 1;
Hugo Benichicb883232017-05-11 13:16:17 +09002953 }
2954
2955 /** @hide */
2956 public static class TooManyRequestsException extends RuntimeException {}
2957
2958 private static RuntimeException convertServiceException(ServiceSpecificException e) {
2959 switch (e.errorCode) {
2960 case Errors.TOO_MANY_REQUESTS:
2961 return new TooManyRequestsException();
2962 default:
2963 Log.w(TAG, "Unknown service error code " + e.errorCode);
2964 return new RuntimeException(e);
2965 }
2966 }
2967
Robert Greenwalt9258c642014-03-26 16:47:06 -07002968 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002969 /** @hide */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002970 public static final int CALLBACK_PRECHECK = BASE + 1;
2971 /** @hide */
2972 public static final int CALLBACK_AVAILABLE = BASE + 2;
2973 /** @hide arg1 = TTL */
2974 public static final int CALLBACK_LOSING = BASE + 3;
2975 /** @hide */
2976 public static final int CALLBACK_LOST = BASE + 4;
2977 /** @hide */
2978 public static final int CALLBACK_UNAVAIL = BASE + 5;
2979 /** @hide */
2980 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2981 /** @hide */
2982 public static final int CALLBACK_IP_CHANGED = BASE + 7;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002983 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Hugo Benichidba33db2017-03-23 22:40:44 +09002984 private static final int EXPIRE_LEGACY_REQUEST = BASE + 8;
Robert Greenwalt8d482522015-06-24 13:23:42 -07002985 /** @hide */
Hugo Benichidba33db2017-03-23 22:40:44 +09002986 public static final int CALLBACK_SUSPENDED = BASE + 9;
Robert Greenwalt8d482522015-06-24 13:23:42 -07002987 /** @hide */
Hugo Benichidba33db2017-03-23 22:40:44 +09002988 public static final int CALLBACK_RESUMED = BASE + 10;
junyulai05986c62018-08-07 19:50:45 +08002989 /** @hide */
2990 public static final int CALLBACK_BLK_CHANGED = BASE + 11;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002991
Erik Kline57faba92015-11-25 12:49:38 +09002992 /** @hide */
2993 public static String getCallbackName(int whichCallback) {
2994 switch (whichCallback) {
2995 case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK";
2996 case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE";
2997 case CALLBACK_LOSING: return "CALLBACK_LOSING";
2998 case CALLBACK_LOST: return "CALLBACK_LOST";
2999 case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL";
3000 case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED";
3001 case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED";
Erik Kline57faba92015-11-25 12:49:38 +09003002 case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST";
3003 case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED";
3004 case CALLBACK_RESUMED: return "CALLBACK_RESUMED";
junyulai05986c62018-08-07 19:50:45 +08003005 case CALLBACK_BLK_CHANGED: return "CALLBACK_BLK_CHANGED";
Erik Kline57faba92015-11-25 12:49:38 +09003006 default:
3007 return Integer.toString(whichCallback);
3008 }
3009 }
3010
Robert Greenwalt562cc542014-05-15 18:07:26 -07003011 private class CallbackHandler extends Handler {
Robert Greenwalt9258c642014-03-26 16:47:06 -07003012 private static final String TAG = "ConnectivityManager.CallbackHandler";
Robert Greenwalta9ebeef2015-09-03 16:41:45 -07003013 private static final boolean DBG = false;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003014
Hugo Benichid42650f2016-07-06 22:53:17 +09003015 CallbackHandler(Looper looper) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07003016 super(looper);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003017 }
3018
Hugo Benichi2583ef02017-02-02 17:02:36 +09003019 CallbackHandler(Handler handler) {
Hugo Benichie7678512017-05-09 15:19:01 +09003020 this(Preconditions.checkNotNull(handler, "Handler cannot be null.").getLooper());
Hugo Benichi2583ef02017-02-02 17:02:36 +09003021 }
3022
Robert Greenwalt9258c642014-03-26 16:47:06 -07003023 @Override
3024 public void handleMessage(Message message) {
Hugo Benichi2c684522017-05-09 14:36:02 +09003025 if (message.what == EXPIRE_LEGACY_REQUEST) {
3026 expireRequest((NetworkCapabilities) message.obj, message.arg1);
3027 return;
3028 }
3029
3030 final NetworkRequest request = getObject(message, NetworkRequest.class);
3031 final Network network = getObject(message, Network.class);
3032 final NetworkCallback callback;
3033 synchronized (sCallbacks) {
3034 callback = sCallbacks.get(request);
3035 }
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09003036 if (DBG) {
Hugo Benichia0385682017-03-22 17:07:57 +09003037 Log.d(TAG, getCallbackName(message.what) + " for network " + network);
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09003038 }
Hugo Benichi2c684522017-05-09 14:36:02 +09003039 if (callback == null) {
3040 Log.w(TAG, "callback not found for " + getCallbackName(message.what) + " message");
3041 return;
3042 }
3043
Robert Greenwalt9258c642014-03-26 16:47:06 -07003044 switch (message.what) {
3045 case CALLBACK_PRECHECK: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003046 callback.onPreCheck(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003047 break;
3048 }
3049 case CALLBACK_AVAILABLE: {
Chalard Jean804b8fb2018-01-30 22:41:41 +09003050 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
3051 LinkProperties lp = getObject(message, LinkProperties.class);
junyulai05986c62018-08-07 19:50:45 +08003052 callback.onAvailable(network, cap, lp, message.arg1 != 0);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003053 break;
3054 }
3055 case CALLBACK_LOSING: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003056 callback.onLosing(network, message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003057 break;
3058 }
3059 case CALLBACK_LOST: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003060 callback.onLost(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003061 break;
3062 }
3063 case CALLBACK_UNAVAIL: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003064 callback.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07003065 break;
3066 }
3067 case CALLBACK_CAP_CHANGED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003068 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
3069 callback.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003070 break;
3071 }
3072 case CALLBACK_IP_CHANGED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003073 LinkProperties lp = getObject(message, LinkProperties.class);
3074 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003075 break;
3076 }
Robert Greenwalt8d482522015-06-24 13:23:42 -07003077 case CALLBACK_SUSPENDED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003078 callback.onNetworkSuspended(network);
Robert Greenwalt8d482522015-06-24 13:23:42 -07003079 break;
3080 }
3081 case CALLBACK_RESUMED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003082 callback.onNetworkResumed(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07003083 break;
3084 }
junyulai05986c62018-08-07 19:50:45 +08003085 case CALLBACK_BLK_CHANGED: {
3086 boolean blocked = message.arg1 != 0;
3087 callback.onBlockedStatusChanged(network, blocked);
3088 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003089 }
3090 }
3091
Hugo Benichid42650f2016-07-06 22:53:17 +09003092 private <T> T getObject(Message msg, Class<T> c) {
3093 return (T) msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07003094 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003095 }
3096
Hugo Benichi2583ef02017-02-02 17:02:36 +09003097 private CallbackHandler getDefaultHandler() {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003098 synchronized (sCallbacks) {
3099 if (sCallbackHandler == null) {
3100 sCallbackHandler = new CallbackHandler(ConnectivityThread.getInstanceLooper());
Robert Greenwalt9258c642014-03-26 16:47:06 -07003101 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003102 return sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003103 }
3104 }
3105
Hugo Benichi6f260f32017-02-03 14:18:44 +09003106 private static final HashMap<NetworkRequest, NetworkCallback> sCallbacks = new HashMap<>();
3107 private static CallbackHandler sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003108
Hugo Benichi6f260f32017-02-03 14:18:44 +09003109 private static final int LISTEN = 1;
3110 private static final int REQUEST = 2;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003111
Hugo Benichi6f260f32017-02-03 14:18:44 +09003112 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, NetworkCallback callback,
3113 int timeoutMs, int action, int legacyType, CallbackHandler handler) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09003114 printStackTrace();
Hugo Benichie7678512017-05-09 15:19:01 +09003115 checkCallbackNotNull(callback);
Hugo Benichidafed3d2017-03-06 09:17:06 +09003116 Preconditions.checkArgument(action == REQUEST || need != null, "null NetworkCapabilities");
Hugo Benichid42650f2016-07-06 22:53:17 +09003117 final NetworkRequest request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003118 try {
Hugo Benichid42650f2016-07-06 22:53:17 +09003119 synchronized(sCallbacks) {
Hugo Benichi31c176d2017-06-17 13:14:12 +09003120 if (callback.networkRequest != null
3121 && callback.networkRequest != ALREADY_UNREGISTERED) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09003122 // TODO: throw exception instead and enforce 1:1 mapping of callbacks
3123 // and requests (http://b/20701525).
3124 Log.e(TAG, "NetworkCallback was already registered");
3125 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003126 Messenger messenger = new Messenger(handler);
Hugo Benichid42650f2016-07-06 22:53:17 +09003127 Binder binder = new Binder();
Paul Jensen7221cc32014-06-27 11:05:32 -04003128 if (action == LISTEN) {
Hugo Benichid42650f2016-07-06 22:53:17 +09003129 request = mService.listenForNetwork(need, messenger, binder);
Paul Jensen7221cc32014-06-27 11:05:32 -04003130 } else {
Hugo Benichid42650f2016-07-06 22:53:17 +09003131 request = mService.requestNetwork(
3132 need, messenger, timeoutMs, binder, legacyType);
Paul Jensen7221cc32014-06-27 11:05:32 -04003133 }
Hugo Benichid42650f2016-07-06 22:53:17 +09003134 if (request != null) {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003135 sCallbacks.put(request, callback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003136 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003137 callback.networkRequest = request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003138 }
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003139 } catch (RemoteException e) {
3140 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003141 } catch (ServiceSpecificException e) {
3142 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003143 }
Hugo Benichid42650f2016-07-06 22:53:17 +09003144 return request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003145 }
3146
3147 /**
Erik Klinea2d29402016-03-16 15:31:39 +09003148 * Helper function to request a network with a particular legacy type.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09003149 *
3150 * This is temporarily public @hide so it can be called by system code that uses the
3151 * NetworkRequest API to request networks but relies on CONNECTIVITY_ACTION broadcasts for
3152 * instead network notifications.
3153 *
3154 * TODO: update said system code to rely on NetworkCallbacks and make this method private.
3155 *
3156 * @hide
3157 */
Lorenzo Colittid1179462015-11-25 15:47:14 +09003158 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
Hugo Benichi2583ef02017-02-02 17:02:36 +09003159 int timeoutMs, int legacyType, Handler handler) {
3160 CallbackHandler cbHandler = new CallbackHandler(handler);
3161 NetworkCapabilities nc = request.networkCapabilities;
3162 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, legacyType, cbHandler);
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09003163 }
3164
3165 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09003166 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003167 *
3168 * This {@link NetworkRequest} will live until released via
Etan Cohenaebf17e2017-03-01 12:47:28 -08003169 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A
3170 * version of the method which takes a timeout is
Hugo Benichi0eec03f2017-05-15 15:15:33 +09003171 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003172 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07003173 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003174 * can be used to direct traffic to the network.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003175 * <p>It is presently unsupported to request a network with mutable
3176 * {@link NetworkCapabilities} such as
3177 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3178 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3179 * as these {@code NetworkCapabilities} represent states that a particular
3180 * network may never attain, and whether a network will attain these states
3181 * is unknown prior to bringing up the network so the framework does not
3182 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003183 *
3184 * <p>This method requires the caller to hold either the
3185 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3186 * or the ability to modify system settings as determined by
3187 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt9258c642014-03-26 16:47:06 -07003188 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003189 * @param request {@link NetworkRequest} describing this request.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003190 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3191 * the callback must not be shared - it uniquely specifies this request.
3192 * The callback is invoked on the default internal Handler.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003193 * @throws IllegalArgumentException if {@code request} specifies any mutable
3194 * {@code NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003195 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003196 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003197 requestNetwork(request, networkCallback, getDefaultHandler());
3198 }
3199
3200 /**
3201 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
3202 *
3203 * This {@link NetworkRequest} will live until released via
Etan Cohenaebf17e2017-03-01 12:47:28 -08003204 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A
3205 * version of the method which takes a timeout is
Hugo Benichi0eec03f2017-05-15 15:15:33 +09003206 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003207 * Status of the request can be followed by listening to the various
3208 * callbacks described in {@link NetworkCallback}. The {@link Network}
3209 * can be used to direct traffic to the network.
3210 * <p>It is presently unsupported to request a network with mutable
3211 * {@link NetworkCapabilities} such as
3212 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3213 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3214 * as these {@code NetworkCapabilities} represent states that a particular
3215 * network may never attain, and whether a network will attain these states
3216 * is unknown prior to bringing up the network so the framework does not
Chalard Jean4d660112018-06-04 16:52:49 +09003217 * know how to go about satisfying a request with these capabilities.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003218 *
3219 * <p>This method requires the caller to hold either the
3220 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3221 * or the ability to modify system settings as determined by
3222 * {@link android.provider.Settings.System#canWrite}.</p>
3223 *
3224 * @param request {@link NetworkRequest} describing this request.
3225 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3226 * the callback must not be shared - it uniquely specifies this request.
3227 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
3228 * @throws IllegalArgumentException if {@code request} specifies any mutable
3229 * {@code NetworkCapabilities}.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003230 */
3231 public void requestNetwork(
3232 NetworkRequest request, NetworkCallback networkCallback, Handler handler) {
3233 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
3234 CallbackHandler cbHandler = new CallbackHandler(handler);
3235 requestNetwork(request, networkCallback, 0, legacyType, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003236 }
3237
3238 /**
Etan Cohenaebf17e2017-03-01 12:47:28 -08003239 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
3240 * by a timeout.
3241 *
3242 * This function behaves identically to the non-timed-out version
3243 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network
3244 * is not found within the given time (in milliseconds) the
3245 * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be
3246 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3247 * not have to be released if timed-out (it is automatically released). Unregistering a
3248 * request that timed out is not an error.
3249 *
3250 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3251 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3252 * for that purpose. Calling this method will attempt to bring up the requested network.
3253 *
3254 * <p>This method requires the caller to hold either the
3255 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3256 * or the ability to modify system settings as determined by
3257 * {@link android.provider.Settings.System#canWrite}.</p>
3258 *
3259 * @param request {@link NetworkRequest} describing this request.
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003260 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3261 * the callback must not be shared - it uniquely specifies this request.
Etan Cohenaebf17e2017-03-01 12:47:28 -08003262 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3263 * before {@link NetworkCallback#onUnavailable()} is called. The timeout must
3264 * be a positive value (i.e. >0).
Etan Cohenaebf17e2017-03-01 12:47:28 -08003265 */
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003266 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
3267 int timeoutMs) {
Hugo Benichie7678512017-05-09 15:19:01 +09003268 checkTimeout(timeoutMs);
Hugo Benichi2583ef02017-02-02 17:02:36 +09003269 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
Erik Klineb583b032017-02-22 12:58:24 +09003270 requestNetwork(request, networkCallback, timeoutMs, legacyType, getDefaultHandler());
Hugo Benichi2583ef02017-02-02 17:02:36 +09003271 }
3272
Hugo Benichi2583ef02017-02-02 17:02:36 +09003273 /**
3274 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
3275 * by a timeout.
3276 *
Chalard Jean4d660112018-06-04 16:52:49 +09003277 * This function behaves identically to the version without timeout, but if a suitable
Hugo Benichi2583ef02017-02-02 17:02:36 +09003278 * network is not found within the given time (in milliseconds) the
Etan Cohenaebf17e2017-03-01 12:47:28 -08003279 * {@link NetworkCallback#onUnavailable} callback is called. The request can still be
3280 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3281 * not have to be released if timed-out (it is automatically released). Unregistering a
3282 * request that timed out is not an error.
3283 *
3284 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3285 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3286 * for that purpose. Calling this method will attempt to bring up the requested network.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003287 *
3288 * <p>This method requires the caller to hold either the
3289 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3290 * or the ability to modify system settings as determined by
3291 * {@link android.provider.Settings.System#canWrite}.</p>
3292 *
3293 * @param request {@link NetworkRequest} describing this request.
Etan Cohenaebf17e2017-03-01 12:47:28 -08003294 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3295 * the callback must not be shared - it uniquely specifies this request.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003296 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003297 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3298 * before {@link NetworkCallback#onUnavailable} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003299 */
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003300 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
3301 Handler handler, int timeoutMs) {
Hugo Benichie7678512017-05-09 15:19:01 +09003302 checkTimeout(timeoutMs);
Hugo Benichi2583ef02017-02-02 17:02:36 +09003303 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
3304 CallbackHandler cbHandler = new CallbackHandler(handler);
3305 requestNetwork(request, networkCallback, timeoutMs, legacyType, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003306 }
3307
3308 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003309 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003310 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003311 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfcde58f2015-02-11 16:51:13 -08003312 * <p>
Paul Jensen72db88e2015-03-10 10:54:12 -04003313 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
3314 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003315 */
Erik Kline90e93072014-11-19 12:12:24 +09003316 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003317
3318 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07003319 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003320 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003321 * {@link android.content.Intent#getParcelableExtra(String)}.
3322 */
Erik Kline90e93072014-11-19 12:12:24 +09003323 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003324
3325
3326 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09003327 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003328 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003329 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07003330 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003331 * the request may outlive the calling application and get called back when a suitable
3332 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003333 * <p>
3334 * The operation is an Intent broadcast that goes to a broadcast receiver that
3335 * you registered with {@link Context#registerReceiver} or through the
3336 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3337 * <p>
3338 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09003339 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3340 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07003341 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07003342 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07003343 * Intent to reserve the network or it will be released shortly after the Intent
3344 * is processed.
3345 * <p>
Paul Jensen694f2b82015-06-17 14:15:39 -04003346 * If there is already a request for this Intent registered (with the equality of
Robert Greenwalt9258c642014-03-26 16:47:06 -07003347 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003348 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003349 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003350 * The request may be released normally by calling
3351 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003352 * <p>It is presently unsupported to request a network with either
3353 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3354 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3355 * as these {@code NetworkCapabilities} represent states that a particular
3356 * network may never attain, and whether a network will attain these states
3357 * is unknown prior to bringing up the network so the framework does not
Chalard Jean4d660112018-06-04 16:52:49 +09003358 * know how to go about satisfying a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003359 *
3360 * <p>This method requires the caller to hold either the
3361 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3362 * or the ability to modify system settings as determined by
3363 * {@link android.provider.Settings.System#canWrite}.</p>
3364 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003365 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003366 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07003367 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003368 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003369 * @throws IllegalArgumentException if {@code request} contains either
3370 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3371 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003372 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003373 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09003374 printStackTrace();
Hugo Benichie7678512017-05-09 15:19:01 +09003375 checkPendingIntentNotNull(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003376 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07003377 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003378 } catch (RemoteException e) {
3379 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003380 } catch (ServiceSpecificException e) {
3381 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003382 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003383 }
3384
3385 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003386 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
3387 * <p>
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003388 * This method has the same behavior as
3389 * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003390 * releasing network resources and disconnecting.
3391 *
3392 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3393 * PendingIntent passed to
3394 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
3395 * corresponding NetworkRequest you'd like to remove. Cannot be null.
3396 */
3397 public void releaseNetworkRequest(PendingIntent operation) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09003398 printStackTrace();
Hugo Benichie7678512017-05-09 15:19:01 +09003399 checkPendingIntentNotNull(operation);
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003400 try {
3401 mService.releasePendingNetworkRequest(operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003402 } catch (RemoteException e) {
3403 throw e.rethrowFromSystemServer();
3404 }
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003405 }
3406
Hugo Benichie7678512017-05-09 15:19:01 +09003407 private static void checkPendingIntentNotNull(PendingIntent intent) {
3408 Preconditions.checkNotNull(intent, "PendingIntent cannot be null.");
3409 }
3410
3411 private static void checkCallbackNotNull(NetworkCallback callback) {
3412 Preconditions.checkNotNull(callback, "null NetworkCallback");
3413 }
3414
3415 private static void checkTimeout(int timeoutMs) {
3416 Preconditions.checkArgumentPositive(timeoutMs, "timeoutMs must be strictly positive.");
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003417 }
3418
3419 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07003420 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07003421 * {@link NetworkRequest}. The callbacks will continue to be called until
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003422 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003423 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003424 * @param request {@link NetworkRequest} describing this request.
3425 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3426 * networks change state.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003427 * The callback is invoked on the default internal Handler.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003428 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003429 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt6078b502014-06-11 16:05:07 -07003430 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003431 registerNetworkCallback(request, networkCallback, getDefaultHandler());
3432 }
3433
3434 /**
3435 * Registers to receive notifications about all networks which satisfy the given
3436 * {@link NetworkRequest}. The callbacks will continue to be called until
3437 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003438 *
3439 * @param request {@link NetworkRequest} describing this request.
3440 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3441 * networks change state.
3442 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003443 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003444 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Hugo Benichi2583ef02017-02-02 17:02:36 +09003445 public void registerNetworkCallback(
3446 NetworkRequest request, NetworkCallback networkCallback, Handler handler) {
3447 CallbackHandler cbHandler = new CallbackHandler(handler);
3448 NetworkCapabilities nc = request.networkCapabilities;
3449 sendRequestForNetwork(nc, networkCallback, 0, LISTEN, TYPE_NONE, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003450 }
3451
3452 /**
Paul Jensen694f2b82015-06-17 14:15:39 -04003453 * Registers a PendingIntent to be sent when a network is available which satisfies the given
3454 * {@link NetworkRequest}.
3455 *
3456 * This function behaves identically to the version that takes a NetworkCallback, but instead
3457 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
3458 * the request may outlive the calling application and get called back when a suitable
3459 * network is found.
3460 * <p>
3461 * The operation is an Intent broadcast that goes to a broadcast receiver that
3462 * you registered with {@link Context#registerReceiver} or through the
3463 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3464 * <p>
3465 * The operation Intent is delivered with two extras, a {@link Network} typed
3466 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3467 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
3468 * the original requests parameters.
3469 * <p>
3470 * If there is already a request for this Intent registered (with the equality of
3471 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
3472 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
3473 * <p>
3474 * The request may be released normally by calling
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003475 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Paul Jensen694f2b82015-06-17 14:15:39 -04003476 * @param request {@link NetworkRequest} describing this request.
3477 * @param operation Action to perform when the network is available (corresponds
3478 * to the {@link NetworkCallback#onAvailable} call. Typically
3479 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
3480 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003481 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Paul Jensen694f2b82015-06-17 14:15:39 -04003482 public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09003483 printStackTrace();
Hugo Benichie7678512017-05-09 15:19:01 +09003484 checkPendingIntentNotNull(operation);
Paul Jensen694f2b82015-06-17 14:15:39 -04003485 try {
3486 mService.pendingListenForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003487 } catch (RemoteException e) {
3488 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003489 } catch (ServiceSpecificException e) {
3490 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003491 }
Paul Jensen694f2b82015-06-17 14:15:39 -04003492 }
3493
3494 /**
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003495 * Registers to receive notifications about changes in the system default network. The callbacks
3496 * will continue to be called until either the application exits or
3497 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Erik Klinea2d29402016-03-16 15:31:39 +09003498 *
3499 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3500 * system default network changes.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003501 * The callback is invoked on the default internal Handler.
Erik Klinea2d29402016-03-16 15:31:39 +09003502 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003503 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Erik Klinea2d29402016-03-16 15:31:39 +09003504 public void registerDefaultNetworkCallback(NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003505 registerDefaultNetworkCallback(networkCallback, getDefaultHandler());
3506 }
3507
3508 /**
3509 * Registers to receive notifications about changes in the system default network. The callbacks
3510 * will continue to be called until either the application exits or
3511 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003512 *
3513 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3514 * system default network changes.
3515 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003516 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003517 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Hugo Benichi2583ef02017-02-02 17:02:36 +09003518 public void registerDefaultNetworkCallback(NetworkCallback networkCallback, Handler handler) {
Erik Klinea2d29402016-03-16 15:31:39 +09003519 // This works because if the NetworkCapabilities are null,
3520 // ConnectivityService takes them from the default request.
3521 //
3522 // Since the capabilities are exactly the same as the default request's
3523 // capabilities, this request is guaranteed, at all times, to be
3524 // satisfied by the same network, if any, that satisfies the default
3525 // request, i.e., the system default network.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003526 CallbackHandler cbHandler = new CallbackHandler(handler);
Chalard Jean4d660112018-06-04 16:52:49 +09003527 sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0,
3528 REQUEST, TYPE_NONE, cbHandler);
Erik Klinea2d29402016-03-16 15:31:39 +09003529 }
3530
3531 /**
fengludb571472015-04-21 17:12:05 -07003532 * Requests bandwidth update for a given {@link Network} and returns whether the update request
3533 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
3534 * network connection for updated bandwidth information. The caller will be notified via
3535 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003536 * method assumes that the caller has previously called
3537 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network
3538 * changes.
fenglub15e72b2015-03-20 11:29:56 -07003539 *
fengluae519192015-04-27 14:28:04 -07003540 * @param network {@link Network} specifying which network you're interested.
fengludb571472015-04-21 17:12:05 -07003541 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglub15e72b2015-03-20 11:29:56 -07003542 */
fengludb571472015-04-21 17:12:05 -07003543 public boolean requestBandwidthUpdate(Network network) {
fenglub15e72b2015-03-20 11:29:56 -07003544 try {
fengludb571472015-04-21 17:12:05 -07003545 return mService.requestBandwidthUpdate(network);
fenglub15e72b2015-03-20 11:29:56 -07003546 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003547 throw e.rethrowFromSystemServer();
fenglub15e72b2015-03-20 11:29:56 -07003548 }
3549 }
3550
3551 /**
Hugo Benichidafed3d2017-03-06 09:17:06 +09003552 * Unregisters a {@code NetworkCallback} and possibly releases networks originating from
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003553 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and
3554 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls.
3555 * If the given {@code NetworkCallback} had previously been used with
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09003556 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
3557 * will be disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003558 *
Hugo Benichidafed3d2017-03-06 09:17:06 +09003559 * Notifications that would have triggered that {@code NetworkCallback} will immediately stop
3560 * triggering it as soon as this call returns.
3561 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003562 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003563 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003564 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09003565 printStackTrace();
Hugo Benichie7678512017-05-09 15:19:01 +09003566 checkCallbackNotNull(networkCallback);
Hugo Benichidafed3d2017-03-06 09:17:06 +09003567 final List<NetworkRequest> reqs = new ArrayList<>();
3568 // Find all requests associated to this callback and stop callback triggers immediately.
3569 // Callback is reusable immediately. http://b/20701525, http://b/35921499.
3570 synchronized (sCallbacks) {
Hugo Benichi31c176d2017-06-17 13:14:12 +09003571 Preconditions.checkArgument(networkCallback.networkRequest != null,
3572 "NetworkCallback was not registered");
3573 Preconditions.checkArgument(networkCallback.networkRequest != ALREADY_UNREGISTERED,
3574 "NetworkCallback was already unregistered");
Hugo Benichidafed3d2017-03-06 09:17:06 +09003575 for (Map.Entry<NetworkRequest, NetworkCallback> e : sCallbacks.entrySet()) {
3576 if (e.getValue() == networkCallback) {
3577 reqs.add(e.getKey());
3578 }
3579 }
3580 // TODO: throw exception if callback was registered more than once (http://b/20701525).
3581 for (NetworkRequest r : reqs) {
3582 try {
3583 mService.releaseNetworkRequest(r);
3584 } catch (RemoteException e) {
3585 throw e.rethrowFromSystemServer();
3586 }
3587 // Only remove mapping if rpc was successful.
3588 sCallbacks.remove(r);
3589 }
Hugo Benichi31c176d2017-06-17 13:14:12 +09003590 networkCallback.networkRequest = ALREADY_UNREGISTERED;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003591 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003592 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003593
3594 /**
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003595 * Unregisters a callback previously registered via
3596 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3597 *
3598 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3599 * PendingIntent passed to
3600 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3601 * Cannot be null.
3602 */
3603 public void unregisterNetworkCallback(PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003604 checkPendingIntentNotNull(operation);
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003605 releaseNetworkRequest(operation);
3606 }
3607
3608 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003609 * Informs the system whether it should switch to {@code network} regardless of whether it is
3610 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
3611 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
3612 * the system default network regardless of any other network that's currently connected. If
3613 * {@code always} is true, then the choice is remembered, so that the next time the user
3614 * connects to this network, the system will switch to it.
3615 *
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003616 * @param network The network to accept.
3617 * @param accept Whether to accept the network even if unvalidated.
3618 * @param always Whether to remember this choice in the future.
3619 *
3620 * @hide
3621 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003622 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003623 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
3624 try {
3625 mService.setAcceptUnvalidated(network, accept, always);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003626 } catch (RemoteException e) {
3627 throw e.rethrowFromSystemServer();
3628 }
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003629 }
3630
3631 /**
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003632 * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is
3633 * only meaningful if the system is configured not to penalize such networks, e.g., if the
3634 * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code
3635 * NETWORK_AVOID_BAD_WIFI setting is unset}.
3636 *
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003637 * @param network The network to accept.
3638 *
3639 * @hide
3640 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003641 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003642 public void setAvoidUnvalidated(Network network) {
3643 try {
3644 mService.setAvoidUnvalidated(network);
3645 } catch (RemoteException e) {
3646 throw e.rethrowFromSystemServer();
3647 }
3648 }
3649
3650 /**
Lorenzo Colitti4734cdb2017-04-27 14:30:21 +09003651 * Requests that the system open the captive portal app on the specified network.
3652 *
3653 * @param network The network to log into.
3654 *
3655 * @hide
3656 */
3657 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
3658 public void startCaptivePortalApp(Network network) {
3659 try {
3660 mService.startCaptivePortalApp(network);
3661 } catch (RemoteException e) {
3662 throw e.rethrowFromSystemServer();
3663 }
3664 }
3665
3666 /**
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003667 * It is acceptable to briefly use multipath data to provide seamless connectivity for
3668 * time-sensitive user-facing operations when the system default network is temporarily
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003669 * unresponsive. The amount of data should be limited (less than one megabyte for every call to
3670 * this method), and the operation should be infrequent to ensure that data usage is limited.
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003671 *
3672 * An example of such an operation might be a time-sensitive foreground activity, such as a
3673 * voice command, that the user is performing while walking out of range of a Wi-Fi network.
3674 */
3675 public static final int MULTIPATH_PREFERENCE_HANDOVER = 1 << 0;
3676
3677 /**
3678 * It is acceptable to use small amounts of multipath data on an ongoing basis to provide
3679 * a backup channel for traffic that is primarily going over another network.
3680 *
3681 * An example might be maintaining backup connections to peers or servers for the purpose of
3682 * fast fallback if the default network is temporarily unresponsive or disconnects. The traffic
3683 * on backup paths should be negligible compared to the traffic on the main path.
3684 */
3685 public static final int MULTIPATH_PREFERENCE_RELIABILITY = 1 << 1;
3686
3687 /**
3688 * It is acceptable to use metered data to improve network latency and performance.
3689 */
3690 public static final int MULTIPATH_PREFERENCE_PERFORMANCE = 1 << 2;
3691
3692 /**
3693 * Return value to use for unmetered networks. On such networks we currently set all the flags
3694 * to true.
3695 * @hide
3696 */
3697 public static final int MULTIPATH_PREFERENCE_UNMETERED =
3698 MULTIPATH_PREFERENCE_HANDOVER |
3699 MULTIPATH_PREFERENCE_RELIABILITY |
3700 MULTIPATH_PREFERENCE_PERFORMANCE;
3701
3702 /** @hide */
3703 @Retention(RetentionPolicy.SOURCE)
3704 @IntDef(flag = true, value = {
3705 MULTIPATH_PREFERENCE_HANDOVER,
3706 MULTIPATH_PREFERENCE_RELIABILITY,
3707 MULTIPATH_PREFERENCE_PERFORMANCE,
3708 })
3709 public @interface MultipathPreference {
3710 }
3711
3712 /**
3713 * Provides a hint to the calling application on whether it is desirable to use the
3714 * multinetwork APIs (e.g., {@link Network#openConnection}, {@link Network#bindSocket}, etc.)
3715 * for multipath data transfer on this network when it is not the system default network.
3716 * Applications desiring to use multipath network protocols should call this method before
3717 * each such operation.
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003718 *
3719 * @param network The network on which the application desires to use multipath data.
3720 * If {@code null}, this method will return the a preference that will generally
3721 * apply to metered networks.
3722 * @return a bitwise OR of zero or more of the {@code MULTIPATH_PREFERENCE_*} constants.
3723 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003724 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003725 public @MultipathPreference int getMultipathPreference(Network network) {
3726 try {
3727 return mService.getMultipathPreference(network);
3728 } catch (RemoteException e) {
3729 throw e.rethrowFromSystemServer();
3730 }
3731 }
3732
3733 /**
Stuart Scott984dc852015-03-30 13:17:11 -07003734 * Resets all connectivity manager settings back to factory defaults.
3735 * @hide
3736 */
3737 public void factoryReset() {
Stuart Scott984dc852015-03-30 13:17:11 -07003738 try {
Stuart Scottf1fb3972015-04-02 18:00:02 -07003739 mService.factoryReset();
Stuart Scott984dc852015-03-30 13:17:11 -07003740 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003741 throw e.rethrowFromSystemServer();
Stuart Scott984dc852015-03-30 13:17:11 -07003742 }
3743 }
3744
3745 /**
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003746 * Binds the current process to {@code network}. All Sockets created in the future
3747 * (and not explicitly bound via a bound SocketFactory from
3748 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3749 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3750 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3751 * work and all host name resolutions will fail. This is by design so an application doesn't
3752 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3753 * To clear binding pass {@code null} for {@code network}. Using individually bound
3754 * Sockets created by Network.getSocketFactory().createSocket() and
3755 * performing network-specific host name resolutions via
3756 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen72db88e2015-03-10 10:54:12 -04003757 * {@code bindProcessToNetwork}.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003758 *
3759 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3760 * the current binding.
3761 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3762 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003763 public boolean bindProcessToNetwork(Network network) {
Chalard Jean4d660112018-06-04 16:52:49 +09003764 // Forcing callers to call through non-static function ensures ConnectivityManager
Paul Jensen72db88e2015-03-10 10:54:12 -04003765 // instantiated.
3766 return setProcessDefaultNetwork(network);
3767 }
3768
3769 /**
3770 * Binds the current process to {@code network}. All Sockets created in the future
3771 * (and not explicitly bound via a bound SocketFactory from
3772 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3773 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3774 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3775 * work and all host name resolutions will fail. This is by design so an application doesn't
3776 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3777 * To clear binding pass {@code null} for {@code network}. Using individually bound
3778 * Sockets created by Network.getSocketFactory().createSocket() and
3779 * performing network-specific host name resolutions via
3780 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
3781 * {@code setProcessDefaultNetwork}.
3782 *
3783 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3784 * the current binding.
3785 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3786 * @deprecated This function can throw {@link IllegalStateException}. Use
3787 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
3788 * is a direct replacement.
3789 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003790 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003791 public static boolean setProcessDefaultNetwork(Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003792 int netId = (network == null) ? NETID_UNSET : network.netId;
Paul Jensen72db88e2015-03-10 10:54:12 -04003793 if (netId == NetworkUtils.getBoundNetworkForProcess()) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003794 return true;
3795 }
3796 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05003797 // Set HTTP proxy system properties to match network.
3798 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colittiec4c5552015-04-22 11:52:48 +09003799 try {
3800 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
3801 } catch (SecurityException e) {
3802 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
3803 Log.e(TAG, "Can't set proxy properties", e);
3804 }
Paul Jensenc91b5342014-08-27 12:38:45 -04003805 // Must flush DNS cache as new network may have different DNS resolutions.
3806 InetAddress.clearDnsCache();
3807 // Must flush socket pool as idle sockets will be bound to previous network and may
3808 // cause subsequent fetches to be performed on old network.
3809 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
3810 return true;
3811 } else {
3812 return false;
3813 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003814 }
3815
3816 /**
3817 * Returns the {@link Network} currently bound to this process via
Paul Jensen72db88e2015-03-10 10:54:12 -04003818 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003819 *
3820 * @return {@code Network} to which this process is bound, or {@code null}.
3821 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003822 public Network getBoundNetworkForProcess() {
3823 // Forcing callers to call thru non-static function ensures ConnectivityManager
3824 // instantiated.
3825 return getProcessDefaultNetwork();
3826 }
3827
3828 /**
3829 * Returns the {@link Network} currently bound to this process via
3830 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
3831 *
3832 * @return {@code Network} to which this process is bound, or {@code null}.
3833 * @deprecated Using this function can lead to other functions throwing
3834 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
3835 * {@code getBoundNetworkForProcess} is a direct replacement.
3836 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003837 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003838 public static Network getProcessDefaultNetwork() {
Paul Jensen72db88e2015-03-10 10:54:12 -04003839 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04003840 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003841 return new Network(netId);
3842 }
3843
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003844 private void unsupportedStartingFrom(int version) {
3845 if (Process.myUid() == Process.SYSTEM_UID) {
Lorenzo Colitti2e31a7c2018-09-28 11:31:55 +09003846 // The getApplicationInfo() call we make below is not supported in system context. Let
3847 // the call through here, and rely on the fact that ConnectivityService will refuse to
3848 // allow the system to use these APIs anyway.
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003849 return;
3850 }
3851
3852 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
3853 throw new UnsupportedOperationException(
3854 "This method is not supported in target SDK version " + version + " and above");
3855 }
3856 }
3857
3858 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
3859 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
Lifu Tang30f95a72016-01-07 23:20:38 -08003860 // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003861 // remove these exemptions. Note that this check is not secure, and apps can still access these
3862 // functions by accessing ConnectivityService directly. However, it should be clear that doing
3863 // so is unsupported and may break in the future. http://b/22728205
3864 private void checkLegacyRoutingApiAccess() {
Dianne Hackborn692a2442015-07-31 10:35:34 -07003865 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003866 }
3867
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003868 /**
3869 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen72db88e2015-03-10 10:54:12 -04003870 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003871 *
3872 * @param network The {@link Network} to bind host resolutions from the current process to, or
3873 * {@code null} to clear the current binding.
3874 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3875 * @hide
3876 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
3877 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003878 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01003879 @UnsupportedAppUsage
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003880 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04003881 return NetworkUtils.bindProcessToNetworkForHostResolution(
Erik Klinef4fa9822018-04-27 22:48:33 +09003882 (network == null) ? NETID_UNSET : network.getNetIdForResolv());
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003883 }
Felipe Leme1b103232016-01-22 09:44:57 -08003884
3885 /**
3886 * Device is not restricting metered network activity while application is running on
3887 * background.
3888 */
3889 public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1;
3890
3891 /**
3892 * Device is restricting metered network activity while application is running on background,
3893 * but application is allowed to bypass it.
3894 * <p>
3895 * In this state, application should take action to mitigate metered network access.
3896 * For example, a music streaming application should switch to a low-bandwidth bitrate.
3897 */
3898 public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2;
3899
3900 /**
3901 * Device is restricting metered network activity while application is running on background.
Felipe Leme9778f762016-01-27 14:46:39 -08003902 * <p>
Felipe Leme1b103232016-01-22 09:44:57 -08003903 * In this state, application should not try to use the network while running on background,
3904 * because it would be denied.
3905 */
3906 public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3;
3907
Felipe Leme9778f762016-01-27 14:46:39 -08003908 /**
3909 * A change in the background metered network activity restriction has occurred.
3910 * <p>
3911 * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction
3912 * applies to them.
3913 * <p>
3914 * This is only sent to registered receivers, not manifest receivers.
3915 */
3916 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
3917 public static final String ACTION_RESTRICT_BACKGROUND_CHANGED =
3918 "android.net.conn.RESTRICT_BACKGROUND_CHANGED";
3919
Felipe Lemeecfccea2016-01-25 11:48:04 -08003920 /** @hide */
3921 @Retention(RetentionPolicy.SOURCE)
Felipe Leme1b103232016-01-22 09:44:57 -08003922 @IntDef(flag = false, value = {
3923 RESTRICT_BACKGROUND_STATUS_DISABLED,
3924 RESTRICT_BACKGROUND_STATUS_WHITELISTED,
3925 RESTRICT_BACKGROUND_STATUS_ENABLED,
3926 })
Felipe Leme1b103232016-01-22 09:44:57 -08003927 public @interface RestrictBackgroundStatus {
3928 }
3929
3930 private INetworkPolicyManager getNetworkPolicyManager() {
3931 synchronized (this) {
3932 if (mNPManager != null) {
3933 return mNPManager;
3934 }
3935 mNPManager = INetworkPolicyManager.Stub.asInterface(ServiceManager
3936 .getService(Context.NETWORK_POLICY_SERVICE));
3937 return mNPManager;
3938 }
3939 }
3940
3941 /**
3942 * Determines if the calling application is subject to metered network restrictions while
3943 * running on background.
Felipe Lemec9c7be52016-05-16 13:57:19 -07003944 *
3945 * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED},
3946 * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED},
3947 * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED}
Felipe Leme1b103232016-01-22 09:44:57 -08003948 */
3949 public @RestrictBackgroundStatus int getRestrictBackgroundStatus() {
3950 try {
3951 return getNetworkPolicyManager().getRestrictBackgroundByCaller();
3952 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003953 throw e.rethrowFromSystemServer();
Felipe Leme1b103232016-01-22 09:44:57 -08003954 }
3955 }
Ricky Wai44dcbde2018-01-23 04:09:45 +00003956
3957 /**
3958 * The network watchlist is a list of domains and IP addresses that are associated with
Ricky Waia86d5d52018-03-20 14:20:54 +00003959 * potentially harmful apps. This method returns the SHA-256 of the watchlist config file
3960 * currently used by the system for validation purposes.
Ricky Wai44dcbde2018-01-23 04:09:45 +00003961 *
3962 * @return Hash of network watchlist config file. Null if config does not exist.
3963 */
3964 public byte[] getNetworkWatchlistConfigHash() {
3965 try {
3966 return mService.getNetworkWatchlistConfigHash();
3967 } catch (RemoteException e) {
3968 Log.e(TAG, "Unable to get watchlist config hash");
3969 throw e.rethrowFromSystemServer();
3970 }
3971 }
Jeff Vander Stoep0ac2c092018-07-23 10:57:53 -07003972
3973 /**
3974 * Returns the {@code uid} of the owner of a network connection.
3975 *
3976 * @param protocol The protocol of the connection. Only {@code IPPROTO_TCP} and
3977 * {@code IPPROTO_UDP} currently supported.
3978 * @param local The local {@link InetSocketAddress} of a connection.
3979 * @param remote The remote {@link InetSocketAddress} of a connection.
3980 *
3981 * @return {@code uid} if the connection is found and the app has permission to observe it
3982 * (e.g., if it is associated with the calling VPN app's tunnel) or
3983 * {@link android.os.Process#INVALID_UID} if the connection is not found.
3984 */
3985 public int getConnectionOwnerUid(int protocol, InetSocketAddress local,
3986 InetSocketAddress remote) {
3987 ConnectionInfo connectionInfo = new ConnectionInfo(protocol, local, remote);
3988 try {
3989 return mService.getConnectionOwnerUid(connectionInfo);
3990 } catch (RemoteException e) {
3991 throw e.rethrowFromSystemServer();
3992 }
3993 }
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09003994
3995 private void printStackTrace() {
3996 if (DEBUG) {
3997 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
3998 final StringBuffer sb = new StringBuffer();
3999 for (int i = 3; i < callStack.length; i++) {
4000 final String stackTrace = callStack[i].toString();
4001 if (stackTrace == null || stackTrace.contains("android.os")) {
4002 break;
4003 }
4004 sb.append(" [").append(stackTrace).append("]");
4005 }
4006 Log.d(TAG, "StackLog:" + sb.toString());
4007 }
4008 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08004009}