blob: 766963593f32fd8c0bc2249078792de7a52f7792 [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 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800346 * Invalid tethering type.
347 * @see #startTethering(int, OnStartTetheringCallback, boolean)
348 * @hide
349 */
350 public static final int TETHERING_INVALID = -1;
351
352 /**
353 * Wifi tethering type.
354 * @see #startTethering(int, OnStartTetheringCallback, boolean)
355 * @hide
356 */
357 @SystemApi
358 public static final int TETHERING_WIFI = 0;
359
360 /**
361 * USB tethering type.
362 * @see #startTethering(int, OnStartTetheringCallback, boolean)
363 * @hide
364 */
365 @SystemApi
366 public static final int TETHERING_USB = 1;
367
368 /**
369 * Bluetooth tethering type.
370 * @see #startTethering(int, OnStartTetheringCallback, boolean)
371 * @hide
372 */
373 @SystemApi
374 public static final int TETHERING_BLUETOOTH = 2;
375
376 /**
377 * Extra used for communicating with the TetherService. Includes the type of tethering to
378 * enable if any.
379 * @hide
380 */
381 public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType";
382
383 /**
384 * Extra used for communicating with the TetherService. Includes the type of tethering for
385 * which to cancel provisioning.
386 * @hide
387 */
388 public static final String EXTRA_REM_TETHER_TYPE = "extraRemTetherType";
389
390 /**
391 * Extra used for communicating with the TetherService. True to schedule a recheck of tether
392 * provisioning.
393 * @hide
394 */
395 public static final String EXTRA_SET_ALARM = "extraSetAlarm";
396
397 /**
398 * Tells the TetherService to run a provision check now.
399 * @hide
400 */
401 public static final String EXTRA_RUN_PROVISION = "extraRunProvision";
402
403 /**
404 * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver}
405 * which will receive provisioning results. Can be left empty.
406 * @hide
407 */
408 public static final String EXTRA_PROVISION_CALLBACK = "extraProvisionCallback";
409
410 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800411 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700412 * @hide
413 */
414 public static final int TYPE_NONE = -1;
415
416 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800417 * The Mobile data connection. When active, all data traffic
418 * will use this network type's interface by default
419 * (it has a default route)
Robert Greenwalt42acef32009-08-12 16:08:25 -0700420 */
421 public static final int TYPE_MOBILE = 0;
422 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800423 * The WIFI data connection. When active, all data traffic
424 * will use this network type's interface by default
425 * (it has a default route).
Robert Greenwalt42acef32009-08-12 16:08:25 -0700426 */
427 public static final int TYPE_WIFI = 1;
428 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800429 * An MMS-specific Mobile data connection. This network type may use the
430 * same network interface as {@link #TYPE_MOBILE} or it may use a different
431 * one. This is used by applications needing to talk to the carrier's
432 * Multimedia Messaging Service servers.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900433 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900434 * @deprecated Applications should instead use
435 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900436 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700437 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700438 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700439 public static final int TYPE_MOBILE_MMS = 2;
440 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800441 * A SUPL-specific Mobile data connection. This network type may use the
442 * same network interface as {@link #TYPE_MOBILE} or it may use a different
443 * one. This is used by applications needing to talk to the carrier's
444 * Secure User Plane Location servers for help locating the device.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900445 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900446 * @deprecated Applications should instead use
447 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900448 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700449 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700450 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700451 public static final int TYPE_MOBILE_SUPL = 3;
452 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800453 * A DUN-specific Mobile data connection. This network type may use the
454 * same network interface as {@link #TYPE_MOBILE} or it may use a different
455 * one. This is sometimes by the system when setting up an upstream connection
456 * for tethering so that the carrier is aware of DUN traffic.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700457 */
458 public static final int TYPE_MOBILE_DUN = 4;
459 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800460 * A High Priority Mobile data connection. This network type uses the
461 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colittie285b432015-04-23 15:32:42 +0900462 * is different.
463 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900464 * @deprecated Applications should instead use
465 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900466 * uses the {@link NetworkCapabilities#TRANSPORT_CELLULAR} transport.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700467 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700468 @Deprecated
Robert Greenwalt42acef32009-08-12 16:08:25 -0700469 public static final int TYPE_MOBILE_HIPRI = 5;
jsh8214deb2010-03-11 15:04:43 -0800470 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800471 * The WiMAX data connection. When active, all data traffic
472 * will use this network type's interface by default
473 * (it has a default route).
jsh8214deb2010-03-11 15:04:43 -0800474 */
475 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800476
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800477 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800478 * The Bluetooth data connection. When active, all data traffic
479 * will use this network type's interface by default
480 * (it has a default route).
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800481 */
482 public static final int TYPE_BLUETOOTH = 7;
483
Robert Greenwalt60810842011-04-22 15:28:18 -0700484 /**
485 * Dummy data connection. This should not be used on shipping devices.
486 */
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800487 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800488
Robert Greenwalt60810842011-04-22 15:28:18 -0700489 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800490 * The Ethernet data connection. When active, all data traffic
491 * will use this network type's interface by default
492 * (it has a default route).
Robert Greenwalt60810842011-04-22 15:28:18 -0700493 */
Robert Greenwalte12aec92011-01-28 14:48:37 -0800494 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700495
Wink Saville9d7d6282011-03-12 14:52:01 -0800496 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800497 * Over the air Administration.
Wink Saville9d7d6282011-03-12 14:52:01 -0800498 * {@hide}
499 */
500 public static final int TYPE_MOBILE_FOTA = 10;
501
502 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800503 * IP Multimedia Subsystem.
Wink Saville9d7d6282011-03-12 14:52:01 -0800504 * {@hide}
505 */
506 public static final int TYPE_MOBILE_IMS = 11;
507
508 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800509 * Carrier Branded Services.
Wink Saville9d7d6282011-03-12 14:52:01 -0800510 * {@hide}
511 */
512 public static final int TYPE_MOBILE_CBS = 12;
513
repo syncaea743a2011-07-29 23:55:49 -0700514 /**
515 * A Wi-Fi p2p connection. Only requesting processes will have access to
516 * the peers connected.
517 * {@hide}
518 */
519 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800520
Wink Saville5e56bc52013-07-29 15:00:57 -0700521 /**
522 * The network to use for initially attaching to the network
523 * {@hide}
524 */
525 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700526
Lorenzo Colittie285b432015-04-23 15:32:42 +0900527 /**
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700528 * Emergency PDN connection for emergency services. This
529 * may include IMS and MMS in emergency situations.
Ram3e0e3bc2014-06-26 11:03:44 -0700530 * {@hide}
531 */
532 public static final int TYPE_MOBILE_EMERGENCY = 15;
533
Hui Lu1c5624a2014-01-15 11:05:36 -0500534 /**
535 * The network that uses proxy to achieve connectivity.
536 * {@hide}
537 */
538 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700539
Robert Greenwalt8283f882014-07-07 17:09:01 -0700540 /**
541 * A virtual network using one or more native bearers.
542 * It may or may not be providing security services.
543 */
544 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500545
546 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700547 public static final int MAX_RADIO_TYPE = TYPE_VPN;
548
549 /** {@hide} */
550 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800551
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800552 /**
553 * If you want to set the default network preference,you can directly
554 * change the networkAttributes array in framework's config.xml.
555 *
556 * @deprecated Since we support so many more networks now, the single
557 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800558 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800559 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800560 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800561 * from an App.
562 */
563 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800564 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
565
Jeff Sharkey625239a2012-09-26 22:03:49 -0700566 /**
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700567 * @hide
568 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700569 public final static int REQUEST_ID_UNSET = 0;
570
Paul Jensen5d59e782014-07-11 12:28:19 -0400571 /**
572 * A NetID indicating no Network is selected.
573 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
574 * @hide
575 */
576 public static final int NETID_UNSET = 0;
577
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700578 private final IConnectivityManager mService;
Paul Jensene0bef712014-12-10 15:12:18 -0500579 /**
580 * A kludge to facilitate static access where a Context pointer isn't available, like in the
581 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
582 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
583 * methods that take a Context argument.
584 */
585 private static ConnectivityManager sInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800586
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900587 private final Context mContext;
588
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800589 private INetworkManagementService mNMService;
Felipe Leme1b103232016-01-22 09:44:57 -0800590 private INetworkPolicyManager mNPManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800591
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800592 /**
593 * Tests if a given integer represents a valid network type.
594 * @param networkType the type to be tested
595 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensen9e59e122015-05-06 10:42:25 -0400596 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
597 * validate a network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800598 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700599 @Deprecated
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700600 public static boolean isNetworkTypeValid(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700601 return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800602 }
603
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800604 /**
605 * Returns a non-localized string representing a given network type.
606 * ONLY used for debugging output.
607 * @param type the type needing naming
608 * @return a String for the given type, or a string version of the type ("87")
609 * if no name is known.
610 * {@hide}
611 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700612 public static String getNetworkTypeName(int type) {
613 switch (type) {
614 case TYPE_MOBILE:
615 return "MOBILE";
616 case TYPE_WIFI:
617 return "WIFI";
618 case TYPE_MOBILE_MMS:
619 return "MOBILE_MMS";
620 case TYPE_MOBILE_SUPL:
621 return "MOBILE_SUPL";
622 case TYPE_MOBILE_DUN:
623 return "MOBILE_DUN";
624 case TYPE_MOBILE_HIPRI:
625 return "MOBILE_HIPRI";
626 case TYPE_WIMAX:
627 return "WIMAX";
628 case TYPE_BLUETOOTH:
629 return "BLUETOOTH";
630 case TYPE_DUMMY:
631 return "DUMMY";
632 case TYPE_ETHERNET:
633 return "ETHERNET";
634 case TYPE_MOBILE_FOTA:
635 return "MOBILE_FOTA";
636 case TYPE_MOBILE_IMS:
637 return "MOBILE_IMS";
638 case TYPE_MOBILE_CBS:
639 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700640 case TYPE_WIFI_P2P:
641 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700642 case TYPE_MOBILE_IA:
643 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700644 case TYPE_MOBILE_EMERGENCY:
645 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500646 case TYPE_PROXY:
647 return "PROXY";
Erik Kline37fbfa12014-11-19 17:23:41 +0900648 case TYPE_VPN:
649 return "VPN";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700650 default:
651 return Integer.toString(type);
652 }
653 }
654
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800655 /**
656 * Checks if a given type uses the cellular data connection.
657 * This should be replaced in the future by a network property.
658 * @param networkType the type to check
659 * @return a boolean - {@code true} if uses cellular network, else {@code false}
660 * {@hide}
661 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700662 public static boolean isNetworkTypeMobile(int networkType) {
663 switch (networkType) {
664 case TYPE_MOBILE:
665 case TYPE_MOBILE_MMS:
666 case TYPE_MOBILE_SUPL:
667 case TYPE_MOBILE_DUN:
668 case TYPE_MOBILE_HIPRI:
669 case TYPE_MOBILE_FOTA:
670 case TYPE_MOBILE_IMS:
671 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700672 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700673 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700674 return true;
675 default:
676 return false;
677 }
678 }
679
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800680 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700681 * Checks if the given network type is backed by a Wi-Fi radio.
682 *
683 * @hide
684 */
685 public static boolean isNetworkTypeWifi(int networkType) {
686 switch (networkType) {
687 case TYPE_WIFI:
688 case TYPE_WIFI_P2P:
689 return true;
690 default:
691 return false;
692 }
693 }
694
695 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800696 * Specifies the preferred network type. When the device has more
697 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800698 *
699 * @param preference the network type to prefer over all others. It is
700 * unspecified what happens to the old preferred network in the
701 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700702 * @deprecated Functionality has been removed as it no longer makes sense,
703 * with many more than two networks - we'd need an array to express
704 * preference. Instead we use dynamic network properties of
705 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800706 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700707 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800708 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 }
710
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800711 /**
712 * Retrieves the current preferred network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400713 * <p>This method requires the caller to hold the permission
714 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800715 *
716 * @return an integer representing the preferred network type
717 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700718 * @deprecated Functionality has been removed as it no longer makes sense,
719 * with many more than two networks - we'd need an array to express
720 * preference. Instead we use dynamic network properties of
721 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800722 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700723 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800724 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700725 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800726 }
727
Scott Main671644c2011-10-06 19:02:28 -0700728 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800729 * Returns details about the currently active default data network. When
730 * connected, this network is the default route for outgoing connections.
731 * You should always check {@link NetworkInfo#isConnected()} before initiating
732 * network traffic. This may return {@code null} when there is no default
733 * network.
Paul Jensenb2748922015-05-06 11:10:18 -0400734 * <p>This method requires the caller to hold the permission
735 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800736 *
737 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500738 * or {@code null} if no default network is currently active
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700739 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800740 public NetworkInfo getActiveNetworkInfo() {
741 try {
742 return mService.getActiveNetworkInfo();
743 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700744 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800745 }
746 }
747
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800748 /**
Paul Jensen31a94f42015-02-13 14:18:39 -0500749 * Returns a {@link Network} object corresponding to the currently active
750 * default data network. In the event that the current active default data
751 * network disconnects, the returned {@code Network} object will no longer
752 * be usable. This will return {@code null} when there is no default
753 * network.
Paul Jensenb2748922015-05-06 11:10:18 -0400754 * <p>This method requires the caller to hold the permission
755 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Paul Jensen31a94f42015-02-13 14:18:39 -0500756 *
757 * @return a {@link Network} object for the current default network or
758 * {@code null} if no default network is currently active
Paul Jensen31a94f42015-02-13 14:18:39 -0500759 */
760 public Network getActiveNetwork() {
761 try {
762 return mService.getActiveNetwork();
763 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700764 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -0500765 }
766 }
767
768 /**
Robin Leed2baf792016-03-24 12:07:00 +0000769 * Returns a {@link Network} object corresponding to the currently active
770 * default data network for a specific UID. In the event that the default data
771 * network disconnects, the returned {@code Network} object will no longer
772 * be usable. This will return {@code null} when there is no default
773 * network for the UID.
774 * <p>This method requires the caller to hold the permission
775 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
776 *
777 * @return a {@link Network} object for the current default network for the
778 * given UID or {@code null} if no default network is currently active
779 *
780 * @hide
781 */
782 public Network getActiveNetworkForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600783 return getActiveNetworkForUid(uid, false);
784 }
785
786 /** {@hide} */
787 public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) {
Robin Leed2baf792016-03-24 12:07:00 +0000788 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600789 return mService.getActiveNetworkForUid(uid, ignoreBlocked);
Robin Leed2baf792016-03-24 12:07:00 +0000790 } catch (RemoteException e) {
791 throw e.rethrowFromSystemServer();
792 }
793 }
794
795 /**
Robin Lee244ce8e2016-01-05 18:03:46 +0000796 * Configures an always-on VPN connection through a specific application.
797 * This connection is automatically granted and persisted after a reboot.
798 *
799 * <p>The designated package should declare a {@link VpnService} in its
800 * manifest guarded by {@link android.Manifest.permission.BIND_VPN_SERVICE},
801 * otherwise the call will fail.
802 *
803 * @param userId The identifier of the user to set an always-on VPN for.
804 * @param vpnPackage The package name for an installed VPN app on the device, or {@code null}
805 * to remove an existing always-on VPN configuration.
Robin Leedc679712016-05-03 13:23:03 +0100806 * @param lockdownEnabled {@code true} to disallow networking when the VPN is not connected or
807 * {@code false} otherwise.
Robin Lee244ce8e2016-01-05 18:03:46 +0000808 * @return {@code true} if the package is set as always-on VPN controller;
809 * {@code false} otherwise.
810 * @hide
811 */
Robin Leedc679712016-05-03 13:23:03 +0100812 public boolean setAlwaysOnVpnPackageForUser(int userId, @Nullable String vpnPackage,
813 boolean lockdownEnabled) {
Robin Lee244ce8e2016-01-05 18:03:46 +0000814 try {
Robin Leedc679712016-05-03 13:23:03 +0100815 return mService.setAlwaysOnVpnPackage(userId, vpnPackage, lockdownEnabled);
Robin Lee244ce8e2016-01-05 18:03:46 +0000816 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700817 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +0000818 }
819 }
820
821 /**
822 * Returns the package name of the currently set always-on VPN application.
823 * If there is no always-on VPN set, or the VPN is provided by the system instead
824 * of by an app, {@code null} will be returned.
825 *
826 * @return Package name of VPN controller responsible for always-on VPN,
827 * or {@code null} if none is set.
828 * @hide
829 */
830 public String getAlwaysOnVpnPackageForUser(int userId) {
831 try {
832 return mService.getAlwaysOnVpnPackage(userId);
833 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700834 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +0000835 }
836 }
837
838 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800839 * Returns details about the currently active default data network
840 * for a given uid. This is for internal use only to avoid spying
841 * other apps.
Paul Jensenb2748922015-05-06 11:10:18 -0400842 * <p>This method requires the caller to hold the permission
843 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800844 *
845 * @return a {@link NetworkInfo} object for the current default network
846 * for the given uid or {@code null} if no default network is
847 * available for the specified uid.
848 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800849 * {@hide}
850 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700851 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600852 return getActiveNetworkInfoForUid(uid, false);
853 }
854
855 /** {@hide} */
856 public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) {
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700857 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600858 return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked);
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700859 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700860 throw e.rethrowFromSystemServer();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700861 }
862 }
863
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800864 /**
865 * Returns connection status information about a particular
866 * network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400867 * <p>This method requires the caller to hold the permission
868 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800869 *
870 * @param networkType integer specifying which networkType in
871 * which you're interested.
872 * @return a {@link NetworkInfo} object for the requested
873 * network type or {@code null} if the type is not
874 * supported by the device.
875 *
Paul Jensen3541e9f2015-03-18 12:23:02 -0400876 * @deprecated This method does not support multiple connected networks
877 * of the same type. Use {@link #getAllNetworks} and
878 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800879 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700880 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 public NetworkInfo getNetworkInfo(int networkType) {
882 try {
883 return mService.getNetworkInfo(networkType);
884 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700885 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 }
887 }
888
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800889 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700890 * Returns connection status information about a particular
891 * Network.
Paul Jensenb2748922015-05-06 11:10:18 -0400892 * <p>This method requires the caller to hold the permission
893 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700894 *
895 * @param network {@link Network} specifying which network
896 * in which you're interested.
897 * @return a {@link NetworkInfo} object for the requested
898 * network or {@code null} if the {@code Network}
899 * is not valid.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700900 */
901 public NetworkInfo getNetworkInfo(Network network) {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600902 return getNetworkInfoForUid(network, Process.myUid(), false);
903 }
904
905 /** {@hide} */
906 public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) {
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700907 try {
Jeff Sharkey1b6519b2016-04-28 15:33:18 -0600908 return mService.getNetworkInfoForUid(network, uid, ignoreBlocked);
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700909 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700910 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700911 }
912 }
913
914 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800915 * Returns connection status information about all network
916 * types supported by the device.
Paul Jensenb2748922015-05-06 11:10:18 -0400917 * <p>This method requires the caller to hold the permission
918 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800919 *
920 * @return an array of {@link NetworkInfo} objects. Check each
921 * {@link NetworkInfo#getType} for which type each applies.
922 *
Paul Jensen3541e9f2015-03-18 12:23:02 -0400923 * @deprecated This method does not support multiple connected networks
924 * of the same type. Use {@link #getAllNetworks} and
925 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800926 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700927 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800928 public NetworkInfo[] getAllNetworkInfo() {
929 try {
930 return mService.getAllNetworkInfo();
931 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700932 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800933 }
934 }
935
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800936 /**
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700937 * Returns the {@link Network} object currently serving a given type, or
938 * null if the given type is not connected.
939 *
940 * <p>This method requires the caller to hold the permission
941 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
942 *
943 * @hide
Paul Jensen3541e9f2015-03-18 12:23:02 -0400944 * @deprecated This method does not support multiple connected networks
945 * of the same type. Use {@link #getAllNetworks} and
946 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700947 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -0700948 @Deprecated
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700949 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 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001021 @Deprecated
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001022 public LinkProperties getLinkProperties(int networkType) {
1023 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001024 return mService.getLinkPropertiesForType(networkType);
1025 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001026 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001027 }
1028 }
1029
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001030 /**
1031 * Get the {@link LinkProperties} for the given {@link Network}. This
1032 * will return {@code null} if the network is unknown.
Paul Jensenb2748922015-05-06 11:10:18 -04001033 * <p>This method requires the caller to hold the permission
1034 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001035 *
1036 * @param network The {@link Network} object identifying the network in question.
1037 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensenb2748922015-05-06 11:10:18 -04001038 */
Robert Greenwalt9258c642014-03-26 16:47:06 -07001039 public LinkProperties getLinkProperties(Network network) {
1040 try {
1041 return mService.getLinkProperties(network);
1042 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001043 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001044 }
1045 }
1046
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001047 /**
Erik Klineacdd6392016-07-07 16:50:58 +09001048 * Request that this callback be invoked at ConnectivityService's earliest
1049 * convenience with the current satisfying network's LinkProperties.
1050 * If no such network exists no callback invocation is performed.
1051 *
1052 * The callback must have been registered with #requestNetwork() or
1053 * #registerDefaultNetworkCallback(); callbacks registered with
1054 * registerNetworkCallback() are not specific to any particular Network so
1055 * do not cause any updates.
1056 *
1057 * @hide
1058 */
1059 public void requestLinkProperties(NetworkCallback networkCallback) {
1060 try {
1061 mService.requestLinkProperties(networkCallback.networkRequest);
1062 } catch (RemoteException e) {
1063 throw e.rethrowFromSystemServer();
1064 }
1065 }
1066
1067 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001068 * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001069 * will return {@code null} if the network is unknown.
Paul Jensenb2748922015-05-06 11:10:18 -04001070 * <p>This method requires the caller to hold the permission
1071 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001072 *
1073 * @param network The {@link Network} object identifying the network in question.
Lorenzo Colittie285b432015-04-23 15:32:42 +09001074 * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001075 */
Robert Greenwalt9258c642014-03-26 16:47:06 -07001076 public NetworkCapabilities getNetworkCapabilities(Network network) {
1077 try {
1078 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001079 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001080 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001081 }
1082 }
1083
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001084 /**
Erik Klineacdd6392016-07-07 16:50:58 +09001085 * Request that this callback be invoked at ConnectivityService's earliest
1086 * convenience with the current satisfying network's NetworkCapabilities.
1087 * If no such network exists no callback invocation is performed.
1088 *
1089 * The callback must have been registered with #requestNetwork() or
1090 * #registerDefaultNetworkCallback(); callbacks registered with
1091 * registerNetworkCallback() are not specific to any particular Network so
1092 * do not cause any updates.
1093 *
1094 * @hide
1095 */
1096 public void requestNetworkCapabilities(NetworkCallback networkCallback) {
1097 try {
1098 mService.requestNetworkCapabilities(networkCallback.networkRequest);
1099 } catch (RemoteException e) {
1100 throw e.rethrowFromSystemServer();
1101 }
1102 }
1103
1104 /**
Udam Sainib7c24872016-01-04 12:16:14 -08001105 * Gets the URL that should be used for resolving whether a captive portal is present.
1106 * 1. This URL should respond with a 204 response to a GET request to indicate no captive
1107 * portal is present.
1108 * 2. This URL must be HTTP as redirect responses are used to find captive portal
1109 * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects.
1110 *
1111 * @hide
1112 */
1113 @SystemApi
1114 public String getCaptivePortalServerUrl() {
1115 try {
1116 return mService.getCaptivePortalServerUrl();
1117 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001118 throw e.rethrowFromSystemServer();
Udam Sainib7c24872016-01-04 12:16:14 -08001119 }
1120 }
1121
1122 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 * Tells the underlying networking system that the caller wants to
1124 * begin using the named feature. The interpretation of {@code feature}
1125 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001126 *
1127 * <p>This method requires the caller to hold either the
1128 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1129 * or the ability to modify system settings as determined by
1130 * {@link android.provider.Settings.System#canWrite}.</p>
1131 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001132 * @param networkType specifies which network the request pertains to
1133 * @param feature the name of the feature to be used
1134 * @return an integer value representing the outcome of the request.
1135 * The interpretation of this value is specific to each networking
1136 * implementation+feature combination, except that the value {@code -1}
1137 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001138 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001139 * @deprecated Deprecated in favor of the cleaner
1140 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001141 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001142 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001144 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001146 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001147 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1148 if (netCap == null) {
1149 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
1150 feature);
1151 return PhoneConstants.APN_REQUEST_FAILED;
1152 }
1153
1154 NetworkRequest request = null;
1155 synchronized (sLegacyRequests) {
1156 LegacyRequest l = sLegacyRequests.get(netCap);
1157 if (l != null) {
1158 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
1159 renewRequestLocked(l);
1160 if (l.currentNetwork != null) {
1161 return PhoneConstants.APN_ALREADY_ACTIVE;
1162 } else {
1163 return PhoneConstants.APN_REQUEST_STARTED;
1164 }
1165 }
1166
1167 request = requestNetworkForFeatureLocked(netCap);
1168 }
1169 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -07001170 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001171 return PhoneConstants.APN_REQUEST_STARTED;
1172 } else {
1173 Log.d(TAG, " request Failed");
1174 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 }
1176 }
1177
1178 /**
1179 * Tells the underlying networking system that the caller is finished
1180 * using the named feature. The interpretation of {@code feature}
1181 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001182 *
1183 * <p>This method requires the caller to hold either the
1184 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1185 * or the ability to modify system settings as determined by
1186 * {@link android.provider.Settings.System#canWrite}.</p>
1187 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001188 * @param networkType specifies which network the request pertains to
1189 * @param feature the name of the feature that is no longer needed
1190 * @return an integer value representing the outcome of the request.
1191 * The interpretation of this value is specific to each networking
1192 * implementation+feature combination, except that the value {@code -1}
1193 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001194 *
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09001195 * @deprecated Deprecated in favor of the cleaner
1196 * {@link #unregisterNetworkCallback(NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001197 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001198 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001199 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001200 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001201 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001202 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001203 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1204 if (netCap == null) {
1205 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
1206 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 return -1;
1208 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001209
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001210 if (removeRequestForFeature(netCap)) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001211 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001212 }
1213 return 1;
1214 }
1215
1216 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1217 if (networkType == TYPE_MOBILE) {
1218 int cap = -1;
1219 if ("enableMMS".equals(feature)) {
1220 cap = NetworkCapabilities.NET_CAPABILITY_MMS;
1221 } else if ("enableSUPL".equals(feature)) {
1222 cap = NetworkCapabilities.NET_CAPABILITY_SUPL;
1223 } else if ("enableDUN".equals(feature) || "enableDUNAlways".equals(feature)) {
1224 cap = NetworkCapabilities.NET_CAPABILITY_DUN;
1225 } else if ("enableHIPRI".equals(feature)) {
1226 cap = NetworkCapabilities.NET_CAPABILITY_INTERNET;
1227 } else if ("enableFOTA".equals(feature)) {
1228 cap = NetworkCapabilities.NET_CAPABILITY_FOTA;
1229 } else if ("enableIMS".equals(feature)) {
1230 cap = NetworkCapabilities.NET_CAPABILITY_IMS;
1231 } else if ("enableCBS".equals(feature)) {
1232 cap = NetworkCapabilities.NET_CAPABILITY_CBS;
1233 } else {
1234 return null;
1235 }
1236 NetworkCapabilities netCap = new NetworkCapabilities();
Robert Greenwalt7569f182014-06-08 16:42:59 -07001237 netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(cap);
Paul Jensen487ffe72015-07-24 15:57:11 -04001238 netCap.maybeMarkCapabilitiesRestricted();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001239 return netCap;
1240 } else if (networkType == TYPE_WIFI) {
1241 if ("p2p".equals(feature)) {
1242 NetworkCapabilities netCap = new NetworkCapabilities();
1243 netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
Robert Greenwalt7569f182014-06-08 16:42:59 -07001244 netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
Paul Jensen487ffe72015-07-24 15:57:11 -04001245 netCap.maybeMarkCapabilitiesRestricted();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001246 return netCap;
1247 }
1248 }
1249 return null;
1250 }
1251
Robert Greenwalt06314e42014-10-29 14:04:06 -07001252 /**
1253 * Guess what the network request was trying to say so that the resulting
1254 * network is accessible via the legacy (deprecated) API such as
1255 * requestRouteToHost.
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001256 *
1257 * This means we should try to be fairly precise about transport and
Robert Greenwalt06314e42014-10-29 14:04:06 -07001258 * capability but ignore things such as networkSpecifier.
1259 * If the request has more than one transport or capability it doesn't
1260 * match the old legacy requests (they selected only single transport/capability)
1261 * so this function cannot map the request to a single legacy type and
1262 * the resulting network will not be available to the legacy APIs.
1263 *
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001264 * This code is only called from the requestNetwork API (L and above).
1265 *
1266 * Setting a legacy type causes CONNECTIVITY_ACTION broadcasts, which are expensive
1267 * because they wake up lots of apps - see http://b/23350688 . So we currently only
1268 * do this for SUPL requests, which are the only ones that we know need it. If
1269 * omitting these broadcasts causes unacceptable app breakage, then for backwards
1270 * compatibility we can send them:
1271 *
1272 * if (targetSdkVersion < Build.VERSION_CODES.M) && // legacy API unsupported >= M
1273 * targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP)) // requestNetwork not present < L
1274 *
Robert Greenwalt06314e42014-10-29 14:04:06 -07001275 * TODO - This should be removed when the legacy APIs are removed.
1276 */
Ye Wenb87875e2014-07-21 14:19:01 -07001277 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1278 if (netCap == null) {
1279 return TYPE_NONE;
1280 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001281
Ye Wenb87875e2014-07-21 14:19:01 -07001282 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1283 return TYPE_NONE;
1284 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001285
Lifu Tang30f95a72016-01-07 23:20:38 -08001286 // Do this only for SUPL, until GnssLocationProvider is fixed. http://b/25876485 .
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001287 if (!netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1288 // NOTE: if this causes app breakage, we should not just comment out this early return;
1289 // instead, we should make this early return conditional on the requesting app's target
1290 // SDK version, as described in the comment above.
1291 return TYPE_NONE;
1292 }
1293
Robert Greenwalt06314e42014-10-29 14:04:06 -07001294 String type = null;
1295 int result = TYPE_NONE;
1296
Ye Wenb87875e2014-07-21 14:19:01 -07001297 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
Robert Greenwalt06314e42014-10-29 14:04:06 -07001298 type = "enableCBS";
1299 result = TYPE_MOBILE_CBS;
1300 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1301 type = "enableIMS";
1302 result = TYPE_MOBILE_IMS;
1303 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1304 type = "enableFOTA";
1305 result = TYPE_MOBILE_FOTA;
1306 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1307 type = "enableDUN";
1308 result = TYPE_MOBILE_DUN;
1309 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001310 type = "enableSUPL";
Robert Greenwalt06314e42014-10-29 14:04:06 -07001311 result = TYPE_MOBILE_SUPL;
Robert Greenwalt74ab4fa2015-08-28 12:37:54 -07001312 // back out this hack for mms as they no longer need this and it's causing
1313 // device slowdowns - b/23350688 (note, supl still needs this)
1314 //} else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1315 // type = "enableMMS";
1316 // result = TYPE_MOBILE_MMS;
Robert Greenwalt06314e42014-10-29 14:04:06 -07001317 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1318 type = "enableHIPRI";
1319 result = TYPE_MOBILE_HIPRI;
Ye Wenb87875e2014-07-21 14:19:01 -07001320 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001321 if (type != null) {
1322 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1323 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1324 return result;
Ye Wenb87875e2014-07-21 14:19:01 -07001325 }
1326 }
1327 return TYPE_NONE;
1328 }
1329
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001330 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001331 if (netCap == null) return TYPE_NONE;
1332 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1333 return TYPE_MOBILE_CBS;
1334 }
1335 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1336 return TYPE_MOBILE_IMS;
1337 }
1338 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1339 return TYPE_MOBILE_FOTA;
1340 }
1341 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1342 return TYPE_MOBILE_DUN;
1343 }
1344 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1345 return TYPE_MOBILE_SUPL;
1346 }
1347 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1348 return TYPE_MOBILE_MMS;
1349 }
1350 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1351 return TYPE_MOBILE_HIPRI;
1352 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001353 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1354 return TYPE_WIFI_P2P;
1355 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001356 return TYPE_NONE;
1357 }
1358
1359 private static class LegacyRequest {
1360 NetworkCapabilities networkCapabilities;
1361 NetworkRequest networkRequest;
1362 int expireSequenceNumber;
1363 Network currentNetwork;
1364 int delay = -1;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001365
1366 private void clearDnsBinding() {
1367 if (currentNetwork != null) {
1368 currentNetwork = null;
1369 setProcessDefaultNetworkForHostResolution(null);
1370 }
1371 }
1372
Robert Greenwalt6078b502014-06-11 16:05:07 -07001373 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001374 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001375 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001376 currentNetwork = network;
1377 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001378 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001379 }
1380 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001381 public void onLost(Network network) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001382 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001383 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1384 }
1385 };
1386 }
1387
Robert Greenwaltfab501672014-07-23 11:44:01 -07001388 private static HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
Robert Greenwalt562cc542014-05-15 18:07:26 -07001389 new HashMap<NetworkCapabilities, LegacyRequest>();
1390
1391 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1392 synchronized (sLegacyRequests) {
1393 LegacyRequest l = sLegacyRequests.get(netCap);
1394 if (l != null) return l.networkRequest;
1395 }
1396 return null;
1397 }
1398
1399 private void renewRequestLocked(LegacyRequest l) {
1400 l.expireSequenceNumber++;
1401 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1402 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1403 }
1404
1405 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1406 int ourSeqNum = -1;
1407 synchronized (sLegacyRequests) {
1408 LegacyRequest l = sLegacyRequests.get(netCap);
1409 if (l == null) return;
1410 ourSeqNum = l.expireSequenceNumber;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001411 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001412 }
1413 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1414 }
1415
1416 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1417 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001418 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001419 try {
1420 delay = mService.getRestoreDefaultNetworkDelay(type);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001421 } catch (RemoteException e) {
1422 throw e.rethrowFromSystemServer();
1423 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001424 LegacyRequest l = new LegacyRequest();
1425 l.networkCapabilities = netCap;
1426 l.delay = delay;
1427 l.expireSequenceNumber = 0;
Robert Greenwalt6078b502014-06-11 16:05:07 -07001428 l.networkRequest = sendRequestForNetwork(netCap, l.networkCallback, 0,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001429 REQUEST, type);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001430 if (l.networkRequest == null) return null;
1431 sLegacyRequests.put(netCap, l);
1432 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1433 return l.networkRequest;
1434 }
1435
1436 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1437 if (delay >= 0) {
1438 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
1439 Message msg = sCallbackHandler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1440 sCallbackHandler.sendMessageDelayed(msg, delay);
1441 }
1442 }
1443
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001444 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1445 final LegacyRequest l;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001446 synchronized (sLegacyRequests) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001447 l = sLegacyRequests.remove(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001448 }
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001449 if (l == null) return false;
1450 unregisterNetworkCallback(l.networkCallback);
1451 l.clearDnsBinding();
1452 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001453 }
1454
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001455 /** @hide */
1456 public static class PacketKeepaliveCallback {
1457 /** The requested keepalive was successfully started. */
1458 public void onStarted() {}
1459 /** The keepalive was successfully stopped. */
1460 public void onStopped() {}
1461 /** An error occurred. */
1462 public void onError(int error) {}
1463 }
1464
1465 /**
1466 * Allows applications to request that the system periodically send specific packets on their
1467 * behalf, using hardware offload to save battery power.
1468 *
1469 * To request that the system send keepalives, call one of the methods that return a
1470 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1471 * passing in a non-null callback. If the callback is successfully started, the callback's
1472 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1473 * specifying one of the {@code ERROR_*} constants in this class.
1474 *
1475 * To stop an existing keepalive, call {@link stop}. The system will call {@code onStopped} if
1476 * the operation was successfull or {@code onError} if an error occurred.
1477 *
1478 * @hide
1479 */
1480 public class PacketKeepalive {
1481
1482 private static final String TAG = "PacketKeepalive";
1483
1484 /** @hide */
1485 public static final int SUCCESS = 0;
1486
1487 /** @hide */
1488 public static final int NO_KEEPALIVE = -1;
1489
1490 /** @hide */
1491 public static final int BINDER_DIED = -10;
1492
1493 /** The specified {@code Network} is not connected. */
1494 public static final int ERROR_INVALID_NETWORK = -20;
1495 /** The specified IP addresses are invalid. For example, the specified source IP address is
1496 * not configured on the specified {@code Network}. */
1497 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1498 /** The requested port is invalid. */
1499 public static final int ERROR_INVALID_PORT = -22;
1500 /** The packet length is invalid (e.g., too long). */
1501 public static final int ERROR_INVALID_LENGTH = -23;
1502 /** The packet transmission interval is invalid (e.g., too short). */
1503 public static final int ERROR_INVALID_INTERVAL = -24;
1504
1505 /** The hardware does not support this request. */
1506 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti9d1284e2015-09-08 16:46:36 +09001507 /** The hardware returned an error. */
1508 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001509
1510 public static final int NATT_PORT = 4500;
1511
1512 private final Network mNetwork;
1513 private final PacketKeepaliveCallback mCallback;
1514 private final Looper mLooper;
1515 private final Messenger mMessenger;
1516
1517 private volatile Integer mSlot;
1518
1519 void stopLooper() {
1520 mLooper.quit();
1521 }
1522
1523 public void stop() {
1524 try {
1525 mService.stopKeepalive(mNetwork, mSlot);
1526 } catch (RemoteException e) {
1527 Log.e(TAG, "Error stopping packet keepalive: ", e);
1528 stopLooper();
1529 }
1530 }
1531
1532 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
1533 checkNotNull(network, "network cannot be null");
1534 checkNotNull(callback, "callback cannot be null");
1535 mNetwork = network;
1536 mCallback = callback;
1537 HandlerThread thread = new HandlerThread(TAG);
1538 thread.start();
1539 mLooper = thread.getLooper();
1540 mMessenger = new Messenger(new Handler(mLooper) {
1541 @Override
1542 public void handleMessage(Message message) {
1543 switch (message.what) {
1544 case NetworkAgent.EVENT_PACKET_KEEPALIVE:
1545 int error = message.arg2;
1546 try {
1547 if (error == SUCCESS) {
1548 if (mSlot == null) {
1549 mSlot = message.arg1;
1550 mCallback.onStarted();
1551 } else {
1552 mSlot = null;
1553 stopLooper();
1554 mCallback.onStopped();
1555 }
1556 } else {
1557 stopLooper();
1558 mCallback.onError(error);
1559 }
1560 } catch (Exception e) {
1561 Log.e(TAG, "Exception in keepalive callback(" + error + ")", e);
1562 }
1563 break;
1564 default:
1565 Log.e(TAG, "Unhandled message " + Integer.toHexString(message.what));
1566 break;
1567 }
1568 }
1569 });
1570 }
1571 }
1572
1573 /**
1574 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
1575 *
1576 * @hide
1577 */
1578 public PacketKeepalive startNattKeepalive(
1579 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
1580 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
1581 final PacketKeepalive k = new PacketKeepalive(network, callback);
1582 try {
1583 mService.startNattKeepalive(network, intervalSeconds, k.mMessenger, new Binder(),
1584 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
1585 } catch (RemoteException e) {
1586 Log.e(TAG, "Error starting packet keepalive: ", e);
1587 k.stopLooper();
1588 return null;
1589 }
1590 return k;
1591 }
1592
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001593 /**
1594 * Ensure that a network route exists to deliver traffic to the specified
1595 * host via the specified network interface. An attempt to add a route that
1596 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001597 *
1598 * <p>This method requires the caller to hold either the
1599 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1600 * or the ability to modify system settings as determined by
1601 * {@link android.provider.Settings.System#canWrite}.</p>
1602 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001603 * @param networkType the type of the network over which traffic to the specified
1604 * host is to be routed
1605 * @param hostAddress the IP address of the host to which the route is desired
1606 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001607 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001608 * @deprecated Deprecated in favor of the
1609 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
1610 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001611 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001612 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001613 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001614 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001615 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001616 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001617 }
1618
1619 /**
1620 * Ensure that a network route exists to deliver traffic to the specified
1621 * host via the specified network interface. An attempt to add a route that
1622 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001623 *
1624 * <p>This method requires the caller to hold either the
1625 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1626 * or the ability to modify system settings as determined by
1627 * {@link android.provider.Settings.System#canWrite}.</p>
1628 *
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001629 * @param networkType the type of the network over which traffic to the specified
1630 * host is to be routed
1631 * @param hostAddress the IP address of the host to which the route is desired
1632 * @return {@code true} on success, {@code false} on failure
1633 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001634 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001635 * {@link #bindProcessToNetwork} API.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001636 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001637 @Deprecated
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001638 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001639 checkLegacyRoutingApiAccess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001640 try {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001641 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001642 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001643 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001644 }
1645 }
1646
1647 /**
1648 * Returns the value of the setting for background data usage. If false,
1649 * applications should not use the network if the application is not in the
1650 * foreground. Developers should respect this setting, and check the value
1651 * of this before performing any background data operations.
1652 * <p>
1653 * All applications that have background services that use the network
1654 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001655 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001656 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001657 * background data depends on several combined factors, and this method will
1658 * always return {@code true}. Instead, when background data is unavailable,
1659 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001660 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001661 * @return Whether background data usage is allowed.
1662 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001663 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001664 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001665 // assume that background data is allowed; final authority is
1666 // NetworkInfo which may be blocked.
1667 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001668 }
1669
1670 /**
1671 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001672 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001673 * @param allowBackgroundData Whether an application should use data while
1674 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001675 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001676 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1677 * @see #getBackgroundDataSetting()
1678 * @hide
1679 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001680 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001681 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001682 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001683 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001684
1685 /**
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001686 * Return quota status for the current active network, or {@code null} if no
1687 * network is active. Quota status can change rapidly, so these values
1688 * shouldn't be cached.
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001689 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001690 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001691 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1692 *
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001693 * @hide
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001694 */
1695 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1696 try {
1697 return mService.getActiveNetworkQuotaInfo();
1698 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001699 throw e.rethrowFromSystemServer();
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001700 }
1701 }
1702
1703 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001704 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001705 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001706 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001707 @Deprecated
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001708 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001709 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1710 if (b != null) {
1711 try {
1712 ITelephony it = ITelephony.Stub.asInterface(b);
Shishir Agrawal7ea3e8b2016-01-25 13:03:07 -08001713 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Wink Saville36ffb042014-12-05 11:10:30 -08001714 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
1715 boolean retVal = it.getDataEnabled(subId);
1716 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1717 + " retVal=" + retVal);
1718 return retVal;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001719 } catch (RemoteException e) {
1720 throw e.rethrowFromSystemServer();
1721 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001722 }
Wink Saville36ffb042014-12-05 11:10:30 -08001723 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001724 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001725 }
1726
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001727 /**
Robert Greenwaltb2489872014-09-04 16:44:35 -07001728 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt6078b502014-06-11 16:05:07 -07001729 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001730 */
1731 public interface OnNetworkActiveListener {
1732 /**
1733 * Called on the main thread of the process to report that the current data network
1734 * has become active, and it is now a good time to perform any pending network
1735 * operations. Note that this listener only tells you when the network becomes
1736 * active; if at any other time you want to know whether it is active (and thus okay
1737 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001738 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001739 */
1740 public void onNetworkActive();
1741 }
1742
1743 private INetworkManagementService getNetworkManagementService() {
1744 synchronized (this) {
1745 if (mNMService != null) {
1746 return mNMService;
1747 }
1748 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1749 mNMService = INetworkManagementService.Stub.asInterface(b);
1750 return mNMService;
1751 }
1752 }
1753
1754 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
1755 mNetworkActivityListeners
1756 = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>();
1757
1758 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001759 * Start listening to reports when the system's default data network is active, meaning it is
1760 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1761 * to determine the current state of the system's default network after registering the
1762 * listener.
1763 * <p>
1764 * If the process default network has been set with
Paul Jensen72db88e2015-03-10 10:54:12 -04001765 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt6078b502014-06-11 16:05:07 -07001766 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001767 *
1768 * @param l The listener to be told when the network is active.
1769 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001770 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001771 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1772 @Override
1773 public void onNetworkActive() throws RemoteException {
1774 l.onNetworkActive();
1775 }
1776 };
1777
1778 try {
1779 getNetworkManagementService().registerNetworkActivityListener(rl);
1780 mNetworkActivityListeners.put(l, rl);
1781 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001782 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001783 }
1784 }
1785
1786 /**
1787 * Remove network active listener previously registered with
Robert Greenwaltb2489872014-09-04 16:44:35 -07001788 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001789 *
1790 * @param l Previously registered listener.
1791 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001792 public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001793 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
1794 if (rl == null) {
1795 throw new IllegalArgumentException("Listener not registered: " + l);
1796 }
1797 try {
1798 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1799 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001800 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001801 }
1802 }
1803
1804 /**
1805 * Return whether the data network is currently active. An active network means that
1806 * it is currently in a high power state for performing data transmission. On some
1807 * types of networks, it may be expensive to move and stay in such a state, so it is
1808 * more power efficient to batch network traffic together when the radio is already in
1809 * this state. This method tells you whether right now is currently a good time to
1810 * initiate network traffic, as the network is already active.
1811 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001812 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001813 try {
1814 return getNetworkManagementService().isNetworkActive();
1815 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001816 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001817 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001818 }
1819
1820 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001821 * {@hide}
1822 */
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001823 public ConnectivityManager(Context context, IConnectivityManager service) {
1824 mContext = checkNotNull(context, "missing context");
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001825 mService = checkNotNull(service, "missing IConnectivityManager");
Paul Jensene0bef712014-12-10 15:12:18 -05001826 sInstance = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001827 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001828
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07001829 /** {@hide} */
1830 public static ConnectivityManager from(Context context) {
1831 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1832 }
1833
Lorenzo Colittid5427052015-10-15 16:29:00 +09001834 /** {@hide} */
1835 public static final void enforceChangePermission(Context context) {
1836 int uid = Binder.getCallingUid();
1837 Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
1838 .getPackageNameForUid(context, uid), true /* throwException */);
1839 }
1840
Robert Greenwaltedb47662014-09-16 17:54:19 -07001841 /** {@hide */
1842 public static final void enforceTetherChangePermission(Context context) {
1843 if (context.getResources().getStringArray(
1844 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
1845 // Have a provisioning app - must only let system apps (which check this app)
1846 // turn on tethering
1847 context.enforceCallingOrSelfPermission(
Jeremy Kleind42209d2015-12-28 15:11:58 -08001848 android.Manifest.permission.TETHER_PRIVILEGED, "ConnectivityService");
Robert Greenwaltedb47662014-09-16 17:54:19 -07001849 } else {
Billy Laua7238a32015-08-01 12:45:02 +01001850 int uid = Binder.getCallingUid();
Lorenzo Colittid5427052015-10-15 16:29:00 +09001851 Settings.checkAndNoteWriteSettingsOperation(context, uid, Settings
1852 .getPackageNameForUid(context, uid), true /* throwException */);
Robert Greenwaltedb47662014-09-16 17:54:19 -07001853 }
1854 }
1855
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001856 /**
Paul Jensene0bef712014-12-10 15:12:18 -05001857 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1858 * situations where a Context pointer is unavailable.
1859 * @hide
1860 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001861 @Deprecated
Paul Jensen72db88e2015-03-10 10:54:12 -04001862 static ConnectivityManager getInstanceOrNull() {
1863 return sInstance;
1864 }
1865
1866 /**
1867 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1868 * situations where a Context pointer is unavailable.
1869 * @hide
1870 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07001871 @Deprecated
Paul Jensen72db88e2015-03-10 10:54:12 -04001872 private static ConnectivityManager getInstance() {
1873 if (getInstanceOrNull() == null) {
Paul Jensene0bef712014-12-10 15:12:18 -05001874 throw new IllegalStateException("No ConnectivityManager yet constructed");
1875 }
Paul Jensen72db88e2015-03-10 10:54:12 -04001876 return getInstanceOrNull();
Paul Jensene0bef712014-12-10 15:12:18 -05001877 }
1878
1879 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001880 * Get the set of tetherable, available interfaces. This list is limited by
1881 * device configuration and current interface existence.
Paul Jensenb2748922015-05-06 11:10:18 -04001882 * <p>This method requires the caller to hold the permission
1883 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001884 *
1885 * @return an array of 0 or more Strings of tetherable interface names.
1886 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001887 * {@hide}
1888 */
1889 public String[] getTetherableIfaces() {
1890 try {
1891 return mService.getTetherableIfaces();
1892 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001893 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001894 }
1895 }
1896
1897 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001898 * Get the set of tethered interfaces.
Paul Jensenb2748922015-05-06 11:10:18 -04001899 * <p>This method requires the caller to hold the permission
1900 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001901 *
1902 * @return an array of 0 or more String of currently tethered interface names.
1903 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001904 * {@hide}
1905 */
1906 public String[] getTetheredIfaces() {
1907 try {
1908 return mService.getTetheredIfaces();
1909 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001910 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001911 }
1912 }
1913
1914 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001915 * Get the set of interface names which attempted to tether but
1916 * failed. Re-attempting to tether may cause them to reset to the Tethered
1917 * state. Alternatively, causing the interface to be destroyed and recreated
1918 * may cause them to reset to the available state.
1919 * {@link ConnectivityManager#getLastTetherError} can be used to get more
1920 * information on the cause of the errors.
Paul Jensenb2748922015-05-06 11:10:18 -04001921 * <p>This method requires the caller to hold the permission
1922 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001923 *
1924 * @return an array of 0 or more String indicating the interface names
1925 * which failed to tether.
1926 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001927 * {@hide}
1928 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001929 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001930 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001931 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001932 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001933 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001934 }
1935 }
1936
1937 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001938 * Get the set of tethered dhcp ranges.
1939 *
1940 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
1941 * {@hide}
1942 */
1943 public String[] getTetheredDhcpRanges() {
1944 try {
1945 return mService.getTetheredDhcpRanges();
1946 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001947 throw e.rethrowFromSystemServer();
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001948 }
1949 }
1950
1951 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001952 * Attempt to tether the named interface. This will setup a dhcp server
1953 * on the interface, forward and NAT IP packets and forward DNS requests
1954 * to the best active upstream network interface. Note that if no upstream
1955 * IP network interface is available, dhcp will still run and traffic will be
1956 * allowed between the tethered devices and this device, though upstream net
1957 * access will of course fail until an upstream network interface becomes
1958 * active.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001959 *
1960 * <p>This method requires the caller to hold either the
1961 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1962 * or the ability to modify system settings as determined by
1963 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001964 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08001965 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
1966 * and WifiStateMachine which need direct access. All other clients should use
1967 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
1968 * logic.</p>
1969 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001970 * @param iface the interface name to tether.
1971 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1972 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001973 * {@hide}
1974 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001975 public int tether(String iface) {
1976 try {
1977 return mService.tether(iface);
1978 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001979 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001980 }
1981 }
1982
1983 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001984 * Stop tethering the named interface.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001985 *
1986 * <p>This method requires the caller to hold either the
1987 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1988 * or the ability to modify system settings as determined by
1989 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001990 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08001991 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
1992 * and WifiStateMachine which need direct access. All other clients should use
1993 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
1994 * logic.</p>
1995 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001996 * @param iface the interface name to untether.
1997 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1998 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08001999 * {@hide}
2000 */
2001 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002002 try {
2003 return mService.untether(iface);
2004 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002005 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08002006 }
2007 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002008
2009 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002010 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08002011 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002012 * due to device configuration.
Paul Jensenb2748922015-05-06 11:10:18 -04002013 * <p>This method requires the caller to hold the permission
2014 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002015 *
2016 * @return a boolean - {@code true} indicating Tethering is supported.
2017 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002018 * {@hide}
2019 */
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002020 @SystemApi
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002021 public boolean isTetheringSupported() {
2022 try {
2023 return mService.isTetheringSupported();
2024 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002025 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002026 }
2027 }
2028
2029 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002030 * Callback for use with {@link #startTethering} to find out whether tethering succeeded.
2031 * @hide
2032 */
2033 @SystemApi
2034 public static abstract class OnStartTetheringCallback {
2035 /**
2036 * Called when tethering has been successfully started.
2037 */
2038 public void onTetheringStarted() {};
2039
2040 /**
2041 * Called when starting tethering failed.
2042 */
2043 public void onTetheringFailed() {};
2044 }
2045
2046 /**
2047 * Convenient overload for
2048 * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null
2049 * handler to run on the current thread's {@link Looper}.
2050 * @hide
2051 */
2052 @SystemApi
2053 public void startTethering(int type, boolean showProvisioningUi,
2054 final OnStartTetheringCallback callback) {
2055 startTethering(type, showProvisioningUi, callback, null);
2056 }
2057
2058 /**
2059 * Runs tether provisioning for the given type if needed and then starts tethering if
2060 * the check succeeds. If no carrier provisioning is required for tethering, tethering is
2061 * enabled immediately. If provisioning fails, tethering will not be enabled. It also
2062 * schedules tether provisioning re-checks if appropriate.
2063 *
2064 * @param type The type of tethering to start. Must be one of
2065 * {@link ConnectivityManager.TETHERING_WIFI},
2066 * {@link ConnectivityManager.TETHERING_USB}, or
2067 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2068 * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there
2069 * is one. This should be true the first time this function is called and also any time
2070 * the user can see this UI. It gives users information from their carrier about the
2071 * check failing and how they can sign up for tethering if possible.
2072 * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller
2073 * of the result of trying to tether.
2074 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
2075 * @hide
2076 */
2077 @SystemApi
2078 public void startTethering(int type, boolean showProvisioningUi,
2079 final OnStartTetheringCallback callback, Handler handler) {
Jeremy Klein5f277e12016-03-12 16:29:54 -08002080 checkNotNull(callback, "OnStartTetheringCallback cannot be null.");
2081
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002082 ResultReceiver wrappedCallback = new ResultReceiver(handler) {
2083 @Override
2084 protected void onReceiveResult(int resultCode, Bundle resultData) {
2085 if (resultCode == TETHER_ERROR_NO_ERROR) {
2086 callback.onTetheringStarted();
2087 } else {
2088 callback.onTetheringFailed();
2089 }
2090 }
2091 };
Jeremy Klein5f277e12016-03-12 16:29:54 -08002092
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002093 try {
2094 mService.startTethering(type, wrappedCallback, showProvisioningUi);
2095 } catch (RemoteException e) {
2096 Log.e(TAG, "Exception trying to start tethering.", e);
2097 wrappedCallback.send(TETHER_ERROR_SERVICE_UNAVAIL, null);
2098 }
2099 }
2100
2101 /**
2102 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if
2103 * applicable.
2104 *
2105 * @param type The type of tethering to stop. Must be one of
2106 * {@link ConnectivityManager.TETHERING_WIFI},
2107 * {@link ConnectivityManager.TETHERING_USB}, or
2108 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2109 * @hide
2110 */
2111 @SystemApi
2112 public void stopTethering(int type) {
2113 try {
2114 mService.stopTethering(type);
2115 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002116 throw e.rethrowFromSystemServer();
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002117 }
2118 }
2119
2120 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002121 * Get the list of regular expressions that define any tetherable
2122 * USB network interfaces. If USB tethering is not supported by the
2123 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04002124 * <p>This method requires the caller to hold the permission
2125 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002126 *
2127 * @return an array of 0 or more regular expression Strings defining
2128 * what interfaces are considered tetherable usb interfaces.
2129 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002130 * {@hide}
2131 */
2132 public String[] getTetherableUsbRegexs() {
2133 try {
2134 return mService.getTetherableUsbRegexs();
2135 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002136 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002137 }
2138 }
2139
2140 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002141 * Get the list of regular expressions that define any tetherable
2142 * Wifi network interfaces. If Wifi tethering is not supported by the
2143 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04002144 * <p>This method requires the caller to hold the permission
2145 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002146 *
2147 * @return an array of 0 or more regular expression Strings defining
2148 * what interfaces are considered tetherable wifi interfaces.
2149 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002150 * {@hide}
2151 */
2152 public String[] getTetherableWifiRegexs() {
2153 try {
2154 return mService.getTetherableWifiRegexs();
2155 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002156 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002157 }
2158 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08002159
Danica Chang6fdd0c62010-08-11 14:54:43 -07002160 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002161 * Get the list of regular expressions that define any tetherable
2162 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
2163 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04002164 * <p>This method requires the caller to hold the permission
2165 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002166 *
2167 * @return an array of 0 or more regular expression Strings defining
2168 * what interfaces are considered tetherable bluetooth interfaces.
2169 *
Danica Chang6fdd0c62010-08-11 14:54:43 -07002170 * {@hide}
2171 */
2172 public String[] getTetherableBluetoothRegexs() {
2173 try {
2174 return mService.getTetherableBluetoothRegexs();
2175 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002176 throw e.rethrowFromSystemServer();
Danica Chang6fdd0c62010-08-11 14:54:43 -07002177 }
2178 }
2179
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002180 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002181 * Attempt to both alter the mode of USB and Tethering of USB. A
2182 * utility method to deal with some of the complexity of USB - will
2183 * attempt to switch to Rndis and subsequently tether the resulting
2184 * interface on {@code true} or turn off tethering and switch off
2185 * Rndis on {@code false}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002186 *
2187 * <p>This method requires the caller to hold either the
2188 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2189 * or the ability to modify system settings as determined by
2190 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002191 *
2192 * @param enable a boolean - {@code true} to enable tethering
2193 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2194 *
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002195 * {@hide}
2196 */
2197 public int setUsbTethering(boolean enable) {
2198 try {
2199 return mService.setUsbTethering(enable);
2200 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002201 throw e.rethrowFromSystemServer();
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002202 }
2203 }
2204
Robert Greenwalt5a735062010-03-02 17:25:02 -08002205 /** {@hide} */
2206 public static final int TETHER_ERROR_NO_ERROR = 0;
2207 /** {@hide} */
2208 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
2209 /** {@hide} */
2210 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
2211 /** {@hide} */
2212 public static final int TETHER_ERROR_UNSUPPORTED = 3;
2213 /** {@hide} */
2214 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
2215 /** {@hide} */
2216 public static final int TETHER_ERROR_MASTER_ERROR = 5;
2217 /** {@hide} */
2218 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
2219 /** {@hide} */
2220 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
2221 /** {@hide} */
2222 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
2223 /** {@hide} */
2224 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
2225 /** {@hide} */
2226 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002227 /** {@hide} */
2228 public static final int TETHER_ERROR_PROVISION_FAILED = 11;
Robert Greenwalt5a735062010-03-02 17:25:02 -08002229
2230 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002231 * Get a more detailed error code after a Tethering or Untethering
2232 * request asynchronously failed.
Paul Jensenb2748922015-05-06 11:10:18 -04002233 * <p>This method requires the caller to hold the permission
2234 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002235 *
2236 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08002237 * @return error The error code of the last error tethering or untethering the named
2238 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002239 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002240 * {@hide}
2241 */
2242 public int getLastTetherError(String iface) {
2243 try {
2244 return mService.getLastTetherError(iface);
2245 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002246 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002247 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002248 }
2249
2250 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002251 * Report network connectivity status. This is currently used only
2252 * to alter status bar UI.
Paul Jensenb2748922015-05-06 11:10:18 -04002253 * <p>This method requires the caller to hold the permission
2254 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002255 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002256 * @param networkType The type of network you want to report on
2257 * @param percentage The quality of the connection 0 is bad, 100 is good
2258 * {@hide}
2259 */
2260 public void reportInetCondition(int networkType, int percentage) {
2261 try {
2262 mService.reportInetCondition(networkType, percentage);
2263 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002264 throw e.rethrowFromSystemServer();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002265 }
2266 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002267
2268 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002269 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07002270 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002271 * the framework to re-evaluate network connectivity and/or switch to another
2272 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002273 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002274 * @param network The {@link Network} the application was attempting to use
2275 * or {@code null} to indicate the current default network.
Paul Jensenbfd17b72015-04-07 12:43:13 -04002276 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
2277 * working and non-working connectivity.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002278 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002279 @Deprecated
Robert Greenwalt9258c642014-03-26 16:47:06 -07002280 public void reportBadNetwork(Network network) {
2281 try {
Paul Jensenbfd17b72015-04-07 12:43:13 -04002282 // One of these will be ignored because it matches system's current state.
2283 // The other will trigger the necessary reevaluation.
2284 mService.reportNetworkConnectivity(network, true);
2285 mService.reportNetworkConnectivity(network, false);
2286 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002287 throw e.rethrowFromSystemServer();
Paul Jensenbfd17b72015-04-07 12:43:13 -04002288 }
2289 }
2290
2291 /**
2292 * Report to the framework whether a network has working connectivity.
2293 * This provides a hint to the system that a particular network is providing
2294 * working connectivity or not. In response the framework may re-evaluate
2295 * the network's connectivity and might take further action thereafter.
2296 *
2297 * @param network The {@link Network} the application was attempting to use
2298 * or {@code null} to indicate the current default network.
2299 * @param hasConnectivity {@code true} if the application was able to successfully access the
2300 * Internet using {@code network} or {@code false} if not.
2301 */
2302 public void reportNetworkConnectivity(Network network, boolean hasConnectivity) {
2303 try {
2304 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002305 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002306 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002307 }
2308 }
2309
2310 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002311 * Set a network-independent global http proxy. This is not normally what you want
2312 * for typical HTTP proxies - they are general network dependent. However if you're
2313 * doing something unusual like general internal filtering this may be useful. On
2314 * a private network where the proxy is not accessible, you may break HTTP using this.
Paul Jensen0d719ca2015-02-13 14:18:39 -05002315 * <p>This method requires the caller to hold the permission
Jason Monkfaf3fd52014-05-07 18:41:13 -04002316 * android.Manifest.permission#CONNECTIVITY_INTERNAL.
Paul Jensenb2748922015-05-06 11:10:18 -04002317 *
2318 * @param p A {@link ProxyInfo} object defining the new global
2319 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002320 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002321 */
Jason Monk207900c2014-04-25 15:00:09 -04002322 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002323 try {
2324 mService.setGlobalProxy(p);
2325 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002326 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002327 }
2328 }
2329
2330 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002331 * Retrieve any network-independent global HTTP proxy.
2332 *
Jason Monk207900c2014-04-25 15:00:09 -04002333 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002334 * if no global HTTP proxy is set.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002335 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002336 */
Jason Monk207900c2014-04-25 15:00:09 -04002337 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002338 try {
2339 return mService.getGlobalProxy();
2340 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002341 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002342 }
2343 }
2344
2345 /**
Paul Jensencee9b512015-05-06 07:32:40 -04002346 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
2347 * network-specific HTTP proxy. If {@code network} is null, the
2348 * network-specific proxy returned is the proxy of the default active
2349 * network.
2350 *
2351 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
2352 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
2353 * or when {@code network} is {@code null},
2354 * the {@code ProxyInfo} for the default active network. Returns
2355 * {@code null} when no proxy applies or the caller doesn't have
2356 * permission to use {@code network}.
2357 * @hide
2358 */
2359 public ProxyInfo getProxyForNetwork(Network network) {
2360 try {
2361 return mService.getProxyForNetwork(network);
2362 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002363 throw e.rethrowFromSystemServer();
Paul Jensencee9b512015-05-06 07:32:40 -04002364 }
2365 }
2366
2367 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002368 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
2369 * otherwise if this process is bound to a {@link Network} using
Paul Jensen72db88e2015-03-10 10:54:12 -04002370 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensene0bef712014-12-10 15:12:18 -05002371 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002372 *
Jason Monk207900c2014-04-25 15:00:09 -04002373 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002374 * HTTP proxy is active.
Robert Greenwalt434203a2010-10-11 16:00:27 -07002375 */
Paul Jensene0bef712014-12-10 15:12:18 -05002376 public ProxyInfo getDefaultProxy() {
Paul Jensencee9b512015-05-06 07:32:40 -04002377 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwalt434203a2010-10-11 16:00:27 -07002378 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07002379
2380 /**
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002381 * Returns true if the hardware supports the given network type
2382 * else it returns false. This doesn't indicate we have coverage
2383 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002384 * hardware supports it. For example a GSM phone without a SIM
2385 * should still return {@code true} for mobile data, but a wifi only
2386 * tablet would return {@code false}.
Paul Jensenb2748922015-05-06 11:10:18 -04002387 * <p>This method requires the caller to hold the permission
2388 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002389 *
2390 * @param networkType The network type we'd like to check
2391 * @return {@code true} if supported, else {@code false}
2392 *
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002393 * @hide
2394 */
2395 public boolean isNetworkSupported(int networkType) {
2396 try {
2397 return mService.isNetworkSupported(networkType);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002398 } catch (RemoteException e) {
2399 throw e.rethrowFromSystemServer();
2400 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002401 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002402
2403 /**
2404 * Returns if the currently active data network is metered. A network is
2405 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002406 * that connection due to monetary costs, data limitations or
2407 * battery/performance issues. You should check this before doing large
2408 * data transfers, and warn the user or delay the operation until another
2409 * network is available.
Paul Jensenb2748922015-05-06 11:10:18 -04002410 * <p>This method requires the caller to hold the permission
2411 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002412 *
2413 * @return {@code true} if large transfers should be avoided, otherwise
2414 * {@code false}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002415 */
2416 public boolean isActiveNetworkMetered() {
2417 try {
2418 return mService.isActiveNetworkMetered();
2419 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002420 throw e.rethrowFromSystemServer();
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002421 }
2422 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002423
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002424 /**
2425 * If the LockdownVpn mechanism is enabled, updates the vpn
2426 * with a reload of its profile.
2427 *
2428 * @return a boolean with {@code} indicating success
2429 *
2430 * <p>This method can only be called by the system UID
2431 * {@hide}
2432 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002433 public boolean updateLockdownVpn() {
2434 try {
2435 return mService.updateLockdownVpn();
2436 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002437 throw e.rethrowFromSystemServer();
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002438 }
2439 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07002440
2441 /**
Wink Saville948282b2013-08-29 08:55:16 -07002442 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07002443 *
Wink Savilleab9321d2013-06-29 21:10:57 -07002444 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07002445 *
2446 * @return time out that will be used, maybe less that suggestedTimeOutMs
2447 * -1 if an error.
2448 *
2449 * {@hide}
2450 */
Wink Saville948282b2013-08-29 08:55:16 -07002451 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07002452 int timeOutMs = -1;
2453 try {
Wink Saville948282b2013-08-29 08:55:16 -07002454 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07002455 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002456 throw e.rethrowFromSystemServer();
Wink Savilleab9321d2013-06-29 21:10:57 -07002457 }
2458 return timeOutMs;
2459 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002460
2461 /**
Wink Saville42d4f082013-07-20 20:31:59 -07002462 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002463 * {@hide}
2464 */
2465 public String getMobileProvisioningUrl() {
2466 try {
2467 return mService.getMobileProvisioningUrl();
2468 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002469 throw e.rethrowFromSystemServer();
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002470 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002471 }
Wink Saville42d4f082013-07-20 20:31:59 -07002472
2473 /**
Wink Saville948282b2013-08-29 08:55:16 -07002474 * Set sign in error notification to visible or in visible
2475 *
2476 * @param visible
2477 * @param networkType
2478 *
2479 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04002480 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville948282b2013-08-29 08:55:16 -07002481 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07002482 @Deprecated
Wink Saville948282b2013-08-29 08:55:16 -07002483 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04002484 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07002485 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04002486 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07002487 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002488 throw e.rethrowFromSystemServer();
Wink Saville948282b2013-08-29 08:55:16 -07002489 }
2490 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002491
2492 /**
2493 * Set the value for enabling/disabling airplane mode
Paul Jensenb2748922015-05-06 11:10:18 -04002494 * <p>This method requires the caller to hold the permission
2495 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002496 *
2497 * @param enable whether to enable airplane mode or not
2498 *
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002499 * @hide
2500 */
2501 public void setAirplaneMode(boolean enable) {
2502 try {
2503 mService.setAirplaneMode(enable);
2504 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002505 throw e.rethrowFromSystemServer();
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002506 }
2507 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002508
2509 /** {@hide} */
Robert Greenwalta67be032014-05-16 15:49:14 -07002510 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07002511 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07002512 mService.registerNetworkFactory(messenger, name);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002513 } catch (RemoteException e) {
2514 throw e.rethrowFromSystemServer();
2515 }
Robert Greenwalta67be032014-05-16 15:49:14 -07002516 }
2517
2518 /** {@hide} */
2519 public void unregisterNetworkFactory(Messenger messenger) {
2520 try {
2521 mService.unregisterNetworkFactory(messenger);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002522 } catch (RemoteException e) {
2523 throw e.rethrowFromSystemServer();
2524 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002525 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002526
Paul Jensen31a94f42015-02-13 14:18:39 -05002527 /**
2528 * @hide
2529 * Register a NetworkAgent with ConnectivityService.
2530 * @return NetID corresponding to NetworkAgent.
2531 */
2532 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002533 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002534 try {
Paul Jensen31a94f42015-02-13 14:18:39 -05002535 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
2536 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002537 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -05002538 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002539 }
2540
Robert Greenwalt9258c642014-03-26 16:47:06 -07002541 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002542 * Base class for NetworkRequest callbacks. Used for notifications about network
2543 * changes. Should be extended by applications wanting notifications.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002544 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002545 public static class NetworkCallback {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002546 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002547 * Called when the framework connects to a new network to evaluate whether it satisfies this
2548 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
2549 * callback. There is no guarantee that this new network will satisfy any requests, or that
2550 * the network will stay connected for longer than the time necessary to evaluate it.
2551 * <p>
2552 * Most applications <b>should not</b> act on this callback, and should instead use
2553 * {@link #onAvailable}. This callback is intended for use by applications that can assist
2554 * the framework in properly evaluating the network &mdash; for example, an application that
2555 * can automatically log in to a captive portal without user intervention.
2556 *
2557 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti66276122015-06-11 14:27:17 +09002558 *
2559 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002560 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002561 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002562
2563 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002564 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002565 * This callback may be called more than once if the {@link Network} that is
2566 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002567 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002568 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002569 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002570 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002571
2572 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002573 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002574 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002575 * for graceful handover. This may not be called if we have a hard loss
2576 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002577 * {@link NetworkCallback#onLost} call or a
2578 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002579 * on whether we lose or regain it.
2580 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002581 * @param network The {@link Network} that is about to be disconnected.
2582 * @param maxMsToLive The time in ms the framework will attempt to keep the
2583 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002584 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002585 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002586 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002587
2588 /**
2589 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002590 * graceful failure ends.
2591 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002592 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002593 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002594 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002595
2596 /**
2597 * Called if no network is found in the given timeout time. If no timeout is given,
Erik Kline57faba92015-11-25 12:49:38 +09002598 * this will not be called. The associated {@link NetworkRequest} will have already
2599 * been removed and released, as if {@link #unregisterNetworkCallback} had been called.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002600 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002601 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002602 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002603
2604 /**
2605 * Called when the network the framework connected to for this request
2606 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002607 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002608 * @param network The {@link Network} whose capabilities have changed.
Lorenzo Colittie285b432015-04-23 15:32:42 +09002609 * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002610 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002611 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002612 NetworkCapabilities networkCapabilities) {}
2613
2614 /**
2615 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002616 * changes {@link LinkProperties}.
2617 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002618 * @param network The {@link Network} whose link properties have changed.
2619 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002620 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002621 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002622
Robert Greenwalt8d482522015-06-24 13:23:42 -07002623 /**
2624 * Called when the network the framework connected to for this request
2625 * goes into {@link NetworkInfo.DetailedState.SUSPENDED}.
2626 * This generally means that while the TCP connections are still live,
2627 * temporarily network data fails to transfer. Specifically this is used
2628 * on cellular networks to mask temporary outages when driving through
2629 * a tunnel, etc.
2630 * @hide
2631 */
2632 public void onNetworkSuspended(Network network) {}
2633
2634 /**
2635 * Called when the network the framework connected to for this request
2636 * returns from a {@link NetworkInfo.DetailedState.SUSPENDED} state.
2637 * This should always be preceeded by a matching {@code onNetworkSuspended}
2638 * call.
2639 * @hide
2640 */
2641 public void onNetworkResumed(Network network) {}
2642
Robert Greenwalt6078b502014-06-11 16:05:07 -07002643 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002644 }
2645
Robert Greenwalt9258c642014-03-26 16:47:06 -07002646 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002647 /** @hide */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002648 public static final int CALLBACK_PRECHECK = BASE + 1;
2649 /** @hide */
2650 public static final int CALLBACK_AVAILABLE = BASE + 2;
2651 /** @hide arg1 = TTL */
2652 public static final int CALLBACK_LOSING = BASE + 3;
2653 /** @hide */
2654 public static final int CALLBACK_LOST = BASE + 4;
2655 /** @hide */
2656 public static final int CALLBACK_UNAVAIL = BASE + 5;
2657 /** @hide */
2658 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2659 /** @hide */
2660 public static final int CALLBACK_IP_CHANGED = BASE + 7;
2661 /** @hide */
2662 public static final int CALLBACK_RELEASED = BASE + 8;
2663 /** @hide */
2664 public static final int CALLBACK_EXIT = BASE + 9;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002665 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002666 private static final int EXPIRE_LEGACY_REQUEST = BASE + 10;
2667 /** @hide */
2668 public static final int CALLBACK_SUSPENDED = BASE + 11;
2669 /** @hide */
2670 public static final int CALLBACK_RESUMED = BASE + 12;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002671
Erik Kline57faba92015-11-25 12:49:38 +09002672 /** @hide */
2673 public static String getCallbackName(int whichCallback) {
2674 switch (whichCallback) {
2675 case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK";
2676 case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE";
2677 case CALLBACK_LOSING: return "CALLBACK_LOSING";
2678 case CALLBACK_LOST: return "CALLBACK_LOST";
2679 case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL";
2680 case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED";
2681 case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED";
2682 case CALLBACK_RELEASED: return "CALLBACK_RELEASED";
2683 case CALLBACK_EXIT: return "CALLBACK_EXIT";
2684 case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST";
2685 case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED";
2686 case CALLBACK_RESUMED: return "CALLBACK_RESUMED";
2687 default:
2688 return Integer.toString(whichCallback);
2689 }
2690 }
2691
Robert Greenwalt562cc542014-05-15 18:07:26 -07002692 private class CallbackHandler extends Handler {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002693 private final HashMap<NetworkRequest, NetworkCallback>mCallbackMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002694 private final AtomicInteger mRefCount;
2695 private static final String TAG = "ConnectivityManager.CallbackHandler";
2696 private final ConnectivityManager mCm;
Robert Greenwalta9ebeef2015-09-03 16:41:45 -07002697 private static final boolean DBG = false;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002698
Robert Greenwalt6078b502014-06-11 16:05:07 -07002699 CallbackHandler(Looper looper, HashMap<NetworkRequest, NetworkCallback>callbackMap,
Robert Greenwalt9258c642014-03-26 16:47:06 -07002700 AtomicInteger refCount, ConnectivityManager cm) {
2701 super(looper);
2702 mCallbackMap = callbackMap;
2703 mRefCount = refCount;
2704 mCm = cm;
2705 }
2706
2707 @Override
2708 public void handleMessage(Message message) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002709 NetworkRequest request = (NetworkRequest) getObject(message, NetworkRequest.class);
2710 Network network = (Network) getObject(message, Network.class);
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09002711 if (DBG) {
2712 Log.d(TAG, whatToString(message.what) + " for network " + network);
2713 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002714 switch (message.what) {
2715 case CALLBACK_PRECHECK: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002716 NetworkCallback callback = getCallback(request, "PRECHECK");
2717 if (callback != null) {
2718 callback.onPreCheck(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002719 }
2720 break;
2721 }
2722 case CALLBACK_AVAILABLE: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002723 NetworkCallback callback = getCallback(request, "AVAILABLE");
2724 if (callback != null) {
2725 callback.onAvailable(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002726 }
2727 break;
2728 }
2729 case CALLBACK_LOSING: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002730 NetworkCallback callback = getCallback(request, "LOSING");
2731 if (callback != null) {
2732 callback.onLosing(network, message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002733 }
2734 break;
2735 }
2736 case CALLBACK_LOST: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002737 NetworkCallback callback = getCallback(request, "LOST");
2738 if (callback != null) {
2739 callback.onLost(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002740 }
2741 break;
2742 }
2743 case CALLBACK_UNAVAIL: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002744 NetworkCallback callback = getCallback(request, "UNAVAIL");
2745 if (callback != null) {
2746 callback.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002747 }
2748 break;
2749 }
2750 case CALLBACK_CAP_CHANGED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002751 NetworkCallback callback = getCallback(request, "CAP_CHANGED");
2752 if (callback != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002753 NetworkCapabilities cap = (NetworkCapabilities)getObject(message,
2754 NetworkCapabilities.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002755
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002756 callback.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002757 }
2758 break;
2759 }
2760 case CALLBACK_IP_CHANGED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002761 NetworkCallback callback = getCallback(request, "IP_CHANGED");
2762 if (callback != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002763 LinkProperties lp = (LinkProperties)getObject(message,
2764 LinkProperties.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002765
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002766 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002767 }
2768 break;
2769 }
Robert Greenwalt8d482522015-06-24 13:23:42 -07002770 case CALLBACK_SUSPENDED: {
2771 NetworkCallback callback = getCallback(request, "SUSPENDED");
2772 if (callback != null) {
2773 callback.onNetworkSuspended(network);
2774 }
2775 break;
2776 }
2777 case CALLBACK_RESUMED: {
2778 NetworkCallback callback = getCallback(request, "RESUMED");
2779 if (callback != null) {
2780 callback.onNetworkResumed(network);
2781 }
2782 break;
2783 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002784 case CALLBACK_RELEASED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002785 NetworkCallback callback = null;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002786 synchronized(mCallbackMap) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002787 callback = mCallbackMap.remove(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002788 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002789 if (callback != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002790 synchronized(mRefCount) {
2791 if (mRefCount.decrementAndGet() == 0) {
2792 getLooper().quit();
2793 }
2794 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002795 } else {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002796 Log.e(TAG, "callback not found for RELEASED message");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002797 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002798 break;
2799 }
2800 case CALLBACK_EXIT: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002801 Log.d(TAG, "Listener quitting");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002802 getLooper().quit();
2803 break;
2804 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07002805 case EXPIRE_LEGACY_REQUEST: {
2806 expireRequest((NetworkCapabilities)message.obj, message.arg1);
2807 break;
2808 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002809 }
2810 }
2811
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002812 private Object getObject(Message msg, Class c) {
2813 return msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07002814 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002815
2816 private NetworkCallback getCallback(NetworkRequest req, String name) {
2817 NetworkCallback callback;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002818 synchronized(mCallbackMap) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002819 callback = mCallbackMap.get(req);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002820 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002821 if (callback == null) {
2822 Log.e(TAG, "callback not found for " + name + " message");
2823 }
2824 return callback;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002825 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002826 }
2827
Robert Greenwalt6078b502014-06-11 16:05:07 -07002828 private void incCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002829 synchronized(sCallbackRefCount) {
2830 if (sCallbackRefCount.incrementAndGet() == 1) {
Pierre Imaib290c0a2016-02-19 16:26:01 +09002831 // TODO: switch this to ConnectivityThread
Robert Greenwalt9258c642014-03-26 16:47:06 -07002832 HandlerThread callbackThread = new HandlerThread("ConnectivityManager");
2833 callbackThread.start();
2834 sCallbackHandler = new CallbackHandler(callbackThread.getLooper(),
Robert Greenwalt6078b502014-06-11 16:05:07 -07002835 sNetworkCallback, sCallbackRefCount, this);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002836 }
2837 }
2838 }
2839
Robert Greenwalt6078b502014-06-11 16:05:07 -07002840 private void decCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002841 synchronized(sCallbackRefCount) {
2842 if (sCallbackRefCount.decrementAndGet() == 0) {
2843 sCallbackHandler.obtainMessage(CALLBACK_EXIT).sendToTarget();
2844 sCallbackHandler = null;
2845 }
2846 }
2847 }
2848
Robert Greenwalt6078b502014-06-11 16:05:07 -07002849 static final HashMap<NetworkRequest, NetworkCallback> sNetworkCallback =
2850 new HashMap<NetworkRequest, NetworkCallback>();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002851 static final AtomicInteger sCallbackRefCount = new AtomicInteger(0);
2852 static CallbackHandler sCallbackHandler = null;
2853
2854 private final static int LISTEN = 1;
2855 private final static int REQUEST = 2;
2856
Robert Greenwalt562cc542014-05-15 18:07:26 -07002857 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need,
Erik Kline57faba92015-11-25 12:49:38 +09002858 NetworkCallback networkCallback, int timeoutMs, int action,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07002859 int legacyType) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002860 if (networkCallback == null) {
2861 throw new IllegalArgumentException("null NetworkCallback");
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002862 }
Erik Klinea2d29402016-03-16 15:31:39 +09002863 if (need == null && action != REQUEST) {
2864 throw new IllegalArgumentException("null NetworkCapabilities");
2865 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002866 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002867 incCallbackHandlerRefCount();
Paul Jensen7221cc32014-06-27 11:05:32 -04002868 synchronized(sNetworkCallback) {
2869 if (action == LISTEN) {
2870 networkCallback.networkRequest = mService.listenForNetwork(need,
2871 new Messenger(sCallbackHandler), new Binder());
2872 } else {
2873 networkCallback.networkRequest = mService.requestNetwork(need,
Erik Kline57faba92015-11-25 12:49:38 +09002874 new Messenger(sCallbackHandler), timeoutMs, new Binder(), legacyType);
Paul Jensen7221cc32014-06-27 11:05:32 -04002875 }
2876 if (networkCallback.networkRequest != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002877 sNetworkCallback.put(networkCallback.networkRequest, networkCallback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002878 }
2879 }
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002880 } catch (RemoteException e) {
2881 throw e.rethrowFromSystemServer();
2882 }
Robert Greenwalt6078b502014-06-11 16:05:07 -07002883 if (networkCallback.networkRequest == null) decCallbackHandlerRefCount();
2884 return networkCallback.networkRequest;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002885 }
2886
2887 /**
Erik Klinea2d29402016-03-16 15:31:39 +09002888 * Helper function to request a network with a particular legacy type.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002889 *
2890 * This is temporarily public @hide so it can be called by system code that uses the
2891 * NetworkRequest API to request networks but relies on CONNECTIVITY_ACTION broadcasts for
2892 * instead network notifications.
2893 *
2894 * TODO: update said system code to rely on NetworkCallbacks and make this method private.
2895 *
2896 * @hide
2897 */
Lorenzo Colittid1179462015-11-25 15:47:14 +09002898 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002899 int timeoutMs, int legacyType) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002900 sendRequestForNetwork(request.networkCapabilities, networkCallback, timeoutMs, REQUEST,
2901 legacyType);
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002902 }
2903
2904 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002905 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002906 *
2907 * This {@link NetworkRequest} will live until released via
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09002908 * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002909 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07002910 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002911 * can be used to direct traffic to the network.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002912 * <p>It is presently unsupported to request a network with mutable
2913 * {@link NetworkCapabilities} such as
2914 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2915 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
2916 * as these {@code NetworkCapabilities} represent states that a particular
2917 * network may never attain, and whether a network will attain these states
2918 * is unknown prior to bringing up the network so the framework does not
2919 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002920 *
2921 * <p>This method requires the caller to hold either the
2922 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2923 * or the ability to modify system settings as determined by
2924 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt9258c642014-03-26 16:47:06 -07002925 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002926 * @param request {@link NetworkRequest} describing this request.
2927 * @param networkCallback The {@link NetworkCallback} to be utilized for this
2928 * request. Note the callback must not be shared - they
2929 * uniquely specify this request.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002930 * @throws IllegalArgumentException if {@code request} specifies any mutable
2931 * {@code NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002932 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002933 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002934 requestNetwork(request, networkCallback, 0,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002935 inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002936 }
2937
2938 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002939 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
Robert Greenwalt9258c642014-03-26 16:47:06 -07002940 * by a timeout.
2941 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002942 * This function behaves identically to the non-timedout version, but if a suitable
Robert Greenwalt6078b502014-06-11 16:05:07 -07002943 * network is not found within the given time (in milliseconds) the
2944 * {@link NetworkCallback#unavailable} callback is called. The request must
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09002945 * still be released normally by calling {@link unregisterNetworkCallback(NetworkCallback)}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002946 *
2947 * <p>This method requires the caller to hold either the
2948 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2949 * or the ability to modify system settings as determined by
2950 * {@link android.provider.Settings.System#canWrite}.</p>
2951 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002952 * @param request {@link NetworkRequest} describing this request.
2953 * @param networkCallback The callbacks to be utilized for this request. Note
2954 * the callbacks must not be shared - they uniquely specify
2955 * this request.
2956 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
2957 * before {@link NetworkCallback#unavailable} is called.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002958 *
2959 * TODO: Make timeouts work and then unhide this method.
2960 *
Robert Greenwalt9258c642014-03-26 16:47:06 -07002961 * @hide
2962 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002963 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
2964 int timeoutMs) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002965 requestNetwork(request, networkCallback, timeoutMs,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002966 inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002967 }
2968
2969 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002970 * The maximum number of milliseconds the framework will look for a suitable network
Robert Greenwalt9258c642014-03-26 16:47:06 -07002971 * during a timeout-equiped call to {@link requestNetwork}.
2972 * {@hide}
2973 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002974 public final static int MAX_NETWORK_REQUEST_TIMEOUT_MS = 100 * 60 * 1000;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002975
2976 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002977 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002978 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002979 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfcde58f2015-02-11 16:51:13 -08002980 * <p>
Paul Jensen72db88e2015-03-10 10:54:12 -04002981 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
2982 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002983 */
Erik Kline90e93072014-11-19 12:12:24 +09002984 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002985
2986 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002987 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002988 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002989 * {@link android.content.Intent#getParcelableExtra(String)}.
2990 */
Erik Kline90e93072014-11-19 12:12:24 +09002991 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002992
2993
2994 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002995 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002996 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002997 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07002998 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002999 * the request may outlive the calling application and get called back when a suitable
3000 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003001 * <p>
3002 * The operation is an Intent broadcast that goes to a broadcast receiver that
3003 * you registered with {@link Context#registerReceiver} or through the
3004 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3005 * <p>
3006 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09003007 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3008 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07003009 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07003010 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07003011 * Intent to reserve the network or it will be released shortly after the Intent
3012 * is processed.
3013 * <p>
Paul Jensen694f2b82015-06-17 14:15:39 -04003014 * If there is already a request for this Intent registered (with the equality of
Robert Greenwalt9258c642014-03-26 16:47:06 -07003015 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07003016 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003017 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003018 * The request may be released normally by calling
3019 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003020 * <p>It is presently unsupported to request a network with either
3021 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3022 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
3023 * as these {@code NetworkCapabilities} represent states that a particular
3024 * network may never attain, and whether a network will attain these states
3025 * is unknown prior to bringing up the network so the framework does not
3026 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09003027 *
3028 * <p>This method requires the caller to hold either the
3029 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
3030 * or the ability to modify system settings as determined by
3031 * {@link android.provider.Settings.System#canWrite}.</p>
3032 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003033 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003034 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07003035 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003036 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04003037 * @throws IllegalArgumentException if {@code request} contains either
3038 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
3039 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003040 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003041 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003042 checkPendingIntent(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003043 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07003044 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003045 } catch (RemoteException e) {
3046 throw e.rethrowFromSystemServer();
3047 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003048 }
3049
3050 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003051 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
3052 * <p>
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003053 * This method has the same behavior as
3054 * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003055 * releasing network resources and disconnecting.
3056 *
3057 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3058 * PendingIntent passed to
3059 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
3060 * corresponding NetworkRequest you'd like to remove. Cannot be null.
3061 */
3062 public void releaseNetworkRequest(PendingIntent operation) {
3063 checkPendingIntent(operation);
3064 try {
3065 mService.releasePendingNetworkRequest(operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003066 } catch (RemoteException e) {
3067 throw e.rethrowFromSystemServer();
3068 }
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08003069 }
3070
3071 private void checkPendingIntent(PendingIntent intent) {
3072 if (intent == null) {
3073 throw new IllegalArgumentException("PendingIntent cannot be null.");
3074 }
3075 }
3076
3077 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07003078 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07003079 * {@link NetworkRequest}. The callbacks will continue to be called until
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003080 * either the application exits or link #unregisterNetworkCallback(NetworkCallback)} is called.
Paul Jensenb2748922015-05-06 11:10:18 -04003081 * <p>This method requires the caller to hold the permission
3082 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003083 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003084 * @param request {@link NetworkRequest} describing this request.
3085 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
3086 * networks change state.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003087 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003088 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
3089 sendRequestForNetwork(request.networkCapabilities, networkCallback, 0, LISTEN, TYPE_NONE);
Robert Greenwalt9258c642014-03-26 16:47:06 -07003090 }
3091
3092 /**
Paul Jensen694f2b82015-06-17 14:15:39 -04003093 * Registers a PendingIntent to be sent when a network is available which satisfies the given
3094 * {@link NetworkRequest}.
3095 *
3096 * This function behaves identically to the version that takes a NetworkCallback, but instead
3097 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
3098 * the request may outlive the calling application and get called back when a suitable
3099 * network is found.
3100 * <p>
3101 * The operation is an Intent broadcast that goes to a broadcast receiver that
3102 * you registered with {@link Context#registerReceiver} or through the
3103 * &lt;receiver&gt; tag in an AndroidManifest.xml file
3104 * <p>
3105 * The operation Intent is delivered with two extras, a {@link Network} typed
3106 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
3107 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
3108 * the original requests parameters.
3109 * <p>
3110 * If there is already a request for this Intent registered (with the equality of
3111 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
3112 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
3113 * <p>
3114 * The request may be released normally by calling
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003115 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Paul Jensen694f2b82015-06-17 14:15:39 -04003116 * <p>This method requires the caller to hold the permission
3117 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
3118 * @param request {@link NetworkRequest} describing this request.
3119 * @param operation Action to perform when the network is available (corresponds
3120 * to the {@link NetworkCallback#onAvailable} call. Typically
3121 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
3122 */
3123 public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) {
3124 checkPendingIntent(operation);
3125 try {
3126 mService.pendingListenForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003127 } catch (RemoteException e) {
3128 throw e.rethrowFromSystemServer();
3129 }
Paul Jensen694f2b82015-06-17 14:15:39 -04003130 }
3131
3132 /**
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003133 * Registers to receive notifications about changes in the system default network. The callbacks
3134 * will continue to be called until either the application exits or
3135 * {@link #unregisterNetworkCallback(NetworkCallback)} is called.
Erik Klinea2d29402016-03-16 15:31:39 +09003136 * <p>This method requires the caller to hold the permission
3137 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
3138 *
3139 * @param networkCallback The {@link NetworkCallback} that the system will call as the
3140 * system default network changes.
Erik Klinea2d29402016-03-16 15:31:39 +09003141 */
3142 public void registerDefaultNetworkCallback(NetworkCallback networkCallback) {
3143 // This works because if the NetworkCapabilities are null,
3144 // ConnectivityService takes them from the default request.
3145 //
3146 // Since the capabilities are exactly the same as the default request's
3147 // capabilities, this request is guaranteed, at all times, to be
3148 // satisfied by the same network, if any, that satisfies the default
3149 // request, i.e., the system default network.
3150 sendRequestForNetwork(null, networkCallback, 0, REQUEST, TYPE_NONE);
3151 }
3152
3153 /**
fengludb571472015-04-21 17:12:05 -07003154 * Requests bandwidth update for a given {@link Network} and returns whether the update request
3155 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
3156 * network connection for updated bandwidth information. The caller will be notified via
3157 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003158 * method assumes that the caller has previously called
3159 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network
3160 * changes.
fenglub15e72b2015-03-20 11:29:56 -07003161 *
fengluae519192015-04-27 14:28:04 -07003162 * @param network {@link Network} specifying which network you're interested.
fengludb571472015-04-21 17:12:05 -07003163 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglub15e72b2015-03-20 11:29:56 -07003164 */
fengludb571472015-04-21 17:12:05 -07003165 public boolean requestBandwidthUpdate(Network network) {
fenglub15e72b2015-03-20 11:29:56 -07003166 try {
fengludb571472015-04-21 17:12:05 -07003167 return mService.requestBandwidthUpdate(network);
fenglub15e72b2015-03-20 11:29:56 -07003168 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003169 throw e.rethrowFromSystemServer();
fenglub15e72b2015-03-20 11:29:56 -07003170 }
3171 }
3172
3173 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07003174 * Unregisters callbacks about and possibly releases networks originating from
Lorenzo Colitti88bc0bb2016-04-13 22:00:02 +09003175 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and
3176 * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls.
3177 * If the given {@code NetworkCallback} had previously been used with
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09003178 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
3179 * will be disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003180 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003181 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003182 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003183 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
3184 if (networkCallback == null || networkCallback.networkRequest == null ||
3185 networkCallback.networkRequest.requestId == REQUEST_ID_UNSET) {
3186 throw new IllegalArgumentException("Invalid NetworkCallback");
3187 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003188 try {
Hugo Benichi39e10e82016-07-07 09:36:12 +09003189 // CallbackHandler will release callback when receiving CALLBACK_RELEASED.
Robert Greenwalt6078b502014-06-11 16:05:07 -07003190 mService.releaseNetworkRequest(networkCallback.networkRequest);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003191 } catch (RemoteException e) {
3192 throw e.rethrowFromSystemServer();
3193 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003194 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003195
3196 /**
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003197 * Unregisters a callback previously registered via
3198 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3199 *
3200 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3201 * PendingIntent passed to
3202 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3203 * Cannot be null.
3204 */
3205 public void unregisterNetworkCallback(PendingIntent operation) {
3206 releaseNetworkRequest(operation);
3207 }
3208
3209 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003210 * Informs the system whether it should switch to {@code network} regardless of whether it is
3211 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
3212 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
3213 * the system default network regardless of any other network that's currently connected. If
3214 * {@code always} is true, then the choice is remembered, so that the next time the user
3215 * connects to this network, the system will switch to it.
3216 *
3217 * <p>This method requires the caller to hold the permission
3218 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
3219 *
3220 * @param network The network to accept.
3221 * @param accept Whether to accept the network even if unvalidated.
3222 * @param always Whether to remember this choice in the future.
3223 *
3224 * @hide
3225 */
3226 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
3227 try {
3228 mService.setAcceptUnvalidated(network, accept, always);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003229 } catch (RemoteException e) {
3230 throw e.rethrowFromSystemServer();
3231 }
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003232 }
3233
3234 /**
Stuart Scott984dc852015-03-30 13:17:11 -07003235 * Resets all connectivity manager settings back to factory defaults.
3236 * @hide
3237 */
3238 public void factoryReset() {
Stuart Scott984dc852015-03-30 13:17:11 -07003239 try {
Stuart Scottf1fb3972015-04-02 18:00:02 -07003240 mService.factoryReset();
Stuart Scott984dc852015-03-30 13:17:11 -07003241 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003242 throw e.rethrowFromSystemServer();
Stuart Scott984dc852015-03-30 13:17:11 -07003243 }
3244 }
3245
3246 /**
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003247 * Binds the current process to {@code network}. All Sockets created in the future
3248 * (and not explicitly bound via a bound SocketFactory from
3249 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3250 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3251 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3252 * work and all host name resolutions will fail. This is by design so an application doesn't
3253 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3254 * To clear binding pass {@code null} for {@code network}. Using individually bound
3255 * Sockets created by Network.getSocketFactory().createSocket() and
3256 * performing network-specific host name resolutions via
3257 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen72db88e2015-03-10 10:54:12 -04003258 * {@code bindProcessToNetwork}.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003259 *
3260 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3261 * the current binding.
3262 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3263 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003264 public boolean bindProcessToNetwork(Network network) {
3265 // Forcing callers to call thru non-static function ensures ConnectivityManager
3266 // instantiated.
3267 return setProcessDefaultNetwork(network);
3268 }
3269
3270 /**
3271 * Binds the current process to {@code network}. All Sockets created in the future
3272 * (and not explicitly bound via a bound SocketFactory from
3273 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3274 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3275 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3276 * work and all host name resolutions will fail. This is by design so an application doesn't
3277 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3278 * To clear binding pass {@code null} for {@code network}. Using individually bound
3279 * Sockets created by Network.getSocketFactory().createSocket() and
3280 * performing network-specific host name resolutions via
3281 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
3282 * {@code setProcessDefaultNetwork}.
3283 *
3284 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3285 * the current binding.
3286 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3287 * @deprecated This function can throw {@link IllegalStateException}. Use
3288 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
3289 * is a direct replacement.
3290 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003291 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003292 public static boolean setProcessDefaultNetwork(Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003293 int netId = (network == null) ? NETID_UNSET : network.netId;
Paul Jensen72db88e2015-03-10 10:54:12 -04003294 if (netId == NetworkUtils.getBoundNetworkForProcess()) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003295 return true;
3296 }
3297 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05003298 // Set HTTP proxy system properties to match network.
3299 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colittiec4c5552015-04-22 11:52:48 +09003300 try {
3301 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
3302 } catch (SecurityException e) {
3303 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
3304 Log.e(TAG, "Can't set proxy properties", e);
3305 }
Paul Jensenc91b5342014-08-27 12:38:45 -04003306 // Must flush DNS cache as new network may have different DNS resolutions.
3307 InetAddress.clearDnsCache();
3308 // Must flush socket pool as idle sockets will be bound to previous network and may
3309 // cause subsequent fetches to be performed on old network.
3310 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
3311 return true;
3312 } else {
3313 return false;
3314 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003315 }
3316
3317 /**
3318 * Returns the {@link Network} currently bound to this process via
Paul Jensen72db88e2015-03-10 10:54:12 -04003319 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003320 *
3321 * @return {@code Network} to which this process is bound, or {@code null}.
3322 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003323 public Network getBoundNetworkForProcess() {
3324 // Forcing callers to call thru non-static function ensures ConnectivityManager
3325 // instantiated.
3326 return getProcessDefaultNetwork();
3327 }
3328
3329 /**
3330 * Returns the {@link Network} currently bound to this process via
3331 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
3332 *
3333 * @return {@code Network} to which this process is bound, or {@code null}.
3334 * @deprecated Using this function can lead to other functions throwing
3335 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
3336 * {@code getBoundNetworkForProcess} is a direct replacement.
3337 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003338 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003339 public static Network getProcessDefaultNetwork() {
Paul Jensen72db88e2015-03-10 10:54:12 -04003340 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04003341 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003342 return new Network(netId);
3343 }
3344
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003345 private void unsupportedStartingFrom(int version) {
3346 if (Process.myUid() == Process.SYSTEM_UID) {
3347 // The getApplicationInfo() call we make below is not supported in system context, and
3348 // we want to allow the system to use these APIs anyway.
3349 return;
3350 }
3351
3352 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
3353 throw new UnsupportedOperationException(
3354 "This method is not supported in target SDK version " + version + " and above");
3355 }
3356 }
3357
3358 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
3359 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
Lifu Tang30f95a72016-01-07 23:20:38 -08003360 // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003361 // remove these exemptions. Note that this check is not secure, and apps can still access these
3362 // functions by accessing ConnectivityService directly. However, it should be clear that doing
3363 // so is unsupported and may break in the future. http://b/22728205
3364 private void checkLegacyRoutingApiAccess() {
3365 if (mContext.checkCallingOrSelfPermission("com.android.permission.INJECT_OMADM_SETTINGS")
3366 == PackageManager.PERMISSION_GRANTED) {
3367 return;
3368 }
3369
Dianne Hackborn692a2442015-07-31 10:35:34 -07003370 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003371 }
3372
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003373 /**
3374 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen72db88e2015-03-10 10:54:12 -04003375 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003376 *
3377 * @param network The {@link Network} to bind host resolutions from the current process to, or
3378 * {@code null} to clear the current binding.
3379 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3380 * @hide
3381 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
3382 */
Aurimas Liutikas514c5ef2016-05-24 15:22:55 -07003383 @Deprecated
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003384 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04003385 return NetworkUtils.bindProcessToNetworkForHostResolution(
3386 network == null ? NETID_UNSET : network.netId);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003387 }
Felipe Leme1b103232016-01-22 09:44:57 -08003388
3389 /**
3390 * Device is not restricting metered network activity while application is running on
3391 * background.
3392 */
3393 public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1;
3394
3395 /**
3396 * Device is restricting metered network activity while application is running on background,
3397 * but application is allowed to bypass it.
3398 * <p>
3399 * In this state, application should take action to mitigate metered network access.
3400 * For example, a music streaming application should switch to a low-bandwidth bitrate.
3401 */
3402 public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2;
3403
3404 /**
3405 * Device is restricting metered network activity while application is running on background.
Felipe Leme9778f762016-01-27 14:46:39 -08003406 * <p>
Felipe Leme1b103232016-01-22 09:44:57 -08003407 * In this state, application should not try to use the network while running on background,
3408 * because it would be denied.
3409 */
3410 public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3;
3411
Felipe Leme9778f762016-01-27 14:46:39 -08003412 /**
3413 * A change in the background metered network activity restriction has occurred.
3414 * <p>
3415 * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction
3416 * applies to them.
3417 * <p>
3418 * This is only sent to registered receivers, not manifest receivers.
3419 */
3420 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
3421 public static final String ACTION_RESTRICT_BACKGROUND_CHANGED =
3422 "android.net.conn.RESTRICT_BACKGROUND_CHANGED";
3423
Felipe Lemeecfccea2016-01-25 11:48:04 -08003424 /** @hide */
3425 @Retention(RetentionPolicy.SOURCE)
Felipe Leme1b103232016-01-22 09:44:57 -08003426 @IntDef(flag = false, value = {
3427 RESTRICT_BACKGROUND_STATUS_DISABLED,
3428 RESTRICT_BACKGROUND_STATUS_WHITELISTED,
3429 RESTRICT_BACKGROUND_STATUS_ENABLED,
3430 })
Felipe Leme1b103232016-01-22 09:44:57 -08003431 public @interface RestrictBackgroundStatus {
3432 }
3433
3434 private INetworkPolicyManager getNetworkPolicyManager() {
3435 synchronized (this) {
3436 if (mNPManager != null) {
3437 return mNPManager;
3438 }
3439 mNPManager = INetworkPolicyManager.Stub.asInterface(ServiceManager
3440 .getService(Context.NETWORK_POLICY_SERVICE));
3441 return mNPManager;
3442 }
3443 }
3444
3445 /**
3446 * Determines if the calling application is subject to metered network restrictions while
3447 * running on background.
Felipe Lemec9c7be52016-05-16 13:57:19 -07003448 *
3449 * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED},
3450 * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED},
3451 * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED}
Felipe Leme1b103232016-01-22 09:44:57 -08003452 */
3453 public @RestrictBackgroundStatus int getRestrictBackgroundStatus() {
3454 try {
3455 return getNetworkPolicyManager().getRestrictBackgroundByCaller();
3456 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003457 throw e.rethrowFromSystemServer();
Felipe Leme1b103232016-01-22 09:44:57 -08003458 }
3459 }
Andreas Gampe34802132016-04-20 14:33:51 -07003460
3461 /**
3462 * A holder class for debug info (mapping CALLBACK values to field names). This is stored
3463 * in a holder for two reasons:
3464 * 1) The reflection necessary to establish the map can't be run at compile-time. Thus, this
3465 * code will make the enclosing class not compile-time initializeable, deferring its
3466 * initialization to zygote startup. This leads to dirty (but shared) memory.
3467 * As this is debug info, use a holder that isn't initialized by default. This way the map
3468 * will be created on demand, while ConnectivityManager can be compile-time initialized.
3469 * 2) Static initialization is still preferred for its strong thread safety guarantees without
3470 * requiring a lock.
3471 */
3472 private static class NoPreloadHolder {
3473 public static final SparseArray<String> sMagicDecoderRing = MessageUtils.findMessageNames(
3474 new Class[]{ConnectivityManager.class}, new String[]{"CALLBACK_"});
3475 }
3476
3477 static {
3478 // When debug is enabled, aggressively initialize the holder by touching the field (which
3479 // will guarantee static initialization).
3480 if (CallbackHandler.DBG) {
3481 Object dummy = NoPreloadHolder.sMagicDecoderRing;
3482 }
3483 }
3484
3485 private static final String whatToString(int what) {
3486 return NoPreloadHolder.sMagicDecoderRing.get(what, Integer.toString(what));
3487 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003488}