blob: 1a5180841602410388177bcc8c64b5f88f3251f4 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016package android.net;
17
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -070018import static com.android.internal.util.Preconditions.checkNotNull;
19
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.annotation.SdkConstant;
21import android.annotation.SdkConstant.SdkConstantType;
Robert Greenwalt9258c642014-03-26 16:47:06 -070022import android.app.PendingIntent;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070023import android.content.Context;
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070024import android.content.Intent;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -040025import android.net.NetworkUtils;
Robert Greenwalt42acef32009-08-12 16:08:25 -070026import android.os.Binder;
Jeff Sharkey3a844fc2011-08-16 14:37:57 -070027import android.os.Build.VERSION_CODES;
Robert Greenwalta848c1c2014-09-30 16:50:07 -070028import android.os.Bundle;
Robert Greenwalt9258c642014-03-26 16:47:06 -070029import android.os.Handler;
30import android.os.HandlerThread;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080031import android.os.IBinder;
32import android.os.INetworkActivityListener;
33import android.os.INetworkManagementService;
Robert Greenwalt9258c642014-03-26 16:47:06 -070034import android.os.Looper;
35import android.os.Message;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -070036import android.os.Messenger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080037import android.os.RemoteException;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080038import android.os.ServiceManager;
Jeff Sharkey961e3042011-08-29 16:02:57 -070039import android.provider.Settings;
Wink Saville36ffb042014-12-05 11:10:30 -080040import android.telephony.SubscriptionManager;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070041import android.telephony.TelephonyManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080042import android.util.ArrayMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -070043import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080044
Robert Greenwaltafa05c02014-05-21 20:04:36 -070045import com.android.internal.telephony.ITelephony;
Robert Greenwalt562cc542014-05-15 18:07:26 -070046import com.android.internal.telephony.PhoneConstants;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070047import com.android.internal.util.Protocol;
48
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070049import java.net.InetAddress;
Robert Greenwalt9258c642014-03-26 16:47:06 -070050import java.util.concurrent.atomic.AtomicInteger;
51import java.util.HashMap;
52
Paul Jensenc91b5342014-08-27 12:38:45 -040053import libcore.net.event.NetworkEventDispatcher;
54
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080055/**
56 * Class that answers queries about the state of network connectivity. It also
57 * notifies applications when network connectivity changes. Get an instance
58 * of this class by calling
59 * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}.
60 * <p>
61 * The primary responsibilities of this class are to:
62 * <ol>
63 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
64 * <li>Send broadcast intents when network connectivity changes</li>
65 * <li>Attempt to "fail over" to another network when connectivity to a network
66 * is lost</li>
67 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
68 * state of the available networks</li>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070069 * <li>Provide an API that allows applications to request and select networks for their data
70 * traffic</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 * </ol>
72 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070073public class ConnectivityManager {
74 private static final String TAG = "ConnectivityManager";
75
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070077 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080078 * been established or lost. The NetworkInfo for the affected network is
79 * sent as an extra; it should be consulted to see what kind of
80 * connectivity event occurred.
81 * <p/>
82 * If this is a connection that was the result of failing over from a
83 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
84 * set to true.
85 * <p/>
86 * For a loss of connectivity, if the connectivity manager is attempting
87 * to connect (or has already connected) to another network, the
88 * NetworkInfo for the new network is also passed as an extra. This lets
89 * any receivers of the broadcast know that they should not necessarily
90 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -080091 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 * the failover attempt succeeded (and so there is still overall data
93 * connectivity), or that the failover attempt failed, meaning that all
94 * connectivity has been lost.
95 * <p/>
96 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
97 * is set to {@code true} if there are no connected networks at all.
98 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -080099 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700101
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800102 /**
Jeff Sharkey961e3042011-08-29 16:02:57 -0700103 * Identical to {@link #CONNECTIVITY_ACTION} broadcast, but sent without any
Erik Kline8f29dcf2014-12-08 16:25:20 +0900104 * historic {@link Settings.Global#CONNECTIVITY_CHANGE_DELAY}.
Jeff Sharkey961e3042011-08-29 16:02:57 -0700105 *
106 * @hide
107 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800108 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey961e3042011-08-29 16:02:57 -0700109 public static final String CONNECTIVITY_ACTION_IMMEDIATE =
110 "android.net.conn.CONNECTIVITY_CHANGE_IMMEDIATE";
111
112 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 * The lookup key for a {@link NetworkInfo} object. Retrieve with
114 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700115 *
116 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
117 * should always obtain network information through
118 * {@link #getActiveNetworkInfo()} or
119 * {@link #getAllNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700120 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800121 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700122 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700124
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800125 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700126 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
127 * Can be used with {@link #getNetworkInfo(int)} to get {@link NetworkInfo}
128 * state based on the calling application.
129 *
130 * @see android.content.Intent#getIntExtra(String, int)
131 */
132 public static final String EXTRA_NETWORK_TYPE = "networkType";
133
134 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800135 * The lookup key for a boolean that indicates whether a connect event
136 * is for a network to which the connectivity manager was failing over
137 * following a disconnect on another network.
138 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
139 */
140 public static final String EXTRA_IS_FAILOVER = "isFailover";
141 /**
142 * The lookup key for a {@link NetworkInfo} object. This is supplied when
143 * there is another network that it may be possible to connect to. Retrieve with
144 * {@link android.content.Intent#getParcelableExtra(String)}.
145 */
146 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
147 /**
148 * The lookup key for a boolean that indicates whether there is a
149 * complete lack of connectivity, i.e., no network is available.
150 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
151 */
152 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
153 /**
154 * The lookup key for a string that indicates why an attempt to connect
155 * to a network failed. The string has no particular structure. It is
156 * intended to be used in notifications presented to users. Retrieve
157 * it with {@link android.content.Intent#getStringExtra(String)}.
158 */
159 public static final String EXTRA_REASON = "reason";
160 /**
161 * The lookup key for a string that provides optionally supplied
162 * extra information about the network state. The information
163 * may be passed up from the lower networking layers, and its
164 * meaning may be specific to a particular network type. Retrieve
165 * it with {@link android.content.Intent#getStringExtra(String)}.
166 */
167 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700168 /**
169 * The lookup key for an int that provides information about
170 * our connection to the internet at large. 0 indicates no connection,
171 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700172 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700173 * {@hide}
174 */
175 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800176
177 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700178 * Broadcast action to indicate the change of data activity status
179 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800180 * The network becomes active when data transmission is started, or
181 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700182 * {@hide}
183 */
184 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
185 public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
186 /**
187 * The lookup key for an enum that indicates the network device type on which this data activity
188 * change happens.
189 * {@hide}
190 */
191 public static final String EXTRA_DEVICE_TYPE = "deviceType";
192 /**
193 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
194 * it is actively sending or receiving data and {@code false} means it is idle.
195 * {@hide}
196 */
197 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700198 /**
199 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
200 * {@hide}
201 */
202 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700203
204 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800205 * Broadcast Action: The setting for background data usage has changed
206 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
207 * <p>
208 * If an application uses the network in the background, it should listen
209 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700210 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800211 * <p>
212 *
213 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
214 * of background data depends on several combined factors, and
215 * this broadcast is no longer sent. Instead, when background
216 * data is unavailable, {@link #getActiveNetworkInfo()} will now
217 * appear disconnected. During first boot after a platform
218 * upgrade, this broadcast will be sent once if
219 * {@link #getBackgroundDataSetting()} was {@code false} before
220 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 */
222 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800223 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800224 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
225 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
226
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700227 /**
228 * Broadcast Action: The network connection may not be good
229 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
230 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
231 * the network and it's condition.
232 * @hide
233 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800234 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700235 public static final String INET_CONDITION_ACTION =
236 "android.net.conn.INET_CONDITION_ACTION";
237
Robert Greenwalt42acef32009-08-12 16:08:25 -0700238 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800239 * Broadcast Action: A tetherable connection has come or gone.
240 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
241 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER} and
242 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
243 * the current state of tethering. Each include a list of
244 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800245 * @hide
246 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800247 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800248 public static final String ACTION_TETHER_STATE_CHANGED =
249 "android.net.conn.TETHER_STATE_CHANGED";
250
251 /**
252 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800253 * gives a String[] listing all the interfaces configured for
254 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800255 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800256 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800257
258 /**
259 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800260 * gives a String[] listing all the interfaces currently tethered
261 * (ie, has dhcp support and packets potentially forwarded/NATed)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800262 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800263 public static final String EXTRA_ACTIVE_TETHER = "activeArray";
264
265 /**
266 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800267 * gives a String[] listing all the interfaces we tried to tether and
268 * failed. Use {@link #getLastTetherError} to find the error code
269 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800270 */
271 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800272
273 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800274 * Broadcast Action: The captive portal tracker has finished its test.
275 * Sent only while running Setup Wizard, in lieu of showing a user
276 * notification.
277 * @hide
278 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800279 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800280 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
281 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
282 /**
283 * The lookup key for a boolean that indicates whether a captive portal was detected.
284 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
285 * @hide
286 */
287 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
288
289 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800290 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700291 * @hide
292 */
293 public static final int TYPE_NONE = -1;
294
295 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800296 * The Mobile data connection. When active, all data traffic
297 * will use this network type's interface by default
298 * (it has a default route)
Robert Greenwalt42acef32009-08-12 16:08:25 -0700299 */
300 public static final int TYPE_MOBILE = 0;
301 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800302 * The WIFI data connection. When active, all data traffic
303 * will use this network type's interface by default
304 * (it has a default route).
Robert Greenwalt42acef32009-08-12 16:08:25 -0700305 */
306 public static final int TYPE_WIFI = 1;
307 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800308 * An MMS-specific Mobile data connection. This network type may use the
309 * same network interface as {@link #TYPE_MOBILE} or it may use a different
310 * one. This is used by applications needing to talk to the carrier's
311 * Multimedia Messaging Service servers.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700312 */
313 public static final int TYPE_MOBILE_MMS = 2;
314 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800315 * A SUPL-specific Mobile data connection. This network type may use the
316 * same network interface as {@link #TYPE_MOBILE} or it may use a different
317 * one. This is used by applications needing to talk to the carrier's
318 * Secure User Plane Location servers for help locating the device.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700319 */
320 public static final int TYPE_MOBILE_SUPL = 3;
321 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800322 * A DUN-specific Mobile data connection. This network type may use the
323 * same network interface as {@link #TYPE_MOBILE} or it may use a different
324 * one. This is sometimes by the system when setting up an upstream connection
325 * for tethering so that the carrier is aware of DUN traffic.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700326 */
327 public static final int TYPE_MOBILE_DUN = 4;
328 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800329 * A High Priority Mobile data connection. This network type uses the
330 * same network interface as {@link #TYPE_MOBILE} but the routing setup
331 * is different. Only requesting processes will have access to the
332 * Mobile DNS servers and only IP's explicitly requested via {@link #requestRouteToHost}
333 * will route over this interface if no default route exists.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700334 */
335 public static final int TYPE_MOBILE_HIPRI = 5;
jsh8214deb2010-03-11 15:04:43 -0800336 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800337 * The WiMAX data connection. When active, all data traffic
338 * will use this network type's interface by default
339 * (it has a default route).
jsh8214deb2010-03-11 15:04:43 -0800340 */
341 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800342
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800343 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800344 * The Bluetooth data connection. When active, all data traffic
345 * will use this network type's interface by default
346 * (it has a default route).
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800347 */
348 public static final int TYPE_BLUETOOTH = 7;
349
Robert Greenwalt60810842011-04-22 15:28:18 -0700350 /**
351 * Dummy data connection. This should not be used on shipping devices.
352 */
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800353 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800354
Robert Greenwalt60810842011-04-22 15:28:18 -0700355 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800356 * The Ethernet data connection. When active, all data traffic
357 * will use this network type's interface by default
358 * (it has a default route).
Robert Greenwalt60810842011-04-22 15:28:18 -0700359 */
Robert Greenwalte12aec92011-01-28 14:48:37 -0800360 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700361
Wink Saville9d7d6282011-03-12 14:52:01 -0800362 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800363 * Over the air Administration.
Wink Saville9d7d6282011-03-12 14:52:01 -0800364 * {@hide}
365 */
366 public static final int TYPE_MOBILE_FOTA = 10;
367
368 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800369 * IP Multimedia Subsystem.
Wink Saville9d7d6282011-03-12 14:52:01 -0800370 * {@hide}
371 */
372 public static final int TYPE_MOBILE_IMS = 11;
373
374 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800375 * Carrier Branded Services.
Wink Saville9d7d6282011-03-12 14:52:01 -0800376 * {@hide}
377 */
378 public static final int TYPE_MOBILE_CBS = 12;
379
repo syncaea743a2011-07-29 23:55:49 -0700380 /**
381 * A Wi-Fi p2p connection. Only requesting processes will have access to
382 * the peers connected.
383 * {@hide}
384 */
385 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800386
Wink Saville5e56bc52013-07-29 15:00:57 -0700387 /**
388 * The network to use for initially attaching to the network
389 * {@hide}
390 */
391 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700392
Ram3e0e3bc2014-06-26 11:03:44 -0700393/**
394 * Emergency PDN connection for emergency calls
395 * {@hide}
396 */
397 public static final int TYPE_MOBILE_EMERGENCY = 15;
398
Hui Lu1c5624a2014-01-15 11:05:36 -0500399 /**
400 * The network that uses proxy to achieve connectivity.
401 * {@hide}
402 */
403 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700404
Robert Greenwalt8283f882014-07-07 17:09:01 -0700405 /**
406 * A virtual network using one or more native bearers.
407 * It may or may not be providing security services.
408 */
409 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500410
411 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700412 public static final int MAX_RADIO_TYPE = TYPE_VPN;
413
414 /** {@hide} */
415 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800416
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800417 /**
418 * If you want to set the default network preference,you can directly
419 * change the networkAttributes array in framework's config.xml.
420 *
421 * @deprecated Since we support so many more networks now, the single
422 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800423 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800424 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800425 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800426 * from an App.
427 */
428 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
430
Jeff Sharkey625239a2012-09-26 22:03:49 -0700431 /**
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700432 * @hide
433 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700434 public final static int REQUEST_ID_UNSET = 0;
435
Paul Jensen5d59e782014-07-11 12:28:19 -0400436 /**
437 * A NetID indicating no Network is selected.
438 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
439 * @hide
440 */
441 public static final int NETID_UNSET = 0;
442
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700443 private final IConnectivityManager mService;
Paul Jensene0bef712014-12-10 15:12:18 -0500444 /**
445 * A kludge to facilitate static access where a Context pointer isn't available, like in the
446 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
447 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
448 * methods that take a Context argument.
449 */
450 private static ConnectivityManager sInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800452 private INetworkManagementService mNMService;
453
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800454 /**
455 * Tests if a given integer represents a valid network type.
456 * @param networkType the type to be tested
457 * @return a boolean. {@code true} if the type is valid, else {@code false}
458 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700459 public static boolean isNetworkTypeValid(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700460 return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800461 }
462
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800463 /**
464 * Returns a non-localized string representing a given network type.
465 * ONLY used for debugging output.
466 * @param type the type needing naming
467 * @return a String for the given type, or a string version of the type ("87")
468 * if no name is known.
469 * {@hide}
470 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700471 public static String getNetworkTypeName(int type) {
472 switch (type) {
473 case TYPE_MOBILE:
474 return "MOBILE";
475 case TYPE_WIFI:
476 return "WIFI";
477 case TYPE_MOBILE_MMS:
478 return "MOBILE_MMS";
479 case TYPE_MOBILE_SUPL:
480 return "MOBILE_SUPL";
481 case TYPE_MOBILE_DUN:
482 return "MOBILE_DUN";
483 case TYPE_MOBILE_HIPRI:
484 return "MOBILE_HIPRI";
485 case TYPE_WIMAX:
486 return "WIMAX";
487 case TYPE_BLUETOOTH:
488 return "BLUETOOTH";
489 case TYPE_DUMMY:
490 return "DUMMY";
491 case TYPE_ETHERNET:
492 return "ETHERNET";
493 case TYPE_MOBILE_FOTA:
494 return "MOBILE_FOTA";
495 case TYPE_MOBILE_IMS:
496 return "MOBILE_IMS";
497 case TYPE_MOBILE_CBS:
498 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700499 case TYPE_WIFI_P2P:
500 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700501 case TYPE_MOBILE_IA:
502 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700503 case TYPE_MOBILE_EMERGENCY:
504 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500505 case TYPE_PROXY:
506 return "PROXY";
Erik Kline37fbfa12014-11-19 17:23:41 +0900507 case TYPE_VPN:
508 return "VPN";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700509 default:
510 return Integer.toString(type);
511 }
512 }
513
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800514 /**
515 * Checks if a given type uses the cellular data connection.
516 * This should be replaced in the future by a network property.
517 * @param networkType the type to check
518 * @return a boolean - {@code true} if uses cellular network, else {@code false}
519 * {@hide}
520 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700521 public static boolean isNetworkTypeMobile(int networkType) {
522 switch (networkType) {
523 case TYPE_MOBILE:
524 case TYPE_MOBILE_MMS:
525 case TYPE_MOBILE_SUPL:
526 case TYPE_MOBILE_DUN:
527 case TYPE_MOBILE_HIPRI:
528 case TYPE_MOBILE_FOTA:
529 case TYPE_MOBILE_IMS:
530 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700531 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700532 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700533 return true;
534 default:
535 return false;
536 }
537 }
538
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800539 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700540 * Checks if the given network type is backed by a Wi-Fi radio.
541 *
542 * @hide
543 */
544 public static boolean isNetworkTypeWifi(int networkType) {
545 switch (networkType) {
546 case TYPE_WIFI:
547 case TYPE_WIFI_P2P:
548 return true;
549 default:
550 return false;
551 }
552 }
553
554 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800555 * Specifies the preferred network type. When the device has more
556 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800557 *
558 * @param preference the network type to prefer over all others. It is
559 * unspecified what happens to the old preferred network in the
560 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700561 * @deprecated Functionality has been removed as it no longer makes sense,
562 * with many more than two networks - we'd need an array to express
563 * preference. Instead we use dynamic network properties of
564 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800565 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800566 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800567 }
568
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800569 /**
570 * Retrieves the current preferred network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800571 *
572 * @return an integer representing the preferred network type
573 *
574 * <p>This method requires the caller to hold the permission
575 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700576 * @deprecated Functionality has been removed as it no longer makes sense,
577 * with many more than two networks - we'd need an array to express
578 * preference. Instead we use dynamic network properties of
579 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800580 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800581 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700582 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 }
584
Scott Main671644c2011-10-06 19:02:28 -0700585 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800586 * Returns details about the currently active default data network. When
587 * connected, this network is the default route for outgoing connections.
588 * You should always check {@link NetworkInfo#isConnected()} before initiating
589 * network traffic. This may return {@code null} when there is no default
590 * network.
591 *
592 * @return a {@link NetworkInfo} object for the current default network
593 * or {@code null} if no network default network is currently active
594 *
595 * <p>This method requires the call to hold the permission
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700596 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700597 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800598 public NetworkInfo getActiveNetworkInfo() {
599 try {
600 return mService.getActiveNetworkInfo();
601 } catch (RemoteException e) {
602 return null;
603 }
604 }
605
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800606 /**
607 * Returns details about the currently active default data network
608 * for a given uid. This is for internal use only to avoid spying
609 * other apps.
610 *
611 * @return a {@link NetworkInfo} object for the current default network
612 * for the given uid or {@code null} if no default network is
613 * available for the specified uid.
614 *
615 * <p>This method requires the caller to hold the permission
616 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
617 * {@hide}
618 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700619 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
620 try {
621 return mService.getActiveNetworkInfoForUid(uid);
622 } catch (RemoteException e) {
623 return null;
624 }
625 }
626
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800627 /**
628 * Returns connection status information about a particular
629 * network type.
630 *
631 * @param networkType integer specifying which networkType in
632 * which you're interested.
633 * @return a {@link NetworkInfo} object for the requested
634 * network type or {@code null} if the type is not
635 * supported by the device.
636 *
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700637 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800638 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
639 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800640 public NetworkInfo getNetworkInfo(int networkType) {
641 try {
642 return mService.getNetworkInfo(networkType);
643 } catch (RemoteException e) {
644 return null;
645 }
646 }
647
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800648 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700649 * Returns connection status information about a particular
650 * Network.
651 *
652 * @param network {@link Network} specifying which network
653 * in which you're interested.
654 * @return a {@link NetworkInfo} object for the requested
655 * network or {@code null} if the {@code Network}
656 * is not valid.
657 *
658 * <p>This method requires the caller to hold the permission
659 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
660 */
661 public NetworkInfo getNetworkInfo(Network network) {
662 try {
663 return mService.getNetworkInfoForNetwork(network);
664 } catch (RemoteException e) {
665 return null;
666 }
667 }
668
669 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800670 * Returns connection status information about all network
671 * types supported by the device.
672 *
673 * @return an array of {@link NetworkInfo} objects. Check each
674 * {@link NetworkInfo#getType} for which type each applies.
675 *
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700676 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800677 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
678 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800679 public NetworkInfo[] getAllNetworkInfo() {
680 try {
681 return mService.getAllNetworkInfo();
682 } catch (RemoteException e) {
683 return null;
684 }
685 }
686
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800687 /**
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700688 * Returns the {@link Network} object currently serving a given type, or
689 * null if the given type is not connected.
690 *
691 * <p>This method requires the caller to hold the permission
692 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
693 *
694 * @hide
695 */
696 public Network getNetworkForType(int networkType) {
697 try {
698 return mService.getNetworkForType(networkType);
699 } catch (RemoteException e) {
700 return null;
701 }
702 }
703
704 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700705 * Returns an array of all {@link Network} currently tracked by the
706 * framework.
707 *
708 * @return an array of {@link Network} objects.
709 *
710 * <p>This method requires the caller to hold the permission
711 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
712 */
713 public Network[] getAllNetworks() {
714 try {
715 return mService.getAllNetworks();
716 } catch (RemoteException e) {
717 return null;
718 }
719 }
720
721 /**
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900722 * Returns an array of of {@link NetworkCapabilities} objects, representing
723 * the Networks that applications run by the given user will use by default.
724 * @hide
725 */
726 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
727 try {
728 return mService.getDefaultNetworkCapabilitiesForUser(userId);
729 } catch (RemoteException e) {
730 return null;
731 }
732 }
733
734 /**
Wink Saville948282b2013-08-29 08:55:16 -0700735 * Returns details about the Provisioning or currently active default data network. When
736 * connected, this network is the default route for outgoing connections.
737 * You should always check {@link NetworkInfo#isConnected()} before initiating
738 * network traffic. This may return {@code null} when there is no default
739 * network.
740 *
741 * @return a {@link NetworkInfo} object for the current default network
742 * or {@code null} if no network default network is currently active
743 *
744 * <p>This method requires the call to hold the permission
745 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
746 *
747 * {@hide}
748 */
749 public NetworkInfo getProvisioningOrActiveNetworkInfo() {
750 try {
751 return mService.getProvisioningOrActiveNetworkInfo();
752 } catch (RemoteException e) {
753 return null;
754 }
755 }
756
757 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800758 * Returns the IP information for the current default network.
759 *
760 * @return a {@link LinkProperties} object describing the IP info
761 * for the current default network, or {@code null} if there
762 * is no current default network.
763 *
764 * <p>This method requires the call to hold the permission
765 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
766 * {@hide}
767 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700768 public LinkProperties getActiveLinkProperties() {
769 try {
770 return mService.getActiveLinkProperties();
771 } catch (RemoteException e) {
772 return null;
773 }
774 }
775
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800776 /**
777 * Returns the IP information for a given network type.
778 *
779 * @param networkType the network type of interest.
780 * @return a {@link LinkProperties} object describing the IP info
781 * for the given networkType, or {@code null} if there is
782 * no current default network.
783 *
784 * <p>This method requires the call to hold the permission
785 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
786 * {@hide}
787 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700788 public LinkProperties getLinkProperties(int networkType) {
789 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -0700790 return mService.getLinkPropertiesForType(networkType);
791 } catch (RemoteException e) {
792 return null;
793 }
794 }
795
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700796 /**
797 * Get the {@link LinkProperties} for the given {@link Network}. This
798 * will return {@code null} if the network is unknown.
799 *
800 * @param network The {@link Network} object identifying the network in question.
801 * @return The {@link LinkProperties} for the network, or {@code null}.
802 **/
Robert Greenwalt9258c642014-03-26 16:47:06 -0700803 public LinkProperties getLinkProperties(Network network) {
804 try {
805 return mService.getLinkProperties(network);
806 } catch (RemoteException e) {
807 return null;
808 }
809 }
810
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700811 /**
812 * Get the {@link NetworkCapabilities} for the given {@link Network}. This
813 * will return {@code null} if the network is unknown.
814 *
815 * @param network The {@link Network} object identifying the network in question.
816 * @return The {@link NetworkCapabilities} for the network, or {@code null}.
817 */
Robert Greenwalt9258c642014-03-26 16:47:06 -0700818 public NetworkCapabilities getNetworkCapabilities(Network network) {
819 try {
820 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700821 } catch (RemoteException e) {
822 return null;
823 }
824 }
825
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800826 /**
827 * Tells each network type to set its radio power state as directed.
828 *
829 * @param turnOn a boolean, {@code true} to turn the radios on,
830 * {@code false} to turn them off.
831 * @return a boolean, {@code true} indicating success. All network types
832 * will be tried, even if some fail.
833 *
834 * <p>This method requires the call to hold the permission
835 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
836 * {@hide}
837 */
Robert Greenwalt7b816022014-04-18 15:25:25 -0700838// TODO - check for any callers and remove
839// public boolean setRadios(boolean turnOn) {
840// try {
841// return mService.setRadios(turnOn);
842// } catch (RemoteException e) {
843// return false;
844// }
845// }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800846
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800847 /**
848 * Tells a given networkType to set its radio power state as directed.
849 *
850 * @param networkType the int networkType of interest.
851 * @param turnOn a boolean, {@code true} to turn the radio on,
852 * {@code} false to turn it off.
853 * @return a boolean, {@code true} indicating success.
854 *
855 * <p>This method requires the call to hold the permission
856 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
857 * {@hide}
858 */
Robert Greenwalt7b816022014-04-18 15:25:25 -0700859// TODO - check for any callers and remove
860// public boolean setRadio(int networkType, boolean turnOn) {
861// try {
862// return mService.setRadio(networkType, turnOn);
863// } catch (RemoteException e) {
864// return false;
865// }
866// }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800867
868 /**
869 * Tells the underlying networking system that the caller wants to
870 * begin using the named feature. The interpretation of {@code feature}
871 * is completely up to each networking implementation.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700872 * <p>This method requires the caller to hold the permission
873 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800874 * @param networkType specifies which network the request pertains to
875 * @param feature the name of the feature to be used
876 * @return an integer value representing the outcome of the request.
877 * The interpretation of this value is specific to each networking
878 * implementation+feature combination, except that the value {@code -1}
879 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700880 *
881 * @deprecated Deprecated in favor of the cleaner {@link #requestNetwork} api.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800882 */
883 public int startUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700884 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
885 if (netCap == null) {
886 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
887 feature);
888 return PhoneConstants.APN_REQUEST_FAILED;
889 }
890
891 NetworkRequest request = null;
892 synchronized (sLegacyRequests) {
893 LegacyRequest l = sLegacyRequests.get(netCap);
894 if (l != null) {
895 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
896 renewRequestLocked(l);
897 if (l.currentNetwork != null) {
898 return PhoneConstants.APN_ALREADY_ACTIVE;
899 } else {
900 return PhoneConstants.APN_REQUEST_STARTED;
901 }
902 }
903
904 request = requestNetworkForFeatureLocked(netCap);
905 }
906 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -0700907 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -0700908 return PhoneConstants.APN_REQUEST_STARTED;
909 } else {
910 Log.d(TAG, " request Failed");
911 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 }
913 }
914
915 /**
916 * Tells the underlying networking system that the caller is finished
917 * using the named feature. The interpretation of {@code feature}
918 * is completely up to each networking implementation.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700919 * <p>This method requires the caller to hold the permission
920 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800921 * @param networkType specifies which network the request pertains to
922 * @param feature the name of the feature that is no longer needed
923 * @return an integer value representing the outcome of the request.
924 * The interpretation of this value is specific to each networking
925 * implementation+feature combination, except that the value {@code -1}
926 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700927 *
928 * @deprecated Deprecated in favor of the cleaner {@link #requestNetwork} api.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800929 */
930 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700931 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
932 if (netCap == null) {
933 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
934 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800935 return -1;
936 }
Robert Greenwalt562cc542014-05-15 18:07:26 -0700937
Paul Jensen9ffb53c2014-12-17 10:39:34 -0500938 if (removeRequestForFeature(netCap)) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700939 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt562cc542014-05-15 18:07:26 -0700940 }
941 return 1;
942 }
943
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900944 /**
945 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability from the given
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900946 * NetworkCapabilities object if all the capabilities it provides are
947 * typically provided by restricted networks.
948 *
949 * TODO: consider:
950 * - Moving to NetworkCapabilities
951 * - Renaming it to guessRestrictedCapability and make it set the
952 * restricted capability bit in addition to clearing it.
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900953 * @hide
954 */
955 public static void maybeMarkCapabilitiesRestricted(NetworkCapabilities nc) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700956 for (int capability : nc.getCapabilities()) {
957 switch (capability) {
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900958 case NetworkCapabilities.NET_CAPABILITY_CBS:
959 case NetworkCapabilities.NET_CAPABILITY_DUN:
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900960 case NetworkCapabilities.NET_CAPABILITY_EIMS:
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900961 case NetworkCapabilities.NET_CAPABILITY_FOTA:
962 case NetworkCapabilities.NET_CAPABILITY_IA:
963 case NetworkCapabilities.NET_CAPABILITY_IMS:
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900964 case NetworkCapabilities.NET_CAPABILITY_RCS:
965 case NetworkCapabilities.NET_CAPABILITY_XCAP:
Robert Greenwalt9ac3dbe2014-06-05 16:39:28 -0700966 case NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED: //there by default
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900967 continue;
968 default:
969 // At least one capability usually provided by unrestricted
970 // networks. Conclude that this network is unrestricted.
971 return;
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900972 }
973 }
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900974 // All the capabilities are typically provided by restricted networks.
975 // Conclude that this network is restricted.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700976 nc.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900977 }
978
Robert Greenwalt562cc542014-05-15 18:07:26 -0700979 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
980 if (networkType == TYPE_MOBILE) {
981 int cap = -1;
982 if ("enableMMS".equals(feature)) {
983 cap = NetworkCapabilities.NET_CAPABILITY_MMS;
984 } else if ("enableSUPL".equals(feature)) {
985 cap = NetworkCapabilities.NET_CAPABILITY_SUPL;
986 } else if ("enableDUN".equals(feature) || "enableDUNAlways".equals(feature)) {
987 cap = NetworkCapabilities.NET_CAPABILITY_DUN;
988 } else if ("enableHIPRI".equals(feature)) {
989 cap = NetworkCapabilities.NET_CAPABILITY_INTERNET;
990 } else if ("enableFOTA".equals(feature)) {
991 cap = NetworkCapabilities.NET_CAPABILITY_FOTA;
992 } else if ("enableIMS".equals(feature)) {
993 cap = NetworkCapabilities.NET_CAPABILITY_IMS;
994 } else if ("enableCBS".equals(feature)) {
995 cap = NetworkCapabilities.NET_CAPABILITY_CBS;
996 } else {
997 return null;
998 }
999 NetworkCapabilities netCap = new NetworkCapabilities();
Robert Greenwalt7569f182014-06-08 16:42:59 -07001000 netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(cap);
Lorenzo Colitti4077acb2014-06-04 12:20:06 +09001001 maybeMarkCapabilitiesRestricted(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001002 return netCap;
1003 } else if (networkType == TYPE_WIFI) {
1004 if ("p2p".equals(feature)) {
1005 NetworkCapabilities netCap = new NetworkCapabilities();
1006 netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
Robert Greenwalt7569f182014-06-08 16:42:59 -07001007 netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
Lorenzo Colitti4077acb2014-06-04 12:20:06 +09001008 maybeMarkCapabilitiesRestricted(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001009 return netCap;
1010 }
1011 }
1012 return null;
1013 }
1014
Robert Greenwalt06314e42014-10-29 14:04:06 -07001015 /**
1016 * Guess what the network request was trying to say so that the resulting
1017 * network is accessible via the legacy (deprecated) API such as
1018 * requestRouteToHost.
1019 * This means we should try to be fairly preceise about transport and
1020 * capability but ignore things such as networkSpecifier.
1021 * If the request has more than one transport or capability it doesn't
1022 * match the old legacy requests (they selected only single transport/capability)
1023 * so this function cannot map the request to a single legacy type and
1024 * the resulting network will not be available to the legacy APIs.
1025 *
1026 * TODO - This should be removed when the legacy APIs are removed.
1027 */
Ye Wenb87875e2014-07-21 14:19:01 -07001028 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1029 if (netCap == null) {
1030 return TYPE_NONE;
1031 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001032
Ye Wenb87875e2014-07-21 14:19:01 -07001033 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1034 return TYPE_NONE;
1035 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001036
1037 String type = null;
1038 int result = TYPE_NONE;
1039
Ye Wenb87875e2014-07-21 14:19:01 -07001040 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
Robert Greenwalt06314e42014-10-29 14:04:06 -07001041 type = "enableCBS";
1042 result = TYPE_MOBILE_CBS;
1043 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1044 type = "enableIMS";
1045 result = TYPE_MOBILE_IMS;
1046 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1047 type = "enableFOTA";
1048 result = TYPE_MOBILE_FOTA;
1049 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1050 type = "enableDUN";
1051 result = TYPE_MOBILE_DUN;
1052 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1053 type = "enableSUPL";
1054 result = TYPE_MOBILE_SUPL;
1055 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1056 type = "enableMMS";
1057 result = TYPE_MOBILE_MMS;
1058 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1059 type = "enableHIPRI";
1060 result = TYPE_MOBILE_HIPRI;
Ye Wenb87875e2014-07-21 14:19:01 -07001061 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001062 if (type != null) {
1063 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1064 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1065 return result;
Ye Wenb87875e2014-07-21 14:19:01 -07001066 }
1067 }
1068 return TYPE_NONE;
1069 }
1070
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001071 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001072 if (netCap == null) return TYPE_NONE;
1073 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1074 return TYPE_MOBILE_CBS;
1075 }
1076 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1077 return TYPE_MOBILE_IMS;
1078 }
1079 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1080 return TYPE_MOBILE_FOTA;
1081 }
1082 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1083 return TYPE_MOBILE_DUN;
1084 }
1085 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1086 return TYPE_MOBILE_SUPL;
1087 }
1088 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1089 return TYPE_MOBILE_MMS;
1090 }
1091 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1092 return TYPE_MOBILE_HIPRI;
1093 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001094 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1095 return TYPE_WIFI_P2P;
1096 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001097 return TYPE_NONE;
1098 }
1099
1100 private static class LegacyRequest {
1101 NetworkCapabilities networkCapabilities;
1102 NetworkRequest networkRequest;
1103 int expireSequenceNumber;
1104 Network currentNetwork;
1105 int delay = -1;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001106
1107 private void clearDnsBinding() {
1108 if (currentNetwork != null) {
1109 currentNetwork = null;
1110 setProcessDefaultNetworkForHostResolution(null);
1111 }
1112 }
1113
Robert Greenwalt6078b502014-06-11 16:05:07 -07001114 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001115 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001116 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001117 currentNetwork = network;
1118 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001119 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001120 }
1121 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001122 public void onLost(Network network) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001123 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001124 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1125 }
1126 };
1127 }
1128
Robert Greenwaltfab501672014-07-23 11:44:01 -07001129 private static HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
Robert Greenwalt562cc542014-05-15 18:07:26 -07001130 new HashMap<NetworkCapabilities, LegacyRequest>();
1131
1132 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1133 synchronized (sLegacyRequests) {
1134 LegacyRequest l = sLegacyRequests.get(netCap);
1135 if (l != null) return l.networkRequest;
1136 }
1137 return null;
1138 }
1139
1140 private void renewRequestLocked(LegacyRequest l) {
1141 l.expireSequenceNumber++;
1142 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1143 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1144 }
1145
1146 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1147 int ourSeqNum = -1;
1148 synchronized (sLegacyRequests) {
1149 LegacyRequest l = sLegacyRequests.get(netCap);
1150 if (l == null) return;
1151 ourSeqNum = l.expireSequenceNumber;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001152 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001153 }
1154 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1155 }
1156
1157 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1158 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001159 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001160 try {
1161 delay = mService.getRestoreDefaultNetworkDelay(type);
1162 } catch (RemoteException e) {}
1163 LegacyRequest l = new LegacyRequest();
1164 l.networkCapabilities = netCap;
1165 l.delay = delay;
1166 l.expireSequenceNumber = 0;
Robert Greenwalt6078b502014-06-11 16:05:07 -07001167 l.networkRequest = sendRequestForNetwork(netCap, l.networkCallback, 0,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001168 REQUEST, type);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001169 if (l.networkRequest == null) return null;
1170 sLegacyRequests.put(netCap, l);
1171 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1172 return l.networkRequest;
1173 }
1174
1175 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1176 if (delay >= 0) {
1177 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
1178 Message msg = sCallbackHandler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1179 sCallbackHandler.sendMessageDelayed(msg, delay);
1180 }
1181 }
1182
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001183 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1184 final LegacyRequest l;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001185 synchronized (sLegacyRequests) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001186 l = sLegacyRequests.remove(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001187 }
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001188 if (l == null) return false;
1189 unregisterNetworkCallback(l.networkCallback);
1190 l.clearDnsBinding();
1191 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001192 }
1193
1194 /**
1195 * Ensure that a network route exists to deliver traffic to the specified
1196 * host via the specified network interface. An attempt to add a route that
1197 * already exists is ignored, but treated as successful.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001198 * <p>This method requires the caller to hold the permission
1199 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001200 * @param networkType the type of the network over which traffic to the specified
1201 * host is to be routed
1202 * @param hostAddress the IP address of the host to which the route is desired
1203 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001204 *
1205 * @deprecated Deprecated in favor of the {@link #requestNetwork},
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001206 * {@link #setProcessDefaultNetwork} and {@link Network#getSocketFactory} api.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001207 */
1208 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001209 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001210 }
1211
1212 /**
1213 * Ensure that a network route exists to deliver traffic to the specified
1214 * host via the specified network interface. An attempt to add a route that
1215 * already exists is ignored, but treated as successful.
Jake Hamby8f9b33e2014-01-15 13:08:03 -08001216 * <p>This method requires the caller to hold the permission
1217 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001218 * @param networkType the type of the network over which traffic to the specified
1219 * host is to be routed
1220 * @param hostAddress the IP address of the host to which the route is desired
1221 * @return {@code true} on success, {@code false} on failure
1222 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001223 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001224 * {@link #setProcessDefaultNetwork} api.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001225 */
1226 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001227 try {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001228 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001229 } catch (RemoteException e) {
1230 return false;
1231 }
1232 }
1233
1234 /**
1235 * Returns the value of the setting for background data usage. If false,
1236 * applications should not use the network if the application is not in the
1237 * foreground. Developers should respect this setting, and check the value
1238 * of this before performing any background data operations.
1239 * <p>
1240 * All applications that have background services that use the network
1241 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001242 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001243 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001244 * background data depends on several combined factors, and this method will
1245 * always return {@code true}. Instead, when background data is unavailable,
1246 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001247 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001248 * @return Whether background data usage is allowed.
1249 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001250 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001251 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001252 // assume that background data is allowed; final authority is
1253 // NetworkInfo which may be blocked.
1254 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001255 }
1256
1257 /**
1258 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001259 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001260 * @param allowBackgroundData Whether an application should use data while
1261 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001262 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001263 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1264 * @see #getBackgroundDataSetting()
1265 * @hide
1266 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001267 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001268 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001269 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001270 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001271
1272 /**
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001273 * Return quota status for the current active network, or {@code null} if no
1274 * network is active. Quota status can change rapidly, so these values
1275 * shouldn't be cached.
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001276 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001277 * <p>This method requires the call to hold the permission
1278 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1279 *
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001280 * @hide
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001281 */
1282 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1283 try {
1284 return mService.getActiveNetworkQuotaInfo();
1285 } catch (RemoteException e) {
1286 return null;
1287 }
1288 }
1289
1290 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001291 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001292 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001293 */
1294 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001295 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1296 if (b != null) {
1297 try {
1298 ITelephony it = ITelephony.Stub.asInterface(b);
Wink Saville36ffb042014-12-05 11:10:30 -08001299 int subId = SubscriptionManager.getDefaultDataSubId();
1300 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
1301 boolean retVal = it.getDataEnabled(subId);
1302 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1303 + " retVal=" + retVal);
1304 return retVal;
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001305 } catch (RemoteException e) { }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001306 }
Wink Saville36ffb042014-12-05 11:10:30 -08001307 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001308 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001309 }
1310
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001311 /**
Robert Greenwaltb2489872014-09-04 16:44:35 -07001312 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt6078b502014-06-11 16:05:07 -07001313 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001314 */
1315 public interface OnNetworkActiveListener {
1316 /**
1317 * Called on the main thread of the process to report that the current data network
1318 * has become active, and it is now a good time to perform any pending network
1319 * operations. Note that this listener only tells you when the network becomes
1320 * active; if at any other time you want to know whether it is active (and thus okay
1321 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001322 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001323 */
1324 public void onNetworkActive();
1325 }
1326
1327 private INetworkManagementService getNetworkManagementService() {
1328 synchronized (this) {
1329 if (mNMService != null) {
1330 return mNMService;
1331 }
1332 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1333 mNMService = INetworkManagementService.Stub.asInterface(b);
1334 return mNMService;
1335 }
1336 }
1337
1338 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
1339 mNetworkActivityListeners
1340 = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>();
1341
1342 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001343 * Start listening to reports when the system's default data network is active, meaning it is
1344 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1345 * to determine the current state of the system's default network after registering the
1346 * listener.
1347 * <p>
1348 * If the process default network has been set with
1349 * {@link ConnectivityManager#setProcessDefaultNetwork} this function will not
1350 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001351 *
1352 * @param l The listener to be told when the network is active.
1353 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001354 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001355 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1356 @Override
1357 public void onNetworkActive() throws RemoteException {
1358 l.onNetworkActive();
1359 }
1360 };
1361
1362 try {
1363 getNetworkManagementService().registerNetworkActivityListener(rl);
1364 mNetworkActivityListeners.put(l, rl);
1365 } catch (RemoteException e) {
1366 }
1367 }
1368
1369 /**
1370 * Remove network active listener previously registered with
Robert Greenwaltb2489872014-09-04 16:44:35 -07001371 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001372 *
1373 * @param l Previously registered listener.
1374 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001375 public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001376 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
1377 if (rl == null) {
1378 throw new IllegalArgumentException("Listener not registered: " + l);
1379 }
1380 try {
1381 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1382 } catch (RemoteException e) {
1383 }
1384 }
1385
1386 /**
1387 * Return whether the data network is currently active. An active network means that
1388 * it is currently in a high power state for performing data transmission. On some
1389 * types of networks, it may be expensive to move and stay in such a state, so it is
1390 * more power efficient to batch network traffic together when the radio is already in
1391 * this state. This method tells you whether right now is currently a good time to
1392 * initiate network traffic, as the network is already active.
1393 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001394 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001395 try {
1396 return getNetworkManagementService().isNetworkActive();
1397 } catch (RemoteException e) {
1398 }
1399 return false;
1400 }
1401
1402 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001403 * {@hide}
1404 */
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001405 public ConnectivityManager(IConnectivityManager service) {
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001406 mService = checkNotNull(service, "missing IConnectivityManager");
Paul Jensene0bef712014-12-10 15:12:18 -05001407 sInstance = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001408 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001409
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07001410 /** {@hide} */
1411 public static ConnectivityManager from(Context context) {
1412 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1413 }
1414
Robert Greenwaltedb47662014-09-16 17:54:19 -07001415 /** {@hide */
1416 public static final void enforceTetherChangePermission(Context context) {
1417 if (context.getResources().getStringArray(
1418 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
1419 // Have a provisioning app - must only let system apps (which check this app)
1420 // turn on tethering
1421 context.enforceCallingOrSelfPermission(
1422 android.Manifest.permission.CONNECTIVITY_INTERNAL, "ConnectivityService");
1423 } else {
1424 context.enforceCallingOrSelfPermission(
1425 android.Manifest.permission.CHANGE_NETWORK_STATE, "ConnectivityService");
1426 }
1427 }
1428
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001429 /**
Paul Jensene0bef712014-12-10 15:12:18 -05001430 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1431 * situations where a Context pointer is unavailable.
1432 * @hide
1433 */
1434 public static ConnectivityManager getInstance() {
1435 if (sInstance == null) {
1436 throw new IllegalStateException("No ConnectivityManager yet constructed");
1437 }
1438 return sInstance;
1439 }
1440
1441 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001442 * Get the set of tetherable, available interfaces. This list is limited by
1443 * device configuration and current interface existence.
1444 *
1445 * @return an array of 0 or more Strings of tetherable interface names.
1446 *
1447 * <p>This method requires the call to hold the permission
1448 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001449 * {@hide}
1450 */
1451 public String[] getTetherableIfaces() {
1452 try {
1453 return mService.getTetherableIfaces();
1454 } catch (RemoteException e) {
1455 return new String[0];
1456 }
1457 }
1458
1459 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001460 * Get the set of tethered interfaces.
1461 *
1462 * @return an array of 0 or more String of currently tethered interface names.
1463 *
1464 * <p>This method requires the call to hold the permission
1465 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001466 * {@hide}
1467 */
1468 public String[] getTetheredIfaces() {
1469 try {
1470 return mService.getTetheredIfaces();
1471 } catch (RemoteException e) {
1472 return new String[0];
1473 }
1474 }
1475
1476 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001477 * Get the set of interface names which attempted to tether but
1478 * failed. Re-attempting to tether may cause them to reset to the Tethered
1479 * state. Alternatively, causing the interface to be destroyed and recreated
1480 * may cause them to reset to the available state.
1481 * {@link ConnectivityManager#getLastTetherError} can be used to get more
1482 * information on the cause of the errors.
1483 *
1484 * @return an array of 0 or more String indicating the interface names
1485 * which failed to tether.
1486 *
1487 * <p>This method requires the call to hold the permission
1488 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001489 * {@hide}
1490 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001491 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001492 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001493 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001494 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001495 return new String[0];
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001496 }
1497 }
1498
1499 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001500 * Get the set of tethered dhcp ranges.
1501 *
1502 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
1503 * {@hide}
1504 */
1505 public String[] getTetheredDhcpRanges() {
1506 try {
1507 return mService.getTetheredDhcpRanges();
1508 } catch (RemoteException e) {
1509 return new String[0];
1510 }
1511 }
1512
1513 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001514 * Attempt to tether the named interface. This will setup a dhcp server
1515 * on the interface, forward and NAT IP packets and forward DNS requests
1516 * to the best active upstream network interface. Note that if no upstream
1517 * IP network interface is available, dhcp will still run and traffic will be
1518 * allowed between the tethered devices and this device, though upstream net
1519 * access will of course fail until an upstream network interface becomes
1520 * active.
1521 *
1522 * @param iface the interface name to tether.
1523 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1524 *
1525 * <p>This method requires the call to hold the permission
1526 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001527 * {@hide}
1528 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001529 public int tether(String iface) {
1530 try {
1531 return mService.tether(iface);
1532 } catch (RemoteException e) {
1533 return TETHER_ERROR_SERVICE_UNAVAIL;
1534 }
1535 }
1536
1537 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001538 * Stop tethering the named interface.
1539 *
1540 * @param iface the interface name to untether.
1541 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1542 *
1543 * <p>This method requires the call to hold the permission
1544 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt5a735062010-03-02 17:25:02 -08001545 * {@hide}
1546 */
1547 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001548 try {
1549 return mService.untether(iface);
1550 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001551 return TETHER_ERROR_SERVICE_UNAVAIL;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001552 }
1553 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001554
1555 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001556 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001557 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001558 * due to device configuration.
1559 *
1560 * @return a boolean - {@code true} indicating Tethering is supported.
1561 *
1562 * <p>This method requires the call to hold the permission
1563 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001564 * {@hide}
1565 */
1566 public boolean isTetheringSupported() {
1567 try {
1568 return mService.isTetheringSupported();
1569 } catch (RemoteException e) {
1570 return false;
1571 }
1572 }
1573
1574 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001575 * Get the list of regular expressions that define any tetherable
1576 * USB network interfaces. If USB tethering is not supported by the
1577 * device, this list should be empty.
1578 *
1579 * @return an array of 0 or more regular expression Strings defining
1580 * what interfaces are considered tetherable usb interfaces.
1581 *
1582 * <p>This method requires the call to hold the permission
1583 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001584 * {@hide}
1585 */
1586 public String[] getTetherableUsbRegexs() {
1587 try {
1588 return mService.getTetherableUsbRegexs();
1589 } catch (RemoteException e) {
1590 return new String[0];
1591 }
1592 }
1593
1594 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001595 * Get the list of regular expressions that define any tetherable
1596 * Wifi network interfaces. If Wifi tethering is not supported by the
1597 * device, this list should be empty.
1598 *
1599 * @return an array of 0 or more regular expression Strings defining
1600 * what interfaces are considered tetherable wifi interfaces.
1601 *
1602 * <p>This method requires the call to hold the permission
1603 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001604 * {@hide}
1605 */
1606 public String[] getTetherableWifiRegexs() {
1607 try {
1608 return mService.getTetherableWifiRegexs();
1609 } catch (RemoteException e) {
1610 return new String[0];
1611 }
1612 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08001613
Danica Chang6fdd0c62010-08-11 14:54:43 -07001614 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001615 * Get the list of regular expressions that define any tetherable
1616 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
1617 * device, this list should be empty.
1618 *
1619 * @return an array of 0 or more regular expression Strings defining
1620 * what interfaces are considered tetherable bluetooth interfaces.
1621 *
1622 * <p>This method requires the call to hold the permission
1623 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001624 * {@hide}
1625 */
1626 public String[] getTetherableBluetoothRegexs() {
1627 try {
1628 return mService.getTetherableBluetoothRegexs();
1629 } catch (RemoteException e) {
1630 return new String[0];
1631 }
1632 }
1633
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001634 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001635 * Attempt to both alter the mode of USB and Tethering of USB. A
1636 * utility method to deal with some of the complexity of USB - will
1637 * attempt to switch to Rndis and subsequently tether the resulting
1638 * interface on {@code true} or turn off tethering and switch off
1639 * Rndis on {@code false}.
1640 *
1641 * @param enable a boolean - {@code true} to enable tethering
1642 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1643 *
1644 * <p>This method requires the call to hold the permission
1645 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001646 * {@hide}
1647 */
1648 public int setUsbTethering(boolean enable) {
1649 try {
1650 return mService.setUsbTethering(enable);
1651 } catch (RemoteException e) {
1652 return TETHER_ERROR_SERVICE_UNAVAIL;
1653 }
1654 }
1655
Robert Greenwalt5a735062010-03-02 17:25:02 -08001656 /** {@hide} */
1657 public static final int TETHER_ERROR_NO_ERROR = 0;
1658 /** {@hide} */
1659 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
1660 /** {@hide} */
1661 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
1662 /** {@hide} */
1663 public static final int TETHER_ERROR_UNSUPPORTED = 3;
1664 /** {@hide} */
1665 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
1666 /** {@hide} */
1667 public static final int TETHER_ERROR_MASTER_ERROR = 5;
1668 /** {@hide} */
1669 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
1670 /** {@hide} */
1671 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
1672 /** {@hide} */
1673 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
1674 /** {@hide} */
1675 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
1676 /** {@hide} */
1677 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
1678
1679 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001680 * Get a more detailed error code after a Tethering or Untethering
1681 * request asynchronously failed.
1682 *
1683 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08001684 * @return error The error code of the last error tethering or untethering the named
1685 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001686 *
1687 * <p>This method requires the call to hold the permission
1688 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a735062010-03-02 17:25:02 -08001689 * {@hide}
1690 */
1691 public int getLastTetherError(String iface) {
1692 try {
1693 return mService.getLastTetherError(iface);
1694 } catch (RemoteException e) {
1695 return TETHER_ERROR_SERVICE_UNAVAIL;
1696 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001697 }
1698
1699 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001700 * Report network connectivity status. This is currently used only
1701 * to alter status bar UI.
1702 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001703 * @param networkType The type of network you want to report on
1704 * @param percentage The quality of the connection 0 is bad, 100 is good
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001705 *
1706 * <p>This method requires the call to hold the permission
1707 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001708 * {@hide}
1709 */
1710 public void reportInetCondition(int networkType, int percentage) {
1711 try {
1712 mService.reportInetCondition(networkType, percentage);
1713 } catch (RemoteException e) {
1714 }
1715 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001716
1717 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001718 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07001719 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001720 * the framework to re-evaluate network connectivity and/or switch to another
1721 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001722 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001723 * @param network The {@link Network} the application was attempting to use
1724 * or {@code null} to indicate the current default network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001725 */
1726 public void reportBadNetwork(Network network) {
1727 try {
1728 mService.reportBadNetwork(network);
1729 } catch (RemoteException e) {
1730 }
1731 }
1732
1733 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001734 * Set a network-independent global http proxy. This is not normally what you want
1735 * for typical HTTP proxies - they are general network dependent. However if you're
1736 * doing something unusual like general internal filtering this may be useful. On
1737 * a private network where the proxy is not accessible, you may break HTTP using this.
1738 *
Jason Monk207900c2014-04-25 15:00:09 -04001739 * @param p The a {@link ProxyInfo} object defining the new global
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001740 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
1741 *
1742 * <p>This method requires the call to hold the permission
Jason Monkfaf3fd52014-05-07 18:41:13 -04001743 * android.Manifest.permission#CONNECTIVITY_INTERNAL.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001744 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07001745 */
Jason Monk207900c2014-04-25 15:00:09 -04001746 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001747 try {
1748 mService.setGlobalProxy(p);
1749 } catch (RemoteException e) {
1750 }
1751 }
1752
1753 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001754 * Retrieve any network-independent global HTTP proxy.
1755 *
Jason Monk207900c2014-04-25 15:00:09 -04001756 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001757 * if no global HTTP proxy is set.
1758 *
1759 * <p>This method requires the call to hold the permission
1760 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001761 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07001762 */
Jason Monk207900c2014-04-25 15:00:09 -04001763 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001764 try {
1765 return mService.getGlobalProxy();
1766 } catch (RemoteException e) {
1767 return null;
1768 }
1769 }
1770
1771 /**
Paul Jensene0bef712014-12-10 15:12:18 -05001772 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
1773 * otherwise if this process is bound to a {@link Network} using
1774 * {@link #setProcessDefaultNetwork} then that {@code Network}'s proxy is returned, otherwise
1775 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001776 *
Jason Monk207900c2014-04-25 15:00:09 -04001777 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001778 * HTTP proxy is active.
Paul Jensene0bef712014-12-10 15:12:18 -05001779 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07001780 */
Paul Jensene0bef712014-12-10 15:12:18 -05001781 public ProxyInfo getDefaultProxy() {
1782 final Network network = getProcessDefaultNetwork();
1783 if (network != null) {
1784 final ProxyInfo globalProxy = getGlobalProxy();
1785 if (globalProxy != null) return globalProxy;
1786 final LinkProperties lp = getLinkProperties(network);
1787 if (lp != null) return lp.getHttpProxy();
1788 return null;
1789 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001790 try {
Paul Jensene0bef712014-12-10 15:12:18 -05001791 return mService.getDefaultProxy();
Robert Greenwalt434203a2010-10-11 16:00:27 -07001792 } catch (RemoteException e) {
1793 return null;
1794 }
1795 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001796
1797 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001798 * Sets a secondary requirement bit for the given networkType.
1799 * This requirement bit is generally under the control of the carrier
1800 * or its agents and is not directly controlled by the user.
1801 *
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001802 * @param networkType The network who's dependence has changed
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001803 * @param met Boolean - true if network use is OK, false if not
1804 *
1805 * <p>This method requires the call to hold the permission
1806 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001807 * {@hide}
1808 */
1809 public void setDataDependency(int networkType, boolean met) {
1810 try {
1811 mService.setDataDependency(networkType, met);
1812 } catch (RemoteException e) {
1813 }
1814 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07001815
1816 /**
1817 * Returns true if the hardware supports the given network type
1818 * else it returns false. This doesn't indicate we have coverage
1819 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001820 * hardware supports it. For example a GSM phone without a SIM
1821 * should still return {@code true} for mobile data, but a wifi only
1822 * tablet would return {@code false}.
1823 *
1824 * @param networkType The network type we'd like to check
1825 * @return {@code true} if supported, else {@code false}
1826 *
1827 * <p>This method requires the call to hold the permission
1828 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07001829 * @hide
1830 */
1831 public boolean isNetworkSupported(int networkType) {
1832 try {
1833 return mService.isNetworkSupported(networkType);
1834 } catch (RemoteException e) {}
1835 return false;
1836 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07001837
1838 /**
1839 * Returns if the currently active data network is metered. A network is
1840 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001841 * that connection due to monetary costs, data limitations or
1842 * battery/performance issues. You should check this before doing large
1843 * data transfers, and warn the user or delay the operation until another
1844 * network is available.
1845 *
1846 * @return {@code true} if large transfers should be avoided, otherwise
1847 * {@code false}.
1848 *
1849 * <p>This method requires the call to hold the permission
1850 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07001851 */
1852 public boolean isActiveNetworkMetered() {
1853 try {
1854 return mService.isActiveNetworkMetered();
1855 } catch (RemoteException e) {
1856 return false;
1857 }
1858 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07001859
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001860 /**
1861 * If the LockdownVpn mechanism is enabled, updates the vpn
1862 * with a reload of its profile.
1863 *
1864 * @return a boolean with {@code} indicating success
1865 *
1866 * <p>This method can only be called by the system UID
1867 * {@hide}
1868 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07001869 public boolean updateLockdownVpn() {
1870 try {
1871 return mService.updateLockdownVpn();
1872 } catch (RemoteException e) {
1873 return false;
1874 }
1875 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07001876
1877 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001878 * Signal that the captive portal check on the indicated network
Wink Savilled747cbc2013-08-07 16:22:47 -07001879 * is complete and whether its a captive portal or not.
1880 *
1881 * @param info the {@link NetworkInfo} object for the networkType
1882 * in question.
1883 * @param isCaptivePortal true/false.
1884 *
1885 * <p>This method requires the call to hold the permission
1886 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
1887 * {@hide}
1888 */
1889 public void captivePortalCheckCompleted(NetworkInfo info, boolean isCaptivePortal) {
1890 try {
1891 mService.captivePortalCheckCompleted(info, isCaptivePortal);
1892 } catch (RemoteException e) {
1893 }
1894 }
1895
1896 /**
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001897 * Supply the backend messenger for a network tracker
1898 *
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001899 * @param networkType NetworkType to set
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001900 * @param messenger {@link Messenger}
1901 * {@hide}
1902 */
1903 public void supplyMessenger(int networkType, Messenger messenger) {
1904 try {
1905 mService.supplyMessenger(networkType, messenger);
1906 } catch (RemoteException e) {
1907 }
1908 }
Wink Savilleab9321d2013-06-29 21:10:57 -07001909
1910 /**
Wink Saville948282b2013-08-29 08:55:16 -07001911 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07001912 *
Wink Savilleab9321d2013-06-29 21:10:57 -07001913 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07001914 *
1915 * @return time out that will be used, maybe less that suggestedTimeOutMs
1916 * -1 if an error.
1917 *
1918 * {@hide}
1919 */
Wink Saville948282b2013-08-29 08:55:16 -07001920 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07001921 int timeOutMs = -1;
1922 try {
Wink Saville948282b2013-08-29 08:55:16 -07001923 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07001924 } catch (RemoteException e) {
1925 }
1926 return timeOutMs;
1927 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07001928
1929 /**
Wink Saville42d4f082013-07-20 20:31:59 -07001930 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07001931 * {@hide}
1932 */
1933 public String getMobileProvisioningUrl() {
1934 try {
1935 return mService.getMobileProvisioningUrl();
1936 } catch (RemoteException e) {
1937 }
1938 return null;
1939 }
Wink Saville42d4f082013-07-20 20:31:59 -07001940
1941 /**
1942 * Get the mobile redirected provisioning url.
1943 * {@hide}
1944 */
1945 public String getMobileRedirectedProvisioningUrl() {
1946 try {
1947 return mService.getMobileRedirectedProvisioningUrl();
1948 } catch (RemoteException e) {
1949 }
1950 return null;
1951 }
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001952
1953 /**
Wink Saville948282b2013-08-29 08:55:16 -07001954 * Set sign in error notification to visible or in visible
1955 *
1956 * @param visible
1957 * @param networkType
1958 *
1959 * {@hide}
1960 */
1961 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04001962 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07001963 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04001964 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07001965 } catch (RemoteException e) {
1966 }
1967 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07001968
1969 /**
1970 * Set the value for enabling/disabling airplane mode
1971 *
1972 * @param enable whether to enable airplane mode or not
1973 *
1974 * <p>This method requires the call to hold the permission
1975 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
1976 * @hide
1977 */
1978 public void setAirplaneMode(boolean enable) {
1979 try {
1980 mService.setAirplaneMode(enable);
1981 } catch (RemoteException e) {
1982 }
1983 }
Robert Greenwalte049c232014-04-11 15:53:27 -07001984
1985 /** {@hide} */
Robert Greenwalta67be032014-05-16 15:49:14 -07001986 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07001987 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07001988 mService.registerNetworkFactory(messenger, name);
1989 } catch (RemoteException e) { }
1990 }
1991
1992 /** {@hide} */
1993 public void unregisterNetworkFactory(Messenger messenger) {
1994 try {
1995 mService.unregisterNetworkFactory(messenger);
Robert Greenwalte049c232014-04-11 15:53:27 -07001996 } catch (RemoteException e) { }
1997 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07001998
1999 /** {@hide} */
2000 public void registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002001 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07002002 try {
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07002003 mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
Robert Greenwalt7b816022014-04-18 15:25:25 -07002004 } catch (RemoteException e) { }
2005 }
2006
Robert Greenwalt9258c642014-03-26 16:47:06 -07002007 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002008 * Base class for NetworkRequest callbacks. Used for notifications about network
2009 * changes. Should be extended by applications wanting notifications.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002010 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002011 public static class NetworkCallback {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002012 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002013 public static final int PRECHECK = 1;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002014 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002015 public static final int AVAILABLE = 2;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002016 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002017 public static final int LOSING = 3;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002018 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002019 public static final int LOST = 4;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002020 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002021 public static final int UNAVAIL = 5;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002022 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002023 public static final int CAP_CHANGED = 6;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002024 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002025 public static final int PROP_CHANGED = 7;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002026 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002027 public static final int CANCELED = 8;
2028
2029 /**
2030 * @hide
2031 * Called whenever the framework connects to a network that it may use to
2032 * satisfy this request
2033 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002034 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002035
2036 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002037 * Called when the framework connects and has declared new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002038 * This callback may be called more than once if the {@link Network} that is
2039 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002040 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002041 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002042 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002043 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002044
2045 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002046 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002047 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002048 * for graceful handover. This may not be called if we have a hard loss
2049 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002050 * {@link NetworkCallback#onLost} call or a
2051 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002052 * on whether we lose or regain it.
2053 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002054 * @param network The {@link Network} that is about to be disconnected.
2055 * @param maxMsToLive The time in ms the framework will attempt to keep the
2056 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002057 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002058 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002059 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002060
2061 /**
2062 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002063 * graceful failure ends.
2064 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002065 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002066 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002067 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002068
2069 /**
2070 * Called if no network is found in the given timeout time. If no timeout is given,
2071 * this will not be called.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002072 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002073 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002074 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002075
2076 /**
2077 * Called when the network the framework connected to for this request
2078 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002079 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002080 * @param network The {@link Network} whose capabilities have changed.
2081 * @param networkCapabilities The new {@link NetworkCapabilities} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002082 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002083 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002084 NetworkCapabilities networkCapabilities) {}
2085
2086 /**
2087 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002088 * changes {@link LinkProperties}.
2089 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002090 * @param network The {@link Network} whose link properties have changed.
2091 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002092 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002093 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002094
Robert Greenwalt6078b502014-06-11 16:05:07 -07002095 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002096 }
2097
Robert Greenwalt9258c642014-03-26 16:47:06 -07002098 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
2099 /** @hide obj = pair(NetworkRequest, Network) */
2100 public static final int CALLBACK_PRECHECK = BASE + 1;
2101 /** @hide obj = pair(NetworkRequest, Network) */
2102 public static final int CALLBACK_AVAILABLE = BASE + 2;
2103 /** @hide obj = pair(NetworkRequest, Network), arg1 = ttl */
2104 public static final int CALLBACK_LOSING = BASE + 3;
2105 /** @hide obj = pair(NetworkRequest, Network) */
2106 public static final int CALLBACK_LOST = BASE + 4;
2107 /** @hide obj = NetworkRequest */
2108 public static final int CALLBACK_UNAVAIL = BASE + 5;
2109 /** @hide obj = pair(NetworkRequest, Network) */
2110 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2111 /** @hide obj = pair(NetworkRequest, Network) */
2112 public static final int CALLBACK_IP_CHANGED = BASE + 7;
2113 /** @hide obj = NetworkRequest */
2114 public static final int CALLBACK_RELEASED = BASE + 8;
2115 /** @hide */
2116 public static final int CALLBACK_EXIT = BASE + 9;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002117 /** @hide obj = NetworkCapabilities, arg1 = seq number */
2118 private static final int EXPIRE_LEGACY_REQUEST = BASE + 10;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002119
Robert Greenwalt562cc542014-05-15 18:07:26 -07002120 private class CallbackHandler extends Handler {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002121 private final HashMap<NetworkRequest, NetworkCallback>mCallbackMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002122 private final AtomicInteger mRefCount;
2123 private static final String TAG = "ConnectivityManager.CallbackHandler";
2124 private final ConnectivityManager mCm;
2125
Robert Greenwalt6078b502014-06-11 16:05:07 -07002126 CallbackHandler(Looper looper, HashMap<NetworkRequest, NetworkCallback>callbackMap,
Robert Greenwalt9258c642014-03-26 16:47:06 -07002127 AtomicInteger refCount, ConnectivityManager cm) {
2128 super(looper);
2129 mCallbackMap = callbackMap;
2130 mRefCount = refCount;
2131 mCm = cm;
2132 }
2133
2134 @Override
2135 public void handleMessage(Message message) {
2136 Log.d(TAG, "CM callback handler got msg " + message.what);
2137 switch (message.what) {
2138 case CALLBACK_PRECHECK: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002139 NetworkRequest request = (NetworkRequest)getObject(message,
2140 NetworkRequest.class);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002141 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002142 if (callbacks != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002143 callbacks.onPreCheck((Network)getObject(message, Network.class));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002144 } else {
2145 Log.e(TAG, "callback not found for PRECHECK message");
2146 }
2147 break;
2148 }
2149 case CALLBACK_AVAILABLE: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002150 NetworkRequest request = (NetworkRequest)getObject(message,
2151 NetworkRequest.class);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002152 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002153 if (callbacks != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002154 callbacks.onAvailable((Network)getObject(message, Network.class));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002155 } else {
2156 Log.e(TAG, "callback not found for AVAILABLE message");
2157 }
2158 break;
2159 }
2160 case CALLBACK_LOSING: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002161 NetworkRequest request = (NetworkRequest)getObject(message,
2162 NetworkRequest.class);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002163 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002164 if (callbacks != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002165 callbacks.onLosing((Network)getObject(message, Network.class),
2166 message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002167 } else {
2168 Log.e(TAG, "callback not found for LOSING message");
2169 }
2170 break;
2171 }
2172 case CALLBACK_LOST: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002173 NetworkRequest request = (NetworkRequest)getObject(message,
2174 NetworkRequest.class);
2175
Robert Greenwalt6078b502014-06-11 16:05:07 -07002176 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002177 if (callbacks != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002178 callbacks.onLost((Network)getObject(message, Network.class));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002179 } else {
2180 Log.e(TAG, "callback not found for LOST message");
2181 }
2182 break;
2183 }
2184 case CALLBACK_UNAVAIL: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002185 NetworkRequest request = (NetworkRequest)getObject(message,
2186 NetworkRequest.class);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002187 NetworkCallback callbacks = null;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002188 synchronized(mCallbackMap) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002189 callbacks = mCallbackMap.get(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002190 }
2191 if (callbacks != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002192 callbacks.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002193 } else {
2194 Log.e(TAG, "callback not found for UNAVAIL message");
2195 }
2196 break;
2197 }
2198 case CALLBACK_CAP_CHANGED: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002199 NetworkRequest request = (NetworkRequest)getObject(message,
2200 NetworkRequest.class);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002201 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002202 if (callbacks != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002203 Network network = (Network)getObject(message, Network.class);
2204 NetworkCapabilities cap = (NetworkCapabilities)getObject(message,
2205 NetworkCapabilities.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002206
Robert Greenwalt6078b502014-06-11 16:05:07 -07002207 callbacks.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002208 } else {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002209 Log.e(TAG, "callback not found for CAP_CHANGED message");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002210 }
2211 break;
2212 }
2213 case CALLBACK_IP_CHANGED: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002214 NetworkRequest request = (NetworkRequest)getObject(message,
2215 NetworkRequest.class);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002216 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002217 if (callbacks != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002218 Network network = (Network)getObject(message, Network.class);
2219 LinkProperties lp = (LinkProperties)getObject(message,
2220 LinkProperties.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002221
Robert Greenwalt6078b502014-06-11 16:05:07 -07002222 callbacks.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002223 } else {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002224 Log.e(TAG, "callback not found for IP_CHANGED message");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002225 }
2226 break;
2227 }
2228 case CALLBACK_RELEASED: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002229 NetworkRequest req = (NetworkRequest)getObject(message, NetworkRequest.class);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002230 NetworkCallback callbacks = null;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002231 synchronized(mCallbackMap) {
2232 callbacks = mCallbackMap.remove(req);
2233 }
2234 if (callbacks != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002235 synchronized(mRefCount) {
2236 if (mRefCount.decrementAndGet() == 0) {
2237 getLooper().quit();
2238 }
2239 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002240 } else {
2241 Log.e(TAG, "callback not found for CANCELED message");
2242 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002243 break;
2244 }
2245 case CALLBACK_EXIT: {
2246 Log.d(TAG, "Listener quiting");
2247 getLooper().quit();
2248 break;
2249 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07002250 case EXPIRE_LEGACY_REQUEST: {
2251 expireRequest((NetworkCapabilities)message.obj, message.arg1);
2252 break;
2253 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002254 }
2255 }
2256
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002257 private Object getObject(Message msg, Class c) {
2258 return msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07002259 }
Robert Greenwalt6078b502014-06-11 16:05:07 -07002260 private NetworkCallback getCallbacks(NetworkRequest req) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002261 synchronized(mCallbackMap) {
2262 return mCallbackMap.get(req);
2263 }
2264 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002265 }
2266
Robert Greenwalt6078b502014-06-11 16:05:07 -07002267 private void incCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002268 synchronized(sCallbackRefCount) {
2269 if (sCallbackRefCount.incrementAndGet() == 1) {
2270 // TODO - switch this over to a ManagerThread or expire it when done
2271 HandlerThread callbackThread = new HandlerThread("ConnectivityManager");
2272 callbackThread.start();
2273 sCallbackHandler = new CallbackHandler(callbackThread.getLooper(),
Robert Greenwalt6078b502014-06-11 16:05:07 -07002274 sNetworkCallback, sCallbackRefCount, this);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002275 }
2276 }
2277 }
2278
Robert Greenwalt6078b502014-06-11 16:05:07 -07002279 private void decCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002280 synchronized(sCallbackRefCount) {
2281 if (sCallbackRefCount.decrementAndGet() == 0) {
2282 sCallbackHandler.obtainMessage(CALLBACK_EXIT).sendToTarget();
2283 sCallbackHandler = null;
2284 }
2285 }
2286 }
2287
Robert Greenwalt6078b502014-06-11 16:05:07 -07002288 static final HashMap<NetworkRequest, NetworkCallback> sNetworkCallback =
2289 new HashMap<NetworkRequest, NetworkCallback>();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002290 static final AtomicInteger sCallbackRefCount = new AtomicInteger(0);
2291 static CallbackHandler sCallbackHandler = null;
2292
2293 private final static int LISTEN = 1;
2294 private final static int REQUEST = 2;
2295
Robert Greenwalt562cc542014-05-15 18:07:26 -07002296 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002297 NetworkCallback networkCallback, int timeoutSec, int action,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07002298 int legacyType) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002299 if (networkCallback == null) {
2300 throw new IllegalArgumentException("null NetworkCallback");
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002301 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002302 if (need == null) throw new IllegalArgumentException("null NetworkCapabilities");
2303 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002304 incCallbackHandlerRefCount();
Paul Jensen7221cc32014-06-27 11:05:32 -04002305 synchronized(sNetworkCallback) {
2306 if (action == LISTEN) {
2307 networkCallback.networkRequest = mService.listenForNetwork(need,
2308 new Messenger(sCallbackHandler), new Binder());
2309 } else {
2310 networkCallback.networkRequest = mService.requestNetwork(need,
2311 new Messenger(sCallbackHandler), timeoutSec, new Binder(), legacyType);
2312 }
2313 if (networkCallback.networkRequest != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002314 sNetworkCallback.put(networkCallback.networkRequest, networkCallback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002315 }
2316 }
2317 } catch (RemoteException e) {}
Robert Greenwalt6078b502014-06-11 16:05:07 -07002318 if (networkCallback.networkRequest == null) decCallbackHandlerRefCount();
2319 return networkCallback.networkRequest;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002320 }
2321
2322 /**
2323 * Request a network to satisfy a set of {@link NetworkCapabilities}.
2324 *
2325 * This {@link NetworkRequest} will live until released via
Robert Greenwalt6078b502014-06-11 16:05:07 -07002326 * {@link #unregisterNetworkCallback} or the calling application exits.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002327 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07002328 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002329 * can be used to direct traffic to the network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002330 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002331 * @param request {@link NetworkRequest} describing this request.
2332 * @param networkCallback The {@link NetworkCallback} to be utilized for this
2333 * request. Note the callback must not be shared - they
2334 * uniquely specify this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002335 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002336 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
2337 sendRequestForNetwork(request.networkCapabilities, networkCallback, 0,
Ye Wenb87875e2014-07-21 14:19:01 -07002338 REQUEST, inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002339 }
2340
2341 /**
2342 * Request a network to satisfy a set of {@link NetworkCapabilities}, limited
2343 * by a timeout.
2344 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002345 * This function behaves identically to the non-timedout version, but if a suitable
Robert Greenwalt6078b502014-06-11 16:05:07 -07002346 * network is not found within the given time (in milliseconds) the
2347 * {@link NetworkCallback#unavailable} callback is called. The request must
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002348 * still be released normally by calling {@link releaseNetworkRequest}.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002349 * @param request {@link NetworkRequest} describing this request.
2350 * @param networkCallback The callbacks to be utilized for this request. Note
2351 * the callbacks must not be shared - they uniquely specify
2352 * this request.
2353 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
2354 * before {@link NetworkCallback#unavailable} is called.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002355 * @hide
2356 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002357 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
2358 int timeoutMs) {
2359 sendRequestForNetwork(request.networkCapabilities, networkCallback, timeoutMs,
Ye Wenb87875e2014-07-21 14:19:01 -07002360 REQUEST, inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002361 }
2362
2363 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002364 * The maximum number of milliseconds the framework will look for a suitable network
Robert Greenwalt9258c642014-03-26 16:47:06 -07002365 * during a timeout-equiped call to {@link requestNetwork}.
2366 * {@hide}
2367 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002368 public final static int MAX_NETWORK_REQUEST_TIMEOUT_MS = 100 * 60 * 1000;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002369
2370 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002371 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002372 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002373 * {@link android.content.Intent#getParcelableExtra(String)}.
2374 */
Erik Kline90e93072014-11-19 12:12:24 +09002375 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002376
2377 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002378 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002379 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002380 * {@link android.content.Intent#getParcelableExtra(String)}.
2381 */
Erik Kline90e93072014-11-19 12:12:24 +09002382 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002383
2384
2385 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07002386 * Request a network to satisfy a set of {@link NetworkCapabilities}.
2387 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002388 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07002389 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002390 * the request may outlive the calling application and get called back when a suitable
2391 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002392 * <p>
2393 * The operation is an Intent broadcast that goes to a broadcast receiver that
2394 * you registered with {@link Context#registerReceiver} or through the
2395 * &lt;receiver&gt; tag in an AndroidManifest.xml file
2396 * <p>
2397 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09002398 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
2399 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07002400 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002401 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07002402 * Intent to reserve the network or it will be released shortly after the Intent
2403 * is processed.
2404 * <p>
2405 * If there is already an request for this Intent registered (with the equality of
2406 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002407 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002408 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002409 * The request may be released normally by calling
2410 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002411 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002412 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002413 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07002414 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002415 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002416 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002417 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002418 checkPendingIntent(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002419 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002420 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002421 } catch (RemoteException e) {}
Robert Greenwalt9258c642014-03-26 16:47:06 -07002422 }
2423
2424 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002425 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
2426 * <p>
2427 * This method has the same behavior as {@link #unregisterNetworkCallback} with respect to
2428 * releasing network resources and disconnecting.
2429 *
2430 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
2431 * PendingIntent passed to
2432 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
2433 * corresponding NetworkRequest you'd like to remove. Cannot be null.
2434 */
2435 public void releaseNetworkRequest(PendingIntent operation) {
2436 checkPendingIntent(operation);
2437 try {
2438 mService.releasePendingNetworkRequest(operation);
2439 } catch (RemoteException e) {}
2440 }
2441
2442 private void checkPendingIntent(PendingIntent intent) {
2443 if (intent == null) {
2444 throw new IllegalArgumentException("PendingIntent cannot be null.");
2445 }
2446 }
2447
2448 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07002449 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07002450 * {@link NetworkRequest}. The callbacks will continue to be called until
2451 * either the application exits or {@link #unregisterNetworkCallback} is called
Robert Greenwalt9258c642014-03-26 16:47:06 -07002452 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002453 * @param request {@link NetworkRequest} describing this request.
2454 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
2455 * networks change state.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002456 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002457 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
2458 sendRequestForNetwork(request.networkCapabilities, networkCallback, 0, LISTEN, TYPE_NONE);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002459 }
2460
2461 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002462 * Unregisters callbacks about and possibly releases networks originating from
2463 * {@link #requestNetwork} and {@link #registerNetworkCallback} calls. If the
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002464 * given {@code NetworkCallback} had previously been used with {@code #requestNetwork},
Robert Greenwalt6078b502014-06-11 16:05:07 -07002465 * any networks that had been connected to only to satisfy that request will be
2466 * disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002467 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002468 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002469 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002470 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
2471 if (networkCallback == null || networkCallback.networkRequest == null ||
2472 networkCallback.networkRequest.requestId == REQUEST_ID_UNSET) {
2473 throw new IllegalArgumentException("Invalid NetworkCallback");
2474 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002475 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002476 mService.releaseNetworkRequest(networkCallback.networkRequest);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002477 } catch (RemoteException e) {}
2478 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002479
2480 /**
2481 * Binds the current process to {@code network}. All Sockets created in the future
2482 * (and not explicitly bound via a bound SocketFactory from
2483 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
2484 * {@code network}. All host name resolutions will be limited to {@code network} as well.
2485 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
2486 * work and all host name resolutions will fail. This is by design so an application doesn't
2487 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
2488 * To clear binding pass {@code null} for {@code network}. Using individually bound
2489 * Sockets created by Network.getSocketFactory().createSocket() and
2490 * performing network-specific host name resolutions via
2491 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
2492 * {@code setProcessDefaultNetwork}.
2493 *
2494 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
2495 * the current binding.
2496 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2497 */
2498 public static boolean setProcessDefaultNetwork(Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04002499 int netId = (network == null) ? NETID_UNSET : network.netId;
2500 if (netId == NetworkUtils.getNetworkBoundToProcess()) {
2501 return true;
2502 }
2503 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05002504 // Set HTTP proxy system properties to match network.
2505 // TODO: Deprecate this static method and replace it with a non-static version.
2506 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
Paul Jensenc91b5342014-08-27 12:38:45 -04002507 // Must flush DNS cache as new network may have different DNS resolutions.
2508 InetAddress.clearDnsCache();
2509 // Must flush socket pool as idle sockets will be bound to previous network and may
2510 // cause subsequent fetches to be performed on old network.
2511 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
2512 return true;
2513 } else {
2514 return false;
2515 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002516 }
2517
2518 /**
2519 * Returns the {@link Network} currently bound to this process via
2520 * {@link #setProcessDefaultNetwork}, or {@code null} if no {@link Network} is explicitly bound.
2521 *
2522 * @return {@code Network} to which this process is bound, or {@code null}.
2523 */
2524 public static Network getProcessDefaultNetwork() {
2525 int netId = NetworkUtils.getNetworkBoundToProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04002526 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002527 return new Network(netId);
2528 }
2529
2530 /**
2531 * Binds host resolutions performed by this process to {@code network}.
2532 * {@link #setProcessDefaultNetwork} takes precedence over this setting.
2533 *
2534 * @param network The {@link Network} to bind host resolutions from the current process to, or
2535 * {@code null} to clear the current binding.
2536 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2537 * @hide
2538 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
2539 */
2540 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04002541 return NetworkUtils.bindProcessToNetworkForHostResolution(
2542 network == null ? NETID_UNSET : network.netId);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002543 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002544}