blob: b9e9b283106f98dd6c2dfe27538551bbe0b734bc [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
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -070018import static com.android.internal.util.Preconditions.checkNotNull;
Jeremy Klein36c7aa02016-01-22 14:11:45 -080019
Felipe Leme1b103232016-01-22 09:44:57 -080020import android.annotation.IntDef;
Robin Lee244ce8e2016-01-05 18:03:46 +000021import android.annotation.Nullable;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022import android.annotation.SdkConstant;
23import android.annotation.SdkConstant.SdkConstantType;
Udam Sainib7c24872016-01-04 12:16:14 -080024import android.annotation.SystemApi;
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;
Jeff Sharkey961e3042011-08-29 16:02:57 -070044import android.provider.Settings;
Wink Saville36ffb042014-12-05 11:10:30 -080045import android.telephony.SubscriptionManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080046import android.util.ArrayMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -070047import android.util.Log;
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +090048import android.util.SparseArray;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080049
Robert Greenwaltafa05c02014-05-21 20:04:36 -070050import com.android.internal.telephony.ITelephony;
Robert Greenwalt562cc542014-05-15 18:07:26 -070051import com.android.internal.telephony.PhoneConstants;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070052import com.android.internal.util.Protocol;
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +090053import com.android.internal.util.MessageUtils;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070054
Paul Jensenc91b5342014-08-27 12:38:45 -040055import libcore.net.event.NetworkEventDispatcher;
56
Felipe Leme1b103232016-01-22 09:44:57 -080057import java.lang.annotation.Retention;
58import java.lang.annotation.RetentionPolicy;
Jeremy Kleind42209d2015-12-28 15:11:58 -080059import java.net.InetAddress;
60import java.util.HashMap;
61import java.util.concurrent.atomic.AtomicInteger;
62
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063/**
64 * Class that answers queries about the state of network connectivity. It also
65 * notifies applications when network connectivity changes. Get an instance
66 * of this class by calling
67 * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}.
68 * <p>
69 * The primary responsibilities of this class are to:
70 * <ol>
71 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
72 * <li>Send broadcast intents when network connectivity changes</li>
73 * <li>Attempt to "fail over" to another network when connectivity to a network
74 * is lost</li>
75 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
76 * state of the available networks</li>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070077 * <li>Provide an API that allows applications to request and select networks for their data
78 * traffic</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080079 * </ol>
80 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070081public class ConnectivityManager {
82 private static final String TAG = "ConnectivityManager";
83
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070085 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080086 * been established or lost. The NetworkInfo for the affected network is
87 * sent as an extra; it should be consulted to see what kind of
88 * connectivity event occurred.
89 * <p/>
90 * If this is a connection that was the result of failing over from a
91 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
92 * set to true.
93 * <p/>
94 * For a loss of connectivity, if the connectivity manager is attempting
95 * to connect (or has already connected) to another network, the
96 * NetworkInfo for the new network is also passed as an extra. This lets
97 * any receivers of the broadcast know that they should not necessarily
98 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -080099 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 * the failover attempt succeeded (and so there is still overall data
101 * connectivity), or that the failover attempt failed, meaning that all
102 * connectivity has been lost.
103 * <p/>
104 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
105 * is set to {@code true} if there are no connected networks at all.
106 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800107 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700109
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 /**
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700111 * A temporary hack until SUPL system can get off the legacy APIS.
112 * They do too many network requests and the long list of apps listening
113 * and waking due to the CONNECTIVITY_ACTION bcast makes it expensive.
114 * Use this bcast intent instead for SUPL requests.
115 * @hide
116 */
117 public static final String CONNECTIVITY_ACTION_SUPL =
118 "android.net.conn.CONNECTIVITY_CHANGE_SUPL";
119
120 /**
Paul Jensen25a217c2015-02-27 22:55:47 -0500121 * The device has connected to a network that has presented a captive
122 * portal, which is blocking Internet connectivity. The user was presented
123 * with a notification that network sign in is required,
124 * and the user invoked the notification's action indicating they
Paul Jensen49e3edf2015-05-22 10:50:39 -0400125 * desire to sign in to the network. Apps handling this activity should
Paul Jensen25a217c2015-02-27 22:55:47 -0500126 * facilitate signing in to the network. This action includes a
127 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents
128 * the network presenting the captive portal; all communication with the
129 * captive portal must be done using this {@code Network} object.
130 * <p/>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400131 * This activity includes a {@link CaptivePortal} extra named
132 * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different
133 * outcomes of the captive portal sign in to the system:
134 * <ul>
135 * <li> When the app handling this action believes the user has signed in to
136 * the network and the captive portal has been dismissed, the app should
137 * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can
138 * reevaluate the network. If reevaluation finds the network no longer
139 * subject to a captive portal, the network may become the default active
140 * data network. </li>
141 * <li> When the app handling this action believes the user explicitly wants
Paul Jensen25a217c2015-02-27 22:55:47 -0500142 * to ignore the captive portal and the network, the app should call
Paul Jensen49e3edf2015-05-22 10:50:39 -0400143 * {@link CaptivePortal#ignoreNetwork}. </li>
144 * </ul>
Paul Jensen25a217c2015-02-27 22:55:47 -0500145 */
146 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
147 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL";
148
149 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800150 * The lookup key for a {@link NetworkInfo} object. Retrieve with
151 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700152 *
153 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
154 * should always obtain network information through
Paul Jensen3541e9f2015-03-18 12:23:02 -0400155 * {@link #getActiveNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700156 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700158 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800159 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700160
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700162 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700163 *
164 * @see android.content.Intent#getIntExtra(String, int)
165 */
166 public static final String EXTRA_NETWORK_TYPE = "networkType";
167
168 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 * The lookup key for a boolean that indicates whether a connect event
170 * is for a network to which the connectivity manager was failing over
171 * following a disconnect on another network.
172 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
173 */
174 public static final String EXTRA_IS_FAILOVER = "isFailover";
175 /**
176 * The lookup key for a {@link NetworkInfo} object. This is supplied when
177 * there is another network that it may be possible to connect to. Retrieve with
178 * {@link android.content.Intent#getParcelableExtra(String)}.
179 */
180 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
181 /**
182 * The lookup key for a boolean that indicates whether there is a
183 * complete lack of connectivity, i.e., no network is available.
184 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
185 */
186 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
187 /**
188 * The lookup key for a string that indicates why an attempt to connect
189 * to a network failed. The string has no particular structure. It is
190 * intended to be used in notifications presented to users. Retrieve
191 * it with {@link android.content.Intent#getStringExtra(String)}.
192 */
193 public static final String EXTRA_REASON = "reason";
194 /**
195 * The lookup key for a string that provides optionally supplied
196 * extra information about the network state. The information
197 * may be passed up from the lower networking layers, and its
198 * meaning may be specific to a particular network type. Retrieve
199 * it with {@link android.content.Intent#getStringExtra(String)}.
200 */
201 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700202 /**
203 * The lookup key for an int that provides information about
204 * our connection to the internet at large. 0 indicates no connection,
205 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700206 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700207 * {@hide}
208 */
209 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800210 /**
Paul Jensen49e3edf2015-05-22 10:50:39 -0400211 * The lookup key for a {@link CaptivePortal} object included with the
212 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal}
213 * object can be used to either indicate to the system that the captive
214 * portal has been dismissed or that the user does not want to pursue
215 * signing in to captive portal. Retrieve it with
216 * {@link android.content.Intent#getParcelableExtra(String)}.
Paul Jensen25a217c2015-02-27 22:55:47 -0500217 */
Paul Jensen49e3edf2015-05-22 10:50:39 -0400218 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL";
Jan Nordqvist52eb29f2015-09-22 15:54:32 -0700219
220 /**
221 * Key for passing a URL to the captive portal login activity.
222 */
223 public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL";
224
Paul Jensen25a217c2015-02-27 22:55:47 -0500225 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700226 * Broadcast action to indicate the change of data activity status
227 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800228 * The network becomes active when data transmission is started, or
229 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700230 * {@hide}
231 */
232 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
233 public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
234 /**
235 * The lookup key for an enum that indicates the network device type on which this data activity
236 * change happens.
237 * {@hide}
238 */
239 public static final String EXTRA_DEVICE_TYPE = "deviceType";
240 /**
241 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
242 * it is actively sending or receiving data and {@code false} means it is idle.
243 * {@hide}
244 */
245 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700246 /**
247 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
248 * {@hide}
249 */
250 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700251
252 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800253 * Broadcast Action: The setting for background data usage has changed
254 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
255 * <p>
256 * If an application uses the network in the background, it should listen
257 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700258 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800259 * <p>
260 *
261 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
262 * of background data depends on several combined factors, and
263 * this broadcast is no longer sent. Instead, when background
264 * data is unavailable, {@link #getActiveNetworkInfo()} will now
265 * appear disconnected. During first boot after a platform
266 * upgrade, this broadcast will be sent once if
267 * {@link #getBackgroundDataSetting()} was {@code false} before
268 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800269 */
270 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800271 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800272 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
273 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
274
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700275 /**
276 * Broadcast Action: The network connection may not be good
277 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
278 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
279 * the network and it's condition.
280 * @hide
281 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800282 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700283 public static final String INET_CONDITION_ACTION =
284 "android.net.conn.INET_CONDITION_ACTION";
285
Robert Greenwalt42acef32009-08-12 16:08:25 -0700286 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800287 * Broadcast Action: A tetherable connection has come or gone.
288 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
289 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER} and
290 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
291 * the current state of tethering. Each include a list of
292 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800293 * @hide
294 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800295 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800296 public static final String ACTION_TETHER_STATE_CHANGED =
297 "android.net.conn.TETHER_STATE_CHANGED";
298
299 /**
300 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800301 * gives a String[] listing all the interfaces configured for
302 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800303 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800304 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800305
306 /**
307 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800308 * gives a String[] listing all the interfaces currently tethered
309 * (ie, has dhcp support and packets potentially forwarded/NATed)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800310 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800311 public static final String EXTRA_ACTIVE_TETHER = "activeArray";
312
313 /**
314 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800315 * gives a String[] listing all the interfaces we tried to tether and
316 * failed. Use {@link #getLastTetherError} to find the error code
317 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800318 */
319 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800320
321 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800322 * Broadcast Action: The captive portal tracker has finished its test.
323 * Sent only while running Setup Wizard, in lieu of showing a user
324 * notification.
325 * @hide
326 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800327 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800328 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
329 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
330 /**
331 * The lookup key for a boolean that indicates whether a captive portal was detected.
332 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
333 * @hide
334 */
335 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
336
337 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900338 * Action used to display a dialog that asks the user whether to connect to a network that is
339 * not validated. This intent is used to start the dialog in settings via startActivity.
340 *
341 * @hide
342 */
343 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED";
344
345 /**
Lorenzo Colitti9be58c52016-09-15 14:02:29 +0900346 * Action used to display a dialog that asks the user whether to avoid a network that is no
347 * longer validated. This intent is used to start the dialog in settings via startActivity.
348 *
349 * @hide
350 */
351 public static final String ACTION_PROMPT_LOST_VALIDATION =
352 "android.net.conn.PROMPT_LOST_VALIDATION";
353
354 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800355 * Invalid tethering type.
356 * @see #startTethering(int, OnStartTetheringCallback, boolean)
357 * @hide
358 */
359 public static final int TETHERING_INVALID = -1;
360
361 /**
362 * Wifi tethering type.
363 * @see #startTethering(int, OnStartTetheringCallback, boolean)
364 * @hide
365 */
366 @SystemApi
367 public static final int TETHERING_WIFI = 0;
368
369 /**
370 * USB tethering type.
371 * @see #startTethering(int, OnStartTetheringCallback, boolean)
372 * @hide
373 */
374 @SystemApi
375 public static final int TETHERING_USB = 1;
376
377 /**
378 * Bluetooth tethering type.
379 * @see #startTethering(int, OnStartTetheringCallback, boolean)
380 * @hide
381 */
382 @SystemApi
383 public static final int TETHERING_BLUETOOTH = 2;
384
385 /**
386 * Extra used for communicating with the TetherService. Includes the type of tethering to
387 * enable if any.
388 * @hide
389 */
390 public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType";
391
392 /**
393 * Extra used for communicating with the TetherService. Includes the type of tethering for
394 * which to cancel provisioning.
395 * @hide
396 */
397 public static final String EXTRA_REM_TETHER_TYPE = "extraRemTetherType";
398
399 /**
400 * Extra used for communicating with the TetherService. True to schedule a recheck of tether
401 * provisioning.
402 * @hide
403 */
404 public static final String EXTRA_SET_ALARM = "extraSetAlarm";
405
406 /**
407 * Tells the TetherService to run a provision check now.
408 * @hide
409 */
410 public static final String EXTRA_RUN_PROVISION = "extraRunProvision";
411
412 /**
413 * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver}
414 * which will receive provisioning results. Can be left empty.
415 * @hide
416 */
417 public static final String EXTRA_PROVISION_CALLBACK = "extraProvisionCallback";
418
419 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800420 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700421 * @hide
422 */
423 public static final int TYPE_NONE = -1;
424
425 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800426 * The Mobile data connection. When active, all data traffic
427 * will use this network type's interface by default
428 * (it has a default route)
Robert Greenwalt42acef32009-08-12 16:08:25 -0700429 */
430 public static final int TYPE_MOBILE = 0;
431 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800432 * The WIFI data connection. When active, all data traffic
433 * will use this network type's interface by default
434 * (it has a default route).
Robert Greenwalt42acef32009-08-12 16:08:25 -0700435 */
436 public static final int TYPE_WIFI = 1;
437 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800438 * An MMS-specific Mobile data connection. This network type may use the
439 * same network interface as {@link #TYPE_MOBILE} or it may use a different
440 * one. This is used by applications needing to talk to the carrier's
441 * Multimedia Messaging Service servers.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900442 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900443 * @deprecated Applications should instead use
444 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900445 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700446 */
447 public static final int TYPE_MOBILE_MMS = 2;
448 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800449 * A SUPL-specific Mobile data connection. This network type may use the
450 * same network interface as {@link #TYPE_MOBILE} or it may use a different
451 * one. This is used by applications needing to talk to the carrier's
452 * Secure User Plane Location servers for help locating the device.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900453 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900454 * @deprecated Applications should instead use
455 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900456 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700457 */
458 public static final int TYPE_MOBILE_SUPL = 3;
459 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800460 * A DUN-specific Mobile data connection. This network type may use the
461 * same network interface as {@link #TYPE_MOBILE} or it may use a different
462 * one. This is sometimes by the system when setting up an upstream connection
463 * for tethering so that the carrier is aware of DUN traffic.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700464 */
465 public static final int TYPE_MOBILE_DUN = 4;
466 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800467 * A High Priority Mobile data connection. This network type uses the
468 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colittie285b432015-04-23 15:32:42 +0900469 * is different.
470 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900471 * @deprecated Applications should instead use
472 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900473 * uses the {@link NetworkCapabilities#TRANSPORT_CELLULAR} transport.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700474 */
475 public static final int TYPE_MOBILE_HIPRI = 5;
jsh8214deb2010-03-11 15:04:43 -0800476 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800477 * The WiMAX data connection. When active, all data traffic
478 * will use this network type's interface by default
479 * (it has a default route).
jsh8214deb2010-03-11 15:04:43 -0800480 */
481 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800482
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800483 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800484 * The Bluetooth data connection. When active, all data traffic
485 * will use this network type's interface by default
486 * (it has a default route).
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800487 */
488 public static final int TYPE_BLUETOOTH = 7;
489
Robert Greenwalt60810842011-04-22 15:28:18 -0700490 /**
491 * Dummy data connection. This should not be used on shipping devices.
492 */
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800493 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800494
Robert Greenwalt60810842011-04-22 15:28:18 -0700495 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800496 * The Ethernet data connection. When active, all data traffic
497 * will use this network type's interface by default
498 * (it has a default route).
Robert Greenwalt60810842011-04-22 15:28:18 -0700499 */
Robert Greenwalte12aec92011-01-28 14:48:37 -0800500 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700501
Wink Saville9d7d6282011-03-12 14:52:01 -0800502 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800503 * Over the air Administration.
Wink Saville9d7d6282011-03-12 14:52:01 -0800504 * {@hide}
505 */
506 public static final int TYPE_MOBILE_FOTA = 10;
507
508 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800509 * IP Multimedia Subsystem.
Wink Saville9d7d6282011-03-12 14:52:01 -0800510 * {@hide}
511 */
512 public static final int TYPE_MOBILE_IMS = 11;
513
514 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800515 * Carrier Branded Services.
Wink Saville9d7d6282011-03-12 14:52:01 -0800516 * {@hide}
517 */
518 public static final int TYPE_MOBILE_CBS = 12;
519
repo syncaea743a2011-07-29 23:55:49 -0700520 /**
521 * A Wi-Fi p2p connection. Only requesting processes will have access to
522 * the peers connected.
523 * {@hide}
524 */
525 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800526
Wink Saville5e56bc52013-07-29 15:00:57 -0700527 /**
528 * The network to use for initially attaching to the network
529 * {@hide}
530 */
531 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700532
Lorenzo Colittie285b432015-04-23 15:32:42 +0900533 /**
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700534 * Emergency PDN connection for emergency services. This
535 * may include IMS and MMS in emergency situations.
Ram3e0e3bc2014-06-26 11:03:44 -0700536 * {@hide}
537 */
538 public static final int TYPE_MOBILE_EMERGENCY = 15;
539
Hui Lu1c5624a2014-01-15 11:05:36 -0500540 /**
541 * The network that uses proxy to achieve connectivity.
542 * {@hide}
543 */
544 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700545
Robert Greenwalt8283f882014-07-07 17:09:01 -0700546 /**
547 * A virtual network using one or more native bearers.
548 * It may or may not be providing security services.
549 */
550 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500551
552 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700553 public static final int MAX_RADIO_TYPE = TYPE_VPN;
554
555 /** {@hide} */
556 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800558 /**
559 * If you want to set the default network preference,you can directly
560 * change the networkAttributes array in framework's config.xml.
561 *
562 * @deprecated Since we support so many more networks now, the single
563 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800564 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800565 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800566 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800567 * from an App.
568 */
569 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800570 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
571
Jeff Sharkey625239a2012-09-26 22:03:49 -0700572 /**
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700573 * @hide
574 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700575 public final static int REQUEST_ID_UNSET = 0;
576
Paul Jensen5d59e782014-07-11 12:28:19 -0400577 /**
578 * A NetID indicating no Network is selected.
579 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
580 * @hide
581 */
582 public static final int NETID_UNSET = 0;
583
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700584 private final IConnectivityManager mService;
Paul Jensene0bef712014-12-10 15:12:18 -0500585 /**
586 * A kludge to facilitate static access where a Context pointer isn't available, like in the
587 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
588 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
589 * methods that take a Context argument.
590 */
591 private static ConnectivityManager sInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800592
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900593 private final Context mContext;
594
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800595 private INetworkManagementService mNMService;
Felipe Leme1b103232016-01-22 09:44:57 -0800596 private INetworkPolicyManager mNPManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800597
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800598 /**
599 * Tests if a given integer represents a valid network type.
600 * @param networkType the type to be tested
601 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensen9e59e122015-05-06 10:42:25 -0400602 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
603 * validate a network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800604 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700605 public static boolean isNetworkTypeValid(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700606 return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800607 }
608
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800609 /**
610 * Returns a non-localized string representing a given network type.
611 * ONLY used for debugging output.
612 * @param type the type needing naming
613 * @return a String for the given type, or a string version of the type ("87")
614 * if no name is known.
615 * {@hide}
616 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700617 public static String getNetworkTypeName(int type) {
618 switch (type) {
619 case TYPE_MOBILE:
620 return "MOBILE";
621 case TYPE_WIFI:
622 return "WIFI";
623 case TYPE_MOBILE_MMS:
624 return "MOBILE_MMS";
625 case TYPE_MOBILE_SUPL:
626 return "MOBILE_SUPL";
627 case TYPE_MOBILE_DUN:
628 return "MOBILE_DUN";
629 case TYPE_MOBILE_HIPRI:
630 return "MOBILE_HIPRI";
631 case TYPE_WIMAX:
632 return "WIMAX";
633 case TYPE_BLUETOOTH:
634 return "BLUETOOTH";
635 case TYPE_DUMMY:
636 return "DUMMY";
637 case TYPE_ETHERNET:
638 return "ETHERNET";
639 case TYPE_MOBILE_FOTA:
640 return "MOBILE_FOTA";
641 case TYPE_MOBILE_IMS:
642 return "MOBILE_IMS";
643 case TYPE_MOBILE_CBS:
644 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700645 case TYPE_WIFI_P2P:
646 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700647 case TYPE_MOBILE_IA:
648 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700649 case TYPE_MOBILE_EMERGENCY:
650 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500651 case TYPE_PROXY:
652 return "PROXY";
Erik Kline37fbfa12014-11-19 17:23:41 +0900653 case TYPE_VPN:
654 return "VPN";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700655 default:
656 return Integer.toString(type);
657 }
658 }
659
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800660 /**
661 * Checks if a given type uses the cellular data connection.
662 * This should be replaced in the future by a network property.
663 * @param networkType the type to check
664 * @return a boolean - {@code true} if uses cellular network, else {@code false}
665 * {@hide}
666 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700667 public static boolean isNetworkTypeMobile(int networkType) {
668 switch (networkType) {
669 case TYPE_MOBILE:
670 case TYPE_MOBILE_MMS:
671 case TYPE_MOBILE_SUPL:
672 case TYPE_MOBILE_DUN:
673 case TYPE_MOBILE_HIPRI:
674 case TYPE_MOBILE_FOTA:
675 case TYPE_MOBILE_IMS:
676 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700677 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700678 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700679 return true;
680 default:
681 return false;
682 }
683 }
684
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800685 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700686 * Checks if the given network type is backed by a Wi-Fi radio.
687 *
688 * @hide
689 */
690 public static boolean isNetworkTypeWifi(int networkType) {
691 switch (networkType) {
692 case TYPE_WIFI:
693 case TYPE_WIFI_P2P:
694 return true;
695 default:
696 return false;
697 }
698 }
699
700 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800701 * Specifies the preferred network type. When the device has more
702 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800703 *
704 * @param preference the network type to prefer over all others. It is
705 * unspecified what happens to the old preferred network in the
706 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700707 * @deprecated Functionality has been removed as it no longer makes sense,
708 * with many more than two networks - we'd need an array to express
709 * preference. Instead we use dynamic network properties of
710 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800711 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800712 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800713 }
714
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800715 /**
716 * Retrieves the current preferred network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400717 * <p>This method requires the caller to hold the permission
718 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800719 *
720 * @return an integer representing the preferred network type
721 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700722 * @deprecated Functionality has been removed as it no longer makes sense,
723 * with many more than two networks - we'd need an array to express
724 * preference. Instead we use dynamic network properties of
725 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800726 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700728 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800729 }
730
Scott Main671644c2011-10-06 19:02:28 -0700731 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800732 * Returns details about the currently active default data network. When
733 * connected, this network is the default route for outgoing connections.
734 * You should always check {@link NetworkInfo#isConnected()} before initiating
735 * network traffic. This may return {@code null} when there is no default
736 * network.
Paul Jensenb2748922015-05-06 11:10:18 -0400737 * <p>This method requires the caller to hold the permission
738 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800739 *
740 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500741 * or {@code null} if no default network is currently active
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700742 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800743 public NetworkInfo getActiveNetworkInfo() {
744 try {
745 return mService.getActiveNetworkInfo();
746 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700747 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800748 }
749 }
750
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800751 /**
Paul Jensen31a94f42015-02-13 14:18:39 -0500752 * Returns a {@link Network} object corresponding to the currently active
753 * default data network. In the event that the current active default data
754 * network disconnects, the returned {@code Network} object will no longer
755 * be usable. This will return {@code null} when there is no default
756 * network.
Paul Jensenb2748922015-05-06 11:10:18 -0400757 * <p>This method requires the caller to hold the permission
758 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Paul Jensen31a94f42015-02-13 14:18:39 -0500759 *
760 * @return a {@link Network} object for the current default network or
761 * {@code null} if no default network is currently active
Paul Jensen31a94f42015-02-13 14:18:39 -0500762 */
763 public Network getActiveNetwork() {
764 try {
765 return mService.getActiveNetwork();
766 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700767 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -0500768 }
769 }
770
771 /**
Robin Leed2baf792016-03-24 12:07:00 +0000772 * Returns a {@link Network} object corresponding to the currently active
773 * default data network for a specific UID. In the event that the default data
774 * network disconnects, the returned {@code Network} object will no longer
775 * be usable. This will return {@code null} when there is no default
776 * network for the UID.
777 * <p>This method requires the caller to hold the permission
778 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
779 *
780 * @return a {@link Network} object for the current default network for the
781 * given UID or {@code null} if no default network is currently active
782 *
783 * @hide
784 */
785 public Network getActiveNetworkForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600786 return getActiveNetworkForUid(uid, false);
787 }
788
789 /** {@hide} */
790 public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) {
Robin Leed2baf792016-03-24 12:07:00 +0000791 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600792 return mService.getActiveNetworkForUid(uid, ignoreBlocked);
Robin Leed2baf792016-03-24 12:07:00 +0000793 } catch (RemoteException e) {
794 throw e.rethrowFromSystemServer();
795 }
796 }
797
798 /**
Robin Lee244ce8e2016-01-05 18:03:46 +0000799 * Configures an always-on VPN connection through a specific application.
800 * This connection is automatically granted and persisted after a reboot.
801 *
802 * <p>The designated package should declare a {@link VpnService} in its
803 * manifest guarded by {@link android.Manifest.permission.BIND_VPN_SERVICE},
804 * otherwise the call will fail.
805 *
806 * @param userId The identifier of the user to set an always-on VPN for.
807 * @param vpnPackage The package name for an installed VPN app on the device, or {@code null}
808 * to remove an existing always-on VPN configuration.
Robin Leedc679712016-05-03 13:23:03 +0100809 * @param lockdownEnabled {@code true} to disallow networking when the VPN is not connected or
810 * {@code false} otherwise.
Robin Lee244ce8e2016-01-05 18:03:46 +0000811 * @return {@code true} if the package is set as always-on VPN controller;
812 * {@code false} otherwise.
813 * @hide
814 */
Robin Leedc679712016-05-03 13:23:03 +0100815 public boolean setAlwaysOnVpnPackageForUser(int userId, @Nullable String vpnPackage,
816 boolean lockdownEnabled) {
Robin Lee244ce8e2016-01-05 18:03:46 +0000817 try {
Robin Leedc679712016-05-03 13:23:03 +0100818 return mService.setAlwaysOnVpnPackage(userId, vpnPackage, lockdownEnabled);
Robin Lee244ce8e2016-01-05 18:03:46 +0000819 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700820 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +0000821 }
822 }
823
824 /**
825 * Returns the package name of the currently set always-on VPN application.
826 * If there is no always-on VPN set, or the VPN is provided by the system instead
827 * of by an app, {@code null} will be returned.
828 *
829 * @return Package name of VPN controller responsible for always-on VPN,
830 * or {@code null} if none is set.
831 * @hide
832 */
833 public String getAlwaysOnVpnPackageForUser(int userId) {
834 try {
835 return mService.getAlwaysOnVpnPackage(userId);
836 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700837 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +0000838 }
839 }
840
841 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800842 * Returns details about the currently active default data network
843 * for a given uid. This is for internal use only to avoid spying
844 * other apps.
Paul Jensenb2748922015-05-06 11:10:18 -0400845 * <p>This method requires the caller to hold the permission
846 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800847 *
848 * @return a {@link NetworkInfo} object for the current default network
849 * for the given uid or {@code null} if no default network is
850 * available for the specified uid.
851 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800852 * {@hide}
853 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700854 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600855 return getActiveNetworkInfoForUid(uid, false);
856 }
857
858 /** {@hide} */
859 public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700860 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600861 return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700862 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700863 throw e.rethrowFromSystemServer();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700864 }
865 }
866
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800867 /**
868 * Returns connection status information about a particular
869 * network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400870 * <p>This method requires the caller to hold the permission
871 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800872 *
873 * @param networkType integer specifying which networkType in
874 * which you're interested.
875 * @return a {@link NetworkInfo} object for the requested
876 * network type or {@code null} if the type is not
877 * supported by the device.
878 *
Paul Jensen3541e9f2015-03-18 12:23:02 -0400879 * @deprecated This method does not support multiple connected networks
880 * of the same type. Use {@link #getAllNetworks} and
881 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800882 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800883 public NetworkInfo getNetworkInfo(int networkType) {
884 try {
885 return mService.getNetworkInfo(networkType);
886 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700887 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 }
889 }
890
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800891 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700892 * Returns connection status information about a particular
893 * Network.
Paul Jensenb2748922015-05-06 11:10:18 -0400894 * <p>This method requires the caller to hold the permission
895 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700896 *
897 * @param network {@link Network} specifying which network
898 * in which you're interested.
899 * @return a {@link NetworkInfo} object for the requested
900 * network or {@code null} if the {@code Network}
901 * is not valid.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700902 */
903 public NetworkInfo getNetworkInfo(Network network) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600904 return getNetworkInfoForUid(network, Process.myUid(), false);
905 }
906
907 /** {@hide} */
908 public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700909 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600910 return mService.getNetworkInfoForUid(network, uid, ignoreBlocked);
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700911 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700912 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700913 }
914 }
915
916 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800917 * Returns connection status information about all network
918 * types supported by the device.
Paul Jensenb2748922015-05-06 11:10:18 -0400919 * <p>This method requires the caller to hold the permission
920 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800921 *
922 * @return an array of {@link NetworkInfo} objects. Check each
923 * {@link NetworkInfo#getType} for which type each applies.
924 *
Paul Jensen3541e9f2015-03-18 12:23:02 -0400925 * @deprecated This method does not support multiple connected networks
926 * of the same type. Use {@link #getAllNetworks} and
927 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800928 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 public NetworkInfo[] getAllNetworkInfo() {
930 try {
931 return mService.getAllNetworkInfo();
932 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700933 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 }
935 }
936
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800937 /**
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700938 * Returns the {@link Network} object currently serving a given type, or
939 * null if the given type is not connected.
940 *
941 * <p>This method requires the caller to hold the permission
942 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
943 *
944 * @hide
Paul Jensen3541e9f2015-03-18 12:23:02 -0400945 * @deprecated This method does not support multiple connected networks
946 * of the same type. Use {@link #getAllNetworks} and
947 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700948 */
949 public Network getNetworkForType(int networkType) {
950 try {
951 return mService.getNetworkForType(networkType);
952 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700953 throw e.rethrowFromSystemServer();
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700954 }
955 }
956
957 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700958 * Returns an array of all {@link Network} currently tracked by the
959 * framework.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700960 * <p>This method requires the caller to hold the permission
961 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Paul Jensenb2748922015-05-06 11:10:18 -0400962 *
963 * @return an array of {@link Network} objects.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700964 */
965 public Network[] getAllNetworks() {
966 try {
967 return mService.getAllNetworks();
968 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700969 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700970 }
971 }
972
973 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +0900974 * Returns an array of {@link android.net.NetworkCapabilities} objects, representing
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900975 * the Networks that applications run by the given user will use by default.
976 * @hide
977 */
978 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
979 try {
980 return mService.getDefaultNetworkCapabilitiesForUser(userId);
981 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700982 throw e.rethrowFromSystemServer();
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900983 }
984 }
985
986 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800987 * Returns the IP information for the current default network.
Paul Jensenb2748922015-05-06 11:10:18 -0400988 * <p>This method requires the caller to hold the permission
989 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800990 *
991 * @return a {@link LinkProperties} object describing the IP info
992 * for the current default network, or {@code null} if there
993 * is no current default network.
994 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800995 * {@hide}
996 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700997 public LinkProperties getActiveLinkProperties() {
998 try {
999 return mService.getActiveLinkProperties();
1000 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001001 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001002 }
1003 }
1004
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001005 /**
1006 * Returns the IP information for a given network type.
Paul Jensenb2748922015-05-06 11:10:18 -04001007 * <p>This method requires the caller to hold the permission
1008 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001009 *
1010 * @param networkType the network type of interest.
1011 * @return a {@link LinkProperties} object describing the IP info
1012 * for the given networkType, or {@code null} if there is
1013 * no current default network.
1014 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001015 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04001016 * @deprecated This method does not support multiple connected networks
1017 * of the same type. Use {@link #getAllNetworks},
1018 * {@link #getNetworkInfo(android.net.Network)}, and
1019 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001020 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001021 public LinkProperties getLinkProperties(int networkType) {
1022 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001023 return mService.getLinkPropertiesForType(networkType);
1024 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001025 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001026 }
1027 }
1028
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001029 /**
1030 * Get the {@link LinkProperties} for the given {@link Network}. This
1031 * will return {@code null} if the network is unknown.
Paul Jensenb2748922015-05-06 11:10:18 -04001032 * <p>This method requires the caller to hold the permission
1033 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001034 *
1035 * @param network The {@link Network} object identifying the network in question.
1036 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensenb2748922015-05-06 11:10:18 -04001037 */
Robert Greenwalt9258c642014-03-26 16:47:06 -07001038 public LinkProperties getLinkProperties(Network network) {
1039 try {
1040 return mService.getLinkProperties(network);
1041 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001042 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001043 }
1044 }
1045
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001046 /**
Erik Klineacdd6392016-07-07 16:50:58 +09001047 * Request that this callback be invoked at ConnectivityService's earliest
1048 * convenience with the current satisfying network's LinkProperties.
1049 * If no such network exists no callback invocation is performed.
1050 *
1051 * The callback must have been registered with #requestNetwork() or
1052 * #registerDefaultNetworkCallback(); callbacks registered with
1053 * registerNetworkCallback() are not specific to any particular Network so
1054 * do not cause any updates.
1055 *
1056 * @hide
1057 */
1058 public void requestLinkProperties(NetworkCallback networkCallback) {
1059 try {
1060 mService.requestLinkProperties(networkCallback.networkRequest);
1061 } catch (RemoteException e) {
1062 throw e.rethrowFromSystemServer();
1063 }
1064 }
1065
1066 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001067 * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001068 * will return {@code null} if the network is unknown.
Paul Jensenb2748922015-05-06 11:10:18 -04001069 * <p>This method requires the caller to hold the permission
1070 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001071 *
1072 * @param network The {@link Network} object identifying the network in question.
Lorenzo Colittie285b432015-04-23 15:32:42 +09001073 * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001074 */
Robert Greenwalt9258c642014-03-26 16:47:06 -07001075 public NetworkCapabilities getNetworkCapabilities(Network network) {
1076 try {
1077 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001078 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001079 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001080 }
1081 }
1082
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001083 /**
Erik Klineacdd6392016-07-07 16:50:58 +09001084 * Request that this callback be invoked at ConnectivityService's earliest
1085 * convenience with the current satisfying network's NetworkCapabilities.
1086 * If no such network exists no callback invocation is performed.
1087 *
1088 * The callback must have been registered with #requestNetwork() or
1089 * #registerDefaultNetworkCallback(); callbacks registered with
1090 * registerNetworkCallback() are not specific to any particular Network so
1091 * do not cause any updates.
1092 *
1093 * @hide
1094 */
1095 public void requestNetworkCapabilities(NetworkCallback networkCallback) {
1096 try {
1097 mService.requestNetworkCapabilities(networkCallback.networkRequest);
1098 } catch (RemoteException e) {
1099 throw e.rethrowFromSystemServer();
1100 }
1101 }
1102
1103 /**
Udam Sainib7c24872016-01-04 12:16:14 -08001104 * Gets the URL that should be used for resolving whether a captive portal is present.
1105 * 1. This URL should respond with a 204 response to a GET request to indicate no captive
1106 * portal is present.
1107 * 2. This URL must be HTTP as redirect responses are used to find captive portal
1108 * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects.
1109 *
1110 * @hide
1111 */
1112 @SystemApi
1113 public String getCaptivePortalServerUrl() {
1114 try {
1115 return mService.getCaptivePortalServerUrl();
1116 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001117 throw e.rethrowFromSystemServer();
Udam Sainib7c24872016-01-04 12:16:14 -08001118 }
1119 }
1120
1121 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 * Tells the underlying networking system that the caller wants to
1123 * begin using the named feature. The interpretation of {@code feature}
1124 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001125 *
1126 * <p>This method requires the caller to hold either the
1127 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1128 * or the ability to modify system settings as determined by
1129 * {@link android.provider.Settings.System#canWrite}.</p>
1130 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001131 * @param networkType specifies which network the request pertains to
1132 * @param feature the name of the feature to be used
1133 * @return an integer value representing the outcome of the request.
1134 * The interpretation of this value is specific to each networking
1135 * implementation+feature combination, except that the value {@code -1}
1136 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001137 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001138 * @deprecated Deprecated in favor of the cleaner
1139 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001140 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001141 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001142 */
1143 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001144 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001145 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1146 if (netCap == null) {
1147 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
1148 feature);
1149 return PhoneConstants.APN_REQUEST_FAILED;
1150 }
1151
1152 NetworkRequest request = null;
1153 synchronized (sLegacyRequests) {
1154 LegacyRequest l = sLegacyRequests.get(netCap);
1155 if (l != null) {
1156 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
1157 renewRequestLocked(l);
1158 if (l.currentNetwork != null) {
1159 return PhoneConstants.APN_ALREADY_ACTIVE;
1160 } else {
1161 return PhoneConstants.APN_REQUEST_STARTED;
1162 }
1163 }
1164
1165 request = requestNetworkForFeatureLocked(netCap);
1166 }
1167 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -07001168 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001169 return PhoneConstants.APN_REQUEST_STARTED;
1170 } else {
1171 Log.d(TAG, " request Failed");
1172 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001173 }
1174 }
1175
1176 /**
1177 * Tells the underlying networking system that the caller is finished
1178 * using the named feature. The interpretation of {@code feature}
1179 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001180 *
1181 * <p>This method requires the caller to hold either the
1182 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1183 * or the ability to modify system settings as determined by
1184 * {@link android.provider.Settings.System#canWrite}.</p>
1185 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001186 * @param networkType specifies which network the request pertains to
1187 * @param feature the name of the feature that is no longer needed
1188 * @return an integer value representing the outcome of the request.
1189 * The interpretation of this value is specific to each networking
1190 * implementation+feature combination, except that the value {@code -1}
1191 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001192 *
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09001193 * @deprecated Deprecated in favor of the cleaner
1194 * {@link #unregisterNetworkCallback(NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001195 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001196 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001197 */
1198 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001199 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001200 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1201 if (netCap == null) {
1202 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
1203 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 return -1;
1205 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001206
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001207 if (removeRequestForFeature(netCap)) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001208 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001209 }
1210 return 1;
1211 }
1212
1213 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1214 if (networkType == TYPE_MOBILE) {
1215 int cap = -1;
1216 if ("enableMMS".equals(feature)) {
1217 cap = NetworkCapabilities.NET_CAPABILITY_MMS;
1218 } else if ("enableSUPL".equals(feature)) {
1219 cap = NetworkCapabilities.NET_CAPABILITY_SUPL;
1220 } else if ("enableDUN".equals(feature) || "enableDUNAlways".equals(feature)) {
1221 cap = NetworkCapabilities.NET_CAPABILITY_DUN;
1222 } else if ("enableHIPRI".equals(feature)) {
1223 cap = NetworkCapabilities.NET_CAPABILITY_INTERNET;
1224 } else if ("enableFOTA".equals(feature)) {
1225 cap = NetworkCapabilities.NET_CAPABILITY_FOTA;
1226 } else if ("enableIMS".equals(feature)) {
1227 cap = NetworkCapabilities.NET_CAPABILITY_IMS;
1228 } else if ("enableCBS".equals(feature)) {
1229 cap = NetworkCapabilities.NET_CAPABILITY_CBS;
1230 } else {
1231 return null;
1232 }
1233 NetworkCapabilities netCap = new NetworkCapabilities();
Robert Greenwalt7569f182014-06-08 16:42:59 -07001234 netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(cap);
Paul Jensen487ffe72015-07-24 15:57:11 -04001235 netCap.maybeMarkCapabilitiesRestricted();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001236 return netCap;
1237 } else if (networkType == TYPE_WIFI) {
1238 if ("p2p".equals(feature)) {
1239 NetworkCapabilities netCap = new NetworkCapabilities();
1240 netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
Robert Greenwalt7569f182014-06-08 16:42:59 -07001241 netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
Paul Jensen487ffe72015-07-24 15:57:11 -04001242 netCap.maybeMarkCapabilitiesRestricted();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001243 return netCap;
1244 }
1245 }
1246 return null;
1247 }
1248
Robert Greenwalt06314e42014-10-29 14:04:06 -07001249 /**
1250 * Guess what the network request was trying to say so that the resulting
1251 * network is accessible via the legacy (deprecated) API such as
1252 * requestRouteToHost.
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001253 *
1254 * This means we should try to be fairly precise about transport and
Robert Greenwalt06314e42014-10-29 14:04:06 -07001255 * capability but ignore things such as networkSpecifier.
1256 * If the request has more than one transport or capability it doesn't
1257 * match the old legacy requests (they selected only single transport/capability)
1258 * so this function cannot map the request to a single legacy type and
1259 * the resulting network will not be available to the legacy APIs.
1260 *
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001261 * This code is only called from the requestNetwork API (L and above).
1262 *
1263 * Setting a legacy type causes CONNECTIVITY_ACTION broadcasts, which are expensive
1264 * because they wake up lots of apps - see http://b/23350688 . So we currently only
1265 * do this for SUPL requests, which are the only ones that we know need it. If
1266 * omitting these broadcasts causes unacceptable app breakage, then for backwards
1267 * compatibility we can send them:
1268 *
1269 * if (targetSdkVersion < Build.VERSION_CODES.M) && // legacy API unsupported >= M
1270 * targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP)) // requestNetwork not present < L
1271 *
Robert Greenwalt06314e42014-10-29 14:04:06 -07001272 * TODO - This should be removed when the legacy APIs are removed.
1273 */
Ye Wenb87875e2014-07-21 14:19:01 -07001274 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1275 if (netCap == null) {
1276 return TYPE_NONE;
1277 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001278
Ye Wenb87875e2014-07-21 14:19:01 -07001279 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1280 return TYPE_NONE;
1281 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001282
Lifu Tang30f95a72016-01-07 23:20:38 -08001283 // Do this only for SUPL, until GnssLocationProvider is fixed. http://b/25876485 .
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001284 if (!netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1285 // NOTE: if this causes app breakage, we should not just comment out this early return;
1286 // instead, we should make this early return conditional on the requesting app's target
1287 // SDK version, as described in the comment above.
1288 return TYPE_NONE;
1289 }
1290
Robert Greenwalt06314e42014-10-29 14:04:06 -07001291 String type = null;
1292 int result = TYPE_NONE;
1293
Ye Wenb87875e2014-07-21 14:19:01 -07001294 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
Robert Greenwalt06314e42014-10-29 14:04:06 -07001295 type = "enableCBS";
1296 result = TYPE_MOBILE_CBS;
1297 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1298 type = "enableIMS";
1299 result = TYPE_MOBILE_IMS;
1300 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1301 type = "enableFOTA";
1302 result = TYPE_MOBILE_FOTA;
1303 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1304 type = "enableDUN";
1305 result = TYPE_MOBILE_DUN;
1306 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001307 type = "enableSUPL";
Robert Greenwalt06314e42014-10-29 14:04:06 -07001308 result = TYPE_MOBILE_SUPL;
Robert Greenwalt74ab4fa2015-08-28 12:37:54 -07001309 // back out this hack for mms as they no longer need this and it's causing
1310 // device slowdowns - b/23350688 (note, supl still needs this)
1311 //} else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1312 // type = "enableMMS";
1313 // result = TYPE_MOBILE_MMS;
Robert Greenwalt06314e42014-10-29 14:04:06 -07001314 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1315 type = "enableHIPRI";
1316 result = TYPE_MOBILE_HIPRI;
Ye Wenb87875e2014-07-21 14:19:01 -07001317 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001318 if (type != null) {
1319 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1320 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1321 return result;
Ye Wenb87875e2014-07-21 14:19:01 -07001322 }
1323 }
1324 return TYPE_NONE;
1325 }
1326
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001327 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001328 if (netCap == null) return TYPE_NONE;
1329 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1330 return TYPE_MOBILE_CBS;
1331 }
1332 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1333 return TYPE_MOBILE_IMS;
1334 }
1335 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1336 return TYPE_MOBILE_FOTA;
1337 }
1338 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1339 return TYPE_MOBILE_DUN;
1340 }
1341 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1342 return TYPE_MOBILE_SUPL;
1343 }
1344 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1345 return TYPE_MOBILE_MMS;
1346 }
1347 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1348 return TYPE_MOBILE_HIPRI;
1349 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001350 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1351 return TYPE_WIFI_P2P;
1352 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001353 return TYPE_NONE;
1354 }
1355
1356 private static class LegacyRequest {
1357 NetworkCapabilities networkCapabilities;
1358 NetworkRequest networkRequest;
1359 int expireSequenceNumber;
1360 Network currentNetwork;
1361 int delay = -1;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001362
1363 private void clearDnsBinding() {
1364 if (currentNetwork != null) {
1365 currentNetwork = null;
1366 setProcessDefaultNetworkForHostResolution(null);
1367 }
1368 }
1369
Robert Greenwalt6078b502014-06-11 16:05:07 -07001370 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001371 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001372 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001373 currentNetwork = network;
1374 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001375 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001376 }
1377 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001378 public void onLost(Network network) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001379 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001380 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1381 }
1382 };
1383 }
1384
Robert Greenwaltfab501672014-07-23 11:44:01 -07001385 private static HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
Robert Greenwalt562cc542014-05-15 18:07:26 -07001386 new HashMap<NetworkCapabilities, LegacyRequest>();
1387
1388 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1389 synchronized (sLegacyRequests) {
1390 LegacyRequest l = sLegacyRequests.get(netCap);
1391 if (l != null) return l.networkRequest;
1392 }
1393 return null;
1394 }
1395
1396 private void renewRequestLocked(LegacyRequest l) {
1397 l.expireSequenceNumber++;
1398 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1399 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1400 }
1401
1402 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1403 int ourSeqNum = -1;
1404 synchronized (sLegacyRequests) {
1405 LegacyRequest l = sLegacyRequests.get(netCap);
1406 if (l == null) return;
1407 ourSeqNum = l.expireSequenceNumber;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001408 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001409 }
1410 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1411 }
1412
1413 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1414 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001415 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001416 try {
1417 delay = mService.getRestoreDefaultNetworkDelay(type);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001418 } catch (RemoteException e) {
1419 throw e.rethrowFromSystemServer();
1420 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001421 LegacyRequest l = new LegacyRequest();
1422 l.networkCapabilities = netCap;
1423 l.delay = delay;
1424 l.expireSequenceNumber = 0;
Robert Greenwalt6078b502014-06-11 16:05:07 -07001425 l.networkRequest = sendRequestForNetwork(netCap, l.networkCallback, 0,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001426 REQUEST, type);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001427 if (l.networkRequest == null) return null;
1428 sLegacyRequests.put(netCap, l);
1429 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1430 return l.networkRequest;
1431 }
1432
1433 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1434 if (delay >= 0) {
1435 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
1436 Message msg = sCallbackHandler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1437 sCallbackHandler.sendMessageDelayed(msg, delay);
1438 }
1439 }
1440
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001441 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1442 final LegacyRequest l;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001443 synchronized (sLegacyRequests) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001444 l = sLegacyRequests.remove(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001445 }
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001446 if (l == null) return false;
1447 unregisterNetworkCallback(l.networkCallback);
1448 l.clearDnsBinding();
1449 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001450 }
1451
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001452 /** @hide */
1453 public static class PacketKeepaliveCallback {
1454 /** The requested keepalive was successfully started. */
1455 public void onStarted() {}
1456 /** The keepalive was successfully stopped. */
1457 public void onStopped() {}
1458 /** An error occurred. */
1459 public void onError(int error) {}
1460 }
1461
1462 /**
1463 * Allows applications to request that the system periodically send specific packets on their
1464 * behalf, using hardware offload to save battery power.
1465 *
1466 * To request that the system send keepalives, call one of the methods that return a
1467 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1468 * passing in a non-null callback. If the callback is successfully started, the callback's
1469 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1470 * specifying one of the {@code ERROR_*} constants in this class.
1471 *
1472 * To stop an existing keepalive, call {@link stop}. The system will call {@code onStopped} if
1473 * the operation was successfull or {@code onError} if an error occurred.
1474 *
1475 * @hide
1476 */
1477 public class PacketKeepalive {
1478
1479 private static final String TAG = "PacketKeepalive";
1480
1481 /** @hide */
1482 public static final int SUCCESS = 0;
1483
1484 /** @hide */
1485 public static final int NO_KEEPALIVE = -1;
1486
1487 /** @hide */
1488 public static final int BINDER_DIED = -10;
1489
1490 /** The specified {@code Network} is not connected. */
1491 public static final int ERROR_INVALID_NETWORK = -20;
1492 /** The specified IP addresses are invalid. For example, the specified source IP address is
1493 * not configured on the specified {@code Network}. */
1494 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1495 /** The requested port is invalid. */
1496 public static final int ERROR_INVALID_PORT = -22;
1497 /** The packet length is invalid (e.g., too long). */
1498 public static final int ERROR_INVALID_LENGTH = -23;
1499 /** The packet transmission interval is invalid (e.g., too short). */
1500 public static final int ERROR_INVALID_INTERVAL = -24;
1501
1502 /** The hardware does not support this request. */
1503 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti9d1284e2015-09-08 16:46:36 +09001504 /** The hardware returned an error. */
1505 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001506
1507 public static final int NATT_PORT = 4500;
1508
1509 private final Network mNetwork;
1510 private final PacketKeepaliveCallback mCallback;
1511 private final Looper mLooper;
1512 private final Messenger mMessenger;
1513
1514 private volatile Integer mSlot;
1515
1516 void stopLooper() {
1517 mLooper.quit();
1518 }
1519
1520 public void stop() {
1521 try {
1522 mService.stopKeepalive(mNetwork, mSlot);
1523 } catch (RemoteException e) {
1524 Log.e(TAG, "Error stopping packet keepalive: ", e);
1525 stopLooper();
1526 }
1527 }
1528
1529 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
1530 checkNotNull(network, "network cannot be null");
1531 checkNotNull(callback, "callback cannot be null");
1532 mNetwork = network;
1533 mCallback = callback;
1534 HandlerThread thread = new HandlerThread(TAG);
1535 thread.start();
1536 mLooper = thread.getLooper();
1537 mMessenger = new Messenger(new Handler(mLooper) {
1538 @Override
1539 public void handleMessage(Message message) {
1540 switch (message.what) {
1541 case NetworkAgent.EVENT_PACKET_KEEPALIVE:
1542 int error = message.arg2;
1543 try {
1544 if (error == SUCCESS) {
1545 if (mSlot == null) {
1546 mSlot = message.arg1;
1547 mCallback.onStarted();
1548 } else {
1549 mSlot = null;
1550 stopLooper();
1551 mCallback.onStopped();
1552 }
1553 } else {
1554 stopLooper();
1555 mCallback.onError(error);
1556 }
1557 } catch (Exception e) {
1558 Log.e(TAG, "Exception in keepalive callback(" + error + ")", e);
1559 }
1560 break;
1561 default:
1562 Log.e(TAG, "Unhandled message " + Integer.toHexString(message.what));
1563 break;
1564 }
1565 }
1566 });
1567 }
1568 }
1569
1570 /**
1571 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
1572 *
1573 * @hide
1574 */
1575 public PacketKeepalive startNattKeepalive(
1576 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
1577 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
1578 final PacketKeepalive k = new PacketKeepalive(network, callback);
1579 try {
1580 mService.startNattKeepalive(network, intervalSeconds, k.mMessenger, new Binder(),
1581 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
1582 } catch (RemoteException e) {
1583 Log.e(TAG, "Error starting packet keepalive: ", e);
1584 k.stopLooper();
1585 return null;
1586 }
1587 return k;
1588 }
1589
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001590 /**
1591 * Ensure that a network route exists to deliver traffic to the specified
1592 * host via the specified network interface. An attempt to add a route that
1593 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001594 *
1595 * <p>This method requires the caller to hold either the
1596 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1597 * or the ability to modify system settings as determined by
1598 * {@link android.provider.Settings.System#canWrite}.</p>
1599 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001600 * @param networkType the type of the network over which traffic to the specified
1601 * host is to be routed
1602 * @param hostAddress the IP address of the host to which the route is desired
1603 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001604 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001605 * @deprecated Deprecated in favor of the
1606 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
1607 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001608 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001609 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001610 */
1611 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001612 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001613 }
1614
1615 /**
1616 * Ensure that a network route exists to deliver traffic to the specified
1617 * host via the specified network interface. An attempt to add a route that
1618 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001619 *
1620 * <p>This method requires the caller to hold either the
1621 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1622 * or the ability to modify system settings as determined by
1623 * {@link android.provider.Settings.System#canWrite}.</p>
1624 *
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001625 * @param networkType the type of the network over which traffic to the specified
1626 * host is to be routed
1627 * @param hostAddress the IP address of the host to which the route is desired
1628 * @return {@code true} on success, {@code false} on failure
1629 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001630 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001631 * {@link #bindProcessToNetwork} API.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001632 */
1633 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001634 checkLegacyRoutingApiAccess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001635 try {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001636 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001637 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001638 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 }
1640 }
1641
1642 /**
1643 * Returns the value of the setting for background data usage. If false,
1644 * applications should not use the network if the application is not in the
1645 * foreground. Developers should respect this setting, and check the value
1646 * of this before performing any background data operations.
1647 * <p>
1648 * All applications that have background services that use the network
1649 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001650 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001651 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001652 * background data depends on several combined factors, and this method will
1653 * always return {@code true}. Instead, when background data is unavailable,
1654 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001655 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001656 * @return Whether background data usage is allowed.
1657 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001658 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001659 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001660 // assume that background data is allowed; final authority is
1661 // NetworkInfo which may be blocked.
1662 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001663 }
1664
1665 /**
1666 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001667 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 * @param allowBackgroundData Whether an application should use data while
1669 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001670 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001671 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1672 * @see #getBackgroundDataSetting()
1673 * @hide
1674 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001675 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001677 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001678 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001679
1680 /**
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001681 * Return quota status for the current active network, or {@code null} if no
1682 * network is active. Quota status can change rapidly, so these values
1683 * shouldn't be cached.
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001684 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001685 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001686 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1687 *
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001688 * @hide
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001689 */
1690 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1691 try {
1692 return mService.getActiveNetworkQuotaInfo();
1693 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001694 throw e.rethrowFromSystemServer();
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001695 }
1696 }
1697
1698 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001699 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001700 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001701 */
1702 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001703 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1704 if (b != null) {
1705 try {
1706 ITelephony it = ITelephony.Stub.asInterface(b);
Shishir Agrawal7ea3e8b2016-01-25 13:03:07 -08001707 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Wink Saville36ffb042014-12-05 11:10:30 -08001708 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
1709 boolean retVal = it.getDataEnabled(subId);
1710 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1711 + " retVal=" + retVal);
1712 return retVal;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001713 } catch (RemoteException e) {
1714 throw e.rethrowFromSystemServer();
1715 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001716 }
Wink Saville36ffb042014-12-05 11:10:30 -08001717 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001718 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001719 }
1720
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001721 /**
Robert Greenwaltb2489872014-09-04 16:44:35 -07001722 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt6078b502014-06-11 16:05:07 -07001723 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001724 */
1725 public interface OnNetworkActiveListener {
1726 /**
1727 * Called on the main thread of the process to report that the current data network
1728 * has become active, and it is now a good time to perform any pending network
1729 * operations. Note that this listener only tells you when the network becomes
1730 * active; if at any other time you want to know whether it is active (and thus okay
1731 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001732 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001733 */
1734 public void onNetworkActive();
1735 }
1736
1737 private INetworkManagementService getNetworkManagementService() {
1738 synchronized (this) {
1739 if (mNMService != null) {
1740 return mNMService;
1741 }
1742 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1743 mNMService = INetworkManagementService.Stub.asInterface(b);
1744 return mNMService;
1745 }
1746 }
1747
1748 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
1749 mNetworkActivityListeners
1750 = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>();
1751
1752 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001753 * Start listening to reports when the system's default data network is active, meaning it is
1754 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1755 * to determine the current state of the system's default network after registering the
1756 * listener.
1757 * <p>
1758 * If the process default network has been set with
Paul Jensen72db88e2015-03-10 10:54:12 -04001759 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt6078b502014-06-11 16:05:07 -07001760 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001761 *
1762 * @param l The listener to be told when the network is active.
1763 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001764 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001765 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1766 @Override
1767 public void onNetworkActive() throws RemoteException {
1768 l.onNetworkActive();
1769 }
1770 };
1771
1772 try {
1773 getNetworkManagementService().registerNetworkActivityListener(rl);
1774 mNetworkActivityListeners.put(l, rl);
1775 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001776 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001777 }
1778 }
1779
1780 /**
1781 * Remove network active listener previously registered with
Robert Greenwaltb2489872014-09-04 16:44:35 -07001782 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001783 *
1784 * @param l Previously registered listener.
1785 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001786 public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001787 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
1788 if (rl == null) {
1789 throw new IllegalArgumentException("Listener not registered: " + l);
1790 }
1791 try {
1792 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1793 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001794 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001795 }
1796 }
1797
1798 /**
1799 * Return whether the data network is currently active. An active network means that
1800 * it is currently in a high power state for performing data transmission. On some
1801 * types of networks, it may be expensive to move and stay in such a state, so it is
1802 * more power efficient to batch network traffic together when the radio is already in
1803 * this state. This method tells you whether right now is currently a good time to
1804 * initiate network traffic, as the network is already active.
1805 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001806 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001807 try {
1808 return getNetworkManagementService().isNetworkActive();
1809 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001810 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001811 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001812 }
1813
1814 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001815 * {@hide}
1816 */
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001817 public ConnectivityManager(Context context, IConnectivityManager service) {
1818 mContext = checkNotNull(context, "missing context");
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001819 mService = checkNotNull(service, "missing IConnectivityManager");
Paul Jensene0bef712014-12-10 15:12:18 -05001820 sInstance = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001822
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07001823 /** {@hide} */
1824 public static ConnectivityManager from(Context context) {
1825 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1826 }
1827
Lorenzo Colittid5427052015-10-15 16:29:00 +09001828 /** {@hide} */
1829 public static final void enforceChangePermission(Context context) {
1830 int uid = Binder.getCallingUid();
1831 Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
1832 .getPackageNameForUid(context, uid), true /* throwException */);
1833 }
1834
Robert Greenwaltedb47662014-09-16 17:54:19 -07001835 /** {@hide */
1836 public static final void enforceTetherChangePermission(Context context) {
1837 if (context.getResources().getStringArray(
1838 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
1839 // Have a provisioning app - must only let system apps (which check this app)
1840 // turn on tethering
1841 context.enforceCallingOrSelfPermission(
Jeremy Kleind42209d2015-12-28 15:11:58 -08001842 android.Manifest.permission.TETHER_PRIVILEGED, "ConnectivityService");
Robert Greenwaltedb47662014-09-16 17:54:19 -07001843 } else {
Billy Laua7238a32015-08-01 12:45:02 +01001844 int uid = Binder.getCallingUid();
Lorenzo Colittid5427052015-10-15 16:29:00 +09001845 Settings.checkAndNoteWriteSettingsOperation(context, uid, Settings
1846 .getPackageNameForUid(context, uid), true /* throwException */);
Robert Greenwaltedb47662014-09-16 17:54:19 -07001847 }
1848 }
1849
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001850 /**
Paul Jensene0bef712014-12-10 15:12:18 -05001851 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1852 * situations where a Context pointer is unavailable.
1853 * @hide
1854 */
Paul Jensen72db88e2015-03-10 10:54:12 -04001855 static ConnectivityManager getInstanceOrNull() {
1856 return sInstance;
1857 }
1858
1859 /**
1860 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1861 * situations where a Context pointer is unavailable.
1862 * @hide
1863 */
1864 private static ConnectivityManager getInstance() {
1865 if (getInstanceOrNull() == null) {
Paul Jensene0bef712014-12-10 15:12:18 -05001866 throw new IllegalStateException("No ConnectivityManager yet constructed");
1867 }
Paul Jensen72db88e2015-03-10 10:54:12 -04001868 return getInstanceOrNull();
Paul Jensene0bef712014-12-10 15:12:18 -05001869 }
1870
1871 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001872 * Get the set of tetherable, available interfaces. This list is limited by
1873 * device configuration and current interface existence.
Paul Jensenb2748922015-05-06 11:10:18 -04001874 * <p>This method requires the caller to hold the permission
1875 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001876 *
1877 * @return an array of 0 or more Strings of tetherable interface names.
1878 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001879 * {@hide}
1880 */
1881 public String[] getTetherableIfaces() {
1882 try {
1883 return mService.getTetherableIfaces();
1884 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001885 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001886 }
1887 }
1888
1889 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001890 * Get the set of tethered interfaces.
Paul Jensenb2748922015-05-06 11:10:18 -04001891 * <p>This method requires the caller to hold the permission
1892 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001893 *
1894 * @return an array of 0 or more String of currently tethered interface names.
1895 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001896 * {@hide}
1897 */
1898 public String[] getTetheredIfaces() {
1899 try {
1900 return mService.getTetheredIfaces();
1901 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001902 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001903 }
1904 }
1905
1906 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001907 * Get the set of interface names which attempted to tether but
1908 * failed. Re-attempting to tether may cause them to reset to the Tethered
1909 * state. Alternatively, causing the interface to be destroyed and recreated
1910 * may cause them to reset to the available state.
1911 * {@link ConnectivityManager#getLastTetherError} can be used to get more
1912 * information on the cause of the errors.
Paul Jensenb2748922015-05-06 11:10:18 -04001913 * <p>This method requires the caller to hold the permission
1914 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001915 *
1916 * @return an array of 0 or more String indicating the interface names
1917 * which failed to tether.
1918 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001919 * {@hide}
1920 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001921 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001922 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001923 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001924 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001925 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001926 }
1927 }
1928
1929 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001930 * Get the set of tethered dhcp ranges.
1931 *
1932 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
1933 * {@hide}
1934 */
1935 public String[] getTetheredDhcpRanges() {
1936 try {
1937 return mService.getTetheredDhcpRanges();
1938 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001939 throw e.rethrowFromSystemServer();
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001940 }
1941 }
1942
1943 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001944 * Attempt to tether the named interface. This will setup a dhcp server
1945 * on the interface, forward and NAT IP packets and forward DNS requests
1946 * to the best active upstream network interface. Note that if no upstream
1947 * IP network interface is available, dhcp will still run and traffic will be
1948 * allowed between the tethered devices and this device, though upstream net
1949 * access will of course fail until an upstream network interface becomes
1950 * active.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001951 *
1952 * <p>This method requires the caller to hold either the
1953 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1954 * or the ability to modify system settings as determined by
1955 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001956 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08001957 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
1958 * and WifiStateMachine which need direct access. All other clients should use
1959 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
1960 * logic.</p>
1961 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001962 * @param iface the interface name to tether.
1963 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1964 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001965 * {@hide}
1966 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001967 public int tether(String iface) {
1968 try {
1969 return mService.tether(iface);
1970 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001971 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001972 }
1973 }
1974
1975 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001976 * Stop tethering the named interface.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001977 *
1978 * <p>This method requires the caller to hold either the
1979 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1980 * or the ability to modify system settings as determined by
1981 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001982 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08001983 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
1984 * and WifiStateMachine which need direct access. All other clients should use
1985 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
1986 * logic.</p>
1987 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001988 * @param iface the interface name to untether.
1989 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1990 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08001991 * {@hide}
1992 */
1993 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001994 try {
1995 return mService.untether(iface);
1996 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001997 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001998 }
1999 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002000
2001 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002002 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002003 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002004 * due to device configuration.
Paul Jensenb2748922015-05-06 11:10:18 -04002005 * <p>This method requires the caller to hold the permission
2006 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002007 *
2008 * @return a boolean - {@code true} indicating Tethering is supported.
2009 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002010 * {@hide}
2011 */
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002012 @SystemApi
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002013 public boolean isTetheringSupported() {
2014 try {
2015 return mService.isTetheringSupported();
2016 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002017 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002018 }
2019 }
2020
2021 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002022 * Callback for use with {@link #startTethering} to find out whether tethering succeeded.
2023 * @hide
2024 */
2025 @SystemApi
2026 public static abstract class OnStartTetheringCallback {
2027 /**
2028 * Called when tethering has been successfully started.
2029 */
2030 public void onTetheringStarted() {};
2031
2032 /**
2033 * Called when starting tethering failed.
2034 */
2035 public void onTetheringFailed() {};
2036 }
2037
2038 /**
2039 * Convenient overload for
2040 * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null
2041 * handler to run on the current thread's {@link Looper}.
2042 * @hide
2043 */
2044 @SystemApi
2045 public void startTethering(int type, boolean showProvisioningUi,
2046 final OnStartTetheringCallback callback) {
2047 startTethering(type, showProvisioningUi, callback, null);
2048 }
2049
2050 /**
2051 * Runs tether provisioning for the given type if needed and then starts tethering if
2052 * the check succeeds. If no carrier provisioning is required for tethering, tethering is
2053 * enabled immediately. If provisioning fails, tethering will not be enabled. It also
2054 * schedules tether provisioning re-checks if appropriate.
2055 *
2056 * @param type The type of tethering to start. Must be one of
2057 * {@link ConnectivityManager.TETHERING_WIFI},
2058 * {@link ConnectivityManager.TETHERING_USB}, or
2059 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2060 * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there
2061 * is one. This should be true the first time this function is called and also any time
2062 * the user can see this UI. It gives users information from their carrier about the
2063 * check failing and how they can sign up for tethering if possible.
2064 * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller
2065 * of the result of trying to tether.
2066 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
2067 * @hide
2068 */
2069 @SystemApi
2070 public void startTethering(int type, boolean showProvisioningUi,
2071 final OnStartTetheringCallback callback, Handler handler) {
2072 ResultReceiver wrappedCallback = new ResultReceiver(handler) {
2073 @Override
2074 protected void onReceiveResult(int resultCode, Bundle resultData) {
2075 if (resultCode == TETHER_ERROR_NO_ERROR) {
2076 callback.onTetheringStarted();
2077 } else {
2078 callback.onTetheringFailed();
2079 }
2080 }
2081 };
2082 try {
2083 mService.startTethering(type, wrappedCallback, showProvisioningUi);
2084 } catch (RemoteException e) {
2085 Log.e(TAG, "Exception trying to start tethering.", e);
2086 wrappedCallback.send(TETHER_ERROR_SERVICE_UNAVAIL, null);
2087 }
2088 }
2089
2090 /**
2091 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if
2092 * applicable.
2093 *
2094 * @param type The type of tethering to stop. Must be one of
2095 * {@link ConnectivityManager.TETHERING_WIFI},
2096 * {@link ConnectivityManager.TETHERING_USB}, or
2097 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2098 * @hide
2099 */
2100 @SystemApi
2101 public void stopTethering(int type) {
2102 try {
2103 mService.stopTethering(type);
2104 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002105 throw e.rethrowFromSystemServer();
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002106 }
2107 }
2108
2109 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002110 * Get the list of regular expressions that define any tetherable
2111 * USB network interfaces. If USB tethering is not supported by the
2112 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04002113 * <p>This method requires the caller to hold the permission
2114 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002115 *
2116 * @return an array of 0 or more regular expression Strings defining
2117 * what interfaces are considered tetherable usb interfaces.
2118 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002119 * {@hide}
2120 */
2121 public String[] getTetherableUsbRegexs() {
2122 try {
2123 return mService.getTetherableUsbRegexs();
2124 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002125 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002126 }
2127 }
2128
2129 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002130 * Get the list of regular expressions that define any tetherable
2131 * Wifi network interfaces. If Wifi tethering is not supported by the
2132 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04002133 * <p>This method requires the caller to hold the permission
2134 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002135 *
2136 * @return an array of 0 or more regular expression Strings defining
2137 * what interfaces are considered tetherable wifi interfaces.
2138 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002139 * {@hide}
2140 */
2141 public String[] getTetherableWifiRegexs() {
2142 try {
2143 return mService.getTetherableWifiRegexs();
2144 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002145 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002146 }
2147 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08002148
Danica Chang6fdd0c62010-08-11 14:54:43 -07002149 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002150 * Get the list of regular expressions that define any tetherable
2151 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
2152 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04002153 * <p>This method requires the caller to hold the permission
2154 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002155 *
2156 * @return an array of 0 or more regular expression Strings defining
2157 * what interfaces are considered tetherable bluetooth interfaces.
2158 *
Danica Chang6fdd0c62010-08-11 14:54:43 -07002159 * {@hide}
2160 */
2161 public String[] getTetherableBluetoothRegexs() {
2162 try {
2163 return mService.getTetherableBluetoothRegexs();
2164 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002165 throw e.rethrowFromSystemServer();
Danica Chang6fdd0c62010-08-11 14:54:43 -07002166 }
2167 }
2168
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002169 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002170 * Attempt to both alter the mode of USB and Tethering of USB. A
2171 * utility method to deal with some of the complexity of USB - will
2172 * attempt to switch to Rndis and subsequently tether the resulting
2173 * interface on {@code true} or turn off tethering and switch off
2174 * Rndis on {@code false}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002175 *
2176 * <p>This method requires the caller to hold either the
2177 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2178 * or the ability to modify system settings as determined by
2179 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002180 *
2181 * @param enable a boolean - {@code true} to enable tethering
2182 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2183 *
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002184 * {@hide}
2185 */
2186 public int setUsbTethering(boolean enable) {
2187 try {
2188 return mService.setUsbTethering(enable);
2189 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002190 throw e.rethrowFromSystemServer();
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002191 }
2192 }
2193
Robert Greenwalt5a735062010-03-02 17:25:02 -08002194 /** {@hide} */
2195 public static final int TETHER_ERROR_NO_ERROR = 0;
2196 /** {@hide} */
2197 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
2198 /** {@hide} */
2199 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
2200 /** {@hide} */
2201 public static final int TETHER_ERROR_UNSUPPORTED = 3;
2202 /** {@hide} */
2203 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
2204 /** {@hide} */
2205 public static final int TETHER_ERROR_MASTER_ERROR = 5;
2206 /** {@hide} */
2207 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
2208 /** {@hide} */
2209 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
2210 /** {@hide} */
2211 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
2212 /** {@hide} */
2213 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
2214 /** {@hide} */
2215 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002216 /** {@hide} */
2217 public static final int TETHER_ERROR_PROVISION_FAILED = 11;
Robert Greenwalt5a735062010-03-02 17:25:02 -08002218
2219 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002220 * Get a more detailed error code after a Tethering or Untethering
2221 * request asynchronously failed.
Paul Jensenb2748922015-05-06 11:10:18 -04002222 * <p>This method requires the caller to hold the permission
2223 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002224 *
2225 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08002226 * @return error The error code of the last error tethering or untethering the named
2227 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002228 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002229 * {@hide}
2230 */
2231 public int getLastTetherError(String iface) {
2232 try {
2233 return mService.getLastTetherError(iface);
2234 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002235 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002236 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002237 }
2238
2239 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002240 * Report network connectivity status. This is currently used only
2241 * to alter status bar UI.
Paul Jensenb2748922015-05-06 11:10:18 -04002242 * <p>This method requires the caller to hold the permission
2243 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002244 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002245 * @param networkType The type of network you want to report on
2246 * @param percentage The quality of the connection 0 is bad, 100 is good
2247 * {@hide}
2248 */
2249 public void reportInetCondition(int networkType, int percentage) {
2250 try {
2251 mService.reportInetCondition(networkType, percentage);
2252 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002253 throw e.rethrowFromSystemServer();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002254 }
2255 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002256
2257 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002258 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07002259 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002260 * the framework to re-evaluate network connectivity and/or switch to another
2261 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002262 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002263 * @param network The {@link Network} the application was attempting to use
2264 * or {@code null} to indicate the current default network.
Paul Jensenbfd17b72015-04-07 12:43:13 -04002265 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
2266 * working and non-working connectivity.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002267 */
2268 public void reportBadNetwork(Network network) {
2269 try {
Paul Jensenbfd17b72015-04-07 12:43:13 -04002270 // One of these will be ignored because it matches system's current state.
2271 // The other will trigger the necessary reevaluation.
2272 mService.reportNetworkConnectivity(network, true);
2273 mService.reportNetworkConnectivity(network, false);
2274 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002275 throw e.rethrowFromSystemServer();
Paul Jensenbfd17b72015-04-07 12:43:13 -04002276 }
2277 }
2278
2279 /**
2280 * Report to the framework whether a network has working connectivity.
2281 * This provides a hint to the system that a particular network is providing
2282 * working connectivity or not. In response the framework may re-evaluate
2283 * the network's connectivity and might take further action thereafter.
2284 *
2285 * @param network The {@link Network} the application was attempting to use
2286 * or {@code null} to indicate the current default network.
2287 * @param hasConnectivity {@code true} if the application was able to successfully access the
2288 * Internet using {@code network} or {@code false} if not.
2289 */
2290 public void reportNetworkConnectivity(Network network, boolean hasConnectivity) {
2291 try {
2292 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002293 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002294 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002295 }
2296 }
2297
2298 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002299 * Set a network-independent global http proxy. This is not normally what you want
2300 * for typical HTTP proxies - they are general network dependent. However if you're
2301 * doing something unusual like general internal filtering this may be useful. On
2302 * a private network where the proxy is not accessible, you may break HTTP using this.
Paul Jensen0d719ca2015-02-13 14:18:39 -05002303 * <p>This method requires the caller to hold the permission
Jason Monkfaf3fd52014-05-07 18:41:13 -04002304 * android.Manifest.permission#CONNECTIVITY_INTERNAL.
Paul Jensenb2748922015-05-06 11:10:18 -04002305 *
2306 * @param p A {@link ProxyInfo} object defining the new global
2307 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002308 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002309 */
Jason Monk207900c2014-04-25 15:00:09 -04002310 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002311 try {
2312 mService.setGlobalProxy(p);
2313 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002314 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002315 }
2316 }
2317
2318 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002319 * Retrieve any network-independent global HTTP proxy.
2320 *
Jason Monk207900c2014-04-25 15:00:09 -04002321 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002322 * if no global HTTP proxy is set.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002323 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002324 */
Jason Monk207900c2014-04-25 15:00:09 -04002325 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002326 try {
2327 return mService.getGlobalProxy();
2328 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002329 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002330 }
2331 }
2332
2333 /**
Paul Jensencee9b512015-05-06 07:32:40 -04002334 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
2335 * network-specific HTTP proxy. If {@code network} is null, the
2336 * network-specific proxy returned is the proxy of the default active
2337 * network.
2338 *
2339 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
2340 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
2341 * or when {@code network} is {@code null},
2342 * the {@code ProxyInfo} for the default active network. Returns
2343 * {@code null} when no proxy applies or the caller doesn't have
2344 * permission to use {@code network}.
2345 * @hide
2346 */
2347 public ProxyInfo getProxyForNetwork(Network network) {
2348 try {
2349 return mService.getProxyForNetwork(network);
2350 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002351 throw e.rethrowFromSystemServer();
Paul Jensencee9b512015-05-06 07:32:40 -04002352 }
2353 }
2354
2355 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002356 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
2357 * otherwise if this process is bound to a {@link Network} using
Paul Jensen72db88e2015-03-10 10:54:12 -04002358 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensene0bef712014-12-10 15:12:18 -05002359 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002360 *
Jason Monk207900c2014-04-25 15:00:09 -04002361 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002362 * HTTP proxy is active.
Robert Greenwalt434203a2010-10-11 16:00:27 -07002363 */
Paul Jensene0bef712014-12-10 15:12:18 -05002364 public ProxyInfo getDefaultProxy() {
Paul Jensencee9b512015-05-06 07:32:40 -04002365 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwalt434203a2010-10-11 16:00:27 -07002366 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07002367
2368 /**
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002369 * Returns true if the hardware supports the given network type
2370 * else it returns false. This doesn't indicate we have coverage
2371 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002372 * hardware supports it. For example a GSM phone without a SIM
2373 * should still return {@code true} for mobile data, but a wifi only
2374 * tablet would return {@code false}.
Paul Jensenb2748922015-05-06 11:10:18 -04002375 * <p>This method requires the caller to hold the permission
2376 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002377 *
2378 * @param networkType The network type we'd like to check
2379 * @return {@code true} if supported, else {@code false}
2380 *
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002381 * @hide
2382 */
2383 public boolean isNetworkSupported(int networkType) {
2384 try {
2385 return mService.isNetworkSupported(networkType);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002386 } catch (RemoteException e) {
2387 throw e.rethrowFromSystemServer();
2388 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002389 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002390
2391 /**
2392 * Returns if the currently active data network is metered. A network is
2393 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002394 * that connection due to monetary costs, data limitations or
2395 * battery/performance issues. You should check this before doing large
2396 * data transfers, and warn the user or delay the operation until another
2397 * network is available.
Paul Jensenb2748922015-05-06 11:10:18 -04002398 * <p>This method requires the caller to hold the permission
2399 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002400 *
2401 * @return {@code true} if large transfers should be avoided, otherwise
2402 * {@code false}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002403 */
2404 public boolean isActiveNetworkMetered() {
2405 try {
2406 return mService.isActiveNetworkMetered();
2407 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002408 throw e.rethrowFromSystemServer();
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002409 }
2410 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002411
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002412 /**
2413 * If the LockdownVpn mechanism is enabled, updates the vpn
2414 * with a reload of its profile.
2415 *
2416 * @return a boolean with {@code} indicating success
2417 *
2418 * <p>This method can only be called by the system UID
2419 * {@hide}
2420 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002421 public boolean updateLockdownVpn() {
2422 try {
2423 return mService.updateLockdownVpn();
2424 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002425 throw e.rethrowFromSystemServer();
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002426 }
2427 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07002428
2429 /**
Wink Saville948282b2013-08-29 08:55:16 -07002430 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07002431 *
Wink Savilleab9321d2013-06-29 21:10:57 -07002432 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07002433 *
2434 * @return time out that will be used, maybe less that suggestedTimeOutMs
2435 * -1 if an error.
2436 *
2437 * {@hide}
2438 */
Wink Saville948282b2013-08-29 08:55:16 -07002439 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07002440 int timeOutMs = -1;
2441 try {
Wink Saville948282b2013-08-29 08:55:16 -07002442 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07002443 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002444 throw e.rethrowFromSystemServer();
Wink Savilleab9321d2013-06-29 21:10:57 -07002445 }
2446 return timeOutMs;
2447 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002448
2449 /**
Wink Saville42d4f082013-07-20 20:31:59 -07002450 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002451 * {@hide}
2452 */
2453 public String getMobileProvisioningUrl() {
2454 try {
2455 return mService.getMobileProvisioningUrl();
2456 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002457 throw e.rethrowFromSystemServer();
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002458 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002459 }
Wink Saville42d4f082013-07-20 20:31:59 -07002460
2461 /**
Wink Saville948282b2013-08-29 08:55:16 -07002462 * Set sign in error notification to visible or in visible
2463 *
2464 * @param visible
2465 * @param networkType
2466 *
2467 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04002468 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville948282b2013-08-29 08:55:16 -07002469 */
2470 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04002471 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07002472 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04002473 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07002474 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002475 throw e.rethrowFromSystemServer();
Wink Saville948282b2013-08-29 08:55:16 -07002476 }
2477 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002478
2479 /**
2480 * Set the value for enabling/disabling airplane mode
Paul Jensenb2748922015-05-06 11:10:18 -04002481 * <p>This method requires the caller to hold the permission
2482 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002483 *
2484 * @param enable whether to enable airplane mode or not
2485 *
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002486 * @hide
2487 */
2488 public void setAirplaneMode(boolean enable) {
2489 try {
2490 mService.setAirplaneMode(enable);
2491 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002492 throw e.rethrowFromSystemServer();
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002493 }
2494 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002495
2496 /** {@hide} */
Robert Greenwalta67be032014-05-16 15:49:14 -07002497 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07002498 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07002499 mService.registerNetworkFactory(messenger, name);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002500 } catch (RemoteException e) {
2501 throw e.rethrowFromSystemServer();
2502 }
Robert Greenwalta67be032014-05-16 15:49:14 -07002503 }
2504
2505 /** {@hide} */
2506 public void unregisterNetworkFactory(Messenger messenger) {
2507 try {
2508 mService.unregisterNetworkFactory(messenger);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002509 } catch (RemoteException e) {
2510 throw e.rethrowFromSystemServer();
2511 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002512 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002513
Paul Jensen31a94f42015-02-13 14:18:39 -05002514 /**
2515 * @hide
2516 * Register a NetworkAgent with ConnectivityService.
2517 * @return NetID corresponding to NetworkAgent.
2518 */
2519 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002520 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002521 try {
Paul Jensen31a94f42015-02-13 14:18:39 -05002522 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
2523 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002524 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -05002525 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002526 }
2527
Robert Greenwalt9258c642014-03-26 16:47:06 -07002528 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002529 * Base class for NetworkRequest callbacks. Used for notifications about network
2530 * changes. Should be extended by applications wanting notifications.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002531 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002532 public static class NetworkCallback {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002533 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002534 * Called when the framework connects to a new network to evaluate whether it satisfies this
2535 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
2536 * callback. There is no guarantee that this new network will satisfy any requests, or that
2537 * the network will stay connected for longer than the time necessary to evaluate it.
2538 * <p>
2539 * Most applications <b>should not</b> act on this callback, and should instead use
2540 * {@link #onAvailable}. This callback is intended for use by applications that can assist
2541 * the framework in properly evaluating the network &mdash; for example, an application that
2542 * can automatically log in to a captive portal without user intervention.
2543 *
2544 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti66276122015-06-11 14:27:17 +09002545 *
2546 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002547 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002548 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002549
2550 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002551 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002552 * This callback may be called more than once if the {@link Network} that is
2553 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002554 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002555 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002556 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002557 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002558
2559 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002560 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002561 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002562 * for graceful handover. This may not be called if we have a hard loss
2563 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002564 * {@link NetworkCallback#onLost} call or a
2565 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002566 * on whether we lose or regain it.
2567 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002568 * @param network The {@link Network} that is about to be disconnected.
2569 * @param maxMsToLive The time in ms the framework will attempt to keep the
2570 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002571 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002572 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002573 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002574
2575 /**
2576 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002577 * graceful failure ends.
2578 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002579 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002580 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002581 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002582
2583 /**
2584 * Called if no network is found in the given timeout time. If no timeout is given,
2585 * this will not be called.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002586 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002587 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002588 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002589
2590 /**
2591 * Called when the network the framework connected to for this request
2592 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002593 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002594 * @param network The {@link Network} whose capabilities have changed.
Lorenzo Colittie285b432015-04-23 15:32:42 +09002595 * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002596 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002597 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002598 NetworkCapabilities networkCapabilities) {}
2599
2600 /**
2601 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002602 * changes {@link LinkProperties}.
2603 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002604 * @param network The {@link Network} whose link properties have changed.
2605 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002606 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002607 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002608
Robert Greenwalt8d482522015-06-24 13:23:42 -07002609 /**
2610 * Called when the network the framework connected to for this request
2611 * goes into {@link NetworkInfo.DetailedState.SUSPENDED}.
2612 * This generally means that while the TCP connections are still live,
2613 * temporarily network data fails to transfer. Specifically this is used
2614 * on cellular networks to mask temporary outages when driving through
2615 * a tunnel, etc.
2616 * @hide
2617 */
2618 public void onNetworkSuspended(Network network) {}
2619
2620 /**
2621 * Called when the network the framework connected to for this request
2622 * returns from a {@link NetworkInfo.DetailedState.SUSPENDED} state.
2623 * This should always be preceeded by a matching {@code onNetworkSuspended}
2624 * call.
2625 * @hide
2626 */
2627 public void onNetworkResumed(Network network) {}
2628
Robert Greenwalt6078b502014-06-11 16:05:07 -07002629 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002630 }
2631
Robert Greenwalt9258c642014-03-26 16:47:06 -07002632 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002633 /** @hide */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002634 public static final int CALLBACK_PRECHECK = BASE + 1;
2635 /** @hide */
2636 public static final int CALLBACK_AVAILABLE = BASE + 2;
2637 /** @hide arg1 = TTL */
2638 public static final int CALLBACK_LOSING = BASE + 3;
2639 /** @hide */
2640 public static final int CALLBACK_LOST = BASE + 4;
2641 /** @hide */
2642 public static final int CALLBACK_UNAVAIL = BASE + 5;
2643 /** @hide */
2644 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2645 /** @hide */
2646 public static final int CALLBACK_IP_CHANGED = BASE + 7;
2647 /** @hide */
2648 public static final int CALLBACK_RELEASED = BASE + 8;
2649 /** @hide */
2650 public static final int CALLBACK_EXIT = BASE + 9;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002651 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002652 private static final int EXPIRE_LEGACY_REQUEST = BASE + 10;
2653 /** @hide */
2654 public static final int CALLBACK_SUSPENDED = BASE + 11;
2655 /** @hide */
2656 public static final int CALLBACK_RESUMED = BASE + 12;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002657
Robert Greenwalt562cc542014-05-15 18:07:26 -07002658 private class CallbackHandler extends Handler {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002659 private final HashMap<NetworkRequest, NetworkCallback>mCallbackMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002660 private final AtomicInteger mRefCount;
2661 private static final String TAG = "ConnectivityManager.CallbackHandler";
2662 private final ConnectivityManager mCm;
Robert Greenwalta9ebeef2015-09-03 16:41:45 -07002663 private static final boolean DBG = false;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002664
Robert Greenwalt6078b502014-06-11 16:05:07 -07002665 CallbackHandler(Looper looper, HashMap<NetworkRequest, NetworkCallback>callbackMap,
Robert Greenwalt9258c642014-03-26 16:47:06 -07002666 AtomicInteger refCount, ConnectivityManager cm) {
2667 super(looper);
2668 mCallbackMap = callbackMap;
2669 mRefCount = refCount;
2670 mCm = cm;
2671 }
2672
2673 @Override
2674 public void handleMessage(Message message) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002675 NetworkRequest request = (NetworkRequest) getObject(message, NetworkRequest.class);
2676 Network network = (Network) getObject(message, Network.class);
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09002677 if (DBG) {
2678 Log.d(TAG, whatToString(message.what) + " for network " + network);
2679 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002680 switch (message.what) {
2681 case CALLBACK_PRECHECK: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002682 NetworkCallback callback = getCallback(request, "PRECHECK");
2683 if (callback != null) {
2684 callback.onPreCheck(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002685 }
2686 break;
2687 }
2688 case CALLBACK_AVAILABLE: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002689 NetworkCallback callback = getCallback(request, "AVAILABLE");
2690 if (callback != null) {
2691 callback.onAvailable(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002692 }
2693 break;
2694 }
2695 case CALLBACK_LOSING: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002696 NetworkCallback callback = getCallback(request, "LOSING");
2697 if (callback != null) {
2698 callback.onLosing(network, message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002699 }
2700 break;
2701 }
2702 case CALLBACK_LOST: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002703 NetworkCallback callback = getCallback(request, "LOST");
2704 if (callback != null) {
2705 callback.onLost(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002706 }
2707 break;
2708 }
2709 case CALLBACK_UNAVAIL: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002710 NetworkCallback callback = getCallback(request, "UNAVAIL");
2711 if (callback != null) {
2712 callback.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002713 }
2714 break;
2715 }
2716 case CALLBACK_CAP_CHANGED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002717 NetworkCallback callback = getCallback(request, "CAP_CHANGED");
2718 if (callback != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002719 NetworkCapabilities cap = (NetworkCapabilities)getObject(message,
2720 NetworkCapabilities.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002721
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002722 callback.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002723 }
2724 break;
2725 }
2726 case CALLBACK_IP_CHANGED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002727 NetworkCallback callback = getCallback(request, "IP_CHANGED");
2728 if (callback != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002729 LinkProperties lp = (LinkProperties)getObject(message,
2730 LinkProperties.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002731
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002732 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002733 }
2734 break;
2735 }
Robert Greenwalt8d482522015-06-24 13:23:42 -07002736 case CALLBACK_SUSPENDED: {
2737 NetworkCallback callback = getCallback(request, "SUSPENDED");
2738 if (callback != null) {
2739 callback.onNetworkSuspended(network);
2740 }
2741 break;
2742 }
2743 case CALLBACK_RESUMED: {
2744 NetworkCallback callback = getCallback(request, "RESUMED");
2745 if (callback != null) {
2746 callback.onNetworkResumed(network);
2747 }
2748 break;
2749 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002750 case CALLBACK_RELEASED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002751 NetworkCallback callback = null;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002752 synchronized(mCallbackMap) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002753 callback = mCallbackMap.remove(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002754 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002755 if (callback != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002756 synchronized(mRefCount) {
2757 if (mRefCount.decrementAndGet() == 0) {
2758 getLooper().quit();
2759 }
2760 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002761 } else {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002762 Log.e(TAG, "callback not found for RELEASED message");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002763 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002764 break;
2765 }
2766 case CALLBACK_EXIT: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002767 Log.d(TAG, "Listener quitting");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002768 getLooper().quit();
2769 break;
2770 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07002771 case EXPIRE_LEGACY_REQUEST: {
2772 expireRequest((NetworkCapabilities)message.obj, message.arg1);
2773 break;
2774 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002775 }
2776 }
2777
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002778 private Object getObject(Message msg, Class c) {
2779 return msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07002780 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002781
2782 private NetworkCallback getCallback(NetworkRequest req, String name) {
2783 NetworkCallback callback;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002784 synchronized(mCallbackMap) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002785 callback = mCallbackMap.get(req);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002786 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002787 if (callback == null) {
2788 Log.e(TAG, "callback not found for " + name + " message");
2789 }
2790 return callback;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002791 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002792 }
2793
Robert Greenwalt6078b502014-06-11 16:05:07 -07002794 private void incCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002795 synchronized(sCallbackRefCount) {
2796 if (sCallbackRefCount.incrementAndGet() == 1) {
Pierre Imaib290c0a2016-02-19 16:26:01 +09002797 // TODO: switch this to ConnectivityThread
Robert Greenwalt9258c642014-03-26 16:47:06 -07002798 HandlerThread callbackThread = new HandlerThread("ConnectivityManager");
2799 callbackThread.start();
2800 sCallbackHandler = new CallbackHandler(callbackThread.getLooper(),
Robert Greenwalt6078b502014-06-11 16:05:07 -07002801 sNetworkCallback, sCallbackRefCount, this);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002802 }
2803 }
2804 }
2805
Robert Greenwalt6078b502014-06-11 16:05:07 -07002806 private void decCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002807 synchronized(sCallbackRefCount) {
2808 if (sCallbackRefCount.decrementAndGet() == 0) {
2809 sCallbackHandler.obtainMessage(CALLBACK_EXIT).sendToTarget();
2810 sCallbackHandler = null;
2811 }
2812 }
2813 }
2814
Robert Greenwalt6078b502014-06-11 16:05:07 -07002815 static final HashMap<NetworkRequest, NetworkCallback> sNetworkCallback =
2816 new HashMap<NetworkRequest, NetworkCallback>();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002817 static final AtomicInteger sCallbackRefCount = new AtomicInteger(0);
2818 static CallbackHandler sCallbackHandler = null;
2819
2820 private final static int LISTEN = 1;
2821 private final static int REQUEST = 2;
2822
Robert Greenwalt562cc542014-05-15 18:07:26 -07002823 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002824 NetworkCallback networkCallback, int timeoutSec, int action,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07002825 int legacyType) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002826 if (networkCallback == null) {
2827 throw new IllegalArgumentException("null NetworkCallback");
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002828 }
Erik Klinea2d29402016-03-16 15:31:39 +09002829 if (need == null && action != REQUEST) {
2830 throw new IllegalArgumentException("null NetworkCapabilities");
2831 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002832 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002833 incCallbackHandlerRefCount();
Paul Jensen7221cc32014-06-27 11:05:32 -04002834 synchronized(sNetworkCallback) {
2835 if (action == LISTEN) {
2836 networkCallback.networkRequest = mService.listenForNetwork(need,
2837 new Messenger(sCallbackHandler), new Binder());
2838 } else {
2839 networkCallback.networkRequest = mService.requestNetwork(need,
2840 new Messenger(sCallbackHandler), timeoutSec, new Binder(), legacyType);
2841 }
2842 if (networkCallback.networkRequest != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002843 sNetworkCallback.put(networkCallback.networkRequest, networkCallback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002844 }
2845 }
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002846 } catch (RemoteException e) {
2847 throw e.rethrowFromSystemServer();
2848 }
Robert Greenwalt6078b502014-06-11 16:05:07 -07002849 if (networkCallback.networkRequest == null) decCallbackHandlerRefCount();
2850 return networkCallback.networkRequest;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002851 }
2852
2853 /**
Erik Klinea2d29402016-03-16 15:31:39 +09002854 * Helper function to request a network with a particular legacy type.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002855 *
2856 * This is temporarily public @hide so it can be called by system code that uses the
2857 * NetworkRequest API to request networks but relies on CONNECTIVITY_ACTION broadcasts for
2858 * instead network notifications.
2859 *
2860 * TODO: update said system code to rely on NetworkCallbacks and make this method private.
2861 *
2862 * @hide
2863 */
Lorenzo Colittid1179462015-11-25 15:47:14 +09002864 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002865 int timeoutMs, int legacyType) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002866 sendRequestForNetwork(request.networkCapabilities, networkCallback, timeoutMs, REQUEST,
2867 legacyType);
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002868 }
2869
2870 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002871 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002872 *
2873 * This {@link NetworkRequest} will live until released via
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09002874 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002875 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07002876 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002877 * can be used to direct traffic to the network.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002878 * <p>It is presently unsupported to request a network with mutable
2879 * {@link NetworkCapabilities} such as
2880 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2881 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
2882 * as these {@code NetworkCapabilities} represent states that a particular
2883 * network may never attain, and whether a network will attain these states
2884 * is unknown prior to bringing up the network so the framework does not
2885 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002886 *
2887 * <p>This method requires the caller to hold either the
2888 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2889 * or the ability to modify system settings as determined by
2890 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt9258c642014-03-26 16:47:06 -07002891 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002892 * @param request {@link NetworkRequest} describing this request.
2893 * @param networkCallback The {@link NetworkCallback} to be utilized for this
2894 * request. Note the callback must not be shared - they
2895 * uniquely specify this request.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002896 * @throws IllegalArgumentException if {@code request} specifies any mutable
2897 * {@code NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002898 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002899 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002900 requestNetwork(request, networkCallback, 0,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002901 inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002902 }
2903
2904 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002905 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
Robert Greenwalt9258c642014-03-26 16:47:06 -07002906 * by a timeout.
2907 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002908 * This function behaves identically to the non-timedout version, but if a suitable
Robert Greenwalt6078b502014-06-11 16:05:07 -07002909 * network is not found within the given time (in milliseconds) the
2910 * {@link NetworkCallback#unavailable} callback is called. The request must
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09002911 * still be released normally by calling {@link unregisterNetworkCallback(NetworkCallback)}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002912 *
2913 * <p>This method requires the caller to hold either the
2914 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2915 * or the ability to modify system settings as determined by
2916 * {@link android.provider.Settings.System#canWrite}.</p>
2917 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002918 * @param request {@link NetworkRequest} describing this request.
2919 * @param networkCallback The callbacks to be utilized for this request. Note
2920 * the callbacks must not be shared - they uniquely specify
2921 * this request.
2922 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
2923 * before {@link NetworkCallback#unavailable} is called.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002924 *
2925 * TODO: Make timeouts work and then unhide this method.
2926 *
Robert Greenwalt9258c642014-03-26 16:47:06 -07002927 * @hide
2928 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002929 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
2930 int timeoutMs) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002931 requestNetwork(request, networkCallback, timeoutMs,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002932 inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002933 }
2934
2935 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002936 * The maximum number of milliseconds the framework will look for a suitable network
Robert Greenwalt9258c642014-03-26 16:47:06 -07002937 * during a timeout-equiped call to {@link requestNetwork}.
2938 * {@hide}
2939 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002940 public final static int MAX_NETWORK_REQUEST_TIMEOUT_MS = 100 * 60 * 1000;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002941
2942 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002943 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002944 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002945 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfcde58f2015-02-11 16:51:13 -08002946 * <p>
Paul Jensen72db88e2015-03-10 10:54:12 -04002947 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
2948 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002949 */
Erik Kline90e93072014-11-19 12:12:24 +09002950 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002951
2952 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002953 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002954 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002955 * {@link android.content.Intent#getParcelableExtra(String)}.
2956 */
Erik Kline90e93072014-11-19 12:12:24 +09002957 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002958
2959
2960 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002961 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002962 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002963 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07002964 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002965 * the request may outlive the calling application and get called back when a suitable
2966 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002967 * <p>
2968 * The operation is an Intent broadcast that goes to a broadcast receiver that
2969 * you registered with {@link Context#registerReceiver} or through the
2970 * &lt;receiver&gt; tag in an AndroidManifest.xml file
2971 * <p>
2972 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09002973 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
2974 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07002975 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002976 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07002977 * Intent to reserve the network or it will be released shortly after the Intent
2978 * is processed.
2979 * <p>
Paul Jensen694f2b82015-06-17 14:15:39 -04002980 * If there is already a request for this Intent registered (with the equality of
Robert Greenwalt9258c642014-03-26 16:47:06 -07002981 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002982 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002983 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002984 * The request may be released normally by calling
2985 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002986 * <p>It is presently unsupported to request a network with either
2987 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2988 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
2989 * as these {@code NetworkCapabilities} represent states that a particular
2990 * network may never attain, and whether a network will attain these states
2991 * is unknown prior to bringing up the network so the framework does not
2992 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002993 *
2994 * <p>This method requires the caller to hold either the
2995 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2996 * or the ability to modify system settings as determined by
2997 * {@link android.provider.Settings.System#canWrite}.</p>
2998 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002999 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003000 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07003001 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003002 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003003 * @throws IllegalArgumentException if {@code request} contains either
3004 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3005 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003006 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003007 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003008 checkPendingIntent(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003009 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07003010 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003011 } catch (RemoteException e) {
3012 throw e.rethrowFromSystemServer();
3013 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003014 }
3015
3016 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003017 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
3018 * <p>
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003019 * This method has the same behavior as
3020 * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003021 * releasing network resources and disconnecting.
3022 *
3023 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3024 * PendingIntent passed to
3025 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
3026 * corresponding NetworkRequest you'd like to remove. Cannot be null.
3027 */
3028 public void releaseNetworkRequest(PendingIntent operation) {
3029 checkPendingIntent(operation);
3030 try {
3031 mService.releasePendingNetworkRequest(operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003032 } catch (RemoteException e) {
3033 throw e.rethrowFromSystemServer();
3034 }
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003035 }
3036
3037 private void checkPendingIntent(PendingIntent intent) {
3038 if (intent == null) {
3039 throw new IllegalArgumentException("PendingIntent cannot be null.");
3040 }
3041 }
3042
3043 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07003044 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07003045 * {@link NetworkRequest}. The callbacks will continue to be called until
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003046 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Paul Jensenb2748922015-05-06 11:10:18 -04003047 * <p>This method requires the caller to hold the permission
3048 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003049 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003050 * @param request {@link NetworkRequest} describing this request.
3051 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3052 * networks change state.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003053 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003054 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
3055 sendRequestForNetwork(request.networkCapabilities, networkCallback, 0, LISTEN, TYPE_NONE);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003056 }
3057
3058 /**
Paul Jensen694f2b82015-06-17 14:15:39 -04003059 * Registers a PendingIntent to be sent when a network is available which satisfies the given
3060 * {@link NetworkRequest}.
3061 *
3062 * This function behaves identically to the version that takes a NetworkCallback, but instead
3063 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
3064 * the request may outlive the calling application and get called back when a suitable
3065 * network is found.
3066 * <p>
3067 * The operation is an Intent broadcast that goes to a broadcast receiver that
3068 * you registered with {@link Context#registerReceiver} or through the
3069 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3070 * <p>
3071 * The operation Intent is delivered with two extras, a {@link Network} typed
3072 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3073 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
3074 * the original requests parameters.
3075 * <p>
3076 * If there is already a request for this Intent registered (with the equality of
3077 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
3078 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
3079 * <p>
3080 * The request may be released normally by calling
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003081 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Paul Jensen694f2b82015-06-17 14:15:39 -04003082 * <p>This method requires the caller to hold the permission
3083 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
3084 * @param request {@link NetworkRequest} describing this request.
3085 * @param operation Action to perform when the network is available (corresponds
3086 * to the {@link NetworkCallback#onAvailable} call. Typically
3087 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
3088 */
3089 public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) {
3090 checkPendingIntent(operation);
3091 try {
3092 mService.pendingListenForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003093 } catch (RemoteException e) {
3094 throw e.rethrowFromSystemServer();
3095 }
Paul Jensen694f2b82015-06-17 14:15:39 -04003096 }
3097
3098 /**
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003099 * Registers to receive notifications about changes in the system default network. The callbacks
3100 * will continue to be called until either the application exits or
3101 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Erik Klinea2d29402016-03-16 15:31:39 +09003102 * <p>This method requires the caller to hold the permission
3103 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
3104 *
3105 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3106 * system default network changes.
Erik Klinea2d29402016-03-16 15:31:39 +09003107 */
3108 public void registerDefaultNetworkCallback(NetworkCallback networkCallback) {
3109 // This works because if the NetworkCapabilities are null,
3110 // ConnectivityService takes them from the default request.
3111 //
3112 // Since the capabilities are exactly the same as the default request's
3113 // capabilities, this request is guaranteed, at all times, to be
3114 // satisfied by the same network, if any, that satisfies the default
3115 // request, i.e., the system default network.
3116 sendRequestForNetwork(null, networkCallback, 0, REQUEST, TYPE_NONE);
3117 }
3118
3119 /**
fengludb571472015-04-21 17:12:05 -07003120 * Requests bandwidth update for a given {@link Network} and returns whether the update request
3121 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
3122 * network connection for updated bandwidth information. The caller will be notified via
3123 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003124 * method assumes that the caller has previously called
3125 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network
3126 * changes.
fenglub15e72b2015-03-20 11:29:56 -07003127 *
fengluae519192015-04-27 14:28:04 -07003128 * @param network {@link Network} specifying which network you're interested.
fengludb571472015-04-21 17:12:05 -07003129 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglub15e72b2015-03-20 11:29:56 -07003130 */
fengludb571472015-04-21 17:12:05 -07003131 public boolean requestBandwidthUpdate(Network network) {
fenglub15e72b2015-03-20 11:29:56 -07003132 try {
fengludb571472015-04-21 17:12:05 -07003133 return mService.requestBandwidthUpdate(network);
fenglub15e72b2015-03-20 11:29:56 -07003134 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003135 throw e.rethrowFromSystemServer();
fenglub15e72b2015-03-20 11:29:56 -07003136 }
3137 }
3138
3139 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07003140 * Unregisters callbacks about and possibly releases networks originating from
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003141 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and
3142 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls.
3143 * If the given {@code NetworkCallback} had previously been used with
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09003144 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
3145 * will be disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003146 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003147 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003148 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003149 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
3150 if (networkCallback == null || networkCallback.networkRequest == null ||
3151 networkCallback.networkRequest.requestId == REQUEST_ID_UNSET) {
3152 throw new IllegalArgumentException("Invalid NetworkCallback");
3153 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003154 try {
Hugo Benichi39e10e82016-07-07 09:36:12 +09003155 // CallbackHandler will release callback when receiving CALLBACK_RELEASED.
Robert Greenwalt6078b502014-06-11 16:05:07 -07003156 mService.releaseNetworkRequest(networkCallback.networkRequest);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003157 } catch (RemoteException e) {
3158 throw e.rethrowFromSystemServer();
3159 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003160 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003161
3162 /**
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003163 * Unregisters a callback previously registered via
3164 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3165 *
3166 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3167 * PendingIntent passed to
3168 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3169 * Cannot be null.
3170 */
3171 public void unregisterNetworkCallback(PendingIntent operation) {
3172 releaseNetworkRequest(operation);
3173 }
3174
3175 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003176 * Informs the system whether it should switch to {@code network} regardless of whether it is
3177 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
3178 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
3179 * the system default network regardless of any other network that's currently connected. If
3180 * {@code always} is true, then the choice is remembered, so that the next time the user
3181 * connects to this network, the system will switch to it.
3182 *
3183 * <p>This method requires the caller to hold the permission
3184 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
3185 *
3186 * @param network The network to accept.
3187 * @param accept Whether to accept the network even if unvalidated.
3188 * @param always Whether to remember this choice in the future.
3189 *
3190 * @hide
3191 */
3192 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
3193 try {
3194 mService.setAcceptUnvalidated(network, accept, always);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003195 } catch (RemoteException e) {
3196 throw e.rethrowFromSystemServer();
3197 }
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003198 }
3199
3200 /**
Lorenzo Colitti165c51c2016-09-19 01:00:19 +09003201 * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is
3202 * only meaningful if the system is configured not to penalize such networks, e.g., if the
3203 * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code
3204 * NETWORK_AVOID_BAD_WIFI setting is unset}.
3205 *
3206 * <p>This method requires the caller to hold the permission
3207 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
3208 *
3209 * @param network The network to accept.
3210 *
3211 * @hide
3212 */
3213 public void setAvoidUnvalidated(Network network) {
3214 try {
3215 mService.setAvoidUnvalidated(network);
3216 } catch (RemoteException e) {
3217 throw e.rethrowFromSystemServer();
3218 }
3219 }
3220
3221 /**
Stuart Scott984dc852015-03-30 13:17:11 -07003222 * Resets all connectivity manager settings back to factory defaults.
3223 * @hide
3224 */
3225 public void factoryReset() {
Stuart Scott984dc852015-03-30 13:17:11 -07003226 try {
Stuart Scottf1fb3972015-04-02 18:00:02 -07003227 mService.factoryReset();
Stuart Scott984dc852015-03-30 13:17:11 -07003228 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003229 throw e.rethrowFromSystemServer();
Stuart Scott984dc852015-03-30 13:17:11 -07003230 }
3231 }
3232
3233 /**
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003234 * Binds the current process to {@code network}. All Sockets created in the future
3235 * (and not explicitly bound via a bound SocketFactory from
3236 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3237 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3238 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3239 * work and all host name resolutions will fail. This is by design so an application doesn't
3240 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3241 * To clear binding pass {@code null} for {@code network}. Using individually bound
3242 * Sockets created by Network.getSocketFactory().createSocket() and
3243 * performing network-specific host name resolutions via
3244 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen72db88e2015-03-10 10:54:12 -04003245 * {@code bindProcessToNetwork}.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003246 *
3247 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3248 * the current binding.
3249 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3250 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003251 public boolean bindProcessToNetwork(Network network) {
3252 // Forcing callers to call thru non-static function ensures ConnectivityManager
3253 // instantiated.
3254 return setProcessDefaultNetwork(network);
3255 }
3256
3257 /**
3258 * Binds the current process to {@code network}. All Sockets created in the future
3259 * (and not explicitly bound via a bound SocketFactory from
3260 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3261 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3262 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3263 * work and all host name resolutions will fail. This is by design so an application doesn't
3264 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3265 * To clear binding pass {@code null} for {@code network}. Using individually bound
3266 * Sockets created by Network.getSocketFactory().createSocket() and
3267 * performing network-specific host name resolutions via
3268 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
3269 * {@code setProcessDefaultNetwork}.
3270 *
3271 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3272 * the current binding.
3273 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3274 * @deprecated This function can throw {@link IllegalStateException}. Use
3275 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
3276 * is a direct replacement.
3277 */
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003278 public static boolean setProcessDefaultNetwork(Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003279 int netId = (network == null) ? NETID_UNSET : network.netId;
Paul Jensen72db88e2015-03-10 10:54:12 -04003280 if (netId == NetworkUtils.getBoundNetworkForProcess()) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003281 return true;
3282 }
3283 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05003284 // Set HTTP proxy system properties to match network.
3285 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colittiec4c5552015-04-22 11:52:48 +09003286 try {
3287 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
3288 } catch (SecurityException e) {
3289 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
3290 Log.e(TAG, "Can't set proxy properties", e);
3291 }
Paul Jensenc91b5342014-08-27 12:38:45 -04003292 // Must flush DNS cache as new network may have different DNS resolutions.
3293 InetAddress.clearDnsCache();
3294 // Must flush socket pool as idle sockets will be bound to previous network and may
3295 // cause subsequent fetches to be performed on old network.
3296 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
3297 return true;
3298 } else {
3299 return false;
3300 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003301 }
3302
3303 /**
3304 * Returns the {@link Network} currently bound to this process via
Paul Jensen72db88e2015-03-10 10:54:12 -04003305 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003306 *
3307 * @return {@code Network} to which this process is bound, or {@code null}.
3308 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003309 public Network getBoundNetworkForProcess() {
3310 // Forcing callers to call thru non-static function ensures ConnectivityManager
3311 // instantiated.
3312 return getProcessDefaultNetwork();
3313 }
3314
3315 /**
3316 * Returns the {@link Network} currently bound to this process via
3317 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
3318 *
3319 * @return {@code Network} to which this process is bound, or {@code null}.
3320 * @deprecated Using this function can lead to other functions throwing
3321 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
3322 * {@code getBoundNetworkForProcess} is a direct replacement.
3323 */
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003324 public static Network getProcessDefaultNetwork() {
Paul Jensen72db88e2015-03-10 10:54:12 -04003325 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04003326 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003327 return new Network(netId);
3328 }
3329
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003330 private void unsupportedStartingFrom(int version) {
3331 if (Process.myUid() == Process.SYSTEM_UID) {
3332 // The getApplicationInfo() call we make below is not supported in system context, and
3333 // we want to allow the system to use these APIs anyway.
3334 return;
3335 }
3336
3337 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
3338 throw new UnsupportedOperationException(
3339 "This method is not supported in target SDK version " + version + " and above");
3340 }
3341 }
3342
3343 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
3344 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
Lifu Tang30f95a72016-01-07 23:20:38 -08003345 // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003346 // remove these exemptions. Note that this check is not secure, and apps can still access these
3347 // functions by accessing ConnectivityService directly. However, it should be clear that doing
3348 // so is unsupported and may break in the future. http://b/22728205
3349 private void checkLegacyRoutingApiAccess() {
3350 if (mContext.checkCallingOrSelfPermission("com.android.permission.INJECT_OMADM_SETTINGS")
3351 == PackageManager.PERMISSION_GRANTED) {
3352 return;
3353 }
3354
Dianne Hackborn692a2442015-07-31 10:35:34 -07003355 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003356 }
3357
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003358 /**
3359 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen72db88e2015-03-10 10:54:12 -04003360 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003361 *
3362 * @param network The {@link Network} to bind host resolutions from the current process to, or
3363 * {@code null} to clear the current binding.
3364 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3365 * @hide
3366 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
3367 */
3368 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04003369 return NetworkUtils.bindProcessToNetworkForHostResolution(
3370 network == null ? NETID_UNSET : network.netId);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003371 }
Felipe Leme1b103232016-01-22 09:44:57 -08003372
3373 /**
3374 * Device is not restricting metered network activity while application is running on
3375 * background.
3376 */
3377 public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1;
3378
3379 /**
3380 * Device is restricting metered network activity while application is running on background,
3381 * but application is allowed to bypass it.
3382 * <p>
3383 * In this state, application should take action to mitigate metered network access.
3384 * For example, a music streaming application should switch to a low-bandwidth bitrate.
3385 */
3386 public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2;
3387
3388 /**
3389 * Device is restricting metered network activity while application is running on background.
Felipe Leme9778f762016-01-27 14:46:39 -08003390 * <p>
Felipe Leme1b103232016-01-22 09:44:57 -08003391 * In this state, application should not try to use the network while running on background,
3392 * because it would be denied.
3393 */
3394 public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3;
3395
Felipe Leme9778f762016-01-27 14:46:39 -08003396 /**
3397 * A change in the background metered network activity restriction has occurred.
3398 * <p>
3399 * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction
3400 * applies to them.
3401 * <p>
3402 * This is only sent to registered receivers, not manifest receivers.
3403 */
3404 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
3405 public static final String ACTION_RESTRICT_BACKGROUND_CHANGED =
3406 "android.net.conn.RESTRICT_BACKGROUND_CHANGED";
3407
Felipe Lemeecfccea2016-01-25 11:48:04 -08003408 /** @hide */
3409 @Retention(RetentionPolicy.SOURCE)
Felipe Leme1b103232016-01-22 09:44:57 -08003410 @IntDef(flag = false, value = {
3411 RESTRICT_BACKGROUND_STATUS_DISABLED,
3412 RESTRICT_BACKGROUND_STATUS_WHITELISTED,
3413 RESTRICT_BACKGROUND_STATUS_ENABLED,
3414 })
Felipe Leme1b103232016-01-22 09:44:57 -08003415 public @interface RestrictBackgroundStatus {
3416 }
3417
3418 private INetworkPolicyManager getNetworkPolicyManager() {
3419 synchronized (this) {
3420 if (mNPManager != null) {
3421 return mNPManager;
3422 }
3423 mNPManager = INetworkPolicyManager.Stub.asInterface(ServiceManager
3424 .getService(Context.NETWORK_POLICY_SERVICE));
3425 return mNPManager;
3426 }
3427 }
3428
3429 /**
3430 * Determines if the calling application is subject to metered network restrictions while
3431 * running on background.
Felipe Lemec9c7be52016-05-16 13:57:19 -07003432 *
3433 * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED},
3434 * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED},
3435 * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED}
Felipe Leme1b103232016-01-22 09:44:57 -08003436 */
3437 public @RestrictBackgroundStatus int getRestrictBackgroundStatus() {
3438 try {
3439 return getNetworkPolicyManager().getRestrictBackgroundByCaller();
3440 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003441 throw e.rethrowFromSystemServer();
Felipe Leme1b103232016-01-22 09:44:57 -08003442 }
3443 }
Andreas Gampe34802132016-04-20 14:33:51 -07003444
3445 /**
3446 * A holder class for debug info (mapping CALLBACK values to field names). This is stored
3447 * in a holder for two reasons:
3448 * 1) The reflection necessary to establish the map can't be run at compile-time. Thus, this
3449 * code will make the enclosing class not compile-time initializeable, deferring its
3450 * initialization to zygote startup. This leads to dirty (but shared) memory.
3451 * As this is debug info, use a holder that isn't initialized by default. This way the map
3452 * will be created on demand, while ConnectivityManager can be compile-time initialized.
3453 * 2) Static initialization is still preferred for its strong thread safety guarantees without
3454 * requiring a lock.
3455 */
3456 private static class NoPreloadHolder {
3457 public static final SparseArray<String> sMagicDecoderRing = MessageUtils.findMessageNames(
3458 new Class[]{ConnectivityManager.class}, new String[]{"CALLBACK_"});
3459 }
3460
3461 static {
3462 // When debug is enabled, aggressively initialize the holder by touching the field (which
3463 // will guarantee static initialization).
3464 if (CallbackHandler.DBG) {
3465 Object dummy = NoPreloadHolder.sMagicDecoderRing;
3466 }
3467 }
3468
3469 private static final String whatToString(int what) {
3470 return NoPreloadHolder.sMagicDecoderRing.get(what, Integer.toString(what));
3471 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003472}