blob: 18373356158651750a5262bc63d320e2e83cba50 [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;
Robert Greenwalt42acef32009-08-12 16:08:25 -070025import android.os.Binder;
Jeff Sharkey3a844fc2011-08-16 14:37:57 -070026import android.os.Build.VERSION_CODES;
Robert Greenwalt9258c642014-03-26 16:47:06 -070027import android.os.Handler;
28import android.os.HandlerThread;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080029import android.os.IBinder;
30import android.os.INetworkActivityListener;
31import android.os.INetworkManagementService;
Robert Greenwalt9258c642014-03-26 16:47:06 -070032import android.os.Looper;
33import android.os.Message;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -070034import android.os.Messenger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080035import android.os.RemoteException;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080036import android.os.ServiceManager;
Jeff Sharkey961e3042011-08-29 16:02:57 -070037import android.provider.Settings;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080038import android.util.ArrayMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -070039import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080040
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070041import java.net.InetAddress;
Robert Greenwalt9258c642014-03-26 16:47:06 -070042import java.util.concurrent.atomic.AtomicInteger;
43import java.util.HashMap;
44
45import com.android.internal.util.Protocol;
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070046
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080047/**
48 * Class that answers queries about the state of network connectivity. It also
49 * notifies applications when network connectivity changes. Get an instance
50 * of this class by calling
51 * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}.
52 * <p>
53 * The primary responsibilities of this class are to:
54 * <ol>
55 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
56 * <li>Send broadcast intents when network connectivity changes</li>
57 * <li>Attempt to "fail over" to another network when connectivity to a network
58 * is lost</li>
59 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
60 * state of the available networks</li>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070061 * <li>Provide an API that allows applications to request and select networks for their data
62 * traffic</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 * </ol>
64 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070065public class ConnectivityManager {
66 private static final String TAG = "ConnectivityManager";
67
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070069 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 * been established or lost. The NetworkInfo for the affected network is
71 * sent as an extra; it should be consulted to see what kind of
72 * connectivity event occurred.
73 * <p/>
74 * If this is a connection that was the result of failing over from a
75 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
76 * set to true.
77 * <p/>
78 * For a loss of connectivity, if the connectivity manager is attempting
79 * to connect (or has already connected) to another network, the
80 * NetworkInfo for the new network is also passed as an extra. This lets
81 * any receivers of the broadcast know that they should not necessarily
82 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -080083 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084 * the failover attempt succeeded (and so there is still overall data
85 * connectivity), or that the failover attempt failed, meaning that all
86 * connectivity has been lost.
87 * <p/>
88 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
89 * is set to {@code true} if there are no connected networks at all.
90 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -080091 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -070093
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080094 /**
Jeff Sharkey961e3042011-08-29 16:02:57 -070095 * Identical to {@link #CONNECTIVITY_ACTION} broadcast, but sent without any
Dianne Hackborn77b987f2014-02-26 16:20:52 -080096 * applicable {@link Settings.Global#CONNECTIVITY_CHANGE_DELAY}.
Jeff Sharkey961e3042011-08-29 16:02:57 -070097 *
98 * @hide
99 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800100 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey961e3042011-08-29 16:02:57 -0700101 public static final String CONNECTIVITY_ACTION_IMMEDIATE =
102 "android.net.conn.CONNECTIVITY_CHANGE_IMMEDIATE";
103
104 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800105 * The lookup key for a {@link NetworkInfo} object. Retrieve with
106 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700107 *
108 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
109 * should always obtain network information through
110 * {@link #getActiveNetworkInfo()} or
111 * {@link #getAllNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700112 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700114 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800115 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700116
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800117 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700118 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
119 * Can be used with {@link #getNetworkInfo(int)} to get {@link NetworkInfo}
120 * state based on the calling application.
121 *
122 * @see android.content.Intent#getIntExtra(String, int)
123 */
124 public static final String EXTRA_NETWORK_TYPE = "networkType";
125
126 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800127 * The lookup key for a boolean that indicates whether a connect event
128 * is for a network to which the connectivity manager was failing over
129 * following a disconnect on another network.
130 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
131 */
132 public static final String EXTRA_IS_FAILOVER = "isFailover";
133 /**
134 * The lookup key for a {@link NetworkInfo} object. This is supplied when
135 * there is another network that it may be possible to connect to. Retrieve with
136 * {@link android.content.Intent#getParcelableExtra(String)}.
137 */
138 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
139 /**
140 * The lookup key for a boolean that indicates whether there is a
141 * complete lack of connectivity, i.e., no network is available.
142 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
143 */
144 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
145 /**
146 * The lookup key for a string that indicates why an attempt to connect
147 * to a network failed. The string has no particular structure. It is
148 * intended to be used in notifications presented to users. Retrieve
149 * it with {@link android.content.Intent#getStringExtra(String)}.
150 */
151 public static final String EXTRA_REASON = "reason";
152 /**
153 * The lookup key for a string that provides optionally supplied
154 * extra information about the network state. The information
155 * may be passed up from the lower networking layers, and its
156 * meaning may be specific to a particular network type. Retrieve
157 * it with {@link android.content.Intent#getStringExtra(String)}.
158 */
159 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700160 /**
161 * The lookup key for an int that provides information about
162 * our connection to the internet at large. 0 indicates no connection,
163 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700164 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700165 * {@hide}
166 */
167 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800168
169 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700170 * Broadcast action to indicate the change of data activity status
171 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800172 * The network becomes active when data transmission is started, or
173 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700174 * {@hide}
175 */
176 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
177 public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
178 /**
179 * The lookup key for an enum that indicates the network device type on which this data activity
180 * change happens.
181 * {@hide}
182 */
183 public static final String EXTRA_DEVICE_TYPE = "deviceType";
184 /**
185 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
186 * it is actively sending or receiving data and {@code false} means it is idle.
187 * {@hide}
188 */
189 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700190 /**
191 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
192 * {@hide}
193 */
194 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700195
196 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800197 * Broadcast Action: The setting for background data usage has changed
198 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
199 * <p>
200 * If an application uses the network in the background, it should listen
201 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700202 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800203 * <p>
204 *
205 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
206 * of background data depends on several combined factors, and
207 * this broadcast is no longer sent. Instead, when background
208 * data is unavailable, {@link #getActiveNetworkInfo()} will now
209 * appear disconnected. During first boot after a platform
210 * upgrade, this broadcast will be sent once if
211 * {@link #getBackgroundDataSetting()} was {@code false} before
212 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800213 */
214 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800215 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
217 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
218
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700219 /**
220 * Broadcast Action: The network connection may not be good
221 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
222 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
223 * the network and it's condition.
224 * @hide
225 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800226 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700227 public static final String INET_CONDITION_ACTION =
228 "android.net.conn.INET_CONDITION_ACTION";
229
Robert Greenwalt42acef32009-08-12 16:08:25 -0700230 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800231 * Broadcast Action: A tetherable connection has come or gone.
232 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
233 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER} and
234 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
235 * the current state of tethering. Each include a list of
236 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800237 * @hide
238 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800239 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800240 public static final String ACTION_TETHER_STATE_CHANGED =
241 "android.net.conn.TETHER_STATE_CHANGED";
242
243 /**
244 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800245 * gives a String[] listing all the interfaces configured for
246 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800247 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800248 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800249
250 /**
251 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800252 * gives a String[] listing all the interfaces currently tethered
253 * (ie, has dhcp support and packets potentially forwarded/NATed)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800254 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800255 public static final String EXTRA_ACTIVE_TETHER = "activeArray";
256
257 /**
258 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800259 * gives a String[] listing all the interfaces we tried to tether and
260 * failed. Use {@link #getLastTetherError} to find the error code
261 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800262 */
263 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800264
265 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800266 * Broadcast Action: The captive portal tracker has finished its test.
267 * Sent only while running Setup Wizard, in lieu of showing a user
268 * notification.
269 * @hide
270 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800271 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800272 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
273 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
274 /**
275 * The lookup key for a boolean that indicates whether a captive portal was detected.
276 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
277 * @hide
278 */
279 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
280
281 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800282 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700283 * @hide
284 */
285 public static final int TYPE_NONE = -1;
286
287 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800288 * The Mobile data connection. When active, all data traffic
289 * will use this network type's interface by default
290 * (it has a default route)
Robert Greenwalt42acef32009-08-12 16:08:25 -0700291 */
292 public static final int TYPE_MOBILE = 0;
293 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800294 * The WIFI data connection. When active, all data traffic
295 * will use this network type's interface by default
296 * (it has a default route).
Robert Greenwalt42acef32009-08-12 16:08:25 -0700297 */
298 public static final int TYPE_WIFI = 1;
299 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800300 * An MMS-specific Mobile data connection. This network type may use the
301 * same network interface as {@link #TYPE_MOBILE} or it may use a different
302 * one. This is used by applications needing to talk to the carrier's
303 * Multimedia Messaging Service servers.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700304 */
305 public static final int TYPE_MOBILE_MMS = 2;
306 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800307 * A SUPL-specific Mobile data connection. This network type may use the
308 * same network interface as {@link #TYPE_MOBILE} or it may use a different
309 * one. This is used by applications needing to talk to the carrier's
310 * Secure User Plane Location servers for help locating the device.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700311 */
312 public static final int TYPE_MOBILE_SUPL = 3;
313 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800314 * A DUN-specific Mobile data connection. This network type may use the
315 * same network interface as {@link #TYPE_MOBILE} or it may use a different
316 * one. This is sometimes by the system when setting up an upstream connection
317 * for tethering so that the carrier is aware of DUN traffic.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700318 */
319 public static final int TYPE_MOBILE_DUN = 4;
320 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800321 * A High Priority Mobile data connection. This network type uses the
322 * same network interface as {@link #TYPE_MOBILE} but the routing setup
323 * is different. Only requesting processes will have access to the
324 * Mobile DNS servers and only IP's explicitly requested via {@link #requestRouteToHost}
325 * will route over this interface if no default route exists.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700326 */
327 public static final int TYPE_MOBILE_HIPRI = 5;
jsh8214deb2010-03-11 15:04:43 -0800328 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800329 * The WiMAX data connection. When active, all data traffic
330 * will use this network type's interface by default
331 * (it has a default route).
jsh8214deb2010-03-11 15:04:43 -0800332 */
333 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800334
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800335 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800336 * The Bluetooth data connection. When active, all data traffic
337 * will use this network type's interface by default
338 * (it has a default route).
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800339 */
340 public static final int TYPE_BLUETOOTH = 7;
341
Robert Greenwalt60810842011-04-22 15:28:18 -0700342 /**
343 * Dummy data connection. This should not be used on shipping devices.
344 */
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800345 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800346
Robert Greenwalt60810842011-04-22 15:28:18 -0700347 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800348 * The Ethernet data connection. When active, all data traffic
349 * will use this network type's interface by default
350 * (it has a default route).
Robert Greenwalt60810842011-04-22 15:28:18 -0700351 */
Robert Greenwalte12aec92011-01-28 14:48:37 -0800352 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700353
Wink Saville9d7d6282011-03-12 14:52:01 -0800354 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800355 * Over the air Administration.
Wink Saville9d7d6282011-03-12 14:52:01 -0800356 * {@hide}
357 */
358 public static final int TYPE_MOBILE_FOTA = 10;
359
360 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800361 * IP Multimedia Subsystem.
Wink Saville9d7d6282011-03-12 14:52:01 -0800362 * {@hide}
363 */
364 public static final int TYPE_MOBILE_IMS = 11;
365
366 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800367 * Carrier Branded Services.
Wink Saville9d7d6282011-03-12 14:52:01 -0800368 * {@hide}
369 */
370 public static final int TYPE_MOBILE_CBS = 12;
371
repo syncaea743a2011-07-29 23:55:49 -0700372 /**
373 * A Wi-Fi p2p connection. Only requesting processes will have access to
374 * the peers connected.
375 * {@hide}
376 */
377 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800378
Wink Saville5e56bc52013-07-29 15:00:57 -0700379 /**
380 * The network to use for initially attaching to the network
381 * {@hide}
382 */
383 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700384
Hui Lu1c5624a2014-01-15 11:05:36 -0500385 /**
386 * The network that uses proxy to achieve connectivity.
387 * {@hide}
388 */
389 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700390
391 /** {@hide} */
Hui Lu1c5624a2014-01-15 11:05:36 -0500392 public static final int MAX_RADIO_TYPE = TYPE_PROXY;
393
394 /** {@hide} */
395 public static final int MAX_NETWORK_TYPE = TYPE_PROXY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800396
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800397 /**
398 * If you want to set the default network preference,you can directly
399 * change the networkAttributes array in framework's config.xml.
400 *
401 * @deprecated Since we support so many more networks now, the single
402 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800403 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800404 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800405 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800406 * from an App.
407 */
408 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800409 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
410
Jeff Sharkey625239a2012-09-26 22:03:49 -0700411 /**
412 * Default value for {@link Settings.Global#CONNECTIVITY_CHANGE_DELAY} in
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800413 * milliseconds. This was introduced because IPv6 routes seem to take a
414 * moment to settle - trying network activity before the routes are adjusted
415 * can lead to packets using the wrong interface or having the wrong IP address.
416 * This delay is a bit crude, but in the future hopefully we will have kernel
417 * notifications letting us know when it's safe to use the new network.
Jeff Sharkey625239a2012-09-26 22:03:49 -0700418 *
419 * @hide
420 */
421 public static final int CONNECTIVITY_CHANGE_DELAY_DEFAULT = 3000;
422
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700423 /**
424 * @hide
425 */
426 public final static int INVALID_NET_ID = 0;
427
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700428 private final IConnectivityManager mService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800429
Chad Brubakerf81daa92014-02-14 13:22:34 -0800430 private final String mPackageName;
431
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800432 private INetworkManagementService mNMService;
433
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800434 /**
435 * Tests if a given integer represents a valid network type.
436 * @param networkType the type to be tested
437 * @return a boolean. {@code true} if the type is valid, else {@code false}
438 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700439 public static boolean isNetworkTypeValid(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700440 return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800441 }
442
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800443 /**
444 * Returns a non-localized string representing a given network type.
445 * ONLY used for debugging output.
446 * @param type the type needing naming
447 * @return a String for the given type, or a string version of the type ("87")
448 * if no name is known.
449 * {@hide}
450 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700451 public static String getNetworkTypeName(int type) {
452 switch (type) {
453 case TYPE_MOBILE:
454 return "MOBILE";
455 case TYPE_WIFI:
456 return "WIFI";
457 case TYPE_MOBILE_MMS:
458 return "MOBILE_MMS";
459 case TYPE_MOBILE_SUPL:
460 return "MOBILE_SUPL";
461 case TYPE_MOBILE_DUN:
462 return "MOBILE_DUN";
463 case TYPE_MOBILE_HIPRI:
464 return "MOBILE_HIPRI";
465 case TYPE_WIMAX:
466 return "WIMAX";
467 case TYPE_BLUETOOTH:
468 return "BLUETOOTH";
469 case TYPE_DUMMY:
470 return "DUMMY";
471 case TYPE_ETHERNET:
472 return "ETHERNET";
473 case TYPE_MOBILE_FOTA:
474 return "MOBILE_FOTA";
475 case TYPE_MOBILE_IMS:
476 return "MOBILE_IMS";
477 case TYPE_MOBILE_CBS:
478 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700479 case TYPE_WIFI_P2P:
480 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700481 case TYPE_MOBILE_IA:
482 return "MOBILE_IA";
Hui Lu1c5624a2014-01-15 11:05:36 -0500483 case TYPE_PROXY:
484 return "PROXY";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700485 default:
486 return Integer.toString(type);
487 }
488 }
489
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800490 /**
491 * Checks if a given type uses the cellular data connection.
492 * This should be replaced in the future by a network property.
493 * @param networkType the type to check
494 * @return a boolean - {@code true} if uses cellular network, else {@code false}
495 * {@hide}
496 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700497 public static boolean isNetworkTypeMobile(int networkType) {
498 switch (networkType) {
499 case TYPE_MOBILE:
500 case TYPE_MOBILE_MMS:
501 case TYPE_MOBILE_SUPL:
502 case TYPE_MOBILE_DUN:
503 case TYPE_MOBILE_HIPRI:
504 case TYPE_MOBILE_FOTA:
505 case TYPE_MOBILE_IMS:
506 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700507 case TYPE_MOBILE_IA:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700508 return true;
509 default:
510 return false;
511 }
512 }
513
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800514 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700515 * Checks if the given network type is backed by a Wi-Fi radio.
516 *
517 * @hide
518 */
519 public static boolean isNetworkTypeWifi(int networkType) {
520 switch (networkType) {
521 case TYPE_WIFI:
522 case TYPE_WIFI_P2P:
523 return true;
524 default:
525 return false;
526 }
527 }
528
529 /**
Chad Brubakerf336d722013-07-15 16:34:04 -0700530 * Checks if the given network type should be exempt from VPN routing rules
531 *
532 * @hide
533 */
534 public static boolean isNetworkTypeExempt(int networkType) {
535 switch (networkType) {
536 case TYPE_MOBILE_MMS:
537 case TYPE_MOBILE_SUPL:
538 case TYPE_MOBILE_HIPRI:
Wink Saville5e56bc52013-07-29 15:00:57 -0700539 case TYPE_MOBILE_IA:
Chad Brubakerf336d722013-07-15 16:34:04 -0700540 return true;
541 default:
542 return false;
543 }
544 }
545
546 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800547 * Specifies the preferred network type. When the device has more
548 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800549 *
550 * @param preference the network type to prefer over all others. It is
551 * unspecified what happens to the old preferred network in the
552 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700553 * @deprecated Functionality has been removed as it no longer makes sense,
554 * with many more than two networks - we'd need an array to express
555 * preference. Instead we use dynamic network properties of
556 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800557 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800558 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800559 }
560
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800561 /**
562 * Retrieves the current preferred network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800563 *
564 * @return an integer representing the preferred network type
565 *
566 * <p>This method requires the caller to hold the permission
567 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700568 * @deprecated Functionality has been removed as it no longer makes sense,
569 * with many more than two networks - we'd need an array to express
570 * preference. Instead we use dynamic network properties of
571 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800572 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800573 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700574 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800575 }
576
Scott Main671644c2011-10-06 19:02:28 -0700577 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800578 * Returns details about the currently active default data network. When
579 * connected, this network is the default route for outgoing connections.
580 * You should always check {@link NetworkInfo#isConnected()} before initiating
581 * network traffic. This may return {@code null} when there is no default
582 * network.
583 *
584 * @return a {@link NetworkInfo} object for the current default network
585 * or {@code null} if no network default network is currently active
586 *
587 * <p>This method requires the call to hold the permission
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700588 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700589 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800590 public NetworkInfo getActiveNetworkInfo() {
591 try {
592 return mService.getActiveNetworkInfo();
593 } catch (RemoteException e) {
594 return null;
595 }
596 }
597
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800598 /**
599 * Returns details about the currently active default data network
600 * for a given uid. This is for internal use only to avoid spying
601 * other apps.
602 *
603 * @return a {@link NetworkInfo} object for the current default network
604 * for the given uid or {@code null} if no default network is
605 * available for the specified uid.
606 *
607 * <p>This method requires the caller to hold the permission
608 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
609 * {@hide}
610 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700611 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
612 try {
613 return mService.getActiveNetworkInfoForUid(uid);
614 } catch (RemoteException e) {
615 return null;
616 }
617 }
618
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800619 /**
620 * Returns connection status information about a particular
621 * network type.
622 *
623 * @param networkType integer specifying which networkType in
624 * which you're interested.
625 * @return a {@link NetworkInfo} object for the requested
626 * network type or {@code null} if the type is not
627 * supported by the device.
628 *
629 * <p>This method requires the call to hold the permission
630 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
631 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800632 public NetworkInfo getNetworkInfo(int networkType) {
633 try {
634 return mService.getNetworkInfo(networkType);
635 } catch (RemoteException e) {
636 return null;
637 }
638 }
639
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800640 /**
641 * Returns connection status information about all network
642 * types supported by the device.
643 *
644 * @return an array of {@link NetworkInfo} objects. Check each
645 * {@link NetworkInfo#getType} for which type each applies.
646 *
647 * <p>This method requires the call to hold the permission
648 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
649 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800650 public NetworkInfo[] getAllNetworkInfo() {
651 try {
652 return mService.getAllNetworkInfo();
653 } catch (RemoteException e) {
654 return null;
655 }
656 }
657
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800658 /**
Wink Saville948282b2013-08-29 08:55:16 -0700659 * Returns details about the Provisioning or currently active default data network. When
660 * connected, this network is the default route for outgoing connections.
661 * You should always check {@link NetworkInfo#isConnected()} before initiating
662 * network traffic. This may return {@code null} when there is no default
663 * network.
664 *
665 * @return a {@link NetworkInfo} object for the current default network
666 * or {@code null} if no network default network is currently active
667 *
668 * <p>This method requires the call to hold the permission
669 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
670 *
671 * {@hide}
672 */
673 public NetworkInfo getProvisioningOrActiveNetworkInfo() {
674 try {
675 return mService.getProvisioningOrActiveNetworkInfo();
676 } catch (RemoteException e) {
677 return null;
678 }
679 }
680
681 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800682 * Returns the IP information for the current default network.
683 *
684 * @return a {@link LinkProperties} object describing the IP info
685 * for the current default network, or {@code null} if there
686 * is no current default network.
687 *
688 * <p>This method requires the call to hold the permission
689 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
690 * {@hide}
691 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700692 public LinkProperties getActiveLinkProperties() {
693 try {
694 return mService.getActiveLinkProperties();
695 } catch (RemoteException e) {
696 return null;
697 }
698 }
699
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800700 /**
701 * Returns the IP information for a given network type.
702 *
703 * @param networkType the network type of interest.
704 * @return a {@link LinkProperties} object describing the IP info
705 * for the given networkType, or {@code null} if there is
706 * no current default network.
707 *
708 * <p>This method requires the call to hold the permission
709 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
710 * {@hide}
711 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700712 public LinkProperties getLinkProperties(int networkType) {
713 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -0700714 return mService.getLinkPropertiesForType(networkType);
715 } catch (RemoteException e) {
716 return null;
717 }
718 }
719
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700720 /**
721 * Get the {@link LinkProperties} for the given {@link Network}. This
722 * will return {@code null} if the network is unknown.
723 *
724 * @param network The {@link Network} object identifying the network in question.
725 * @return The {@link LinkProperties} for the network, or {@code null}.
726 **/
Robert Greenwalt9258c642014-03-26 16:47:06 -0700727 public LinkProperties getLinkProperties(Network network) {
728 try {
729 return mService.getLinkProperties(network);
730 } catch (RemoteException e) {
731 return null;
732 }
733 }
734
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700735 /**
736 * Get the {@link NetworkCapabilities} for the given {@link Network}. This
737 * will return {@code null} if the network is unknown.
738 *
739 * @param network The {@link Network} object identifying the network in question.
740 * @return The {@link NetworkCapabilities} for the network, or {@code null}.
741 */
Robert Greenwalt9258c642014-03-26 16:47:06 -0700742 public NetworkCapabilities getNetworkCapabilities(Network network) {
743 try {
744 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700745 } catch (RemoteException e) {
746 return null;
747 }
748 }
749
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800750 /**
751 * Tells each network type to set its radio power state as directed.
752 *
753 * @param turnOn a boolean, {@code true} to turn the radios on,
754 * {@code false} to turn them off.
755 * @return a boolean, {@code true} indicating success. All network types
756 * will be tried, even if some fail.
757 *
758 * <p>This method requires the call to hold the permission
759 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
760 * {@hide}
761 */
Robert Greenwalt7b816022014-04-18 15:25:25 -0700762// TODO - check for any callers and remove
763// public boolean setRadios(boolean turnOn) {
764// try {
765// return mService.setRadios(turnOn);
766// } catch (RemoteException e) {
767// return false;
768// }
769// }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800770
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800771 /**
772 * Tells a given networkType to set its radio power state as directed.
773 *
774 * @param networkType the int networkType of interest.
775 * @param turnOn a boolean, {@code true} to turn the radio on,
776 * {@code} false to turn it off.
777 * @return a boolean, {@code true} indicating success.
778 *
779 * <p>This method requires the call to hold the permission
780 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
781 * {@hide}
782 */
Robert Greenwalt7b816022014-04-18 15:25:25 -0700783// TODO - check for any callers and remove
784// public boolean setRadio(int networkType, boolean turnOn) {
785// try {
786// return mService.setRadio(networkType, turnOn);
787// } catch (RemoteException e) {
788// return false;
789// }
790// }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800791
792 /**
793 * Tells the underlying networking system that the caller wants to
794 * begin using the named feature. The interpretation of {@code feature}
795 * is completely up to each networking implementation.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700796 * <p>This method requires the caller to hold the permission
797 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800798 * @param networkType specifies which network the request pertains to
799 * @param feature the name of the feature to be used
800 * @return an integer value representing the outcome of the request.
801 * The interpretation of this value is specific to each networking
802 * implementation+feature combination, except that the value {@code -1}
803 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700804 *
805 * @deprecated Deprecated in favor of the cleaner {@link #requestNetwork} api.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800806 */
807 public int startUsingNetworkFeature(int networkType, String feature) {
808 try {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700809 return mService.startUsingNetworkFeature(networkType, feature,
810 new Binder());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800811 } catch (RemoteException e) {
812 return -1;
813 }
814 }
815
816 /**
817 * Tells the underlying networking system that the caller is finished
818 * using the named feature. The interpretation of {@code feature}
819 * is completely up to each networking implementation.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700820 * <p>This method requires the caller to hold the permission
821 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800822 * @param networkType specifies which network the request pertains to
823 * @param feature the name of the feature that is no longer needed
824 * @return an integer value representing the outcome of the request.
825 * The interpretation of this value is specific to each networking
826 * implementation+feature combination, except that the value {@code -1}
827 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700828 *
829 * @deprecated Deprecated in favor of the cleaner {@link #requestNetwork} api.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800830 */
831 public int stopUsingNetworkFeature(int networkType, String feature) {
832 try {
833 return mService.stopUsingNetworkFeature(networkType, feature);
834 } catch (RemoteException e) {
835 return -1;
836 }
837 }
838
839 /**
840 * Ensure that a network route exists to deliver traffic to the specified
841 * host via the specified network interface. An attempt to add a route that
842 * already exists is ignored, but treated as successful.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700843 * <p>This method requires the caller to hold the permission
844 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800845 * @param networkType the type of the network over which traffic to the specified
846 * host is to be routed
847 * @param hostAddress the IP address of the host to which the route is desired
848 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700849 *
850 * @deprecated Deprecated in favor of the {@link #requestNetwork},
851 * {@link Network#bindProcess} and {@link Network#socketFactory} api.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800852 */
853 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700854 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
855
856 if (inetAddress == null) {
857 return false;
858 }
859
860 return requestRouteToHostAddress(networkType, inetAddress);
861 }
862
863 /**
864 * Ensure that a network route exists to deliver traffic to the specified
865 * host via the specified network interface. An attempt to add a route that
866 * already exists is ignored, but treated as successful.
Jake Hamby8f9b33e2014-01-15 13:08:03 -0800867 * <p>This method requires the caller to hold the permission
868 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700869 * @param networkType the type of the network over which traffic to the specified
870 * host is to be routed
871 * @param hostAddress the IP address of the host to which the route is desired
872 * @return {@code true} on success, {@code false} on failure
873 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700874 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
875 * {@link Network#bindProcess} api.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -0700876 */
877 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
878 byte[] address = hostAddress.getAddress();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800879 try {
Chad Brubakerf81daa92014-02-14 13:22:34 -0800880 return mService.requestRouteToHostAddress(networkType, address, mPackageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800881 } catch (RemoteException e) {
882 return false;
883 }
884 }
885
886 /**
887 * Returns the value of the setting for background data usage. If false,
888 * applications should not use the network if the application is not in the
889 * foreground. Developers should respect this setting, and check the value
890 * of this before performing any background data operations.
891 * <p>
892 * All applications that have background services that use the network
893 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -0700894 * <p>
Scott Main4cc53332011-10-06 18:32:43 -0700895 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -0700896 * background data depends on several combined factors, and this method will
897 * always return {@code true}. Instead, when background data is unavailable,
898 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -0700899 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800900 * @return Whether background data usage is allowed.
901 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -0700902 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800903 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -0700904 // assume that background data is allowed; final authority is
905 // NetworkInfo which may be blocked.
906 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800907 }
908
909 /**
910 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800911 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800912 * @param allowBackgroundData Whether an application should use data while
913 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800914 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800915 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
916 * @see #getBackgroundDataSetting()
917 * @hide
918 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -0700919 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800920 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -0700921 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800922 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800923
924 /**
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700925 * Return quota status for the current active network, or {@code null} if no
926 * network is active. Quota status can change rapidly, so these values
927 * shouldn't be cached.
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -0700928 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800929 * <p>This method requires the call to hold the permission
930 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
931 *
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -0700932 * @hide
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700933 */
934 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
935 try {
936 return mService.getActiveNetworkQuotaInfo();
937 } catch (RemoteException e) {
938 return null;
939 }
940 }
941
942 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800943 * Gets the value of the setting for enabling Mobile data.
944 *
945 * @return Whether mobile data is enabled.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800946 *
947 * <p>This method requires the call to hold the permission
948 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800949 * @hide
950 */
951 public boolean getMobileDataEnabled() {
952 try {
953 return mService.getMobileDataEnabled();
954 } catch (RemoteException e) {
955 return true;
956 }
957 }
958
959 /**
960 * Sets the persisted value for enabling/disabling Mobile data.
961 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800962 * @param enabled Whether the user wants the mobile data connection used
963 * or not.
Robert Greenwaltc03fa502010-02-23 18:58:05 -0800964 * @hide
965 */
966 public void setMobileDataEnabled(boolean enabled) {
967 try {
968 mService.setMobileDataEnabled(enabled);
969 } catch (RemoteException e) {
970 }
971 }
972
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800973 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800974 * Callback for use with {@link ConnectivityManager#registerNetworkActiveListener} to
975 * find out when the current network has gone in to a high power state.
976 */
977 public interface OnNetworkActiveListener {
978 /**
979 * Called on the main thread of the process to report that the current data network
980 * has become active, and it is now a good time to perform any pending network
981 * operations. Note that this listener only tells you when the network becomes
982 * active; if at any other time you want to know whether it is active (and thus okay
983 * to initiate network traffic), you can retrieve its instantaneous state with
984 * {@link ConnectivityManager#isNetworkActive}.
985 */
986 public void onNetworkActive();
987 }
988
989 private INetworkManagementService getNetworkManagementService() {
990 synchronized (this) {
991 if (mNMService != null) {
992 return mNMService;
993 }
994 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
995 mNMService = INetworkManagementService.Stub.asInterface(b);
996 return mNMService;
997 }
998 }
999
1000 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
1001 mNetworkActivityListeners
1002 = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>();
1003
1004 /**
1005 * Start listening to reports when the data network is active, meaning it is
1006 * a good time to perform network traffic. Use {@link #isNetworkActive()}
1007 * to determine the current state of the network after registering the listener.
1008 *
1009 * @param l The listener to be told when the network is active.
1010 */
1011 public void registerNetworkActiveListener(final OnNetworkActiveListener l) {
1012 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1013 @Override
1014 public void onNetworkActive() throws RemoteException {
1015 l.onNetworkActive();
1016 }
1017 };
1018
1019 try {
1020 getNetworkManagementService().registerNetworkActivityListener(rl);
1021 mNetworkActivityListeners.put(l, rl);
1022 } catch (RemoteException e) {
1023 }
1024 }
1025
1026 /**
1027 * Remove network active listener previously registered with
1028 * {@link #registerNetworkActiveListener}.
1029 *
1030 * @param l Previously registered listener.
1031 */
1032 public void unregisterNetworkActiveListener(OnNetworkActiveListener l) {
1033 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
1034 if (rl == null) {
1035 throw new IllegalArgumentException("Listener not registered: " + l);
1036 }
1037 try {
1038 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1039 } catch (RemoteException e) {
1040 }
1041 }
1042
1043 /**
1044 * Return whether the data network is currently active. An active network means that
1045 * it is currently in a high power state for performing data transmission. On some
1046 * types of networks, it may be expensive to move and stay in such a state, so it is
1047 * more power efficient to batch network traffic together when the radio is already in
1048 * this state. This method tells you whether right now is currently a good time to
1049 * initiate network traffic, as the network is already active.
1050 */
1051 public boolean isNetworkActive() {
1052 try {
1053 return getNetworkManagementService().isNetworkActive();
1054 } catch (RemoteException e) {
1055 }
1056 return false;
1057 }
1058
1059 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 * {@hide}
1061 */
Chad Brubakerf81daa92014-02-14 13:22:34 -08001062 public ConnectivityManager(IConnectivityManager service, String packageName) {
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001063 mService = checkNotNull(service, "missing IConnectivityManager");
Chad Brubakerf81daa92014-02-14 13:22:34 -08001064 mPackageName = checkNotNull(packageName, "missing package name");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001065 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001066
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07001067 /** {@hide} */
1068 public static ConnectivityManager from(Context context) {
1069 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1070 }
1071
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001072 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001073 * Get the set of tetherable, available interfaces. This list is limited by
1074 * device configuration and current interface existence.
1075 *
1076 * @return an array of 0 or more Strings of tetherable interface names.
1077 *
1078 * <p>This method requires the call to hold the permission
1079 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001080 * {@hide}
1081 */
1082 public String[] getTetherableIfaces() {
1083 try {
1084 return mService.getTetherableIfaces();
1085 } catch (RemoteException e) {
1086 return new String[0];
1087 }
1088 }
1089
1090 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001091 * Get the set of tethered interfaces.
1092 *
1093 * @return an array of 0 or more String of currently tethered interface names.
1094 *
1095 * <p>This method requires the call to hold the permission
1096 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001097 * {@hide}
1098 */
1099 public String[] getTetheredIfaces() {
1100 try {
1101 return mService.getTetheredIfaces();
1102 } catch (RemoteException e) {
1103 return new String[0];
1104 }
1105 }
1106
1107 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001108 * Get the set of interface names which attempted to tether but
1109 * failed. Re-attempting to tether may cause them to reset to the Tethered
1110 * state. Alternatively, causing the interface to be destroyed and recreated
1111 * may cause them to reset to the available state.
1112 * {@link ConnectivityManager#getLastTetherError} can be used to get more
1113 * information on the cause of the errors.
1114 *
1115 * @return an array of 0 or more String indicating the interface names
1116 * which failed to tether.
1117 *
1118 * <p>This method requires the call to hold the permission
1119 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001120 * {@hide}
1121 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001122 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001123 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001124 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001125 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001126 return new String[0];
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001127 }
1128 }
1129
1130 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001131 * Attempt to tether the named interface. This will setup a dhcp server
1132 * on the interface, forward and NAT IP packets and forward DNS requests
1133 * to the best active upstream network interface. Note that if no upstream
1134 * IP network interface is available, dhcp will still run and traffic will be
1135 * allowed between the tethered devices and this device, though upstream net
1136 * access will of course fail until an upstream network interface becomes
1137 * active.
1138 *
1139 * @param iface the interface name to tether.
1140 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1141 *
1142 * <p>This method requires the call to hold the permission
1143 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001144 * {@hide}
1145 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001146 public int tether(String iface) {
1147 try {
1148 return mService.tether(iface);
1149 } catch (RemoteException e) {
1150 return TETHER_ERROR_SERVICE_UNAVAIL;
1151 }
1152 }
1153
1154 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001155 * Stop tethering the named interface.
1156 *
1157 * @param iface the interface name to untether.
1158 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1159 *
1160 * <p>This method requires the call to hold the permission
1161 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt5a735062010-03-02 17:25:02 -08001162 * {@hide}
1163 */
1164 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001165 try {
1166 return mService.untether(iface);
1167 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001168 return TETHER_ERROR_SERVICE_UNAVAIL;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001169 }
1170 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001171
1172 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001173 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001174 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001175 * due to device configuration.
1176 *
1177 * @return a boolean - {@code true} indicating Tethering is supported.
1178 *
1179 * <p>This method requires the call to hold the permission
1180 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001181 * {@hide}
1182 */
1183 public boolean isTetheringSupported() {
1184 try {
1185 return mService.isTetheringSupported();
1186 } catch (RemoteException e) {
1187 return false;
1188 }
1189 }
1190
1191 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001192 * Get the list of regular expressions that define any tetherable
1193 * USB network interfaces. If USB tethering is not supported by the
1194 * device, this list should be empty.
1195 *
1196 * @return an array of 0 or more regular expression Strings defining
1197 * what interfaces are considered tetherable usb interfaces.
1198 *
1199 * <p>This method requires the call to hold the permission
1200 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001201 * {@hide}
1202 */
1203 public String[] getTetherableUsbRegexs() {
1204 try {
1205 return mService.getTetherableUsbRegexs();
1206 } catch (RemoteException e) {
1207 return new String[0];
1208 }
1209 }
1210
1211 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001212 * Get the list of regular expressions that define any tetherable
1213 * Wifi network interfaces. If Wifi tethering is not supported by the
1214 * device, this list should be empty.
1215 *
1216 * @return an array of 0 or more regular expression Strings defining
1217 * what interfaces are considered tetherable wifi interfaces.
1218 *
1219 * <p>This method requires the call to hold the permission
1220 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001221 * {@hide}
1222 */
1223 public String[] getTetherableWifiRegexs() {
1224 try {
1225 return mService.getTetherableWifiRegexs();
1226 } catch (RemoteException e) {
1227 return new String[0];
1228 }
1229 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08001230
Danica Chang6fdd0c62010-08-11 14:54:43 -07001231 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001232 * Get the list of regular expressions that define any tetherable
1233 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
1234 * device, this list should be empty.
1235 *
1236 * @return an array of 0 or more regular expression Strings defining
1237 * what interfaces are considered tetherable bluetooth interfaces.
1238 *
1239 * <p>This method requires the call to hold the permission
1240 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001241 * {@hide}
1242 */
1243 public String[] getTetherableBluetoothRegexs() {
1244 try {
1245 return mService.getTetherableBluetoothRegexs();
1246 } catch (RemoteException e) {
1247 return new String[0];
1248 }
1249 }
1250
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001251 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001252 * Attempt to both alter the mode of USB and Tethering of USB. A
1253 * utility method to deal with some of the complexity of USB - will
1254 * attempt to switch to Rndis and subsequently tether the resulting
1255 * interface on {@code true} or turn off tethering and switch off
1256 * Rndis on {@code false}.
1257 *
1258 * @param enable a boolean - {@code true} to enable tethering
1259 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1260 *
1261 * <p>This method requires the call to hold the permission
1262 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001263 * {@hide}
1264 */
1265 public int setUsbTethering(boolean enable) {
1266 try {
1267 return mService.setUsbTethering(enable);
1268 } catch (RemoteException e) {
1269 return TETHER_ERROR_SERVICE_UNAVAIL;
1270 }
1271 }
1272
Robert Greenwalt5a735062010-03-02 17:25:02 -08001273 /** {@hide} */
1274 public static final int TETHER_ERROR_NO_ERROR = 0;
1275 /** {@hide} */
1276 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
1277 /** {@hide} */
1278 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
1279 /** {@hide} */
1280 public static final int TETHER_ERROR_UNSUPPORTED = 3;
1281 /** {@hide} */
1282 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
1283 /** {@hide} */
1284 public static final int TETHER_ERROR_MASTER_ERROR = 5;
1285 /** {@hide} */
1286 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
1287 /** {@hide} */
1288 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
1289 /** {@hide} */
1290 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
1291 /** {@hide} */
1292 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
1293 /** {@hide} */
1294 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
1295
1296 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001297 * Get a more detailed error code after a Tethering or Untethering
1298 * request asynchronously failed.
1299 *
1300 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08001301 * @return error The error code of the last error tethering or untethering the named
1302 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001303 *
1304 * <p>This method requires the call to hold the permission
1305 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a735062010-03-02 17:25:02 -08001306 * {@hide}
1307 */
1308 public int getLastTetherError(String iface) {
1309 try {
1310 return mService.getLastTetherError(iface);
1311 } catch (RemoteException e) {
1312 return TETHER_ERROR_SERVICE_UNAVAIL;
1313 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001314 }
1315
1316 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001317 * Try to ensure the device stays awake until we connect with the next network.
1318 * Actually just holds a wakelock for a number of seconds while we try to connect
1319 * to any default networks. This will expire if the timeout passes or if we connect
1320 * to a default after this is called. For internal use only.
1321 *
1322 * @param forWhom the name of the network going down for logging purposes
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001323 * @return {@code true} on success, {@code false} on failure
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001324 *
1325 * <p>This method requires the call to hold the permission
1326 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001327 * {@hide}
1328 */
1329 public boolean requestNetworkTransitionWakelock(String forWhom) {
1330 try {
1331 mService.requestNetworkTransitionWakelock(forWhom);
1332 return true;
1333 } catch (RemoteException e) {
1334 return false;
1335 }
1336 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07001337
Robert Greenwalt67fd6c92010-09-09 14:22:59 -07001338 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001339 * Report network connectivity status. This is currently used only
1340 * to alter status bar UI.
1341 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001342 * @param networkType The type of network you want to report on
1343 * @param percentage The quality of the connection 0 is bad, 100 is good
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001344 *
1345 * <p>This method requires the call to hold the permission
1346 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001347 * {@hide}
1348 */
1349 public void reportInetCondition(int networkType, int percentage) {
1350 try {
1351 mService.reportInetCondition(networkType, percentage);
1352 } catch (RemoteException e) {
1353 }
1354 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001355
1356 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001357 * Report a problem network to the framework. This provides a hint to the system
1358 * that there might be connectivity problems on this network and may cause
1359 * the framework to re-evaluate network connectivity and/or switch to another
1360 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001361 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001362 * @param network The {@link Network} the application was attempting to use
1363 * or {@code null} to indicate the current default network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001364 */
1365 public void reportBadNetwork(Network network) {
1366 try {
1367 mService.reportBadNetwork(network);
1368 } catch (RemoteException e) {
1369 }
1370 }
1371
1372 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001373 * Set a network-independent global http proxy. This is not normally what you want
1374 * for typical HTTP proxies - they are general network dependent. However if you're
1375 * doing something unusual like general internal filtering this may be useful. On
1376 * a private network where the proxy is not accessible, you may break HTTP using this.
1377 *
Jason Monk207900c2014-04-25 15:00:09 -04001378 * @param p The a {@link ProxyInfo} object defining the new global
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001379 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
1380 *
1381 * <p>This method requires the call to hold the permission
Jason Monkfaf3fd52014-05-07 18:41:13 -04001382 * android.Manifest.permission#CONNECTIVITY_INTERNAL.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001383 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07001384 */
Jason Monk207900c2014-04-25 15:00:09 -04001385 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001386 try {
1387 mService.setGlobalProxy(p);
1388 } catch (RemoteException e) {
1389 }
1390 }
1391
1392 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001393 * Retrieve any network-independent global HTTP proxy.
1394 *
Jason Monk207900c2014-04-25 15:00:09 -04001395 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001396 * if no global HTTP proxy is set.
1397 *
1398 * <p>This method requires the call to hold the permission
1399 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001400 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07001401 */
Jason Monk207900c2014-04-25 15:00:09 -04001402 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001403 try {
1404 return mService.getGlobalProxy();
1405 } catch (RemoteException e) {
1406 return null;
1407 }
1408 }
1409
1410 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001411 * Get the HTTP proxy settings for the current default network. Note that
1412 * if a global proxy is set, it will override any per-network setting.
1413 *
Jason Monk207900c2014-04-25 15:00:09 -04001414 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001415 * HTTP proxy is active.
1416 *
1417 * <p>This method requires the call to hold the permission
1418 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt434203a2010-10-11 16:00:27 -07001419 * {@hide}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001420 * @deprecated Deprecated in favor of {@link #getLinkProperties}
Robert Greenwalt434203a2010-10-11 16:00:27 -07001421 */
Jason Monk207900c2014-04-25 15:00:09 -04001422 public ProxyInfo getProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001423 try {
1424 return mService.getProxy();
1425 } catch (RemoteException e) {
1426 return null;
1427 }
1428 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001429
1430 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001431 * Sets a secondary requirement bit for the given networkType.
1432 * This requirement bit is generally under the control of the carrier
1433 * or its agents and is not directly controlled by the user.
1434 *
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001435 * @param networkType The network who's dependence has changed
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001436 * @param met Boolean - true if network use is OK, false if not
1437 *
1438 * <p>This method requires the call to hold the permission
1439 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001440 * {@hide}
1441 */
1442 public void setDataDependency(int networkType, boolean met) {
1443 try {
1444 mService.setDataDependency(networkType, met);
1445 } catch (RemoteException e) {
1446 }
1447 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07001448
1449 /**
1450 * Returns true if the hardware supports the given network type
1451 * else it returns false. This doesn't indicate we have coverage
1452 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001453 * hardware supports it. For example a GSM phone without a SIM
1454 * should still return {@code true} for mobile data, but a wifi only
1455 * tablet would return {@code false}.
1456 *
1457 * @param networkType The network type we'd like to check
1458 * @return {@code true} if supported, else {@code false}
1459 *
1460 * <p>This method requires the call to hold the permission
1461 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07001462 * @hide
1463 */
1464 public boolean isNetworkSupported(int networkType) {
1465 try {
1466 return mService.isNetworkSupported(networkType);
1467 } catch (RemoteException e) {}
1468 return false;
1469 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07001470
1471 /**
1472 * Returns if the currently active data network is metered. A network is
1473 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001474 * that connection due to monetary costs, data limitations or
1475 * battery/performance issues. You should check this before doing large
1476 * data transfers, and warn the user or delay the operation until another
1477 * network is available.
1478 *
1479 * @return {@code true} if large transfers should be avoided, otherwise
1480 * {@code false}.
1481 *
1482 * <p>This method requires the call to hold the permission
1483 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07001484 */
1485 public boolean isActiveNetworkMetered() {
1486 try {
1487 return mService.isActiveNetworkMetered();
1488 } catch (RemoteException e) {
1489 return false;
1490 }
1491 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07001492
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001493 /**
1494 * If the LockdownVpn mechanism is enabled, updates the vpn
1495 * with a reload of its profile.
1496 *
1497 * @return a boolean with {@code} indicating success
1498 *
1499 * <p>This method can only be called by the system UID
1500 * {@hide}
1501 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07001502 public boolean updateLockdownVpn() {
1503 try {
1504 return mService.updateLockdownVpn();
1505 } catch (RemoteException e) {
1506 return false;
1507 }
1508 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07001509
1510 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001511 * Signal that the captive portal check on the indicated network
Wink Savilled747cbc2013-08-07 16:22:47 -07001512 * is complete and whether its a captive portal or not.
1513 *
1514 * @param info the {@link NetworkInfo} object for the networkType
1515 * in question.
1516 * @param isCaptivePortal true/false.
1517 *
1518 * <p>This method requires the call to hold the permission
1519 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
1520 * {@hide}
1521 */
1522 public void captivePortalCheckCompleted(NetworkInfo info, boolean isCaptivePortal) {
1523 try {
1524 mService.captivePortalCheckCompleted(info, isCaptivePortal);
1525 } catch (RemoteException e) {
1526 }
1527 }
1528
1529 /**
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001530 * Supply the backend messenger for a network tracker
1531 *
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001532 * @param networkType NetworkType to set
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001533 * @param messenger {@link Messenger}
1534 * {@hide}
1535 */
1536 public void supplyMessenger(int networkType, Messenger messenger) {
1537 try {
1538 mService.supplyMessenger(networkType, messenger);
1539 } catch (RemoteException e) {
1540 }
1541 }
Wink Savilleab9321d2013-06-29 21:10:57 -07001542
1543 /**
Wink Saville948282b2013-08-29 08:55:16 -07001544 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07001545 *
Wink Savilleab9321d2013-06-29 21:10:57 -07001546 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07001547 *
1548 * @return time out that will be used, maybe less that suggestedTimeOutMs
1549 * -1 if an error.
1550 *
1551 * {@hide}
1552 */
Wink Saville948282b2013-08-29 08:55:16 -07001553 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07001554 int timeOutMs = -1;
1555 try {
Wink Saville948282b2013-08-29 08:55:16 -07001556 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07001557 } catch (RemoteException e) {
1558 }
1559 return timeOutMs;
1560 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07001561
1562 /**
Wink Saville42d4f082013-07-20 20:31:59 -07001563 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07001564 * {@hide}
1565 */
1566 public String getMobileProvisioningUrl() {
1567 try {
1568 return mService.getMobileProvisioningUrl();
1569 } catch (RemoteException e) {
1570 }
1571 return null;
1572 }
Wink Saville42d4f082013-07-20 20:31:59 -07001573
1574 /**
1575 * Get the mobile redirected provisioning url.
1576 * {@hide}
1577 */
1578 public String getMobileRedirectedProvisioningUrl() {
1579 try {
1580 return mService.getMobileRedirectedProvisioningUrl();
1581 } catch (RemoteException e) {
1582 }
1583 return null;
1584 }
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001585
1586 /**
1587 * get the information about a specific network link
1588 * @hide
1589 */
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001590 public LinkQualityInfo getLinkQualityInfo(int networkType) {
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001591 try {
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001592 LinkQualityInfo li = mService.getLinkQualityInfo(networkType);
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001593 return li;
1594 } catch (RemoteException e) {
1595 return null;
1596 }
1597 }
1598
1599 /**
1600 * get the information of currently active network link
1601 * @hide
1602 */
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001603 public LinkQualityInfo getActiveLinkQualityInfo() {
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001604 try {
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001605 LinkQualityInfo li = mService.getActiveLinkQualityInfo();
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001606 return li;
1607 } catch (RemoteException e) {
1608 return null;
1609 }
1610 }
1611
1612 /**
1613 * get the information of all network links
1614 * @hide
1615 */
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001616 public LinkQualityInfo[] getAllLinkQualityInfo() {
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001617 try {
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001618 LinkQualityInfo[] li = mService.getAllLinkQualityInfo();
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001619 return li;
1620 } catch (RemoteException e) {
1621 return null;
1622 }
1623 }
Wink Saville7788c612013-08-29 14:57:08 -07001624
1625 /**
Wink Saville948282b2013-08-29 08:55:16 -07001626 * Set sign in error notification to visible or in visible
1627 *
1628 * @param visible
1629 * @param networkType
1630 *
1631 * {@hide}
1632 */
1633 public void setProvisioningNotificationVisible(boolean visible, int networkType,
1634 String extraInfo, String url) {
1635 try {
1636 mService.setProvisioningNotificationVisible(visible, networkType, extraInfo, url);
1637 } catch (RemoteException e) {
1638 }
1639 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07001640
1641 /**
1642 * Set the value for enabling/disabling airplane mode
1643 *
1644 * @param enable whether to enable airplane mode or not
1645 *
1646 * <p>This method requires the call to hold the permission
1647 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
1648 * @hide
1649 */
1650 public void setAirplaneMode(boolean enable) {
1651 try {
1652 mService.setAirplaneMode(enable);
1653 } catch (RemoteException e) {
1654 }
1655 }
Robert Greenwalte049c232014-04-11 15:53:27 -07001656
1657 /** {@hide} */
1658 public void registerNetworkFactory(Messenger messenger) {
1659 try {
1660 mService.registerNetworkFactory(messenger);
1661 } catch (RemoteException e) { }
1662 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07001663
1664 /** {@hide} */
1665 public void registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
1666 NetworkCapabilities nc, int score) {
1667 try {
1668 mService.registerNetworkAgent(messenger, ni, lp, nc, score);
1669 } catch (RemoteException e) { }
1670 }
1671
Robert Greenwalt9258c642014-03-26 16:47:06 -07001672 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001673 * Base class for NetworkRequest callbacks. Used for notifications about network
1674 * changes. Should be extended by applications wanting notifications.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001675 */
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001676 public static class NetworkCallbackListener {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001677 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001678 public static final int PRECHECK = 1;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001679 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001680 public static final int AVAILABLE = 2;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001681 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001682 public static final int LOSING = 3;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001683 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001684 public static final int LOST = 4;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001685 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001686 public static final int UNAVAIL = 5;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001687 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001688 public static final int CAP_CHANGED = 6;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001689 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001690 public static final int PROP_CHANGED = 7;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001691 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001692 public static final int CANCELED = 8;
1693
1694 /**
1695 * @hide
1696 * Called whenever the framework connects to a network that it may use to
1697 * satisfy this request
1698 */
1699 public void onPreCheck(NetworkRequest networkRequest, Network network) {}
1700
1701 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001702 * Called when the framework connects and has declared new network ready for use.
1703 *
1704 * @param networkRequest The {@link NetworkRequest} used to initiate the request.
1705 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07001706 */
1707 public void onAvailable(NetworkRequest networkRequest, Network network) {}
1708
1709 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001710 * Called when the network is about to be disconnected. Often paired with an
1711 * {@link NetworkCallbackListener#onAvailable} call with the new replacement network
1712 * for graceful handover. This may not be called if we have a hard loss
1713 * (loss without warning). This may be followed by either a
1714 * {@link NetworkCallbackListener#onLost} call or a
1715 * {@link NetworkCallbackListener#onAvailable} call for this network depending
1716 * on whether we lose or regain it.
1717 *
1718 * @param networkRequest The {@link NetworkRequest} used to initiate the request.
1719 * @param network The {@link Network} of the failing network.
1720 * @param maxSecToLive The time in seconds the framework will attempt to keep the
1721 * network connected. Note that the network may suffers a
1722 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07001723 */
1724 public void onLosing(NetworkRequest networkRequest, Network network, int maxSecToLive) {}
1725
1726 /**
1727 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001728 * graceful failure ends.
1729 *
1730 * @param networkRequest The {@link NetworkRequest} used to initiate the request.
1731 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07001732 */
1733 public void onLost(NetworkRequest networkRequest, Network network) {}
1734
1735 /**
1736 * Called if no network is found in the given timeout time. If no timeout is given,
1737 * this will not be called.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001738 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07001739 */
1740 public void onUnavailable(NetworkRequest networkRequest) {}
1741
1742 /**
1743 * Called when the network the framework connected to for this request
1744 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001745 *
1746 * @param networkRequest The {@link NetworkRequest} used to initiate the request.
1747 * @param network The {@link Network} whose capabilities have changed.
1748 * @param networkCapabilities The new {@link NetworkCapabilities} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07001749 */
Robert Greenwalt9258c642014-03-26 16:47:06 -07001750 public void onNetworkCapabilitiesChanged(NetworkRequest networkRequest, Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07001751 NetworkCapabilities networkCapabilities) {}
1752
1753 /**
1754 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001755 * changes {@link LinkProperties}.
1756 *
1757 * @param networkRequest The {@link NetworkRequest} used to initiate the request.
1758 * @param network The {@link Network} whose link properties have changed.
1759 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07001760 */
Robert Greenwalt9258c642014-03-26 16:47:06 -07001761 public void onLinkPropertiesChanged(NetworkRequest networkRequest, Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07001762 LinkProperties linkProperties) {}
1763
1764 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001765 * Called when a {@link #releaseNetworkRequest} call concludes and the registered
1766 * callbacks will no longer be used.
1767 *
1768 * @param networkRequest The {@link NetworkRequest} used to initiate the request.
Robert Greenwalt7b816022014-04-18 15:25:25 -07001769 */
Robert Greenwalt9258c642014-03-26 16:47:06 -07001770 public void onReleased(NetworkRequest networkRequest) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07001771 }
1772
Robert Greenwalt9258c642014-03-26 16:47:06 -07001773 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
1774 /** @hide obj = pair(NetworkRequest, Network) */
1775 public static final int CALLBACK_PRECHECK = BASE + 1;
1776 /** @hide obj = pair(NetworkRequest, Network) */
1777 public static final int CALLBACK_AVAILABLE = BASE + 2;
1778 /** @hide obj = pair(NetworkRequest, Network), arg1 = ttl */
1779 public static final int CALLBACK_LOSING = BASE + 3;
1780 /** @hide obj = pair(NetworkRequest, Network) */
1781 public static final int CALLBACK_LOST = BASE + 4;
1782 /** @hide obj = NetworkRequest */
1783 public static final int CALLBACK_UNAVAIL = BASE + 5;
1784 /** @hide obj = pair(NetworkRequest, Network) */
1785 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
1786 /** @hide obj = pair(NetworkRequest, Network) */
1787 public static final int CALLBACK_IP_CHANGED = BASE + 7;
1788 /** @hide obj = NetworkRequest */
1789 public static final int CALLBACK_RELEASED = BASE + 8;
1790 /** @hide */
1791 public static final int CALLBACK_EXIT = BASE + 9;
1792
1793 private static class CallbackHandler extends Handler {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001794 private final HashMap<NetworkRequest, NetworkCallbackListener>mCallbackMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001795 private final AtomicInteger mRefCount;
1796 private static final String TAG = "ConnectivityManager.CallbackHandler";
1797 private final ConnectivityManager mCm;
1798
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001799 CallbackHandler(Looper looper, HashMap<NetworkRequest, NetworkCallbackListener>callbackMap,
Robert Greenwalt9258c642014-03-26 16:47:06 -07001800 AtomicInteger refCount, ConnectivityManager cm) {
1801 super(looper);
1802 mCallbackMap = callbackMap;
1803 mRefCount = refCount;
1804 mCm = cm;
1805 }
1806
1807 @Override
1808 public void handleMessage(Message message) {
1809 Log.d(TAG, "CM callback handler got msg " + message.what);
1810 switch (message.what) {
1811 case CALLBACK_PRECHECK: {
1812 NetworkRequest request = getNetworkRequest(message);
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001813 NetworkCallbackListener callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07001814 if (callbacks != null) {
1815 callbacks.onPreCheck(request, getNetwork(message));
1816 } else {
1817 Log.e(TAG, "callback not found for PRECHECK message");
1818 }
1819 break;
1820 }
1821 case CALLBACK_AVAILABLE: {
1822 NetworkRequest request = getNetworkRequest(message);
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001823 NetworkCallbackListener callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07001824 if (callbacks != null) {
1825 callbacks.onAvailable(request, getNetwork(message));
1826 } else {
1827 Log.e(TAG, "callback not found for AVAILABLE message");
1828 }
1829 break;
1830 }
1831 case CALLBACK_LOSING: {
1832 NetworkRequest request = getNetworkRequest(message);
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001833 NetworkCallbackListener callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07001834 if (callbacks != null) {
1835 callbacks.onLosing(request, getNetwork(message), message.arg1);
1836 } else {
1837 Log.e(TAG, "callback not found for LOSING message");
1838 }
1839 break;
1840 }
1841 case CALLBACK_LOST: {
1842 NetworkRequest request = getNetworkRequest(message);
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001843 NetworkCallbackListener callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07001844 if (callbacks != null) {
1845 callbacks.onLost(request, getNetwork(message));
1846 } else {
1847 Log.e(TAG, "callback not found for LOST message");
1848 }
1849 break;
1850 }
1851 case CALLBACK_UNAVAIL: {
1852 NetworkRequest req = (NetworkRequest)message.obj;
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001853 NetworkCallbackListener callbacks = null;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001854 synchronized(mCallbackMap) {
1855 callbacks = mCallbackMap.get(req);
1856 }
1857 if (callbacks != null) {
1858 callbacks.onUnavailable(req);
1859 } else {
1860 Log.e(TAG, "callback not found for UNAVAIL message");
1861 }
1862 break;
1863 }
1864 case CALLBACK_CAP_CHANGED: {
1865 NetworkRequest request = getNetworkRequest(message);
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001866 NetworkCallbackListener callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07001867 if (callbacks != null) {
1868 Network network = getNetwork(message);
1869 NetworkCapabilities cap = mCm.getNetworkCapabilities(network);
1870
1871 callbacks.onNetworkCapabilitiesChanged(request, network, cap);
1872 } else {
1873 Log.e(TAG, "callback not found for CHANGED message");
1874 }
1875 break;
1876 }
1877 case CALLBACK_IP_CHANGED: {
1878 NetworkRequest request = getNetworkRequest(message);
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001879 NetworkCallbackListener callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07001880 if (callbacks != null) {
1881 Network network = getNetwork(message);
1882 LinkProperties lp = mCm.getLinkProperties(network);
1883
1884 callbacks.onLinkPropertiesChanged(request, network, lp);
1885 } else {
1886 Log.e(TAG, "callback not found for CHANGED message");
1887 }
1888 break;
1889 }
1890 case CALLBACK_RELEASED: {
1891 NetworkRequest req = (NetworkRequest)message.obj;
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001892 NetworkCallbackListener callbacks = null;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001893 synchronized(mCallbackMap) {
1894 callbacks = mCallbackMap.remove(req);
1895 }
1896 if (callbacks != null) {
1897 callbacks.onReleased(req);
1898 } else {
1899 Log.e(TAG, "callback not found for CANCELED message");
1900 }
1901 synchronized(mRefCount) {
1902 if (mRefCount.decrementAndGet() == 0) {
1903 getLooper().quit();
1904 }
1905 }
1906 break;
1907 }
1908 case CALLBACK_EXIT: {
1909 Log.d(TAG, "Listener quiting");
1910 getLooper().quit();
1911 break;
1912 }
1913 }
1914 }
1915
1916 private NetworkRequest getNetworkRequest(Message msg) {
1917 return (NetworkRequest)(msg.obj);
1918 }
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001919 private NetworkCallbackListener getCallbacks(NetworkRequest req) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001920 synchronized(mCallbackMap) {
1921 return mCallbackMap.get(req);
1922 }
1923 }
1924 private Network getNetwork(Message msg) {
1925 return new Network(msg.arg2);
1926 }
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001927 private NetworkCallbackListener removeCallbacks(Message msg) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001928 NetworkRequest req = (NetworkRequest)msg.obj;
1929 synchronized(mCallbackMap) {
1930 return mCallbackMap.remove(req);
1931 }
1932 }
1933 }
1934
1935 private void addCallbackListener() {
1936 synchronized(sCallbackRefCount) {
1937 if (sCallbackRefCount.incrementAndGet() == 1) {
1938 // TODO - switch this over to a ManagerThread or expire it when done
1939 HandlerThread callbackThread = new HandlerThread("ConnectivityManager");
1940 callbackThread.start();
1941 sCallbackHandler = new CallbackHandler(callbackThread.getLooper(),
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001942 sNetworkCallbackListener, sCallbackRefCount, this);
Robert Greenwalt9258c642014-03-26 16:47:06 -07001943 }
1944 }
1945 }
1946
1947 private void removeCallbackListener() {
1948 synchronized(sCallbackRefCount) {
1949 if (sCallbackRefCount.decrementAndGet() == 0) {
1950 sCallbackHandler.obtainMessage(CALLBACK_EXIT).sendToTarget();
1951 sCallbackHandler = null;
1952 }
1953 }
1954 }
1955
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001956 static final HashMap<NetworkRequest, NetworkCallbackListener> sNetworkCallbackListener =
1957 new HashMap<NetworkRequest, NetworkCallbackListener>();
Robert Greenwalt9258c642014-03-26 16:47:06 -07001958 static final AtomicInteger sCallbackRefCount = new AtomicInteger(0);
1959 static CallbackHandler sCallbackHandler = null;
1960
1961 private final static int LISTEN = 1;
1962 private final static int REQUEST = 2;
1963
1964 private NetworkRequest somethingForNetwork(NetworkCapabilities need,
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001965 NetworkCallbackListener networkCallbackListener, int timeoutSec, int action) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001966 NetworkRequest networkRequest = null;
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001967 if (networkCallbackListener == null) {
1968 throw new IllegalArgumentException("null NetworkCallbackListener");
1969 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07001970 if (need == null) throw new IllegalArgumentException("null NetworkCapabilities");
1971 try {
1972 addCallbackListener();
1973 if (action == LISTEN) {
1974 networkRequest = mService.listenForNetwork(need, new Messenger(sCallbackHandler),
1975 new Binder());
1976 } else {
1977 networkRequest = mService.requestNetwork(need, new Messenger(sCallbackHandler),
1978 timeoutSec, new Binder());
1979 }
1980 if (networkRequest != null) {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001981 synchronized(sNetworkCallbackListener) {
1982 sNetworkCallbackListener.put(networkRequest, networkCallbackListener);
Robert Greenwalt9258c642014-03-26 16:47:06 -07001983 }
1984 }
1985 } catch (RemoteException e) {}
1986 if (networkRequest == null) removeCallbackListener();
1987 return networkRequest;
1988 }
1989
1990 /**
1991 * Request a network to satisfy a set of {@link NetworkCapabilities}.
1992 *
1993 * This {@link NetworkRequest} will live until released via
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001994 * {@link #releaseNetworkRequest} or the calling application exits.
1995 * Status of the request can be followed by listening to the various
1996 * callbacks described in {@link NetworkCallbackListener}. The {@link Network}
1997 * can be used to direct traffic to the network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001998 *
1999 * @param need {@link NetworkCapabilities} required by this request.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002000 * @param networkCallbackListener The {@link NetworkCallbackListener} to be utilized for this
2001 * request. Note the callbacks can be shared by multiple
2002 * requests and the NetworkRequest token utilized to
2003 * determine to which request the callback relates.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002004 * @return A {@link NetworkRequest} object identifying the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002005 */
2006 public NetworkRequest requestNetwork(NetworkCapabilities need,
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002007 NetworkCallbackListener networkCallbackListener) {
2008 return somethingForNetwork(need, networkCallbackListener, 0, REQUEST);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002009 }
2010
2011 /**
2012 * Request a network to satisfy a set of {@link NetworkCapabilities}, limited
2013 * by a timeout.
2014 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002015 * This function behaves identically to the non-timedout version, but if a suitable
2016 * network is not found within the given time (in Seconds) the
2017 * {@link NetworkCallbackListener#unavailable} callback is called. The request must
2018 * still be released normally by calling {@link releaseNetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002019 * @param need {@link NetworkCapabilities} required by this request.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002020 * @param networkCallbackListener The callbacks to be utilized for this request. Note
Robert Greenwalt9258c642014-03-26 16:47:06 -07002021 * the callbacks can be shared by multiple requests and
2022 * the NetworkRequest token utilized to determine to which
2023 * request the callback relates.
2024 * @param timeoutSec The time in seconds to attempt looking for a suitable network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002025 * before {@link NetworkCallbackListener#unavailable} is called.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002026 * @return A {@link NetworkRequest} object identifying the request.
2027 * @hide
2028 */
2029 public NetworkRequest requestNetwork(NetworkCapabilities need,
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002030 NetworkCallbackListener networkCallbackListener, int timeoutSec) {
2031 return somethingForNetwork(need, networkCallbackListener, timeoutSec, REQUEST);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002032 }
2033
2034 /**
2035 * The maximum number of seconds the framework will look for a suitable network
2036 * during a timeout-equiped call to {@link requestNetwork}.
2037 * {@hide}
2038 */
2039 public final static int MAX_NETWORK_REQUEST_TIMEOUT_SEC = 100 * 60;
2040
2041 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002042 * The lookup key for a {@link Network} object included with the intent after
2043 * succesfully finding a network for the applications request. Retrieve it with
2044 * {@link android.content.Intent#getParcelableExtra(String)}.
2045 */
2046 public static final String EXTRA_NETWORK_REQUEST_NETWORK = "networkRequestNetwork";
2047
2048 /**
2049 * The lookup key for a {@link NetworkCapabilities} object included with the intent after
2050 * succesfully finding a network for the applications request. Retrieve it with
2051 * {@link android.content.Intent#getParcelableExtra(String)}.
2052 */
2053 public static final String EXTRA_NETWORK_REQUEST_NETWORK_CAPABILITIES =
2054 "networkRequestNetworkCapabilities";
2055
2056
2057 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07002058 * Request a network to satisfy a set of {@link NetworkCapabilities}.
2059 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002060 * This function behavies identically to the callback-equiped version, but instead
2061 * of {@link NetworkCallbackListener} a {@link PendingIntent} is used. This means
2062 * the request may outlive the calling application and get called back when a suitable
2063 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002064 * <p>
2065 * The operation is an Intent broadcast that goes to a broadcast receiver that
2066 * you registered with {@link Context#registerReceiver} or through the
2067 * &lt;receiver&gt; tag in an AndroidManifest.xml file
2068 * <p>
2069 * The operation Intent is delivered with two extras, a {@link Network} typed
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002070 * extra called {@link #EXTRA_NETWORK_REQUEST_NETWORK} and a {@link NetworkCapabilities}
2071 * typed extra called {@link #EXTRA_NETWORK_REQUEST_NETWORK_CAPABILITIES} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07002072 * the original requests parameters. It is important to create a new,
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002073 * {@link NetworkCallbackListener} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07002074 * Intent to reserve the network or it will be released shortly after the Intent
2075 * is processed.
2076 * <p>
2077 * If there is already an request for this Intent registered (with the equality of
2078 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002079 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002080 * <p>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002081 * The request may be released normally by calling {@link #releaseNetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002082 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002083 * @param need {@link NetworkCapabilities} required by this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002084 * @param operation Action to perform when the network is available (corresponds
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002085 * to the {@link NetworkCallbackListener#onAvailable} call. Typically
Robert Greenwalt9258c642014-03-26 16:47:06 -07002086 * comes from {@link PendingIntent#getBroadcast}.
2087 * @return A {@link NetworkRequest} object identifying the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002088 */
2089 public NetworkRequest requestNetwork(NetworkCapabilities need, PendingIntent operation) {
2090 try {
2091 return mService.pendingRequestForNetwork(need, operation);
2092 } catch (RemoteException e) {}
2093 return null;
2094 }
2095
2096 /**
2097 * Registers to receive notifications about all networks which satisfy the given
2098 * {@link NetworkCapabilities}. The callbacks will continue to be called until
2099 * either the application exits or the request is released using
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002100 * {@link #releaseNetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002101 *
2102 * @param need {@link NetworkCapabilities} required by this request.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002103 * @param networkCallbackListener The {@link NetworkCallbackListener} to be called as suitable
Robert Greenwalt9258c642014-03-26 16:47:06 -07002104 * networks change state.
2105 * @return A {@link NetworkRequest} object identifying the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002106 */
2107 public NetworkRequest listenForNetwork(NetworkCapabilities need,
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002108 NetworkCallbackListener networkCallbackListener) {
2109 return somethingForNetwork(need, networkCallbackListener, 0, LISTEN);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002110 }
2111
2112 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002113 * Releases a {@link NetworkRequest} generated either through a {@link #requestNetwork}
2114 * or a {@link #listenForNetwork} call. The {@link NetworkCallbackListener} given in the
2115 * earlier call may continue receiving calls until the
2116 * {@link NetworkCallbackListener#onReleased} function is called, signifying the end
2117 * of the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002118 *
2119 * @param networkRequest The {@link NetworkRequest} generated by an earlier call to
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002120 * {@link #requestNetwork} or {@link #listenForNetwork}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002121 */
2122 public void releaseNetworkRequest(NetworkRequest networkRequest) {
2123 if (networkRequest == null) throw new IllegalArgumentException("null NetworkRequest");
2124 try {
2125 mService.releaseNetworkRequest(networkRequest);
2126 } catch (RemoteException e) {}
2127 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002128}