blob: 7be708a16f83dde5b5e6e6461f775bbc293f8e39 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016package android.net;
17
Felipe Leme1b103232016-01-22 09:44:57 -080018import android.annotation.IntDef;
Robin Lee244ce8e2016-01-05 18:03:46 +000019import android.annotation.Nullable;
Jeff Sharkey30e06bb2017-04-24 11:18:03 -060020import android.annotation.RequiresPermission;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080021import android.annotation.SdkConstant;
22import android.annotation.SdkConstant.SdkConstantType;
Udam Sainib7c24872016-01-04 12:16:14 -080023import android.annotation.SystemApi;
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060024import android.annotation.SystemService;
Robert Greenwalt9258c642014-03-26 16:47:06 -070025import android.app.PendingIntent;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070026import android.content.Context;
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070027import android.content.Intent;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +090028import android.content.pm.PackageManager;
Robert Greenwalt42acef32009-08-12 16:08:25 -070029import android.os.Binder;
Jeff Sharkey3a844fc2011-08-16 14:37:57 -070030import android.os.Build.VERSION_CODES;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080031import android.os.Bundle;
Robert Greenwalt9258c642014-03-26 16:47:06 -070032import android.os.Handler;
33import android.os.HandlerThread;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080034import android.os.IBinder;
35import android.os.INetworkActivityListener;
36import android.os.INetworkManagementService;
Robert Greenwalt9258c642014-03-26 16:47:06 -070037import android.os.Looper;
38import android.os.Message;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -070039import android.os.Messenger;
Lorenzo Colittiffc42b02015-07-29 11:41:21 +090040import android.os.Process;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041import android.os.RemoteException;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080042import android.os.ResultReceiver;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080043import android.os.ServiceManager;
Hugo Benichicb883232017-05-11 13:16:17 +090044import android.os.ServiceSpecificException;
Jeff Sharkey961e3042011-08-29 16:02:57 -070045import android.provider.Settings;
Wink Saville36ffb042014-12-05 11:10:30 -080046import android.telephony.SubscriptionManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080047import android.util.ArrayMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -070048import android.util.Log;
Erik Kline35bf06c2017-01-26 18:08:28 +090049import android.util.SparseIntArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050
Robert Greenwaltafa05c02014-05-21 20:04:36 -070051import com.android.internal.telephony.ITelephony;
Robert Greenwalt562cc542014-05-15 18:07:26 -070052import com.android.internal.telephony.PhoneConstants;
Hugo Benichidafed3d2017-03-06 09:17:06 +090053import com.android.internal.util.Preconditions;
54import com.android.internal.util.Protocol;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070055
Paul Jensenc91b5342014-08-27 12:38:45 -040056import libcore.net.event.NetworkEventDispatcher;
57
Felipe Leme1b103232016-01-22 09:44:57 -080058import java.lang.annotation.Retention;
59import java.lang.annotation.RetentionPolicy;
Jeremy Kleind42209d2015-12-28 15:11:58 -080060import java.net.InetAddress;
Hugo Benichidafed3d2017-03-06 09:17:06 +090061import java.util.ArrayList;
Jeremy Kleind42209d2015-12-28 15:11:58 -080062import java.util.HashMap;
Hugo Benichidafed3d2017-03-06 09:17:06 +090063import java.util.List;
64import java.util.Map;
Jeremy Kleind42209d2015-12-28 15:11:58 -080065
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066/**
67 * Class that answers queries about the state of network connectivity. It also
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060068 * notifies applications when network connectivity changes.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 * <p>
70 * The primary responsibilities of this class are to:
71 * <ol>
72 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
73 * <li>Send broadcast intents when network connectivity changes</li>
74 * <li>Attempt to "fail over" to another network when connectivity to a network
75 * is lost</li>
76 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
77 * state of the available networks</li>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070078 * <li>Provide an API that allows applications to request and select networks for their data
79 * traffic</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080080 * </ol>
81 */
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -060082@SystemService(Context.CONNECTIVITY_SERVICE)
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070083public class ConnectivityManager {
84 private static final String TAG = "ConnectivityManager";
85
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070087 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080088 * been established or lost. The NetworkInfo for the affected network is
89 * sent as an extra; it should be consulted to see what kind of
90 * connectivity event occurred.
91 * <p/>
Mark Lu33ec1062016-12-05 10:57:55 -080092 * Apps targeting Android 7.0 (API level 24) and higher do not receive this
93 * broadcast if they declare the broadcast receiver in their manifest. Apps
94 * will still receive broadcasts if they register their
95 * {@link android.content.BroadcastReceiver} with
96 * {@link android.content.Context#registerReceiver Context.registerReceiver()}
97 * and that context is still valid.
98 * <p/>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 * If this is a connection that was the result of failing over from a
100 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
101 * set to true.
102 * <p/>
103 * For a loss of connectivity, if the connectivity manager is attempting
104 * to connect (or has already connected) to another network, the
105 * NetworkInfo for the new network is also passed as an extra. This lets
106 * any receivers of the broadcast know that they should not necessarily
107 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800108 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 * the failover attempt succeeded (and so there is still overall data
110 * connectivity), or that the failover attempt failed, meaning that all
111 * connectivity has been lost.
112 * <p/>
113 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
114 * is set to {@code true} if there are no connected networks at all.
Chalard Jean054cd162018-02-10 05:33:50 +0900115 *
116 * @deprecated apps should use the more versatile {@link #requestNetwork},
117 * {@link #registerNetworkCallback} or {@link #registerDefaultNetworkCallback}
118 * functions instead for faster and more detailed updates about the network
119 * changes they care about.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800121 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jean054cd162018-02-10 05:33:50 +0900122 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 /**
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700126 * A temporary hack until SUPL system can get off the legacy APIS.
127 * They do too many network requests and the long list of apps listening
Chalard Jean4d660112018-06-04 16:52:49 +0900128 * and waking due to the CONNECTIVITY_ACTION broadcast makes it expensive.
129 * Use this broadcast intent instead for SUPL requests.
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700130 * @hide
131 */
132 public static final String CONNECTIVITY_ACTION_SUPL =
133 "android.net.conn.CONNECTIVITY_CHANGE_SUPL";
134
135 /**
Paul Jensen25a217c2015-02-27 22:55:47 -0500136 * The device has connected to a network that has presented a captive
137 * portal, which is blocking Internet connectivity. The user was presented
138 * with a notification that network sign in is required,
139 * and the user invoked the notification's action indicating they
Paul Jensen49e3edf2015-05-22 10:50:39 -0400140 * desire to sign in to the network. Apps handling this activity should
Paul Jensen25a217c2015-02-27 22:55:47 -0500141 * facilitate signing in to the network. This action includes a
142 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents
143 * the network presenting the captive portal; all communication with the
144 * captive portal must be done using this {@code Network} object.
145 * <p/>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400146 * This activity includes a {@link CaptivePortal} extra named
147 * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different
148 * outcomes of the captive portal sign in to the system:
149 * <ul>
150 * <li> When the app handling this action believes the user has signed in to
151 * the network and the captive portal has been dismissed, the app should
152 * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can
153 * reevaluate the network. If reevaluation finds the network no longer
154 * subject to a captive portal, the network may become the default active
Chalard Jean4d660112018-06-04 16:52:49 +0900155 * data network.</li>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400156 * <li> When the app handling this action believes the user explicitly wants
Paul Jensen25a217c2015-02-27 22:55:47 -0500157 * to ignore the captive portal and the network, the app should call
Paul Jensen49e3edf2015-05-22 10:50:39 -0400158 * {@link CaptivePortal#ignoreNetwork}. </li>
159 * </ul>
Paul Jensen25a217c2015-02-27 22:55:47 -0500160 */
161 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
162 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL";
163
164 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165 * The lookup key for a {@link NetworkInfo} object. Retrieve with
166 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700167 *
168 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
169 * should always obtain network information through
Paul Jensen3541e9f2015-03-18 12:23:02 -0400170 * {@link #getActiveNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700171 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700173 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800174 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700175
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700177 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700178 *
179 * @see android.content.Intent#getIntExtra(String, int)
180 */
181 public static final String EXTRA_NETWORK_TYPE = "networkType";
182
183 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800184 * The lookup key for a boolean that indicates whether a connect event
185 * is for a network to which the connectivity manager was failing over
186 * following a disconnect on another network.
187 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
188 */
189 public static final String EXTRA_IS_FAILOVER = "isFailover";
190 /**
191 * The lookup key for a {@link NetworkInfo} object. This is supplied when
192 * there is another network that it may be possible to connect to. Retrieve with
193 * {@link android.content.Intent#getParcelableExtra(String)}.
194 */
195 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
196 /**
197 * The lookup key for a boolean that indicates whether there is a
198 * complete lack of connectivity, i.e., no network is available.
199 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
200 */
201 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
202 /**
203 * The lookup key for a string that indicates why an attempt to connect
204 * to a network failed. The string has no particular structure. It is
205 * intended to be used in notifications presented to users. Retrieve
206 * it with {@link android.content.Intent#getStringExtra(String)}.
207 */
208 public static final String EXTRA_REASON = "reason";
209 /**
210 * The lookup key for a string that provides optionally supplied
211 * extra information about the network state. The information
212 * may be passed up from the lower networking layers, and its
213 * meaning may be specific to a particular network type. Retrieve
214 * it with {@link android.content.Intent#getStringExtra(String)}.
215 */
216 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700217 /**
218 * The lookup key for an int that provides information about
219 * our connection to the internet at large. 0 indicates no connection,
220 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700221 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700222 * {@hide}
223 */
224 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 /**
Paul Jensen49e3edf2015-05-22 10:50:39 -0400226 * The lookup key for a {@link CaptivePortal} object included with the
227 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal}
228 * object can be used to either indicate to the system that the captive
229 * portal has been dismissed or that the user does not want to pursue
230 * signing in to captive portal. Retrieve it with
231 * {@link android.content.Intent#getParcelableExtra(String)}.
Paul Jensen25a217c2015-02-27 22:55:47 -0500232 */
Paul Jensen49e3edf2015-05-22 10:50:39 -0400233 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL";
Jan Nordqvist52eb29f2015-09-22 15:54:32 -0700234
235 /**
236 * Key for passing a URL to the captive portal login activity.
237 */
238 public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL";
239
Paul Jensen25a217c2015-02-27 22:55:47 -0500240 /**
Remi NGUYEN VAN13e6e212018-05-22 10:01:53 +0900241 * Key for passing a {@link android.net.captiveportal.CaptivePortalProbeSpec} to the captive
242 * portal login activity.
243 * {@hide}
244 */
245 public static final String EXTRA_CAPTIVE_PORTAL_PROBE_SPEC =
246 "android.net.extra.CAPTIVE_PORTAL_PROBE_SPEC";
247
248 /**
Hugo Benichicdf3ba42016-12-14 08:23:40 +0900249 * Key for passing a user agent string to the captive portal login activity.
250 * {@hide}
251 */
252 public static final String EXTRA_CAPTIVE_PORTAL_USER_AGENT =
253 "android.net.extra.CAPTIVE_PORTAL_USER_AGENT";
254
255 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700256 * Broadcast action to indicate the change of data activity status
257 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800258 * The network becomes active when data transmission is started, or
259 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700260 * {@hide}
261 */
262 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chalard Jean4d660112018-06-04 16:52:49 +0900263 public static final String ACTION_DATA_ACTIVITY_CHANGE =
264 "android.net.conn.DATA_ACTIVITY_CHANGE";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700265 /**
266 * The lookup key for an enum that indicates the network device type on which this data activity
267 * change happens.
268 * {@hide}
269 */
270 public static final String EXTRA_DEVICE_TYPE = "deviceType";
271 /**
272 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
273 * it is actively sending or receiving data and {@code false} means it is idle.
274 * {@hide}
275 */
276 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700277 /**
278 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
279 * {@hide}
280 */
281 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700282
283 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800284 * Broadcast Action: The setting for background data usage has changed
285 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
286 * <p>
287 * If an application uses the network in the background, it should listen
288 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700289 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800290 * <p>
291 *
292 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
293 * of background data depends on several combined factors, and
294 * this broadcast is no longer sent. Instead, when background
295 * data is unavailable, {@link #getActiveNetworkInfo()} will now
296 * appear disconnected. During first boot after a platform
297 * upgrade, this broadcast will be sent once if
298 * {@link #getBackgroundDataSetting()} was {@code false} before
299 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800300 */
301 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800302 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800303 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
304 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
305
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700306 /**
307 * Broadcast Action: The network connection may not be good
308 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
309 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
310 * the network and it's condition.
311 * @hide
312 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800313 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700314 public static final String INET_CONDITION_ACTION =
315 "android.net.conn.INET_CONDITION_ACTION";
316
Robert Greenwalt42acef32009-08-12 16:08:25 -0700317 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800318 * Broadcast Action: A tetherable connection has come or gone.
319 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
Erik Kline8351faa2017-04-17 16:47:23 +0900320 * {@code ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY},
321 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER}, and
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800322 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
323 * the current state of tethering. Each include a list of
324 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800325 * @hide
326 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800327 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800328 public static final String ACTION_TETHER_STATE_CHANGED =
329 "android.net.conn.TETHER_STATE_CHANGED";
330
331 /**
332 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800333 * gives a String[] listing all the interfaces configured for
334 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800335 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800336 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800337
338 /**
339 * @hide
Erik Kline8351faa2017-04-17 16:47:23 +0900340 * gives a String[] listing all the interfaces currently in local-only
341 * mode (ie, has DHCPv4+IPv6-ULA support and no packet forwarding)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800342 */
Erik Kline8351faa2017-04-17 16:47:23 +0900343 public static final String EXTRA_ACTIVE_LOCAL_ONLY = "localOnlyArray";
344
345 /**
346 * @hide
347 * gives a String[] listing all the interfaces currently tethered
348 * (ie, has DHCPv4 support and packets potentially forwarded/NATed)
349 */
350 public static final String EXTRA_ACTIVE_TETHER = "tetherArray";
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800351
352 /**
353 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800354 * gives a String[] listing all the interfaces we tried to tether and
355 * failed. Use {@link #getLastTetherError} to find the error code
356 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800357 */
358 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800359
360 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800361 * Broadcast Action: The captive portal tracker has finished its test.
362 * Sent only while running Setup Wizard, in lieu of showing a user
363 * notification.
364 * @hide
365 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800366 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800367 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
368 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
369 /**
370 * The lookup key for a boolean that indicates whether a captive portal was detected.
371 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
372 * @hide
373 */
374 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
375
376 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900377 * Action used to display a dialog that asks the user whether to connect to a network that is
378 * not validated. This intent is used to start the dialog in settings via startActivity.
379 *
380 * @hide
381 */
382 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED";
383
384 /**
Lorenzo Colitti9be58c52016-09-15 14:02:29 +0900385 * Action used to display a dialog that asks the user whether to avoid a network that is no
386 * longer validated. This intent is used to start the dialog in settings via startActivity.
387 *
388 * @hide
389 */
390 public static final String ACTION_PROMPT_LOST_VALIDATION =
391 "android.net.conn.PROMPT_LOST_VALIDATION";
392
393 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800394 * Invalid tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900395 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800396 * @hide
397 */
398 public static final int TETHERING_INVALID = -1;
399
400 /**
401 * Wifi tethering type.
Chalard Jean4d660112018-06-04 16:52:49 +0900402 * @see #startTethering(int, boolean, OnStartTetheringCallback)
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800403 * @hide
404 */
405 @SystemApi
406 public static final int TETHERING_WIFI = 0;
407
408 /**
409 * USB 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_USB = 1;
415
416 /**
417 * Bluetooth 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_BLUETOOTH = 2;
423
424 /**
425 * Extra used for communicating with the TetherService. Includes the type of tethering to
426 * enable if any.
427 * @hide
428 */
429 public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType";
430
431 /**
432 * Extra used for communicating with the TetherService. Includes the type of tethering for
433 * which to cancel provisioning.
434 * @hide
435 */
436 public static final String EXTRA_REM_TETHER_TYPE = "extraRemTetherType";
437
438 /**
439 * Extra used for communicating with the TetherService. True to schedule a recheck of tether
440 * provisioning.
441 * @hide
442 */
443 public static final String EXTRA_SET_ALARM = "extraSetAlarm";
444
445 /**
446 * Tells the TetherService to run a provision check now.
447 * @hide
448 */
449 public static final String EXTRA_RUN_PROVISION = "extraRunProvision";
450
451 /**
452 * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver}
453 * which will receive provisioning results. Can be left empty.
454 * @hide
455 */
456 public static final String EXTRA_PROVISION_CALLBACK = "extraProvisionCallback";
457
458 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800459 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700460 * @hide
461 */
462 public static final int TYPE_NONE = -1;
463
464 /**
Chalard Jean8117f932018-03-08 13:54:53 +0900465 * A Mobile data connection. Devices may support more than one.
466 *
467 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
468 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
469 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700470 */
Chalard Jean8117f932018-03-08 13:54:53 +0900471 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700472 public static final int TYPE_MOBILE = 0;
Chalard Jean8117f932018-03-08 13:54:53 +0900473
Robert Greenwalt42acef32009-08-12 16:08:25 -0700474 /**
Chalard Jean8117f932018-03-08 13:54:53 +0900475 * A WIFI data connection. Devices may support more than one.
476 *
477 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
478 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
479 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700480 */
Chalard Jean8117f932018-03-08 13:54:53 +0900481 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700482 public static final int TYPE_WIFI = 1;
Chalard Jean8117f932018-03-08 13:54:53 +0900483
Robert Greenwalt42acef32009-08-12 16:08:25 -0700484 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800485 * An MMS-specific Mobile data connection. This network type may use the
486 * same network interface as {@link #TYPE_MOBILE} or it may use a different
487 * one. This is used by applications needing to talk to the carrier's
488 * Multimedia Messaging Service servers.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900489 *
Chalard Jean8117f932018-03-08 13:54:53 +0900490 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900491 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900492 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700493 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700494 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700495 public static final int TYPE_MOBILE_MMS = 2;
Chalard Jean8117f932018-03-08 13:54:53 +0900496
Robert Greenwalt42acef32009-08-12 16:08:25 -0700497 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800498 * A SUPL-specific Mobile data connection. This network type may use the
499 * same network interface as {@link #TYPE_MOBILE} or it may use a different
500 * one. This is used by applications needing to talk to the carrier's
501 * Secure User Plane Location servers for help locating the device.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900502 *
Chalard Jean8117f932018-03-08 13:54:53 +0900503 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900504 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900505 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700506 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700507 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700508 public static final int TYPE_MOBILE_SUPL = 3;
Chalard Jean8117f932018-03-08 13:54:53 +0900509
Robert Greenwalt42acef32009-08-12 16:08:25 -0700510 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800511 * A DUN-specific Mobile data connection. This network type may use the
512 * same network interface as {@link #TYPE_MOBILE} or it may use a different
513 * one. This is sometimes by the system when setting up an upstream connection
514 * for tethering so that the carrier is aware of DUN traffic.
Chalard Jean8117f932018-03-08 13:54:53 +0900515 *
516 * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or
517 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
518 * provides the {@link NetworkCapabilities#NET_CAPABILITY_DUN} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700519 */
Chalard Jean8117f932018-03-08 13:54:53 +0900520 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700521 public static final int TYPE_MOBILE_DUN = 4;
Chalard Jean8117f932018-03-08 13:54:53 +0900522
Robert Greenwalt42acef32009-08-12 16:08:25 -0700523 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800524 * A High Priority Mobile data connection. This network type uses the
525 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colittie285b432015-04-23 15:32:42 +0900526 * is different.
527 *
Chalard Jean8117f932018-03-08 13:54:53 +0900528 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
529 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
530 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700531 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700532 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700533 public static final int TYPE_MOBILE_HIPRI = 5;
Chalard Jean8117f932018-03-08 13:54:53 +0900534
jsh8214deb2010-03-11 15:04:43 -0800535 /**
Chalard Jean8117f932018-03-08 13:54:53 +0900536 * A WiMAX data connection.
537 *
538 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
539 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
540 * appropriate network. {@see NetworkCapabilities} for supported transports.
jsh8214deb2010-03-11 15:04:43 -0800541 */
Chalard Jean8117f932018-03-08 13:54:53 +0900542 @Deprecated
jsh8214deb2010-03-11 15:04:43 -0800543 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800544
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800545 /**
Chalard Jean8117f932018-03-08 13:54:53 +0900546 * A Bluetooth data connection.
547 *
548 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
549 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
550 * appropriate network. {@see NetworkCapabilities} for supported transports.
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800551 */
Chalard Jean8117f932018-03-08 13:54:53 +0900552 @Deprecated
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800553 public static final int TYPE_BLUETOOTH = 7;
554
Robert Greenwalt60810842011-04-22 15:28:18 -0700555 /**
556 * Dummy data connection. This should not be used on shipping devices.
Chalard Jean8117f932018-03-08 13:54:53 +0900557 * @deprecated This is not used any more.
Robert Greenwalt60810842011-04-22 15:28:18 -0700558 */
Chalard Jean8117f932018-03-08 13:54:53 +0900559 @Deprecated
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800560 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800561
Robert Greenwalt60810842011-04-22 15:28:18 -0700562 /**
Chalard Jean8117f932018-03-08 13:54:53 +0900563 * An Ethernet data connection.
564 *
565 * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or
566 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an
567 * appropriate network. {@see NetworkCapabilities} for supported transports.
Robert Greenwalt60810842011-04-22 15:28:18 -0700568 */
Chalard Jean8117f932018-03-08 13:54:53 +0900569 @Deprecated
Robert Greenwalte12aec92011-01-28 14:48:37 -0800570 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700571
Wink Saville9d7d6282011-03-12 14:52:01 -0800572 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800573 * Over the air Administration.
Chalard Jean8117f932018-03-08 13:54:53 +0900574 * @deprecated Use {@link NetworkCapabilities} instead.
Wink Saville9d7d6282011-03-12 14:52:01 -0800575 * {@hide}
576 */
Chalard Jean8117f932018-03-08 13:54:53 +0900577 @Deprecated
Wink Saville9d7d6282011-03-12 14:52:01 -0800578 public static final int TYPE_MOBILE_FOTA = 10;
579
580 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800581 * IP Multimedia Subsystem.
Chalard Jean8117f932018-03-08 13:54:53 +0900582 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IMS} instead.
Wink Saville9d7d6282011-03-12 14:52:01 -0800583 * {@hide}
584 */
Chalard Jean8117f932018-03-08 13:54:53 +0900585 @Deprecated
Wink Saville9d7d6282011-03-12 14:52:01 -0800586 public static final int TYPE_MOBILE_IMS = 11;
587
588 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800589 * Carrier Branded Services.
Chalard Jean8117f932018-03-08 13:54:53 +0900590 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_CBS} instead.
Wink Saville9d7d6282011-03-12 14:52:01 -0800591 * {@hide}
592 */
Chalard Jean8117f932018-03-08 13:54:53 +0900593 @Deprecated
Wink Saville9d7d6282011-03-12 14:52:01 -0800594 public static final int TYPE_MOBILE_CBS = 12;
595
repo syncaea743a2011-07-29 23:55:49 -0700596 /**
597 * A Wi-Fi p2p connection. Only requesting processes will have access to
598 * the peers connected.
Chalard Jean8117f932018-03-08 13:54:53 +0900599 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_WIFI_P2P} instead.
repo syncaea743a2011-07-29 23:55:49 -0700600 * {@hide}
601 */
Chalard Jean8117f932018-03-08 13:54:53 +0900602 @Deprecated
repo syncaea743a2011-07-29 23:55:49 -0700603 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800604
Wink Saville5e56bc52013-07-29 15:00:57 -0700605 /**
606 * The network to use for initially attaching to the network
Chalard Jean8117f932018-03-08 13:54:53 +0900607 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IA} instead.
Wink Saville5e56bc52013-07-29 15:00:57 -0700608 * {@hide}
609 */
Chalard Jean8117f932018-03-08 13:54:53 +0900610 @Deprecated
Wink Saville5e56bc52013-07-29 15:00:57 -0700611 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700612
Lorenzo Colittie285b432015-04-23 15:32:42 +0900613 /**
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700614 * Emergency PDN connection for emergency services. This
615 * may include IMS and MMS in emergency situations.
Chalard Jean8117f932018-03-08 13:54:53 +0900616 * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_EIMS} instead.
Ram3e0e3bc2014-06-26 11:03:44 -0700617 * {@hide}
618 */
Chalard Jean8117f932018-03-08 13:54:53 +0900619 @Deprecated
Ram3e0e3bc2014-06-26 11:03:44 -0700620 public static final int TYPE_MOBILE_EMERGENCY = 15;
621
Hui Lu1c5624a2014-01-15 11:05:36 -0500622 /**
623 * The network that uses proxy to achieve connectivity.
Chalard Jean8117f932018-03-08 13:54:53 +0900624 * @deprecated Use {@link NetworkCapabilities} instead.
Hui Lu1c5624a2014-01-15 11:05:36 -0500625 * {@hide}
626 */
Chalard Jean8117f932018-03-08 13:54:53 +0900627 @Deprecated
Hui Lu1c5624a2014-01-15 11:05:36 -0500628 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700629
Robert Greenwalt8283f882014-07-07 17:09:01 -0700630 /**
631 * A virtual network using one or more native bearers.
632 * It may or may not be providing security services.
Chalard Jean8117f932018-03-08 13:54:53 +0900633 * @deprecated Applications should use {@link NetworkCapabilities#TRANSPORT_VPN} instead.
Robert Greenwalt8283f882014-07-07 17:09:01 -0700634 */
Chalard Jean8117f932018-03-08 13:54:53 +0900635 @Deprecated
Robert Greenwalt8283f882014-07-07 17:09:01 -0700636 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500637
638 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700639 public static final int MAX_RADIO_TYPE = TYPE_VPN;
640
641 /** {@hide} */
642 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800643
Hugo Benichi16f0a942017-06-20 14:07:59 +0900644 private static final int MIN_NETWORK_TYPE = TYPE_MOBILE;
645
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800646 /**
647 * If you want to set the default network preference,you can directly
648 * change the networkAttributes array in framework's config.xml.
649 *
650 * @deprecated Since we support so many more networks now, the single
651 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800652 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800653 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800654 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800655 * from an App.
656 */
657 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800658 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
659
Jeff Sharkey625239a2012-09-26 22:03:49 -0700660 /**
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700661 * @hide
662 */
Hugo Benichia5c1f7f2017-06-20 14:10:14 +0900663 public static final int REQUEST_ID_UNSET = 0;
Robert Greenwalt7569f182014-06-08 16:42:59 -0700664
Paul Jensen5d59e782014-07-11 12:28:19 -0400665 /**
Hugo Benichi31c176d2017-06-17 13:14:12 +0900666 * Static unique request used as a tombstone for NetworkCallbacks that have been unregistered.
667 * This allows to distinguish when unregistering NetworkCallbacks those that were never
Chalard Jean4d660112018-06-04 16:52:49 +0900668 * registered from those that were already unregistered.
Hugo Benichi31c176d2017-06-17 13:14:12 +0900669 * @hide
670 */
Hugo Benichia5c1f7f2017-06-20 14:10:14 +0900671 private static final NetworkRequest ALREADY_UNREGISTERED =
Hugo Benichi31c176d2017-06-17 13:14:12 +0900672 new NetworkRequest.Builder().clearCapabilities().build();
673
674 /**
Paul Jensen5d59e782014-07-11 12:28:19 -0400675 * A NetID indicating no Network is selected.
676 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
677 * @hide
678 */
679 public static final int NETID_UNSET = 0;
680
Erik Kline4d092232017-10-30 15:29:44 +0900681 /**
682 * Private DNS Mode values.
683 *
684 * The "private_dns_mode" global setting stores a String value which is
685 * expected to be one of the following.
686 */
687
688 /**
689 * @hide
690 */
691 public static final String PRIVATE_DNS_MODE_OFF = "off";
692 /**
693 * @hide
694 */
695 public static final String PRIVATE_DNS_MODE_OPPORTUNISTIC = "opportunistic";
696 /**
697 * @hide
698 */
699 public static final String PRIVATE_DNS_MODE_PROVIDER_HOSTNAME = "hostname";
700 /**
701 * The default Private DNS mode.
702 *
703 * This may change from release to release or may become dependent upon
704 * the capabilities of the underlying platform.
705 *
706 * @hide
707 */
708 public static final String PRIVATE_DNS_DEFAULT_MODE = PRIVATE_DNS_MODE_OPPORTUNISTIC;
709
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700710 private final IConnectivityManager mService;
Paul Jensene0bef712014-12-10 15:12:18 -0500711 /**
712 * A kludge to facilitate static access where a Context pointer isn't available, like in the
713 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
714 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
715 * methods that take a Context argument.
716 */
717 private static ConnectivityManager sInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800718
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900719 private final Context mContext;
720
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800721 private INetworkManagementService mNMService;
Felipe Leme1b103232016-01-22 09:44:57 -0800722 private INetworkPolicyManager mNPManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800723
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800724 /**
725 * Tests if a given integer represents a valid network type.
726 * @param networkType the type to be tested
727 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensen9e59e122015-05-06 10:42:25 -0400728 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
729 * validate a network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800730 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700731 @Deprecated
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700732 public static boolean isNetworkTypeValid(int networkType) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900733 return MIN_NETWORK_TYPE <= networkType && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800734 }
735
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800736 /**
737 * Returns a non-localized string representing a given network type.
738 * ONLY used for debugging output.
739 * @param type the type needing naming
740 * @return a String for the given type, or a string version of the type ("87")
741 * if no name is known.
Chalard Jean8117f932018-03-08 13:54:53 +0900742 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800743 * {@hide}
744 */
Chalard Jean8117f932018-03-08 13:54:53 +0900745 @Deprecated
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700746 public static String getNetworkTypeName(int type) {
747 switch (type) {
Hugo Benichi16f0a942017-06-20 14:07:59 +0900748 case TYPE_NONE:
749 return "NONE";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700750 case TYPE_MOBILE:
751 return "MOBILE";
752 case TYPE_WIFI:
753 return "WIFI";
754 case TYPE_MOBILE_MMS:
755 return "MOBILE_MMS";
756 case TYPE_MOBILE_SUPL:
757 return "MOBILE_SUPL";
758 case TYPE_MOBILE_DUN:
759 return "MOBILE_DUN";
760 case TYPE_MOBILE_HIPRI:
761 return "MOBILE_HIPRI";
762 case TYPE_WIMAX:
763 return "WIMAX";
764 case TYPE_BLUETOOTH:
765 return "BLUETOOTH";
766 case TYPE_DUMMY:
767 return "DUMMY";
768 case TYPE_ETHERNET:
769 return "ETHERNET";
770 case TYPE_MOBILE_FOTA:
771 return "MOBILE_FOTA";
772 case TYPE_MOBILE_IMS:
773 return "MOBILE_IMS";
774 case TYPE_MOBILE_CBS:
775 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700776 case TYPE_WIFI_P2P:
777 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700778 case TYPE_MOBILE_IA:
779 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700780 case TYPE_MOBILE_EMERGENCY:
781 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500782 case TYPE_PROXY:
783 return "PROXY";
Erik Kline37fbfa12014-11-19 17:23:41 +0900784 case TYPE_VPN:
785 return "VPN";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700786 default:
787 return Integer.toString(type);
788 }
789 }
790
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800791 /**
792 * Checks if a given type uses the cellular data connection.
793 * This should be replaced in the future by a network property.
794 * @param networkType the type to check
795 * @return a boolean - {@code true} if uses cellular network, else {@code false}
Chalard Jean8117f932018-03-08 13:54:53 +0900796 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800797 * {@hide}
798 */
Chalard Jean8117f932018-03-08 13:54:53 +0900799 @Deprecated
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700800 public static boolean isNetworkTypeMobile(int networkType) {
801 switch (networkType) {
802 case TYPE_MOBILE:
803 case TYPE_MOBILE_MMS:
804 case TYPE_MOBILE_SUPL:
805 case TYPE_MOBILE_DUN:
806 case TYPE_MOBILE_HIPRI:
807 case TYPE_MOBILE_FOTA:
808 case TYPE_MOBILE_IMS:
809 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700810 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700811 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700812 return true;
813 default:
814 return false;
815 }
816 }
817
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800818 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700819 * Checks if the given network type is backed by a Wi-Fi radio.
820 *
Chalard Jean8117f932018-03-08 13:54:53 +0900821 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700822 * @hide
823 */
Chalard Jean8117f932018-03-08 13:54:53 +0900824 @Deprecated
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700825 public static boolean isNetworkTypeWifi(int networkType) {
826 switch (networkType) {
827 case TYPE_WIFI:
828 case TYPE_WIFI_P2P:
829 return true;
830 default:
831 return false;
832 }
833 }
834
835 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800836 * Specifies the preferred network type. When the device has more
837 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800838 *
839 * @param preference the network type to prefer over all others. It is
840 * unspecified what happens to the old preferred network in the
841 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700842 * @deprecated Functionality has been removed as it no longer makes sense,
843 * with many more than two networks - we'd need an array to express
844 * preference. Instead we use dynamic network properties of
845 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800846 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700847 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 }
850
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800851 /**
852 * Retrieves the current preferred network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800853 *
854 * @return an integer representing the preferred network type
855 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700856 * @deprecated Functionality has been removed as it no longer makes sense,
857 * with many more than two networks - we'd need an array to express
858 * preference. Instead we use dynamic network properties of
859 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800860 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700861 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600862 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700864 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800865 }
866
Scott Main671644c2011-10-06 19:02:28 -0700867 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800868 * Returns details about the currently active default data network. When
869 * connected, this network is the default route for outgoing connections.
870 * You should always check {@link NetworkInfo#isConnected()} before initiating
871 * network traffic. This may return {@code null} when there is no default
872 * network.
Chalard Jeane8785fb2018-03-29 17:45:24 +0900873 * Note that if the default network is a VPN, this method will return the
874 * NetworkInfo for one of its underlying networks instead, or null if the
875 * VPN agent did not specify any. Apps interested in learning about VPNs
876 * should use {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800877 *
878 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500879 * or {@code null} if no default network is currently active
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700880 */
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 NetworkInfo getActiveNetworkInfo() {
883 try {
884 return mService.getActiveNetworkInfo();
885 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700886 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 }
888 }
889
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800890 /**
Paul Jensen31a94f42015-02-13 14:18:39 -0500891 * Returns a {@link Network} object corresponding to the currently active
892 * default data network. In the event that the current active default data
893 * network disconnects, the returned {@code Network} object will no longer
894 * be usable. This will return {@code null} when there is no default
895 * network.
896 *
897 * @return a {@link Network} object for the current default network or
898 * {@code null} if no default network is currently active
Paul Jensen31a94f42015-02-13 14:18:39 -0500899 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600900 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Paul Jensen31a94f42015-02-13 14:18:39 -0500901 public Network getActiveNetwork() {
902 try {
903 return mService.getActiveNetwork();
904 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700905 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -0500906 }
907 }
908
909 /**
Robin Leed2baf792016-03-24 12:07:00 +0000910 * Returns a {@link Network} object corresponding to the currently active
911 * default data network for a specific UID. In the event that the 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 for the UID.
Robin Leed2baf792016-03-24 12:07:00 +0000915 *
916 * @return a {@link Network} object for the current default network for the
917 * given UID or {@code null} if no default network is currently active
918 *
919 * @hide
920 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -0600921 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Robin Leed2baf792016-03-24 12:07:00 +0000922 public Network getActiveNetworkForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600923 return getActiveNetworkForUid(uid, false);
924 }
925
926 /** {@hide} */
927 public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) {
Robin Leed2baf792016-03-24 12:07:00 +0000928 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600929 return mService.getActiveNetworkForUid(uid, ignoreBlocked);
Robin Leed2baf792016-03-24 12:07:00 +0000930 } catch (RemoteException e) {
931 throw e.rethrowFromSystemServer();
932 }
933 }
934
935 /**
Charles Hea0a87e82017-05-15 17:07:18 +0100936 * Checks if a VPN app supports always-on mode.
937 *
938 * In order to support the always-on feature, an app has to
939 * <ul>
940 * <li>target {@link VERSION_CODES#N API 24} or above, and
Charles He5da5ae32017-08-15 15:30:22 +0100941 * <li>not opt out through the {@link VpnService#SERVICE_META_DATA_SUPPORTS_ALWAYS_ON}
942 * meta-data field.
Charles Hea0a87e82017-05-15 17:07:18 +0100943 * </ul>
944 *
945 * @param userId The identifier of the user for whom the VPN app is installed.
946 * @param vpnPackage The canonical package name of the VPN app.
947 * @return {@code true} if and only if the VPN app exists and supports always-on mode.
948 * @hide
949 */
950 public boolean isAlwaysOnVpnPackageSupportedForUser(int userId, @Nullable String vpnPackage) {
951 try {
952 return mService.isAlwaysOnVpnPackageSupported(userId, vpnPackage);
953 } catch (RemoteException e) {
954 throw e.rethrowFromSystemServer();
955 }
956 }
957
958 /**
Robin Lee244ce8e2016-01-05 18:03:46 +0000959 * Configures an always-on VPN connection through a specific application.
960 * This connection is automatically granted and persisted after a reboot.
961 *
962 * <p>The designated package should declare a {@link VpnService} in its
963 * manifest guarded by {@link android.Manifest.permission.BIND_VPN_SERVICE},
964 * otherwise the call will fail.
965 *
966 * @param userId The identifier of the user to set an always-on VPN for.
967 * @param vpnPackage The package name for an installed VPN app on the device, or {@code null}
968 * to remove an existing always-on VPN configuration.
Robin Leedc679712016-05-03 13:23:03 +0100969 * @param lockdownEnabled {@code true} to disallow networking when the VPN is not connected or
970 * {@code false} otherwise.
Robin Lee244ce8e2016-01-05 18:03:46 +0000971 * @return {@code true} if the package is set as always-on VPN controller;
972 * {@code false} otherwise.
973 * @hide
974 */
Robin Leedc679712016-05-03 13:23:03 +0100975 public boolean setAlwaysOnVpnPackageForUser(int userId, @Nullable String vpnPackage,
976 boolean lockdownEnabled) {
Robin Lee244ce8e2016-01-05 18:03:46 +0000977 try {
Robin Leedc679712016-05-03 13:23:03 +0100978 return mService.setAlwaysOnVpnPackage(userId, vpnPackage, lockdownEnabled);
Robin Lee244ce8e2016-01-05 18:03:46 +0000979 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700980 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +0000981 }
982 }
983
984 /**
985 * Returns the package name of the currently set always-on VPN application.
986 * If there is no always-on VPN set, or the VPN is provided by the system instead
987 * of by an app, {@code null} will be returned.
988 *
989 * @return Package name of VPN controller responsible for always-on VPN,
990 * or {@code null} if none is set.
991 * @hide
992 */
993 public String getAlwaysOnVpnPackageForUser(int userId) {
994 try {
995 return mService.getAlwaysOnVpnPackage(userId);
996 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700997 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +0000998 }
999 }
1000
1001 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001002 * Returns details about the currently active default data network
1003 * for a given uid. This is for internal use only to avoid spying
1004 * other apps.
1005 *
1006 * @return a {@link NetworkInfo} object for the current default network
1007 * for the given uid or {@code null} if no default network is
1008 * available for the specified uid.
1009 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001010 * {@hide}
1011 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001012 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001013 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001014 return getActiveNetworkInfoForUid(uid, false);
1015 }
1016
1017 /** {@hide} */
1018 public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001019 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001020 return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001021 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001022 throw e.rethrowFromSystemServer();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -07001023 }
1024 }
1025
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001026 /**
1027 * Returns connection status information about a particular
1028 * network type.
1029 *
1030 * @param networkType integer specifying which networkType in
1031 * which you're interested.
1032 * @return a {@link NetworkInfo} object for the requested
1033 * network type or {@code null} if the type is not
Chalard Jeane8785fb2018-03-29 17:45:24 +09001034 * supported by the device. If {@code networkType} is
1035 * TYPE_VPN and a VPN is active for the calling app,
1036 * then this method will try to return one of the
1037 * underlying networks for the VPN or null if the
1038 * VPN agent didn't specify any.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001039 *
Paul Jensen3541e9f2015-03-18 12:23:02 -04001040 * @deprecated This method does not support multiple connected networks
1041 * of the same type. Use {@link #getAllNetworks} and
1042 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001043 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001044 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001045 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001046 public NetworkInfo getNetworkInfo(int networkType) {
1047 try {
1048 return mService.getNetworkInfo(networkType);
1049 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001050 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001051 }
1052 }
1053
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001054 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001055 * Returns connection status information about a particular
1056 * Network.
1057 *
1058 * @param network {@link Network} specifying which network
1059 * in which you're interested.
1060 * @return a {@link NetworkInfo} object for the requested
1061 * network or {@code null} if the {@code Network}
1062 * is not valid.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001063 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001064 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001065 public NetworkInfo getNetworkInfo(Network network) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001066 return getNetworkInfoForUid(network, Process.myUid(), false);
1067 }
1068
1069 /** {@hide} */
1070 public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001071 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -06001072 return mService.getNetworkInfoForUid(network, uid, ignoreBlocked);
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001073 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001074 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001075 }
1076 }
1077
1078 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001079 * Returns connection status information about all network
1080 * types supported by the device.
1081 *
1082 * @return an array of {@link NetworkInfo} objects. Check each
1083 * {@link NetworkInfo#getType} for which type each applies.
1084 *
Paul Jensen3541e9f2015-03-18 12:23:02 -04001085 * @deprecated This method does not support multiple connected networks
1086 * of the same type. Use {@link #getAllNetworks} and
1087 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001088 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001089 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001090 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001091 public NetworkInfo[] getAllNetworkInfo() {
1092 try {
1093 return mService.getAllNetworkInfo();
1094 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001095 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001096 }
1097 }
1098
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001099 /**
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001100 * Returns the {@link Network} object currently serving a given type, or
1101 * null if the given type is not connected.
1102 *
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001103 * @hide
Paul Jensen3541e9f2015-03-18 12:23:02 -04001104 * @deprecated This method does not support multiple connected networks
1105 * of the same type. Use {@link #getAllNetworks} and
1106 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001107 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001108 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001109 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001110 public Network getNetworkForType(int networkType) {
1111 try {
1112 return mService.getNetworkForType(networkType);
1113 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001114 throw e.rethrowFromSystemServer();
Lorenzo Colittib57edc52014-08-22 17:10:50 -07001115 }
1116 }
1117
1118 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001119 * Returns an array of all {@link Network} currently tracked by the
1120 * framework.
Paul Jensenb2748922015-05-06 11:10:18 -04001121 *
1122 * @return an array of {@link Network} objects.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001123 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001124 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001125 public Network[] getAllNetworks() {
1126 try {
1127 return mService.getAllNetworks();
1128 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001129 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -07001130 }
1131 }
1132
1133 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001134 * Returns an array of {@link android.net.NetworkCapabilities} objects, representing
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001135 * the Networks that applications run by the given user will use by default.
1136 * @hide
1137 */
1138 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
1139 try {
1140 return mService.getDefaultNetworkCapabilitiesForUser(userId);
1141 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001142 throw e.rethrowFromSystemServer();
Lorenzo Colitti403aa262014-11-28 11:21:30 +09001143 }
1144 }
1145
1146 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001147 * Returns the IP information for the current default network.
1148 *
1149 * @return a {@link LinkProperties} object describing the IP info
1150 * for the current default network, or {@code null} if there
1151 * is no current default network.
1152 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001153 * {@hide}
1154 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001155 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001156 public LinkProperties getActiveLinkProperties() {
1157 try {
1158 return mService.getActiveLinkProperties();
1159 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001160 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001161 }
1162 }
1163
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001164 /**
1165 * Returns the IP information for a given network type.
1166 *
1167 * @param networkType the network type of interest.
1168 * @return a {@link LinkProperties} object describing the IP info
1169 * for the given networkType, or {@code null} if there is
1170 * no current default network.
1171 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001172 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04001173 * @deprecated This method does not support multiple connected networks
1174 * of the same type. Use {@link #getAllNetworks},
1175 * {@link #getNetworkInfo(android.net.Network)}, and
1176 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001177 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001178 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001179 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001180 public LinkProperties getLinkProperties(int networkType) {
1181 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001182 return mService.getLinkPropertiesForType(networkType);
1183 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001184 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001185 }
1186 }
1187
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001188 /**
1189 * Get the {@link LinkProperties} for the given {@link Network}. This
1190 * will return {@code null} if the network is unknown.
1191 *
1192 * @param network The {@link Network} object identifying the network in question.
1193 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensenb2748922015-05-06 11:10:18 -04001194 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001195 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt9258c642014-03-26 16:47:06 -07001196 public LinkProperties getLinkProperties(Network network) {
1197 try {
1198 return mService.getLinkProperties(network);
1199 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001200 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001201 }
1202 }
1203
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001204 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001205 * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001206 * will return {@code null} if the network is unknown.
1207 *
1208 * @param network The {@link Network} object identifying the network in question.
Lorenzo Colittie285b432015-04-23 15:32:42 +09001209 * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001210 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06001211 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt9258c642014-03-26 16:47:06 -07001212 public NetworkCapabilities getNetworkCapabilities(Network network) {
1213 try {
1214 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001215 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001216 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001217 }
1218 }
1219
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001220 /**
Udam Sainib7c24872016-01-04 12:16:14 -08001221 * Gets the URL that should be used for resolving whether a captive portal is present.
1222 * 1. This URL should respond with a 204 response to a GET request to indicate no captive
1223 * portal is present.
1224 * 2. This URL must be HTTP as redirect responses are used to find captive portal
1225 * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects.
1226 *
1227 * @hide
1228 */
1229 @SystemApi
Udam Sainic3b640c2017-06-07 12:06:28 -07001230 @RequiresPermission(android.Manifest.permission.LOCAL_MAC_ADDRESS)
Udam Sainib7c24872016-01-04 12:16:14 -08001231 public String getCaptivePortalServerUrl() {
1232 try {
1233 return mService.getCaptivePortalServerUrl();
1234 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001235 throw e.rethrowFromSystemServer();
Udam Sainib7c24872016-01-04 12:16:14 -08001236 }
1237 }
1238
1239 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001240 * Tells the underlying networking system that the caller wants to
1241 * begin using the named feature. The interpretation of {@code feature}
1242 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001243 *
1244 * <p>This method requires the caller to hold either the
1245 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1246 * or the ability to modify system settings as determined by
1247 * {@link android.provider.Settings.System#canWrite}.</p>
1248 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001249 * @param networkType specifies which network the request pertains to
1250 * @param feature the name of the feature to be used
1251 * @return an integer value representing the outcome of the request.
1252 * The interpretation of this value is specific to each networking
1253 * implementation+feature combination, except that the value {@code -1}
1254 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001255 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001256 * @deprecated Deprecated in favor of the cleaner
1257 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001258 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001259 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001260 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001261 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001262 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001264 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001265 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1266 if (netCap == null) {
1267 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
1268 feature);
1269 return PhoneConstants.APN_REQUEST_FAILED;
1270 }
1271
1272 NetworkRequest request = null;
1273 synchronized (sLegacyRequests) {
1274 LegacyRequest l = sLegacyRequests.get(netCap);
1275 if (l != null) {
1276 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
1277 renewRequestLocked(l);
1278 if (l.currentNetwork != null) {
1279 return PhoneConstants.APN_ALREADY_ACTIVE;
1280 } else {
1281 return PhoneConstants.APN_REQUEST_STARTED;
1282 }
1283 }
1284
1285 request = requestNetworkForFeatureLocked(netCap);
1286 }
1287 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -07001288 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001289 return PhoneConstants.APN_REQUEST_STARTED;
1290 } else {
1291 Log.d(TAG, " request Failed");
1292 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001293 }
1294 }
1295
1296 /**
1297 * Tells the underlying networking system that the caller is finished
1298 * using the named feature. The interpretation of {@code feature}
1299 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001300 *
1301 * <p>This method requires the caller to hold either the
1302 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1303 * or the ability to modify system settings as determined by
1304 * {@link android.provider.Settings.System#canWrite}.</p>
1305 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001306 * @param networkType specifies which network the request pertains to
1307 * @param feature the name of the feature that is no longer needed
1308 * @return an integer value representing the outcome of the request.
1309 * The interpretation of this value is specific to each networking
1310 * implementation+feature combination, except that the value {@code -1}
1311 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001312 *
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09001313 * @deprecated Deprecated in favor of the cleaner
1314 * {@link #unregisterNetworkCallback(NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001315 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001316 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001317 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001318 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001319 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001320 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001321 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001322 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1323 if (netCap == null) {
1324 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
1325 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001326 return -1;
1327 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001328
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001329 if (removeRequestForFeature(netCap)) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001330 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001331 }
1332 return 1;
1333 }
1334
1335 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1336 if (networkType == TYPE_MOBILE) {
Erik Kline35bf06c2017-01-26 18:08:28 +09001337 switch (feature) {
1338 case "enableCBS":
1339 return networkCapabilitiesForType(TYPE_MOBILE_CBS);
1340 case "enableDUN":
1341 case "enableDUNAlways":
1342 return networkCapabilitiesForType(TYPE_MOBILE_DUN);
1343 case "enableFOTA":
1344 return networkCapabilitiesForType(TYPE_MOBILE_FOTA);
1345 case "enableHIPRI":
1346 return networkCapabilitiesForType(TYPE_MOBILE_HIPRI);
1347 case "enableIMS":
1348 return networkCapabilitiesForType(TYPE_MOBILE_IMS);
1349 case "enableMMS":
1350 return networkCapabilitiesForType(TYPE_MOBILE_MMS);
1351 case "enableSUPL":
1352 return networkCapabilitiesForType(TYPE_MOBILE_SUPL);
1353 default:
1354 return null;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001355 }
Erik Kline35bf06c2017-01-26 18:08:28 +09001356 } else if (networkType == TYPE_WIFI && "p2p".equals(feature)) {
1357 return networkCapabilitiesForType(TYPE_WIFI_P2P);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001358 }
1359 return null;
1360 }
1361
Robert Greenwalt06314e42014-10-29 14:04:06 -07001362 /**
1363 * Guess what the network request was trying to say so that the resulting
1364 * network is accessible via the legacy (deprecated) API such as
1365 * requestRouteToHost.
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001366 *
1367 * This means we should try to be fairly precise about transport and
Robert Greenwalt06314e42014-10-29 14:04:06 -07001368 * capability but ignore things such as networkSpecifier.
1369 * If the request has more than one transport or capability it doesn't
1370 * match the old legacy requests (they selected only single transport/capability)
1371 * so this function cannot map the request to a single legacy type and
1372 * the resulting network will not be available to the legacy APIs.
1373 *
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001374 * This code is only called from the requestNetwork API (L and above).
1375 *
1376 * Setting a legacy type causes CONNECTIVITY_ACTION broadcasts, which are expensive
1377 * because they wake up lots of apps - see http://b/23350688 . So we currently only
1378 * do this for SUPL requests, which are the only ones that we know need it. If
1379 * omitting these broadcasts causes unacceptable app breakage, then for backwards
1380 * compatibility we can send them:
1381 *
1382 * if (targetSdkVersion < Build.VERSION_CODES.M) && // legacy API unsupported >= M
1383 * targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP)) // requestNetwork not present < L
1384 *
Robert Greenwalt06314e42014-10-29 14:04:06 -07001385 * TODO - This should be removed when the legacy APIs are removed.
1386 */
Ye Wenb87875e2014-07-21 14:19:01 -07001387 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1388 if (netCap == null) {
1389 return TYPE_NONE;
1390 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001391
Ye Wenb87875e2014-07-21 14:19:01 -07001392 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1393 return TYPE_NONE;
1394 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001395
Lifu Tang30f95a72016-01-07 23:20:38 -08001396 // Do this only for SUPL, until GnssLocationProvider is fixed. http://b/25876485 .
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001397 if (!netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1398 // NOTE: if this causes app breakage, we should not just comment out this early return;
1399 // instead, we should make this early return conditional on the requesting app's target
1400 // SDK version, as described in the comment above.
1401 return TYPE_NONE;
1402 }
1403
Robert Greenwalt06314e42014-10-29 14:04:06 -07001404 String type = null;
1405 int result = TYPE_NONE;
1406
Ye Wenb87875e2014-07-21 14:19:01 -07001407 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
Robert Greenwalt06314e42014-10-29 14:04:06 -07001408 type = "enableCBS";
1409 result = TYPE_MOBILE_CBS;
1410 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1411 type = "enableIMS";
1412 result = TYPE_MOBILE_IMS;
1413 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1414 type = "enableFOTA";
1415 result = TYPE_MOBILE_FOTA;
1416 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1417 type = "enableDUN";
1418 result = TYPE_MOBILE_DUN;
1419 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001420 type = "enableSUPL";
Robert Greenwalt06314e42014-10-29 14:04:06 -07001421 result = TYPE_MOBILE_SUPL;
Robert Greenwalt74ab4fa2015-08-28 12:37:54 -07001422 // back out this hack for mms as they no longer need this and it's causing
1423 // device slowdowns - b/23350688 (note, supl still needs this)
1424 //} else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1425 // type = "enableMMS";
1426 // result = TYPE_MOBILE_MMS;
Robert Greenwalt06314e42014-10-29 14:04:06 -07001427 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1428 type = "enableHIPRI";
1429 result = TYPE_MOBILE_HIPRI;
Ye Wenb87875e2014-07-21 14:19:01 -07001430 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001431 if (type != null) {
1432 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1433 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1434 return result;
Ye Wenb87875e2014-07-21 14:19:01 -07001435 }
1436 }
1437 return TYPE_NONE;
1438 }
1439
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001440 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001441 if (netCap == null) return TYPE_NONE;
1442 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1443 return TYPE_MOBILE_CBS;
1444 }
1445 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1446 return TYPE_MOBILE_IMS;
1447 }
1448 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1449 return TYPE_MOBILE_FOTA;
1450 }
1451 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1452 return TYPE_MOBILE_DUN;
1453 }
1454 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1455 return TYPE_MOBILE_SUPL;
1456 }
1457 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1458 return TYPE_MOBILE_MMS;
1459 }
1460 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1461 return TYPE_MOBILE_HIPRI;
1462 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001463 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1464 return TYPE_WIFI_P2P;
1465 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001466 return TYPE_NONE;
1467 }
1468
1469 private static class LegacyRequest {
1470 NetworkCapabilities networkCapabilities;
1471 NetworkRequest networkRequest;
1472 int expireSequenceNumber;
1473 Network currentNetwork;
1474 int delay = -1;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001475
1476 private void clearDnsBinding() {
1477 if (currentNetwork != null) {
1478 currentNetwork = null;
1479 setProcessDefaultNetworkForHostResolution(null);
1480 }
1481 }
1482
Robert Greenwalt6078b502014-06-11 16:05:07 -07001483 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001484 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001485 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001486 currentNetwork = network;
1487 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001488 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001489 }
1490 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001491 public void onLost(Network network) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001492 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001493 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1494 }
1495 };
1496 }
1497
Chalard Jean4d660112018-06-04 16:52:49 +09001498 private static final HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
1499 new HashMap<>();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001500
1501 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1502 synchronized (sLegacyRequests) {
1503 LegacyRequest l = sLegacyRequests.get(netCap);
1504 if (l != null) return l.networkRequest;
1505 }
1506 return null;
1507 }
1508
1509 private void renewRequestLocked(LegacyRequest l) {
1510 l.expireSequenceNumber++;
1511 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1512 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1513 }
1514
1515 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1516 int ourSeqNum = -1;
1517 synchronized (sLegacyRequests) {
1518 LegacyRequest l = sLegacyRequests.get(netCap);
1519 if (l == null) return;
1520 ourSeqNum = l.expireSequenceNumber;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001521 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001522 }
1523 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1524 }
1525
1526 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1527 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001528 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001529 try {
1530 delay = mService.getRestoreDefaultNetworkDelay(type);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001531 } catch (RemoteException e) {
1532 throw e.rethrowFromSystemServer();
1533 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001534 LegacyRequest l = new LegacyRequest();
1535 l.networkCapabilities = netCap;
1536 l.delay = delay;
1537 l.expireSequenceNumber = 0;
Hugo Benichi2583ef02017-02-02 17:02:36 +09001538 l.networkRequest = sendRequestForNetwork(
1539 netCap, l.networkCallback, 0, REQUEST, type, getDefaultHandler());
Robert Greenwalt562cc542014-05-15 18:07:26 -07001540 if (l.networkRequest == null) return null;
1541 sLegacyRequests.put(netCap, l);
1542 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1543 return l.networkRequest;
1544 }
1545
1546 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1547 if (delay >= 0) {
1548 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
Hugo Benichi2583ef02017-02-02 17:02:36 +09001549 CallbackHandler handler = getDefaultHandler();
Hugo Benichi6f260f32017-02-03 14:18:44 +09001550 Message msg = handler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1551 handler.sendMessageDelayed(msg, delay);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001552 }
1553 }
1554
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001555 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1556 final LegacyRequest l;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001557 synchronized (sLegacyRequests) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001558 l = sLegacyRequests.remove(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001559 }
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001560 if (l == null) return false;
1561 unregisterNetworkCallback(l.networkCallback);
1562 l.clearDnsBinding();
1563 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001564 }
1565
Erik Kline35bf06c2017-01-26 18:08:28 +09001566 private static final SparseIntArray sLegacyTypeToTransport = new SparseIntArray();
1567 static {
1568 sLegacyTypeToTransport.put(TYPE_MOBILE, NetworkCapabilities.TRANSPORT_CELLULAR);
1569 sLegacyTypeToTransport.put(TYPE_MOBILE_CBS, NetworkCapabilities.TRANSPORT_CELLULAR);
1570 sLegacyTypeToTransport.put(TYPE_MOBILE_DUN, NetworkCapabilities.TRANSPORT_CELLULAR);
1571 sLegacyTypeToTransport.put(TYPE_MOBILE_FOTA, NetworkCapabilities.TRANSPORT_CELLULAR);
1572 sLegacyTypeToTransport.put(TYPE_MOBILE_HIPRI, NetworkCapabilities.TRANSPORT_CELLULAR);
1573 sLegacyTypeToTransport.put(TYPE_MOBILE_IMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1574 sLegacyTypeToTransport.put(TYPE_MOBILE_MMS, NetworkCapabilities.TRANSPORT_CELLULAR);
1575 sLegacyTypeToTransport.put(TYPE_MOBILE_SUPL, NetworkCapabilities.TRANSPORT_CELLULAR);
1576 sLegacyTypeToTransport.put(TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI);
1577 sLegacyTypeToTransport.put(TYPE_WIFI_P2P, NetworkCapabilities.TRANSPORT_WIFI);
1578 sLegacyTypeToTransport.put(TYPE_BLUETOOTH, NetworkCapabilities.TRANSPORT_BLUETOOTH);
1579 sLegacyTypeToTransport.put(TYPE_ETHERNET, NetworkCapabilities.TRANSPORT_ETHERNET);
1580 }
1581
1582 private static final SparseIntArray sLegacyTypeToCapability = new SparseIntArray();
1583 static {
1584 sLegacyTypeToCapability.put(TYPE_MOBILE_CBS, NetworkCapabilities.NET_CAPABILITY_CBS);
1585 sLegacyTypeToCapability.put(TYPE_MOBILE_DUN, NetworkCapabilities.NET_CAPABILITY_DUN);
1586 sLegacyTypeToCapability.put(TYPE_MOBILE_FOTA, NetworkCapabilities.NET_CAPABILITY_FOTA);
1587 sLegacyTypeToCapability.put(TYPE_MOBILE_IMS, NetworkCapabilities.NET_CAPABILITY_IMS);
1588 sLegacyTypeToCapability.put(TYPE_MOBILE_MMS, NetworkCapabilities.NET_CAPABILITY_MMS);
1589 sLegacyTypeToCapability.put(TYPE_MOBILE_SUPL, NetworkCapabilities.NET_CAPABILITY_SUPL);
1590 sLegacyTypeToCapability.put(TYPE_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
1591 }
1592
1593 /**
1594 * Given a legacy type (TYPE_WIFI, ...) returns a NetworkCapabilities
1595 * instance suitable for registering a request or callback. Throws an
1596 * IllegalArgumentException if no mapping from the legacy type to
1597 * NetworkCapabilities is known.
1598 *
Chalard Jean8117f932018-03-08 13:54:53 +09001599 * @deprecated Types are deprecated. Use {@link NetworkCallback} or {@link NetworkRequest}
1600 * to find the network instead.
Erik Kline35bf06c2017-01-26 18:08:28 +09001601 * @hide
1602 */
1603 public static NetworkCapabilities networkCapabilitiesForType(int type) {
1604 final NetworkCapabilities nc = new NetworkCapabilities();
1605
1606 // Map from type to transports.
1607 final int NOT_FOUND = -1;
1608 final int transport = sLegacyTypeToTransport.get(type, NOT_FOUND);
Hugo Benichie7678512017-05-09 15:19:01 +09001609 Preconditions.checkArgument(transport != NOT_FOUND, "unknown legacy type: " + type);
Erik Kline35bf06c2017-01-26 18:08:28 +09001610 nc.addTransportType(transport);
1611
1612 // Map from type to capabilities.
1613 nc.addCapability(sLegacyTypeToCapability.get(
1614 type, NetworkCapabilities.NET_CAPABILITY_INTERNET));
1615 nc.maybeMarkCapabilitiesRestricted();
1616 return nc;
1617 }
1618
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001619 /** @hide */
1620 public static class PacketKeepaliveCallback {
1621 /** The requested keepalive was successfully started. */
1622 public void onStarted() {}
1623 /** The keepalive was successfully stopped. */
1624 public void onStopped() {}
1625 /** An error occurred. */
1626 public void onError(int error) {}
1627 }
1628
1629 /**
1630 * Allows applications to request that the system periodically send specific packets on their
1631 * behalf, using hardware offload to save battery power.
1632 *
1633 * To request that the system send keepalives, call one of the methods that return a
1634 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1635 * passing in a non-null callback. If the callback is successfully started, the callback's
1636 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1637 * specifying one of the {@code ERROR_*} constants in this class.
1638 *
Chalard Jean4d660112018-06-04 16:52:49 +09001639 * To stop an existing keepalive, call {@link PacketKeepalive#stop}. The system will call
1640 * {@link PacketKeepaliveCallback#onStopped} if the operation was successful or
1641 * {@link PacketKeepaliveCallback#onError} if an error occurred.
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001642 *
1643 * @hide
1644 */
1645 public class PacketKeepalive {
1646
1647 private static final String TAG = "PacketKeepalive";
1648
1649 /** @hide */
1650 public static final int SUCCESS = 0;
1651
1652 /** @hide */
1653 public static final int NO_KEEPALIVE = -1;
1654
1655 /** @hide */
1656 public static final int BINDER_DIED = -10;
1657
1658 /** The specified {@code Network} is not connected. */
1659 public static final int ERROR_INVALID_NETWORK = -20;
1660 /** The specified IP addresses are invalid. For example, the specified source IP address is
1661 * not configured on the specified {@code Network}. */
1662 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1663 /** The requested port is invalid. */
1664 public static final int ERROR_INVALID_PORT = -22;
1665 /** The packet length is invalid (e.g., too long). */
1666 public static final int ERROR_INVALID_LENGTH = -23;
1667 /** The packet transmission interval is invalid (e.g., too short). */
1668 public static final int ERROR_INVALID_INTERVAL = -24;
1669
1670 /** The hardware does not support this request. */
1671 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti9d1284e2015-09-08 16:46:36 +09001672 /** The hardware returned an error. */
1673 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001674
Nathan Harold63dd8132018-02-14 13:09:45 -08001675 /** The NAT-T destination port for IPsec */
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001676 public static final int NATT_PORT = 4500;
1677
Nathan Harold63dd8132018-02-14 13:09:45 -08001678 /** The minimum interval in seconds between keepalive packet transmissions */
1679 public static final int MIN_INTERVAL = 10;
1680
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001681 private final Network mNetwork;
1682 private final PacketKeepaliveCallback mCallback;
1683 private final Looper mLooper;
1684 private final Messenger mMessenger;
1685
1686 private volatile Integer mSlot;
1687
1688 void stopLooper() {
1689 mLooper.quit();
1690 }
1691
1692 public void stop() {
1693 try {
1694 mService.stopKeepalive(mNetwork, mSlot);
1695 } catch (RemoteException e) {
1696 Log.e(TAG, "Error stopping packet keepalive: ", e);
1697 stopLooper();
1698 }
1699 }
1700
1701 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09001702 Preconditions.checkNotNull(network, "network cannot be null");
1703 Preconditions.checkNotNull(callback, "callback cannot be null");
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001704 mNetwork = network;
1705 mCallback = callback;
1706 HandlerThread thread = new HandlerThread(TAG);
1707 thread.start();
1708 mLooper = thread.getLooper();
1709 mMessenger = new Messenger(new Handler(mLooper) {
1710 @Override
1711 public void handleMessage(Message message) {
1712 switch (message.what) {
1713 case NetworkAgent.EVENT_PACKET_KEEPALIVE:
1714 int error = message.arg2;
1715 try {
1716 if (error == SUCCESS) {
1717 if (mSlot == null) {
1718 mSlot = message.arg1;
1719 mCallback.onStarted();
1720 } else {
1721 mSlot = null;
1722 stopLooper();
1723 mCallback.onStopped();
1724 }
1725 } else {
1726 stopLooper();
1727 mCallback.onError(error);
1728 }
1729 } catch (Exception e) {
1730 Log.e(TAG, "Exception in keepalive callback(" + error + ")", e);
1731 }
1732 break;
1733 default:
1734 Log.e(TAG, "Unhandled message " + Integer.toHexString(message.what));
1735 break;
1736 }
1737 }
1738 });
1739 }
1740 }
1741
1742 /**
1743 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
1744 *
1745 * @hide
1746 */
1747 public PacketKeepalive startNattKeepalive(
1748 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
1749 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
1750 final PacketKeepalive k = new PacketKeepalive(network, callback);
1751 try {
1752 mService.startNattKeepalive(network, intervalSeconds, k.mMessenger, new Binder(),
1753 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
1754 } catch (RemoteException e) {
1755 Log.e(TAG, "Error starting packet keepalive: ", e);
1756 k.stopLooper();
1757 return null;
1758 }
1759 return k;
1760 }
1761
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001762 /**
1763 * Ensure that a network route exists to deliver traffic to the specified
1764 * host via the specified network interface. An attempt to add a route that
1765 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001766 *
1767 * <p>This method requires the caller to hold either the
1768 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1769 * or the ability to modify system settings as determined by
1770 * {@link android.provider.Settings.System#canWrite}.</p>
1771 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001772 * @param networkType the type of the network over which traffic to the specified
1773 * host is to be routed
1774 * @param hostAddress the IP address of the host to which the route is desired
1775 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001776 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001777 * @deprecated Deprecated in favor of the
1778 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
1779 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001780 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001781 * throw {@code UnsupportedOperationException} if called.
Lorenzo Colitti2187df72016-12-09 18:39:30 +09001782 * @removed
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001783 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001784 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001785 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001786 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001787 }
1788
1789 /**
1790 * Ensure that a network route exists to deliver traffic to the specified
1791 * host via the specified network interface. An attempt to add a route that
1792 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001793 *
1794 * <p>This method requires the caller to hold either the
1795 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1796 * or the ability to modify system settings as determined by
1797 * {@link android.provider.Settings.System#canWrite}.</p>
1798 *
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001799 * @param networkType the type of the network over which traffic to the specified
1800 * host is to be routed
1801 * @param hostAddress the IP address of the host to which the route is desired
1802 * @return {@code true} on success, {@code false} on failure
1803 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001804 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001805 * {@link #bindProcessToNetwork} API.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001806 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001807 @Deprecated
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001808 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001809 checkLegacyRoutingApiAccess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001810 try {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001811 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001812 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001813 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001814 }
1815 }
1816
1817 /**
1818 * Returns the value of the setting for background data usage. If false,
1819 * applications should not use the network if the application is not in the
1820 * foreground. Developers should respect this setting, and check the value
1821 * of this before performing any background data operations.
1822 * <p>
1823 * All applications that have background services that use the network
1824 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001825 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001826 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001827 * background data depends on several combined factors, and this method will
1828 * always return {@code true}. Instead, when background data is unavailable,
1829 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001830 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001831 * @return Whether background data usage is allowed.
1832 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001833 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001834 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001835 // assume that background data is allowed; final authority is
1836 // NetworkInfo which may be blocked.
1837 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001838 }
1839
1840 /**
1841 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001842 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001843 * @param allowBackgroundData Whether an application should use data while
1844 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001845 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001846 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1847 * @see #getBackgroundDataSetting()
1848 * @hide
1849 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001850 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001851 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001852 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001853 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001854
Jeff Sharkey43d2a172017-07-12 10:50:42 -06001855 /** {@hide} */
1856 @Deprecated
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001857 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1858 try {
1859 return mService.getActiveNetworkQuotaInfo();
1860 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001861 throw e.rethrowFromSystemServer();
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001862 }
1863 }
1864
1865 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001866 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001867 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001868 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001869 @Deprecated
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001870 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001871 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1872 if (b != null) {
1873 try {
1874 ITelephony it = ITelephony.Stub.asInterface(b);
Shishir Agrawal7ea3e8b2016-01-25 13:03:07 -08001875 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Wink Saville36ffb042014-12-05 11:10:30 -08001876 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
Malcolm Chenffbaa9d2017-11-28 15:57:14 -08001877 boolean retVal = it.isUserDataEnabled(subId);
Wink Saville36ffb042014-12-05 11:10:30 -08001878 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1879 + " retVal=" + retVal);
1880 return retVal;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001881 } catch (RemoteException e) {
1882 throw e.rethrowFromSystemServer();
1883 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001884 }
Wink Saville36ffb042014-12-05 11:10:30 -08001885 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001886 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001887 }
1888
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001889 /**
Robert Greenwaltb2489872014-09-04 16:44:35 -07001890 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt6078b502014-06-11 16:05:07 -07001891 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001892 */
1893 public interface OnNetworkActiveListener {
1894 /**
1895 * Called on the main thread of the process to report that the current data network
1896 * has become active, and it is now a good time to perform any pending network
1897 * operations. Note that this listener only tells you when the network becomes
1898 * active; if at any other time you want to know whether it is active (and thus okay
1899 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001900 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001901 */
Chalard Jean4d660112018-06-04 16:52:49 +09001902 void onNetworkActive();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001903 }
1904
1905 private INetworkManagementService getNetworkManagementService() {
1906 synchronized (this) {
1907 if (mNMService != null) {
1908 return mNMService;
1909 }
1910 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1911 mNMService = INetworkManagementService.Stub.asInterface(b);
1912 return mNMService;
1913 }
1914 }
1915
1916 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
Chalard Jean4d660112018-06-04 16:52:49 +09001917 mNetworkActivityListeners = new ArrayMap<>();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001918
1919 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001920 * Start listening to reports when the system's default data network is active, meaning it is
1921 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1922 * to determine the current state of the system's default network after registering the
1923 * listener.
1924 * <p>
1925 * If the process default network has been set with
Paul Jensen72db88e2015-03-10 10:54:12 -04001926 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt6078b502014-06-11 16:05:07 -07001927 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001928 *
1929 * @param l The listener to be told when the network is active.
1930 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001931 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001932 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1933 @Override
1934 public void onNetworkActive() throws RemoteException {
1935 l.onNetworkActive();
1936 }
1937 };
1938
1939 try {
1940 getNetworkManagementService().registerNetworkActivityListener(rl);
1941 mNetworkActivityListeners.put(l, rl);
1942 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001943 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001944 }
1945 }
1946
1947 /**
1948 * Remove network active listener previously registered with
Robert Greenwaltb2489872014-09-04 16:44:35 -07001949 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001950 *
1951 * @param l Previously registered listener.
1952 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001953 public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001954 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
Hugo Benichie7678512017-05-09 15:19:01 +09001955 Preconditions.checkArgument(rl != null, "Listener was not registered.");
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001956 try {
1957 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1958 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001959 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001960 }
1961 }
1962
1963 /**
1964 * Return whether the data network is currently active. An active network means that
1965 * it is currently in a high power state for performing data transmission. On some
1966 * types of networks, it may be expensive to move and stay in such a state, so it is
1967 * more power efficient to batch network traffic together when the radio is already in
1968 * this state. This method tells you whether right now is currently a good time to
1969 * initiate network traffic, as the network is already active.
1970 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001971 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001972 try {
1973 return getNetworkManagementService().isNetworkActive();
1974 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001975 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001976 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001977 }
1978
1979 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001980 * {@hide}
1981 */
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001982 public ConnectivityManager(Context context, IConnectivityManager service) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09001983 mContext = Preconditions.checkNotNull(context, "missing context");
1984 mService = Preconditions.checkNotNull(service, "missing IConnectivityManager");
Paul Jensene0bef712014-12-10 15:12:18 -05001985 sInstance = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001986 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001987
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07001988 /** {@hide} */
1989 public static ConnectivityManager from(Context context) {
1990 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1991 }
1992
Lorenzo Colittifbe9b1a2016-07-28 17:14:11 +09001993 /* TODO: These permissions checks don't belong in client-side code. Move them to
1994 * services.jar, possibly in com.android.server.net. */
1995
1996 /** {@hide} */
Lorenzo Colittid5427052015-10-15 16:29:00 +09001997 public static final void enforceChangePermission(Context context) {
1998 int uid = Binder.getCallingUid();
1999 Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
2000 .getPackageNameForUid(context, uid), true /* throwException */);
2001 }
2002
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002003 /** {@hide} */
2004 public static final void enforceTetherChangePermission(Context context, String callingPkg) {
Hugo Benichie7678512017-05-09 15:19:01 +09002005 Preconditions.checkNotNull(context, "Context cannot be null");
2006 Preconditions.checkNotNull(callingPkg, "callingPkg cannot be null");
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002007
Robert Greenwaltedb47662014-09-16 17:54:19 -07002008 if (context.getResources().getStringArray(
2009 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
2010 // Have a provisioning app - must only let system apps (which check this app)
2011 // turn on tethering
2012 context.enforceCallingOrSelfPermission(
Jeremy Kleind42209d2015-12-28 15:11:58 -08002013 android.Manifest.permission.TETHER_PRIVILEGED, "ConnectivityService");
Robert Greenwaltedb47662014-09-16 17:54:19 -07002014 } else {
Billy Laua7238a32015-08-01 12:45:02 +01002015 int uid = Binder.getCallingUid();
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002016 // If callingPkg's uid is not same as Binder.getCallingUid(),
2017 // AppOpsService throws SecurityException.
2018 Settings.checkAndNoteWriteSettingsOperation(context, uid, callingPkg,
2019 true /* throwException */);
Robert Greenwaltedb47662014-09-16 17:54:19 -07002020 }
2021 }
2022
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002023 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002024 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2025 * situations where a Context pointer is unavailable.
2026 * @hide
2027 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002028 @Deprecated
Paul Jensen72db88e2015-03-10 10:54:12 -04002029 static ConnectivityManager getInstanceOrNull() {
2030 return sInstance;
2031 }
2032
2033 /**
2034 * @deprecated - use getSystemService. This is a kludge to support static access in certain
2035 * situations where a Context pointer is unavailable.
2036 * @hide
2037 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002038 @Deprecated
Paul Jensen72db88e2015-03-10 10:54:12 -04002039 private static ConnectivityManager getInstance() {
2040 if (getInstanceOrNull() == null) {
Paul Jensene0bef712014-12-10 15:12:18 -05002041 throw new IllegalStateException("No ConnectivityManager yet constructed");
2042 }
Paul Jensen72db88e2015-03-10 10:54:12 -04002043 return getInstanceOrNull();
Paul Jensene0bef712014-12-10 15:12:18 -05002044 }
2045
2046 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002047 * Get the set of tetherable, available interfaces. This list is limited by
2048 * device configuration and current interface existence.
2049 *
2050 * @return an array of 0 or more Strings of tetherable interface names.
2051 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002052 * {@hide}
2053 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002054 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002055 public String[] getTetherableIfaces() {
2056 try {
2057 return mService.getTetherableIfaces();
2058 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002059 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002060 }
2061 }
2062
2063 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002064 * Get the set of tethered interfaces.
2065 *
2066 * @return an array of 0 or more String of currently tethered interface names.
2067 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002068 * {@hide}
2069 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002070 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002071 public String[] getTetheredIfaces() {
2072 try {
2073 return mService.getTetheredIfaces();
2074 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002075 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002076 }
2077 }
2078
2079 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002080 * Get the set of interface names which attempted to tether but
2081 * failed. Re-attempting to tether may cause them to reset to the Tethered
2082 * state. Alternatively, causing the interface to be destroyed and recreated
2083 * may cause them to reset to the available state.
2084 * {@link ConnectivityManager#getLastTetherError} can be used to get more
2085 * information on the cause of the errors.
2086 *
2087 * @return an array of 0 or more String indicating the interface names
2088 * which failed to tether.
2089 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002090 * {@hide}
2091 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002092 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt5a735062010-03-02 17:25:02 -08002093 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002094 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08002095 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002096 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002097 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002098 }
2099 }
2100
2101 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07002102 * Get the set of tethered dhcp ranges.
2103 *
2104 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
2105 * {@hide}
2106 */
2107 public String[] getTetheredDhcpRanges() {
2108 try {
2109 return mService.getTetheredDhcpRanges();
2110 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002111 throw e.rethrowFromSystemServer();
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07002112 }
2113 }
2114
2115 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002116 * Attempt to tether the named interface. This will setup a dhcp server
2117 * on the interface, forward and NAT IP packets and forward DNS requests
2118 * to the best active upstream network interface. Note that if no upstream
2119 * IP network interface is available, dhcp will still run and traffic will be
2120 * allowed between the tethered devices and this device, though upstream net
2121 * access will of course fail until an upstream network interface becomes
2122 * active.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002123 *
2124 * <p>This method requires the caller to hold either the
2125 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2126 * or the ability to modify system settings as determined by
2127 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002128 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002129 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2130 * and WifiStateMachine which need direct access. All other clients should use
2131 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2132 * logic.</p>
2133 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002134 * @param iface the interface name to tether.
2135 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2136 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002137 * {@hide}
2138 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08002139 public int tether(String iface) {
2140 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002141 String pkgName = mContext.getOpPackageName();
2142 Log.i(TAG, "tether caller:" + pkgName);
2143 return mService.tether(iface, pkgName);
Robert Greenwalt5a735062010-03-02 17:25:02 -08002144 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002145 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002146 }
2147 }
2148
2149 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002150 * Stop tethering the named interface.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002151 *
2152 * <p>This method requires the caller to hold either the
2153 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2154 * or the ability to modify system settings as determined by
2155 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002156 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002157 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
2158 * and WifiStateMachine which need direct access. All other clients should use
2159 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
2160 * logic.</p>
2161 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002162 * @param iface the interface name to untether.
2163 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2164 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002165 * {@hide}
2166 */
2167 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002168 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002169 String pkgName = mContext.getOpPackageName();
2170 Log.i(TAG, "untether caller:" + pkgName);
2171 return mService.untether(iface, pkgName);
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002172 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002173 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002174 }
2175 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002176
2177 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002178 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002179 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002180 * due to device configuration.
2181 *
Chalard Jean092d50d2017-09-26 15:45:18 +09002182 * <p>If this app does not have permission to use this API, it will always
2183 * return false rather than throw an exception.</p>
2184 *
2185 * <p>If the device has a hotspot provisioning app, the caller is required to hold the
2186 * {@link android.Manifest.permission.TETHER_PRIVILEGED} permission.</p>
2187 *
2188 * <p>Otherwise, this method requires the caller to hold the ability to modify system
2189 * settings as determined by {@link android.provider.Settings.System#canWrite}.</p>
2190 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002191 * @return a boolean - {@code true} indicating Tethering is supported.
2192 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002193 * {@hide}
2194 */
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002195 @SystemApi
Chalard Jean092d50d2017-09-26 15:45:18 +09002196 @RequiresPermission(anyOf = {android.Manifest.permission.TETHER_PRIVILEGED,
2197 android.Manifest.permission.WRITE_SETTINGS})
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002198 public boolean isTetheringSupported() {
Chalard Jean092d50d2017-09-26 15:45:18 +09002199 String pkgName = mContext.getOpPackageName();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002200 try {
Udam Sainic3b640c2017-06-07 12:06:28 -07002201 return mService.isTetheringSupported(pkgName);
Chalard Jean092d50d2017-09-26 15:45:18 +09002202 } catch (SecurityException e) {
2203 // This API is not available to this caller, but for backward-compatibility
2204 // this will just return false instead of throwing.
2205 return false;
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002206 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002207 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002208 }
2209 }
2210
2211 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002212 * Callback for use with {@link #startTethering} to find out whether tethering succeeded.
2213 * @hide
2214 */
2215 @SystemApi
2216 public static abstract class OnStartTetheringCallback {
2217 /**
2218 * Called when tethering has been successfully started.
2219 */
Chalard Jean4d660112018-06-04 16:52:49 +09002220 public void onTetheringStarted() {}
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002221
2222 /**
2223 * Called when starting tethering failed.
2224 */
Chalard Jean4d660112018-06-04 16:52:49 +09002225 public void onTetheringFailed() {}
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002226 }
2227
2228 /**
2229 * Convenient overload for
2230 * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null
2231 * handler to run on the current thread's {@link Looper}.
2232 * @hide
2233 */
2234 @SystemApi
Udam Sainic3b640c2017-06-07 12:06:28 -07002235 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002236 public void startTethering(int type, boolean showProvisioningUi,
2237 final OnStartTetheringCallback callback) {
2238 startTethering(type, showProvisioningUi, callback, null);
2239 }
2240
2241 /**
2242 * Runs tether provisioning for the given type if needed and then starts tethering if
2243 * the check succeeds. If no carrier provisioning is required for tethering, tethering is
2244 * enabled immediately. If provisioning fails, tethering will not be enabled. It also
2245 * schedules tether provisioning re-checks if appropriate.
2246 *
2247 * @param type The type of tethering to start. Must be one of
2248 * {@link ConnectivityManager.TETHERING_WIFI},
2249 * {@link ConnectivityManager.TETHERING_USB}, or
2250 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2251 * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there
2252 * is one. This should be true the first time this function is called and also any time
2253 * the user can see this UI. It gives users information from their carrier about the
2254 * check failing and how they can sign up for tethering if possible.
2255 * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller
2256 * of the result of trying to tether.
2257 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
2258 * @hide
2259 */
2260 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002261 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002262 public void startTethering(int type, boolean showProvisioningUi,
2263 final OnStartTetheringCallback callback, Handler handler) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09002264 Preconditions.checkNotNull(callback, "OnStartTetheringCallback cannot be null.");
Jeremy Klein5f277e12016-03-12 16:29:54 -08002265
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002266 ResultReceiver wrappedCallback = new ResultReceiver(handler) {
2267 @Override
2268 protected void onReceiveResult(int resultCode, Bundle resultData) {
2269 if (resultCode == TETHER_ERROR_NO_ERROR) {
2270 callback.onTetheringStarted();
2271 } else {
2272 callback.onTetheringFailed();
2273 }
2274 }
2275 };
Jeremy Klein5f277e12016-03-12 16:29:54 -08002276
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002277 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002278 String pkgName = mContext.getOpPackageName();
2279 Log.i(TAG, "startTethering caller:" + pkgName);
2280 mService.startTethering(type, wrappedCallback, showProvisioningUi, pkgName);
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002281 } catch (RemoteException e) {
2282 Log.e(TAG, "Exception trying to start tethering.", e);
2283 wrappedCallback.send(TETHER_ERROR_SERVICE_UNAVAIL, null);
2284 }
2285 }
2286
2287 /**
2288 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if
2289 * applicable.
2290 *
2291 * @param type The type of tethering to stop. Must be one of
2292 * {@link ConnectivityManager.TETHERING_WIFI},
2293 * {@link ConnectivityManager.TETHERING_USB}, or
2294 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2295 * @hide
2296 */
2297 @SystemApi
Jeff Sharkeyd86b8fe2017-06-02 17:36:26 -06002298 @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED)
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002299 public void stopTethering(int type) {
2300 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002301 String pkgName = mContext.getOpPackageName();
2302 Log.i(TAG, "stopTethering caller:" + pkgName);
2303 mService.stopTethering(type, pkgName);
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002304 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002305 throw e.rethrowFromSystemServer();
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002306 }
2307 }
2308
2309 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002310 * Get the list of regular expressions that define any tetherable
2311 * USB network interfaces. If USB tethering is not supported by the
2312 * device, this list should be empty.
2313 *
2314 * @return an array of 0 or more regular expression Strings defining
2315 * what interfaces are considered tetherable usb interfaces.
2316 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002317 * {@hide}
2318 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002319 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002320 public String[] getTetherableUsbRegexs() {
2321 try {
2322 return mService.getTetherableUsbRegexs();
2323 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002324 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002325 }
2326 }
2327
2328 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002329 * Get the list of regular expressions that define any tetherable
2330 * Wifi network interfaces. If Wifi tethering is not supported by the
2331 * device, this list should be empty.
2332 *
2333 * @return an array of 0 or more regular expression Strings defining
2334 * what interfaces are considered tetherable wifi interfaces.
2335 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002336 * {@hide}
2337 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002338 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002339 public String[] getTetherableWifiRegexs() {
2340 try {
2341 return mService.getTetherableWifiRegexs();
2342 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002343 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002344 }
2345 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08002346
Danica Chang6fdd0c62010-08-11 14:54:43 -07002347 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002348 * Get the list of regular expressions that define any tetherable
2349 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
2350 * device, this list should be empty.
2351 *
2352 * @return an array of 0 or more regular expression Strings defining
2353 * what interfaces are considered tetherable bluetooth interfaces.
2354 *
Danica Chang6fdd0c62010-08-11 14:54:43 -07002355 * {@hide}
2356 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002357 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Danica Chang6fdd0c62010-08-11 14:54:43 -07002358 public String[] getTetherableBluetoothRegexs() {
2359 try {
2360 return mService.getTetherableBluetoothRegexs();
2361 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002362 throw e.rethrowFromSystemServer();
Danica Chang6fdd0c62010-08-11 14:54:43 -07002363 }
2364 }
2365
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002366 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002367 * Attempt to both alter the mode of USB and Tethering of USB. A
2368 * utility method to deal with some of the complexity of USB - will
2369 * attempt to switch to Rndis and subsequently tether the resulting
2370 * interface on {@code true} or turn off tethering and switch off
2371 * Rndis on {@code false}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002372 *
2373 * <p>This method requires the caller to hold either the
2374 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2375 * or the ability to modify system settings as determined by
2376 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002377 *
2378 * @param enable a boolean - {@code true} to enable tethering
2379 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2380 *
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002381 * {@hide}
2382 */
2383 public int setUsbTethering(boolean enable) {
2384 try {
Tetsutoki Shiozawa335d2ed2016-03-16 23:30:57 +09002385 String pkgName = mContext.getOpPackageName();
2386 Log.i(TAG, "setUsbTethering caller:" + pkgName);
2387 return mService.setUsbTethering(enable, pkgName);
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002388 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002389 throw e.rethrowFromSystemServer();
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002390 }
2391 }
2392
Robert Greenwalt5a735062010-03-02 17:25:02 -08002393 /** {@hide} */
2394 public static final int TETHER_ERROR_NO_ERROR = 0;
2395 /** {@hide} */
2396 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
2397 /** {@hide} */
2398 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
2399 /** {@hide} */
2400 public static final int TETHER_ERROR_UNSUPPORTED = 3;
2401 /** {@hide} */
2402 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
2403 /** {@hide} */
2404 public static final int TETHER_ERROR_MASTER_ERROR = 5;
2405 /** {@hide} */
2406 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
2407 /** {@hide} */
2408 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
2409 /** {@hide} */
2410 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
2411 /** {@hide} */
2412 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
2413 /** {@hide} */
2414 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002415 /** {@hide} */
2416 public static final int TETHER_ERROR_PROVISION_FAILED = 11;
Robert Greenwalt5a735062010-03-02 17:25:02 -08002417
2418 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002419 * Get a more detailed error code after a Tethering or Untethering
2420 * request asynchronously failed.
2421 *
2422 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08002423 * @return error The error code of the last error tethering or untethering the named
2424 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002425 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002426 * {@hide}
2427 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002428 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt5a735062010-03-02 17:25:02 -08002429 public int getLastTetherError(String iface) {
2430 try {
2431 return mService.getLastTetherError(iface);
2432 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002433 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002434 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002435 }
2436
2437 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002438 * Report network connectivity status. This is currently used only
2439 * to alter status bar UI.
Paul Jensenb2748922015-05-06 11:10:18 -04002440 * <p>This method requires the caller to hold the permission
2441 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002442 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002443 * @param networkType The type of network you want to report on
2444 * @param percentage The quality of the connection 0 is bad, 100 is good
Chalard Jean8117f932018-03-08 13:54:53 +09002445 * @deprecated Types are deprecated. Use {@link #reportNetworkConnectivity} instead.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002446 * {@hide}
2447 */
2448 public void reportInetCondition(int networkType, int percentage) {
2449 try {
2450 mService.reportInetCondition(networkType, percentage);
2451 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002452 throw e.rethrowFromSystemServer();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002453 }
2454 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002455
2456 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002457 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07002458 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002459 * the framework to re-evaluate network connectivity and/or switch to another
2460 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002461 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002462 * @param network The {@link Network} the application was attempting to use
2463 * or {@code null} to indicate the current default network.
Paul Jensenbfd17b72015-04-07 12:43:13 -04002464 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
2465 * working and non-working connectivity.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002466 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002467 @Deprecated
Robert Greenwalt9258c642014-03-26 16:47:06 -07002468 public void reportBadNetwork(Network network) {
2469 try {
Paul Jensenbfd17b72015-04-07 12:43:13 -04002470 // One of these will be ignored because it matches system's current state.
2471 // The other will trigger the necessary reevaluation.
2472 mService.reportNetworkConnectivity(network, true);
2473 mService.reportNetworkConnectivity(network, false);
2474 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002475 throw e.rethrowFromSystemServer();
Paul Jensenbfd17b72015-04-07 12:43:13 -04002476 }
2477 }
2478
2479 /**
2480 * Report to the framework whether a network has working connectivity.
2481 * This provides a hint to the system that a particular network is providing
2482 * working connectivity or not. In response the framework may re-evaluate
2483 * the network's connectivity and might take further action thereafter.
2484 *
2485 * @param network The {@link Network} the application was attempting to use
2486 * or {@code null} to indicate the current default network.
2487 * @param hasConnectivity {@code true} if the application was able to successfully access the
2488 * Internet using {@code network} or {@code false} if not.
2489 */
2490 public void reportNetworkConnectivity(Network network, boolean hasConnectivity) {
2491 try {
2492 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002493 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002494 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002495 }
2496 }
2497
2498 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002499 * Set a network-independent global http proxy. This is not normally what you want
2500 * for typical HTTP proxies - they are general network dependent. However if you're
2501 * doing something unusual like general internal filtering this may be useful. On
2502 * a private network where the proxy is not accessible, you may break HTTP using this.
Paul Jensenb2748922015-05-06 11:10:18 -04002503 *
2504 * @param p A {@link ProxyInfo} object defining the new global
2505 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002506 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002507 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002508 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Jason Monk207900c2014-04-25 15:00:09 -04002509 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002510 try {
2511 mService.setGlobalProxy(p);
2512 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002513 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002514 }
2515 }
2516
2517 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002518 * Retrieve any network-independent global HTTP proxy.
2519 *
Jason Monk207900c2014-04-25 15:00:09 -04002520 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002521 * if no global HTTP proxy is set.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002522 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002523 */
Jason Monk207900c2014-04-25 15:00:09 -04002524 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002525 try {
2526 return mService.getGlobalProxy();
2527 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002528 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002529 }
2530 }
2531
2532 /**
Paul Jensencee9b512015-05-06 07:32:40 -04002533 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
2534 * network-specific HTTP proxy. If {@code network} is null, the
2535 * network-specific proxy returned is the proxy of the default active
2536 * network.
2537 *
2538 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
2539 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
2540 * or when {@code network} is {@code null},
2541 * the {@code ProxyInfo} for the default active network. Returns
2542 * {@code null} when no proxy applies or the caller doesn't have
2543 * permission to use {@code network}.
2544 * @hide
2545 */
2546 public ProxyInfo getProxyForNetwork(Network network) {
2547 try {
2548 return mService.getProxyForNetwork(network);
2549 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002550 throw e.rethrowFromSystemServer();
Paul Jensencee9b512015-05-06 07:32:40 -04002551 }
2552 }
2553
2554 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002555 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
2556 * otherwise if this process is bound to a {@link Network} using
Paul Jensen72db88e2015-03-10 10:54:12 -04002557 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensene0bef712014-12-10 15:12:18 -05002558 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002559 *
Jason Monk207900c2014-04-25 15:00:09 -04002560 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002561 * HTTP proxy is active.
Robert Greenwalt434203a2010-10-11 16:00:27 -07002562 */
Paul Jensene0bef712014-12-10 15:12:18 -05002563 public ProxyInfo getDefaultProxy() {
Paul Jensencee9b512015-05-06 07:32:40 -04002564 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwalt434203a2010-10-11 16:00:27 -07002565 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07002566
2567 /**
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002568 * Returns true if the hardware supports the given network type
2569 * else it returns false. This doesn't indicate we have coverage
2570 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002571 * hardware supports it. For example a GSM phone without a SIM
2572 * should still return {@code true} for mobile data, but a wifi only
2573 * tablet would return {@code false}.
2574 *
2575 * @param networkType The network type we'd like to check
2576 * @return {@code true} if supported, else {@code false}
Chalard Jean8117f932018-03-08 13:54:53 +09002577 * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead.
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002578 * @hide
2579 */
Chalard Jean8117f932018-03-08 13:54:53 +09002580 @Deprecated
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002581 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002582 public boolean isNetworkSupported(int networkType) {
2583 try {
2584 return mService.isNetworkSupported(networkType);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002585 } catch (RemoteException e) {
2586 throw e.rethrowFromSystemServer();
2587 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002588 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002589
2590 /**
2591 * Returns if the currently active data network is metered. A network is
2592 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002593 * that connection due to monetary costs, data limitations or
2594 * battery/performance issues. You should check this before doing large
2595 * data transfers, and warn the user or delay the operation until another
2596 * network is available.
2597 *
2598 * @return {@code true} if large transfers should be avoided, otherwise
2599 * {@code false}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002600 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002601 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002602 public boolean isActiveNetworkMetered() {
2603 try {
2604 return mService.isActiveNetworkMetered();
2605 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002606 throw e.rethrowFromSystemServer();
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002607 }
2608 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002609
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002610 /**
2611 * If the LockdownVpn mechanism is enabled, updates the vpn
2612 * with a reload of its profile.
2613 *
2614 * @return a boolean with {@code} indicating success
2615 *
2616 * <p>This method can only be called by the system UID
2617 * {@hide}
2618 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002619 public boolean updateLockdownVpn() {
2620 try {
2621 return mService.updateLockdownVpn();
2622 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002623 throw e.rethrowFromSystemServer();
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002624 }
2625 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07002626
2627 /**
Wink Saville948282b2013-08-29 08:55:16 -07002628 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07002629 *
Wink Savilleab9321d2013-06-29 21:10:57 -07002630 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07002631 *
2632 * @return time out that will be used, maybe less that suggestedTimeOutMs
2633 * -1 if an error.
2634 *
2635 * {@hide}
2636 */
Wink Saville948282b2013-08-29 08:55:16 -07002637 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07002638 int timeOutMs = -1;
2639 try {
Wink Saville948282b2013-08-29 08:55:16 -07002640 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07002641 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002642 throw e.rethrowFromSystemServer();
Wink Savilleab9321d2013-06-29 21:10:57 -07002643 }
2644 return timeOutMs;
2645 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002646
2647 /**
Wink Saville42d4f082013-07-20 20:31:59 -07002648 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002649 * {@hide}
2650 */
2651 public String getMobileProvisioningUrl() {
2652 try {
2653 return mService.getMobileProvisioningUrl();
2654 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002655 throw e.rethrowFromSystemServer();
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002656 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002657 }
Wink Saville42d4f082013-07-20 20:31:59 -07002658
2659 /**
Wink Saville948282b2013-08-29 08:55:16 -07002660 * Set sign in error notification to visible or in visible
2661 *
Wink Saville948282b2013-08-29 08:55:16 -07002662 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04002663 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville948282b2013-08-29 08:55:16 -07002664 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002665 @Deprecated
Wink Saville948282b2013-08-29 08:55:16 -07002666 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04002667 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07002668 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04002669 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07002670 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002671 throw e.rethrowFromSystemServer();
Wink Saville948282b2013-08-29 08:55:16 -07002672 }
2673 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002674
2675 /**
2676 * Set the value for enabling/disabling airplane mode
2677 *
2678 * @param enable whether to enable airplane mode or not
2679 *
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002680 * @hide
2681 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06002682 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002683 public void setAirplaneMode(boolean enable) {
2684 try {
2685 mService.setAirplaneMode(enable);
2686 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002687 throw e.rethrowFromSystemServer();
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002688 }
2689 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002690
2691 /** {@hide} */
Robert Greenwalta67be032014-05-16 15:49:14 -07002692 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07002693 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07002694 mService.registerNetworkFactory(messenger, name);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002695 } catch (RemoteException e) {
2696 throw e.rethrowFromSystemServer();
2697 }
Robert Greenwalta67be032014-05-16 15:49:14 -07002698 }
2699
2700 /** {@hide} */
2701 public void unregisterNetworkFactory(Messenger messenger) {
2702 try {
2703 mService.unregisterNetworkFactory(messenger);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002704 } catch (RemoteException e) {
2705 throw e.rethrowFromSystemServer();
2706 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002707 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002708
Paul Jensen31a94f42015-02-13 14:18:39 -05002709 /**
2710 * @hide
2711 * Register a NetworkAgent with ConnectivityService.
2712 * @return NetID corresponding to NetworkAgent.
2713 */
2714 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002715 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002716 try {
Paul Jensen31a94f42015-02-13 14:18:39 -05002717 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
2718 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002719 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -05002720 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002721 }
2722
Robert Greenwalt9258c642014-03-26 16:47:06 -07002723 /**
Hugo Benichidafed3d2017-03-06 09:17:06 +09002724 * Base class for {@code NetworkRequest} callbacks. Used for notifications about network
2725 * changes. Should be extended by applications wanting notifications.
2726 *
2727 * A {@code NetworkCallback} is registered by calling
2728 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
2729 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)},
Hugo Benichica867dc2018-02-07 21:17:43 +09002730 * or {@link #registerDefaultNetworkCallback(NetworkCallback)}. A {@code NetworkCallback} is
Hugo Benichidafed3d2017-03-06 09:17:06 +09002731 * unregistered by calling {@link #unregisterNetworkCallback(NetworkCallback)}.
2732 * A {@code NetworkCallback} should be registered at most once at any time.
2733 * A {@code NetworkCallback} that has been unregistered can be registered again.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002734 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002735 public static class NetworkCallback {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002736 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002737 * Called when the framework connects to a new network to evaluate whether it satisfies this
2738 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
2739 * callback. There is no guarantee that this new network will satisfy any requests, or that
2740 * the network will stay connected for longer than the time necessary to evaluate it.
2741 * <p>
2742 * Most applications <b>should not</b> act on this callback, and should instead use
2743 * {@link #onAvailable}. This callback is intended for use by applications that can assist
2744 * the framework in properly evaluating the network &mdash; for example, an application that
2745 * can automatically log in to a captive portal without user intervention.
2746 *
2747 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti66276122015-06-11 14:27:17 +09002748 *
2749 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002750 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002751 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002752
2753 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002754 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002755 * This callback may be called more than once if the {@link Network} that is
2756 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002757 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002758 * @param network The {@link Network} of the satisfying network.
Chalard Jean804b8fb2018-01-30 22:41:41 +09002759 * @param networkCapabilities The {@link NetworkCapabilities} of the satisfying network.
2760 * @param linkProperties The {@link LinkProperties} of the satisfying network.
2761 * @hide
2762 */
2763 public void onAvailable(Network network, NetworkCapabilities networkCapabilities,
2764 LinkProperties linkProperties) {
2765 // Internally only this method is called when a new network is available, and
2766 // it calls the callback in the same way and order that older versions used
2767 // to call so as not to change the behavior.
2768 onAvailable(network);
2769 if (!networkCapabilities.hasCapability(
2770 NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)) {
2771 onNetworkSuspended(network);
2772 }
2773 onCapabilitiesChanged(network, networkCapabilities);
2774 onLinkPropertiesChanged(network, linkProperties);
2775 }
2776
2777 /**
2778 * Called when the framework connects and has declared a new network ready for use.
2779 * This callback may be called more than once if the {@link Network} that is
2780 * satisfying the request changes. This will always immediately be followed by a
2781 * call to {@link #onCapabilitiesChanged(Network, NetworkCapabilities)} then by a
2782 * call to {@link #onLinkPropertiesChanged(Network, LinkProperties)}.
2783 *
2784 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002785 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002786 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002787
2788 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002789 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002790 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002791 * for graceful handover. This may not be called if we have a hard loss
2792 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002793 * {@link NetworkCallback#onLost} call or a
2794 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002795 * on whether we lose or regain it.
2796 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002797 * @param network The {@link Network} that is about to be disconnected.
2798 * @param maxMsToLive The time in ms the framework will attempt to keep the
2799 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002800 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002801 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002802 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002803
2804 /**
2805 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002806 * graceful failure ends.
2807 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002808 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002809 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002810 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002811
2812 /**
Etan Cohenaebf17e2017-03-01 12:47:28 -08002813 * Called if no network is found in the timeout time specified in
Hugo Benichi0eec03f2017-05-15 15:15:33 +09002814 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} call. This callback is not
Etan Cohenaebf17e2017-03-01 12:47:28 -08002815 * called for the version of {@link #requestNetwork(NetworkRequest, NetworkCallback)}
2816 * without timeout. When this callback is invoked the associated
2817 * {@link NetworkRequest} will have already been removed and released, as if
2818 * {@link #unregisterNetworkCallback(NetworkCallback)} had been called.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002819 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002820 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002821
2822 /**
2823 * Called when the network the framework connected to for this request
2824 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002825 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002826 * @param network The {@link Network} whose capabilities have changed.
Chalard Jean804b8fb2018-01-30 22:41:41 +09002827 * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this
2828 * network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002829 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002830 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002831 NetworkCapabilities networkCapabilities) {}
2832
2833 /**
2834 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002835 * changes {@link LinkProperties}.
2836 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002837 * @param network The {@link Network} whose link properties have changed.
2838 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002839 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002840 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002841
Robert Greenwalt8d482522015-06-24 13:23:42 -07002842 /**
2843 * Called when the network the framework connected to for this request
Chalard Jean804b8fb2018-01-30 22:41:41 +09002844 * goes into {@link NetworkInfo.State#SUSPENDED}.
Robert Greenwalt8d482522015-06-24 13:23:42 -07002845 * This generally means that while the TCP connections are still live,
2846 * temporarily network data fails to transfer. Specifically this is used
2847 * on cellular networks to mask temporary outages when driving through
2848 * a tunnel, etc.
2849 * @hide
2850 */
2851 public void onNetworkSuspended(Network network) {}
2852
2853 /**
2854 * Called when the network the framework connected to for this request
Chalard Jean804b8fb2018-01-30 22:41:41 +09002855 * returns from a {@link NetworkInfo.State#SUSPENDED} state. This should always be
2856 * preceded by a matching {@link NetworkCallback#onNetworkSuspended} call.
Robert Greenwalt8d482522015-06-24 13:23:42 -07002857 * @hide
2858 */
2859 public void onNetworkResumed(Network network) {}
2860
Robert Greenwalt6078b502014-06-11 16:05:07 -07002861 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002862 }
2863
Hugo Benichicb883232017-05-11 13:16:17 +09002864 /**
2865 * Constant error codes used by ConnectivityService to communicate about failures and errors
2866 * across a Binder boundary.
2867 * @hide
2868 */
2869 public interface Errors {
Chalard Jean4d660112018-06-04 16:52:49 +09002870 int TOO_MANY_REQUESTS = 1;
Hugo Benichicb883232017-05-11 13:16:17 +09002871 }
2872
2873 /** @hide */
2874 public static class TooManyRequestsException extends RuntimeException {}
2875
2876 private static RuntimeException convertServiceException(ServiceSpecificException e) {
2877 switch (e.errorCode) {
2878 case Errors.TOO_MANY_REQUESTS:
2879 return new TooManyRequestsException();
2880 default:
2881 Log.w(TAG, "Unknown service error code " + e.errorCode);
2882 return new RuntimeException(e);
2883 }
2884 }
2885
Robert Greenwalt9258c642014-03-26 16:47:06 -07002886 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002887 /** @hide */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002888 public static final int CALLBACK_PRECHECK = BASE + 1;
2889 /** @hide */
2890 public static final int CALLBACK_AVAILABLE = BASE + 2;
2891 /** @hide arg1 = TTL */
2892 public static final int CALLBACK_LOSING = BASE + 3;
2893 /** @hide */
2894 public static final int CALLBACK_LOST = BASE + 4;
2895 /** @hide */
2896 public static final int CALLBACK_UNAVAIL = BASE + 5;
2897 /** @hide */
2898 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2899 /** @hide */
2900 public static final int CALLBACK_IP_CHANGED = BASE + 7;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002901 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Hugo Benichidba33db2017-03-23 22:40:44 +09002902 private static final int EXPIRE_LEGACY_REQUEST = BASE + 8;
Robert Greenwalt8d482522015-06-24 13:23:42 -07002903 /** @hide */
Hugo Benichidba33db2017-03-23 22:40:44 +09002904 public static final int CALLBACK_SUSPENDED = BASE + 9;
Robert Greenwalt8d482522015-06-24 13:23:42 -07002905 /** @hide */
Hugo Benichidba33db2017-03-23 22:40:44 +09002906 public static final int CALLBACK_RESUMED = BASE + 10;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002907
Erik Kline57faba92015-11-25 12:49:38 +09002908 /** @hide */
2909 public static String getCallbackName(int whichCallback) {
2910 switch (whichCallback) {
2911 case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK";
2912 case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE";
2913 case CALLBACK_LOSING: return "CALLBACK_LOSING";
2914 case CALLBACK_LOST: return "CALLBACK_LOST";
2915 case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL";
2916 case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED";
2917 case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED";
Erik Kline57faba92015-11-25 12:49:38 +09002918 case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST";
2919 case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED";
2920 case CALLBACK_RESUMED: return "CALLBACK_RESUMED";
2921 default:
2922 return Integer.toString(whichCallback);
2923 }
2924 }
2925
Robert Greenwalt562cc542014-05-15 18:07:26 -07002926 private class CallbackHandler extends Handler {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002927 private static final String TAG = "ConnectivityManager.CallbackHandler";
Robert Greenwalta9ebeef2015-09-03 16:41:45 -07002928 private static final boolean DBG = false;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002929
Hugo Benichid42650f2016-07-06 22:53:17 +09002930 CallbackHandler(Looper looper) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002931 super(looper);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002932 }
2933
Hugo Benichi2583ef02017-02-02 17:02:36 +09002934 CallbackHandler(Handler handler) {
Hugo Benichie7678512017-05-09 15:19:01 +09002935 this(Preconditions.checkNotNull(handler, "Handler cannot be null.").getLooper());
Hugo Benichi2583ef02017-02-02 17:02:36 +09002936 }
2937
Robert Greenwalt9258c642014-03-26 16:47:06 -07002938 @Override
2939 public void handleMessage(Message message) {
Hugo Benichi2c684522017-05-09 14:36:02 +09002940 if (message.what == EXPIRE_LEGACY_REQUEST) {
2941 expireRequest((NetworkCapabilities) message.obj, message.arg1);
2942 return;
2943 }
2944
2945 final NetworkRequest request = getObject(message, NetworkRequest.class);
2946 final Network network = getObject(message, Network.class);
2947 final NetworkCallback callback;
2948 synchronized (sCallbacks) {
2949 callback = sCallbacks.get(request);
2950 }
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09002951 if (DBG) {
Hugo Benichia0385682017-03-22 17:07:57 +09002952 Log.d(TAG, getCallbackName(message.what) + " for network " + network);
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09002953 }
Hugo Benichi2c684522017-05-09 14:36:02 +09002954 if (callback == null) {
2955 Log.w(TAG, "callback not found for " + getCallbackName(message.what) + " message");
2956 return;
2957 }
2958
Robert Greenwalt9258c642014-03-26 16:47:06 -07002959 switch (message.what) {
2960 case CALLBACK_PRECHECK: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002961 callback.onPreCheck(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002962 break;
2963 }
2964 case CALLBACK_AVAILABLE: {
Chalard Jean804b8fb2018-01-30 22:41:41 +09002965 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
2966 LinkProperties lp = getObject(message, LinkProperties.class);
2967 callback.onAvailable(network, cap, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002968 break;
2969 }
2970 case CALLBACK_LOSING: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002971 callback.onLosing(network, message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002972 break;
2973 }
2974 case CALLBACK_LOST: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002975 callback.onLost(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002976 break;
2977 }
2978 case CALLBACK_UNAVAIL: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002979 callback.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002980 break;
2981 }
2982 case CALLBACK_CAP_CHANGED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002983 NetworkCapabilities cap = getObject(message, NetworkCapabilities.class);
2984 callback.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002985 break;
2986 }
2987 case CALLBACK_IP_CHANGED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002988 LinkProperties lp = getObject(message, LinkProperties.class);
2989 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002990 break;
2991 }
Robert Greenwalt8d482522015-06-24 13:23:42 -07002992 case CALLBACK_SUSPENDED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002993 callback.onNetworkSuspended(network);
Robert Greenwalt8d482522015-06-24 13:23:42 -07002994 break;
2995 }
2996 case CALLBACK_RESUMED: {
Hugo Benichi2c684522017-05-09 14:36:02 +09002997 callback.onNetworkResumed(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07002998 break;
2999 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003000 }
3001 }
3002
Hugo Benichid42650f2016-07-06 22:53:17 +09003003 private <T> T getObject(Message msg, Class<T> c) {
3004 return (T) msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07003005 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003006 }
3007
Hugo Benichi2583ef02017-02-02 17:02:36 +09003008 private CallbackHandler getDefaultHandler() {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003009 synchronized (sCallbacks) {
3010 if (sCallbackHandler == null) {
3011 sCallbackHandler = new CallbackHandler(ConnectivityThread.getInstanceLooper());
Robert Greenwalt9258c642014-03-26 16:47:06 -07003012 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003013 return sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003014 }
3015 }
3016
Hugo Benichi6f260f32017-02-03 14:18:44 +09003017 private static final HashMap<NetworkRequest, NetworkCallback> sCallbacks = new HashMap<>();
3018 private static CallbackHandler sCallbackHandler;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003019
Hugo Benichi6f260f32017-02-03 14:18:44 +09003020 private static final int LISTEN = 1;
3021 private static final int REQUEST = 2;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003022
Hugo Benichi6f260f32017-02-03 14:18:44 +09003023 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, NetworkCallback callback,
3024 int timeoutMs, int action, int legacyType, CallbackHandler handler) {
Hugo Benichie7678512017-05-09 15:19:01 +09003025 checkCallbackNotNull(callback);
Hugo Benichidafed3d2017-03-06 09:17:06 +09003026 Preconditions.checkArgument(action == REQUEST || need != null, "null NetworkCapabilities");
Hugo Benichid42650f2016-07-06 22:53:17 +09003027 final NetworkRequest request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003028 try {
Hugo Benichid42650f2016-07-06 22:53:17 +09003029 synchronized(sCallbacks) {
Hugo Benichi31c176d2017-06-17 13:14:12 +09003030 if (callback.networkRequest != null
3031 && callback.networkRequest != ALREADY_UNREGISTERED) {
Hugo Benichidafed3d2017-03-06 09:17:06 +09003032 // TODO: throw exception instead and enforce 1:1 mapping of callbacks
3033 // and requests (http://b/20701525).
3034 Log.e(TAG, "NetworkCallback was already registered");
3035 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003036 Messenger messenger = new Messenger(handler);
Hugo Benichid42650f2016-07-06 22:53:17 +09003037 Binder binder = new Binder();
Paul Jensen7221cc32014-06-27 11:05:32 -04003038 if (action == LISTEN) {
Hugo Benichid42650f2016-07-06 22:53:17 +09003039 request = mService.listenForNetwork(need, messenger, binder);
Paul Jensen7221cc32014-06-27 11:05:32 -04003040 } else {
Hugo Benichid42650f2016-07-06 22:53:17 +09003041 request = mService.requestNetwork(
3042 need, messenger, timeoutMs, binder, legacyType);
Paul Jensen7221cc32014-06-27 11:05:32 -04003043 }
Hugo Benichid42650f2016-07-06 22:53:17 +09003044 if (request != null) {
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003045 sCallbacks.put(request, callback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003046 }
Hugo Benichi7724cdd2016-07-07 10:15:56 +09003047 callback.networkRequest = request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003048 }
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003049 } catch (RemoteException e) {
3050 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003051 } catch (ServiceSpecificException e) {
3052 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003053 }
Hugo Benichid42650f2016-07-06 22:53:17 +09003054 return request;
Robert Greenwalt9258c642014-03-26 16:47:06 -07003055 }
3056
3057 /**
Erik Klinea2d29402016-03-16 15:31:39 +09003058 * Helper function to request a network with a particular legacy type.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09003059 *
3060 * This is temporarily public @hide so it can be called by system code that uses the
3061 * NetworkRequest API to request networks but relies on CONNECTIVITY_ACTION broadcasts for
3062 * instead network notifications.
3063 *
3064 * TODO: update said system code to rely on NetworkCallbacks and make this method private.
3065 *
3066 * @hide
3067 */
Lorenzo Colittid1179462015-11-25 15:47:14 +09003068 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
Hugo Benichi2583ef02017-02-02 17:02:36 +09003069 int timeoutMs, int legacyType, Handler handler) {
3070 CallbackHandler cbHandler = new CallbackHandler(handler);
3071 NetworkCapabilities nc = request.networkCapabilities;
3072 sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, legacyType, cbHandler);
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09003073 }
3074
3075 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09003076 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003077 *
3078 * This {@link NetworkRequest} will live until released via
Etan Cohenaebf17e2017-03-01 12:47:28 -08003079 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A
3080 * version of the method which takes a timeout is
Hugo Benichi0eec03f2017-05-15 15:15:33 +09003081 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003082 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07003083 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003084 * can be used to direct traffic to the network.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003085 * <p>It is presently unsupported to request a network with mutable
3086 * {@link NetworkCapabilities} such as
3087 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3088 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3089 * as these {@code NetworkCapabilities} represent states that a particular
3090 * network may never attain, and whether a network will attain these states
3091 * is unknown prior to bringing up the network so the framework does not
3092 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003093 *
3094 * <p>This method requires the caller to hold either the
3095 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3096 * or the ability to modify system settings as determined by
3097 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt9258c642014-03-26 16:47:06 -07003098 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003099 * @param request {@link NetworkRequest} describing this request.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003100 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3101 * the callback must not be shared - it uniquely specifies this request.
3102 * The callback is invoked on the default internal Handler.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003103 * @throws IllegalArgumentException if {@code request} specifies any mutable
3104 * {@code NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003105 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003106 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003107 requestNetwork(request, networkCallback, getDefaultHandler());
3108 }
3109
3110 /**
3111 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
3112 *
3113 * This {@link NetworkRequest} will live until released via
Etan Cohenaebf17e2017-03-01 12:47:28 -08003114 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits. A
3115 * version of the method which takes a timeout is
Hugo Benichi0eec03f2017-05-15 15:15:33 +09003116 * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003117 * Status of the request can be followed by listening to the various
3118 * callbacks described in {@link NetworkCallback}. The {@link Network}
3119 * can be used to direct traffic to the network.
3120 * <p>It is presently unsupported to request a network with mutable
3121 * {@link NetworkCapabilities} such as
3122 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3123 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3124 * as these {@code NetworkCapabilities} represent states that a particular
3125 * network may never attain, and whether a network will attain these states
3126 * is unknown prior to bringing up the network so the framework does not
Chalard Jean4d660112018-06-04 16:52:49 +09003127 * know how to go about satisfying a request with these capabilities.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003128 *
3129 * <p>This method requires the caller to hold either the
3130 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3131 * or the ability to modify system settings as determined by
3132 * {@link android.provider.Settings.System#canWrite}.</p>
3133 *
3134 * @param request {@link NetworkRequest} describing this request.
3135 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3136 * the callback must not be shared - it uniquely specifies this request.
3137 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
3138 * @throws IllegalArgumentException if {@code request} specifies any mutable
3139 * {@code NetworkCapabilities}.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003140 */
3141 public void requestNetwork(
3142 NetworkRequest request, NetworkCallback networkCallback, Handler handler) {
3143 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
3144 CallbackHandler cbHandler = new CallbackHandler(handler);
3145 requestNetwork(request, networkCallback, 0, legacyType, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003146 }
3147
3148 /**
Etan Cohenaebf17e2017-03-01 12:47:28 -08003149 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
3150 * by a timeout.
3151 *
3152 * This function behaves identically to the non-timed-out version
3153 * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network
3154 * is not found within the given time (in milliseconds) the
3155 * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be
3156 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3157 * not have to be released if timed-out (it is automatically released). Unregistering a
3158 * request that timed out is not an error.
3159 *
3160 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3161 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3162 * for that purpose. Calling this method will attempt to bring up the requested network.
3163 *
3164 * <p>This method requires the caller to hold either the
3165 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3166 * or the ability to modify system settings as determined by
3167 * {@link android.provider.Settings.System#canWrite}.</p>
3168 *
3169 * @param request {@link NetworkRequest} describing this request.
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003170 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3171 * the callback must not be shared - it uniquely specifies this request.
Etan Cohenaebf17e2017-03-01 12:47:28 -08003172 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3173 * before {@link NetworkCallback#onUnavailable()} is called. The timeout must
3174 * be a positive value (i.e. >0).
Etan Cohenaebf17e2017-03-01 12:47:28 -08003175 */
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003176 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
3177 int timeoutMs) {
Hugo Benichie7678512017-05-09 15:19:01 +09003178 checkTimeout(timeoutMs);
Hugo Benichi2583ef02017-02-02 17:02:36 +09003179 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
Erik Klineb583b032017-02-22 12:58:24 +09003180 requestNetwork(request, networkCallback, timeoutMs, legacyType, getDefaultHandler());
Hugo Benichi2583ef02017-02-02 17:02:36 +09003181 }
3182
Hugo Benichi2583ef02017-02-02 17:02:36 +09003183 /**
3184 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
3185 * by a timeout.
3186 *
Chalard Jean4d660112018-06-04 16:52:49 +09003187 * This function behaves identically to the version without timeout, but if a suitable
Hugo Benichi2583ef02017-02-02 17:02:36 +09003188 * network is not found within the given time (in milliseconds) the
Etan Cohenaebf17e2017-03-01 12:47:28 -08003189 * {@link NetworkCallback#onUnavailable} callback is called. The request can still be
3190 * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does
3191 * not have to be released if timed-out (it is automatically released). Unregistering a
3192 * request that timed out is not an error.
3193 *
3194 * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small
3195 * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided
3196 * for that purpose. Calling this method will attempt to bring up the requested network.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003197 *
3198 * <p>This method requires the caller to hold either the
3199 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3200 * or the ability to modify system settings as determined by
3201 * {@link android.provider.Settings.System#canWrite}.</p>
3202 *
3203 * @param request {@link NetworkRequest} describing this request.
Etan Cohenaebf17e2017-03-01 12:47:28 -08003204 * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note
3205 * the callback must not be shared - it uniquely specifies this request.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003206 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003207 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
3208 * before {@link NetworkCallback#onUnavailable} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003209 */
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003210 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
3211 Handler handler, int timeoutMs) {
Hugo Benichie7678512017-05-09 15:19:01 +09003212 checkTimeout(timeoutMs);
Hugo Benichi2583ef02017-02-02 17:02:36 +09003213 int legacyType = inferLegacyTypeForNetworkCapabilities(request.networkCapabilities);
3214 CallbackHandler cbHandler = new CallbackHandler(handler);
3215 requestNetwork(request, networkCallback, timeoutMs, legacyType, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003216 }
3217
3218 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003219 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003220 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003221 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfcde58f2015-02-11 16:51:13 -08003222 * <p>
Paul Jensen72db88e2015-03-10 10:54:12 -04003223 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
3224 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003225 */
Erik Kline90e93072014-11-19 12:12:24 +09003226 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003227
3228 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07003229 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003230 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003231 * {@link android.content.Intent#getParcelableExtra(String)}.
3232 */
Erik Kline90e93072014-11-19 12:12:24 +09003233 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003234
3235
3236 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09003237 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003238 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003239 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07003240 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003241 * the request may outlive the calling application and get called back when a suitable
3242 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003243 * <p>
3244 * The operation is an Intent broadcast that goes to a broadcast receiver that
3245 * you registered with {@link Context#registerReceiver} or through the
3246 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3247 * <p>
3248 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09003249 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3250 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07003251 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07003252 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07003253 * Intent to reserve the network or it will be released shortly after the Intent
3254 * is processed.
3255 * <p>
Paul Jensen694f2b82015-06-17 14:15:39 -04003256 * If there is already a request for this Intent registered (with the equality of
Robert Greenwalt9258c642014-03-26 16:47:06 -07003257 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003258 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003259 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003260 * The request may be released normally by calling
3261 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003262 * <p>It is presently unsupported to request a network with either
3263 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3264 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3265 * as these {@code NetworkCapabilities} represent states that a particular
3266 * network may never attain, and whether a network will attain these states
3267 * is unknown prior to bringing up the network so the framework does not
Chalard Jean4d660112018-06-04 16:52:49 +09003268 * know how to go about satisfying a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003269 *
3270 * <p>This method requires the caller to hold either the
3271 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3272 * or the ability to modify system settings as determined by
3273 * {@link android.provider.Settings.System#canWrite}.</p>
3274 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003275 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003276 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07003277 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003278 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003279 * @throws IllegalArgumentException if {@code request} contains either
3280 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3281 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003282 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003283 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003284 checkPendingIntentNotNull(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003285 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07003286 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003287 } catch (RemoteException e) {
3288 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003289 } catch (ServiceSpecificException e) {
3290 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003291 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003292 }
3293
3294 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003295 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
3296 * <p>
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003297 * This method has the same behavior as
3298 * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003299 * releasing network resources and disconnecting.
3300 *
3301 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3302 * PendingIntent passed to
3303 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
3304 * corresponding NetworkRequest you'd like to remove. Cannot be null.
3305 */
3306 public void releaseNetworkRequest(PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003307 checkPendingIntentNotNull(operation);
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003308 try {
3309 mService.releasePendingNetworkRequest(operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003310 } catch (RemoteException e) {
3311 throw e.rethrowFromSystemServer();
3312 }
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003313 }
3314
Hugo Benichie7678512017-05-09 15:19:01 +09003315 private static void checkPendingIntentNotNull(PendingIntent intent) {
3316 Preconditions.checkNotNull(intent, "PendingIntent cannot be null.");
3317 }
3318
3319 private static void checkCallbackNotNull(NetworkCallback callback) {
3320 Preconditions.checkNotNull(callback, "null NetworkCallback");
3321 }
3322
3323 private static void checkTimeout(int timeoutMs) {
3324 Preconditions.checkArgumentPositive(timeoutMs, "timeoutMs must be strictly positive.");
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003325 }
3326
3327 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07003328 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07003329 * {@link NetworkRequest}. The callbacks will continue to be called until
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003330 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003331 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003332 * @param request {@link NetworkRequest} describing this request.
3333 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3334 * networks change state.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003335 * The callback is invoked on the default internal Handler.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003336 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003337 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Robert Greenwalt6078b502014-06-11 16:05:07 -07003338 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003339 registerNetworkCallback(request, networkCallback, getDefaultHandler());
3340 }
3341
3342 /**
3343 * Registers to receive notifications about all networks which satisfy the given
3344 * {@link NetworkRequest}. The callbacks will continue to be called until
3345 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003346 *
3347 * @param request {@link NetworkRequest} describing this request.
3348 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3349 * networks change state.
3350 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003351 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003352 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Hugo Benichi2583ef02017-02-02 17:02:36 +09003353 public void registerNetworkCallback(
3354 NetworkRequest request, NetworkCallback networkCallback, Handler handler) {
3355 CallbackHandler cbHandler = new CallbackHandler(handler);
3356 NetworkCapabilities nc = request.networkCapabilities;
3357 sendRequestForNetwork(nc, networkCallback, 0, LISTEN, TYPE_NONE, cbHandler);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003358 }
3359
3360 /**
Paul Jensen694f2b82015-06-17 14:15:39 -04003361 * Registers a PendingIntent to be sent when a network is available which satisfies the given
3362 * {@link NetworkRequest}.
3363 *
3364 * This function behaves identically to the version that takes a NetworkCallback, but instead
3365 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
3366 * the request may outlive the calling application and get called back when a suitable
3367 * network is found.
3368 * <p>
3369 * The operation is an Intent broadcast that goes to a broadcast receiver that
3370 * you registered with {@link Context#registerReceiver} or through the
3371 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3372 * <p>
3373 * The operation Intent is delivered with two extras, a {@link Network} typed
3374 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3375 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
3376 * the original requests parameters.
3377 * <p>
3378 * If there is already a request for this Intent registered (with the equality of
3379 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
3380 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
3381 * <p>
3382 * The request may be released normally by calling
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003383 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Paul Jensen694f2b82015-06-17 14:15:39 -04003384 * @param request {@link NetworkRequest} describing this request.
3385 * @param operation Action to perform when the network is available (corresponds
3386 * to the {@link NetworkCallback#onAvailable} call. Typically
3387 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
3388 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003389 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Paul Jensen694f2b82015-06-17 14:15:39 -04003390 public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003391 checkPendingIntentNotNull(operation);
Paul Jensen694f2b82015-06-17 14:15:39 -04003392 try {
3393 mService.pendingListenForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003394 } catch (RemoteException e) {
3395 throw e.rethrowFromSystemServer();
Hugo Benichicb883232017-05-11 13:16:17 +09003396 } catch (ServiceSpecificException e) {
3397 throw convertServiceException(e);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003398 }
Paul Jensen694f2b82015-06-17 14:15:39 -04003399 }
3400
3401 /**
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003402 * Registers to receive notifications about changes in the system default network. The callbacks
3403 * will continue to be called until either the application exits or
3404 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Erik Klinea2d29402016-03-16 15:31:39 +09003405 *
3406 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3407 * system default network changes.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003408 * The callback is invoked on the default internal Handler.
Erik Klinea2d29402016-03-16 15:31:39 +09003409 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003410 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Erik Klinea2d29402016-03-16 15:31:39 +09003411 public void registerDefaultNetworkCallback(NetworkCallback networkCallback) {
Hugo Benichi2583ef02017-02-02 17:02:36 +09003412 registerDefaultNetworkCallback(networkCallback, getDefaultHandler());
3413 }
3414
3415 /**
3416 * Registers to receive notifications about changes in the system default network. The callbacks
3417 * will continue to be called until either the application exits or
3418 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003419 *
3420 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3421 * system default network changes.
3422 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003423 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003424 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Hugo Benichi2583ef02017-02-02 17:02:36 +09003425 public void registerDefaultNetworkCallback(NetworkCallback networkCallback, Handler handler) {
Erik Klinea2d29402016-03-16 15:31:39 +09003426 // This works because if the NetworkCapabilities are null,
3427 // ConnectivityService takes them from the default request.
3428 //
3429 // Since the capabilities are exactly the same as the default request's
3430 // capabilities, this request is guaranteed, at all times, to be
3431 // satisfied by the same network, if any, that satisfies the default
3432 // request, i.e., the system default network.
Hugo Benichi2583ef02017-02-02 17:02:36 +09003433 CallbackHandler cbHandler = new CallbackHandler(handler);
Chalard Jean4d660112018-06-04 16:52:49 +09003434 sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0,
3435 REQUEST, TYPE_NONE, cbHandler);
Erik Klinea2d29402016-03-16 15:31:39 +09003436 }
3437
3438 /**
fengludb571472015-04-21 17:12:05 -07003439 * Requests bandwidth update for a given {@link Network} and returns whether the update request
3440 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
3441 * network connection for updated bandwidth information. The caller will be notified via
3442 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003443 * method assumes that the caller has previously called
3444 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network
3445 * changes.
fenglub15e72b2015-03-20 11:29:56 -07003446 *
fengluae519192015-04-27 14:28:04 -07003447 * @param network {@link Network} specifying which network you're interested.
fengludb571472015-04-21 17:12:05 -07003448 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglub15e72b2015-03-20 11:29:56 -07003449 */
fengludb571472015-04-21 17:12:05 -07003450 public boolean requestBandwidthUpdate(Network network) {
fenglub15e72b2015-03-20 11:29:56 -07003451 try {
fengludb571472015-04-21 17:12:05 -07003452 return mService.requestBandwidthUpdate(network);
fenglub15e72b2015-03-20 11:29:56 -07003453 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003454 throw e.rethrowFromSystemServer();
fenglub15e72b2015-03-20 11:29:56 -07003455 }
3456 }
3457
3458 /**
Hugo Benichidafed3d2017-03-06 09:17:06 +09003459 * Unregisters a {@code NetworkCallback} and possibly releases networks originating from
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003460 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and
3461 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls.
3462 * If the given {@code NetworkCallback} had previously been used with
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09003463 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
3464 * will be disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003465 *
Hugo Benichidafed3d2017-03-06 09:17:06 +09003466 * Notifications that would have triggered that {@code NetworkCallback} will immediately stop
3467 * triggering it as soon as this call returns.
3468 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003469 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003470 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003471 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
Hugo Benichie7678512017-05-09 15:19:01 +09003472 checkCallbackNotNull(networkCallback);
Hugo Benichidafed3d2017-03-06 09:17:06 +09003473 final List<NetworkRequest> reqs = new ArrayList<>();
3474 // Find all requests associated to this callback and stop callback triggers immediately.
3475 // Callback is reusable immediately. http://b/20701525, http://b/35921499.
3476 synchronized (sCallbacks) {
Hugo Benichi31c176d2017-06-17 13:14:12 +09003477 Preconditions.checkArgument(networkCallback.networkRequest != null,
3478 "NetworkCallback was not registered");
3479 Preconditions.checkArgument(networkCallback.networkRequest != ALREADY_UNREGISTERED,
3480 "NetworkCallback was already unregistered");
Hugo Benichidafed3d2017-03-06 09:17:06 +09003481 for (Map.Entry<NetworkRequest, NetworkCallback> e : sCallbacks.entrySet()) {
3482 if (e.getValue() == networkCallback) {
3483 reqs.add(e.getKey());
3484 }
3485 }
3486 // TODO: throw exception if callback was registered more than once (http://b/20701525).
3487 for (NetworkRequest r : reqs) {
3488 try {
3489 mService.releaseNetworkRequest(r);
3490 } catch (RemoteException e) {
3491 throw e.rethrowFromSystemServer();
3492 }
3493 // Only remove mapping if rpc was successful.
3494 sCallbacks.remove(r);
3495 }
Hugo Benichi31c176d2017-06-17 13:14:12 +09003496 networkCallback.networkRequest = ALREADY_UNREGISTERED;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003497 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003498 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003499
3500 /**
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003501 * Unregisters a callback previously registered via
3502 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3503 *
3504 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3505 * PendingIntent passed to
3506 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3507 * Cannot be null.
3508 */
3509 public void unregisterNetworkCallback(PendingIntent operation) {
Hugo Benichie7678512017-05-09 15:19:01 +09003510 checkPendingIntentNotNull(operation);
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003511 releaseNetworkRequest(operation);
3512 }
3513
3514 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003515 * Informs the system whether it should switch to {@code network} regardless of whether it is
3516 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
3517 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
3518 * the system default network regardless of any other network that's currently connected. If
3519 * {@code always} is true, then the choice is remembered, so that the next time the user
3520 * connects to this network, the system will switch to it.
3521 *
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003522 * @param network The network to accept.
3523 * @param accept Whether to accept the network even if unvalidated.
3524 * @param always Whether to remember this choice in the future.
3525 *
3526 * @hide
3527 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003528 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003529 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
3530 try {
3531 mService.setAcceptUnvalidated(network, accept, always);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003532 } catch (RemoteException e) {
3533 throw e.rethrowFromSystemServer();
3534 }
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003535 }
3536
3537 /**
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003538 * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is
3539 * only meaningful if the system is configured not to penalize such networks, e.g., if the
3540 * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code
3541 * NETWORK_AVOID_BAD_WIFI setting is unset}.
3542 *
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003543 * @param network The network to accept.
3544 *
3545 * @hide
3546 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003547 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003548 public void setAvoidUnvalidated(Network network) {
3549 try {
3550 mService.setAvoidUnvalidated(network);
3551 } catch (RemoteException e) {
3552 throw e.rethrowFromSystemServer();
3553 }
3554 }
3555
3556 /**
Lorenzo Colitti4734cdb2017-04-27 14:30:21 +09003557 * Requests that the system open the captive portal app on the specified network.
3558 *
3559 * @param network The network to log into.
3560 *
3561 * @hide
3562 */
3563 @RequiresPermission(android.Manifest.permission.CONNECTIVITY_INTERNAL)
3564 public void startCaptivePortalApp(Network network) {
3565 try {
3566 mService.startCaptivePortalApp(network);
3567 } catch (RemoteException e) {
3568 throw e.rethrowFromSystemServer();
3569 }
3570 }
3571
3572 /**
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003573 * It is acceptable to briefly use multipath data to provide seamless connectivity for
3574 * time-sensitive user-facing operations when the system default network is temporarily
Lorenzo Colitti15fd4392017-04-28 00:56:30 +09003575 * unresponsive. The amount of data should be limited (less than one megabyte for every call to
3576 * this method), and the operation should be infrequent to ensure that data usage is limited.
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003577 *
3578 * An example of such an operation might be a time-sensitive foreground activity, such as a
3579 * voice command, that the user is performing while walking out of range of a Wi-Fi network.
3580 */
3581 public static final int MULTIPATH_PREFERENCE_HANDOVER = 1 << 0;
3582
3583 /**
3584 * It is acceptable to use small amounts of multipath data on an ongoing basis to provide
3585 * a backup channel for traffic that is primarily going over another network.
3586 *
3587 * An example might be maintaining backup connections to peers or servers for the purpose of
3588 * fast fallback if the default network is temporarily unresponsive or disconnects. The traffic
3589 * on backup paths should be negligible compared to the traffic on the main path.
3590 */
3591 public static final int MULTIPATH_PREFERENCE_RELIABILITY = 1 << 1;
3592
3593 /**
3594 * It is acceptable to use metered data to improve network latency and performance.
3595 */
3596 public static final int MULTIPATH_PREFERENCE_PERFORMANCE = 1 << 2;
3597
3598 /**
3599 * Return value to use for unmetered networks. On such networks we currently set all the flags
3600 * to true.
3601 * @hide
3602 */
3603 public static final int MULTIPATH_PREFERENCE_UNMETERED =
3604 MULTIPATH_PREFERENCE_HANDOVER |
3605 MULTIPATH_PREFERENCE_RELIABILITY |
3606 MULTIPATH_PREFERENCE_PERFORMANCE;
3607
3608 /** @hide */
3609 @Retention(RetentionPolicy.SOURCE)
3610 @IntDef(flag = true, value = {
3611 MULTIPATH_PREFERENCE_HANDOVER,
3612 MULTIPATH_PREFERENCE_RELIABILITY,
3613 MULTIPATH_PREFERENCE_PERFORMANCE,
3614 })
3615 public @interface MultipathPreference {
3616 }
3617
3618 /**
3619 * Provides a hint to the calling application on whether it is desirable to use the
3620 * multinetwork APIs (e.g., {@link Network#openConnection}, {@link Network#bindSocket}, etc.)
3621 * for multipath data transfer on this network when it is not the system default network.
3622 * Applications desiring to use multipath network protocols should call this method before
3623 * each such operation.
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003624 *
3625 * @param network The network on which the application desires to use multipath data.
3626 * If {@code null}, this method will return the a preference that will generally
3627 * apply to metered networks.
3628 * @return a bitwise OR of zero or more of the {@code MULTIPATH_PREFERENCE_*} constants.
3629 */
Jeff Sharkey30e06bb2017-04-24 11:18:03 -06003630 @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
Lorenzo Colitti2de49252017-01-24 18:08:41 +09003631 public @MultipathPreference int getMultipathPreference(Network network) {
3632 try {
3633 return mService.getMultipathPreference(network);
3634 } catch (RemoteException e) {
3635 throw e.rethrowFromSystemServer();
3636 }
3637 }
3638
3639 /**
Stuart Scott984dc852015-03-30 13:17:11 -07003640 * Resets all connectivity manager settings back to factory defaults.
3641 * @hide
3642 */
3643 public void factoryReset() {
Stuart Scott984dc852015-03-30 13:17:11 -07003644 try {
Stuart Scottf1fb3972015-04-02 18:00:02 -07003645 mService.factoryReset();
Stuart Scott984dc852015-03-30 13:17:11 -07003646 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003647 throw e.rethrowFromSystemServer();
Stuart Scott984dc852015-03-30 13:17:11 -07003648 }
3649 }
3650
3651 /**
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003652 * Binds the current process to {@code network}. All Sockets created in the future
3653 * (and not explicitly bound via a bound SocketFactory from
3654 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3655 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3656 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3657 * work and all host name resolutions will fail. This is by design so an application doesn't
3658 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3659 * To clear binding pass {@code null} for {@code network}. Using individually bound
3660 * Sockets created by Network.getSocketFactory().createSocket() and
3661 * performing network-specific host name resolutions via
3662 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen72db88e2015-03-10 10:54:12 -04003663 * {@code bindProcessToNetwork}.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003664 *
3665 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3666 * the current binding.
3667 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3668 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003669 public boolean bindProcessToNetwork(Network network) {
Chalard Jean4d660112018-06-04 16:52:49 +09003670 // Forcing callers to call through non-static function ensures ConnectivityManager
Paul Jensen72db88e2015-03-10 10:54:12 -04003671 // instantiated.
3672 return setProcessDefaultNetwork(network);
3673 }
3674
3675 /**
3676 * Binds the current process to {@code network}. All Sockets created in the future
3677 * (and not explicitly bound via a bound SocketFactory from
3678 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3679 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3680 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3681 * work and all host name resolutions will fail. This is by design so an application doesn't
3682 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3683 * To clear binding pass {@code null} for {@code network}. Using individually bound
3684 * Sockets created by Network.getSocketFactory().createSocket() and
3685 * performing network-specific host name resolutions via
3686 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
3687 * {@code setProcessDefaultNetwork}.
3688 *
3689 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3690 * the current binding.
3691 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3692 * @deprecated This function can throw {@link IllegalStateException}. Use
3693 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
3694 * is a direct replacement.
3695 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003696 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003697 public static boolean setProcessDefaultNetwork(Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003698 int netId = (network == null) ? NETID_UNSET : network.netId;
Paul Jensen72db88e2015-03-10 10:54:12 -04003699 if (netId == NetworkUtils.getBoundNetworkForProcess()) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003700 return true;
3701 }
3702 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05003703 // Set HTTP proxy system properties to match network.
3704 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colittiec4c5552015-04-22 11:52:48 +09003705 try {
3706 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
3707 } catch (SecurityException e) {
3708 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
3709 Log.e(TAG, "Can't set proxy properties", e);
3710 }
Paul Jensenc91b5342014-08-27 12:38:45 -04003711 // Must flush DNS cache as new network may have different DNS resolutions.
3712 InetAddress.clearDnsCache();
3713 // Must flush socket pool as idle sockets will be bound to previous network and may
3714 // cause subsequent fetches to be performed on old network.
3715 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
3716 return true;
3717 } else {
3718 return false;
3719 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003720 }
3721
3722 /**
3723 * Returns the {@link Network} currently bound to this process via
Paul Jensen72db88e2015-03-10 10:54:12 -04003724 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003725 *
3726 * @return {@code Network} to which this process is bound, or {@code null}.
3727 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003728 public Network getBoundNetworkForProcess() {
3729 // Forcing callers to call thru non-static function ensures ConnectivityManager
3730 // instantiated.
3731 return getProcessDefaultNetwork();
3732 }
3733
3734 /**
3735 * Returns the {@link Network} currently bound to this process via
3736 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
3737 *
3738 * @return {@code Network} to which this process is bound, or {@code null}.
3739 * @deprecated Using this function can lead to other functions throwing
3740 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
3741 * {@code getBoundNetworkForProcess} is a direct replacement.
3742 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003743 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003744 public static Network getProcessDefaultNetwork() {
Paul Jensen72db88e2015-03-10 10:54:12 -04003745 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04003746 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003747 return new Network(netId);
3748 }
3749
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003750 private void unsupportedStartingFrom(int version) {
3751 if (Process.myUid() == Process.SYSTEM_UID) {
3752 // The getApplicationInfo() call we make below is not supported in system context, and
3753 // we want to allow the system to use these APIs anyway.
3754 return;
3755 }
3756
3757 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
3758 throw new UnsupportedOperationException(
3759 "This method is not supported in target SDK version " + version + " and above");
3760 }
3761 }
3762
3763 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
3764 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
Lifu Tang30f95a72016-01-07 23:20:38 -08003765 // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003766 // remove these exemptions. Note that this check is not secure, and apps can still access these
3767 // functions by accessing ConnectivityService directly. However, it should be clear that doing
3768 // so is unsupported and may break in the future. http://b/22728205
3769 private void checkLegacyRoutingApiAccess() {
3770 if (mContext.checkCallingOrSelfPermission("com.android.permission.INJECT_OMADM_SETTINGS")
3771 == PackageManager.PERMISSION_GRANTED) {
3772 return;
3773 }
3774
Dianne Hackborn692a2442015-07-31 10:35:34 -07003775 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003776 }
3777
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003778 /**
3779 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen72db88e2015-03-10 10:54:12 -04003780 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003781 *
3782 * @param network The {@link Network} to bind host resolutions from the current process to, or
3783 * {@code null} to clear the current binding.
3784 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3785 * @hide
3786 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
3787 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003788 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003789 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04003790 return NetworkUtils.bindProcessToNetworkForHostResolution(
3791 network == null ? NETID_UNSET : network.netId);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003792 }
Felipe Leme1b103232016-01-22 09:44:57 -08003793
3794 /**
3795 * Device is not restricting metered network activity while application is running on
3796 * background.
3797 */
3798 public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1;
3799
3800 /**
3801 * Device is restricting metered network activity while application is running on background,
3802 * but application is allowed to bypass it.
3803 * <p>
3804 * In this state, application should take action to mitigate metered network access.
3805 * For example, a music streaming application should switch to a low-bandwidth bitrate.
3806 */
3807 public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2;
3808
3809 /**
3810 * Device is restricting metered network activity while application is running on background.
Felipe Leme9778f762016-01-27 14:46:39 -08003811 * <p>
Felipe Leme1b103232016-01-22 09:44:57 -08003812 * In this state, application should not try to use the network while running on background,
3813 * because it would be denied.
3814 */
3815 public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3;
3816
Felipe Leme9778f762016-01-27 14:46:39 -08003817 /**
3818 * A change in the background metered network activity restriction has occurred.
3819 * <p>
3820 * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction
3821 * applies to them.
3822 * <p>
3823 * This is only sent to registered receivers, not manifest receivers.
3824 */
3825 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
3826 public static final String ACTION_RESTRICT_BACKGROUND_CHANGED =
3827 "android.net.conn.RESTRICT_BACKGROUND_CHANGED";
3828
Felipe Lemeecfccea2016-01-25 11:48:04 -08003829 /** @hide */
3830 @Retention(RetentionPolicy.SOURCE)
Felipe Leme1b103232016-01-22 09:44:57 -08003831 @IntDef(flag = false, value = {
3832 RESTRICT_BACKGROUND_STATUS_DISABLED,
3833 RESTRICT_BACKGROUND_STATUS_WHITELISTED,
3834 RESTRICT_BACKGROUND_STATUS_ENABLED,
3835 })
Felipe Leme1b103232016-01-22 09:44:57 -08003836 public @interface RestrictBackgroundStatus {
3837 }
3838
3839 private INetworkPolicyManager getNetworkPolicyManager() {
3840 synchronized (this) {
3841 if (mNPManager != null) {
3842 return mNPManager;
3843 }
3844 mNPManager = INetworkPolicyManager.Stub.asInterface(ServiceManager
3845 .getService(Context.NETWORK_POLICY_SERVICE));
3846 return mNPManager;
3847 }
3848 }
3849
3850 /**
3851 * Determines if the calling application is subject to metered network restrictions while
3852 * running on background.
Felipe Lemec9c7be52016-05-16 13:57:19 -07003853 *
3854 * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED},
3855 * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED},
3856 * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED}
Felipe Leme1b103232016-01-22 09:44:57 -08003857 */
3858 public @RestrictBackgroundStatus int getRestrictBackgroundStatus() {
3859 try {
3860 return getNetworkPolicyManager().getRestrictBackgroundByCaller();
3861 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003862 throw e.rethrowFromSystemServer();
Felipe Leme1b103232016-01-22 09:44:57 -08003863 }
3864 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003865}