blob: b68ce367ee871c5d40a4531477e4400804ba3f32 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080016package android.net;
17
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -070018import static com.android.internal.util.Preconditions.checkNotNull;
19
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020import android.annotation.SdkConstant;
21import android.annotation.SdkConstant.SdkConstantType;
Robert Greenwalt9258c642014-03-26 16:47:06 -070022import android.app.PendingIntent;
Jeff Sharkey8fc27e82012-04-04 20:40:58 -070023import android.content.Context;
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070024import android.content.Intent;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -040025import android.net.NetworkUtils;
Robert Greenwalt42acef32009-08-12 16:08:25 -070026import android.os.Binder;
Jeff Sharkey3a844fc2011-08-16 14:37:57 -070027import android.os.Build.VERSION_CODES;
Robert Greenwalt9258c642014-03-26 16:47:06 -070028import android.os.Handler;
29import android.os.HandlerThread;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080030import android.os.IBinder;
31import android.os.INetworkActivityListener;
32import android.os.INetworkManagementService;
Robert Greenwalt9258c642014-03-26 16:47:06 -070033import android.os.Looper;
34import android.os.Message;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -070035import android.os.Messenger;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080036import android.os.RemoteException;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080037import android.os.ServiceManager;
Jeff Sharkey961e3042011-08-29 16:02:57 -070038import android.provider.Settings;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070039import android.telephony.TelephonyManager;
Dianne Hackborn77b987f2014-02-26 16:20:52 -080040import android.util.ArrayMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -070041import android.util.Log;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080042
Robert Greenwaltafa05c02014-05-21 20:04:36 -070043import com.android.internal.telephony.ITelephony;
Robert Greenwalt562cc542014-05-15 18:07:26 -070044import com.android.internal.telephony.PhoneConstants;
Robert Greenwaltafa05c02014-05-21 20:04:36 -070045import com.android.internal.util.Protocol;
46
Robert Greenwalt585ac0f2010-08-27 09:24:29 -070047import java.net.InetAddress;
Robert Greenwalt9258c642014-03-26 16:47:06 -070048import java.util.concurrent.atomic.AtomicInteger;
49import java.util.HashMap;
50
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080051/**
52 * Class that answers queries about the state of network connectivity. It also
53 * notifies applications when network connectivity changes. Get an instance
54 * of this class by calling
55 * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}.
56 * <p>
57 * The primary responsibilities of this class are to:
58 * <ol>
59 * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li>
60 * <li>Send broadcast intents when network connectivity changes</li>
61 * <li>Attempt to "fail over" to another network when connectivity to a network
62 * is lost</li>
63 * <li>Provide an API that allows applications to query the coarse-grained or fine-grained
64 * state of the available networks</li>
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070065 * <li>Provide an API that allows applications to request and select networks for their data
66 * traffic</li>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080067 * </ol>
68 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070069public class ConnectivityManager {
70 private static final String TAG = "ConnectivityManager";
Robert Greenwalt257ee5f2014-06-20 10:58:45 -070071 private static final boolean LEGACY_DBG = true; // STOPSHIP
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -070072
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080073 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -070074 * A change in network connectivity has occurred. A default connection has either
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080075 * been established or lost. The NetworkInfo for the affected network is
76 * sent as an extra; it should be consulted to see what kind of
77 * connectivity event occurred.
78 * <p/>
79 * If this is a connection that was the result of failing over from a
80 * disconnected network, then the FAILOVER_CONNECTION boolean extra is
81 * set to true.
82 * <p/>
83 * For a loss of connectivity, if the connectivity manager is attempting
84 * to connect (or has already connected) to another network, the
85 * NetworkInfo for the new network is also passed as an extra. This lets
86 * any receivers of the broadcast know that they should not necessarily
87 * tell the user that no data traffic will be possible. Instead, the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -080088 * receiver should expect another broadcast soon, indicating either that
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080089 * the failover attempt succeeded (and so there is still overall data
90 * connectivity), or that the failover attempt failed, meaning that all
91 * connectivity has been lost.
92 * <p/>
93 * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY
94 * is set to {@code true} if there are no connected networks at all.
95 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -080096 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097 public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -070098
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080099 /**
Jeff Sharkey961e3042011-08-29 16:02:57 -0700100 * Identical to {@link #CONNECTIVITY_ACTION} broadcast, but sent without any
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800101 * applicable {@link Settings.Global#CONNECTIVITY_CHANGE_DELAY}.
Jeff Sharkey961e3042011-08-29 16:02:57 -0700102 *
103 * @hide
104 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800105 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey961e3042011-08-29 16:02:57 -0700106 public static final String CONNECTIVITY_ACTION_IMMEDIATE =
107 "android.net.conn.CONNECTIVITY_CHANGE_IMMEDIATE";
108
109 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800110 * The lookup key for a {@link NetworkInfo} object. Retrieve with
111 * {@link android.content.Intent#getParcelableExtra(String)}.
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700112 *
113 * @deprecated Since {@link NetworkInfo} can vary based on UID, applications
114 * should always obtain network information through
115 * {@link #getActiveNetworkInfo()} or
116 * {@link #getAllNetworkInfo()}.
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700117 * @see #EXTRA_NETWORK_TYPE
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800118 */
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700119 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800120 public static final String EXTRA_NETWORK_INFO = "networkInfo";
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -0700121
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800122 /**
Jeff Sharkey75fbb4b2012-08-06 11:41:50 -0700123 * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast.
124 * Can be used with {@link #getNetworkInfo(int)} to get {@link NetworkInfo}
125 * state based on the calling application.
126 *
127 * @see android.content.Intent#getIntExtra(String, int)
128 */
129 public static final String EXTRA_NETWORK_TYPE = "networkType";
130
131 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800132 * The lookup key for a boolean that indicates whether a connect event
133 * is for a network to which the connectivity manager was failing over
134 * following a disconnect on another network.
135 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
136 */
137 public static final String EXTRA_IS_FAILOVER = "isFailover";
138 /**
139 * The lookup key for a {@link NetworkInfo} object. This is supplied when
140 * there is another network that it may be possible to connect to. Retrieve with
141 * {@link android.content.Intent#getParcelableExtra(String)}.
142 */
143 public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork";
144 /**
145 * The lookup key for a boolean that indicates whether there is a
146 * complete lack of connectivity, i.e., no network is available.
147 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
148 */
149 public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity";
150 /**
151 * The lookup key for a string that indicates why an attempt to connect
152 * to a network failed. The string has no particular structure. It is
153 * intended to be used in notifications presented to users. Retrieve
154 * it with {@link android.content.Intent#getStringExtra(String)}.
155 */
156 public static final String EXTRA_REASON = "reason";
157 /**
158 * The lookup key for a string that provides optionally supplied
159 * extra information about the network state. The information
160 * may be passed up from the lower networking layers, and its
161 * meaning may be specific to a particular network type. Retrieve
162 * it with {@link android.content.Intent#getStringExtra(String)}.
163 */
164 public static final String EXTRA_EXTRA_INFO = "extraInfo";
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700165 /**
166 * The lookup key for an int that provides information about
167 * our connection to the internet at large. 0 indicates no connection,
168 * 100 indicates a great connection. Retrieve it with
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700169 * {@link android.content.Intent#getIntExtra(String, int)}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -0700170 * {@hide}
171 */
172 public static final String EXTRA_INET_CONDITION = "inetCondition";
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800173
174 /**
Haoyu Baidb3c8672012-06-20 14:29:57 -0700175 * Broadcast action to indicate the change of data activity status
176 * (idle or active) on a network in a recent period.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800177 * The network becomes active when data transmission is started, or
178 * idle if there is no data transmission for a period of time.
Haoyu Baidb3c8672012-06-20 14:29:57 -0700179 * {@hide}
180 */
181 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
182 public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE";
183 /**
184 * The lookup key for an enum that indicates the network device type on which this data activity
185 * change happens.
186 * {@hide}
187 */
188 public static final String EXTRA_DEVICE_TYPE = "deviceType";
189 /**
190 * The lookup key for a boolean that indicates the device is active or not. {@code true} means
191 * it is actively sending or receiving data and {@code false} means it is idle.
192 * {@hide}
193 */
194 public static final String EXTRA_IS_ACTIVE = "isActive";
Ashish Sharma0535a9f2014-03-12 18:42:23 -0700195 /**
196 * The lookup key for a long that contains the timestamp (nanos) of the radio state change.
197 * {@hide}
198 */
199 public static final String EXTRA_REALTIME_NS = "tsNanos";
Haoyu Baidb3c8672012-06-20 14:29:57 -0700200
201 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800202 * Broadcast Action: The setting for background data usage has changed
203 * values. Use {@link #getBackgroundDataSetting()} to get the current value.
204 * <p>
205 * If an application uses the network in the background, it should listen
206 * for this broadcast and stop using the background data if the value is
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700207 * {@code false}.
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800208 * <p>
209 *
210 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability
211 * of background data depends on several combined factors, and
212 * this broadcast is no longer sent. Instead, when background
213 * data is unavailable, {@link #getActiveNetworkInfo()} will now
214 * appear disconnected. During first boot after a platform
215 * upgrade, this broadcast will be sent once if
216 * {@link #getBackgroundDataSetting()} was {@code false} before
217 * the upgrade.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800218 */
219 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Jeff Sharkey54ee2ad2012-01-30 16:29:24 -0800220 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800221 public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED =
222 "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED";
223
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700224 /**
225 * Broadcast Action: The network connection may not be good
226 * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and
227 * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify
228 * the network and it's condition.
229 * @hide
230 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800231 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwalt1e9aac22010-09-15 17:36:33 -0700232 public static final String INET_CONDITION_ACTION =
233 "android.net.conn.INET_CONDITION_ACTION";
234
Robert Greenwalt42acef32009-08-12 16:08:25 -0700235 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800236 * Broadcast Action: A tetherable connection has come or gone.
237 * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER},
238 * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER} and
239 * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate
240 * the current state of tethering. Each include a list of
241 * interface names in that state (may be empty).
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800242 * @hide
243 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800244 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800245 public static final String ACTION_TETHER_STATE_CHANGED =
246 "android.net.conn.TETHER_STATE_CHANGED";
247
248 /**
249 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800250 * gives a String[] listing all the interfaces configured for
251 * tethering and currently available for tethering.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800252 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800253 public static final String EXTRA_AVAILABLE_TETHER = "availableArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800254
255 /**
256 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800257 * gives a String[] listing all the interfaces currently tethered
258 * (ie, has dhcp support and packets potentially forwarded/NATed)
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800259 */
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800260 public static final String EXTRA_ACTIVE_TETHER = "activeArray";
261
262 /**
263 * @hide
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800264 * gives a String[] listing all the interfaces we tried to tether and
265 * failed. Use {@link #getLastTetherError} to find the error code
266 * for any interfaces listed here.
Robert Greenwalt2a091d72010-02-11 18:18:40 -0800267 */
268 public static final String EXTRA_ERRORED_TETHER = "erroredArray";
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -0800269
270 /**
Russell Brenner108da0c2013-02-12 10:03:14 -0800271 * Broadcast Action: The captive portal tracker has finished its test.
272 * Sent only while running Setup Wizard, in lieu of showing a user
273 * notification.
274 * @hide
275 */
Jeff Sharkey4fa63b22013-02-20 18:21:19 -0800276 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Russell Brenner108da0c2013-02-12 10:03:14 -0800277 public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED =
278 "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED";
279 /**
280 * The lookup key for a boolean that indicates whether a captive portal was detected.
281 * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}.
282 * @hide
283 */
284 public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal";
285
286 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800287 * The absence of a connection type.
Robert Greenwaltccf83af12011-06-02 17:30:47 -0700288 * @hide
289 */
290 public static final int TYPE_NONE = -1;
291
292 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800293 * The Mobile data connection. When active, all data traffic
294 * will use this network type's interface by default
295 * (it has a default route)
Robert Greenwalt42acef32009-08-12 16:08:25 -0700296 */
297 public static final int TYPE_MOBILE = 0;
298 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800299 * The WIFI data connection. When active, all data traffic
300 * will use this network type's interface by default
301 * (it has a default route).
Robert Greenwalt42acef32009-08-12 16:08:25 -0700302 */
303 public static final int TYPE_WIFI = 1;
304 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800305 * An MMS-specific Mobile data connection. This network type may use the
306 * same network interface as {@link #TYPE_MOBILE} or it may use a different
307 * one. This is used by applications needing to talk to the carrier's
308 * Multimedia Messaging Service servers.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700309 */
310 public static final int TYPE_MOBILE_MMS = 2;
311 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800312 * A SUPL-specific Mobile data connection. This network type may use the
313 * same network interface as {@link #TYPE_MOBILE} or it may use a different
314 * one. This is used by applications needing to talk to the carrier's
315 * Secure User Plane Location servers for help locating the device.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700316 */
317 public static final int TYPE_MOBILE_SUPL = 3;
318 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800319 * A DUN-specific Mobile data connection. This network type may use the
320 * same network interface as {@link #TYPE_MOBILE} or it may use a different
321 * one. This is sometimes by the system when setting up an upstream connection
322 * for tethering so that the carrier is aware of DUN traffic.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700323 */
324 public static final int TYPE_MOBILE_DUN = 4;
325 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800326 * A High Priority Mobile data connection. This network type uses the
327 * same network interface as {@link #TYPE_MOBILE} but the routing setup
328 * is different. Only requesting processes will have access to the
329 * Mobile DNS servers and only IP's explicitly requested via {@link #requestRouteToHost}
330 * will route over this interface if no default route exists.
Robert Greenwalt42acef32009-08-12 16:08:25 -0700331 */
332 public static final int TYPE_MOBILE_HIPRI = 5;
jsh8214deb2010-03-11 15:04:43 -0800333 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800334 * The WiMAX data connection. When active, all data traffic
335 * will use this network type's interface by default
336 * (it has a default route).
jsh8214deb2010-03-11 15:04:43 -0800337 */
338 public static final int TYPE_WIMAX = 6;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800339
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800340 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800341 * The Bluetooth data connection. When active, all data traffic
342 * will use this network type's interface by default
343 * (it has a default route).
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800344 */
345 public static final int TYPE_BLUETOOTH = 7;
346
Robert Greenwalt60810842011-04-22 15:28:18 -0700347 /**
348 * Dummy data connection. This should not be used on shipping devices.
349 */
Jaikumar Ganesh15c74392010-12-21 22:31:44 -0800350 public static final int TYPE_DUMMY = 8;
Wink Saville9d7d6282011-03-12 14:52:01 -0800351
Robert Greenwalt60810842011-04-22 15:28:18 -0700352 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800353 * The Ethernet data connection. When active, all data traffic
354 * will use this network type's interface by default
355 * (it has a default route).
Robert Greenwalt60810842011-04-22 15:28:18 -0700356 */
Robert Greenwalte12aec92011-01-28 14:48:37 -0800357 public static final int TYPE_ETHERNET = 9;
Robert Greenwalt60810842011-04-22 15:28:18 -0700358
Wink Saville9d7d6282011-03-12 14:52:01 -0800359 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800360 * Over the air Administration.
Wink Saville9d7d6282011-03-12 14:52:01 -0800361 * {@hide}
362 */
363 public static final int TYPE_MOBILE_FOTA = 10;
364
365 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800366 * IP Multimedia Subsystem.
Wink Saville9d7d6282011-03-12 14:52:01 -0800367 * {@hide}
368 */
369 public static final int TYPE_MOBILE_IMS = 11;
370
371 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800372 * Carrier Branded Services.
Wink Saville9d7d6282011-03-12 14:52:01 -0800373 * {@hide}
374 */
375 public static final int TYPE_MOBILE_CBS = 12;
376
repo syncaea743a2011-07-29 23:55:49 -0700377 /**
378 * A Wi-Fi p2p connection. Only requesting processes will have access to
379 * the peers connected.
380 * {@hide}
381 */
382 public static final int TYPE_WIFI_P2P = 13;
Wink Saville9d7d6282011-03-12 14:52:01 -0800383
Wink Saville5e56bc52013-07-29 15:00:57 -0700384 /**
385 * The network to use for initially attaching to the network
386 * {@hide}
387 */
388 public static final int TYPE_MOBILE_IA = 14;
repo syncaea743a2011-07-29 23:55:49 -0700389
Hui Lu1c5624a2014-01-15 11:05:36 -0500390 /**
391 * The network that uses proxy to achieve connectivity.
392 * {@hide}
393 */
394 public static final int TYPE_PROXY = 16;
Wink Saville5e56bc52013-07-29 15:00:57 -0700395
396 /** {@hide} */
Hui Lu1c5624a2014-01-15 11:05:36 -0500397 public static final int MAX_RADIO_TYPE = TYPE_PROXY;
398
399 /** {@hide} */
400 public static final int MAX_NETWORK_TYPE = TYPE_PROXY;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800401
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800402 /**
403 * If you want to set the default network preference,you can directly
404 * change the networkAttributes array in framework's config.xml.
405 *
406 * @deprecated Since we support so many more networks now, the single
407 * network default network preference can't really express
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800408 * the hierarchy. Instead, the default is defined by the
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800409 * networkAttributes in config.xml. You can determine
Robert Greenwalt4c8b7482012-12-07 09:56:50 -0800410 * the current value by calling {@link #getNetworkPreference()}
Jianzheng Zhoudcf03f32012-11-16 13:45:20 +0800411 * from an App.
412 */
413 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800414 public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI;
415
Jeff Sharkey625239a2012-09-26 22:03:49 -0700416 /**
417 * Default value for {@link Settings.Global#CONNECTIVITY_CHANGE_DELAY} in
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800418 * milliseconds. This was introduced because IPv6 routes seem to take a
419 * moment to settle - trying network activity before the routes are adjusted
420 * can lead to packets using the wrong interface or having the wrong IP address.
421 * This delay is a bit crude, but in the future hopefully we will have kernel
422 * notifications letting us know when it's safe to use the new network.
Jeff Sharkey625239a2012-09-26 22:03:49 -0700423 *
424 * @hide
425 */
426 public static final int CONNECTIVITY_CHANGE_DELAY_DEFAULT = 3000;
427
Robert Greenwalt9ba9c582014-03-19 17:56:12 -0700428 /**
429 * @hide
430 */
431 public final static int INVALID_NET_ID = 0;
432
Robert Greenwalt7569f182014-06-08 16:42:59 -0700433 /**
434 * @hide
435 */
436 public final static int REQUEST_ID_UNSET = 0;
437
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700438 private final IConnectivityManager mService;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800439
Chad Brubakerf81daa92014-02-14 13:22:34 -0800440 private final String mPackageName;
441
Dianne Hackborn77b987f2014-02-26 16:20:52 -0800442 private INetworkManagementService mNMService;
443
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800444 /**
445 * Tests if a given integer represents a valid network type.
446 * @param networkType the type to be tested
447 * @return a boolean. {@code true} if the type is valid, else {@code false}
448 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700449 public static boolean isNetworkTypeValid(int networkType) {
Robert Greenwalt42acef32009-08-12 16:08:25 -0700450 return networkType >= 0 && networkType <= MAX_NETWORK_TYPE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800451 }
452
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800453 /**
454 * Returns a non-localized string representing a given network type.
455 * ONLY used for debugging output.
456 * @param type the type needing naming
457 * @return a String for the given type, or a string version of the type ("87")
458 * if no name is known.
459 * {@hide}
460 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700461 public static String getNetworkTypeName(int type) {
462 switch (type) {
463 case TYPE_MOBILE:
464 return "MOBILE";
465 case TYPE_WIFI:
466 return "WIFI";
467 case TYPE_MOBILE_MMS:
468 return "MOBILE_MMS";
469 case TYPE_MOBILE_SUPL:
470 return "MOBILE_SUPL";
471 case TYPE_MOBILE_DUN:
472 return "MOBILE_DUN";
473 case TYPE_MOBILE_HIPRI:
474 return "MOBILE_HIPRI";
475 case TYPE_WIMAX:
476 return "WIMAX";
477 case TYPE_BLUETOOTH:
478 return "BLUETOOTH";
479 case TYPE_DUMMY:
480 return "DUMMY";
481 case TYPE_ETHERNET:
482 return "ETHERNET";
483 case TYPE_MOBILE_FOTA:
484 return "MOBILE_FOTA";
485 case TYPE_MOBILE_IMS:
486 return "MOBILE_IMS";
487 case TYPE_MOBILE_CBS:
488 return "MOBILE_CBS";
repo syncaea743a2011-07-29 23:55:49 -0700489 case TYPE_WIFI_P2P:
490 return "WIFI_P2P";
Wink Saville5e56bc52013-07-29 15:00:57 -0700491 case TYPE_MOBILE_IA:
492 return "MOBILE_IA";
Hui Lu1c5624a2014-01-15 11:05:36 -0500493 case TYPE_PROXY:
494 return "PROXY";
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700495 default:
496 return Integer.toString(type);
497 }
498 }
499
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800500 /**
501 * Checks if a given type uses the cellular data connection.
502 * This should be replaced in the future by a network property.
503 * @param networkType the type to check
504 * @return a boolean - {@code true} if uses cellular network, else {@code false}
505 * {@hide}
506 */
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700507 public static boolean isNetworkTypeMobile(int networkType) {
508 switch (networkType) {
509 case TYPE_MOBILE:
510 case TYPE_MOBILE_MMS:
511 case TYPE_MOBILE_SUPL:
512 case TYPE_MOBILE_DUN:
513 case TYPE_MOBILE_HIPRI:
514 case TYPE_MOBILE_FOTA:
515 case TYPE_MOBILE_IMS:
516 case TYPE_MOBILE_CBS:
Wink Saville5e56bc52013-07-29 15:00:57 -0700517 case TYPE_MOBILE_IA:
Jeff Sharkeyd2a45872011-05-28 20:56:34 -0700518 return true;
519 default:
520 return false;
521 }
522 }
523
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800524 /**
Jeff Sharkey7a1c3fc2013-06-04 12:29:00 -0700525 * Checks if the given network type is backed by a Wi-Fi radio.
526 *
527 * @hide
528 */
529 public static boolean isNetworkTypeWifi(int networkType) {
530 switch (networkType) {
531 case TYPE_WIFI:
532 case TYPE_WIFI_P2P:
533 return true;
534 default:
535 return false;
536 }
537 }
538
539 /**
Chad Brubakerf336d722013-07-15 16:34:04 -0700540 * Checks if the given network type should be exempt from VPN routing rules
541 *
542 * @hide
543 */
544 public static boolean isNetworkTypeExempt(int networkType) {
545 switch (networkType) {
546 case TYPE_MOBILE_MMS:
547 case TYPE_MOBILE_SUPL:
548 case TYPE_MOBILE_HIPRI:
Wink Saville5e56bc52013-07-29 15:00:57 -0700549 case TYPE_MOBILE_IA:
Chad Brubakerf336d722013-07-15 16:34:04 -0700550 return true;
551 default:
552 return false;
553 }
554 }
555
556 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800557 * Specifies the preferred network type. When the device has more
558 * than one type available the preferred network type will be used.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800559 *
560 * @param preference the network type to prefer over all others. It is
561 * unspecified what happens to the old preferred network in the
562 * overall ordering.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700563 * @deprecated Functionality has been removed as it no longer makes sense,
564 * with many more than two networks - we'd need an array to express
565 * preference. Instead we use dynamic network properties of
566 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800567 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800568 public void setNetworkPreference(int preference) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800569 }
570
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800571 /**
572 * Retrieves the current preferred network type.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800573 *
574 * @return an integer representing the preferred network type
575 *
576 * <p>This method requires the caller to hold the permission
577 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700578 * @deprecated Functionality has been removed as it no longer makes sense,
579 * with many more than two networks - we'd need an array to express
580 * preference. Instead we use dynamic network properties of
581 * the networks to describe their precedence.
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800582 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800583 public int getNetworkPreference() {
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700584 return TYPE_NONE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800585 }
586
Scott Main671644c2011-10-06 19:02:28 -0700587 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800588 * Returns details about the currently active default data network. When
589 * connected, this network is the default route for outgoing connections.
590 * You should always check {@link NetworkInfo#isConnected()} before initiating
591 * network traffic. This may return {@code null} when there is no default
592 * network.
593 *
594 * @return a {@link NetworkInfo} object for the current default network
595 * or {@code null} if no network default network is currently active
596 *
597 * <p>This method requires the call to hold the permission
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700598 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -0700599 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800600 public NetworkInfo getActiveNetworkInfo() {
601 try {
602 return mService.getActiveNetworkInfo();
603 } catch (RemoteException e) {
604 return null;
605 }
606 }
607
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800608 /**
609 * Returns details about the currently active default data network
610 * for a given uid. This is for internal use only to avoid spying
611 * other apps.
612 *
613 * @return a {@link NetworkInfo} object for the current default network
614 * for the given uid or {@code null} if no default network is
615 * available for the specified uid.
616 *
617 * <p>This method requires the caller to hold the permission
618 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}
619 * {@hide}
620 */
Jeff Sharkeyc006f1a2011-05-19 17:12:49 -0700621 public NetworkInfo getActiveNetworkInfoForUid(int uid) {
622 try {
623 return mService.getActiveNetworkInfoForUid(uid);
624 } catch (RemoteException e) {
625 return null;
626 }
627 }
628
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800629 /**
630 * Returns connection status information about a particular
631 * network type.
632 *
633 * @param networkType integer specifying which networkType in
634 * which you're interested.
635 * @return a {@link NetworkInfo} object for the requested
636 * network type or {@code null} if the type is not
637 * supported by the device.
638 *
639 * <p>This method requires the call to hold the permission
640 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
641 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800642 public NetworkInfo getNetworkInfo(int networkType) {
643 try {
644 return mService.getNetworkInfo(networkType);
645 } catch (RemoteException e) {
646 return null;
647 }
648 }
649
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800650 /**
651 * Returns connection status information about all network
652 * types supported by the device.
653 *
654 * @return an array of {@link NetworkInfo} objects. Check each
655 * {@link NetworkInfo#getType} for which type each applies.
656 *
657 * <p>This method requires the call to hold the permission
658 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
659 */
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800660 public NetworkInfo[] getAllNetworkInfo() {
661 try {
662 return mService.getAllNetworkInfo();
663 } catch (RemoteException e) {
664 return null;
665 }
666 }
667
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800668 /**
Wink Saville948282b2013-08-29 08:55:16 -0700669 * Returns details about the Provisioning or currently active default data network. When
670 * connected, this network is the default route for outgoing connections.
671 * You should always check {@link NetworkInfo#isConnected()} before initiating
672 * network traffic. This may return {@code null} when there is no default
673 * network.
674 *
675 * @return a {@link NetworkInfo} object for the current default network
676 * or {@code null} if no network default network is currently active
677 *
678 * <p>This method requires the call to hold the permission
679 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
680 *
681 * {@hide}
682 */
683 public NetworkInfo getProvisioningOrActiveNetworkInfo() {
684 try {
685 return mService.getProvisioningOrActiveNetworkInfo();
686 } catch (RemoteException e) {
687 return null;
688 }
689 }
690
691 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800692 * Returns the IP information for the current default network.
693 *
694 * @return a {@link LinkProperties} object describing the IP info
695 * for the current default network, or {@code null} if there
696 * is no current default network.
697 *
698 * <p>This method requires the call to hold the permission
699 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
700 * {@hide}
701 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700702 public LinkProperties getActiveLinkProperties() {
703 try {
704 return mService.getActiveLinkProperties();
705 } catch (RemoteException e) {
706 return null;
707 }
708 }
709
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800710 /**
711 * Returns the IP information for a given network type.
712 *
713 * @param networkType the network type of interest.
714 * @return a {@link LinkProperties} object describing the IP info
715 * for the given networkType, or {@code null} if there is
716 * no current default network.
717 *
718 * <p>This method requires the call to hold the permission
719 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
720 * {@hide}
721 */
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700722 public LinkProperties getLinkProperties(int networkType) {
723 try {
Robert Greenwalt9258c642014-03-26 16:47:06 -0700724 return mService.getLinkPropertiesForType(networkType);
725 } catch (RemoteException e) {
726 return null;
727 }
728 }
729
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700730 /**
731 * Get the {@link LinkProperties} for the given {@link Network}. This
732 * will return {@code null} if the network is unknown.
733 *
734 * @param network The {@link Network} object identifying the network in question.
735 * @return The {@link LinkProperties} for the network, or {@code null}.
736 **/
Robert Greenwalt9258c642014-03-26 16:47:06 -0700737 public LinkProperties getLinkProperties(Network network) {
738 try {
739 return mService.getLinkProperties(network);
740 } catch (RemoteException e) {
741 return null;
742 }
743 }
744
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700745 /**
746 * Get the {@link NetworkCapabilities} for the given {@link Network}. This
747 * will return {@code null} if the network is unknown.
748 *
749 * @param network The {@link Network} object identifying the network in question.
750 * @return The {@link NetworkCapabilities} for the network, or {@code null}.
751 */
Robert Greenwalt9258c642014-03-26 16:47:06 -0700752 public NetworkCapabilities getNetworkCapabilities(Network network) {
753 try {
754 return mService.getNetworkCapabilities(network);
Robert Greenwaltd192dad2010-09-14 09:18:02 -0700755 } catch (RemoteException e) {
756 return null;
757 }
758 }
759
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800760 /**
761 * Tells each network type to set its radio power state as directed.
762 *
763 * @param turnOn a boolean, {@code true} to turn the radios on,
764 * {@code false} to turn them off.
765 * @return a boolean, {@code true} indicating success. All network types
766 * will be tried, even if some fail.
767 *
768 * <p>This method requires the call to hold the permission
769 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
770 * {@hide}
771 */
Robert Greenwalt7b816022014-04-18 15:25:25 -0700772// TODO - check for any callers and remove
773// public boolean setRadios(boolean turnOn) {
774// try {
775// return mService.setRadios(turnOn);
776// } catch (RemoteException e) {
777// return false;
778// }
779// }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800780
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -0800781 /**
782 * Tells a given networkType to set its radio power state as directed.
783 *
784 * @param networkType the int networkType of interest.
785 * @param turnOn a boolean, {@code true} to turn the radio on,
786 * {@code} false to turn it off.
787 * @return a boolean, {@code true} indicating success.
788 *
789 * <p>This method requires the call to hold the permission
790 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
791 * {@hide}
792 */
Robert Greenwalt7b816022014-04-18 15:25:25 -0700793// TODO - check for any callers and remove
794// public boolean setRadio(int networkType, boolean turnOn) {
795// try {
796// return mService.setRadio(networkType, turnOn);
797// } catch (RemoteException e) {
798// return false;
799// }
800// }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800801
802 /**
803 * Tells the underlying networking system that the caller wants to
804 * begin using the named feature. The interpretation of {@code feature}
805 * is completely up to each networking implementation.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700806 * <p>This method requires the caller to hold the permission
807 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800808 * @param networkType specifies which network the request pertains to
809 * @param feature the name of the feature to be used
810 * @return an integer value representing the outcome of the request.
811 * The interpretation of this value is specific to each networking
812 * implementation+feature combination, except that the value {@code -1}
813 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700814 *
815 * @deprecated Deprecated in favor of the cleaner {@link #requestNetwork} api.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800816 */
817 public int startUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700818 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
819 if (netCap == null) {
820 Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " +
821 feature);
822 return PhoneConstants.APN_REQUEST_FAILED;
823 }
824
825 NetworkRequest request = null;
826 synchronized (sLegacyRequests) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -0700827 if (LEGACY_DBG) {
828 Log.d(TAG, "Looking for legacyRequest for netCap with hash: " + netCap + " (" +
829 netCap.hashCode() + ")");
830 Log.d(TAG, "sLegacyRequests has:");
831 for (NetworkCapabilities nc : sLegacyRequests.keySet()) {
832 Log.d(TAG, " " + nc + " (" + nc.hashCode() + ")");
833 }
834 }
Robert Greenwalt562cc542014-05-15 18:07:26 -0700835 LegacyRequest l = sLegacyRequests.get(netCap);
836 if (l != null) {
837 Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest);
838 renewRequestLocked(l);
839 if (l.currentNetwork != null) {
840 return PhoneConstants.APN_ALREADY_ACTIVE;
841 } else {
842 return PhoneConstants.APN_REQUEST_STARTED;
843 }
844 }
845
846 request = requestNetworkForFeatureLocked(netCap);
847 }
848 if (request != null) {
Robert Greenwalt257ee5f2014-06-20 10:58:45 -0700849 Log.d(TAG, "starting startUsingNetworkFeature for request " + request);
Robert Greenwalt562cc542014-05-15 18:07:26 -0700850 return PhoneConstants.APN_REQUEST_STARTED;
851 } else {
852 Log.d(TAG, " request Failed");
853 return PhoneConstants.APN_REQUEST_FAILED;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800854 }
855 }
856
857 /**
858 * Tells the underlying networking system that the caller is finished
859 * using the named feature. The interpretation of {@code feature}
860 * is completely up to each networking implementation.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -0700861 * <p>This method requires the caller to hold the permission
862 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800863 * @param networkType specifies which network the request pertains to
864 * @param feature the name of the feature that is no longer needed
865 * @return an integer value representing the outcome of the request.
866 * The interpretation of this value is specific to each networking
867 * implementation+feature combination, except that the value {@code -1}
868 * always indicates failure.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -0700869 *
870 * @deprecated Deprecated in favor of the cleaner {@link #requestNetwork} api.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800871 */
872 public int stopUsingNetworkFeature(int networkType, String feature) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700873 NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature);
874 if (netCap == null) {
875 Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " +
876 feature);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800877 return -1;
878 }
Robert Greenwalt562cc542014-05-15 18:07:26 -0700879
Robert Greenwalt6078b502014-06-11 16:05:07 -0700880 NetworkCallback networkCallback = removeRequestForFeature(netCap);
881 if (networkCallback != null) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700882 Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature);
Robert Greenwalt6078b502014-06-11 16:05:07 -0700883 unregisterNetworkCallback(networkCallback);
Robert Greenwalt562cc542014-05-15 18:07:26 -0700884 }
885 return 1;
886 }
887
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900888 /**
889 * Removes the NET_CAPABILITY_NOT_RESTRICTED capability from the given
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900890 * NetworkCapabilities object if all the capabilities it provides are
891 * typically provided by restricted networks.
892 *
893 * TODO: consider:
894 * - Moving to NetworkCapabilities
895 * - Renaming it to guessRestrictedCapability and make it set the
896 * restricted capability bit in addition to clearing it.
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900897 * @hide
898 */
899 public static void maybeMarkCapabilitiesRestricted(NetworkCapabilities nc) {
Robert Greenwalt7569f182014-06-08 16:42:59 -0700900 for (int capability : nc.getCapabilities()) {
901 switch (capability) {
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900902 case NetworkCapabilities.NET_CAPABILITY_CBS:
903 case NetworkCapabilities.NET_CAPABILITY_DUN:
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900904 case NetworkCapabilities.NET_CAPABILITY_EIMS:
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900905 case NetworkCapabilities.NET_CAPABILITY_FOTA:
906 case NetworkCapabilities.NET_CAPABILITY_IA:
907 case NetworkCapabilities.NET_CAPABILITY_IMS:
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900908 case NetworkCapabilities.NET_CAPABILITY_RCS:
909 case NetworkCapabilities.NET_CAPABILITY_XCAP:
Robert Greenwalt9ac3dbe2014-06-05 16:39:28 -0700910 case NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED: //there by default
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900911 continue;
912 default:
913 // At least one capability usually provided by unrestricted
914 // networks. Conclude that this network is unrestricted.
915 return;
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900916 }
917 }
Lorenzo Colittiea1984f2014-06-04 19:59:21 +0900918 // All the capabilities are typically provided by restricted networks.
919 // Conclude that this network is restricted.
Robert Greenwalt7569f182014-06-08 16:42:59 -0700920 nc.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED);
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900921 }
922
Robert Greenwalt562cc542014-05-15 18:07:26 -0700923 private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) {
924 if (networkType == TYPE_MOBILE) {
925 int cap = -1;
926 if ("enableMMS".equals(feature)) {
927 cap = NetworkCapabilities.NET_CAPABILITY_MMS;
928 } else if ("enableSUPL".equals(feature)) {
929 cap = NetworkCapabilities.NET_CAPABILITY_SUPL;
930 } else if ("enableDUN".equals(feature) || "enableDUNAlways".equals(feature)) {
931 cap = NetworkCapabilities.NET_CAPABILITY_DUN;
932 } else if ("enableHIPRI".equals(feature)) {
933 cap = NetworkCapabilities.NET_CAPABILITY_INTERNET;
934 } else if ("enableFOTA".equals(feature)) {
935 cap = NetworkCapabilities.NET_CAPABILITY_FOTA;
936 } else if ("enableIMS".equals(feature)) {
937 cap = NetworkCapabilities.NET_CAPABILITY_IMS;
938 } else if ("enableCBS".equals(feature)) {
939 cap = NetworkCapabilities.NET_CAPABILITY_CBS;
940 } else {
941 return null;
942 }
943 NetworkCapabilities netCap = new NetworkCapabilities();
Robert Greenwalt7569f182014-06-08 16:42:59 -0700944 netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(cap);
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900945 maybeMarkCapabilitiesRestricted(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -0700946 return netCap;
947 } else if (networkType == TYPE_WIFI) {
948 if ("p2p".equals(feature)) {
949 NetworkCapabilities netCap = new NetworkCapabilities();
950 netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
Robert Greenwalt7569f182014-06-08 16:42:59 -0700951 netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P);
Lorenzo Colitti4077acb2014-06-04 12:20:06 +0900952 maybeMarkCapabilitiesRestricted(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -0700953 return netCap;
954 }
955 }
956 return null;
957 }
958
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700959 private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700960 if (netCap == null) return TYPE_NONE;
961 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) {
962 return TYPE_MOBILE_CBS;
963 }
964 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) {
965 return TYPE_MOBILE_IMS;
966 }
967 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) {
968 return TYPE_MOBILE_FOTA;
969 }
970 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) {
971 return TYPE_MOBILE_DUN;
972 }
973 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) {
974 return TYPE_MOBILE_SUPL;
975 }
976 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) {
977 return TYPE_MOBILE_MMS;
978 }
979 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
980 return TYPE_MOBILE_HIPRI;
981 }
Robert Greenwalt32aa65a2014-06-02 15:32:02 -0700982 if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) {
983 return TYPE_WIFI_P2P;
984 }
Robert Greenwalt562cc542014-05-15 18:07:26 -0700985 return TYPE_NONE;
986 }
987
988 private static class LegacyRequest {
989 NetworkCapabilities networkCapabilities;
990 NetworkRequest networkRequest;
991 int expireSequenceNumber;
992 Network currentNetwork;
993 int delay = -1;
Robert Greenwalt6078b502014-06-11 16:05:07 -0700994 NetworkCallback networkCallback = new NetworkCallback() {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700995 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -0700996 public void onAvailable(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -0700997 currentNetwork = network;
998 Log.d(TAG, "startUsingNetworkFeature got Network:" + network);
Paul Jensen6d3ff9e2014-05-29 10:12:39 -0400999 setProcessDefaultNetworkForHostResolution(network);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001000 }
1001 @Override
Robert Greenwalt6078b502014-06-11 16:05:07 -07001002 public void onLost(Network network) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001003 if (network.equals(currentNetwork)) {
1004 currentNetwork = null;
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001005 setProcessDefaultNetworkForHostResolution(null);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001006 }
1007 Log.d(TAG, "startUsingNetworkFeature lost Network:" + network);
1008 }
1009 };
1010 }
1011
1012 private HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests =
1013 new HashMap<NetworkCapabilities, LegacyRequest>();
1014
1015 private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) {
1016 synchronized (sLegacyRequests) {
1017 LegacyRequest l = sLegacyRequests.get(netCap);
1018 if (l != null) return l.networkRequest;
1019 }
1020 return null;
1021 }
1022
1023 private void renewRequestLocked(LegacyRequest l) {
1024 l.expireSequenceNumber++;
1025 Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber);
1026 sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay);
1027 }
1028
1029 private void expireRequest(NetworkCapabilities netCap, int sequenceNum) {
1030 int ourSeqNum = -1;
1031 synchronized (sLegacyRequests) {
1032 LegacyRequest l = sLegacyRequests.get(netCap);
1033 if (l == null) return;
1034 ourSeqNum = l.expireSequenceNumber;
1035 if (l.expireSequenceNumber == sequenceNum) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07001036 unregisterNetworkCallback(l.networkCallback);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001037 sLegacyRequests.remove(netCap);
1038 }
1039 }
1040 Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum);
1041 }
1042
1043 private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) {
1044 int delay = -1;
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001045 int type = legacyTypeForNetworkCapabilities(netCap);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001046 try {
1047 delay = mService.getRestoreDefaultNetworkDelay(type);
1048 } catch (RemoteException e) {}
1049 LegacyRequest l = new LegacyRequest();
1050 l.networkCapabilities = netCap;
1051 l.delay = delay;
1052 l.expireSequenceNumber = 0;
Robert Greenwalt6078b502014-06-11 16:05:07 -07001053 l.networkRequest = sendRequestForNetwork(netCap, l.networkCallback, 0,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07001054 REQUEST, type);
Robert Greenwalt562cc542014-05-15 18:07:26 -07001055 if (l.networkRequest == null) return null;
1056 sLegacyRequests.put(netCap, l);
1057 sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay);
1058 return l.networkRequest;
1059 }
1060
1061 private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) {
1062 if (delay >= 0) {
1063 Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay);
1064 Message msg = sCallbackHandler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap);
1065 sCallbackHandler.sendMessageDelayed(msg, delay);
1066 }
1067 }
1068
Robert Greenwalt6078b502014-06-11 16:05:07 -07001069 private NetworkCallback removeRequestForFeature(NetworkCapabilities netCap) {
Robert Greenwalt562cc542014-05-15 18:07:26 -07001070 synchronized (sLegacyRequests) {
1071 LegacyRequest l = sLegacyRequests.remove(netCap);
1072 if (l == null) return null;
Robert Greenwalt6078b502014-06-11 16:05:07 -07001073 return l.networkCallback;
Robert Greenwalt562cc542014-05-15 18:07:26 -07001074 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001075 }
1076
1077 /**
1078 * Ensure that a network route exists to deliver traffic to the specified
1079 * host via the specified network interface. An attempt to add a route that
1080 * already exists is ignored, but treated as successful.
Nicolas Falliere9530e3a2012-06-18 17:21:06 -07001081 * <p>This method requires the caller to hold the permission
1082 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001083 * @param networkType the type of the network over which traffic to the specified
1084 * host is to be routed
1085 * @param hostAddress the IP address of the host to which the route is desired
1086 * @return {@code true} on success, {@code false} on failure
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001087 *
1088 * @deprecated Deprecated in favor of the {@link #requestNetwork},
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001089 * {@link #setProcessDefaultNetwork} and {@link Network#getSocketFactory} api.
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001090 */
1091 public boolean requestRouteToHost(int networkType, int hostAddress) {
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001092 InetAddress inetAddress = NetworkUtils.intToInetAddress(hostAddress);
1093
1094 if (inetAddress == null) {
1095 return false;
1096 }
1097
1098 return requestRouteToHostAddress(networkType, inetAddress);
1099 }
1100
1101 /**
1102 * Ensure that a network route exists to deliver traffic to the specified
1103 * host via the specified network interface. An attempt to add a route that
1104 * already exists is ignored, but treated as successful.
Jake Hamby8f9b33e2014-01-15 13:08:03 -08001105 * <p>This method requires the caller to hold the permission
1106 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001107 * @param networkType the type of the network over which traffic to the specified
1108 * host is to be routed
1109 * @param hostAddress the IP address of the host to which the route is desired
1110 * @return {@code true} on success, {@code false} on failure
1111 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001112 * @deprecated Deprecated in favor of the {@link #requestNetwork} and
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04001113 * {@link #setProcessDefaultNetwork} api.
Robert Greenwalt585ac0f2010-08-27 09:24:29 -07001114 */
1115 public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) {
1116 byte[] address = hostAddress.getAddress();
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001117 try {
Chad Brubakerf81daa92014-02-14 13:22:34 -08001118 return mService.requestRouteToHostAddress(networkType, address, mPackageName);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001119 } catch (RemoteException e) {
1120 return false;
1121 }
1122 }
1123
1124 /**
1125 * Returns the value of the setting for background data usage. If false,
1126 * applications should not use the network if the application is not in the
1127 * foreground. Developers should respect this setting, and check the value
1128 * of this before performing any background data operations.
1129 * <p>
1130 * All applications that have background services that use the network
1131 * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}.
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001132 * <p>
Scott Main4cc53332011-10-06 18:32:43 -07001133 * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001134 * background data depends on several combined factors, and this method will
1135 * always return {@code true}. Instead, when background data is unavailable,
1136 * {@link #getActiveNetworkInfo()} will now appear disconnected.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001137 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001138 * @return Whether background data usage is allowed.
1139 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001140 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001141 public boolean getBackgroundDataSetting() {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001142 // assume that background data is allowed; final authority is
1143 // NetworkInfo which may be blocked.
1144 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001145 }
1146
1147 /**
1148 * Sets the value of the setting for background data usage.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001149 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001150 * @param allowBackgroundData Whether an application should use data while
1151 * it is in the background.
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001152 *
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001153 * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING
1154 * @see #getBackgroundDataSetting()
1155 * @hide
1156 */
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001157 @Deprecated
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001158 public void setBackgroundDataSetting(boolean allowBackgroundData) {
Jeff Sharkey3a844fc2011-08-16 14:37:57 -07001159 // ignored
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001160 }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001161
1162 /**
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001163 * Return quota status for the current active network, or {@code null} if no
1164 * network is active. Quota status can change rapidly, so these values
1165 * shouldn't be cached.
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001166 *
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001167 * <p>This method requires the call to hold the permission
1168 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
1169 *
Jeff Sharkey44a3e0d2011-10-06 10:50:09 -07001170 * @hide
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001171 */
1172 public NetworkQuotaInfo getActiveNetworkQuotaInfo() {
1173 try {
1174 return mService.getActiveNetworkQuotaInfo();
1175 } catch (RemoteException e) {
1176 return null;
1177 }
1178 }
1179
1180 /**
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001181 * @hide
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001182 * @deprecated Talk to TelephonyManager directly
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001183 */
1184 public boolean getMobileDataEnabled() {
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001185 IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE);
1186 if (b != null) {
1187 try {
1188 ITelephony it = ITelephony.Stub.asInterface(b);
1189 return it.getDataEnabled();
1190 } catch (RemoteException e) { }
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001191 }
Robert Greenwaltafa05c02014-05-21 20:04:36 -07001192 return false;
Robert Greenwaltc03fa502010-02-23 18:58:05 -08001193 }
1194
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001195 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001196 * Callback for use with {@link ConnectivityManager#registerDefaultNetworkActiveListener}
1197 * to find out when the system default network has gone in to a high power state.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001198 */
1199 public interface OnNetworkActiveListener {
1200 /**
1201 * Called on the main thread of the process to report that the current data network
1202 * has become active, and it is now a good time to perform any pending network
1203 * operations. Note that this listener only tells you when the network becomes
1204 * active; if at any other time you want to know whether it is active (and thus okay
1205 * to initiate network traffic), you can retrieve its instantaneous state with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001206 * {@link ConnectivityManager#isDefaultNetworkActive}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001207 */
1208 public void onNetworkActive();
1209 }
1210
1211 private INetworkManagementService getNetworkManagementService() {
1212 synchronized (this) {
1213 if (mNMService != null) {
1214 return mNMService;
1215 }
1216 IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1217 mNMService = INetworkManagementService.Stub.asInterface(b);
1218 return mNMService;
1219 }
1220 }
1221
1222 private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener>
1223 mNetworkActivityListeners
1224 = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>();
1225
1226 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07001227 * Start listening to reports when the system's default data network is active, meaning it is
1228 * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()}
1229 * to determine the current state of the system's default network after registering the
1230 * listener.
1231 * <p>
1232 * If the process default network has been set with
1233 * {@link ConnectivityManager#setProcessDefaultNetwork} this function will not
1234 * reflect the process's default, but the system default.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001235 *
1236 * @param l The listener to be told when the network is active.
1237 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001238 public void registerDefaultNetworkActiveListener(final OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001239 INetworkActivityListener rl = new INetworkActivityListener.Stub() {
1240 @Override
1241 public void onNetworkActive() throws RemoteException {
1242 l.onNetworkActive();
1243 }
1244 };
1245
1246 try {
1247 getNetworkManagementService().registerNetworkActivityListener(rl);
1248 mNetworkActivityListeners.put(l, rl);
1249 } catch (RemoteException e) {
1250 }
1251 }
1252
1253 /**
1254 * Remove network active listener previously registered with
Robert Greenwalt6078b502014-06-11 16:05:07 -07001255 * {@link #registerDefaultNetworkActiveListener}.
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001256 *
1257 * @param l Previously registered listener.
1258 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001259 public void unregisterDefaultNetworkActiveListener(OnNetworkActiveListener l) {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001260 INetworkActivityListener rl = mNetworkActivityListeners.get(l);
1261 if (rl == null) {
1262 throw new IllegalArgumentException("Listener not registered: " + l);
1263 }
1264 try {
1265 getNetworkManagementService().unregisterNetworkActivityListener(rl);
1266 } catch (RemoteException e) {
1267 }
1268 }
1269
1270 /**
1271 * Return whether the data network is currently active. An active network means that
1272 * it is currently in a high power state for performing data transmission. On some
1273 * types of networks, it may be expensive to move and stay in such a state, so it is
1274 * more power efficient to batch network traffic together when the radio is already in
1275 * this state. This method tells you whether right now is currently a good time to
1276 * initiate network traffic, as the network is already active.
1277 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001278 public boolean isDefaultNetworkActive() {
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001279 try {
1280 return getNetworkManagementService().isNetworkActive();
1281 } catch (RemoteException e) {
1282 }
1283 return false;
1284 }
1285
1286 /**
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001287 * {@hide}
1288 */
Chad Brubakerf81daa92014-02-14 13:22:34 -08001289 public ConnectivityManager(IConnectivityManager service, String packageName) {
Jeff Sharkeyf0ceede2011-08-02 17:22:34 -07001290 mService = checkNotNull(service, "missing IConnectivityManager");
Chad Brubakerf81daa92014-02-14 13:22:34 -08001291 mPackageName = checkNotNull(packageName, "missing package name");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001292 }
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001293
Jeff Sharkey8fc27e82012-04-04 20:40:58 -07001294 /** {@hide} */
1295 public static ConnectivityManager from(Context context) {
1296 return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
1297 }
1298
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001299 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001300 * Get the set of tetherable, available interfaces. This list is limited by
1301 * device configuration and current interface existence.
1302 *
1303 * @return an array of 0 or more Strings of tetherable interface names.
1304 *
1305 * <p>This method requires the call to hold the permission
1306 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001307 * {@hide}
1308 */
1309 public String[] getTetherableIfaces() {
1310 try {
1311 return mService.getTetherableIfaces();
1312 } catch (RemoteException e) {
1313 return new String[0];
1314 }
1315 }
1316
1317 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001318 * Get the set of tethered interfaces.
1319 *
1320 * @return an array of 0 or more String of currently tethered interface names.
1321 *
1322 * <p>This method requires the call to hold the permission
1323 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001324 * {@hide}
1325 */
1326 public String[] getTetheredIfaces() {
1327 try {
1328 return mService.getTetheredIfaces();
1329 } catch (RemoteException e) {
1330 return new String[0];
1331 }
1332 }
1333
1334 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001335 * Get the set of interface names which attempted to tether but
1336 * failed. Re-attempting to tether may cause them to reset to the Tethered
1337 * state. Alternatively, causing the interface to be destroyed and recreated
1338 * may cause them to reset to the available state.
1339 * {@link ConnectivityManager#getLastTetherError} can be used to get more
1340 * information on the cause of the errors.
1341 *
1342 * @return an array of 0 or more String indicating the interface names
1343 * which failed to tether.
1344 *
1345 * <p>This method requires the call to hold the permission
1346 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001347 * {@hide}
1348 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001349 public String[] getTetheringErroredIfaces() {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001350 try {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001351 return mService.getTetheringErroredIfaces();
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001352 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001353 return new String[0];
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001354 }
1355 }
1356
1357 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001358 * Attempt to tether the named interface. This will setup a dhcp server
1359 * on the interface, forward and NAT IP packets and forward DNS requests
1360 * to the best active upstream network interface. Note that if no upstream
1361 * IP network interface is available, dhcp will still run and traffic will be
1362 * allowed between the tethered devices and this device, though upstream net
1363 * access will of course fail until an upstream network interface becomes
1364 * active.
1365 *
1366 * @param iface the interface name to tether.
1367 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1368 *
1369 * <p>This method requires the call to hold the permission
1370 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001371 * {@hide}
1372 */
Robert Greenwalt5a735062010-03-02 17:25:02 -08001373 public int tether(String iface) {
1374 try {
1375 return mService.tether(iface);
1376 } catch (RemoteException e) {
1377 return TETHER_ERROR_SERVICE_UNAVAIL;
1378 }
1379 }
1380
1381 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001382 * Stop tethering the named interface.
1383 *
1384 * @param iface the interface name to untether.
1385 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1386 *
1387 * <p>This method requires the call to hold the permission
1388 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Robert Greenwalt5a735062010-03-02 17:25:02 -08001389 * {@hide}
1390 */
1391 public int untether(String iface) {
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001392 try {
1393 return mService.untether(iface);
1394 } catch (RemoteException e) {
Robert Greenwalt5a735062010-03-02 17:25:02 -08001395 return TETHER_ERROR_SERVICE_UNAVAIL;
Robert Greenwaltd0e18ff2010-01-26 11:40:34 -08001396 }
1397 }
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001398
1399 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001400 * Check if the device allows for tethering. It may be disabled via
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001401 * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001402 * due to device configuration.
1403 *
1404 * @return a boolean - {@code true} indicating Tethering is supported.
1405 *
1406 * <p>This method requires the call to hold the permission
1407 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001408 * {@hide}
1409 */
1410 public boolean isTetheringSupported() {
1411 try {
1412 return mService.isTetheringSupported();
1413 } catch (RemoteException e) {
1414 return false;
1415 }
1416 }
1417
1418 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001419 * Get the list of regular expressions that define any tetherable
1420 * USB network interfaces. If USB tethering is not supported by the
1421 * device, this list should be empty.
1422 *
1423 * @return an array of 0 or more regular expression Strings defining
1424 * what interfaces are considered tetherable usb interfaces.
1425 *
1426 * <p>This method requires the call to hold the permission
1427 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001428 * {@hide}
1429 */
1430 public String[] getTetherableUsbRegexs() {
1431 try {
1432 return mService.getTetherableUsbRegexs();
1433 } catch (RemoteException e) {
1434 return new String[0];
1435 }
1436 }
1437
1438 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001439 * Get the list of regular expressions that define any tetherable
1440 * Wifi network interfaces. If Wifi tethering is not supported by the
1441 * device, this list should be empty.
1442 *
1443 * @return an array of 0 or more regular expression Strings defining
1444 * what interfaces are considered tetherable wifi interfaces.
1445 *
1446 * <p>This method requires the call to hold the permission
1447 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt2a091d72010-02-11 18:18:40 -08001448 * {@hide}
1449 */
1450 public String[] getTetherableWifiRegexs() {
1451 try {
1452 return mService.getTetherableWifiRegexs();
1453 } catch (RemoteException e) {
1454 return new String[0];
1455 }
1456 }
Robert Greenwalt5a735062010-03-02 17:25:02 -08001457
Danica Chang6fdd0c62010-08-11 14:54:43 -07001458 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001459 * Get the list of regular expressions that define any tetherable
1460 * Bluetooth network interfaces. If Bluetooth tethering is not supported by the
1461 * device, this list should be empty.
1462 *
1463 * @return an array of 0 or more regular expression Strings defining
1464 * what interfaces are considered tetherable bluetooth interfaces.
1465 *
1466 * <p>This method requires the call to hold the permission
1467 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Danica Chang6fdd0c62010-08-11 14:54:43 -07001468 * {@hide}
1469 */
1470 public String[] getTetherableBluetoothRegexs() {
1471 try {
1472 return mService.getTetherableBluetoothRegexs();
1473 } catch (RemoteException e) {
1474 return new String[0];
1475 }
1476 }
1477
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001478 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001479 * Attempt to both alter the mode of USB and Tethering of USB. A
1480 * utility method to deal with some of the complexity of USB - will
1481 * attempt to switch to Rndis and subsequently tether the resulting
1482 * interface on {@code true} or turn off tethering and switch off
1483 * Rndis on {@code false}.
1484 *
1485 * @param enable a boolean - {@code true} to enable tethering
1486 * @return error a {@code TETHER_ERROR} value indicating success or failure type
1487 *
1488 * <p>This method requires the call to hold the permission
1489 * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}.
Mike Lockwood6c2260b2011-07-19 13:04:47 -07001490 * {@hide}
1491 */
1492 public int setUsbTethering(boolean enable) {
1493 try {
1494 return mService.setUsbTethering(enable);
1495 } catch (RemoteException e) {
1496 return TETHER_ERROR_SERVICE_UNAVAIL;
1497 }
1498 }
1499
Robert Greenwalt5a735062010-03-02 17:25:02 -08001500 /** {@hide} */
1501 public static final int TETHER_ERROR_NO_ERROR = 0;
1502 /** {@hide} */
1503 public static final int TETHER_ERROR_UNKNOWN_IFACE = 1;
1504 /** {@hide} */
1505 public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2;
1506 /** {@hide} */
1507 public static final int TETHER_ERROR_UNSUPPORTED = 3;
1508 /** {@hide} */
1509 public static final int TETHER_ERROR_UNAVAIL_IFACE = 4;
1510 /** {@hide} */
1511 public static final int TETHER_ERROR_MASTER_ERROR = 5;
1512 /** {@hide} */
1513 public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6;
1514 /** {@hide} */
1515 public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7;
1516 /** {@hide} */
1517 public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8;
1518 /** {@hide} */
1519 public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9;
1520 /** {@hide} */
1521 public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10;
1522
1523 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001524 * Get a more detailed error code after a Tethering or Untethering
1525 * request asynchronously failed.
1526 *
1527 * @param iface The name of the interface of interest
Robert Greenwalt5a735062010-03-02 17:25:02 -08001528 * @return error The error code of the last error tethering or untethering the named
1529 * interface
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001530 *
1531 * <p>This method requires the call to hold the permission
1532 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt5a735062010-03-02 17:25:02 -08001533 * {@hide}
1534 */
1535 public int getLastTetherError(String iface) {
1536 try {
1537 return mService.getLastTetherError(iface);
1538 } catch (RemoteException e) {
1539 return TETHER_ERROR_SERVICE_UNAVAIL;
1540 }
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001541 }
1542
1543 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001544 * Try to ensure the device stays awake until we connect with the next network.
1545 * Actually just holds a wakelock for a number of seconds while we try to connect
1546 * to any default networks. This will expire if the timeout passes or if we connect
1547 * to a default after this is called. For internal use only.
1548 *
1549 * @param forWhom the name of the network going down for logging purposes
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001550 * @return {@code true} on success, {@code false} on failure
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001551 *
1552 * <p>This method requires the call to hold the permission
1553 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Robert Greenwalt14f2ef42010-06-15 12:19:37 -07001554 * {@hide}
1555 */
1556 public boolean requestNetworkTransitionWakelock(String forWhom) {
1557 try {
1558 mService.requestNetworkTransitionWakelock(forWhom);
1559 return true;
1560 } catch (RemoteException e) {
1561 return false;
1562 }
1563 }
Robert Greenwaltca4306c2010-09-09 13:15:32 -07001564
Robert Greenwalt67fd6c92010-09-09 14:22:59 -07001565 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001566 * Report network connectivity status. This is currently used only
1567 * to alter status bar UI.
1568 *
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001569 * @param networkType The type of network you want to report on
1570 * @param percentage The quality of the connection 0 is bad, 100 is good
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001571 *
1572 * <p>This method requires the call to hold the permission
1573 * {@link android.Manifest.permission#STATUS_BAR}.
Robert Greenwaltd7085fc2010-09-08 15:24:47 -07001574 * {@hide}
1575 */
1576 public void reportInetCondition(int networkType, int percentage) {
1577 try {
1578 mService.reportInetCondition(networkType, percentage);
1579 } catch (RemoteException e) {
1580 }
1581 }
Robert Greenwalt434203a2010-10-11 16:00:27 -07001582
1583 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001584 * Report a problem network to the framework. This provides a hint to the system
1585 * that there might be connectivity problems on this network and may cause
1586 * the framework to re-evaluate network connectivity and/or switch to another
1587 * network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001588 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001589 * @param network The {@link Network} the application was attempting to use
1590 * or {@code null} to indicate the current default network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001591 */
1592 public void reportBadNetwork(Network network) {
1593 try {
1594 mService.reportBadNetwork(network);
1595 } catch (RemoteException e) {
1596 }
1597 }
1598
1599 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001600 * Set a network-independent global http proxy. This is not normally what you want
1601 * for typical HTTP proxies - they are general network dependent. However if you're
1602 * doing something unusual like general internal filtering this may be useful. On
1603 * a private network where the proxy is not accessible, you may break HTTP using this.
1604 *
Jason Monk207900c2014-04-25 15:00:09 -04001605 * @param p The a {@link ProxyInfo} object defining the new global
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001606 * HTTP proxy. A {@code null} value will clear the global HTTP proxy.
1607 *
1608 * <p>This method requires the call to hold the permission
Jason Monkfaf3fd52014-05-07 18:41:13 -04001609 * android.Manifest.permission#CONNECTIVITY_INTERNAL.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001610 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07001611 */
Jason Monk207900c2014-04-25 15:00:09 -04001612 public void setGlobalProxy(ProxyInfo p) {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001613 try {
1614 mService.setGlobalProxy(p);
1615 } catch (RemoteException e) {
1616 }
1617 }
1618
1619 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001620 * Retrieve any network-independent global HTTP proxy.
1621 *
Jason Monk207900c2014-04-25 15:00:09 -04001622 * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null}
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001623 * if no global HTTP proxy is set.
1624 *
1625 * <p>This method requires the call to hold the permission
1626 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001627 * @hide
Robert Greenwalt434203a2010-10-11 16:00:27 -07001628 */
Jason Monk207900c2014-04-25 15:00:09 -04001629 public ProxyInfo getGlobalProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001630 try {
1631 return mService.getGlobalProxy();
1632 } catch (RemoteException e) {
1633 return null;
1634 }
1635 }
1636
1637 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001638 * Get the HTTP proxy settings for the current default network. Note that
1639 * if a global proxy is set, it will override any per-network setting.
1640 *
Jason Monk207900c2014-04-25 15:00:09 -04001641 * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001642 * HTTP proxy is active.
1643 *
1644 * <p>This method requires the call to hold the permission
1645 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt434203a2010-10-11 16:00:27 -07001646 * {@hide}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001647 * @deprecated Deprecated in favor of {@link #getLinkProperties}
Robert Greenwalt434203a2010-10-11 16:00:27 -07001648 */
Jason Monk207900c2014-04-25 15:00:09 -04001649 public ProxyInfo getProxy() {
Robert Greenwalt434203a2010-10-11 16:00:27 -07001650 try {
1651 return mService.getProxy();
1652 } catch (RemoteException e) {
1653 return null;
1654 }
1655 }
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001656
1657 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001658 * Sets a secondary requirement bit for the given networkType.
1659 * This requirement bit is generally under the control of the carrier
1660 * or its agents and is not directly controlled by the user.
1661 *
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001662 * @param networkType The network who's dependence has changed
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001663 * @param met Boolean - true if network use is OK, false if not
1664 *
1665 * <p>This method requires the call to hold the permission
1666 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
Robert Greenwaltd55a6b42011-03-25 13:09:25 -07001667 * {@hide}
1668 */
1669 public void setDataDependency(int networkType, boolean met) {
1670 try {
1671 mService.setDataDependency(networkType, met);
1672 } catch (RemoteException e) {
1673 }
1674 }
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07001675
1676 /**
1677 * Returns true if the hardware supports the given network type
1678 * else it returns false. This doesn't indicate we have coverage
1679 * or are authorized onto a network, just whether or not the
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001680 * hardware supports it. For example a GSM phone without a SIM
1681 * should still return {@code true} for mobile data, but a wifi only
1682 * tablet would return {@code false}.
1683 *
1684 * @param networkType The network type we'd like to check
1685 * @return {@code true} if supported, else {@code false}
1686 *
1687 * <p>This method requires the call to hold the permission
1688 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Robert Greenwalt9b2886e2011-08-31 11:46:42 -07001689 * @hide
1690 */
1691 public boolean isNetworkSupported(int networkType) {
1692 try {
1693 return mService.isNetworkSupported(networkType);
1694 } catch (RemoteException e) {}
1695 return false;
1696 }
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07001697
1698 /**
1699 * Returns if the currently active data network is metered. A network is
1700 * classified as metered when the user is sensitive to heavy data usage on
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001701 * that connection due to monetary costs, data limitations or
1702 * battery/performance issues. You should check this before doing large
1703 * data transfers, and warn the user or delay the operation until another
1704 * network is available.
1705 *
1706 * @return {@code true} if large transfers should be avoided, otherwise
1707 * {@code false}.
1708 *
1709 * <p>This method requires the call to hold the permission
1710 * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}.
Jeff Sharkey9f7cbf02012-04-12 18:34:54 -07001711 */
1712 public boolean isActiveNetworkMetered() {
1713 try {
1714 return mService.isActiveNetworkMetered();
1715 } catch (RemoteException e) {
1716 return false;
1717 }
1718 }
Jeff Sharkey69ddab42012-08-25 00:05:46 -07001719
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001720 /**
1721 * If the LockdownVpn mechanism is enabled, updates the vpn
1722 * with a reload of its profile.
1723 *
1724 * @return a boolean with {@code} indicating success
1725 *
1726 * <p>This method can only be called by the system UID
1727 * {@hide}
1728 */
Jeff Sharkey69ddab42012-08-25 00:05:46 -07001729 public boolean updateLockdownVpn() {
1730 try {
1731 return mService.updateLockdownVpn();
1732 } catch (RemoteException e) {
1733 return false;
1734 }
1735 }
Irfan Sheriffda6da092012-08-16 12:49:23 -07001736
1737 /**
Robert Greenwalt5a6bdc42013-02-15 10:56:35 -08001738 * Signal that the captive portal check on the indicated network
Wink Savilled747cbc2013-08-07 16:22:47 -07001739 * is complete and whether its a captive portal or not.
1740 *
1741 * @param info the {@link NetworkInfo} object for the networkType
1742 * in question.
1743 * @param isCaptivePortal true/false.
1744 *
1745 * <p>This method requires the call to hold the permission
1746 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
1747 * {@hide}
1748 */
1749 public void captivePortalCheckCompleted(NetworkInfo info, boolean isCaptivePortal) {
1750 try {
1751 mService.captivePortalCheckCompleted(info, isCaptivePortal);
1752 } catch (RemoteException e) {
1753 }
1754 }
1755
1756 /**
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001757 * Supply the backend messenger for a network tracker
1758 *
Dianne Hackborn77b987f2014-02-26 16:20:52 -08001759 * @param networkType NetworkType to set
Robert Greenwalt665e1ae2012-08-21 19:27:00 -07001760 * @param messenger {@link Messenger}
1761 * {@hide}
1762 */
1763 public void supplyMessenger(int networkType, Messenger messenger) {
1764 try {
1765 mService.supplyMessenger(networkType, messenger);
1766 } catch (RemoteException e) {
1767 }
1768 }
Wink Savilleab9321d2013-06-29 21:10:57 -07001769
1770 /**
Wink Saville948282b2013-08-29 08:55:16 -07001771 * Check mobile provisioning.
Wink Savilleab9321d2013-06-29 21:10:57 -07001772 *
Wink Savilleab9321d2013-06-29 21:10:57 -07001773 * @param suggestedTimeOutMs, timeout in milliseconds
Wink Savilleab9321d2013-06-29 21:10:57 -07001774 *
1775 * @return time out that will be used, maybe less that suggestedTimeOutMs
1776 * -1 if an error.
1777 *
1778 * {@hide}
1779 */
Wink Saville948282b2013-08-29 08:55:16 -07001780 public int checkMobileProvisioning(int suggestedTimeOutMs) {
Wink Savilleab9321d2013-06-29 21:10:57 -07001781 int timeOutMs = -1;
1782 try {
Wink Saville948282b2013-08-29 08:55:16 -07001783 timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs);
Wink Savilleab9321d2013-06-29 21:10:57 -07001784 } catch (RemoteException e) {
1785 }
1786 return timeOutMs;
1787 }
Robert Greenwalte182bfe2013-07-16 12:06:09 -07001788
1789 /**
Wink Saville42d4f082013-07-20 20:31:59 -07001790 * Get the mobile provisioning url.
Robert Greenwalte182bfe2013-07-16 12:06:09 -07001791 * {@hide}
1792 */
1793 public String getMobileProvisioningUrl() {
1794 try {
1795 return mService.getMobileProvisioningUrl();
1796 } catch (RemoteException e) {
1797 }
1798 return null;
1799 }
Wink Saville42d4f082013-07-20 20:31:59 -07001800
1801 /**
1802 * Get the mobile redirected provisioning url.
1803 * {@hide}
1804 */
1805 public String getMobileRedirectedProvisioningUrl() {
1806 try {
1807 return mService.getMobileRedirectedProvisioningUrl();
1808 } catch (RemoteException e) {
1809 }
1810 return null;
1811 }
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001812
1813 /**
1814 * get the information about a specific network link
1815 * @hide
1816 */
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001817 public LinkQualityInfo getLinkQualityInfo(int networkType) {
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001818 try {
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001819 LinkQualityInfo li = mService.getLinkQualityInfo(networkType);
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001820 return li;
1821 } catch (RemoteException e) {
1822 return null;
1823 }
1824 }
1825
1826 /**
1827 * get the information of currently active network link
1828 * @hide
1829 */
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001830 public LinkQualityInfo getActiveLinkQualityInfo() {
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001831 try {
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001832 LinkQualityInfo li = mService.getActiveLinkQualityInfo();
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001833 return li;
1834 } catch (RemoteException e) {
1835 return null;
1836 }
1837 }
1838
1839 /**
1840 * get the information of all network links
1841 * @hide
1842 */
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001843 public LinkQualityInfo[] getAllLinkQualityInfo() {
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001844 try {
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -07001845 LinkQualityInfo[] li = mService.getAllLinkQualityInfo();
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -07001846 return li;
1847 } catch (RemoteException e) {
1848 return null;
1849 }
1850 }
Wink Saville7788c612013-08-29 14:57:08 -07001851
1852 /**
Wink Saville948282b2013-08-29 08:55:16 -07001853 * Set sign in error notification to visible or in visible
1854 *
1855 * @param visible
1856 * @param networkType
1857 *
1858 * {@hide}
1859 */
1860 public void setProvisioningNotificationVisible(boolean visible, int networkType,
1861 String extraInfo, String url) {
1862 try {
1863 mService.setProvisioningNotificationVisible(visible, networkType, extraInfo, url);
1864 } catch (RemoteException e) {
1865 }
1866 }
Yuhao Zheng5cd1a0e2013-09-09 17:00:04 -07001867
1868 /**
1869 * Set the value for enabling/disabling airplane mode
1870 *
1871 * @param enable whether to enable airplane mode or not
1872 *
1873 * <p>This method requires the call to hold the permission
1874 * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}.
1875 * @hide
1876 */
1877 public void setAirplaneMode(boolean enable) {
1878 try {
1879 mService.setAirplaneMode(enable);
1880 } catch (RemoteException e) {
1881 }
1882 }
Robert Greenwalte049c232014-04-11 15:53:27 -07001883
1884 /** {@hide} */
Robert Greenwalta67be032014-05-16 15:49:14 -07001885 public void registerNetworkFactory(Messenger messenger, String name) {
Robert Greenwalte049c232014-04-11 15:53:27 -07001886 try {
Robert Greenwalta67be032014-05-16 15:49:14 -07001887 mService.registerNetworkFactory(messenger, name);
1888 } catch (RemoteException e) { }
1889 }
1890
1891 /** {@hide} */
1892 public void unregisterNetworkFactory(Messenger messenger) {
1893 try {
1894 mService.unregisterNetworkFactory(messenger);
Robert Greenwalte049c232014-04-11 15:53:27 -07001895 } catch (RemoteException e) { }
1896 }
Robert Greenwalt7b816022014-04-18 15:25:25 -07001897
1898 /** {@hide} */
1899 public void registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp,
1900 NetworkCapabilities nc, int score) {
1901 try {
1902 mService.registerNetworkAgent(messenger, ni, lp, nc, score);
1903 } catch (RemoteException e) { }
1904 }
1905
Robert Greenwalt9258c642014-03-26 16:47:06 -07001906 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001907 * Base class for NetworkRequest callbacks. Used for notifications about network
1908 * changes. Should be extended by applications wanting notifications.
Robert Greenwalt9258c642014-03-26 16:47:06 -07001909 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001910 public static class NetworkCallback {
Robert Greenwalt9258c642014-03-26 16:47:06 -07001911 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001912 public static final int PRECHECK = 1;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001913 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001914 public static final int AVAILABLE = 2;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001915 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001916 public static final int LOSING = 3;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001917 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001918 public static final int LOST = 4;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001919 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001920 public static final int UNAVAIL = 5;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001921 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001922 public static final int CAP_CHANGED = 6;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001923 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001924 public static final int PROP_CHANGED = 7;
Robert Greenwalt9258c642014-03-26 16:47:06 -07001925 /** @hide */
Robert Greenwalt7b816022014-04-18 15:25:25 -07001926 public static final int CANCELED = 8;
1927
1928 /**
1929 * @hide
1930 * Called whenever the framework connects to a network that it may use to
1931 * satisfy this request
1932 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001933 public void onPreCheck(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07001934
1935 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001936 * Called when the framework connects and has declared new network ready for use.
Robert Greenwalt6078b502014-06-11 16:05:07 -07001937 * This callback may be called more than once if the {@link Network} that is
1938 * satisfying the request changes.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001939 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001940 * @param network The {@link Network} of the satisfying network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07001941 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001942 public void onAvailable(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07001943
1944 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001945 * Called when the network is about to be disconnected. Often paired with an
Robert Greenwalt6078b502014-06-11 16:05:07 -07001946 * {@link NetworkCallback#onAvailable} call with the new replacement network
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001947 * for graceful handover. This may not be called if we have a hard loss
1948 * (loss without warning). This may be followed by either a
Robert Greenwalt6078b502014-06-11 16:05:07 -07001949 * {@link NetworkCallback#onLost} call or a
1950 * {@link NetworkCallback#onAvailable} call for this network depending
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001951 * on whether we lose or regain it.
1952 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07001953 * @param network The {@link Network} that is about to be disconnected.
1954 * @param maxMsToLive The time in ms the framework will attempt to keep the
1955 * network connected. Note that the network may suffer a
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001956 * hard loss at any time.
Robert Greenwalt7b816022014-04-18 15:25:25 -07001957 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001958 public void onLosing(Network network, int maxMsToLive) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07001959
1960 /**
1961 * Called when the framework has a hard loss of the network or when the
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001962 * graceful failure ends.
1963 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001964 * @param network The {@link Network} lost.
Robert Greenwalt7b816022014-04-18 15:25:25 -07001965 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001966 public void onLost(Network network) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07001967
1968 /**
1969 * Called if no network is found in the given timeout time. If no timeout is given,
1970 * this will not be called.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001971 * @hide
Robert Greenwalt7b816022014-04-18 15:25:25 -07001972 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001973 public void onUnavailable() {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07001974
1975 /**
1976 * Called when the network the framework connected to for this request
1977 * changes capabilities but still satisfies the stated need.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001978 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001979 * @param network The {@link Network} whose capabilities have changed.
1980 * @param networkCapabilities The new {@link NetworkCapabilities} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07001981 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001982 public void onCapabilitiesChanged(Network network,
Robert Greenwalt7b816022014-04-18 15:25:25 -07001983 NetworkCapabilities networkCapabilities) {}
1984
1985 /**
1986 * Called when the network the framework connected to for this request
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001987 * changes {@link LinkProperties}.
1988 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07001989 * @param network The {@link Network} whose link properties have changed.
1990 * @param linkProperties The new {@link LinkProperties} for this network.
Robert Greenwalt7b816022014-04-18 15:25:25 -07001991 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07001992 public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {}
Robert Greenwalt7b816022014-04-18 15:25:25 -07001993
Robert Greenwalt6078b502014-06-11 16:05:07 -07001994 private NetworkRequest networkRequest;
Robert Greenwalt7b816022014-04-18 15:25:25 -07001995 }
1996
Robert Greenwalt9258c642014-03-26 16:47:06 -07001997 private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER;
1998 /** @hide obj = pair(NetworkRequest, Network) */
1999 public static final int CALLBACK_PRECHECK = BASE + 1;
2000 /** @hide obj = pair(NetworkRequest, Network) */
2001 public static final int CALLBACK_AVAILABLE = BASE + 2;
2002 /** @hide obj = pair(NetworkRequest, Network), arg1 = ttl */
2003 public static final int CALLBACK_LOSING = BASE + 3;
2004 /** @hide obj = pair(NetworkRequest, Network) */
2005 public static final int CALLBACK_LOST = BASE + 4;
2006 /** @hide obj = NetworkRequest */
2007 public static final int CALLBACK_UNAVAIL = BASE + 5;
2008 /** @hide obj = pair(NetworkRequest, Network) */
2009 public static final int CALLBACK_CAP_CHANGED = BASE + 6;
2010 /** @hide obj = pair(NetworkRequest, Network) */
2011 public static final int CALLBACK_IP_CHANGED = BASE + 7;
2012 /** @hide obj = NetworkRequest */
2013 public static final int CALLBACK_RELEASED = BASE + 8;
2014 /** @hide */
2015 public static final int CALLBACK_EXIT = BASE + 9;
Robert Greenwalt562cc542014-05-15 18:07:26 -07002016 /** @hide obj = NetworkCapabilities, arg1 = seq number */
2017 private static final int EXPIRE_LEGACY_REQUEST = BASE + 10;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002018
Robert Greenwalt562cc542014-05-15 18:07:26 -07002019 private class CallbackHandler extends Handler {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002020 private final HashMap<NetworkRequest, NetworkCallback>mCallbackMap;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002021 private final AtomicInteger mRefCount;
2022 private static final String TAG = "ConnectivityManager.CallbackHandler";
2023 private final ConnectivityManager mCm;
2024
Robert Greenwalt6078b502014-06-11 16:05:07 -07002025 CallbackHandler(Looper looper, HashMap<NetworkRequest, NetworkCallback>callbackMap,
Robert Greenwalt9258c642014-03-26 16:47:06 -07002026 AtomicInteger refCount, ConnectivityManager cm) {
2027 super(looper);
2028 mCallbackMap = callbackMap;
2029 mRefCount = refCount;
2030 mCm = cm;
2031 }
2032
2033 @Override
2034 public void handleMessage(Message message) {
2035 Log.d(TAG, "CM callback handler got msg " + message.what);
2036 switch (message.what) {
2037 case CALLBACK_PRECHECK: {
2038 NetworkRequest request = getNetworkRequest(message);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002039 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002040 if (callbacks != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002041 callbacks.onPreCheck(getNetwork(message));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002042 } else {
2043 Log.e(TAG, "callback not found for PRECHECK message");
2044 }
2045 break;
2046 }
2047 case CALLBACK_AVAILABLE: {
2048 NetworkRequest request = getNetworkRequest(message);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002049 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002050 if (callbacks != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002051 callbacks.onAvailable(getNetwork(message));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002052 } else {
2053 Log.e(TAG, "callback not found for AVAILABLE message");
2054 }
2055 break;
2056 }
2057 case CALLBACK_LOSING: {
2058 NetworkRequest request = getNetworkRequest(message);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002059 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002060 if (callbacks != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002061 callbacks.onLosing(getNetwork(message), message.arg1);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002062 } else {
2063 Log.e(TAG, "callback not found for LOSING message");
2064 }
2065 break;
2066 }
2067 case CALLBACK_LOST: {
2068 NetworkRequest request = getNetworkRequest(message);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002069 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002070 if (callbacks != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002071 callbacks.onLost(getNetwork(message));
Robert Greenwalt9258c642014-03-26 16:47:06 -07002072 } else {
2073 Log.e(TAG, "callback not found for LOST message");
2074 }
2075 break;
2076 }
2077 case CALLBACK_UNAVAIL: {
2078 NetworkRequest req = (NetworkRequest)message.obj;
Robert Greenwalt6078b502014-06-11 16:05:07 -07002079 NetworkCallback callbacks = null;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002080 synchronized(mCallbackMap) {
2081 callbacks = mCallbackMap.get(req);
2082 }
2083 if (callbacks != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002084 callbacks.onUnavailable();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002085 } else {
2086 Log.e(TAG, "callback not found for UNAVAIL message");
2087 }
2088 break;
2089 }
2090 case CALLBACK_CAP_CHANGED: {
2091 NetworkRequest request = getNetworkRequest(message);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002092 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002093 if (callbacks != null) {
2094 Network network = getNetwork(message);
2095 NetworkCapabilities cap = mCm.getNetworkCapabilities(network);
2096
Robert Greenwalt6078b502014-06-11 16:05:07 -07002097 callbacks.onCapabilitiesChanged(network, cap);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002098 } else {
2099 Log.e(TAG, "callback not found for CHANGED message");
2100 }
2101 break;
2102 }
2103 case CALLBACK_IP_CHANGED: {
2104 NetworkRequest request = getNetworkRequest(message);
Robert Greenwalt6078b502014-06-11 16:05:07 -07002105 NetworkCallback callbacks = getCallbacks(request);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002106 if (callbacks != null) {
2107 Network network = getNetwork(message);
2108 LinkProperties lp = mCm.getLinkProperties(network);
2109
Robert Greenwalt6078b502014-06-11 16:05:07 -07002110 callbacks.onLinkPropertiesChanged(network, lp);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002111 } else {
2112 Log.e(TAG, "callback not found for CHANGED message");
2113 }
2114 break;
2115 }
2116 case CALLBACK_RELEASED: {
2117 NetworkRequest req = (NetworkRequest)message.obj;
Robert Greenwalt6078b502014-06-11 16:05:07 -07002118 NetworkCallback callbacks = null;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002119 synchronized(mCallbackMap) {
2120 callbacks = mCallbackMap.remove(req);
2121 }
2122 if (callbacks != null) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002123 synchronized(mRefCount) {
2124 if (mRefCount.decrementAndGet() == 0) {
2125 getLooper().quit();
2126 }
2127 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002128 } else {
2129 Log.e(TAG, "callback not found for CANCELED message");
2130 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002131 break;
2132 }
2133 case CALLBACK_EXIT: {
2134 Log.d(TAG, "Listener quiting");
2135 getLooper().quit();
2136 break;
2137 }
Robert Greenwalt562cc542014-05-15 18:07:26 -07002138 case EXPIRE_LEGACY_REQUEST: {
2139 expireRequest((NetworkCapabilities)message.obj, message.arg1);
2140 break;
2141 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002142 }
2143 }
2144
2145 private NetworkRequest getNetworkRequest(Message msg) {
2146 return (NetworkRequest)(msg.obj);
2147 }
Robert Greenwalt6078b502014-06-11 16:05:07 -07002148 private NetworkCallback getCallbacks(NetworkRequest req) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002149 synchronized(mCallbackMap) {
2150 return mCallbackMap.get(req);
2151 }
2152 }
2153 private Network getNetwork(Message msg) {
2154 return new Network(msg.arg2);
2155 }
Robert Greenwalt6078b502014-06-11 16:05:07 -07002156 private NetworkCallback removeCallbacks(Message msg) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002157 NetworkRequest req = (NetworkRequest)msg.obj;
2158 synchronized(mCallbackMap) {
2159 return mCallbackMap.remove(req);
2160 }
2161 }
2162 }
2163
Robert Greenwalt6078b502014-06-11 16:05:07 -07002164 private void incCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002165 synchronized(sCallbackRefCount) {
2166 if (sCallbackRefCount.incrementAndGet() == 1) {
2167 // TODO - switch this over to a ManagerThread or expire it when done
2168 HandlerThread callbackThread = new HandlerThread("ConnectivityManager");
2169 callbackThread.start();
2170 sCallbackHandler = new CallbackHandler(callbackThread.getLooper(),
Robert Greenwalt6078b502014-06-11 16:05:07 -07002171 sNetworkCallback, sCallbackRefCount, this);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002172 }
2173 }
2174 }
2175
Robert Greenwalt6078b502014-06-11 16:05:07 -07002176 private void decCallbackHandlerRefCount() {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002177 synchronized(sCallbackRefCount) {
2178 if (sCallbackRefCount.decrementAndGet() == 0) {
2179 sCallbackHandler.obtainMessage(CALLBACK_EXIT).sendToTarget();
2180 sCallbackHandler = null;
2181 }
2182 }
2183 }
2184
Robert Greenwalt6078b502014-06-11 16:05:07 -07002185 static final HashMap<NetworkRequest, NetworkCallback> sNetworkCallback =
2186 new HashMap<NetworkRequest, NetworkCallback>();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002187 static final AtomicInteger sCallbackRefCount = new AtomicInteger(0);
2188 static CallbackHandler sCallbackHandler = null;
2189
2190 private final static int LISTEN = 1;
2191 private final static int REQUEST = 2;
2192
Robert Greenwalt562cc542014-05-15 18:07:26 -07002193 private NetworkRequest sendRequestForNetwork(NetworkCapabilities need,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002194 NetworkCallback networkCallback, int timeoutSec, int action,
Robert Greenwalt32aa65a2014-06-02 15:32:02 -07002195 int legacyType) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002196 if (networkCallback == null) {
2197 throw new IllegalArgumentException("null NetworkCallback");
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002198 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002199 if (need == null) throw new IllegalArgumentException("null NetworkCapabilities");
2200 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002201 incCallbackHandlerRefCount();
Robert Greenwalt9258c642014-03-26 16:47:06 -07002202 if (action == LISTEN) {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002203 networkCallback.networkRequest = mService.listenForNetwork(need,
2204 new Messenger(sCallbackHandler), new Binder());
Robert Greenwalt9258c642014-03-26 16:47:06 -07002205 } else {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002206 networkCallback.networkRequest = mService.requestNetwork(need,
2207 new Messenger(sCallbackHandler), timeoutSec, new Binder(), legacyType);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002208 }
Robert Greenwalt6078b502014-06-11 16:05:07 -07002209 if (networkCallback.networkRequest != null) {
2210 synchronized(sNetworkCallback) {
2211 sNetworkCallback.put(networkCallback.networkRequest, networkCallback);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002212 }
2213 }
2214 } catch (RemoteException e) {}
Robert Greenwalt6078b502014-06-11 16:05:07 -07002215 if (networkCallback.networkRequest == null) decCallbackHandlerRefCount();
2216 return networkCallback.networkRequest;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002217 }
2218
2219 /**
2220 * Request a network to satisfy a set of {@link NetworkCapabilities}.
2221 *
2222 * This {@link NetworkRequest} will live until released via
Robert Greenwalt6078b502014-06-11 16:05:07 -07002223 * {@link #unregisterNetworkCallback} or the calling application exits.
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002224 * Status of the request can be followed by listening to the various
Robert Greenwalt6078b502014-06-11 16:05:07 -07002225 * callbacks described in {@link NetworkCallback}. The {@link Network}
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002226 * can be used to direct traffic to the network.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002227 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002228 * @param request {@link NetworkRequest} describing this request.
2229 * @param networkCallback The {@link NetworkCallback} to be utilized for this
2230 * request. Note the callback must not be shared - they
2231 * uniquely specify this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002232 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002233 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) {
2234 sendRequestForNetwork(request.networkCapabilities, networkCallback, 0,
2235 REQUEST, TYPE_NONE);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002236 }
2237
2238 /**
2239 * Request a network to satisfy a set of {@link NetworkCapabilities}, limited
2240 * by a timeout.
2241 *
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002242 * This function behaves identically to the non-timedout version, but if a suitable
Robert Greenwalt6078b502014-06-11 16:05:07 -07002243 * network is not found within the given time (in milliseconds) the
2244 * {@link NetworkCallback#unavailable} callback is called. The request must
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002245 * still be released normally by calling {@link releaseNetworkRequest}.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002246 * @param request {@link NetworkRequest} describing this request.
2247 * @param networkCallback The callbacks to be utilized for this request. Note
2248 * the callbacks must not be shared - they uniquely specify
2249 * this request.
2250 * @param timeoutMs The time in milliseconds to attempt looking for a suitable network
2251 * before {@link NetworkCallback#unavailable} is called.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002252 * @hide
2253 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002254 public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback,
2255 int timeoutMs) {
2256 sendRequestForNetwork(request.networkCapabilities, networkCallback, timeoutMs,
2257 REQUEST, TYPE_NONE);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002258 }
2259
2260 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002261 * The maximum number of milliseconds the framework will look for a suitable network
Robert Greenwalt9258c642014-03-26 16:47:06 -07002262 * during a timeout-equiped call to {@link requestNetwork}.
2263 * {@hide}
2264 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002265 public final static int MAX_NETWORK_REQUEST_TIMEOUT_MS = 100 * 60 * 1000;
Robert Greenwalt9258c642014-03-26 16:47:06 -07002266
2267 /**
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002268 * The lookup key for a {@link Network} object included with the intent after
2269 * succesfully finding a network for the applications request. Retrieve it with
2270 * {@link android.content.Intent#getParcelableExtra(String)}.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002271 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002272 */
2273 public static final String EXTRA_NETWORK_REQUEST_NETWORK = "networkRequestNetwork";
2274
2275 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002276 * The lookup key for a {@link NetworkRequest} object included with the intent after
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002277 * succesfully finding a network for the applications request. Retrieve it with
2278 * {@link android.content.Intent#getParcelableExtra(String)}.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002279 * @hide
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002280 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002281 public static final String EXTRA_NETWORK_REQUEST_NETWORK_REQUEST =
2282 "networkRequestNetworkRequest";
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002283
2284
2285 /**
Robert Greenwalt9258c642014-03-26 16:47:06 -07002286 * Request a network to satisfy a set of {@link NetworkCapabilities}.
2287 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002288 * This function behavies identically to the version that takes a NetworkCallback, but instead
2289 * of {@link NetworkCallback} a {@link PendingIntent} is used. This means
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002290 * the request may outlive the calling application and get called back when a suitable
2291 * network is found.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002292 * <p>
2293 * The operation is an Intent broadcast that goes to a broadcast receiver that
2294 * you registered with {@link Context#registerReceiver} or through the
2295 * &lt;receiver&gt; tag in an AndroidManifest.xml file
2296 * <p>
2297 * The operation Intent is delivered with two extras, a {@link Network} typed
Robert Greenwalt6078b502014-06-11 16:05:07 -07002298 * extra called {@link #EXTRA_NETWORK_REQUEST_NETWORK} and a {@link NetworkRequest}
2299 * typed extra called {@link #EXTRA_NETWORK_REQUEST_NETWORK_REQUEST} containing
Robert Greenwalt9258c642014-03-26 16:47:06 -07002300 * the original requests parameters. It is important to create a new,
Robert Greenwalt6078b502014-06-11 16:05:07 -07002301 * {@link NetworkCallback} based request before completing the processing of the
Robert Greenwalt9258c642014-03-26 16:47:06 -07002302 * Intent to reserve the network or it will be released shortly after the Intent
2303 * is processed.
2304 * <p>
2305 * If there is already an request for this Intent registered (with the equality of
2306 * two Intents defined by {@link Intent#filterEquals}), then it will be removed and
Robert Greenwaltd19c41c2014-05-18 23:07:25 -07002307 * replaced by this one, effectively releasing the previous {@link NetworkRequest}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002308 * <p>
Robert Greenwalt6078b502014-06-11 16:05:07 -07002309 * The request may be released normally by calling {@link #unregisterNetworkCallback}.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002310 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002311 * @param request {@link NetworkRequest} describing this request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002312 * @param operation Action to perform when the network is available (corresponds
Robert Greenwalt6078b502014-06-11 16:05:07 -07002313 * to the {@link NetworkCallback#onAvailable} call. Typically
Robert Greenwalt9258c642014-03-26 16:47:06 -07002314 * comes from {@link PendingIntent#getBroadcast}.
Robert Greenwalt6078b502014-06-11 16:05:07 -07002315 * @hide
Robert Greenwalt9258c642014-03-26 16:47:06 -07002316 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002317 public void requestNetwork(NetworkRequest request, PendingIntent operation) {
Robert Greenwalt9258c642014-03-26 16:47:06 -07002318 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002319 mService.pendingRequestForNetwork(request.networkCapabilities, operation);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002320 } catch (RemoteException e) {}
Robert Greenwalt9258c642014-03-26 16:47:06 -07002321 }
2322
2323 /**
2324 * Registers to receive notifications about all networks which satisfy the given
Robert Greenwalt6078b502014-06-11 16:05:07 -07002325 * {@link NetworkRequest}. The callbacks will continue to be called until
2326 * either the application exits or {@link #unregisterNetworkCallback} is called
Robert Greenwalt9258c642014-03-26 16:47:06 -07002327 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002328 * @param request {@link NetworkRequest} describing this request.
2329 * @param networkCallback The {@link NetworkCallback} that the system will call as suitable
2330 * networks change state.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002331 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002332 public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) {
2333 sendRequestForNetwork(request.networkCapabilities, networkCallback, 0, LISTEN, TYPE_NONE);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002334 }
2335
2336 /**
Robert Greenwalt6078b502014-06-11 16:05:07 -07002337 * Unregisters callbacks about and possibly releases networks originating from
2338 * {@link #requestNetwork} and {@link #registerNetworkCallback} calls. If the
2339 * given {@code NetworkCallback} had previosuly been used with {@code #requestNetwork},
2340 * any networks that had been connected to only to satisfy that request will be
2341 * disconnected.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002342 *
Robert Greenwalt6078b502014-06-11 16:05:07 -07002343 * @param networkCallback The {@link NetworkCallback} used when making the request.
Robert Greenwalt9258c642014-03-26 16:47:06 -07002344 */
Robert Greenwalt6078b502014-06-11 16:05:07 -07002345 public void unregisterNetworkCallback(NetworkCallback networkCallback) {
2346 if (networkCallback == null || networkCallback.networkRequest == null ||
2347 networkCallback.networkRequest.requestId == REQUEST_ID_UNSET) {
2348 throw new IllegalArgumentException("Invalid NetworkCallback");
2349 }
Robert Greenwalt9258c642014-03-26 16:47:06 -07002350 try {
Robert Greenwalt6078b502014-06-11 16:05:07 -07002351 mService.releaseNetworkRequest(networkCallback.networkRequest);
Robert Greenwalt9258c642014-03-26 16:47:06 -07002352 } catch (RemoteException e) {}
2353 }
Paul Jensen6d3ff9e2014-05-29 10:12:39 -04002354
2355 /**
2356 * Binds the current process to {@code network}. All Sockets created in the future
2357 * (and not explicitly bound via a bound SocketFactory from
2358 * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to
2359 * {@code network}. All host name resolutions will be limited to {@code network} as well.
2360 * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to
2361 * work and all host name resolutions will fail. This is by design so an application doesn't
2362 * accidentally use Sockets it thinks are still bound to a particular {@link Network}.
2363 * To clear binding pass {@code null} for {@code network}. Using individually bound
2364 * Sockets created by Network.getSocketFactory().createSocket() and
2365 * performing network-specific host name resolutions via
2366 * {@link Network#getAllByName Network.getAllByName} is preferred to calling
2367 * {@code setProcessDefaultNetwork}.
2368 *
2369 * @param network The {@link Network} to bind the current process to, or {@code null} to clear
2370 * the current binding.
2371 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2372 */
2373 public static boolean setProcessDefaultNetwork(Network network) {
2374 if (network == null) {
2375 NetworkUtils.unbindProcessToNetwork();
2376 } else {
2377 NetworkUtils.bindProcessToNetwork(network.netId);
2378 }
2379 // TODO fix return value
2380 return true;
2381 }
2382
2383 /**
2384 * Returns the {@link Network} currently bound to this process via
2385 * {@link #setProcessDefaultNetwork}, or {@code null} if no {@link Network} is explicitly bound.
2386 *
2387 * @return {@code Network} to which this process is bound, or {@code null}.
2388 */
2389 public static Network getProcessDefaultNetwork() {
2390 int netId = NetworkUtils.getNetworkBoundToProcess();
2391 if (netId == 0) return null;
2392 return new Network(netId);
2393 }
2394
2395 /**
2396 * Binds host resolutions performed by this process to {@code network}.
2397 * {@link #setProcessDefaultNetwork} takes precedence over this setting.
2398 *
2399 * @param network The {@link Network} to bind host resolutions from the current process to, or
2400 * {@code null} to clear the current binding.
2401 * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid.
2402 * @hide
2403 * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}.
2404 */
2405 public static boolean setProcessDefaultNetworkForHostResolution(Network network) {
2406 if (network == null) {
2407 NetworkUtils.unbindProcessToNetworkForHostResolution();
2408 } else {
2409 NetworkUtils.bindProcessToNetworkForHostResolution(network.netId);
2410 }
2411 // TODO hook up the return value.
2412 return true;
2413 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08002414}