blob: 3e9b3d61984c2cfca95accc550cccfbb4ea5c70a [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016package android.net;
17
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -070018import static com.android.internal.util.Preconditions.checkNotNull;
19
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.annotation.SdkConstant;
21import android.annotation.SdkConstant.SdkConstantType;
Robert Greenwalt9258c642014-03-26 16:47:06 -070022import android.app.PendingIntent;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070023import android.content.Context;
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070024import android.content.Intent;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -040025import android.net.NetworkUtils;
Robert Greenwalt42acef32009-08-12 16:08:25 -070026import android.os.Binder;
Jeff Sharkey3a844fc2011-08-16 14:37:57 -070027import android.os.Build.VERSION_CODES;
Robert Greenwalt9258c642014-03-26 16:47:06 -070028import android.os.Handler;
29import android.os.HandlerThread;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080030import android.os.IBinder;
31import android.os.INetworkActivityListener;
32import android.os.INetworkManagementService;
Robert Greenwalt9258c642014-03-26 16:47:06 -070033import android.os.Looper;
34import android.os.Message;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -070035import android.os.Messenger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.RemoteException;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080037import android.os.ServiceManager;
Jeff Sharkey961e3042011-08-29 16:02:57 -070038import android.provider.Settings;
Wink Saville36ffb042014-12-05 11:10:30 -080039import android.telephony.SubscriptionManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080040import android.util.ArrayMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -070041import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
Robert Greenwaltafa05c02014-05-21 20:04:36 -070043import com.android.internal.telephony.ITelephony;
Robert Greenwalt562cc542014-05-15 18:07:26 -070044import com.android.internal.telephony.PhoneConstants;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070045import com.android.internal.util.Protocol;
46
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070047import java.net.InetAddress;
Robert Greenwalt9258c642014-03-26 16:47:06 -070048import java.util.concurrent.atomic.AtomicInteger;
49import java.util.HashMap;
50
Paul Jensenc91b5342014-08-27 12:38:45 -040051import libcore.net.event.NetworkEventDispatcher;
52
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080053/**
54 * Class that answers queries about the state of network connectivity. It also
55 * notifies applications when network connectivity changes. Get an instance
56 * of this class by calling
57 * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}.
58 * <p>
59 * The primary responsibilities of this class are to:
60 * <ol>
61 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
62 * <li>Send broadcast intents when network connectivity changes</li>
63 * <li>Attempt to "fail over" to another network when connectivity to a network
64 * is lost</li>
65 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
66 * state of the available networks</li>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070067 * <li>Provide an API that allows applications to request and select networks for their data
68 * traffic</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080069 * </ol>
70 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070071public class ConnectivityManager {
72 private static final String TAG = "ConnectivityManager";
73
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080074 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070075 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080076 * been established or lost. The NetworkInfo for the affected network is
77 * sent as an extra; it should be consulted to see what kind of
78 * connectivity event occurred.
79 * <p/>
80 * If this is a connection that was the result of failing over from a
81 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
82 * set to true.
83 * <p/>
84 * For a loss of connectivity, if the connectivity manager is attempting
85 * to connect (or has already connected) to another network, the
86 * NetworkInfo for the new network is also passed as an extra. This lets
87 * any receivers of the broadcast know that they should not necessarily
88 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -080089 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090 * the failover attempt succeeded (and so there is still overall data
91 * connectivity), or that the failover attempt failed, meaning that all
92 * connectivity has been lost.
93 * <p/>
94 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
95 * is set to {@code true} if there are no connected networks at all.
96 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -080097 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080098 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -070099
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800100 /**
101 * The lookup key for a {@link NetworkInfo} object. Retrieve with
102 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700103 *
104 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
105 * should always obtain network information through
106 * {@link #getActiveNetworkInfo()} or
107 * {@link #getAllNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700108 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800109 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700110 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800111 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700112
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800113 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700114 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
115 * Can be used with {@link #getNetworkInfo(int)} to get {@link NetworkInfo}
116 * state based on the calling application.
117 *
118 * @see android.content.Intent#getIntExtra(String, int)
119 */
120 public static final String EXTRA_NETWORK_TYPE = "networkType";
121
122 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800123 * The lookup key for a boolean that indicates whether a connect event
124 * is for a network to which the connectivity manager was failing over
125 * following a disconnect on another network.
126 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
127 */
128 public static final String EXTRA_IS_FAILOVER = "isFailover";
129 /**
130 * The lookup key for a {@link NetworkInfo} object. This is supplied when
131 * there is another network that it may be possible to connect to. Retrieve with
132 * {@link android.content.Intent#getParcelableExtra(String)}.
133 */
134 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
135 /**
136 * The lookup key for a boolean that indicates whether there is a
137 * complete lack of connectivity, i.e., no network is available.
138 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
139 */
140 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
141 /**
142 * The lookup key for a string that indicates why an attempt to connect
143 * to a network failed. The string has no particular structure. It is
144 * intended to be used in notifications presented to users. Retrieve
145 * it with {@link android.content.Intent#getStringExtra(String)}.
146 */
147 public static final String EXTRA_REASON = "reason";
148 /**
149 * The lookup key for a string that provides optionally supplied
150 * extra information about the network state. The information
151 * may be passed up from the lower networking layers, and its
152 * meaning may be specific to a particular network type. Retrieve
153 * it with {@link android.content.Intent#getStringExtra(String)}.
154 */
155 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700156 /**
157 * The lookup key for an int that provides information about
158 * our connection to the internet at large. 0 indicates no connection,
159 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700160 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700161 * {@hide}
162 */
163 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164
165 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700166 * Broadcast action to indicate the change of data activity status
167 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800168 * The network becomes active when data transmission is started, or
169 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700170 * {@hide}
171 */
172 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
173 public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
174 /**
175 * The lookup key for an enum that indicates the network device type on which this data activity
176 * change happens.
177 * {@hide}
178 */
179 public static final String EXTRA_DEVICE_TYPE = "deviceType";
180 /**
181 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
182 * it is actively sending or receiving data and {@code false} means it is idle.
183 * {@hide}
184 */
185 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700186 /**
187 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
188 * {@hide}
189 */
190 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700191
192 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800193 * Broadcast Action: The setting for background data usage has changed
194 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
195 * <p>
196 * If an application uses the network in the background, it should listen
197 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700198 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800199 * <p>
200 *
201 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
202 * of background data depends on several combined factors, and
203 * this broadcast is no longer sent. Instead, when background
204 * data is unavailable, {@link #getActiveNetworkInfo()} will now
205 * appear disconnected. During first boot after a platform
206 * upgrade, this broadcast will be sent once if
207 * {@link #getBackgroundDataSetting()} was {@code false} before
208 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800209 */
210 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800211 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800212 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
213 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
214
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700215 /**
216 * Broadcast Action: The network connection may not be good
217 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
218 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
219 * the network and it's condition.
220 * @hide
221 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800222 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700223 public static final String INET_CONDITION_ACTION =
224 "android.net.conn.INET_CONDITION_ACTION";
225
Robert Greenwalt42acef32009-08-12 16:08:25 -0700226 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800227 * Broadcast Action: A tetherable connection has come or gone.
228 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
229 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER} and
230 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
231 * the current state of tethering. Each include a list of
232 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800233 * @hide
234 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800235 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800236 public static final String ACTION_TETHER_STATE_CHANGED =
237 "android.net.conn.TETHER_STATE_CHANGED";
238
239 /**
240 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800241 * gives a String[] listing all the interfaces configured for
242 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800243 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800244 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800245
246 /**
247 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800248 * gives a String[] listing all the interfaces currently tethered
249 * (ie, has dhcp support and packets potentially forwarded/NATed)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800250 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800251 public static final String EXTRA_ACTIVE_TETHER = "activeArray";
252
253 /**
254 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800255 * gives a String[] listing all the interfaces we tried to tether and
256 * failed. Use {@link #getLastTetherError} to find the error code
257 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800258 */
259 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800260
261 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800262 * Broadcast Action: The captive portal tracker has finished its test.
263 * Sent only while running Setup Wizard, in lieu of showing a user
264 * notification.
265 * @hide
266 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800267 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800268 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
269 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
270 /**
271 * The lookup key for a boolean that indicates whether a captive portal was detected.
272 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
273 * @hide
274 */
275 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
276
277 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +0900278 * Action used to display a dialog that asks the user whether to connect to a network that is
279 * not validated. This intent is used to start the dialog in settings via startActivity.
280 *
281 * @hide
282 */
283 public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED";
284
285 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800286 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700287 * @hide
288 */
289 public static final int TYPE_NONE = -1;
290
291 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800292 * The Mobile data connection. When active, all data traffic
293 * will use this network type's interface by default
294 * (it has a default route)
Robert Greenwalt42acef32009-08-12 16:08:25 -0700295 */
296 public static final int TYPE_MOBILE = 0;
297 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800298 * The WIFI data connection. When active, all data traffic
299 * will use this network type's interface by default
300 * (it has a default route).
Robert Greenwalt42acef32009-08-12 16:08:25 -0700301 */
302 public static final int TYPE_WIFI = 1;
303 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800304 * An MMS-specific Mobile data connection. This network type may use the
305 * same network interface as {@link #TYPE_MOBILE} or it may use a different
306 * one. This is used by applications needing to talk to the carrier's
307 * Multimedia Messaging Service servers.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700308 */
309 public static final int TYPE_MOBILE_MMS = 2;
310 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800311 * A SUPL-specific Mobile data connection. This network type may use the
312 * same network interface as {@link #TYPE_MOBILE} or it may use a different
313 * one. This is used by applications needing to talk to the carrier's
314 * Secure User Plane Location servers for help locating the device.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700315 */
316 public static final int TYPE_MOBILE_SUPL = 3;
317 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800318 * A DUN-specific Mobile data connection. This network type may use the
319 * same network interface as {@link #TYPE_MOBILE} or it may use a different
320 * one. This is sometimes by the system when setting up an upstream connection
321 * for tethering so that the carrier is aware of DUN traffic.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700322 */
323 public static final int TYPE_MOBILE_DUN = 4;
324 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800325 * A High Priority Mobile data connection. This network type uses the
326 * same network interface as {@link #TYPE_MOBILE} but the routing setup
327 * is different. Only requesting processes will have access to the
328 * Mobile DNS servers and only IP's explicitly requested via {@link #requestRouteToHost}
329 * will route over this interface if no default route exists.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700330 */
331 public static final int TYPE_MOBILE_HIPRI = 5;
jsh8214deb2010-03-11 15:04:43 -0800332 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800333 * The WiMAX data connection. When active, all data traffic
334 * will use this network type's interface by default
335 * (it has a default route).
jsh8214deb2010-03-11 15:04:43 -0800336 */
337 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800338
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800339 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800340 * The Bluetooth data connection. When active, all data traffic
341 * will use this network type's interface by default
342 * (it has a default route).
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800343 */
344 public static final int TYPE_BLUETOOTH = 7;
345
Robert Greenwalt60810842011-04-22 15:28:18 -0700346 /**
347 * Dummy data connection. This should not be used on shipping devices.
348 */
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800349 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800350
Robert Greenwalt60810842011-04-22 15:28:18 -0700351 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800352 * The Ethernet data connection. When active, all data traffic
353 * will use this network type's interface by default
354 * (it has a default route).
Robert Greenwalt60810842011-04-22 15:28:18 -0700355 */
Robert Greenwalte12aec92011-01-28 14:48:37 -0800356 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700357
Wink Saville9d7d6282011-03-12 14:52:01 -0800358 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800359 * Over the air Administration.
Wink Saville9d7d6282011-03-12 14:52:01 -0800360 * {@hide}
361 */
362 public static final int TYPE_MOBILE_FOTA = 10;
363
364 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800365 * IP Multimedia Subsystem.
Wink Saville9d7d6282011-03-12 14:52:01 -0800366 * {@hide}
367 */
368 public static final int TYPE_MOBILE_IMS = 11;
369
370 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800371 * Carrier Branded Services.
Wink Saville9d7d6282011-03-12 14:52:01 -0800372 * {@hide}
373 */
374 public static final int TYPE_MOBILE_CBS = 12;
375
repo syncaea743a2011-07-29 23:55:49 -0700376 /**
377 * A Wi-Fi p2p connection. Only requesting processes will have access to
378 * the peers connected.
379 * {@hide}
380 */
381 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800382
Wink Saville5e56bc52013-07-29 15:00:57 -0700383 /**
384 * The network to use for initially attaching to the network
385 * {@hide}
386 */
387 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700388
Ram3e0e3bc2014-06-26 11:03:44 -0700389/**
390 * Emergency PDN connection for emergency calls
391 * {@hide}
392 */
393 public static final int TYPE_MOBILE_EMERGENCY = 15;
394
Hui Lu1c5624a2014-01-15 11:05:36 -0500395 /**
396 * The network that uses proxy to achieve connectivity.
397 * {@hide}
398 */
399 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700400
Robert Greenwalt8283f882014-07-07 17:09:01 -0700401 /**
402 * A virtual network using one or more native bearers.
403 * It may or may not be providing security services.
404 */
405 public static final int TYPE_VPN = 17;
Hui Lu1c5624a2014-01-15 11:05:36 -0500406
407 /** {@hide} */
Robert Greenwalt8283f882014-07-07 17:09:01 -0700408 public static final int MAX_RADIO_TYPE = TYPE_VPN;
409
410 /** {@hide} */
411 public static final int MAX_NETWORK_TYPE = TYPE_VPN;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800412
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800413 /**
414 * If you want to set the default network preference,you can directly
415 * change the networkAttributes array in framework's config.xml.
416 *
417 * @deprecated Since we support so many more networks now, the single
418 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800419 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800420 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800421 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800422 * from an App.
423 */
424 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800425 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
426
Jeff Sharkey625239a2012-09-26 22:03:49 -0700427 /**
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700428 * @hide
429 */
Robert Greenwalt7569f182014-06-08 16:42:59 -0700430 public final static int REQUEST_ID_UNSET = 0;
431
Paul Jensen5d59e782014-07-11 12:28:19 -0400432 /**
433 * A NetID indicating no Network is selected.
434 * Keep in sync with bionic/libc/dns/include/resolv_netid.h
435 * @hide
436 */
437 public static final int NETID_UNSET = 0;
438
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700439 private final IConnectivityManager mService;
Paul Jensene0bef712014-12-10 15:12:18 -0500440 /**
441 * A kludge to facilitate static access where a Context pointer isn't available, like in the
442 * case of the static set/getProcessDefaultNetwork methods and from the Network class.
443 * TODO: Remove this after deprecating the static methods in favor of non-static methods or
444 * methods that take a Context argument.
445 */
446 private static ConnectivityManager sInstance;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800447
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800448 private INetworkManagementService mNMService;
449
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800450 /**
451 * Tests if a given integer represents a valid network type.
452 * @param networkType the type to be tested
453 * @return a boolean. {@code true} if the type is valid, else {@code false}
454 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700455 public static boolean isNetworkTypeValid(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700456 return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800457 }
458
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800459 /**
460 * Returns a non-localized string representing a given network type.
461 * ONLY used for debugging output.
462 * @param type the type needing naming
463 * @return a String for the given type, or a string version of the type ("87")
464 * if no name is known.
465 * {@hide}
466 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700467 public static String getNetworkTypeName(int type) {
468 switch (type) {
469 case TYPE_MOBILE:
470 return "MOBILE";
471 case TYPE_WIFI:
472 return "WIFI";
473 case TYPE_MOBILE_MMS:
474 return "MOBILE_MMS";
475 case TYPE_MOBILE_SUPL:
476 return "MOBILE_SUPL";
477 case TYPE_MOBILE_DUN:
478 return "MOBILE_DUN";
479 case TYPE_MOBILE_HIPRI:
480 return "MOBILE_HIPRI";
481 case TYPE_WIMAX:
482 return "WIMAX";
483 case TYPE_BLUETOOTH:
484 return "BLUETOOTH";
485 case TYPE_DUMMY:
486 return "DUMMY";
487 case TYPE_ETHERNET:
488 return "ETHERNET";
489 case TYPE_MOBILE_FOTA:
490 return "MOBILE_FOTA";
491 case TYPE_MOBILE_IMS:
492 return "MOBILE_IMS";
493 case TYPE_MOBILE_CBS:
494 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700495 case TYPE_WIFI_P2P:
496 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700497 case TYPE_MOBILE_IA:
498 return "MOBILE_IA";
Ram3e0e3bc2014-06-26 11:03:44 -0700499 case TYPE_MOBILE_EMERGENCY:
500 return "MOBILE_EMERGENCY";
Hui Lu1c5624a2014-01-15 11:05:36 -0500501 case TYPE_PROXY:
502 return "PROXY";
Erik Kline37fbfa12014-11-19 17:23:41 +0900503 case TYPE_VPN:
504 return "VPN";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700505 default:
506 return Integer.toString(type);
507 }
508 }
509
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800510 /**
511 * Checks if a given type uses the cellular data connection.
512 * This should be replaced in the future by a network property.
513 * @param networkType the type to check
514 * @return a boolean - {@code true} if uses cellular network, else {@code false}
515 * {@hide}
516 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700517 public static boolean isNetworkTypeMobile(int networkType) {
518 switch (networkType) {
519 case TYPE_MOBILE:
520 case TYPE_MOBILE_MMS:
521 case TYPE_MOBILE_SUPL:
522 case TYPE_MOBILE_DUN:
523 case TYPE_MOBILE_HIPRI:
524 case TYPE_MOBILE_FOTA:
525 case TYPE_MOBILE_IMS:
526 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700527 case TYPE_MOBILE_IA:
Ram3e0e3bc2014-06-26 11:03:44 -0700528 case TYPE_MOBILE_EMERGENCY:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700529 return true;
530 default:
531 return false;
532 }
533 }
534
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800535 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700536 * Checks if the given network type is backed by a Wi-Fi radio.
537 *
538 * @hide
539 */
540 public static boolean isNetworkTypeWifi(int networkType) {
541 switch (networkType) {
542 case TYPE_WIFI:
543 case TYPE_WIFI_P2P:
544 return true;
545 default:
546 return false;
547 }
548 }
549
550 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800551 * Specifies the preferred network type. When the device has more
552 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800553 *
554 * @param preference the network type to prefer over all others. It is
555 * unspecified what happens to the old preferred network in the
556 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700557 * @deprecated Functionality has been removed as it no longer makes sense,
558 * with many more than two networks - we'd need an array to express
559 * preference. Instead we use dynamic network properties of
560 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800561 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800562 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800563 }
564
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800565 /**
566 * Retrieves the current preferred network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800567 *
568 * @return an integer representing the preferred network type
569 *
570 * <p>This method requires the caller to hold the permission
571 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700572 * @deprecated Functionality has been removed as it no longer makes sense,
573 * with many more than two networks - we'd need an array to express
574 * preference. Instead we use dynamic network properties of
575 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800576 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800577 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700578 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800579 }
580
Scott Main671644c2011-10-06 19:02:28 -0700581 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800582 * Returns details about the currently active default data network. When
583 * connected, this network is the default route for outgoing connections.
584 * You should always check {@link NetworkInfo#isConnected()} before initiating
585 * network traffic. This may return {@code null} when there is no default
586 * network.
587 *
588 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500589 * or {@code null} if no default network is currently active
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800590 *
Paul Jensen0d719ca2015-02-13 14:18:39 -0500591 * <p>This method requires the caller to hold the permission
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700592 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700593 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800594 public NetworkInfo getActiveNetworkInfo() {
595 try {
596 return mService.getActiveNetworkInfo();
597 } catch (RemoteException e) {
598 return null;
599 }
600 }
601
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800602 /**
Paul Jensen31a94f42015-02-13 14:18:39 -0500603 * Returns a {@link Network} object corresponding to the currently active
604 * default data network. In the event that the current active default data
605 * network disconnects, the returned {@code Network} object will no longer
606 * be usable. This will return {@code null} when there is no default
607 * network.
608 *
609 * @return a {@link Network} object for the current default network or
610 * {@code null} if no default network is currently active
611 *
612 * <p>This method requires the caller to hold the permission
613 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
614 */
615 public Network getActiveNetwork() {
616 try {
617 return mService.getActiveNetwork();
618 } catch (RemoteException e) {
619 return null;
620 }
621 }
622
623 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800624 * Returns details about the currently active default data network
625 * for a given uid. This is for internal use only to avoid spying
626 * other apps.
627 *
628 * @return a {@link NetworkInfo} object for the current default network
629 * for the given uid or {@code null} if no default network is
630 * available for the specified uid.
631 *
632 * <p>This method requires the caller to hold the permission
633 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
634 * {@hide}
635 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700636 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
637 try {
638 return mService.getActiveNetworkInfoForUid(uid);
639 } catch (RemoteException e) {
640 return null;
641 }
642 }
643
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800644 /**
645 * Returns connection status information about a particular
646 * network type.
647 *
648 * @param networkType integer specifying which networkType in
649 * which you're interested.
650 * @return a {@link NetworkInfo} object for the requested
651 * network type or {@code null} if the type is not
652 * supported by the device.
653 *
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700654 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800655 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
656 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800657 public NetworkInfo getNetworkInfo(int networkType) {
658 try {
659 return mService.getNetworkInfo(networkType);
660 } catch (RemoteException e) {
661 return null;
662 }
663 }
664
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800665 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700666 * Returns connection status information about a particular
667 * Network.
668 *
669 * @param network {@link Network} specifying which network
670 * in which you're interested.
671 * @return a {@link NetworkInfo} object for the requested
672 * network or {@code null} if the {@code Network}
673 * is not valid.
674 *
675 * <p>This method requires the caller to hold the permission
676 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
677 */
678 public NetworkInfo getNetworkInfo(Network network) {
679 try {
680 return mService.getNetworkInfoForNetwork(network);
681 } catch (RemoteException e) {
682 return null;
683 }
684 }
685
686 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800687 * Returns connection status information about all network
688 * types supported by the device.
689 *
690 * @return an array of {@link NetworkInfo} objects. Check each
691 * {@link NetworkInfo#getType} for which type each applies.
692 *
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700693 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800694 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
695 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800696 public NetworkInfo[] getAllNetworkInfo() {
697 try {
698 return mService.getAllNetworkInfo();
699 } catch (RemoteException e) {
700 return null;
701 }
702 }
703
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800704 /**
Lorenzo Colittib57edc52014-08-22 17:10:50 -0700705 * Returns the {@link Network} object currently serving a given type, or
706 * null if the given type is not connected.
707 *
708 * <p>This method requires the caller to hold the permission
709 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
710 *
711 * @hide
712 */
713 public Network getNetworkForType(int networkType) {
714 try {
715 return mService.getNetworkForType(networkType);
716 } catch (RemoteException e) {
717 return null;
718 }
719 }
720
721 /**
Robert Greenwalt73b6cbae2014-06-23 11:40:00 -0700722 * Returns an array of all {@link Network} currently tracked by the
723 * framework.
724 *
725 * @return an array of {@link Network} objects.
726 *
727 * <p>This method requires the caller to hold the permission
728 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
729 */
730 public Network[] getAllNetworks() {
731 try {
732 return mService.getAllNetworks();
733 } catch (RemoteException e) {
734 return null;
735 }
736 }
737
738 /**
Lorenzo Colitti403aa262014-11-28 11:21:30 +0900739 * Returns an array of of {@link NetworkCapabilities} objects, representing
740 * the Networks that applications run by the given user will use by default.
741 * @hide
742 */
743 public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) {
744 try {
745 return mService.getDefaultNetworkCapabilitiesForUser(userId);
746 } catch (RemoteException e) {
747 return null;
748 }
749 }
750
751 /**
Wink Saville948282b2013-08-29 08:55:16 -0700752 * Returns details about the Provisioning or currently active default data network. When
753 * connected, this network is the default route for outgoing connections.
754 * You should always check {@link NetworkInfo#isConnected()} before initiating
755 * network traffic. This may return {@code null} when there is no default
756 * network.
757 *
758 * @return a {@link NetworkInfo} object for the current default network
Paul Jensen0d719ca2015-02-13 14:18:39 -0500759 * or {@code null} if no default network is currently active
Wink Saville948282b2013-08-29 08:55:16 -0700760 *
Paul Jensen0d719ca2015-02-13 14:18:39 -0500761 * <p>This method requires the caller to hold the permission
Wink Saville948282b2013-08-29 08:55:16 -0700762 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
763 *
764 * {@hide}
765 */
766 public NetworkInfo getProvisioningOrActiveNetworkInfo() {
767 try {
768 return mService.getProvisioningOrActiveNetworkInfo();
769 } catch (RemoteException e) {
770 return null;
771 }
772 }
773
774 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800775 * Returns the IP information for the current default network.
776 *
777 * @return a {@link LinkProperties} object describing the IP info
778 * for the current default network, or {@code null} if there
779 * is no current default network.
780 *
Paul Jensen0d719ca2015-02-13 14:18:39 -0500781 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800782 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
783 * {@hide}
784 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700785 public LinkProperties getActiveLinkProperties() {
786 try {
787 return mService.getActiveLinkProperties();
788 } catch (RemoteException e) {
789 return null;
790 }
791 }
792
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800793 /**
794 * Returns the IP information for a given network type.
795 *
796 * @param networkType the network type of interest.
797 * @return a {@link LinkProperties} object describing the IP info
798 * for the given networkType, or {@code null} if there is
799 * no current default network.
800 *
Paul Jensen0d719ca2015-02-13 14:18:39 -0500801 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800802 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
803 * {@hide}
804 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700805 public LinkProperties getLinkProperties(int networkType) {
806 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -0700807 return mService.getLinkPropertiesForType(networkType);
808 } catch (RemoteException e) {
809 return null;
810 }
811 }
812
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700813 /**
814 * Get the {@link LinkProperties} for the given {@link Network}. This
815 * will return {@code null} if the network is unknown.
816 *
817 * @param network The {@link Network} object identifying the network in question.
818 * @return The {@link LinkProperties} for the network, or {@code null}.
819 **/
Robert Greenwalt9258c642014-03-26 16:47:06 -0700820 public LinkProperties getLinkProperties(Network network) {
821 try {
822 return mService.getLinkProperties(network);
823 } catch (RemoteException e) {
824 return null;
825 }
826 }
827
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700828 /**
829 * Get the {@link NetworkCapabilities} for the given {@link Network}. This
830 * will return {@code null} if the network is unknown.
831 *
832 * @param network The {@link Network} object identifying the network in question.
833 * @return The {@link NetworkCapabilities} for the network, or {@code null}.
834 */
Robert Greenwalt9258c642014-03-26 16:47:06 -0700835 public NetworkCapabilities getNetworkCapabilities(Network network) {
836 try {
837 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700838 } catch (RemoteException e) {
839 return null;
840 }
841 }
842
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800843 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800844 * Tells the underlying networking system that the caller wants to
845 * begin using the named feature. The interpretation of {@code feature}
846 * is completely up to each networking implementation.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700847 * <p>This method requires the caller to hold the permission
848 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800849 * @param networkType specifies which network the request pertains to
850 * @param feature the name of the feature to be used
851 * @return an integer value representing the outcome of the request.
852 * The interpretation of this value is specific to each networking
853 * implementation+feature combination, except that the value {@code -1}
854 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700855 *
856 * @deprecated Deprecated in favor of the cleaner {@link #requestNetwork} api.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800857 */
858 public int startUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700859 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
860 if (netCap == null) {
861 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
862 feature);
863 return PhoneConstants.APN_REQUEST_FAILED;
864 }
865
866 NetworkRequest request = null;
867 synchronized (sLegacyRequests) {
868 LegacyRequest l = sLegacyRequests.get(netCap);
869 if (l != null) {
870 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
871 renewRequestLocked(l);
872 if (l.currentNetwork != null) {
873 return PhoneConstants.APN_ALREADY_ACTIVE;
874 } else {
875 return PhoneConstants.APN_REQUEST_STARTED;
876 }
877 }
878
879 request = requestNetworkForFeatureLocked(netCap);
880 }
881 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -0700882 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -0700883 return PhoneConstants.APN_REQUEST_STARTED;
884 } else {
885 Log.d(TAG, " request Failed");
886 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800887 }
888 }
889
890 /**
891 * Tells the underlying networking system that the caller is finished
892 * using the named feature. The interpretation of {@code feature}
893 * is completely up to each networking implementation.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700894 * <p>This method requires the caller to hold the permission
895 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800896 * @param networkType specifies which network the request pertains to
897 * @param feature the name of the feature that is no longer needed
898 * @return an integer value representing the outcome of the request.
899 * The interpretation of this value is specific to each networking
900 * implementation+feature combination, except that the value {@code -1}
901 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700902 *
903 * @deprecated Deprecated in favor of the cleaner {@link #requestNetwork} api.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800904 */
905 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700906 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
907 if (netCap == null) {
908 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
909 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800910 return -1;
911 }
Robert Greenwalt562cc542014-05-15 18:07:26 -0700912
Paul Jensen9ffb53c2014-12-17 10:39:34 -0500913 if (removeRequestForFeature(netCap)) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700914 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt562cc542014-05-15 18:07:26 -0700915 }
916 return 1;
917 }
918
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900919 /**
920 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability from the given
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900921 * NetworkCapabilities object if all the capabilities it provides are
922 * typically provided by restricted networks.
923 *
924 * TODO: consider:
925 * - Moving to NetworkCapabilities
926 * - Renaming it to guessRestrictedCapability and make it set the
927 * restricted capability bit in addition to clearing it.
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900928 * @hide
929 */
930 public static void maybeMarkCapabilitiesRestricted(NetworkCapabilities nc) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700931 for (int capability : nc.getCapabilities()) {
932 switch (capability) {
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900933 case NetworkCapabilities.NET_CAPABILITY_CBS:
934 case NetworkCapabilities.NET_CAPABILITY_DUN:
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900935 case NetworkCapabilities.NET_CAPABILITY_EIMS:
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900936 case NetworkCapabilities.NET_CAPABILITY_FOTA:
937 case NetworkCapabilities.NET_CAPABILITY_IA:
938 case NetworkCapabilities.NET_CAPABILITY_IMS:
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900939 case NetworkCapabilities.NET_CAPABILITY_RCS:
940 case NetworkCapabilities.NET_CAPABILITY_XCAP:
Robert Greenwalt9ac3dbe2014-06-05 16:39:28 -0700941 case NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED: //there by default
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900942 continue;
943 default:
944 // At least one capability usually provided by unrestricted
945 // networks. Conclude that this network is unrestricted.
946 return;
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900947 }
948 }
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900949 // All the capabilities are typically provided by restricted networks.
950 // Conclude that this network is restricted.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700951 nc.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900952 }
953
Robert Greenwalt562cc542014-05-15 18:07:26 -0700954 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
955 if (networkType == TYPE_MOBILE) {
956 int cap = -1;
957 if ("enableMMS".equals(feature)) {
958 cap = NetworkCapabilities.NET_CAPABILITY_MMS;
959 } else if ("enableSUPL".equals(feature)) {
960 cap = NetworkCapabilities.NET_CAPABILITY_SUPL;
961 } else if ("enableDUN".equals(feature) || "enableDUNAlways".equals(feature)) {
962 cap = NetworkCapabilities.NET_CAPABILITY_DUN;
963 } else if ("enableHIPRI".equals(feature)) {
964 cap = NetworkCapabilities.NET_CAPABILITY_INTERNET;
965 } else if ("enableFOTA".equals(feature)) {
966 cap = NetworkCapabilities.NET_CAPABILITY_FOTA;
967 } else if ("enableIMS".equals(feature)) {
968 cap = NetworkCapabilities.NET_CAPABILITY_IMS;
969 } else if ("enableCBS".equals(feature)) {
970 cap = NetworkCapabilities.NET_CAPABILITY_CBS;
971 } else {
972 return null;
973 }
974 NetworkCapabilities netCap = new NetworkCapabilities();
Robert Greenwalt7569f182014-06-08 16:42:59 -0700975 netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(cap);
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900976 maybeMarkCapabilitiesRestricted(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -0700977 return netCap;
978 } else if (networkType == TYPE_WIFI) {
979 if ("p2p".equals(feature)) {
980 NetworkCapabilities netCap = new NetworkCapabilities();
981 netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700982 netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900983 maybeMarkCapabilitiesRestricted(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -0700984 return netCap;
985 }
986 }
987 return null;
988 }
989
Robert Greenwalt06314e42014-10-29 14:04:06 -0700990 /**
991 * Guess what the network request was trying to say so that the resulting
992 * network is accessible via the legacy (deprecated) API such as
993 * requestRouteToHost.
994 * This means we should try to be fairly preceise about transport and
995 * capability but ignore things such as networkSpecifier.
996 * If the request has more than one transport or capability it doesn't
997 * match the old legacy requests (they selected only single transport/capability)
998 * so this function cannot map the request to a single legacy type and
999 * the resulting network will not be available to the legacy APIs.
1000 *
1001 * TODO - This should be removed when the legacy APIs are removed.
1002 */
Ye Wenb87875e2014-07-21 14:19:01 -07001003 private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
1004 if (netCap == null) {
1005 return TYPE_NONE;
1006 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001007
Ye Wenb87875e2014-07-21 14:19:01 -07001008 if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) {
1009 return TYPE_NONE;
1010 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001011
1012 String type = null;
1013 int result = TYPE_NONE;
1014
Ye Wenb87875e2014-07-21 14:19:01 -07001015 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
Robert Greenwalt06314e42014-10-29 14:04:06 -07001016 type = "enableCBS";
1017 result = TYPE_MOBILE_CBS;
1018 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1019 type = "enableIMS";
1020 result = TYPE_MOBILE_IMS;
1021 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1022 type = "enableFOTA";
1023 result = TYPE_MOBILE_FOTA;
1024 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1025 type = "enableDUN";
1026 result = TYPE_MOBILE_DUN;
1027 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1028 type = "enableSUPL";
1029 result = TYPE_MOBILE_SUPL;
1030 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1031 type = "enableMMS";
1032 result = TYPE_MOBILE_MMS;
1033 } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1034 type = "enableHIPRI";
1035 result = TYPE_MOBILE_HIPRI;
Ye Wenb87875e2014-07-21 14:19:01 -07001036 }
Robert Greenwalt06314e42014-10-29 14:04:06 -07001037 if (type != null) {
1038 NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type);
1039 if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) {
1040 return result;
Ye Wenb87875e2014-07-21 14:19:01 -07001041 }
1042 }
1043 return TYPE_NONE;
1044 }
1045
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001046 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001047 if (netCap == null) return TYPE_NONE;
1048 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
1049 return TYPE_MOBILE_CBS;
1050 }
1051 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
1052 return TYPE_MOBILE_IMS;
1053 }
1054 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
1055 return TYPE_MOBILE_FOTA;
1056 }
1057 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
1058 return TYPE_MOBILE_DUN;
1059 }
1060 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
1061 return TYPE_MOBILE_SUPL;
1062 }
1063 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
1064 return TYPE_MOBILE_MMS;
1065 }
1066 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
1067 return TYPE_MOBILE_HIPRI;
1068 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001069 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
1070 return TYPE_WIFI_P2P;
1071 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07001072 return TYPE_NONE;
1073 }
1074
1075 private static class LegacyRequest {
1076 NetworkCapabilities networkCapabilities;
1077 NetworkRequest networkRequest;
1078 int expireSequenceNumber;
1079 Network currentNetwork;
1080 int delay = -1;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001081
1082 private void clearDnsBinding() {
1083 if (currentNetwork != null) {
1084 currentNetwork = null;
1085 setProcessDefaultNetworkForHostResolution(null);
1086 }
1087 }
1088
Robert Greenwalt6078b502014-06-11 16:05:07 -07001089 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001090 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001091 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001092 currentNetwork = network;
1093 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001094 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001095 }
1096 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001097 public void onLost(Network network) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001098 if (network.equals(currentNetwork)) clearDnsBinding();
Robert Greenwalt562cc542014-05-15 18:07:26 -07001099 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1100 }
1101 };
1102 }
1103
Robert Greenwaltfab501672014-07-23 11:44:01 -07001104 private static HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
Robert Greenwalt562cc542014-05-15 18:07:26 -07001105 new HashMap<NetworkCapabilities, LegacyRequest>();
1106
1107 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1108 synchronized (sLegacyRequests) {
1109 LegacyRequest l = sLegacyRequests.get(netCap);
1110 if (l != null) return l.networkRequest;
1111 }
1112 return null;
1113 }
1114
1115 private void renewRequestLocked(LegacyRequest l) {
1116 l.expireSequenceNumber++;
1117 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1118 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1119 }
1120
1121 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1122 int ourSeqNum = -1;
1123 synchronized (sLegacyRequests) {
1124 LegacyRequest l = sLegacyRequests.get(netCap);
1125 if (l == null) return;
1126 ourSeqNum = l.expireSequenceNumber;
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001127 if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001128 }
1129 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1130 }
1131
1132 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1133 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001134 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001135 try {
1136 delay = mService.getRestoreDefaultNetworkDelay(type);
1137 } catch (RemoteException e) {}
1138 LegacyRequest l = new LegacyRequest();
1139 l.networkCapabilities = netCap;
1140 l.delay = delay;
1141 l.expireSequenceNumber = 0;
Robert Greenwalt6078b502014-06-11 16:05:07 -07001142 l.networkRequest = sendRequestForNetwork(netCap, l.networkCallback, 0,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001143 REQUEST, type);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001144 if (l.networkRequest == null) return null;
1145 sLegacyRequests.put(netCap, l);
1146 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1147 return l.networkRequest;
1148 }
1149
1150 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1151 if (delay >= 0) {
1152 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
1153 Message msg = sCallbackHandler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1154 sCallbackHandler.sendMessageDelayed(msg, delay);
1155 }
1156 }
1157
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001158 private boolean removeRequestForFeature(NetworkCapabilities netCap) {
1159 final LegacyRequest l;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001160 synchronized (sLegacyRequests) {
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001161 l = sLegacyRequests.remove(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001162 }
Paul Jensen9ffb53c2014-12-17 10:39:34 -05001163 if (l == null) return false;
1164 unregisterNetworkCallback(l.networkCallback);
1165 l.clearDnsBinding();
1166 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001167 }
1168
1169 /**
1170 * Ensure that a network route exists to deliver traffic to the specified
1171 * host via the specified network interface. An attempt to add a route that
1172 * already exists is ignored, but treated as successful.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001173 * <p>This method requires the caller to hold the permission
1174 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001175 * @param networkType the type of the network over which traffic to the specified
1176 * host is to be routed
1177 * @param hostAddress the IP address of the host to which the route is desired
1178 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001179 *
1180 * @deprecated Deprecated in favor of the {@link #requestNetwork},
Paul Jensen72db88e2015-03-10 10:54:12 -04001181 * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} api.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001182 */
1183 public boolean requestRouteToHost(int networkType, int hostAddress) {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001184 return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress));
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001185 }
1186
1187 /**
1188 * Ensure that a network route exists to deliver traffic to the specified
1189 * host via the specified network interface. An attempt to add a route that
1190 * already exists is ignored, but treated as successful.
Jake Hamby8f9b33e2014-01-15 13:08:03 -08001191 * <p>This method requires the caller to hold the permission
1192 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001193 * @param networkType the type of the network over which traffic to the specified
1194 * host is to be routed
1195 * @param hostAddress the IP address of the host to which the route is desired
1196 * @return {@code true} on success, {@code false} on failure
1197 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001198 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Paul Jensen72db88e2015-03-10 10:54:12 -04001199 * {@link #bindProcessToNetwork} api.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001200 */
1201 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001202 try {
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001203 return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress());
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001204 } catch (RemoteException e) {
1205 return false;
1206 }
1207 }
1208
1209 /**
1210 * Returns the value of the setting for background data usage. If false,
1211 * applications should not use the network if the application is not in the
1212 * foreground. Developers should respect this setting, and check the value
1213 * of this before performing any background data operations.
1214 * <p>
1215 * All applications that have background services that use the network
1216 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001217 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001218 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001219 * background data depends on several combined factors, and this method will
1220 * always return {@code true}. Instead, when background data is unavailable,
1221 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001222 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001223 * @return Whether background data usage is allowed.
1224 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001225 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001226 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001227 // assume that background data is allowed; final authority is
1228 // NetworkInfo which may be blocked.
1229 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001230 }
1231
1232 /**
1233 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001234 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001235 * @param allowBackgroundData Whether an application should use data while
1236 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001237 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001238 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1239 * @see #getBackgroundDataSetting()
1240 * @hide
1241 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001242 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001243 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001244 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001245 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001246
1247 /**
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001248 * Return quota status for the current active network, or {@code null} if no
1249 * network is active. Quota status can change rapidly, so these values
1250 * shouldn't be cached.
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001251 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001252 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001253 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1254 *
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001255 * @hide
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001256 */
1257 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1258 try {
1259 return mService.getActiveNetworkQuotaInfo();
1260 } catch (RemoteException e) {
1261 return null;
1262 }
1263 }
1264
1265 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001266 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001267 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001268 */
1269 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001270 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1271 if (b != null) {
1272 try {
1273 ITelephony it = ITelephony.Stub.asInterface(b);
Wink Saville36ffb042014-12-05 11:10:30 -08001274 int subId = SubscriptionManager.getDefaultDataSubId();
1275 Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId);
1276 boolean retVal = it.getDataEnabled(subId);
1277 Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId
1278 + " retVal=" + retVal);
1279 return retVal;
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001280 } catch (RemoteException e) { }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001281 }
Wink Saville36ffb042014-12-05 11:10:30 -08001282 Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false");
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001283 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001284 }
1285
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001286 /**
Robert Greenwaltb2489872014-09-04 16:44:35 -07001287 * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener}
Robert Greenwalt6078b502014-06-11 16:05:07 -07001288 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001289 */
1290 public interface OnNetworkActiveListener {
1291 /**
1292 * Called on the main thread of the process to report that the current data network
1293 * has become active, and it is now a good time to perform any pending network
1294 * operations. Note that this listener only tells you when the network becomes
1295 * active; if at any other time you want to know whether it is active (and thus okay
1296 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001297 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001298 */
1299 public void onNetworkActive();
1300 }
1301
1302 private INetworkManagementService getNetworkManagementService() {
1303 synchronized (this) {
1304 if (mNMService != null) {
1305 return mNMService;
1306 }
1307 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1308 mNMService = INetworkManagementService.Stub.asInterface(b);
1309 return mNMService;
1310 }
1311 }
1312
1313 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
1314 mNetworkActivityListeners
1315 = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>();
1316
1317 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001318 * Start listening to reports when the system's default data network is active, meaning it is
1319 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1320 * to determine the current state of the system's default network after registering the
1321 * listener.
1322 * <p>
1323 * If the process default network has been set with
Paul Jensen72db88e2015-03-10 10:54:12 -04001324 * {@link ConnectivityManager#bindProcessToNetwork} this function will not
Robert Greenwalt6078b502014-06-11 16:05:07 -07001325 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001326 *
1327 * @param l The listener to be told when the network is active.
1328 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001329 public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001330 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1331 @Override
1332 public void onNetworkActive() throws RemoteException {
1333 l.onNetworkActive();
1334 }
1335 };
1336
1337 try {
1338 getNetworkManagementService().registerNetworkActivityListener(rl);
1339 mNetworkActivityListeners.put(l, rl);
1340 } catch (RemoteException e) {
1341 }
1342 }
1343
1344 /**
1345 * Remove network active listener previously registered with
Robert Greenwaltb2489872014-09-04 16:44:35 -07001346 * {@link #addDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001347 *
1348 * @param l Previously registered listener.
1349 */
Robert Greenwaltb2489872014-09-04 16:44:35 -07001350 public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001351 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
1352 if (rl == null) {
1353 throw new IllegalArgumentException("Listener not registered: " + l);
1354 }
1355 try {
1356 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1357 } catch (RemoteException e) {
1358 }
1359 }
1360
1361 /**
1362 * Return whether the data network is currently active. An active network means that
1363 * it is currently in a high power state for performing data transmission. On some
1364 * types of networks, it may be expensive to move and stay in such a state, so it is
1365 * more power efficient to batch network traffic together when the radio is already in
1366 * this state. This method tells you whether right now is currently a good time to
1367 * initiate network traffic, as the network is already active.
1368 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001369 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001370 try {
1371 return getNetworkManagementService().isNetworkActive();
1372 } catch (RemoteException e) {
1373 }
1374 return false;
1375 }
1376
1377 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001378 * {@hide}
1379 */
Sreeram Ramachandran03666c72014-07-19 23:21:46 -07001380 public ConnectivityManager(IConnectivityManager service) {
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001381 mService = checkNotNull(service, "missing IConnectivityManager");
Paul Jensene0bef712014-12-10 15:12:18 -05001382 sInstance = this;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001383 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001384
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07001385 /** {@hide} */
1386 public static ConnectivityManager from(Context context) {
1387 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1388 }
1389
Robert Greenwaltedb47662014-09-16 17:54:19 -07001390 /** {@hide */
1391 public static final void enforceTetherChangePermission(Context context) {
1392 if (context.getResources().getStringArray(
1393 com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) {
1394 // Have a provisioning app - must only let system apps (which check this app)
1395 // turn on tethering
1396 context.enforceCallingOrSelfPermission(
1397 android.Manifest.permission.CONNECTIVITY_INTERNAL, "ConnectivityService");
1398 } else {
1399 context.enforceCallingOrSelfPermission(
1400 android.Manifest.permission.CHANGE_NETWORK_STATE, "ConnectivityService");
1401 }
1402 }
1403
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001404 /**
Paul Jensene0bef712014-12-10 15:12:18 -05001405 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1406 * situations where a Context pointer is unavailable.
1407 * @hide
1408 */
Paul Jensen72db88e2015-03-10 10:54:12 -04001409 static ConnectivityManager getInstanceOrNull() {
1410 return sInstance;
1411 }
1412
1413 /**
1414 * @deprecated - use getSystemService. This is a kludge to support static access in certain
1415 * situations where a Context pointer is unavailable.
1416 * @hide
1417 */
1418 private static ConnectivityManager getInstance() {
1419 if (getInstanceOrNull() == null) {
Paul Jensene0bef712014-12-10 15:12:18 -05001420 throw new IllegalStateException("No ConnectivityManager yet constructed");
1421 }
Paul Jensen72db88e2015-03-10 10:54:12 -04001422 return getInstanceOrNull();
Paul Jensene0bef712014-12-10 15:12:18 -05001423 }
1424
1425 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001426 * Get the set of tetherable, available interfaces. This list is limited by
1427 * device configuration and current interface existence.
1428 *
1429 * @return an array of 0 or more Strings of tetherable interface names.
1430 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001431 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001432 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001433 * {@hide}
1434 */
1435 public String[] getTetherableIfaces() {
1436 try {
1437 return mService.getTetherableIfaces();
1438 } catch (RemoteException e) {
1439 return new String[0];
1440 }
1441 }
1442
1443 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001444 * Get the set of tethered interfaces.
1445 *
1446 * @return an array of 0 or more String of currently tethered interface names.
1447 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001448 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001449 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001450 * {@hide}
1451 */
1452 public String[] getTetheredIfaces() {
1453 try {
1454 return mService.getTetheredIfaces();
1455 } catch (RemoteException e) {
1456 return new String[0];
1457 }
1458 }
1459
1460 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001461 * Get the set of interface names which attempted to tether but
1462 * failed. Re-attempting to tether may cause them to reset to the Tethered
1463 * state. Alternatively, causing the interface to be destroyed and recreated
1464 * may cause them to reset to the available state.
1465 * {@link ConnectivityManager#getLastTetherError} can be used to get more
1466 * information on the cause of the errors.
1467 *
1468 * @return an array of 0 or more String indicating the interface names
1469 * which failed to tether.
1470 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001471 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001472 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001473 * {@hide}
1474 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001475 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001476 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001477 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001478 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001479 return new String[0];
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001480 }
1481 }
1482
1483 /**
Robert Greenwalt9c7e2c22014-06-23 14:53:42 -07001484 * Get the set of tethered dhcp ranges.
1485 *
1486 * @return an array of 0 or more {@code String} of tethered dhcp ranges.
1487 * {@hide}
1488 */
1489 public String[] getTetheredDhcpRanges() {
1490 try {
1491 return mService.getTetheredDhcpRanges();
1492 } catch (RemoteException e) {
1493 return new String[0];
1494 }
1495 }
1496
1497 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001498 * Attempt to tether the named interface. This will setup a dhcp server
1499 * on the interface, forward and NAT IP packets and forward DNS requests
1500 * to the best active upstream network interface. Note that if no upstream
1501 * IP network interface is available, dhcp will still run and traffic will be
1502 * allowed between the tethered devices and this device, though upstream net
1503 * access will of course fail until an upstream network interface becomes
1504 * active.
1505 *
1506 * @param iface the interface name to tether.
1507 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1508 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001509 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001510 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001511 * {@hide}
1512 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001513 public int tether(String iface) {
1514 try {
1515 return mService.tether(iface);
1516 } catch (RemoteException e) {
1517 return TETHER_ERROR_SERVICE_UNAVAIL;
1518 }
1519 }
1520
1521 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001522 * Stop tethering the named interface.
1523 *
1524 * @param iface the interface name to untether.
1525 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1526 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001527 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001528 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt5a735062010-03-02 17:25:02 -08001529 * {@hide}
1530 */
1531 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001532 try {
1533 return mService.untether(iface);
1534 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001535 return TETHER_ERROR_SERVICE_UNAVAIL;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001536 }
1537 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001538
1539 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001540 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001541 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001542 * due to device configuration.
1543 *
1544 * @return a boolean - {@code true} indicating Tethering is supported.
1545 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001546 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001547 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001548 * {@hide}
1549 */
1550 public boolean isTetheringSupported() {
1551 try {
1552 return mService.isTetheringSupported();
1553 } catch (RemoteException e) {
1554 return false;
1555 }
1556 }
1557
1558 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001559 * Get the list of regular expressions that define any tetherable
1560 * USB network interfaces. If USB tethering is not supported by the
1561 * device, this list should be empty.
1562 *
1563 * @return an array of 0 or more regular expression Strings defining
1564 * what interfaces are considered tetherable usb interfaces.
1565 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001566 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001567 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001568 * {@hide}
1569 */
1570 public String[] getTetherableUsbRegexs() {
1571 try {
1572 return mService.getTetherableUsbRegexs();
1573 } catch (RemoteException e) {
1574 return new String[0];
1575 }
1576 }
1577
1578 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001579 * Get the list of regular expressions that define any tetherable
1580 * Wifi network interfaces. If Wifi tethering is not supported by the
1581 * device, this list should be empty.
1582 *
1583 * @return an array of 0 or more regular expression Strings defining
1584 * what interfaces are considered tetherable wifi interfaces.
1585 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001586 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001587 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001588 * {@hide}
1589 */
1590 public String[] getTetherableWifiRegexs() {
1591 try {
1592 return mService.getTetherableWifiRegexs();
1593 } catch (RemoteException e) {
1594 return new String[0];
1595 }
1596 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08001597
Danica Chang6fdd0c62010-08-11 14:54:43 -07001598 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001599 * Get the list of regular expressions that define any tetherable
1600 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
1601 * device, this list should be empty.
1602 *
1603 * @return an array of 0 or more regular expression Strings defining
1604 * what interfaces are considered tetherable bluetooth interfaces.
1605 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001606 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001607 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001608 * {@hide}
1609 */
1610 public String[] getTetherableBluetoothRegexs() {
1611 try {
1612 return mService.getTetherableBluetoothRegexs();
1613 } catch (RemoteException e) {
1614 return new String[0];
1615 }
1616 }
1617
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001618 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001619 * Attempt to both alter the mode of USB and Tethering of USB. A
1620 * utility method to deal with some of the complexity of USB - will
1621 * attempt to switch to Rndis and subsequently tether the resulting
1622 * interface on {@code true} or turn off tethering and switch off
1623 * Rndis on {@code false}.
1624 *
1625 * @param enable a boolean - {@code true} to enable tethering
1626 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1627 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001628 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001629 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001630 * {@hide}
1631 */
1632 public int setUsbTethering(boolean enable) {
1633 try {
1634 return mService.setUsbTethering(enable);
1635 } catch (RemoteException e) {
1636 return TETHER_ERROR_SERVICE_UNAVAIL;
1637 }
1638 }
1639
Robert Greenwalt5a735062010-03-02 17:25:02 -08001640 /** {@hide} */
1641 public static final int TETHER_ERROR_NO_ERROR = 0;
1642 /** {@hide} */
1643 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
1644 /** {@hide} */
1645 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
1646 /** {@hide} */
1647 public static final int TETHER_ERROR_UNSUPPORTED = 3;
1648 /** {@hide} */
1649 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
1650 /** {@hide} */
1651 public static final int TETHER_ERROR_MASTER_ERROR = 5;
1652 /** {@hide} */
1653 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
1654 /** {@hide} */
1655 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
1656 /** {@hide} */
1657 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
1658 /** {@hide} */
1659 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
1660 /** {@hide} */
1661 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
1662
1663 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001664 * Get a more detailed error code after a Tethering or Untethering
1665 * request asynchronously failed.
1666 *
1667 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08001668 * @return error The error code of the last error tethering or untethering the named
1669 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001670 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001671 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001672 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a735062010-03-02 17:25:02 -08001673 * {@hide}
1674 */
1675 public int getLastTetherError(String iface) {
1676 try {
1677 return mService.getLastTetherError(iface);
1678 } catch (RemoteException e) {
1679 return TETHER_ERROR_SERVICE_UNAVAIL;
1680 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001681 }
1682
1683 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001684 * Report network connectivity status. This is currently used only
1685 * to alter status bar UI.
1686 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001687 * @param networkType The type of network you want to report on
1688 * @param percentage The quality of the connection 0 is bad, 100 is good
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001689 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001690 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001691 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001692 * {@hide}
1693 */
1694 public void reportInetCondition(int networkType, int percentage) {
1695 try {
1696 mService.reportInetCondition(networkType, percentage);
1697 } catch (RemoteException e) {
1698 }
1699 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001700
1701 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001702 * Report a problem network to the framework. This provides a hint to the system
Ye Wenb87875e2014-07-21 14:19:01 -07001703 * that there might be connectivity problems on this network and may cause
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001704 * the framework to re-evaluate network connectivity and/or switch to another
1705 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001706 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001707 * @param network The {@link Network} the application was attempting to use
1708 * or {@code null} to indicate the current default network.
Paul Jensenbfd17b72015-04-07 12:43:13 -04001709 * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both
1710 * working and non-working connectivity.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001711 */
1712 public void reportBadNetwork(Network network) {
1713 try {
Paul Jensenbfd17b72015-04-07 12:43:13 -04001714 // One of these will be ignored because it matches system's current state.
1715 // The other will trigger the necessary reevaluation.
1716 mService.reportNetworkConnectivity(network, true);
1717 mService.reportNetworkConnectivity(network, false);
1718 } catch (RemoteException e) {
1719 }
1720 }
1721
1722 /**
1723 * Report to the framework whether a network has working connectivity.
1724 * This provides a hint to the system that a particular network is providing
1725 * working connectivity or not. In response the framework may re-evaluate
1726 * the network's connectivity and might take further action thereafter.
1727 *
1728 * @param network The {@link Network} the application was attempting to use
1729 * or {@code null} to indicate the current default network.
1730 * @param hasConnectivity {@code true} if the application was able to successfully access the
1731 * Internet using {@code network} or {@code false} if not.
1732 */
1733 public void reportNetworkConnectivity(Network network, boolean hasConnectivity) {
1734 try {
1735 mService.reportNetworkConnectivity(network, hasConnectivity);
Robert Greenwalt9258c642014-03-26 16:47:06 -07001736 } catch (RemoteException e) {
1737 }
1738 }
1739
1740 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001741 * Set a network-independent global http proxy. This is not normally what you want
1742 * for typical HTTP proxies - they are general network dependent. However if you're
1743 * doing something unusual like general internal filtering this may be useful. On
1744 * a private network where the proxy is not accessible, you may break HTTP using this.
1745 *
Jason Monk207900c2014-04-25 15:00:09 -04001746 * @param p The a {@link ProxyInfo} object defining the new global
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001747 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
1748 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001749 * <p>This method requires the caller to hold the permission
Jason Monkfaf3fd52014-05-07 18:41:13 -04001750 * android.Manifest.permission#CONNECTIVITY_INTERNAL.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001751 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07001752 */
Jason Monk207900c2014-04-25 15:00:09 -04001753 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001754 try {
1755 mService.setGlobalProxy(p);
1756 } catch (RemoteException e) {
1757 }
1758 }
1759
1760 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001761 * Retrieve any network-independent global HTTP proxy.
1762 *
Jason Monk207900c2014-04-25 15:00:09 -04001763 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001764 * if no global HTTP proxy is set.
1765 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001766 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001767 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001768 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07001769 */
Jason Monk207900c2014-04-25 15:00:09 -04001770 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001771 try {
1772 return mService.getGlobalProxy();
1773 } catch (RemoteException e) {
1774 return null;
1775 }
1776 }
1777
1778 /**
Paul Jensene0bef712014-12-10 15:12:18 -05001779 * Get the current default HTTP proxy settings. If a global proxy is set it will be returned,
1780 * otherwise if this process is bound to a {@link Network} using
Paul Jensen72db88e2015-03-10 10:54:12 -04001781 * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise
Paul Jensene0bef712014-12-10 15:12:18 -05001782 * the default network's proxy is returned.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001783 *
Jason Monk207900c2014-04-25 15:00:09 -04001784 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001785 * HTTP proxy is active.
Robert Greenwalt434203a2010-10-11 16:00:27 -07001786 */
Paul Jensene0bef712014-12-10 15:12:18 -05001787 public ProxyInfo getDefaultProxy() {
Paul Jensen72db88e2015-03-10 10:54:12 -04001788 final Network network = getBoundNetworkForProcess();
Paul Jensene0bef712014-12-10 15:12:18 -05001789 if (network != null) {
1790 final ProxyInfo globalProxy = getGlobalProxy();
1791 if (globalProxy != null) return globalProxy;
1792 final LinkProperties lp = getLinkProperties(network);
1793 if (lp != null) return lp.getHttpProxy();
1794 return null;
1795 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001796 try {
Paul Jensene0bef712014-12-10 15:12:18 -05001797 return mService.getDefaultProxy();
Robert Greenwalt434203a2010-10-11 16:00:27 -07001798 } catch (RemoteException e) {
1799 return null;
1800 }
1801 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001802
1803 /**
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07001804 * Returns true if the hardware supports the given network type
1805 * else it returns false. This doesn't indicate we have coverage
1806 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001807 * hardware supports it. For example a GSM phone without a SIM
1808 * should still return {@code true} for mobile data, but a wifi only
1809 * tablet would return {@code false}.
1810 *
1811 * @param networkType The network type we'd like to check
1812 * @return {@code true} if supported, else {@code false}
1813 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001814 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001815 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07001816 * @hide
1817 */
1818 public boolean isNetworkSupported(int networkType) {
1819 try {
1820 return mService.isNetworkSupported(networkType);
1821 } catch (RemoteException e) {}
1822 return false;
1823 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07001824
1825 /**
1826 * Returns if the currently active data network is metered. A network is
1827 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001828 * that connection due to monetary costs, data limitations or
1829 * battery/performance issues. You should check this before doing large
1830 * data transfers, and warn the user or delay the operation until another
1831 * network is available.
1832 *
1833 * @return {@code true} if large transfers should be avoided, otherwise
1834 * {@code false}.
1835 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001836 * <p>This method requires the caller to hold the permission
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001837 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07001838 */
1839 public boolean isActiveNetworkMetered() {
1840 try {
1841 return mService.isActiveNetworkMetered();
1842 } catch (RemoteException e) {
1843 return false;
1844 }
1845 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07001846
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001847 /**
1848 * If the LockdownVpn mechanism is enabled, updates the vpn
1849 * with a reload of its profile.
1850 *
1851 * @return a boolean with {@code} indicating success
1852 *
1853 * <p>This method can only be called by the system UID
1854 * {@hide}
1855 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07001856 public boolean updateLockdownVpn() {
1857 try {
1858 return mService.updateLockdownVpn();
1859 } catch (RemoteException e) {
1860 return false;
1861 }
1862 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07001863
1864 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001865 * Signal that the captive portal check on the indicated network
Wink Savilled747cbc2013-08-07 16:22:47 -07001866 * is complete and whether its a captive portal or not.
1867 *
1868 * @param info the {@link NetworkInfo} object for the networkType
1869 * in question.
1870 * @param isCaptivePortal true/false.
1871 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001872 * <p>This method requires the caller to hold the permission
Wink Savilled747cbc2013-08-07 16:22:47 -07001873 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
1874 * {@hide}
1875 */
1876 public void captivePortalCheckCompleted(NetworkInfo info, boolean isCaptivePortal) {
1877 try {
1878 mService.captivePortalCheckCompleted(info, isCaptivePortal);
1879 } catch (RemoteException e) {
1880 }
1881 }
1882
1883 /**
Wink Saville948282b2013-08-29 08:55:16 -07001884 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07001885 *
Wink Savilleab9321d2013-06-29 21:10:57 -07001886 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07001887 *
1888 * @return time out that will be used, maybe less that suggestedTimeOutMs
1889 * -1 if an error.
1890 *
1891 * {@hide}
1892 */
Wink Saville948282b2013-08-29 08:55:16 -07001893 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07001894 int timeOutMs = -1;
1895 try {
Wink Saville948282b2013-08-29 08:55:16 -07001896 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07001897 } catch (RemoteException e) {
1898 }
1899 return timeOutMs;
1900 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07001901
1902 /**
Wink Saville42d4f082013-07-20 20:31:59 -07001903 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07001904 * {@hide}
1905 */
1906 public String getMobileProvisioningUrl() {
1907 try {
1908 return mService.getMobileProvisioningUrl();
1909 } catch (RemoteException e) {
1910 }
1911 return null;
1912 }
Wink Saville42d4f082013-07-20 20:31:59 -07001913
1914 /**
1915 * Get the mobile redirected provisioning url.
1916 * {@hide}
1917 */
1918 public String getMobileRedirectedProvisioningUrl() {
1919 try {
1920 return mService.getMobileRedirectedProvisioningUrl();
1921 } catch (RemoteException e) {
1922 }
1923 return null;
1924 }
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001925
1926 /**
Wink Saville948282b2013-08-29 08:55:16 -07001927 * Set sign in error notification to visible or in visible
1928 *
1929 * @param visible
1930 * @param networkType
1931 *
1932 * {@hide}
1933 */
1934 public void setProvisioningNotificationVisible(boolean visible, int networkType,
Paul Jensen89e0f092014-09-15 15:59:36 -04001935 String action) {
Wink Saville948282b2013-08-29 08:55:16 -07001936 try {
Paul Jensen89e0f092014-09-15 15:59:36 -04001937 mService.setProvisioningNotificationVisible(visible, networkType, action);
Wink Saville948282b2013-08-29 08:55:16 -07001938 } catch (RemoteException e) {
1939 }
1940 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07001941
1942 /**
1943 * Set the value for enabling/disabling airplane mode
1944 *
1945 * @param enable whether to enable airplane mode or not
1946 *
Paul Jensen0d719ca2015-02-13 14:18:39 -05001947 * <p>This method requires the caller to hold the permission
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07001948 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
1949 * @hide
1950 */
1951 public void setAirplaneMode(boolean enable) {
1952 try {
1953 mService.setAirplaneMode(enable);
1954 } catch (RemoteException e) {
1955 }
1956 }
Robert Greenwalte049c232014-04-11 15:53:27 -07001957
1958 /** {@hide} */
Robert Greenwalta67be032014-05-16 15:49:14 -07001959 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07001960 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07001961 mService.registerNetworkFactory(messenger, name);
1962 } catch (RemoteException e) { }
1963 }
1964
1965 /** {@hide} */
1966 public void unregisterNetworkFactory(Messenger messenger) {
1967 try {
1968 mService.unregisterNetworkFactory(messenger);
Robert Greenwalte049c232014-04-11 15:53:27 -07001969 } catch (RemoteException e) { }
1970 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07001971
Paul Jensen31a94f42015-02-13 14:18:39 -05001972 /**
1973 * @hide
1974 * Register a NetworkAgent with ConnectivityService.
1975 * @return NetID corresponding to NetworkAgent.
1976 */
1977 public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
Sreeram Ramachandran8cd33ed2014-07-23 15:23:15 -07001978 NetworkCapabilities nc, int score, NetworkMisc misc) {
Robert Greenwalt7b816022014-04-18 15:25:25 -07001979 try {
Paul Jensen31a94f42015-02-13 14:18:39 -05001980 return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc);
1981 } catch (RemoteException e) {
1982 return NETID_UNSET;
1983 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07001984 }
1985
Robert Greenwalt9258c642014-03-26 16:47:06 -07001986 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001987 * Base class for NetworkRequest callbacks. Used for notifications about network
1988 * changes. Should be extended by applications wanting notifications.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001989 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001990 public static class NetworkCallback {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001991 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001992 public static final int PRECHECK = 1;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001993 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001994 public static final int AVAILABLE = 2;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001995 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001996 public static final int LOSING = 3;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001997 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001998 public static final int LOST = 4;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001999 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002000 public static final int UNAVAIL = 5;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002001 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002002 public static final int CAP_CHANGED = 6;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002003 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002004 public static final int PROP_CHANGED = 7;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002005 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07002006 public static final int CANCELED = 8;
2007
2008 /**
2009 * @hide
2010 * Called whenever the framework connects to a network that it may use to
2011 * satisfy this request
2012 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002013 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002014
2015 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002016 * Called when the framework connects and has declared new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002017 * This callback may be called more than once if the {@link Network} that is
2018 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002019 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002020 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002021 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002022 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002023
2024 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002025 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07002026 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002027 * for graceful handover. This may not be called if we have a hard loss
2028 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07002029 * {@link NetworkCallback#onLost} call or a
2030 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002031 * on whether we lose or regain it.
2032 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002033 * @param network The {@link Network} that is about to be disconnected.
2034 * @param maxMsToLive The time in ms the framework will attempt to keep the
2035 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002036 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002037 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002038 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002039
2040 /**
2041 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002042 * graceful failure ends.
2043 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002044 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002045 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002046 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002047
2048 /**
2049 * Called if no network is found in the given timeout time. If no timeout is given,
2050 * this will not be called.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002051 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07002052 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002053 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002054
2055 /**
2056 * Called when the network the framework connected to for this request
2057 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002058 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002059 * @param network The {@link Network} whose capabilities have changed.
2060 * @param networkCapabilities The new {@link NetworkCapabilities} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002061 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002062 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07002063 NetworkCapabilities networkCapabilities) {}
2064
2065 /**
2066 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002067 * changes {@link LinkProperties}.
2068 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002069 * @param network The {@link Network} whose link properties have changed.
2070 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07002071 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002072 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07002073
Robert Greenwalt6078b502014-06-11 16:05:07 -07002074 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07002075 }
2076
Robert Greenwalt9258c642014-03-26 16:47:06 -07002077 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
2078 /** @hide obj = pair(NetworkRequest, Network) */
2079 public static final int CALLBACK_PRECHECK = BASE + 1;
2080 /** @hide obj = pair(NetworkRequest, Network) */
2081 public static final int CALLBACK_AVAILABLE = BASE + 2;
2082 /** @hide obj = pair(NetworkRequest, Network), arg1 = ttl */
2083 public static final int CALLBACK_LOSING = BASE + 3;
2084 /** @hide obj = pair(NetworkRequest, Network) */
2085 public static final int CALLBACK_LOST = BASE + 4;
2086 /** @hide obj = NetworkRequest */
2087 public static final int CALLBACK_UNAVAIL = BASE + 5;
2088 /** @hide obj = pair(NetworkRequest, Network) */
2089 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2090 /** @hide obj = pair(NetworkRequest, Network) */
2091 public static final int CALLBACK_IP_CHANGED = BASE + 7;
2092 /** @hide obj = NetworkRequest */
2093 public static final int CALLBACK_RELEASED = BASE + 8;
2094 /** @hide */
2095 public static final int CALLBACK_EXIT = BASE + 9;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002096 /** @hide obj = NetworkCapabilities, arg1 = seq number */
2097 private static final int EXPIRE_LEGACY_REQUEST = BASE + 10;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002098
Robert Greenwalt562cc542014-05-15 18:07:26 -07002099 private class CallbackHandler extends Handler {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002100 private final HashMap<NetworkRequest, NetworkCallback>mCallbackMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002101 private final AtomicInteger mRefCount;
2102 private static final String TAG = "ConnectivityManager.CallbackHandler";
2103 private final ConnectivityManager mCm;
2104
Robert Greenwalt6078b502014-06-11 16:05:07 -07002105 CallbackHandler(Looper looper, HashMap<NetworkRequest, NetworkCallback>callbackMap,
Robert Greenwalt9258c642014-03-26 16:47:06 -07002106 AtomicInteger refCount, ConnectivityManager cm) {
2107 super(looper);
2108 mCallbackMap = callbackMap;
2109 mRefCount = refCount;
2110 mCm = cm;
2111 }
2112
2113 @Override
2114 public void handleMessage(Message message) {
2115 Log.d(TAG, "CM callback handler got msg " + message.what);
2116 switch (message.what) {
2117 case CALLBACK_PRECHECK: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002118 NetworkRequest request = (NetworkRequest)getObject(message,
2119 NetworkRequest.class);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002120 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002121 if (callbacks != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002122 callbacks.onPreCheck((Network)getObject(message, Network.class));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002123 } else {
2124 Log.e(TAG, "callback not found for PRECHECK message");
2125 }
2126 break;
2127 }
2128 case CALLBACK_AVAILABLE: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002129 NetworkRequest request = (NetworkRequest)getObject(message,
2130 NetworkRequest.class);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002131 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002132 if (callbacks != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002133 callbacks.onAvailable((Network)getObject(message, Network.class));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002134 } else {
2135 Log.e(TAG, "callback not found for AVAILABLE message");
2136 }
2137 break;
2138 }
2139 case CALLBACK_LOSING: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002140 NetworkRequest request = (NetworkRequest)getObject(message,
2141 NetworkRequest.class);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002142 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002143 if (callbacks != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002144 callbacks.onLosing((Network)getObject(message, Network.class),
2145 message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002146 } else {
2147 Log.e(TAG, "callback not found for LOSING message");
2148 }
2149 break;
2150 }
2151 case CALLBACK_LOST: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002152 NetworkRequest request = (NetworkRequest)getObject(message,
2153 NetworkRequest.class);
2154
Robert Greenwalt6078b502014-06-11 16:05:07 -07002155 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002156 if (callbacks != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002157 callbacks.onLost((Network)getObject(message, Network.class));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002158 } else {
2159 Log.e(TAG, "callback not found for LOST message");
2160 }
2161 break;
2162 }
2163 case CALLBACK_UNAVAIL: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002164 NetworkRequest request = (NetworkRequest)getObject(message,
2165 NetworkRequest.class);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002166 NetworkCallback callbacks = null;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002167 synchronized(mCallbackMap) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002168 callbacks = mCallbackMap.get(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002169 }
2170 if (callbacks != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002171 callbacks.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002172 } else {
2173 Log.e(TAG, "callback not found for UNAVAIL message");
2174 }
2175 break;
2176 }
2177 case CALLBACK_CAP_CHANGED: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002178 NetworkRequest request = (NetworkRequest)getObject(message,
2179 NetworkRequest.class);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002180 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002181 if (callbacks != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002182 Network network = (Network)getObject(message, Network.class);
2183 NetworkCapabilities cap = (NetworkCapabilities)getObject(message,
2184 NetworkCapabilities.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002185
Robert Greenwalt6078b502014-06-11 16:05:07 -07002186 callbacks.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002187 } else {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002188 Log.e(TAG, "callback not found for CAP_CHANGED message");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002189 }
2190 break;
2191 }
2192 case CALLBACK_IP_CHANGED: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002193 NetworkRequest request = (NetworkRequest)getObject(message,
2194 NetworkRequest.class);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002195 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002196 if (callbacks != null) {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002197 Network network = (Network)getObject(message, Network.class);
2198 LinkProperties lp = (LinkProperties)getObject(message,
2199 LinkProperties.class);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002200
Robert Greenwalt6078b502014-06-11 16:05:07 -07002201 callbacks.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002202 } else {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002203 Log.e(TAG, "callback not found for IP_CHANGED message");
Robert Greenwalt9258c642014-03-26 16:47:06 -07002204 }
2205 break;
2206 }
2207 case CALLBACK_RELEASED: {
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002208 NetworkRequest req = (NetworkRequest)getObject(message, NetworkRequest.class);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002209 NetworkCallback callbacks = null;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002210 synchronized(mCallbackMap) {
2211 callbacks = mCallbackMap.remove(req);
2212 }
2213 if (callbacks != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002214 synchronized(mRefCount) {
2215 if (mRefCount.decrementAndGet() == 0) {
2216 getLooper().quit();
2217 }
2218 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002219 } else {
2220 Log.e(TAG, "callback not found for CANCELED message");
2221 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002222 break;
2223 }
2224 case CALLBACK_EXIT: {
2225 Log.d(TAG, "Listener quiting");
2226 getLooper().quit();
2227 break;
2228 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07002229 case EXPIRE_LEGACY_REQUEST: {
2230 expireRequest((NetworkCapabilities)message.obj, message.arg1);
2231 break;
2232 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002233 }
2234 }
2235
Robert Greenwalta848c1c2014-09-30 16:50:07 -07002236 private Object getObject(Message msg, Class c) {
2237 return msg.getData().getParcelable(c.getSimpleName());
Robert Greenwalt9258c642014-03-26 16:47:06 -07002238 }
Robert Greenwalt6078b502014-06-11 16:05:07 -07002239 private NetworkCallback getCallbacks(NetworkRequest req) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002240 synchronized(mCallbackMap) {
2241 return mCallbackMap.get(req);
2242 }
2243 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002244 }
2245
Robert Greenwalt6078b502014-06-11 16:05:07 -07002246 private void incCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002247 synchronized(sCallbackRefCount) {
2248 if (sCallbackRefCount.incrementAndGet() == 1) {
2249 // TODO - switch this over to a ManagerThread or expire it when done
2250 HandlerThread callbackThread = new HandlerThread("ConnectivityManager");
2251 callbackThread.start();
2252 sCallbackHandler = new CallbackHandler(callbackThread.getLooper(),
Robert Greenwalt6078b502014-06-11 16:05:07 -07002253 sNetworkCallback, sCallbackRefCount, this);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002254 }
2255 }
2256 }
2257
Robert Greenwalt6078b502014-06-11 16:05:07 -07002258 private void decCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002259 synchronized(sCallbackRefCount) {
2260 if (sCallbackRefCount.decrementAndGet() == 0) {
2261 sCallbackHandler.obtainMessage(CALLBACK_EXIT).sendToTarget();
2262 sCallbackHandler = null;
2263 }
2264 }
2265 }
2266
Robert Greenwalt6078b502014-06-11 16:05:07 -07002267 static final HashMap<NetworkRequest, NetworkCallback> sNetworkCallback =
2268 new HashMap<NetworkRequest, NetworkCallback>();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002269 static final AtomicInteger sCallbackRefCount = new AtomicInteger(0);
2270 static CallbackHandler sCallbackHandler = null;
2271
2272 private final static int LISTEN = 1;
2273 private final static int REQUEST = 2;
2274
Robert Greenwalt562cc542014-05-15 18:07:26 -07002275 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002276 NetworkCallback networkCallback, int timeoutSec, int action,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07002277 int legacyType) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002278 if (networkCallback == null) {
2279 throw new IllegalArgumentException("null NetworkCallback");
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002280 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002281 if (need == null) throw new IllegalArgumentException("null NetworkCapabilities");
2282 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002283 incCallbackHandlerRefCount();
Paul Jensen7221cc32014-06-27 11:05:32 -04002284 synchronized(sNetworkCallback) {
2285 if (action == LISTEN) {
2286 networkCallback.networkRequest = mService.listenForNetwork(need,
2287 new Messenger(sCallbackHandler), new Binder());
2288 } else {
2289 networkCallback.networkRequest = mService.requestNetwork(need,
2290 new Messenger(sCallbackHandler), timeoutSec, new Binder(), legacyType);
2291 }
2292 if (networkCallback.networkRequest != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002293 sNetworkCallback.put(networkCallback.networkRequest, networkCallback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002294 }
2295 }
2296 } catch (RemoteException e) {}
Robert Greenwalt6078b502014-06-11 16:05:07 -07002297 if (networkCallback.networkRequest == null) decCallbackHandlerRefCount();
2298 return networkCallback.networkRequest;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002299 }
2300
2301 /**
2302 * Request a network to satisfy a set of {@link NetworkCapabilities}.
2303 *
2304 * This {@link NetworkRequest} will live until released via
Robert Greenwalt6078b502014-06-11 16:05:07 -07002305 * {@link #unregisterNetworkCallback} or the calling application exits.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002306 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07002307 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002308 * can be used to direct traffic to the network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002309 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002310 * @param request {@link NetworkRequest} describing this request.
2311 * @param networkCallback The {@link NetworkCallback} to be utilized for this
2312 * request. Note the callback must not be shared - they
2313 * uniquely specify this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002314 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002315 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
2316 sendRequestForNetwork(request.networkCapabilities, networkCallback, 0,
Ye Wenb87875e2014-07-21 14:19:01 -07002317 REQUEST, inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002318 }
2319
2320 /**
2321 * Request a network to satisfy a set of {@link NetworkCapabilities}, limited
2322 * by a timeout.
2323 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002324 * This function behaves identically to the non-timedout version, but if a suitable
Robert Greenwalt6078b502014-06-11 16:05:07 -07002325 * network is not found within the given time (in milliseconds) the
2326 * {@link NetworkCallback#unavailable} callback is called. The request must
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002327 * still be released normally by calling {@link releaseNetworkRequest}.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002328 * @param request {@link NetworkRequest} describing this request.
2329 * @param networkCallback The callbacks to be utilized for this request. Note
2330 * the callbacks must not be shared - they uniquely specify
2331 * this request.
2332 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
2333 * before {@link NetworkCallback#unavailable} is called.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002334 * @hide
2335 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002336 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
2337 int timeoutMs) {
2338 sendRequestForNetwork(request.networkCapabilities, networkCallback, timeoutMs,
Ye Wenb87875e2014-07-21 14:19:01 -07002339 REQUEST, inferLegacyTypeForNetworkCapabilities(request.networkCapabilities));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002340 }
2341
2342 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002343 * The maximum number of milliseconds the framework will look for a suitable network
Robert Greenwalt9258c642014-03-26 16:47:06 -07002344 * during a timeout-equiped call to {@link requestNetwork}.
2345 * {@hide}
2346 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002347 public final static int MAX_NETWORK_REQUEST_TIMEOUT_MS = 100 * 60 * 1000;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002348
2349 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002350 * The lookup key for a {@link Network} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002351 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002352 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeremy Joslinfcde58f2015-02-11 16:51:13 -08002353 * <p>
Paul Jensen72db88e2015-03-10 10:54:12 -04002354 * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)}
2355 * then you must get a ConnectivityManager instance before doing so.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002356 */
Erik Kline90e93072014-11-19 12:12:24 +09002357 public static final String EXTRA_NETWORK = "android.net.extra.NETWORK";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002358
2359 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002360 * The lookup key for a {@link NetworkRequest} object included with the intent after
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002361 * successfully finding a network for the applications request. Retrieve it with
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002362 * {@link android.content.Intent#getParcelableExtra(String)}.
2363 */
Erik Kline90e93072014-11-19 12:12:24 +09002364 public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002365
2366
2367 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07002368 * Request a network to satisfy a set of {@link NetworkCapabilities}.
2369 *
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002370 * This function behaves identically to the version that takes a NetworkCallback, but instead
Robert Greenwalt6078b502014-06-11 16:05:07 -07002371 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002372 * the request may outlive the calling application and get called back when a suitable
2373 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002374 * <p>
2375 * The operation is an Intent broadcast that goes to a broadcast receiver that
2376 * you registered with {@link Context#registerReceiver} or through the
2377 * &lt;receiver&gt; tag in an AndroidManifest.xml file
2378 * <p>
2379 * The operation Intent is delivered with two extras, a {@link Network} typed
Erik Kline90e93072014-11-19 12:12:24 +09002380 * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest}
2381 * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07002382 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002383 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07002384 * Intent to reserve the network or it will be released shortly after the Intent
2385 * is processed.
2386 * <p>
2387 * If there is already an request for this Intent registered (with the equality of
2388 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002389 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002390 * <p>
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002391 * The request may be released normally by calling
2392 * {@link #releaseNetworkRequest(android.app.PendingIntent)}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002393 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002394 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002395 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07002396 * to the {@link NetworkCallback#onAvailable} call. Typically
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002397 * comes from {@link PendingIntent#getBroadcast}. Cannot be null.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002398 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002399 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002400 checkPendingIntent(operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002401 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002402 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002403 } catch (RemoteException e) {}
Robert Greenwalt9258c642014-03-26 16:47:06 -07002404 }
2405
2406 /**
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002407 * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)}
2408 * <p>
2409 * This method has the same behavior as {@link #unregisterNetworkCallback} with respect to
2410 * releasing network resources and disconnecting.
2411 *
2412 * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the
2413 * PendingIntent passed to
2414 * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the
2415 * corresponding NetworkRequest you'd like to remove. Cannot be null.
2416 */
2417 public void releaseNetworkRequest(PendingIntent operation) {
2418 checkPendingIntent(operation);
2419 try {
2420 mService.releasePendingNetworkRequest(operation);
2421 } catch (RemoteException e) {}
2422 }
2423
2424 private void checkPendingIntent(PendingIntent intent) {
2425 if (intent == null) {
2426 throw new IllegalArgumentException("PendingIntent cannot be null.");
2427 }
2428 }
2429
2430 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07002431 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07002432 * {@link NetworkRequest}. The callbacks will continue to be called until
2433 * either the application exits or {@link #unregisterNetworkCallback} is called
Robert Greenwalt9258c642014-03-26 16:47:06 -07002434 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002435 * @param request {@link NetworkRequest} describing this request.
2436 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
2437 * networks change state.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002438 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002439 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
2440 sendRequestForNetwork(request.networkCapabilities, networkCallback, 0, LISTEN, TYPE_NONE);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002441 }
2442
2443 /**
Robert Greenwalt2c22a912015-04-21 23:59:14 +00002444 * Request connectivityservice to refresh network capabilities for the given
2445 * {@link network}. This method returns true if the network is still active, false
2446 * otherwise. Notice the method call assumes the caller has registered for
2447 * listening NetworkCapabilities updates.
fenglub15e72b2015-03-20 11:29:56 -07002448 *
2449 * @param network{@link Network} specifying which network you're interested.
Robert Greenwalt2c22a912015-04-21 23:59:14 +00002450 * @hide
fenglub15e72b2015-03-20 11:29:56 -07002451 */
Robert Greenwalt2c22a912015-04-21 23:59:14 +00002452 public boolean requestBwUpdate(Network network) {
fenglub15e72b2015-03-20 11:29:56 -07002453 try {
Robert Greenwalt2c22a912015-04-21 23:59:14 +00002454 return mService.requestBwUpdate(network);
fenglub15e72b2015-03-20 11:29:56 -07002455 } catch (RemoteException e) {
2456 return false;
2457 }
2458 }
2459
2460 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002461 * Unregisters callbacks about and possibly releases networks originating from
2462 * {@link #requestNetwork} and {@link #registerNetworkCallback} calls. If the
Jeremy Joslin46e3ac82014-11-05 10:32:09 -08002463 * given {@code NetworkCallback} had previously been used with {@code #requestNetwork},
Robert Greenwalt6078b502014-06-11 16:05:07 -07002464 * any networks that had been connected to only to satisfy that request will be
2465 * disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002466 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002467 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002468 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002469 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
2470 if (networkCallback == null || networkCallback.networkRequest == null ||
2471 networkCallback.networkRequest.requestId == REQUEST_ID_UNSET) {
2472 throw new IllegalArgumentException("Invalid NetworkCallback");
2473 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002474 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002475 mService.releaseNetworkRequest(networkCallback.networkRequest);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002476 } catch (RemoteException e) {}
2477 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002478
2479 /**
Lorenzo Colittie03c3c72015-04-03 16:38:52 +09002480 * Informs the system whether it should switch to {@code network} regardless of whether it is
2481 * validated or not. If {@code accept} is true, and the network was explicitly selected by the
2482 * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become
2483 * the system default network regardless of any other network that's currently connected. If
2484 * {@code always} is true, then the choice is remembered, so that the next time the user
2485 * connects to this network, the system will switch to it.
2486 *
2487 * <p>This method requires the caller to hold the permission
2488 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
2489 *
2490 * @param network The network to accept.
2491 * @param accept Whether to accept the network even if unvalidated.
2492 * @param always Whether to remember this choice in the future.
2493 *
2494 * @hide
2495 */
2496 public void setAcceptUnvalidated(Network network, boolean accept, boolean always) {
2497 try {
2498 mService.setAcceptUnvalidated(network, accept, always);
2499 } catch (RemoteException e) {}
2500 }
2501
2502 /**
Stuart Scott984dc852015-03-30 13:17:11 -07002503 * Resets all connectivity manager settings back to factory defaults.
2504 * @hide
2505 */
2506 public void factoryReset() {
Stuart Scott984dc852015-03-30 13:17:11 -07002507 try {
Stuart Scottf1fb3972015-04-02 18:00:02 -07002508 mService.factoryReset();
Stuart Scott984dc852015-03-30 13:17:11 -07002509 } catch (RemoteException e) {
Stuart Scott984dc852015-03-30 13:17:11 -07002510 }
2511 }
2512
2513 /**
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002514 * Binds the current process to {@code network}. All Sockets created in the future
2515 * (and not explicitly bound via a bound SocketFactory from
2516 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
2517 * {@code network}. All host name resolutions will be limited to {@code network} as well.
2518 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
2519 * work and all host name resolutions will fail. This is by design so an application doesn't
2520 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
2521 * To clear binding pass {@code null} for {@code network}. Using individually bound
2522 * Sockets created by Network.getSocketFactory().createSocket() and
2523 * performing network-specific host name resolutions via
2524 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
Paul Jensen72db88e2015-03-10 10:54:12 -04002525 * {@code bindProcessToNetwork}.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002526 *
2527 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
2528 * the current binding.
2529 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2530 */
Paul Jensen72db88e2015-03-10 10:54:12 -04002531 public boolean bindProcessToNetwork(Network network) {
2532 // Forcing callers to call thru non-static function ensures ConnectivityManager
2533 // instantiated.
2534 return setProcessDefaultNetwork(network);
2535 }
2536
2537 /**
2538 * Binds the current process to {@code network}. All Sockets created in the future
2539 * (and not explicitly bound via a bound SocketFactory from
2540 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
2541 * {@code network}. All host name resolutions will be limited to {@code network} as well.
2542 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
2543 * work and all host name resolutions will fail. This is by design so an application doesn't
2544 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
2545 * To clear binding pass {@code null} for {@code network}. Using individually bound
2546 * Sockets created by Network.getSocketFactory().createSocket() and
2547 * performing network-specific host name resolutions via
2548 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
2549 * {@code setProcessDefaultNetwork}.
2550 *
2551 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
2552 * the current binding.
2553 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2554 * @deprecated This function can throw {@link IllegalStateException}. Use
2555 * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork}
2556 * is a direct replacement.
2557 */
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002558 public static boolean setProcessDefaultNetwork(Network network) {
Paul Jensenc91b5342014-08-27 12:38:45 -04002559 int netId = (network == null) ? NETID_UNSET : network.netId;
Paul Jensen72db88e2015-03-10 10:54:12 -04002560 if (netId == NetworkUtils.getBoundNetworkForProcess()) {
Paul Jensenc91b5342014-08-27 12:38:45 -04002561 return true;
2562 }
2563 if (NetworkUtils.bindProcessToNetwork(netId)) {
Paul Jensene0bef712014-12-10 15:12:18 -05002564 // Set HTTP proxy system properties to match network.
2565 // TODO: Deprecate this static method and replace it with a non-static version.
2566 Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy());
Paul Jensenc91b5342014-08-27 12:38:45 -04002567 // Must flush DNS cache as new network may have different DNS resolutions.
2568 InetAddress.clearDnsCache();
2569 // Must flush socket pool as idle sockets will be bound to previous network and may
2570 // cause subsequent fetches to be performed on old network.
2571 NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged();
2572 return true;
2573 } else {
2574 return false;
2575 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002576 }
2577
2578 /**
2579 * Returns the {@link Network} currently bound to this process via
Paul Jensen72db88e2015-03-10 10:54:12 -04002580 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002581 *
2582 * @return {@code Network} to which this process is bound, or {@code null}.
2583 */
Paul Jensen72db88e2015-03-10 10:54:12 -04002584 public Network getBoundNetworkForProcess() {
2585 // Forcing callers to call thru non-static function ensures ConnectivityManager
2586 // instantiated.
2587 return getProcessDefaultNetwork();
2588 }
2589
2590 /**
2591 * Returns the {@link Network} currently bound to this process via
2592 * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound.
2593 *
2594 * @return {@code Network} to which this process is bound, or {@code null}.
2595 * @deprecated Using this function can lead to other functions throwing
2596 * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead.
2597 * {@code getBoundNetworkForProcess} is a direct replacement.
2598 */
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002599 public static Network getProcessDefaultNetwork() {
Paul Jensen72db88e2015-03-10 10:54:12 -04002600 int netId = NetworkUtils.getBoundNetworkForProcess();
Paul Jensenbcc76d32014-07-11 08:17:29 -04002601 if (netId == NETID_UNSET) return null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002602 return new Network(netId);
2603 }
2604
2605 /**
2606 * Binds host resolutions performed by this process to {@code network}.
Paul Jensen72db88e2015-03-10 10:54:12 -04002607 * {@link #bindProcessToNetwork} takes precedence over this setting.
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002608 *
2609 * @param network The {@link Network} to bind host resolutions from the current process to, or
2610 * {@code null} to clear the current binding.
2611 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2612 * @hide
2613 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
2614 */
2615 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
Paul Jensenbcc76d32014-07-11 08:17:29 -04002616 return NetworkUtils.bindProcessToNetworkForHostResolution(
2617 network == null ? NETID_UNSET : network.netId);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002618 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002619}