blob: 7b758bb80ff851df574d10b16428b0d4c3197ef6 [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;
19
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.annotation.SdkConstant;
21import android.annotation.SdkConstant.SdkConstantType;
Robert Greenwalt9258c642014-03-26 16:47:06 -070022import android.app.PendingIntent;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070023import android.content.Context;
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070024import android.content.Intent;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -040025import android.net.NetworkUtils;
Robert Greenwalt42acef32009-08-12 16:08:25 -070026import android.os.Binder;
Jeff Sharkey3a844fc2011-08-16 14:37:57 -070027import android.os.Build.VERSION_CODES;
Robert Greenwalta848c1c2014-09-30 16:50:07 -070028import android.os.Bundle;
Robert Greenwalt9258c642014-03-26 16:47:06 -070029import android.os.Handler;
30import android.os.HandlerThread;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080031import android.os.IBinder;
32import android.os.INetworkActivityListener;
33import android.os.INetworkManagementService;
Robert Greenwalt9258c642014-03-26 16:47:06 -070034import android.os.Looper;
35import android.os.Message;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -070036import android.os.Messenger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.RemoteException;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080038import android.os.ServiceManager;
Jeff Sharkey961e3042011-08-29 16:02:57 -070039import android.provider.Settings;
Wink Saville36ffb042014-12-05 11:10:30 -080040import android.telephony.SubscriptionManager;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070041import android.telephony.TelephonyManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080042import android.util.ArrayMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -070043import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
Robert Greenwaltafa05c02014-05-21 20:04:36 -070045import com.android.internal.telephony.ITelephony;
Robert Greenwalt562cc542014-05-15 18:07:26 -070046import com.android.internal.telephony.PhoneConstants;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070047import com.android.internal.util.Protocol;
48
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070049import java.net.InetAddress;
Robert Greenwalt9258c642014-03-26 16:47:06 -070050import java.util.concurrent.atomic.AtomicInteger;
51import java.util.HashMap;
52
Paul Jensenc91b5342014-08-27 12:38:45 -040053import libcore.net.event.NetworkEventDispatcher;
54
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055/**
56 * Class that answers queries about the state of network connectivity. It also
57 * notifies applications when network connectivity changes. Get an instance
58 * of this class by calling
59 * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}.
60 * <p>
61 * The primary responsibilities of this class are to:
62 * <ol>
63 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
64 * <li>Send broadcast intents when network connectivity changes</li>
65 * <li>Attempt to "fail over" to another network when connectivity to a network
66 * is lost</li>
67 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
68 * state of the available networks</li>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070069 * <li>Provide an API that allows applications to request and select networks for their data
70 * traffic</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 * </ol>
72 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070073public class ConnectivityManager {
74 private static final String TAG = "ConnectivityManager";
75
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070077 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 * been established or lost. The NetworkInfo for the affected network is
79 * sent as an extra; it should be consulted to see what kind of
80 * connectivity event occurred.
81 * <p/>
82 * If this is a connection that was the result of failing over from a
83 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
84 * set to true.
85 * <p/>
86 * For a loss of connectivity, if the connectivity manager is attempting
87 * to connect (or has already connected) to another network, the
88 * NetworkInfo for the new network is also passed as an extra. This lets
89 * any receivers of the broadcast know that they should not necessarily
90 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -080091 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 * the failover attempt succeeded (and so there is still overall data
93 * connectivity), or that the failover attempt failed, meaning that all
94 * connectivity has been lost.
95 * <p/>
96 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
97 * is set to {@code true} if there are no connected networks at all.
98 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -080099 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 /**
Jeff Sharkey961e3042011-08-29 16:02:57 -0700103 * Identical to {@link #CONNECTIVITY_ACTION} broadcast, but sent without any
Erik Kline8f29dcf2014-12-08 16:25:20 +0900104 * historic {@link Settings.Global#CONNECTIVITY_CHANGE_DELAY}.
Jeff Sharkey961e3042011-08-29 16:02:57 -0700105 *
106 * @hide
107 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800108 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey961e3042011-08-29 16:02:57 -0700109 public static final String CONNECTIVITY_ACTION_IMMEDIATE =
110 "android.net.conn.CONNECTIVITY_CHANGE_IMMEDIATE";
111
112 /**
Paul Jensen25a217c2015-02-27 22:55:47 -0500113 * The device has connected to a network that has presented a captive
114 * portal, which is blocking Internet connectivity. The user was presented
115 * with a notification that network sign in is required,
116 * and the user invoked the notification's action indicating they
117 * desire to sign in to the network. Apps handling this action should
118 * facilitate signing in to the network. This action includes a
119 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents
120 * the network presenting the captive portal; all communication with the
121 * captive portal must be done using this {@code Network} object.
122 * <p/>
123 * When the app handling this action believes the user has signed in to
124 * the network and the captive portal has been dismissed, the app should call
125 * {@link #reportCaptivePortalDismissed} so the system can reevaluate the network.
126 * If reevaluation finds the network no longer subject to a captive portal,
127 * the network may become the default active data network.
128 * <p/>
129 * When the app handling this action believes the user explicitly wants
130 * to ignore the captive portal and the network, the app should call
131 * {@link #ignoreNetworkWithCaptivePortal}.
132 * <p/>
133 * Note that this action includes a {@code String} extra named
134 * {@link #EXTRA_CAPTIVE_PORTAL_TOKEN} that must
135 * be passed in to {@link #reportCaptivePortalDismissed} and
136 * {@link #ignoreNetworkWithCaptivePortal}.
137 */
138 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
139 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL";
140
141 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800142 * The lookup key for a {@link NetworkInfo} object. Retrieve with
143 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700144 *
145 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
146 * should always obtain network information through
Paul Jensen3541e9f2015-03-18 12:23:02 -0400147 * {@link #getActiveNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700148 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700150 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800151 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700152
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700154 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700155 *
156 * @see android.content.Intent#getIntExtra(String, int)
157 */
158 public static final String EXTRA_NETWORK_TYPE = "networkType";
159
160 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 * The lookup key for a boolean that indicates whether a connect event
162 * is for a network to which the connectivity manager was failing over
163 * following a disconnect on another network.
164 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
165 */
166 public static final String EXTRA_IS_FAILOVER = "isFailover";
167 /**
168 * The lookup key for a {@link NetworkInfo} object. This is supplied when
169 * there is another network that it may be possible to connect to. Retrieve with
170 * {@link android.content.Intent#getParcelableExtra(String)}.
171 */
172 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
173 /**
174 * The lookup key for a boolean that indicates whether there is a
175 * complete lack of connectivity, i.e., no network is available.
176 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
177 */
178 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
179 /**
180 * The lookup key for a string that indicates why an attempt to connect
181 * to a network failed. The string has no particular structure. It is
182 * intended to be used in notifications presented to users. Retrieve
183 * it with {@link android.content.Intent#getStringExtra(String)}.
184 */
185 public static final String EXTRA_REASON = "reason";
186 /**
187 * The lookup key for a string that provides optionally supplied
188 * extra information about the network state. The information
189 * may be passed up from the lower networking layers, and its
190 * meaning may be specific to a particular network type. Retrieve
191 * it with {@link android.content.Intent#getStringExtra(String)}.
192 */
193 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700194 /**
195 * The lookup key for an int that provides information about
196 * our connection to the internet at large. 0 indicates no connection,
197 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700198 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700199 * {@hide}
200 */
201 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202
203 /**
Paul Jensen25a217c2015-02-27 22:55:47 -0500204 * The lookup key for a string that is sent out with
205 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN}. This string must be
206 * passed in to {@link #reportCaptivePortalDismissed} and
207 * {@link #ignoreNetworkWithCaptivePortal}. Retrieve it with
208 * {@link android.content.Intent#getStringExtra(String)}.
209 */
210 public static final String EXTRA_CAPTIVE_PORTAL_TOKEN = "captivePortalToken";
211
212 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700213 * Broadcast action to indicate the change of data activity status
214 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800215 * The network becomes active when data transmission is started, or
216 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700217 * {@hide}
218 */
219 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
220 public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
221 /**
222 * The lookup key for an enum that indicates the network device type on which this data activity
223 * change happens.
224 * {@hide}
225 */
226 public static final String EXTRA_DEVICE_TYPE = "deviceType";
227 /**
228 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
229 * it is actively sending or receiving data and {@code false} means it is idle.
230 * {@hide}
231 */
232 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700233 /**
234 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
235 * {@hide}
236 */
237 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700238
239 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800240 * Broadcast Action: The setting for background data usage has changed
241 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
242 * <p>
243 * If an application uses the network in the background, it should listen
244 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700245 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800246 * <p>
247 *
248 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
249 * of background data depends on several combined factors, and
250 * this broadcast is no longer sent. Instead, when background
251 * data is unavailable, {@link #getActiveNetworkInfo()} will now
252 * appear disconnected. During first boot after a platform
253 * upgrade, this broadcast will be sent once if
254 * {@link #getBackgroundDataSetting()} was {@code false} before
255 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800256 */
257 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800258 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800259 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
260 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
261
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700262 /**
263 * Broadcast Action: The network connection may not be good
264 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
265 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
266 * the network and it's condition.
267 * @hide
268 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800269 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700270 public static final String INET_CONDITION_ACTION =
271 "android.net.conn.INET_CONDITION_ACTION";
272
Robert Greenwalt42acef32009-08-12 16:08:25 -0700273 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800274 * Broadcast Action: A tetherable connection has come or gone.
275 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
276 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER} and
277 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
278 * the current state of tethering. Each include a list of
279 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800280 * @hide
281 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800282 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800283 public static final String ACTION_TETHER_STATE_CHANGED =
284 "android.net.conn.TETHER_STATE_CHANGED";
285
286 /**
287 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800288 * gives a String[] listing all the interfaces configured for
289 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800290 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800291 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800292
293 /**
294 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800295 * gives a String[] listing all the interfaces currently tethered
296 * (ie, has dhcp support and packets potentially forwarded/NATed)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800297 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800298 public static final String EXTRA_ACTIVE_TETHER = "activeArray";
299
300 /**
301 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800302 * gives a String[] listing all the interfaces we tried to tether and
303 * failed. Use {@link #getLastTetherError} to find the error code
304 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800305 */
306 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800307
308 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800309 * Broadcast Action: The captive portal tracker has finished its test.
310 * Sent only while running Setup Wizard, in lieu of showing a user
311 * notification.
312 * @hide
313 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800314 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800315 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
316 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
317 /**
318 * The lookup key for a boolean that indicates whether a captive portal was detected.
319 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
320 * @hide
321 */
322 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
323
324 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800325 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700326 * @hide
327 */
328 public static final int TYPE_NONE = -1;
329
330 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800331 * The Mobile data connection. When active, all data traffic
332 * will use this network type's interface by default
333 * (it has a default route)
Robert Greenwalt42acef32009-08-12 16:08:25 -0700334 */
335 public static final int TYPE_MOBILE = 0;
336 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800337 * The WIFI data connection. When active, all data traffic
338 * will use this network type's interface by default
339 * (it has a default route).
Robert Greenwalt42acef32009-08-12 16:08:25 -0700340 */
341 public static final int TYPE_WIFI = 1;
342 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800343 * An MMS-specific Mobile data connection. This network type may use the
344 * same network interface as {@link #TYPE_MOBILE} or it may use a different
345 * one. This is used by applications needing to talk to the carrier's
346 * Multimedia Messaging Service servers.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700347 */
348 public static final int TYPE_MOBILE_MMS = 2;
349 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800350 * A SUPL-specific Mobile data connection. This network type may use the
351 * same network interface as {@link #TYPE_MOBILE} or it may use a different
352 * one. This is used by applications needing to talk to the carrier's
353 * Secure User Plane Location servers for help locating the device.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700354 */
355 public static final int TYPE_MOBILE_SUPL = 3;
356 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800357 * A DUN-specific Mobile data connection. This network type may use the
358 * same network interface as {@link #TYPE_MOBILE} or it may use a different
359 * one. This is sometimes by the system when setting up an upstream connection
360 * for tethering so that the carrier is aware of DUN traffic.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700361 */
362 public static final int TYPE_MOBILE_DUN = 4;
363 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800364 * A High Priority Mobile data connection. This network type uses the
365 * same network interface as {@link #TYPE_MOBILE} but the routing setup
366 * is different. Only requesting processes will have access to the
367 * Mobile DNS servers and only IP's explicitly requested via {@link #requestRouteToHost}
368 * will route over this interface if no default route exists.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700369 */
370 public static final int TYPE_MOBILE_HIPRI = 5;
jsh8214deb2010-03-11 15:04:43 -0800371 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800372 * The WiMAX data connection. When active, all data traffic
373 * will use this network type's interface by default
374 * (it has a default route).
jsh8214deb2010-03-11 15:04:43 -0800375 */
376 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800377
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800378 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800379 * The Bluetooth data connection. When active, all data traffic
380 * will use this network type's interface by default
381 * (it has a default route).
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800382 */
383 public static final int TYPE_BLUETOOTH = 7;
384
Robert Greenwalt60810842011-04-22 15:28:18 -0700385 /**
386 * Dummy data connection. This should not be used on shipping devices.
387 */
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800388 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800389
Robert Greenwalt60810842011-04-22 15:28:18 -0700390 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800391 * The Ethernet data connection. When active, all data traffic
392 * will use this network type's interface by default
393 * (it has a default route).
Robert Greenwalt60810842011-04-22 15:28:18 -0700394 */
Robert Greenwalte12aec92011-01-28 14:48:37 -0800395 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700396
Wink Saville9d7d6282011-03-12 14:52:01 -0800397 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800398 * Over the air Administration.
Wink Saville9d7d6282011-03-12 14:52:01 -0800399 * {@hide}
400 */
401 public static final int TYPE_MOBILE_FOTA = 10;
402
403 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800404 * IP Multimedia Subsystem.
Wink Saville9d7d6282011-03-12 14:52:01 -0800405 * {@hide}
406 */
407 public static final int TYPE_MOBILE_IMS = 11;
408
409 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800410 * Carrier Branded Services.
Wink Saville9d7d6282011-03-12 14:52:01 -0800411 * {@hide}
412 */
413 public static final int TYPE_MOBILE_CBS = 12;
414
repo syncaea743a2011-07-29 23:55:49 -0700415 /**
416 * A Wi-Fi p2p connection. Only requesting processes will have access to
417 * the peers connected.
418 * {@hide}
419 */
420 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800421
Wink Saville5e56bc52013-07-29 15:00:57 -0700422 /**
423 * The network to use for initially attaching to the network
424 * {@hide}
425 */
426 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700427
Ram3e0e3bc2014-06-26 11:03:44 -0700428/**
429 * Emergency PDN connection for emergency calls
430 * {@hide}
431 */
432 public static final int TYPE_MOBILE_EMERGENCY = 15;
433
Hui Lu1c5624a2014-01-15 11:05:36 -0500434 /**
435 * The network that uses proxy to achieve connectivity.
436 * {@hide}
437 */
438 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700439
Robert Greenwalt8283f882014-07-07 17:09:01 -0700440 /**
441 * A virtual network using one or more native bearers.
442 * It may or may not be providing security services.
443 */
444 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500445
446 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700447 public static final int MAX_RADIO_TYPE = TYPE_VPN;
448
449 /** {@hide} */
450 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800452 /**
453 * If you want to set the default network preference,you can directly
454 * change the networkAttributes array in framework's config.xml.
455 *
456 * @deprecated Since we support so many more networks now, the single
457 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800458 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800459 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800460 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800461 * from an App.
462 */
463 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800464 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
465
Jeff Sharkey625239a2012-09-26 22:03:49 -0700466 /**
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700467 * @hide
468 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700469 public final static int REQUEST_ID_UNSET = 0;
470
Paul Jensen5d59e782014-07-11 12:28:19 -0400471 /**
472 * A NetID indicating no Network is selected.
473 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
474 * @hide
475 */
476 public static final int NETID_UNSET = 0;
477
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700478 private final IConnectivityManager mService;
Paul Jensene0bef712014-12-10 15:12:18 -0500479 /**
480 * A kludge to facilitate static access where a Context pointer isn't available, like in the
481 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
482 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
483 * methods that take a Context argument.
484 */
485 private static ConnectivityManager sInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800486
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800487 private INetworkManagementService mNMService;
488
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800489 /**
490 * Tests if a given integer represents a valid network type.
491 * @param networkType the type to be tested
492 * @return a boolean. {@code true} if the type is valid, else {@code false}
493 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700494 public static boolean isNetworkTypeValid(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700495 return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800496 }
497
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800498 /**
499 * Returns a non-localized string representing a given network type.
500 * ONLY used for debugging output.
501 * @param type the type needing naming
502 * @return a String for the given type, or a string version of the type ("87")
503 * if no name is known.
504 * {@hide}
505 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700506 public static String getNetworkTypeName(int type) {
507 switch (type) {
508 case TYPE_MOBILE:
509 return "MOBILE";
510 case TYPE_WIFI:
511 return "WIFI";
512 case TYPE_MOBILE_MMS:
513 return "MOBILE_MMS";
514 case TYPE_MOBILE_SUPL:
515 return "MOBILE_SUPL";
516 case TYPE_MOBILE_DUN:
517 return "MOBILE_DUN";
518 case TYPE_MOBILE_HIPRI:
519 return "MOBILE_HIPRI";
520 case TYPE_WIMAX:
521 return "WIMAX";
522 case TYPE_BLUETOOTH:
523 return "BLUETOOTH";
524 case TYPE_DUMMY:
525 return "DUMMY";
526 case TYPE_ETHERNET:
527 return "ETHERNET";
528 case TYPE_MOBILE_FOTA:
529 return "MOBILE_FOTA";
530 case TYPE_MOBILE_IMS:
531 return "MOBILE_IMS";
532 case TYPE_MOBILE_CBS:
533 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700534 case TYPE_WIFI_P2P:
535 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700536 case TYPE_MOBILE_IA:
537 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700538 case TYPE_MOBILE_EMERGENCY:
539 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500540 case TYPE_PROXY:
541 return "PROXY";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700542 default:
543 return Integer.toString(type);
544 }
545 }
546
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800547 /**
548 * Checks if a given type uses the cellular data connection.
549 * This should be replaced in the future by a network property.
550 * @param networkType the type to check
551 * @return a boolean - {@code true} if uses cellular network, else {@code false}
552 * {@hide}
553 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700554 public static boolean isNetworkTypeMobile(int networkType) {
555 switch (networkType) {
556 case TYPE_MOBILE:
557 case TYPE_MOBILE_MMS:
558 case TYPE_MOBILE_SUPL:
559 case TYPE_MOBILE_DUN:
560 case TYPE_MOBILE_HIPRI:
561 case TYPE_MOBILE_FOTA:
562 case TYPE_MOBILE_IMS:
563 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700564 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700565 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700566 return true;
567 default:
568 return false;
569 }
570 }
571
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800572 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700573 * Checks if the given network type is backed by a Wi-Fi radio.
574 *
575 * @hide
576 */
577 public static boolean isNetworkTypeWifi(int networkType) {
578 switch (networkType) {
579 case TYPE_WIFI:
580 case TYPE_WIFI_P2P:
581 return true;
582 default:
583 return false;
584 }
585 }
586
587 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800588 * Specifies the preferred network type. When the device has more
589 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800590 *
591 * @param preference the network type to prefer over all others. It is
592 * unspecified what happens to the old preferred network in the
593 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700594 * @deprecated Functionality has been removed as it no longer makes sense,
595 * with many more than two networks - we'd need an array to express
596 * preference. Instead we use dynamic network properties of
597 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800598 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800599 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 }
601
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800602 /**
603 * Retrieves the current preferred network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800604 *
605 * @return an integer representing the preferred network type
606 *
607 * <p>This method requires the caller to hold the permission
608 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700609 * @deprecated Functionality has been removed as it no longer makes sense,
610 * with many more than two networks - we'd need an array to express
611 * preference. Instead we use dynamic network properties of
612 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800613 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800614 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700615 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800616 }
617
Scott Main671644c2011-10-06 19:02:28 -0700618 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800619 * Returns details about the currently active default data network. When
620 * connected, this network is the default route for outgoing connections.
621 * You should always check {@link NetworkInfo#isConnected()} before initiating
622 * network traffic. This may return {@code null} when there is no default
623 * network.
624 *
625 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500626 * or {@code null} if no default network is currently active
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800627 *
Paul Jensen0d719ca2015-02-13 14:18:39 -0500628 * <p>This method requires the caller to hold the permission
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700629 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700630 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800631 public NetworkInfo getActiveNetworkInfo() {
632 try {
633 return mService.getActiveNetworkInfo();
634 } catch (RemoteException e) {
635 return null;
636 }
637 }
638
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800639 /**
Paul Jensen31a94f42015-02-13 14:18:39 -0500640 * Returns a {@link Network} object corresponding to the currently active
641 * default data network. In the event that the current active default data
642 * network disconnects, the returned {@code Network} object will no longer
643 * be usable. This will return {@code null} when there is no default
644 * network.
645 *
646 * @return a {@link Network} object for the current default network or
647 * {@code null} if no default network is currently active
648 *
649 * <p>This method requires the caller to hold the permission
650 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
651 */
652 public Network getActiveNetwork() {
653 try {
654 return mService.getActiveNetwork();
655 } catch (RemoteException e) {
656 return null;
657 }
658 }
659
660 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800661 * Returns details about the currently active default data network
662 * for a given uid. This is for internal use only to avoid spying
663 * other apps.
664 *
665 * @return a {@link NetworkInfo} object for the current default network
666 * for the given uid or {@code null} if no default network is
667 * available for the specified uid.
668 *
669 * <p>This method requires the caller to hold the permission
670 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
671 * {@hide}
672 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700673 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
674 try {
675 return mService.getActiveNetworkInfoForUid(uid);
676 } catch (RemoteException e) {
677 return null;
678 }
679 }
680
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800681 /**
682 * Returns connection status information about a particular
683 * network type.
684 *
685 * @param networkType integer specifying which networkType in
686 * which you're interested.
687 * @return a {@link NetworkInfo} object for the requested
688 * network type or {@code null} if the type is not
689 * supported by the device.
690 *
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700691 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800692 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Paul Jensen3541e9f2015-03-18 12:23:02 -0400693 *
694 * @deprecated This method does not support multiple connected networks
695 * of the same type. Use {@link #getAllNetworks} and
696 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800697 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800698 public NetworkInfo getNetworkInfo(int networkType) {
699 try {
700 return mService.getNetworkInfo(networkType);
701 } catch (RemoteException e) {
702 return null;
703 }
704 }
705
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800706 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700707 * Returns connection status information about a particular
708 * Network.
709 *
710 * @param network {@link Network} specifying which network
711 * in which you're interested.
712 * @return a {@link NetworkInfo} object for the requested
713 * network or {@code null} if the {@code Network}
714 * is not valid.
715 *
716 * <p>This method requires the caller to hold the permission
717 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
718 */
719 public NetworkInfo getNetworkInfo(Network network) {
720 try {
721 return mService.getNetworkInfoForNetwork(network);
722 } catch (RemoteException e) {
723 return null;
724 }
725 }
726
727 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800728 * Returns connection status information about all network
729 * types supported by the device.
730 *
731 * @return an array of {@link NetworkInfo} objects. Check each
732 * {@link NetworkInfo#getType} for which type each applies.
733 *
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700734 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800735 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Paul Jensen3541e9f2015-03-18 12:23:02 -0400736 *
737 * @deprecated This method does not support multiple connected networks
738 * of the same type. Use {@link #getAllNetworks} and
739 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800740 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800741 public NetworkInfo[] getAllNetworkInfo() {
742 try {
743 return mService.getAllNetworkInfo();
744 } catch (RemoteException e) {
745 return null;
746 }
747 }
748
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800749 /**
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700750 * Returns the {@link Network} object currently serving a given type, or
751 * null if the given type is not connected.
752 *
753 * <p>This method requires the caller to hold the permission
754 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
755 *
756 * @hide
Paul Jensen3541e9f2015-03-18 12:23:02 -0400757 * @deprecated This method does not support multiple connected networks
758 * of the same type. Use {@link #getAllNetworks} and
759 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700760 */
761 public Network getNetworkForType(int networkType) {
762 try {
763 return mService.getNetworkForType(networkType);
764 } catch (RemoteException e) {
765 return null;
766 }
767 }
768
769 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700770 * Returns an array of all {@link Network} currently tracked by the
771 * framework.
772 *
773 * @return an array of {@link Network} objects.
774 *
775 * <p>This method requires the caller to hold the permission
776 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
777 */
778 public Network[] getAllNetworks() {
779 try {
780 return mService.getAllNetworks();
781 } catch (RemoteException e) {
782 return null;
783 }
784 }
785
786 /**
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900787 * Returns an array of of {@link NetworkCapabilities} objects, representing
788 * the Networks that applications run by the given user will use by default.
789 * @hide
790 */
791 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
792 try {
793 return mService.getDefaultNetworkCapabilitiesForUser(userId);
794 } catch (RemoteException e) {
795 return null;
796 }
797 }
798
799 /**
Wink Saville948282b2013-08-29 08:55:16 -0700800 * Returns details about the Provisioning or currently active default data network. When
801 * connected, this network is the default route for outgoing connections.
802 * You should always check {@link NetworkInfo#isConnected()} before initiating
803 * network traffic. This may return {@code null} when there is no default
804 * network.
805 *
806 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500807 * or {@code null} if no default network is currently active
Wink Saville948282b2013-08-29 08:55:16 -0700808 *
Paul Jensen0d719ca2015-02-13 14:18:39 -0500809 * <p>This method requires the caller to hold the permission
Wink Saville948282b2013-08-29 08:55:16 -0700810 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
811 *
812 * {@hide}
813 */
814 public NetworkInfo getProvisioningOrActiveNetworkInfo() {
815 try {
816 return mService.getProvisioningOrActiveNetworkInfo();
817 } catch (RemoteException e) {
818 return null;
819 }
820 }
821
822 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800823 * Returns the IP information for the current default network.
824 *
825 * @return a {@link LinkProperties} object describing the IP info
826 * for the current default network, or {@code null} if there
827 * is no current default network.
828 *
Paul Jensen0d719ca2015-02-13 14:18:39 -0500829 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800830 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
831 * {@hide}
832 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700833 public LinkProperties getActiveLinkProperties() {
834 try {
835 return mService.getActiveLinkProperties();
836 } catch (RemoteException e) {
837 return null;
838 }
839 }
840
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800841 /**
842 * Returns the IP information for a given network type.
843 *
844 * @param networkType the network type of interest.
845 * @return a {@link LinkProperties} object describing the IP info
846 * for the given networkType, or {@code null} if there is
847 * no current default network.
848 *
Paul Jensen0d719ca2015-02-13 14:18:39 -0500849 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800850 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
851 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -0400852 * @deprecated This method does not support multiple connected networks
853 * of the same type. Use {@link #getAllNetworks},
854 * {@link #getNetworkInfo(android.net.Network)}, and
855 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800856 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700857 public LinkProperties getLinkProperties(int networkType) {
858 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -0700859 return mService.getLinkPropertiesForType(networkType);
860 } catch (RemoteException e) {
861 return null;
862 }
863 }
864
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700865 /**
866 * Get the {@link LinkProperties} for the given {@link Network}. This
867 * will return {@code null} if the network is unknown.
868 *
869 * @param network The {@link Network} object identifying the network in question.
870 * @return The {@link LinkProperties} for the network, or {@code null}.
871 **/
Robert Greenwalt9258c642014-03-26 16:47:06 -0700872 public LinkProperties getLinkProperties(Network network) {
873 try {
874 return mService.getLinkProperties(network);
875 } catch (RemoteException e) {
876 return null;
877 }
878 }
879
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700880 /**
881 * Get the {@link NetworkCapabilities} for the given {@link Network}. This
882 * will return {@code null} if the network is unknown.
883 *
884 * @param network The {@link Network} object identifying the network in question.
885 * @return The {@link NetworkCapabilities} for the network, or {@code null}.
886 */
Robert Greenwalt9258c642014-03-26 16:47:06 -0700887 public NetworkCapabilities getNetworkCapabilities(Network network) {
888 try {
889 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700890 } catch (RemoteException e) {
891 return null;
892 }
893 }
894
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800895 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 * Tells the underlying networking system that the caller wants to
897 * begin using the named feature. The interpretation of {@code feature}
898 * is completely up to each networking implementation.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700899 * <p>This method requires the caller to hold the permission
900 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800901 * @param networkType specifies which network the request pertains to
902 * @param feature the name of the feature to be used
903 * @return an integer value representing the outcome of the request.
904 * The interpretation of this value is specific to each networking
905 * implementation+feature combination, except that the value {@code -1}
906 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700907 *
908 * @deprecated Deprecated in favor of the cleaner {@link #requestNetwork} api.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800909 */
910 public int startUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700911 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
912 if (netCap == null) {
913 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
914 feature);
915 return PhoneConstants.APN_REQUEST_FAILED;
916 }
917
918 NetworkRequest request = null;
919 synchronized (sLegacyRequests) {
920 LegacyRequest l = sLegacyRequests.get(netCap);
921 if (l != null) {
922 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
923 renewRequestLocked(l);
924 if (l.currentNetwork != null) {
925 return PhoneConstants.APN_ALREADY_ACTIVE;
926 } else {
927 return PhoneConstants.APN_REQUEST_STARTED;
928 }
929 }
930
931 request = requestNetworkForFeatureLocked(netCap);
932 }
933 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -0700934 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -0700935 return PhoneConstants.APN_REQUEST_STARTED;
936 } else {
937 Log.d(TAG, " request Failed");
938 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800939 }
940 }
941
942 /**
943 * Tells the underlying networking system that the caller is finished
944 * using the named feature. The interpretation of {@code feature}
945 * is completely up to each networking implementation.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700946 * <p>This method requires the caller to hold the permission
947 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 * @param networkType specifies which network the request pertains to
949 * @param feature the name of the feature that is no longer needed
950 * @return an integer value representing the outcome of the request.
951 * The interpretation of this value is specific to each networking
952 * implementation+feature combination, except that the value {@code -1}
953 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700954 *
955 * @deprecated Deprecated in favor of the cleaner {@link #requestNetwork} api.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800956 */
957 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700958 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
959 if (netCap == null) {
960 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
961 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800962 return -1;
963 }
Robert Greenwalt562cc542014-05-15 18:07:26 -0700964
Paul Jensen9ffb53c2014-12-17 10:39:34 -0500965 if (removeRequestForFeature(netCap)) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700966 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt562cc542014-05-15 18:07:26 -0700967 }
968 return 1;
969 }
970
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900971 /**
972 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability from the given
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900973 * NetworkCapabilities object if all the capabilities it provides are
974 * typically provided by restricted networks.
975 *
976 * TODO: consider:
977 * - Moving to NetworkCapabilities
978 * - Renaming it to guessRestrictedCapability and make it set the
979 * restricted capability bit in addition to clearing it.
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900980 * @hide
981 */
982 public static void maybeMarkCapabilitiesRestricted(NetworkCapabilities nc) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700983 for (int capability : nc.getCapabilities()) {
984 switch (capability) {
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900985 case NetworkCapabilities.NET_CAPABILITY_CBS:
986 case NetworkCapabilities.NET_CAPABILITY_DUN:
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900987 case NetworkCapabilities.NET_CAPABILITY_EIMS:
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900988 case NetworkCapabilities.NET_CAPABILITY_FOTA:
989 case NetworkCapabilities.NET_CAPABILITY_IA:
990 case NetworkCapabilities.NET_CAPABILITY_IMS:
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900991 case NetworkCapabilities.NET_CAPABILITY_RCS:
992 case NetworkCapabilities.NET_CAPABILITY_XCAP:
Robert Greenwalt9ac3dbe2014-06-05 16:39:28 -0700993 case NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED: //there by default
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900994 continue;
995 default:
996 // At least one capability usually provided by unrestricted
997 // networks. Conclude that this network is unrestricted.
998 return;
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900999 }
1000 }
Lorenzo Colittiea1984f2014-06-04 19:59:21 +09001001 // All the capabilities are typically provided by restricted networks.
1002 // Conclude that this network is restricted.
Robert Greenwalt7569f182014-06-08 16:42:59 -07001003 nc.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
Lorenzo Colitti4077acb2014-06-04 12:20:06 +09001004 }
1005
Robert Greenwalt562cc542014-05-15 18:07:26 -07001006 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1007 if (networkType == TYPE_MOBILE) {
1008 int cap = -1;
1009 if ("enableMMS".equals(feature)) {
1010 cap = NetworkCapabilities.NET_CAPABILITY_MMS;
1011 } else if ("enableSUPL".equals(feature)) {
1012 cap = NetworkCapabilities.NET_CAPABILITY_SUPL;
1013 } else if ("enableDUN".equals(feature) || "enableDUNAlways".equals(feature)) {
1014 cap = NetworkCapabilities.NET_CAPABILITY_DUN;
1015 } else if ("enableHIPRI".equals(feature)) {
1016 cap = NetworkCapabilities.NET_CAPABILITY_INTERNET;
1017 } else if ("enableFOTA".equals(feature)) {
1018 cap = NetworkCapabilities.NET_CAPABILITY_FOTA;
1019 } else if ("enableIMS".equals(feature)) {
1020 cap = NetworkCapabilities.NET_CAPABILITY_IMS;
1021 } else if ("enableCBS".equals(feature)) {
1022 cap = NetworkCapabilities.NET_CAPABILITY_CBS;
1023 } else {
1024 return null;
1025 }
1026 NetworkCapabilities netCap = new NetworkCapabilities();
Robert Greenwalt7569f182014-06-08 16:42:59 -07001027 netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(cap);
Lorenzo Colitti4077acb2014-06-04 12:20:06 +09001028 maybeMarkCapabilitiesRestricted(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001029 return netCap;
1030 } else if (networkType == TYPE_WIFI) {
1031 if ("p2p".equals(feature)) {
1032 NetworkCapabilities netCap = new NetworkCapabilities();
1033 netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
Robert Greenwalt7569f182014-06-08 16:42:59 -07001034 netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
Lorenzo Colitti4077acb2014-06-04 12:20:06 +09001035 maybeMarkCapabilitiesRestricted(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001036 return netCap;
1037 }
1038 }
1039 return null;
1040 }
1041
Robert Greenwalt06314e42014-10-29 14:04:06 -07001042 /**
1043 * Guess what the network request was trying to say so that the resulting
1044 * network is accessible via the legacy (deprecated) API such as
1045 * requestRouteToHost.
1046 * This means we should try to be fairly preceise about transport and
1047 * capability but ignore things such as networkSpecifier.
1048 * If the request has more than one transport or capability it doesn't
1049 * match the old legacy requests (they selected only single transport/capability)
1050 * so this function cannot map the request to a single legacy type and
1051 * the resulting network will not be available to the legacy APIs.
1052 *
1053 * TODO - This should be removed when the legacy APIs are removed.
1054 */
Ye Wenb87875e2014-07-21 14:19:01 -07001055 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1056 if (netCap == null) {
1057 return TYPE_NONE;
1058 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001059
Ye Wenb87875e2014-07-21 14:19:01 -07001060 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1061 return TYPE_NONE;
1062 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001063
1064 String type = null;
1065 int result = TYPE_NONE;
1066
Ye Wenb87875e2014-07-21 14:19:01 -07001067 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
Robert Greenwalt06314e42014-10-29 14:04:06 -07001068 type = "enableCBS";
1069 result = TYPE_MOBILE_CBS;
1070 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1071 type = "enableIMS";
1072 result = TYPE_MOBILE_IMS;
1073 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1074 type = "enableFOTA";
1075 result = TYPE_MOBILE_FOTA;
1076 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1077 type = "enableDUN";
1078 result = TYPE_MOBILE_DUN;
1079 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1080 type = "enableSUPL";
1081 result = TYPE_MOBILE_SUPL;
1082 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1083 type = "enableMMS";
1084 result = TYPE_MOBILE_MMS;
1085 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1086 type = "enableHIPRI";
1087 result = TYPE_MOBILE_HIPRI;
Ye Wenb87875e2014-07-21 14:19:01 -07001088 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001089 if (type != null) {
1090 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1091 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1092 return result;
Ye Wenb87875e2014-07-21 14:19:01 -07001093 }
1094 }
1095 return TYPE_NONE;
1096 }
1097
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001098 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001099 if (netCap == null) return TYPE_NONE;
1100 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1101 return TYPE_MOBILE_CBS;
1102 }
1103 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1104 return TYPE_MOBILE_IMS;
1105 }
1106 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1107 return TYPE_MOBILE_FOTA;
1108 }
1109 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1110 return TYPE_MOBILE_DUN;
1111 }
1112 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1113 return TYPE_MOBILE_SUPL;
1114 }
1115 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1116 return TYPE_MOBILE_MMS;
1117 }
1118 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1119 return TYPE_MOBILE_HIPRI;
1120 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001121 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1122 return TYPE_WIFI_P2P;
1123 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001124 return TYPE_NONE;
1125 }
1126
1127 private static class LegacyRequest {
1128 NetworkCapabilities networkCapabilities;
1129 NetworkRequest networkRequest;
1130 int expireSequenceNumber;
1131 Network currentNetwork;
1132 int delay = -1;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001133
1134 private void clearDnsBinding() {
1135 if (currentNetwork != null) {
1136 currentNetwork = null;
1137 setProcessDefaultNetworkForHostResolution(null);
1138 }
1139 }
1140
Robert Greenwalt6078b502014-06-11 16:05:07 -07001141 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001142 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001143 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001144 currentNetwork = network;
1145 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001146 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001147 }
1148 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001149 public void onLost(Network network) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001150 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001151 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1152 }
1153 };
1154 }
1155
Robert Greenwaltfab501672014-07-23 11:44:01 -07001156 private static HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
Robert Greenwalt562cc542014-05-15 18:07:26 -07001157 new HashMap<NetworkCapabilities, LegacyRequest>();
1158
1159 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1160 synchronized (sLegacyRequests) {
1161 LegacyRequest l = sLegacyRequests.get(netCap);
1162 if (l != null) return l.networkRequest;
1163 }
1164 return null;
1165 }
1166
1167 private void renewRequestLocked(LegacyRequest l) {
1168 l.expireSequenceNumber++;
1169 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1170 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1171 }
1172
1173 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1174 int ourSeqNum = -1;
1175 synchronized (sLegacyRequests) {
1176 LegacyRequest l = sLegacyRequests.get(netCap);
1177 if (l == null) return;
1178 ourSeqNum = l.expireSequenceNumber;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001179 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001180 }
1181 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1182 }
1183
1184 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1185 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001186 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001187 try {
1188 delay = mService.getRestoreDefaultNetworkDelay(type);
1189 } catch (RemoteException e) {}
1190 LegacyRequest l = new LegacyRequest();
1191 l.networkCapabilities = netCap;
1192 l.delay = delay;
1193 l.expireSequenceNumber = 0;
Robert Greenwalt6078b502014-06-11 16:05:07 -07001194 l.networkRequest = sendRequestForNetwork(netCap, l.networkCallback, 0,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001195 REQUEST, type);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001196 if (l.networkRequest == null) return null;
1197 sLegacyRequests.put(netCap, l);
1198 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1199 return l.networkRequest;
1200 }
1201
1202 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1203 if (delay >= 0) {
1204 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
1205 Message msg = sCallbackHandler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1206 sCallbackHandler.sendMessageDelayed(msg, delay);
1207 }
1208 }
1209
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001210 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1211 final LegacyRequest l;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001212 synchronized (sLegacyRequests) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001213 l = sLegacyRequests.remove(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001214 }
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001215 if (l == null) return false;
1216 unregisterNetworkCallback(l.networkCallback);
1217 l.clearDnsBinding();
1218 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001219 }
1220
1221 /**
1222 * Ensure that a network route exists to deliver traffic to the specified
1223 * host via the specified network interface. An attempt to add a route that
1224 * already exists is ignored, but treated as successful.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001225 * <p>This method requires the caller to hold the permission
1226 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 * @param networkType the type of the network over which traffic to the specified
1228 * host is to be routed
1229 * @param hostAddress the IP address of the host to which the route is desired
1230 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001231 *
1232 * @deprecated Deprecated in favor of the {@link #requestNetwork},
Paul Jensen72db88e2015-03-10 10:54:12 -04001233 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} api.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001234 */
1235 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001236 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001237 }
1238
1239 /**
1240 * Ensure that a network route exists to deliver traffic to the specified
1241 * host via the specified network interface. An attempt to add a route that
1242 * already exists is ignored, but treated as successful.
Jake Hamby8f9b33e2014-01-15 13:08:03 -08001243 * <p>This method requires the caller to hold the permission
1244 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001245 * @param networkType the type of the network over which traffic to the specified
1246 * host is to be routed
1247 * @param hostAddress the IP address of the host to which the route is desired
1248 * @return {@code true} on success, {@code false} on failure
1249 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001250 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Paul Jensen72db88e2015-03-10 10:54:12 -04001251 * {@link #bindProcessToNetwork} api.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001252 */
1253 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001254 try {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001255 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001256 } catch (RemoteException e) {
1257 return false;
1258 }
1259 }
1260
1261 /**
1262 * Returns the value of the setting for background data usage. If false,
1263 * applications should not use the network if the application is not in the
1264 * foreground. Developers should respect this setting, and check the value
1265 * of this before performing any background data operations.
1266 * <p>
1267 * All applications that have background services that use the network
1268 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001269 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001270 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001271 * background data depends on several combined factors, and this method will
1272 * always return {@code true}. Instead, when background data is unavailable,
1273 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001274 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001275 * @return Whether background data usage is allowed.
1276 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001277 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001278 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001279 // assume that background data is allowed; final authority is
1280 // NetworkInfo which may be blocked.
1281 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 }
1283
1284 /**
1285 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001286 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 * @param allowBackgroundData Whether an application should use data while
1288 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001289 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001290 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1291 * @see #getBackgroundDataSetting()
1292 * @hide
1293 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001294 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001295 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001296 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001298
1299 /**
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001300 * Return quota status for the current active network, or {@code null} if no
1301 * network is active. Quota status can change rapidly, so these values
1302 * shouldn't be cached.
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001303 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001304 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001305 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1306 *
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001307 * @hide
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001308 */
1309 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1310 try {
1311 return mService.getActiveNetworkQuotaInfo();
1312 } catch (RemoteException e) {
1313 return null;
1314 }
1315 }
1316
1317 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001318 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001319 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001320 */
1321 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001322 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1323 if (b != null) {
1324 try {
1325 ITelephony it = ITelephony.Stub.asInterface(b);
Wink Saville36ffb042014-12-05 11:10:30 -08001326 int subId = SubscriptionManager.getDefaultDataSubId();
1327 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
1328 boolean retVal = it.getDataEnabled(subId);
1329 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1330 + " retVal=" + retVal);
1331 return retVal;
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001332 } catch (RemoteException e) { }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001333 }
Wink Saville36ffb042014-12-05 11:10:30 -08001334 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001335 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001336 }
1337
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001338 /**
Robert Greenwaltb2489872014-09-04 16:44:35 -07001339 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt6078b502014-06-11 16:05:07 -07001340 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001341 */
1342 public interface OnNetworkActiveListener {
1343 /**
1344 * Called on the main thread of the process to report that the current data network
1345 * has become active, and it is now a good time to perform any pending network
1346 * operations. Note that this listener only tells you when the network becomes
1347 * active; if at any other time you want to know whether it is active (and thus okay
1348 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001349 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001350 */
1351 public void onNetworkActive();
1352 }
1353
1354 private INetworkManagementService getNetworkManagementService() {
1355 synchronized (this) {
1356 if (mNMService != null) {
1357 return mNMService;
1358 }
1359 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1360 mNMService = INetworkManagementService.Stub.asInterface(b);
1361 return mNMService;
1362 }
1363 }
1364
1365 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
1366 mNetworkActivityListeners
1367 = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>();
1368
1369 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001370 * Start listening to reports when the system's default data network is active, meaning it is
1371 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1372 * to determine the current state of the system's default network after registering the
1373 * listener.
1374 * <p>
1375 * If the process default network has been set with
Paul Jensen72db88e2015-03-10 10:54:12 -04001376 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt6078b502014-06-11 16:05:07 -07001377 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001378 *
1379 * @param l The listener to be told when the network is active.
1380 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001381 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001382 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1383 @Override
1384 public void onNetworkActive() throws RemoteException {
1385 l.onNetworkActive();
1386 }
1387 };
1388
1389 try {
1390 getNetworkManagementService().registerNetworkActivityListener(rl);
1391 mNetworkActivityListeners.put(l, rl);
1392 } catch (RemoteException e) {
1393 }
1394 }
1395
1396 /**
1397 * Remove network active listener previously registered with
Robert Greenwaltb2489872014-09-04 16:44:35 -07001398 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001399 *
1400 * @param l Previously registered listener.
1401 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001402 public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001403 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
1404 if (rl == null) {
1405 throw new IllegalArgumentException("Listener not registered: " + l);
1406 }
1407 try {
1408 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1409 } catch (RemoteException e) {
1410 }
1411 }
1412
1413 /**
1414 * Return whether the data network is currently active. An active network means that
1415 * it is currently in a high power state for performing data transmission. On some
1416 * types of networks, it may be expensive to move and stay in such a state, so it is
1417 * more power efficient to batch network traffic together when the radio is already in
1418 * this state. This method tells you whether right now is currently a good time to
1419 * initiate network traffic, as the network is already active.
1420 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001421 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001422 try {
1423 return getNetworkManagementService().isNetworkActive();
1424 } catch (RemoteException e) {
1425 }
1426 return false;
1427 }
1428
1429 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001430 * {@hide}
1431 */
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001432 public ConnectivityManager(IConnectivityManager service) {
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001433 mService = checkNotNull(service, "missing IConnectivityManager");
Paul Jensene0bef712014-12-10 15:12:18 -05001434 sInstance = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001435 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001436
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07001437 /** {@hide} */
1438 public static ConnectivityManager from(Context context) {
1439 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1440 }
1441
Robert Greenwaltedb47662014-09-16 17:54:19 -07001442 /** {@hide */
1443 public static final void enforceTetherChangePermission(Context context) {
1444 if (context.getResources().getStringArray(
1445 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
1446 // Have a provisioning app - must only let system apps (which check this app)
1447 // turn on tethering
1448 context.enforceCallingOrSelfPermission(
1449 android.Manifest.permission.CONNECTIVITY_INTERNAL, "ConnectivityService");
1450 } else {
1451 context.enforceCallingOrSelfPermission(
1452 android.Manifest.permission.CHANGE_NETWORK_STATE, "ConnectivityService");
1453 }
1454 }
1455
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001456 /**
Paul Jensene0bef712014-12-10 15:12:18 -05001457 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1458 * situations where a Context pointer is unavailable.
1459 * @hide
1460 */
Paul Jensen72db88e2015-03-10 10:54:12 -04001461 static ConnectivityManager getInstanceOrNull() {
1462 return sInstance;
1463 }
1464
1465 /**
1466 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1467 * situations where a Context pointer is unavailable.
1468 * @hide
1469 */
1470 private static ConnectivityManager getInstance() {
1471 if (getInstanceOrNull() == null) {
Paul Jensene0bef712014-12-10 15:12:18 -05001472 throw new IllegalStateException("No ConnectivityManager yet constructed");
1473 }
Paul Jensen72db88e2015-03-10 10:54:12 -04001474 return getInstanceOrNull();
Paul Jensene0bef712014-12-10 15:12:18 -05001475 }
1476
1477 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001478 * Get the set of tetherable, available interfaces. This list is limited by
1479 * device configuration and current interface existence.
1480 *
1481 * @return an array of 0 or more Strings of tetherable interface names.
1482 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001483 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001484 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001485 * {@hide}
1486 */
1487 public String[] getTetherableIfaces() {
1488 try {
1489 return mService.getTetherableIfaces();
1490 } catch (RemoteException e) {
1491 return new String[0];
1492 }
1493 }
1494
1495 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001496 * Get the set of tethered interfaces.
1497 *
1498 * @return an array of 0 or more String of currently tethered interface names.
1499 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001500 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001501 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001502 * {@hide}
1503 */
1504 public String[] getTetheredIfaces() {
1505 try {
1506 return mService.getTetheredIfaces();
1507 } catch (RemoteException e) {
1508 return new String[0];
1509 }
1510 }
1511
1512 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001513 * Get the set of interface names which attempted to tether but
1514 * failed. Re-attempting to tether may cause them to reset to the Tethered
1515 * state. Alternatively, causing the interface to be destroyed and recreated
1516 * may cause them to reset to the available state.
1517 * {@link ConnectivityManager#getLastTetherError} can be used to get more
1518 * information on the cause of the errors.
1519 *
1520 * @return an array of 0 or more String indicating the interface names
1521 * which failed to tether.
1522 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001523 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001524 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001525 * {@hide}
1526 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001527 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001528 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001529 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001530 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001531 return new String[0];
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001532 }
1533 }
1534
1535 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001536 * Get the set of tethered dhcp ranges.
1537 *
1538 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
1539 * {@hide}
1540 */
1541 public String[] getTetheredDhcpRanges() {
1542 try {
1543 return mService.getTetheredDhcpRanges();
1544 } catch (RemoteException e) {
1545 return new String[0];
1546 }
1547 }
1548
1549 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001550 * Attempt to tether the named interface. This will setup a dhcp server
1551 * on the interface, forward and NAT IP packets and forward DNS requests
1552 * to the best active upstream network interface. Note that if no upstream
1553 * IP network interface is available, dhcp will still run and traffic will be
1554 * allowed between the tethered devices and this device, though upstream net
1555 * access will of course fail until an upstream network interface becomes
1556 * active.
1557 *
1558 * @param iface the interface name to tether.
1559 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1560 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001561 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001562 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001563 * {@hide}
1564 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001565 public int tether(String iface) {
1566 try {
1567 return mService.tether(iface);
1568 } catch (RemoteException e) {
1569 return TETHER_ERROR_SERVICE_UNAVAIL;
1570 }
1571 }
1572
1573 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001574 * Stop tethering the named interface.
1575 *
1576 * @param iface the interface name to untether.
1577 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1578 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001579 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001580 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt5a735062010-03-02 17:25:02 -08001581 * {@hide}
1582 */
1583 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001584 try {
1585 return mService.untether(iface);
1586 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001587 return TETHER_ERROR_SERVICE_UNAVAIL;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001588 }
1589 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001590
1591 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001592 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001593 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001594 * due to device configuration.
1595 *
1596 * @return a boolean - {@code true} indicating Tethering is supported.
1597 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001598 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001599 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001600 * {@hide}
1601 */
1602 public boolean isTetheringSupported() {
1603 try {
1604 return mService.isTetheringSupported();
1605 } catch (RemoteException e) {
1606 return false;
1607 }
1608 }
1609
1610 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001611 * Get the list of regular expressions that define any tetherable
1612 * USB network interfaces. If USB tethering is not supported by the
1613 * device, this list should be empty.
1614 *
1615 * @return an array of 0 or more regular expression Strings defining
1616 * what interfaces are considered tetherable usb interfaces.
1617 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001618 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001619 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001620 * {@hide}
1621 */
1622 public String[] getTetherableUsbRegexs() {
1623 try {
1624 return mService.getTetherableUsbRegexs();
1625 } catch (RemoteException e) {
1626 return new String[0];
1627 }
1628 }
1629
1630 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001631 * Get the list of regular expressions that define any tetherable
1632 * Wifi network interfaces. If Wifi tethering is not supported by the
1633 * device, this list should be empty.
1634 *
1635 * @return an array of 0 or more regular expression Strings defining
1636 * what interfaces are considered tetherable wifi interfaces.
1637 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001638 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001639 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001640 * {@hide}
1641 */
1642 public String[] getTetherableWifiRegexs() {
1643 try {
1644 return mService.getTetherableWifiRegexs();
1645 } catch (RemoteException e) {
1646 return new String[0];
1647 }
1648 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08001649
Danica Chang6fdd0c62010-08-11 14:54:43 -07001650 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001651 * Get the list of regular expressions that define any tetherable
1652 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
1653 * device, this list should be empty.
1654 *
1655 * @return an array of 0 or more regular expression Strings defining
1656 * what interfaces are considered tetherable bluetooth interfaces.
1657 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001658 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001659 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001660 * {@hide}
1661 */
1662 public String[] getTetherableBluetoothRegexs() {
1663 try {
1664 return mService.getTetherableBluetoothRegexs();
1665 } catch (RemoteException e) {
1666 return new String[0];
1667 }
1668 }
1669
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001670 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001671 * Attempt to both alter the mode of USB and Tethering of USB. A
1672 * utility method to deal with some of the complexity of USB - will
1673 * attempt to switch to Rndis and subsequently tether the resulting
1674 * interface on {@code true} or turn off tethering and switch off
1675 * Rndis on {@code false}.
1676 *
1677 * @param enable a boolean - {@code true} to enable tethering
1678 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1679 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001680 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001681 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001682 * {@hide}
1683 */
1684 public int setUsbTethering(boolean enable) {
1685 try {
1686 return mService.setUsbTethering(enable);
1687 } catch (RemoteException e) {
1688 return TETHER_ERROR_SERVICE_UNAVAIL;
1689 }
1690 }
1691
Robert Greenwalt5a735062010-03-02 17:25:02 -08001692 /** {@hide} */
1693 public static final int TETHER_ERROR_NO_ERROR = 0;
1694 /** {@hide} */
1695 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
1696 /** {@hide} */
1697 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
1698 /** {@hide} */
1699 public static final int TETHER_ERROR_UNSUPPORTED = 3;
1700 /** {@hide} */
1701 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
1702 /** {@hide} */
1703 public static final int TETHER_ERROR_MASTER_ERROR = 5;
1704 /** {@hide} */
1705 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
1706 /** {@hide} */
1707 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
1708 /** {@hide} */
1709 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
1710 /** {@hide} */
1711 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
1712 /** {@hide} */
1713 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
1714
1715 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001716 * Get a more detailed error code after a Tethering or Untethering
1717 * request asynchronously failed.
1718 *
1719 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08001720 * @return error The error code of the last error tethering or untethering the named
1721 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001722 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001723 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001724 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a735062010-03-02 17:25:02 -08001725 * {@hide}
1726 */
1727 public int getLastTetherError(String iface) {
1728 try {
1729 return mService.getLastTetherError(iface);
1730 } catch (RemoteException e) {
1731 return TETHER_ERROR_SERVICE_UNAVAIL;
1732 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001733 }
1734
1735 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001736 * Report network connectivity status. This is currently used only
1737 * to alter status bar UI.
1738 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001739 * @param networkType The type of network you want to report on
1740 * @param percentage The quality of the connection 0 is bad, 100 is good
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001741 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001742 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001743 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001744 * {@hide}
1745 */
1746 public void reportInetCondition(int networkType, int percentage) {
1747 try {
1748 mService.reportInetCondition(networkType, percentage);
1749 } catch (RemoteException e) {
1750 }
1751 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001752
1753 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001754 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07001755 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001756 * the framework to re-evaluate network connectivity and/or switch to another
1757 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001758 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001759 * @param network The {@link Network} the application was attempting to use
1760 * or {@code null} to indicate the current default network.
Paul Jensenbfd17b72015-04-07 12:43:13 -04001761 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
1762 * working and non-working connectivity.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001763 */
1764 public void reportBadNetwork(Network network) {
1765 try {
Paul Jensenbfd17b72015-04-07 12:43:13 -04001766 // One of these will be ignored because it matches system's current state.
1767 // The other will trigger the necessary reevaluation.
1768 mService.reportNetworkConnectivity(network, true);
1769 mService.reportNetworkConnectivity(network, false);
1770 } catch (RemoteException e) {
1771 }
1772 }
1773
1774 /**
1775 * Report to the framework whether a network has working connectivity.
1776 * This provides a hint to the system that a particular network is providing
1777 * working connectivity or not. In response the framework may re-evaluate
1778 * the network's connectivity and might take further action thereafter.
1779 *
1780 * @param network The {@link Network} the application was attempting to use
1781 * or {@code null} to indicate the current default network.
1782 * @param hasConnectivity {@code true} if the application was able to successfully access the
1783 * Internet using {@code network} or {@code false} if not.
1784 */
1785 public void reportNetworkConnectivity(Network network, boolean hasConnectivity) {
1786 try {
1787 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt9258c642014-03-26 16:47:06 -07001788 } catch (RemoteException e) {
1789 }
1790 }
1791
Paul Jensen25a217c2015-02-27 22:55:47 -05001792 /** {@hide} */
1793 public static final int CAPTIVE_PORTAL_APP_RETURN_DISMISSED = 0;
1794 /** {@hide} */
1795 public static final int CAPTIVE_PORTAL_APP_RETURN_UNWANTED = 1;
1796 /** {@hide} */
1797 public static final int CAPTIVE_PORTAL_APP_RETURN_WANTED_AS_IS = 2;
1798
1799 /**
1800 * Called by an app handling the {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN}
1801 * action to indicate to the system that the captive portal has been
1802 * dismissed. In response the framework will re-evaluate the network's
1803 * connectivity and might take further action thereafter.
1804 *
1805 * @param network The {@link Network} object passed via
1806 * {@link #EXTRA_NETWORK} with the
1807 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} action.
1808 * @param actionToken The {@code String} passed via
1809 * {@link #EXTRA_CAPTIVE_PORTAL_TOKEN} with the
1810 * {@code ACTION_CAPTIVE_PORTAL_SIGN_IN} action.
1811 */
1812 public void reportCaptivePortalDismissed(Network network, String actionToken) {
1813 try {
1814 mService.captivePortalAppResponse(network, CAPTIVE_PORTAL_APP_RETURN_DISMISSED,
1815 actionToken);
1816 } catch (RemoteException e) {
1817 }
1818 }
1819
1820 /**
1821 * Called by an app handling the {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN}
1822 * action to indicate that the user does not want to pursue signing in to
1823 * captive portal and the system should continue to prefer other networks
1824 * without captive portals for use as the default active data network. The
1825 * system will not retest the network for a captive portal so as to avoid
1826 * disturbing the user with further sign in to network notifications.
1827 *
1828 * @param network The {@link Network} object passed via
1829 * {@link #EXTRA_NETWORK} with the
1830 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} action.
1831 * @param actionToken The {@code String} passed via
1832 * {@link #EXTRA_CAPTIVE_PORTAL_TOKEN} with the
1833 * {@code ACTION_CAPTIVE_PORTAL_SIGN_IN} action.
1834 */
1835 public void ignoreNetworkWithCaptivePortal(Network network, String actionToken) {
1836 try {
1837 mService.captivePortalAppResponse(network, CAPTIVE_PORTAL_APP_RETURN_UNWANTED,
1838 actionToken);
1839 } catch (RemoteException e) {
1840 }
1841 }
1842
1843 /**
1844 * Called by an app handling the {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN}
1845 * action to indicate the user wants to use this network as is, even though
1846 * the captive portal is still in place. The system will treat the network
1847 * as if it did not have a captive portal when selecting the network to use
1848 * as the default active data network. This may result in this network
1849 * becoming the default active data network, which could disrupt network
1850 * connectivity for apps because the captive portal is still in place.
1851 *
1852 * @param network The {@link Network} object passed via
1853 * {@link #EXTRA_NETWORK} with the
1854 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} action.
1855 * @param actionToken The {@code String} passed via
1856 * {@link #EXTRA_CAPTIVE_PORTAL_TOKEN} with the
1857 * {@code ACTION_CAPTIVE_PORTAL_SIGN_IN} action.
1858 * @hide
1859 */
1860 public void useNetworkWithCaptivePortal(Network network, String actionToken) {
1861 try {
1862 mService.captivePortalAppResponse(network, CAPTIVE_PORTAL_APP_RETURN_WANTED_AS_IS,
1863 actionToken);
1864 } catch (RemoteException e) {
1865 }
1866 }
1867
Robert Greenwalt9258c642014-03-26 16:47:06 -07001868 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001869 * Set a network-independent global http proxy. This is not normally what you want
1870 * for typical HTTP proxies - they are general network dependent. However if you're
1871 * doing something unusual like general internal filtering this may be useful. On
1872 * a private network where the proxy is not accessible, you may break HTTP using this.
1873 *
Jason Monk207900c2014-04-25 15:00:09 -04001874 * @param p The a {@link ProxyInfo} object defining the new global
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001875 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
1876 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001877 * <p>This method requires the caller to hold the permission
Jason Monkfaf3fd52014-05-07 18:41:13 -04001878 * android.Manifest.permission#CONNECTIVITY_INTERNAL.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001879 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07001880 */
Jason Monk207900c2014-04-25 15:00:09 -04001881 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001882 try {
1883 mService.setGlobalProxy(p);
1884 } catch (RemoteException e) {
1885 }
1886 }
1887
1888 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001889 * Retrieve any network-independent global HTTP proxy.
1890 *
Jason Monk207900c2014-04-25 15:00:09 -04001891 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001892 * if no global HTTP proxy is set.
1893 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001894 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001895 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001896 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07001897 */
Jason Monk207900c2014-04-25 15:00:09 -04001898 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001899 try {
1900 return mService.getGlobalProxy();
1901 } catch (RemoteException e) {
1902 return null;
1903 }
1904 }
1905
1906 /**
Paul Jensene0bef712014-12-10 15:12:18 -05001907 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
1908 * otherwise if this process is bound to a {@link Network} using
Paul Jensen72db88e2015-03-10 10:54:12 -04001909 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensene0bef712014-12-10 15:12:18 -05001910 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001911 *
Jason Monk207900c2014-04-25 15:00:09 -04001912 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001913 * HTTP proxy is active.
Robert Greenwalt434203a2010-10-11 16:00:27 -07001914 */
Paul Jensene0bef712014-12-10 15:12:18 -05001915 public ProxyInfo getDefaultProxy() {
Paul Jensen72db88e2015-03-10 10:54:12 -04001916 final Network network = getBoundNetworkForProcess();
Paul Jensene0bef712014-12-10 15:12:18 -05001917 if (network != null) {
1918 final ProxyInfo globalProxy = getGlobalProxy();
1919 if (globalProxy != null) return globalProxy;
1920 final LinkProperties lp = getLinkProperties(network);
1921 if (lp != null) return lp.getHttpProxy();
1922 return null;
1923 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001924 try {
Paul Jensene0bef712014-12-10 15:12:18 -05001925 return mService.getDefaultProxy();
Robert Greenwalt434203a2010-10-11 16:00:27 -07001926 } catch (RemoteException e) {
1927 return null;
1928 }
1929 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001930
1931 /**
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07001932 * Returns true if the hardware supports the given network type
1933 * else it returns false. This doesn't indicate we have coverage
1934 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001935 * hardware supports it. For example a GSM phone without a SIM
1936 * should still return {@code true} for mobile data, but a wifi only
1937 * tablet would return {@code false}.
1938 *
1939 * @param networkType The network type we'd like to check
1940 * @return {@code true} if supported, else {@code false}
1941 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001942 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001943 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07001944 * @hide
1945 */
1946 public boolean isNetworkSupported(int networkType) {
1947 try {
1948 return mService.isNetworkSupported(networkType);
1949 } catch (RemoteException e) {}
1950 return false;
1951 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07001952
1953 /**
1954 * Returns if the currently active data network is metered. A network is
1955 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001956 * that connection due to monetary costs, data limitations or
1957 * battery/performance issues. You should check this before doing large
1958 * data transfers, and warn the user or delay the operation until another
1959 * network is available.
1960 *
1961 * @return {@code true} if large transfers should be avoided, otherwise
1962 * {@code false}.
1963 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001964 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001965 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07001966 */
1967 public boolean isActiveNetworkMetered() {
1968 try {
1969 return mService.isActiveNetworkMetered();
1970 } catch (RemoteException e) {
1971 return false;
1972 }
1973 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07001974
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001975 /**
1976 * If the LockdownVpn mechanism is enabled, updates the vpn
1977 * with a reload of its profile.
1978 *
1979 * @return a boolean with {@code} indicating success
1980 *
1981 * <p>This method can only be called by the system UID
1982 * {@hide}
1983 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07001984 public boolean updateLockdownVpn() {
1985 try {
1986 return mService.updateLockdownVpn();
1987 } catch (RemoteException e) {
1988 return false;
1989 }
1990 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07001991
1992 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001993 * Signal that the captive portal check on the indicated network
Wink Savilled747cbc2013-08-07 16:22:47 -07001994 * is complete and whether its a captive portal or not.
1995 *
1996 * @param info the {@link NetworkInfo} object for the networkType
1997 * in question.
1998 * @param isCaptivePortal true/false.
1999 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05002000 * <p>This method requires the caller to hold the permission
Wink Savilled747cbc2013-08-07 16:22:47 -07002001 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
2002 * {@hide}
2003 */
2004 public void captivePortalCheckCompleted(NetworkInfo info, boolean isCaptivePortal) {
2005 try {
2006 mService.captivePortalCheckCompleted(info, isCaptivePortal);
2007 } catch (RemoteException e) {
2008 }
2009 }
2010
2011 /**
Wink Saville948282b2013-08-29 08:55:16 -07002012 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07002013 *
Wink Savilleab9321d2013-06-29 21:10:57 -07002014 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07002015 *
2016 * @return time out that will be used, maybe less that suggestedTimeOutMs
2017 * -1 if an error.
2018 *
2019 * {@hide}
2020 */
Wink Saville948282b2013-08-29 08:55:16 -07002021 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07002022 int timeOutMs = -1;
2023 try {
Wink Saville948282b2013-08-29 08:55:16 -07002024 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07002025 } catch (RemoteException e) {
2026 }
2027 return timeOutMs;
2028 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002029
2030 /**
Wink Saville42d4f082013-07-20 20:31:59 -07002031 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07002032 * {@hide}
2033 */
2034 public String getMobileProvisioningUrl() {
2035 try {
2036 return mService.getMobileProvisioningUrl();
2037 } catch (RemoteException e) {
2038 }
2039 return null;
2040 }
Wink Saville42d4f082013-07-20 20:31:59 -07002041
2042 /**
2043 * Get the mobile redirected provisioning url.
2044 * {@hide}
2045 */
2046 public String getMobileRedirectedProvisioningUrl() {
2047 try {
2048 return mService.getMobileRedirectedProvisioningUrl();
2049 } catch (RemoteException e) {
2050 }
2051 return null;
2052 }
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07002053
2054 /**
Wink Saville948282b2013-08-29 08:55:16 -07002055 * Set sign in error notification to visible or in visible
2056 *
2057 * @param visible
2058 * @param networkType
2059 *
2060 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04002061 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville948282b2013-08-29 08:55:16 -07002062 */
2063 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04002064 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07002065 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04002066 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07002067 } catch (RemoteException e) {
2068 }
2069 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002070
2071 /**
2072 * Set the value for enabling/disabling airplane mode
2073 *
2074 * @param enable whether to enable airplane mode or not
2075 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05002076 * <p>This method requires the caller to hold the permission
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002077 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
2078 * @hide
2079 */
2080 public void setAirplaneMode(boolean enable) {
2081 try {
2082 mService.setAirplaneMode(enable);
2083 } catch (RemoteException e) {
2084 }
2085 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002086
2087 /** {@hide} */
Robert Greenwalta67be032014-05-16 15:49:14 -07002088 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07002089 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07002090 mService.registerNetworkFactory(messenger, name);
2091 } catch (RemoteException e) { }
2092 }
2093
2094 /** {@hide} */
2095 public void unregisterNetworkFactory(Messenger messenger) {
2096 try {
2097 mService.unregisterNetworkFactory(messenger);
Robert Greenwalte049c232014-04-11 15:53:27 -07002098 } catch (RemoteException e) { }
2099 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002100
Paul Jensen31a94f42015-02-13 14:18:39 -05002101 /**
2102 * @hide
2103 * Register a NetworkAgent with ConnectivityService.
2104 * @return NetID corresponding to NetworkAgent.
2105 */
2106 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002107 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002108 try {
Paul Jensen31a94f42015-02-13 14:18:39 -05002109 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
2110 } catch (RemoteException e) {
2111 return NETID_UNSET;
2112 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002113 }
2114
Robert Greenwalt9258c642014-03-26 16:47:06 -07002115 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002116 * Base class for NetworkRequest callbacks. Used for notifications about network
2117 * changes. Should be extended by applications wanting notifications.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002118 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002119 public static class NetworkCallback {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002120 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002121 public static final int PRECHECK = 1;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002122 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002123 public static final int AVAILABLE = 2;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002124 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002125 public static final int LOSING = 3;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002126 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002127 public static final int LOST = 4;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002128 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002129 public static final int UNAVAIL = 5;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002130 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002131 public static final int CAP_CHANGED = 6;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002132 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002133 public static final int PROP_CHANGED = 7;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002134 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002135 public static final int CANCELED = 8;
2136
2137 /**
2138 * @hide
2139 * Called whenever the framework connects to a network that it may use to
2140 * satisfy this request
2141 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002142 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002143
2144 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002145 * Called when the framework connects and has declared new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002146 * This callback may be called more than once if the {@link Network} that is
2147 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002148 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002149 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002150 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002151 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002152
2153 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002154 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002155 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002156 * for graceful handover. This may not be called if we have a hard loss
2157 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002158 * {@link NetworkCallback#onLost} call or a
2159 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002160 * on whether we lose or regain it.
2161 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002162 * @param network The {@link Network} that is about to be disconnected.
2163 * @param maxMsToLive The time in ms the framework will attempt to keep the
2164 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002165 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002166 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002167 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002168
2169 /**
2170 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002171 * graceful failure ends.
2172 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002173 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002174 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002175 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002176
2177 /**
2178 * Called if no network is found in the given timeout time. If no timeout is given,
2179 * this will not be called.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002180 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002181 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002182 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002183
2184 /**
2185 * Called when the network the framework connected to for this request
2186 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002187 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002188 * @param network The {@link Network} whose capabilities have changed.
2189 * @param networkCapabilities The new {@link NetworkCapabilities} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002190 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002191 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002192 NetworkCapabilities networkCapabilities) {}
2193
2194 /**
2195 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002196 * changes {@link LinkProperties}.
2197 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002198 * @param network The {@link Network} whose link properties have changed.
2199 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002200 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002201 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002202
Robert Greenwalt6078b502014-06-11 16:05:07 -07002203 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002204 }
2205
Robert Greenwalt9258c642014-03-26 16:47:06 -07002206 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
2207 /** @hide obj = pair(NetworkRequest, Network) */
2208 public static final int CALLBACK_PRECHECK = BASE + 1;
2209 /** @hide obj = pair(NetworkRequest, Network) */
2210 public static final int CALLBACK_AVAILABLE = BASE + 2;
2211 /** @hide obj = pair(NetworkRequest, Network), arg1 = ttl */
2212 public static final int CALLBACK_LOSING = BASE + 3;
2213 /** @hide obj = pair(NetworkRequest, Network) */
2214 public static final int CALLBACK_LOST = BASE + 4;
2215 /** @hide obj = NetworkRequest */
2216 public static final int CALLBACK_UNAVAIL = BASE + 5;
2217 /** @hide obj = pair(NetworkRequest, Network) */
2218 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2219 /** @hide obj = pair(NetworkRequest, Network) */
2220 public static final int CALLBACK_IP_CHANGED = BASE + 7;
2221 /** @hide obj = NetworkRequest */
2222 public static final int CALLBACK_RELEASED = BASE + 8;
2223 /** @hide */
2224 public static final int CALLBACK_EXIT = BASE + 9;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002225 /** @hide obj = NetworkCapabilities, arg1 = seq number */
2226 private static final int EXPIRE_LEGACY_REQUEST = BASE + 10;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002227
Robert Greenwalt562cc542014-05-15 18:07:26 -07002228 private class CallbackHandler extends Handler {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002229 private final HashMap<NetworkRequest, NetworkCallback>mCallbackMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002230 private final AtomicInteger mRefCount;
2231 private static final String TAG = "ConnectivityManager.CallbackHandler";
2232 private final ConnectivityManager mCm;
2233
Robert Greenwalt6078b502014-06-11 16:05:07 -07002234 CallbackHandler(Looper looper, HashMap<NetworkRequest, NetworkCallback>callbackMap,
Robert Greenwalt9258c642014-03-26 16:47:06 -07002235 AtomicInteger refCount, ConnectivityManager cm) {
2236 super(looper);
2237 mCallbackMap = callbackMap;
2238 mRefCount = refCount;
2239 mCm = cm;
2240 }
2241
2242 @Override
2243 public void handleMessage(Message message) {
2244 Log.d(TAG, "CM callback handler got msg " + message.what);
2245 switch (message.what) {
2246 case CALLBACK_PRECHECK: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002247 NetworkRequest request = (NetworkRequest)getObject(message,
2248 NetworkRequest.class);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002249 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002250 if (callbacks != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002251 callbacks.onPreCheck((Network)getObject(message, Network.class));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002252 } else {
2253 Log.e(TAG, "callback not found for PRECHECK message");
2254 }
2255 break;
2256 }
2257 case CALLBACK_AVAILABLE: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002258 NetworkRequest request = (NetworkRequest)getObject(message,
2259 NetworkRequest.class);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002260 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002261 if (callbacks != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002262 callbacks.onAvailable((Network)getObject(message, Network.class));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002263 } else {
2264 Log.e(TAG, "callback not found for AVAILABLE message");
2265 }
2266 break;
2267 }
2268 case CALLBACK_LOSING: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002269 NetworkRequest request = (NetworkRequest)getObject(message,
2270 NetworkRequest.class);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002271 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002272 if (callbacks != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002273 callbacks.onLosing((Network)getObject(message, Network.class),
2274 message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002275 } else {
2276 Log.e(TAG, "callback not found for LOSING message");
2277 }
2278 break;
2279 }
2280 case CALLBACK_LOST: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002281 NetworkRequest request = (NetworkRequest)getObject(message,
2282 NetworkRequest.class);
2283
Robert Greenwalt6078b502014-06-11 16:05:07 -07002284 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002285 if (callbacks != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002286 callbacks.onLost((Network)getObject(message, Network.class));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002287 } else {
2288 Log.e(TAG, "callback not found for LOST message");
2289 }
2290 break;
2291 }
2292 case CALLBACK_UNAVAIL: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002293 NetworkRequest request = (NetworkRequest)getObject(message,
2294 NetworkRequest.class);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002295 NetworkCallback callbacks = null;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002296 synchronized(mCallbackMap) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002297 callbacks = mCallbackMap.get(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002298 }
2299 if (callbacks != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002300 callbacks.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002301 } else {
2302 Log.e(TAG, "callback not found for UNAVAIL message");
2303 }
2304 break;
2305 }
2306 case CALLBACK_CAP_CHANGED: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002307 NetworkRequest request = (NetworkRequest)getObject(message,
2308 NetworkRequest.class);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002309 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002310 if (callbacks != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002311 Network network = (Network)getObject(message, Network.class);
2312 NetworkCapabilities cap = (NetworkCapabilities)getObject(message,
2313 NetworkCapabilities.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002314
Robert Greenwalt6078b502014-06-11 16:05:07 -07002315 callbacks.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002316 } else {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002317 Log.e(TAG, "callback not found for CAP_CHANGED message");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002318 }
2319 break;
2320 }
2321 case CALLBACK_IP_CHANGED: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002322 NetworkRequest request = (NetworkRequest)getObject(message,
2323 NetworkRequest.class);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002324 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002325 if (callbacks != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002326 Network network = (Network)getObject(message, Network.class);
2327 LinkProperties lp = (LinkProperties)getObject(message,
2328 LinkProperties.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002329
Robert Greenwalt6078b502014-06-11 16:05:07 -07002330 callbacks.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002331 } else {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002332 Log.e(TAG, "callback not found for IP_CHANGED message");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002333 }
2334 break;
2335 }
2336 case CALLBACK_RELEASED: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002337 NetworkRequest req = (NetworkRequest)getObject(message, NetworkRequest.class);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002338 NetworkCallback callbacks = null;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002339 synchronized(mCallbackMap) {
2340 callbacks = mCallbackMap.remove(req);
2341 }
2342 if (callbacks != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002343 synchronized(mRefCount) {
2344 if (mRefCount.decrementAndGet() == 0) {
2345 getLooper().quit();
2346 }
2347 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002348 } else {
2349 Log.e(TAG, "callback not found for CANCELED message");
2350 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002351 break;
2352 }
2353 case CALLBACK_EXIT: {
2354 Log.d(TAG, "Listener quiting");
2355 getLooper().quit();
2356 break;
2357 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07002358 case EXPIRE_LEGACY_REQUEST: {
2359 expireRequest((NetworkCapabilities)message.obj, message.arg1);
2360 break;
2361 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002362 }
2363 }
2364
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002365 private Object getObject(Message msg, Class c) {
2366 return msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07002367 }
Robert Greenwalt6078b502014-06-11 16:05:07 -07002368 private NetworkCallback getCallbacks(NetworkRequest req) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002369 synchronized(mCallbackMap) {
2370 return mCallbackMap.get(req);
2371 }
2372 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002373 }
2374
Robert Greenwalt6078b502014-06-11 16:05:07 -07002375 private void incCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002376 synchronized(sCallbackRefCount) {
2377 if (sCallbackRefCount.incrementAndGet() == 1) {
2378 // TODO - switch this over to a ManagerThread or expire it when done
2379 HandlerThread callbackThread = new HandlerThread("ConnectivityManager");
2380 callbackThread.start();
2381 sCallbackHandler = new CallbackHandler(callbackThread.getLooper(),
Robert Greenwalt6078b502014-06-11 16:05:07 -07002382 sNetworkCallback, sCallbackRefCount, this);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002383 }
2384 }
2385 }
2386
Robert Greenwalt6078b502014-06-11 16:05:07 -07002387 private void decCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002388 synchronized(sCallbackRefCount) {
2389 if (sCallbackRefCount.decrementAndGet() == 0) {
2390 sCallbackHandler.obtainMessage(CALLBACK_EXIT).sendToTarget();
2391 sCallbackHandler = null;
2392 }
2393 }
2394 }
2395
Robert Greenwalt6078b502014-06-11 16:05:07 -07002396 static final HashMap<NetworkRequest, NetworkCallback> sNetworkCallback =
2397 new HashMap<NetworkRequest, NetworkCallback>();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002398 static final AtomicInteger sCallbackRefCount = new AtomicInteger(0);
2399 static CallbackHandler sCallbackHandler = null;
2400
2401 private final static int LISTEN = 1;
2402 private final static int REQUEST = 2;
2403
Robert Greenwalt562cc542014-05-15 18:07:26 -07002404 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002405 NetworkCallback networkCallback, int timeoutSec, int action,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07002406 int legacyType) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002407 if (networkCallback == null) {
2408 throw new IllegalArgumentException("null NetworkCallback");
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002409 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002410 if (need == null) throw new IllegalArgumentException("null NetworkCapabilities");
2411 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002412 incCallbackHandlerRefCount();
Paul Jensen7221cc32014-06-27 11:05:32 -04002413 synchronized(sNetworkCallback) {
2414 if (action == LISTEN) {
2415 networkCallback.networkRequest = mService.listenForNetwork(need,
2416 new Messenger(sCallbackHandler), new Binder());
2417 } else {
2418 networkCallback.networkRequest = mService.requestNetwork(need,
2419 new Messenger(sCallbackHandler), timeoutSec, new Binder(), legacyType);
2420 }
2421 if (networkCallback.networkRequest != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002422 sNetworkCallback.put(networkCallback.networkRequest, networkCallback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002423 }
2424 }
2425 } catch (RemoteException e) {}
Robert Greenwalt6078b502014-06-11 16:05:07 -07002426 if (networkCallback.networkRequest == null) decCallbackHandlerRefCount();
2427 return networkCallback.networkRequest;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002428 }
2429
2430 /**
2431 * Request a network to satisfy a set of {@link NetworkCapabilities}.
2432 *
2433 * This {@link NetworkRequest} will live until released via
Robert Greenwalt6078b502014-06-11 16:05:07 -07002434 * {@link #unregisterNetworkCallback} or the calling application exits.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002435 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07002436 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002437 * can be used to direct traffic to the network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002438 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002439 * @param request {@link NetworkRequest} describing this request.
2440 * @param networkCallback The {@link NetworkCallback} to be utilized for this
2441 * request. Note the callback must not be shared - they
2442 * uniquely specify this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002443 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002444 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
2445 sendRequestForNetwork(request.networkCapabilities, networkCallback, 0,
Ye Wenb87875e2014-07-21 14:19:01 -07002446 REQUEST, inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002447 }
2448
2449 /**
2450 * Request a network to satisfy a set of {@link NetworkCapabilities}, limited
2451 * by a timeout.
2452 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002453 * This function behaves identically to the non-timedout version, but if a suitable
Robert Greenwalt6078b502014-06-11 16:05:07 -07002454 * network is not found within the given time (in milliseconds) the
2455 * {@link NetworkCallback#unavailable} callback is called. The request must
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002456 * still be released normally by calling {@link releaseNetworkRequest}.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002457 * @param request {@link NetworkRequest} describing this request.
2458 * @param networkCallback The callbacks to be utilized for this request. Note
2459 * the callbacks must not be shared - they uniquely specify
2460 * this request.
2461 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
2462 * before {@link NetworkCallback#unavailable} is called.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002463 * @hide
2464 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002465 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
2466 int timeoutMs) {
2467 sendRequestForNetwork(request.networkCapabilities, networkCallback, timeoutMs,
Ye Wenb87875e2014-07-21 14:19:01 -07002468 REQUEST, inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002469 }
2470
2471 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002472 * The maximum number of milliseconds the framework will look for a suitable network
Robert Greenwalt9258c642014-03-26 16:47:06 -07002473 * during a timeout-equiped call to {@link requestNetwork}.
2474 * {@hide}
2475 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002476 public final static int MAX_NETWORK_REQUEST_TIMEOUT_MS = 100 * 60 * 1000;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002477
2478 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002479 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002480 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002481 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfcde58f2015-02-11 16:51:13 -08002482 * <p>
Paul Jensen72db88e2015-03-10 10:54:12 -04002483 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
2484 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002485 */
Erik Kline90e93072014-11-19 12:12:24 +09002486 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002487
2488 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002489 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002490 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002491 * {@link android.content.Intent#getParcelableExtra(String)}.
2492 */
Erik Kline90e93072014-11-19 12:12:24 +09002493 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002494
2495
2496 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07002497 * Request a network to satisfy a set of {@link NetworkCapabilities}.
2498 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002499 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07002500 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002501 * the request may outlive the calling application and get called back when a suitable
2502 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002503 * <p>
2504 * The operation is an Intent broadcast that goes to a broadcast receiver that
2505 * you registered with {@link Context#registerReceiver} or through the
2506 * &lt;receiver&gt; tag in an AndroidManifest.xml file
2507 * <p>
2508 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09002509 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
2510 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07002511 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002512 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07002513 * Intent to reserve the network or it will be released shortly after the Intent
2514 * is processed.
2515 * <p>
2516 * If there is already an request for this Intent registered (with the equality of
2517 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002518 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002519 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002520 * The request may be released normally by calling
2521 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002522 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002523 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002524 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07002525 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002526 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002527 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002528 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002529 checkPendingIntent(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002530 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002531 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002532 } catch (RemoteException e) {}
Robert Greenwalt9258c642014-03-26 16:47:06 -07002533 }
2534
2535 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002536 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
2537 * <p>
2538 * This method has the same behavior as {@link #unregisterNetworkCallback} with respect to
2539 * releasing network resources and disconnecting.
2540 *
2541 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
2542 * PendingIntent passed to
2543 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
2544 * corresponding NetworkRequest you'd like to remove. Cannot be null.
2545 */
2546 public void releaseNetworkRequest(PendingIntent operation) {
2547 checkPendingIntent(operation);
2548 try {
2549 mService.releasePendingNetworkRequest(operation);
2550 } catch (RemoteException e) {}
2551 }
2552
2553 private void checkPendingIntent(PendingIntent intent) {
2554 if (intent == null) {
2555 throw new IllegalArgumentException("PendingIntent cannot be null.");
2556 }
2557 }
2558
2559 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07002560 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07002561 * {@link NetworkRequest}. The callbacks will continue to be called until
2562 * either the application exits or {@link #unregisterNetworkCallback} is called
Robert Greenwalt9258c642014-03-26 16:47:06 -07002563 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002564 * @param request {@link NetworkRequest} describing this request.
2565 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
2566 * networks change state.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002567 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002568 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
2569 sendRequestForNetwork(request.networkCapabilities, networkCallback, 0, LISTEN, TYPE_NONE);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002570 }
2571
2572 /**
fenglu2bbd2b62015-03-20 11:29:56 -07002573 * Request connectivityservice to refresh network capabilities for the given
2574 * {@link network}. This method returns true if the network is still active, false
2575 * otherwise. Notice the method call assumes the caller has registered for
2576 * listening NetworkCapabilities updates.
2577 *
2578 * @param network{@link Network} specifying which network you're interested.
2579 * @hide
2580 */
2581 public boolean requestBwUpdate(Network network) {
2582 try {
2583 return mService.requestBwUpdate(network);
2584 } catch (RemoteException e) {
2585 return false;
2586 }
2587 }
2588
2589 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002590 * Unregisters callbacks about and possibly releases networks originating from
2591 * {@link #requestNetwork} and {@link #registerNetworkCallback} calls. If the
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002592 * given {@code NetworkCallback} had previously been used with {@code #requestNetwork},
Robert Greenwalt6078b502014-06-11 16:05:07 -07002593 * any networks that had been connected to only to satisfy that request will be
2594 * disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002595 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002596 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002597 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002598 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
2599 if (networkCallback == null || networkCallback.networkRequest == null ||
2600 networkCallback.networkRequest.requestId == REQUEST_ID_UNSET) {
2601 throw new IllegalArgumentException("Invalid NetworkCallback");
2602 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002603 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002604 mService.releaseNetworkRequest(networkCallback.networkRequest);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002605 } catch (RemoteException e) {}
2606 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002607
2608 /**
2609 * Binds the current process to {@code network}. All Sockets created in the future
2610 * (and not explicitly bound via a bound SocketFactory from
2611 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
2612 * {@code network}. All host name resolutions will be limited to {@code network} as well.
2613 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
2614 * work and all host name resolutions will fail. This is by design so an application doesn't
2615 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
2616 * To clear binding pass {@code null} for {@code network}. Using individually bound
2617 * Sockets created by Network.getSocketFactory().createSocket() and
2618 * performing network-specific host name resolutions via
2619 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen72db88e2015-03-10 10:54:12 -04002620 * {@code bindProcessToNetwork}.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002621 *
2622 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
2623 * the current binding.
2624 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2625 */
Paul Jensen72db88e2015-03-10 10:54:12 -04002626 public boolean bindProcessToNetwork(Network network) {
2627 // Forcing callers to call thru non-static function ensures ConnectivityManager
2628 // instantiated.
2629 return setProcessDefaultNetwork(network);
2630 }
2631
2632 /**
2633 * Binds the current process to {@code network}. All Sockets created in the future
2634 * (and not explicitly bound via a bound SocketFactory from
2635 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
2636 * {@code network}. All host name resolutions will be limited to {@code network} as well.
2637 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
2638 * work and all host name resolutions will fail. This is by design so an application doesn't
2639 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
2640 * To clear binding pass {@code null} for {@code network}. Using individually bound
2641 * Sockets created by Network.getSocketFactory().createSocket() and
2642 * performing network-specific host name resolutions via
2643 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
2644 * {@code setProcessDefaultNetwork}.
2645 *
2646 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
2647 * the current binding.
2648 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2649 * @deprecated This function can throw {@link IllegalStateException}. Use
2650 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
2651 * is a direct replacement.
2652 */
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002653 public static boolean setProcessDefaultNetwork(Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04002654 int netId = (network == null) ? NETID_UNSET : network.netId;
Paul Jensen72db88e2015-03-10 10:54:12 -04002655 if (netId == NetworkUtils.getBoundNetworkForProcess()) {
Paul Jensenc91b5342014-08-27 12:38:45 -04002656 return true;
2657 }
2658 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05002659 // Set HTTP proxy system properties to match network.
2660 // TODO: Deprecate this static method and replace it with a non-static version.
2661 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
Paul Jensenc91b5342014-08-27 12:38:45 -04002662 // Must flush DNS cache as new network may have different DNS resolutions.
2663 InetAddress.clearDnsCache();
2664 // Must flush socket pool as idle sockets will be bound to previous network and may
2665 // cause subsequent fetches to be performed on old network.
2666 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
2667 return true;
2668 } else {
2669 return false;
2670 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002671 }
2672
2673 /**
2674 * Returns the {@link Network} currently bound to this process via
Paul Jensen72db88e2015-03-10 10:54:12 -04002675 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002676 *
2677 * @return {@code Network} to which this process is bound, or {@code null}.
2678 */
Paul Jensen72db88e2015-03-10 10:54:12 -04002679 public Network getBoundNetworkForProcess() {
2680 // Forcing callers to call thru non-static function ensures ConnectivityManager
2681 // instantiated.
2682 return getProcessDefaultNetwork();
2683 }
2684
2685 /**
2686 * Returns the {@link Network} currently bound to this process via
2687 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
2688 *
2689 * @return {@code Network} to which this process is bound, or {@code null}.
2690 * @deprecated Using this function can lead to other functions throwing
2691 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
2692 * {@code getBoundNetworkForProcess} is a direct replacement.
2693 */
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002694 public static Network getProcessDefaultNetwork() {
Paul Jensen72db88e2015-03-10 10:54:12 -04002695 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04002696 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002697 return new Network(netId);
2698 }
2699
2700 /**
2701 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen72db88e2015-03-10 10:54:12 -04002702 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002703 *
2704 * @param network The {@link Network} to bind host resolutions from the current process to, or
2705 * {@code null} to clear the current binding.
2706 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2707 * @hide
2708 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
2709 */
2710 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04002711 return NetworkUtils.bindProcessToNetworkForHostResolution(
2712 network == null ? NETID_UNSET : network.netId);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002713 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002714}