blob: c3b8f3959fecf02581a4f56a9f90a4acec525412 [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;
Robert Greenwalt9258c642014-03-26 16:47:06 -070025import android.app.PendingIntent;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070026import android.content.Context;
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070027import android.content.Intent;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +090028import android.content.pm.PackageManager;
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;
Hugo Benichidafed3d2017-03-06 09:17:06 +090061import java.util.ArrayList;
Jeremy Kleind42209d2015-12-28 15:11:58 -080062import java.util.HashMap;
Hugo Benichidafed3d2017-03-06 09:17:06 +090063import java.util.List;
64import java.util.Map;
Jeremy Kleind42209d2015-12-28 15:11:58 -080065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066/**
67 * Class that answers queries about the state of network connectivity. It also
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060068 * notifies applications when network connectivity changes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 * <p>
70 * The primary responsibilities of this class are to:
71 * <ol>
72 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
73 * <li>Send broadcast intents when network connectivity changes</li>
74 * <li>Attempt to "fail over" to another network when connectivity to a network
75 * is lost</li>
76 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
77 * state of the available networks</li>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070078 * <li>Provide an API that allows applications to request and select networks for their data
79 * traffic</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 * </ol>
81 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060082@SystemService(Context.CONNECTIVITY_SERVICE)
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070083public class ConnectivityManager {
84 private static final String TAG = "ConnectivityManager";
85
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070087 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 * been established or lost. The NetworkInfo for the affected network is
89 * sent as an extra; it should be consulted to see what kind of
90 * connectivity event occurred.
91 * <p/>
Mark Lu33ec1062016-12-05 10:57:55 -080092 * Apps targeting Android 7.0 (API level 24) and higher do not receive this
93 * broadcast if they declare the broadcast receiver in their manifest. Apps
94 * will still receive broadcasts if they register their
95 * {@link android.content.BroadcastReceiver} with
96 * {@link android.content.Context#registerReceiver Context.registerReceiver()}
97 * and that context is still valid.
98 * <p/>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 * If this is a connection that was the result of failing over from a
100 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
101 * set to true.
102 * <p/>
103 * For a loss of connectivity, if the connectivity manager is attempting
104 * to connect (or has already connected) to another network, the
105 * NetworkInfo for the new network is also passed as an extra. This lets
106 * any receivers of the broadcast know that they should not necessarily
107 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800108 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 * the failover attempt succeeded (and so there is still overall data
110 * connectivity), or that the failover attempt failed, meaning that all
111 * connectivity has been lost.
112 * <p/>
113 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
114 * is set to {@code true} if there are no connected networks at all.
Chalard Jean054cd162018-02-10 05:33:50 +0900115 *
116 * @deprecated apps should use the more versatile {@link #requestNetwork},
117 * {@link #registerNetworkCallback} or {@link #registerDefaultNetworkCallback}
118 * functions instead for faster and more detailed updates about the network
119 * changes they care about.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800121 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jean054cd162018-02-10 05:33:50 +0900122 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 /**
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700126 * A temporary hack until SUPL system can get off the legacy APIS.
127 * They do too many network requests and the long list of apps listening
128 * and waking due to the CONNECTIVITY_ACTION bcast makes it expensive.
129 * Use this bcast intent instead for SUPL requests.
130 * @hide
131 */
132 public static final String CONNECTIVITY_ACTION_SUPL =
133 "android.net.conn.CONNECTIVITY_CHANGE_SUPL";
134
135 /**
Paul Jensen25a217c2015-02-27 22:55:47 -0500136 * The device has connected to a network that has presented a captive
137 * portal, which is blocking Internet connectivity. The user was presented
138 * with a notification that network sign in is required,
139 * and the user invoked the notification's action indicating they
Paul Jensen49e3edf2015-05-22 10:50:39 -0400140 * desire to sign in to the network. Apps handling this activity should
Paul Jensen25a217c2015-02-27 22:55:47 -0500141 * facilitate signing in to the network. This action includes a
142 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents
143 * the network presenting the captive portal; all communication with the
144 * captive portal must be done using this {@code Network} object.
145 * <p/>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400146 * This activity includes a {@link CaptivePortal} extra named
147 * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different
148 * outcomes of the captive portal sign in to the system:
149 * <ul>
150 * <li> When the app handling this action believes the user has signed in to
151 * the network and the captive portal has been dismissed, the app should
152 * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can
153 * reevaluate the network. If reevaluation finds the network no longer
154 * subject to a captive portal, the network may become the default active
155 * data network. </li>
156 * <li> When the app handling this action believes the user explicitly wants
Paul Jensen25a217c2015-02-27 22:55:47 -0500157 * to ignore the captive portal and the network, the app should call
Paul Jensen49e3edf2015-05-22 10:50:39 -0400158 * {@link CaptivePortal#ignoreNetwork}. </li>
159 * </ul>
Paul Jensen25a217c2015-02-27 22:55:47 -0500160 */
161 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
162 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL";
163
164 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 * The lookup key for a {@link NetworkInfo} object. Retrieve with
166 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700167 *
168 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
169 * should always obtain network information through
Paul Jensen3541e9f2015-03-18 12:23:02 -0400170 * {@link #getActiveNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700171 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700173 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700177 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700178 *
179 * @see android.content.Intent#getIntExtra(String, int)
180 */
181 public static final String EXTRA_NETWORK_TYPE = "networkType";
182
183 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 * The lookup key for a boolean that indicates whether a connect event
185 * is for a network to which the connectivity manager was failing over
186 * following a disconnect on another network.
187 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
188 */
189 public static final String EXTRA_IS_FAILOVER = "isFailover";
190 /**
191 * The lookup key for a {@link NetworkInfo} object. This is supplied when
192 * there is another network that it may be possible to connect to. Retrieve with
193 * {@link android.content.Intent#getParcelableExtra(String)}.
194 */
195 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
196 /**
197 * The lookup key for a boolean that indicates whether there is a
198 * complete lack of connectivity, i.e., no network is available.
199 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
200 */
201 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
202 /**
203 * The lookup key for a string that indicates why an attempt to connect
204 * to a network failed. The string has no particular structure. It is
205 * intended to be used in notifications presented to users. Retrieve
206 * it with {@link android.content.Intent#getStringExtra(String)}.
207 */
208 public static final String EXTRA_REASON = "reason";
209 /**
210 * The lookup key for a string that provides optionally supplied
211 * extra information about the network state. The information
212 * may be passed up from the lower networking layers, and its
213 * meaning may be specific to a particular network type. Retrieve
214 * it with {@link android.content.Intent#getStringExtra(String)}.
215 */
216 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700217 /**
218 * The lookup key for an int that provides information about
219 * our connection to the internet at large. 0 indicates no connection,
220 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700221 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700222 * {@hide}
223 */
224 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 /**
Paul Jensen49e3edf2015-05-22 10:50:39 -0400226 * The lookup key for a {@link CaptivePortal} object included with the
227 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal}
228 * object can be used to either indicate to the system that the captive
229 * portal has been dismissed or that the user does not want to pursue
230 * signing in to captive portal. Retrieve it with
231 * {@link android.content.Intent#getParcelableExtra(String)}.
Paul Jensen25a217c2015-02-27 22:55:47 -0500232 */
Paul Jensen49e3edf2015-05-22 10:50:39 -0400233 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL";
Jan Nordqvist52eb29f2015-09-22 15:54:32 -0700234
235 /**
236 * Key for passing a URL to the captive portal login activity.
237 */
238 public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL";
239
Paul Jensen25a217c2015-02-27 22:55:47 -0500240 /**
Hugo Benichicdf3ba42016-12-14 08:23:40 +0900241 * Key for passing a user agent string to the captive portal login activity.
242 * {@hide}
243 */
244 public static final String EXTRA_CAPTIVE_PORTAL_USER_AGENT =
245 "android.net.extra.CAPTIVE_PORTAL_USER_AGENT";
246
247 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700248 * Broadcast action to indicate the change of data activity status
249 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800250 * The network becomes active when data transmission is started, or
251 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700252 * {@hide}
253 */
254 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
255 public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
256 /**
257 * The lookup key for an enum that indicates the network device type on which this data activity
258 * change happens.
259 * {@hide}
260 */
261 public static final String EXTRA_DEVICE_TYPE = "deviceType";
262 /**
263 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
264 * it is actively sending or receiving data and {@code false} means it is idle.
265 * {@hide}
266 */
267 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700268 /**
269 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
270 * {@hide}
271 */
272 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700273
274 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800275 * Broadcast Action: The setting for background data usage has changed
276 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
277 * <p>
278 * If an application uses the network in the background, it should listen
279 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700280 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800281 * <p>
282 *
283 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
284 * of background data depends on several combined factors, and
285 * this broadcast is no longer sent. Instead, when background
286 * data is unavailable, {@link #getActiveNetworkInfo()} will now
287 * appear disconnected. During first boot after a platform
288 * upgrade, this broadcast will be sent once if
289 * {@link #getBackgroundDataSetting()} was {@code false} before
290 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800291 */
292 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800293 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800294 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
295 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
296
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700297 /**
298 * Broadcast Action: The network connection may not be good
299 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
300 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
301 * the network and it's condition.
302 * @hide
303 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800304 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700305 public static final String INET_CONDITION_ACTION =
306 "android.net.conn.INET_CONDITION_ACTION";
307
Robert Greenwalt42acef32009-08-12 16:08:25 -0700308 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800309 * Broadcast Action: A tetherable connection has come or gone.
310 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
Erik Kline8351faa2017-04-17 16:47:23 +0900311 * {@code ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY},
312 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER}, and
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800313 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
314 * the current state of tethering. Each include a list of
315 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800316 * @hide
317 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800318 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800319 public static final String ACTION_TETHER_STATE_CHANGED =
320 "android.net.conn.TETHER_STATE_CHANGED";
321
322 /**
323 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800324 * gives a String[] listing all the interfaces configured for
325 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800326 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800327 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800328
329 /**
330 * @hide
Erik Kline8351faa2017-04-17 16:47:23 +0900331 * gives a String[] listing all the interfaces currently in local-only
332 * mode (ie, has DHCPv4+IPv6-ULA support and no packet forwarding)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800333 */
Erik Kline8351faa2017-04-17 16:47:23 +0900334 public static final String EXTRA_ACTIVE_LOCAL_ONLY = "localOnlyArray";
335
336 /**
337 * @hide
338 * gives a String[] listing all the interfaces currently tethered
339 * (ie, has DHCPv4 support and packets potentially forwarded/NATed)
340 */
341 public static final String EXTRA_ACTIVE_TETHER = "tetherArray";
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800342
343 /**
344 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800345 * gives a String[] listing all the interfaces we tried to tether and
346 * failed. Use {@link #getLastTetherError} to find the error code
347 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800348 */
349 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800350
351 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800352 * Broadcast Action: The captive portal tracker has finished its test.
353 * Sent only while running Setup Wizard, in lieu of showing a user
354 * notification.
355 * @hide
356 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800357 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800358 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
359 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
360 /**
361 * The lookup key for a boolean that indicates whether a captive portal was detected.
362 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
363 * @hide
364 */
365 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
366
367 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900368 * Action used to display a dialog that asks the user whether to connect to a network that is
369 * not validated. This intent is used to start the dialog in settings via startActivity.
370 *
371 * @hide
372 */
373 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED";
374
375 /**
Lorenzo Colitti9be58c52016-09-15 14:02:29 +0900376 * Action used to display a dialog that asks the user whether to avoid a network that is no
377 * longer validated. This intent is used to start the dialog in settings via startActivity.
378 *
379 * @hide
380 */
381 public static final String ACTION_PROMPT_LOST_VALIDATION =
382 "android.net.conn.PROMPT_LOST_VALIDATION";
383
384 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800385 * Invalid tethering type.
386 * @see #startTethering(int, OnStartTetheringCallback, boolean)
387 * @hide
388 */
389 public static final int TETHERING_INVALID = -1;
390
391 /**
392 * Wifi tethering type.
393 * @see #startTethering(int, OnStartTetheringCallback, boolean)
394 * @hide
395 */
396 @SystemApi
397 public static final int TETHERING_WIFI = 0;
398
399 /**
400 * USB tethering type.
401 * @see #startTethering(int, OnStartTetheringCallback, boolean)
402 * @hide
403 */
404 @SystemApi
405 public static final int TETHERING_USB = 1;
406
407 /**
408 * Bluetooth tethering type.
409 * @see #startTethering(int, OnStartTetheringCallback, boolean)
410 * @hide
411 */
412 @SystemApi
413 public static final int TETHERING_BLUETOOTH = 2;
414
415 /**
416 * Extra used for communicating with the TetherService. Includes the type of tethering to
417 * enable if any.
418 * @hide
419 */
420 public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType";
421
422 /**
423 * Extra used for communicating with the TetherService. Includes the type of tethering for
424 * which to cancel provisioning.
425 * @hide
426 */
427 public static final String EXTRA_REM_TETHER_TYPE = "extraRemTetherType";
428
429 /**
430 * Extra used for communicating with the TetherService. True to schedule a recheck of tether
431 * provisioning.
432 * @hide
433 */
434 public static final String EXTRA_SET_ALARM = "extraSetAlarm";
435
436 /**
437 * Tells the TetherService to run a provision check now.
438 * @hide
439 */
440 public static final String EXTRA_RUN_PROVISION = "extraRunProvision";
441
442 /**
443 * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver}
444 * which will receive provisioning results. Can be left empty.
445 * @hide
446 */
447 public static final String EXTRA_PROVISION_CALLBACK = "extraProvisionCallback";
448
449 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800450 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700451 * @hide
452 */
453 public static final int TYPE_NONE = -1;
454
455 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900456 * A Mobile data connection. Devices may support more than one.
457 *
458 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
459 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
460 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700461 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900462 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700463 public static final int TYPE_MOBILE = 0;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900464
Robert Greenwalt42acef32009-08-12 16:08:25 -0700465 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900466 * A WIFI data connection. Devices may support more than one.
467 *
468 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
469 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
470 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700471 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900472 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700473 public static final int TYPE_WIFI = 1;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900474
Robert Greenwalt42acef32009-08-12 16:08:25 -0700475 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800476 * An MMS-specific Mobile data connection. This network type may use the
477 * same network interface as {@link #TYPE_MOBILE} or it may use a different
478 * one. This is used by applications needing to talk to the carrier's
479 * Multimedia Messaging Service servers.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900480 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900481 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900482 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900483 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700484 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700485 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700486 public static final int TYPE_MOBILE_MMS = 2;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900487
Robert Greenwalt42acef32009-08-12 16:08:25 -0700488 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800489 * A SUPL-specific Mobile data connection. This network type may use the
490 * same network interface as {@link #TYPE_MOBILE} or it may use a different
491 * one. This is used by applications needing to talk to the carrier's
492 * Secure User Plane Location servers for help locating the device.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900493 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900494 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900495 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900496 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700497 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700498 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700499 public static final int TYPE_MOBILE_SUPL = 3;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900500
Robert Greenwalt42acef32009-08-12 16:08:25 -0700501 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800502 * A DUN-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 sometimes by the system when setting up an upstream connection
505 * for tethering so that the carrier is aware of DUN traffic.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900506 *
507 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
508 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
509 * provides the {@link NetworkCapabilities#NET_CAPABILITY_DUN} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700510 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900511 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700512 public static final int TYPE_MOBILE_DUN = 4;
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 High Priority Mobile data connection. This network type uses the
516 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colittie285b432015-04-23 15:32:42 +0900517 * is different.
518 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900519 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
520 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
521 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700522 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700523 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700524 public static final int TYPE_MOBILE_HIPRI = 5;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900525
jsh8214deb2010-03-11 15:04:43 -0800526 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900527 * A WiMAX data connection.
528 *
529 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
530 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
531 * appropriate network. {@see NetworkCapabilities} for supported transports.
jsh8214deb2010-03-11 15:04:43 -0800532 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900533 @Deprecated
jsh8214deb2010-03-11 15:04:43 -0800534 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800535
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800536 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900537 * A Bluetooth data connection.
538 *
539 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
540 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
541 * appropriate network. {@see NetworkCapabilities} for supported transports.
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800542 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900543 @Deprecated
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800544 public static final int TYPE_BLUETOOTH = 7;
545
Robert Greenwalt60810842011-04-22 15:28:18 -0700546 /**
547 * Dummy data connection. This should not be used on shipping devices.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900548 * @deprecated This is not used any more.
Robert Greenwalt60810842011-04-22 15:28:18 -0700549 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900550 @Deprecated
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800551 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800552
Robert Greenwalt60810842011-04-22 15:28:18 -0700553 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900554 * An Ethernet data connection.
555 *
556 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
557 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
558 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt60810842011-04-22 15:28:18 -0700559 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900560 @Deprecated
Robert Greenwalte12aec92011-01-28 14:48:37 -0800561 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700562
Wink Saville9d7d6282011-03-12 14:52:01 -0800563 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800564 * Over the air Administration.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900565 * @deprecated Use {@link NetworkCapabilities} instead.
Wink Saville9d7d6282011-03-12 14:52:01 -0800566 * {@hide}
567 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900568 @Deprecated
Wink Saville9d7d6282011-03-12 14:52:01 -0800569 public static final int TYPE_MOBILE_FOTA = 10;
570
571 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800572 * IP Multimedia Subsystem.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900573 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IMS} instead.
Wink Saville9d7d6282011-03-12 14:52:01 -0800574 * {@hide}
575 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900576 @Deprecated
Wink Saville9d7d6282011-03-12 14:52:01 -0800577 public static final int TYPE_MOBILE_IMS = 11;
578
579 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800580 * Carrier Branded Services.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900581 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_CBS} instead.
Wink Saville9d7d6282011-03-12 14:52:01 -0800582 * {@hide}
583 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900584 @Deprecated
Wink Saville9d7d6282011-03-12 14:52:01 -0800585 public static final int TYPE_MOBILE_CBS = 12;
586
repo syncaea743a2011-07-29 23:55:49 -0700587 /**
588 * A Wi-Fi p2p connection. Only requesting processes will have access to
589 * the peers connected.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900590 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_WIFI_P2P} instead.
repo syncaea743a2011-07-29 23:55:49 -0700591 * {@hide}
592 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900593 @Deprecated
repo syncaea743a2011-07-29 23:55:49 -0700594 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800595
Wink Saville5e56bc52013-07-29 15:00:57 -0700596 /**
597 * The network to use for initially attaching to the network
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900598 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IA} instead.
Wink Saville5e56bc52013-07-29 15:00:57 -0700599 * {@hide}
600 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900601 @Deprecated
Wink Saville5e56bc52013-07-29 15:00:57 -0700602 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700603
Lorenzo Colittie285b432015-04-23 15:32:42 +0900604 /**
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700605 * Emergency PDN connection for emergency services. This
606 * may include IMS and MMS in emergency situations.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900607 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_EIMS} instead.
Ram3e0e3bc2014-06-26 11:03:44 -0700608 * {@hide}
609 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900610 @Deprecated
Ram3e0e3bc2014-06-26 11:03:44 -0700611 public static final int TYPE_MOBILE_EMERGENCY = 15;
612
Hui Lu1c5624a2014-01-15 11:05:36 -0500613 /**
614 * The network that uses proxy to achieve connectivity.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900615 * @deprecated Use {@link NetworkCapabilities} instead.
Hui Lu1c5624a2014-01-15 11:05:36 -0500616 * {@hide}
617 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900618 @Deprecated
Hui Lu1c5624a2014-01-15 11:05:36 -0500619 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700620
Robert Greenwalt8283f882014-07-07 17:09:01 -0700621 /**
622 * A virtual network using one or more native bearers.
623 * It may or may not be providing security services.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900624 * @deprecated Applications should use {@link NetworkCapabilities#TRANSPORT_VPN} instead.
Robert Greenwalt8283f882014-07-07 17:09:01 -0700625 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900626 @Deprecated
Robert Greenwalt8283f882014-07-07 17:09:01 -0700627 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500628
629 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700630 public static final int MAX_RADIO_TYPE = TYPE_VPN;
631
632 /** {@hide} */
633 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800634
Hugo Benichi16f0a942017-06-20 14:07:59 +0900635 private static final int MIN_NETWORK_TYPE = TYPE_MOBILE;
636
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800637 /**
638 * If you want to set the default network preference,you can directly
639 * change the networkAttributes array in framework's config.xml.
640 *
641 * @deprecated Since we support so many more networks now, the single
642 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800643 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800644 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800645 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800646 * from an App.
647 */
648 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800649 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
650
Jeff Sharkey625239a2012-09-26 22:03:49 -0700651 /**
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700652 * @hide
653 */
Hugo Benichia5c1f7f2017-06-20 14:10:14 +0900654 public static final int REQUEST_ID_UNSET = 0;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700655
Paul Jensen5d59e782014-07-11 12:28:19 -0400656 /**
Hugo Benichi31c176d2017-06-17 13:14:12 +0900657 * Static unique request used as a tombstone for NetworkCallbacks that have been unregistered.
658 * This allows to distinguish when unregistering NetworkCallbacks those that were never
659 * registered and those that were already unregistered.
660 * @hide
661 */
Hugo Benichia5c1f7f2017-06-20 14:10:14 +0900662 private static final NetworkRequest ALREADY_UNREGISTERED =
Hugo Benichi31c176d2017-06-17 13:14:12 +0900663 new NetworkRequest.Builder().clearCapabilities().build();
664
665 /**
Paul Jensen5d59e782014-07-11 12:28:19 -0400666 * A NetID indicating no Network is selected.
667 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
668 * @hide
669 */
670 public static final int NETID_UNSET = 0;
671
Erik Kline4d092232017-10-30 15:29:44 +0900672 /**
673 * Private DNS Mode values.
674 *
675 * The "private_dns_mode" global setting stores a String value which is
676 * expected to be one of the following.
677 */
678
679 /**
680 * @hide
681 */
682 public static final String PRIVATE_DNS_MODE_OFF = "off";
683 /**
684 * @hide
685 */
686 public static final String PRIVATE_DNS_MODE_OPPORTUNISTIC = "opportunistic";
687 /**
688 * @hide
689 */
690 public static final String PRIVATE_DNS_MODE_PROVIDER_HOSTNAME = "hostname";
691 /**
692 * The default Private DNS mode.
693 *
694 * This may change from release to release or may become dependent upon
695 * the capabilities of the underlying platform.
696 *
697 * @hide
698 */
Erik Kline19841792018-05-16 16:41:57 +0900699 public static final String PRIVATE_DNS_DEFAULT_MODE_FALLBACK = PRIVATE_DNS_MODE_OPPORTUNISTIC;
Erik Kline4d092232017-10-30 15:29:44 +0900700
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700701 private final IConnectivityManager mService;
Paul Jensene0bef712014-12-10 15:12:18 -0500702 /**
703 * A kludge to facilitate static access where a Context pointer isn't available, like in the
704 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
705 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
706 * methods that take a Context argument.
707 */
708 private static ConnectivityManager sInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900710 private final Context mContext;
711
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800712 private INetworkManagementService mNMService;
Felipe Leme1b103232016-01-22 09:44:57 -0800713 private INetworkPolicyManager mNPManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800714
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800715 /**
716 * Tests if a given integer represents a valid network type.
717 * @param networkType the type to be tested
718 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensen9e59e122015-05-06 10:42:25 -0400719 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
720 * validate a network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800721 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700722 @Deprecated
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700723 public static boolean isNetworkTypeValid(int networkType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900724 return MIN_NETWORK_TYPE <= networkType && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 }
726
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800727 /**
728 * Returns a non-localized string representing a given network type.
729 * ONLY used for debugging output.
730 * @param type the type needing naming
731 * @return a String for the given type, or a string version of the type ("87")
732 * if no name is known.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900733 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800734 * {@hide}
735 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900736 @Deprecated
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700737 public static String getNetworkTypeName(int type) {
738 switch (type) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900739 case TYPE_NONE:
740 return "NONE";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700741 case TYPE_MOBILE:
742 return "MOBILE";
743 case TYPE_WIFI:
744 return "WIFI";
745 case TYPE_MOBILE_MMS:
746 return "MOBILE_MMS";
747 case TYPE_MOBILE_SUPL:
748 return "MOBILE_SUPL";
749 case TYPE_MOBILE_DUN:
750 return "MOBILE_DUN";
751 case TYPE_MOBILE_HIPRI:
752 return "MOBILE_HIPRI";
753 case TYPE_WIMAX:
754 return "WIMAX";
755 case TYPE_BLUETOOTH:
756 return "BLUETOOTH";
757 case TYPE_DUMMY:
758 return "DUMMY";
759 case TYPE_ETHERNET:
760 return "ETHERNET";
761 case TYPE_MOBILE_FOTA:
762 return "MOBILE_FOTA";
763 case TYPE_MOBILE_IMS:
764 return "MOBILE_IMS";
765 case TYPE_MOBILE_CBS:
766 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700767 case TYPE_WIFI_P2P:
768 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700769 case TYPE_MOBILE_IA:
770 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700771 case TYPE_MOBILE_EMERGENCY:
772 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500773 case TYPE_PROXY:
774 return "PROXY";
Erik Kline37fbfa12014-11-19 17:23:41 +0900775 case TYPE_VPN:
776 return "VPN";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700777 default:
778 return Integer.toString(type);
779 }
780 }
781
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800782 /**
783 * Checks if a given type uses the cellular data connection.
784 * This should be replaced in the future by a network property.
785 * @param networkType the type to check
786 * @return a boolean - {@code true} if uses cellular network, else {@code false}
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900787 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800788 * {@hide}
789 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900790 @Deprecated
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700791 public static boolean isNetworkTypeMobile(int networkType) {
792 switch (networkType) {
793 case TYPE_MOBILE:
794 case TYPE_MOBILE_MMS:
795 case TYPE_MOBILE_SUPL:
796 case TYPE_MOBILE_DUN:
797 case TYPE_MOBILE_HIPRI:
798 case TYPE_MOBILE_FOTA:
799 case TYPE_MOBILE_IMS:
800 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700801 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700802 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700803 return true;
804 default:
805 return false;
806 }
807 }
808
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800809 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700810 * Checks if the given network type is backed by a Wi-Fi radio.
811 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900812 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700813 * @hide
814 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900815 @Deprecated
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700816 public static boolean isNetworkTypeWifi(int networkType) {
817 switch (networkType) {
818 case TYPE_WIFI:
819 case TYPE_WIFI_P2P:
820 return true;
821 default:
822 return false;
823 }
824 }
825
826 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800827 * Specifies the preferred network type. When the device has more
828 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800829 *
830 * @param preference the network type to prefer over all others. It is
831 * unspecified what happens to the old preferred network in the
832 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700833 * @deprecated Functionality has been removed as it no longer makes sense,
834 * with many more than two networks - we'd need an array to express
835 * preference. Instead we use dynamic network properties of
836 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800837 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700838 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800840 }
841
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800842 /**
843 * Retrieves the current preferred network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800844 *
845 * @return an integer representing the preferred network type
846 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700847 * @deprecated Functionality has been removed as it no longer makes sense,
848 * with many more than two networks - we'd need an array to express
849 * preference. Instead we use dynamic network properties of
850 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800851 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700852 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600853 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700855 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 }
857
Scott Main671644c2011-10-06 19:02:28 -0700858 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800859 * Returns details about the currently active default data network. When
860 * connected, this network is the default route for outgoing connections.
861 * You should always check {@link NetworkInfo#isConnected()} before initiating
862 * network traffic. This may return {@code null} when there is no default
863 * network.
Chalard Jean5a041d12018-03-29 17:45:24 +0900864 * Note that if the default network is a VPN, this method will return the
865 * NetworkInfo for one of its underlying networks instead, or null if the
866 * VPN agent did not specify any. Apps interested in learning about VPNs
867 * should use {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800868 *
869 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500870 * or {@code null} if no default network is currently active
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700871 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600872 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800873 public NetworkInfo getActiveNetworkInfo() {
874 try {
875 return mService.getActiveNetworkInfo();
876 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700877 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800878 }
879 }
880
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800881 /**
Paul Jensen31a94f42015-02-13 14:18:39 -0500882 * Returns a {@link Network} object corresponding to the currently active
883 * default data network. In the event that the current active default data
884 * network disconnects, the returned {@code Network} object will no longer
885 * be usable. This will return {@code null} when there is no default
886 * network.
887 *
888 * @return a {@link Network} object for the current default network or
889 * {@code null} if no default network is currently active
Paul Jensen31a94f42015-02-13 14:18:39 -0500890 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600891 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Paul Jensen31a94f42015-02-13 14:18:39 -0500892 public Network getActiveNetwork() {
893 try {
894 return mService.getActiveNetwork();
895 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700896 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -0500897 }
898 }
899
900 /**
Robin Leed2baf792016-03-24 12:07:00 +0000901 * Returns a {@link Network} object corresponding to the currently active
902 * default data network for a specific UID. In the event that the default data
903 * network disconnects, the returned {@code Network} object will no longer
904 * be usable. This will return {@code null} when there is no default
905 * network for the UID.
Robin Leed2baf792016-03-24 12:07:00 +0000906 *
907 * @return a {@link Network} object for the current default network for the
908 * given UID or {@code null} if no default network is currently active
909 *
910 * @hide
911 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600912 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Robin Leed2baf792016-03-24 12:07:00 +0000913 public Network getActiveNetworkForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600914 return getActiveNetworkForUid(uid, false);
915 }
916
917 /** {@hide} */
918 public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) {
Robin Leed2baf792016-03-24 12:07:00 +0000919 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600920 return mService.getActiveNetworkForUid(uid, ignoreBlocked);
Robin Leed2baf792016-03-24 12:07:00 +0000921 } catch (RemoteException e) {
922 throw e.rethrowFromSystemServer();
923 }
924 }
925
926 /**
Charles He36738632017-05-15 17:07:18 +0100927 * Checks if a VPN app supports always-on mode.
928 *
929 * In order to support the always-on feature, an app has to
930 * <ul>
931 * <li>target {@link VERSION_CODES#N API 24} or above, and
Charles Hec57a01c2017-08-15 15:30:22 +0100932 * <li>not opt out through the {@link VpnService#SERVICE_META_DATA_SUPPORTS_ALWAYS_ON}
933 * meta-data field.
Charles He36738632017-05-15 17:07:18 +0100934 * </ul>
935 *
936 * @param userId The identifier of the user for whom the VPN app is installed.
937 * @param vpnPackage The canonical package name of the VPN app.
938 * @return {@code true} if and only if the VPN app exists and supports always-on mode.
939 * @hide
940 */
941 public boolean isAlwaysOnVpnPackageSupportedForUser(int userId, @Nullable String vpnPackage) {
942 try {
943 return mService.isAlwaysOnVpnPackageSupported(userId, vpnPackage);
944 } catch (RemoteException e) {
945 throw e.rethrowFromSystemServer();
946 }
947 }
948
949 /**
Robin Lee244ce8e2016-01-05 18:03:46 +0000950 * Configures an always-on VPN connection through a specific application.
951 * This connection is automatically granted and persisted after a reboot.
952 *
953 * <p>The designated package should declare a {@link VpnService} in its
954 * manifest guarded by {@link android.Manifest.permission.BIND_VPN_SERVICE},
955 * otherwise the call will fail.
956 *
957 * @param userId The identifier of the user to set an always-on VPN for.
958 * @param vpnPackage The package name for an installed VPN app on the device, or {@code null}
959 * to remove an existing always-on VPN configuration.
Robin Leedc679712016-05-03 13:23:03 +0100960 * @param lockdownEnabled {@code true} to disallow networking when the VPN is not connected or
961 * {@code false} otherwise.
Robin Lee244ce8e2016-01-05 18:03:46 +0000962 * @return {@code true} if the package is set as always-on VPN controller;
963 * {@code false} otherwise.
964 * @hide
965 */
Robin Leedc679712016-05-03 13:23:03 +0100966 public boolean setAlwaysOnVpnPackageForUser(int userId, @Nullable String vpnPackage,
967 boolean lockdownEnabled) {
Robin Lee244ce8e2016-01-05 18:03:46 +0000968 try {
Robin Leedc679712016-05-03 13:23:03 +0100969 return mService.setAlwaysOnVpnPackage(userId, vpnPackage, lockdownEnabled);
Robin Lee244ce8e2016-01-05 18:03:46 +0000970 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700971 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +0000972 }
973 }
974
975 /**
976 * Returns the package name of the currently set always-on VPN application.
977 * If there is no always-on VPN set, or the VPN is provided by the system instead
978 * of by an app, {@code null} will be returned.
979 *
980 * @return Package name of VPN controller responsible for always-on VPN,
981 * or {@code null} if none is set.
982 * @hide
983 */
984 public String getAlwaysOnVpnPackageForUser(int userId) {
985 try {
986 return mService.getAlwaysOnVpnPackage(userId);
987 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700988 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +0000989 }
990 }
991
992 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800993 * Returns details about the currently active default data network
994 * for a given uid. This is for internal use only to avoid spying
995 * other apps.
996 *
997 * @return a {@link NetworkInfo} object for the current default network
998 * for the given uid or {@code null} if no default network is
999 * available for the specified uid.
1000 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001001 * {@hide}
1002 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001003 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001004 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001005 return getActiveNetworkInfoForUid(uid, false);
1006 }
1007
1008 /** {@hide} */
1009 public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001010 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001011 return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001012 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001013 throw e.rethrowFromSystemServer();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001014 }
1015 }
1016
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001017 /**
1018 * Returns connection status information about a particular
1019 * network type.
1020 *
1021 * @param networkType integer specifying which networkType in
1022 * which you're interested.
1023 * @return a {@link NetworkInfo} object for the requested
1024 * network type or {@code null} if the type is not
Chalard Jean5a041d12018-03-29 17:45:24 +09001025 * supported by the device. If {@code networkType} is
1026 * TYPE_VPN and a VPN is active for the calling app,
1027 * then this method will try to return one of the
1028 * underlying networks for the VPN or null if the
1029 * VPN agent didn't specify any.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001030 *
Paul Jensen3541e9f2015-03-18 12:23:02 -04001031 * @deprecated This method does not support multiple connected networks
1032 * of the same type. Use {@link #getAllNetworks} and
1033 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001034 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001035 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001036 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001037 public NetworkInfo getNetworkInfo(int networkType) {
1038 try {
1039 return mService.getNetworkInfo(networkType);
1040 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001041 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001042 }
1043 }
1044
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001045 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001046 * Returns connection status information about a particular
1047 * Network.
1048 *
1049 * @param network {@link Network} specifying which network
1050 * in which you're interested.
1051 * @return a {@link NetworkInfo} object for the requested
1052 * network or {@code null} if the {@code Network}
1053 * is not valid.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001054 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001055 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001056 public NetworkInfo getNetworkInfo(Network network) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001057 return getNetworkInfoForUid(network, Process.myUid(), false);
1058 }
1059
1060 /** {@hide} */
1061 public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001062 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001063 return mService.getNetworkInfoForUid(network, uid, ignoreBlocked);
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001064 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001065 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001066 }
1067 }
1068
1069 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001070 * Returns connection status information about all network
1071 * types supported by the device.
1072 *
1073 * @return an array of {@link NetworkInfo} objects. Check each
1074 * {@link NetworkInfo#getType} for which type each applies.
1075 *
Paul Jensen3541e9f2015-03-18 12:23:02 -04001076 * @deprecated This method does not support multiple connected networks
1077 * of the same type. Use {@link #getAllNetworks} and
1078 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001079 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001080 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001081 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001082 public NetworkInfo[] getAllNetworkInfo() {
1083 try {
1084 return mService.getAllNetworkInfo();
1085 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001086 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001087 }
1088 }
1089
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001090 /**
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001091 * Returns the {@link Network} object currently serving a given type, or
1092 * null if the given type is not connected.
1093 *
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001094 * @hide
Paul Jensen3541e9f2015-03-18 12:23:02 -04001095 * @deprecated This method does not support multiple connected networks
1096 * of the same type. Use {@link #getAllNetworks} and
1097 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001098 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001099 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001100 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001101 public Network getNetworkForType(int networkType) {
1102 try {
1103 return mService.getNetworkForType(networkType);
1104 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001105 throw e.rethrowFromSystemServer();
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001106 }
1107 }
1108
1109 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001110 * Returns an array of all {@link Network} currently tracked by the
1111 * framework.
Paul Jensenb2748922015-05-06 11:10:18 -04001112 *
1113 * @return an array of {@link Network} objects.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001114 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001115 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001116 public Network[] getAllNetworks() {
1117 try {
1118 return mService.getAllNetworks();
1119 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001120 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001121 }
1122 }
1123
1124 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001125 * Returns an array of {@link android.net.NetworkCapabilities} objects, representing
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001126 * the Networks that applications run by the given user will use by default.
1127 * @hide
1128 */
1129 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
1130 try {
1131 return mService.getDefaultNetworkCapabilitiesForUser(userId);
1132 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001133 throw e.rethrowFromSystemServer();
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001134 }
1135 }
1136
1137 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001138 * Returns the IP information for the current default network.
1139 *
1140 * @return a {@link LinkProperties} object describing the IP info
1141 * for the current default network, or {@code null} if there
1142 * is no current default network.
1143 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001144 * {@hide}
1145 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001146 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001147 public LinkProperties getActiveLinkProperties() {
1148 try {
1149 return mService.getActiveLinkProperties();
1150 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001151 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001152 }
1153 }
1154
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001155 /**
1156 * Returns the IP information for a given network type.
1157 *
1158 * @param networkType the network type of interest.
1159 * @return a {@link LinkProperties} object describing the IP info
1160 * for the given networkType, or {@code null} if there is
1161 * no current default network.
1162 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001163 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04001164 * @deprecated This method does not support multiple connected networks
1165 * of the same type. Use {@link #getAllNetworks},
1166 * {@link #getNetworkInfo(android.net.Network)}, and
1167 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001168 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001169 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001170 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001171 public LinkProperties getLinkProperties(int networkType) {
1172 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001173 return mService.getLinkPropertiesForType(networkType);
1174 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001175 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001176 }
1177 }
1178
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001179 /**
1180 * Get the {@link LinkProperties} for the given {@link Network}. This
1181 * will return {@code null} if the network is unknown.
1182 *
1183 * @param network The {@link Network} object identifying the network in question.
1184 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensenb2748922015-05-06 11:10:18 -04001185 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001186 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt9258c642014-03-26 16:47:06 -07001187 public LinkProperties getLinkProperties(Network network) {
1188 try {
1189 return mService.getLinkProperties(network);
1190 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001191 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001192 }
1193 }
1194
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001195 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001196 * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001197 * will return {@code null} if the network is unknown.
1198 *
1199 * @param network The {@link Network} object identifying the network in question.
Lorenzo Colittie285b432015-04-23 15:32:42 +09001200 * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001201 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001202 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt9258c642014-03-26 16:47:06 -07001203 public NetworkCapabilities getNetworkCapabilities(Network network) {
1204 try {
1205 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001206 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001207 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001208 }
1209 }
1210
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001211 /**
Udam Sainib7c24872016-01-04 12:16:14 -08001212 * Gets the URL that should be used for resolving whether a captive portal is present.
1213 * 1. This URL should respond with a 204 response to a GET request to indicate no captive
1214 * portal is present.
1215 * 2. This URL must be HTTP as redirect responses are used to find captive portal
1216 * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects.
1217 *
1218 * @hide
1219 */
1220 @SystemApi
Udam Saini0e94c362017-06-07 12:06:28 -07001221 @RequiresPermission(android.Manifest.permission.LOCAL_MAC_ADDRESS)
Udam Sainib7c24872016-01-04 12:16:14 -08001222 public String getCaptivePortalServerUrl() {
1223 try {
1224 return mService.getCaptivePortalServerUrl();
1225 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001226 throw e.rethrowFromSystemServer();
Udam Sainib7c24872016-01-04 12:16:14 -08001227 }
1228 }
1229
1230 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001231 * Tells the underlying networking system that the caller wants to
1232 * begin using the named feature. The interpretation of {@code feature}
1233 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001234 *
1235 * <p>This method requires the caller to hold either the
1236 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1237 * or the ability to modify system settings as determined by
1238 * {@link android.provider.Settings.System#canWrite}.</p>
1239 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 * @param networkType specifies which network the request pertains to
1241 * @param feature the name of the feature to be used
1242 * @return an integer value representing the outcome of the request.
1243 * The interpretation of this value is specific to each networking
1244 * implementation+feature combination, except that the value {@code -1}
1245 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001246 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001247 * @deprecated Deprecated in favor of the cleaner
1248 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001249 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001250 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001251 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001252 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001253 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001255 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001256 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1257 if (netCap == null) {
1258 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
1259 feature);
1260 return PhoneConstants.APN_REQUEST_FAILED;
1261 }
1262
1263 NetworkRequest request = null;
1264 synchronized (sLegacyRequests) {
1265 LegacyRequest l = sLegacyRequests.get(netCap);
1266 if (l != null) {
1267 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
1268 renewRequestLocked(l);
1269 if (l.currentNetwork != null) {
1270 return PhoneConstants.APN_ALREADY_ACTIVE;
1271 } else {
1272 return PhoneConstants.APN_REQUEST_STARTED;
1273 }
1274 }
1275
1276 request = requestNetworkForFeatureLocked(netCap);
1277 }
1278 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -07001279 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001280 return PhoneConstants.APN_REQUEST_STARTED;
1281 } else {
1282 Log.d(TAG, " request Failed");
1283 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 }
1285 }
1286
1287 /**
1288 * Tells the underlying networking system that the caller is finished
1289 * using the named feature. The interpretation of {@code feature}
1290 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001291 *
1292 * <p>This method requires the caller to hold either the
1293 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1294 * or the ability to modify system settings as determined by
1295 * {@link android.provider.Settings.System#canWrite}.</p>
1296 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 * @param networkType specifies which network the request pertains to
1298 * @param feature the name of the feature that is no longer needed
1299 * @return an integer value representing the outcome of the request.
1300 * The interpretation of this value is specific to each networking
1301 * implementation+feature combination, except that the value {@code -1}
1302 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001303 *
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09001304 * @deprecated Deprecated in favor of the cleaner
1305 * {@link #unregisterNetworkCallback(NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001306 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001307 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001308 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001309 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001310 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001312 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001313 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1314 if (netCap == null) {
1315 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
1316 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 return -1;
1318 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001319
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001320 if (removeRequestForFeature(netCap)) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001321 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001322 }
1323 return 1;
1324 }
1325
1326 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1327 if (networkType == TYPE_MOBILE) {
Erik Kline35bf06c2017-01-26 18:08:28 +09001328 switch (feature) {
1329 case "enableCBS":
1330 return networkCapabilitiesForType(TYPE_MOBILE_CBS);
1331 case "enableDUN":
1332 case "enableDUNAlways":
1333 return networkCapabilitiesForType(TYPE_MOBILE_DUN);
1334 case "enableFOTA":
1335 return networkCapabilitiesForType(TYPE_MOBILE_FOTA);
1336 case "enableHIPRI":
1337 return networkCapabilitiesForType(TYPE_MOBILE_HIPRI);
1338 case "enableIMS":
1339 return networkCapabilitiesForType(TYPE_MOBILE_IMS);
1340 case "enableMMS":
1341 return networkCapabilitiesForType(TYPE_MOBILE_MMS);
1342 case "enableSUPL":
1343 return networkCapabilitiesForType(TYPE_MOBILE_SUPL);
1344 default:
1345 return null;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001346 }
Erik Kline35bf06c2017-01-26 18:08:28 +09001347 } else if (networkType == TYPE_WIFI && "p2p".equals(feature)) {
1348 return networkCapabilitiesForType(TYPE_WIFI_P2P);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001349 }
1350 return null;
1351 }
1352
Robert Greenwalt06314e42014-10-29 14:04:06 -07001353 /**
1354 * Guess what the network request was trying to say so that the resulting
1355 * network is accessible via the legacy (deprecated) API such as
1356 * requestRouteToHost.
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001357 *
1358 * This means we should try to be fairly precise about transport and
Robert Greenwalt06314e42014-10-29 14:04:06 -07001359 * capability but ignore things such as networkSpecifier.
1360 * If the request has more than one transport or capability it doesn't
1361 * match the old legacy requests (they selected only single transport/capability)
1362 * so this function cannot map the request to a single legacy type and
1363 * the resulting network will not be available to the legacy APIs.
1364 *
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001365 * This code is only called from the requestNetwork API (L and above).
1366 *
1367 * Setting a legacy type causes CONNECTIVITY_ACTION broadcasts, which are expensive
1368 * because they wake up lots of apps - see http://b/23350688 . So we currently only
1369 * do this for SUPL requests, which are the only ones that we know need it. If
1370 * omitting these broadcasts causes unacceptable app breakage, then for backwards
1371 * compatibility we can send them:
1372 *
1373 * if (targetSdkVersion < Build.VERSION_CODES.M) && // legacy API unsupported >= M
1374 * targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP)) // requestNetwork not present < L
1375 *
Robert Greenwalt06314e42014-10-29 14:04:06 -07001376 * TODO - This should be removed when the legacy APIs are removed.
1377 */
Ye Wenb87875e2014-07-21 14:19:01 -07001378 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1379 if (netCap == null) {
1380 return TYPE_NONE;
1381 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001382
Ye Wenb87875e2014-07-21 14:19:01 -07001383 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1384 return TYPE_NONE;
1385 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001386
Lifu Tang30f95a72016-01-07 23:20:38 -08001387 // Do this only for SUPL, until GnssLocationProvider is fixed. http://b/25876485 .
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001388 if (!netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1389 // NOTE: if this causes app breakage, we should not just comment out this early return;
1390 // instead, we should make this early return conditional on the requesting app's target
1391 // SDK version, as described in the comment above.
1392 return TYPE_NONE;
1393 }
1394
Robert Greenwalt06314e42014-10-29 14:04:06 -07001395 String type = null;
1396 int result = TYPE_NONE;
1397
Ye Wenb87875e2014-07-21 14:19:01 -07001398 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
Robert Greenwalt06314e42014-10-29 14:04:06 -07001399 type = "enableCBS";
1400 result = TYPE_MOBILE_CBS;
1401 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1402 type = "enableIMS";
1403 result = TYPE_MOBILE_IMS;
1404 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1405 type = "enableFOTA";
1406 result = TYPE_MOBILE_FOTA;
1407 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1408 type = "enableDUN";
1409 result = TYPE_MOBILE_DUN;
1410 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001411 type = "enableSUPL";
Robert Greenwalt06314e42014-10-29 14:04:06 -07001412 result = TYPE_MOBILE_SUPL;
Robert Greenwalt74ab4fa2015-08-28 12:37:54 -07001413 // back out this hack for mms as they no longer need this and it's causing
1414 // device slowdowns - b/23350688 (note, supl still needs this)
1415 //} else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1416 // type = "enableMMS";
1417 // result = TYPE_MOBILE_MMS;
Robert Greenwalt06314e42014-10-29 14:04:06 -07001418 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1419 type = "enableHIPRI";
1420 result = TYPE_MOBILE_HIPRI;
Ye Wenb87875e2014-07-21 14:19:01 -07001421 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001422 if (type != null) {
1423 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1424 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1425 return result;
Ye Wenb87875e2014-07-21 14:19:01 -07001426 }
1427 }
1428 return TYPE_NONE;
1429 }
1430
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001431 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001432 if (netCap == null) return TYPE_NONE;
1433 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1434 return TYPE_MOBILE_CBS;
1435 }
1436 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1437 return TYPE_MOBILE_IMS;
1438 }
1439 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1440 return TYPE_MOBILE_FOTA;
1441 }
1442 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1443 return TYPE_MOBILE_DUN;
1444 }
1445 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1446 return TYPE_MOBILE_SUPL;
1447 }
1448 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1449 return TYPE_MOBILE_MMS;
1450 }
1451 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1452 return TYPE_MOBILE_HIPRI;
1453 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001454 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1455 return TYPE_WIFI_P2P;
1456 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001457 return TYPE_NONE;
1458 }
1459
1460 private static class LegacyRequest {
1461 NetworkCapabilities networkCapabilities;
1462 NetworkRequest networkRequest;
1463 int expireSequenceNumber;
1464 Network currentNetwork;
1465 int delay = -1;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001466
1467 private void clearDnsBinding() {
1468 if (currentNetwork != null) {
1469 currentNetwork = null;
1470 setProcessDefaultNetworkForHostResolution(null);
1471 }
1472 }
1473
Robert Greenwalt6078b502014-06-11 16:05:07 -07001474 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001475 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001476 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001477 currentNetwork = network;
1478 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001479 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001480 }
1481 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001482 public void onLost(Network network) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001483 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001484 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1485 }
1486 };
1487 }
1488
Robert Greenwaltfab501672014-07-23 11:44:01 -07001489 private static HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
Robert Greenwalt562cc542014-05-15 18:07:26 -07001490 new HashMap<NetworkCapabilities, LegacyRequest>();
1491
1492 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1493 synchronized (sLegacyRequests) {
1494 LegacyRequest l = sLegacyRequests.get(netCap);
1495 if (l != null) return l.networkRequest;
1496 }
1497 return null;
1498 }
1499
1500 private void renewRequestLocked(LegacyRequest l) {
1501 l.expireSequenceNumber++;
1502 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1503 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1504 }
1505
1506 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1507 int ourSeqNum = -1;
1508 synchronized (sLegacyRequests) {
1509 LegacyRequest l = sLegacyRequests.get(netCap);
1510 if (l == null) return;
1511 ourSeqNum = l.expireSequenceNumber;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001512 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001513 }
1514 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1515 }
1516
1517 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1518 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001519 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001520 try {
1521 delay = mService.getRestoreDefaultNetworkDelay(type);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001522 } catch (RemoteException e) {
1523 throw e.rethrowFromSystemServer();
1524 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001525 LegacyRequest l = new LegacyRequest();
1526 l.networkCapabilities = netCap;
1527 l.delay = delay;
1528 l.expireSequenceNumber = 0;
Hugo Benichi2583ef02017-02-02 17:02:36 +09001529 l.networkRequest = sendRequestForNetwork(
1530 netCap, l.networkCallback, 0, REQUEST, type, getDefaultHandler());
Robert Greenwalt562cc542014-05-15 18:07:26 -07001531 if (l.networkRequest == null) return null;
1532 sLegacyRequests.put(netCap, l);
1533 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1534 return l.networkRequest;
1535 }
1536
1537 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1538 if (delay >= 0) {
1539 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
Hugo Benichi2583ef02017-02-02 17:02:36 +09001540 CallbackHandler handler = getDefaultHandler();
Hugo Benichi6f260f32017-02-03 14:18:44 +09001541 Message msg = handler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1542 handler.sendMessageDelayed(msg, delay);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001543 }
1544 }
1545
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001546 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1547 final LegacyRequest l;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001548 synchronized (sLegacyRequests) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001549 l = sLegacyRequests.remove(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001550 }
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001551 if (l == null) return false;
1552 unregisterNetworkCallback(l.networkCallback);
1553 l.clearDnsBinding();
1554 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 }
1556
Erik Kline35bf06c2017-01-26 18:08:28 +09001557 private static final SparseIntArray sLegacyTypeToTransport = new SparseIntArray();
1558 static {
1559 sLegacyTypeToTransport.put(TYPE_MOBILE, NetworkCapabilities.TRANSPORT_CELLULAR);
1560 sLegacyTypeToTransport.put(TYPE_MOBILE_CBS, NetworkCapabilities.TRANSPORT_CELLULAR);
1561 sLegacyTypeToTransport.put(TYPE_MOBILE_DUN, NetworkCapabilities.TRANSPORT_CELLULAR);
1562 sLegacyTypeToTransport.put(TYPE_MOBILE_FOTA, NetworkCapabilities.TRANSPORT_CELLULAR);
1563 sLegacyTypeToTransport.put(TYPE_MOBILE_HIPRI, NetworkCapabilities.TRANSPORT_CELLULAR);
1564 sLegacyTypeToTransport.put(TYPE_MOBILE_IMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1565 sLegacyTypeToTransport.put(TYPE_MOBILE_MMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1566 sLegacyTypeToTransport.put(TYPE_MOBILE_SUPL, NetworkCapabilities.TRANSPORT_CELLULAR);
1567 sLegacyTypeToTransport.put(TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI);
1568 sLegacyTypeToTransport.put(TYPE_WIFI_P2P, NetworkCapabilities.TRANSPORT_WIFI);
1569 sLegacyTypeToTransport.put(TYPE_BLUETOOTH, NetworkCapabilities.TRANSPORT_BLUETOOTH);
1570 sLegacyTypeToTransport.put(TYPE_ETHERNET, NetworkCapabilities.TRANSPORT_ETHERNET);
1571 }
1572
1573 private static final SparseIntArray sLegacyTypeToCapability = new SparseIntArray();
1574 static {
1575 sLegacyTypeToCapability.put(TYPE_MOBILE_CBS, NetworkCapabilities.NET_CAPABILITY_CBS);
1576 sLegacyTypeToCapability.put(TYPE_MOBILE_DUN, NetworkCapabilities.NET_CAPABILITY_DUN);
1577 sLegacyTypeToCapability.put(TYPE_MOBILE_FOTA, NetworkCapabilities.NET_CAPABILITY_FOTA);
1578 sLegacyTypeToCapability.put(TYPE_MOBILE_IMS, NetworkCapabilities.NET_CAPABILITY_IMS);
1579 sLegacyTypeToCapability.put(TYPE_MOBILE_MMS, NetworkCapabilities.NET_CAPABILITY_MMS);
1580 sLegacyTypeToCapability.put(TYPE_MOBILE_SUPL, NetworkCapabilities.NET_CAPABILITY_SUPL);
1581 sLegacyTypeToCapability.put(TYPE_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
1582 }
1583
1584 /**
1585 * Given a legacy type (TYPE_WIFI, ...) returns a NetworkCapabilities
1586 * instance suitable for registering a request or callback. Throws an
1587 * IllegalArgumentException if no mapping from the legacy type to
1588 * NetworkCapabilities is known.
1589 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +09001590 * @deprecated Types are deprecated. Use {@link NetworkCallback} or {@link NetworkRequest}
1591 * to find the network instead.
Erik Kline35bf06c2017-01-26 18:08:28 +09001592 * @hide
1593 */
1594 public static NetworkCapabilities networkCapabilitiesForType(int type) {
1595 final NetworkCapabilities nc = new NetworkCapabilities();
1596
1597 // Map from type to transports.
1598 final int NOT_FOUND = -1;
1599 final int transport = sLegacyTypeToTransport.get(type, NOT_FOUND);
Hugo Benichie7678512017-05-09 15:19:01 +09001600 Preconditions.checkArgument(transport != NOT_FOUND, "unknown legacy type: " + type);
Erik Kline35bf06c2017-01-26 18:08:28 +09001601 nc.addTransportType(transport);
1602
1603 // Map from type to capabilities.
1604 nc.addCapability(sLegacyTypeToCapability.get(
1605 type, NetworkCapabilities.NET_CAPABILITY_INTERNET));
1606 nc.maybeMarkCapabilitiesRestricted();
1607 return nc;
1608 }
1609
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001610 /** @hide */
1611 public static class PacketKeepaliveCallback {
1612 /** The requested keepalive was successfully started. */
1613 public void onStarted() {}
1614 /** The keepalive was successfully stopped. */
1615 public void onStopped() {}
1616 /** An error occurred. */
1617 public void onError(int error) {}
1618 }
1619
1620 /**
1621 * Allows applications to request that the system periodically send specific packets on their
1622 * behalf, using hardware offload to save battery power.
1623 *
1624 * To request that the system send keepalives, call one of the methods that return a
1625 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1626 * passing in a non-null callback. If the callback is successfully started, the callback's
1627 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1628 * specifying one of the {@code ERROR_*} constants in this class.
1629 *
1630 * To stop an existing keepalive, call {@link stop}. The system will call {@code onStopped} if
1631 * the operation was successfull or {@code onError} if an error occurred.
1632 *
1633 * @hide
1634 */
1635 public class PacketKeepalive {
1636
1637 private static final String TAG = "PacketKeepalive";
1638
1639 /** @hide */
1640 public static final int SUCCESS = 0;
1641
1642 /** @hide */
1643 public static final int NO_KEEPALIVE = -1;
1644
1645 /** @hide */
1646 public static final int BINDER_DIED = -10;
1647
1648 /** The specified {@code Network} is not connected. */
1649 public static final int ERROR_INVALID_NETWORK = -20;
1650 /** The specified IP addresses are invalid. For example, the specified source IP address is
1651 * not configured on the specified {@code Network}. */
1652 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1653 /** The requested port is invalid. */
1654 public static final int ERROR_INVALID_PORT = -22;
1655 /** The packet length is invalid (e.g., too long). */
1656 public static final int ERROR_INVALID_LENGTH = -23;
1657 /** The packet transmission interval is invalid (e.g., too short). */
1658 public static final int ERROR_INVALID_INTERVAL = -24;
1659
1660 /** The hardware does not support this request. */
1661 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti9d1284e2015-09-08 16:46:36 +09001662 /** The hardware returned an error. */
1663 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001664
Nathan Harold63dd8132018-02-14 13:09:45 -08001665 /** The NAT-T destination port for IPsec */
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001666 public static final int NATT_PORT = 4500;
1667
Nathan Harold63dd8132018-02-14 13:09:45 -08001668 /** The minimum interval in seconds between keepalive packet transmissions */
1669 public static final int MIN_INTERVAL = 10;
1670
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001671 private final Network mNetwork;
1672 private final PacketKeepaliveCallback mCallback;
1673 private final Looper mLooper;
1674 private final Messenger mMessenger;
1675
1676 private volatile Integer mSlot;
1677
1678 void stopLooper() {
1679 mLooper.quit();
1680 }
1681
1682 public void stop() {
1683 try {
1684 mService.stopKeepalive(mNetwork, mSlot);
1685 } catch (RemoteException e) {
1686 Log.e(TAG, "Error stopping packet keepalive: ", e);
1687 stopLooper();
1688 }
1689 }
1690
1691 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09001692 Preconditions.checkNotNull(network, "network cannot be null");
1693 Preconditions.checkNotNull(callback, "callback cannot be null");
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001694 mNetwork = network;
1695 mCallback = callback;
1696 HandlerThread thread = new HandlerThread(TAG);
1697 thread.start();
1698 mLooper = thread.getLooper();
1699 mMessenger = new Messenger(new Handler(mLooper) {
1700 @Override
1701 public void handleMessage(Message message) {
1702 switch (message.what) {
1703 case NetworkAgent.EVENT_PACKET_KEEPALIVE:
1704 int error = message.arg2;
1705 try {
1706 if (error == SUCCESS) {
1707 if (mSlot == null) {
1708 mSlot = message.arg1;
1709 mCallback.onStarted();
1710 } else {
1711 mSlot = null;
1712 stopLooper();
1713 mCallback.onStopped();
1714 }
1715 } else {
1716 stopLooper();
1717 mCallback.onError(error);
1718 }
1719 } catch (Exception e) {
1720 Log.e(TAG, "Exception in keepalive callback(" + error + ")", e);
1721 }
1722 break;
1723 default:
1724 Log.e(TAG, "Unhandled message " + Integer.toHexString(message.what));
1725 break;
1726 }
1727 }
1728 });
1729 }
1730 }
1731
1732 /**
1733 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
1734 *
1735 * @hide
1736 */
1737 public PacketKeepalive startNattKeepalive(
1738 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
1739 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
1740 final PacketKeepalive k = new PacketKeepalive(network, callback);
1741 try {
1742 mService.startNattKeepalive(network, intervalSeconds, k.mMessenger, new Binder(),
1743 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
1744 } catch (RemoteException e) {
1745 Log.e(TAG, "Error starting packet keepalive: ", e);
1746 k.stopLooper();
1747 return null;
1748 }
1749 return k;
1750 }
1751
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001752 /**
1753 * Ensure that a network route exists to deliver traffic to the specified
1754 * host via the specified network interface. An attempt to add a route that
1755 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001756 *
1757 * <p>This method requires the caller to hold either the
1758 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1759 * or the ability to modify system settings as determined by
1760 * {@link android.provider.Settings.System#canWrite}.</p>
1761 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 * @param networkType the type of the network over which traffic to the specified
1763 * host is to be routed
1764 * @param hostAddress the IP address of the host to which the route is desired
1765 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001766 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001767 * @deprecated Deprecated in favor of the
1768 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
1769 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001770 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001771 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001772 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001773 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001774 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001775 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001776 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001777 }
1778
1779 /**
1780 * Ensure that a network route exists to deliver traffic to the specified
1781 * host via the specified network interface. An attempt to add a route that
1782 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001783 *
1784 * <p>This method requires the caller to hold either the
1785 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1786 * or the ability to modify system settings as determined by
1787 * {@link android.provider.Settings.System#canWrite}.</p>
1788 *
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001789 * @param networkType the type of the network over which traffic to the specified
1790 * host is to be routed
1791 * @param hostAddress the IP address of the host to which the route is desired
1792 * @return {@code true} on success, {@code false} on failure
1793 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001794 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001795 * {@link #bindProcessToNetwork} API.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001796 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001797 @Deprecated
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001798 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001799 checkLegacyRoutingApiAccess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001800 try {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001801 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001802 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001803 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001804 }
1805 }
1806
1807 /**
1808 * Returns the value of the setting for background data usage. If false,
1809 * applications should not use the network if the application is not in the
1810 * foreground. Developers should respect this setting, and check the value
1811 * of this before performing any background data operations.
1812 * <p>
1813 * All applications that have background services that use the network
1814 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001815 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001816 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001817 * background data depends on several combined factors, and this method will
1818 * always return {@code true}. Instead, when background data is unavailable,
1819 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001820 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 * @return Whether background data usage is allowed.
1822 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001823 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001824 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001825 // assume that background data is allowed; final authority is
1826 // NetworkInfo which may be blocked.
1827 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001828 }
1829
1830 /**
1831 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001832 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001833 * @param allowBackgroundData Whether an application should use data while
1834 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001835 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001836 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1837 * @see #getBackgroundDataSetting()
1838 * @hide
1839 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001840 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001841 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001842 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001844
Jeff Sharkey43d2a172017-07-12 10:50:42 -06001845 /** {@hide} */
1846 @Deprecated
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001847 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1848 try {
1849 return mService.getActiveNetworkQuotaInfo();
1850 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001851 throw e.rethrowFromSystemServer();
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001852 }
1853 }
1854
1855 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001856 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001857 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001858 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001859 @Deprecated
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001860 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001861 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1862 if (b != null) {
1863 try {
1864 ITelephony it = ITelephony.Stub.asInterface(b);
Shishir Agrawal7ea3e8b2016-01-25 13:03:07 -08001865 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Wink Saville36ffb042014-12-05 11:10:30 -08001866 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
Malcolm Chenb455e722017-11-28 15:57:14 -08001867 boolean retVal = it.isUserDataEnabled(subId);
Wink Saville36ffb042014-12-05 11:10:30 -08001868 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1869 + " retVal=" + retVal);
1870 return retVal;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001871 } catch (RemoteException e) {
1872 throw e.rethrowFromSystemServer();
1873 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001874 }
Wink Saville36ffb042014-12-05 11:10:30 -08001875 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001876 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001877 }
1878
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001879 /**
Robert Greenwaltb2489872014-09-04 16:44:35 -07001880 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt6078b502014-06-11 16:05:07 -07001881 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001882 */
1883 public interface OnNetworkActiveListener {
1884 /**
1885 * Called on the main thread of the process to report that the current data network
1886 * has become active, and it is now a good time to perform any pending network
1887 * operations. Note that this listener only tells you when the network becomes
1888 * active; if at any other time you want to know whether it is active (and thus okay
1889 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001890 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001891 */
1892 public void onNetworkActive();
1893 }
1894
1895 private INetworkManagementService getNetworkManagementService() {
1896 synchronized (this) {
1897 if (mNMService != null) {
1898 return mNMService;
1899 }
1900 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1901 mNMService = INetworkManagementService.Stub.asInterface(b);
1902 return mNMService;
1903 }
1904 }
1905
1906 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
1907 mNetworkActivityListeners
1908 = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>();
1909
1910 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001911 * Start listening to reports when the system's default data network is active, meaning it is
1912 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1913 * to determine the current state of the system's default network after registering the
1914 * listener.
1915 * <p>
1916 * If the process default network has been set with
Paul Jensen72db88e2015-03-10 10:54:12 -04001917 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt6078b502014-06-11 16:05:07 -07001918 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001919 *
1920 * @param l The listener to be told when the network is active.
1921 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001922 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001923 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1924 @Override
1925 public void onNetworkActive() throws RemoteException {
1926 l.onNetworkActive();
1927 }
1928 };
1929
1930 try {
1931 getNetworkManagementService().registerNetworkActivityListener(rl);
1932 mNetworkActivityListeners.put(l, rl);
1933 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001934 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001935 }
1936 }
1937
1938 /**
1939 * Remove network active listener previously registered with
Robert Greenwaltb2489872014-09-04 16:44:35 -07001940 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001941 *
1942 * @param l Previously registered listener.
1943 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001944 public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001945 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
Hugo Benichie7678512017-05-09 15:19:01 +09001946 Preconditions.checkArgument(rl != null, "Listener was not registered.");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001947 try {
1948 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1949 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001950 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001951 }
1952 }
1953
1954 /**
1955 * Return whether the data network is currently active. An active network means that
1956 * it is currently in a high power state for performing data transmission. On some
1957 * types of networks, it may be expensive to move and stay in such a state, so it is
1958 * more power efficient to batch network traffic together when the radio is already in
1959 * this state. This method tells you whether right now is currently a good time to
1960 * initiate network traffic, as the network is already active.
1961 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001962 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001963 try {
1964 return getNetworkManagementService().isNetworkActive();
1965 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001966 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001967 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001968 }
1969
1970 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001971 * {@hide}
1972 */
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001973 public ConnectivityManager(Context context, IConnectivityManager service) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09001974 mContext = Preconditions.checkNotNull(context, "missing context");
1975 mService = Preconditions.checkNotNull(service, "missing IConnectivityManager");
Paul Jensene0bef712014-12-10 15:12:18 -05001976 sInstance = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001977 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001978
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07001979 /** {@hide} */
1980 public static ConnectivityManager from(Context context) {
1981 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1982 }
1983
Lorenzo Colittifbe9b1a2016-07-28 17:14:11 +09001984 /* TODO: These permissions checks don't belong in client-side code. Move them to
1985 * services.jar, possibly in com.android.server.net. */
1986
1987 /** {@hide} */
Lorenzo Colittid5427052015-10-15 16:29:00 +09001988 public static final void enforceChangePermission(Context context) {
1989 int uid = Binder.getCallingUid();
1990 Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
1991 .getPackageNameForUid(context, uid), true /* throwException */);
1992 }
1993
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09001994 /** {@hide} */
1995 public static final void enforceTetherChangePermission(Context context, String callingPkg) {
Hugo Benichie7678512017-05-09 15:19:01 +09001996 Preconditions.checkNotNull(context, "Context cannot be null");
1997 Preconditions.checkNotNull(callingPkg, "callingPkg cannot be null");
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09001998
Robert Greenwaltedb47662014-09-16 17:54:19 -07001999 if (context.getResources().getStringArray(
2000 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
2001 // Have a provisioning app - must only let system apps (which check this app)
2002 // turn on tethering
2003 context.enforceCallingOrSelfPermission(
Jeremy Kleind42209d2015-12-28 15:11:58 -08002004 android.Manifest.permission.TETHER_PRIVILEGED, "ConnectivityService");
Robert Greenwaltedb47662014-09-16 17:54:19 -07002005 } else {
Billy Laua7238a32015-08-01 12:45:02 +01002006 int uid = Binder.getCallingUid();
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002007 // If callingPkg's uid is not same as Binder.getCallingUid(),
2008 // AppOpsService throws SecurityException.
2009 Settings.checkAndNoteWriteSettingsOperation(context, uid, callingPkg,
2010 true /* throwException */);
Robert Greenwaltedb47662014-09-16 17:54:19 -07002011 }
2012 }
2013
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002014 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002015 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2016 * situations where a Context pointer is unavailable.
2017 * @hide
2018 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002019 @Deprecated
Paul Jensen72db88e2015-03-10 10:54:12 -04002020 static ConnectivityManager getInstanceOrNull() {
2021 return sInstance;
2022 }
2023
2024 /**
2025 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2026 * situations where a Context pointer is unavailable.
2027 * @hide
2028 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002029 @Deprecated
Paul Jensen72db88e2015-03-10 10:54:12 -04002030 private static ConnectivityManager getInstance() {
2031 if (getInstanceOrNull() == null) {
Paul Jensene0bef712014-12-10 15:12:18 -05002032 throw new IllegalStateException("No ConnectivityManager yet constructed");
2033 }
Paul Jensen72db88e2015-03-10 10:54:12 -04002034 return getInstanceOrNull();
Paul Jensene0bef712014-12-10 15:12:18 -05002035 }
2036
2037 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002038 * Get the set of tetherable, available interfaces. This list is limited by
2039 * device configuration and current interface existence.
2040 *
2041 * @return an array of 0 or more Strings of tetherable interface names.
2042 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002043 * {@hide}
2044 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002045 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002046 public String[] getTetherableIfaces() {
2047 try {
2048 return mService.getTetherableIfaces();
2049 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002050 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002051 }
2052 }
2053
2054 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002055 * Get the set of tethered interfaces.
2056 *
2057 * @return an array of 0 or more String of currently tethered interface names.
2058 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002059 * {@hide}
2060 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002061 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002062 public String[] getTetheredIfaces() {
2063 try {
2064 return mService.getTetheredIfaces();
2065 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002066 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002067 }
2068 }
2069
2070 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002071 * Get the set of interface names which attempted to tether but
2072 * failed. Re-attempting to tether may cause them to reset to the Tethered
2073 * state. Alternatively, causing the interface to be destroyed and recreated
2074 * may cause them to reset to the available state.
2075 * {@link ConnectivityManager#getLastTetherError} can be used to get more
2076 * information on the cause of the errors.
2077 *
2078 * @return an array of 0 or more String indicating the interface names
2079 * which failed to tether.
2080 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002081 * {@hide}
2082 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002083 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt5a735062010-03-02 17:25:02 -08002084 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002085 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08002086 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002087 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002088 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002089 }
2090 }
2091
2092 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07002093 * Get the set of tethered dhcp ranges.
2094 *
2095 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
2096 * {@hide}
2097 */
2098 public String[] getTetheredDhcpRanges() {
2099 try {
2100 return mService.getTetheredDhcpRanges();
2101 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002102 throw e.rethrowFromSystemServer();
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07002103 }
2104 }
2105
2106 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002107 * Attempt to tether the named interface. This will setup a dhcp server
2108 * on the interface, forward and NAT IP packets and forward DNS requests
2109 * to the best active upstream network interface. Note that if no upstream
2110 * IP network interface is available, dhcp will still run and traffic will be
2111 * allowed between the tethered devices and this device, though upstream net
2112 * access will of course fail until an upstream network interface becomes
2113 * active.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002114 *
2115 * <p>This method requires the caller to hold either the
2116 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2117 * or the ability to modify system settings as determined by
2118 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002119 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002120 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2121 * and WifiStateMachine which need direct access. All other clients should use
2122 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2123 * logic.</p>
2124 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002125 * @param iface the interface name to tether.
2126 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2127 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002128 * {@hide}
2129 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08002130 public int tether(String iface) {
2131 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002132 String pkgName = mContext.getOpPackageName();
2133 Log.i(TAG, "tether caller:" + pkgName);
2134 return mService.tether(iface, pkgName);
Robert Greenwalt5a735062010-03-02 17:25:02 -08002135 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002136 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002137 }
2138 }
2139
2140 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002141 * Stop tethering the named interface.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002142 *
2143 * <p>This method requires the caller to hold either the
2144 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2145 * or the ability to modify system settings as determined by
2146 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002147 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002148 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2149 * and WifiStateMachine which need direct access. All other clients should use
2150 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2151 * logic.</p>
2152 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002153 * @param iface the interface name to untether.
2154 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2155 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002156 * {@hide}
2157 */
2158 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002159 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002160 String pkgName = mContext.getOpPackageName();
2161 Log.i(TAG, "untether caller:" + pkgName);
2162 return mService.untether(iface, pkgName);
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002163 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002164 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002165 }
2166 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002167
2168 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002169 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002170 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002171 * due to device configuration.
2172 *
Chalard Jean8f76fc32017-09-26 15:45:18 +09002173 * <p>If this app does not have permission to use this API, it will always
2174 * return false rather than throw an exception.</p>
2175 *
2176 * <p>If the device has a hotspot provisioning app, the caller is required to hold the
2177 * {@link android.Manifest.permission.TETHER_PRIVILEGED} permission.</p>
2178 *
2179 * <p>Otherwise, this method requires the caller to hold the ability to modify system
2180 * settings as determined by {@link android.provider.Settings.System#canWrite}.</p>
2181 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002182 * @return a boolean - {@code true} indicating Tethering is supported.
2183 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002184 * {@hide}
2185 */
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002186 @SystemApi
Chalard Jean8f76fc32017-09-26 15:45:18 +09002187 @RequiresPermission(anyOf = {android.Manifest.permission.TETHER_PRIVILEGED,
2188 android.Manifest.permission.WRITE_SETTINGS})
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002189 public boolean isTetheringSupported() {
Chalard Jean8f76fc32017-09-26 15:45:18 +09002190 String pkgName = mContext.getOpPackageName();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002191 try {
Udam Saini0e94c362017-06-07 12:06:28 -07002192 return mService.isTetheringSupported(pkgName);
Chalard Jean8f76fc32017-09-26 15:45:18 +09002193 } catch (SecurityException e) {
2194 // This API is not available to this caller, but for backward-compatibility
2195 // this will just return false instead of throwing.
2196 return false;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002197 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002198 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002199 }
2200 }
2201
2202 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002203 * Callback for use with {@link #startTethering} to find out whether tethering succeeded.
2204 * @hide
2205 */
2206 @SystemApi
2207 public static abstract class OnStartTetheringCallback {
2208 /**
2209 * Called when tethering has been successfully started.
2210 */
2211 public void onTetheringStarted() {};
2212
2213 /**
2214 * Called when starting tethering failed.
2215 */
2216 public void onTetheringFailed() {};
2217 }
2218
2219 /**
2220 * Convenient overload for
2221 * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null
2222 * handler to run on the current thread's {@link Looper}.
2223 * @hide
2224 */
2225 @SystemApi
Udam Saini0e94c362017-06-07 12:06:28 -07002226 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002227 public void startTethering(int type, boolean showProvisioningUi,
2228 final OnStartTetheringCallback callback) {
2229 startTethering(type, showProvisioningUi, callback, null);
2230 }
2231
2232 /**
2233 * Runs tether provisioning for the given type if needed and then starts tethering if
2234 * the check succeeds. If no carrier provisioning is required for tethering, tethering is
2235 * enabled immediately. If provisioning fails, tethering will not be enabled. It also
2236 * schedules tether provisioning re-checks if appropriate.
2237 *
2238 * @param type The type of tethering to start. Must be one of
2239 * {@link ConnectivityManager.TETHERING_WIFI},
2240 * {@link ConnectivityManager.TETHERING_USB}, or
2241 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2242 * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there
2243 * is one. This should be true the first time this function is called and also any time
2244 * the user can see this UI. It gives users information from their carrier about the
2245 * check failing and how they can sign up for tethering if possible.
2246 * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller
2247 * of the result of trying to tether.
2248 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
2249 * @hide
2250 */
2251 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002252 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002253 public void startTethering(int type, boolean showProvisioningUi,
2254 final OnStartTetheringCallback callback, Handler handler) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09002255 Preconditions.checkNotNull(callback, "OnStartTetheringCallback cannot be null.");
Jeremy Klein5f277e12016-03-12 16:29:54 -08002256
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002257 ResultReceiver wrappedCallback = new ResultReceiver(handler) {
2258 @Override
2259 protected void onReceiveResult(int resultCode, Bundle resultData) {
2260 if (resultCode == TETHER_ERROR_NO_ERROR) {
2261 callback.onTetheringStarted();
2262 } else {
2263 callback.onTetheringFailed();
2264 }
2265 }
2266 };
Jeremy Klein5f277e12016-03-12 16:29:54 -08002267
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002268 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002269 String pkgName = mContext.getOpPackageName();
2270 Log.i(TAG, "startTethering caller:" + pkgName);
2271 mService.startTethering(type, wrappedCallback, showProvisioningUi, pkgName);
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002272 } catch (RemoteException e) {
2273 Log.e(TAG, "Exception trying to start tethering.", e);
2274 wrappedCallback.send(TETHER_ERROR_SERVICE_UNAVAIL, null);
2275 }
2276 }
2277
2278 /**
2279 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if
2280 * applicable.
2281 *
2282 * @param type The type of tethering to stop. Must be one of
2283 * {@link ConnectivityManager.TETHERING_WIFI},
2284 * {@link ConnectivityManager.TETHERING_USB}, or
2285 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2286 * @hide
2287 */
2288 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002289 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002290 public void stopTethering(int type) {
2291 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002292 String pkgName = mContext.getOpPackageName();
2293 Log.i(TAG, "stopTethering caller:" + pkgName);
2294 mService.stopTethering(type, pkgName);
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002295 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002296 throw e.rethrowFromSystemServer();
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002297 }
2298 }
2299
2300 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002301 * Get the list of regular expressions that define any tetherable
2302 * USB network interfaces. If USB tethering is not supported by the
2303 * device, this list should be empty.
2304 *
2305 * @return an array of 0 or more regular expression Strings defining
2306 * what interfaces are considered tetherable usb interfaces.
2307 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002308 * {@hide}
2309 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002310 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002311 public String[] getTetherableUsbRegexs() {
2312 try {
2313 return mService.getTetherableUsbRegexs();
2314 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002315 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002316 }
2317 }
2318
2319 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002320 * Get the list of regular expressions that define any tetherable
2321 * Wifi network interfaces. If Wifi tethering is not supported by the
2322 * device, this list should be empty.
2323 *
2324 * @return an array of 0 or more regular expression Strings defining
2325 * what interfaces are considered tetherable wifi interfaces.
2326 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002327 * {@hide}
2328 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002329 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002330 public String[] getTetherableWifiRegexs() {
2331 try {
2332 return mService.getTetherableWifiRegexs();
2333 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002334 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002335 }
2336 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08002337
Danica Chang6fdd0c62010-08-11 14:54:43 -07002338 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002339 * Get the list of regular expressions that define any tetherable
2340 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
2341 * device, this list should be empty.
2342 *
2343 * @return an array of 0 or more regular expression Strings defining
2344 * what interfaces are considered tetherable bluetooth interfaces.
2345 *
Danica Chang6fdd0c62010-08-11 14:54:43 -07002346 * {@hide}
2347 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002348 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Danica Chang6fdd0c62010-08-11 14:54:43 -07002349 public String[] getTetherableBluetoothRegexs() {
2350 try {
2351 return mService.getTetherableBluetoothRegexs();
2352 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002353 throw e.rethrowFromSystemServer();
Danica Chang6fdd0c62010-08-11 14:54:43 -07002354 }
2355 }
2356
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002357 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002358 * Attempt to both alter the mode of USB and Tethering of USB. A
2359 * utility method to deal with some of the complexity of USB - will
2360 * attempt to switch to Rndis and subsequently tether the resulting
2361 * interface on {@code true} or turn off tethering and switch off
2362 * Rndis on {@code false}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002363 *
2364 * <p>This method requires the caller to hold either the
2365 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2366 * or the ability to modify system settings as determined by
2367 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002368 *
2369 * @param enable a boolean - {@code true} to enable tethering
2370 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2371 *
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002372 * {@hide}
2373 */
2374 public int setUsbTethering(boolean enable) {
2375 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002376 String pkgName = mContext.getOpPackageName();
2377 Log.i(TAG, "setUsbTethering caller:" + pkgName);
2378 return mService.setUsbTethering(enable, pkgName);
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002379 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002380 throw e.rethrowFromSystemServer();
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002381 }
2382 }
2383
Robert Greenwalt5a735062010-03-02 17:25:02 -08002384 /** {@hide} */
2385 public static final int TETHER_ERROR_NO_ERROR = 0;
2386 /** {@hide} */
2387 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
2388 /** {@hide} */
2389 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
2390 /** {@hide} */
2391 public static final int TETHER_ERROR_UNSUPPORTED = 3;
2392 /** {@hide} */
2393 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
2394 /** {@hide} */
2395 public static final int TETHER_ERROR_MASTER_ERROR = 5;
2396 /** {@hide} */
2397 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
2398 /** {@hide} */
2399 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
2400 /** {@hide} */
2401 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
2402 /** {@hide} */
2403 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
2404 /** {@hide} */
2405 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002406 /** {@hide} */
2407 public static final int TETHER_ERROR_PROVISION_FAILED = 11;
Robert Greenwalt5a735062010-03-02 17:25:02 -08002408
2409 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002410 * Get a more detailed error code after a Tethering or Untethering
2411 * request asynchronously failed.
2412 *
2413 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08002414 * @return error The error code of the last error tethering or untethering the named
2415 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002416 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002417 * {@hide}
2418 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002419 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt5a735062010-03-02 17:25:02 -08002420 public int getLastTetherError(String iface) {
2421 try {
2422 return mService.getLastTetherError(iface);
2423 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002424 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002425 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002426 }
2427
2428 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002429 * Report network connectivity status. This is currently used only
2430 * to alter status bar UI.
Paul Jensenb2748922015-05-06 11:10:18 -04002431 * <p>This method requires the caller to hold the permission
2432 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002433 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002434 * @param networkType The type of network you want to report on
2435 * @param percentage The quality of the connection 0 is bad, 100 is good
Chalard Jean6b1da6e2018-03-08 13:54:53 +09002436 * @deprecated Types are deprecated. Use {@link #reportNetworkConnectivity} instead.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002437 * {@hide}
2438 */
2439 public void reportInetCondition(int networkType, int percentage) {
2440 try {
2441 mService.reportInetCondition(networkType, percentage);
2442 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002443 throw e.rethrowFromSystemServer();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002444 }
2445 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002446
2447 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002448 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07002449 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002450 * the framework to re-evaluate network connectivity and/or switch to another
2451 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002452 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002453 * @param network The {@link Network} the application was attempting to use
2454 * or {@code null} to indicate the current default network.
Paul Jensenbfd17b72015-04-07 12:43:13 -04002455 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
2456 * working and non-working connectivity.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002457 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002458 @Deprecated
Robert Greenwalt9258c642014-03-26 16:47:06 -07002459 public void reportBadNetwork(Network network) {
2460 try {
Paul Jensenbfd17b72015-04-07 12:43:13 -04002461 // One of these will be ignored because it matches system's current state.
2462 // The other will trigger the necessary reevaluation.
2463 mService.reportNetworkConnectivity(network, true);
2464 mService.reportNetworkConnectivity(network, false);
2465 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002466 throw e.rethrowFromSystemServer();
Paul Jensenbfd17b72015-04-07 12:43:13 -04002467 }
2468 }
2469
2470 /**
2471 * Report to the framework whether a network has working connectivity.
2472 * This provides a hint to the system that a particular network is providing
2473 * working connectivity or not. In response the framework may re-evaluate
2474 * the network's connectivity and might take further action thereafter.
2475 *
2476 * @param network The {@link Network} the application was attempting to use
2477 * or {@code null} to indicate the current default network.
2478 * @param hasConnectivity {@code true} if the application was able to successfully access the
2479 * Internet using {@code network} or {@code false} if not.
2480 */
2481 public void reportNetworkConnectivity(Network network, boolean hasConnectivity) {
2482 try {
2483 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002484 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002485 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002486 }
2487 }
2488
2489 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002490 * Set a network-independent global http proxy. This is not normally what you want
2491 * for typical HTTP proxies - they are general network dependent. However if you're
2492 * doing something unusual like general internal filtering this may be useful. On
2493 * a private network where the proxy is not accessible, you may break HTTP using this.
Paul Jensenb2748922015-05-06 11:10:18 -04002494 *
2495 * @param p A {@link ProxyInfo} object defining the new global
2496 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002497 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002498 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002499 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Jason Monk207900c2014-04-25 15:00:09 -04002500 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002501 try {
2502 mService.setGlobalProxy(p);
2503 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002504 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002505 }
2506 }
2507
2508 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002509 * Retrieve any network-independent global HTTP proxy.
2510 *
Jason Monk207900c2014-04-25 15:00:09 -04002511 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002512 * if no global HTTP proxy is set.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002513 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002514 */
Jason Monk207900c2014-04-25 15:00:09 -04002515 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002516 try {
2517 return mService.getGlobalProxy();
2518 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002519 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002520 }
2521 }
2522
2523 /**
Paul Jensencee9b512015-05-06 07:32:40 -04002524 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
2525 * network-specific HTTP proxy. If {@code network} is null, the
2526 * network-specific proxy returned is the proxy of the default active
2527 * network.
2528 *
2529 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
2530 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
2531 * or when {@code network} is {@code null},
2532 * the {@code ProxyInfo} for the default active network. Returns
2533 * {@code null} when no proxy applies or the caller doesn't have
2534 * permission to use {@code network}.
2535 * @hide
2536 */
2537 public ProxyInfo getProxyForNetwork(Network network) {
2538 try {
2539 return mService.getProxyForNetwork(network);
2540 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002541 throw e.rethrowFromSystemServer();
Paul Jensencee9b512015-05-06 07:32:40 -04002542 }
2543 }
2544
2545 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002546 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
2547 * otherwise if this process is bound to a {@link Network} using
Paul Jensen72db88e2015-03-10 10:54:12 -04002548 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensene0bef712014-12-10 15:12:18 -05002549 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002550 *
Jason Monk207900c2014-04-25 15:00:09 -04002551 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002552 * HTTP proxy is active.
Robert Greenwalt434203a2010-10-11 16:00:27 -07002553 */
Paul Jensene0bef712014-12-10 15:12:18 -05002554 public ProxyInfo getDefaultProxy() {
Paul Jensencee9b512015-05-06 07:32:40 -04002555 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwalt434203a2010-10-11 16:00:27 -07002556 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07002557
2558 /**
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002559 * Returns true if the hardware supports the given network type
2560 * else it returns false. This doesn't indicate we have coverage
2561 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002562 * hardware supports it. For example a GSM phone without a SIM
2563 * should still return {@code true} for mobile data, but a wifi only
2564 * tablet would return {@code false}.
2565 *
2566 * @param networkType The network type we'd like to check
2567 * @return {@code true} if supported, else {@code false}
Chalard Jean6b1da6e2018-03-08 13:54:53 +09002568 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002569 * @hide
2570 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +09002571 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002572 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002573 public boolean isNetworkSupported(int networkType) {
2574 try {
2575 return mService.isNetworkSupported(networkType);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002576 } catch (RemoteException e) {
2577 throw e.rethrowFromSystemServer();
2578 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002579 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002580
2581 /**
2582 * Returns if the currently active data network is metered. A network is
2583 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002584 * that connection due to monetary costs, data limitations or
2585 * battery/performance issues. You should check this before doing large
2586 * data transfers, and warn the user or delay the operation until another
2587 * network is available.
2588 *
2589 * @return {@code true} if large transfers should be avoided, otherwise
2590 * {@code false}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002591 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002592 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002593 public boolean isActiveNetworkMetered() {
2594 try {
2595 return mService.isActiveNetworkMetered();
2596 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002597 throw e.rethrowFromSystemServer();
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002598 }
2599 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002600
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002601 /**
2602 * If the LockdownVpn mechanism is enabled, updates the vpn
2603 * with a reload of its profile.
2604 *
2605 * @return a boolean with {@code} indicating success
2606 *
2607 * <p>This method can only be called by the system UID
2608 * {@hide}
2609 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002610 public boolean updateLockdownVpn() {
2611 try {
2612 return mService.updateLockdownVpn();
2613 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002614 throw e.rethrowFromSystemServer();
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002615 }
2616 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07002617
2618 /**
Wink Saville948282b2013-08-29 08:55:16 -07002619 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07002620 *
Wink Savilleab9321d2013-06-29 21:10:57 -07002621 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07002622 *
2623 * @return time out that will be used, maybe less that suggestedTimeOutMs
2624 * -1 if an error.
2625 *
2626 * {@hide}
2627 */
Wink Saville948282b2013-08-29 08:55:16 -07002628 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07002629 int timeOutMs = -1;
2630 try {
Wink Saville948282b2013-08-29 08:55:16 -07002631 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07002632 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002633 throw e.rethrowFromSystemServer();
Wink Savilleab9321d2013-06-29 21:10:57 -07002634 }
2635 return timeOutMs;
2636 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002637
2638 /**
Wink Saville42d4f082013-07-20 20:31:59 -07002639 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002640 * {@hide}
2641 */
2642 public String getMobileProvisioningUrl() {
2643 try {
2644 return mService.getMobileProvisioningUrl();
2645 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002646 throw e.rethrowFromSystemServer();
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002647 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002648 }
Wink Saville42d4f082013-07-20 20:31:59 -07002649
2650 /**
Wink Saville948282b2013-08-29 08:55:16 -07002651 * Set sign in error notification to visible or in visible
2652 *
2653 * @param visible
2654 * @param networkType
2655 *
2656 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04002657 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville948282b2013-08-29 08:55:16 -07002658 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002659 @Deprecated
Wink Saville948282b2013-08-29 08:55:16 -07002660 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04002661 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07002662 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04002663 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07002664 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002665 throw e.rethrowFromSystemServer();
Wink Saville948282b2013-08-29 08:55:16 -07002666 }
2667 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002668
2669 /**
2670 * Set the value for enabling/disabling airplane mode
2671 *
2672 * @param enable whether to enable airplane mode or not
2673 *
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002674 * @hide
2675 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002676 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002677 public void setAirplaneMode(boolean enable) {
2678 try {
2679 mService.setAirplaneMode(enable);
2680 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002681 throw e.rethrowFromSystemServer();
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002682 }
2683 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002684
2685 /** {@hide} */
Robert Greenwalta67be032014-05-16 15:49:14 -07002686 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07002687 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07002688 mService.registerNetworkFactory(messenger, name);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002689 } catch (RemoteException e) {
2690 throw e.rethrowFromSystemServer();
2691 }
Robert Greenwalta67be032014-05-16 15:49:14 -07002692 }
2693
2694 /** {@hide} */
2695 public void unregisterNetworkFactory(Messenger messenger) {
2696 try {
2697 mService.unregisterNetworkFactory(messenger);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002698 } catch (RemoteException e) {
2699 throw e.rethrowFromSystemServer();
2700 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002701 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002702
Paul Jensen31a94f42015-02-13 14:18:39 -05002703 /**
2704 * @hide
2705 * Register a NetworkAgent with ConnectivityService.
2706 * @return NetID corresponding to NetworkAgent.
2707 */
2708 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002709 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002710 try {
Paul Jensen31a94f42015-02-13 14:18:39 -05002711 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
2712 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002713 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -05002714 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002715 }
2716
Robert Greenwalt9258c642014-03-26 16:47:06 -07002717 /**
Hugo Benichidafed3d2017-03-06 09:17:06 +09002718 * Base class for {@code NetworkRequest} callbacks. Used for notifications about network
2719 * changes. Should be extended by applications wanting notifications.
2720 *
2721 * A {@code NetworkCallback} is registered by calling
2722 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
2723 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)},
Hugo Benichica867dc2018-02-07 21:17:43 +09002724 * or {@link #registerDefaultNetworkCallback(NetworkCallback)}. A {@code NetworkCallback} is
Hugo Benichidafed3d2017-03-06 09:17:06 +09002725 * unregistered by calling {@link #unregisterNetworkCallback(NetworkCallback)}.
2726 * A {@code NetworkCallback} should be registered at most once at any time.
2727 * A {@code NetworkCallback} that has been unregistered can be registered again.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002728 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002729 public static class NetworkCallback {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002730 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002731 * Called when the framework connects to a new network to evaluate whether it satisfies this
2732 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
2733 * callback. There is no guarantee that this new network will satisfy any requests, or that
2734 * the network will stay connected for longer than the time necessary to evaluate it.
2735 * <p>
2736 * Most applications <b>should not</b> act on this callback, and should instead use
2737 * {@link #onAvailable}. This callback is intended for use by applications that can assist
2738 * the framework in properly evaluating the network &mdash; for example, an application that
2739 * can automatically log in to a captive portal without user intervention.
2740 *
2741 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti66276122015-06-11 14:27:17 +09002742 *
2743 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002744 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002745 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002746
2747 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002748 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002749 * This callback may be called more than once if the {@link Network} that is
2750 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002751 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002752 * @param network The {@link Network} of the satisfying network.
Chalard Jean804b8fb2018-01-30 22:41:41 +09002753 * @param networkCapabilities The {@link NetworkCapabilities} of the satisfying network.
2754 * @param linkProperties The {@link LinkProperties} of the satisfying network.
2755 * @hide
2756 */
2757 public void onAvailable(Network network, NetworkCapabilities networkCapabilities,
2758 LinkProperties linkProperties) {
2759 // Internally only this method is called when a new network is available, and
2760 // it calls the callback in the same way and order that older versions used
2761 // to call so as not to change the behavior.
2762 onAvailable(network);
2763 if (!networkCapabilities.hasCapability(
2764 NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)) {
2765 onNetworkSuspended(network);
2766 }
2767 onCapabilitiesChanged(network, networkCapabilities);
2768 onLinkPropertiesChanged(network, linkProperties);
2769 }
2770
2771 /**
2772 * Called when the framework connects and has declared a new network ready for use.
2773 * This callback may be called more than once if the {@link Network} that is
2774 * satisfying the request changes. This will always immediately be followed by a
2775 * call to {@link #onCapabilitiesChanged(Network, NetworkCapabilities)} then by a
2776 * call to {@link #onLinkPropertiesChanged(Network, LinkProperties)}.
2777 *
2778 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002779 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002780 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002781
2782 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002783 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002784 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002785 * for graceful handover. This may not be called if we have a hard loss
2786 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002787 * {@link NetworkCallback#onLost} call or a
2788 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002789 * on whether we lose or regain it.
2790 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002791 * @param network The {@link Network} that is about to be disconnected.
2792 * @param maxMsToLive The time in ms the framework will attempt to keep the
2793 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002794 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002795 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002796 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002797
2798 /**
2799 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002800 * graceful failure ends.
2801 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002802 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002803 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002804 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002805
2806 /**
Etan Cohenaebf17e2017-03-01 12:47:28 -08002807 * Called if no network is found in the timeout time specified in
Hugo Benichi0eec03f2017-05-15 15:15:33 +09002808 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} call. This callback is not
Etan Cohenaebf17e2017-03-01 12:47:28 -08002809 * called for the version of {@link #requestNetwork(NetworkRequest, NetworkCallback)}
2810 * without timeout. When this callback is invoked the associated
2811 * {@link NetworkRequest} will have already been removed and released, as if
2812 * {@link #unregisterNetworkCallback(NetworkCallback)} had been called.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002813 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002814 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002815
2816 /**
2817 * Called when the network the framework connected to for this request
2818 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002819 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002820 * @param network The {@link Network} whose capabilities have changed.
Chalard Jean804b8fb2018-01-30 22:41:41 +09002821 * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this
2822 * network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002823 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002824 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002825 NetworkCapabilities networkCapabilities) {}
2826
2827 /**
2828 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002829 * changes {@link LinkProperties}.
2830 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002831 * @param network The {@link Network} whose link properties have changed.
2832 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002833 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002834 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002835
Robert Greenwalt8d482522015-06-24 13:23:42 -07002836 /**
2837 * Called when the network the framework connected to for this request
Chalard Jean804b8fb2018-01-30 22:41:41 +09002838 * goes into {@link NetworkInfo.State#SUSPENDED}.
Robert Greenwalt8d482522015-06-24 13:23:42 -07002839 * This generally means that while the TCP connections are still live,
2840 * temporarily network data fails to transfer. Specifically this is used
2841 * on cellular networks to mask temporary outages when driving through
2842 * a tunnel, etc.
2843 * @hide
2844 */
2845 public void onNetworkSuspended(Network network) {}
2846
2847 /**
2848 * Called when the network the framework connected to for this request
Chalard Jean804b8fb2018-01-30 22:41:41 +09002849 * returns from a {@link NetworkInfo.State#SUSPENDED} state. This should always be
2850 * preceded by a matching {@link NetworkCallback#onNetworkSuspended} call.
Robert Greenwalt8d482522015-06-24 13:23:42 -07002851 * @hide
2852 */
2853 public void onNetworkResumed(Network network) {}
2854
Robert Greenwalt6078b502014-06-11 16:05:07 -07002855 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002856 }
2857
Hugo Benichicb883232017-05-11 13:16:17 +09002858 /**
2859 * Constant error codes used by ConnectivityService to communicate about failures and errors
2860 * across a Binder boundary.
2861 * @hide
2862 */
2863 public interface Errors {
2864 static int TOO_MANY_REQUESTS = 1;
2865 }
2866
2867 /** @hide */
2868 public static class TooManyRequestsException extends RuntimeException {}
2869
2870 private static RuntimeException convertServiceException(ServiceSpecificException e) {
2871 switch (e.errorCode) {
2872 case Errors.TOO_MANY_REQUESTS:
2873 return new TooManyRequestsException();
2874 default:
2875 Log.w(TAG, "Unknown service error code " + e.errorCode);
2876 return new RuntimeException(e);
2877 }
2878 }
2879
Robert Greenwalt9258c642014-03-26 16:47:06 -07002880 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002881 /** @hide */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002882 public static final int CALLBACK_PRECHECK = BASE + 1;
2883 /** @hide */
2884 public static final int CALLBACK_AVAILABLE = BASE + 2;
2885 /** @hide arg1 = TTL */
2886 public static final int CALLBACK_LOSING = BASE + 3;
2887 /** @hide */
2888 public static final int CALLBACK_LOST = BASE + 4;
2889 /** @hide */
2890 public static final int CALLBACK_UNAVAIL = BASE + 5;
2891 /** @hide */
2892 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2893 /** @hide */
2894 public static final int CALLBACK_IP_CHANGED = BASE + 7;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002895 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Hugo Benichidba33db2017-03-23 22:40:44 +09002896 private static final int EXPIRE_LEGACY_REQUEST = BASE + 8;
Robert Greenwalt8d482522015-06-24 13:23:42 -07002897 /** @hide */
Hugo Benichidba33db2017-03-23 22:40:44 +09002898 public static final int CALLBACK_SUSPENDED = BASE + 9;
Robert Greenwalt8d482522015-06-24 13:23:42 -07002899 /** @hide */
Hugo Benichidba33db2017-03-23 22:40:44 +09002900 public static final int CALLBACK_RESUMED = BASE + 10;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002901
Erik Kline57faba92015-11-25 12:49:38 +09002902 /** @hide */
2903 public static String getCallbackName(int whichCallback) {
2904 switch (whichCallback) {
2905 case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK";
2906 case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE";
2907 case CALLBACK_LOSING: return "CALLBACK_LOSING";
2908 case CALLBACK_LOST: return "CALLBACK_LOST";
2909 case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL";
2910 case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED";
2911 case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED";
Erik Kline57faba92015-11-25 12:49:38 +09002912 case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST";
2913 case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED";
2914 case CALLBACK_RESUMED: return "CALLBACK_RESUMED";
2915 default:
2916 return Integer.toString(whichCallback);
2917 }
2918 }
2919
Robert Greenwalt562cc542014-05-15 18:07:26 -07002920 private class CallbackHandler extends Handler {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002921 private static final String TAG = "ConnectivityManager.CallbackHandler";
Robert Greenwalta9ebeef2015-09-03 16:41:45 -07002922 private static final boolean DBG = false;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002923
Hugo Benichid42650f2016-07-06 22:53:17 +09002924 CallbackHandler(Looper looper) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002925 super(looper);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002926 }
2927
Hugo Benichi2583ef02017-02-02 17:02:36 +09002928 CallbackHandler(Handler handler) {
Hugo Benichie7678512017-05-09 15:19:01 +09002929 this(Preconditions.checkNotNull(handler, "Handler cannot be null.").getLooper());
Hugo Benichi2583ef02017-02-02 17:02:36 +09002930 }
2931
Robert Greenwalt9258c642014-03-26 16:47:06 -07002932 @Override
2933 public void handleMessage(Message message) {
Hugo Benichi2c684522017-05-09 14:36:02 +09002934 if (message.what == EXPIRE_LEGACY_REQUEST) {
2935 expireRequest((NetworkCapabilities) message.obj, message.arg1);
2936 return;
2937 }
2938
2939 final NetworkRequest request = getObject(message, NetworkRequest.class);
2940 final Network network = getObject(message, Network.class);
2941 final NetworkCallback callback;
2942 synchronized (sCallbacks) {
2943 callback = sCallbacks.get(request);
2944 }
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09002945 if (DBG) {
Hugo Benichia0385682017-03-22 17:07:57 +09002946 Log.d(TAG, getCallbackName(message.what) + " for network " + network);
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09002947 }
Hugo Benichi2c684522017-05-09 14:36:02 +09002948 if (callback == null) {
2949 Log.w(TAG, "callback not found for " + getCallbackName(message.what) + " message");
2950 return;
2951 }
2952
Robert Greenwalt9258c642014-03-26 16:47:06 -07002953 switch (message.what) {
2954 case CALLBACK_PRECHECK: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002955 callback.onPreCheck(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002956 break;
2957 }
2958 case CALLBACK_AVAILABLE: {
Chalard Jean804b8fb2018-01-30 22:41:41 +09002959 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
2960 LinkProperties lp = getObject(message, LinkProperties.class);
2961 callback.onAvailable(network, cap, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002962 break;
2963 }
2964 case CALLBACK_LOSING: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002965 callback.onLosing(network, message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002966 break;
2967 }
2968 case CALLBACK_LOST: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002969 callback.onLost(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002970 break;
2971 }
2972 case CALLBACK_UNAVAIL: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002973 callback.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002974 break;
2975 }
2976 case CALLBACK_CAP_CHANGED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002977 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
2978 callback.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002979 break;
2980 }
2981 case CALLBACK_IP_CHANGED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002982 LinkProperties lp = getObject(message, LinkProperties.class);
2983 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002984 break;
2985 }
Robert Greenwalt8d482522015-06-24 13:23:42 -07002986 case CALLBACK_SUSPENDED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002987 callback.onNetworkSuspended(network);
Robert Greenwalt8d482522015-06-24 13:23:42 -07002988 break;
2989 }
2990 case CALLBACK_RESUMED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002991 callback.onNetworkResumed(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07002992 break;
2993 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002994 }
2995 }
2996
Hugo Benichid42650f2016-07-06 22:53:17 +09002997 private <T> T getObject(Message msg, Class<T> c) {
2998 return (T) msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07002999 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003000 }
3001
Hugo Benichi2583ef02017-02-02 17:02:36 +09003002 private CallbackHandler getDefaultHandler() {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003003 synchronized (sCallbacks) {
3004 if (sCallbackHandler == null) {
3005 sCallbackHandler = new CallbackHandler(ConnectivityThread.getInstanceLooper());
Robert Greenwalt9258c642014-03-26 16:47:06 -07003006 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003007 return sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003008 }
3009 }
3010
Hugo Benichi6f260f32017-02-03 14:18:44 +09003011 private static final HashMap<NetworkRequest, NetworkCallback> sCallbacks = new HashMap<>();
3012 private static CallbackHandler sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003013
Hugo Benichi6f260f32017-02-03 14:18:44 +09003014 private static final int LISTEN = 1;
3015 private static final int REQUEST = 2;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003016
Hugo Benichi6f260f32017-02-03 14:18:44 +09003017 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, NetworkCallback callback,
3018 int timeoutMs, int action, int legacyType, CallbackHandler handler) {
Hugo Benichie7678512017-05-09 15:19:01 +09003019 checkCallbackNotNull(callback);
Hugo Benichidafed3d2017-03-06 09:17:06 +09003020 Preconditions.checkArgument(action == REQUEST || need != null, "null NetworkCapabilities");
Hugo Benichid42650f2016-07-06 22:53:17 +09003021 final NetworkRequest request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003022 try {
Hugo Benichid42650f2016-07-06 22:53:17 +09003023 synchronized(sCallbacks) {
Hugo Benichi31c176d2017-06-17 13:14:12 +09003024 if (callback.networkRequest != null
3025 && callback.networkRequest != ALREADY_UNREGISTERED) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09003026 // TODO: throw exception instead and enforce 1:1 mapping of callbacks
3027 // and requests (http://b/20701525).
3028 Log.e(TAG, "NetworkCallback was already registered");
3029 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003030 Messenger messenger = new Messenger(handler);
Hugo Benichid42650f2016-07-06 22:53:17 +09003031 Binder binder = new Binder();
Paul Jensen7221cc32014-06-27 11:05:32 -04003032 if (action == LISTEN) {
Hugo Benichid42650f2016-07-06 22:53:17 +09003033 request = mService.listenForNetwork(need, messenger, binder);
Paul Jensen7221cc32014-06-27 11:05:32 -04003034 } else {
Hugo Benichid42650f2016-07-06 22:53:17 +09003035 request = mService.requestNetwork(
3036 need, messenger, timeoutMs, binder, legacyType);
Paul Jensen7221cc32014-06-27 11:05:32 -04003037 }
Hugo Benichid42650f2016-07-06 22:53:17 +09003038 if (request != null) {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003039 sCallbacks.put(request, callback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003040 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003041 callback.networkRequest = request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003042 }
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003043 } catch (RemoteException e) {
3044 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003045 } catch (ServiceSpecificException e) {
3046 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003047 }
Hugo Benichid42650f2016-07-06 22:53:17 +09003048 return request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003049 }
3050
3051 /**
Erik Klinea2d29402016-03-16 15:31:39 +09003052 * Helper function to request a network with a particular legacy type.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09003053 *
3054 * This is temporarily public @hide so it can be called by system code that uses the
3055 * NetworkRequest API to request networks but relies on CONNECTIVITY_ACTION broadcasts for
3056 * instead network notifications.
3057 *
3058 * TODO: update said system code to rely on NetworkCallbacks and make this method private.
3059 *
3060 * @hide
3061 */
Lorenzo Colittid1179462015-11-25 15:47:14 +09003062 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
Hugo Benichi2583ef02017-02-02 17:02:36 +09003063 int timeoutMs, int legacyType, Handler handler) {
3064 CallbackHandler cbHandler = new CallbackHandler(handler);
3065 NetworkCapabilities nc = request.networkCapabilities;
3066 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, legacyType, cbHandler);
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09003067 }
3068
3069 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09003070 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003071 *
3072 * This {@link NetworkRequest} will live until released via
Etan Cohenaebf17e2017-03-01 12:47:28 -08003073 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A
3074 * version of the method which takes a timeout is
Hugo Benichi0eec03f2017-05-15 15:15:33 +09003075 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003076 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07003077 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003078 * can be used to direct traffic to the network.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003079 * <p>It is presently unsupported to request a network with mutable
3080 * {@link NetworkCapabilities} such as
3081 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3082 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3083 * as these {@code NetworkCapabilities} represent states that a particular
3084 * network may never attain, and whether a network will attain these states
3085 * is unknown prior to bringing up the network so the framework does not
3086 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003087 *
3088 * <p>This method requires the caller to hold either the
3089 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3090 * or the ability to modify system settings as determined by
3091 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt9258c642014-03-26 16:47:06 -07003092 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003093 * @param request {@link NetworkRequest} describing this request.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003094 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3095 * the callback must not be shared - it uniquely specifies this request.
3096 * The callback is invoked on the default internal Handler.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003097 * @throws IllegalArgumentException if {@code request} specifies any mutable
3098 * {@code NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003099 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003100 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003101 requestNetwork(request, networkCallback, getDefaultHandler());
3102 }
3103
3104 /**
3105 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
3106 *
3107 * This {@link NetworkRequest} will live until released via
Etan Cohenaebf17e2017-03-01 12:47:28 -08003108 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A
3109 * version of the method which takes a timeout is
Hugo Benichi0eec03f2017-05-15 15:15:33 +09003110 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003111 * Status of the request can be followed by listening to the various
3112 * callbacks described in {@link NetworkCallback}. The {@link Network}
3113 * can be used to direct traffic to the network.
3114 * <p>It is presently unsupported to request a network with mutable
3115 * {@link NetworkCapabilities} such as
3116 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3117 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3118 * as these {@code NetworkCapabilities} represent states that a particular
3119 * network may never attain, and whether a network will attain these states
3120 * is unknown prior to bringing up the network so the framework does not
3121 * know how to go about satisfing a request with these capabilities.
3122 *
3123 * <p>This method requires the caller to hold either the
3124 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3125 * or the ability to modify system settings as determined by
3126 * {@link android.provider.Settings.System#canWrite}.</p>
3127 *
3128 * @param request {@link NetworkRequest} describing this request.
3129 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3130 * the callback must not be shared - it uniquely specifies this request.
3131 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
3132 * @throws IllegalArgumentException if {@code request} specifies any mutable
3133 * {@code NetworkCapabilities}.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003134 */
3135 public void requestNetwork(
3136 NetworkRequest request, NetworkCallback networkCallback, Handler handler) {
3137 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
3138 CallbackHandler cbHandler = new CallbackHandler(handler);
3139 requestNetwork(request, networkCallback, 0, legacyType, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003140 }
3141
3142 /**
Etan Cohenaebf17e2017-03-01 12:47:28 -08003143 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
3144 * by a timeout.
3145 *
3146 * This function behaves identically to the non-timed-out version
3147 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network
3148 * is not found within the given time (in milliseconds) the
3149 * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be
3150 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3151 * not have to be released if timed-out (it is automatically released). Unregistering a
3152 * request that timed out is not an error.
3153 *
3154 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3155 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3156 * for that purpose. Calling this method will attempt to bring up the requested network.
3157 *
3158 * <p>This method requires the caller to hold either the
3159 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3160 * or the ability to modify system settings as determined by
3161 * {@link android.provider.Settings.System#canWrite}.</p>
3162 *
3163 * @param request {@link NetworkRequest} describing this request.
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003164 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3165 * the callback must not be shared - it uniquely specifies this request.
Etan Cohenaebf17e2017-03-01 12:47:28 -08003166 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3167 * before {@link NetworkCallback#onUnavailable()} is called. The timeout must
3168 * be a positive value (i.e. >0).
Etan Cohenaebf17e2017-03-01 12:47:28 -08003169 */
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003170 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
3171 int timeoutMs) {
Hugo Benichie7678512017-05-09 15:19:01 +09003172 checkTimeout(timeoutMs);
Hugo Benichi2583ef02017-02-02 17:02:36 +09003173 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
Erik Klineb583b032017-02-22 12:58:24 +09003174 requestNetwork(request, networkCallback, timeoutMs, legacyType, getDefaultHandler());
Hugo Benichi2583ef02017-02-02 17:02:36 +09003175 }
3176
Hugo Benichi2583ef02017-02-02 17:02:36 +09003177 /**
3178 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
3179 * by a timeout.
3180 *
3181 * This function behaves identically to the non-timedout version, but if a suitable
3182 * network is not found within the given time (in milliseconds) the
Etan Cohenaebf17e2017-03-01 12:47:28 -08003183 * {@link NetworkCallback#onUnavailable} callback is called. The request can still be
3184 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3185 * not have to be released if timed-out (it is automatically released). Unregistering a
3186 * request that timed out is not an error.
3187 *
3188 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3189 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3190 * for that purpose. Calling this method will attempt to bring up the requested network.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003191 *
3192 * <p>This method requires the caller to hold either the
3193 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3194 * or the ability to modify system settings as determined by
3195 * {@link android.provider.Settings.System#canWrite}.</p>
3196 *
3197 * @param request {@link NetworkRequest} describing this request.
Etan Cohenaebf17e2017-03-01 12:47:28 -08003198 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3199 * the callback must not be shared - it uniquely specifies this request.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003200 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003201 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3202 * before {@link NetworkCallback#onUnavailable} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003203 */
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003204 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
3205 Handler handler, int timeoutMs) {
Hugo Benichie7678512017-05-09 15:19:01 +09003206 checkTimeout(timeoutMs);
Hugo Benichi2583ef02017-02-02 17:02:36 +09003207 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
3208 CallbackHandler cbHandler = new CallbackHandler(handler);
3209 requestNetwork(request, networkCallback, timeoutMs, legacyType, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003210 }
3211
3212 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003213 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003214 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003215 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfcde58f2015-02-11 16:51:13 -08003216 * <p>
Paul Jensen72db88e2015-03-10 10:54:12 -04003217 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
3218 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003219 */
Erik Kline90e93072014-11-19 12:12:24 +09003220 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003221
3222 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07003223 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003224 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003225 * {@link android.content.Intent#getParcelableExtra(String)}.
3226 */
Erik Kline90e93072014-11-19 12:12:24 +09003227 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003228
3229
3230 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09003231 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003232 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003233 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07003234 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003235 * the request may outlive the calling application and get called back when a suitable
3236 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003237 * <p>
3238 * The operation is an Intent broadcast that goes to a broadcast receiver that
3239 * you registered with {@link Context#registerReceiver} or through the
3240 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3241 * <p>
3242 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09003243 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3244 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07003245 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07003246 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07003247 * Intent to reserve the network or it will be released shortly after the Intent
3248 * is processed.
3249 * <p>
Paul Jensen694f2b82015-06-17 14:15:39 -04003250 * If there is already a request for this Intent registered (with the equality of
Robert Greenwalt9258c642014-03-26 16:47:06 -07003251 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003252 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003253 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003254 * The request may be released normally by calling
3255 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003256 * <p>It is presently unsupported to request a network with either
3257 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3258 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3259 * as these {@code NetworkCapabilities} represent states that a particular
3260 * network may never attain, and whether a network will attain these states
3261 * is unknown prior to bringing up the network so the framework does not
3262 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003263 *
3264 * <p>This method requires the caller to hold either the
3265 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3266 * or the ability to modify system settings as determined by
3267 * {@link android.provider.Settings.System#canWrite}.</p>
3268 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003269 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003270 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07003271 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003272 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003273 * @throws IllegalArgumentException if {@code request} contains either
3274 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3275 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003276 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003277 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003278 checkPendingIntentNotNull(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003279 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07003280 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003281 } catch (RemoteException e) {
3282 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003283 } catch (ServiceSpecificException e) {
3284 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003285 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003286 }
3287
3288 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003289 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
3290 * <p>
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003291 * This method has the same behavior as
3292 * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003293 * releasing network resources and disconnecting.
3294 *
3295 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3296 * PendingIntent passed to
3297 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
3298 * corresponding NetworkRequest you'd like to remove. Cannot be null.
3299 */
3300 public void releaseNetworkRequest(PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003301 checkPendingIntentNotNull(operation);
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003302 try {
3303 mService.releasePendingNetworkRequest(operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003304 } catch (RemoteException e) {
3305 throw e.rethrowFromSystemServer();
3306 }
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003307 }
3308
Hugo Benichie7678512017-05-09 15:19:01 +09003309 private static void checkPendingIntentNotNull(PendingIntent intent) {
3310 Preconditions.checkNotNull(intent, "PendingIntent cannot be null.");
3311 }
3312
3313 private static void checkCallbackNotNull(NetworkCallback callback) {
3314 Preconditions.checkNotNull(callback, "null NetworkCallback");
3315 }
3316
3317 private static void checkTimeout(int timeoutMs) {
3318 Preconditions.checkArgumentPositive(timeoutMs, "timeoutMs must be strictly positive.");
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003319 }
3320
3321 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07003322 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07003323 * {@link NetworkRequest}. The callbacks will continue to be called until
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003324 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003325 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003326 * @param request {@link NetworkRequest} describing this request.
3327 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3328 * networks change state.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003329 * The callback is invoked on the default internal Handler.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003330 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003331 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt6078b502014-06-11 16:05:07 -07003332 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003333 registerNetworkCallback(request, networkCallback, getDefaultHandler());
3334 }
3335
3336 /**
3337 * Registers to receive notifications about all networks which satisfy the given
3338 * {@link NetworkRequest}. The callbacks will continue to be called until
3339 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003340 *
3341 * @param request {@link NetworkRequest} describing this request.
3342 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3343 * networks change state.
3344 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003345 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003346 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Hugo Benichi2583ef02017-02-02 17:02:36 +09003347 public void registerNetworkCallback(
3348 NetworkRequest request, NetworkCallback networkCallback, Handler handler) {
3349 CallbackHandler cbHandler = new CallbackHandler(handler);
3350 NetworkCapabilities nc = request.networkCapabilities;
3351 sendRequestForNetwork(nc, networkCallback, 0, LISTEN, TYPE_NONE, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003352 }
3353
3354 /**
Paul Jensen694f2b82015-06-17 14:15:39 -04003355 * Registers a PendingIntent to be sent when a network is available which satisfies the given
3356 * {@link NetworkRequest}.
3357 *
3358 * This function behaves identically to the version that takes a NetworkCallback, but instead
3359 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
3360 * the request may outlive the calling application and get called back when a suitable
3361 * network is found.
3362 * <p>
3363 * The operation is an Intent broadcast that goes to a broadcast receiver that
3364 * you registered with {@link Context#registerReceiver} or through the
3365 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3366 * <p>
3367 * The operation Intent is delivered with two extras, a {@link Network} typed
3368 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3369 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
3370 * the original requests parameters.
3371 * <p>
3372 * If there is already a request for this Intent registered (with the equality of
3373 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
3374 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
3375 * <p>
3376 * The request may be released normally by calling
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003377 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Paul Jensen694f2b82015-06-17 14:15:39 -04003378 * @param request {@link NetworkRequest} describing this request.
3379 * @param operation Action to perform when the network is available (corresponds
3380 * to the {@link NetworkCallback#onAvailable} call. Typically
3381 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
3382 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003383 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Paul Jensen694f2b82015-06-17 14:15:39 -04003384 public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003385 checkPendingIntentNotNull(operation);
Paul Jensen694f2b82015-06-17 14:15:39 -04003386 try {
3387 mService.pendingListenForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003388 } catch (RemoteException e) {
3389 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003390 } catch (ServiceSpecificException e) {
3391 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003392 }
Paul Jensen694f2b82015-06-17 14:15:39 -04003393 }
3394
3395 /**
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003396 * Registers to receive notifications about changes in the system default network. The callbacks
3397 * will continue to be called until either the application exits or
3398 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Erik Klinea2d29402016-03-16 15:31:39 +09003399 *
3400 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3401 * system default network changes.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003402 * The callback is invoked on the default internal Handler.
Erik Klinea2d29402016-03-16 15:31:39 +09003403 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003404 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Erik Klinea2d29402016-03-16 15:31:39 +09003405 public void registerDefaultNetworkCallback(NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003406 registerDefaultNetworkCallback(networkCallback, getDefaultHandler());
3407 }
3408
3409 /**
3410 * Registers to receive notifications about changes in the system default network. The callbacks
3411 * will continue to be called until either the application exits or
3412 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003413 *
3414 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3415 * system default network changes.
3416 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003417 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003418 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Hugo Benichi2583ef02017-02-02 17:02:36 +09003419 public void registerDefaultNetworkCallback(NetworkCallback networkCallback, Handler handler) {
Erik Klinea2d29402016-03-16 15:31:39 +09003420 // This works because if the NetworkCapabilities are null,
3421 // ConnectivityService takes them from the default request.
3422 //
3423 // Since the capabilities are exactly the same as the default request's
3424 // capabilities, this request is guaranteed, at all times, to be
3425 // satisfied by the same network, if any, that satisfies the default
3426 // request, i.e., the system default network.
Hugo Benichie7678512017-05-09 15:19:01 +09003427 NetworkCapabilities nullCapabilities = null;
Hugo Benichi2583ef02017-02-02 17:02:36 +09003428 CallbackHandler cbHandler = new CallbackHandler(handler);
Hugo Benichie7678512017-05-09 15:19:01 +09003429 sendRequestForNetwork(nullCapabilities, networkCallback, 0, REQUEST, TYPE_NONE, cbHandler);
Erik Klinea2d29402016-03-16 15:31:39 +09003430 }
3431
3432 /**
fengludb571472015-04-21 17:12:05 -07003433 * Requests bandwidth update for a given {@link Network} and returns whether the update request
3434 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
3435 * network connection for updated bandwidth information. The caller will be notified via
3436 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003437 * method assumes that the caller has previously called
3438 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network
3439 * changes.
fenglub15e72b2015-03-20 11:29:56 -07003440 *
fengluae519192015-04-27 14:28:04 -07003441 * @param network {@link Network} specifying which network you're interested.
fengludb571472015-04-21 17:12:05 -07003442 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglub15e72b2015-03-20 11:29:56 -07003443 */
fengludb571472015-04-21 17:12:05 -07003444 public boolean requestBandwidthUpdate(Network network) {
fenglub15e72b2015-03-20 11:29:56 -07003445 try {
fengludb571472015-04-21 17:12:05 -07003446 return mService.requestBandwidthUpdate(network);
fenglub15e72b2015-03-20 11:29:56 -07003447 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003448 throw e.rethrowFromSystemServer();
fenglub15e72b2015-03-20 11:29:56 -07003449 }
3450 }
3451
3452 /**
Hugo Benichidafed3d2017-03-06 09:17:06 +09003453 * Unregisters a {@code NetworkCallback} and possibly releases networks originating from
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003454 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and
3455 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls.
3456 * If the given {@code NetworkCallback} had previously been used with
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09003457 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
3458 * will be disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003459 *
Hugo Benichidafed3d2017-03-06 09:17:06 +09003460 * Notifications that would have triggered that {@code NetworkCallback} will immediately stop
3461 * triggering it as soon as this call returns.
3462 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003463 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003464 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003465 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
Hugo Benichie7678512017-05-09 15:19:01 +09003466 checkCallbackNotNull(networkCallback);
Hugo Benichidafed3d2017-03-06 09:17:06 +09003467 final List<NetworkRequest> reqs = new ArrayList<>();
3468 // Find all requests associated to this callback and stop callback triggers immediately.
3469 // Callback is reusable immediately. http://b/20701525, http://b/35921499.
3470 synchronized (sCallbacks) {
Hugo Benichi31c176d2017-06-17 13:14:12 +09003471 Preconditions.checkArgument(networkCallback.networkRequest != null,
3472 "NetworkCallback was not registered");
3473 Preconditions.checkArgument(networkCallback.networkRequest != ALREADY_UNREGISTERED,
3474 "NetworkCallback was already unregistered");
Hugo Benichidafed3d2017-03-06 09:17:06 +09003475 for (Map.Entry<NetworkRequest, NetworkCallback> e : sCallbacks.entrySet()) {
3476 if (e.getValue() == networkCallback) {
3477 reqs.add(e.getKey());
3478 }
3479 }
3480 // TODO: throw exception if callback was registered more than once (http://b/20701525).
3481 for (NetworkRequest r : reqs) {
3482 try {
3483 mService.releaseNetworkRequest(r);
3484 } catch (RemoteException e) {
3485 throw e.rethrowFromSystemServer();
3486 }
3487 // Only remove mapping if rpc was successful.
3488 sCallbacks.remove(r);
3489 }
Hugo Benichi31c176d2017-06-17 13:14:12 +09003490 networkCallback.networkRequest = ALREADY_UNREGISTERED;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003491 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003492 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003493
3494 /**
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003495 * Unregisters a callback previously registered via
3496 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3497 *
3498 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3499 * PendingIntent passed to
3500 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3501 * Cannot be null.
3502 */
3503 public void unregisterNetworkCallback(PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003504 checkPendingIntentNotNull(operation);
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003505 releaseNetworkRequest(operation);
3506 }
3507
3508 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003509 * Informs the system whether it should switch to {@code network} regardless of whether it is
3510 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
3511 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
3512 * the system default network regardless of any other network that's currently connected. If
3513 * {@code always} is true, then the choice is remembered, so that the next time the user
3514 * connects to this network, the system will switch to it.
3515 *
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003516 * @param network The network to accept.
3517 * @param accept Whether to accept the network even if unvalidated.
3518 * @param always Whether to remember this choice in the future.
3519 *
3520 * @hide
3521 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003522 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003523 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
3524 try {
3525 mService.setAcceptUnvalidated(network, accept, always);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003526 } catch (RemoteException e) {
3527 throw e.rethrowFromSystemServer();
3528 }
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003529 }
3530
3531 /**
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003532 * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is
3533 * only meaningful if the system is configured not to penalize such networks, e.g., if the
3534 * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code
3535 * NETWORK_AVOID_BAD_WIFI setting is unset}.
3536 *
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003537 * @param network The network to accept.
3538 *
3539 * @hide
3540 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003541 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003542 public void setAvoidUnvalidated(Network network) {
3543 try {
3544 mService.setAvoidUnvalidated(network);
3545 } catch (RemoteException e) {
3546 throw e.rethrowFromSystemServer();
3547 }
3548 }
3549
3550 /**
Lorenzo Colitti4734cdb2017-04-27 14:30:21 +09003551 * Requests that the system open the captive portal app on the specified network.
3552 *
3553 * @param network The network to log into.
3554 *
3555 * @hide
3556 */
3557 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
3558 public void startCaptivePortalApp(Network network) {
3559 try {
3560 mService.startCaptivePortalApp(network);
3561 } catch (RemoteException e) {
3562 throw e.rethrowFromSystemServer();
3563 }
3564 }
3565
3566 /**
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003567 * It is acceptable to briefly use multipath data to provide seamless connectivity for
3568 * time-sensitive user-facing operations when the system default network is temporarily
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003569 * unresponsive. The amount of data should be limited (less than one megabyte for every call to
3570 * this method), and the operation should be infrequent to ensure that data usage is limited.
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003571 *
3572 * An example of such an operation might be a time-sensitive foreground activity, such as a
3573 * voice command, that the user is performing while walking out of range of a Wi-Fi network.
3574 */
3575 public static final int MULTIPATH_PREFERENCE_HANDOVER = 1 << 0;
3576
3577 /**
3578 * It is acceptable to use small amounts of multipath data on an ongoing basis to provide
3579 * a backup channel for traffic that is primarily going over another network.
3580 *
3581 * An example might be maintaining backup connections to peers or servers for the purpose of
3582 * fast fallback if the default network is temporarily unresponsive or disconnects. The traffic
3583 * on backup paths should be negligible compared to the traffic on the main path.
3584 */
3585 public static final int MULTIPATH_PREFERENCE_RELIABILITY = 1 << 1;
3586
3587 /**
3588 * It is acceptable to use metered data to improve network latency and performance.
3589 */
3590 public static final int MULTIPATH_PREFERENCE_PERFORMANCE = 1 << 2;
3591
3592 /**
3593 * Return value to use for unmetered networks. On such networks we currently set all the flags
3594 * to true.
3595 * @hide
3596 */
3597 public static final int MULTIPATH_PREFERENCE_UNMETERED =
3598 MULTIPATH_PREFERENCE_HANDOVER |
3599 MULTIPATH_PREFERENCE_RELIABILITY |
3600 MULTIPATH_PREFERENCE_PERFORMANCE;
3601
3602 /** @hide */
3603 @Retention(RetentionPolicy.SOURCE)
3604 @IntDef(flag = true, value = {
3605 MULTIPATH_PREFERENCE_HANDOVER,
3606 MULTIPATH_PREFERENCE_RELIABILITY,
3607 MULTIPATH_PREFERENCE_PERFORMANCE,
3608 })
3609 public @interface MultipathPreference {
3610 }
3611
3612 /**
3613 * Provides a hint to the calling application on whether it is desirable to use the
3614 * multinetwork APIs (e.g., {@link Network#openConnection}, {@link Network#bindSocket}, etc.)
3615 * for multipath data transfer on this network when it is not the system default network.
3616 * Applications desiring to use multipath network protocols should call this method before
3617 * each such operation.
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003618 *
3619 * @param network The network on which the application desires to use multipath data.
3620 * If {@code null}, this method will return the a preference that will generally
3621 * apply to metered networks.
3622 * @return a bitwise OR of zero or more of the {@code MULTIPATH_PREFERENCE_*} constants.
3623 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003624 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003625 public @MultipathPreference int getMultipathPreference(Network network) {
3626 try {
3627 return mService.getMultipathPreference(network);
3628 } catch (RemoteException e) {
3629 throw e.rethrowFromSystemServer();
3630 }
3631 }
3632
3633 /**
Stuart Scott984dc852015-03-30 13:17:11 -07003634 * Resets all connectivity manager settings back to factory defaults.
3635 * @hide
3636 */
3637 public void factoryReset() {
Stuart Scott984dc852015-03-30 13:17:11 -07003638 try {
Stuart Scottf1fb3972015-04-02 18:00:02 -07003639 mService.factoryReset();
Stuart Scott984dc852015-03-30 13:17:11 -07003640 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003641 throw e.rethrowFromSystemServer();
Stuart Scott984dc852015-03-30 13:17:11 -07003642 }
3643 }
3644
3645 /**
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003646 * Binds the current process to {@code network}. All Sockets created in the future
3647 * (and not explicitly bound via a bound SocketFactory from
3648 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3649 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3650 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3651 * work and all host name resolutions will fail. This is by design so an application doesn't
3652 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3653 * To clear binding pass {@code null} for {@code network}. Using individually bound
3654 * Sockets created by Network.getSocketFactory().createSocket() and
3655 * performing network-specific host name resolutions via
3656 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen72db88e2015-03-10 10:54:12 -04003657 * {@code bindProcessToNetwork}.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003658 *
3659 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3660 * the current binding.
3661 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3662 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003663 public boolean bindProcessToNetwork(Network network) {
3664 // Forcing callers to call thru non-static function ensures ConnectivityManager
3665 // instantiated.
3666 return setProcessDefaultNetwork(network);
3667 }
3668
3669 /**
3670 * Binds the current process to {@code network}. All Sockets created in the future
3671 * (and not explicitly bound via a bound SocketFactory from
3672 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3673 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3674 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3675 * work and all host name resolutions will fail. This is by design so an application doesn't
3676 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3677 * To clear binding pass {@code null} for {@code network}. Using individually bound
3678 * Sockets created by Network.getSocketFactory().createSocket() and
3679 * performing network-specific host name resolutions via
3680 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
3681 * {@code setProcessDefaultNetwork}.
3682 *
3683 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3684 * the current binding.
3685 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3686 * @deprecated This function can throw {@link IllegalStateException}. Use
3687 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
3688 * is a direct replacement.
3689 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003690 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003691 public static boolean setProcessDefaultNetwork(Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003692 int netId = (network == null) ? NETID_UNSET : network.netId;
Paul Jensen72db88e2015-03-10 10:54:12 -04003693 if (netId == NetworkUtils.getBoundNetworkForProcess()) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003694 return true;
3695 }
3696 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05003697 // Set HTTP proxy system properties to match network.
3698 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colittiec4c5552015-04-22 11:52:48 +09003699 try {
3700 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
3701 } catch (SecurityException e) {
3702 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
3703 Log.e(TAG, "Can't set proxy properties", e);
3704 }
Paul Jensenc91b5342014-08-27 12:38:45 -04003705 // Must flush DNS cache as new network may have different DNS resolutions.
3706 InetAddress.clearDnsCache();
3707 // Must flush socket pool as idle sockets will be bound to previous network and may
3708 // cause subsequent fetches to be performed on old network.
3709 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
3710 return true;
3711 } else {
3712 return false;
3713 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003714 }
3715
3716 /**
3717 * Returns the {@link Network} currently bound to this process via
Paul Jensen72db88e2015-03-10 10:54:12 -04003718 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003719 *
3720 * @return {@code Network} to which this process is bound, or {@code null}.
3721 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003722 public Network getBoundNetworkForProcess() {
3723 // Forcing callers to call thru non-static function ensures ConnectivityManager
3724 // instantiated.
3725 return getProcessDefaultNetwork();
3726 }
3727
3728 /**
3729 * Returns the {@link Network} currently bound to this process via
3730 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
3731 *
3732 * @return {@code Network} to which this process is bound, or {@code null}.
3733 * @deprecated Using this function can lead to other functions throwing
3734 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
3735 * {@code getBoundNetworkForProcess} is a direct replacement.
3736 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003737 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003738 public static Network getProcessDefaultNetwork() {
Paul Jensen72db88e2015-03-10 10:54:12 -04003739 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04003740 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003741 return new Network(netId);
3742 }
3743
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003744 private void unsupportedStartingFrom(int version) {
3745 if (Process.myUid() == Process.SYSTEM_UID) {
3746 // The getApplicationInfo() call we make below is not supported in system context, and
3747 // we want to allow the system to use these APIs anyway.
3748 return;
3749 }
3750
3751 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
3752 throw new UnsupportedOperationException(
3753 "This method is not supported in target SDK version " + version + " and above");
3754 }
3755 }
3756
3757 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
3758 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
Lifu Tang30f95a72016-01-07 23:20:38 -08003759 // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003760 // remove these exemptions. Note that this check is not secure, and apps can still access these
3761 // functions by accessing ConnectivityService directly. However, it should be clear that doing
3762 // so is unsupported and may break in the future. http://b/22728205
3763 private void checkLegacyRoutingApiAccess() {
3764 if (mContext.checkCallingOrSelfPermission("com.android.permission.INJECT_OMADM_SETTINGS")
3765 == PackageManager.PERMISSION_GRANTED) {
3766 return;
3767 }
3768
Dianne Hackborn692a2442015-07-31 10:35:34 -07003769 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003770 }
3771
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003772 /**
3773 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen72db88e2015-03-10 10:54:12 -04003774 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003775 *
3776 * @param network The {@link Network} to bind host resolutions from the current process to, or
3777 * {@code null} to clear the current binding.
3778 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3779 * @hide
3780 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
3781 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003782 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003783 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04003784 return NetworkUtils.bindProcessToNetworkForHostResolution(
3785 network == null ? NETID_UNSET : network.netId);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003786 }
Felipe Leme1b103232016-01-22 09:44:57 -08003787
3788 /**
3789 * Device is not restricting metered network activity while application is running on
3790 * background.
3791 */
3792 public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1;
3793
3794 /**
3795 * Device is restricting metered network activity while application is running on background,
3796 * but application is allowed to bypass it.
3797 * <p>
3798 * In this state, application should take action to mitigate metered network access.
3799 * For example, a music streaming application should switch to a low-bandwidth bitrate.
3800 */
3801 public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2;
3802
3803 /**
3804 * Device is restricting metered network activity while application is running on background.
Felipe Leme9778f762016-01-27 14:46:39 -08003805 * <p>
Felipe Leme1b103232016-01-22 09:44:57 -08003806 * In this state, application should not try to use the network while running on background,
3807 * because it would be denied.
3808 */
3809 public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3;
3810
Felipe Leme9778f762016-01-27 14:46:39 -08003811 /**
3812 * A change in the background metered network activity restriction has occurred.
3813 * <p>
3814 * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction
3815 * applies to them.
3816 * <p>
3817 * This is only sent to registered receivers, not manifest receivers.
3818 */
3819 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
3820 public static final String ACTION_RESTRICT_BACKGROUND_CHANGED =
3821 "android.net.conn.RESTRICT_BACKGROUND_CHANGED";
3822
Felipe Lemeecfccea2016-01-25 11:48:04 -08003823 /** @hide */
3824 @Retention(RetentionPolicy.SOURCE)
Felipe Leme1b103232016-01-22 09:44:57 -08003825 @IntDef(flag = false, value = {
3826 RESTRICT_BACKGROUND_STATUS_DISABLED,
3827 RESTRICT_BACKGROUND_STATUS_WHITELISTED,
3828 RESTRICT_BACKGROUND_STATUS_ENABLED,
3829 })
Felipe Leme1b103232016-01-22 09:44:57 -08003830 public @interface RestrictBackgroundStatus {
3831 }
3832
3833 private INetworkPolicyManager getNetworkPolicyManager() {
3834 synchronized (this) {
3835 if (mNPManager != null) {
3836 return mNPManager;
3837 }
3838 mNPManager = INetworkPolicyManager.Stub.asInterface(ServiceManager
3839 .getService(Context.NETWORK_POLICY_SERVICE));
3840 return mNPManager;
3841 }
3842 }
3843
3844 /**
3845 * Determines if the calling application is subject to metered network restrictions while
3846 * running on background.
Felipe Lemec9c7be52016-05-16 13:57:19 -07003847 *
3848 * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED},
3849 * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED},
3850 * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED}
Felipe Leme1b103232016-01-22 09:44:57 -08003851 */
3852 public @RestrictBackgroundStatus int getRestrictBackgroundStatus() {
3853 try {
3854 return getNetworkPolicyManager().getRestrictBackgroundByCaller();
3855 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003856 throw e.rethrowFromSystemServer();
Felipe Leme1b103232016-01-22 09:44:57 -08003857 }
3858 }
Ricky Wai44dcbde2018-01-23 04:09:45 +00003859
3860 /**
3861 * The network watchlist is a list of domains and IP addresses that are associated with
Ricky Waia86d5d52018-03-20 14:20:54 +00003862 * potentially harmful apps. This method returns the SHA-256 of the watchlist config file
3863 * currently used by the system for validation purposes.
Ricky Wai44dcbde2018-01-23 04:09:45 +00003864 *
3865 * @return Hash of network watchlist config file. Null if config does not exist.
3866 */
3867 public byte[] getNetworkWatchlistConfigHash() {
3868 try {
3869 return mService.getNetworkWatchlistConfigHash();
3870 } catch (RemoteException e) {
3871 Log.e(TAG, "Unable to get watchlist config hash");
3872 throw e.rethrowFromSystemServer();
3873 }
3874 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003875}