blob: 5f1043ba8f1ca654bada4029429f559f10e1d685 [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
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +090084 private static final SparseArray<String> sMagicDecoderRing = MessageUtils.findMessageNames(
85 new Class[]{ConnectivityManager.class}, new String[]{"CALLBACK_"});
86
87 private static final String whatToString(int what) {
88 return sMagicDecoderRing.get(what, Integer.toString(what));
89 }
90
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070092 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093 * been established or lost. The NetworkInfo for the affected network is
94 * sent as an extra; it should be consulted to see what kind of
95 * connectivity event occurred.
96 * <p/>
97 * If this is a connection that was the result of failing over from a
98 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
99 * set to true.
100 * <p/>
101 * For a loss of connectivity, if the connectivity manager is attempting
102 * to connect (or has already connected) to another network, the
103 * NetworkInfo for the new network is also passed as an extra. This lets
104 * any receivers of the broadcast know that they should not necessarily
105 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800106 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800107 * the failover attempt succeeded (and so there is still overall data
108 * connectivity), or that the failover attempt failed, meaning that all
109 * connectivity has been lost.
110 * <p/>
111 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
112 * is set to {@code true} if there are no connected networks at all.
113 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800114 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 /**
Robert Greenwalte94a6ff2015-09-01 08:23:04 -0700118 * A temporary hack until SUPL system can get off the legacy APIS.
119 * They do too many network requests and the long list of apps listening
120 * and waking due to the CONNECTIVITY_ACTION bcast makes it expensive.
121 * Use this bcast intent instead for SUPL requests.
122 * @hide
123 */
124 public static final String CONNECTIVITY_ACTION_SUPL =
125 "android.net.conn.CONNECTIVITY_CHANGE_SUPL";
126
127 /**
Paul Jensen25a217c2015-02-27 22:55:47 -0500128 * The device has connected to a network that has presented a captive
129 * portal, which is blocking Internet connectivity. The user was presented
130 * with a notification that network sign in is required,
131 * and the user invoked the notification's action indicating they
Paul Jensen49e3edf2015-05-22 10:50:39 -0400132 * desire to sign in to the network. Apps handling this activity should
Paul Jensen25a217c2015-02-27 22:55:47 -0500133 * facilitate signing in to the network. This action includes a
134 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents
135 * the network presenting the captive portal; all communication with the
136 * captive portal must be done using this {@code Network} object.
137 * <p/>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400138 * This activity includes a {@link CaptivePortal} extra named
139 * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different
140 * outcomes of the captive portal sign in to the system:
141 * <ul>
142 * <li> When the app handling this action believes the user has signed in to
143 * the network and the captive portal has been dismissed, the app should
144 * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can
145 * reevaluate the network. If reevaluation finds the network no longer
146 * subject to a captive portal, the network may become the default active
147 * data network. </li>
148 * <li> When the app handling this action believes the user explicitly wants
Paul Jensen25a217c2015-02-27 22:55:47 -0500149 * to ignore the captive portal and the network, the app should call
Paul Jensen49e3edf2015-05-22 10:50:39 -0400150 * {@link CaptivePortal#ignoreNetwork}. </li>
151 * </ul>
Paul Jensen25a217c2015-02-27 22:55:47 -0500152 */
153 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
154 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL";
155
156 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 * The lookup key for a {@link NetworkInfo} object. Retrieve with
158 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700159 *
160 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
161 * should always obtain network information through
Paul Jensen3541e9f2015-03-18 12:23:02 -0400162 * {@link #getActiveNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700163 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700165 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700167
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700169 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700170 *
171 * @see android.content.Intent#getIntExtra(String, int)
172 */
173 public static final String EXTRA_NETWORK_TYPE = "networkType";
174
175 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176 * The lookup key for a boolean that indicates whether a connect event
177 * is for a network to which the connectivity manager was failing over
178 * following a disconnect on another network.
179 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
180 */
181 public static final String EXTRA_IS_FAILOVER = "isFailover";
182 /**
183 * The lookup key for a {@link NetworkInfo} object. This is supplied when
184 * there is another network that it may be possible to connect to. Retrieve with
185 * {@link android.content.Intent#getParcelableExtra(String)}.
186 */
187 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
188 /**
189 * The lookup key for a boolean that indicates whether there is a
190 * complete lack of connectivity, i.e., no network is available.
191 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
192 */
193 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
194 /**
195 * The lookup key for a string that indicates why an attempt to connect
196 * to a network failed. The string has no particular structure. It is
197 * intended to be used in notifications presented to users. Retrieve
198 * it with {@link android.content.Intent#getStringExtra(String)}.
199 */
200 public static final String EXTRA_REASON = "reason";
201 /**
202 * The lookup key for a string that provides optionally supplied
203 * extra information about the network state. The information
204 * may be passed up from the lower networking layers, and its
205 * meaning may be specific to a particular network type. Retrieve
206 * it with {@link android.content.Intent#getStringExtra(String)}.
207 */
208 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700209 /**
210 * The lookup key for an int that provides information about
211 * our connection to the internet at large. 0 indicates no connection,
212 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700213 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700214 * {@hide}
215 */
216 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 /**
Paul Jensen49e3edf2015-05-22 10:50:39 -0400218 * The lookup key for a {@link CaptivePortal} object included with the
219 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal}
220 * object can be used to either indicate to the system that the captive
221 * portal has been dismissed or that the user does not want to pursue
222 * signing in to captive portal. Retrieve it with
223 * {@link android.content.Intent#getParcelableExtra(String)}.
Paul Jensen25a217c2015-02-27 22:55:47 -0500224 */
Paul Jensen49e3edf2015-05-22 10:50:39 -0400225 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL";
Jan Nordqvist52eb29f2015-09-22 15:54:32 -0700226
227 /**
228 * Key for passing a URL to the captive portal login activity.
229 */
230 public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL";
231
Paul Jensen25a217c2015-02-27 22:55:47 -0500232 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700233 * Broadcast action to indicate the change of data activity status
234 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800235 * The network becomes active when data transmission is started, or
236 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700237 * {@hide}
238 */
239 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
240 public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
241 /**
242 * The lookup key for an enum that indicates the network device type on which this data activity
243 * change happens.
244 * {@hide}
245 */
246 public static final String EXTRA_DEVICE_TYPE = "deviceType";
247 /**
248 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
249 * it is actively sending or receiving data and {@code false} means it is idle.
250 * {@hide}
251 */
252 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700253 /**
254 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
255 * {@hide}
256 */
257 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700258
259 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800260 * Broadcast Action: The setting for background data usage has changed
261 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
262 * <p>
263 * If an application uses the network in the background, it should listen
264 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700265 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800266 * <p>
267 *
268 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
269 * of background data depends on several combined factors, and
270 * this broadcast is no longer sent. Instead, when background
271 * data is unavailable, {@link #getActiveNetworkInfo()} will now
272 * appear disconnected. During first boot after a platform
273 * upgrade, this broadcast will be sent once if
274 * {@link #getBackgroundDataSetting()} was {@code false} before
275 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800276 */
277 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800278 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800279 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
280 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
281
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700282 /**
283 * Broadcast Action: The network connection may not be good
284 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
285 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
286 * the network and it's condition.
287 * @hide
288 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800289 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700290 public static final String INET_CONDITION_ACTION =
291 "android.net.conn.INET_CONDITION_ACTION";
292
Robert Greenwalt42acef32009-08-12 16:08:25 -0700293 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800294 * Broadcast Action: A tetherable connection has come or gone.
295 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
296 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER} and
297 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
298 * the current state of tethering. Each include a list of
299 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800300 * @hide
301 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800302 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800303 public static final String ACTION_TETHER_STATE_CHANGED =
304 "android.net.conn.TETHER_STATE_CHANGED";
305
306 /**
307 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800308 * gives a String[] listing all the interfaces configured for
309 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800310 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800311 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800312
313 /**
314 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800315 * gives a String[] listing all the interfaces currently tethered
316 * (ie, has dhcp support and packets potentially forwarded/NATed)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800317 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800318 public static final String EXTRA_ACTIVE_TETHER = "activeArray";
319
320 /**
321 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800322 * gives a String[] listing all the interfaces we tried to tether and
323 * failed. Use {@link #getLastTetherError} to find the error code
324 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800325 */
326 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800327
328 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800329 * Broadcast Action: The captive portal tracker has finished its test.
330 * Sent only while running Setup Wizard, in lieu of showing a user
331 * notification.
332 * @hide
333 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800334 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800335 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
336 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
337 /**
338 * The lookup key for a boolean that indicates whether a captive portal was detected.
339 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
340 * @hide
341 */
342 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
343
344 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900345 * Action used to display a dialog that asks the user whether to connect to a network that is
346 * not validated. This intent is used to start the dialog in settings via startActivity.
347 *
348 * @hide
349 */
350 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED";
351
352 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -0800353 * Invalid tethering type.
354 * @see #startTethering(int, OnStartTetheringCallback, boolean)
355 * @hide
356 */
357 public static final int TETHERING_INVALID = -1;
358
359 /**
360 * Wifi tethering type.
361 * @see #startTethering(int, OnStartTetheringCallback, boolean)
362 * @hide
363 */
364 @SystemApi
365 public static final int TETHERING_WIFI = 0;
366
367 /**
368 * USB tethering type.
369 * @see #startTethering(int, OnStartTetheringCallback, boolean)
370 * @hide
371 */
372 @SystemApi
373 public static final int TETHERING_USB = 1;
374
375 /**
376 * Bluetooth tethering type.
377 * @see #startTethering(int, OnStartTetheringCallback, boolean)
378 * @hide
379 */
380 @SystemApi
381 public static final int TETHERING_BLUETOOTH = 2;
382
383 /**
384 * Extra used for communicating with the TetherService. Includes the type of tethering to
385 * enable if any.
386 * @hide
387 */
388 public static final String EXTRA_ADD_TETHER_TYPE = "extraAddTetherType";
389
390 /**
391 * Extra used for communicating with the TetherService. Includes the type of tethering for
392 * which to cancel provisioning.
393 * @hide
394 */
395 public static final String EXTRA_REM_TETHER_TYPE = "extraRemTetherType";
396
397 /**
398 * Extra used for communicating with the TetherService. True to schedule a recheck of tether
399 * provisioning.
400 * @hide
401 */
402 public static final String EXTRA_SET_ALARM = "extraSetAlarm";
403
404 /**
405 * Tells the TetherService to run a provision check now.
406 * @hide
407 */
408 public static final String EXTRA_RUN_PROVISION = "extraRunProvision";
409
410 /**
411 * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver}
412 * which will receive provisioning results. Can be left empty.
413 * @hide
414 */
415 public static final String EXTRA_PROVISION_CALLBACK = "extraProvisionCallback";
416
417 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800418 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700419 * @hide
420 */
421 public static final int TYPE_NONE = -1;
422
423 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800424 * The Mobile data connection. When active, all data traffic
425 * will use this network type's interface by default
426 * (it has a default route)
Robert Greenwalt42acef32009-08-12 16:08:25 -0700427 */
428 public static final int TYPE_MOBILE = 0;
429 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800430 * The WIFI data connection. When active, all data traffic
431 * will use this network type's interface by default
432 * (it has a default route).
Robert Greenwalt42acef32009-08-12 16:08:25 -0700433 */
434 public static final int TYPE_WIFI = 1;
435 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800436 * An MMS-specific Mobile data connection. This network type may use the
437 * same network interface as {@link #TYPE_MOBILE} or it may use a different
438 * one. This is used by applications needing to talk to the carrier's
439 * Multimedia Messaging Service servers.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900440 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900441 * @deprecated Applications should instead use
442 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900443 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700444 */
445 public static final int TYPE_MOBILE_MMS = 2;
446 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800447 * A SUPL-specific Mobile data connection. This network type may use the
448 * same network interface as {@link #TYPE_MOBILE} or it may use a different
449 * one. This is used by applications needing to talk to the carrier's
450 * Secure User Plane Location servers for help locating the device.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900451 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900452 * @deprecated Applications should instead use
453 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900454 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700455 */
456 public static final int TYPE_MOBILE_SUPL = 3;
457 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800458 * A DUN-specific Mobile data connection. This network type may use the
459 * same network interface as {@link #TYPE_MOBILE} or it may use a different
460 * one. This is sometimes by the system when setting up an upstream connection
461 * for tethering so that the carrier is aware of DUN traffic.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700462 */
463 public static final int TYPE_MOBILE_DUN = 4;
464 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800465 * A High Priority Mobile data connection. This network type uses the
466 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colittie285b432015-04-23 15:32:42 +0900467 * is different.
468 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900469 * @deprecated Applications should instead use
470 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900471 * uses the {@link NetworkCapabilities#TRANSPORT_CELLULAR} transport.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700472 */
473 public static final int TYPE_MOBILE_HIPRI = 5;
jsh8214deb2010-03-11 15:04:43 -0800474 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800475 * The WiMAX data connection. When active, all data traffic
476 * will use this network type's interface by default
477 * (it has a default route).
jsh8214deb2010-03-11 15:04:43 -0800478 */
479 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800480
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800481 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800482 * The Bluetooth data connection. When active, all data traffic
483 * will use this network type's interface by default
484 * (it has a default route).
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800485 */
486 public static final int TYPE_BLUETOOTH = 7;
487
Robert Greenwalt60810842011-04-22 15:28:18 -0700488 /**
489 * Dummy data connection. This should not be used on shipping devices.
490 */
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800491 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800492
Robert Greenwalt60810842011-04-22 15:28:18 -0700493 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800494 * The Ethernet data connection. When active, all data traffic
495 * will use this network type's interface by default
496 * (it has a default route).
Robert Greenwalt60810842011-04-22 15:28:18 -0700497 */
Robert Greenwalte12aec92011-01-28 14:48:37 -0800498 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700499
Wink Saville9d7d6282011-03-12 14:52:01 -0800500 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800501 * Over the air Administration.
Wink Saville9d7d6282011-03-12 14:52:01 -0800502 * {@hide}
503 */
504 public static final int TYPE_MOBILE_FOTA = 10;
505
506 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800507 * IP Multimedia Subsystem.
Wink Saville9d7d6282011-03-12 14:52:01 -0800508 * {@hide}
509 */
510 public static final int TYPE_MOBILE_IMS = 11;
511
512 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800513 * Carrier Branded Services.
Wink Saville9d7d6282011-03-12 14:52:01 -0800514 * {@hide}
515 */
516 public static final int TYPE_MOBILE_CBS = 12;
517
repo syncaea743a2011-07-29 23:55:49 -0700518 /**
519 * A Wi-Fi p2p connection. Only requesting processes will have access to
520 * the peers connected.
521 * {@hide}
522 */
523 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800524
Wink Saville5e56bc52013-07-29 15:00:57 -0700525 /**
526 * The network to use for initially attaching to the network
527 * {@hide}
528 */
529 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700530
Lorenzo Colittie285b432015-04-23 15:32:42 +0900531 /**
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700532 * Emergency PDN connection for emergency services. This
533 * may include IMS and MMS in emergency situations.
Ram3e0e3bc2014-06-26 11:03:44 -0700534 * {@hide}
535 */
536 public static final int TYPE_MOBILE_EMERGENCY = 15;
537
Hui Lu1c5624a2014-01-15 11:05:36 -0500538 /**
539 * The network that uses proxy to achieve connectivity.
540 * {@hide}
541 */
542 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700543
Robert Greenwalt8283f882014-07-07 17:09:01 -0700544 /**
545 * A virtual network using one or more native bearers.
546 * It may or may not be providing security services.
547 */
548 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500549
550 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700551 public static final int MAX_RADIO_TYPE = TYPE_VPN;
552
553 /** {@hide} */
554 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800555
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800556 /**
557 * If you want to set the default network preference,you can directly
558 * change the networkAttributes array in framework's config.xml.
559 *
560 * @deprecated Since we support so many more networks now, the single
561 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800562 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800563 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800564 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800565 * from an App.
566 */
567 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
569
Jeff Sharkey625239a2012-09-26 22:03:49 -0700570 /**
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700571 * @hide
572 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700573 public final static int REQUEST_ID_UNSET = 0;
574
Paul Jensen5d59e782014-07-11 12:28:19 -0400575 /**
576 * A NetID indicating no Network is selected.
577 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
578 * @hide
579 */
580 public static final int NETID_UNSET = 0;
581
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700582 private final IConnectivityManager mService;
Paul Jensene0bef712014-12-10 15:12:18 -0500583 /**
584 * A kludge to facilitate static access where a Context pointer isn't available, like in the
585 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
586 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
587 * methods that take a Context argument.
588 */
589 private static ConnectivityManager sInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590
Lorenzo Colittiffc42b02015-07-29 11:41:21 +0900591 private final Context mContext;
592
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800593 private INetworkManagementService mNMService;
Felipe Leme1b103232016-01-22 09:44:57 -0800594 private INetworkPolicyManager mNPManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800595
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800596 /**
597 * Tests if a given integer represents a valid network type.
598 * @param networkType the type to be tested
599 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensen9e59e122015-05-06 10:42:25 -0400600 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
601 * validate a network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800602 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700603 public static boolean isNetworkTypeValid(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700604 return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800605 }
606
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800607 /**
608 * Returns a non-localized string representing a given network type.
609 * ONLY used for debugging output.
610 * @param type the type needing naming
611 * @return a String for the given type, or a string version of the type ("87")
612 * if no name is known.
613 * {@hide}
614 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700615 public static String getNetworkTypeName(int type) {
616 switch (type) {
617 case TYPE_MOBILE:
618 return "MOBILE";
619 case TYPE_WIFI:
620 return "WIFI";
621 case TYPE_MOBILE_MMS:
622 return "MOBILE_MMS";
623 case TYPE_MOBILE_SUPL:
624 return "MOBILE_SUPL";
625 case TYPE_MOBILE_DUN:
626 return "MOBILE_DUN";
627 case TYPE_MOBILE_HIPRI:
628 return "MOBILE_HIPRI";
629 case TYPE_WIMAX:
630 return "WIMAX";
631 case TYPE_BLUETOOTH:
632 return "BLUETOOTH";
633 case TYPE_DUMMY:
634 return "DUMMY";
635 case TYPE_ETHERNET:
636 return "ETHERNET";
637 case TYPE_MOBILE_FOTA:
638 return "MOBILE_FOTA";
639 case TYPE_MOBILE_IMS:
640 return "MOBILE_IMS";
641 case TYPE_MOBILE_CBS:
642 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700643 case TYPE_WIFI_P2P:
644 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700645 case TYPE_MOBILE_IA:
646 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700647 case TYPE_MOBILE_EMERGENCY:
648 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500649 case TYPE_PROXY:
650 return "PROXY";
Erik Kline37fbfa12014-11-19 17:23:41 +0900651 case TYPE_VPN:
652 return "VPN";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700653 default:
654 return Integer.toString(type);
655 }
656 }
657
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800658 /**
659 * Checks if a given type uses the cellular data connection.
660 * This should be replaced in the future by a network property.
661 * @param networkType the type to check
662 * @return a boolean - {@code true} if uses cellular network, else {@code false}
663 * {@hide}
664 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700665 public static boolean isNetworkTypeMobile(int networkType) {
666 switch (networkType) {
667 case TYPE_MOBILE:
668 case TYPE_MOBILE_MMS:
669 case TYPE_MOBILE_SUPL:
670 case TYPE_MOBILE_DUN:
671 case TYPE_MOBILE_HIPRI:
672 case TYPE_MOBILE_FOTA:
673 case TYPE_MOBILE_IMS:
674 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700675 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700676 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700677 return true;
678 default:
679 return false;
680 }
681 }
682
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800683 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700684 * Checks if the given network type is backed by a Wi-Fi radio.
685 *
686 * @hide
687 */
688 public static boolean isNetworkTypeWifi(int networkType) {
689 switch (networkType) {
690 case TYPE_WIFI:
691 case TYPE_WIFI_P2P:
692 return true;
693 default:
694 return false;
695 }
696 }
697
698 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800699 * Specifies the preferred network type. When the device has more
700 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800701 *
702 * @param preference the network type to prefer over all others. It is
703 * unspecified what happens to the old preferred network in the
704 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700705 * @deprecated Functionality has been removed as it no longer makes sense,
706 * with many more than two networks - we'd need an array to express
707 * preference. Instead we use dynamic network properties of
708 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800709 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800710 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800711 }
712
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800713 /**
714 * Retrieves the current preferred network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400715 * <p>This method requires the caller to hold the permission
716 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800717 *
718 * @return an integer representing the preferred network type
719 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700720 * @deprecated Functionality has been removed as it no longer makes sense,
721 * with many more than two networks - we'd need an array to express
722 * preference. Instead we use dynamic network properties of
723 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800724 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800725 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700726 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800727 }
728
Scott Main671644c2011-10-06 19:02:28 -0700729 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800730 * Returns details about the currently active default data network. When
731 * connected, this network is the default route for outgoing connections.
732 * You should always check {@link NetworkInfo#isConnected()} before initiating
733 * network traffic. This may return {@code null} when there is no default
734 * network.
Paul Jensenb2748922015-05-06 11:10:18 -0400735 * <p>This method requires the caller to hold the permission
736 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800737 *
738 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500739 * or {@code null} if no default network is currently active
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700740 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 public NetworkInfo getActiveNetworkInfo() {
742 try {
743 return mService.getActiveNetworkInfo();
744 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700745 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 }
747 }
748
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800749 /**
Paul Jensen31a94f42015-02-13 14:18:39 -0500750 * Returns a {@link Network} object corresponding to the currently active
751 * default data network. In the event that the current active default data
752 * network disconnects, the returned {@code Network} object will no longer
753 * be usable. This will return {@code null} when there is no default
754 * network.
Paul Jensenb2748922015-05-06 11:10:18 -0400755 * <p>This method requires the caller to hold the permission
756 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Paul Jensen31a94f42015-02-13 14:18:39 -0500757 *
758 * @return a {@link Network} object for the current default network or
759 * {@code null} if no default network is currently active
Paul Jensen31a94f42015-02-13 14:18:39 -0500760 */
761 public Network getActiveNetwork() {
762 try {
763 return mService.getActiveNetwork();
764 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700765 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -0500766 }
767 }
768
769 /**
Robin Lee244ce8e2016-01-05 18:03:46 +0000770 * Configures an always-on VPN connection through a specific application.
771 * This connection is automatically granted and persisted after a reboot.
772 *
773 * <p>The designated package should declare a {@link VpnService} in its
774 * manifest guarded by {@link android.Manifest.permission.BIND_VPN_SERVICE},
775 * otherwise the call will fail.
776 *
777 * @param userId The identifier of the user to set an always-on VPN for.
778 * @param vpnPackage The package name for an installed VPN app on the device, or {@code null}
779 * to remove an existing always-on VPN configuration.
780
781 * @return {@code true} if the package is set as always-on VPN controller;
782 * {@code false} otherwise.
783 * @hide
784 */
785 public boolean setAlwaysOnVpnPackageForUser(int userId, @Nullable String vpnPackage) {
786 try {
787 return mService.setAlwaysOnVpnPackage(userId, vpnPackage);
788 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700789 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +0000790 }
791 }
792
793 /**
794 * Returns the package name of the currently set always-on VPN application.
795 * If there is no always-on VPN set, or the VPN is provided by the system instead
796 * of by an app, {@code null} will be returned.
797 *
798 * @return Package name of VPN controller responsible for always-on VPN,
799 * or {@code null} if none is set.
800 * @hide
801 */
802 public String getAlwaysOnVpnPackageForUser(int userId) {
803 try {
804 return mService.getAlwaysOnVpnPackage(userId);
805 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700806 throw e.rethrowFromSystemServer();
Robin Lee244ce8e2016-01-05 18:03:46 +0000807 }
808 }
809
810 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800811 * Returns details about the currently active default data network
812 * for a given uid. This is for internal use only to avoid spying
813 * other apps.
Paul Jensenb2748922015-05-06 11:10:18 -0400814 * <p>This method requires the caller to hold the permission
815 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800816 *
817 * @return a {@link NetworkInfo} object for the current default network
818 * for the given uid or {@code null} if no default network is
819 * available for the specified uid.
820 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800821 * {@hide}
822 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700823 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
824 try {
825 return mService.getActiveNetworkInfoForUid(uid);
826 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700827 throw e.rethrowFromSystemServer();
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700828 }
829 }
830
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800831 /**
832 * Returns connection status information about a particular
833 * network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400834 * <p>This method requires the caller to hold the permission
835 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800836 *
837 * @param networkType integer specifying which networkType in
838 * which you're interested.
839 * @return a {@link NetworkInfo} object for the requested
840 * network type or {@code null} if the type is not
841 * supported by the device.
842 *
Paul Jensen3541e9f2015-03-18 12:23:02 -0400843 * @deprecated This method does not support multiple connected networks
844 * of the same type. Use {@link #getAllNetworks} and
845 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800846 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800847 public NetworkInfo getNetworkInfo(int networkType) {
848 try {
849 return mService.getNetworkInfo(networkType);
850 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700851 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 }
853 }
854
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800855 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700856 * Returns connection status information about a particular
857 * Network.
Paul Jensenb2748922015-05-06 11:10:18 -0400858 * <p>This method requires the caller to hold the permission
859 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700860 *
861 * @param network {@link Network} specifying which network
862 * in which you're interested.
863 * @return a {@link NetworkInfo} object for the requested
864 * network or {@code null} if the {@code Network}
865 * is not valid.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700866 */
867 public NetworkInfo getNetworkInfo(Network network) {
868 try {
869 return mService.getNetworkInfoForNetwork(network);
870 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700871 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700872 }
873 }
874
875 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800876 * Returns connection status information about all network
877 * types supported by the device.
Paul Jensenb2748922015-05-06 11:10:18 -0400878 * <p>This method requires the caller to hold the permission
879 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800880 *
881 * @return an array of {@link NetworkInfo} objects. Check each
882 * {@link NetworkInfo#getType} for which type each applies.
883 *
Paul Jensen3541e9f2015-03-18 12:23:02 -0400884 * @deprecated This method does not support multiple connected networks
885 * of the same type. Use {@link #getAllNetworks} and
886 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800887 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800888 public NetworkInfo[] getAllNetworkInfo() {
889 try {
890 return mService.getAllNetworkInfo();
891 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700892 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800893 }
894 }
895
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800896 /**
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700897 * Returns the {@link Network} object currently serving a given type, or
898 * null if the given type is not connected.
899 *
900 * <p>This method requires the caller to hold the permission
901 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
902 *
903 * @hide
Paul Jensen3541e9f2015-03-18 12:23:02 -0400904 * @deprecated This method does not support multiple connected networks
905 * of the same type. Use {@link #getAllNetworks} and
906 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700907 */
908 public Network getNetworkForType(int networkType) {
909 try {
910 return mService.getNetworkForType(networkType);
911 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700912 throw e.rethrowFromSystemServer();
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700913 }
914 }
915
916 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700917 * Returns an array of all {@link Network} currently tracked by the
918 * framework.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700919 * <p>This method requires the caller to hold the permission
920 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Paul Jensenb2748922015-05-06 11:10:18 -0400921 *
922 * @return an array of {@link Network} objects.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700923 */
924 public Network[] getAllNetworks() {
925 try {
926 return mService.getAllNetworks();
927 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700928 throw e.rethrowFromSystemServer();
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700929 }
930 }
931
932 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +0900933 * Returns an array of {@link android.net.NetworkCapabilities} objects, representing
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900934 * the Networks that applications run by the given user will use by default.
935 * @hide
936 */
937 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
938 try {
939 return mService.getDefaultNetworkCapabilitiesForUser(userId);
940 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700941 throw e.rethrowFromSystemServer();
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900942 }
943 }
944
945 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800946 * Returns the IP information for the current default network.
Paul Jensenb2748922015-05-06 11:10:18 -0400947 * <p>This method requires the caller to hold the permission
948 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800949 *
950 * @return a {@link LinkProperties} object describing the IP info
951 * for the current default network, or {@code null} if there
952 * is no current default network.
953 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800954 * {@hide}
955 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700956 public LinkProperties getActiveLinkProperties() {
957 try {
958 return mService.getActiveLinkProperties();
959 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700960 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700961 }
962 }
963
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800964 /**
965 * Returns the IP information for a given network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400966 * <p>This method requires the caller to hold the permission
967 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800968 *
969 * @param networkType the network type of interest.
970 * @return a {@link LinkProperties} object describing the IP info
971 * for the given networkType, or {@code null} if there is
972 * no current default network.
973 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800974 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -0400975 * @deprecated This method does not support multiple connected networks
976 * of the same type. Use {@link #getAllNetworks},
977 * {@link #getNetworkInfo(android.net.Network)}, and
978 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800979 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700980 public LinkProperties getLinkProperties(int networkType) {
981 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -0700982 return mService.getLinkPropertiesForType(networkType);
983 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -0700984 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -0700985 }
986 }
987
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700988 /**
989 * Get the {@link LinkProperties} for the given {@link Network}. This
990 * will return {@code null} if the network is unknown.
Paul Jensenb2748922015-05-06 11:10:18 -0400991 * <p>This method requires the caller to hold the permission
992 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700993 *
994 * @param network The {@link Network} object identifying the network in question.
995 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensenb2748922015-05-06 11:10:18 -0400996 */
Robert Greenwalt9258c642014-03-26 16:47:06 -0700997 public LinkProperties getLinkProperties(Network network) {
998 try {
999 return mService.getLinkProperties(network);
1000 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001001 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001002 }
1003 }
1004
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001005 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09001006 * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001007 * will return {@code null} if the network is unknown.
Paul Jensenb2748922015-05-06 11:10:18 -04001008 * <p>This method requires the caller to hold the permission
1009 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001010 *
1011 * @param network The {@link Network} object identifying the network in question.
Lorenzo Colittie285b432015-04-23 15:32:42 +09001012 * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001013 */
Robert Greenwalt9258c642014-03-26 16:47:06 -07001014 public NetworkCapabilities getNetworkCapabilities(Network network) {
1015 try {
1016 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001017 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001018 throw e.rethrowFromSystemServer();
Robert Greenwaltd192dad2010-09-14 09:18:02 -07001019 }
1020 }
1021
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001022 /**
Udam Sainib7c24872016-01-04 12:16:14 -08001023 * Gets the URL that should be used for resolving whether a captive portal is present.
1024 * 1. This URL should respond with a 204 response to a GET request to indicate no captive
1025 * portal is present.
1026 * 2. This URL must be HTTP as redirect responses are used to find captive portal
1027 * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects.
1028 *
1029 * @hide
1030 */
1031 @SystemApi
1032 public String getCaptivePortalServerUrl() {
1033 try {
1034 return mService.getCaptivePortalServerUrl();
1035 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001036 throw e.rethrowFromSystemServer();
Udam Sainib7c24872016-01-04 12:16:14 -08001037 }
1038 }
1039
1040 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001041 * Tells the underlying networking system that the caller wants to
1042 * begin using the named feature. The interpretation of {@code feature}
1043 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001044 *
1045 * <p>This method requires the caller to hold either the
1046 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1047 * or the ability to modify system settings as determined by
1048 * {@link android.provider.Settings.System#canWrite}.</p>
1049 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001050 * @param networkType specifies which network the request pertains to
1051 * @param feature the name of the feature to be used
1052 * @return an integer value representing the outcome of the request.
1053 * The interpretation of this value is specific to each networking
1054 * implementation+feature combination, except that the value {@code -1}
1055 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001056 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001057 * @deprecated Deprecated in favor of the cleaner
1058 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001059 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001060 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001061 */
1062 public int startUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001063 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001064 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1065 if (netCap == null) {
1066 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
1067 feature);
1068 return PhoneConstants.APN_REQUEST_FAILED;
1069 }
1070
1071 NetworkRequest request = null;
1072 synchronized (sLegacyRequests) {
1073 LegacyRequest l = sLegacyRequests.get(netCap);
1074 if (l != null) {
1075 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
1076 renewRequestLocked(l);
1077 if (l.currentNetwork != null) {
1078 return PhoneConstants.APN_ALREADY_ACTIVE;
1079 } else {
1080 return PhoneConstants.APN_REQUEST_STARTED;
1081 }
1082 }
1083
1084 request = requestNetworkForFeatureLocked(netCap);
1085 }
1086 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -07001087 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001088 return PhoneConstants.APN_REQUEST_STARTED;
1089 } else {
1090 Log.d(TAG, " request Failed");
1091 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001092 }
1093 }
1094
1095 /**
1096 * Tells the underlying networking system that the caller is finished
1097 * using the named feature. The interpretation of {@code feature}
1098 * is completely up to each networking implementation.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001099 *
1100 * <p>This method requires the caller to hold either the
1101 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1102 * or the ability to modify system settings as determined by
1103 * {@link android.provider.Settings.System#canWrite}.</p>
1104 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001105 * @param networkType specifies which network the request pertains to
1106 * @param feature the name of the feature that is no longer needed
1107 * @return an integer value representing the outcome of the request.
1108 * The interpretation of this value is specific to each networking
1109 * implementation+feature combination, except that the value {@code -1}
1110 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001111 *
Robert Greenwalta36c0742015-07-28 11:41:31 -07001112 * @deprecated Deprecated in favor of the cleaner {@link #unregisterNetworkCallback} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001113 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001114 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001115 */
1116 public int stopUsingNetworkFeature(int networkType, String feature) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001117 checkLegacyRoutingApiAccess();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001118 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1119 if (netCap == null) {
1120 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
1121 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001122 return -1;
1123 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001124
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001125 if (removeRequestForFeature(netCap)) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001126 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001127 }
1128 return 1;
1129 }
1130
1131 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1132 if (networkType == TYPE_MOBILE) {
1133 int cap = -1;
1134 if ("enableMMS".equals(feature)) {
1135 cap = NetworkCapabilities.NET_CAPABILITY_MMS;
1136 } else if ("enableSUPL".equals(feature)) {
1137 cap = NetworkCapabilities.NET_CAPABILITY_SUPL;
1138 } else if ("enableDUN".equals(feature) || "enableDUNAlways".equals(feature)) {
1139 cap = NetworkCapabilities.NET_CAPABILITY_DUN;
1140 } else if ("enableHIPRI".equals(feature)) {
1141 cap = NetworkCapabilities.NET_CAPABILITY_INTERNET;
1142 } else if ("enableFOTA".equals(feature)) {
1143 cap = NetworkCapabilities.NET_CAPABILITY_FOTA;
1144 } else if ("enableIMS".equals(feature)) {
1145 cap = NetworkCapabilities.NET_CAPABILITY_IMS;
1146 } else if ("enableCBS".equals(feature)) {
1147 cap = NetworkCapabilities.NET_CAPABILITY_CBS;
1148 } else {
1149 return null;
1150 }
1151 NetworkCapabilities netCap = new NetworkCapabilities();
Robert Greenwalt7569f182014-06-08 16:42:59 -07001152 netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(cap);
Paul Jensen487ffe72015-07-24 15:57:11 -04001153 netCap.maybeMarkCapabilitiesRestricted();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001154 return netCap;
1155 } else if (networkType == TYPE_WIFI) {
1156 if ("p2p".equals(feature)) {
1157 NetworkCapabilities netCap = new NetworkCapabilities();
1158 netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
Robert Greenwalt7569f182014-06-08 16:42:59 -07001159 netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
Paul Jensen487ffe72015-07-24 15:57:11 -04001160 netCap.maybeMarkCapabilitiesRestricted();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001161 return netCap;
1162 }
1163 }
1164 return null;
1165 }
1166
Robert Greenwalt06314e42014-10-29 14:04:06 -07001167 /**
1168 * Guess what the network request was trying to say so that the resulting
1169 * network is accessible via the legacy (deprecated) API such as
1170 * requestRouteToHost.
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001171 *
1172 * This means we should try to be fairly precise about transport and
Robert Greenwalt06314e42014-10-29 14:04:06 -07001173 * capability but ignore things such as networkSpecifier.
1174 * If the request has more than one transport or capability it doesn't
1175 * match the old legacy requests (they selected only single transport/capability)
1176 * so this function cannot map the request to a single legacy type and
1177 * the resulting network will not be available to the legacy APIs.
1178 *
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001179 * This code is only called from the requestNetwork API (L and above).
1180 *
1181 * Setting a legacy type causes CONNECTIVITY_ACTION broadcasts, which are expensive
1182 * because they wake up lots of apps - see http://b/23350688 . So we currently only
1183 * do this for SUPL requests, which are the only ones that we know need it. If
1184 * omitting these broadcasts causes unacceptable app breakage, then for backwards
1185 * compatibility we can send them:
1186 *
1187 * if (targetSdkVersion < Build.VERSION_CODES.M) && // legacy API unsupported >= M
1188 * targetSdkVersion >= Build.VERSION_CODES.LOLLIPOP)) // requestNetwork not present < L
1189 *
Robert Greenwalt06314e42014-10-29 14:04:06 -07001190 * TODO - This should be removed when the legacy APIs are removed.
1191 */
Ye Wenb87875e2014-07-21 14:19:01 -07001192 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1193 if (netCap == null) {
1194 return TYPE_NONE;
1195 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001196
Ye Wenb87875e2014-07-21 14:19:01 -07001197 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1198 return TYPE_NONE;
1199 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001200
Lifu Tang30f95a72016-01-07 23:20:38 -08001201 // Do this only for SUPL, until GnssLocationProvider is fixed. http://b/25876485 .
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001202 if (!netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1203 // NOTE: if this causes app breakage, we should not just comment out this early return;
1204 // instead, we should make this early return conditional on the requesting app's target
1205 // SDK version, as described in the comment above.
1206 return TYPE_NONE;
1207 }
1208
Robert Greenwalt06314e42014-10-29 14:04:06 -07001209 String type = null;
1210 int result = TYPE_NONE;
1211
Ye Wenb87875e2014-07-21 14:19:01 -07001212 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
Robert Greenwalt06314e42014-10-29 14:04:06 -07001213 type = "enableCBS";
1214 result = TYPE_MOBILE_CBS;
1215 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1216 type = "enableIMS";
1217 result = TYPE_MOBILE_IMS;
1218 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1219 type = "enableFOTA";
1220 result = TYPE_MOBILE_FOTA;
1221 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1222 type = "enableDUN";
1223 result = TYPE_MOBILE_DUN;
1224 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
Lorenzo Colittidef4cb02015-11-25 20:28:50 +09001225 type = "enableSUPL";
Robert Greenwalt06314e42014-10-29 14:04:06 -07001226 result = TYPE_MOBILE_SUPL;
Robert Greenwalt74ab4fa2015-08-28 12:37:54 -07001227 // back out this hack for mms as they no longer need this and it's causing
1228 // device slowdowns - b/23350688 (note, supl still needs this)
1229 //} else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1230 // type = "enableMMS";
1231 // result = TYPE_MOBILE_MMS;
Robert Greenwalt06314e42014-10-29 14:04:06 -07001232 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1233 type = "enableHIPRI";
1234 result = TYPE_MOBILE_HIPRI;
Ye Wenb87875e2014-07-21 14:19:01 -07001235 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001236 if (type != null) {
1237 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1238 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1239 return result;
Ye Wenb87875e2014-07-21 14:19:01 -07001240 }
1241 }
1242 return TYPE_NONE;
1243 }
1244
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001245 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001246 if (netCap == null) return TYPE_NONE;
1247 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1248 return TYPE_MOBILE_CBS;
1249 }
1250 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1251 return TYPE_MOBILE_IMS;
1252 }
1253 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1254 return TYPE_MOBILE_FOTA;
1255 }
1256 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1257 return TYPE_MOBILE_DUN;
1258 }
1259 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1260 return TYPE_MOBILE_SUPL;
1261 }
1262 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1263 return TYPE_MOBILE_MMS;
1264 }
1265 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1266 return TYPE_MOBILE_HIPRI;
1267 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001268 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1269 return TYPE_WIFI_P2P;
1270 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001271 return TYPE_NONE;
1272 }
1273
1274 private static class LegacyRequest {
1275 NetworkCapabilities networkCapabilities;
1276 NetworkRequest networkRequest;
1277 int expireSequenceNumber;
1278 Network currentNetwork;
1279 int delay = -1;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001280
1281 private void clearDnsBinding() {
1282 if (currentNetwork != null) {
1283 currentNetwork = null;
1284 setProcessDefaultNetworkForHostResolution(null);
1285 }
1286 }
1287
Robert Greenwalt6078b502014-06-11 16:05:07 -07001288 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001289 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001290 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001291 currentNetwork = network;
1292 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001293 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001294 }
1295 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001296 public void onLost(Network network) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001297 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001298 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1299 }
1300 };
1301 }
1302
Robert Greenwaltfab501672014-07-23 11:44:01 -07001303 private static HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
Robert Greenwalt562cc542014-05-15 18:07:26 -07001304 new HashMap<NetworkCapabilities, LegacyRequest>();
1305
1306 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1307 synchronized (sLegacyRequests) {
1308 LegacyRequest l = sLegacyRequests.get(netCap);
1309 if (l != null) return l.networkRequest;
1310 }
1311 return null;
1312 }
1313
1314 private void renewRequestLocked(LegacyRequest l) {
1315 l.expireSequenceNumber++;
1316 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1317 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1318 }
1319
1320 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1321 int ourSeqNum = -1;
1322 synchronized (sLegacyRequests) {
1323 LegacyRequest l = sLegacyRequests.get(netCap);
1324 if (l == null) return;
1325 ourSeqNum = l.expireSequenceNumber;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001326 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001327 }
1328 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1329 }
1330
1331 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1332 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001333 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001334 try {
1335 delay = mService.getRestoreDefaultNetworkDelay(type);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001336 } catch (RemoteException e) {
1337 throw e.rethrowFromSystemServer();
1338 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001339 LegacyRequest l = new LegacyRequest();
1340 l.networkCapabilities = netCap;
1341 l.delay = delay;
1342 l.expireSequenceNumber = 0;
Robert Greenwalt6078b502014-06-11 16:05:07 -07001343 l.networkRequest = sendRequestForNetwork(netCap, l.networkCallback, 0,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001344 REQUEST, type);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001345 if (l.networkRequest == null) return null;
1346 sLegacyRequests.put(netCap, l);
1347 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1348 return l.networkRequest;
1349 }
1350
1351 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1352 if (delay >= 0) {
1353 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
1354 Message msg = sCallbackHandler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1355 sCallbackHandler.sendMessageDelayed(msg, delay);
1356 }
1357 }
1358
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001359 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1360 final LegacyRequest l;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001361 synchronized (sLegacyRequests) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001362 l = sLegacyRequests.remove(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001363 }
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001364 if (l == null) return false;
1365 unregisterNetworkCallback(l.networkCallback);
1366 l.clearDnsBinding();
1367 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001368 }
1369
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001370 /** @hide */
1371 public static class PacketKeepaliveCallback {
1372 /** The requested keepalive was successfully started. */
1373 public void onStarted() {}
1374 /** The keepalive was successfully stopped. */
1375 public void onStopped() {}
1376 /** An error occurred. */
1377 public void onError(int error) {}
1378 }
1379
1380 /**
1381 * Allows applications to request that the system periodically send specific packets on their
1382 * behalf, using hardware offload to save battery power.
1383 *
1384 * To request that the system send keepalives, call one of the methods that return a
1385 * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive},
1386 * passing in a non-null callback. If the callback is successfully started, the callback's
1387 * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called,
1388 * specifying one of the {@code ERROR_*} constants in this class.
1389 *
1390 * To stop an existing keepalive, call {@link stop}. The system will call {@code onStopped} if
1391 * the operation was successfull or {@code onError} if an error occurred.
1392 *
1393 * @hide
1394 */
1395 public class PacketKeepalive {
1396
1397 private static final String TAG = "PacketKeepalive";
1398
1399 /** @hide */
1400 public static final int SUCCESS = 0;
1401
1402 /** @hide */
1403 public static final int NO_KEEPALIVE = -1;
1404
1405 /** @hide */
1406 public static final int BINDER_DIED = -10;
1407
1408 /** The specified {@code Network} is not connected. */
1409 public static final int ERROR_INVALID_NETWORK = -20;
1410 /** The specified IP addresses are invalid. For example, the specified source IP address is
1411 * not configured on the specified {@code Network}. */
1412 public static final int ERROR_INVALID_IP_ADDRESS = -21;
1413 /** The requested port is invalid. */
1414 public static final int ERROR_INVALID_PORT = -22;
1415 /** The packet length is invalid (e.g., too long). */
1416 public static final int ERROR_INVALID_LENGTH = -23;
1417 /** The packet transmission interval is invalid (e.g., too short). */
1418 public static final int ERROR_INVALID_INTERVAL = -24;
1419
1420 /** The hardware does not support this request. */
1421 public static final int ERROR_HARDWARE_UNSUPPORTED = -30;
Lorenzo Colitti9d1284e2015-09-08 16:46:36 +09001422 /** The hardware returned an error. */
1423 public static final int ERROR_HARDWARE_ERROR = -31;
Lorenzo Colitti8bf977d2015-06-15 14:29:22 +09001424
1425 public static final int NATT_PORT = 4500;
1426
1427 private final Network mNetwork;
1428 private final PacketKeepaliveCallback mCallback;
1429 private final Looper mLooper;
1430 private final Messenger mMessenger;
1431
1432 private volatile Integer mSlot;
1433
1434 void stopLooper() {
1435 mLooper.quit();
1436 }
1437
1438 public void stop() {
1439 try {
1440 mService.stopKeepalive(mNetwork, mSlot);
1441 } catch (RemoteException e) {
1442 Log.e(TAG, "Error stopping packet keepalive: ", e);
1443 stopLooper();
1444 }
1445 }
1446
1447 private PacketKeepalive(Network network, PacketKeepaliveCallback callback) {
1448 checkNotNull(network, "network cannot be null");
1449 checkNotNull(callback, "callback cannot be null");
1450 mNetwork = network;
1451 mCallback = callback;
1452 HandlerThread thread = new HandlerThread(TAG);
1453 thread.start();
1454 mLooper = thread.getLooper();
1455 mMessenger = new Messenger(new Handler(mLooper) {
1456 @Override
1457 public void handleMessage(Message message) {
1458 switch (message.what) {
1459 case NetworkAgent.EVENT_PACKET_KEEPALIVE:
1460 int error = message.arg2;
1461 try {
1462 if (error == SUCCESS) {
1463 if (mSlot == null) {
1464 mSlot = message.arg1;
1465 mCallback.onStarted();
1466 } else {
1467 mSlot = null;
1468 stopLooper();
1469 mCallback.onStopped();
1470 }
1471 } else {
1472 stopLooper();
1473 mCallback.onError(error);
1474 }
1475 } catch (Exception e) {
1476 Log.e(TAG, "Exception in keepalive callback(" + error + ")", e);
1477 }
1478 break;
1479 default:
1480 Log.e(TAG, "Unhandled message " + Integer.toHexString(message.what));
1481 break;
1482 }
1483 }
1484 });
1485 }
1486 }
1487
1488 /**
1489 * Starts an IPsec NAT-T keepalive packet with the specified parameters.
1490 *
1491 * @hide
1492 */
1493 public PacketKeepalive startNattKeepalive(
1494 Network network, int intervalSeconds, PacketKeepaliveCallback callback,
1495 InetAddress srcAddr, int srcPort, InetAddress dstAddr) {
1496 final PacketKeepalive k = new PacketKeepalive(network, callback);
1497 try {
1498 mService.startNattKeepalive(network, intervalSeconds, k.mMessenger, new Binder(),
1499 srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress());
1500 } catch (RemoteException e) {
1501 Log.e(TAG, "Error starting packet keepalive: ", e);
1502 k.stopLooper();
1503 return null;
1504 }
1505 return k;
1506 }
1507
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001508 /**
1509 * Ensure that a network route exists to deliver traffic to the specified
1510 * host via the specified network interface. An attempt to add a route that
1511 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001512 *
1513 * <p>This method requires the caller to hold either the
1514 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1515 * or the ability to modify system settings as determined by
1516 * {@link android.provider.Settings.System#canWrite}.</p>
1517 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001518 * @param networkType the type of the network over which traffic to the specified
1519 * host is to be routed
1520 * @param hostAddress the IP address of the host to which the route is desired
1521 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001522 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001523 * @deprecated Deprecated in favor of the
1524 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
1525 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
Dianne Hackborn692a2442015-07-31 10:35:34 -07001526 * In {@link VERSION_CODES#M}, and above, this method is unsupported and will
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001527 * throw {@code UnsupportedOperationException} if called.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001528 */
1529 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001530 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001531 }
1532
1533 /**
1534 * Ensure that a network route exists to deliver traffic to the specified
1535 * host via the specified network interface. An attempt to add a route that
1536 * already exists is ignored, but treated as successful.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001537 *
1538 * <p>This method requires the caller to hold either the
1539 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1540 * or the ability to modify system settings as determined by
1541 * {@link android.provider.Settings.System#canWrite}.</p>
1542 *
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001543 * @param networkType the type of the network over which traffic to the specified
1544 * host is to be routed
1545 * @param hostAddress the IP address of the host to which the route is desired
1546 * @return {@code true} on success, {@code false} on failure
1547 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001548 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001549 * {@link #bindProcessToNetwork} API.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001550 */
1551 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001552 checkLegacyRoutingApiAccess();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001553 try {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001554 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001555 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001556 throw e.rethrowFromSystemServer();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001557 }
1558 }
1559
1560 /**
1561 * Returns the value of the setting for background data usage. If false,
1562 * applications should not use the network if the application is not in the
1563 * foreground. Developers should respect this setting, and check the value
1564 * of this before performing any background data operations.
1565 * <p>
1566 * All applications that have background services that use the network
1567 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001568 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001569 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001570 * background data depends on several combined factors, and this method will
1571 * always return {@code true}. Instead, when background data is unavailable,
1572 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001573 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001574 * @return Whether background data usage is allowed.
1575 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001576 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001577 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001578 // assume that background data is allowed; final authority is
1579 // NetworkInfo which may be blocked.
1580 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001581 }
1582
1583 /**
1584 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001585 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001586 * @param allowBackgroundData Whether an application should use data while
1587 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001588 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001589 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1590 * @see #getBackgroundDataSetting()
1591 * @hide
1592 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001593 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001594 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001595 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001596 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001597
1598 /**
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001599 * Return quota status for the current active network, or {@code null} if no
1600 * network is active. Quota status can change rapidly, so these values
1601 * shouldn't be cached.
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001602 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001603 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001604 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1605 *
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001606 * @hide
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001607 */
1608 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1609 try {
1610 return mService.getActiveNetworkQuotaInfo();
1611 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001612 throw e.rethrowFromSystemServer();
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001613 }
1614 }
1615
1616 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001617 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001618 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001619 */
1620 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001621 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1622 if (b != null) {
1623 try {
1624 ITelephony it = ITelephony.Stub.asInterface(b);
Shishir Agrawal7ea3e8b2016-01-25 13:03:07 -08001625 int subId = SubscriptionManager.getDefaultDataSubscriptionId();
Wink Saville36ffb042014-12-05 11:10:30 -08001626 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
1627 boolean retVal = it.getDataEnabled(subId);
1628 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1629 + " retVal=" + retVal);
1630 return retVal;
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001631 } catch (RemoteException e) {
1632 throw e.rethrowFromSystemServer();
1633 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001634 }
Wink Saville36ffb042014-12-05 11:10:30 -08001635 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001636 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001637 }
1638
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001639 /**
Robert Greenwaltb2489872014-09-04 16:44:35 -07001640 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt6078b502014-06-11 16:05:07 -07001641 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001642 */
1643 public interface OnNetworkActiveListener {
1644 /**
1645 * Called on the main thread of the process to report that the current data network
1646 * has become active, and it is now a good time to perform any pending network
1647 * operations. Note that this listener only tells you when the network becomes
1648 * active; if at any other time you want to know whether it is active (and thus okay
1649 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001650 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001651 */
1652 public void onNetworkActive();
1653 }
1654
1655 private INetworkManagementService getNetworkManagementService() {
1656 synchronized (this) {
1657 if (mNMService != null) {
1658 return mNMService;
1659 }
1660 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1661 mNMService = INetworkManagementService.Stub.asInterface(b);
1662 return mNMService;
1663 }
1664 }
1665
1666 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
1667 mNetworkActivityListeners
1668 = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>();
1669
1670 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001671 * Start listening to reports when the system's default data network is active, meaning it is
1672 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1673 * to determine the current state of the system's default network after registering the
1674 * listener.
1675 * <p>
1676 * If the process default network has been set with
Paul Jensen72db88e2015-03-10 10:54:12 -04001677 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt6078b502014-06-11 16:05:07 -07001678 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001679 *
1680 * @param l The listener to be told when the network is active.
1681 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001682 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001683 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1684 @Override
1685 public void onNetworkActive() throws RemoteException {
1686 l.onNetworkActive();
1687 }
1688 };
1689
1690 try {
1691 getNetworkManagementService().registerNetworkActivityListener(rl);
1692 mNetworkActivityListeners.put(l, rl);
1693 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001694 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001695 }
1696 }
1697
1698 /**
1699 * Remove network active listener previously registered with
Robert Greenwaltb2489872014-09-04 16:44:35 -07001700 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001701 *
1702 * @param l Previously registered listener.
1703 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001704 public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001705 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
1706 if (rl == null) {
1707 throw new IllegalArgumentException("Listener not registered: " + l);
1708 }
1709 try {
1710 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1711 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001712 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001713 }
1714 }
1715
1716 /**
1717 * Return whether the data network is currently active. An active network means that
1718 * it is currently in a high power state for performing data transmission. On some
1719 * types of networks, it may be expensive to move and stay in such a state, so it is
1720 * more power efficient to batch network traffic together when the radio is already in
1721 * this state. This method tells you whether right now is currently a good time to
1722 * initiate network traffic, as the network is already active.
1723 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001724 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001725 try {
1726 return getNetworkManagementService().isNetworkActive();
1727 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001728 throw e.rethrowFromSystemServer();
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001729 }
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001730 }
1731
1732 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001733 * {@hide}
1734 */
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09001735 public ConnectivityManager(Context context, IConnectivityManager service) {
1736 mContext = checkNotNull(context, "missing context");
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001737 mService = checkNotNull(service, "missing IConnectivityManager");
Paul Jensene0bef712014-12-10 15:12:18 -05001738 sInstance = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001739 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001740
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07001741 /** {@hide} */
1742 public static ConnectivityManager from(Context context) {
1743 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1744 }
1745
Lorenzo Colittid5427052015-10-15 16:29:00 +09001746 /** {@hide} */
1747 public static final void enforceChangePermission(Context context) {
1748 int uid = Binder.getCallingUid();
1749 Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings
1750 .getPackageNameForUid(context, uid), true /* throwException */);
1751 }
1752
Robert Greenwaltedb47662014-09-16 17:54:19 -07001753 /** {@hide */
1754 public static final void enforceTetherChangePermission(Context context) {
1755 if (context.getResources().getStringArray(
1756 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
1757 // Have a provisioning app - must only let system apps (which check this app)
1758 // turn on tethering
1759 context.enforceCallingOrSelfPermission(
Jeremy Kleind42209d2015-12-28 15:11:58 -08001760 android.Manifest.permission.TETHER_PRIVILEGED, "ConnectivityService");
Robert Greenwaltedb47662014-09-16 17:54:19 -07001761 } else {
Billy Laua7238a32015-08-01 12:45:02 +01001762 int uid = Binder.getCallingUid();
Lorenzo Colittid5427052015-10-15 16:29:00 +09001763 Settings.checkAndNoteWriteSettingsOperation(context, uid, Settings
1764 .getPackageNameForUid(context, uid), true /* throwException */);
Robert Greenwaltedb47662014-09-16 17:54:19 -07001765 }
1766 }
1767
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001768 /**
Paul Jensene0bef712014-12-10 15:12:18 -05001769 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1770 * situations where a Context pointer is unavailable.
1771 * @hide
1772 */
Paul Jensen72db88e2015-03-10 10:54:12 -04001773 static ConnectivityManager getInstanceOrNull() {
1774 return sInstance;
1775 }
1776
1777 /**
1778 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1779 * situations where a Context pointer is unavailable.
1780 * @hide
1781 */
1782 private static ConnectivityManager getInstance() {
1783 if (getInstanceOrNull() == null) {
Paul Jensene0bef712014-12-10 15:12:18 -05001784 throw new IllegalStateException("No ConnectivityManager yet constructed");
1785 }
Paul Jensen72db88e2015-03-10 10:54:12 -04001786 return getInstanceOrNull();
Paul Jensene0bef712014-12-10 15:12:18 -05001787 }
1788
1789 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001790 * Get the set of tetherable, available interfaces. This list is limited by
1791 * device configuration and current interface existence.
Paul Jensenb2748922015-05-06 11:10:18 -04001792 * <p>This method requires the caller to hold the permission
1793 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001794 *
1795 * @return an array of 0 or more Strings of tetherable interface names.
1796 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001797 * {@hide}
1798 */
1799 public String[] getTetherableIfaces() {
1800 try {
1801 return mService.getTetherableIfaces();
1802 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001803 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001804 }
1805 }
1806
1807 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001808 * Get the set of tethered interfaces.
Paul Jensenb2748922015-05-06 11:10:18 -04001809 * <p>This method requires the caller to hold the permission
1810 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001811 *
1812 * @return an array of 0 or more String of currently tethered interface names.
1813 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001814 * {@hide}
1815 */
1816 public String[] getTetheredIfaces() {
1817 try {
1818 return mService.getTetheredIfaces();
1819 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001820 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001821 }
1822 }
1823
1824 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001825 * Get the set of interface names which attempted to tether but
1826 * failed. Re-attempting to tether may cause them to reset to the Tethered
1827 * state. Alternatively, causing the interface to be destroyed and recreated
1828 * may cause them to reset to the available state.
1829 * {@link ConnectivityManager#getLastTetherError} can be used to get more
1830 * information on the cause of the errors.
Paul Jensenb2748922015-05-06 11:10:18 -04001831 * <p>This method requires the caller to hold the permission
1832 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001833 *
1834 * @return an array of 0 or more String indicating the interface names
1835 * which failed to tether.
1836 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001837 * {@hide}
1838 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001839 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001840 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001841 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001842 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001843 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001844 }
1845 }
1846
1847 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001848 * Get the set of tethered dhcp ranges.
1849 *
1850 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
1851 * {@hide}
1852 */
1853 public String[] getTetheredDhcpRanges() {
1854 try {
1855 return mService.getTetheredDhcpRanges();
1856 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001857 throw e.rethrowFromSystemServer();
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001858 }
1859 }
1860
1861 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001862 * Attempt to tether the named interface. This will setup a dhcp server
1863 * on the interface, forward and NAT IP packets and forward DNS requests
1864 * to the best active upstream network interface. Note that if no upstream
1865 * IP network interface is available, dhcp will still run and traffic will be
1866 * allowed between the tethered devices and this device, though upstream net
1867 * access will of course fail until an upstream network interface becomes
1868 * active.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001869 *
1870 * <p>This method requires the caller to hold either the
1871 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1872 * or the ability to modify system settings as determined by
1873 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001874 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08001875 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
1876 * and WifiStateMachine which need direct access. All other clients should use
1877 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
1878 * logic.</p>
1879 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001880 * @param iface the interface name to tether.
1881 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1882 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001883 * {@hide}
1884 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001885 public int tether(String iface) {
1886 try {
1887 return mService.tether(iface);
1888 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001889 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08001890 }
1891 }
1892
1893 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001894 * Stop tethering the named interface.
Lorenzo Colittid5427052015-10-15 16:29:00 +09001895 *
1896 * <p>This method requires the caller to hold either the
1897 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
1898 * or the ability to modify system settings as determined by
1899 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001900 *
Jeremy Klein36c7aa02016-01-22 14:11:45 -08001901 * <p>WARNING: New clients should not use this function. The only usages should be in PanService
1902 * and WifiStateMachine which need direct access. All other clients should use
1903 * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning
1904 * logic.</p>
1905 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001906 * @param iface the interface name to untether.
1907 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1908 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08001909 * {@hide}
1910 */
1911 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001912 try {
1913 return mService.untether(iface);
1914 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001915 throw e.rethrowFromSystemServer();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001916 }
1917 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001918
1919 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001920 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001921 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001922 * due to device configuration.
Paul Jensenb2748922015-05-06 11:10:18 -04001923 * <p>This method requires the caller to hold the permission
1924 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001925 *
1926 * @return a boolean - {@code true} indicating Tethering is supported.
1927 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001928 * {@hide}
1929 */
Jeremy Klein36c7aa02016-01-22 14:11:45 -08001930 @SystemApi
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001931 public boolean isTetheringSupported() {
1932 try {
1933 return mService.isTetheringSupported();
1934 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07001935 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001936 }
1937 }
1938
1939 /**
Jeremy Klein36c7aa02016-01-22 14:11:45 -08001940 * Callback for use with {@link #startTethering} to find out whether tethering succeeded.
1941 * @hide
1942 */
1943 @SystemApi
1944 public static abstract class OnStartTetheringCallback {
1945 /**
1946 * Called when tethering has been successfully started.
1947 */
1948 public void onTetheringStarted() {};
1949
1950 /**
1951 * Called when starting tethering failed.
1952 */
1953 public void onTetheringFailed() {};
1954 }
1955
1956 /**
1957 * Convenient overload for
1958 * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null
1959 * handler to run on the current thread's {@link Looper}.
1960 * @hide
1961 */
1962 @SystemApi
1963 public void startTethering(int type, boolean showProvisioningUi,
1964 final OnStartTetheringCallback callback) {
1965 startTethering(type, showProvisioningUi, callback, null);
1966 }
1967
1968 /**
1969 * Runs tether provisioning for the given type if needed and then starts tethering if
1970 * the check succeeds. If no carrier provisioning is required for tethering, tethering is
1971 * enabled immediately. If provisioning fails, tethering will not be enabled. It also
1972 * schedules tether provisioning re-checks if appropriate.
1973 *
1974 * @param type The type of tethering to start. Must be one of
1975 * {@link ConnectivityManager.TETHERING_WIFI},
1976 * {@link ConnectivityManager.TETHERING_USB}, or
1977 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
1978 * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there
1979 * is one. This should be true the first time this function is called and also any time
1980 * the user can see this UI. It gives users information from their carrier about the
1981 * check failing and how they can sign up for tethering if possible.
1982 * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller
1983 * of the result of trying to tether.
1984 * @param handler {@link Handler} to specify the thread upon which the callback will be invoked.
1985 * @hide
1986 */
1987 @SystemApi
1988 public void startTethering(int type, boolean showProvisioningUi,
1989 final OnStartTetheringCallback callback, Handler handler) {
1990 ResultReceiver wrappedCallback = new ResultReceiver(handler) {
1991 @Override
1992 protected void onReceiveResult(int resultCode, Bundle resultData) {
1993 if (resultCode == TETHER_ERROR_NO_ERROR) {
1994 callback.onTetheringStarted();
1995 } else {
1996 callback.onTetheringFailed();
1997 }
1998 }
1999 };
2000 try {
2001 mService.startTethering(type, wrappedCallback, showProvisioningUi);
2002 } catch (RemoteException e) {
2003 Log.e(TAG, "Exception trying to start tethering.", e);
2004 wrappedCallback.send(TETHER_ERROR_SERVICE_UNAVAIL, null);
2005 }
2006 }
2007
2008 /**
2009 * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if
2010 * applicable.
2011 *
2012 * @param type The type of tethering to stop. Must be one of
2013 * {@link ConnectivityManager.TETHERING_WIFI},
2014 * {@link ConnectivityManager.TETHERING_USB}, or
2015 * {@link ConnectivityManager.TETHERING_BLUETOOTH}.
2016 * @hide
2017 */
2018 @SystemApi
2019 public void stopTethering(int type) {
2020 try {
2021 mService.stopTethering(type);
2022 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002023 throw e.rethrowFromSystemServer();
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002024 }
2025 }
2026
2027 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002028 * Get the list of regular expressions that define any tetherable
2029 * USB network interfaces. If USB tethering is not supported by the
2030 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04002031 * <p>This method requires the caller to hold the permission
2032 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002033 *
2034 * @return an array of 0 or more regular expression Strings defining
2035 * what interfaces are considered tetherable usb interfaces.
2036 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002037 * {@hide}
2038 */
2039 public String[] getTetherableUsbRegexs() {
2040 try {
2041 return mService.getTetherableUsbRegexs();
2042 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002043 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002044 }
2045 }
2046
2047 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002048 * Get the list of regular expressions that define any tetherable
2049 * Wifi network interfaces. If Wifi tethering is not supported by the
2050 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04002051 * <p>This method requires the caller to hold the permission
2052 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002053 *
2054 * @return an array of 0 or more regular expression Strings defining
2055 * what interfaces are considered tetherable wifi interfaces.
2056 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002057 * {@hide}
2058 */
2059 public String[] getTetherableWifiRegexs() {
2060 try {
2061 return mService.getTetherableWifiRegexs();
2062 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002063 throw e.rethrowFromSystemServer();
Robert Greenwalt2a091d72010-02-11 18:18:40 -08002064 }
2065 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08002066
Danica Chang6fdd0c62010-08-11 14:54:43 -07002067 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002068 * Get the list of regular expressions that define any tetherable
2069 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
2070 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04002071 * <p>This method requires the caller to hold the permission
2072 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002073 *
2074 * @return an array of 0 or more regular expression Strings defining
2075 * what interfaces are considered tetherable bluetooth interfaces.
2076 *
Danica Chang6fdd0c62010-08-11 14:54:43 -07002077 * {@hide}
2078 */
2079 public String[] getTetherableBluetoothRegexs() {
2080 try {
2081 return mService.getTetherableBluetoothRegexs();
2082 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002083 throw e.rethrowFromSystemServer();
Danica Chang6fdd0c62010-08-11 14:54:43 -07002084 }
2085 }
2086
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002087 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002088 * Attempt to both alter the mode of USB and Tethering of USB. A
2089 * utility method to deal with some of the complexity of USB - will
2090 * attempt to switch to Rndis and subsequently tether the resulting
2091 * interface on {@code true} or turn off tethering and switch off
2092 * Rndis on {@code false}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002093 *
2094 * <p>This method requires the caller to hold either the
2095 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2096 * or the ability to modify system settings as determined by
2097 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002098 *
2099 * @param enable a boolean - {@code true} to enable tethering
2100 * @return error a {@code TETHER_ERROR} value indicating success or failure type
2101 *
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002102 * {@hide}
2103 */
2104 public int setUsbTethering(boolean enable) {
2105 try {
2106 return mService.setUsbTethering(enable);
2107 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002108 throw e.rethrowFromSystemServer();
Mike Lockwood6c2260b2011-07-19 13:04:47 -07002109 }
2110 }
2111
Robert Greenwalt5a735062010-03-02 17:25:02 -08002112 /** {@hide} */
2113 public static final int TETHER_ERROR_NO_ERROR = 0;
2114 /** {@hide} */
2115 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
2116 /** {@hide} */
2117 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
2118 /** {@hide} */
2119 public static final int TETHER_ERROR_UNSUPPORTED = 3;
2120 /** {@hide} */
2121 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
2122 /** {@hide} */
2123 public static final int TETHER_ERROR_MASTER_ERROR = 5;
2124 /** {@hide} */
2125 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
2126 /** {@hide} */
2127 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
2128 /** {@hide} */
2129 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
2130 /** {@hide} */
2131 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
2132 /** {@hide} */
2133 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
Jeremy Klein36c7aa02016-01-22 14:11:45 -08002134 /** {@hide} */
2135 public static final int TETHER_ERROR_PROVISION_FAILED = 11;
Robert Greenwalt5a735062010-03-02 17:25:02 -08002136
2137 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002138 * Get a more detailed error code after a Tethering or Untethering
2139 * request asynchronously failed.
Paul Jensenb2748922015-05-06 11:10:18 -04002140 * <p>This method requires the caller to hold the permission
2141 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002142 *
2143 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08002144 * @return error The error code of the last error tethering or untethering the named
2145 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002146 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08002147 * {@hide}
2148 */
2149 public int getLastTetherError(String iface) {
2150 try {
2151 return mService.getLastTetherError(iface);
2152 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002153 throw e.rethrowFromSystemServer();
Robert Greenwalt5a735062010-03-02 17:25:02 -08002154 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07002155 }
2156
2157 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002158 * Report network connectivity status. This is currently used only
2159 * to alter status bar UI.
Paul Jensenb2748922015-05-06 11:10:18 -04002160 * <p>This method requires the caller to hold the permission
2161 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002162 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002163 * @param networkType The type of network you want to report on
2164 * @param percentage The quality of the connection 0 is bad, 100 is good
2165 * {@hide}
2166 */
2167 public void reportInetCondition(int networkType, int percentage) {
2168 try {
2169 mService.reportInetCondition(networkType, percentage);
2170 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002171 throw e.rethrowFromSystemServer();
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07002172 }
2173 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07002174
2175 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002176 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07002177 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002178 * the framework to re-evaluate network connectivity and/or switch to another
2179 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002180 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002181 * @param network The {@link Network} the application was attempting to use
2182 * or {@code null} to indicate the current default network.
Paul Jensenbfd17b72015-04-07 12:43:13 -04002183 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
2184 * working and non-working connectivity.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002185 */
2186 public void reportBadNetwork(Network network) {
2187 try {
Paul Jensenbfd17b72015-04-07 12:43:13 -04002188 // One of these will be ignored because it matches system's current state.
2189 // The other will trigger the necessary reevaluation.
2190 mService.reportNetworkConnectivity(network, true);
2191 mService.reportNetworkConnectivity(network, false);
2192 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002193 throw e.rethrowFromSystemServer();
Paul Jensenbfd17b72015-04-07 12:43:13 -04002194 }
2195 }
2196
2197 /**
2198 * Report to the framework whether a network has working connectivity.
2199 * This provides a hint to the system that a particular network is providing
2200 * working connectivity or not. In response the framework may re-evaluate
2201 * the network's connectivity and might take further action thereafter.
2202 *
2203 * @param network The {@link Network} the application was attempting to use
2204 * or {@code null} to indicate the current default network.
2205 * @param hasConnectivity {@code true} if the application was able to successfully access the
2206 * Internet using {@code network} or {@code false} if not.
2207 */
2208 public void reportNetworkConnectivity(Network network, boolean hasConnectivity) {
2209 try {
2210 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002211 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002212 throw e.rethrowFromSystemServer();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002213 }
2214 }
2215
2216 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002217 * Set a network-independent global http proxy. This is not normally what you want
2218 * for typical HTTP proxies - they are general network dependent. However if you're
2219 * doing something unusual like general internal filtering this may be useful. On
2220 * a private network where the proxy is not accessible, you may break HTTP using this.
Paul Jensen0d719ca2015-02-13 14:18:39 -05002221 * <p>This method requires the caller to hold the permission
Jason Monkfaf3fd52014-05-07 18:41:13 -04002222 * android.Manifest.permission#CONNECTIVITY_INTERNAL.
Paul Jensenb2748922015-05-06 11:10:18 -04002223 *
2224 * @param p A {@link ProxyInfo} object defining the new global
2225 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002226 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002227 */
Jason Monk207900c2014-04-25 15:00:09 -04002228 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002229 try {
2230 mService.setGlobalProxy(p);
2231 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002232 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002233 }
2234 }
2235
2236 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002237 * Retrieve any network-independent global HTTP proxy.
2238 *
Jason Monk207900c2014-04-25 15:00:09 -04002239 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002240 * if no global HTTP proxy is set.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002241 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07002242 */
Jason Monk207900c2014-04-25 15:00:09 -04002243 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07002244 try {
2245 return mService.getGlobalProxy();
2246 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002247 throw e.rethrowFromSystemServer();
Robert Greenwalt434203a2010-10-11 16:00:27 -07002248 }
2249 }
2250
2251 /**
Paul Jensencee9b512015-05-06 07:32:40 -04002252 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
2253 * network-specific HTTP proxy. If {@code network} is null, the
2254 * network-specific proxy returned is the proxy of the default active
2255 * network.
2256 *
2257 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
2258 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
2259 * or when {@code network} is {@code null},
2260 * the {@code ProxyInfo} for the default active network. Returns
2261 * {@code null} when no proxy applies or the caller doesn't have
2262 * permission to use {@code network}.
2263 * @hide
2264 */
2265 public ProxyInfo getProxyForNetwork(Network network) {
2266 try {
2267 return mService.getProxyForNetwork(network);
2268 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002269 throw e.rethrowFromSystemServer();
Paul Jensencee9b512015-05-06 07:32:40 -04002270 }
2271 }
2272
2273 /**
Paul Jensene0bef712014-12-10 15:12:18 -05002274 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
2275 * otherwise if this process is bound to a {@link Network} using
Paul Jensen72db88e2015-03-10 10:54:12 -04002276 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensene0bef712014-12-10 15:12:18 -05002277 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002278 *
Jason Monk207900c2014-04-25 15:00:09 -04002279 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002280 * HTTP proxy is active.
Robert Greenwalt434203a2010-10-11 16:00:27 -07002281 */
Paul Jensene0bef712014-12-10 15:12:18 -05002282 public ProxyInfo getDefaultProxy() {
Paul Jensencee9b512015-05-06 07:32:40 -04002283 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwalt434203a2010-10-11 16:00:27 -07002284 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07002285
2286 /**
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002287 * Returns true if the hardware supports the given network type
2288 * else it returns false. This doesn't indicate we have coverage
2289 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002290 * hardware supports it. For example a GSM phone without a SIM
2291 * should still return {@code true} for mobile data, but a wifi only
2292 * tablet would return {@code false}.
Paul Jensenb2748922015-05-06 11:10:18 -04002293 * <p>This method requires the caller to hold the permission
2294 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002295 *
2296 * @param networkType The network type we'd like to check
2297 * @return {@code true} if supported, else {@code false}
2298 *
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002299 * @hide
2300 */
2301 public boolean isNetworkSupported(int networkType) {
2302 try {
2303 return mService.isNetworkSupported(networkType);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002304 } catch (RemoteException e) {
2305 throw e.rethrowFromSystemServer();
2306 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07002307 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002308
2309 /**
2310 * Returns if the currently active data network is metered. A network is
2311 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002312 * that connection due to monetary costs, data limitations or
2313 * battery/performance issues. You should check this before doing large
2314 * data transfers, and warn the user or delay the operation until another
2315 * network is available.
Paul Jensenb2748922015-05-06 11:10:18 -04002316 * <p>This method requires the caller to hold the permission
2317 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002318 *
2319 * @return {@code true} if large transfers should be avoided, otherwise
2320 * {@code false}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002321 */
2322 public boolean isActiveNetworkMetered() {
2323 try {
2324 return mService.isActiveNetworkMetered();
2325 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002326 throw e.rethrowFromSystemServer();
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07002327 }
2328 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002329
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08002330 /**
2331 * If the LockdownVpn mechanism is enabled, updates the vpn
2332 * with a reload of its profile.
2333 *
2334 * @return a boolean with {@code} indicating success
2335 *
2336 * <p>This method can only be called by the system UID
2337 * {@hide}
2338 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002339 public boolean updateLockdownVpn() {
2340 try {
2341 return mService.updateLockdownVpn();
2342 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002343 throw e.rethrowFromSystemServer();
Jeff Sharkey69ddab42012-08-25 00:05:46 -07002344 }
2345 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07002346
2347 /**
Wink Saville948282b2013-08-29 08:55:16 -07002348 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07002349 *
Wink Savilleab9321d2013-06-29 21:10:57 -07002350 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07002351 *
2352 * @return time out that will be used, maybe less that suggestedTimeOutMs
2353 * -1 if an error.
2354 *
2355 * {@hide}
2356 */
Wink Saville948282b2013-08-29 08:55:16 -07002357 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07002358 int timeOutMs = -1;
2359 try {
Wink Saville948282b2013-08-29 08:55:16 -07002360 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07002361 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002362 throw e.rethrowFromSystemServer();
Wink Savilleab9321d2013-06-29 21:10:57 -07002363 }
2364 return timeOutMs;
2365 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002366
2367 /**
Wink Saville42d4f082013-07-20 20:31:59 -07002368 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002369 * {@hide}
2370 */
2371 public String getMobileProvisioningUrl() {
2372 try {
2373 return mService.getMobileProvisioningUrl();
2374 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002375 throw e.rethrowFromSystemServer();
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002376 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002377 }
Wink Saville42d4f082013-07-20 20:31:59 -07002378
2379 /**
Wink Saville948282b2013-08-29 08:55:16 -07002380 * Set sign in error notification to visible or in visible
2381 *
2382 * @param visible
2383 * @param networkType
2384 *
2385 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04002386 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville948282b2013-08-29 08:55:16 -07002387 */
2388 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04002389 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07002390 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04002391 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07002392 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002393 throw e.rethrowFromSystemServer();
Wink Saville948282b2013-08-29 08:55:16 -07002394 }
2395 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002396
2397 /**
2398 * Set the value for enabling/disabling airplane mode
Paul Jensenb2748922015-05-06 11:10:18 -04002399 * <p>This method requires the caller to hold the permission
2400 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002401 *
2402 * @param enable whether to enable airplane mode or not
2403 *
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002404 * @hide
2405 */
2406 public void setAirplaneMode(boolean enable) {
2407 try {
2408 mService.setAirplaneMode(enable);
2409 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002410 throw e.rethrowFromSystemServer();
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002411 }
2412 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002413
2414 /** {@hide} */
Robert Greenwalta67be032014-05-16 15:49:14 -07002415 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07002416 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07002417 mService.registerNetworkFactory(messenger, name);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002418 } catch (RemoteException e) {
2419 throw e.rethrowFromSystemServer();
2420 }
Robert Greenwalta67be032014-05-16 15:49:14 -07002421 }
2422
2423 /** {@hide} */
2424 public void unregisterNetworkFactory(Messenger messenger) {
2425 try {
2426 mService.unregisterNetworkFactory(messenger);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002427 } catch (RemoteException e) {
2428 throw e.rethrowFromSystemServer();
2429 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002430 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002431
Paul Jensen31a94f42015-02-13 14:18:39 -05002432 /**
2433 * @hide
2434 * Register a NetworkAgent with ConnectivityService.
2435 * @return NetID corresponding to NetworkAgent.
2436 */
2437 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002438 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002439 try {
Paul Jensen31a94f42015-02-13 14:18:39 -05002440 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
2441 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002442 throw e.rethrowFromSystemServer();
Paul Jensen31a94f42015-02-13 14:18:39 -05002443 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002444 }
2445
Robert Greenwalt9258c642014-03-26 16:47:06 -07002446 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002447 * Base class for NetworkRequest callbacks. Used for notifications about network
2448 * changes. Should be extended by applications wanting notifications.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002449 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002450 public static class NetworkCallback {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002451 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002452 * Called when the framework connects to a new network to evaluate whether it satisfies this
2453 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
2454 * callback. There is no guarantee that this new network will satisfy any requests, or that
2455 * the network will stay connected for longer than the time necessary to evaluate it.
2456 * <p>
2457 * Most applications <b>should not</b> act on this callback, and should instead use
2458 * {@link #onAvailable}. This callback is intended for use by applications that can assist
2459 * the framework in properly evaluating the network &mdash; for example, an application that
2460 * can automatically log in to a captive portal without user intervention.
2461 *
2462 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti66276122015-06-11 14:27:17 +09002463 *
2464 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002465 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002466 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002467
2468 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002469 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002470 * This callback may be called more than once if the {@link Network} that is
2471 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002472 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002473 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002474 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002475 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002476
2477 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002478 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002479 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002480 * for graceful handover. This may not be called if we have a hard loss
2481 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002482 * {@link NetworkCallback#onLost} call or a
2483 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002484 * on whether we lose or regain it.
2485 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002486 * @param network The {@link Network} that is about to be disconnected.
2487 * @param maxMsToLive The time in ms the framework will attempt to keep the
2488 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002489 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002490 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002491 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002492
2493 /**
2494 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002495 * graceful failure ends.
2496 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002497 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002498 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002499 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002500
2501 /**
2502 * Called if no network is found in the given timeout time. If no timeout is given,
2503 * this will not be called.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002504 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002505 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002506 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002507
2508 /**
2509 * Called when the network the framework connected to for this request
2510 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002511 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002512 * @param network The {@link Network} whose capabilities have changed.
Lorenzo Colittie285b432015-04-23 15:32:42 +09002513 * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002514 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002515 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002516 NetworkCapabilities networkCapabilities) {}
2517
2518 /**
2519 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002520 * changes {@link LinkProperties}.
2521 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002522 * @param network The {@link Network} whose link properties have changed.
2523 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002524 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002525 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002526
Robert Greenwalt8d482522015-06-24 13:23:42 -07002527 /**
2528 * Called when the network the framework connected to for this request
2529 * goes into {@link NetworkInfo.DetailedState.SUSPENDED}.
2530 * This generally means that while the TCP connections are still live,
2531 * temporarily network data fails to transfer. Specifically this is used
2532 * on cellular networks to mask temporary outages when driving through
2533 * a tunnel, etc.
2534 * @hide
2535 */
2536 public void onNetworkSuspended(Network network) {}
2537
2538 /**
2539 * Called when the network the framework connected to for this request
2540 * returns from a {@link NetworkInfo.DetailedState.SUSPENDED} state.
2541 * This should always be preceeded by a matching {@code onNetworkSuspended}
2542 * call.
2543 * @hide
2544 */
2545 public void onNetworkResumed(Network network) {}
2546
Robert Greenwalt6078b502014-06-11 16:05:07 -07002547 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002548 }
2549
Robert Greenwalt9258c642014-03-26 16:47:06 -07002550 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002551 /** @hide */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002552 public static final int CALLBACK_PRECHECK = BASE + 1;
2553 /** @hide */
2554 public static final int CALLBACK_AVAILABLE = BASE + 2;
2555 /** @hide arg1 = TTL */
2556 public static final int CALLBACK_LOSING = BASE + 3;
2557 /** @hide */
2558 public static final int CALLBACK_LOST = BASE + 4;
2559 /** @hide */
2560 public static final int CALLBACK_UNAVAIL = BASE + 5;
2561 /** @hide */
2562 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2563 /** @hide */
2564 public static final int CALLBACK_IP_CHANGED = BASE + 7;
2565 /** @hide */
2566 public static final int CALLBACK_RELEASED = BASE + 8;
2567 /** @hide */
2568 public static final int CALLBACK_EXIT = BASE + 9;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002569 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002570 private static final int EXPIRE_LEGACY_REQUEST = BASE + 10;
2571 /** @hide */
2572 public static final int CALLBACK_SUSPENDED = BASE + 11;
2573 /** @hide */
2574 public static final int CALLBACK_RESUMED = BASE + 12;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002575
Robert Greenwalt562cc542014-05-15 18:07:26 -07002576 private class CallbackHandler extends Handler {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002577 private final HashMap<NetworkRequest, NetworkCallback>mCallbackMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002578 private final AtomicInteger mRefCount;
2579 private static final String TAG = "ConnectivityManager.CallbackHandler";
2580 private final ConnectivityManager mCm;
Robert Greenwalta9ebeef2015-09-03 16:41:45 -07002581 private static final boolean DBG = false;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002582
Robert Greenwalt6078b502014-06-11 16:05:07 -07002583 CallbackHandler(Looper looper, HashMap<NetworkRequest, NetworkCallback>callbackMap,
Robert Greenwalt9258c642014-03-26 16:47:06 -07002584 AtomicInteger refCount, ConnectivityManager cm) {
2585 super(looper);
2586 mCallbackMap = callbackMap;
2587 mRefCount = refCount;
2588 mCm = cm;
2589 }
2590
2591 @Override
2592 public void handleMessage(Message message) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002593 NetworkRequest request = (NetworkRequest) getObject(message, NetworkRequest.class);
2594 Network network = (Network) getObject(message, Network.class);
Lorenzo Colittifcfa7d92016-03-01 22:56:37 +09002595 if (DBG) {
2596 Log.d(TAG, whatToString(message.what) + " for network " + network);
2597 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002598 switch (message.what) {
2599 case CALLBACK_PRECHECK: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002600 NetworkCallback callback = getCallback(request, "PRECHECK");
2601 if (callback != null) {
2602 callback.onPreCheck(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002603 }
2604 break;
2605 }
2606 case CALLBACK_AVAILABLE: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002607 NetworkCallback callback = getCallback(request, "AVAILABLE");
2608 if (callback != null) {
2609 callback.onAvailable(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002610 }
2611 break;
2612 }
2613 case CALLBACK_LOSING: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002614 NetworkCallback callback = getCallback(request, "LOSING");
2615 if (callback != null) {
2616 callback.onLosing(network, message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002617 }
2618 break;
2619 }
2620 case CALLBACK_LOST: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002621 NetworkCallback callback = getCallback(request, "LOST");
2622 if (callback != null) {
2623 callback.onLost(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002624 }
2625 break;
2626 }
2627 case CALLBACK_UNAVAIL: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002628 NetworkCallback callback = getCallback(request, "UNAVAIL");
2629 if (callback != null) {
2630 callback.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002631 }
2632 break;
2633 }
2634 case CALLBACK_CAP_CHANGED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002635 NetworkCallback callback = getCallback(request, "CAP_CHANGED");
2636 if (callback != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002637 NetworkCapabilities cap = (NetworkCapabilities)getObject(message,
2638 NetworkCapabilities.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002639
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002640 callback.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002641 }
2642 break;
2643 }
2644 case CALLBACK_IP_CHANGED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002645 NetworkCallback callback = getCallback(request, "IP_CHANGED");
2646 if (callback != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002647 LinkProperties lp = (LinkProperties)getObject(message,
2648 LinkProperties.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002649
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002650 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002651 }
2652 break;
2653 }
Robert Greenwalt8d482522015-06-24 13:23:42 -07002654 case CALLBACK_SUSPENDED: {
2655 NetworkCallback callback = getCallback(request, "SUSPENDED");
2656 if (callback != null) {
2657 callback.onNetworkSuspended(network);
2658 }
2659 break;
2660 }
2661 case CALLBACK_RESUMED: {
2662 NetworkCallback callback = getCallback(request, "RESUMED");
2663 if (callback != null) {
2664 callback.onNetworkResumed(network);
2665 }
2666 break;
2667 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002668 case CALLBACK_RELEASED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002669 NetworkCallback callback = null;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002670 synchronized(mCallbackMap) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002671 callback = mCallbackMap.remove(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002672 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002673 if (callback != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002674 synchronized(mRefCount) {
2675 if (mRefCount.decrementAndGet() == 0) {
2676 getLooper().quit();
2677 }
2678 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002679 } else {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002680 Log.e(TAG, "callback not found for RELEASED message");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002681 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002682 break;
2683 }
2684 case CALLBACK_EXIT: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002685 Log.d(TAG, "Listener quitting");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002686 getLooper().quit();
2687 break;
2688 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07002689 case EXPIRE_LEGACY_REQUEST: {
2690 expireRequest((NetworkCapabilities)message.obj, message.arg1);
2691 break;
2692 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002693 }
2694 }
2695
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002696 private Object getObject(Message msg, Class c) {
2697 return msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07002698 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002699
2700 private NetworkCallback getCallback(NetworkRequest req, String name) {
2701 NetworkCallback callback;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002702 synchronized(mCallbackMap) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002703 callback = mCallbackMap.get(req);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002704 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002705 if (callback == null) {
2706 Log.e(TAG, "callback not found for " + name + " message");
2707 }
2708 return callback;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002709 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002710 }
2711
Robert Greenwalt6078b502014-06-11 16:05:07 -07002712 private void incCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002713 synchronized(sCallbackRefCount) {
2714 if (sCallbackRefCount.incrementAndGet() == 1) {
2715 // TODO - switch this over to a ManagerThread or expire it when done
2716 HandlerThread callbackThread = new HandlerThread("ConnectivityManager");
2717 callbackThread.start();
2718 sCallbackHandler = new CallbackHandler(callbackThread.getLooper(),
Robert Greenwalt6078b502014-06-11 16:05:07 -07002719 sNetworkCallback, sCallbackRefCount, this);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002720 }
2721 }
2722 }
2723
Robert Greenwalt6078b502014-06-11 16:05:07 -07002724 private void decCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002725 synchronized(sCallbackRefCount) {
2726 if (sCallbackRefCount.decrementAndGet() == 0) {
2727 sCallbackHandler.obtainMessage(CALLBACK_EXIT).sendToTarget();
2728 sCallbackHandler = null;
2729 }
2730 }
2731 }
2732
Robert Greenwalt6078b502014-06-11 16:05:07 -07002733 static final HashMap<NetworkRequest, NetworkCallback> sNetworkCallback =
2734 new HashMap<NetworkRequest, NetworkCallback>();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002735 static final AtomicInteger sCallbackRefCount = new AtomicInteger(0);
2736 static CallbackHandler sCallbackHandler = null;
2737
2738 private final static int LISTEN = 1;
2739 private final static int REQUEST = 2;
2740
Robert Greenwalt562cc542014-05-15 18:07:26 -07002741 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002742 NetworkCallback networkCallback, int timeoutSec, int action,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07002743 int legacyType) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002744 if (networkCallback == null) {
2745 throw new IllegalArgumentException("null NetworkCallback");
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002746 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002747 if (need == null) throw new IllegalArgumentException("null NetworkCapabilities");
2748 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002749 incCallbackHandlerRefCount();
Paul Jensen7221cc32014-06-27 11:05:32 -04002750 synchronized(sNetworkCallback) {
2751 if (action == LISTEN) {
2752 networkCallback.networkRequest = mService.listenForNetwork(need,
2753 new Messenger(sCallbackHandler), new Binder());
2754 } else {
2755 networkCallback.networkRequest = mService.requestNetwork(need,
2756 new Messenger(sCallbackHandler), timeoutSec, new Binder(), legacyType);
2757 }
2758 if (networkCallback.networkRequest != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002759 sNetworkCallback.put(networkCallback.networkRequest, networkCallback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002760 }
2761 }
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002762 } catch (RemoteException e) {
2763 throw e.rethrowFromSystemServer();
2764 }
Robert Greenwalt6078b502014-06-11 16:05:07 -07002765 if (networkCallback.networkRequest == null) decCallbackHandlerRefCount();
2766 return networkCallback.networkRequest;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002767 }
2768
2769 /**
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002770 * Helper function to requests a network with a particular legacy type.
2771 *
2772 * This is temporarily public @hide so it can be called by system code that uses the
2773 * NetworkRequest API to request networks but relies on CONNECTIVITY_ACTION broadcasts for
2774 * instead network notifications.
2775 *
2776 * TODO: update said system code to rely on NetworkCallbacks and make this method private.
2777 *
2778 * @hide
2779 */
Lorenzo Colittid1179462015-11-25 15:47:14 +09002780 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002781 int timeoutMs, int legacyType) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002782 sendRequestForNetwork(request.networkCapabilities, networkCallback, timeoutMs, REQUEST,
2783 legacyType);
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002784 }
2785
2786 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002787 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002788 *
2789 * This {@link NetworkRequest} will live until released via
Robert Greenwalt6078b502014-06-11 16:05:07 -07002790 * {@link #unregisterNetworkCallback} or the calling application exits.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002791 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07002792 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002793 * can be used to direct traffic to the network.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002794 * <p>It is presently unsupported to request a network with mutable
2795 * {@link NetworkCapabilities} such as
2796 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2797 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
2798 * as these {@code NetworkCapabilities} represent states that a particular
2799 * network may never attain, and whether a network will attain these states
2800 * is unknown prior to bringing up the network so the framework does not
2801 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002802 *
2803 * <p>This method requires the caller to hold either the
2804 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2805 * or the ability to modify system settings as determined by
2806 * {@link android.provider.Settings.System#canWrite}.</p>
Robert Greenwalt9258c642014-03-26 16:47:06 -07002807 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002808 * @param request {@link NetworkRequest} describing this request.
2809 * @param networkCallback The {@link NetworkCallback} to be utilized for this
2810 * request. Note the callback must not be shared - they
2811 * uniquely specify this request.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002812 * @throws IllegalArgumentException if {@code request} specifies any mutable
2813 * {@code NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002814 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002815 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002816 requestNetwork(request, networkCallback, 0,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002817 inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002818 }
2819
2820 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002821 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
Robert Greenwalt9258c642014-03-26 16:47:06 -07002822 * by a timeout.
2823 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002824 * This function behaves identically to the non-timedout version, but if a suitable
Robert Greenwalt6078b502014-06-11 16:05:07 -07002825 * network is not found within the given time (in milliseconds) the
2826 * {@link NetworkCallback#unavailable} callback is called. The request must
Lorenzo Colitti36728a92015-11-26 11:03:17 +09002827 * still be released normally by calling {@link unregisterNetworkCallback}.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002828 *
2829 * <p>This method requires the caller to hold either the
2830 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2831 * or the ability to modify system settings as determined by
2832 * {@link android.provider.Settings.System#canWrite}.</p>
2833 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002834 * @param request {@link NetworkRequest} describing this request.
2835 * @param networkCallback The callbacks to be utilized for this request. Note
2836 * the callbacks must not be shared - they uniquely specify
2837 * this request.
2838 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
2839 * before {@link NetworkCallback#unavailable} is called.
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002840 *
2841 * TODO: Make timeouts work and then unhide this method.
2842 *
Robert Greenwalt9258c642014-03-26 16:47:06 -07002843 * @hide
2844 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002845 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
2846 int timeoutMs) {
Lorenzo Colittid1179462015-11-25 15:47:14 +09002847 requestNetwork(request, networkCallback, timeoutMs,
Lorenzo Colitti7de289f2015-11-25 12:00:52 +09002848 inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002849 }
2850
2851 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002852 * The maximum number of milliseconds the framework will look for a suitable network
Robert Greenwalt9258c642014-03-26 16:47:06 -07002853 * during a timeout-equiped call to {@link requestNetwork}.
2854 * {@hide}
2855 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002856 public final static int MAX_NETWORK_REQUEST_TIMEOUT_MS = 100 * 60 * 1000;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002857
2858 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002859 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002860 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002861 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfcde58f2015-02-11 16:51:13 -08002862 * <p>
Paul Jensen72db88e2015-03-10 10:54:12 -04002863 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
2864 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002865 */
Erik Kline90e93072014-11-19 12:12:24 +09002866 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002867
2868 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002869 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002870 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002871 * {@link android.content.Intent#getParcelableExtra(String)}.
2872 */
Erik Kline90e93072014-11-19 12:12:24 +09002873 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002874
2875
2876 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002877 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002878 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002879 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07002880 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002881 * the request may outlive the calling application and get called back when a suitable
2882 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002883 * <p>
2884 * The operation is an Intent broadcast that goes to a broadcast receiver that
2885 * you registered with {@link Context#registerReceiver} or through the
2886 * &lt;receiver&gt; tag in an AndroidManifest.xml file
2887 * <p>
2888 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09002889 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
2890 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07002891 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002892 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07002893 * Intent to reserve the network or it will be released shortly after the Intent
2894 * is processed.
2895 * <p>
Paul Jensen694f2b82015-06-17 14:15:39 -04002896 * If there is already a request for this Intent registered (with the equality of
Robert Greenwalt9258c642014-03-26 16:47:06 -07002897 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002898 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002899 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002900 * The request may be released normally by calling
2901 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002902 * <p>It is presently unsupported to request a network with either
2903 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2904 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
2905 * as these {@code NetworkCapabilities} represent states that a particular
2906 * network may never attain, and whether a network will attain these states
2907 * is unknown prior to bringing up the network so the framework does not
2908 * know how to go about satisfing a request with these capabilities.
Lorenzo Colittid5427052015-10-15 16:29:00 +09002909 *
2910 * <p>This method requires the caller to hold either the
2911 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission
2912 * or the ability to modify system settings as determined by
2913 * {@link android.provider.Settings.System#canWrite}.</p>
2914 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002915 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002916 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07002917 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002918 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002919 * @throws IllegalArgumentException if {@code request} contains either
2920 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2921 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002922 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002923 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002924 checkPendingIntent(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002925 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002926 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002927 } catch (RemoteException e) {
2928 throw e.rethrowFromSystemServer();
2929 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002930 }
2931
2932 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002933 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
2934 * <p>
2935 * This method has the same behavior as {@link #unregisterNetworkCallback} with respect to
2936 * releasing network resources and disconnecting.
2937 *
2938 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
2939 * PendingIntent passed to
2940 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
2941 * corresponding NetworkRequest you'd like to remove. Cannot be null.
2942 */
2943 public void releaseNetworkRequest(PendingIntent operation) {
2944 checkPendingIntent(operation);
2945 try {
2946 mService.releasePendingNetworkRequest(operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07002947 } catch (RemoteException e) {
2948 throw e.rethrowFromSystemServer();
2949 }
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002950 }
2951
2952 private void checkPendingIntent(PendingIntent intent) {
2953 if (intent == null) {
2954 throw new IllegalArgumentException("PendingIntent cannot be null.");
2955 }
2956 }
2957
2958 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07002959 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07002960 * {@link NetworkRequest}. The callbacks will continue to be called until
2961 * either the application exits or {@link #unregisterNetworkCallback} is called
Paul Jensenb2748922015-05-06 11:10:18 -04002962 * <p>This method requires the caller to hold the permission
2963 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002964 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002965 * @param request {@link NetworkRequest} describing this request.
2966 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
2967 * networks change state.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002968 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002969 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
2970 sendRequestForNetwork(request.networkCapabilities, networkCallback, 0, LISTEN, TYPE_NONE);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002971 }
2972
2973 /**
Paul Jensen694f2b82015-06-17 14:15:39 -04002974 * Registers a PendingIntent to be sent when a network is available which satisfies the given
2975 * {@link NetworkRequest}.
2976 *
2977 * This function behaves identically to the version that takes a NetworkCallback, but instead
2978 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
2979 * the request may outlive the calling application and get called back when a suitable
2980 * network is found.
2981 * <p>
2982 * The operation is an Intent broadcast that goes to a broadcast receiver that
2983 * you registered with {@link Context#registerReceiver} or through the
2984 * &lt;receiver&gt; tag in an AndroidManifest.xml file
2985 * <p>
2986 * The operation Intent is delivered with two extras, a {@link Network} typed
2987 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
2988 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
2989 * the original requests parameters.
2990 * <p>
2991 * If there is already a request for this Intent registered (with the equality of
2992 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
2993 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
2994 * <p>
2995 * The request may be released normally by calling
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04002996 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Paul Jensen694f2b82015-06-17 14:15:39 -04002997 * <p>This method requires the caller to hold the permission
2998 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
2999 * @param request {@link NetworkRequest} describing this request.
3000 * @param operation Action to perform when the network is available (corresponds
3001 * to the {@link NetworkCallback#onAvailable} call. Typically
3002 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
3003 */
3004 public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) {
3005 checkPendingIntent(operation);
3006 try {
3007 mService.pendingListenForNetwork(request.networkCapabilities, operation);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003008 } catch (RemoteException e) {
3009 throw e.rethrowFromSystemServer();
3010 }
Paul Jensen694f2b82015-06-17 14:15:39 -04003011 }
3012
3013 /**
fengludb571472015-04-21 17:12:05 -07003014 * Requests bandwidth update for a given {@link Network} and returns whether the update request
3015 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
3016 * network connection for updated bandwidth information. The caller will be notified via
3017 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
3018 * method assumes that the caller has previously called {@link #registerNetworkCallback} to
3019 * listen for network changes.
fenglub15e72b2015-03-20 11:29:56 -07003020 *
fengluae519192015-04-27 14:28:04 -07003021 * @param network {@link Network} specifying which network you're interested.
fengludb571472015-04-21 17:12:05 -07003022 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglub15e72b2015-03-20 11:29:56 -07003023 */
fengludb571472015-04-21 17:12:05 -07003024 public boolean requestBandwidthUpdate(Network network) {
fenglub15e72b2015-03-20 11:29:56 -07003025 try {
fengludb571472015-04-21 17:12:05 -07003026 return mService.requestBandwidthUpdate(network);
fenglub15e72b2015-03-20 11:29:56 -07003027 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003028 throw e.rethrowFromSystemServer();
fenglub15e72b2015-03-20 11:29:56 -07003029 }
3030 }
3031
3032 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07003033 * Unregisters callbacks about and possibly releases networks originating from
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09003034 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and {@link #registerNetworkCallback}
3035 * calls. If the given {@code NetworkCallback} had previously been used with
3036 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
3037 * will be disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003038 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07003039 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07003040 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07003041 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
3042 if (networkCallback == null || networkCallback.networkRequest == null ||
3043 networkCallback.networkRequest.requestId == REQUEST_ID_UNSET) {
3044 throw new IllegalArgumentException("Invalid NetworkCallback");
3045 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003046 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07003047 mService.releaseNetworkRequest(networkCallback.networkRequest);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003048 } catch (RemoteException e) {
3049 throw e.rethrowFromSystemServer();
3050 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07003051 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003052
3053 /**
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04003054 * Unregisters a callback previously registered via
3055 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3056 *
3057 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
3058 * PendingIntent passed to
3059 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
3060 * Cannot be null.
3061 */
3062 public void unregisterNetworkCallback(PendingIntent operation) {
3063 releaseNetworkRequest(operation);
3064 }
3065
3066 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003067 * Informs the system whether it should switch to {@code network} regardless of whether it is
3068 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
3069 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
3070 * the system default network regardless of any other network that's currently connected. If
3071 * {@code always} is true, then the choice is remembered, so that the next time the user
3072 * connects to this network, the system will switch to it.
3073 *
3074 * <p>This method requires the caller to hold the permission
3075 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
3076 *
3077 * @param network The network to accept.
3078 * @param accept Whether to accept the network even if unvalidated.
3079 * @param always Whether to remember this choice in the future.
3080 *
3081 * @hide
3082 */
3083 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
3084 try {
3085 mService.setAcceptUnvalidated(network, accept, always);
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003086 } catch (RemoteException e) {
3087 throw e.rethrowFromSystemServer();
3088 }
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09003089 }
3090
3091 /**
Stuart Scott984dc852015-03-30 13:17:11 -07003092 * Resets all connectivity manager settings back to factory defaults.
3093 * @hide
3094 */
3095 public void factoryReset() {
Stuart Scott984dc852015-03-30 13:17:11 -07003096 try {
Stuart Scottf1fb3972015-04-02 18:00:02 -07003097 mService.factoryReset();
Stuart Scott984dc852015-03-30 13:17:11 -07003098 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003099 throw e.rethrowFromSystemServer();
Stuart Scott984dc852015-03-30 13:17:11 -07003100 }
3101 }
3102
3103 /**
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003104 * Binds the current process to {@code network}. All Sockets created in the future
3105 * (and not explicitly bound via a bound SocketFactory from
3106 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3107 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3108 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3109 * work and all host name resolutions will fail. This is by design so an application doesn't
3110 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3111 * To clear binding pass {@code null} for {@code network}. Using individually bound
3112 * Sockets created by Network.getSocketFactory().createSocket() and
3113 * performing network-specific host name resolutions via
3114 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen72db88e2015-03-10 10:54:12 -04003115 * {@code bindProcessToNetwork}.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003116 *
3117 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3118 * the current binding.
3119 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3120 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003121 public boolean bindProcessToNetwork(Network network) {
3122 // Forcing callers to call thru non-static function ensures ConnectivityManager
3123 // instantiated.
3124 return setProcessDefaultNetwork(network);
3125 }
3126
3127 /**
3128 * Binds the current process to {@code network}. All Sockets created in the future
3129 * (and not explicitly bound via a bound SocketFactory from
3130 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
3131 * {@code network}. All host name resolutions will be limited to {@code network} as well.
3132 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
3133 * work and all host name resolutions will fail. This is by design so an application doesn't
3134 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
3135 * To clear binding pass {@code null} for {@code network}. Using individually bound
3136 * Sockets created by Network.getSocketFactory().createSocket() and
3137 * performing network-specific host name resolutions via
3138 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
3139 * {@code setProcessDefaultNetwork}.
3140 *
3141 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
3142 * the current binding.
3143 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3144 * @deprecated This function can throw {@link IllegalStateException}. Use
3145 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
3146 * is a direct replacement.
3147 */
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003148 public static boolean setProcessDefaultNetwork(Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003149 int netId = (network == null) ? NETID_UNSET : network.netId;
Paul Jensen72db88e2015-03-10 10:54:12 -04003150 if (netId == NetworkUtils.getBoundNetworkForProcess()) {
Paul Jensenc91b5342014-08-27 12:38:45 -04003151 return true;
3152 }
3153 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05003154 // Set HTTP proxy system properties to match network.
3155 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colittiec4c5552015-04-22 11:52:48 +09003156 try {
3157 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
3158 } catch (SecurityException e) {
3159 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
3160 Log.e(TAG, "Can't set proxy properties", e);
3161 }
Paul Jensenc91b5342014-08-27 12:38:45 -04003162 // Must flush DNS cache as new network may have different DNS resolutions.
3163 InetAddress.clearDnsCache();
3164 // Must flush socket pool as idle sockets will be bound to previous network and may
3165 // cause subsequent fetches to be performed on old network.
3166 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
3167 return true;
3168 } else {
3169 return false;
3170 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003171 }
3172
3173 /**
3174 * Returns the {@link Network} currently bound to this process via
Paul Jensen72db88e2015-03-10 10:54:12 -04003175 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003176 *
3177 * @return {@code Network} to which this process is bound, or {@code null}.
3178 */
Paul Jensen72db88e2015-03-10 10:54:12 -04003179 public Network getBoundNetworkForProcess() {
3180 // Forcing callers to call thru non-static function ensures ConnectivityManager
3181 // instantiated.
3182 return getProcessDefaultNetwork();
3183 }
3184
3185 /**
3186 * Returns the {@link Network} currently bound to this process via
3187 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
3188 *
3189 * @return {@code Network} to which this process is bound, or {@code null}.
3190 * @deprecated Using this function can lead to other functions throwing
3191 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
3192 * {@code getBoundNetworkForProcess} is a direct replacement.
3193 */
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003194 public static Network getProcessDefaultNetwork() {
Paul Jensen72db88e2015-03-10 10:54:12 -04003195 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04003196 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003197 return new Network(netId);
3198 }
3199
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003200 private void unsupportedStartingFrom(int version) {
3201 if (Process.myUid() == Process.SYSTEM_UID) {
3202 // The getApplicationInfo() call we make below is not supported in system context, and
3203 // we want to allow the system to use these APIs anyway.
3204 return;
3205 }
3206
3207 if (mContext.getApplicationInfo().targetSdkVersion >= version) {
3208 throw new UnsupportedOperationException(
3209 "This method is not supported in target SDK version " + version + " and above");
3210 }
3211 }
3212
3213 // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature,
3214 // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException.
Lifu Tang30f95a72016-01-07 23:20:38 -08003215 // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003216 // remove these exemptions. Note that this check is not secure, and apps can still access these
3217 // functions by accessing ConnectivityService directly. However, it should be clear that doing
3218 // so is unsupported and may break in the future. http://b/22728205
3219 private void checkLegacyRoutingApiAccess() {
3220 if (mContext.checkCallingOrSelfPermission("com.android.permission.INJECT_OMADM_SETTINGS")
3221 == PackageManager.PERMISSION_GRANTED) {
3222 return;
3223 }
3224
Dianne Hackborn692a2442015-07-31 10:35:34 -07003225 unsupportedStartingFrom(VERSION_CODES.M);
Lorenzo Colittiffc42b02015-07-29 11:41:21 +09003226 }
3227
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003228 /**
3229 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen72db88e2015-03-10 10:54:12 -04003230 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003231 *
3232 * @param network The {@link Network} to bind host resolutions from the current process to, or
3233 * {@code null} to clear the current binding.
3234 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
3235 * @hide
3236 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
3237 */
3238 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04003239 return NetworkUtils.bindProcessToNetworkForHostResolution(
3240 network == null ? NETID_UNSET : network.netId);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04003241 }
Felipe Leme1b103232016-01-22 09:44:57 -08003242
3243 /**
3244 * Device is not restricting metered network activity while application is running on
3245 * background.
3246 */
3247 public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1;
3248
3249 /**
3250 * Device is restricting metered network activity while application is running on background,
3251 * but application is allowed to bypass it.
3252 * <p>
3253 * In this state, application should take action to mitigate metered network access.
3254 * For example, a music streaming application should switch to a low-bandwidth bitrate.
3255 */
3256 public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2;
3257
3258 /**
3259 * Device is restricting metered network activity while application is running on background.
Felipe Leme9778f762016-01-27 14:46:39 -08003260 * <p>
Felipe Leme1b103232016-01-22 09:44:57 -08003261 * In this state, application should not try to use the network while running on background,
3262 * because it would be denied.
3263 */
3264 public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3;
3265
Felipe Leme9778f762016-01-27 14:46:39 -08003266 /**
3267 * A change in the background metered network activity restriction has occurred.
3268 * <p>
3269 * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction
3270 * applies to them.
3271 * <p>
3272 * This is only sent to registered receivers, not manifest receivers.
3273 */
3274 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
3275 public static final String ACTION_RESTRICT_BACKGROUND_CHANGED =
3276 "android.net.conn.RESTRICT_BACKGROUND_CHANGED";
3277
Felipe Lemeecfccea2016-01-25 11:48:04 -08003278 /** @hide */
3279 @Retention(RetentionPolicy.SOURCE)
Felipe Leme1b103232016-01-22 09:44:57 -08003280 @IntDef(flag = false, value = {
3281 RESTRICT_BACKGROUND_STATUS_DISABLED,
3282 RESTRICT_BACKGROUND_STATUS_WHITELISTED,
3283 RESTRICT_BACKGROUND_STATUS_ENABLED,
3284 })
Felipe Leme1b103232016-01-22 09:44:57 -08003285 public @interface RestrictBackgroundStatus {
3286 }
3287
3288 private INetworkPolicyManager getNetworkPolicyManager() {
3289 synchronized (this) {
3290 if (mNPManager != null) {
3291 return mNPManager;
3292 }
3293 mNPManager = INetworkPolicyManager.Stub.asInterface(ServiceManager
3294 .getService(Context.NETWORK_POLICY_SERVICE));
3295 return mNPManager;
3296 }
3297 }
3298
3299 /**
3300 * Determines if the calling application is subject to metered network restrictions while
3301 * running on background.
3302 */
3303 public @RestrictBackgroundStatus int getRestrictBackgroundStatus() {
3304 try {
3305 return getNetworkPolicyManager().getRestrictBackgroundByCaller();
3306 } catch (RemoteException e) {
Jeff Sharkeyc53962d2016-03-01 19:27:23 -07003307 throw e.rethrowFromSystemServer();
Felipe Leme1b103232016-01-22 09:44:57 -08003308 }
3309 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08003310}