blob: e2627fd3f04cb5560ff24c325cade0416431f325 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016package android.net;
17
Felipe Leme1b103232016-01-22 09:44:57 -080018import android.annotation.IntDef;
Robin Lee244ce8e2016-01-05 18:03:46 +000019import android.annotation.Nullable;
Jeff Sharkey30e06bb2017-04-24 11:18:03 -060020import android.annotation.RequiresPermission;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.annotation.SdkConstant;
22import android.annotation.SdkConstant.SdkConstantType;
Udam Sainib7c24872016-01-04 12:16:14 -080023import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060024import android.annotation.SystemService;
Mathew Inwoodfa3a7462018-08-08 14:52:47 +010025import android.annotation.UnsupportedAppUsage;
Robert Greenwalt9258c642014-03-26 16:47:06 -070026import android.app.PendingIntent;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070027import android.content.Context;
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070028import android.content.Intent;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +090029import android.content.pm.PackageManager;
Robert Greenwalt42acef32009-08-12 16:08:25 -070030import android.os.Binder;
Jeff Sharkey3a844fc2011-08-16 14:37:57 -070031import android.os.Build.VERSION_CODES;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080032import android.os.Bundle;
Robert Greenwalt9258c642014-03-26 16:47:06 -070033import android.os.Handler;
34import android.os.HandlerThread;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080035import android.os.IBinder;
36import android.os.INetworkActivityListener;
37import android.os.INetworkManagementService;
Robert Greenwalt9258c642014-03-26 16:47:06 -070038import android.os.Looper;
39import android.os.Message;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -070040import android.os.Messenger;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +090041import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042import android.os.RemoteException;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080043import android.os.ResultReceiver;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080044import android.os.ServiceManager;
Hugo Benichicb883232017-05-11 13:16:17 +090045import android.os.ServiceSpecificException;
Jeff Sharkey961e3042011-08-29 16:02:57 -070046import android.provider.Settings;
Wink Saville36ffb042014-12-05 11:10:30 -080047import android.telephony.SubscriptionManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080048import android.util.ArrayMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -070049import android.util.Log;
Erik Kline35bf06c2017-01-26 18:08:28 +090050import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051
Robert Greenwaltafa05c02014-05-21 20:04:36 -070052import com.android.internal.telephony.ITelephony;
Robert Greenwalt562cc542014-05-15 18:07:26 -070053import com.android.internal.telephony.PhoneConstants;
Hugo Benichidafed3d2017-03-06 09:17:06 +090054import com.android.internal.util.Preconditions;
55import com.android.internal.util.Protocol;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070056
Paul Jensenc91b5342014-08-27 12:38:45 -040057import libcore.net.event.NetworkEventDispatcher;
58
Felipe Leme1b103232016-01-22 09:44:57 -080059import java.lang.annotation.Retention;
60import java.lang.annotation.RetentionPolicy;
Jeremy Kleind42209d2015-12-28 15:11:58 -080061import java.net.InetAddress;
Jeff Vander Stoep0ac2c092018-07-23 10:57:53 -070062import java.net.InetSocketAddress;
Hugo Benichidafed3d2017-03-06 09:17:06 +090063import java.util.ArrayList;
Jeremy Kleind42209d2015-12-28 15:11:58 -080064import java.util.HashMap;
Hugo Benichidafed3d2017-03-06 09:17:06 +090065import java.util.List;
66import java.util.Map;
Jeremy Kleind42209d2015-12-28 15:11:58 -080067
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068/**
69 * Class that answers queries about the state of network connectivity. It also
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060070 * notifies applications when network connectivity changes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 * <p>
72 * The primary responsibilities of this class are to:
73 * <ol>
74 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
75 * <li>Send broadcast intents when network connectivity changes</li>
76 * <li>Attempt to "fail over" to another network when connectivity to a network
77 * is lost</li>
78 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
79 * state of the available networks</li>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070080 * <li>Provide an API that allows applications to request and select networks for their data
81 * traffic</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080082 * </ol>
83 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060084@SystemService(Context.CONNECTIVITY_SERVICE)
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070085public class ConnectivityManager {
86 private static final String TAG = "ConnectivityManager";
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +090087 private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070088
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070090 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 * been established or lost. The NetworkInfo for the affected network is
92 * sent as an extra; it should be consulted to see what kind of
93 * connectivity event occurred.
94 * <p/>
Mark Lu33ec1062016-12-05 10:57:55 -080095 * Apps targeting Android 7.0 (API level 24) and higher do not receive this
96 * broadcast if they declare the broadcast receiver in their manifest. Apps
97 * will still receive broadcasts if they register their
98 * {@link android.content.BroadcastReceiver} with
99 * {@link android.content.Context#registerReceiver Context.registerReceiver()}
100 * and that context is still valid.
101 * <p/>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 * If this is a connection that was the result of failing over from a
103 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
104 * set to true.
105 * <p/>
106 * For a loss of connectivity, if the connectivity manager is attempting
107 * to connect (or has already connected) to another network, the
108 * NetworkInfo for the new network is also passed as an extra. This lets
109 * any receivers of the broadcast know that they should not necessarily
110 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800111 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800112 * the failover attempt succeeded (and so there is still overall data
113 * connectivity), or that the failover attempt failed, meaning that all
114 * connectivity has been lost.
115 * <p/>
116 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
117 * is set to {@code true} if there are no connected networks at all.
Chalard Jean054cd162018-02-10 05:33:50 +0900118 *
119 * @deprecated apps should use the more versatile {@link #requestNetwork},
120 * {@link #registerNetworkCallback} or {@link #registerDefaultNetworkCallback}
121 * functions instead for faster and more detailed updates about the network
122 * changes they care about.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800124 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jean054cd162018-02-10 05:33:50 +0900125 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800126 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700127
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800128 /**
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700129 * A temporary hack until SUPL system can get off the legacy APIS.
130 * They do too many network requests and the long list of apps listening
Chalard Jean4d660112018-06-04 16:52:49 +0900131 * and waking due to the CONNECTIVITY_ACTION broadcast makes it expensive.
132 * Use this broadcast intent instead for SUPL requests.
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700133 * @hide
134 */
135 public static final String CONNECTIVITY_ACTION_SUPL =
136 "android.net.conn.CONNECTIVITY_CHANGE_SUPL";
137
138 /**
Paul Jensen25a217c2015-02-27 22:55:47 -0500139 * The device has connected to a network that has presented a captive
140 * portal, which is blocking Internet connectivity. The user was presented
141 * with a notification that network sign in is required,
142 * and the user invoked the notification's action indicating they
Paul Jensen49e3edf2015-05-22 10:50:39 -0400143 * desire to sign in to the network. Apps handling this activity should
Paul Jensen25a217c2015-02-27 22:55:47 -0500144 * facilitate signing in to the network. This action includes a
145 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents
146 * the network presenting the captive portal; all communication with the
147 * captive portal must be done using this {@code Network} object.
148 * <p/>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400149 * This activity includes a {@link CaptivePortal} extra named
150 * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different
151 * outcomes of the captive portal sign in to the system:
152 * <ul>
153 * <li> When the app handling this action believes the user has signed in to
154 * the network and the captive portal has been dismissed, the app should
155 * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can
156 * reevaluate the network. If reevaluation finds the network no longer
157 * subject to a captive portal, the network may become the default active
Chalard Jean4d660112018-06-04 16:52:49 +0900158 * data network.</li>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400159 * <li> When the app handling this action believes the user explicitly wants
Paul Jensen25a217c2015-02-27 22:55:47 -0500160 * to ignore the captive portal and the network, the app should call
Paul Jensen49e3edf2015-05-22 10:50:39 -0400161 * {@link CaptivePortal#ignoreNetwork}. </li>
162 * </ul>
Paul Jensen25a217c2015-02-27 22:55:47 -0500163 */
164 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
165 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL";
166
167 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 * The lookup key for a {@link NetworkInfo} object. Retrieve with
169 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700170 *
171 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
172 * should always obtain network information through
Paul Jensen3541e9f2015-03-18 12:23:02 -0400173 * {@link #getActiveNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700174 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700176 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700178
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800179 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700180 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700181 *
182 * @see android.content.Intent#getIntExtra(String, int)
183 */
184 public static final String EXTRA_NETWORK_TYPE = "networkType";
185
186 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800187 * The lookup key for a boolean that indicates whether a connect event
188 * is for a network to which the connectivity manager was failing over
189 * following a disconnect on another network.
190 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
191 */
192 public static final String EXTRA_IS_FAILOVER = "isFailover";
193 /**
194 * The lookup key for a {@link NetworkInfo} object. This is supplied when
195 * there is another network that it may be possible to connect to. Retrieve with
196 * {@link android.content.Intent#getParcelableExtra(String)}.
197 */
198 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
199 /**
200 * The lookup key for a boolean that indicates whether there is a
201 * complete lack of connectivity, i.e., no network is available.
202 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
203 */
204 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
205 /**
206 * The lookup key for a string that indicates why an attempt to connect
207 * to a network failed. The string has no particular structure. It is
208 * intended to be used in notifications presented to users. Retrieve
209 * it with {@link android.content.Intent#getStringExtra(String)}.
210 */
211 public static final String EXTRA_REASON = "reason";
212 /**
213 * The lookup key for a string that provides optionally supplied
214 * extra information about the network state. The information
215 * may be passed up from the lower networking layers, and its
216 * meaning may be specific to a particular network type. Retrieve
217 * it with {@link android.content.Intent#getStringExtra(String)}.
218 */
219 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700220 /**
221 * The lookup key for an int that provides information about
222 * our connection to the internet at large. 0 indicates no connection,
223 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700224 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700225 * {@hide}
226 */
227 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 /**
Paul Jensen49e3edf2015-05-22 10:50:39 -0400229 * The lookup key for a {@link CaptivePortal} object included with the
230 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal}
231 * object can be used to either indicate to the system that the captive
232 * portal has been dismissed or that the user does not want to pursue
233 * signing in to captive portal. Retrieve it with
234 * {@link android.content.Intent#getParcelableExtra(String)}.
Paul Jensen25a217c2015-02-27 22:55:47 -0500235 */
Paul Jensen49e3edf2015-05-22 10:50:39 -0400236 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL";
Jan Nordqvist52eb29f2015-09-22 15:54:32 -0700237
238 /**
239 * Key for passing a URL to the captive portal login activity.
240 */
241 public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL";
242
Paul Jensen25a217c2015-02-27 22:55:47 -0500243 /**
Remi NGUYEN VAN8255c2d2018-05-22 10:01:53 +0900244 * Key for passing a {@link android.net.captiveportal.CaptivePortalProbeSpec} to the captive
245 * portal login activity.
246 * {@hide}
247 */
248 public static final String EXTRA_CAPTIVE_PORTAL_PROBE_SPEC =
249 "android.net.extra.CAPTIVE_PORTAL_PROBE_SPEC";
250
251 /**
Hugo Benichicdf3ba42016-12-14 08:23:40 +0900252 * Key for passing a user agent string to the captive portal login activity.
253 * {@hide}
254 */
255 public static final String EXTRA_CAPTIVE_PORTAL_USER_AGENT =
256 "android.net.extra.CAPTIVE_PORTAL_USER_AGENT";
257
258 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700259 * Broadcast action to indicate the change of data activity status
260 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800261 * The network becomes active when data transmission is started, or
262 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700263 * {@hide}
264 */
265 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jean4d660112018-06-04 16:52:49 +0900266 public static final String ACTION_DATA_ACTIVITY_CHANGE =
267 "android.net.conn.DATA_ACTIVITY_CHANGE";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700268 /**
269 * The lookup key for an enum that indicates the network device type on which this data activity
270 * change happens.
271 * {@hide}
272 */
273 public static final String EXTRA_DEVICE_TYPE = "deviceType";
274 /**
275 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
276 * it is actively sending or receiving data and {@code false} means it is idle.
277 * {@hide}
278 */
279 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700280 /**
281 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
282 * {@hide}
283 */
284 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700285
286 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800287 * Broadcast Action: The setting for background data usage has changed
288 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
289 * <p>
290 * If an application uses the network in the background, it should listen
291 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700292 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800293 * <p>
294 *
295 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
296 * of background data depends on several combined factors, and
297 * this broadcast is no longer sent. Instead, when background
298 * data is unavailable, {@link #getActiveNetworkInfo()} will now
299 * appear disconnected. During first boot after a platform
300 * upgrade, this broadcast will be sent once if
301 * {@link #getBackgroundDataSetting()} was {@code false} before
302 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 */
304 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800305 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800306 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
307 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
308
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700309 /**
310 * Broadcast Action: The network connection may not be good
311 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
312 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
313 * the network and it's condition.
314 * @hide
315 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800316 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100317 @UnsupportedAppUsage
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700318 public static final String INET_CONDITION_ACTION =
319 "android.net.conn.INET_CONDITION_ACTION";
320
Robert Greenwalt42acef32009-08-12 16:08:25 -0700321 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800322 * Broadcast Action: A tetherable connection has come or gone.
323 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
Erik Kline8351faa2017-04-17 16:47:23 +0900324 * {@code ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY},
325 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER}, and
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800326 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
327 * the current state of tethering. Each include a list of
328 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800329 * @hide
330 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800331 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100332 @UnsupportedAppUsage
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800333 public static final String ACTION_TETHER_STATE_CHANGED =
334 "android.net.conn.TETHER_STATE_CHANGED";
335
336 /**
337 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800338 * gives a String[] listing all the interfaces configured for
339 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800340 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100341 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800342 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800343
344 /**
345 * @hide
Erik Kline8351faa2017-04-17 16:47:23 +0900346 * gives a String[] listing all the interfaces currently in local-only
347 * mode (ie, has DHCPv4+IPv6-ULA support and no packet forwarding)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800348 */
Erik Kline8351faa2017-04-17 16:47:23 +0900349 public static final String EXTRA_ACTIVE_LOCAL_ONLY = "localOnlyArray";
350
351 /**
352 * @hide
353 * gives a String[] listing all the interfaces currently tethered
354 * (ie, has DHCPv4 support and packets potentially forwarded/NATed)
355 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100356 @UnsupportedAppUsage
Erik Kline8351faa2017-04-17 16:47:23 +0900357 public static final String EXTRA_ACTIVE_TETHER = "tetherArray";
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800358
359 /**
360 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800361 * gives a String[] listing all the interfaces we tried to tether and
362 * failed. Use {@link #getLastTetherError} to find the error code
363 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800364 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100365 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800366 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800367
368 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800369 * Broadcast Action: The captive portal tracker has finished its test.
370 * Sent only while running Setup Wizard, in lieu of showing a user
371 * notification.
372 * @hide
373 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800374 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800375 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
376 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
377 /**
378 * The lookup key for a boolean that indicates whether a captive portal was detected.
379 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
380 * @hide
381 */
382 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
383
384 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900385 * Action used to display a dialog that asks the user whether to connect to a network that is
386 * not validated. This intent is used to start the dialog in settings via startActivity.
387 *
388 * @hide
389 */
390 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED";
391
392 /**
Lorenzo Colitti9be58c52016-09-15 14:02:29 +0900393 * Action used to display a dialog that asks the user whether to avoid a network that is no
394 * longer validated. This intent is used to start the dialog in settings via startActivity.
395 *
396 * @hide
397 */
398 public static final String ACTION_PROMPT_LOST_VALIDATION =
399 "android.net.conn.PROMPT_LOST_VALIDATION";
400
401 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800402 * Invalid tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900403 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800404 * @hide
405 */
406 public static final int TETHERING_INVALID = -1;
407
408 /**
409 * Wifi tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900410 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800411 * @hide
412 */
413 @SystemApi
414 public static final int TETHERING_WIFI = 0;
415
416 /**
417 * USB tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900418 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800419 * @hide
420 */
421 @SystemApi
422 public static final int TETHERING_USB = 1;
423
424 /**
425 * Bluetooth tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900426 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800427 * @hide
428 */
429 @SystemApi
430 public static final int TETHERING_BLUETOOTH = 2;
431
432 /**
433 * Extra used for communicating with the TetherService. Includes the type of tethering to
434 * enable if any.
435 * @hide
436 */
437 public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType";
438
439 /**
440 * Extra used for communicating with the TetherService. Includes the type of tethering for
441 * which to cancel provisioning.
442 * @hide
443 */
444 public static final String EXTRA_REM_TETHER_TYPE = "extraRemTetherType";
445
446 /**
447 * Extra used for communicating with the TetherService. True to schedule a recheck of tether
448 * provisioning.
449 * @hide
450 */
451 public static final String EXTRA_SET_ALARM = "extraSetAlarm";
452
453 /**
454 * Tells the TetherService to run a provision check now.
455 * @hide
456 */
457 public static final String EXTRA_RUN_PROVISION = "extraRunProvision";
458
459 /**
460 * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver}
461 * which will receive provisioning results. Can be left empty.
462 * @hide
463 */
464 public static final String EXTRA_PROVISION_CALLBACK = "extraProvisionCallback";
465
466 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800467 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700468 * @hide
469 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100470 @UnsupportedAppUsage
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700471 public static final int TYPE_NONE = -1;
472
473 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900474 * A Mobile data connection. Devices may support more than one.
475 *
476 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
477 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
478 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700479 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900480 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700481 public static final int TYPE_MOBILE = 0;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900482
Robert Greenwalt42acef32009-08-12 16:08:25 -0700483 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900484 * A WIFI data connection. Devices may support more than one.
485 *
486 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
487 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
488 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700489 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900490 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700491 public static final int TYPE_WIFI = 1;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900492
Robert Greenwalt42acef32009-08-12 16:08:25 -0700493 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800494 * An MMS-specific Mobile data connection. This network type may use the
495 * same network interface as {@link #TYPE_MOBILE} or it may use a different
496 * one. This is used by applications needing to talk to the carrier's
497 * Multimedia Messaging Service servers.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900498 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900499 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900500 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900501 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700502 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700503 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700504 public static final int TYPE_MOBILE_MMS = 2;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900505
Robert Greenwalt42acef32009-08-12 16:08:25 -0700506 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800507 * A SUPL-specific Mobile data connection. This network type may use the
508 * same network interface as {@link #TYPE_MOBILE} or it may use a different
509 * one. This is used by applications needing to talk to the carrier's
510 * Secure User Plane Location servers for help locating the device.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900511 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900512 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900513 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900514 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700515 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700516 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700517 public static final int TYPE_MOBILE_SUPL = 3;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900518
Robert Greenwalt42acef32009-08-12 16:08:25 -0700519 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800520 * A DUN-specific Mobile data connection. This network type may use the
521 * same network interface as {@link #TYPE_MOBILE} or it may use a different
522 * one. This is sometimes by the system when setting up an upstream connection
523 * for tethering so that the carrier is aware of DUN traffic.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900524 *
525 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
526 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
527 * provides the {@link NetworkCapabilities#NET_CAPABILITY_DUN} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700528 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900529 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700530 public static final int TYPE_MOBILE_DUN = 4;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900531
Robert Greenwalt42acef32009-08-12 16:08:25 -0700532 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800533 * A High Priority Mobile data connection. This network type uses the
534 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colittie285b432015-04-23 15:32:42 +0900535 * is different.
536 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900537 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
538 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
539 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700540 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700541 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700542 public static final int TYPE_MOBILE_HIPRI = 5;
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900543
jsh8214deb2010-03-11 15:04:43 -0800544 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900545 * A WiMAX data connection.
546 *
547 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
548 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
549 * appropriate network. {@see NetworkCapabilities} for supported transports.
jsh8214deb2010-03-11 15:04:43 -0800550 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900551 @Deprecated
jsh8214deb2010-03-11 15:04:43 -0800552 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800553
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800554 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900555 * A Bluetooth data connection.
556 *
557 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
558 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
559 * appropriate network. {@see NetworkCapabilities} for supported transports.
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800560 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900561 @Deprecated
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800562 public static final int TYPE_BLUETOOTH = 7;
563
Robert Greenwalt60810842011-04-22 15:28:18 -0700564 /**
565 * Dummy data connection. This should not be used on shipping devices.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900566 * @deprecated This is not used any more.
Robert Greenwalt60810842011-04-22 15:28:18 -0700567 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900568 @Deprecated
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800569 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800570
Robert Greenwalt60810842011-04-22 15:28:18 -0700571 /**
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900572 * An Ethernet data connection.
573 *
574 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
575 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
576 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt60810842011-04-22 15:28:18 -0700577 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900578 @Deprecated
Robert Greenwalte12aec92011-01-28 14:48:37 -0800579 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700580
Wink Saville9d7d6282011-03-12 14:52:01 -0800581 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800582 * Over the air Administration.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900583 * @deprecated Use {@link NetworkCapabilities} instead.
Wink Saville9d7d6282011-03-12 14:52:01 -0800584 * {@hide}
585 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900586 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100587 @UnsupportedAppUsage
Wink Saville9d7d6282011-03-12 14:52:01 -0800588 public static final int TYPE_MOBILE_FOTA = 10;
589
590 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800591 * IP Multimedia Subsystem.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900592 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IMS} instead.
Wink Saville9d7d6282011-03-12 14:52:01 -0800593 * {@hide}
594 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900595 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100596 @UnsupportedAppUsage
Wink Saville9d7d6282011-03-12 14:52:01 -0800597 public static final int TYPE_MOBILE_IMS = 11;
598
599 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800600 * Carrier Branded Services.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900601 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_CBS} instead.
Wink Saville9d7d6282011-03-12 14:52:01 -0800602 * {@hide}
603 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900604 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100605 @UnsupportedAppUsage
Wink Saville9d7d6282011-03-12 14:52:01 -0800606 public static final int TYPE_MOBILE_CBS = 12;
607
repo syncaea743a2011-07-29 23:55:49 -0700608 /**
609 * A Wi-Fi p2p connection. Only requesting processes will have access to
610 * the peers connected.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900611 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_WIFI_P2P} instead.
repo syncaea743a2011-07-29 23:55:49 -0700612 * {@hide}
613 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900614 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100615 @UnsupportedAppUsage
repo syncaea743a2011-07-29 23:55:49 -0700616 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800617
Wink Saville5e56bc52013-07-29 15:00:57 -0700618 /**
619 * The network to use for initially attaching to the network
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900620 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IA} instead.
Wink Saville5e56bc52013-07-29 15:00:57 -0700621 * {@hide}
622 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900623 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100624 @UnsupportedAppUsage
Wink Saville5e56bc52013-07-29 15:00:57 -0700625 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700626
Lorenzo Colittie285b432015-04-23 15:32:42 +0900627 /**
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700628 * Emergency PDN connection for emergency services. This
629 * may include IMS and MMS in emergency situations.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900630 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_EIMS} instead.
Ram3e0e3bc2014-06-26 11:03:44 -0700631 * {@hide}
632 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900633 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100634 @UnsupportedAppUsage
Ram3e0e3bc2014-06-26 11:03:44 -0700635 public static final int TYPE_MOBILE_EMERGENCY = 15;
636
Hui Lu1c5624a2014-01-15 11:05:36 -0500637 /**
638 * The network that uses proxy to achieve connectivity.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900639 * @deprecated Use {@link NetworkCapabilities} instead.
Hui Lu1c5624a2014-01-15 11:05:36 -0500640 * {@hide}
641 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900642 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100643 @UnsupportedAppUsage
Hui Lu1c5624a2014-01-15 11:05:36 -0500644 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700645
Robert Greenwalt8283f882014-07-07 17:09:01 -0700646 /**
647 * A virtual network using one or more native bearers.
648 * It may or may not be providing security services.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900649 * @deprecated Applications should use {@link NetworkCapabilities#TRANSPORT_VPN} instead.
Robert Greenwalt8283f882014-07-07 17:09:01 -0700650 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900651 @Deprecated
Robert Greenwalt8283f882014-07-07 17:09:01 -0700652 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500653
654 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700655 public static final int MAX_RADIO_TYPE = TYPE_VPN;
656
657 /** {@hide} */
658 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800659
Hugo Benichi16f0a942017-06-20 14:07:59 +0900660 private static final int MIN_NETWORK_TYPE = TYPE_MOBILE;
661
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800662 /**
663 * If you want to set the default network preference,you can directly
664 * change the networkAttributes array in framework's config.xml.
665 *
666 * @deprecated Since we support so many more networks now, the single
667 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800668 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800669 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800670 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800671 * from an App.
672 */
673 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800674 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
675
Jeff Sharkey625239a2012-09-26 22:03:49 -0700676 /**
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700677 * @hide
678 */
Hugo Benichia5c1f7f2017-06-20 14:10:14 +0900679 public static final int REQUEST_ID_UNSET = 0;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700680
Paul Jensen5d59e782014-07-11 12:28:19 -0400681 /**
Hugo Benichi31c176d2017-06-17 13:14:12 +0900682 * Static unique request used as a tombstone for NetworkCallbacks that have been unregistered.
683 * This allows to distinguish when unregistering NetworkCallbacks those that were never
Chalard Jean4d660112018-06-04 16:52:49 +0900684 * registered from those that were already unregistered.
Hugo Benichi31c176d2017-06-17 13:14:12 +0900685 * @hide
686 */
Hugo Benichia5c1f7f2017-06-20 14:10:14 +0900687 private static final NetworkRequest ALREADY_UNREGISTERED =
Hugo Benichi31c176d2017-06-17 13:14:12 +0900688 new NetworkRequest.Builder().clearCapabilities().build();
689
690 /**
Paul Jensen5d59e782014-07-11 12:28:19 -0400691 * A NetID indicating no Network is selected.
692 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
693 * @hide
694 */
695 public static final int NETID_UNSET = 0;
696
Erik Kline4d092232017-10-30 15:29:44 +0900697 /**
698 * Private DNS Mode values.
699 *
700 * The "private_dns_mode" global setting stores a String value which is
701 * expected to be one of the following.
702 */
703
704 /**
705 * @hide
706 */
707 public static final String PRIVATE_DNS_MODE_OFF = "off";
708 /**
709 * @hide
710 */
711 public static final String PRIVATE_DNS_MODE_OPPORTUNISTIC = "opportunistic";
712 /**
713 * @hide
714 */
715 public static final String PRIVATE_DNS_MODE_PROVIDER_HOSTNAME = "hostname";
716 /**
717 * The default Private DNS mode.
718 *
719 * This may change from release to release or may become dependent upon
720 * the capabilities of the underlying platform.
721 *
722 * @hide
723 */
Erik Kline19841792018-05-16 16:41:57 +0900724 public static final String PRIVATE_DNS_DEFAULT_MODE_FALLBACK = PRIVATE_DNS_MODE_OPPORTUNISTIC;
Erik Kline4d092232017-10-30 15:29:44 +0900725
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100726 @UnsupportedAppUsage
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700727 private final IConnectivityManager mService;
Paul Jensene0bef712014-12-10 15:12:18 -0500728 /**
729 * A kludge to facilitate static access where a Context pointer isn't available, like in the
730 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
731 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
732 * methods that take a Context argument.
733 */
734 private static ConnectivityManager sInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800735
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900736 private final Context mContext;
737
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800738 private INetworkManagementService mNMService;
Felipe Leme1b103232016-01-22 09:44:57 -0800739 private INetworkPolicyManager mNPManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800740
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800741 /**
742 * Tests if a given integer represents a valid network type.
743 * @param networkType the type to be tested
744 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensen9e59e122015-05-06 10:42:25 -0400745 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
746 * validate a network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800747 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700748 @Deprecated
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700749 public static boolean isNetworkTypeValid(int networkType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900750 return MIN_NETWORK_TYPE <= networkType && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 }
752
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800753 /**
754 * Returns a non-localized string representing a given network type.
755 * ONLY used for debugging output.
756 * @param type the type needing naming
757 * @return a String for the given type, or a string version of the type ("87")
758 * if no name is known.
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900759 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800760 * {@hide}
761 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900762 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100763 @UnsupportedAppUsage
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700764 public static String getNetworkTypeName(int type) {
765 switch (type) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900766 case TYPE_NONE:
767 return "NONE";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700768 case TYPE_MOBILE:
769 return "MOBILE";
770 case TYPE_WIFI:
771 return "WIFI";
772 case TYPE_MOBILE_MMS:
773 return "MOBILE_MMS";
774 case TYPE_MOBILE_SUPL:
775 return "MOBILE_SUPL";
776 case TYPE_MOBILE_DUN:
777 return "MOBILE_DUN";
778 case TYPE_MOBILE_HIPRI:
779 return "MOBILE_HIPRI";
780 case TYPE_WIMAX:
781 return "WIMAX";
782 case TYPE_BLUETOOTH:
783 return "BLUETOOTH";
784 case TYPE_DUMMY:
785 return "DUMMY";
786 case TYPE_ETHERNET:
787 return "ETHERNET";
788 case TYPE_MOBILE_FOTA:
789 return "MOBILE_FOTA";
790 case TYPE_MOBILE_IMS:
791 return "MOBILE_IMS";
792 case TYPE_MOBILE_CBS:
793 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700794 case TYPE_WIFI_P2P:
795 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700796 case TYPE_MOBILE_IA:
797 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700798 case TYPE_MOBILE_EMERGENCY:
799 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500800 case TYPE_PROXY:
801 return "PROXY";
Erik Kline37fbfa12014-11-19 17:23:41 +0900802 case TYPE_VPN:
803 return "VPN";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700804 default:
805 return Integer.toString(type);
806 }
807 }
808
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800809 /**
810 * Checks if a given type uses the cellular data connection.
811 * This should be replaced in the future by a network property.
812 * @param networkType the type to check
813 * @return a boolean - {@code true} if uses cellular network, else {@code false}
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900814 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800815 * {@hide}
816 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900817 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +0100818 @UnsupportedAppUsage
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700819 public static boolean isNetworkTypeMobile(int networkType) {
820 switch (networkType) {
821 case TYPE_MOBILE:
822 case TYPE_MOBILE_MMS:
823 case TYPE_MOBILE_SUPL:
824 case TYPE_MOBILE_DUN:
825 case TYPE_MOBILE_HIPRI:
826 case TYPE_MOBILE_FOTA:
827 case TYPE_MOBILE_IMS:
828 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700829 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700830 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700831 return true;
832 default:
833 return false;
834 }
835 }
836
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800837 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700838 * Checks if the given network type is backed by a Wi-Fi radio.
839 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900840 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700841 * @hide
842 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +0900843 @Deprecated
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700844 public static boolean isNetworkTypeWifi(int networkType) {
845 switch (networkType) {
846 case TYPE_WIFI:
847 case TYPE_WIFI_P2P:
848 return true;
849 default:
850 return false;
851 }
852 }
853
854 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800855 * Specifies the preferred network type. When the device has more
856 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800857 *
858 * @param preference the network type to prefer over all others. It is
859 * unspecified what happens to the old preferred network in the
860 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700861 * @deprecated Functionality has been removed as it no longer makes sense,
862 * with many more than two networks - we'd need an array to express
863 * preference. Instead we use dynamic network properties of
864 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800865 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700866 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800868 }
869
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800870 /**
871 * Retrieves the current preferred network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800872 *
873 * @return an integer representing the preferred network type
874 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700875 * @deprecated Functionality has been removed as it no longer makes sense,
876 * with many more than two networks - we'd need an array to express
877 * preference. Instead we use dynamic network properties of
878 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800879 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700880 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600881 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700883 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800884 }
885
Scott Main671644c2011-10-06 19:02:28 -0700886 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800887 * Returns details about the currently active default data network. When
888 * connected, this network is the default route for outgoing connections.
889 * You should always check {@link NetworkInfo#isConnected()} before initiating
890 * network traffic. This may return {@code null} when there is no default
891 * network.
Chalard Jean5a041d12018-03-29 17:45:24 +0900892 * Note that if the default network is a VPN, this method will return the
893 * NetworkInfo for one of its underlying networks instead, or null if the
894 * VPN agent did not specify any. Apps interested in learning about VPNs
895 * should use {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800896 *
897 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500898 * or {@code null} if no default network is currently active
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700899 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600900 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 public NetworkInfo getActiveNetworkInfo() {
902 try {
903 return mService.getActiveNetworkInfo();
904 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700905 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800906 }
907 }
908
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800909 /**
Paul Jensen31a94f42015-02-13 14:18:39 -0500910 * Returns a {@link Network} object corresponding to the currently active
911 * default data network. In the event that the current active default data
912 * network disconnects, the returned {@code Network} object will no longer
913 * be usable. This will return {@code null} when there is no default
914 * network.
915 *
916 * @return a {@link Network} object for the current default network or
917 * {@code null} if no default network is currently active
Paul Jensen31a94f42015-02-13 14:18:39 -0500918 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600919 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Paul Jensen31a94f42015-02-13 14:18:39 -0500920 public Network getActiveNetwork() {
921 try {
922 return mService.getActiveNetwork();
923 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700924 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -0500925 }
926 }
927
928 /**
Robin Leed2baf792016-03-24 12:07:00 +0000929 * Returns a {@link Network} object corresponding to the currently active
930 * default data network for a specific UID. In the event that the default data
931 * network disconnects, the returned {@code Network} object will no longer
932 * be usable. This will return {@code null} when there is no default
933 * network for the UID.
Robin Leed2baf792016-03-24 12:07:00 +0000934 *
935 * @return a {@link Network} object for the current default network for the
936 * given UID or {@code null} if no default network is currently active
937 *
938 * @hide
939 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600940 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Robin Leed2baf792016-03-24 12:07:00 +0000941 public Network getActiveNetworkForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600942 return getActiveNetworkForUid(uid, false);
943 }
944
945 /** {@hide} */
946 public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) {
Robin Leed2baf792016-03-24 12:07:00 +0000947 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600948 return mService.getActiveNetworkForUid(uid, ignoreBlocked);
Robin Leed2baf792016-03-24 12:07:00 +0000949 } catch (RemoteException e) {
950 throw e.rethrowFromSystemServer();
951 }
952 }
953
954 /**
Charles He36738632017-05-15 17:07:18 +0100955 * Checks if a VPN app supports always-on mode.
956 *
957 * In order to support the always-on feature, an app has to
958 * <ul>
959 * <li>target {@link VERSION_CODES#N API 24} or above, and
Charles Hec57a01c2017-08-15 15:30:22 +0100960 * <li>not opt out through the {@link VpnService#SERVICE_META_DATA_SUPPORTS_ALWAYS_ON}
961 * meta-data field.
Charles He36738632017-05-15 17:07:18 +0100962 * </ul>
963 *
964 * @param userId The identifier of the user for whom the VPN app is installed.
965 * @param vpnPackage The canonical package name of the VPN app.
966 * @return {@code true} if and only if the VPN app exists and supports always-on mode.
967 * @hide
968 */
969 public boolean isAlwaysOnVpnPackageSupportedForUser(int userId, @Nullable String vpnPackage) {
970 try {
971 return mService.isAlwaysOnVpnPackageSupported(userId, vpnPackage);
972 } catch (RemoteException e) {
973 throw e.rethrowFromSystemServer();
974 }
975 }
976
977 /**
Robin Lee244ce8e2016-01-05 18:03:46 +0000978 * Configures an always-on VPN connection through a specific application.
979 * This connection is automatically granted and persisted after a reboot.
980 *
981 * <p>The designated package should declare a {@link VpnService} in its
982 * manifest guarded by {@link android.Manifest.permission.BIND_VPN_SERVICE},
983 * otherwise the call will fail.
984 *
985 * @param userId The identifier of the user to set an always-on VPN for.
986 * @param vpnPackage The package name for an installed VPN app on the device, or {@code null}
987 * to remove an existing always-on VPN configuration.
Robin Leedc679712016-05-03 13:23:03 +0100988 * @param lockdownEnabled {@code true} to disallow networking when the VPN is not connected or
989 * {@code false} otherwise.
Robin Lee244ce8e2016-01-05 18:03:46 +0000990 * @return {@code true} if the package is set as always-on VPN controller;
991 * {@code false} otherwise.
992 * @hide
993 */
Robin Leedc679712016-05-03 13:23:03 +0100994 public boolean setAlwaysOnVpnPackageForUser(int userId, @Nullable String vpnPackage,
995 boolean lockdownEnabled) {
Robin Lee244ce8e2016-01-05 18:03:46 +0000996 try {
Robin Leedc679712016-05-03 13:23:03 +0100997 return mService.setAlwaysOnVpnPackage(userId, vpnPackage, lockdownEnabled);
Robin Lee244ce8e2016-01-05 18:03:46 +0000998 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700999 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +00001000 }
1001 }
1002
1003 /**
1004 * Returns the package name of the currently set always-on VPN application.
1005 * If there is no always-on VPN set, or the VPN is provided by the system instead
1006 * of by an app, {@code null} will be returned.
1007 *
1008 * @return Package name of VPN controller responsible for always-on VPN,
1009 * or {@code null} if none is set.
1010 * @hide
1011 */
1012 public String getAlwaysOnVpnPackageForUser(int userId) {
1013 try {
1014 return mService.getAlwaysOnVpnPackage(userId);
1015 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001016 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +00001017 }
1018 }
1019
1020 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001021 * Returns details about the currently active default data network
1022 * for a given uid. This is for internal use only to avoid spying
1023 * other apps.
1024 *
1025 * @return a {@link NetworkInfo} object for the current default network
1026 * for the given uid or {@code null} if no default network is
1027 * available for the specified uid.
1028 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001029 * {@hide}
1030 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001031 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001032 @UnsupportedAppUsage
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001033 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001034 return getActiveNetworkInfoForUid(uid, false);
1035 }
1036
1037 /** {@hide} */
1038 public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001039 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001040 return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001041 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001042 throw e.rethrowFromSystemServer();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001043 }
1044 }
1045
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001046 /**
1047 * Returns connection status information about a particular
1048 * network type.
1049 *
1050 * @param networkType integer specifying which networkType in
1051 * which you're interested.
1052 * @return a {@link NetworkInfo} object for the requested
1053 * network type or {@code null} if the type is not
Chalard Jean5a041d12018-03-29 17:45:24 +09001054 * supported by the device. If {@code networkType} is
1055 * TYPE_VPN and a VPN is active for the calling app,
1056 * then this method will try to return one of the
1057 * underlying networks for the VPN or null if the
1058 * VPN agent didn't specify any.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001059 *
Paul Jensen3541e9f2015-03-18 12:23:02 -04001060 * @deprecated This method does not support multiple connected networks
1061 * of the same type. Use {@link #getAllNetworks} and
1062 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001063 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001064 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001065 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001066 public NetworkInfo getNetworkInfo(int networkType) {
1067 try {
1068 return mService.getNetworkInfo(networkType);
1069 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001070 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001071 }
1072 }
1073
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001074 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001075 * Returns connection status information about a particular
1076 * Network.
1077 *
1078 * @param network {@link Network} specifying which network
1079 * in which you're interested.
1080 * @return a {@link NetworkInfo} object for the requested
1081 * network or {@code null} if the {@code Network}
1082 * is not valid.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001083 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001084 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001085 public NetworkInfo getNetworkInfo(Network network) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001086 return getNetworkInfoForUid(network, Process.myUid(), false);
1087 }
1088
1089 /** {@hide} */
1090 public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001091 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001092 return mService.getNetworkInfoForUid(network, uid, ignoreBlocked);
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001093 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001094 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001095 }
1096 }
1097
1098 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001099 * Returns connection status information about all network
1100 * types supported by the device.
1101 *
1102 * @return an array of {@link NetworkInfo} objects. Check each
1103 * {@link NetworkInfo#getType} for which type each applies.
1104 *
Paul Jensen3541e9f2015-03-18 12:23:02 -04001105 * @deprecated This method does not support multiple connected networks
1106 * of the same type. Use {@link #getAllNetworks} and
1107 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001108 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001109 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001110 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001111 public NetworkInfo[] getAllNetworkInfo() {
1112 try {
1113 return mService.getAllNetworkInfo();
1114 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001115 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001116 }
1117 }
1118
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001119 /**
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001120 * Returns the {@link Network} object currently serving a given type, or
1121 * null if the given type is not connected.
1122 *
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001123 * @hide
Paul Jensen3541e9f2015-03-18 12:23:02 -04001124 * @deprecated This method does not support multiple connected networks
1125 * of the same type. Use {@link #getAllNetworks} and
1126 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001127 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001128 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001129 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001130 @UnsupportedAppUsage
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001131 public Network getNetworkForType(int networkType) {
1132 try {
1133 return mService.getNetworkForType(networkType);
1134 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001135 throw e.rethrowFromSystemServer();
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001136 }
1137 }
1138
1139 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001140 * Returns an array of all {@link Network} currently tracked by the
1141 * framework.
Paul Jensenb2748922015-05-06 11:10:18 -04001142 *
1143 * @return an array of {@link Network} objects.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001144 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001145 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001146 public Network[] getAllNetworks() {
1147 try {
1148 return mService.getAllNetworks();
1149 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001150 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001151 }
1152 }
1153
1154 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001155 * Returns an array of {@link android.net.NetworkCapabilities} objects, representing
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001156 * the Networks that applications run by the given user will use by default.
1157 * @hide
1158 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001159 @UnsupportedAppUsage
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001160 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
1161 try {
1162 return mService.getDefaultNetworkCapabilitiesForUser(userId);
1163 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001164 throw e.rethrowFromSystemServer();
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001165 }
1166 }
1167
1168 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001169 * Returns the IP information for the current default network.
1170 *
1171 * @return a {@link LinkProperties} object describing the IP info
1172 * for the current default network, or {@code null} if there
1173 * is no current default network.
1174 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001175 * {@hide}
1176 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001177 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001178 @UnsupportedAppUsage
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001179 public LinkProperties getActiveLinkProperties() {
1180 try {
1181 return mService.getActiveLinkProperties();
1182 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001183 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001184 }
1185 }
1186
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001187 /**
1188 * Returns the IP information for a given network type.
1189 *
1190 * @param networkType the network type of interest.
1191 * @return a {@link LinkProperties} object describing the IP info
1192 * for the given networkType, or {@code null} if there is
1193 * no current default network.
1194 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001195 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04001196 * @deprecated This method does not support multiple connected networks
1197 * of the same type. Use {@link #getAllNetworks},
1198 * {@link #getNetworkInfo(android.net.Network)}, and
1199 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001200 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001201 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001202 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001203 @UnsupportedAppUsage
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001204 public LinkProperties getLinkProperties(int networkType) {
1205 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001206 return mService.getLinkPropertiesForType(networkType);
1207 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001208 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001209 }
1210 }
1211
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001212 /**
1213 * Get the {@link LinkProperties} for the given {@link Network}. This
1214 * will return {@code null} if the network is unknown.
1215 *
1216 * @param network The {@link Network} object identifying the network in question.
1217 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensenb2748922015-05-06 11:10:18 -04001218 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001219 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt9258c642014-03-26 16:47:06 -07001220 public LinkProperties getLinkProperties(Network network) {
1221 try {
1222 return mService.getLinkProperties(network);
1223 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001224 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001225 }
1226 }
1227
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001228 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001229 * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001230 * will return {@code null} if the network is unknown.
1231 *
1232 * @param network The {@link Network} object identifying the network in question.
Lorenzo Colittie285b432015-04-23 15:32:42 +09001233 * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001234 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001235 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt9258c642014-03-26 16:47:06 -07001236 public NetworkCapabilities getNetworkCapabilities(Network network) {
1237 try {
1238 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001239 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001240 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001241 }
1242 }
1243
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001244 /**
Udam Sainib7c24872016-01-04 12:16:14 -08001245 * Gets the URL that should be used for resolving whether a captive portal is present.
1246 * 1. This URL should respond with a 204 response to a GET request to indicate no captive
1247 * portal is present.
1248 * 2. This URL must be HTTP as redirect responses are used to find captive portal
1249 * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects.
1250 *
1251 * @hide
1252 */
1253 @SystemApi
Udam Saini0e94c362017-06-07 12:06:28 -07001254 @RequiresPermission(android.Manifest.permission.LOCAL_MAC_ADDRESS)
Udam Sainib7c24872016-01-04 12:16:14 -08001255 public String getCaptivePortalServerUrl() {
1256 try {
1257 return mService.getCaptivePortalServerUrl();
1258 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001259 throw e.rethrowFromSystemServer();
Udam Sainib7c24872016-01-04 12:16:14 -08001260 }
1261 }
1262
1263 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 * Tells the underlying networking system that the caller wants to
1265 * begin using the named feature. The interpretation of {@code feature}
1266 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001267 *
1268 * <p>This method requires the caller to hold either the
1269 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1270 * or the ability to modify system settings as determined by
1271 * {@link android.provider.Settings.System#canWrite}.</p>
1272 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001273 * @param networkType specifies which network the request pertains to
1274 * @param feature the name of the feature to be used
1275 * @return an integer value representing the outcome of the request.
1276 * The interpretation of this value is specific to each networking
1277 * implementation+feature combination, except that the value {@code -1}
1278 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001279 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001280 * @deprecated Deprecated in favor of the cleaner
1281 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001282 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001283 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001284 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001286 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001288 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001289 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1290 if (netCap == null) {
1291 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
1292 feature);
1293 return PhoneConstants.APN_REQUEST_FAILED;
1294 }
1295
1296 NetworkRequest request = null;
1297 synchronized (sLegacyRequests) {
1298 LegacyRequest l = sLegacyRequests.get(netCap);
1299 if (l != null) {
1300 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
1301 renewRequestLocked(l);
1302 if (l.currentNetwork != null) {
1303 return PhoneConstants.APN_ALREADY_ACTIVE;
1304 } else {
1305 return PhoneConstants.APN_REQUEST_STARTED;
1306 }
1307 }
1308
1309 request = requestNetworkForFeatureLocked(netCap);
1310 }
1311 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -07001312 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001313 return PhoneConstants.APN_REQUEST_STARTED;
1314 } else {
1315 Log.d(TAG, " request Failed");
1316 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001317 }
1318 }
1319
1320 /**
1321 * Tells the underlying networking system that the caller is finished
1322 * using the named feature. The interpretation of {@code feature}
1323 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001324 *
1325 * <p>This method requires the caller to hold either the
1326 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1327 * or the ability to modify system settings as determined by
1328 * {@link android.provider.Settings.System#canWrite}.</p>
1329 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001330 * @param networkType specifies which network the request pertains to
1331 * @param feature the name of the feature that is no longer needed
1332 * @return an integer value representing the outcome of the request.
1333 * The interpretation of this value is specific to each networking
1334 * implementation+feature combination, except that the value {@code -1}
1335 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001336 *
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09001337 * @deprecated Deprecated in favor of the cleaner
1338 * {@link #unregisterNetworkCallback(NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001339 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001340 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001341 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001343 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001344 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001345 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001346 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1347 if (netCap == null) {
1348 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
1349 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001350 return -1;
1351 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001352
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001353 if (removeRequestForFeature(netCap)) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001354 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001355 }
1356 return 1;
1357 }
1358
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001359 @UnsupportedAppUsage
Robert Greenwalt562cc542014-05-15 18:07:26 -07001360 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1361 if (networkType == TYPE_MOBILE) {
Erik Kline35bf06c2017-01-26 18:08:28 +09001362 switch (feature) {
1363 case "enableCBS":
1364 return networkCapabilitiesForType(TYPE_MOBILE_CBS);
1365 case "enableDUN":
1366 case "enableDUNAlways":
1367 return networkCapabilitiesForType(TYPE_MOBILE_DUN);
1368 case "enableFOTA":
1369 return networkCapabilitiesForType(TYPE_MOBILE_FOTA);
1370 case "enableHIPRI":
1371 return networkCapabilitiesForType(TYPE_MOBILE_HIPRI);
1372 case "enableIMS":
1373 return networkCapabilitiesForType(TYPE_MOBILE_IMS);
1374 case "enableMMS":
1375 return networkCapabilitiesForType(TYPE_MOBILE_MMS);
1376 case "enableSUPL":
1377 return networkCapabilitiesForType(TYPE_MOBILE_SUPL);
1378 default:
1379 return null;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001380 }
Erik Kline35bf06c2017-01-26 18:08:28 +09001381 } else if (networkType == TYPE_WIFI && "p2p".equals(feature)) {
1382 return networkCapabilitiesForType(TYPE_WIFI_P2P);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001383 }
1384 return null;
1385 }
1386
Robert Greenwalt06314e42014-10-29 14:04:06 -07001387 /**
1388 * Guess what the network request was trying to say so that the resulting
1389 * network is accessible via the legacy (deprecated) API such as
1390 * requestRouteToHost.
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001391 *
1392 * This means we should try to be fairly precise about transport and
Robert Greenwalt06314e42014-10-29 14:04:06 -07001393 * capability but ignore things such as networkSpecifier.
1394 * If the request has more than one transport or capability it doesn't
1395 * match the old legacy requests (they selected only single transport/capability)
1396 * so this function cannot map the request to a single legacy type and
1397 * the resulting network will not be available to the legacy APIs.
1398 *
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001399 * This code is only called from the requestNetwork API (L and above).
1400 *
1401 * Setting a legacy type causes CONNECTIVITY_ACTION broadcasts, which are expensive
1402 * because they wake up lots of apps - see http://b/23350688 . So we currently only
1403 * do this for SUPL requests, which are the only ones that we know need it. If
1404 * omitting these broadcasts causes unacceptable app breakage, then for backwards
1405 * compatibility we can send them:
1406 *
1407 * if (targetSdkVersion < Build.VERSION_CODES.M) && // legacy API unsupported >= M
1408 * targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP)) // requestNetwork not present < L
1409 *
Robert Greenwalt06314e42014-10-29 14:04:06 -07001410 * TODO - This should be removed when the legacy APIs are removed.
1411 */
Ye Wenb87875e2014-07-21 14:19:01 -07001412 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1413 if (netCap == null) {
1414 return TYPE_NONE;
1415 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001416
Ye Wenb87875e2014-07-21 14:19:01 -07001417 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1418 return TYPE_NONE;
1419 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001420
Lifu Tang30f95a72016-01-07 23:20:38 -08001421 // Do this only for SUPL, until GnssLocationProvider is fixed. http://b/25876485 .
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001422 if (!netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1423 // NOTE: if this causes app breakage, we should not just comment out this early return;
1424 // instead, we should make this early return conditional on the requesting app's target
1425 // SDK version, as described in the comment above.
1426 return TYPE_NONE;
1427 }
1428
Robert Greenwalt06314e42014-10-29 14:04:06 -07001429 String type = null;
1430 int result = TYPE_NONE;
1431
Ye Wenb87875e2014-07-21 14:19:01 -07001432 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
Robert Greenwalt06314e42014-10-29 14:04:06 -07001433 type = "enableCBS";
1434 result = TYPE_MOBILE_CBS;
1435 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1436 type = "enableIMS";
1437 result = TYPE_MOBILE_IMS;
1438 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1439 type = "enableFOTA";
1440 result = TYPE_MOBILE_FOTA;
1441 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1442 type = "enableDUN";
1443 result = TYPE_MOBILE_DUN;
1444 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001445 type = "enableSUPL";
Robert Greenwalt06314e42014-10-29 14:04:06 -07001446 result = TYPE_MOBILE_SUPL;
Robert Greenwalt74ab4fa2015-08-28 12:37:54 -07001447 // back out this hack for mms as they no longer need this and it's causing
1448 // device slowdowns - b/23350688 (note, supl still needs this)
1449 //} else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1450 // type = "enableMMS";
1451 // result = TYPE_MOBILE_MMS;
Robert Greenwalt06314e42014-10-29 14:04:06 -07001452 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1453 type = "enableHIPRI";
1454 result = TYPE_MOBILE_HIPRI;
Ye Wenb87875e2014-07-21 14:19:01 -07001455 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001456 if (type != null) {
1457 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1458 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1459 return result;
Ye Wenb87875e2014-07-21 14:19:01 -07001460 }
1461 }
1462 return TYPE_NONE;
1463 }
1464
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001465 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001466 if (netCap == null) return TYPE_NONE;
1467 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1468 return TYPE_MOBILE_CBS;
1469 }
1470 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1471 return TYPE_MOBILE_IMS;
1472 }
1473 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1474 return TYPE_MOBILE_FOTA;
1475 }
1476 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1477 return TYPE_MOBILE_DUN;
1478 }
1479 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1480 return TYPE_MOBILE_SUPL;
1481 }
1482 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1483 return TYPE_MOBILE_MMS;
1484 }
1485 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1486 return TYPE_MOBILE_HIPRI;
1487 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001488 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1489 return TYPE_WIFI_P2P;
1490 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001491 return TYPE_NONE;
1492 }
1493
1494 private static class LegacyRequest {
1495 NetworkCapabilities networkCapabilities;
1496 NetworkRequest networkRequest;
1497 int expireSequenceNumber;
1498 Network currentNetwork;
1499 int delay = -1;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001500
1501 private void clearDnsBinding() {
1502 if (currentNetwork != null) {
1503 currentNetwork = null;
1504 setProcessDefaultNetworkForHostResolution(null);
1505 }
1506 }
1507
Robert Greenwalt6078b502014-06-11 16:05:07 -07001508 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001509 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001510 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001511 currentNetwork = network;
1512 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001513 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001514 }
1515 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001516 public void onLost(Network network) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001517 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001518 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1519 }
1520 };
1521 }
1522
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001523 @UnsupportedAppUsage
Chalard Jean4d660112018-06-04 16:52:49 +09001524 private static final HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
1525 new HashMap<>();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001526
1527 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1528 synchronized (sLegacyRequests) {
1529 LegacyRequest l = sLegacyRequests.get(netCap);
1530 if (l != null) return l.networkRequest;
1531 }
1532 return null;
1533 }
1534
1535 private void renewRequestLocked(LegacyRequest l) {
1536 l.expireSequenceNumber++;
1537 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1538 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1539 }
1540
1541 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1542 int ourSeqNum = -1;
1543 synchronized (sLegacyRequests) {
1544 LegacyRequest l = sLegacyRequests.get(netCap);
1545 if (l == null) return;
1546 ourSeqNum = l.expireSequenceNumber;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001547 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001548 }
1549 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1550 }
1551
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001552 @UnsupportedAppUsage
Robert Greenwalt562cc542014-05-15 18:07:26 -07001553 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1554 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001555 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001556 try {
1557 delay = mService.getRestoreDefaultNetworkDelay(type);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001558 } catch (RemoteException e) {
1559 throw e.rethrowFromSystemServer();
1560 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001561 LegacyRequest l = new LegacyRequest();
1562 l.networkCapabilities = netCap;
1563 l.delay = delay;
1564 l.expireSequenceNumber = 0;
Hugo Benichi2583ef02017-02-02 17:02:36 +09001565 l.networkRequest = sendRequestForNetwork(
1566 netCap, l.networkCallback, 0, REQUEST, type, getDefaultHandler());
Robert Greenwalt562cc542014-05-15 18:07:26 -07001567 if (l.networkRequest == null) return null;
1568 sLegacyRequests.put(netCap, l);
1569 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1570 return l.networkRequest;
1571 }
1572
1573 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1574 if (delay >= 0) {
1575 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
Hugo Benichi2583ef02017-02-02 17:02:36 +09001576 CallbackHandler handler = getDefaultHandler();
Hugo Benichi6f260f32017-02-03 14:18:44 +09001577 Message msg = handler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1578 handler.sendMessageDelayed(msg, delay);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001579 }
1580 }
1581
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001582 @UnsupportedAppUsage
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001583 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1584 final LegacyRequest l;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001585 synchronized (sLegacyRequests) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001586 l = sLegacyRequests.remove(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001587 }
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001588 if (l == null) return false;
1589 unregisterNetworkCallback(l.networkCallback);
1590 l.clearDnsBinding();
1591 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001592 }
1593
Erik Kline35bf06c2017-01-26 18:08:28 +09001594 private static final SparseIntArray sLegacyTypeToTransport = new SparseIntArray();
1595 static {
1596 sLegacyTypeToTransport.put(TYPE_MOBILE, NetworkCapabilities.TRANSPORT_CELLULAR);
1597 sLegacyTypeToTransport.put(TYPE_MOBILE_CBS, NetworkCapabilities.TRANSPORT_CELLULAR);
1598 sLegacyTypeToTransport.put(TYPE_MOBILE_DUN, NetworkCapabilities.TRANSPORT_CELLULAR);
1599 sLegacyTypeToTransport.put(TYPE_MOBILE_FOTA, NetworkCapabilities.TRANSPORT_CELLULAR);
1600 sLegacyTypeToTransport.put(TYPE_MOBILE_HIPRI, NetworkCapabilities.TRANSPORT_CELLULAR);
1601 sLegacyTypeToTransport.put(TYPE_MOBILE_IMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1602 sLegacyTypeToTransport.put(TYPE_MOBILE_MMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1603 sLegacyTypeToTransport.put(TYPE_MOBILE_SUPL, NetworkCapabilities.TRANSPORT_CELLULAR);
1604 sLegacyTypeToTransport.put(TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI);
1605 sLegacyTypeToTransport.put(TYPE_WIFI_P2P, NetworkCapabilities.TRANSPORT_WIFI);
1606 sLegacyTypeToTransport.put(TYPE_BLUETOOTH, NetworkCapabilities.TRANSPORT_BLUETOOTH);
1607 sLegacyTypeToTransport.put(TYPE_ETHERNET, NetworkCapabilities.TRANSPORT_ETHERNET);
1608 }
1609
1610 private static final SparseIntArray sLegacyTypeToCapability = new SparseIntArray();
1611 static {
1612 sLegacyTypeToCapability.put(TYPE_MOBILE_CBS, NetworkCapabilities.NET_CAPABILITY_CBS);
1613 sLegacyTypeToCapability.put(TYPE_MOBILE_DUN, NetworkCapabilities.NET_CAPABILITY_DUN);
1614 sLegacyTypeToCapability.put(TYPE_MOBILE_FOTA, NetworkCapabilities.NET_CAPABILITY_FOTA);
1615 sLegacyTypeToCapability.put(TYPE_MOBILE_IMS, NetworkCapabilities.NET_CAPABILITY_IMS);
1616 sLegacyTypeToCapability.put(TYPE_MOBILE_MMS, NetworkCapabilities.NET_CAPABILITY_MMS);
1617 sLegacyTypeToCapability.put(TYPE_MOBILE_SUPL, NetworkCapabilities.NET_CAPABILITY_SUPL);
1618 sLegacyTypeToCapability.put(TYPE_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
1619 }
1620
1621 /**
1622 * Given a legacy type (TYPE_WIFI, ...) returns a NetworkCapabilities
1623 * instance suitable for registering a request or callback. Throws an
1624 * IllegalArgumentException if no mapping from the legacy type to
1625 * NetworkCapabilities is known.
1626 *
Chalard Jean6b1da6e2018-03-08 13:54:53 +09001627 * @deprecated Types are deprecated. Use {@link NetworkCallback} or {@link NetworkRequest}
1628 * to find the network instead.
Erik Kline35bf06c2017-01-26 18:08:28 +09001629 * @hide
1630 */
1631 public static NetworkCapabilities networkCapabilitiesForType(int type) {
1632 final NetworkCapabilities nc = new NetworkCapabilities();
1633
1634 // Map from type to transports.
1635 final int NOT_FOUND = -1;
1636 final int transport = sLegacyTypeToTransport.get(type, NOT_FOUND);
Hugo Benichie7678512017-05-09 15:19:01 +09001637 Preconditions.checkArgument(transport != NOT_FOUND, "unknown legacy type: " + type);
Erik Kline35bf06c2017-01-26 18:08:28 +09001638 nc.addTransportType(transport);
1639
1640 // Map from type to capabilities.
1641 nc.addCapability(sLegacyTypeToCapability.get(
1642 type, NetworkCapabilities.NET_CAPABILITY_INTERNET));
1643 nc.maybeMarkCapabilitiesRestricted();
1644 return nc;
1645 }
1646
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001647 /** @hide */
1648 public static class PacketKeepaliveCallback {
1649 /** The requested keepalive was successfully started. */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001650 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001651 public void onStarted() {}
1652 /** The keepalive was successfully stopped. */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001653 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001654 public void onStopped() {}
1655 /** An error occurred. */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001656 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001657 public void onError(int error) {}
1658 }
1659
1660 /**
1661 * Allows applications to request that the system periodically send specific packets on their
1662 * behalf, using hardware offload to save battery power.
1663 *
1664 * To request that the system send keepalives, call one of the methods that return a
1665 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1666 * passing in a non-null callback. If the callback is successfully started, the callback's
1667 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1668 * specifying one of the {@code ERROR_*} constants in this class.
1669 *
Chalard Jean4d660112018-06-04 16:52:49 +09001670 * To stop an existing keepalive, call {@link PacketKeepalive#stop}. The system will call
1671 * {@link PacketKeepaliveCallback#onStopped} if the operation was successful or
1672 * {@link PacketKeepaliveCallback#onError} if an error occurred.
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001673 *
1674 * @hide
1675 */
1676 public class PacketKeepalive {
1677
1678 private static final String TAG = "PacketKeepalive";
1679
1680 /** @hide */
1681 public static final int SUCCESS = 0;
1682
1683 /** @hide */
1684 public static final int NO_KEEPALIVE = -1;
1685
1686 /** @hide */
1687 public static final int BINDER_DIED = -10;
1688
1689 /** The specified {@code Network} is not connected. */
1690 public static final int ERROR_INVALID_NETWORK = -20;
1691 /** The specified IP addresses are invalid. For example, the specified source IP address is
1692 * not configured on the specified {@code Network}. */
1693 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1694 /** The requested port is invalid. */
1695 public static final int ERROR_INVALID_PORT = -22;
1696 /** The packet length is invalid (e.g., too long). */
1697 public static final int ERROR_INVALID_LENGTH = -23;
1698 /** The packet transmission interval is invalid (e.g., too short). */
1699 public static final int ERROR_INVALID_INTERVAL = -24;
1700
1701 /** The hardware does not support this request. */
1702 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti9d1284e2015-09-08 16:46:36 +09001703 /** The hardware returned an error. */
1704 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001705
Nathan Harold63dd8132018-02-14 13:09:45 -08001706 /** The NAT-T destination port for IPsec */
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001707 public static final int NATT_PORT = 4500;
1708
Nathan Harold63dd8132018-02-14 13:09:45 -08001709 /** The minimum interval in seconds between keepalive packet transmissions */
1710 public static final int MIN_INTERVAL = 10;
1711
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001712 private final Network mNetwork;
1713 private final PacketKeepaliveCallback mCallback;
1714 private final Looper mLooper;
1715 private final Messenger mMessenger;
1716
1717 private volatile Integer mSlot;
1718
1719 void stopLooper() {
1720 mLooper.quit();
1721 }
1722
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001723 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001724 public void stop() {
1725 try {
1726 mService.stopKeepalive(mNetwork, mSlot);
1727 } catch (RemoteException e) {
1728 Log.e(TAG, "Error stopping packet keepalive: ", e);
1729 stopLooper();
1730 }
1731 }
1732
1733 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09001734 Preconditions.checkNotNull(network, "network cannot be null");
1735 Preconditions.checkNotNull(callback, "callback cannot be null");
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001736 mNetwork = network;
1737 mCallback = callback;
1738 HandlerThread thread = new HandlerThread(TAG);
1739 thread.start();
1740 mLooper = thread.getLooper();
1741 mMessenger = new Messenger(new Handler(mLooper) {
1742 @Override
1743 public void handleMessage(Message message) {
1744 switch (message.what) {
1745 case NetworkAgent.EVENT_PACKET_KEEPALIVE:
1746 int error = message.arg2;
1747 try {
1748 if (error == SUCCESS) {
1749 if (mSlot == null) {
1750 mSlot = message.arg1;
1751 mCallback.onStarted();
1752 } else {
1753 mSlot = null;
1754 stopLooper();
1755 mCallback.onStopped();
1756 }
1757 } else {
1758 stopLooper();
1759 mCallback.onError(error);
1760 }
1761 } catch (Exception e) {
1762 Log.e(TAG, "Exception in keepalive callback(" + error + ")", e);
1763 }
1764 break;
1765 default:
1766 Log.e(TAG, "Unhandled message " + Integer.toHexString(message.what));
1767 break;
1768 }
1769 }
1770 });
1771 }
1772 }
1773
1774 /**
1775 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
1776 *
1777 * @hide
1778 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001779 @UnsupportedAppUsage
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001780 public PacketKeepalive startNattKeepalive(
1781 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
1782 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
1783 final PacketKeepalive k = new PacketKeepalive(network, callback);
1784 try {
1785 mService.startNattKeepalive(network, intervalSeconds, k.mMessenger, new Binder(),
1786 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
1787 } catch (RemoteException e) {
1788 Log.e(TAG, "Error starting packet keepalive: ", e);
1789 k.stopLooper();
1790 return null;
1791 }
1792 return k;
1793 }
1794
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001795 /**
1796 * Ensure that a network route exists to deliver traffic to the specified
1797 * host via the specified network interface. An attempt to add a route that
1798 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001799 *
1800 * <p>This method requires the caller to hold either the
1801 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1802 * or the ability to modify system settings as determined by
1803 * {@link android.provider.Settings.System#canWrite}.</p>
1804 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001805 * @param networkType the type of the network over which traffic to the specified
1806 * host is to be routed
1807 * @param hostAddress the IP address of the host to which the route is desired
1808 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001809 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001810 * @deprecated Deprecated in favor of the
1811 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
1812 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001813 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001814 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001815 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001816 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001817 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001818 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001819 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001820 }
1821
1822 /**
1823 * Ensure that a network route exists to deliver traffic to the specified
1824 * host via the specified network interface. An attempt to add a route that
1825 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001826 *
1827 * <p>This method requires the caller to hold either the
1828 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1829 * or the ability to modify system settings as determined by
1830 * {@link android.provider.Settings.System#canWrite}.</p>
1831 *
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001832 * @param networkType the type of the network over which traffic to the specified
1833 * host is to be routed
1834 * @param hostAddress the IP address of the host to which the route is desired
1835 * @return {@code true} on success, {@code false} on failure
1836 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001837 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001838 * {@link #bindProcessToNetwork} API.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001839 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001840 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001841 @UnsupportedAppUsage
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001842 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001843 checkLegacyRoutingApiAccess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001844 try {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001845 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001847 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001848 }
1849 }
1850
1851 /**
1852 * Returns the value of the setting for background data usage. If false,
1853 * applications should not use the network if the application is not in the
1854 * foreground. Developers should respect this setting, and check the value
1855 * of this before performing any background data operations.
1856 * <p>
1857 * All applications that have background services that use the network
1858 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001859 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001860 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001861 * background data depends on several combined factors, and this method will
1862 * always return {@code true}. Instead, when background data is unavailable,
1863 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001864 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001865 * @return Whether background data usage is allowed.
1866 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001867 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001868 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001869 // assume that background data is allowed; final authority is
1870 // NetworkInfo which may be blocked.
1871 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001872 }
1873
1874 /**
1875 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001876 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001877 * @param allowBackgroundData Whether an application should use data while
1878 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001879 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001880 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1881 * @see #getBackgroundDataSetting()
1882 * @hide
1883 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001884 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001885 @UnsupportedAppUsage
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001886 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001887 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001888 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001889
Jeff Sharkey43d2a172017-07-12 10:50:42 -06001890 /** {@hide} */
1891 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001892 @UnsupportedAppUsage
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001893 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1894 try {
1895 return mService.getActiveNetworkQuotaInfo();
1896 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001897 throw e.rethrowFromSystemServer();
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001898 }
1899 }
1900
1901 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001902 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001903 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001904 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001905 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01001906 @UnsupportedAppUsage
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001907 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001908 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1909 if (b != null) {
1910 try {
1911 ITelephony it = ITelephony.Stub.asInterface(b);
Shishir Agrawal7ea3e8b2016-01-25 13:03:07 -08001912 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Wink Saville36ffb042014-12-05 11:10:30 -08001913 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
Malcolm Chenb455e722017-11-28 15:57:14 -08001914 boolean retVal = it.isUserDataEnabled(subId);
Wink Saville36ffb042014-12-05 11:10:30 -08001915 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1916 + " retVal=" + retVal);
1917 return retVal;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001918 } catch (RemoteException e) {
1919 throw e.rethrowFromSystemServer();
1920 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001921 }
Wink Saville36ffb042014-12-05 11:10:30 -08001922 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001923 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001924 }
1925
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001926 /**
Robert Greenwaltb2489872014-09-04 16:44:35 -07001927 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt6078b502014-06-11 16:05:07 -07001928 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001929 */
1930 public interface OnNetworkActiveListener {
1931 /**
1932 * Called on the main thread of the process to report that the current data network
1933 * has become active, and it is now a good time to perform any pending network
1934 * operations. Note that this listener only tells you when the network becomes
1935 * active; if at any other time you want to know whether it is active (and thus okay
1936 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001937 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001938 */
Chalard Jean4d660112018-06-04 16:52:49 +09001939 void onNetworkActive();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001940 }
1941
1942 private INetworkManagementService getNetworkManagementService() {
1943 synchronized (this) {
1944 if (mNMService != null) {
1945 return mNMService;
1946 }
1947 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1948 mNMService = INetworkManagementService.Stub.asInterface(b);
1949 return mNMService;
1950 }
1951 }
1952
1953 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
Chalard Jean4d660112018-06-04 16:52:49 +09001954 mNetworkActivityListeners = new ArrayMap<>();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001955
1956 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001957 * Start listening to reports when the system's default data network is active, meaning it is
1958 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1959 * to determine the current state of the system's default network after registering the
1960 * listener.
1961 * <p>
1962 * If the process default network has been set with
Paul Jensen72db88e2015-03-10 10:54:12 -04001963 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt6078b502014-06-11 16:05:07 -07001964 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001965 *
1966 * @param l The listener to be told when the network is active.
1967 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001968 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001969 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1970 @Override
1971 public void onNetworkActive() throws RemoteException {
1972 l.onNetworkActive();
1973 }
1974 };
1975
1976 try {
1977 getNetworkManagementService().registerNetworkActivityListener(rl);
1978 mNetworkActivityListeners.put(l, rl);
1979 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001980 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001981 }
1982 }
1983
1984 /**
1985 * Remove network active listener previously registered with
Robert Greenwaltb2489872014-09-04 16:44:35 -07001986 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001987 *
1988 * @param l Previously registered listener.
1989 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001990 public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001991 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
Hugo Benichie7678512017-05-09 15:19:01 +09001992 Preconditions.checkArgument(rl != null, "Listener was not registered.");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001993 try {
1994 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1995 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001996 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001997 }
1998 }
1999
2000 /**
2001 * Return whether the data network is currently active. An active network means that
2002 * it is currently in a high power state for performing data transmission. On some
2003 * types of networks, it may be expensive to move and stay in such a state, so it is
2004 * more power efficient to batch network traffic together when the radio is already in
2005 * this state. This method tells you whether right now is currently a good time to
2006 * initiate network traffic, as the network is already active.
2007 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002008 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002009 try {
2010 return getNetworkManagementService().isNetworkActive();
2011 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002012 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002013 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002014 }
2015
2016 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002017 * {@hide}
2018 */
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09002019 public ConnectivityManager(Context context, IConnectivityManager service) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09002020 mContext = Preconditions.checkNotNull(context, "missing context");
2021 mService = Preconditions.checkNotNull(service, "missing IConnectivityManager");
Paul Jensene0bef712014-12-10 15:12:18 -05002022 sInstance = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002023 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002024
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07002025 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002026 @UnsupportedAppUsage
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07002027 public static ConnectivityManager from(Context context) {
2028 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
2029 }
2030
Lorenzo Colittifbe9b1a2016-07-28 17:14:11 +09002031 /* TODO: These permissions checks don't belong in client-side code. Move them to
2032 * services.jar, possibly in com.android.server.net. */
2033
2034 /** {@hide} */
Lorenzo Colittid5427052015-10-15 16:29:00 +09002035 public static final void enforceChangePermission(Context context) {
2036 int uid = Binder.getCallingUid();
2037 Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
2038 .getPackageNameForUid(context, uid), true /* throwException */);
2039 }
2040
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002041 /** {@hide} */
2042 public static final void enforceTetherChangePermission(Context context, String callingPkg) {
Hugo Benichie7678512017-05-09 15:19:01 +09002043 Preconditions.checkNotNull(context, "Context cannot be null");
2044 Preconditions.checkNotNull(callingPkg, "callingPkg cannot be null");
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002045
Robert Greenwaltedb47662014-09-16 17:54:19 -07002046 if (context.getResources().getStringArray(
2047 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
2048 // Have a provisioning app - must only let system apps (which check this app)
2049 // turn on tethering
2050 context.enforceCallingOrSelfPermission(
Jeremy Kleind42209d2015-12-28 15:11:58 -08002051 android.Manifest.permission.TETHER_PRIVILEGED, "ConnectivityService");
Robert Greenwaltedb47662014-09-16 17:54:19 -07002052 } else {
Billy Laua7238a32015-08-01 12:45:02 +01002053 int uid = Binder.getCallingUid();
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002054 // If callingPkg's uid is not same as Binder.getCallingUid(),
2055 // AppOpsService throws SecurityException.
2056 Settings.checkAndNoteWriteSettingsOperation(context, uid, callingPkg,
2057 true /* throwException */);
Robert Greenwaltedb47662014-09-16 17:54:19 -07002058 }
2059 }
2060
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002061 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002062 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2063 * situations where a Context pointer is unavailable.
2064 * @hide
2065 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002066 @Deprecated
Paul Jensen72db88e2015-03-10 10:54:12 -04002067 static ConnectivityManager getInstanceOrNull() {
2068 return sInstance;
2069 }
2070
2071 /**
2072 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2073 * situations where a Context pointer is unavailable.
2074 * @hide
2075 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002076 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002077 @UnsupportedAppUsage
Paul Jensen72db88e2015-03-10 10:54:12 -04002078 private static ConnectivityManager getInstance() {
2079 if (getInstanceOrNull() == null) {
Paul Jensene0bef712014-12-10 15:12:18 -05002080 throw new IllegalStateException("No ConnectivityManager yet constructed");
2081 }
Paul Jensen72db88e2015-03-10 10:54:12 -04002082 return getInstanceOrNull();
Paul Jensene0bef712014-12-10 15:12:18 -05002083 }
2084
2085 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002086 * Get the set of tetherable, available interfaces. This list is limited by
2087 * device configuration and current interface existence.
2088 *
2089 * @return an array of 0 or more Strings of tetherable interface names.
2090 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002091 * {@hide}
2092 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002093 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002094 @UnsupportedAppUsage
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002095 public String[] getTetherableIfaces() {
2096 try {
2097 return mService.getTetherableIfaces();
2098 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002099 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002100 }
2101 }
2102
2103 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002104 * Get the set of tethered interfaces.
2105 *
2106 * @return an array of 0 or more String of currently tethered interface names.
2107 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002108 * {@hide}
2109 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002110 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002111 @UnsupportedAppUsage
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002112 public String[] getTetheredIfaces() {
2113 try {
2114 return mService.getTetheredIfaces();
2115 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002116 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002117 }
2118 }
2119
2120 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002121 * Get the set of interface names which attempted to tether but
2122 * failed. Re-attempting to tether may cause them to reset to the Tethered
2123 * state. Alternatively, causing the interface to be destroyed and recreated
2124 * may cause them to reset to the available state.
2125 * {@link ConnectivityManager#getLastTetherError} can be used to get more
2126 * information on the cause of the errors.
2127 *
2128 * @return an array of 0 or more String indicating the interface names
2129 * which failed to tether.
2130 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002131 * {@hide}
2132 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002133 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002134 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002135 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002136 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08002137 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002138 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002139 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002140 }
2141 }
2142
2143 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07002144 * Get the set of tethered dhcp ranges.
2145 *
2146 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
2147 * {@hide}
2148 */
2149 public String[] getTetheredDhcpRanges() {
2150 try {
2151 return mService.getTetheredDhcpRanges();
2152 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002153 throw e.rethrowFromSystemServer();
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07002154 }
2155 }
2156
2157 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002158 * Attempt to tether the named interface. This will setup a dhcp server
2159 * on the interface, forward and NAT IP packets and forward DNS requests
2160 * to the best active upstream network interface. Note that if no upstream
2161 * IP network interface is available, dhcp will still run and traffic will be
2162 * allowed between the tethered devices and this device, though upstream net
2163 * access will of course fail until an upstream network interface becomes
2164 * active.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002165 *
2166 * <p>This method requires the caller to hold either the
2167 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2168 * or the ability to modify system settings as determined by
2169 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002170 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002171 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2172 * and WifiStateMachine which need direct access. All other clients should use
2173 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2174 * logic.</p>
2175 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002176 * @param iface the interface name to tether.
2177 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2178 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002179 * {@hide}
2180 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002181 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002182 public int tether(String iface) {
2183 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002184 String pkgName = mContext.getOpPackageName();
2185 Log.i(TAG, "tether caller:" + pkgName);
2186 return mService.tether(iface, pkgName);
Robert Greenwalt5a735062010-03-02 17:25:02 -08002187 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002188 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002189 }
2190 }
2191
2192 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002193 * Stop tethering the named interface.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002194 *
2195 * <p>This method requires the caller to hold either the
2196 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2197 * or the ability to modify system settings as determined by
2198 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002199 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002200 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2201 * and WifiStateMachine which need direct access. All other clients should use
2202 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2203 * logic.</p>
2204 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002205 * @param iface the interface name to untether.
2206 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2207 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002208 * {@hide}
2209 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002210 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002211 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002212 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002213 String pkgName = mContext.getOpPackageName();
2214 Log.i(TAG, "untether caller:" + pkgName);
2215 return mService.untether(iface, pkgName);
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002216 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002217 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002218 }
2219 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002220
2221 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002222 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002223 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002224 * due to device configuration.
2225 *
Chalard Jean8f76fc32017-09-26 15:45:18 +09002226 * <p>If this app does not have permission to use this API, it will always
2227 * return false rather than throw an exception.</p>
2228 *
2229 * <p>If the device has a hotspot provisioning app, the caller is required to hold the
2230 * {@link android.Manifest.permission.TETHER_PRIVILEGED} permission.</p>
2231 *
2232 * <p>Otherwise, this method requires the caller to hold the ability to modify system
2233 * settings as determined by {@link android.provider.Settings.System#canWrite}.</p>
2234 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002235 * @return a boolean - {@code true} indicating Tethering is supported.
2236 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002237 * {@hide}
2238 */
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002239 @SystemApi
Chalard Jean8f76fc32017-09-26 15:45:18 +09002240 @RequiresPermission(anyOf = {android.Manifest.permission.TETHER_PRIVILEGED,
2241 android.Manifest.permission.WRITE_SETTINGS})
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002242 public boolean isTetheringSupported() {
Chalard Jean8f76fc32017-09-26 15:45:18 +09002243 String pkgName = mContext.getOpPackageName();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002244 try {
Udam Saini0e94c362017-06-07 12:06:28 -07002245 return mService.isTetheringSupported(pkgName);
Chalard Jean8f76fc32017-09-26 15:45:18 +09002246 } catch (SecurityException e) {
2247 // This API is not available to this caller, but for backward-compatibility
2248 // this will just return false instead of throwing.
2249 return false;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002250 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002251 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002252 }
2253 }
2254
2255 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002256 * Callback for use with {@link #startTethering} to find out whether tethering succeeded.
2257 * @hide
2258 */
2259 @SystemApi
2260 public static abstract class OnStartTetheringCallback {
2261 /**
2262 * Called when tethering has been successfully started.
2263 */
Chalard Jean4d660112018-06-04 16:52:49 +09002264 public void onTetheringStarted() {}
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002265
2266 /**
2267 * Called when starting tethering failed.
2268 */
Chalard Jean4d660112018-06-04 16:52:49 +09002269 public void onTetheringFailed() {}
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002270 }
2271
2272 /**
2273 * Convenient overload for
2274 * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null
2275 * handler to run on the current thread's {@link Looper}.
2276 * @hide
2277 */
2278 @SystemApi
Udam Saini0e94c362017-06-07 12:06:28 -07002279 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002280 public void startTethering(int type, boolean showProvisioningUi,
2281 final OnStartTetheringCallback callback) {
2282 startTethering(type, showProvisioningUi, callback, null);
2283 }
2284
2285 /**
2286 * Runs tether provisioning for the given type if needed and then starts tethering if
2287 * the check succeeds. If no carrier provisioning is required for tethering, tethering is
2288 * enabled immediately. If provisioning fails, tethering will not be enabled. It also
2289 * schedules tether provisioning re-checks if appropriate.
2290 *
2291 * @param type The type of tethering to start. Must be one of
2292 * {@link ConnectivityManager.TETHERING_WIFI},
2293 * {@link ConnectivityManager.TETHERING_USB}, or
2294 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2295 * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there
2296 * is one. This should be true the first time this function is called and also any time
2297 * the user can see this UI. It gives users information from their carrier about the
2298 * check failing and how they can sign up for tethering if possible.
2299 * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller
2300 * of the result of trying to tether.
2301 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
2302 * @hide
2303 */
2304 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002305 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002306 public void startTethering(int type, boolean showProvisioningUi,
2307 final OnStartTetheringCallback callback, Handler handler) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09002308 Preconditions.checkNotNull(callback, "OnStartTetheringCallback cannot be null.");
Jeremy Klein5f277e12016-03-12 16:29:54 -08002309
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002310 ResultReceiver wrappedCallback = new ResultReceiver(handler) {
2311 @Override
2312 protected void onReceiveResult(int resultCode, Bundle resultData) {
2313 if (resultCode == TETHER_ERROR_NO_ERROR) {
2314 callback.onTetheringStarted();
2315 } else {
2316 callback.onTetheringFailed();
2317 }
2318 }
2319 };
Jeremy Klein5f277e12016-03-12 16:29:54 -08002320
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002321 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002322 String pkgName = mContext.getOpPackageName();
2323 Log.i(TAG, "startTethering caller:" + pkgName);
2324 mService.startTethering(type, wrappedCallback, showProvisioningUi, pkgName);
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002325 } catch (RemoteException e) {
2326 Log.e(TAG, "Exception trying to start tethering.", e);
2327 wrappedCallback.send(TETHER_ERROR_SERVICE_UNAVAIL, null);
2328 }
2329 }
2330
2331 /**
2332 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if
2333 * applicable.
2334 *
2335 * @param type The type of tethering to stop. Must be one of
2336 * {@link ConnectivityManager.TETHERING_WIFI},
2337 * {@link ConnectivityManager.TETHERING_USB}, or
2338 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2339 * @hide
2340 */
2341 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002342 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002343 public void stopTethering(int type) {
2344 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002345 String pkgName = mContext.getOpPackageName();
2346 Log.i(TAG, "stopTethering caller:" + pkgName);
2347 mService.stopTethering(type, pkgName);
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002348 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002349 throw e.rethrowFromSystemServer();
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002350 }
2351 }
2352
2353 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002354 * Get the list of regular expressions that define any tetherable
2355 * USB network interfaces. If USB tethering is not supported by the
2356 * device, this list should be empty.
2357 *
2358 * @return an array of 0 or more regular expression Strings defining
2359 * what interfaces are considered tetherable usb interfaces.
2360 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002361 * {@hide}
2362 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002363 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002364 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002365 public String[] getTetherableUsbRegexs() {
2366 try {
2367 return mService.getTetherableUsbRegexs();
2368 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002369 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002370 }
2371 }
2372
2373 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002374 * Get the list of regular expressions that define any tetherable
2375 * Wifi network interfaces. If Wifi tethering is not supported by the
2376 * device, this list should be empty.
2377 *
2378 * @return an array of 0 or more regular expression Strings defining
2379 * what interfaces are considered tetherable wifi interfaces.
2380 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002381 * {@hide}
2382 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002383 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002384 @UnsupportedAppUsage
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002385 public String[] getTetherableWifiRegexs() {
2386 try {
2387 return mService.getTetherableWifiRegexs();
2388 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002389 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002390 }
2391 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08002392
Danica Chang6fdd0c62010-08-11 14:54:43 -07002393 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002394 * Get the list of regular expressions that define any tetherable
2395 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
2396 * device, this list should be empty.
2397 *
2398 * @return an array of 0 or more regular expression Strings defining
2399 * what interfaces are considered tetherable bluetooth interfaces.
2400 *
Danica Chang6fdd0c62010-08-11 14:54:43 -07002401 * {@hide}
2402 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002403 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002404 @UnsupportedAppUsage
Danica Chang6fdd0c62010-08-11 14:54:43 -07002405 public String[] getTetherableBluetoothRegexs() {
2406 try {
2407 return mService.getTetherableBluetoothRegexs();
2408 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002409 throw e.rethrowFromSystemServer();
Danica Chang6fdd0c62010-08-11 14:54:43 -07002410 }
2411 }
2412
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002413 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002414 * Attempt to both alter the mode of USB and Tethering of USB. A
2415 * utility method to deal with some of the complexity of USB - will
2416 * attempt to switch to Rndis and subsequently tether the resulting
2417 * interface on {@code true} or turn off tethering and switch off
2418 * Rndis on {@code false}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002419 *
2420 * <p>This method requires the caller to hold either the
2421 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2422 * or the ability to modify system settings as determined by
2423 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002424 *
2425 * @param enable a boolean - {@code true} to enable tethering
2426 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2427 *
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002428 * {@hide}
2429 */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002430 @UnsupportedAppUsage
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002431 public int setUsbTethering(boolean enable) {
2432 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002433 String pkgName = mContext.getOpPackageName();
2434 Log.i(TAG, "setUsbTethering caller:" + pkgName);
2435 return mService.setUsbTethering(enable, pkgName);
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002436 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002437 throw e.rethrowFromSystemServer();
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002438 }
2439 }
2440
Robert Greenwalt5a735062010-03-02 17:25:02 -08002441 /** {@hide} */
2442 public static final int TETHER_ERROR_NO_ERROR = 0;
2443 /** {@hide} */
2444 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
2445 /** {@hide} */
2446 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
2447 /** {@hide} */
2448 public static final int TETHER_ERROR_UNSUPPORTED = 3;
2449 /** {@hide} */
2450 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
2451 /** {@hide} */
2452 public static final int TETHER_ERROR_MASTER_ERROR = 5;
2453 /** {@hide} */
2454 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
2455 /** {@hide} */
2456 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
2457 /** {@hide} */
2458 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
2459 /** {@hide} */
2460 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
2461 /** {@hide} */
2462 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002463 /** {@hide} */
2464 public static final int TETHER_ERROR_PROVISION_FAILED = 11;
Robert Greenwalt5a735062010-03-02 17:25:02 -08002465
2466 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002467 * Get a more detailed error code after a Tethering or Untethering
2468 * request asynchronously failed.
2469 *
2470 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08002471 * @return error The error code of the last error tethering or untethering the named
2472 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002473 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002474 * {@hide}
2475 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002476 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002477 @UnsupportedAppUsage
Robert Greenwalt5a735062010-03-02 17:25:02 -08002478 public int getLastTetherError(String iface) {
2479 try {
2480 return mService.getLastTetherError(iface);
2481 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002482 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002483 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002484 }
2485
2486 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002487 * Report network connectivity status. This is currently used only
2488 * to alter status bar UI.
Paul Jensenb2748922015-05-06 11:10:18 -04002489 * <p>This method requires the caller to hold the permission
2490 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002491 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002492 * @param networkType The type of network you want to report on
2493 * @param percentage The quality of the connection 0 is bad, 100 is good
Chalard Jean6b1da6e2018-03-08 13:54:53 +09002494 * @deprecated Types are deprecated. Use {@link #reportNetworkConnectivity} instead.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002495 * {@hide}
2496 */
2497 public void reportInetCondition(int networkType, int percentage) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09002498 printStackTrace();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002499 try {
2500 mService.reportInetCondition(networkType, percentage);
2501 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002502 throw e.rethrowFromSystemServer();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002503 }
2504 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002505
2506 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002507 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07002508 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002509 * the framework to re-evaluate network connectivity and/or switch to another
2510 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002511 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002512 * @param network The {@link Network} the application was attempting to use
2513 * or {@code null} to indicate the current default network.
Paul Jensenbfd17b72015-04-07 12:43:13 -04002514 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
2515 * working and non-working connectivity.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002516 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002517 @Deprecated
Robert Greenwalt9258c642014-03-26 16:47:06 -07002518 public void reportBadNetwork(Network network) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09002519 printStackTrace();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002520 try {
Paul Jensenbfd17b72015-04-07 12:43:13 -04002521 // One of these will be ignored because it matches system's current state.
2522 // The other will trigger the necessary reevaluation.
2523 mService.reportNetworkConnectivity(network, true);
2524 mService.reportNetworkConnectivity(network, false);
2525 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002526 throw e.rethrowFromSystemServer();
Paul Jensenbfd17b72015-04-07 12:43:13 -04002527 }
2528 }
2529
2530 /**
2531 * Report to the framework whether a network has working connectivity.
2532 * This provides a hint to the system that a particular network is providing
2533 * working connectivity or not. In response the framework may re-evaluate
2534 * the network's connectivity and might take further action thereafter.
2535 *
2536 * @param network The {@link Network} the application was attempting to use
2537 * or {@code null} to indicate the current default network.
2538 * @param hasConnectivity {@code true} if the application was able to successfully access the
2539 * Internet using {@code network} or {@code false} if not.
2540 */
2541 public void reportNetworkConnectivity(Network network, boolean hasConnectivity) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09002542 printStackTrace();
Paul Jensenbfd17b72015-04-07 12:43:13 -04002543 try {
2544 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002545 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002546 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002547 }
2548 }
2549
2550 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002551 * Set a network-independent global http proxy. This is not normally what you want
2552 * for typical HTTP proxies - they are general network dependent. However if you're
2553 * doing something unusual like general internal filtering this may be useful. On
2554 * a private network where the proxy is not accessible, you may break HTTP using this.
Paul Jensenb2748922015-05-06 11:10:18 -04002555 *
2556 * @param p A {@link ProxyInfo} object defining the new global
2557 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002558 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002559 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002560 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Jason Monk207900c2014-04-25 15:00:09 -04002561 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002562 try {
2563 mService.setGlobalProxy(p);
2564 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002565 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002566 }
2567 }
2568
2569 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002570 * Retrieve any network-independent global HTTP proxy.
2571 *
Jason Monk207900c2014-04-25 15:00:09 -04002572 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002573 * if no global HTTP proxy is set.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002574 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002575 */
Jason Monk207900c2014-04-25 15:00:09 -04002576 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002577 try {
2578 return mService.getGlobalProxy();
2579 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002580 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002581 }
2582 }
2583
2584 /**
Paul Jensencee9b512015-05-06 07:32:40 -04002585 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
2586 * network-specific HTTP proxy. If {@code network} is null, the
2587 * network-specific proxy returned is the proxy of the default active
2588 * network.
2589 *
2590 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
2591 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
2592 * or when {@code network} is {@code null},
2593 * the {@code ProxyInfo} for the default active network. Returns
2594 * {@code null} when no proxy applies or the caller doesn't have
2595 * permission to use {@code network}.
2596 * @hide
2597 */
2598 public ProxyInfo getProxyForNetwork(Network network) {
2599 try {
2600 return mService.getProxyForNetwork(network);
2601 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002602 throw e.rethrowFromSystemServer();
Paul Jensencee9b512015-05-06 07:32:40 -04002603 }
2604 }
2605
2606 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002607 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
2608 * otherwise if this process is bound to a {@link Network} using
Paul Jensen72db88e2015-03-10 10:54:12 -04002609 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensene0bef712014-12-10 15:12:18 -05002610 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002611 *
Jason Monk207900c2014-04-25 15:00:09 -04002612 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002613 * HTTP proxy is active.
Robert Greenwalt434203a2010-10-11 16:00:27 -07002614 */
Paul Jensene0bef712014-12-10 15:12:18 -05002615 public ProxyInfo getDefaultProxy() {
Paul Jensencee9b512015-05-06 07:32:40 -04002616 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwalt434203a2010-10-11 16:00:27 -07002617 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07002618
2619 /**
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002620 * Returns true if the hardware supports the given network type
2621 * else it returns false. This doesn't indicate we have coverage
2622 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002623 * hardware supports it. For example a GSM phone without a SIM
2624 * should still return {@code true} for mobile data, but a wifi only
2625 * tablet would return {@code false}.
2626 *
2627 * @param networkType The network type we'd like to check
2628 * @return {@code true} if supported, else {@code false}
Chalard Jean6b1da6e2018-03-08 13:54:53 +09002629 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002630 * @hide
2631 */
Chalard Jean6b1da6e2018-03-08 13:54:53 +09002632 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002633 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002634 @UnsupportedAppUsage
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002635 public boolean isNetworkSupported(int networkType) {
2636 try {
2637 return mService.isNetworkSupported(networkType);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002638 } catch (RemoteException e) {
2639 throw e.rethrowFromSystemServer();
2640 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002641 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002642
2643 /**
2644 * Returns if the currently active data network is metered. A network is
2645 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002646 * that connection due to monetary costs, data limitations or
2647 * battery/performance issues. You should check this before doing large
2648 * data transfers, and warn the user or delay the operation until another
2649 * network is available.
2650 *
2651 * @return {@code true} if large transfers should be avoided, otherwise
2652 * {@code false}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002653 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002654 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002655 public boolean isActiveNetworkMetered() {
2656 try {
2657 return mService.isActiveNetworkMetered();
2658 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002659 throw e.rethrowFromSystemServer();
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002660 }
2661 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002662
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002663 /**
2664 * If the LockdownVpn mechanism is enabled, updates the vpn
2665 * with a reload of its profile.
2666 *
2667 * @return a boolean with {@code} indicating success
2668 *
2669 * <p>This method can only be called by the system UID
2670 * {@hide}
2671 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002672 public boolean updateLockdownVpn() {
2673 try {
2674 return mService.updateLockdownVpn();
2675 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002676 throw e.rethrowFromSystemServer();
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002677 }
2678 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07002679
2680 /**
Wink Saville948282b2013-08-29 08:55:16 -07002681 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07002682 *
Wink Savilleab9321d2013-06-29 21:10:57 -07002683 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07002684 *
2685 * @return time out that will be used, maybe less that suggestedTimeOutMs
2686 * -1 if an error.
2687 *
2688 * {@hide}
2689 */
Wink Saville948282b2013-08-29 08:55:16 -07002690 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07002691 int timeOutMs = -1;
2692 try {
Wink Saville948282b2013-08-29 08:55:16 -07002693 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07002694 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002695 throw e.rethrowFromSystemServer();
Wink Savilleab9321d2013-06-29 21:10:57 -07002696 }
2697 return timeOutMs;
2698 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002699
2700 /**
Wink Saville42d4f082013-07-20 20:31:59 -07002701 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002702 * {@hide}
2703 */
2704 public String getMobileProvisioningUrl() {
2705 try {
2706 return mService.getMobileProvisioningUrl();
2707 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002708 throw e.rethrowFromSystemServer();
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002709 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002710 }
Wink Saville42d4f082013-07-20 20:31:59 -07002711
2712 /**
Wink Saville948282b2013-08-29 08:55:16 -07002713 * Set sign in error notification to visible or in visible
2714 *
Wink Saville948282b2013-08-29 08:55:16 -07002715 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04002716 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville948282b2013-08-29 08:55:16 -07002717 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002718 @Deprecated
Wink Saville948282b2013-08-29 08:55:16 -07002719 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04002720 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07002721 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04002722 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07002723 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002724 throw e.rethrowFromSystemServer();
Wink Saville948282b2013-08-29 08:55:16 -07002725 }
2726 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002727
2728 /**
2729 * Set the value for enabling/disabling airplane mode
2730 *
2731 * @param enable whether to enable airplane mode or not
2732 *
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002733 * @hide
2734 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002735 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002736 @UnsupportedAppUsage
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002737 public void setAirplaneMode(boolean enable) {
2738 try {
2739 mService.setAirplaneMode(enable);
2740 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002741 throw e.rethrowFromSystemServer();
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002742 }
2743 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002744
2745 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002746 @UnsupportedAppUsage
Robert Greenwalta67be032014-05-16 15:49:14 -07002747 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07002748 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07002749 mService.registerNetworkFactory(messenger, name);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002750 } catch (RemoteException e) {
2751 throw e.rethrowFromSystemServer();
2752 }
Robert Greenwalta67be032014-05-16 15:49:14 -07002753 }
2754
2755 /** {@hide} */
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01002756 @UnsupportedAppUsage
Robert Greenwalta67be032014-05-16 15:49:14 -07002757 public void unregisterNetworkFactory(Messenger messenger) {
2758 try {
2759 mService.unregisterNetworkFactory(messenger);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002760 } catch (RemoteException e) {
2761 throw e.rethrowFromSystemServer();
2762 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002763 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002764
Paul Jensen31a94f42015-02-13 14:18:39 -05002765 /**
2766 * @hide
2767 * Register a NetworkAgent with ConnectivityService.
2768 * @return NetID corresponding to NetworkAgent.
2769 */
2770 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002771 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002772 try {
Paul Jensen31a94f42015-02-13 14:18:39 -05002773 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
2774 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002775 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -05002776 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002777 }
2778
Robert Greenwalt9258c642014-03-26 16:47:06 -07002779 /**
Hugo Benichidafed3d2017-03-06 09:17:06 +09002780 * Base class for {@code NetworkRequest} callbacks. Used for notifications about network
2781 * changes. Should be extended by applications wanting notifications.
2782 *
2783 * A {@code NetworkCallback} is registered by calling
2784 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
2785 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)},
Hugo Benichica867dc2018-02-07 21:17:43 +09002786 * or {@link #registerDefaultNetworkCallback(NetworkCallback)}. A {@code NetworkCallback} is
Hugo Benichidafed3d2017-03-06 09:17:06 +09002787 * unregistered by calling {@link #unregisterNetworkCallback(NetworkCallback)}.
2788 * A {@code NetworkCallback} should be registered at most once at any time.
2789 * A {@code NetworkCallback} that has been unregistered can be registered again.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002790 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002791 public static class NetworkCallback {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002792 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002793 * Called when the framework connects to a new network to evaluate whether it satisfies this
2794 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
2795 * callback. There is no guarantee that this new network will satisfy any requests, or that
2796 * the network will stay connected for longer than the time necessary to evaluate it.
2797 * <p>
2798 * Most applications <b>should not</b> act on this callback, and should instead use
2799 * {@link #onAvailable}. This callback is intended for use by applications that can assist
2800 * the framework in properly evaluating the network &mdash; for example, an application that
2801 * can automatically log in to a captive portal without user intervention.
2802 *
2803 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti66276122015-06-11 14:27:17 +09002804 *
2805 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002806 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002807 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002808
2809 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002810 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002811 * This callback may be called more than once if the {@link Network} that is
2812 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002813 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002814 * @param network The {@link Network} of the satisfying network.
Chalard Jean804b8fb2018-01-30 22:41:41 +09002815 * @param networkCapabilities The {@link NetworkCapabilities} of the satisfying network.
2816 * @param linkProperties The {@link LinkProperties} of the satisfying network.
2817 * @hide
2818 */
2819 public void onAvailable(Network network, NetworkCapabilities networkCapabilities,
2820 LinkProperties linkProperties) {
2821 // Internally only this method is called when a new network is available, and
2822 // it calls the callback in the same way and order that older versions used
2823 // to call so as not to change the behavior.
2824 onAvailable(network);
2825 if (!networkCapabilities.hasCapability(
2826 NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)) {
2827 onNetworkSuspended(network);
2828 }
2829 onCapabilitiesChanged(network, networkCapabilities);
2830 onLinkPropertiesChanged(network, linkProperties);
2831 }
2832
2833 /**
2834 * Called when the framework connects and has declared a new network ready for use.
2835 * This callback may be called more than once if the {@link Network} that is
2836 * satisfying the request changes. This will always immediately be followed by a
2837 * call to {@link #onCapabilitiesChanged(Network, NetworkCapabilities)} then by a
2838 * call to {@link #onLinkPropertiesChanged(Network, LinkProperties)}.
2839 *
2840 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002841 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002842 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002843
2844 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002845 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002846 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002847 * for graceful handover. This may not be called if we have a hard loss
2848 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002849 * {@link NetworkCallback#onLost} call or a
2850 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002851 * on whether we lose or regain it.
2852 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002853 * @param network The {@link Network} that is about to be disconnected.
2854 * @param maxMsToLive The time in ms the framework will attempt to keep the
2855 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002856 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002857 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002858 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002859
2860 /**
2861 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002862 * graceful failure ends.
2863 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002864 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002865 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002866 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002867
2868 /**
Etan Cohenaebf17e2017-03-01 12:47:28 -08002869 * Called if no network is found in the timeout time specified in
Hugo Benichi0eec03f2017-05-15 15:15:33 +09002870 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} call. This callback is not
Etan Cohenaebf17e2017-03-01 12:47:28 -08002871 * called for the version of {@link #requestNetwork(NetworkRequest, NetworkCallback)}
2872 * without timeout. When this callback is invoked the associated
2873 * {@link NetworkRequest} will have already been removed and released, as if
2874 * {@link #unregisterNetworkCallback(NetworkCallback)} had been called.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002875 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002876 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002877
2878 /**
2879 * Called when the network the framework connected to for this request
2880 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002881 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002882 * @param network The {@link Network} whose capabilities have changed.
Chalard Jean804b8fb2018-01-30 22:41:41 +09002883 * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this
2884 * network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002885 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002886 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002887 NetworkCapabilities networkCapabilities) {}
2888
2889 /**
2890 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002891 * changes {@link LinkProperties}.
2892 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002893 * @param network The {@link Network} whose link properties have changed.
2894 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002895 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002896 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002897
Robert Greenwalt8d482522015-06-24 13:23:42 -07002898 /**
2899 * Called when the network the framework connected to for this request
Chalard Jean804b8fb2018-01-30 22:41:41 +09002900 * goes into {@link NetworkInfo.State#SUSPENDED}.
Robert Greenwalt8d482522015-06-24 13:23:42 -07002901 * This generally means that while the TCP connections are still live,
2902 * temporarily network data fails to transfer. Specifically this is used
2903 * on cellular networks to mask temporary outages when driving through
2904 * a tunnel, etc.
2905 * @hide
2906 */
2907 public void onNetworkSuspended(Network network) {}
2908
2909 /**
2910 * Called when the network the framework connected to for this request
Chalard Jean804b8fb2018-01-30 22:41:41 +09002911 * returns from a {@link NetworkInfo.State#SUSPENDED} state. This should always be
2912 * preceded by a matching {@link NetworkCallback#onNetworkSuspended} call.
Robert Greenwalt8d482522015-06-24 13:23:42 -07002913 * @hide
2914 */
2915 public void onNetworkResumed(Network network) {}
2916
Robert Greenwalt6078b502014-06-11 16:05:07 -07002917 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002918 }
2919
Hugo Benichicb883232017-05-11 13:16:17 +09002920 /**
2921 * Constant error codes used by ConnectivityService to communicate about failures and errors
2922 * across a Binder boundary.
2923 * @hide
2924 */
2925 public interface Errors {
Chalard Jean4d660112018-06-04 16:52:49 +09002926 int TOO_MANY_REQUESTS = 1;
Hugo Benichicb883232017-05-11 13:16:17 +09002927 }
2928
2929 /** @hide */
2930 public static class TooManyRequestsException extends RuntimeException {}
2931
2932 private static RuntimeException convertServiceException(ServiceSpecificException e) {
2933 switch (e.errorCode) {
2934 case Errors.TOO_MANY_REQUESTS:
2935 return new TooManyRequestsException();
2936 default:
2937 Log.w(TAG, "Unknown service error code " + e.errorCode);
2938 return new RuntimeException(e);
2939 }
2940 }
2941
Robert Greenwalt9258c642014-03-26 16:47:06 -07002942 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002943 /** @hide */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002944 public static final int CALLBACK_PRECHECK = BASE + 1;
2945 /** @hide */
2946 public static final int CALLBACK_AVAILABLE = BASE + 2;
2947 /** @hide arg1 = TTL */
2948 public static final int CALLBACK_LOSING = BASE + 3;
2949 /** @hide */
2950 public static final int CALLBACK_LOST = BASE + 4;
2951 /** @hide */
2952 public static final int CALLBACK_UNAVAIL = BASE + 5;
2953 /** @hide */
2954 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2955 /** @hide */
2956 public static final int CALLBACK_IP_CHANGED = BASE + 7;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002957 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Hugo Benichidba33db2017-03-23 22:40:44 +09002958 private static final int EXPIRE_LEGACY_REQUEST = BASE + 8;
Robert Greenwalt8d482522015-06-24 13:23:42 -07002959 /** @hide */
Hugo Benichidba33db2017-03-23 22:40:44 +09002960 public static final int CALLBACK_SUSPENDED = BASE + 9;
Robert Greenwalt8d482522015-06-24 13:23:42 -07002961 /** @hide */
Hugo Benichidba33db2017-03-23 22:40:44 +09002962 public static final int CALLBACK_RESUMED = BASE + 10;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002963
Erik Kline57faba92015-11-25 12:49:38 +09002964 /** @hide */
2965 public static String getCallbackName(int whichCallback) {
2966 switch (whichCallback) {
2967 case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK";
2968 case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE";
2969 case CALLBACK_LOSING: return "CALLBACK_LOSING";
2970 case CALLBACK_LOST: return "CALLBACK_LOST";
2971 case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL";
2972 case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED";
2973 case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED";
Erik Kline57faba92015-11-25 12:49:38 +09002974 case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST";
2975 case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED";
2976 case CALLBACK_RESUMED: return "CALLBACK_RESUMED";
2977 default:
2978 return Integer.toString(whichCallback);
2979 }
2980 }
2981
Robert Greenwalt562cc542014-05-15 18:07:26 -07002982 private class CallbackHandler extends Handler {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002983 private static final String TAG = "ConnectivityManager.CallbackHandler";
Robert Greenwalta9ebeef2015-09-03 16:41:45 -07002984 private static final boolean DBG = false;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002985
Hugo Benichid42650f2016-07-06 22:53:17 +09002986 CallbackHandler(Looper looper) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002987 super(looper);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002988 }
2989
Hugo Benichi2583ef02017-02-02 17:02:36 +09002990 CallbackHandler(Handler handler) {
Hugo Benichie7678512017-05-09 15:19:01 +09002991 this(Preconditions.checkNotNull(handler, "Handler cannot be null.").getLooper());
Hugo Benichi2583ef02017-02-02 17:02:36 +09002992 }
2993
Robert Greenwalt9258c642014-03-26 16:47:06 -07002994 @Override
2995 public void handleMessage(Message message) {
Hugo Benichi2c684522017-05-09 14:36:02 +09002996 if (message.what == EXPIRE_LEGACY_REQUEST) {
2997 expireRequest((NetworkCapabilities) message.obj, message.arg1);
2998 return;
2999 }
3000
3001 final NetworkRequest request = getObject(message, NetworkRequest.class);
3002 final Network network = getObject(message, Network.class);
3003 final NetworkCallback callback;
3004 synchronized (sCallbacks) {
3005 callback = sCallbacks.get(request);
3006 }
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09003007 if (DBG) {
Hugo Benichia0385682017-03-22 17:07:57 +09003008 Log.d(TAG, getCallbackName(message.what) + " for network " + network);
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09003009 }
Hugo Benichi2c684522017-05-09 14:36:02 +09003010 if (callback == null) {
3011 Log.w(TAG, "callback not found for " + getCallbackName(message.what) + " message");
3012 return;
3013 }
3014
Robert Greenwalt9258c642014-03-26 16:47:06 -07003015 switch (message.what) {
3016 case CALLBACK_PRECHECK: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003017 callback.onPreCheck(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003018 break;
3019 }
3020 case CALLBACK_AVAILABLE: {
Chalard Jean804b8fb2018-01-30 22:41:41 +09003021 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
3022 LinkProperties lp = getObject(message, LinkProperties.class);
3023 callback.onAvailable(network, cap, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003024 break;
3025 }
3026 case CALLBACK_LOSING: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003027 callback.onLosing(network, message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003028 break;
3029 }
3030 case CALLBACK_LOST: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003031 callback.onLost(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003032 break;
3033 }
3034 case CALLBACK_UNAVAIL: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003035 callback.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07003036 break;
3037 }
3038 case CALLBACK_CAP_CHANGED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003039 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
3040 callback.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003041 break;
3042 }
3043 case CALLBACK_IP_CHANGED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003044 LinkProperties lp = getObject(message, LinkProperties.class);
3045 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003046 break;
3047 }
Robert Greenwalt8d482522015-06-24 13:23:42 -07003048 case CALLBACK_SUSPENDED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003049 callback.onNetworkSuspended(network);
Robert Greenwalt8d482522015-06-24 13:23:42 -07003050 break;
3051 }
3052 case CALLBACK_RESUMED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09003053 callback.onNetworkResumed(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07003054 break;
3055 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003056 }
3057 }
3058
Hugo Benichid42650f2016-07-06 22:53:17 +09003059 private <T> T getObject(Message msg, Class<T> c) {
3060 return (T) msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07003061 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003062 }
3063
Hugo Benichi2583ef02017-02-02 17:02:36 +09003064 private CallbackHandler getDefaultHandler() {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003065 synchronized (sCallbacks) {
3066 if (sCallbackHandler == null) {
3067 sCallbackHandler = new CallbackHandler(ConnectivityThread.getInstanceLooper());
Robert Greenwalt9258c642014-03-26 16:47:06 -07003068 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003069 return sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003070 }
3071 }
3072
Hugo Benichi6f260f32017-02-03 14:18:44 +09003073 private static final HashMap<NetworkRequest, NetworkCallback> sCallbacks = new HashMap<>();
3074 private static CallbackHandler sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003075
Hugo Benichi6f260f32017-02-03 14:18:44 +09003076 private static final int LISTEN = 1;
3077 private static final int REQUEST = 2;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003078
Hugo Benichi6f260f32017-02-03 14:18:44 +09003079 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, NetworkCallback callback,
3080 int timeoutMs, int action, int legacyType, CallbackHandler handler) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09003081 printStackTrace();
Hugo Benichie7678512017-05-09 15:19:01 +09003082 checkCallbackNotNull(callback);
Hugo Benichidafed3d2017-03-06 09:17:06 +09003083 Preconditions.checkArgument(action == REQUEST || need != null, "null NetworkCapabilities");
Hugo Benichid42650f2016-07-06 22:53:17 +09003084 final NetworkRequest request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003085 try {
Hugo Benichid42650f2016-07-06 22:53:17 +09003086 synchronized(sCallbacks) {
Hugo Benichi31c176d2017-06-17 13:14:12 +09003087 if (callback.networkRequest != null
3088 && callback.networkRequest != ALREADY_UNREGISTERED) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09003089 // TODO: throw exception instead and enforce 1:1 mapping of callbacks
3090 // and requests (http://b/20701525).
3091 Log.e(TAG, "NetworkCallback was already registered");
3092 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003093 Messenger messenger = new Messenger(handler);
Hugo Benichid42650f2016-07-06 22:53:17 +09003094 Binder binder = new Binder();
Paul Jensen7221cc32014-06-27 11:05:32 -04003095 if (action == LISTEN) {
Hugo Benichid42650f2016-07-06 22:53:17 +09003096 request = mService.listenForNetwork(need, messenger, binder);
Paul Jensen7221cc32014-06-27 11:05:32 -04003097 } else {
Hugo Benichid42650f2016-07-06 22:53:17 +09003098 request = mService.requestNetwork(
3099 need, messenger, timeoutMs, binder, legacyType);
Paul Jensen7221cc32014-06-27 11:05:32 -04003100 }
Hugo Benichid42650f2016-07-06 22:53:17 +09003101 if (request != null) {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003102 sCallbacks.put(request, callback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003103 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003104 callback.networkRequest = request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003105 }
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003106 } catch (RemoteException e) {
3107 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003108 } catch (ServiceSpecificException e) {
3109 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003110 }
Hugo Benichid42650f2016-07-06 22:53:17 +09003111 return request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003112 }
3113
3114 /**
Erik Klinea2d29402016-03-16 15:31:39 +09003115 * Helper function to request a network with a particular legacy type.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09003116 *
3117 * This is temporarily public @hide so it can be called by system code that uses the
3118 * NetworkRequest API to request networks but relies on CONNECTIVITY_ACTION broadcasts for
3119 * instead network notifications.
3120 *
3121 * TODO: update said system code to rely on NetworkCallbacks and make this method private.
3122 *
3123 * @hide
3124 */
Lorenzo Colittid1179462015-11-25 15:47:14 +09003125 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
Hugo Benichi2583ef02017-02-02 17:02:36 +09003126 int timeoutMs, int legacyType, Handler handler) {
3127 CallbackHandler cbHandler = new CallbackHandler(handler);
3128 NetworkCapabilities nc = request.networkCapabilities;
3129 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, legacyType, cbHandler);
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09003130 }
3131
3132 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09003133 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003134 *
3135 * This {@link NetworkRequest} will live until released via
Etan Cohenaebf17e2017-03-01 12:47:28 -08003136 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A
3137 * version of the method which takes a timeout is
Hugo Benichi0eec03f2017-05-15 15:15:33 +09003138 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003139 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07003140 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003141 * can be used to direct traffic to the network.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003142 * <p>It is presently unsupported to request a network with mutable
3143 * {@link NetworkCapabilities} such as
3144 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3145 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3146 * as these {@code NetworkCapabilities} represent states that a particular
3147 * network may never attain, and whether a network will attain these states
3148 * is unknown prior to bringing up the network so the framework does not
3149 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003150 *
3151 * <p>This method requires the caller to hold either the
3152 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3153 * or the ability to modify system settings as determined by
3154 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt9258c642014-03-26 16:47:06 -07003155 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003156 * @param request {@link NetworkRequest} describing this request.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003157 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3158 * the callback must not be shared - it uniquely specifies this request.
3159 * The callback is invoked on the default internal Handler.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003160 * @throws IllegalArgumentException if {@code request} specifies any mutable
3161 * {@code NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003162 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003163 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003164 requestNetwork(request, networkCallback, getDefaultHandler());
3165 }
3166
3167 /**
3168 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
3169 *
3170 * This {@link NetworkRequest} will live until released via
Etan Cohenaebf17e2017-03-01 12:47:28 -08003171 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A
3172 * version of the method which takes a timeout is
Hugo Benichi0eec03f2017-05-15 15:15:33 +09003173 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003174 * Status of the request can be followed by listening to the various
3175 * callbacks described in {@link NetworkCallback}. The {@link Network}
3176 * can be used to direct traffic to the network.
3177 * <p>It is presently unsupported to request a network with mutable
3178 * {@link NetworkCapabilities} such as
3179 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3180 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3181 * as these {@code NetworkCapabilities} represent states that a particular
3182 * network may never attain, and whether a network will attain these states
3183 * is unknown prior to bringing up the network so the framework does not
Chalard Jean4d660112018-06-04 16:52:49 +09003184 * know how to go about satisfying a request with these capabilities.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003185 *
3186 * <p>This method requires the caller to hold either the
3187 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3188 * or the ability to modify system settings as determined by
3189 * {@link android.provider.Settings.System#canWrite}.</p>
3190 *
3191 * @param request {@link NetworkRequest} describing this request.
3192 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3193 * the callback must not be shared - it uniquely specifies this request.
3194 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
3195 * @throws IllegalArgumentException if {@code request} specifies any mutable
3196 * {@code NetworkCapabilities}.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003197 */
3198 public void requestNetwork(
3199 NetworkRequest request, NetworkCallback networkCallback, Handler handler) {
3200 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
3201 CallbackHandler cbHandler = new CallbackHandler(handler);
3202 requestNetwork(request, networkCallback, 0, legacyType, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003203 }
3204
3205 /**
Etan Cohenaebf17e2017-03-01 12:47:28 -08003206 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
3207 * by a timeout.
3208 *
3209 * This function behaves identically to the non-timed-out version
3210 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network
3211 * is not found within the given time (in milliseconds) the
3212 * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be
3213 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3214 * not have to be released if timed-out (it is automatically released). Unregistering a
3215 * request that timed out is not an error.
3216 *
3217 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3218 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3219 * for that purpose. Calling this method will attempt to bring up the requested network.
3220 *
3221 * <p>This method requires the caller to hold either the
3222 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3223 * or the ability to modify system settings as determined by
3224 * {@link android.provider.Settings.System#canWrite}.</p>
3225 *
3226 * @param request {@link NetworkRequest} describing this request.
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003227 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3228 * the callback must not be shared - it uniquely specifies this request.
Etan Cohenaebf17e2017-03-01 12:47:28 -08003229 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3230 * before {@link NetworkCallback#onUnavailable()} is called. The timeout must
3231 * be a positive value (i.e. >0).
Etan Cohenaebf17e2017-03-01 12:47:28 -08003232 */
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003233 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
3234 int timeoutMs) {
Hugo Benichie7678512017-05-09 15:19:01 +09003235 checkTimeout(timeoutMs);
Hugo Benichi2583ef02017-02-02 17:02:36 +09003236 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
Erik Klineb583b032017-02-22 12:58:24 +09003237 requestNetwork(request, networkCallback, timeoutMs, legacyType, getDefaultHandler());
Hugo Benichi2583ef02017-02-02 17:02:36 +09003238 }
3239
Hugo Benichi2583ef02017-02-02 17:02:36 +09003240 /**
3241 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
3242 * by a timeout.
3243 *
Chalard Jean4d660112018-06-04 16:52:49 +09003244 * This function behaves identically to the version without timeout, but if a suitable
Hugo Benichi2583ef02017-02-02 17:02:36 +09003245 * network is not found within the given time (in milliseconds) the
Etan Cohenaebf17e2017-03-01 12:47:28 -08003246 * {@link NetworkCallback#onUnavailable} callback is called. The request can still be
3247 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3248 * not have to be released if timed-out (it is automatically released). Unregistering a
3249 * request that timed out is not an error.
3250 *
3251 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3252 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3253 * for that purpose. Calling this method will attempt to bring up the requested network.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003254 *
3255 * <p>This method requires the caller to hold either the
3256 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3257 * or the ability to modify system settings as determined by
3258 * {@link android.provider.Settings.System#canWrite}.</p>
3259 *
3260 * @param request {@link NetworkRequest} describing this request.
Etan Cohenaebf17e2017-03-01 12:47:28 -08003261 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3262 * the callback must not be shared - it uniquely specifies this request.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003263 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003264 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3265 * before {@link NetworkCallback#onUnavailable} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003266 */
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003267 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
3268 Handler handler, int timeoutMs) {
Hugo Benichie7678512017-05-09 15:19:01 +09003269 checkTimeout(timeoutMs);
Hugo Benichi2583ef02017-02-02 17:02:36 +09003270 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
3271 CallbackHandler cbHandler = new CallbackHandler(handler);
3272 requestNetwork(request, networkCallback, timeoutMs, legacyType, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003273 }
3274
3275 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003276 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003277 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003278 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfcde58f2015-02-11 16:51:13 -08003279 * <p>
Paul Jensen72db88e2015-03-10 10:54:12 -04003280 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
3281 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003282 */
Erik Kline90e93072014-11-19 12:12:24 +09003283 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003284
3285 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07003286 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003287 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003288 * {@link android.content.Intent#getParcelableExtra(String)}.
3289 */
Erik Kline90e93072014-11-19 12:12:24 +09003290 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003291
3292
3293 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09003294 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003295 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003296 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07003297 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003298 * the request may outlive the calling application and get called back when a suitable
3299 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003300 * <p>
3301 * The operation is an Intent broadcast that goes to a broadcast receiver that
3302 * you registered with {@link Context#registerReceiver} or through the
3303 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3304 * <p>
3305 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09003306 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3307 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07003308 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07003309 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07003310 * Intent to reserve the network or it will be released shortly after the Intent
3311 * is processed.
3312 * <p>
Paul Jensen694f2b82015-06-17 14:15:39 -04003313 * If there is already a request for this Intent registered (with the equality of
Robert Greenwalt9258c642014-03-26 16:47:06 -07003314 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003315 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003316 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003317 * The request may be released normally by calling
3318 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003319 * <p>It is presently unsupported to request a network with either
3320 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3321 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3322 * as these {@code NetworkCapabilities} represent states that a particular
3323 * network may never attain, and whether a network will attain these states
3324 * is unknown prior to bringing up the network so the framework does not
Chalard Jean4d660112018-06-04 16:52:49 +09003325 * know how to go about satisfying a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003326 *
3327 * <p>This method requires the caller to hold either the
3328 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3329 * or the ability to modify system settings as determined by
3330 * {@link android.provider.Settings.System#canWrite}.</p>
3331 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003332 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003333 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07003334 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003335 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003336 * @throws IllegalArgumentException if {@code request} contains either
3337 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3338 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003339 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003340 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09003341 printStackTrace();
Hugo Benichie7678512017-05-09 15:19:01 +09003342 checkPendingIntentNotNull(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003343 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07003344 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003345 } catch (RemoteException e) {
3346 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003347 } catch (ServiceSpecificException e) {
3348 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003349 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003350 }
3351
3352 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003353 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
3354 * <p>
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003355 * This method has the same behavior as
3356 * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003357 * releasing network resources and disconnecting.
3358 *
3359 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3360 * PendingIntent passed to
3361 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
3362 * corresponding NetworkRequest you'd like to remove. Cannot be null.
3363 */
3364 public void releaseNetworkRequest(PendingIntent operation) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09003365 printStackTrace();
Hugo Benichie7678512017-05-09 15:19:01 +09003366 checkPendingIntentNotNull(operation);
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003367 try {
3368 mService.releasePendingNetworkRequest(operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003369 } catch (RemoteException e) {
3370 throw e.rethrowFromSystemServer();
3371 }
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003372 }
3373
Hugo Benichie7678512017-05-09 15:19:01 +09003374 private static void checkPendingIntentNotNull(PendingIntent intent) {
3375 Preconditions.checkNotNull(intent, "PendingIntent cannot be null.");
3376 }
3377
3378 private static void checkCallbackNotNull(NetworkCallback callback) {
3379 Preconditions.checkNotNull(callback, "null NetworkCallback");
3380 }
3381
3382 private static void checkTimeout(int timeoutMs) {
3383 Preconditions.checkArgumentPositive(timeoutMs, "timeoutMs must be strictly positive.");
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003384 }
3385
3386 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07003387 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07003388 * {@link NetworkRequest}. The callbacks will continue to be called until
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003389 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003390 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003391 * @param request {@link NetworkRequest} describing this request.
3392 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3393 * networks change state.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003394 * The callback is invoked on the default internal Handler.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003395 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003396 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt6078b502014-06-11 16:05:07 -07003397 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003398 registerNetworkCallback(request, networkCallback, getDefaultHandler());
3399 }
3400
3401 /**
3402 * Registers to receive notifications about all networks which satisfy the given
3403 * {@link NetworkRequest}. The callbacks will continue to be called until
3404 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003405 *
3406 * @param request {@link NetworkRequest} describing this request.
3407 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3408 * networks change state.
3409 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003410 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003411 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Hugo Benichi2583ef02017-02-02 17:02:36 +09003412 public void registerNetworkCallback(
3413 NetworkRequest request, NetworkCallback networkCallback, Handler handler) {
3414 CallbackHandler cbHandler = new CallbackHandler(handler);
3415 NetworkCapabilities nc = request.networkCapabilities;
3416 sendRequestForNetwork(nc, networkCallback, 0, LISTEN, TYPE_NONE, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003417 }
3418
3419 /**
Paul Jensen694f2b82015-06-17 14:15:39 -04003420 * Registers a PendingIntent to be sent when a network is available which satisfies the given
3421 * {@link NetworkRequest}.
3422 *
3423 * This function behaves identically to the version that takes a NetworkCallback, but instead
3424 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
3425 * the request may outlive the calling application and get called back when a suitable
3426 * network is found.
3427 * <p>
3428 * The operation is an Intent broadcast that goes to a broadcast receiver that
3429 * you registered with {@link Context#registerReceiver} or through the
3430 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3431 * <p>
3432 * The operation Intent is delivered with two extras, a {@link Network} typed
3433 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3434 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
3435 * the original requests parameters.
3436 * <p>
3437 * If there is already a request for this Intent registered (with the equality of
3438 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
3439 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
3440 * <p>
3441 * The request may be released normally by calling
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003442 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Paul Jensen694f2b82015-06-17 14:15:39 -04003443 * @param request {@link NetworkRequest} describing this request.
3444 * @param operation Action to perform when the network is available (corresponds
3445 * to the {@link NetworkCallback#onAvailable} call. Typically
3446 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
3447 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003448 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Paul Jensen694f2b82015-06-17 14:15:39 -04003449 public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09003450 printStackTrace();
Hugo Benichie7678512017-05-09 15:19:01 +09003451 checkPendingIntentNotNull(operation);
Paul Jensen694f2b82015-06-17 14:15:39 -04003452 try {
3453 mService.pendingListenForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003454 } catch (RemoteException e) {
3455 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003456 } catch (ServiceSpecificException e) {
3457 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003458 }
Paul Jensen694f2b82015-06-17 14:15:39 -04003459 }
3460
3461 /**
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003462 * Registers to receive notifications about changes in the system default network. The callbacks
3463 * will continue to be called until either the application exits or
3464 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Erik Klinea2d29402016-03-16 15:31:39 +09003465 *
3466 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3467 * system default network changes.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003468 * The callback is invoked on the default internal Handler.
Erik Klinea2d29402016-03-16 15:31:39 +09003469 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003470 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Erik Klinea2d29402016-03-16 15:31:39 +09003471 public void registerDefaultNetworkCallback(NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003472 registerDefaultNetworkCallback(networkCallback, getDefaultHandler());
3473 }
3474
3475 /**
3476 * Registers to receive notifications about changes in the system default network. The callbacks
3477 * will continue to be called until either the application exits or
3478 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003479 *
3480 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3481 * system default network changes.
3482 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003483 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003484 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Hugo Benichi2583ef02017-02-02 17:02:36 +09003485 public void registerDefaultNetworkCallback(NetworkCallback networkCallback, Handler handler) {
Erik Klinea2d29402016-03-16 15:31:39 +09003486 // This works because if the NetworkCapabilities are null,
3487 // ConnectivityService takes them from the default request.
3488 //
3489 // Since the capabilities are exactly the same as the default request's
3490 // capabilities, this request is guaranteed, at all times, to be
3491 // satisfied by the same network, if any, that satisfies the default
3492 // request, i.e., the system default network.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003493 CallbackHandler cbHandler = new CallbackHandler(handler);
Chalard Jean4d660112018-06-04 16:52:49 +09003494 sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0,
3495 REQUEST, TYPE_NONE, cbHandler);
Erik Klinea2d29402016-03-16 15:31:39 +09003496 }
3497
3498 /**
fengludb571472015-04-21 17:12:05 -07003499 * Requests bandwidth update for a given {@link Network} and returns whether the update request
3500 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
3501 * network connection for updated bandwidth information. The caller will be notified via
3502 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003503 * method assumes that the caller has previously called
3504 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network
3505 * changes.
fenglub15e72b2015-03-20 11:29:56 -07003506 *
fengluae519192015-04-27 14:28:04 -07003507 * @param network {@link Network} specifying which network you're interested.
fengludb571472015-04-21 17:12:05 -07003508 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglub15e72b2015-03-20 11:29:56 -07003509 */
fengludb571472015-04-21 17:12:05 -07003510 public boolean requestBandwidthUpdate(Network network) {
fenglub15e72b2015-03-20 11:29:56 -07003511 try {
fengludb571472015-04-21 17:12:05 -07003512 return mService.requestBandwidthUpdate(network);
fenglub15e72b2015-03-20 11:29:56 -07003513 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003514 throw e.rethrowFromSystemServer();
fenglub15e72b2015-03-20 11:29:56 -07003515 }
3516 }
3517
3518 /**
Hugo Benichidafed3d2017-03-06 09:17:06 +09003519 * Unregisters a {@code NetworkCallback} and possibly releases networks originating from
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003520 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and
3521 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls.
3522 * If the given {@code NetworkCallback} had previously been used with
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09003523 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
3524 * will be disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003525 *
Hugo Benichidafed3d2017-03-06 09:17:06 +09003526 * Notifications that would have triggered that {@code NetworkCallback} will immediately stop
3527 * triggering it as soon as this call returns.
3528 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003529 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003530 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003531 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09003532 printStackTrace();
Hugo Benichie7678512017-05-09 15:19:01 +09003533 checkCallbackNotNull(networkCallback);
Hugo Benichidafed3d2017-03-06 09:17:06 +09003534 final List<NetworkRequest> reqs = new ArrayList<>();
3535 // Find all requests associated to this callback and stop callback triggers immediately.
3536 // Callback is reusable immediately. http://b/20701525, http://b/35921499.
3537 synchronized (sCallbacks) {
Hugo Benichi31c176d2017-06-17 13:14:12 +09003538 Preconditions.checkArgument(networkCallback.networkRequest != null,
3539 "NetworkCallback was not registered");
3540 Preconditions.checkArgument(networkCallback.networkRequest != ALREADY_UNREGISTERED,
3541 "NetworkCallback was already unregistered");
Hugo Benichidafed3d2017-03-06 09:17:06 +09003542 for (Map.Entry<NetworkRequest, NetworkCallback> e : sCallbacks.entrySet()) {
3543 if (e.getValue() == networkCallback) {
3544 reqs.add(e.getKey());
3545 }
3546 }
3547 // TODO: throw exception if callback was registered more than once (http://b/20701525).
3548 for (NetworkRequest r : reqs) {
3549 try {
3550 mService.releaseNetworkRequest(r);
3551 } catch (RemoteException e) {
3552 throw e.rethrowFromSystemServer();
3553 }
3554 // Only remove mapping if rpc was successful.
3555 sCallbacks.remove(r);
3556 }
Hugo Benichi31c176d2017-06-17 13:14:12 +09003557 networkCallback.networkRequest = ALREADY_UNREGISTERED;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003558 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003559 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003560
3561 /**
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003562 * Unregisters a callback previously registered via
3563 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3564 *
3565 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3566 * PendingIntent passed to
3567 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3568 * Cannot be null.
3569 */
3570 public void unregisterNetworkCallback(PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003571 checkPendingIntentNotNull(operation);
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003572 releaseNetworkRequest(operation);
3573 }
3574
3575 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003576 * Informs the system whether it should switch to {@code network} regardless of whether it is
3577 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
3578 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
3579 * the system default network regardless of any other network that's currently connected. If
3580 * {@code always} is true, then the choice is remembered, so that the next time the user
3581 * connects to this network, the system will switch to it.
3582 *
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003583 * @param network The network to accept.
3584 * @param accept Whether to accept the network even if unvalidated.
3585 * @param always Whether to remember this choice in the future.
3586 *
3587 * @hide
3588 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003589 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003590 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
3591 try {
3592 mService.setAcceptUnvalidated(network, accept, always);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003593 } catch (RemoteException e) {
3594 throw e.rethrowFromSystemServer();
3595 }
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003596 }
3597
3598 /**
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003599 * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is
3600 * only meaningful if the system is configured not to penalize such networks, e.g., if the
3601 * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code
3602 * NETWORK_AVOID_BAD_WIFI setting is unset}.
3603 *
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003604 * @param network The network to accept.
3605 *
3606 * @hide
3607 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003608 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003609 public void setAvoidUnvalidated(Network network) {
3610 try {
3611 mService.setAvoidUnvalidated(network);
3612 } catch (RemoteException e) {
3613 throw e.rethrowFromSystemServer();
3614 }
3615 }
3616
3617 /**
Lorenzo Colitti4734cdb2017-04-27 14:30:21 +09003618 * Requests that the system open the captive portal app on the specified network.
3619 *
3620 * @param network The network to log into.
3621 *
3622 * @hide
3623 */
3624 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
3625 public void startCaptivePortalApp(Network network) {
3626 try {
3627 mService.startCaptivePortalApp(network);
3628 } catch (RemoteException e) {
3629 throw e.rethrowFromSystemServer();
3630 }
3631 }
3632
3633 /**
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003634 * It is acceptable to briefly use multipath data to provide seamless connectivity for
3635 * time-sensitive user-facing operations when the system default network is temporarily
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003636 * unresponsive. The amount of data should be limited (less than one megabyte for every call to
3637 * this method), and the operation should be infrequent to ensure that data usage is limited.
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003638 *
3639 * An example of such an operation might be a time-sensitive foreground activity, such as a
3640 * voice command, that the user is performing while walking out of range of a Wi-Fi network.
3641 */
3642 public static final int MULTIPATH_PREFERENCE_HANDOVER = 1 << 0;
3643
3644 /**
3645 * It is acceptable to use small amounts of multipath data on an ongoing basis to provide
3646 * a backup channel for traffic that is primarily going over another network.
3647 *
3648 * An example might be maintaining backup connections to peers or servers for the purpose of
3649 * fast fallback if the default network is temporarily unresponsive or disconnects. The traffic
3650 * on backup paths should be negligible compared to the traffic on the main path.
3651 */
3652 public static final int MULTIPATH_PREFERENCE_RELIABILITY = 1 << 1;
3653
3654 /**
3655 * It is acceptable to use metered data to improve network latency and performance.
3656 */
3657 public static final int MULTIPATH_PREFERENCE_PERFORMANCE = 1 << 2;
3658
3659 /**
3660 * Return value to use for unmetered networks. On such networks we currently set all the flags
3661 * to true.
3662 * @hide
3663 */
3664 public static final int MULTIPATH_PREFERENCE_UNMETERED =
3665 MULTIPATH_PREFERENCE_HANDOVER |
3666 MULTIPATH_PREFERENCE_RELIABILITY |
3667 MULTIPATH_PREFERENCE_PERFORMANCE;
3668
3669 /** @hide */
3670 @Retention(RetentionPolicy.SOURCE)
3671 @IntDef(flag = true, value = {
3672 MULTIPATH_PREFERENCE_HANDOVER,
3673 MULTIPATH_PREFERENCE_RELIABILITY,
3674 MULTIPATH_PREFERENCE_PERFORMANCE,
3675 })
3676 public @interface MultipathPreference {
3677 }
3678
3679 /**
3680 * Provides a hint to the calling application on whether it is desirable to use the
3681 * multinetwork APIs (e.g., {@link Network#openConnection}, {@link Network#bindSocket}, etc.)
3682 * for multipath data transfer on this network when it is not the system default network.
3683 * Applications desiring to use multipath network protocols should call this method before
3684 * each such operation.
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003685 *
3686 * @param network The network on which the application desires to use multipath data.
3687 * If {@code null}, this method will return the a preference that will generally
3688 * apply to metered networks.
3689 * @return a bitwise OR of zero or more of the {@code MULTIPATH_PREFERENCE_*} constants.
3690 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003691 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003692 public @MultipathPreference int getMultipathPreference(Network network) {
3693 try {
3694 return mService.getMultipathPreference(network);
3695 } catch (RemoteException e) {
3696 throw e.rethrowFromSystemServer();
3697 }
3698 }
3699
3700 /**
Stuart Scott984dc852015-03-30 13:17:11 -07003701 * Resets all connectivity manager settings back to factory defaults.
3702 * @hide
3703 */
3704 public void factoryReset() {
Stuart Scott984dc852015-03-30 13:17:11 -07003705 try {
Stuart Scottf1fb3972015-04-02 18:00:02 -07003706 mService.factoryReset();
Stuart Scott984dc852015-03-30 13:17:11 -07003707 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003708 throw e.rethrowFromSystemServer();
Stuart Scott984dc852015-03-30 13:17:11 -07003709 }
3710 }
3711
3712 /**
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003713 * Binds the current process to {@code network}. All Sockets created in the future
3714 * (and not explicitly bound via a bound SocketFactory from
3715 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3716 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3717 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3718 * work and all host name resolutions will fail. This is by design so an application doesn't
3719 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3720 * To clear binding pass {@code null} for {@code network}. Using individually bound
3721 * Sockets created by Network.getSocketFactory().createSocket() and
3722 * performing network-specific host name resolutions via
3723 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen72db88e2015-03-10 10:54:12 -04003724 * {@code bindProcessToNetwork}.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003725 *
3726 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3727 * the current binding.
3728 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3729 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003730 public boolean bindProcessToNetwork(Network network) {
Chalard Jean4d660112018-06-04 16:52:49 +09003731 // Forcing callers to call through non-static function ensures ConnectivityManager
Paul Jensen72db88e2015-03-10 10:54:12 -04003732 // instantiated.
3733 return setProcessDefaultNetwork(network);
3734 }
3735
3736 /**
3737 * Binds the current process to {@code network}. All Sockets created in the future
3738 * (and not explicitly bound via a bound SocketFactory from
3739 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3740 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3741 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3742 * work and all host name resolutions will fail. This is by design so an application doesn't
3743 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3744 * To clear binding pass {@code null} for {@code network}. Using individually bound
3745 * Sockets created by Network.getSocketFactory().createSocket() and
3746 * performing network-specific host name resolutions via
3747 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
3748 * {@code setProcessDefaultNetwork}.
3749 *
3750 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3751 * the current binding.
3752 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3753 * @deprecated This function can throw {@link IllegalStateException}. Use
3754 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
3755 * is a direct replacement.
3756 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003757 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003758 public static boolean setProcessDefaultNetwork(Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003759 int netId = (network == null) ? NETID_UNSET : network.netId;
Paul Jensen72db88e2015-03-10 10:54:12 -04003760 if (netId == NetworkUtils.getBoundNetworkForProcess()) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003761 return true;
3762 }
3763 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05003764 // Set HTTP proxy system properties to match network.
3765 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colittiec4c5552015-04-22 11:52:48 +09003766 try {
3767 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
3768 } catch (SecurityException e) {
3769 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
3770 Log.e(TAG, "Can't set proxy properties", e);
3771 }
Paul Jensenc91b5342014-08-27 12:38:45 -04003772 // Must flush DNS cache as new network may have different DNS resolutions.
3773 InetAddress.clearDnsCache();
3774 // Must flush socket pool as idle sockets will be bound to previous network and may
3775 // cause subsequent fetches to be performed on old network.
3776 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
3777 return true;
3778 } else {
3779 return false;
3780 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003781 }
3782
3783 /**
3784 * Returns the {@link Network} currently bound to this process via
Paul Jensen72db88e2015-03-10 10:54:12 -04003785 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003786 *
3787 * @return {@code Network} to which this process is bound, or {@code null}.
3788 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003789 public Network getBoundNetworkForProcess() {
3790 // Forcing callers to call thru non-static function ensures ConnectivityManager
3791 // instantiated.
3792 return getProcessDefaultNetwork();
3793 }
3794
3795 /**
3796 * Returns the {@link Network} currently bound to this process via
3797 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
3798 *
3799 * @return {@code Network} to which this process is bound, or {@code null}.
3800 * @deprecated Using this function can lead to other functions throwing
3801 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
3802 * {@code getBoundNetworkForProcess} is a direct replacement.
3803 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003804 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003805 public static Network getProcessDefaultNetwork() {
Paul Jensen72db88e2015-03-10 10:54:12 -04003806 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04003807 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003808 return new Network(netId);
3809 }
3810
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003811 private void unsupportedStartingFrom(int version) {
3812 if (Process.myUid() == Process.SYSTEM_UID) {
3813 // The getApplicationInfo() call we make below is not supported in system context, and
3814 // we want to allow the system to use these APIs anyway.
3815 return;
3816 }
3817
3818 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
3819 throw new UnsupportedOperationException(
3820 "This method is not supported in target SDK version " + version + " and above");
3821 }
3822 }
3823
3824 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
3825 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
Lifu Tang30f95a72016-01-07 23:20:38 -08003826 // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003827 // remove these exemptions. Note that this check is not secure, and apps can still access these
3828 // functions by accessing ConnectivityService directly. However, it should be clear that doing
3829 // so is unsupported and may break in the future. http://b/22728205
3830 private void checkLegacyRoutingApiAccess() {
3831 if (mContext.checkCallingOrSelfPermission("com.android.permission.INJECT_OMADM_SETTINGS")
3832 == PackageManager.PERMISSION_GRANTED) {
3833 return;
3834 }
3835
Dianne Hackborn692a2442015-07-31 10:35:34 -07003836 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003837 }
3838
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003839 /**
3840 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen72db88e2015-03-10 10:54:12 -04003841 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003842 *
3843 * @param network The {@link Network} to bind host resolutions from the current process to, or
3844 * {@code null} to clear the current binding.
3845 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3846 * @hide
3847 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
3848 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003849 @Deprecated
Mathew Inwoodfa3a7462018-08-08 14:52:47 +01003850 @UnsupportedAppUsage
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003851 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04003852 return NetworkUtils.bindProcessToNetworkForHostResolution(
Erik Klinef4fa9822018-04-27 22:48:33 +09003853 (network == null) ? NETID_UNSET : network.getNetIdForResolv());
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003854 }
Felipe Leme1b103232016-01-22 09:44:57 -08003855
3856 /**
3857 * Device is not restricting metered network activity while application is running on
3858 * background.
3859 */
3860 public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1;
3861
3862 /**
3863 * Device is restricting metered network activity while application is running on background,
3864 * but application is allowed to bypass it.
3865 * <p>
3866 * In this state, application should take action to mitigate metered network access.
3867 * For example, a music streaming application should switch to a low-bandwidth bitrate.
3868 */
3869 public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2;
3870
3871 /**
3872 * Device is restricting metered network activity while application is running on background.
Felipe Leme9778f762016-01-27 14:46:39 -08003873 * <p>
Felipe Leme1b103232016-01-22 09:44:57 -08003874 * In this state, application should not try to use the network while running on background,
3875 * because it would be denied.
3876 */
3877 public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3;
3878
Felipe Leme9778f762016-01-27 14:46:39 -08003879 /**
3880 * A change in the background metered network activity restriction has occurred.
3881 * <p>
3882 * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction
3883 * applies to them.
3884 * <p>
3885 * This is only sent to registered receivers, not manifest receivers.
3886 */
3887 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
3888 public static final String ACTION_RESTRICT_BACKGROUND_CHANGED =
3889 "android.net.conn.RESTRICT_BACKGROUND_CHANGED";
3890
Felipe Lemeecfccea2016-01-25 11:48:04 -08003891 /** @hide */
3892 @Retention(RetentionPolicy.SOURCE)
Felipe Leme1b103232016-01-22 09:44:57 -08003893 @IntDef(flag = false, value = {
3894 RESTRICT_BACKGROUND_STATUS_DISABLED,
3895 RESTRICT_BACKGROUND_STATUS_WHITELISTED,
3896 RESTRICT_BACKGROUND_STATUS_ENABLED,
3897 })
Felipe Leme1b103232016-01-22 09:44:57 -08003898 public @interface RestrictBackgroundStatus {
3899 }
3900
3901 private INetworkPolicyManager getNetworkPolicyManager() {
3902 synchronized (this) {
3903 if (mNPManager != null) {
3904 return mNPManager;
3905 }
3906 mNPManager = INetworkPolicyManager.Stub.asInterface(ServiceManager
3907 .getService(Context.NETWORK_POLICY_SERVICE));
3908 return mNPManager;
3909 }
3910 }
3911
3912 /**
3913 * Determines if the calling application is subject to metered network restrictions while
3914 * running on background.
Felipe Lemec9c7be52016-05-16 13:57:19 -07003915 *
3916 * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED},
3917 * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED},
3918 * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED}
Felipe Leme1b103232016-01-22 09:44:57 -08003919 */
3920 public @RestrictBackgroundStatus int getRestrictBackgroundStatus() {
3921 try {
3922 return getNetworkPolicyManager().getRestrictBackgroundByCaller();
3923 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003924 throw e.rethrowFromSystemServer();
Felipe Leme1b103232016-01-22 09:44:57 -08003925 }
3926 }
Ricky Wai44dcbde2018-01-23 04:09:45 +00003927
3928 /**
3929 * The network watchlist is a list of domains and IP addresses that are associated with
Ricky Waia86d5d52018-03-20 14:20:54 +00003930 * potentially harmful apps. This method returns the SHA-256 of the watchlist config file
3931 * currently used by the system for validation purposes.
Ricky Wai44dcbde2018-01-23 04:09:45 +00003932 *
3933 * @return Hash of network watchlist config file. Null if config does not exist.
3934 */
3935 public byte[] getNetworkWatchlistConfigHash() {
3936 try {
3937 return mService.getNetworkWatchlistConfigHash();
3938 } catch (RemoteException e) {
3939 Log.e(TAG, "Unable to get watchlist config hash");
3940 throw e.rethrowFromSystemServer();
3941 }
3942 }
Jeff Vander Stoep0ac2c092018-07-23 10:57:53 -07003943
3944 /**
3945 * Returns the {@code uid} of the owner of a network connection.
3946 *
3947 * @param protocol The protocol of the connection. Only {@code IPPROTO_TCP} and
3948 * {@code IPPROTO_UDP} currently supported.
3949 * @param local The local {@link InetSocketAddress} of a connection.
3950 * @param remote The remote {@link InetSocketAddress} of a connection.
3951 *
3952 * @return {@code uid} if the connection is found and the app has permission to observe it
3953 * (e.g., if it is associated with the calling VPN app's tunnel) or
3954 * {@link android.os.Process#INVALID_UID} if the connection is not found.
3955 */
3956 public int getConnectionOwnerUid(int protocol, InetSocketAddress local,
3957 InetSocketAddress remote) {
3958 ConnectionInfo connectionInfo = new ConnectionInfo(protocol, local, remote);
3959 try {
3960 return mService.getConnectionOwnerUid(connectionInfo);
3961 } catch (RemoteException e) {
3962 throw e.rethrowFromSystemServer();
3963 }
3964 }
Soi, Yoshinaridee2aa42015-11-12 12:09:02 +09003965
3966 private void printStackTrace() {
3967 if (DEBUG) {
3968 final StackTraceElement[] callStack = Thread.currentThread().getStackTrace();
3969 final StringBuffer sb = new StringBuffer();
3970 for (int i = 3; i < callStack.length; i++) {
3971 final String stackTrace = callStack[i].toString();
3972 if (stackTrace == null || stackTrace.contains("android.os")) {
3973 break;
3974 }
3975 sb.append(" [").append(stackTrace).append("]");
3976 }
3977 Log.d(TAG, "StackLog:" + sb.toString());
3978 }
3979 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003980}