blob: ba639696c17c9ddb03aedeee29da007f8cf8c81e [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 Greenwalt9258c642014-03-26 16:47:06 -070028import android.os.Handler;
29import android.os.HandlerThread;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080030import android.os.IBinder;
31import android.os.INetworkActivityListener;
32import android.os.INetworkManagementService;
Robert Greenwalt9258c642014-03-26 16:47:06 -070033import android.os.Looper;
34import android.os.Message;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -070035import android.os.Messenger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.RemoteException;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080037import android.os.ServiceManager;
Jeff Sharkey961e3042011-08-29 16:02:57 -070038import android.provider.Settings;
Wink Saville36ffb042014-12-05 11:10:30 -080039import android.telephony.SubscriptionManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080040import android.util.ArrayMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -070041import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
Robert Greenwaltafa05c02014-05-21 20:04:36 -070043import com.android.internal.telephony.ITelephony;
Robert Greenwalt562cc542014-05-15 18:07:26 -070044import com.android.internal.telephony.PhoneConstants;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070045import com.android.internal.util.Protocol;
46
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070047import java.net.InetAddress;
Robert Greenwalt9258c642014-03-26 16:47:06 -070048import java.util.concurrent.atomic.AtomicInteger;
49import java.util.HashMap;
50
Paul Jensenc91b5342014-08-27 12:38:45 -040051import libcore.net.event.NetworkEventDispatcher;
52
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053/**
54 * Class that answers queries about the state of network connectivity. It also
55 * notifies applications when network connectivity changes. Get an instance
56 * of this class by calling
57 * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}.
58 * <p>
59 * The primary responsibilities of this class are to:
60 * <ol>
61 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
62 * <li>Send broadcast intents when network connectivity changes</li>
63 * <li>Attempt to "fail over" to another network when connectivity to a network
64 * is lost</li>
65 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
66 * state of the available networks</li>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070067 * <li>Provide an API that allows applications to request and select networks for their data
68 * traffic</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 * </ol>
70 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070071public class ConnectivityManager {
72 private static final String TAG = "ConnectivityManager";
73
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070075 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 * been established or lost. The NetworkInfo for the affected network is
77 * sent as an extra; it should be consulted to see what kind of
78 * connectivity event occurred.
79 * <p/>
80 * If this is a connection that was the result of failing over from a
81 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
82 * set to true.
83 * <p/>
84 * For a loss of connectivity, if the connectivity manager is attempting
85 * to connect (or has already connected) to another network, the
86 * NetworkInfo for the new network is also passed as an extra. This lets
87 * any receivers of the broadcast know that they should not necessarily
88 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -080089 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 * the failover attempt succeeded (and so there is still overall data
91 * connectivity), or that the failover attempt failed, meaning that all
92 * connectivity has been lost.
93 * <p/>
94 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
95 * is set to {@code true} if there are no connected networks at all.
96 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -080097 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -070099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 /**
Paul Jensen25a217c2015-02-27 22:55:47 -0500101 * The device has connected to a network that has presented a captive
102 * portal, which is blocking Internet connectivity. The user was presented
103 * with a notification that network sign in is required,
104 * and the user invoked the notification's action indicating they
Paul Jensen49e3edf2015-05-22 10:50:39 -0400105 * desire to sign in to the network. Apps handling this activity should
Paul Jensen25a217c2015-02-27 22:55:47 -0500106 * facilitate signing in to the network. This action includes a
107 * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents
108 * the network presenting the captive portal; all communication with the
109 * captive portal must be done using this {@code Network} object.
110 * <p/>
Paul Jensen49e3edf2015-05-22 10:50:39 -0400111 * This activity includes a {@link CaptivePortal} extra named
112 * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different
113 * outcomes of the captive portal sign in to the system:
114 * <ul>
115 * <li> When the app handling this action believes the user has signed in to
116 * the network and the captive portal has been dismissed, the app should
117 * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can
118 * reevaluate the network. If reevaluation finds the network no longer
119 * subject to a captive portal, the network may become the default active
120 * data network. </li>
121 * <li> When the app handling this action believes the user explicitly wants
Paul Jensen25a217c2015-02-27 22:55:47 -0500122 * to ignore the captive portal and the network, the app should call
Paul Jensen49e3edf2015-05-22 10:50:39 -0400123 * {@link CaptivePortal#ignoreNetwork}. </li>
124 * </ul>
Paul Jensen25a217c2015-02-27 22:55:47 -0500125 */
126 @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
127 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL";
128
129 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 * The lookup key for a {@link NetworkInfo} object. Retrieve with
131 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700132 *
133 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
134 * should always obtain network information through
Paul Jensen3541e9f2015-03-18 12:23:02 -0400135 * {@link #getActiveNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700136 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800137 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700138 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800139 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700140
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800141 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700142 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700143 *
144 * @see android.content.Intent#getIntExtra(String, int)
145 */
146 public static final String EXTRA_NETWORK_TYPE = "networkType";
147
148 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800149 * The lookup key for a boolean that indicates whether a connect event
150 * is for a network to which the connectivity manager was failing over
151 * following a disconnect on another network.
152 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
153 */
154 public static final String EXTRA_IS_FAILOVER = "isFailover";
155 /**
156 * The lookup key for a {@link NetworkInfo} object. This is supplied when
157 * there is another network that it may be possible to connect to. Retrieve with
158 * {@link android.content.Intent#getParcelableExtra(String)}.
159 */
160 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
161 /**
162 * The lookup key for a boolean that indicates whether there is a
163 * complete lack of connectivity, i.e., no network is available.
164 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
165 */
166 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
167 /**
168 * The lookup key for a string that indicates why an attempt to connect
169 * to a network failed. The string has no particular structure. It is
170 * intended to be used in notifications presented to users. Retrieve
171 * it with {@link android.content.Intent#getStringExtra(String)}.
172 */
173 public static final String EXTRA_REASON = "reason";
174 /**
175 * The lookup key for a string that provides optionally supplied
176 * extra information about the network state. The information
177 * may be passed up from the lower networking layers, and its
178 * meaning may be specific to a particular network type. Retrieve
179 * it with {@link android.content.Intent#getStringExtra(String)}.
180 */
181 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700182 /**
183 * The lookup key for an int that provides information about
184 * our connection to the internet at large. 0 indicates no connection,
185 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700186 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700187 * {@hide}
188 */
189 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800190 /**
Paul Jensen49e3edf2015-05-22 10:50:39 -0400191 * The lookup key for a {@link CaptivePortal} object included with the
192 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal}
193 * object can be used to either indicate to the system that the captive
194 * portal has been dismissed or that the user does not want to pursue
195 * signing in to captive portal. Retrieve it with
196 * {@link android.content.Intent#getParcelableExtra(String)}.
Paul Jensen25a217c2015-02-27 22:55:47 -0500197 */
Paul Jensen49e3edf2015-05-22 10:50:39 -0400198 public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL";
Paul Jensen25a217c2015-02-27 22:55:47 -0500199 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700200 * Broadcast action to indicate the change of data activity status
201 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800202 * The network becomes active when data transmission is started, or
203 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700204 * {@hide}
205 */
206 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
207 public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
208 /**
209 * The lookup key for an enum that indicates the network device type on which this data activity
210 * change happens.
211 * {@hide}
212 */
213 public static final String EXTRA_DEVICE_TYPE = "deviceType";
214 /**
215 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
216 * it is actively sending or receiving data and {@code false} means it is idle.
217 * {@hide}
218 */
219 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700220 /**
221 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
222 * {@hide}
223 */
224 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700225
226 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800227 * Broadcast Action: The setting for background data usage has changed
228 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
229 * <p>
230 * If an application uses the network in the background, it should listen
231 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700232 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800233 * <p>
234 *
235 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
236 * of background data depends on several combined factors, and
237 * this broadcast is no longer sent. Instead, when background
238 * data is unavailable, {@link #getActiveNetworkInfo()} will now
239 * appear disconnected. During first boot after a platform
240 * upgrade, this broadcast will be sent once if
241 * {@link #getBackgroundDataSetting()} was {@code false} before
242 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800243 */
244 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800245 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800246 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
247 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
248
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700249 /**
250 * Broadcast Action: The network connection may not be good
251 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
252 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
253 * the network and it's condition.
254 * @hide
255 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800256 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700257 public static final String INET_CONDITION_ACTION =
258 "android.net.conn.INET_CONDITION_ACTION";
259
Robert Greenwalt42acef32009-08-12 16:08:25 -0700260 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800261 * Broadcast Action: A tetherable connection has come or gone.
262 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
263 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER} and
264 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
265 * the current state of tethering. Each include a list of
266 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800267 * @hide
268 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800269 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800270 public static final String ACTION_TETHER_STATE_CHANGED =
271 "android.net.conn.TETHER_STATE_CHANGED";
272
273 /**
274 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800275 * gives a String[] listing all the interfaces configured for
276 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800277 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800278 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800279
280 /**
281 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800282 * gives a String[] listing all the interfaces currently tethered
283 * (ie, has dhcp support and packets potentially forwarded/NATed)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800284 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800285 public static final String EXTRA_ACTIVE_TETHER = "activeArray";
286
287 /**
288 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800289 * gives a String[] listing all the interfaces we tried to tether and
290 * failed. Use {@link #getLastTetherError} to find the error code
291 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800292 */
293 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800294
295 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800296 * Broadcast Action: The captive portal tracker has finished its test.
297 * Sent only while running Setup Wizard, in lieu of showing a user
298 * notification.
299 * @hide
300 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800301 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800302 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
303 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
304 /**
305 * The lookup key for a boolean that indicates whether a captive portal was detected.
306 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
307 * @hide
308 */
309 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
310
311 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900312 * Action used to display a dialog that asks the user whether to connect to a network that is
313 * not validated. This intent is used to start the dialog in settings via startActivity.
314 *
315 * @hide
316 */
317 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED";
318
319 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800320 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700321 * @hide
322 */
323 public static final int TYPE_NONE = -1;
324
325 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800326 * The Mobile data connection. When active, all data traffic
327 * will use this network type's interface by default
328 * (it has a default route)
Robert Greenwalt42acef32009-08-12 16:08:25 -0700329 */
330 public static final int TYPE_MOBILE = 0;
331 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800332 * The WIFI data connection. When active, all data traffic
333 * will use this network type's interface by default
334 * (it has a default route).
Robert Greenwalt42acef32009-08-12 16:08:25 -0700335 */
336 public static final int TYPE_WIFI = 1;
337 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800338 * An MMS-specific Mobile data connection. This network type may use the
339 * same network interface as {@link #TYPE_MOBILE} or it may use a different
340 * one. This is used by applications needing to talk to the carrier's
341 * Multimedia Messaging Service servers.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900342 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900343 * @deprecated Applications should instead use
344 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900345 * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700346 */
347 public static final int TYPE_MOBILE_MMS = 2;
348 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800349 * A SUPL-specific Mobile data connection. This network type may use the
350 * same network interface as {@link #TYPE_MOBILE} or it may use a different
351 * one. This is used by applications needing to talk to the carrier's
352 * Secure User Plane Location servers for help locating the device.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900353 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900354 * @deprecated Applications should instead use
355 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900356 * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700357 */
358 public static final int TYPE_MOBILE_SUPL = 3;
359 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800360 * A DUN-specific Mobile data connection. This network type may use the
361 * same network interface as {@link #TYPE_MOBILE} or it may use a different
362 * one. This is sometimes by the system when setting up an upstream connection
363 * for tethering so that the carrier is aware of DUN traffic.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700364 */
365 public static final int TYPE_MOBILE_DUN = 4;
366 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800367 * A High Priority Mobile data connection. This network type uses the
368 * same network interface as {@link #TYPE_MOBILE} but the routing setup
Lorenzo Colittie285b432015-04-23 15:32:42 +0900369 * is different.
370 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900371 * @deprecated Applications should instead use
372 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that
Lorenzo Colittie285b432015-04-23 15:32:42 +0900373 * uses the {@link NetworkCapabilities#TRANSPORT_CELLULAR} transport.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700374 */
375 public static final int TYPE_MOBILE_HIPRI = 5;
jsh8214deb2010-03-11 15:04:43 -0800376 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800377 * The WiMAX data connection. When active, all data traffic
378 * will use this network type's interface by default
379 * (it has a default route).
jsh8214deb2010-03-11 15:04:43 -0800380 */
381 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800382
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800383 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800384 * The Bluetooth data connection. When active, all data traffic
385 * will use this network type's interface by default
386 * (it has a default route).
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800387 */
388 public static final int TYPE_BLUETOOTH = 7;
389
Robert Greenwalt60810842011-04-22 15:28:18 -0700390 /**
391 * Dummy data connection. This should not be used on shipping devices.
392 */
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800393 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800394
Robert Greenwalt60810842011-04-22 15:28:18 -0700395 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800396 * The Ethernet data connection. When active, all data traffic
397 * will use this network type's interface by default
398 * (it has a default route).
Robert Greenwalt60810842011-04-22 15:28:18 -0700399 */
Robert Greenwalte12aec92011-01-28 14:48:37 -0800400 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700401
Wink Saville9d7d6282011-03-12 14:52:01 -0800402 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800403 * Over the air Administration.
Wink Saville9d7d6282011-03-12 14:52:01 -0800404 * {@hide}
405 */
406 public static final int TYPE_MOBILE_FOTA = 10;
407
408 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800409 * IP Multimedia Subsystem.
Wink Saville9d7d6282011-03-12 14:52:01 -0800410 * {@hide}
411 */
412 public static final int TYPE_MOBILE_IMS = 11;
413
414 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800415 * Carrier Branded Services.
Wink Saville9d7d6282011-03-12 14:52:01 -0800416 * {@hide}
417 */
418 public static final int TYPE_MOBILE_CBS = 12;
419
repo syncaea743a2011-07-29 23:55:49 -0700420 /**
421 * A Wi-Fi p2p connection. Only requesting processes will have access to
422 * the peers connected.
423 * {@hide}
424 */
425 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800426
Wink Saville5e56bc52013-07-29 15:00:57 -0700427 /**
428 * The network to use for initially attaching to the network
429 * {@hide}
430 */
431 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700432
Lorenzo Colittie285b432015-04-23 15:32:42 +0900433 /**
Robert Greenwalt4bd43892015-07-09 14:49:35 -0700434 * Emergency PDN connection for emergency services. This
435 * may include IMS and MMS in emergency situations.
Ram3e0e3bc2014-06-26 11:03:44 -0700436 * {@hide}
437 */
438 public static final int TYPE_MOBILE_EMERGENCY = 15;
439
Hui Lu1c5624a2014-01-15 11:05:36 -0500440 /**
441 * The network that uses proxy to achieve connectivity.
442 * {@hide}
443 */
444 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700445
Robert Greenwalt8283f882014-07-07 17:09:01 -0700446 /**
447 * A virtual network using one or more native bearers.
448 * It may or may not be providing security services.
449 */
450 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500451
452 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700453 public static final int MAX_RADIO_TYPE = TYPE_VPN;
454
455 /** {@hide} */
456 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800458 /**
459 * If you want to set the default network preference,you can directly
460 * change the networkAttributes array in framework's config.xml.
461 *
462 * @deprecated Since we support so many more networks now, the single
463 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800464 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800465 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800466 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800467 * from an App.
468 */
469 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800470 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
471
Jeff Sharkey625239a2012-09-26 22:03:49 -0700472 /**
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700473 * @hide
474 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700475 public final static int REQUEST_ID_UNSET = 0;
476
Paul Jensen5d59e782014-07-11 12:28:19 -0400477 /**
478 * A NetID indicating no Network is selected.
479 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
480 * @hide
481 */
482 public static final int NETID_UNSET = 0;
483
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700484 private final IConnectivityManager mService;
Paul Jensene0bef712014-12-10 15:12:18 -0500485 /**
486 * A kludge to facilitate static access where a Context pointer isn't available, like in the
487 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
488 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
489 * methods that take a Context argument.
490 */
491 private static ConnectivityManager sInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800492
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800493 private INetworkManagementService mNMService;
494
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800495 /**
496 * Tests if a given integer represents a valid network type.
497 * @param networkType the type to be tested
498 * @return a boolean. {@code true} if the type is valid, else {@code false}
Paul Jensen9e59e122015-05-06 10:42:25 -0400499 * @deprecated All APIs accepting a network type are deprecated. There should be no need to
500 * validate a network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800501 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700502 public static boolean isNetworkTypeValid(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700503 return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800504 }
505
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800506 /**
507 * Returns a non-localized string representing a given network type.
508 * ONLY used for debugging output.
509 * @param type the type needing naming
510 * @return a String for the given type, or a string version of the type ("87")
511 * if no name is known.
512 * {@hide}
513 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700514 public static String getNetworkTypeName(int type) {
515 switch (type) {
516 case TYPE_MOBILE:
517 return "MOBILE";
518 case TYPE_WIFI:
519 return "WIFI";
520 case TYPE_MOBILE_MMS:
521 return "MOBILE_MMS";
522 case TYPE_MOBILE_SUPL:
523 return "MOBILE_SUPL";
524 case TYPE_MOBILE_DUN:
525 return "MOBILE_DUN";
526 case TYPE_MOBILE_HIPRI:
527 return "MOBILE_HIPRI";
528 case TYPE_WIMAX:
529 return "WIMAX";
530 case TYPE_BLUETOOTH:
531 return "BLUETOOTH";
532 case TYPE_DUMMY:
533 return "DUMMY";
534 case TYPE_ETHERNET:
535 return "ETHERNET";
536 case TYPE_MOBILE_FOTA:
537 return "MOBILE_FOTA";
538 case TYPE_MOBILE_IMS:
539 return "MOBILE_IMS";
540 case TYPE_MOBILE_CBS:
541 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700542 case TYPE_WIFI_P2P:
543 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700544 case TYPE_MOBILE_IA:
545 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700546 case TYPE_MOBILE_EMERGENCY:
547 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500548 case TYPE_PROXY:
549 return "PROXY";
Erik Kline37fbfa12014-11-19 17:23:41 +0900550 case TYPE_VPN:
551 return "VPN";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700552 default:
553 return Integer.toString(type);
554 }
555 }
556
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800557 /**
558 * Checks if a given type uses the cellular data connection.
559 * This should be replaced in the future by a network property.
560 * @param networkType the type to check
561 * @return a boolean - {@code true} if uses cellular network, else {@code false}
562 * {@hide}
563 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700564 public static boolean isNetworkTypeMobile(int networkType) {
565 switch (networkType) {
566 case TYPE_MOBILE:
567 case TYPE_MOBILE_MMS:
568 case TYPE_MOBILE_SUPL:
569 case TYPE_MOBILE_DUN:
570 case TYPE_MOBILE_HIPRI:
571 case TYPE_MOBILE_FOTA:
572 case TYPE_MOBILE_IMS:
573 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700574 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700575 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700576 return true;
577 default:
578 return false;
579 }
580 }
581
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800582 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700583 * Checks if the given network type is backed by a Wi-Fi radio.
584 *
585 * @hide
586 */
587 public static boolean isNetworkTypeWifi(int networkType) {
588 switch (networkType) {
589 case TYPE_WIFI:
590 case TYPE_WIFI_P2P:
591 return true;
592 default:
593 return false;
594 }
595 }
596
597 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800598 * Specifies the preferred network type. When the device has more
599 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800600 *
601 * @param preference the network type to prefer over all others. It is
602 * unspecified what happens to the old preferred network in the
603 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700604 * @deprecated Functionality has been removed as it no longer makes sense,
605 * with many more than two networks - we'd need an array to express
606 * preference. Instead we use dynamic network properties of
607 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800608 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800609 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800610 }
611
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800612 /**
613 * Retrieves the current preferred network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400614 * <p>This method requires the caller to hold the permission
615 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800616 *
617 * @return an integer representing the preferred network type
618 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700619 * @deprecated Functionality has been removed as it no longer makes sense,
620 * with many more than two networks - we'd need an array to express
621 * preference. Instead we use dynamic network properties of
622 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800623 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800624 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700625 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800626 }
627
Scott Main671644c2011-10-06 19:02:28 -0700628 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800629 * Returns details about the currently active default data network. When
630 * connected, this network is the default route for outgoing connections.
631 * You should always check {@link NetworkInfo#isConnected()} before initiating
632 * network traffic. This may return {@code null} when there is no default
633 * network.
Paul Jensenb2748922015-05-06 11:10:18 -0400634 * <p>This method requires the caller to hold the permission
635 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800636 *
637 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500638 * or {@code null} if no default network is currently active
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700639 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 public NetworkInfo getActiveNetworkInfo() {
641 try {
642 return mService.getActiveNetworkInfo();
643 } catch (RemoteException e) {
644 return null;
645 }
646 }
647
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800648 /**
Paul Jensen31a94f42015-02-13 14:18:39 -0500649 * Returns a {@link Network} object corresponding to the currently active
650 * default data network. In the event that the current active default data
651 * network disconnects, the returned {@code Network} object will no longer
652 * be usable. This will return {@code null} when there is no default
653 * network.
Paul Jensenb2748922015-05-06 11:10:18 -0400654 * <p>This method requires the caller to hold the permission
655 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Paul Jensen31a94f42015-02-13 14:18:39 -0500656 *
657 * @return a {@link Network} object for the current default network or
658 * {@code null} if no default network is currently active
Paul Jensen31a94f42015-02-13 14:18:39 -0500659 */
660 public Network getActiveNetwork() {
661 try {
662 return mService.getActiveNetwork();
663 } catch (RemoteException e) {
664 return null;
665 }
666 }
667
668 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800669 * Returns details about the currently active default data network
670 * for a given uid. This is for internal use only to avoid spying
671 * other apps.
Paul Jensenb2748922015-05-06 11:10:18 -0400672 * <p>This method requires the caller to hold the permission
673 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800674 *
675 * @return a {@link NetworkInfo} object for the current default network
676 * for the given uid or {@code null} if no default network is
677 * available for the specified uid.
678 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800679 * {@hide}
680 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700681 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
682 try {
683 return mService.getActiveNetworkInfoForUid(uid);
684 } catch (RemoteException e) {
685 return null;
686 }
687 }
688
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800689 /**
690 * Returns connection status information about a particular
691 * network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400692 * <p>This method requires the caller to hold the permission
693 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800694 *
695 * @param networkType integer specifying which networkType in
696 * which you're interested.
697 * @return a {@link NetworkInfo} object for the requested
698 * network type or {@code null} if the type is not
699 * supported by the device.
700 *
Paul Jensen3541e9f2015-03-18 12:23:02 -0400701 * @deprecated This method does not support multiple connected networks
702 * of the same type. Use {@link #getAllNetworks} and
703 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800704 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800705 public NetworkInfo getNetworkInfo(int networkType) {
706 try {
707 return mService.getNetworkInfo(networkType);
708 } catch (RemoteException e) {
709 return null;
710 }
711 }
712
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800713 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700714 * Returns connection status information about a particular
715 * Network.
Paul Jensenb2748922015-05-06 11:10:18 -0400716 * <p>This method requires the caller to hold the permission
717 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700718 *
719 * @param network {@link Network} specifying which network
720 * in which you're interested.
721 * @return a {@link NetworkInfo} object for the requested
722 * network or {@code null} if the {@code Network}
723 * is not valid.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700724 */
725 public NetworkInfo getNetworkInfo(Network network) {
726 try {
727 return mService.getNetworkInfoForNetwork(network);
728 } catch (RemoteException e) {
729 return null;
730 }
731 }
732
733 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800734 * Returns connection status information about all network
735 * types supported by the device.
Paul Jensenb2748922015-05-06 11:10:18 -0400736 * <p>This method requires the caller to hold the permission
737 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800738 *
739 * @return an array of {@link NetworkInfo} objects. Check each
740 * {@link NetworkInfo#getType} for which type each applies.
741 *
Paul Jensen3541e9f2015-03-18 12:23:02 -0400742 * @deprecated This method does not support multiple connected networks
743 * of the same type. Use {@link #getAllNetworks} and
744 * {@link #getNetworkInfo(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800745 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800746 public NetworkInfo[] getAllNetworkInfo() {
747 try {
748 return mService.getAllNetworkInfo();
749 } catch (RemoteException e) {
750 return null;
751 }
752 }
753
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800754 /**
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700755 * Returns the {@link Network} object currently serving a given type, or
756 * null if the given type is not connected.
757 *
758 * <p>This method requires the caller to hold the permission
759 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
760 *
761 * @hide
Paul Jensen3541e9f2015-03-18 12:23:02 -0400762 * @deprecated This method does not support multiple connected networks
763 * of the same type. Use {@link #getAllNetworks} and
764 * {@link #getNetworkInfo(android.net.Network)} instead.
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700765 */
766 public Network getNetworkForType(int networkType) {
767 try {
768 return mService.getNetworkForType(networkType);
769 } catch (RemoteException e) {
770 return null;
771 }
772 }
773
774 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700775 * Returns an array of all {@link Network} currently tracked by the
776 * framework.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700777 * <p>This method requires the caller to hold the permission
778 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Paul Jensenb2748922015-05-06 11:10:18 -0400779 *
780 * @return an array of {@link Network} objects.
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700781 */
782 public Network[] getAllNetworks() {
783 try {
784 return mService.getAllNetworks();
785 } catch (RemoteException e) {
786 return null;
787 }
788 }
789
790 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +0900791 * Returns an array of {@link android.net.NetworkCapabilities} objects, representing
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900792 * the Networks that applications run by the given user will use by default.
793 * @hide
794 */
795 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
796 try {
797 return mService.getDefaultNetworkCapabilitiesForUser(userId);
798 } catch (RemoteException e) {
799 return null;
800 }
801 }
802
803 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800804 * Returns the IP information for the current default network.
Paul Jensenb2748922015-05-06 11:10:18 -0400805 * <p>This method requires the caller to hold the permission
806 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800807 *
808 * @return a {@link LinkProperties} object describing the IP info
809 * for the current default network, or {@code null} if there
810 * is no current default network.
811 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800812 * {@hide}
813 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700814 public LinkProperties getActiveLinkProperties() {
815 try {
816 return mService.getActiveLinkProperties();
817 } catch (RemoteException e) {
818 return null;
819 }
820 }
821
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800822 /**
823 * Returns the IP information for a given network type.
Paul Jensenb2748922015-05-06 11:10:18 -0400824 * <p>This method requires the caller to hold the permission
825 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800826 *
827 * @param networkType the network type of interest.
828 * @return a {@link LinkProperties} object describing the IP info
829 * for the given networkType, or {@code null} if there is
830 * no current default network.
831 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800832 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -0400833 * @deprecated This method does not support multiple connected networks
834 * of the same type. Use {@link #getAllNetworks},
835 * {@link #getNetworkInfo(android.net.Network)}, and
836 * {@link #getLinkProperties(android.net.Network)} instead.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800837 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700838 public LinkProperties getLinkProperties(int networkType) {
839 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -0700840 return mService.getLinkPropertiesForType(networkType);
841 } catch (RemoteException e) {
842 return null;
843 }
844 }
845
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700846 /**
847 * Get the {@link LinkProperties} for the given {@link Network}. This
848 * will return {@code null} if the network is unknown.
Paul Jensenb2748922015-05-06 11:10:18 -0400849 * <p>This method requires the caller to hold the permission
850 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700851 *
852 * @param network The {@link Network} object identifying the network in question.
853 * @return The {@link LinkProperties} for the network, or {@code null}.
Paul Jensenb2748922015-05-06 11:10:18 -0400854 */
Robert Greenwalt9258c642014-03-26 16:47:06 -0700855 public LinkProperties getLinkProperties(Network network) {
856 try {
857 return mService.getLinkProperties(network);
858 } catch (RemoteException e) {
859 return null;
860 }
861 }
862
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700863 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +0900864 * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700865 * will return {@code null} if the network is unknown.
Paul Jensenb2748922015-05-06 11:10:18 -0400866 * <p>This method requires the caller to hold the permission
867 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700868 *
869 * @param network The {@link Network} object identifying the network in question.
Lorenzo Colittie285b432015-04-23 15:32:42 +0900870 * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700871 */
Robert Greenwalt9258c642014-03-26 16:47:06 -0700872 public NetworkCapabilities getNetworkCapabilities(Network network) {
873 try {
874 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700875 } catch (RemoteException e) {
876 return null;
877 }
878 }
879
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800880 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 * Tells the underlying networking system that the caller wants to
882 * begin using the named feature. The interpretation of {@code feature}
883 * is completely up to each networking implementation.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700884 * <p>This method requires the caller to hold the permission
885 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800886 * @param networkType specifies which network the request pertains to
887 * @param feature the name of the feature to be used
888 * @return an integer value representing the outcome of the request.
889 * The interpretation of this value is specific to each networking
890 * implementation+feature combination, except that the value {@code -1}
891 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700892 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +0900893 * @deprecated Deprecated in favor of the cleaner
894 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800895 */
896 public int startUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700897 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
898 if (netCap == null) {
899 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
900 feature);
901 return PhoneConstants.APN_REQUEST_FAILED;
902 }
903
904 NetworkRequest request = null;
905 synchronized (sLegacyRequests) {
906 LegacyRequest l = sLegacyRequests.get(netCap);
907 if (l != null) {
908 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
909 renewRequestLocked(l);
910 if (l.currentNetwork != null) {
911 return PhoneConstants.APN_ALREADY_ACTIVE;
912 } else {
913 return PhoneConstants.APN_REQUEST_STARTED;
914 }
915 }
916
917 request = requestNetworkForFeatureLocked(netCap);
918 }
919 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -0700920 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -0700921 return PhoneConstants.APN_REQUEST_STARTED;
922 } else {
923 Log.d(TAG, " request Failed");
924 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800925 }
926 }
927
928 /**
929 * Tells the underlying networking system that the caller is finished
930 * using the named feature. The interpretation of {@code feature}
931 * is completely up to each networking implementation.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700932 * <p>This method requires the caller to hold the permission
933 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800934 * @param networkType specifies which network the request pertains to
935 * @param feature the name of the feature that is no longer needed
936 * @return an integer value representing the outcome of the request.
937 * The interpretation of this value is specific to each networking
938 * implementation+feature combination, except that the value {@code -1}
939 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700940 *
Robert Greenwalta36c0742015-07-28 11:41:31 -0700941 * @deprecated Deprecated in favor of the cleaner {@link #unregisterNetworkCallback} API.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800942 */
943 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700944 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
945 if (netCap == null) {
946 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
947 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948 return -1;
949 }
Robert Greenwalt562cc542014-05-15 18:07:26 -0700950
Paul Jensen9ffb53c2014-12-17 10:39:34 -0500951 if (removeRequestForFeature(netCap)) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700952 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt562cc542014-05-15 18:07:26 -0700953 }
954 return 1;
955 }
956
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900957 /**
958 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability from the given
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900959 * NetworkCapabilities object if all the capabilities it provides are
960 * typically provided by restricted networks.
961 *
962 * TODO: consider:
963 * - Moving to NetworkCapabilities
964 * - Renaming it to guessRestrictedCapability and make it set the
965 * restricted capability bit in addition to clearing it.
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900966 * @hide
967 */
968 public static void maybeMarkCapabilitiesRestricted(NetworkCapabilities nc) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700969 for (int capability : nc.getCapabilities()) {
970 switch (capability) {
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900971 case NetworkCapabilities.NET_CAPABILITY_CBS:
972 case NetworkCapabilities.NET_CAPABILITY_DUN:
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900973 case NetworkCapabilities.NET_CAPABILITY_EIMS:
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900974 case NetworkCapabilities.NET_CAPABILITY_FOTA:
975 case NetworkCapabilities.NET_CAPABILITY_IA:
976 case NetworkCapabilities.NET_CAPABILITY_IMS:
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900977 case NetworkCapabilities.NET_CAPABILITY_RCS:
978 case NetworkCapabilities.NET_CAPABILITY_XCAP:
Robert Greenwalt9ac3dbe2014-06-05 16:39:28 -0700979 case NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED: //there by default
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900980 continue;
981 default:
982 // At least one capability usually provided by unrestricted
983 // networks. Conclude that this network is unrestricted.
984 return;
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900985 }
986 }
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900987 // All the capabilities are typically provided by restricted networks.
988 // Conclude that this network is restricted.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700989 nc.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900990 }
991
Robert Greenwalt562cc542014-05-15 18:07:26 -0700992 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
993 if (networkType == TYPE_MOBILE) {
994 int cap = -1;
995 if ("enableMMS".equals(feature)) {
996 cap = NetworkCapabilities.NET_CAPABILITY_MMS;
997 } else if ("enableSUPL".equals(feature)) {
998 cap = NetworkCapabilities.NET_CAPABILITY_SUPL;
999 } else if ("enableDUN".equals(feature) || "enableDUNAlways".equals(feature)) {
1000 cap = NetworkCapabilities.NET_CAPABILITY_DUN;
1001 } else if ("enableHIPRI".equals(feature)) {
1002 cap = NetworkCapabilities.NET_CAPABILITY_INTERNET;
1003 } else if ("enableFOTA".equals(feature)) {
1004 cap = NetworkCapabilities.NET_CAPABILITY_FOTA;
1005 } else if ("enableIMS".equals(feature)) {
1006 cap = NetworkCapabilities.NET_CAPABILITY_IMS;
1007 } else if ("enableCBS".equals(feature)) {
1008 cap = NetworkCapabilities.NET_CAPABILITY_CBS;
1009 } else {
1010 return null;
1011 }
1012 NetworkCapabilities netCap = new NetworkCapabilities();
Robert Greenwalt7569f182014-06-08 16:42:59 -07001013 netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(cap);
Lorenzo Colitti4077acb2014-06-04 12:20:06 +09001014 maybeMarkCapabilitiesRestricted(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001015 return netCap;
1016 } else if (networkType == TYPE_WIFI) {
1017 if ("p2p".equals(feature)) {
1018 NetworkCapabilities netCap = new NetworkCapabilities();
1019 netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
Robert Greenwalt7569f182014-06-08 16:42:59 -07001020 netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
Lorenzo Colitti4077acb2014-06-04 12:20:06 +09001021 maybeMarkCapabilitiesRestricted(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001022 return netCap;
1023 }
1024 }
1025 return null;
1026 }
1027
Robert Greenwalt06314e42014-10-29 14:04:06 -07001028 /**
1029 * Guess what the network request was trying to say so that the resulting
1030 * network is accessible via the legacy (deprecated) API such as
1031 * requestRouteToHost.
1032 * This means we should try to be fairly preceise about transport and
1033 * capability but ignore things such as networkSpecifier.
1034 * If the request has more than one transport or capability it doesn't
1035 * match the old legacy requests (they selected only single transport/capability)
1036 * so this function cannot map the request to a single legacy type and
1037 * the resulting network will not be available to the legacy APIs.
1038 *
1039 * TODO - This should be removed when the legacy APIs are removed.
1040 */
Ye Wenb87875e2014-07-21 14:19:01 -07001041 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1042 if (netCap == null) {
1043 return TYPE_NONE;
1044 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001045
Ye Wenb87875e2014-07-21 14:19:01 -07001046 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1047 return TYPE_NONE;
1048 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001049
1050 String type = null;
1051 int result = TYPE_NONE;
1052
Ye Wenb87875e2014-07-21 14:19:01 -07001053 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
Robert Greenwalt06314e42014-10-29 14:04:06 -07001054 type = "enableCBS";
1055 result = TYPE_MOBILE_CBS;
1056 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1057 type = "enableIMS";
1058 result = TYPE_MOBILE_IMS;
1059 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1060 type = "enableFOTA";
1061 result = TYPE_MOBILE_FOTA;
1062 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1063 type = "enableDUN";
1064 result = TYPE_MOBILE_DUN;
1065 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1066 type = "enableSUPL";
1067 result = TYPE_MOBILE_SUPL;
1068 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1069 type = "enableMMS";
1070 result = TYPE_MOBILE_MMS;
1071 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1072 type = "enableHIPRI";
1073 result = TYPE_MOBILE_HIPRI;
Ye Wenb87875e2014-07-21 14:19:01 -07001074 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001075 if (type != null) {
1076 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1077 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1078 return result;
Ye Wenb87875e2014-07-21 14:19:01 -07001079 }
1080 }
1081 return TYPE_NONE;
1082 }
1083
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001084 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001085 if (netCap == null) return TYPE_NONE;
1086 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1087 return TYPE_MOBILE_CBS;
1088 }
1089 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1090 return TYPE_MOBILE_IMS;
1091 }
1092 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1093 return TYPE_MOBILE_FOTA;
1094 }
1095 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1096 return TYPE_MOBILE_DUN;
1097 }
1098 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1099 return TYPE_MOBILE_SUPL;
1100 }
1101 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1102 return TYPE_MOBILE_MMS;
1103 }
1104 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1105 return TYPE_MOBILE_HIPRI;
1106 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001107 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1108 return TYPE_WIFI_P2P;
1109 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001110 return TYPE_NONE;
1111 }
1112
1113 private static class LegacyRequest {
1114 NetworkCapabilities networkCapabilities;
1115 NetworkRequest networkRequest;
1116 int expireSequenceNumber;
1117 Network currentNetwork;
1118 int delay = -1;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001119
1120 private void clearDnsBinding() {
1121 if (currentNetwork != null) {
1122 currentNetwork = null;
1123 setProcessDefaultNetworkForHostResolution(null);
1124 }
1125 }
1126
Robert Greenwalt6078b502014-06-11 16:05:07 -07001127 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001128 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001129 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001130 currentNetwork = network;
1131 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001132 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001133 }
1134 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001135 public void onLost(Network network) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001136 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001137 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1138 }
1139 };
1140 }
1141
Robert Greenwaltfab501672014-07-23 11:44:01 -07001142 private static HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
Robert Greenwalt562cc542014-05-15 18:07:26 -07001143 new HashMap<NetworkCapabilities, LegacyRequest>();
1144
1145 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1146 synchronized (sLegacyRequests) {
1147 LegacyRequest l = sLegacyRequests.get(netCap);
1148 if (l != null) return l.networkRequest;
1149 }
1150 return null;
1151 }
1152
1153 private void renewRequestLocked(LegacyRequest l) {
1154 l.expireSequenceNumber++;
1155 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1156 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1157 }
1158
1159 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1160 int ourSeqNum = -1;
1161 synchronized (sLegacyRequests) {
1162 LegacyRequest l = sLegacyRequests.get(netCap);
1163 if (l == null) return;
1164 ourSeqNum = l.expireSequenceNumber;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001165 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001166 }
1167 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1168 }
1169
1170 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1171 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001172 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001173 try {
1174 delay = mService.getRestoreDefaultNetworkDelay(type);
1175 } catch (RemoteException e) {}
1176 LegacyRequest l = new LegacyRequest();
1177 l.networkCapabilities = netCap;
1178 l.delay = delay;
1179 l.expireSequenceNumber = 0;
Robert Greenwalt6078b502014-06-11 16:05:07 -07001180 l.networkRequest = sendRequestForNetwork(netCap, l.networkCallback, 0,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001181 REQUEST, type);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001182 if (l.networkRequest == null) return null;
1183 sLegacyRequests.put(netCap, l);
1184 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1185 return l.networkRequest;
1186 }
1187
1188 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1189 if (delay >= 0) {
1190 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
1191 Message msg = sCallbackHandler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1192 sCallbackHandler.sendMessageDelayed(msg, delay);
1193 }
1194 }
1195
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001196 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1197 final LegacyRequest l;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001198 synchronized (sLegacyRequests) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001199 l = sLegacyRequests.remove(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001200 }
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001201 if (l == null) return false;
1202 unregisterNetworkCallback(l.networkCallback);
1203 l.clearDnsBinding();
1204 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001205 }
1206
1207 /**
1208 * Ensure that a network route exists to deliver traffic to the specified
1209 * host via the specified network interface. An attempt to add a route that
1210 * already exists is ignored, but treated as successful.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001211 * <p>This method requires the caller to hold the permission
1212 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001213 * @param networkType the type of the network over which traffic to the specified
1214 * host is to be routed
1215 * @param hostAddress the IP address of the host to which the route is desired
1216 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001217 *
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001218 * @deprecated Deprecated in favor of the
1219 * {@link #requestNetwork(NetworkRequest, NetworkCallback)},
1220 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001221 */
1222 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001223 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001224 }
1225
1226 /**
1227 * Ensure that a network route exists to deliver traffic to the specified
1228 * host via the specified network interface. An attempt to add a route that
1229 * already exists is ignored, but treated as successful.
Jake Hamby8f9b33e2014-01-15 13:08:03 -08001230 * <p>This method requires the caller to hold the permission
1231 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001232 * @param networkType the type of the network over which traffic to the specified
1233 * host is to be routed
1234 * @param hostAddress the IP address of the host to which the route is desired
1235 * @return {@code true} on success, {@code false} on failure
1236 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001237 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09001238 * {@link #bindProcessToNetwork} API.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001239 */
1240 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001241 try {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001242 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 } catch (RemoteException e) {
1244 return false;
1245 }
1246 }
1247
1248 /**
1249 * Returns the value of the setting for background data usage. If false,
1250 * applications should not use the network if the application is not in the
1251 * foreground. Developers should respect this setting, and check the value
1252 * of this before performing any background data operations.
1253 * <p>
1254 * All applications that have background services that use the network
1255 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001256 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001257 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001258 * background data depends on several combined factors, and this method will
1259 * always return {@code true}. Instead, when background data is unavailable,
1260 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001261 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001262 * @return Whether background data usage is allowed.
1263 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001264 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001265 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001266 // assume that background data is allowed; final authority is
1267 // NetworkInfo which may be blocked.
1268 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001269 }
1270
1271 /**
1272 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001273 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001274 * @param allowBackgroundData Whether an application should use data while
1275 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001276 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001277 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1278 * @see #getBackgroundDataSetting()
1279 * @hide
1280 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001281 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001282 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001283 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001284 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001285
1286 /**
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001287 * Return quota status for the current active network, or {@code null} if no
1288 * network is active. Quota status can change rapidly, so these values
1289 * shouldn't be cached.
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001290 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001291 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001292 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1293 *
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001294 * @hide
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001295 */
1296 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1297 try {
1298 return mService.getActiveNetworkQuotaInfo();
1299 } catch (RemoteException e) {
1300 return null;
1301 }
1302 }
1303
1304 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001305 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001306 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001307 */
1308 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001309 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1310 if (b != null) {
1311 try {
1312 ITelephony it = ITelephony.Stub.asInterface(b);
Wink Saville36ffb042014-12-05 11:10:30 -08001313 int subId = SubscriptionManager.getDefaultDataSubId();
1314 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
1315 boolean retVal = it.getDataEnabled(subId);
1316 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1317 + " retVal=" + retVal);
1318 return retVal;
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001319 } catch (RemoteException e) { }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001320 }
Wink Saville36ffb042014-12-05 11:10:30 -08001321 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001322 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001323 }
1324
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001325 /**
Robert Greenwaltb2489872014-09-04 16:44:35 -07001326 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt6078b502014-06-11 16:05:07 -07001327 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001328 */
1329 public interface OnNetworkActiveListener {
1330 /**
1331 * Called on the main thread of the process to report that the current data network
1332 * has become active, and it is now a good time to perform any pending network
1333 * operations. Note that this listener only tells you when the network becomes
1334 * active; if at any other time you want to know whether it is active (and thus okay
1335 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001336 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001337 */
1338 public void onNetworkActive();
1339 }
1340
1341 private INetworkManagementService getNetworkManagementService() {
1342 synchronized (this) {
1343 if (mNMService != null) {
1344 return mNMService;
1345 }
1346 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1347 mNMService = INetworkManagementService.Stub.asInterface(b);
1348 return mNMService;
1349 }
1350 }
1351
1352 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
1353 mNetworkActivityListeners
1354 = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>();
1355
1356 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001357 * Start listening to reports when the system's default data network is active, meaning it is
1358 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1359 * to determine the current state of the system's default network after registering the
1360 * listener.
1361 * <p>
1362 * If the process default network has been set with
Paul Jensen72db88e2015-03-10 10:54:12 -04001363 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt6078b502014-06-11 16:05:07 -07001364 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001365 *
1366 * @param l The listener to be told when the network is active.
1367 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001368 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001369 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1370 @Override
1371 public void onNetworkActive() throws RemoteException {
1372 l.onNetworkActive();
1373 }
1374 };
1375
1376 try {
1377 getNetworkManagementService().registerNetworkActivityListener(rl);
1378 mNetworkActivityListeners.put(l, rl);
1379 } catch (RemoteException e) {
1380 }
1381 }
1382
1383 /**
1384 * Remove network active listener previously registered with
Robert Greenwaltb2489872014-09-04 16:44:35 -07001385 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001386 *
1387 * @param l Previously registered listener.
1388 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001389 public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001390 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
1391 if (rl == null) {
1392 throw new IllegalArgumentException("Listener not registered: " + l);
1393 }
1394 try {
1395 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1396 } catch (RemoteException e) {
1397 }
1398 }
1399
1400 /**
1401 * Return whether the data network is currently active. An active network means that
1402 * it is currently in a high power state for performing data transmission. On some
1403 * types of networks, it may be expensive to move and stay in such a state, so it is
1404 * more power efficient to batch network traffic together when the radio is already in
1405 * this state. This method tells you whether right now is currently a good time to
1406 * initiate network traffic, as the network is already active.
1407 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001408 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001409 try {
1410 return getNetworkManagementService().isNetworkActive();
1411 } catch (RemoteException e) {
1412 }
1413 return false;
1414 }
1415
1416 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001417 * {@hide}
1418 */
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001419 public ConnectivityManager(IConnectivityManager service) {
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001420 mService = checkNotNull(service, "missing IConnectivityManager");
Paul Jensene0bef712014-12-10 15:12:18 -05001421 sInstance = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001422 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001423
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07001424 /** {@hide} */
1425 public static ConnectivityManager from(Context context) {
1426 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1427 }
1428
Robert Greenwaltedb47662014-09-16 17:54:19 -07001429 /** {@hide */
1430 public static final void enforceTetherChangePermission(Context context) {
1431 if (context.getResources().getStringArray(
1432 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
1433 // Have a provisioning app - must only let system apps (which check this app)
1434 // turn on tethering
1435 context.enforceCallingOrSelfPermission(
1436 android.Manifest.permission.CONNECTIVITY_INTERNAL, "ConnectivityService");
1437 } else {
1438 context.enforceCallingOrSelfPermission(
1439 android.Manifest.permission.CHANGE_NETWORK_STATE, "ConnectivityService");
1440 }
1441 }
1442
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001443 /**
Paul Jensene0bef712014-12-10 15:12:18 -05001444 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1445 * situations where a Context pointer is unavailable.
1446 * @hide
1447 */
Paul Jensen72db88e2015-03-10 10:54:12 -04001448 static ConnectivityManager getInstanceOrNull() {
1449 return sInstance;
1450 }
1451
1452 /**
1453 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1454 * situations where a Context pointer is unavailable.
1455 * @hide
1456 */
1457 private static ConnectivityManager getInstance() {
1458 if (getInstanceOrNull() == null) {
Paul Jensene0bef712014-12-10 15:12:18 -05001459 throw new IllegalStateException("No ConnectivityManager yet constructed");
1460 }
Paul Jensen72db88e2015-03-10 10:54:12 -04001461 return getInstanceOrNull();
Paul Jensene0bef712014-12-10 15:12:18 -05001462 }
1463
1464 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001465 * Get the set of tetherable, available interfaces. This list is limited by
1466 * device configuration and current interface existence.
Paul Jensenb2748922015-05-06 11:10:18 -04001467 * <p>This method requires the caller to hold the permission
1468 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001469 *
1470 * @return an array of 0 or more Strings of tetherable interface names.
1471 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001472 * {@hide}
1473 */
1474 public String[] getTetherableIfaces() {
1475 try {
1476 return mService.getTetherableIfaces();
1477 } catch (RemoteException e) {
1478 return new String[0];
1479 }
1480 }
1481
1482 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001483 * Get the set of tethered interfaces.
Paul Jensenb2748922015-05-06 11:10:18 -04001484 * <p>This method requires the caller to hold the permission
1485 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001486 *
1487 * @return an array of 0 or more String of currently tethered interface names.
1488 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001489 * {@hide}
1490 */
1491 public String[] getTetheredIfaces() {
1492 try {
1493 return mService.getTetheredIfaces();
1494 } catch (RemoteException e) {
1495 return new String[0];
1496 }
1497 }
1498
1499 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001500 * Get the set of interface names which attempted to tether but
1501 * failed. Re-attempting to tether may cause them to reset to the Tethered
1502 * state. Alternatively, causing the interface to be destroyed and recreated
1503 * may cause them to reset to the available state.
1504 * {@link ConnectivityManager#getLastTetherError} can be used to get more
1505 * information on the cause of the errors.
Paul Jensenb2748922015-05-06 11:10:18 -04001506 * <p>This method requires the caller to hold the permission
1507 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001508 *
1509 * @return an array of 0 or more String indicating the interface names
1510 * which failed to tether.
1511 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001512 * {@hide}
1513 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001514 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001515 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001516 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001517 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001518 return new String[0];
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001519 }
1520 }
1521
1522 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001523 * Get the set of tethered dhcp ranges.
1524 *
1525 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
1526 * {@hide}
1527 */
1528 public String[] getTetheredDhcpRanges() {
1529 try {
1530 return mService.getTetheredDhcpRanges();
1531 } catch (RemoteException e) {
1532 return new String[0];
1533 }
1534 }
1535
1536 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001537 * Attempt to tether the named interface. This will setup a dhcp server
1538 * on the interface, forward and NAT IP packets and forward DNS requests
1539 * to the best active upstream network interface. Note that if no upstream
1540 * IP network interface is available, dhcp will still run and traffic will be
1541 * allowed between the tethered devices and this device, though upstream net
1542 * access will of course fail until an upstream network interface becomes
1543 * active.
Paul Jensenb2748922015-05-06 11:10:18 -04001544 * <p>This method requires the caller to hold the permission
1545 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001546 *
1547 * @param iface the interface name to tether.
1548 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1549 *
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001550 * {@hide}
1551 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001552 public int tether(String iface) {
1553 try {
1554 return mService.tether(iface);
1555 } catch (RemoteException e) {
1556 return TETHER_ERROR_SERVICE_UNAVAIL;
1557 }
1558 }
1559
1560 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001561 * Stop tethering the named interface.
Paul Jensenb2748922015-05-06 11:10:18 -04001562 * <p>This method requires the caller to hold the permission
1563 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001564 *
1565 * @param iface the interface name to untether.
1566 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1567 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08001568 * {@hide}
1569 */
1570 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001571 try {
1572 return mService.untether(iface);
1573 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001574 return TETHER_ERROR_SERVICE_UNAVAIL;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001575 }
1576 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001577
1578 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001579 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001580 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001581 * due to device configuration.
Paul Jensenb2748922015-05-06 11:10:18 -04001582 * <p>This method requires the caller to hold the permission
1583 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001584 *
1585 * @return a boolean - {@code true} indicating Tethering is supported.
1586 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001587 * {@hide}
1588 */
1589 public boolean isTetheringSupported() {
1590 try {
1591 return mService.isTetheringSupported();
1592 } catch (RemoteException e) {
1593 return false;
1594 }
1595 }
1596
1597 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001598 * Get the list of regular expressions that define any tetherable
1599 * USB network interfaces. If USB tethering is not supported by the
1600 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04001601 * <p>This method requires the caller to hold the permission
1602 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001603 *
1604 * @return an array of 0 or more regular expression Strings defining
1605 * what interfaces are considered tetherable usb interfaces.
1606 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001607 * {@hide}
1608 */
1609 public String[] getTetherableUsbRegexs() {
1610 try {
1611 return mService.getTetherableUsbRegexs();
1612 } catch (RemoteException e) {
1613 return new String[0];
1614 }
1615 }
1616
1617 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001618 * Get the list of regular expressions that define any tetherable
1619 * Wifi network interfaces. If Wifi tethering is not supported by the
1620 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04001621 * <p>This method requires the caller to hold the permission
1622 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001623 *
1624 * @return an array of 0 or more regular expression Strings defining
1625 * what interfaces are considered tetherable wifi interfaces.
1626 *
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001627 * {@hide}
1628 */
1629 public String[] getTetherableWifiRegexs() {
1630 try {
1631 return mService.getTetherableWifiRegexs();
1632 } catch (RemoteException e) {
1633 return new String[0];
1634 }
1635 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08001636
Danica Chang6fdd0c62010-08-11 14:54:43 -07001637 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001638 * Get the list of regular expressions that define any tetherable
1639 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
1640 * device, this list should be empty.
Paul Jensenb2748922015-05-06 11:10:18 -04001641 * <p>This method requires the caller to hold the permission
1642 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001643 *
1644 * @return an array of 0 or more regular expression Strings defining
1645 * what interfaces are considered tetherable bluetooth interfaces.
1646 *
Danica Chang6fdd0c62010-08-11 14:54:43 -07001647 * {@hide}
1648 */
1649 public String[] getTetherableBluetoothRegexs() {
1650 try {
1651 return mService.getTetherableBluetoothRegexs();
1652 } catch (RemoteException e) {
1653 return new String[0];
1654 }
1655 }
1656
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001657 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001658 * Attempt to both alter the mode of USB and Tethering of USB. A
1659 * utility method to deal with some of the complexity of USB - will
1660 * attempt to switch to Rndis and subsequently tether the resulting
1661 * interface on {@code true} or turn off tethering and switch off
1662 * Rndis on {@code false}.
Paul Jensenb2748922015-05-06 11:10:18 -04001663 * <p>This method requires the caller to hold the permission
1664 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001665 *
1666 * @param enable a boolean - {@code true} to enable tethering
1667 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1668 *
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001669 * {@hide}
1670 */
1671 public int setUsbTethering(boolean enable) {
1672 try {
1673 return mService.setUsbTethering(enable);
1674 } catch (RemoteException e) {
1675 return TETHER_ERROR_SERVICE_UNAVAIL;
1676 }
1677 }
1678
Robert Greenwalt5a735062010-03-02 17:25:02 -08001679 /** {@hide} */
1680 public static final int TETHER_ERROR_NO_ERROR = 0;
1681 /** {@hide} */
1682 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
1683 /** {@hide} */
1684 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
1685 /** {@hide} */
1686 public static final int TETHER_ERROR_UNSUPPORTED = 3;
1687 /** {@hide} */
1688 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
1689 /** {@hide} */
1690 public static final int TETHER_ERROR_MASTER_ERROR = 5;
1691 /** {@hide} */
1692 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
1693 /** {@hide} */
1694 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
1695 /** {@hide} */
1696 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
1697 /** {@hide} */
1698 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
1699 /** {@hide} */
1700 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
1701
1702 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001703 * Get a more detailed error code after a Tethering or Untethering
1704 * request asynchronously failed.
Paul Jensenb2748922015-05-06 11:10:18 -04001705 * <p>This method requires the caller to hold the permission
1706 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001707 *
1708 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08001709 * @return error The error code of the last error tethering or untethering the named
1710 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001711 *
Robert Greenwalt5a735062010-03-02 17:25:02 -08001712 * {@hide}
1713 */
1714 public int getLastTetherError(String iface) {
1715 try {
1716 return mService.getLastTetherError(iface);
1717 } catch (RemoteException e) {
1718 return TETHER_ERROR_SERVICE_UNAVAIL;
1719 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001720 }
1721
1722 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001723 * Report network connectivity status. This is currently used only
1724 * to alter status bar UI.
Paul Jensenb2748922015-05-06 11:10:18 -04001725 * <p>This method requires the caller to hold the permission
1726 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001727 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001728 * @param networkType The type of network you want to report on
1729 * @param percentage The quality of the connection 0 is bad, 100 is good
1730 * {@hide}
1731 */
1732 public void reportInetCondition(int networkType, int percentage) {
1733 try {
1734 mService.reportInetCondition(networkType, percentage);
1735 } catch (RemoteException e) {
1736 }
1737 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001738
1739 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001740 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07001741 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001742 * the framework to re-evaluate network connectivity and/or switch to another
1743 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001744 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001745 * @param network The {@link Network} the application was attempting to use
1746 * or {@code null} to indicate the current default network.
Paul Jensenbfd17b72015-04-07 12:43:13 -04001747 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
1748 * working and non-working connectivity.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001749 */
1750 public void reportBadNetwork(Network network) {
1751 try {
Paul Jensenbfd17b72015-04-07 12:43:13 -04001752 // One of these will be ignored because it matches system's current state.
1753 // The other will trigger the necessary reevaluation.
1754 mService.reportNetworkConnectivity(network, true);
1755 mService.reportNetworkConnectivity(network, false);
1756 } catch (RemoteException e) {
1757 }
1758 }
1759
1760 /**
1761 * Report to the framework whether a network has working connectivity.
1762 * This provides a hint to the system that a particular network is providing
1763 * working connectivity or not. In response the framework may re-evaluate
1764 * the network's connectivity and might take further action thereafter.
1765 *
1766 * @param network The {@link Network} the application was attempting to use
1767 * or {@code null} to indicate the current default network.
1768 * @param hasConnectivity {@code true} if the application was able to successfully access the
1769 * Internet using {@code network} or {@code false} if not.
1770 */
1771 public void reportNetworkConnectivity(Network network, boolean hasConnectivity) {
1772 try {
1773 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt9258c642014-03-26 16:47:06 -07001774 } catch (RemoteException e) {
1775 }
1776 }
1777
1778 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001779 * Set a network-independent global http proxy. This is not normally what you want
1780 * for typical HTTP proxies - they are general network dependent. However if you're
1781 * doing something unusual like general internal filtering this may be useful. On
1782 * a private network where the proxy is not accessible, you may break HTTP using this.
Paul Jensen0d719ca2015-02-13 14:18:39 -05001783 * <p>This method requires the caller to hold the permission
Jason Monkfaf3fd52014-05-07 18:41:13 -04001784 * android.Manifest.permission#CONNECTIVITY_INTERNAL.
Paul Jensenb2748922015-05-06 11:10:18 -04001785 *
1786 * @param p A {@link ProxyInfo} object defining the new global
1787 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001788 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07001789 */
Jason Monk207900c2014-04-25 15:00:09 -04001790 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001791 try {
1792 mService.setGlobalProxy(p);
1793 } catch (RemoteException e) {
1794 }
1795 }
1796
1797 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001798 * Retrieve any network-independent global HTTP proxy.
1799 *
Jason Monk207900c2014-04-25 15:00:09 -04001800 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001801 * if no global HTTP proxy is set.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001802 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07001803 */
Jason Monk207900c2014-04-25 15:00:09 -04001804 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001805 try {
1806 return mService.getGlobalProxy();
1807 } catch (RemoteException e) {
1808 return null;
1809 }
1810 }
1811
1812 /**
Paul Jensencee9b512015-05-06 07:32:40 -04001813 * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a
1814 * network-specific HTTP proxy. If {@code network} is null, the
1815 * network-specific proxy returned is the proxy of the default active
1816 * network.
1817 *
1818 * @return {@link ProxyInfo} for the current global HTTP proxy, or if no
1819 * global HTTP proxy is set, {@code ProxyInfo} for {@code network},
1820 * or when {@code network} is {@code null},
1821 * the {@code ProxyInfo} for the default active network. Returns
1822 * {@code null} when no proxy applies or the caller doesn't have
1823 * permission to use {@code network}.
1824 * @hide
1825 */
1826 public ProxyInfo getProxyForNetwork(Network network) {
1827 try {
1828 return mService.getProxyForNetwork(network);
1829 } catch (RemoteException e) {
1830 return null;
1831 }
1832 }
1833
1834 /**
Paul Jensene0bef712014-12-10 15:12:18 -05001835 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
1836 * otherwise if this process is bound to a {@link Network} using
Paul Jensen72db88e2015-03-10 10:54:12 -04001837 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensene0bef712014-12-10 15:12:18 -05001838 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001839 *
Jason Monk207900c2014-04-25 15:00:09 -04001840 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001841 * HTTP proxy is active.
Robert Greenwalt434203a2010-10-11 16:00:27 -07001842 */
Paul Jensene0bef712014-12-10 15:12:18 -05001843 public ProxyInfo getDefaultProxy() {
Paul Jensencee9b512015-05-06 07:32:40 -04001844 return getProxyForNetwork(getBoundNetworkForProcess());
Robert Greenwalt434203a2010-10-11 16:00:27 -07001845 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001846
1847 /**
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07001848 * Returns true if the hardware supports the given network type
1849 * else it returns false. This doesn't indicate we have coverage
1850 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001851 * hardware supports it. For example a GSM phone without a SIM
1852 * should still return {@code true} for mobile data, but a wifi only
1853 * tablet would return {@code false}.
Paul Jensenb2748922015-05-06 11:10:18 -04001854 * <p>This method requires the caller to hold the permission
1855 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001856 *
1857 * @param networkType The network type we'd like to check
1858 * @return {@code true} if supported, else {@code false}
1859 *
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07001860 * @hide
1861 */
1862 public boolean isNetworkSupported(int networkType) {
1863 try {
1864 return mService.isNetworkSupported(networkType);
1865 } catch (RemoteException e) {}
1866 return false;
1867 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07001868
1869 /**
1870 * Returns if the currently active data network is metered. A network is
1871 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001872 * that connection due to monetary costs, data limitations or
1873 * battery/performance issues. You should check this before doing large
1874 * data transfers, and warn the user or delay the operation until another
1875 * network is available.
Paul Jensenb2748922015-05-06 11:10:18 -04001876 * <p>This method requires the caller to hold the permission
1877 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001878 *
1879 * @return {@code true} if large transfers should be avoided, otherwise
1880 * {@code false}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07001881 */
1882 public boolean isActiveNetworkMetered() {
1883 try {
1884 return mService.isActiveNetworkMetered();
1885 } catch (RemoteException e) {
1886 return false;
1887 }
1888 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07001889
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001890 /**
1891 * If the LockdownVpn mechanism is enabled, updates the vpn
1892 * with a reload of its profile.
1893 *
1894 * @return a boolean with {@code} indicating success
1895 *
1896 * <p>This method can only be called by the system UID
1897 * {@hide}
1898 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07001899 public boolean updateLockdownVpn() {
1900 try {
1901 return mService.updateLockdownVpn();
1902 } catch (RemoteException e) {
1903 return false;
1904 }
1905 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07001906
1907 /**
Wink Saville948282b2013-08-29 08:55:16 -07001908 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07001909 *
Wink Savilleab9321d2013-06-29 21:10:57 -07001910 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07001911 *
1912 * @return time out that will be used, maybe less that suggestedTimeOutMs
1913 * -1 if an error.
1914 *
1915 * {@hide}
1916 */
Wink Saville948282b2013-08-29 08:55:16 -07001917 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07001918 int timeOutMs = -1;
1919 try {
Wink Saville948282b2013-08-29 08:55:16 -07001920 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07001921 } catch (RemoteException e) {
1922 }
1923 return timeOutMs;
1924 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07001925
1926 /**
Wink Saville42d4f082013-07-20 20:31:59 -07001927 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07001928 * {@hide}
1929 */
1930 public String getMobileProvisioningUrl() {
1931 try {
1932 return mService.getMobileProvisioningUrl();
1933 } catch (RemoteException e) {
1934 }
1935 return null;
1936 }
Wink Saville42d4f082013-07-20 20:31:59 -07001937
1938 /**
Wink Saville948282b2013-08-29 08:55:16 -07001939 * Set sign in error notification to visible or in visible
1940 *
1941 * @param visible
1942 * @param networkType
1943 *
1944 * {@hide}
Paul Jensen3541e9f2015-03-18 12:23:02 -04001945 * @deprecated Doesn't properly deal with multiple connected networks of the same type.
Wink Saville948282b2013-08-29 08:55:16 -07001946 */
1947 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04001948 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07001949 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04001950 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07001951 } catch (RemoteException e) {
1952 }
1953 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07001954
1955 /**
1956 * Set the value for enabling/disabling airplane mode
Paul Jensenb2748922015-05-06 11:10:18 -04001957 * <p>This method requires the caller to hold the permission
1958 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07001959 *
1960 * @param enable whether to enable airplane mode or not
1961 *
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07001962 * @hide
1963 */
1964 public void setAirplaneMode(boolean enable) {
1965 try {
1966 mService.setAirplaneMode(enable);
1967 } catch (RemoteException e) {
1968 }
1969 }
Robert Greenwalte049c232014-04-11 15:53:27 -07001970
1971 /** {@hide} */
Robert Greenwalta67be032014-05-16 15:49:14 -07001972 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07001973 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07001974 mService.registerNetworkFactory(messenger, name);
1975 } catch (RemoteException e) { }
1976 }
1977
1978 /** {@hide} */
1979 public void unregisterNetworkFactory(Messenger messenger) {
1980 try {
1981 mService.unregisterNetworkFactory(messenger);
Robert Greenwalte049c232014-04-11 15:53:27 -07001982 } catch (RemoteException e) { }
1983 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07001984
Paul Jensen31a94f42015-02-13 14:18:39 -05001985 /**
1986 * @hide
1987 * Register a NetworkAgent with ConnectivityService.
1988 * @return NetID corresponding to NetworkAgent.
1989 */
1990 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07001991 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07001992 try {
Paul Jensen31a94f42015-02-13 14:18:39 -05001993 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
1994 } catch (RemoteException e) {
1995 return NETID_UNSET;
1996 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07001997 }
1998
Robert Greenwalt9258c642014-03-26 16:47:06 -07001999 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002000 * Base class for NetworkRequest callbacks. Used for notifications about network
2001 * changes. Should be extended by applications wanting notifications.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002002 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002003 public static class NetworkCallback {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002004 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002005 * Called when the framework connects to a new network to evaluate whether it satisfies this
2006 * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable}
2007 * callback. There is no guarantee that this new network will satisfy any requests, or that
2008 * the network will stay connected for longer than the time necessary to evaluate it.
2009 * <p>
2010 * Most applications <b>should not</b> act on this callback, and should instead use
2011 * {@link #onAvailable}. This callback is intended for use by applications that can assist
2012 * the framework in properly evaluating the network &mdash; for example, an application that
2013 * can automatically log in to a captive portal without user intervention.
2014 *
2015 * @param network The {@link Network} of the network that is being evaluated.
Lorenzo Colitti66276122015-06-11 14:27:17 +09002016 *
2017 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002018 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002019 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002020
2021 /**
Lorenzo Colitti07086932015-04-24 12:23:24 +09002022 * Called when the framework connects and has declared a new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002023 * This callback may be called more than once if the {@link Network} that is
2024 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002025 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002026 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002027 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002028 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002029
2030 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002031 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002032 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002033 * for graceful handover. This may not be called if we have a hard loss
2034 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002035 * {@link NetworkCallback#onLost} call or a
2036 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002037 * on whether we lose or regain it.
2038 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002039 * @param network The {@link Network} that is about to be disconnected.
2040 * @param maxMsToLive The time in ms the framework will attempt to keep the
2041 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002042 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002043 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002044 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002045
2046 /**
2047 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002048 * graceful failure ends.
2049 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002050 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002051 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002052 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002053
2054 /**
2055 * Called if no network is found in the given timeout time. If no timeout is given,
2056 * this will not be called.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002057 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002058 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002059 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002060
2061 /**
2062 * Called when the network the framework connected to for this request
2063 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002064 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002065 * @param network The {@link Network} whose capabilities have changed.
Lorenzo Colittie285b432015-04-23 15:32:42 +09002066 * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002067 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002068 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002069 NetworkCapabilities networkCapabilities) {}
2070
2071 /**
2072 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002073 * changes {@link LinkProperties}.
2074 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002075 * @param network The {@link Network} whose link properties have changed.
2076 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002077 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002078 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002079
Robert Greenwalt8d482522015-06-24 13:23:42 -07002080 /**
2081 * Called when the network the framework connected to for this request
2082 * goes into {@link NetworkInfo.DetailedState.SUSPENDED}.
2083 * This generally means that while the TCP connections are still live,
2084 * temporarily network data fails to transfer. Specifically this is used
2085 * on cellular networks to mask temporary outages when driving through
2086 * a tunnel, etc.
2087 * @hide
2088 */
2089 public void onNetworkSuspended(Network network) {}
2090
2091 /**
2092 * Called when the network the framework connected to for this request
2093 * returns from a {@link NetworkInfo.DetailedState.SUSPENDED} state.
2094 * This should always be preceeded by a matching {@code onNetworkSuspended}
2095 * call.
2096 * @hide
2097 */
2098 public void onNetworkResumed(Network network) {}
2099
Robert Greenwalt6078b502014-06-11 16:05:07 -07002100 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002101 }
2102
Robert Greenwalt9258c642014-03-26 16:47:06 -07002103 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002104 /** @hide */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002105 public static final int CALLBACK_PRECHECK = BASE + 1;
2106 /** @hide */
2107 public static final int CALLBACK_AVAILABLE = BASE + 2;
2108 /** @hide arg1 = TTL */
2109 public static final int CALLBACK_LOSING = BASE + 3;
2110 /** @hide */
2111 public static final int CALLBACK_LOST = BASE + 4;
2112 /** @hide */
2113 public static final int CALLBACK_UNAVAIL = BASE + 5;
2114 /** @hide */
2115 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2116 /** @hide */
2117 public static final int CALLBACK_IP_CHANGED = BASE + 7;
2118 /** @hide */
2119 public static final int CALLBACK_RELEASED = BASE + 8;
2120 /** @hide */
2121 public static final int CALLBACK_EXIT = BASE + 9;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002122 /** @hide obj = NetworkCapabilities, arg1 = seq number */
Robert Greenwalt8d482522015-06-24 13:23:42 -07002123 private static final int EXPIRE_LEGACY_REQUEST = BASE + 10;
2124 /** @hide */
2125 public static final int CALLBACK_SUSPENDED = BASE + 11;
2126 /** @hide */
2127 public static final int CALLBACK_RESUMED = BASE + 12;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002128
Robert Greenwalt562cc542014-05-15 18:07:26 -07002129 private class CallbackHandler extends Handler {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002130 private final HashMap<NetworkRequest, NetworkCallback>mCallbackMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002131 private final AtomicInteger mRefCount;
2132 private static final String TAG = "ConnectivityManager.CallbackHandler";
2133 private final ConnectivityManager mCm;
2134
Robert Greenwalt6078b502014-06-11 16:05:07 -07002135 CallbackHandler(Looper looper, HashMap<NetworkRequest, NetworkCallback>callbackMap,
Robert Greenwalt9258c642014-03-26 16:47:06 -07002136 AtomicInteger refCount, ConnectivityManager cm) {
2137 super(looper);
2138 mCallbackMap = callbackMap;
2139 mRefCount = refCount;
2140 mCm = cm;
2141 }
2142
2143 @Override
2144 public void handleMessage(Message message) {
2145 Log.d(TAG, "CM callback handler got msg " + message.what);
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002146 NetworkRequest request = (NetworkRequest) getObject(message, NetworkRequest.class);
2147 Network network = (Network) getObject(message, Network.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002148 switch (message.what) {
2149 case CALLBACK_PRECHECK: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002150 NetworkCallback callback = getCallback(request, "PRECHECK");
2151 if (callback != null) {
2152 callback.onPreCheck(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002153 }
2154 break;
2155 }
2156 case CALLBACK_AVAILABLE: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002157 NetworkCallback callback = getCallback(request, "AVAILABLE");
2158 if (callback != null) {
2159 callback.onAvailable(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002160 }
2161 break;
2162 }
2163 case CALLBACK_LOSING: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002164 NetworkCallback callback = getCallback(request, "LOSING");
2165 if (callback != null) {
2166 callback.onLosing(network, message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002167 }
2168 break;
2169 }
2170 case CALLBACK_LOST: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002171 NetworkCallback callback = getCallback(request, "LOST");
2172 if (callback != null) {
2173 callback.onLost(network);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002174 }
2175 break;
2176 }
2177 case CALLBACK_UNAVAIL: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002178 NetworkCallback callback = getCallback(request, "UNAVAIL");
2179 if (callback != null) {
2180 callback.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002181 }
2182 break;
2183 }
2184 case CALLBACK_CAP_CHANGED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002185 NetworkCallback callback = getCallback(request, "CAP_CHANGED");
2186 if (callback != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002187 NetworkCapabilities cap = (NetworkCapabilities)getObject(message,
2188 NetworkCapabilities.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002189
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002190 callback.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002191 }
2192 break;
2193 }
2194 case CALLBACK_IP_CHANGED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002195 NetworkCallback callback = getCallback(request, "IP_CHANGED");
2196 if (callback != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002197 LinkProperties lp = (LinkProperties)getObject(message,
2198 LinkProperties.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002199
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002200 callback.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002201 }
2202 break;
2203 }
Robert Greenwalt8d482522015-06-24 13:23:42 -07002204 case CALLBACK_SUSPENDED: {
2205 NetworkCallback callback = getCallback(request, "SUSPENDED");
2206 if (callback != null) {
2207 callback.onNetworkSuspended(network);
2208 }
2209 break;
2210 }
2211 case CALLBACK_RESUMED: {
2212 NetworkCallback callback = getCallback(request, "RESUMED");
2213 if (callback != null) {
2214 callback.onNetworkResumed(network);
2215 }
2216 break;
2217 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002218 case CALLBACK_RELEASED: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002219 NetworkCallback callback = null;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002220 synchronized(mCallbackMap) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002221 callback = mCallbackMap.remove(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002222 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002223 if (callback != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002224 synchronized(mRefCount) {
2225 if (mRefCount.decrementAndGet() == 0) {
2226 getLooper().quit();
2227 }
2228 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002229 } else {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002230 Log.e(TAG, "callback not found for RELEASED message");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002231 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002232 break;
2233 }
2234 case CALLBACK_EXIT: {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002235 Log.d(TAG, "Listener quitting");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002236 getLooper().quit();
2237 break;
2238 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07002239 case EXPIRE_LEGACY_REQUEST: {
2240 expireRequest((NetworkCapabilities)message.obj, message.arg1);
2241 break;
2242 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002243 }
2244 }
2245
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002246 private Object getObject(Message msg, Class c) {
2247 return msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07002248 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002249
2250 private NetworkCallback getCallback(NetworkRequest req, String name) {
2251 NetworkCallback callback;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002252 synchronized(mCallbackMap) {
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002253 callback = mCallbackMap.get(req);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002254 }
Lorenzo Colittidb95a602015-04-24 15:21:21 +09002255 if (callback == null) {
2256 Log.e(TAG, "callback not found for " + name + " message");
2257 }
2258 return callback;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002259 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002260 }
2261
Robert Greenwalt6078b502014-06-11 16:05:07 -07002262 private void incCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002263 synchronized(sCallbackRefCount) {
2264 if (sCallbackRefCount.incrementAndGet() == 1) {
2265 // TODO - switch this over to a ManagerThread or expire it when done
2266 HandlerThread callbackThread = new HandlerThread("ConnectivityManager");
2267 callbackThread.start();
2268 sCallbackHandler = new CallbackHandler(callbackThread.getLooper(),
Robert Greenwalt6078b502014-06-11 16:05:07 -07002269 sNetworkCallback, sCallbackRefCount, this);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002270 }
2271 }
2272 }
2273
Robert Greenwalt6078b502014-06-11 16:05:07 -07002274 private void decCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002275 synchronized(sCallbackRefCount) {
2276 if (sCallbackRefCount.decrementAndGet() == 0) {
2277 sCallbackHandler.obtainMessage(CALLBACK_EXIT).sendToTarget();
2278 sCallbackHandler = null;
2279 }
2280 }
2281 }
2282
Robert Greenwalt6078b502014-06-11 16:05:07 -07002283 static final HashMap<NetworkRequest, NetworkCallback> sNetworkCallback =
2284 new HashMap<NetworkRequest, NetworkCallback>();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002285 static final AtomicInteger sCallbackRefCount = new AtomicInteger(0);
2286 static CallbackHandler sCallbackHandler = null;
2287
2288 private final static int LISTEN = 1;
2289 private final static int REQUEST = 2;
2290
Robert Greenwalt562cc542014-05-15 18:07:26 -07002291 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002292 NetworkCallback networkCallback, int timeoutSec, int action,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07002293 int legacyType) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002294 if (networkCallback == null) {
2295 throw new IllegalArgumentException("null NetworkCallback");
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002296 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002297 if (need == null) throw new IllegalArgumentException("null NetworkCapabilities");
2298 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002299 incCallbackHandlerRefCount();
Paul Jensen7221cc32014-06-27 11:05:32 -04002300 synchronized(sNetworkCallback) {
2301 if (action == LISTEN) {
2302 networkCallback.networkRequest = mService.listenForNetwork(need,
2303 new Messenger(sCallbackHandler), new Binder());
2304 } else {
2305 networkCallback.networkRequest = mService.requestNetwork(need,
2306 new Messenger(sCallbackHandler), timeoutSec, new Binder(), legacyType);
2307 }
2308 if (networkCallback.networkRequest != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002309 sNetworkCallback.put(networkCallback.networkRequest, networkCallback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002310 }
2311 }
2312 } catch (RemoteException e) {}
Robert Greenwalt6078b502014-06-11 16:05:07 -07002313 if (networkCallback.networkRequest == null) decCallbackHandlerRefCount();
2314 return networkCallback.networkRequest;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002315 }
2316
2317 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002318 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002319 *
2320 * This {@link NetworkRequest} will live until released via
Robert Greenwalt6078b502014-06-11 16:05:07 -07002321 * {@link #unregisterNetworkCallback} or the calling application exits.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002322 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07002323 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002324 * can be used to direct traffic to the network.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002325 * <p>It is presently unsupported to request a network with mutable
2326 * {@link NetworkCapabilities} such as
2327 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2328 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
2329 * as these {@code NetworkCapabilities} represent states that a particular
2330 * network may never attain, and whether a network will attain these states
2331 * is unknown prior to bringing up the network so the framework does not
2332 * know how to go about satisfing a request with these capabilities.
Paul Jensenb2748922015-05-06 11:10:18 -04002333 * <p>This method requires the caller to hold the permission
2334 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002335 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002336 * @param request {@link NetworkRequest} describing this request.
2337 * @param networkCallback The {@link NetworkCallback} to be utilized for this
2338 * request. Note the callback must not be shared - they
2339 * uniquely specify this request.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002340 * @throws IllegalArgumentException if {@code request} specifies any mutable
2341 * {@code NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002342 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002343 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
2344 sendRequestForNetwork(request.networkCapabilities, networkCallback, 0,
Ye Wenb87875e2014-07-21 14:19:01 -07002345 REQUEST, inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002346 }
2347
2348 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002349 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited
Robert Greenwalt9258c642014-03-26 16:47:06 -07002350 * by a timeout.
2351 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002352 * This function behaves identically to the non-timedout version, but if a suitable
Robert Greenwalt6078b502014-06-11 16:05:07 -07002353 * network is not found within the given time (in milliseconds) the
2354 * {@link NetworkCallback#unavailable} callback is called. The request must
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002355 * still be released normally by calling {@link releaseNetworkRequest}.
Paul Jensenb2748922015-05-06 11:10:18 -04002356 * <p>This method requires the caller to hold the permission
2357 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002358 * @param request {@link NetworkRequest} describing this request.
2359 * @param networkCallback The callbacks to be utilized for this request. Note
2360 * the callbacks must not be shared - they uniquely specify
2361 * this request.
2362 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
2363 * before {@link NetworkCallback#unavailable} is called.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002364 * @hide
2365 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002366 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
2367 int timeoutMs) {
2368 sendRequestForNetwork(request.networkCapabilities, networkCallback, timeoutMs,
Ye Wenb87875e2014-07-21 14:19:01 -07002369 REQUEST, inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002370 }
2371
2372 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002373 * The maximum number of milliseconds the framework will look for a suitable network
Robert Greenwalt9258c642014-03-26 16:47:06 -07002374 * during a timeout-equiped call to {@link requestNetwork}.
2375 * {@hide}
2376 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002377 public final static int MAX_NETWORK_REQUEST_TIMEOUT_MS = 100 * 60 * 1000;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002378
2379 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002380 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002381 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002382 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfcde58f2015-02-11 16:51:13 -08002383 * <p>
Paul Jensen72db88e2015-03-10 10:54:12 -04002384 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
2385 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002386 */
Erik Kline90e93072014-11-19 12:12:24 +09002387 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002388
2389 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002390 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002391 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002392 * {@link android.content.Intent#getParcelableExtra(String)}.
2393 */
Erik Kline90e93072014-11-19 12:12:24 +09002394 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002395
2396
2397 /**
Lorenzo Colittie285b432015-04-23 15:32:42 +09002398 * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002399 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002400 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07002401 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002402 * the request may outlive the calling application and get called back when a suitable
2403 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002404 * <p>
2405 * The operation is an Intent broadcast that goes to a broadcast receiver that
2406 * you registered with {@link Context#registerReceiver} or through the
2407 * &lt;receiver&gt; tag in an AndroidManifest.xml file
2408 * <p>
2409 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09002410 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
2411 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07002412 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002413 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07002414 * Intent to reserve the network or it will be released shortly after the Intent
2415 * is processed.
2416 * <p>
Paul Jensen694f2b82015-06-17 14:15:39 -04002417 * If there is already a request for this Intent registered (with the equality of
Robert Greenwalt9258c642014-03-26 16:47:06 -07002418 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002419 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002420 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002421 * The request may be released normally by calling
2422 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002423 * <p>It is presently unsupported to request a network with either
2424 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2425 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}
2426 * as these {@code NetworkCapabilities} represent states that a particular
2427 * network may never attain, and whether a network will attain these states
2428 * is unknown prior to bringing up the network so the framework does not
2429 * know how to go about satisfing a request with these capabilities.
Paul Jensenb2748922015-05-06 11:10:18 -04002430 * <p>This method requires the caller to hold the permission
2431 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002432 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002433 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07002434 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002435 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Paul Jensenbb2e0e92015-06-16 15:11:58 -04002436 * @throws IllegalArgumentException if {@code request} contains either
2437 * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or
2438 * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002439 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002440 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002441 checkPendingIntent(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002442 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002443 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002444 } catch (RemoteException e) {}
Robert Greenwalt9258c642014-03-26 16:47:06 -07002445 }
2446
2447 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002448 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
2449 * <p>
2450 * This method has the same behavior as {@link #unregisterNetworkCallback} with respect to
2451 * releasing network resources and disconnecting.
2452 *
2453 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
2454 * PendingIntent passed to
2455 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
2456 * corresponding NetworkRequest you'd like to remove. Cannot be null.
2457 */
2458 public void releaseNetworkRequest(PendingIntent operation) {
2459 checkPendingIntent(operation);
2460 try {
2461 mService.releasePendingNetworkRequest(operation);
2462 } catch (RemoteException e) {}
2463 }
2464
2465 private void checkPendingIntent(PendingIntent intent) {
2466 if (intent == null) {
2467 throw new IllegalArgumentException("PendingIntent cannot be null.");
2468 }
2469 }
2470
2471 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07002472 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07002473 * {@link NetworkRequest}. The callbacks will continue to be called until
2474 * either the application exits or {@link #unregisterNetworkCallback} is called
Paul Jensenb2748922015-05-06 11:10:18 -04002475 * <p>This method requires the caller to hold the permission
2476 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002477 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002478 * @param request {@link NetworkRequest} describing this request.
2479 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
2480 * networks change state.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002481 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002482 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
2483 sendRequestForNetwork(request.networkCapabilities, networkCallback, 0, LISTEN, TYPE_NONE);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002484 }
2485
2486 /**
Paul Jensen694f2b82015-06-17 14:15:39 -04002487 * Registers a PendingIntent to be sent when a network is available which satisfies the given
2488 * {@link NetworkRequest}.
2489 *
2490 * This function behaves identically to the version that takes a NetworkCallback, but instead
2491 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
2492 * the request may outlive the calling application and get called back when a suitable
2493 * network is found.
2494 * <p>
2495 * The operation is an Intent broadcast that goes to a broadcast receiver that
2496 * you registered with {@link Context#registerReceiver} or through the
2497 * &lt;receiver&gt; tag in an AndroidManifest.xml file
2498 * <p>
2499 * The operation Intent is delivered with two extras, a {@link Network} typed
2500 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
2501 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
2502 * the original requests parameters.
2503 * <p>
2504 * If there is already a request for this Intent registered (with the equality of
2505 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
2506 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
2507 * <p>
2508 * The request may be released normally by calling
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04002509 * {@link #unregisterNetworkCallback(android.app.PendingIntent)}.
Paul Jensen694f2b82015-06-17 14:15:39 -04002510 * <p>This method requires the caller to hold the permission
2511 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
2512 * @param request {@link NetworkRequest} describing this request.
2513 * @param operation Action to perform when the network is available (corresponds
2514 * to the {@link NetworkCallback#onAvailable} call. Typically
2515 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
2516 */
2517 public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) {
2518 checkPendingIntent(operation);
2519 try {
2520 mService.pendingListenForNetwork(request.networkCapabilities, operation);
2521 } catch (RemoteException e) {}
2522 }
2523
2524 /**
fengludb571472015-04-21 17:12:05 -07002525 * Requests bandwidth update for a given {@link Network} and returns whether the update request
2526 * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying
2527 * network connection for updated bandwidth information. The caller will be notified via
2528 * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this
2529 * method assumes that the caller has previously called {@link #registerNetworkCallback} to
2530 * listen for network changes.
fenglub15e72b2015-03-20 11:29:56 -07002531 *
fengluae519192015-04-27 14:28:04 -07002532 * @param network {@link Network} specifying which network you're interested.
fengludb571472015-04-21 17:12:05 -07002533 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
fenglub15e72b2015-03-20 11:29:56 -07002534 */
fengludb571472015-04-21 17:12:05 -07002535 public boolean requestBandwidthUpdate(Network network) {
fenglub15e72b2015-03-20 11:29:56 -07002536 try {
fengludb571472015-04-21 17:12:05 -07002537 return mService.requestBandwidthUpdate(network);
fenglub15e72b2015-03-20 11:29:56 -07002538 } catch (RemoteException e) {
2539 return false;
2540 }
2541 }
2542
2543 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002544 * Unregisters callbacks about and possibly releases networks originating from
Lorenzo Colitti2ea89e52015-04-24 17:03:31 +09002545 * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and {@link #registerNetworkCallback}
2546 * calls. If the given {@code NetworkCallback} had previously been used with
2547 * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request
2548 * will be disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002549 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002550 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002551 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002552 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
2553 if (networkCallback == null || networkCallback.networkRequest == null ||
2554 networkCallback.networkRequest.requestId == REQUEST_ID_UNSET) {
2555 throw new IllegalArgumentException("Invalid NetworkCallback");
2556 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002557 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002558 mService.releaseNetworkRequest(networkCallback.networkRequest);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002559 } catch (RemoteException e) {}
2560 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002561
2562 /**
Paul Jensenf2c1cfe2015-06-30 14:29:18 -04002563 * Unregisters a callback previously registered via
2564 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
2565 *
2566 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
2567 * PendingIntent passed to
2568 * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}.
2569 * Cannot be null.
2570 */
2571 public void unregisterNetworkCallback(PendingIntent operation) {
2572 releaseNetworkRequest(operation);
2573 }
2574
2575 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09002576 * Informs the system whether it should switch to {@code network} regardless of whether it is
2577 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
2578 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
2579 * the system default network regardless of any other network that's currently connected. If
2580 * {@code always} is true, then the choice is remembered, so that the next time the user
2581 * connects to this network, the system will switch to it.
2582 *
2583 * <p>This method requires the caller to hold the permission
2584 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
2585 *
2586 * @param network The network to accept.
2587 * @param accept Whether to accept the network even if unvalidated.
2588 * @param always Whether to remember this choice in the future.
2589 *
2590 * @hide
2591 */
2592 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
2593 try {
2594 mService.setAcceptUnvalidated(network, accept, always);
2595 } catch (RemoteException e) {}
2596 }
2597
2598 /**
Stuart Scott984dc852015-03-30 13:17:11 -07002599 * Resets all connectivity manager settings back to factory defaults.
2600 * @hide
2601 */
2602 public void factoryReset() {
Stuart Scott984dc852015-03-30 13:17:11 -07002603 try {
Stuart Scottf1fb3972015-04-02 18:00:02 -07002604 mService.factoryReset();
Stuart Scott984dc852015-03-30 13:17:11 -07002605 } catch (RemoteException e) {
Stuart Scott984dc852015-03-30 13:17:11 -07002606 }
2607 }
2608
2609 /**
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002610 * Binds the current process to {@code network}. All Sockets created in the future
2611 * (and not explicitly bound via a bound SocketFactory from
2612 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
2613 * {@code network}. All host name resolutions will be limited to {@code network} as well.
2614 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
2615 * work and all host name resolutions will fail. This is by design so an application doesn't
2616 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
2617 * To clear binding pass {@code null} for {@code network}. Using individually bound
2618 * Sockets created by Network.getSocketFactory().createSocket() and
2619 * performing network-specific host name resolutions via
2620 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen72db88e2015-03-10 10:54:12 -04002621 * {@code bindProcessToNetwork}.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002622 *
2623 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
2624 * the current binding.
2625 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2626 */
Paul Jensen72db88e2015-03-10 10:54:12 -04002627 public boolean bindProcessToNetwork(Network network) {
2628 // Forcing callers to call thru non-static function ensures ConnectivityManager
2629 // instantiated.
2630 return setProcessDefaultNetwork(network);
2631 }
2632
2633 /**
2634 * Binds the current process to {@code network}. All Sockets created in the future
2635 * (and not explicitly bound via a bound SocketFactory from
2636 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
2637 * {@code network}. All host name resolutions will be limited to {@code network} as well.
2638 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
2639 * work and all host name resolutions will fail. This is by design so an application doesn't
2640 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
2641 * To clear binding pass {@code null} for {@code network}. Using individually bound
2642 * Sockets created by Network.getSocketFactory().createSocket() and
2643 * performing network-specific host name resolutions via
2644 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
2645 * {@code setProcessDefaultNetwork}.
2646 *
2647 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
2648 * the current binding.
2649 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2650 * @deprecated This function can throw {@link IllegalStateException}. Use
2651 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
2652 * is a direct replacement.
2653 */
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002654 public static boolean setProcessDefaultNetwork(Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04002655 int netId = (network == null) ? NETID_UNSET : network.netId;
Paul Jensen72db88e2015-03-10 10:54:12 -04002656 if (netId == NetworkUtils.getBoundNetworkForProcess()) {
Paul Jensenc91b5342014-08-27 12:38:45 -04002657 return true;
2658 }
2659 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05002660 // Set HTTP proxy system properties to match network.
2661 // TODO: Deprecate this static method and replace it with a non-static version.
Lorenzo Colittiec4c5552015-04-22 11:52:48 +09002662 try {
2663 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
2664 } catch (SecurityException e) {
2665 // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy.
2666 Log.e(TAG, "Can't set proxy properties", e);
2667 }
Paul Jensenc91b5342014-08-27 12:38:45 -04002668 // Must flush DNS cache as new network may have different DNS resolutions.
2669 InetAddress.clearDnsCache();
2670 // Must flush socket pool as idle sockets will be bound to previous network and may
2671 // cause subsequent fetches to be performed on old network.
2672 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
2673 return true;
2674 } else {
2675 return false;
2676 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002677 }
2678
2679 /**
2680 * Returns the {@link Network} currently bound to this process via
Paul Jensen72db88e2015-03-10 10:54:12 -04002681 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002682 *
2683 * @return {@code Network} to which this process is bound, or {@code null}.
2684 */
Paul Jensen72db88e2015-03-10 10:54:12 -04002685 public Network getBoundNetworkForProcess() {
2686 // Forcing callers to call thru non-static function ensures ConnectivityManager
2687 // instantiated.
2688 return getProcessDefaultNetwork();
2689 }
2690
2691 /**
2692 * Returns the {@link Network} currently bound to this process via
2693 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
2694 *
2695 * @return {@code Network} to which this process is bound, or {@code null}.
2696 * @deprecated Using this function can lead to other functions throwing
2697 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
2698 * {@code getBoundNetworkForProcess} is a direct replacement.
2699 */
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002700 public static Network getProcessDefaultNetwork() {
Paul Jensen72db88e2015-03-10 10:54:12 -04002701 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04002702 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002703 return new Network(netId);
2704 }
2705
2706 /**
2707 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen72db88e2015-03-10 10:54:12 -04002708 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002709 *
2710 * @param network The {@link Network} to bind host resolutions from the current process to, or
2711 * {@code null} to clear the current binding.
2712 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2713 * @hide
2714 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
2715 */
2716 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04002717 return NetworkUtils.bindProcessToNetworkForHostResolution(
2718 network == null ? NETID_UNSET : network.netId);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002719 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002720}