blob: b96f16646c5be3441467c0d48098e403464cf056 [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;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070038import android.telephony.TelephonyManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080039import android.util.ArrayMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -070040import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080041
Robert Greenwaltafa05c02014-05-21 20:04:36 -070042import com.android.internal.telephony.ITelephony;
Robert Greenwalt562cc542014-05-15 18:07:26 -070043import com.android.internal.telephony.PhoneConstants;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070044import com.android.internal.util.Protocol;
45
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070046import java.net.InetAddress;
Robert Greenwalt9258c642014-03-26 16:47:06 -070047import java.util.concurrent.atomic.AtomicInteger;
48import java.util.HashMap;
49
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080050/**
51 * Class that answers queries about the state of network connectivity. It also
52 * notifies applications when network connectivity changes. Get an instance
53 * of this class by calling
54 * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}.
55 * <p>
56 * The primary responsibilities of this class are to:
57 * <ol>
58 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
59 * <li>Send broadcast intents when network connectivity changes</li>
60 * <li>Attempt to "fail over" to another network when connectivity to a network
61 * is lost</li>
62 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
63 * state of the available networks</li>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070064 * <li>Provide an API that allows applications to request and select networks for their data
65 * traffic</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080066 * </ol>
67 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070068public class ConnectivityManager {
69 private static final String TAG = "ConnectivityManager";
70
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080071 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070072 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 * been established or lost. The NetworkInfo for the affected network is
74 * sent as an extra; it should be consulted to see what kind of
75 * connectivity event occurred.
76 * <p/>
77 * If this is a connection that was the result of failing over from a
78 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
79 * set to true.
80 * <p/>
81 * For a loss of connectivity, if the connectivity manager is attempting
82 * to connect (or has already connected) to another network, the
83 * NetworkInfo for the new network is also passed as an extra. This lets
84 * any receivers of the broadcast know that they should not necessarily
85 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -080086 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080087 * the failover attempt succeeded (and so there is still overall data
88 * connectivity), or that the failover attempt failed, meaning that all
89 * connectivity has been lost.
90 * <p/>
91 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
92 * is set to {@code true} if there are no connected networks at all.
93 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -080094 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -070096
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 /**
Jeff Sharkey961e3042011-08-29 16:02:57 -070098 * Identical to {@link #CONNECTIVITY_ACTION} broadcast, but sent without any
Dianne Hackborn77b987f2014-02-26 16:20:52 -080099 * applicable {@link Settings.Global#CONNECTIVITY_CHANGE_DELAY}.
Jeff Sharkey961e3042011-08-29 16:02:57 -0700100 *
101 * @hide
102 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800103 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey961e3042011-08-29 16:02:57 -0700104 public static final String CONNECTIVITY_ACTION_IMMEDIATE =
105 "android.net.conn.CONNECTIVITY_CHANGE_IMMEDIATE";
106
107 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800108 * The lookup key for a {@link NetworkInfo} object. Retrieve with
109 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700110 *
111 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
112 * should always obtain network information through
113 * {@link #getActiveNetworkInfo()} or
114 * {@link #getAllNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700115 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800116 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700117 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700119
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700121 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
122 * Can be used with {@link #getNetworkInfo(int)} to get {@link NetworkInfo}
123 * state based on the calling application.
124 *
125 * @see android.content.Intent#getIntExtra(String, int)
126 */
127 public static final String EXTRA_NETWORK_TYPE = "networkType";
128
129 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800130 * The lookup key for a boolean that indicates whether a connect event
131 * is for a network to which the connectivity manager was failing over
132 * following a disconnect on another network.
133 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
134 */
135 public static final String EXTRA_IS_FAILOVER = "isFailover";
136 /**
137 * The lookup key for a {@link NetworkInfo} object. This is supplied when
138 * there is another network that it may be possible to connect to. Retrieve with
139 * {@link android.content.Intent#getParcelableExtra(String)}.
140 */
141 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
142 /**
143 * The lookup key for a boolean that indicates whether there is a
144 * complete lack of connectivity, i.e., no network is available.
145 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
146 */
147 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
148 /**
149 * The lookup key for a string that indicates why an attempt to connect
150 * to a network failed. The string has no particular structure. It is
151 * intended to be used in notifications presented to users. Retrieve
152 * it with {@link android.content.Intent#getStringExtra(String)}.
153 */
154 public static final String EXTRA_REASON = "reason";
155 /**
156 * The lookup key for a string that provides optionally supplied
157 * extra information about the network state. The information
158 * may be passed up from the lower networking layers, and its
159 * meaning may be specific to a particular network type. Retrieve
160 * it with {@link android.content.Intent#getStringExtra(String)}.
161 */
162 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700163 /**
164 * The lookup key for an int that provides information about
165 * our connection to the internet at large. 0 indicates no connection,
166 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700167 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700168 * {@hide}
169 */
170 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171
172 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700173 * Broadcast action to indicate the change of data activity status
174 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800175 * The network becomes active when data transmission is started, or
176 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700177 * {@hide}
178 */
179 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
180 public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
181 /**
182 * The lookup key for an enum that indicates the network device type on which this data activity
183 * change happens.
184 * {@hide}
185 */
186 public static final String EXTRA_DEVICE_TYPE = "deviceType";
187 /**
188 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
189 * it is actively sending or receiving data and {@code false} means it is idle.
190 * {@hide}
191 */
192 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700193 /**
194 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
195 * {@hide}
196 */
197 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700198
199 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800200 * Broadcast Action: The setting for background data usage has changed
201 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
202 * <p>
203 * If an application uses the network in the background, it should listen
204 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700205 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800206 * <p>
207 *
208 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
209 * of background data depends on several combined factors, and
210 * this broadcast is no longer sent. Instead, when background
211 * data is unavailable, {@link #getActiveNetworkInfo()} will now
212 * appear disconnected. During first boot after a platform
213 * upgrade, this broadcast will be sent once if
214 * {@link #getBackgroundDataSetting()} was {@code false} before
215 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800216 */
217 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800218 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800219 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
220 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
221
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700222 /**
223 * Broadcast Action: The network connection may not be good
224 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
225 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
226 * the network and it's condition.
227 * @hide
228 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800229 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700230 public static final String INET_CONDITION_ACTION =
231 "android.net.conn.INET_CONDITION_ACTION";
232
Robert Greenwalt42acef32009-08-12 16:08:25 -0700233 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800234 * Broadcast Action: A tetherable connection has come or gone.
235 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
236 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER} and
237 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
238 * the current state of tethering. Each include a list of
239 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800240 * @hide
241 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800242 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800243 public static final String ACTION_TETHER_STATE_CHANGED =
244 "android.net.conn.TETHER_STATE_CHANGED";
245
246 /**
247 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800248 * gives a String[] listing all the interfaces configured for
249 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800250 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800251 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800252
253 /**
254 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800255 * gives a String[] listing all the interfaces currently tethered
256 * (ie, has dhcp support and packets potentially forwarded/NATed)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800257 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800258 public static final String EXTRA_ACTIVE_TETHER = "activeArray";
259
260 /**
261 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800262 * gives a String[] listing all the interfaces we tried to tether and
263 * failed. Use {@link #getLastTetherError} to find the error code
264 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800265 */
266 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800267
268 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800269 * Broadcast Action: The captive portal tracker has finished its test.
270 * Sent only while running Setup Wizard, in lieu of showing a user
271 * notification.
272 * @hide
273 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800274 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800275 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
276 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
277 /**
278 * The lookup key for a boolean that indicates whether a captive portal was detected.
279 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
280 * @hide
281 */
282 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
283
284 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800285 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700286 * @hide
287 */
288 public static final int TYPE_NONE = -1;
289
290 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800291 * The Mobile data connection. When active, all data traffic
292 * will use this network type's interface by default
293 * (it has a default route)
Robert Greenwalt42acef32009-08-12 16:08:25 -0700294 */
295 public static final int TYPE_MOBILE = 0;
296 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800297 * The WIFI data connection. When active, all data traffic
298 * will use this network type's interface by default
299 * (it has a default route).
Robert Greenwalt42acef32009-08-12 16:08:25 -0700300 */
301 public static final int TYPE_WIFI = 1;
302 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800303 * An MMS-specific Mobile data connection. This network type may use the
304 * same network interface as {@link #TYPE_MOBILE} or it may use a different
305 * one. This is used by applications needing to talk to the carrier's
306 * Multimedia Messaging Service servers.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700307 */
308 public static final int TYPE_MOBILE_MMS = 2;
309 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800310 * A SUPL-specific Mobile data connection. This network type may use the
311 * same network interface as {@link #TYPE_MOBILE} or it may use a different
312 * one. This is used by applications needing to talk to the carrier's
313 * Secure User Plane Location servers for help locating the device.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700314 */
315 public static final int TYPE_MOBILE_SUPL = 3;
316 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800317 * A DUN-specific Mobile data connection. This network type may use the
318 * same network interface as {@link #TYPE_MOBILE} or it may use a different
319 * one. This is sometimes by the system when setting up an upstream connection
320 * for tethering so that the carrier is aware of DUN traffic.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700321 */
322 public static final int TYPE_MOBILE_DUN = 4;
323 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800324 * A High Priority Mobile data connection. This network type uses the
325 * same network interface as {@link #TYPE_MOBILE} but the routing setup
326 * is different. Only requesting processes will have access to the
327 * Mobile DNS servers and only IP's explicitly requested via {@link #requestRouteToHost}
328 * will route over this interface if no default route exists.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700329 */
330 public static final int TYPE_MOBILE_HIPRI = 5;
jsh8214deb2010-03-11 15:04:43 -0800331 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800332 * The WiMAX data connection. When active, all data traffic
333 * will use this network type's interface by default
334 * (it has a default route).
jsh8214deb2010-03-11 15:04:43 -0800335 */
336 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800337
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800338 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800339 * The Bluetooth data connection. When active, all data traffic
340 * will use this network type's interface by default
341 * (it has a default route).
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800342 */
343 public static final int TYPE_BLUETOOTH = 7;
344
Robert Greenwalt60810842011-04-22 15:28:18 -0700345 /**
346 * Dummy data connection. This should not be used on shipping devices.
347 */
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800348 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800349
Robert Greenwalt60810842011-04-22 15:28:18 -0700350 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800351 * The Ethernet data connection. When active, all data traffic
352 * will use this network type's interface by default
353 * (it has a default route).
Robert Greenwalt60810842011-04-22 15:28:18 -0700354 */
Robert Greenwalte12aec92011-01-28 14:48:37 -0800355 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700356
Wink Saville9d7d6282011-03-12 14:52:01 -0800357 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800358 * Over the air Administration.
Wink Saville9d7d6282011-03-12 14:52:01 -0800359 * {@hide}
360 */
361 public static final int TYPE_MOBILE_FOTA = 10;
362
363 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800364 * IP Multimedia Subsystem.
Wink Saville9d7d6282011-03-12 14:52:01 -0800365 * {@hide}
366 */
367 public static final int TYPE_MOBILE_IMS = 11;
368
369 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800370 * Carrier Branded Services.
Wink Saville9d7d6282011-03-12 14:52:01 -0800371 * {@hide}
372 */
373 public static final int TYPE_MOBILE_CBS = 12;
374
repo syncaea743a2011-07-29 23:55:49 -0700375 /**
376 * A Wi-Fi p2p connection. Only requesting processes will have access to
377 * the peers connected.
378 * {@hide}
379 */
380 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800381
Wink Saville5e56bc52013-07-29 15:00:57 -0700382 /**
383 * The network to use for initially attaching to the network
384 * {@hide}
385 */
386 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700387
Hui Lu1c5624a2014-01-15 11:05:36 -0500388 /**
389 * The network that uses proxy to achieve connectivity.
390 * {@hide}
391 */
392 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700393
394 /** {@hide} */
Hui Lu1c5624a2014-01-15 11:05:36 -0500395 public static final int MAX_RADIO_TYPE = TYPE_PROXY;
396
397 /** {@hide} */
398 public static final int MAX_NETWORK_TYPE = TYPE_PROXY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800399
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800400 /**
401 * If you want to set the default network preference,you can directly
402 * change the networkAttributes array in framework's config.xml.
403 *
404 * @deprecated Since we support so many more networks now, the single
405 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800406 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800407 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800408 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800409 * from an App.
410 */
411 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
413
Jeff Sharkey625239a2012-09-26 22:03:49 -0700414 /**
415 * Default value for {@link Settings.Global#CONNECTIVITY_CHANGE_DELAY} in
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800416 * milliseconds. This was introduced because IPv6 routes seem to take a
417 * moment to settle - trying network activity before the routes are adjusted
418 * can lead to packets using the wrong interface or having the wrong IP address.
419 * This delay is a bit crude, but in the future hopefully we will have kernel
420 * notifications letting us know when it's safe to use the new network.
Jeff Sharkey625239a2012-09-26 22:03:49 -0700421 *
422 * @hide
423 */
424 public static final int CONNECTIVITY_CHANGE_DELAY_DEFAULT = 3000;
425
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700426 /**
427 * @hide
428 */
429 public final static int INVALID_NET_ID = 0;
430
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700431 private final IConnectivityManager mService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800432
Chad Brubakerf81daa92014-02-14 13:22:34 -0800433 private final String mPackageName;
434
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800435 private INetworkManagementService mNMService;
436
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800437 /**
438 * Tests if a given integer represents a valid network type.
439 * @param networkType the type to be tested
440 * @return a boolean. {@code true} if the type is valid, else {@code false}
441 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700442 public static boolean isNetworkTypeValid(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700443 return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800444 }
445
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800446 /**
447 * Returns a non-localized string representing a given network type.
448 * ONLY used for debugging output.
449 * @param type the type needing naming
450 * @return a String for the given type, or a string version of the type ("87")
451 * if no name is known.
452 * {@hide}
453 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700454 public static String getNetworkTypeName(int type) {
455 switch (type) {
456 case TYPE_MOBILE:
457 return "MOBILE";
458 case TYPE_WIFI:
459 return "WIFI";
460 case TYPE_MOBILE_MMS:
461 return "MOBILE_MMS";
462 case TYPE_MOBILE_SUPL:
463 return "MOBILE_SUPL";
464 case TYPE_MOBILE_DUN:
465 return "MOBILE_DUN";
466 case TYPE_MOBILE_HIPRI:
467 return "MOBILE_HIPRI";
468 case TYPE_WIMAX:
469 return "WIMAX";
470 case TYPE_BLUETOOTH:
471 return "BLUETOOTH";
472 case TYPE_DUMMY:
473 return "DUMMY";
474 case TYPE_ETHERNET:
475 return "ETHERNET";
476 case TYPE_MOBILE_FOTA:
477 return "MOBILE_FOTA";
478 case TYPE_MOBILE_IMS:
479 return "MOBILE_IMS";
480 case TYPE_MOBILE_CBS:
481 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700482 case TYPE_WIFI_P2P:
483 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700484 case TYPE_MOBILE_IA:
485 return "MOBILE_IA";
Hui Lu1c5624a2014-01-15 11:05:36 -0500486 case TYPE_PROXY:
487 return "PROXY";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700488 default:
489 return Integer.toString(type);
490 }
491 }
492
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800493 /**
494 * Checks if a given type uses the cellular data connection.
495 * This should be replaced in the future by a network property.
496 * @param networkType the type to check
497 * @return a boolean - {@code true} if uses cellular network, else {@code false}
498 * {@hide}
499 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700500 public static boolean isNetworkTypeMobile(int networkType) {
501 switch (networkType) {
502 case TYPE_MOBILE:
503 case TYPE_MOBILE_MMS:
504 case TYPE_MOBILE_SUPL:
505 case TYPE_MOBILE_DUN:
506 case TYPE_MOBILE_HIPRI:
507 case TYPE_MOBILE_FOTA:
508 case TYPE_MOBILE_IMS:
509 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700510 case TYPE_MOBILE_IA:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700511 return true;
512 default:
513 return false;
514 }
515 }
516
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800517 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700518 * Checks if the given network type is backed by a Wi-Fi radio.
519 *
520 * @hide
521 */
522 public static boolean isNetworkTypeWifi(int networkType) {
523 switch (networkType) {
524 case TYPE_WIFI:
525 case TYPE_WIFI_P2P:
526 return true;
527 default:
528 return false;
529 }
530 }
531
532 /**
Chad Brubakerf336d722013-07-15 16:34:04 -0700533 * Checks if the given network type should be exempt from VPN routing rules
534 *
535 * @hide
536 */
537 public static boolean isNetworkTypeExempt(int networkType) {
538 switch (networkType) {
539 case TYPE_MOBILE_MMS:
540 case TYPE_MOBILE_SUPL:
541 case TYPE_MOBILE_HIPRI:
Wink Saville5e56bc52013-07-29 15:00:57 -0700542 case TYPE_MOBILE_IA:
Chad Brubakerf336d722013-07-15 16:34:04 -0700543 return true;
544 default:
545 return false;
546 }
547 }
548
549 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800550 * Specifies the preferred network type. When the device has more
551 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800552 *
553 * @param preference the network type to prefer over all others. It is
554 * unspecified what happens to the old preferred network in the
555 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700556 * @deprecated Functionality has been removed as it no longer makes sense,
557 * with many more than two networks - we'd need an array to express
558 * preference. Instead we use dynamic network properties of
559 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800560 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800561 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 }
563
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800564 /**
565 * Retrieves the current preferred network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800566 *
567 * @return an integer representing the preferred network type
568 *
569 * <p>This method requires the caller to hold the permission
570 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700571 * @deprecated Functionality has been removed as it no longer makes sense,
572 * with many more than two networks - we'd need an array to express
573 * preference. Instead we use dynamic network properties of
574 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800575 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800576 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700577 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800578 }
579
Scott Main671644c2011-10-06 19:02:28 -0700580 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800581 * Returns details about the currently active default data network. When
582 * connected, this network is the default route for outgoing connections.
583 * You should always check {@link NetworkInfo#isConnected()} before initiating
584 * network traffic. This may return {@code null} when there is no default
585 * network.
586 *
587 * @return a {@link NetworkInfo} object for the current default network
588 * or {@code null} if no network default network is currently active
589 *
590 * <p>This method requires the call to hold the permission
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700591 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700592 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800593 public NetworkInfo getActiveNetworkInfo() {
594 try {
595 return mService.getActiveNetworkInfo();
596 } catch (RemoteException e) {
597 return null;
598 }
599 }
600
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800601 /**
602 * Returns details about the currently active default data network
603 * for a given uid. This is for internal use only to avoid spying
604 * other apps.
605 *
606 * @return a {@link NetworkInfo} object for the current default network
607 * for the given uid or {@code null} if no default network is
608 * available for the specified uid.
609 *
610 * <p>This method requires the caller to hold the permission
611 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
612 * {@hide}
613 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700614 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
615 try {
616 return mService.getActiveNetworkInfoForUid(uid);
617 } catch (RemoteException e) {
618 return null;
619 }
620 }
621
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800622 /**
623 * Returns connection status information about a particular
624 * network type.
625 *
626 * @param networkType integer specifying which networkType in
627 * which you're interested.
628 * @return a {@link NetworkInfo} object for the requested
629 * network type or {@code null} if the type is not
630 * supported by the device.
631 *
632 * <p>This method requires the call to hold the permission
633 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
634 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800635 public NetworkInfo getNetworkInfo(int networkType) {
636 try {
637 return mService.getNetworkInfo(networkType);
638 } catch (RemoteException e) {
639 return null;
640 }
641 }
642
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800643 /**
644 * Returns connection status information about all network
645 * types supported by the device.
646 *
647 * @return an array of {@link NetworkInfo} objects. Check each
648 * {@link NetworkInfo#getType} for which type each applies.
649 *
650 * <p>This method requires the call to hold the permission
651 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
652 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800653 public NetworkInfo[] getAllNetworkInfo() {
654 try {
655 return mService.getAllNetworkInfo();
656 } catch (RemoteException e) {
657 return null;
658 }
659 }
660
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800661 /**
Wink Saville948282b2013-08-29 08:55:16 -0700662 * Returns details about the Provisioning or currently active default data network. When
663 * connected, this network is the default route for outgoing connections.
664 * You should always check {@link NetworkInfo#isConnected()} before initiating
665 * network traffic. This may return {@code null} when there is no default
666 * network.
667 *
668 * @return a {@link NetworkInfo} object for the current default network
669 * or {@code null} if no network default network is currently active
670 *
671 * <p>This method requires the call to hold the permission
672 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
673 *
674 * {@hide}
675 */
676 public NetworkInfo getProvisioningOrActiveNetworkInfo() {
677 try {
678 return mService.getProvisioningOrActiveNetworkInfo();
679 } catch (RemoteException e) {
680 return null;
681 }
682 }
683
684 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800685 * Returns the IP information for the current default network.
686 *
687 * @return a {@link LinkProperties} object describing the IP info
688 * for the current default network, or {@code null} if there
689 * is no current default network.
690 *
691 * <p>This method requires the call to hold the permission
692 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
693 * {@hide}
694 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700695 public LinkProperties getActiveLinkProperties() {
696 try {
697 return mService.getActiveLinkProperties();
698 } catch (RemoteException e) {
699 return null;
700 }
701 }
702
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800703 /**
704 * Returns the IP information for a given network type.
705 *
706 * @param networkType the network type of interest.
707 * @return a {@link LinkProperties} object describing the IP info
708 * for the given networkType, or {@code null} if there is
709 * no current default network.
710 *
711 * <p>This method requires the call to hold the permission
712 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
713 * {@hide}
714 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700715 public LinkProperties getLinkProperties(int networkType) {
716 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -0700717 return mService.getLinkPropertiesForType(networkType);
718 } catch (RemoteException e) {
719 return null;
720 }
721 }
722
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700723 /**
724 * Get the {@link LinkProperties} for the given {@link Network}. This
725 * will return {@code null} if the network is unknown.
726 *
727 * @param network The {@link Network} object identifying the network in question.
728 * @return The {@link LinkProperties} for the network, or {@code null}.
729 **/
Robert Greenwalt9258c642014-03-26 16:47:06 -0700730 public LinkProperties getLinkProperties(Network network) {
731 try {
732 return mService.getLinkProperties(network);
733 } catch (RemoteException e) {
734 return null;
735 }
736 }
737
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700738 /**
739 * Get the {@link NetworkCapabilities} for the given {@link Network}. This
740 * will return {@code null} if the network is unknown.
741 *
742 * @param network The {@link Network} object identifying the network in question.
743 * @return The {@link NetworkCapabilities} for the network, or {@code null}.
744 */
Robert Greenwalt9258c642014-03-26 16:47:06 -0700745 public NetworkCapabilities getNetworkCapabilities(Network network) {
746 try {
747 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700748 } catch (RemoteException e) {
749 return null;
750 }
751 }
752
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800753 /**
754 * Tells each network type to set its radio power state as directed.
755 *
756 * @param turnOn a boolean, {@code true} to turn the radios on,
757 * {@code false} to turn them off.
758 * @return a boolean, {@code true} indicating success. All network types
759 * will be tried, even if some fail.
760 *
761 * <p>This method requires the call to hold the permission
762 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
763 * {@hide}
764 */
Robert Greenwalt7b816022014-04-18 15:25:25 -0700765// TODO - check for any callers and remove
766// public boolean setRadios(boolean turnOn) {
767// try {
768// return mService.setRadios(turnOn);
769// } catch (RemoteException e) {
770// return false;
771// }
772// }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800773
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800774 /**
775 * Tells a given networkType to set its radio power state as directed.
776 *
777 * @param networkType the int networkType of interest.
778 * @param turnOn a boolean, {@code true} to turn the radio on,
779 * {@code} false to turn it off.
780 * @return a boolean, {@code true} indicating success.
781 *
782 * <p>This method requires the call to hold the permission
783 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
784 * {@hide}
785 */
Robert Greenwalt7b816022014-04-18 15:25:25 -0700786// TODO - check for any callers and remove
787// public boolean setRadio(int networkType, boolean turnOn) {
788// try {
789// return mService.setRadio(networkType, turnOn);
790// } catch (RemoteException e) {
791// return false;
792// }
793// }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800794
795 /**
796 * Tells the underlying networking system that the caller wants to
797 * begin using the named feature. The interpretation of {@code feature}
798 * is completely up to each networking implementation.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700799 * <p>This method requires the caller to hold the permission
800 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801 * @param networkType specifies which network the request pertains to
802 * @param feature the name of the feature to be used
803 * @return an integer value representing the outcome of the request.
804 * The interpretation of this value is specific to each networking
805 * implementation+feature combination, except that the value {@code -1}
806 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700807 *
808 * @deprecated Deprecated in favor of the cleaner {@link #requestNetwork} api.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800809 */
810 public int startUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700811 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
812 if (netCap == null) {
813 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
814 feature);
815 return PhoneConstants.APN_REQUEST_FAILED;
816 }
817
818 NetworkRequest request = null;
819 synchronized (sLegacyRequests) {
820 LegacyRequest l = sLegacyRequests.get(netCap);
821 if (l != null) {
822 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
823 renewRequestLocked(l);
824 if (l.currentNetwork != null) {
825 return PhoneConstants.APN_ALREADY_ACTIVE;
826 } else {
827 return PhoneConstants.APN_REQUEST_STARTED;
828 }
829 }
830
831 request = requestNetworkForFeatureLocked(netCap);
832 }
833 if (request != null) {
834 Log.d(TAG, "starting startUsingNeworkFeature for request " + request);
835 return PhoneConstants.APN_REQUEST_STARTED;
836 } else {
837 Log.d(TAG, " request Failed");
838 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800839 }
840 }
841
842 /**
843 * Tells the underlying networking system that the caller is finished
844 * using the named feature. The interpretation of {@code feature}
845 * is completely up to each networking implementation.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700846 * <p>This method requires the caller to hold the permission
847 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800848 * @param networkType specifies which network the request pertains to
849 * @param feature the name of the feature that is no longer needed
850 * @return an integer value representing the outcome of the request.
851 * The interpretation of this value is specific to each networking
852 * implementation+feature combination, except that the value {@code -1}
853 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700854 *
855 * @deprecated Deprecated in favor of the cleaner {@link #requestNetwork} api.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800856 */
857 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700858 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
859 if (netCap == null) {
860 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
861 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800862 return -1;
863 }
Robert Greenwalt562cc542014-05-15 18:07:26 -0700864
865 NetworkRequest request = removeRequestForFeature(netCap);
866 if (request != null) {
867 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
868 releaseNetworkRequest(request);
869 }
870 return 1;
871 }
872
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900873 /**
874 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability from the given
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900875 * NetworkCapabilities object if all the capabilities it provides are
876 * typically provided by restricted networks.
877 *
878 * TODO: consider:
879 * - Moving to NetworkCapabilities
880 * - Renaming it to guessRestrictedCapability and make it set the
881 * restricted capability bit in addition to clearing it.
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900882 * @hide
883 */
884 public static void maybeMarkCapabilitiesRestricted(NetworkCapabilities nc) {
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900885 for (Integer capability : nc.getNetworkCapabilities()) {
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900886 switch (capability.intValue()) {
887 case NetworkCapabilities.NET_CAPABILITY_CBS:
888 case NetworkCapabilities.NET_CAPABILITY_DUN:
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900889 case NetworkCapabilities.NET_CAPABILITY_EIMS:
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900890 case NetworkCapabilities.NET_CAPABILITY_FOTA:
891 case NetworkCapabilities.NET_CAPABILITY_IA:
892 case NetworkCapabilities.NET_CAPABILITY_IMS:
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900893 case NetworkCapabilities.NET_CAPABILITY_RCS:
894 case NetworkCapabilities.NET_CAPABILITY_XCAP:
895 continue;
896 default:
897 // At least one capability usually provided by unrestricted
898 // networks. Conclude that this network is unrestricted.
899 return;
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900900 }
901 }
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900902 // All the capabilities are typically provided by restricted networks.
903 // Conclude that this network is restricted.
904 nc.removeNetworkCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900905 }
906
Robert Greenwalt562cc542014-05-15 18:07:26 -0700907 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
908 if (networkType == TYPE_MOBILE) {
909 int cap = -1;
910 if ("enableMMS".equals(feature)) {
911 cap = NetworkCapabilities.NET_CAPABILITY_MMS;
912 } else if ("enableSUPL".equals(feature)) {
913 cap = NetworkCapabilities.NET_CAPABILITY_SUPL;
914 } else if ("enableDUN".equals(feature) || "enableDUNAlways".equals(feature)) {
915 cap = NetworkCapabilities.NET_CAPABILITY_DUN;
916 } else if ("enableHIPRI".equals(feature)) {
917 cap = NetworkCapabilities.NET_CAPABILITY_INTERNET;
918 } else if ("enableFOTA".equals(feature)) {
919 cap = NetworkCapabilities.NET_CAPABILITY_FOTA;
920 } else if ("enableIMS".equals(feature)) {
921 cap = NetworkCapabilities.NET_CAPABILITY_IMS;
922 } else if ("enableCBS".equals(feature)) {
923 cap = NetworkCapabilities.NET_CAPABILITY_CBS;
924 } else {
925 return null;
926 }
927 NetworkCapabilities netCap = new NetworkCapabilities();
928 netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
929 netCap.addNetworkCapability(cap);
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900930 maybeMarkCapabilitiesRestricted(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -0700931 return netCap;
932 } else if (networkType == TYPE_WIFI) {
933 if ("p2p".equals(feature)) {
934 NetworkCapabilities netCap = new NetworkCapabilities();
935 netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
936 netCap.addNetworkCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900937 maybeMarkCapabilitiesRestricted(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -0700938 return netCap;
939 }
940 }
941 return null;
942 }
943
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700944 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700945 if (netCap == null) return TYPE_NONE;
946 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
947 return TYPE_MOBILE_CBS;
948 }
949 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
950 return TYPE_MOBILE_IMS;
951 }
952 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
953 return TYPE_MOBILE_FOTA;
954 }
955 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
956 return TYPE_MOBILE_DUN;
957 }
958 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
959 return TYPE_MOBILE_SUPL;
960 }
961 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
962 return TYPE_MOBILE_MMS;
963 }
964 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
965 return TYPE_MOBILE_HIPRI;
966 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700967 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
968 return TYPE_WIFI_P2P;
969 }
Robert Greenwalt562cc542014-05-15 18:07:26 -0700970 return TYPE_NONE;
971 }
972
973 private static class LegacyRequest {
974 NetworkCapabilities networkCapabilities;
975 NetworkRequest networkRequest;
976 int expireSequenceNumber;
977 Network currentNetwork;
978 int delay = -1;
979 NetworkCallbackListener networkCallbackListener = new NetworkCallbackListener() {
980 @Override
981 public void onAvailable(NetworkRequest request, Network network) {
982 currentNetwork = network;
983 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
984 network.bindProcessForHostResolution();
985 }
986 @Override
987 public void onLost(NetworkRequest request, Network network) {
988 if (network.equals(currentNetwork)) {
989 currentNetwork = null;
990 network.unbindProcessForHostResolution();
991 }
992 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
993 }
994 };
995 }
996
997 private HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
998 new HashMap<NetworkCapabilities, LegacyRequest>();
999
1000 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1001 synchronized (sLegacyRequests) {
1002 LegacyRequest l = sLegacyRequests.get(netCap);
1003 if (l != null) return l.networkRequest;
1004 }
1005 return null;
1006 }
1007
1008 private void renewRequestLocked(LegacyRequest l) {
1009 l.expireSequenceNumber++;
1010 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1011 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1012 }
1013
1014 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1015 int ourSeqNum = -1;
1016 synchronized (sLegacyRequests) {
1017 LegacyRequest l = sLegacyRequests.get(netCap);
1018 if (l == null) return;
1019 ourSeqNum = l.expireSequenceNumber;
1020 if (l.expireSequenceNumber == sequenceNum) {
1021 releaseNetworkRequest(l.networkRequest);
1022 sLegacyRequests.remove(netCap);
1023 }
1024 }
1025 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1026 }
1027
1028 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1029 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001030 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001031 try {
1032 delay = mService.getRestoreDefaultNetworkDelay(type);
1033 } catch (RemoteException e) {}
1034 LegacyRequest l = new LegacyRequest();
1035 l.networkCapabilities = netCap;
1036 l.delay = delay;
1037 l.expireSequenceNumber = 0;
1038 l.networkRequest = sendRequestForNetwork(netCap, l.networkCallbackListener, 0,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001039 REQUEST, type);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001040 if (l.networkRequest == null) return null;
1041 sLegacyRequests.put(netCap, l);
1042 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1043 return l.networkRequest;
1044 }
1045
1046 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1047 if (delay >= 0) {
1048 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
1049 Message msg = sCallbackHandler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1050 sCallbackHandler.sendMessageDelayed(msg, delay);
1051 }
1052 }
1053
1054 private NetworkRequest removeRequestForFeature(NetworkCapabilities netCap) {
1055 synchronized (sLegacyRequests) {
1056 LegacyRequest l = sLegacyRequests.remove(netCap);
1057 if (l == null) return null;
1058 return l.networkRequest;
1059 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001060 }
1061
1062 /**
1063 * Ensure that a network route exists to deliver traffic to the specified
1064 * host via the specified network interface. An attempt to add a route that
1065 * already exists is ignored, but treated as successful.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001066 * <p>This method requires the caller to hold the permission
1067 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001068 * @param networkType the type of the network over which traffic to the specified
1069 * host is to be routed
1070 * @param hostAddress the IP address of the host to which the route is desired
1071 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001072 *
1073 * @deprecated Deprecated in favor of the {@link #requestNetwork},
1074 * {@link Network#bindProcess} and {@link Network#socketFactory} api.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 */
1076 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001077 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
1078
1079 if (inetAddress == null) {
1080 return false;
1081 }
1082
1083 return requestRouteToHostAddress(networkType, inetAddress);
1084 }
1085
1086 /**
1087 * Ensure that a network route exists to deliver traffic to the specified
1088 * host via the specified network interface. An attempt to add a route that
1089 * already exists is ignored, but treated as successful.
Jake Hamby8f9b33e2014-01-15 13:08:03 -08001090 * <p>This method requires the caller to hold the permission
1091 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001092 * @param networkType the type of the network over which traffic to the specified
1093 * host is to be routed
1094 * @param hostAddress the IP address of the host to which the route is desired
1095 * @return {@code true} on success, {@code false} on failure
1096 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001097 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
1098 * {@link Network#bindProcess} api.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001099 */
1100 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
1101 byte[] address = hostAddress.getAddress();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001102 try {
Chad Brubakerf81daa92014-02-14 13:22:34 -08001103 return mService.requestRouteToHostAddress(networkType, address, mPackageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001104 } catch (RemoteException e) {
1105 return false;
1106 }
1107 }
1108
1109 /**
1110 * Returns the value of the setting for background data usage. If false,
1111 * applications should not use the network if the application is not in the
1112 * foreground. Developers should respect this setting, and check the value
1113 * of this before performing any background data operations.
1114 * <p>
1115 * All applications that have background services that use the network
1116 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001117 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001118 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001119 * background data depends on several combined factors, and this method will
1120 * always return {@code true}. Instead, when background data is unavailable,
1121 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001122 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001123 * @return Whether background data usage is allowed.
1124 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001125 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001126 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001127 // assume that background data is allowed; final authority is
1128 // NetworkInfo which may be blocked.
1129 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001130 }
1131
1132 /**
1133 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001134 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001135 * @param allowBackgroundData Whether an application should use data while
1136 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001137 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1139 * @see #getBackgroundDataSetting()
1140 * @hide
1141 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001142 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001143 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001144 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001146
1147 /**
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001148 * Return quota status for the current active network, or {@code null} if no
1149 * network is active. Quota status can change rapidly, so these values
1150 * shouldn't be cached.
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001151 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001152 * <p>This method requires the call to hold the permission
1153 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1154 *
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001155 * @hide
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001156 */
1157 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1158 try {
1159 return mService.getActiveNetworkQuotaInfo();
1160 } catch (RemoteException e) {
1161 return null;
1162 }
1163 }
1164
1165 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001166 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001167 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001168 */
1169 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001170 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1171 if (b != null) {
1172 try {
1173 ITelephony it = ITelephony.Stub.asInterface(b);
1174 return it.getDataEnabled();
1175 } catch (RemoteException e) { }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001176 }
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001177 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001178 }
1179
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001180 /**
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001181 * Callback for use with {@link ConnectivityManager#registerNetworkActiveListener} to
1182 * find out when the current network has gone in to a high power state.
1183 */
1184 public interface OnNetworkActiveListener {
1185 /**
1186 * Called on the main thread of the process to report that the current data network
1187 * has become active, and it is now a good time to perform any pending network
1188 * operations. Note that this listener only tells you when the network becomes
1189 * active; if at any other time you want to know whether it is active (and thus okay
1190 * to initiate network traffic), you can retrieve its instantaneous state with
1191 * {@link ConnectivityManager#isNetworkActive}.
1192 */
1193 public void onNetworkActive();
1194 }
1195
1196 private INetworkManagementService getNetworkManagementService() {
1197 synchronized (this) {
1198 if (mNMService != null) {
1199 return mNMService;
1200 }
1201 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1202 mNMService = INetworkManagementService.Stub.asInterface(b);
1203 return mNMService;
1204 }
1205 }
1206
1207 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
1208 mNetworkActivityListeners
1209 = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>();
1210
1211 /**
1212 * Start listening to reports when the data network is active, meaning it is
1213 * a good time to perform network traffic. Use {@link #isNetworkActive()}
1214 * to determine the current state of the network after registering the listener.
1215 *
1216 * @param l The listener to be told when the network is active.
1217 */
1218 public void registerNetworkActiveListener(final OnNetworkActiveListener l) {
1219 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1220 @Override
1221 public void onNetworkActive() throws RemoteException {
1222 l.onNetworkActive();
1223 }
1224 };
1225
1226 try {
1227 getNetworkManagementService().registerNetworkActivityListener(rl);
1228 mNetworkActivityListeners.put(l, rl);
1229 } catch (RemoteException e) {
1230 }
1231 }
1232
1233 /**
1234 * Remove network active listener previously registered with
1235 * {@link #registerNetworkActiveListener}.
1236 *
1237 * @param l Previously registered listener.
1238 */
1239 public void unregisterNetworkActiveListener(OnNetworkActiveListener l) {
1240 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
1241 if (rl == null) {
1242 throw new IllegalArgumentException("Listener not registered: " + l);
1243 }
1244 try {
1245 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1246 } catch (RemoteException e) {
1247 }
1248 }
1249
1250 /**
1251 * Return whether the data network is currently active. An active network means that
1252 * it is currently in a high power state for performing data transmission. On some
1253 * types of networks, it may be expensive to move and stay in such a state, so it is
1254 * more power efficient to batch network traffic together when the radio is already in
1255 * this state. This method tells you whether right now is currently a good time to
1256 * initiate network traffic, as the network is already active.
1257 */
1258 public boolean isNetworkActive() {
1259 try {
1260 return getNetworkManagementService().isNetworkActive();
1261 } catch (RemoteException e) {
1262 }
1263 return false;
1264 }
1265
1266 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001267 * {@hide}
1268 */
Chad Brubakerf81daa92014-02-14 13:22:34 -08001269 public ConnectivityManager(IConnectivityManager service, String packageName) {
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001270 mService = checkNotNull(service, "missing IConnectivityManager");
Chad Brubakerf81daa92014-02-14 13:22:34 -08001271 mPackageName = checkNotNull(packageName, "missing package name");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001272 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001273
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07001274 /** {@hide} */
1275 public static ConnectivityManager from(Context context) {
1276 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1277 }
1278
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001279 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001280 * Get the set of tetherable, available interfaces. This list is limited by
1281 * device configuration and current interface existence.
1282 *
1283 * @return an array of 0 or more Strings of tetherable interface names.
1284 *
1285 * <p>This method requires the call to hold the permission
1286 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001287 * {@hide}
1288 */
1289 public String[] getTetherableIfaces() {
1290 try {
1291 return mService.getTetherableIfaces();
1292 } catch (RemoteException e) {
1293 return new String[0];
1294 }
1295 }
1296
1297 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001298 * Get the set of tethered interfaces.
1299 *
1300 * @return an array of 0 or more String of currently tethered interface names.
1301 *
1302 * <p>This method requires the call to hold the permission
1303 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001304 * {@hide}
1305 */
1306 public String[] getTetheredIfaces() {
1307 try {
1308 return mService.getTetheredIfaces();
1309 } catch (RemoteException e) {
1310 return new String[0];
1311 }
1312 }
1313
1314 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001315 * Get the set of interface names which attempted to tether but
1316 * failed. Re-attempting to tether may cause them to reset to the Tethered
1317 * state. Alternatively, causing the interface to be destroyed and recreated
1318 * may cause them to reset to the available state.
1319 * {@link ConnectivityManager#getLastTetherError} can be used to get more
1320 * information on the cause of the errors.
1321 *
1322 * @return an array of 0 or more String indicating the interface names
1323 * which failed to tether.
1324 *
1325 * <p>This method requires the call to hold the permission
1326 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001327 * {@hide}
1328 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001329 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001330 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001331 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001332 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001333 return new String[0];
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001334 }
1335 }
1336
1337 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001338 * Attempt to tether the named interface. This will setup a dhcp server
1339 * on the interface, forward and NAT IP packets and forward DNS requests
1340 * to the best active upstream network interface. Note that if no upstream
1341 * IP network interface is available, dhcp will still run and traffic will be
1342 * allowed between the tethered devices and this device, though upstream net
1343 * access will of course fail until an upstream network interface becomes
1344 * active.
1345 *
1346 * @param iface the interface name to tether.
1347 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1348 *
1349 * <p>This method requires the call to hold the permission
1350 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001351 * {@hide}
1352 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001353 public int tether(String iface) {
1354 try {
1355 return mService.tether(iface);
1356 } catch (RemoteException e) {
1357 return TETHER_ERROR_SERVICE_UNAVAIL;
1358 }
1359 }
1360
1361 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001362 * Stop tethering the named interface.
1363 *
1364 * @param iface the interface name to untether.
1365 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1366 *
1367 * <p>This method requires the call to hold the permission
1368 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt5a735062010-03-02 17:25:02 -08001369 * {@hide}
1370 */
1371 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001372 try {
1373 return mService.untether(iface);
1374 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001375 return TETHER_ERROR_SERVICE_UNAVAIL;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001376 }
1377 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001378
1379 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001380 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001381 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001382 * due to device configuration.
1383 *
1384 * @return a boolean - {@code true} indicating Tethering is supported.
1385 *
1386 * <p>This method requires the call to hold the permission
1387 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001388 * {@hide}
1389 */
1390 public boolean isTetheringSupported() {
1391 try {
1392 return mService.isTetheringSupported();
1393 } catch (RemoteException e) {
1394 return false;
1395 }
1396 }
1397
1398 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001399 * Get the list of regular expressions that define any tetherable
1400 * USB network interfaces. If USB tethering is not supported by the
1401 * device, this list should be empty.
1402 *
1403 * @return an array of 0 or more regular expression Strings defining
1404 * what interfaces are considered tetherable usb interfaces.
1405 *
1406 * <p>This method requires the call to hold the permission
1407 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001408 * {@hide}
1409 */
1410 public String[] getTetherableUsbRegexs() {
1411 try {
1412 return mService.getTetherableUsbRegexs();
1413 } catch (RemoteException e) {
1414 return new String[0];
1415 }
1416 }
1417
1418 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001419 * Get the list of regular expressions that define any tetherable
1420 * Wifi network interfaces. If Wifi tethering is not supported by the
1421 * device, this list should be empty.
1422 *
1423 * @return an array of 0 or more regular expression Strings defining
1424 * what interfaces are considered tetherable wifi interfaces.
1425 *
1426 * <p>This method requires the call to hold the permission
1427 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001428 * {@hide}
1429 */
1430 public String[] getTetherableWifiRegexs() {
1431 try {
1432 return mService.getTetherableWifiRegexs();
1433 } catch (RemoteException e) {
1434 return new String[0];
1435 }
1436 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08001437
Danica Chang6fdd0c62010-08-11 14:54:43 -07001438 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001439 * Get the list of regular expressions that define any tetherable
1440 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
1441 * device, this list should be empty.
1442 *
1443 * @return an array of 0 or more regular expression Strings defining
1444 * what interfaces are considered tetherable bluetooth interfaces.
1445 *
1446 * <p>This method requires the call to hold the permission
1447 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001448 * {@hide}
1449 */
1450 public String[] getTetherableBluetoothRegexs() {
1451 try {
1452 return mService.getTetherableBluetoothRegexs();
1453 } catch (RemoteException e) {
1454 return new String[0];
1455 }
1456 }
1457
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001458 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001459 * Attempt to both alter the mode of USB and Tethering of USB. A
1460 * utility method to deal with some of the complexity of USB - will
1461 * attempt to switch to Rndis and subsequently tether the resulting
1462 * interface on {@code true} or turn off tethering and switch off
1463 * Rndis on {@code false}.
1464 *
1465 * @param enable a boolean - {@code true} to enable tethering
1466 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1467 *
1468 * <p>This method requires the call to hold the permission
1469 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001470 * {@hide}
1471 */
1472 public int setUsbTethering(boolean enable) {
1473 try {
1474 return mService.setUsbTethering(enable);
1475 } catch (RemoteException e) {
1476 return TETHER_ERROR_SERVICE_UNAVAIL;
1477 }
1478 }
1479
Robert Greenwalt5a735062010-03-02 17:25:02 -08001480 /** {@hide} */
1481 public static final int TETHER_ERROR_NO_ERROR = 0;
1482 /** {@hide} */
1483 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
1484 /** {@hide} */
1485 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
1486 /** {@hide} */
1487 public static final int TETHER_ERROR_UNSUPPORTED = 3;
1488 /** {@hide} */
1489 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
1490 /** {@hide} */
1491 public static final int TETHER_ERROR_MASTER_ERROR = 5;
1492 /** {@hide} */
1493 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
1494 /** {@hide} */
1495 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
1496 /** {@hide} */
1497 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
1498 /** {@hide} */
1499 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
1500 /** {@hide} */
1501 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
1502
1503 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001504 * Get a more detailed error code after a Tethering or Untethering
1505 * request asynchronously failed.
1506 *
1507 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08001508 * @return error The error code of the last error tethering or untethering the named
1509 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001510 *
1511 * <p>This method requires the call to hold the permission
1512 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a735062010-03-02 17:25:02 -08001513 * {@hide}
1514 */
1515 public int getLastTetherError(String iface) {
1516 try {
1517 return mService.getLastTetherError(iface);
1518 } catch (RemoteException e) {
1519 return TETHER_ERROR_SERVICE_UNAVAIL;
1520 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001521 }
1522
1523 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001524 * Try to ensure the device stays awake until we connect with the next network.
1525 * Actually just holds a wakelock for a number of seconds while we try to connect
1526 * to any default networks. This will expire if the timeout passes or if we connect
1527 * to a default after this is called. For internal use only.
1528 *
1529 * @param forWhom the name of the network going down for logging purposes
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001530 * @return {@code true} on success, {@code false} on failure
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001531 *
1532 * <p>This method requires the call to hold the permission
1533 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001534 * {@hide}
1535 */
1536 public boolean requestNetworkTransitionWakelock(String forWhom) {
1537 try {
1538 mService.requestNetworkTransitionWakelock(forWhom);
1539 return true;
1540 } catch (RemoteException e) {
1541 return false;
1542 }
1543 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07001544
Robert Greenwalt67fd6c92010-09-09 14:22:59 -07001545 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001546 * Report network connectivity status. This is currently used only
1547 * to alter status bar UI.
1548 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001549 * @param networkType The type of network you want to report on
1550 * @param percentage The quality of the connection 0 is bad, 100 is good
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001551 *
1552 * <p>This method requires the call to hold the permission
1553 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001554 * {@hide}
1555 */
1556 public void reportInetCondition(int networkType, int percentage) {
1557 try {
1558 mService.reportInetCondition(networkType, percentage);
1559 } catch (RemoteException e) {
1560 }
1561 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001562
1563 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001564 * Report a problem network to the framework. This provides a hint to the system
1565 * that there might be connectivity problems on this network and may cause
1566 * the framework to re-evaluate network connectivity and/or switch to another
1567 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001568 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001569 * @param network The {@link Network} the application was attempting to use
1570 * or {@code null} to indicate the current default network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001571 */
1572 public void reportBadNetwork(Network network) {
1573 try {
1574 mService.reportBadNetwork(network);
1575 } catch (RemoteException e) {
1576 }
1577 }
1578
1579 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001580 * Set a network-independent global http proxy. This is not normally what you want
1581 * for typical HTTP proxies - they are general network dependent. However if you're
1582 * doing something unusual like general internal filtering this may be useful. On
1583 * a private network where the proxy is not accessible, you may break HTTP using this.
1584 *
Jason Monk207900c2014-04-25 15:00:09 -04001585 * @param p The a {@link ProxyInfo} object defining the new global
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001586 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
1587 *
1588 * <p>This method requires the call to hold the permission
Jason Monkfaf3fd52014-05-07 18:41:13 -04001589 * android.Manifest.permission#CONNECTIVITY_INTERNAL.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001590 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07001591 */
Jason Monk207900c2014-04-25 15:00:09 -04001592 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001593 try {
1594 mService.setGlobalProxy(p);
1595 } catch (RemoteException e) {
1596 }
1597 }
1598
1599 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001600 * Retrieve any network-independent global HTTP proxy.
1601 *
Jason Monk207900c2014-04-25 15:00:09 -04001602 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001603 * if no global HTTP proxy is set.
1604 *
1605 * <p>This method requires the call to hold the permission
1606 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001607 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07001608 */
Jason Monk207900c2014-04-25 15:00:09 -04001609 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001610 try {
1611 return mService.getGlobalProxy();
1612 } catch (RemoteException e) {
1613 return null;
1614 }
1615 }
1616
1617 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001618 * Get the HTTP proxy settings for the current default network. Note that
1619 * if a global proxy is set, it will override any per-network setting.
1620 *
Jason Monk207900c2014-04-25 15:00:09 -04001621 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001622 * HTTP proxy is active.
1623 *
1624 * <p>This method requires the call to hold the permission
1625 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt434203a2010-10-11 16:00:27 -07001626 * {@hide}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001627 * @deprecated Deprecated in favor of {@link #getLinkProperties}
Robert Greenwalt434203a2010-10-11 16:00:27 -07001628 */
Jason Monk207900c2014-04-25 15:00:09 -04001629 public ProxyInfo getProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001630 try {
1631 return mService.getProxy();
1632 } catch (RemoteException e) {
1633 return null;
1634 }
1635 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001636
1637 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001638 * Sets a secondary requirement bit for the given networkType.
1639 * This requirement bit is generally under the control of the carrier
1640 * or its agents and is not directly controlled by the user.
1641 *
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001642 * @param networkType The network who's dependence has changed
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001643 * @param met Boolean - true if network use is OK, false if not
1644 *
1645 * <p>This method requires the call to hold the permission
1646 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001647 * {@hide}
1648 */
1649 public void setDataDependency(int networkType, boolean met) {
1650 try {
1651 mService.setDataDependency(networkType, met);
1652 } catch (RemoteException e) {
1653 }
1654 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07001655
1656 /**
1657 * Returns true if the hardware supports the given network type
1658 * else it returns false. This doesn't indicate we have coverage
1659 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001660 * hardware supports it. For example a GSM phone without a SIM
1661 * should still return {@code true} for mobile data, but a wifi only
1662 * tablet would return {@code false}.
1663 *
1664 * @param networkType The network type we'd like to check
1665 * @return {@code true} if supported, else {@code false}
1666 *
1667 * <p>This method requires the call to hold the permission
1668 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07001669 * @hide
1670 */
1671 public boolean isNetworkSupported(int networkType) {
1672 try {
1673 return mService.isNetworkSupported(networkType);
1674 } catch (RemoteException e) {}
1675 return false;
1676 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07001677
1678 /**
1679 * Returns if the currently active data network is metered. A network is
1680 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001681 * that connection due to monetary costs, data limitations or
1682 * battery/performance issues. You should check this before doing large
1683 * data transfers, and warn the user or delay the operation until another
1684 * network is available.
1685 *
1686 * @return {@code true} if large transfers should be avoided, otherwise
1687 * {@code false}.
1688 *
1689 * <p>This method requires the call to hold the permission
1690 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07001691 */
1692 public boolean isActiveNetworkMetered() {
1693 try {
1694 return mService.isActiveNetworkMetered();
1695 } catch (RemoteException e) {
1696 return false;
1697 }
1698 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07001699
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001700 /**
1701 * If the LockdownVpn mechanism is enabled, updates the vpn
1702 * with a reload of its profile.
1703 *
1704 * @return a boolean with {@code} indicating success
1705 *
1706 * <p>This method can only be called by the system UID
1707 * {@hide}
1708 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07001709 public boolean updateLockdownVpn() {
1710 try {
1711 return mService.updateLockdownVpn();
1712 } catch (RemoteException e) {
1713 return false;
1714 }
1715 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07001716
1717 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001718 * Signal that the captive portal check on the indicated network
Wink Savilled747cbc2013-08-07 16:22:47 -07001719 * is complete and whether its a captive portal or not.
1720 *
1721 * @param info the {@link NetworkInfo} object for the networkType
1722 * in question.
1723 * @param isCaptivePortal true/false.
1724 *
1725 * <p>This method requires the call to hold the permission
1726 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
1727 * {@hide}
1728 */
1729 public void captivePortalCheckCompleted(NetworkInfo info, boolean isCaptivePortal) {
1730 try {
1731 mService.captivePortalCheckCompleted(info, isCaptivePortal);
1732 } catch (RemoteException e) {
1733 }
1734 }
1735
1736 /**
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001737 * Supply the backend messenger for a network tracker
1738 *
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001739 * @param networkType NetworkType to set
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001740 * @param messenger {@link Messenger}
1741 * {@hide}
1742 */
1743 public void supplyMessenger(int networkType, Messenger messenger) {
1744 try {
1745 mService.supplyMessenger(networkType, messenger);
1746 } catch (RemoteException e) {
1747 }
1748 }
Wink Savilleab9321d2013-06-29 21:10:57 -07001749
1750 /**
Wink Saville948282b2013-08-29 08:55:16 -07001751 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07001752 *
Wink Savilleab9321d2013-06-29 21:10:57 -07001753 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07001754 *
1755 * @return time out that will be used, maybe less that suggestedTimeOutMs
1756 * -1 if an error.
1757 *
1758 * {@hide}
1759 */
Wink Saville948282b2013-08-29 08:55:16 -07001760 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07001761 int timeOutMs = -1;
1762 try {
Wink Saville948282b2013-08-29 08:55:16 -07001763 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07001764 } catch (RemoteException e) {
1765 }
1766 return timeOutMs;
1767 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07001768
1769 /**
Wink Saville42d4f082013-07-20 20:31:59 -07001770 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07001771 * {@hide}
1772 */
1773 public String getMobileProvisioningUrl() {
1774 try {
1775 return mService.getMobileProvisioningUrl();
1776 } catch (RemoteException e) {
1777 }
1778 return null;
1779 }
Wink Saville42d4f082013-07-20 20:31:59 -07001780
1781 /**
1782 * Get the mobile redirected provisioning url.
1783 * {@hide}
1784 */
1785 public String getMobileRedirectedProvisioningUrl() {
1786 try {
1787 return mService.getMobileRedirectedProvisioningUrl();
1788 } catch (RemoteException e) {
1789 }
1790 return null;
1791 }
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001792
1793 /**
1794 * get the information about a specific network link
1795 * @hide
1796 */
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001797 public LinkQualityInfo getLinkQualityInfo(int networkType) {
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001798 try {
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001799 LinkQualityInfo li = mService.getLinkQualityInfo(networkType);
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001800 return li;
1801 } catch (RemoteException e) {
1802 return null;
1803 }
1804 }
1805
1806 /**
1807 * get the information of currently active network link
1808 * @hide
1809 */
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001810 public LinkQualityInfo getActiveLinkQualityInfo() {
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001811 try {
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001812 LinkQualityInfo li = mService.getActiveLinkQualityInfo();
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001813 return li;
1814 } catch (RemoteException e) {
1815 return null;
1816 }
1817 }
1818
1819 /**
1820 * get the information of all network links
1821 * @hide
1822 */
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001823 public LinkQualityInfo[] getAllLinkQualityInfo() {
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001824 try {
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001825 LinkQualityInfo[] li = mService.getAllLinkQualityInfo();
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001826 return li;
1827 } catch (RemoteException e) {
1828 return null;
1829 }
1830 }
Wink Saville7788c612013-08-29 14:57:08 -07001831
1832 /**
Wink Saville948282b2013-08-29 08:55:16 -07001833 * Set sign in error notification to visible or in visible
1834 *
1835 * @param visible
1836 * @param networkType
1837 *
1838 * {@hide}
1839 */
1840 public void setProvisioningNotificationVisible(boolean visible, int networkType,
1841 String extraInfo, String url) {
1842 try {
1843 mService.setProvisioningNotificationVisible(visible, networkType, extraInfo, url);
1844 } catch (RemoteException e) {
1845 }
1846 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07001847
1848 /**
1849 * Set the value for enabling/disabling airplane mode
1850 *
1851 * @param enable whether to enable airplane mode or not
1852 *
1853 * <p>This method requires the call to hold the permission
1854 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
1855 * @hide
1856 */
1857 public void setAirplaneMode(boolean enable) {
1858 try {
1859 mService.setAirplaneMode(enable);
1860 } catch (RemoteException e) {
1861 }
1862 }
Robert Greenwalte049c232014-04-11 15:53:27 -07001863
1864 /** {@hide} */
Robert Greenwalta67be032014-05-16 15:49:14 -07001865 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07001866 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07001867 mService.registerNetworkFactory(messenger, name);
1868 } catch (RemoteException e) { }
1869 }
1870
1871 /** {@hide} */
1872 public void unregisterNetworkFactory(Messenger messenger) {
1873 try {
1874 mService.unregisterNetworkFactory(messenger);
Robert Greenwalte049c232014-04-11 15:53:27 -07001875 } catch (RemoteException e) { }
1876 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07001877
1878 /** {@hide} */
1879 public void registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
1880 NetworkCapabilities nc, int score) {
1881 try {
1882 mService.registerNetworkAgent(messenger, ni, lp, nc, score);
1883 } catch (RemoteException e) { }
1884 }
1885
Robert Greenwalt9258c642014-03-26 16:47:06 -07001886 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001887 * Base class for NetworkRequest callbacks. Used for notifications about network
1888 * changes. Should be extended by applications wanting notifications.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001889 */
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001890 public static class NetworkCallbackListener {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001891 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001892 public static final int PRECHECK = 1;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001893 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001894 public static final int AVAILABLE = 2;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001895 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001896 public static final int LOSING = 3;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001897 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001898 public static final int LOST = 4;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001899 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001900 public static final int UNAVAIL = 5;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001901 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001902 public static final int CAP_CHANGED = 6;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001903 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001904 public static final int PROP_CHANGED = 7;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001905 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001906 public static final int CANCELED = 8;
1907
1908 /**
1909 * @hide
1910 * Called whenever the framework connects to a network that it may use to
1911 * satisfy this request
1912 */
1913 public void onPreCheck(NetworkRequest networkRequest, Network network) {}
1914
1915 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001916 * Called when the framework connects and has declared new network ready for use.
1917 *
1918 * @param networkRequest The {@link NetworkRequest} used to initiate the request.
1919 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07001920 */
1921 public void onAvailable(NetworkRequest networkRequest, Network network) {}
1922
1923 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001924 * Called when the network is about to be disconnected. Often paired with an
1925 * {@link NetworkCallbackListener#onAvailable} call with the new replacement network
1926 * for graceful handover. This may not be called if we have a hard loss
1927 * (loss without warning). This may be followed by either a
1928 * {@link NetworkCallbackListener#onLost} call or a
1929 * {@link NetworkCallbackListener#onAvailable} call for this network depending
1930 * on whether we lose or regain it.
1931 *
1932 * @param networkRequest The {@link NetworkRequest} used to initiate the request.
1933 * @param network The {@link Network} of the failing network.
1934 * @param maxSecToLive The time in seconds the framework will attempt to keep the
1935 * network connected. Note that the network may suffers a
1936 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07001937 */
1938 public void onLosing(NetworkRequest networkRequest, Network network, int maxSecToLive) {}
1939
1940 /**
1941 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001942 * graceful failure ends.
1943 *
1944 * @param networkRequest The {@link NetworkRequest} used to initiate the request.
1945 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07001946 */
1947 public void onLost(NetworkRequest networkRequest, Network network) {}
1948
1949 /**
1950 * Called if no network is found in the given timeout time. If no timeout is given,
1951 * this will not be called.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001952 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07001953 */
1954 public void onUnavailable(NetworkRequest networkRequest) {}
1955
1956 /**
1957 * Called when the network the framework connected to for this request
1958 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001959 *
1960 * @param networkRequest The {@link NetworkRequest} used to initiate the request.
1961 * @param network The {@link Network} whose capabilities have changed.
1962 * @param networkCapabilities The new {@link NetworkCapabilities} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07001963 */
Robert Greenwalt9258c642014-03-26 16:47:06 -07001964 public void onNetworkCapabilitiesChanged(NetworkRequest networkRequest, Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07001965 NetworkCapabilities networkCapabilities) {}
1966
1967 /**
1968 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001969 * changes {@link LinkProperties}.
1970 *
1971 * @param networkRequest The {@link NetworkRequest} used to initiate the request.
1972 * @param network The {@link Network} whose link properties have changed.
1973 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07001974 */
Robert Greenwalt9258c642014-03-26 16:47:06 -07001975 public void onLinkPropertiesChanged(NetworkRequest networkRequest, Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07001976 LinkProperties linkProperties) {}
1977
1978 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001979 * Called when a {@link #releaseNetworkRequest} call concludes and the registered
1980 * callbacks will no longer be used.
1981 *
1982 * @param networkRequest The {@link NetworkRequest} used to initiate the request.
Robert Greenwalt7b816022014-04-18 15:25:25 -07001983 */
Robert Greenwalt9258c642014-03-26 16:47:06 -07001984 public void onReleased(NetworkRequest networkRequest) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07001985 }
1986
Robert Greenwalt9258c642014-03-26 16:47:06 -07001987 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
1988 /** @hide obj = pair(NetworkRequest, Network) */
1989 public static final int CALLBACK_PRECHECK = BASE + 1;
1990 /** @hide obj = pair(NetworkRequest, Network) */
1991 public static final int CALLBACK_AVAILABLE = BASE + 2;
1992 /** @hide obj = pair(NetworkRequest, Network), arg1 = ttl */
1993 public static final int CALLBACK_LOSING = BASE + 3;
1994 /** @hide obj = pair(NetworkRequest, Network) */
1995 public static final int CALLBACK_LOST = BASE + 4;
1996 /** @hide obj = NetworkRequest */
1997 public static final int CALLBACK_UNAVAIL = BASE + 5;
1998 /** @hide obj = pair(NetworkRequest, Network) */
1999 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2000 /** @hide obj = pair(NetworkRequest, Network) */
2001 public static final int CALLBACK_IP_CHANGED = BASE + 7;
2002 /** @hide obj = NetworkRequest */
2003 public static final int CALLBACK_RELEASED = BASE + 8;
2004 /** @hide */
2005 public static final int CALLBACK_EXIT = BASE + 9;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002006 /** @hide obj = NetworkCapabilities, arg1 = seq number */
2007 private static final int EXPIRE_LEGACY_REQUEST = BASE + 10;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002008
Robert Greenwalt562cc542014-05-15 18:07:26 -07002009 private class CallbackHandler extends Handler {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002010 private final HashMap<NetworkRequest, NetworkCallbackListener>mCallbackMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002011 private final AtomicInteger mRefCount;
2012 private static final String TAG = "ConnectivityManager.CallbackHandler";
2013 private final ConnectivityManager mCm;
2014
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002015 CallbackHandler(Looper looper, HashMap<NetworkRequest, NetworkCallbackListener>callbackMap,
Robert Greenwalt9258c642014-03-26 16:47:06 -07002016 AtomicInteger refCount, ConnectivityManager cm) {
2017 super(looper);
2018 mCallbackMap = callbackMap;
2019 mRefCount = refCount;
2020 mCm = cm;
2021 }
2022
2023 @Override
2024 public void handleMessage(Message message) {
2025 Log.d(TAG, "CM callback handler got msg " + message.what);
2026 switch (message.what) {
2027 case CALLBACK_PRECHECK: {
2028 NetworkRequest request = getNetworkRequest(message);
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002029 NetworkCallbackListener callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002030 if (callbacks != null) {
2031 callbacks.onPreCheck(request, getNetwork(message));
2032 } else {
2033 Log.e(TAG, "callback not found for PRECHECK message");
2034 }
2035 break;
2036 }
2037 case CALLBACK_AVAILABLE: {
2038 NetworkRequest request = getNetworkRequest(message);
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002039 NetworkCallbackListener callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002040 if (callbacks != null) {
2041 callbacks.onAvailable(request, getNetwork(message));
2042 } else {
2043 Log.e(TAG, "callback not found for AVAILABLE message");
2044 }
2045 break;
2046 }
2047 case CALLBACK_LOSING: {
2048 NetworkRequest request = getNetworkRequest(message);
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002049 NetworkCallbackListener callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002050 if (callbacks != null) {
2051 callbacks.onLosing(request, getNetwork(message), message.arg1);
2052 } else {
2053 Log.e(TAG, "callback not found for LOSING message");
2054 }
2055 break;
2056 }
2057 case CALLBACK_LOST: {
2058 NetworkRequest request = getNetworkRequest(message);
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002059 NetworkCallbackListener callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002060 if (callbacks != null) {
2061 callbacks.onLost(request, getNetwork(message));
2062 } else {
2063 Log.e(TAG, "callback not found for LOST message");
2064 }
2065 break;
2066 }
2067 case CALLBACK_UNAVAIL: {
2068 NetworkRequest req = (NetworkRequest)message.obj;
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002069 NetworkCallbackListener callbacks = null;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002070 synchronized(mCallbackMap) {
2071 callbacks = mCallbackMap.get(req);
2072 }
2073 if (callbacks != null) {
2074 callbacks.onUnavailable(req);
2075 } else {
2076 Log.e(TAG, "callback not found for UNAVAIL message");
2077 }
2078 break;
2079 }
2080 case CALLBACK_CAP_CHANGED: {
2081 NetworkRequest request = getNetworkRequest(message);
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002082 NetworkCallbackListener callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002083 if (callbacks != null) {
2084 Network network = getNetwork(message);
2085 NetworkCapabilities cap = mCm.getNetworkCapabilities(network);
2086
2087 callbacks.onNetworkCapabilitiesChanged(request, network, cap);
2088 } else {
2089 Log.e(TAG, "callback not found for CHANGED message");
2090 }
2091 break;
2092 }
2093 case CALLBACK_IP_CHANGED: {
2094 NetworkRequest request = getNetworkRequest(message);
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002095 NetworkCallbackListener callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002096 if (callbacks != null) {
2097 Network network = getNetwork(message);
2098 LinkProperties lp = mCm.getLinkProperties(network);
2099
2100 callbacks.onLinkPropertiesChanged(request, network, lp);
2101 } else {
2102 Log.e(TAG, "callback not found for CHANGED message");
2103 }
2104 break;
2105 }
2106 case CALLBACK_RELEASED: {
2107 NetworkRequest req = (NetworkRequest)message.obj;
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002108 NetworkCallbackListener callbacks = null;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002109 synchronized(mCallbackMap) {
2110 callbacks = mCallbackMap.remove(req);
2111 }
2112 if (callbacks != null) {
2113 callbacks.onReleased(req);
2114 } else {
2115 Log.e(TAG, "callback not found for CANCELED message");
2116 }
2117 synchronized(mRefCount) {
2118 if (mRefCount.decrementAndGet() == 0) {
2119 getLooper().quit();
2120 }
2121 }
2122 break;
2123 }
2124 case CALLBACK_EXIT: {
2125 Log.d(TAG, "Listener quiting");
2126 getLooper().quit();
2127 break;
2128 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07002129 case EXPIRE_LEGACY_REQUEST: {
2130 expireRequest((NetworkCapabilities)message.obj, message.arg1);
2131 break;
2132 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002133 }
2134 }
2135
2136 private NetworkRequest getNetworkRequest(Message msg) {
2137 return (NetworkRequest)(msg.obj);
2138 }
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002139 private NetworkCallbackListener getCallbacks(NetworkRequest req) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002140 synchronized(mCallbackMap) {
2141 return mCallbackMap.get(req);
2142 }
2143 }
2144 private Network getNetwork(Message msg) {
2145 return new Network(msg.arg2);
2146 }
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002147 private NetworkCallbackListener removeCallbacks(Message msg) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002148 NetworkRequest req = (NetworkRequest)msg.obj;
2149 synchronized(mCallbackMap) {
2150 return mCallbackMap.remove(req);
2151 }
2152 }
2153 }
2154
2155 private void addCallbackListener() {
2156 synchronized(sCallbackRefCount) {
2157 if (sCallbackRefCount.incrementAndGet() == 1) {
2158 // TODO - switch this over to a ManagerThread or expire it when done
2159 HandlerThread callbackThread = new HandlerThread("ConnectivityManager");
2160 callbackThread.start();
2161 sCallbackHandler = new CallbackHandler(callbackThread.getLooper(),
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002162 sNetworkCallbackListener, sCallbackRefCount, this);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002163 }
2164 }
2165 }
2166
2167 private void removeCallbackListener() {
2168 synchronized(sCallbackRefCount) {
2169 if (sCallbackRefCount.decrementAndGet() == 0) {
2170 sCallbackHandler.obtainMessage(CALLBACK_EXIT).sendToTarget();
2171 sCallbackHandler = null;
2172 }
2173 }
2174 }
2175
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002176 static final HashMap<NetworkRequest, NetworkCallbackListener> sNetworkCallbackListener =
2177 new HashMap<NetworkRequest, NetworkCallbackListener>();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002178 static final AtomicInteger sCallbackRefCount = new AtomicInteger(0);
2179 static CallbackHandler sCallbackHandler = null;
2180
2181 private final static int LISTEN = 1;
2182 private final static int REQUEST = 2;
2183
Robert Greenwalt562cc542014-05-15 18:07:26 -07002184 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need,
2185 NetworkCallbackListener networkCallbackListener, int timeoutSec, int action,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07002186 int legacyType) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002187 NetworkRequest networkRequest = null;
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002188 if (networkCallbackListener == null) {
2189 throw new IllegalArgumentException("null NetworkCallbackListener");
2190 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002191 if (need == null) throw new IllegalArgumentException("null NetworkCapabilities");
2192 try {
2193 addCallbackListener();
2194 if (action == LISTEN) {
2195 networkRequest = mService.listenForNetwork(need, new Messenger(sCallbackHandler),
2196 new Binder());
2197 } else {
2198 networkRequest = mService.requestNetwork(need, new Messenger(sCallbackHandler),
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07002199 timeoutSec, new Binder(), legacyType);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002200 }
2201 if (networkRequest != null) {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002202 synchronized(sNetworkCallbackListener) {
2203 sNetworkCallbackListener.put(networkRequest, networkCallbackListener);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002204 }
2205 }
2206 } catch (RemoteException e) {}
2207 if (networkRequest == null) removeCallbackListener();
2208 return networkRequest;
2209 }
2210
2211 /**
2212 * Request a network to satisfy a set of {@link NetworkCapabilities}.
2213 *
2214 * This {@link NetworkRequest} will live until released via
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002215 * {@link #releaseNetworkRequest} or the calling application exits.
2216 * Status of the request can be followed by listening to the various
2217 * callbacks described in {@link NetworkCallbackListener}. The {@link Network}
2218 * can be used to direct traffic to the network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002219 *
2220 * @param need {@link NetworkCapabilities} required by this request.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002221 * @param networkCallbackListener The {@link NetworkCallbackListener} to be utilized for this
2222 * request. Note the callbacks can be shared by multiple
2223 * requests and the NetworkRequest token utilized to
2224 * determine to which request the callback relates.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002225 * @return A {@link NetworkRequest} object identifying the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002226 */
2227 public NetworkRequest requestNetwork(NetworkCapabilities need,
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002228 NetworkCallbackListener networkCallbackListener) {
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07002229 return sendRequestForNetwork(need, networkCallbackListener, 0, REQUEST, TYPE_NONE);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002230 }
2231
2232 /**
2233 * Request a network to satisfy a set of {@link NetworkCapabilities}, limited
2234 * by a timeout.
2235 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002236 * This function behaves identically to the non-timedout version, but if a suitable
2237 * network is not found within the given time (in Seconds) the
2238 * {@link NetworkCallbackListener#unavailable} callback is called. The request must
2239 * still be released normally by calling {@link releaseNetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002240 * @param need {@link NetworkCapabilities} required by this request.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002241 * @param networkCallbackListener The callbacks to be utilized for this request. Note
Robert Greenwalt9258c642014-03-26 16:47:06 -07002242 * the callbacks can be shared by multiple requests and
2243 * the NetworkRequest token utilized to determine to which
2244 * request the callback relates.
2245 * @param timeoutSec The time in seconds to attempt looking for a suitable network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002246 * before {@link NetworkCallbackListener#unavailable} is called.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002247 * @return A {@link NetworkRequest} object identifying the request.
2248 * @hide
2249 */
2250 public NetworkRequest requestNetwork(NetworkCapabilities need,
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002251 NetworkCallbackListener networkCallbackListener, int timeoutSec) {
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07002252 return sendRequestForNetwork(need, networkCallbackListener, timeoutSec, REQUEST,
2253 TYPE_NONE);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002254 }
2255
2256 /**
2257 * The maximum number of seconds the framework will look for a suitable network
2258 * during a timeout-equiped call to {@link requestNetwork}.
2259 * {@hide}
2260 */
2261 public final static int MAX_NETWORK_REQUEST_TIMEOUT_SEC = 100 * 60;
2262
2263 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002264 * The lookup key for a {@link Network} object included with the intent after
2265 * succesfully finding a network for the applications request. Retrieve it with
2266 * {@link android.content.Intent#getParcelableExtra(String)}.
2267 */
2268 public static final String EXTRA_NETWORK_REQUEST_NETWORK = "networkRequestNetwork";
2269
2270 /**
2271 * The lookup key for a {@link NetworkCapabilities} object included with the intent after
2272 * succesfully finding a network for the applications request. Retrieve it with
2273 * {@link android.content.Intent#getParcelableExtra(String)}.
2274 */
2275 public static final String EXTRA_NETWORK_REQUEST_NETWORK_CAPABILITIES =
2276 "networkRequestNetworkCapabilities";
2277
2278
2279 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07002280 * Request a network to satisfy a set of {@link NetworkCapabilities}.
2281 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002282 * This function behavies identically to the callback-equiped version, but instead
2283 * of {@link NetworkCallbackListener} a {@link PendingIntent} is used. This means
2284 * the request may outlive the calling application and get called back when a suitable
2285 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002286 * <p>
2287 * The operation is an Intent broadcast that goes to a broadcast receiver that
2288 * you registered with {@link Context#registerReceiver} or through the
2289 * &lt;receiver&gt; tag in an AndroidManifest.xml file
2290 * <p>
2291 * The operation Intent is delivered with two extras, a {@link Network} typed
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002292 * extra called {@link #EXTRA_NETWORK_REQUEST_NETWORK} and a {@link NetworkCapabilities}
2293 * typed extra called {@link #EXTRA_NETWORK_REQUEST_NETWORK_CAPABILITIES} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07002294 * the original requests parameters. It is important to create a new,
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002295 * {@link NetworkCallbackListener} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07002296 * Intent to reserve the network or it will be released shortly after the Intent
2297 * is processed.
2298 * <p>
2299 * If there is already an request for this Intent registered (with the equality of
2300 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002301 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002302 * <p>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002303 * The request may be released normally by calling {@link #releaseNetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002304 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002305 * @param need {@link NetworkCapabilities} required by this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002306 * @param operation Action to perform when the network is available (corresponds
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002307 * to the {@link NetworkCallbackListener#onAvailable} call. Typically
Robert Greenwalt9258c642014-03-26 16:47:06 -07002308 * comes from {@link PendingIntent#getBroadcast}.
2309 * @return A {@link NetworkRequest} object identifying the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002310 */
2311 public NetworkRequest requestNetwork(NetworkCapabilities need, PendingIntent operation) {
2312 try {
2313 return mService.pendingRequestForNetwork(need, operation);
2314 } catch (RemoteException e) {}
2315 return null;
2316 }
2317
2318 /**
2319 * Registers to receive notifications about all networks which satisfy the given
2320 * {@link NetworkCapabilities}. The callbacks will continue to be called until
2321 * either the application exits or the request is released using
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002322 * {@link #releaseNetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002323 *
2324 * @param need {@link NetworkCapabilities} required by this request.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002325 * @param networkCallbackListener The {@link NetworkCallbackListener} to be called as suitable
Robert Greenwalt9258c642014-03-26 16:47:06 -07002326 * networks change state.
2327 * @return A {@link NetworkRequest} object identifying the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002328 */
2329 public NetworkRequest listenForNetwork(NetworkCapabilities need,
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002330 NetworkCallbackListener networkCallbackListener) {
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07002331 return sendRequestForNetwork(need, networkCallbackListener, 0, LISTEN, TYPE_NONE);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002332 }
2333
2334 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002335 * Releases a {@link NetworkRequest} generated either through a {@link #requestNetwork}
2336 * or a {@link #listenForNetwork} call. The {@link NetworkCallbackListener} given in the
2337 * earlier call may continue receiving calls until the
2338 * {@link NetworkCallbackListener#onReleased} function is called, signifying the end
2339 * of the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002340 *
2341 * @param networkRequest The {@link NetworkRequest} generated by an earlier call to
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002342 * {@link #requestNetwork} or {@link #listenForNetwork}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002343 */
2344 public void releaseNetworkRequest(NetworkRequest networkRequest) {
2345 if (networkRequest == null) throw new IllegalArgumentException("null NetworkRequest");
2346 try {
2347 mService.releaseNetworkRequest(networkRequest);
2348 } catch (RemoteException e) {}
2349 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002350}