blob: 8de545e5e23615270d1dc30356e5765b06ace1f6 [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;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070039import android.telephony.TelephonyManager;
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
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051/**
52 * Class that answers queries about the state of network connectivity. It also
53 * notifies applications when network connectivity changes. Get an instance
54 * of this class by calling
55 * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}.
56 * <p>
57 * The primary responsibilities of this class are to:
58 * <ol>
59 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
60 * <li>Send broadcast intents when network connectivity changes</li>
61 * <li>Attempt to "fail over" to another network when connectivity to a network
62 * is lost</li>
63 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
64 * state of the available networks</li>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070065 * <li>Provide an API that allows applications to request and select networks for their data
66 * traffic</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 * </ol>
68 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070069public class ConnectivityManager {
70 private static final String TAG = "ConnectivityManager";
Robert Greenwalt257ee5f2014-06-20 10:58:45 -070071 private static final boolean LEGACY_DBG = true; // STOPSHIP
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070074 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 * been established or lost. The NetworkInfo for the affected network is
76 * sent as an extra; it should be consulted to see what kind of
77 * connectivity event occurred.
78 * <p/>
79 * If this is a connection that was the result of failing over from a
80 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
81 * set to true.
82 * <p/>
83 * For a loss of connectivity, if the connectivity manager is attempting
84 * to connect (or has already connected) to another network, the
85 * NetworkInfo for the new network is also passed as an extra. This lets
86 * any receivers of the broadcast know that they should not necessarily
87 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -080088 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 * the failover attempt succeeded (and so there is still overall data
90 * connectivity), or that the failover attempt failed, meaning that all
91 * connectivity has been lost.
92 * <p/>
93 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
94 * is set to {@code true} if there are no connected networks at all.
95 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -080096 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -070098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 /**
Jeff Sharkey961e3042011-08-29 16:02:57 -0700100 * Identical to {@link #CONNECTIVITY_ACTION} broadcast, but sent without any
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800101 * applicable {@link Settings.Global#CONNECTIVITY_CHANGE_DELAY}.
Jeff Sharkey961e3042011-08-29 16:02:57 -0700102 *
103 * @hide
104 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800105 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey961e3042011-08-29 16:02:57 -0700106 public static final String CONNECTIVITY_ACTION_IMMEDIATE =
107 "android.net.conn.CONNECTIVITY_CHANGE_IMMEDIATE";
108
109 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 * The lookup key for a {@link NetworkInfo} object. Retrieve with
111 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700112 *
113 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
114 * should always obtain network information through
115 * {@link #getActiveNetworkInfo()} or
116 * {@link #getAllNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700117 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700119 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700123 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
124 * Can be used with {@link #getNetworkInfo(int)} to get {@link NetworkInfo}
125 * state based on the calling application.
126 *
127 * @see android.content.Intent#getIntExtra(String, int)
128 */
129 public static final String EXTRA_NETWORK_TYPE = "networkType";
130
131 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 * The lookup key for a boolean that indicates whether a connect event
133 * is for a network to which the connectivity manager was failing over
134 * following a disconnect on another network.
135 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
136 */
137 public static final String EXTRA_IS_FAILOVER = "isFailover";
138 /**
139 * The lookup key for a {@link NetworkInfo} object. This is supplied when
140 * there is another network that it may be possible to connect to. Retrieve with
141 * {@link android.content.Intent#getParcelableExtra(String)}.
142 */
143 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
144 /**
145 * The lookup key for a boolean that indicates whether there is a
146 * complete lack of connectivity, i.e., no network is available.
147 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
148 */
149 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
150 /**
151 * The lookup key for a string that indicates why an attempt to connect
152 * to a network failed. The string has no particular structure. It is
153 * intended to be used in notifications presented to users. Retrieve
154 * it with {@link android.content.Intent#getStringExtra(String)}.
155 */
156 public static final String EXTRA_REASON = "reason";
157 /**
158 * The lookup key for a string that provides optionally supplied
159 * extra information about the network state. The information
160 * may be passed up from the lower networking layers, and its
161 * meaning may be specific to a particular network type. Retrieve
162 * it with {@link android.content.Intent#getStringExtra(String)}.
163 */
164 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700165 /**
166 * The lookup key for an int that provides information about
167 * our connection to the internet at large. 0 indicates no connection,
168 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700169 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700170 * {@hide}
171 */
172 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173
174 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700175 * Broadcast action to indicate the change of data activity status
176 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800177 * The network becomes active when data transmission is started, or
178 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700179 * {@hide}
180 */
181 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
182 public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
183 /**
184 * The lookup key for an enum that indicates the network device type on which this data activity
185 * change happens.
186 * {@hide}
187 */
188 public static final String EXTRA_DEVICE_TYPE = "deviceType";
189 /**
190 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
191 * it is actively sending or receiving data and {@code false} means it is idle.
192 * {@hide}
193 */
194 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700195 /**
196 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
197 * {@hide}
198 */
199 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700200
201 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 * Broadcast Action: The setting for background data usage has changed
203 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
204 * <p>
205 * If an application uses the network in the background, it should listen
206 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700207 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800208 * <p>
209 *
210 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
211 * of background data depends on several combined factors, and
212 * this broadcast is no longer sent. Instead, when background
213 * data is unavailable, {@link #getActiveNetworkInfo()} will now
214 * appear disconnected. During first boot after a platform
215 * upgrade, this broadcast will be sent once if
216 * {@link #getBackgroundDataSetting()} was {@code false} before
217 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 */
219 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800220 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
222 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
223
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700224 /**
225 * Broadcast Action: The network connection may not be good
226 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
227 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
228 * the network and it's condition.
229 * @hide
230 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800231 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700232 public static final String INET_CONDITION_ACTION =
233 "android.net.conn.INET_CONDITION_ACTION";
234
Robert Greenwalt42acef32009-08-12 16:08:25 -0700235 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800236 * Broadcast Action: A tetherable connection has come or gone.
237 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
238 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER} and
239 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
240 * the current state of tethering. Each include a list of
241 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800242 * @hide
243 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800244 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800245 public static final String ACTION_TETHER_STATE_CHANGED =
246 "android.net.conn.TETHER_STATE_CHANGED";
247
248 /**
249 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800250 * gives a String[] listing all the interfaces configured for
251 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800252 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800253 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800254
255 /**
256 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800257 * gives a String[] listing all the interfaces currently tethered
258 * (ie, has dhcp support and packets potentially forwarded/NATed)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800259 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800260 public static final String EXTRA_ACTIVE_TETHER = "activeArray";
261
262 /**
263 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800264 * gives a String[] listing all the interfaces we tried to tether and
265 * failed. Use {@link #getLastTetherError} to find the error code
266 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800267 */
268 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800269
270 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800271 * Broadcast Action: The captive portal tracker has finished its test.
272 * Sent only while running Setup Wizard, in lieu of showing a user
273 * notification.
274 * @hide
275 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800276 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800277 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
278 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
279 /**
280 * The lookup key for a boolean that indicates whether a captive portal was detected.
281 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
282 * @hide
283 */
284 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
285
286 /**
Paul Jensene76e4322014-05-19 10:43:47 -0400287 * Broadcast Action: A connection has been established to a new network
288 * but a captive portal has been detected preventing internet connectivity.
289 * This broadcast is sent out prior to providing the user with a
290 * notification allowing them to sign into the network, as such it should
291 * only be used by apps that can automatically and silently (without user
292 * interaction) log into specific captive portals. It should not be used
293 * by apps that prompt the user to sign in, as the user has not yet
294 * indicated they want to proceed with signing in.
295 * The new network is not the default network so it can only be accessed via
296 * the {@link Network} extra {@link #EXTRA_NETWORK}.
297 * This is an ordered broadcast and so it is perfectly acceptable for
298 * multiple receivers to in turn consider whether they are best suited to
299 * address the captive portal.
300 * A receiver should abort the broadcast if they are sure they are the
301 * appropriate handler of the captive portal. If the broadcast is aborted,
302 * the result code must be set to one of the following:
303 * <ul>
304 * <li>{@link #CAPTIVE_PORTAL_SIGNED_IN} The receiver has signed into the
305 * captive portal. After being verified to provide internet
306 * connectivity, this network will be made the default (assuming
307 * it is preferred over all other active networks).
308 * </li>
309 * <li>{@link #CAPTIVE_PORTAL_DISCONNECT} The receiver is familiar with
310 * this captive portal and knows sign-in is impossible or the user
311 * has indicated they do not want to pursue sign-in. No other apps
312 * will be given the option of signing in to the network.
313 * </li>
314 * </ul>
315 */
316 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
317 public static final String ACTION_CAPTIVE_PORTAL_DETECTED =
318 "android.net.conn.CAPTIVE_PORTAL_DETECTED";
319
320 /**
321 * Broadcast Action: A connection has been established to a new network,
322 * a captive portal has been detected preventing internet connectivity,
323 * the user was notified, and elected to sign into the captive portal.
324 * It may be used by apps that prompt the user to sign in.
325 * The new network is not the default network so it can only be accessed via
326 * the {@link Network} extra {@link #EXTRA_NETWORK}.
327 * This is an ordered broadcast and so it is perfectly acceptable for
328 * multiple receivers to in turn consider whether they are best suited to
329 * address the captive portal.
330 * A receiver should abort the broadcast if they are sure they are the
331 * appropriate handler of the captive portal. If the broadcast is aborted,
332 * the result code must be set to one of the following:
333 * <ul>
334 * <li>{@link #CAPTIVE_PORTAL_SIGNED_IN} The receiver has signed into the
335 * captive portal. After being verified to provide internet
336 * connectivity, this network will be made the default (assuming
337 * it is preferred over all other active networks).
338 * </li>
339 * <li>{@link #CAPTIVE_PORTAL_DISCONNECT} The user has indicated they do
340 * not want to pursue sign-in. No other apps will be given the
341 * option of signing in to the network.
342 * </li>
343 * </ul>
344 */
345 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
346 public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN =
347 "android.net.conn.CAPTIVE_PORTAL_SIGN_IN";
348
349 /**
350 * The lookup key for a {@link Network} object passed along with a
351 * {@link #ACTION_CAPTIVE_PORTAL_DETECTED} or
352 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. This network is not the
Glenn Kasten30d51952014-07-07 07:12:29 -0700353 * default network and must be accessed using this {@link Network} object.
Paul Jensene76e4322014-05-19 10:43:47 -0400354 * Retrieve with {@link android.content.Intent#getParcelableExtra(String)}.
355 */
356 public static final String EXTRA_NETWORK = "network";
357
358 /**
359 * Specified as a result code of a {@link #ACTION_CAPTIVE_PORTAL_DETECTED} or
360 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} receiver to indicate
361 * the receiver has signed into the
362 * captive portal. After being verified to provide internet
363 * connectivity, this network will be made the default (assuming
364 * it is preferred over all other active networks).
365 */
366 public static final int CAPTIVE_PORTAL_SIGNED_IN = 1;
367
368 /**
369 * Specified as a result code of a {@link #ACTION_CAPTIVE_PORTAL_DETECTED} or
370 * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} receiver to indicate
371 * the receiver is familiar with
372 * this captive portal and knows sign-in is impossible or the user
373 * has indicated they do not want to pursue sign-in. No other apps will
374 * be given the option of signing in to the network.
375 */
376 public static final int CAPTIVE_PORTAL_DISCONNECT = 2;
377
378 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800379 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700380 * @hide
381 */
382 public static final int TYPE_NONE = -1;
383
384 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800385 * The Mobile data connection. When active, all data traffic
386 * will use this network type's interface by default
387 * (it has a default route)
Robert Greenwalt42acef32009-08-12 16:08:25 -0700388 */
389 public static final int TYPE_MOBILE = 0;
390 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800391 * The WIFI data connection. When active, all data traffic
392 * will use this network type's interface by default
393 * (it has a default route).
Robert Greenwalt42acef32009-08-12 16:08:25 -0700394 */
395 public static final int TYPE_WIFI = 1;
396 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800397 * An MMS-specific Mobile data connection. This network type may use the
398 * same network interface as {@link #TYPE_MOBILE} or it may use a different
399 * one. This is used by applications needing to talk to the carrier's
400 * Multimedia Messaging Service servers.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700401 */
402 public static final int TYPE_MOBILE_MMS = 2;
403 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800404 * A SUPL-specific Mobile data connection. This network type may use the
405 * same network interface as {@link #TYPE_MOBILE} or it may use a different
406 * one. This is used by applications needing to talk to the carrier's
407 * Secure User Plane Location servers for help locating the device.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700408 */
409 public static final int TYPE_MOBILE_SUPL = 3;
410 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800411 * A DUN-specific Mobile data connection. This network type may use the
412 * same network interface as {@link #TYPE_MOBILE} or it may use a different
413 * one. This is sometimes by the system when setting up an upstream connection
414 * for tethering so that the carrier is aware of DUN traffic.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700415 */
416 public static final int TYPE_MOBILE_DUN = 4;
417 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800418 * A High Priority Mobile data connection. This network type uses the
419 * same network interface as {@link #TYPE_MOBILE} but the routing setup
420 * is different. Only requesting processes will have access to the
421 * Mobile DNS servers and only IP's explicitly requested via {@link #requestRouteToHost}
422 * will route over this interface if no default route exists.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700423 */
424 public static final int TYPE_MOBILE_HIPRI = 5;
jsh8214deb2010-03-11 15:04:43 -0800425 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800426 * The WiMAX data connection. When active, all data traffic
427 * will use this network type's interface by default
428 * (it has a default route).
jsh8214deb2010-03-11 15:04:43 -0800429 */
430 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800431
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800432 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800433 * The Bluetooth data connection. When active, all data traffic
434 * will use this network type's interface by default
435 * (it has a default route).
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800436 */
437 public static final int TYPE_BLUETOOTH = 7;
438
Robert Greenwalt60810842011-04-22 15:28:18 -0700439 /**
440 * Dummy data connection. This should not be used on shipping devices.
441 */
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800442 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800443
Robert Greenwalt60810842011-04-22 15:28:18 -0700444 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800445 * The Ethernet data connection. When active, all data traffic
446 * will use this network type's interface by default
447 * (it has a default route).
Robert Greenwalt60810842011-04-22 15:28:18 -0700448 */
Robert Greenwalte12aec92011-01-28 14:48:37 -0800449 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700450
Wink Saville9d7d6282011-03-12 14:52:01 -0800451 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800452 * Over the air Administration.
Wink Saville9d7d6282011-03-12 14:52:01 -0800453 * {@hide}
454 */
455 public static final int TYPE_MOBILE_FOTA = 10;
456
457 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800458 * IP Multimedia Subsystem.
Wink Saville9d7d6282011-03-12 14:52:01 -0800459 * {@hide}
460 */
461 public static final int TYPE_MOBILE_IMS = 11;
462
463 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800464 * Carrier Branded Services.
Wink Saville9d7d6282011-03-12 14:52:01 -0800465 * {@hide}
466 */
467 public static final int TYPE_MOBILE_CBS = 12;
468
repo syncaea743a2011-07-29 23:55:49 -0700469 /**
470 * A Wi-Fi p2p connection. Only requesting processes will have access to
471 * the peers connected.
472 * {@hide}
473 */
474 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800475
Wink Saville5e56bc52013-07-29 15:00:57 -0700476 /**
477 * The network to use for initially attaching to the network
478 * {@hide}
479 */
480 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700481
Ram3e0e3bc2014-06-26 11:03:44 -0700482/**
483 * Emergency PDN connection for emergency calls
484 * {@hide}
485 */
486 public static final int TYPE_MOBILE_EMERGENCY = 15;
487
Hui Lu1c5624a2014-01-15 11:05:36 -0500488 /**
489 * The network that uses proxy to achieve connectivity.
490 * {@hide}
491 */
492 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700493
Robert Greenwalt8283f882014-07-07 17:09:01 -0700494 /**
495 * A virtual network using one or more native bearers.
496 * It may or may not be providing security services.
497 */
498 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500499
500 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700501 public static final int MAX_RADIO_TYPE = TYPE_VPN;
502
503 /** {@hide} */
504 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800505
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800506 /**
507 * If you want to set the default network preference,you can directly
508 * change the networkAttributes array in framework's config.xml.
509 *
510 * @deprecated Since we support so many more networks now, the single
511 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800512 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800513 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800514 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800515 * from an App.
516 */
517 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800518 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
519
Jeff Sharkey625239a2012-09-26 22:03:49 -0700520 /**
521 * Default value for {@link Settings.Global#CONNECTIVITY_CHANGE_DELAY} in
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800522 * milliseconds. This was introduced because IPv6 routes seem to take a
523 * moment to settle - trying network activity before the routes are adjusted
524 * can lead to packets using the wrong interface or having the wrong IP address.
525 * This delay is a bit crude, but in the future hopefully we will have kernel
526 * notifications letting us know when it's safe to use the new network.
Jeff Sharkey625239a2012-09-26 22:03:49 -0700527 *
528 * @hide
529 */
530 public static final int CONNECTIVITY_CHANGE_DELAY_DEFAULT = 3000;
531
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700532 /**
533 * @hide
534 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700535 public final static int REQUEST_ID_UNSET = 0;
536
Paul Jensen5d59e782014-07-11 12:28:19 -0400537 /**
538 * A NetID indicating no Network is selected.
539 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
540 * @hide
541 */
542 public static final int NETID_UNSET = 0;
543
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700544 private final IConnectivityManager mService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800545
Chad Brubakerf81daa92014-02-14 13:22:34 -0800546 private final String mPackageName;
547
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800548 private INetworkManagementService mNMService;
549
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800550 /**
551 * Tests if a given integer represents a valid network type.
552 * @param networkType the type to be tested
553 * @return a boolean. {@code true} if the type is valid, else {@code false}
554 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700555 public static boolean isNetworkTypeValid(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700556 return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800557 }
558
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800559 /**
560 * Returns a non-localized string representing a given network type.
561 * ONLY used for debugging output.
562 * @param type the type needing naming
563 * @return a String for the given type, or a string version of the type ("87")
564 * if no name is known.
565 * {@hide}
566 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700567 public static String getNetworkTypeName(int type) {
568 switch (type) {
569 case TYPE_MOBILE:
570 return "MOBILE";
571 case TYPE_WIFI:
572 return "WIFI";
573 case TYPE_MOBILE_MMS:
574 return "MOBILE_MMS";
575 case TYPE_MOBILE_SUPL:
576 return "MOBILE_SUPL";
577 case TYPE_MOBILE_DUN:
578 return "MOBILE_DUN";
579 case TYPE_MOBILE_HIPRI:
580 return "MOBILE_HIPRI";
581 case TYPE_WIMAX:
582 return "WIMAX";
583 case TYPE_BLUETOOTH:
584 return "BLUETOOTH";
585 case TYPE_DUMMY:
586 return "DUMMY";
587 case TYPE_ETHERNET:
588 return "ETHERNET";
589 case TYPE_MOBILE_FOTA:
590 return "MOBILE_FOTA";
591 case TYPE_MOBILE_IMS:
592 return "MOBILE_IMS";
593 case TYPE_MOBILE_CBS:
594 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700595 case TYPE_WIFI_P2P:
596 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700597 case TYPE_MOBILE_IA:
598 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700599 case TYPE_MOBILE_EMERGENCY:
600 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500601 case TYPE_PROXY:
602 return "PROXY";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700603 default:
604 return Integer.toString(type);
605 }
606 }
607
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800608 /**
609 * Checks if a given type uses the cellular data connection.
610 * This should be replaced in the future by a network property.
611 * @param networkType the type to check
612 * @return a boolean - {@code true} if uses cellular network, else {@code false}
613 * {@hide}
614 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700615 public static boolean isNetworkTypeMobile(int networkType) {
616 switch (networkType) {
617 case TYPE_MOBILE:
618 case TYPE_MOBILE_MMS:
619 case TYPE_MOBILE_SUPL:
620 case TYPE_MOBILE_DUN:
621 case TYPE_MOBILE_HIPRI:
622 case TYPE_MOBILE_FOTA:
623 case TYPE_MOBILE_IMS:
624 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700625 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700626 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700627 return true;
628 default:
629 return false;
630 }
631 }
632
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800633 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700634 * Checks if the given network type is backed by a Wi-Fi radio.
635 *
636 * @hide
637 */
638 public static boolean isNetworkTypeWifi(int networkType) {
639 switch (networkType) {
640 case TYPE_WIFI:
641 case TYPE_WIFI_P2P:
642 return true;
643 default:
644 return false;
645 }
646 }
647
648 /**
Chad Brubakerf336d722013-07-15 16:34:04 -0700649 * Checks if the given network type should be exempt from VPN routing rules
650 *
651 * @hide
652 */
653 public static boolean isNetworkTypeExempt(int networkType) {
654 switch (networkType) {
655 case TYPE_MOBILE_MMS:
656 case TYPE_MOBILE_SUPL:
657 case TYPE_MOBILE_HIPRI:
Wink Saville5e56bc52013-07-29 15:00:57 -0700658 case TYPE_MOBILE_IA:
Chad Brubakerf336d722013-07-15 16:34:04 -0700659 return true;
660 default:
661 return false;
662 }
663 }
664
665 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800666 * Specifies the preferred network type. When the device has more
667 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800668 *
669 * @param preference the network type to prefer over all others. It is
670 * unspecified what happens to the old preferred network in the
671 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700672 * @deprecated Functionality has been removed as it no longer makes sense,
673 * with many more than two networks - we'd need an array to express
674 * preference. Instead we use dynamic network properties of
675 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800676 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800677 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800678 }
679
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800680 /**
681 * Retrieves the current preferred network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800682 *
683 * @return an integer representing the preferred network type
684 *
685 * <p>This method requires the caller to hold the permission
686 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700687 * @deprecated Functionality has been removed as it no longer makes sense,
688 * with many more than two networks - we'd need an array to express
689 * preference. Instead we use dynamic network properties of
690 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800691 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800692 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700693 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800694 }
695
Scott Main671644c2011-10-06 19:02:28 -0700696 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800697 * Returns details about the currently active default data network. When
698 * connected, this network is the default route for outgoing connections.
699 * You should always check {@link NetworkInfo#isConnected()} before initiating
700 * network traffic. This may return {@code null} when there is no default
701 * network.
702 *
703 * @return a {@link NetworkInfo} object for the current default network
704 * or {@code null} if no network default network is currently active
705 *
706 * <p>This method requires the call to hold the permission
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700707 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700708 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800709 public NetworkInfo getActiveNetworkInfo() {
710 try {
711 return mService.getActiveNetworkInfo();
712 } catch (RemoteException e) {
713 return null;
714 }
715 }
716
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800717 /**
718 * Returns details about the currently active default data network
719 * for a given uid. This is for internal use only to avoid spying
720 * other apps.
721 *
722 * @return a {@link NetworkInfo} object for the current default network
723 * for the given uid or {@code null} if no default network is
724 * available for the specified uid.
725 *
726 * <p>This method requires the caller to hold the permission
727 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
728 * {@hide}
729 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700730 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
731 try {
732 return mService.getActiveNetworkInfoForUid(uid);
733 } catch (RemoteException e) {
734 return null;
735 }
736 }
737
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800738 /**
739 * Returns connection status information about a particular
740 * network type.
741 *
742 * @param networkType integer specifying which networkType in
743 * which you're interested.
744 * @return a {@link NetworkInfo} object for the requested
745 * network type or {@code null} if the type is not
746 * supported by the device.
747 *
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700748 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800749 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
750 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800751 public NetworkInfo getNetworkInfo(int networkType) {
752 try {
753 return mService.getNetworkInfo(networkType);
754 } catch (RemoteException e) {
755 return null;
756 }
757 }
758
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800759 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700760 * Returns connection status information about a particular
761 * Network.
762 *
763 * @param network {@link Network} specifying which network
764 * in which you're interested.
765 * @return a {@link NetworkInfo} object for the requested
766 * network or {@code null} if the {@code Network}
767 * is not valid.
768 *
769 * <p>This method requires the caller to hold the permission
770 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
771 */
772 public NetworkInfo getNetworkInfo(Network network) {
773 try {
774 return mService.getNetworkInfoForNetwork(network);
775 } catch (RemoteException e) {
776 return null;
777 }
778 }
779
780 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800781 * Returns connection status information about all network
782 * types supported by the device.
783 *
784 * @return an array of {@link NetworkInfo} objects. Check each
785 * {@link NetworkInfo#getType} for which type each applies.
786 *
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700787 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800788 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
789 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800790 public NetworkInfo[] getAllNetworkInfo() {
791 try {
792 return mService.getAllNetworkInfo();
793 } catch (RemoteException e) {
794 return null;
795 }
796 }
797
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800798 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700799 * Returns an array of all {@link Network} currently tracked by the
800 * framework.
801 *
802 * @return an array of {@link Network} objects.
803 *
804 * <p>This method requires the caller to hold the permission
805 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
806 */
807 public Network[] getAllNetworks() {
808 try {
809 return mService.getAllNetworks();
810 } catch (RemoteException e) {
811 return null;
812 }
813 }
814
815 /**
Wink Saville948282b2013-08-29 08:55:16 -0700816 * Returns details about the Provisioning or currently active default data network. When
817 * connected, this network is the default route for outgoing connections.
818 * You should always check {@link NetworkInfo#isConnected()} before initiating
819 * network traffic. This may return {@code null} when there is no default
820 * network.
821 *
822 * @return a {@link NetworkInfo} object for the current default network
823 * or {@code null} if no network default network is currently active
824 *
825 * <p>This method requires the call to hold the permission
826 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
827 *
828 * {@hide}
829 */
830 public NetworkInfo getProvisioningOrActiveNetworkInfo() {
831 try {
832 return mService.getProvisioningOrActiveNetworkInfo();
833 } catch (RemoteException e) {
834 return null;
835 }
836 }
837
838 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800839 * Returns the IP information for the current default network.
840 *
841 * @return a {@link LinkProperties} object describing the IP info
842 * for the current default network, or {@code null} if there
843 * is no current default network.
844 *
845 * <p>This method requires the call to hold the permission
846 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
847 * {@hide}
848 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700849 public LinkProperties getActiveLinkProperties() {
850 try {
851 return mService.getActiveLinkProperties();
852 } catch (RemoteException e) {
853 return null;
854 }
855 }
856
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800857 /**
858 * Returns the IP information for a given network type.
859 *
860 * @param networkType the network type of interest.
861 * @return a {@link LinkProperties} object describing the IP info
862 * for the given networkType, or {@code null} if there is
863 * no current default network.
864 *
865 * <p>This method requires the call to hold the permission
866 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
867 * {@hide}
868 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700869 public LinkProperties getLinkProperties(int networkType) {
870 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -0700871 return mService.getLinkPropertiesForType(networkType);
872 } catch (RemoteException e) {
873 return null;
874 }
875 }
876
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700877 /**
878 * Get the {@link LinkProperties} for the given {@link Network}. This
879 * will return {@code null} if the network is unknown.
880 *
881 * @param network The {@link Network} object identifying the network in question.
882 * @return The {@link LinkProperties} for the network, or {@code null}.
883 **/
Robert Greenwalt9258c642014-03-26 16:47:06 -0700884 public LinkProperties getLinkProperties(Network network) {
885 try {
886 return mService.getLinkProperties(network);
887 } catch (RemoteException e) {
888 return null;
889 }
890 }
891
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700892 /**
893 * Get the {@link NetworkCapabilities} for the given {@link Network}. This
894 * will return {@code null} if the network is unknown.
895 *
896 * @param network The {@link Network} object identifying the network in question.
897 * @return The {@link NetworkCapabilities} for the network, or {@code null}.
898 */
Robert Greenwalt9258c642014-03-26 16:47:06 -0700899 public NetworkCapabilities getNetworkCapabilities(Network network) {
900 try {
901 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700902 } catch (RemoteException e) {
903 return null;
904 }
905 }
906
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800907 /**
908 * Tells each network type to set its radio power state as directed.
909 *
910 * @param turnOn a boolean, {@code true} to turn the radios on,
911 * {@code false} to turn them off.
912 * @return a boolean, {@code true} indicating success. All network types
913 * will be tried, even if some fail.
914 *
915 * <p>This method requires the call to hold the permission
916 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
917 * {@hide}
918 */
Robert Greenwalt7b816022014-04-18 15:25:25 -0700919// TODO - check for any callers and remove
920// public boolean setRadios(boolean turnOn) {
921// try {
922// return mService.setRadios(turnOn);
923// } catch (RemoteException e) {
924// return false;
925// }
926// }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800927
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800928 /**
929 * Tells a given networkType to set its radio power state as directed.
930 *
931 * @param networkType the int networkType of interest.
932 * @param turnOn a boolean, {@code true} to turn the radio on,
933 * {@code} false to turn it off.
934 * @return a boolean, {@code true} indicating success.
935 *
936 * <p>This method requires the call to hold the permission
937 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
938 * {@hide}
939 */
Robert Greenwalt7b816022014-04-18 15:25:25 -0700940// TODO - check for any callers and remove
941// public boolean setRadio(int networkType, boolean turnOn) {
942// try {
943// return mService.setRadio(networkType, turnOn);
944// } catch (RemoteException e) {
945// return false;
946// }
947// }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800948
949 /**
950 * Tells the underlying networking system that the caller wants to
951 * begin using the named feature. The interpretation of {@code feature}
952 * is completely up to each networking implementation.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700953 * <p>This method requires the caller to hold the permission
954 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800955 * @param networkType specifies which network the request pertains to
956 * @param feature the name of the feature to be used
957 * @return an integer value representing the outcome of the request.
958 * The interpretation of this value is specific to each networking
959 * implementation+feature combination, except that the value {@code -1}
960 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700961 *
962 * @deprecated Deprecated in favor of the cleaner {@link #requestNetwork} api.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800963 */
964 public int startUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700965 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
966 if (netCap == null) {
967 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
968 feature);
969 return PhoneConstants.APN_REQUEST_FAILED;
970 }
971
972 NetworkRequest request = null;
973 synchronized (sLegacyRequests) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -0700974 if (LEGACY_DBG) {
975 Log.d(TAG, "Looking for legacyRequest for netCap with hash: " + netCap + " (" +
976 netCap.hashCode() + ")");
977 Log.d(TAG, "sLegacyRequests has:");
978 for (NetworkCapabilities nc : sLegacyRequests.keySet()) {
979 Log.d(TAG, " " + nc + " (" + nc.hashCode() + ")");
980 }
981 }
Robert Greenwalt562cc542014-05-15 18:07:26 -0700982 LegacyRequest l = sLegacyRequests.get(netCap);
983 if (l != null) {
984 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
985 renewRequestLocked(l);
986 if (l.currentNetwork != null) {
987 return PhoneConstants.APN_ALREADY_ACTIVE;
988 } else {
989 return PhoneConstants.APN_REQUEST_STARTED;
990 }
991 }
992
993 request = requestNetworkForFeatureLocked(netCap);
994 }
995 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -0700996 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -0700997 return PhoneConstants.APN_REQUEST_STARTED;
998 } else {
999 Log.d(TAG, " request Failed");
1000 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001001 }
1002 }
1003
1004 /**
1005 * Tells the underlying networking system that the caller is finished
1006 * using the named feature. The interpretation of {@code feature}
1007 * is completely up to each networking implementation.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001008 * <p>This method requires the caller to hold the permission
1009 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001010 * @param networkType specifies which network the request pertains to
1011 * @param feature the name of the feature that is no longer needed
1012 * @return an integer value representing the outcome of the request.
1013 * The interpretation of this value is specific to each networking
1014 * implementation+feature combination, except that the value {@code -1}
1015 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001016 *
1017 * @deprecated Deprecated in favor of the cleaner {@link #requestNetwork} api.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001018 */
1019 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001020 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
1021 if (netCap == null) {
1022 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
1023 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001024 return -1;
1025 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001026
Robert Greenwalt6078b502014-06-11 16:05:07 -07001027 NetworkCallback networkCallback = removeRequestForFeature(netCap);
1028 if (networkCallback != null) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001029 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt6078b502014-06-11 16:05:07 -07001030 unregisterNetworkCallback(networkCallback);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001031 }
1032 return 1;
1033 }
1034
Lorenzo Colitti4077acb2014-06-04 12:20:06 +09001035 /**
1036 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability from the given
Lorenzo Colittiea1984f2014-06-04 19:59:21 +09001037 * NetworkCapabilities object if all the capabilities it provides are
1038 * typically provided by restricted networks.
1039 *
1040 * TODO: consider:
1041 * - Moving to NetworkCapabilities
1042 * - Renaming it to guessRestrictedCapability and make it set the
1043 * restricted capability bit in addition to clearing it.
Lorenzo Colitti4077acb2014-06-04 12:20:06 +09001044 * @hide
1045 */
1046 public static void maybeMarkCapabilitiesRestricted(NetworkCapabilities nc) {
Robert Greenwalt7569f182014-06-08 16:42:59 -07001047 for (int capability : nc.getCapabilities()) {
1048 switch (capability) {
Lorenzo Colitti4077acb2014-06-04 12:20:06 +09001049 case NetworkCapabilities.NET_CAPABILITY_CBS:
1050 case NetworkCapabilities.NET_CAPABILITY_DUN:
Lorenzo Colittiea1984f2014-06-04 19:59:21 +09001051 case NetworkCapabilities.NET_CAPABILITY_EIMS:
Lorenzo Colitti4077acb2014-06-04 12:20:06 +09001052 case NetworkCapabilities.NET_CAPABILITY_FOTA:
1053 case NetworkCapabilities.NET_CAPABILITY_IA:
1054 case NetworkCapabilities.NET_CAPABILITY_IMS:
Lorenzo Colittiea1984f2014-06-04 19:59:21 +09001055 case NetworkCapabilities.NET_CAPABILITY_RCS:
1056 case NetworkCapabilities.NET_CAPABILITY_XCAP:
Robert Greenwalt9ac3dbe2014-06-05 16:39:28 -07001057 case NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED: //there by default
Lorenzo Colittiea1984f2014-06-04 19:59:21 +09001058 continue;
1059 default:
1060 // At least one capability usually provided by unrestricted
1061 // networks. Conclude that this network is unrestricted.
1062 return;
Lorenzo Colitti4077acb2014-06-04 12:20:06 +09001063 }
1064 }
Lorenzo Colittiea1984f2014-06-04 19:59:21 +09001065 // All the capabilities are typically provided by restricted networks.
1066 // Conclude that this network is restricted.
Robert Greenwalt7569f182014-06-08 16:42:59 -07001067 nc.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
Lorenzo Colitti4077acb2014-06-04 12:20:06 +09001068 }
1069
Robert Greenwalt562cc542014-05-15 18:07:26 -07001070 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
1071 if (networkType == TYPE_MOBILE) {
1072 int cap = -1;
1073 if ("enableMMS".equals(feature)) {
1074 cap = NetworkCapabilities.NET_CAPABILITY_MMS;
1075 } else if ("enableSUPL".equals(feature)) {
1076 cap = NetworkCapabilities.NET_CAPABILITY_SUPL;
1077 } else if ("enableDUN".equals(feature) || "enableDUNAlways".equals(feature)) {
1078 cap = NetworkCapabilities.NET_CAPABILITY_DUN;
1079 } else if ("enableHIPRI".equals(feature)) {
1080 cap = NetworkCapabilities.NET_CAPABILITY_INTERNET;
1081 } else if ("enableFOTA".equals(feature)) {
1082 cap = NetworkCapabilities.NET_CAPABILITY_FOTA;
1083 } else if ("enableIMS".equals(feature)) {
1084 cap = NetworkCapabilities.NET_CAPABILITY_IMS;
1085 } else if ("enableCBS".equals(feature)) {
1086 cap = NetworkCapabilities.NET_CAPABILITY_CBS;
1087 } else {
1088 return null;
1089 }
1090 NetworkCapabilities netCap = new NetworkCapabilities();
Robert Greenwalt7569f182014-06-08 16:42:59 -07001091 netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(cap);
Lorenzo Colitti4077acb2014-06-04 12:20:06 +09001092 maybeMarkCapabilitiesRestricted(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001093 return netCap;
1094 } else if (networkType == TYPE_WIFI) {
1095 if ("p2p".equals(feature)) {
1096 NetworkCapabilities netCap = new NetworkCapabilities();
1097 netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
Robert Greenwalt7569f182014-06-08 16:42:59 -07001098 netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
Lorenzo Colitti4077acb2014-06-04 12:20:06 +09001099 maybeMarkCapabilitiesRestricted(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001100 return netCap;
1101 }
1102 }
1103 return null;
1104 }
1105
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001106 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001107 if (netCap == null) return TYPE_NONE;
1108 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1109 return TYPE_MOBILE_CBS;
1110 }
1111 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1112 return TYPE_MOBILE_IMS;
1113 }
1114 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1115 return TYPE_MOBILE_FOTA;
1116 }
1117 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1118 return TYPE_MOBILE_DUN;
1119 }
1120 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1121 return TYPE_MOBILE_SUPL;
1122 }
1123 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1124 return TYPE_MOBILE_MMS;
1125 }
1126 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1127 return TYPE_MOBILE_HIPRI;
1128 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001129 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1130 return TYPE_WIFI_P2P;
1131 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001132 return TYPE_NONE;
1133 }
1134
1135 private static class LegacyRequest {
1136 NetworkCapabilities networkCapabilities;
1137 NetworkRequest networkRequest;
1138 int expireSequenceNumber;
1139 Network currentNetwork;
1140 int delay = -1;
Robert Greenwalt6078b502014-06-11 16:05:07 -07001141 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001142 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001143 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001144 currentNetwork = network;
1145 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001146 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001147 }
1148 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001149 public void onLost(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001150 if (network.equals(currentNetwork)) {
1151 currentNetwork = null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001152 setProcessDefaultNetworkForHostResolution(null);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001153 }
1154 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1155 }
1156 };
1157 }
1158
1159 private HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
1160 new HashMap<NetworkCapabilities, LegacyRequest>();
1161
1162 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1163 synchronized (sLegacyRequests) {
1164 LegacyRequest l = sLegacyRequests.get(netCap);
1165 if (l != null) return l.networkRequest;
1166 }
1167 return null;
1168 }
1169
1170 private void renewRequestLocked(LegacyRequest l) {
1171 l.expireSequenceNumber++;
1172 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1173 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1174 }
1175
1176 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1177 int ourSeqNum = -1;
1178 synchronized (sLegacyRequests) {
1179 LegacyRequest l = sLegacyRequests.get(netCap);
1180 if (l == null) return;
1181 ourSeqNum = l.expireSequenceNumber;
1182 if (l.expireSequenceNumber == sequenceNum) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07001183 unregisterNetworkCallback(l.networkCallback);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001184 sLegacyRequests.remove(netCap);
1185 }
1186 }
1187 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1188 }
1189
1190 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1191 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001192 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001193 try {
1194 delay = mService.getRestoreDefaultNetworkDelay(type);
1195 } catch (RemoteException e) {}
1196 LegacyRequest l = new LegacyRequest();
1197 l.networkCapabilities = netCap;
1198 l.delay = delay;
1199 l.expireSequenceNumber = 0;
Robert Greenwalt6078b502014-06-11 16:05:07 -07001200 l.networkRequest = sendRequestForNetwork(netCap, l.networkCallback, 0,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001201 REQUEST, type);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001202 if (l.networkRequest == null) return null;
1203 sLegacyRequests.put(netCap, l);
1204 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1205 return l.networkRequest;
1206 }
1207
1208 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1209 if (delay >= 0) {
1210 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
1211 Message msg = sCallbackHandler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1212 sCallbackHandler.sendMessageDelayed(msg, delay);
1213 }
1214 }
1215
Robert Greenwalt6078b502014-06-11 16:05:07 -07001216 private NetworkCallback removeRequestForFeature(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001217 synchronized (sLegacyRequests) {
1218 LegacyRequest l = sLegacyRequests.remove(netCap);
1219 if (l == null) return null;
Robert Greenwalt6078b502014-06-11 16:05:07 -07001220 return l.networkCallback;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001222 }
1223
1224 /**
1225 * Ensure that a network route exists to deliver traffic to the specified
1226 * host via the specified network interface. An attempt to add a route that
1227 * already exists is ignored, but treated as successful.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001228 * <p>This method requires the caller to hold the permission
1229 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 * @param networkType the type of the network over which traffic to the specified
1231 * host is to be routed
1232 * @param hostAddress the IP address of the host to which the route is desired
1233 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001234 *
1235 * @deprecated Deprecated in favor of the {@link #requestNetwork},
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001236 * {@link #setProcessDefaultNetwork} and {@link Network#getSocketFactory} api.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001237 */
1238 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001239 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
1240
1241 if (inetAddress == null) {
1242 return false;
1243 }
1244
1245 return requestRouteToHostAddress(networkType, inetAddress);
1246 }
1247
1248 /**
1249 * Ensure that a network route exists to deliver traffic to the specified
1250 * host via the specified network interface. An attempt to add a route that
1251 * already exists is ignored, but treated as successful.
Jake Hamby8f9b33e2014-01-15 13:08:03 -08001252 * <p>This method requires the caller to hold the permission
1253 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001254 * @param networkType the type of the network over which traffic to the specified
1255 * host is to be routed
1256 * @param hostAddress the IP address of the host to which the route is desired
1257 * @return {@code true} on success, {@code false} on failure
1258 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001259 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001260 * {@link #setProcessDefaultNetwork} api.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001261 */
1262 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
1263 byte[] address = hostAddress.getAddress();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001264 try {
Chad Brubakerf81daa92014-02-14 13:22:34 -08001265 return mService.requestRouteToHostAddress(networkType, address, mPackageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001266 } catch (RemoteException e) {
1267 return false;
1268 }
1269 }
1270
1271 /**
1272 * Returns the value of the setting for background data usage. If false,
1273 * applications should not use the network if the application is not in the
1274 * foreground. Developers should respect this setting, and check the value
1275 * of this before performing any background data operations.
1276 * <p>
1277 * All applications that have background services that use the network
1278 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001279 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001280 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001281 * background data depends on several combined factors, and this method will
1282 * always return {@code true}. Instead, when background data is unavailable,
1283 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001284 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001285 * @return Whether background data usage is allowed.
1286 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001287 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001288 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001289 // assume that background data is allowed; final authority is
1290 // NetworkInfo which may be blocked.
1291 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 }
1293
1294 /**
1295 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001296 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001297 * @param allowBackgroundData Whether an application should use data while
1298 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001299 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001300 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1301 * @see #getBackgroundDataSetting()
1302 * @hide
1303 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001304 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001305 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001306 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001307 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001308
1309 /**
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001310 * Return quota status for the current active network, or {@code null} if no
1311 * network is active. Quota status can change rapidly, so these values
1312 * shouldn't be cached.
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001313 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001314 * <p>This method requires the call to hold the permission
1315 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1316 *
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001317 * @hide
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001318 */
1319 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1320 try {
1321 return mService.getActiveNetworkQuotaInfo();
1322 } catch (RemoteException e) {
1323 return null;
1324 }
1325 }
1326
1327 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001328 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001329 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001330 */
1331 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001332 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1333 if (b != null) {
1334 try {
1335 ITelephony it = ITelephony.Stub.asInterface(b);
1336 return it.getDataEnabled();
1337 } catch (RemoteException e) { }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001338 }
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001339 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001340 }
1341
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001342 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001343 * Callback for use with {@link ConnectivityManager#registerDefaultNetworkActiveListener}
1344 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001345 */
1346 public interface OnNetworkActiveListener {
1347 /**
1348 * Called on the main thread of the process to report that the current data network
1349 * has become active, and it is now a good time to perform any pending network
1350 * operations. Note that this listener only tells you when the network becomes
1351 * active; if at any other time you want to know whether it is active (and thus okay
1352 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001353 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001354 */
1355 public void onNetworkActive();
1356 }
1357
1358 private INetworkManagementService getNetworkManagementService() {
1359 synchronized (this) {
1360 if (mNMService != null) {
1361 return mNMService;
1362 }
1363 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1364 mNMService = INetworkManagementService.Stub.asInterface(b);
1365 return mNMService;
1366 }
1367 }
1368
1369 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
1370 mNetworkActivityListeners
1371 = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>();
1372
1373 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001374 * Start listening to reports when the system's default data network is active, meaning it is
1375 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1376 * to determine the current state of the system's default network after registering the
1377 * listener.
1378 * <p>
1379 * If the process default network has been set with
1380 * {@link ConnectivityManager#setProcessDefaultNetwork} this function will not
1381 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001382 *
1383 * @param l The listener to be told when the network is active.
1384 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001385 public void registerDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001386 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1387 @Override
1388 public void onNetworkActive() throws RemoteException {
1389 l.onNetworkActive();
1390 }
1391 };
1392
1393 try {
1394 getNetworkManagementService().registerNetworkActivityListener(rl);
1395 mNetworkActivityListeners.put(l, rl);
1396 } catch (RemoteException e) {
1397 }
1398 }
1399
1400 /**
1401 * Remove network active listener previously registered with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001402 * {@link #registerDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001403 *
1404 * @param l Previously registered listener.
1405 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001406 public void unregisterDefaultNetworkActiveListener(OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001407 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
1408 if (rl == null) {
1409 throw new IllegalArgumentException("Listener not registered: " + l);
1410 }
1411 try {
1412 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1413 } catch (RemoteException e) {
1414 }
1415 }
1416
1417 /**
1418 * Return whether the data network is currently active. An active network means that
1419 * it is currently in a high power state for performing data transmission. On some
1420 * types of networks, it may be expensive to move and stay in such a state, so it is
1421 * more power efficient to batch network traffic together when the radio is already in
1422 * this state. This method tells you whether right now is currently a good time to
1423 * initiate network traffic, as the network is already active.
1424 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001425 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001426 try {
1427 return getNetworkManagementService().isNetworkActive();
1428 } catch (RemoteException e) {
1429 }
1430 return false;
1431 }
1432
1433 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001434 * {@hide}
1435 */
Chad Brubakerf81daa92014-02-14 13:22:34 -08001436 public ConnectivityManager(IConnectivityManager service, String packageName) {
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001437 mService = checkNotNull(service, "missing IConnectivityManager");
Chad Brubakerf81daa92014-02-14 13:22:34 -08001438 mPackageName = checkNotNull(packageName, "missing package name");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001439 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001440
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07001441 /** {@hide} */
1442 public static ConnectivityManager from(Context context) {
1443 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1444 }
1445
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001446 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001447 * Get the set of tetherable, available interfaces. This list is limited by
1448 * device configuration and current interface existence.
1449 *
1450 * @return an array of 0 or more Strings of tetherable interface names.
1451 *
1452 * <p>This method requires the call to hold the permission
1453 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001454 * {@hide}
1455 */
1456 public String[] getTetherableIfaces() {
1457 try {
1458 return mService.getTetherableIfaces();
1459 } catch (RemoteException e) {
1460 return new String[0];
1461 }
1462 }
1463
1464 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001465 * Get the set of tethered interfaces.
1466 *
1467 * @return an array of 0 or more String of currently tethered interface names.
1468 *
1469 * <p>This method requires the call to hold the permission
1470 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001471 * {@hide}
1472 */
1473 public String[] getTetheredIfaces() {
1474 try {
1475 return mService.getTetheredIfaces();
1476 } catch (RemoteException e) {
1477 return new String[0];
1478 }
1479 }
1480
1481 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001482 * Get the set of interface names which attempted to tether but
1483 * failed. Re-attempting to tether may cause them to reset to the Tethered
1484 * state. Alternatively, causing the interface to be destroyed and recreated
1485 * may cause them to reset to the available state.
1486 * {@link ConnectivityManager#getLastTetherError} can be used to get more
1487 * information on the cause of the errors.
1488 *
1489 * @return an array of 0 or more String indicating the interface names
1490 * which failed to tether.
1491 *
1492 * <p>This method requires the call to hold the permission
1493 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001494 * {@hide}
1495 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001496 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001497 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001498 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001499 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001500 return new String[0];
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001501 }
1502 }
1503
1504 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001505 * Get the set of tethered dhcp ranges.
1506 *
1507 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
1508 * {@hide}
1509 */
1510 public String[] getTetheredDhcpRanges() {
1511 try {
1512 return mService.getTetheredDhcpRanges();
1513 } catch (RemoteException e) {
1514 return new String[0];
1515 }
1516 }
1517
1518 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001519 * Attempt to tether the named interface. This will setup a dhcp server
1520 * on the interface, forward and NAT IP packets and forward DNS requests
1521 * to the best active upstream network interface. Note that if no upstream
1522 * IP network interface is available, dhcp will still run and traffic will be
1523 * allowed between the tethered devices and this device, though upstream net
1524 * access will of course fail until an upstream network interface becomes
1525 * active.
1526 *
1527 * @param iface the interface name to tether.
1528 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1529 *
1530 * <p>This method requires the call to hold the permission
1531 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001532 * {@hide}
1533 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001534 public int tether(String iface) {
1535 try {
1536 return mService.tether(iface);
1537 } catch (RemoteException e) {
1538 return TETHER_ERROR_SERVICE_UNAVAIL;
1539 }
1540 }
1541
1542 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001543 * Stop tethering the named interface.
1544 *
1545 * @param iface the interface name to untether.
1546 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1547 *
1548 * <p>This method requires the call to hold the permission
1549 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt5a735062010-03-02 17:25:02 -08001550 * {@hide}
1551 */
1552 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001553 try {
1554 return mService.untether(iface);
1555 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001556 return TETHER_ERROR_SERVICE_UNAVAIL;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001557 }
1558 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001559
1560 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001561 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001562 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001563 * due to device configuration.
1564 *
1565 * @return a boolean - {@code true} indicating Tethering is supported.
1566 *
1567 * <p>This method requires the call to hold the permission
1568 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001569 * {@hide}
1570 */
1571 public boolean isTetheringSupported() {
1572 try {
1573 return mService.isTetheringSupported();
1574 } catch (RemoteException e) {
1575 return false;
1576 }
1577 }
1578
1579 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001580 * Get the list of regular expressions that define any tetherable
1581 * USB network interfaces. If USB tethering is not supported by the
1582 * device, this list should be empty.
1583 *
1584 * @return an array of 0 or more regular expression Strings defining
1585 * what interfaces are considered tetherable usb interfaces.
1586 *
1587 * <p>This method requires the call to hold the permission
1588 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001589 * {@hide}
1590 */
1591 public String[] getTetherableUsbRegexs() {
1592 try {
1593 return mService.getTetherableUsbRegexs();
1594 } catch (RemoteException e) {
1595 return new String[0];
1596 }
1597 }
1598
1599 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001600 * Get the list of regular expressions that define any tetherable
1601 * Wifi network interfaces. If Wifi tethering is not supported by the
1602 * device, this list should be empty.
1603 *
1604 * @return an array of 0 or more regular expression Strings defining
1605 * what interfaces are considered tetherable wifi interfaces.
1606 *
1607 * <p>This method requires the call to hold the permission
1608 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001609 * {@hide}
1610 */
1611 public String[] getTetherableWifiRegexs() {
1612 try {
1613 return mService.getTetherableWifiRegexs();
1614 } catch (RemoteException e) {
1615 return new String[0];
1616 }
1617 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08001618
Danica Chang6fdd0c62010-08-11 14:54:43 -07001619 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001620 * Get the list of regular expressions that define any tetherable
1621 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
1622 * device, this list should be empty.
1623 *
1624 * @return an array of 0 or more regular expression Strings defining
1625 * what interfaces are considered tetherable bluetooth interfaces.
1626 *
1627 * <p>This method requires the call to hold the permission
1628 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001629 * {@hide}
1630 */
1631 public String[] getTetherableBluetoothRegexs() {
1632 try {
1633 return mService.getTetherableBluetoothRegexs();
1634 } catch (RemoteException e) {
1635 return new String[0];
1636 }
1637 }
1638
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001639 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001640 * Attempt to both alter the mode of USB and Tethering of USB. A
1641 * utility method to deal with some of the complexity of USB - will
1642 * attempt to switch to Rndis and subsequently tether the resulting
1643 * interface on {@code true} or turn off tethering and switch off
1644 * Rndis on {@code false}.
1645 *
1646 * @param enable a boolean - {@code true} to enable tethering
1647 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1648 *
1649 * <p>This method requires the call to hold the permission
1650 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001651 * {@hide}
1652 */
1653 public int setUsbTethering(boolean enable) {
1654 try {
1655 return mService.setUsbTethering(enable);
1656 } catch (RemoteException e) {
1657 return TETHER_ERROR_SERVICE_UNAVAIL;
1658 }
1659 }
1660
Robert Greenwalt5a735062010-03-02 17:25:02 -08001661 /** {@hide} */
1662 public static final int TETHER_ERROR_NO_ERROR = 0;
1663 /** {@hide} */
1664 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
1665 /** {@hide} */
1666 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
1667 /** {@hide} */
1668 public static final int TETHER_ERROR_UNSUPPORTED = 3;
1669 /** {@hide} */
1670 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
1671 /** {@hide} */
1672 public static final int TETHER_ERROR_MASTER_ERROR = 5;
1673 /** {@hide} */
1674 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
1675 /** {@hide} */
1676 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
1677 /** {@hide} */
1678 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
1679 /** {@hide} */
1680 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
1681 /** {@hide} */
1682 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
1683
1684 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001685 * Get a more detailed error code after a Tethering or Untethering
1686 * request asynchronously failed.
1687 *
1688 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08001689 * @return error The error code of the last error tethering or untethering the named
1690 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001691 *
1692 * <p>This method requires the call to hold the permission
1693 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a735062010-03-02 17:25:02 -08001694 * {@hide}
1695 */
1696 public int getLastTetherError(String iface) {
1697 try {
1698 return mService.getLastTetherError(iface);
1699 } catch (RemoteException e) {
1700 return TETHER_ERROR_SERVICE_UNAVAIL;
1701 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001702 }
1703
1704 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001705 * Report network connectivity status. This is currently used only
1706 * to alter status bar UI.
1707 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001708 * @param networkType The type of network you want to report on
1709 * @param percentage The quality of the connection 0 is bad, 100 is good
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001710 *
1711 * <p>This method requires the call to hold the permission
1712 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001713 * {@hide}
1714 */
1715 public void reportInetCondition(int networkType, int percentage) {
1716 try {
1717 mService.reportInetCondition(networkType, percentage);
1718 } catch (RemoteException e) {
1719 }
1720 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001721
1722 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001723 * Report a problem network to the framework. This provides a hint to the system
1724 * that there might be connectivity problems on this network and may cause
1725 * the framework to re-evaluate network connectivity and/or switch to another
1726 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001727 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001728 * @param network The {@link Network} the application was attempting to use
1729 * or {@code null} to indicate the current default network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001730 */
1731 public void reportBadNetwork(Network network) {
1732 try {
1733 mService.reportBadNetwork(network);
1734 } catch (RemoteException e) {
1735 }
1736 }
1737
1738 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001739 * Set a network-independent global http proxy. This is not normally what you want
1740 * for typical HTTP proxies - they are general network dependent. However if you're
1741 * doing something unusual like general internal filtering this may be useful. On
1742 * a private network where the proxy is not accessible, you may break HTTP using this.
1743 *
Jason Monk207900c2014-04-25 15:00:09 -04001744 * @param p The a {@link ProxyInfo} object defining the new global
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001745 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
1746 *
1747 * <p>This method requires the call to hold the permission
Jason Monkfaf3fd52014-05-07 18:41:13 -04001748 * android.Manifest.permission#CONNECTIVITY_INTERNAL.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001749 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07001750 */
Jason Monk207900c2014-04-25 15:00:09 -04001751 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001752 try {
1753 mService.setGlobalProxy(p);
1754 } catch (RemoteException e) {
1755 }
1756 }
1757
1758 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001759 * Retrieve any network-independent global HTTP proxy.
1760 *
Jason Monk207900c2014-04-25 15:00:09 -04001761 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001762 * if no global HTTP proxy is set.
1763 *
1764 * <p>This method requires the call to hold the permission
1765 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001766 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07001767 */
Jason Monk207900c2014-04-25 15:00:09 -04001768 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001769 try {
1770 return mService.getGlobalProxy();
1771 } catch (RemoteException e) {
1772 return null;
1773 }
1774 }
1775
1776 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001777 * Get the HTTP proxy settings for the current default network. Note that
1778 * if a global proxy is set, it will override any per-network setting.
1779 *
Jason Monk207900c2014-04-25 15:00:09 -04001780 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001781 * HTTP proxy is active.
1782 *
1783 * <p>This method requires the call to hold the permission
1784 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt434203a2010-10-11 16:00:27 -07001785 * {@hide}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001786 * @deprecated Deprecated in favor of {@link #getLinkProperties}
Robert Greenwalt434203a2010-10-11 16:00:27 -07001787 */
Jason Monk207900c2014-04-25 15:00:09 -04001788 public ProxyInfo getProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001789 try {
1790 return mService.getProxy();
1791 } catch (RemoteException e) {
1792 return null;
1793 }
1794 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001795
1796 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001797 * Sets a secondary requirement bit for the given networkType.
1798 * This requirement bit is generally under the control of the carrier
1799 * or its agents and is not directly controlled by the user.
1800 *
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001801 * @param networkType The network who's dependence has changed
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001802 * @param met Boolean - true if network use is OK, false if not
1803 *
1804 * <p>This method requires the call to hold the permission
1805 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001806 * {@hide}
1807 */
1808 public void setDataDependency(int networkType, boolean met) {
1809 try {
1810 mService.setDataDependency(networkType, met);
1811 } catch (RemoteException e) {
1812 }
1813 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07001814
1815 /**
1816 * Returns true if the hardware supports the given network type
1817 * else it returns false. This doesn't indicate we have coverage
1818 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001819 * hardware supports it. For example a GSM phone without a SIM
1820 * should still return {@code true} for mobile data, but a wifi only
1821 * tablet would return {@code false}.
1822 *
1823 * @param networkType The network type we'd like to check
1824 * @return {@code true} if supported, else {@code false}
1825 *
1826 * <p>This method requires the call to hold the permission
1827 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07001828 * @hide
1829 */
1830 public boolean isNetworkSupported(int networkType) {
1831 try {
1832 return mService.isNetworkSupported(networkType);
1833 } catch (RemoteException e) {}
1834 return false;
1835 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07001836
1837 /**
1838 * Returns if the currently active data network is metered. A network is
1839 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001840 * that connection due to monetary costs, data limitations or
1841 * battery/performance issues. You should check this before doing large
1842 * data transfers, and warn the user or delay the operation until another
1843 * network is available.
1844 *
1845 * @return {@code true} if large transfers should be avoided, otherwise
1846 * {@code false}.
1847 *
1848 * <p>This method requires the call to hold the permission
1849 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07001850 */
1851 public boolean isActiveNetworkMetered() {
1852 try {
1853 return mService.isActiveNetworkMetered();
1854 } catch (RemoteException e) {
1855 return false;
1856 }
1857 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07001858
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001859 /**
1860 * If the LockdownVpn mechanism is enabled, updates the vpn
1861 * with a reload of its profile.
1862 *
1863 * @return a boolean with {@code} indicating success
1864 *
1865 * <p>This method can only be called by the system UID
1866 * {@hide}
1867 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07001868 public boolean updateLockdownVpn() {
1869 try {
1870 return mService.updateLockdownVpn();
1871 } catch (RemoteException e) {
1872 return false;
1873 }
1874 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07001875
1876 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001877 * Signal that the captive portal check on the indicated network
Wink Savilled747cbc2013-08-07 16:22:47 -07001878 * is complete and whether its a captive portal or not.
1879 *
1880 * @param info the {@link NetworkInfo} object for the networkType
1881 * in question.
1882 * @param isCaptivePortal true/false.
1883 *
1884 * <p>This method requires the call to hold the permission
1885 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
1886 * {@hide}
1887 */
1888 public void captivePortalCheckCompleted(NetworkInfo info, boolean isCaptivePortal) {
1889 try {
1890 mService.captivePortalCheckCompleted(info, isCaptivePortal);
1891 } catch (RemoteException e) {
1892 }
1893 }
1894
1895 /**
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001896 * Supply the backend messenger for a network tracker
1897 *
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001898 * @param networkType NetworkType to set
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001899 * @param messenger {@link Messenger}
1900 * {@hide}
1901 */
1902 public void supplyMessenger(int networkType, Messenger messenger) {
1903 try {
1904 mService.supplyMessenger(networkType, messenger);
1905 } catch (RemoteException e) {
1906 }
1907 }
Wink Savilleab9321d2013-06-29 21:10:57 -07001908
1909 /**
Wink Saville948282b2013-08-29 08:55:16 -07001910 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07001911 *
Wink Savilleab9321d2013-06-29 21:10:57 -07001912 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07001913 *
1914 * @return time out that will be used, maybe less that suggestedTimeOutMs
1915 * -1 if an error.
1916 *
1917 * {@hide}
1918 */
Wink Saville948282b2013-08-29 08:55:16 -07001919 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07001920 int timeOutMs = -1;
1921 try {
Wink Saville948282b2013-08-29 08:55:16 -07001922 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07001923 } catch (RemoteException e) {
1924 }
1925 return timeOutMs;
1926 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07001927
1928 /**
Wink Saville42d4f082013-07-20 20:31:59 -07001929 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07001930 * {@hide}
1931 */
1932 public String getMobileProvisioningUrl() {
1933 try {
1934 return mService.getMobileProvisioningUrl();
1935 } catch (RemoteException e) {
1936 }
1937 return null;
1938 }
Wink Saville42d4f082013-07-20 20:31:59 -07001939
1940 /**
1941 * Get the mobile redirected provisioning url.
1942 * {@hide}
1943 */
1944 public String getMobileRedirectedProvisioningUrl() {
1945 try {
1946 return mService.getMobileRedirectedProvisioningUrl();
1947 } catch (RemoteException e) {
1948 }
1949 return null;
1950 }
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001951
1952 /**
1953 * get the information about a specific network link
1954 * @hide
1955 */
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001956 public LinkQualityInfo getLinkQualityInfo(int networkType) {
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001957 try {
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001958 LinkQualityInfo li = mService.getLinkQualityInfo(networkType);
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001959 return li;
1960 } catch (RemoteException e) {
1961 return null;
1962 }
1963 }
1964
1965 /**
1966 * get the information of currently active network link
1967 * @hide
1968 */
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001969 public LinkQualityInfo getActiveLinkQualityInfo() {
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001970 try {
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001971 LinkQualityInfo li = mService.getActiveLinkQualityInfo();
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001972 return li;
1973 } catch (RemoteException e) {
1974 return null;
1975 }
1976 }
1977
1978 /**
1979 * get the information of all network links
1980 * @hide
1981 */
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001982 public LinkQualityInfo[] getAllLinkQualityInfo() {
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001983 try {
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001984 LinkQualityInfo[] li = mService.getAllLinkQualityInfo();
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001985 return li;
1986 } catch (RemoteException e) {
1987 return null;
1988 }
1989 }
Wink Saville7788c612013-08-29 14:57:08 -07001990
1991 /**
Wink Saville948282b2013-08-29 08:55:16 -07001992 * Set sign in error notification to visible or in visible
1993 *
1994 * @param visible
1995 * @param networkType
1996 *
1997 * {@hide}
1998 */
1999 public void setProvisioningNotificationVisible(boolean visible, int networkType,
2000 String extraInfo, String url) {
2001 try {
2002 mService.setProvisioningNotificationVisible(visible, networkType, extraInfo, url);
2003 } catch (RemoteException e) {
2004 }
2005 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07002006
2007 /**
2008 * Set the value for enabling/disabling airplane mode
2009 *
2010 * @param enable whether to enable airplane mode or not
2011 *
2012 * <p>This method requires the call to hold the permission
2013 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
2014 * @hide
2015 */
2016 public void setAirplaneMode(boolean enable) {
2017 try {
2018 mService.setAirplaneMode(enable);
2019 } catch (RemoteException e) {
2020 }
2021 }
Robert Greenwalte049c232014-04-11 15:53:27 -07002022
2023 /** {@hide} */
Robert Greenwalta67be032014-05-16 15:49:14 -07002024 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07002025 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07002026 mService.registerNetworkFactory(messenger, name);
2027 } catch (RemoteException e) { }
2028 }
2029
2030 /** {@hide} */
2031 public void unregisterNetworkFactory(Messenger messenger) {
2032 try {
2033 mService.unregisterNetworkFactory(messenger);
Robert Greenwalte049c232014-04-11 15:53:27 -07002034 } catch (RemoteException e) { }
2035 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07002036
2037 /** {@hide} */
2038 public void registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
2039 NetworkCapabilities nc, int score) {
2040 try {
2041 mService.registerNetworkAgent(messenger, ni, lp, nc, score);
2042 } catch (RemoteException e) { }
2043 }
2044
Robert Greenwalt9258c642014-03-26 16:47:06 -07002045 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002046 * Base class for NetworkRequest callbacks. Used for notifications about network
2047 * changes. Should be extended by applications wanting notifications.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002048 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002049 public static class NetworkCallback {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002050 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002051 public static final int PRECHECK = 1;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002052 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002053 public static final int AVAILABLE = 2;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002054 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002055 public static final int LOSING = 3;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002056 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002057 public static final int LOST = 4;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002058 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002059 public static final int UNAVAIL = 5;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002060 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002061 public static final int CAP_CHANGED = 6;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002062 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002063 public static final int PROP_CHANGED = 7;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002064 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002065 public static final int CANCELED = 8;
2066
2067 /**
2068 * @hide
2069 * Called whenever the framework connects to a network that it may use to
2070 * satisfy this request
2071 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002072 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002073
2074 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002075 * Called when the framework connects and has declared new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002076 * This callback may be called more than once if the {@link Network} that is
2077 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002078 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002079 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002080 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002081 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002082
2083 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002084 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002085 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002086 * for graceful handover. This may not be called if we have a hard loss
2087 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002088 * {@link NetworkCallback#onLost} call or a
2089 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002090 * on whether we lose or regain it.
2091 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002092 * @param network The {@link Network} that is about to be disconnected.
2093 * @param maxMsToLive The time in ms the framework will attempt to keep the
2094 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002095 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002096 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002097 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002098
2099 /**
2100 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002101 * graceful failure ends.
2102 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002103 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002104 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002105 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002106
2107 /**
2108 * Called if no network is found in the given timeout time. If no timeout is given,
2109 * this will not be called.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002110 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002111 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002112 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002113
2114 /**
2115 * Called when the network the framework connected to for this request
2116 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002117 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002118 * @param network The {@link Network} whose capabilities have changed.
2119 * @param networkCapabilities The new {@link NetworkCapabilities} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002120 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002121 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002122 NetworkCapabilities networkCapabilities) {}
2123
2124 /**
2125 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002126 * changes {@link LinkProperties}.
2127 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002128 * @param network The {@link Network} whose link properties have changed.
2129 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002130 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002131 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002132
Robert Greenwalt6078b502014-06-11 16:05:07 -07002133 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002134 }
2135
Robert Greenwalt9258c642014-03-26 16:47:06 -07002136 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
2137 /** @hide obj = pair(NetworkRequest, Network) */
2138 public static final int CALLBACK_PRECHECK = BASE + 1;
2139 /** @hide obj = pair(NetworkRequest, Network) */
2140 public static final int CALLBACK_AVAILABLE = BASE + 2;
2141 /** @hide obj = pair(NetworkRequest, Network), arg1 = ttl */
2142 public static final int CALLBACK_LOSING = BASE + 3;
2143 /** @hide obj = pair(NetworkRequest, Network) */
2144 public static final int CALLBACK_LOST = BASE + 4;
2145 /** @hide obj = NetworkRequest */
2146 public static final int CALLBACK_UNAVAIL = BASE + 5;
2147 /** @hide obj = pair(NetworkRequest, Network) */
2148 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2149 /** @hide obj = pair(NetworkRequest, Network) */
2150 public static final int CALLBACK_IP_CHANGED = BASE + 7;
2151 /** @hide obj = NetworkRequest */
2152 public static final int CALLBACK_RELEASED = BASE + 8;
2153 /** @hide */
2154 public static final int CALLBACK_EXIT = BASE + 9;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002155 /** @hide obj = NetworkCapabilities, arg1 = seq number */
2156 private static final int EXPIRE_LEGACY_REQUEST = BASE + 10;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002157
Robert Greenwalt562cc542014-05-15 18:07:26 -07002158 private class CallbackHandler extends Handler {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002159 private final HashMap<NetworkRequest, NetworkCallback>mCallbackMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002160 private final AtomicInteger mRefCount;
2161 private static final String TAG = "ConnectivityManager.CallbackHandler";
2162 private final ConnectivityManager mCm;
2163
Robert Greenwalt6078b502014-06-11 16:05:07 -07002164 CallbackHandler(Looper looper, HashMap<NetworkRequest, NetworkCallback>callbackMap,
Robert Greenwalt9258c642014-03-26 16:47:06 -07002165 AtomicInteger refCount, ConnectivityManager cm) {
2166 super(looper);
2167 mCallbackMap = callbackMap;
2168 mRefCount = refCount;
2169 mCm = cm;
2170 }
2171
2172 @Override
2173 public void handleMessage(Message message) {
2174 Log.d(TAG, "CM callback handler got msg " + message.what);
2175 switch (message.what) {
2176 case CALLBACK_PRECHECK: {
2177 NetworkRequest request = getNetworkRequest(message);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002178 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002179 if (callbacks != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002180 callbacks.onPreCheck(getNetwork(message));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002181 } else {
2182 Log.e(TAG, "callback not found for PRECHECK message");
2183 }
2184 break;
2185 }
2186 case CALLBACK_AVAILABLE: {
2187 NetworkRequest request = getNetworkRequest(message);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002188 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002189 if (callbacks != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002190 callbacks.onAvailable(getNetwork(message));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002191 } else {
2192 Log.e(TAG, "callback not found for AVAILABLE message");
2193 }
2194 break;
2195 }
2196 case CALLBACK_LOSING: {
2197 NetworkRequest request = getNetworkRequest(message);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002198 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002199 if (callbacks != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002200 callbacks.onLosing(getNetwork(message), message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002201 } else {
2202 Log.e(TAG, "callback not found for LOSING message");
2203 }
2204 break;
2205 }
2206 case CALLBACK_LOST: {
2207 NetworkRequest request = getNetworkRequest(message);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002208 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002209 if (callbacks != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002210 callbacks.onLost(getNetwork(message));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002211 } else {
2212 Log.e(TAG, "callback not found for LOST message");
2213 }
2214 break;
2215 }
2216 case CALLBACK_UNAVAIL: {
2217 NetworkRequest req = (NetworkRequest)message.obj;
Robert Greenwalt6078b502014-06-11 16:05:07 -07002218 NetworkCallback callbacks = null;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002219 synchronized(mCallbackMap) {
2220 callbacks = mCallbackMap.get(req);
2221 }
2222 if (callbacks != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002223 callbacks.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002224 } else {
2225 Log.e(TAG, "callback not found for UNAVAIL message");
2226 }
2227 break;
2228 }
2229 case CALLBACK_CAP_CHANGED: {
2230 NetworkRequest request = getNetworkRequest(message);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002231 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002232 if (callbacks != null) {
2233 Network network = getNetwork(message);
2234 NetworkCapabilities cap = mCm.getNetworkCapabilities(network);
2235
Robert Greenwalt6078b502014-06-11 16:05:07 -07002236 callbacks.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002237 } else {
2238 Log.e(TAG, "callback not found for CHANGED message");
2239 }
2240 break;
2241 }
2242 case CALLBACK_IP_CHANGED: {
2243 NetworkRequest request = getNetworkRequest(message);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002244 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002245 if (callbacks != null) {
2246 Network network = getNetwork(message);
2247 LinkProperties lp = mCm.getLinkProperties(network);
2248
Robert Greenwalt6078b502014-06-11 16:05:07 -07002249 callbacks.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002250 } else {
2251 Log.e(TAG, "callback not found for CHANGED message");
2252 }
2253 break;
2254 }
2255 case CALLBACK_RELEASED: {
2256 NetworkRequest req = (NetworkRequest)message.obj;
Robert Greenwalt6078b502014-06-11 16:05:07 -07002257 NetworkCallback callbacks = null;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002258 synchronized(mCallbackMap) {
2259 callbacks = mCallbackMap.remove(req);
2260 }
2261 if (callbacks != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002262 synchronized(mRefCount) {
2263 if (mRefCount.decrementAndGet() == 0) {
2264 getLooper().quit();
2265 }
2266 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002267 } else {
2268 Log.e(TAG, "callback not found for CANCELED message");
2269 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002270 break;
2271 }
2272 case CALLBACK_EXIT: {
2273 Log.d(TAG, "Listener quiting");
2274 getLooper().quit();
2275 break;
2276 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07002277 case EXPIRE_LEGACY_REQUEST: {
2278 expireRequest((NetworkCapabilities)message.obj, message.arg1);
2279 break;
2280 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002281 }
2282 }
2283
2284 private NetworkRequest getNetworkRequest(Message msg) {
2285 return (NetworkRequest)(msg.obj);
2286 }
Robert Greenwalt6078b502014-06-11 16:05:07 -07002287 private NetworkCallback getCallbacks(NetworkRequest req) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002288 synchronized(mCallbackMap) {
2289 return mCallbackMap.get(req);
2290 }
2291 }
2292 private Network getNetwork(Message msg) {
2293 return new Network(msg.arg2);
2294 }
Robert Greenwalt6078b502014-06-11 16:05:07 -07002295 private NetworkCallback removeCallbacks(Message msg) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002296 NetworkRequest req = (NetworkRequest)msg.obj;
2297 synchronized(mCallbackMap) {
2298 return mCallbackMap.remove(req);
2299 }
2300 }
2301 }
2302
Robert Greenwalt6078b502014-06-11 16:05:07 -07002303 private void incCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002304 synchronized(sCallbackRefCount) {
2305 if (sCallbackRefCount.incrementAndGet() == 1) {
2306 // TODO - switch this over to a ManagerThread or expire it when done
2307 HandlerThread callbackThread = new HandlerThread("ConnectivityManager");
2308 callbackThread.start();
2309 sCallbackHandler = new CallbackHandler(callbackThread.getLooper(),
Robert Greenwalt6078b502014-06-11 16:05:07 -07002310 sNetworkCallback, sCallbackRefCount, this);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002311 }
2312 }
2313 }
2314
Robert Greenwalt6078b502014-06-11 16:05:07 -07002315 private void decCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002316 synchronized(sCallbackRefCount) {
2317 if (sCallbackRefCount.decrementAndGet() == 0) {
2318 sCallbackHandler.obtainMessage(CALLBACK_EXIT).sendToTarget();
2319 sCallbackHandler = null;
2320 }
2321 }
2322 }
2323
Robert Greenwalt6078b502014-06-11 16:05:07 -07002324 static final HashMap<NetworkRequest, NetworkCallback> sNetworkCallback =
2325 new HashMap<NetworkRequest, NetworkCallback>();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002326 static final AtomicInteger sCallbackRefCount = new AtomicInteger(0);
2327 static CallbackHandler sCallbackHandler = null;
2328
2329 private final static int LISTEN = 1;
2330 private final static int REQUEST = 2;
2331
Robert Greenwalt562cc542014-05-15 18:07:26 -07002332 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002333 NetworkCallback networkCallback, int timeoutSec, int action,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07002334 int legacyType) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002335 if (networkCallback == null) {
2336 throw new IllegalArgumentException("null NetworkCallback");
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002337 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002338 if (need == null) throw new IllegalArgumentException("null NetworkCapabilities");
2339 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002340 incCallbackHandlerRefCount();
Paul Jensen7221cc32014-06-27 11:05:32 -04002341 synchronized(sNetworkCallback) {
2342 if (action == LISTEN) {
2343 networkCallback.networkRequest = mService.listenForNetwork(need,
2344 new Messenger(sCallbackHandler), new Binder());
2345 } else {
2346 networkCallback.networkRequest = mService.requestNetwork(need,
2347 new Messenger(sCallbackHandler), timeoutSec, new Binder(), legacyType);
2348 }
2349 if (networkCallback.networkRequest != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002350 sNetworkCallback.put(networkCallback.networkRequest, networkCallback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002351 }
2352 }
2353 } catch (RemoteException e) {}
Robert Greenwalt6078b502014-06-11 16:05:07 -07002354 if (networkCallback.networkRequest == null) decCallbackHandlerRefCount();
2355 return networkCallback.networkRequest;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002356 }
2357
2358 /**
2359 * Request a network to satisfy a set of {@link NetworkCapabilities}.
2360 *
2361 * This {@link NetworkRequest} will live until released via
Robert Greenwalt6078b502014-06-11 16:05:07 -07002362 * {@link #unregisterNetworkCallback} or the calling application exits.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002363 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07002364 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002365 * can be used to direct traffic to the network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002366 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002367 * @param request {@link NetworkRequest} describing this request.
2368 * @param networkCallback The {@link NetworkCallback} to be utilized for this
2369 * request. Note the callback must not be shared - they
2370 * uniquely specify this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002371 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002372 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
2373 sendRequestForNetwork(request.networkCapabilities, networkCallback, 0,
2374 REQUEST, TYPE_NONE);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002375 }
2376
2377 /**
2378 * Request a network to satisfy a set of {@link NetworkCapabilities}, limited
2379 * by a timeout.
2380 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002381 * This function behaves identically to the non-timedout version, but if a suitable
Robert Greenwalt6078b502014-06-11 16:05:07 -07002382 * network is not found within the given time (in milliseconds) the
2383 * {@link NetworkCallback#unavailable} callback is called. The request must
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002384 * still be released normally by calling {@link releaseNetworkRequest}.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002385 * @param request {@link NetworkRequest} describing this request.
2386 * @param networkCallback The callbacks to be utilized for this request. Note
2387 * the callbacks must not be shared - they uniquely specify
2388 * this request.
2389 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
2390 * before {@link NetworkCallback#unavailable} is called.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002391 * @hide
2392 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002393 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
2394 int timeoutMs) {
2395 sendRequestForNetwork(request.networkCapabilities, networkCallback, timeoutMs,
2396 REQUEST, TYPE_NONE);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002397 }
2398
2399 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002400 * The maximum number of milliseconds the framework will look for a suitable network
Robert Greenwalt9258c642014-03-26 16:47:06 -07002401 * during a timeout-equiped call to {@link requestNetwork}.
2402 * {@hide}
2403 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002404 public final static int MAX_NETWORK_REQUEST_TIMEOUT_MS = 100 * 60 * 1000;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002405
2406 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002407 * The lookup key for a {@link Network} object included with the intent after
2408 * succesfully finding a network for the applications request. Retrieve it with
2409 * {@link android.content.Intent#getParcelableExtra(String)}.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002410 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002411 */
2412 public static final String EXTRA_NETWORK_REQUEST_NETWORK = "networkRequestNetwork";
2413
2414 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002415 * The lookup key for a {@link NetworkRequest} object included with the intent after
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002416 * succesfully finding a network for the applications request. Retrieve it with
2417 * {@link android.content.Intent#getParcelableExtra(String)}.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002418 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002419 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002420 public static final String EXTRA_NETWORK_REQUEST_NETWORK_REQUEST =
2421 "networkRequestNetworkRequest";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002422
2423
2424 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07002425 * Request a network to satisfy a set of {@link NetworkCapabilities}.
2426 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002427 * This function behavies identically to the version that takes a NetworkCallback, but instead
2428 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002429 * the request may outlive the calling application and get called back when a suitable
2430 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002431 * <p>
2432 * The operation is an Intent broadcast that goes to a broadcast receiver that
2433 * you registered with {@link Context#registerReceiver} or through the
2434 * &lt;receiver&gt; tag in an AndroidManifest.xml file
2435 * <p>
2436 * The operation Intent is delivered with two extras, a {@link Network} typed
Robert Greenwalt6078b502014-06-11 16:05:07 -07002437 * extra called {@link #EXTRA_NETWORK_REQUEST_NETWORK} and a {@link NetworkRequest}
2438 * typed extra called {@link #EXTRA_NETWORK_REQUEST_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07002439 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002440 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07002441 * Intent to reserve the network or it will be released shortly after the Intent
2442 * is processed.
2443 * <p>
2444 * If there is already an request for this Intent registered (with the equality of
2445 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002446 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002447 * <p>
Robert Greenwalt6078b502014-06-11 16:05:07 -07002448 * The request may be released normally by calling {@link #unregisterNetworkCallback}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002449 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002450 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002451 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07002452 * to the {@link NetworkCallback#onAvailable} call. Typically
Robert Greenwalt9258c642014-03-26 16:47:06 -07002453 * comes from {@link PendingIntent#getBroadcast}.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002454 * @hide
Robert Greenwalt9258c642014-03-26 16:47:06 -07002455 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002456 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002457 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002458 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002459 } catch (RemoteException e) {}
Robert Greenwalt9258c642014-03-26 16:47:06 -07002460 }
2461
2462 /**
2463 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07002464 * {@link NetworkRequest}. The callbacks will continue to be called until
2465 * either the application exits or {@link #unregisterNetworkCallback} is called
Robert Greenwalt9258c642014-03-26 16:47:06 -07002466 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002467 * @param request {@link NetworkRequest} describing this request.
2468 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
2469 * networks change state.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002470 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002471 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
2472 sendRequestForNetwork(request.networkCapabilities, networkCallback, 0, LISTEN, TYPE_NONE);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002473 }
2474
2475 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002476 * Unregisters callbacks about and possibly releases networks originating from
2477 * {@link #requestNetwork} and {@link #registerNetworkCallback} calls. If the
2478 * given {@code NetworkCallback} had previosuly been used with {@code #requestNetwork},
2479 * any networks that had been connected to only to satisfy that request will be
2480 * disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002481 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002482 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002483 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002484 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
2485 if (networkCallback == null || networkCallback.networkRequest == null ||
2486 networkCallback.networkRequest.requestId == REQUEST_ID_UNSET) {
2487 throw new IllegalArgumentException("Invalid NetworkCallback");
2488 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002489 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002490 mService.releaseNetworkRequest(networkCallback.networkRequest);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002491 } catch (RemoteException e) {}
2492 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002493
2494 /**
2495 * Binds the current process to {@code network}. All Sockets created in the future
2496 * (and not explicitly bound via a bound SocketFactory from
2497 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
2498 * {@code network}. All host name resolutions will be limited to {@code network} as well.
2499 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
2500 * work and all host name resolutions will fail. This is by design so an application doesn't
2501 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
2502 * To clear binding pass {@code null} for {@code network}. Using individually bound
2503 * Sockets created by Network.getSocketFactory().createSocket() and
2504 * performing network-specific host name resolutions via
2505 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
2506 * {@code setProcessDefaultNetwork}.
2507 *
2508 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
2509 * the current binding.
2510 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2511 */
2512 public static boolean setProcessDefaultNetwork(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04002513 return NetworkUtils.bindProcessToNetwork(network == null ? NETID_UNSET : network.netId);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002514 }
2515
2516 /**
2517 * Returns the {@link Network} currently bound to this process via
2518 * {@link #setProcessDefaultNetwork}, or {@code null} if no {@link Network} is explicitly bound.
2519 *
2520 * @return {@code Network} to which this process is bound, or {@code null}.
2521 */
2522 public static Network getProcessDefaultNetwork() {
2523 int netId = NetworkUtils.getNetworkBoundToProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04002524 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002525 return new Network(netId);
2526 }
2527
2528 /**
2529 * Binds host resolutions performed by this process to {@code network}.
2530 * {@link #setProcessDefaultNetwork} takes precedence over this setting.
2531 *
2532 * @param network The {@link Network} to bind host resolutions from the current process to, or
2533 * {@code null} to clear the current binding.
2534 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2535 * @hide
2536 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
2537 */
2538 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04002539 return NetworkUtils.bindProcessToNetworkForHostResolution(
2540 network == null ? NETID_UNSET : network.netId);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002541 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002542}