The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 16 | package android.net; |
| 17 | |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 18 | import static com.android.internal.util.Preconditions.checkNotNull; |
| 19 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.annotation.SdkConstant; |
| 21 | import android.annotation.SdkConstant.SdkConstantType; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 22 | import android.app.PendingIntent; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 24 | import android.content.Intent; |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 25 | import android.net.NetworkUtils; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 26 | import android.os.Binder; |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 27 | import android.os.Build.VERSION_CODES; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 28 | import android.os.Handler; |
| 29 | import android.os.HandlerThread; |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 30 | import android.os.IBinder; |
| 31 | import android.os.INetworkActivityListener; |
| 32 | import android.os.INetworkManagementService; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 33 | import android.os.Looper; |
| 34 | import android.os.Message; |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 35 | import android.os.Messenger; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 36 | import android.os.RemoteException; |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 37 | import android.os.ServiceManager; |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 38 | import android.provider.Settings; |
Wink Saville | 36ffb04 | 2014-12-05 11:10:30 -0800 | [diff] [blame] | 39 | import android.telephony.SubscriptionManager; |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 40 | import android.util.ArrayMap; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 41 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 42 | |
Robert Greenwalt | afa05c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 43 | import com.android.internal.telephony.ITelephony; |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 44 | import com.android.internal.telephony.PhoneConstants; |
Robert Greenwalt | afa05c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 45 | import com.android.internal.util.Protocol; |
| 46 | |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 47 | import java.net.InetAddress; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 48 | import java.util.concurrent.atomic.AtomicInteger; |
| 49 | import java.util.HashMap; |
| 50 | |
Paul Jensen | c91b534 | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 51 | import libcore.net.event.NetworkEventDispatcher; |
| 52 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | /** |
| 54 | * Class that answers queries about the state of network connectivity. It also |
| 55 | * notifies applications when network connectivity changes. Get an instance |
| 56 | * of this class by calling |
| 57 | * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}. |
| 58 | * <p> |
| 59 | * The primary responsibilities of this class are to: |
| 60 | * <ol> |
| 61 | * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li> |
| 62 | * <li>Send broadcast intents when network connectivity changes</li> |
| 63 | * <li>Attempt to "fail over" to another network when connectivity to a network |
| 64 | * is lost</li> |
| 65 | * <li>Provide an API that allows applications to query the coarse-grained or fine-grained |
| 66 | * state of the available networks</li> |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 67 | * <li>Provide an API that allows applications to request and select networks for their data |
| 68 | * traffic</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | * </ol> |
| 70 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 71 | public class ConnectivityManager { |
| 72 | private static final String TAG = "ConnectivityManager"; |
| 73 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 74 | /** |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 75 | * A change in network connectivity has occurred. A default connection has either |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | * been established or lost. The NetworkInfo for the affected network is |
| 77 | * sent as an extra; it should be consulted to see what kind of |
| 78 | * connectivity event occurred. |
| 79 | * <p/> |
| 80 | * If this is a connection that was the result of failing over from a |
| 81 | * disconnected network, then the FAILOVER_CONNECTION boolean extra is |
| 82 | * set to true. |
| 83 | * <p/> |
| 84 | * For a loss of connectivity, if the connectivity manager is attempting |
| 85 | * to connect (or has already connected) to another network, the |
| 86 | * NetworkInfo for the new network is also passed as an extra. This lets |
| 87 | * any receivers of the broadcast know that they should not necessarily |
| 88 | * tell the user that no data traffic will be possible. Instead, the |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 89 | * receiver should expect another broadcast soon, indicating either that |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | * the failover attempt succeeded (and so there is still overall data |
| 91 | * connectivity), or that the failover attempt failed, meaning that all |
| 92 | * connectivity has been lost. |
| 93 | * <p/> |
| 94 | * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY |
| 95 | * is set to {@code true} if there are no connected networks at all. |
| 96 | */ |
Jeff Sharkey | 4fa63b2 | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 97 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 98 | public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 99 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | /** |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 101 | * The device has connected to a network that has presented a captive |
| 102 | * portal, which is blocking Internet connectivity. The user was presented |
| 103 | * with a notification that network sign in is required, |
| 104 | * and the user invoked the notification's action indicating they |
| 105 | * desire to sign in to the network. Apps handling this action should |
| 106 | * facilitate signing in to the network. This action includes a |
| 107 | * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents |
| 108 | * the network presenting the captive portal; all communication with the |
| 109 | * captive portal must be done using this {@code Network} object. |
| 110 | * <p/> |
| 111 | * When the app handling this action believes the user has signed in to |
| 112 | * the network and the captive portal has been dismissed, the app should call |
| 113 | * {@link #reportCaptivePortalDismissed} so the system can reevaluate the network. |
| 114 | * If reevaluation finds the network no longer subject to a captive portal, |
| 115 | * the network may become the default active data network. |
| 116 | * <p/> |
| 117 | * When the app handling this action believes the user explicitly wants |
| 118 | * to ignore the captive portal and the network, the app should call |
| 119 | * {@link #ignoreNetworkWithCaptivePortal}. |
| 120 | * <p/> |
| 121 | * Note that this action includes a {@code String} extra named |
| 122 | * {@link #EXTRA_CAPTIVE_PORTAL_TOKEN} that must |
| 123 | * be passed in to {@link #reportCaptivePortalDismissed} and |
| 124 | * {@link #ignoreNetworkWithCaptivePortal}. |
| 125 | */ |
| 126 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 127 | public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL"; |
| 128 | |
| 129 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 130 | * The lookup key for a {@link NetworkInfo} object. Retrieve with |
| 131 | * {@link android.content.Intent#getParcelableExtra(String)}. |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 132 | * |
| 133 | * @deprecated Since {@link NetworkInfo} can vary based on UID, applications |
| 134 | * should always obtain network information through |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 135 | * {@link #getActiveNetworkInfo()}. |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 136 | * @see #EXTRA_NETWORK_TYPE |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 137 | */ |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 138 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 139 | public static final String EXTRA_NETWORK_INFO = "networkInfo"; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 140 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | /** |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 142 | * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast. |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 143 | * |
| 144 | * @see android.content.Intent#getIntExtra(String, int) |
| 145 | */ |
| 146 | public static final String EXTRA_NETWORK_TYPE = "networkType"; |
| 147 | |
| 148 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | * The lookup key for a boolean that indicates whether a connect event |
| 150 | * is for a network to which the connectivity manager was failing over |
| 151 | * following a disconnect on another network. |
| 152 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
| 153 | */ |
| 154 | public static final String EXTRA_IS_FAILOVER = "isFailover"; |
| 155 | /** |
| 156 | * The lookup key for a {@link NetworkInfo} object. This is supplied when |
| 157 | * there is another network that it may be possible to connect to. Retrieve with |
| 158 | * {@link android.content.Intent#getParcelableExtra(String)}. |
| 159 | */ |
| 160 | public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork"; |
| 161 | /** |
| 162 | * The lookup key for a boolean that indicates whether there is a |
| 163 | * complete lack of connectivity, i.e., no network is available. |
| 164 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
| 165 | */ |
| 166 | public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity"; |
| 167 | /** |
| 168 | * The lookup key for a string that indicates why an attempt to connect |
| 169 | * to a network failed. The string has no particular structure. It is |
| 170 | * intended to be used in notifications presented to users. Retrieve |
| 171 | * it with {@link android.content.Intent#getStringExtra(String)}. |
| 172 | */ |
| 173 | public static final String EXTRA_REASON = "reason"; |
| 174 | /** |
| 175 | * The lookup key for a string that provides optionally supplied |
| 176 | * extra information about the network state. The information |
| 177 | * may be passed up from the lower networking layers, and its |
| 178 | * meaning may be specific to a particular network type. Retrieve |
| 179 | * it with {@link android.content.Intent#getStringExtra(String)}. |
| 180 | */ |
| 181 | public static final String EXTRA_EXTRA_INFO = "extraInfo"; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 182 | /** |
| 183 | * The lookup key for an int that provides information about |
| 184 | * our connection to the internet at large. 0 indicates no connection, |
| 185 | * 100 indicates a great connection. Retrieve it with |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 186 | * {@link android.content.Intent#getIntExtra(String, int)}. |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 187 | * {@hide} |
| 188 | */ |
| 189 | public static final String EXTRA_INET_CONDITION = "inetCondition"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 190 | |
| 191 | /** |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 192 | * The lookup key for a string that is sent out with |
| 193 | * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN}. This string must be |
| 194 | * passed in to {@link #reportCaptivePortalDismissed} and |
| 195 | * {@link #ignoreNetworkWithCaptivePortal}. Retrieve it with |
| 196 | * {@link android.content.Intent#getStringExtra(String)}. |
| 197 | */ |
| 198 | public static final String EXTRA_CAPTIVE_PORTAL_TOKEN = "captivePortalToken"; |
| 199 | |
| 200 | /** |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 201 | * Broadcast action to indicate the change of data activity status |
| 202 | * (idle or active) on a network in a recent period. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 203 | * The network becomes active when data transmission is started, or |
| 204 | * idle if there is no data transmission for a period of time. |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 205 | * {@hide} |
| 206 | */ |
| 207 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 208 | public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE"; |
| 209 | /** |
| 210 | * The lookup key for an enum that indicates the network device type on which this data activity |
| 211 | * change happens. |
| 212 | * {@hide} |
| 213 | */ |
| 214 | public static final String EXTRA_DEVICE_TYPE = "deviceType"; |
| 215 | /** |
| 216 | * The lookup key for a boolean that indicates the device is active or not. {@code true} means |
| 217 | * it is actively sending or receiving data and {@code false} means it is idle. |
| 218 | * {@hide} |
| 219 | */ |
| 220 | public static final String EXTRA_IS_ACTIVE = "isActive"; |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 221 | /** |
| 222 | * The lookup key for a long that contains the timestamp (nanos) of the radio state change. |
| 223 | * {@hide} |
| 224 | */ |
| 225 | public static final String EXTRA_REALTIME_NS = "tsNanos"; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 226 | |
| 227 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 228 | * Broadcast Action: The setting for background data usage has changed |
| 229 | * values. Use {@link #getBackgroundDataSetting()} to get the current value. |
| 230 | * <p> |
| 231 | * If an application uses the network in the background, it should listen |
| 232 | * for this broadcast and stop using the background data if the value is |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 233 | * {@code false}. |
Jeff Sharkey | 54ee2ad | 2012-01-30 16:29:24 -0800 | [diff] [blame] | 234 | * <p> |
| 235 | * |
| 236 | * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability |
| 237 | * of background data depends on several combined factors, and |
| 238 | * this broadcast is no longer sent. Instead, when background |
| 239 | * data is unavailable, {@link #getActiveNetworkInfo()} will now |
| 240 | * appear disconnected. During first boot after a platform |
| 241 | * upgrade, this broadcast will be sent once if |
| 242 | * {@link #getBackgroundDataSetting()} was {@code false} before |
| 243 | * the upgrade. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 244 | */ |
| 245 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Jeff Sharkey | 54ee2ad | 2012-01-30 16:29:24 -0800 | [diff] [blame] | 246 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 247 | public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED = |
| 248 | "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED"; |
| 249 | |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 250 | /** |
| 251 | * Broadcast Action: The network connection may not be good |
| 252 | * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and |
| 253 | * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify |
| 254 | * the network and it's condition. |
| 255 | * @hide |
| 256 | */ |
Jeff Sharkey | 4fa63b2 | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 257 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 258 | public static final String INET_CONDITION_ACTION = |
| 259 | "android.net.conn.INET_CONDITION_ACTION"; |
| 260 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 261 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 262 | * Broadcast Action: A tetherable connection has come or gone. |
| 263 | * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER}, |
| 264 | * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER} and |
| 265 | * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate |
| 266 | * the current state of tethering. Each include a list of |
| 267 | * interface names in that state (may be empty). |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 268 | * @hide |
| 269 | */ |
Jeff Sharkey | 4fa63b2 | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 270 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 271 | public static final String ACTION_TETHER_STATE_CHANGED = |
| 272 | "android.net.conn.TETHER_STATE_CHANGED"; |
| 273 | |
| 274 | /** |
| 275 | * @hide |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 276 | * gives a String[] listing all the interfaces configured for |
| 277 | * tethering and currently available for tethering. |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 278 | */ |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 279 | public static final String EXTRA_AVAILABLE_TETHER = "availableArray"; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 280 | |
| 281 | /** |
| 282 | * @hide |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 283 | * gives a String[] listing all the interfaces currently tethered |
| 284 | * (ie, has dhcp support and packets potentially forwarded/NATed) |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 285 | */ |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 286 | public static final String EXTRA_ACTIVE_TETHER = "activeArray"; |
| 287 | |
| 288 | /** |
| 289 | * @hide |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 290 | * gives a String[] listing all the interfaces we tried to tether and |
| 291 | * failed. Use {@link #getLastTetherError} to find the error code |
| 292 | * for any interfaces listed here. |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 293 | */ |
| 294 | public static final String EXTRA_ERRORED_TETHER = "erroredArray"; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 295 | |
| 296 | /** |
Russell Brenner | 108da0c | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 297 | * Broadcast Action: The captive portal tracker has finished its test. |
| 298 | * Sent only while running Setup Wizard, in lieu of showing a user |
| 299 | * notification. |
| 300 | * @hide |
| 301 | */ |
Jeff Sharkey | 4fa63b2 | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 302 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Russell Brenner | 108da0c | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 303 | public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED = |
| 304 | "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED"; |
| 305 | /** |
| 306 | * The lookup key for a boolean that indicates whether a captive portal was detected. |
| 307 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
| 308 | * @hide |
| 309 | */ |
| 310 | public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal"; |
| 311 | |
| 312 | /** |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 313 | * Action used to display a dialog that asks the user whether to connect to a network that is |
| 314 | * not validated. This intent is used to start the dialog in settings via startActivity. |
| 315 | * |
| 316 | * @hide |
| 317 | */ |
| 318 | public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED"; |
| 319 | |
| 320 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 321 | * The absence of a connection type. |
Robert Greenwalt | ccf83af1 | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 322 | * @hide |
| 323 | */ |
| 324 | public static final int TYPE_NONE = -1; |
| 325 | |
| 326 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 327 | * The Mobile data connection. When active, all data traffic |
| 328 | * will use this network type's interface by default |
| 329 | * (it has a default route) |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 330 | */ |
| 331 | public static final int TYPE_MOBILE = 0; |
| 332 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 333 | * The WIFI data connection. When active, all data traffic |
| 334 | * will use this network type's interface by default |
| 335 | * (it has a default route). |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 336 | */ |
| 337 | public static final int TYPE_WIFI = 1; |
| 338 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 339 | * An MMS-specific Mobile data connection. This network type may use the |
| 340 | * same network interface as {@link #TYPE_MOBILE} or it may use a different |
| 341 | * one. This is used by applications needing to talk to the carrier's |
| 342 | * Multimedia Messaging Service servers. |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 343 | * |
| 344 | * @deprecated Applications should instead use {@link #requestNetwork} to request a network that |
| 345 | * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 346 | */ |
| 347 | public static final int TYPE_MOBILE_MMS = 2; |
| 348 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 349 | * A SUPL-specific Mobile data connection. This network type may use the |
| 350 | * same network interface as {@link #TYPE_MOBILE} or it may use a different |
| 351 | * one. This is used by applications needing to talk to the carrier's |
| 352 | * Secure User Plane Location servers for help locating the device. |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 353 | * |
| 354 | * @deprecated Applications should instead use {@link #requestNetwork} to request a network that |
| 355 | * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 356 | */ |
| 357 | public static final int TYPE_MOBILE_SUPL = 3; |
| 358 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 359 | * A DUN-specific Mobile data connection. This network type may use the |
| 360 | * same network interface as {@link #TYPE_MOBILE} or it may use a different |
| 361 | * one. This is sometimes by the system when setting up an upstream connection |
| 362 | * for tethering so that the carrier is aware of DUN traffic. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 363 | */ |
| 364 | public static final int TYPE_MOBILE_DUN = 4; |
| 365 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 366 | * A High Priority Mobile data connection. This network type uses the |
| 367 | * same network interface as {@link #TYPE_MOBILE} but the routing setup |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 368 | * is different. |
| 369 | * |
| 370 | * @deprecated Applications should instead use {@link #requestNetwork} to request a network that |
| 371 | * uses the {@link NetworkCapabilities#TRANSPORT_CELLULAR} transport. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 372 | */ |
| 373 | public static final int TYPE_MOBILE_HIPRI = 5; |
jsh | 8214deb | 2010-03-11 15:04:43 -0800 | [diff] [blame] | 374 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 375 | * The WiMAX data connection. When active, all data traffic |
| 376 | * will use this network type's interface by default |
| 377 | * (it has a default route). |
jsh | 8214deb | 2010-03-11 15:04:43 -0800 | [diff] [blame] | 378 | */ |
| 379 | public static final int TYPE_WIMAX = 6; |
Robert Greenwalt | da3d5e6 | 2010-12-06 13:56:24 -0800 | [diff] [blame] | 380 | |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 381 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 382 | * The Bluetooth data connection. When active, all data traffic |
| 383 | * will use this network type's interface by default |
| 384 | * (it has a default route). |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 385 | */ |
| 386 | public static final int TYPE_BLUETOOTH = 7; |
| 387 | |
Robert Greenwalt | 6081084 | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 388 | /** |
| 389 | * Dummy data connection. This should not be used on shipping devices. |
| 390 | */ |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 391 | public static final int TYPE_DUMMY = 8; |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 392 | |
Robert Greenwalt | 6081084 | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 393 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 394 | * The Ethernet data connection. When active, all data traffic |
| 395 | * will use this network type's interface by default |
| 396 | * (it has a default route). |
Robert Greenwalt | 6081084 | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 397 | */ |
Robert Greenwalt | e12aec9 | 2011-01-28 14:48:37 -0800 | [diff] [blame] | 398 | public static final int TYPE_ETHERNET = 9; |
Robert Greenwalt | 6081084 | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 399 | |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 400 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 401 | * Over the air Administration. |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 402 | * {@hide} |
| 403 | */ |
| 404 | public static final int TYPE_MOBILE_FOTA = 10; |
| 405 | |
| 406 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 407 | * IP Multimedia Subsystem. |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 408 | * {@hide} |
| 409 | */ |
| 410 | public static final int TYPE_MOBILE_IMS = 11; |
| 411 | |
| 412 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 413 | * Carrier Branded Services. |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 414 | * {@hide} |
| 415 | */ |
| 416 | public static final int TYPE_MOBILE_CBS = 12; |
| 417 | |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 418 | /** |
| 419 | * A Wi-Fi p2p connection. Only requesting processes will have access to |
| 420 | * the peers connected. |
| 421 | * {@hide} |
| 422 | */ |
| 423 | public static final int TYPE_WIFI_P2P = 13; |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 424 | |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 425 | /** |
| 426 | * The network to use for initially attaching to the network |
| 427 | * {@hide} |
| 428 | */ |
| 429 | public static final int TYPE_MOBILE_IA = 14; |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 430 | |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 431 | /** |
Ram | 3e0e3bc | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 432 | * Emergency PDN connection for emergency calls |
| 433 | * {@hide} |
| 434 | */ |
| 435 | public static final int TYPE_MOBILE_EMERGENCY = 15; |
| 436 | |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 437 | /** |
| 438 | * The network that uses proxy to achieve connectivity. |
| 439 | * {@hide} |
| 440 | */ |
| 441 | public static final int TYPE_PROXY = 16; |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 442 | |
Robert Greenwalt | 8283f88 | 2014-07-07 17:09:01 -0700 | [diff] [blame] | 443 | /** |
| 444 | * A virtual network using one or more native bearers. |
| 445 | * It may or may not be providing security services. |
| 446 | */ |
| 447 | public static final int TYPE_VPN = 17; |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 448 | |
| 449 | /** {@hide} */ |
Robert Greenwalt | 8283f88 | 2014-07-07 17:09:01 -0700 | [diff] [blame] | 450 | public static final int MAX_RADIO_TYPE = TYPE_VPN; |
| 451 | |
| 452 | /** {@hide} */ |
| 453 | public static final int MAX_NETWORK_TYPE = TYPE_VPN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 454 | |
Jianzheng Zhou | dcf03f3 | 2012-11-16 13:45:20 +0800 | [diff] [blame] | 455 | /** |
| 456 | * If you want to set the default network preference,you can directly |
| 457 | * change the networkAttributes array in framework's config.xml. |
| 458 | * |
| 459 | * @deprecated Since we support so many more networks now, the single |
| 460 | * network default network preference can't really express |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 461 | * the hierarchy. Instead, the default is defined by the |
Jianzheng Zhou | dcf03f3 | 2012-11-16 13:45:20 +0800 | [diff] [blame] | 462 | * networkAttributes in config.xml. You can determine |
Robert Greenwalt | 4c8b748 | 2012-12-07 09:56:50 -0800 | [diff] [blame] | 463 | * the current value by calling {@link #getNetworkPreference()} |
Jianzheng Zhou | dcf03f3 | 2012-11-16 13:45:20 +0800 | [diff] [blame] | 464 | * from an App. |
| 465 | */ |
| 466 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 467 | public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI; |
| 468 | |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 469 | /** |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 470 | * @hide |
| 471 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 472 | public final static int REQUEST_ID_UNSET = 0; |
| 473 | |
Paul Jensen | 5d59e78 | 2014-07-11 12:28:19 -0400 | [diff] [blame] | 474 | /** |
| 475 | * A NetID indicating no Network is selected. |
| 476 | * Keep in sync with bionic/libc/dns/include/resolv_netid.h |
| 477 | * @hide |
| 478 | */ |
| 479 | public static final int NETID_UNSET = 0; |
| 480 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 481 | private final IConnectivityManager mService; |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 482 | /** |
| 483 | * A kludge to facilitate static access where a Context pointer isn't available, like in the |
| 484 | * case of the static set/getProcessDefaultNetwork methods and from the Network class. |
| 485 | * TODO: Remove this after deprecating the static methods in favor of non-static methods or |
| 486 | * methods that take a Context argument. |
| 487 | */ |
| 488 | private static ConnectivityManager sInstance; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 490 | private INetworkManagementService mNMService; |
| 491 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 492 | /** |
| 493 | * Tests if a given integer represents a valid network type. |
| 494 | * @param networkType the type to be tested |
| 495 | * @return a boolean. {@code true} if the type is valid, else {@code false} |
| 496 | */ |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 497 | public static boolean isNetworkTypeValid(int networkType) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 498 | return networkType >= 0 && networkType <= MAX_NETWORK_TYPE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 499 | } |
| 500 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 501 | /** |
| 502 | * Returns a non-localized string representing a given network type. |
| 503 | * ONLY used for debugging output. |
| 504 | * @param type the type needing naming |
| 505 | * @return a String for the given type, or a string version of the type ("87") |
| 506 | * if no name is known. |
| 507 | * {@hide} |
| 508 | */ |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 509 | public static String getNetworkTypeName(int type) { |
| 510 | switch (type) { |
| 511 | case TYPE_MOBILE: |
| 512 | return "MOBILE"; |
| 513 | case TYPE_WIFI: |
| 514 | return "WIFI"; |
| 515 | case TYPE_MOBILE_MMS: |
| 516 | return "MOBILE_MMS"; |
| 517 | case TYPE_MOBILE_SUPL: |
| 518 | return "MOBILE_SUPL"; |
| 519 | case TYPE_MOBILE_DUN: |
| 520 | return "MOBILE_DUN"; |
| 521 | case TYPE_MOBILE_HIPRI: |
| 522 | return "MOBILE_HIPRI"; |
| 523 | case TYPE_WIMAX: |
| 524 | return "WIMAX"; |
| 525 | case TYPE_BLUETOOTH: |
| 526 | return "BLUETOOTH"; |
| 527 | case TYPE_DUMMY: |
| 528 | return "DUMMY"; |
| 529 | case TYPE_ETHERNET: |
| 530 | return "ETHERNET"; |
| 531 | case TYPE_MOBILE_FOTA: |
| 532 | return "MOBILE_FOTA"; |
| 533 | case TYPE_MOBILE_IMS: |
| 534 | return "MOBILE_IMS"; |
| 535 | case TYPE_MOBILE_CBS: |
| 536 | return "MOBILE_CBS"; |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 537 | case TYPE_WIFI_P2P: |
| 538 | return "WIFI_P2P"; |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 539 | case TYPE_MOBILE_IA: |
| 540 | return "MOBILE_IA"; |
Ram | 3e0e3bc | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 541 | case TYPE_MOBILE_EMERGENCY: |
| 542 | return "MOBILE_EMERGENCY"; |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 543 | case TYPE_PROXY: |
| 544 | return "PROXY"; |
Erik Kline | 37fbfa1 | 2014-11-19 17:23:41 +0900 | [diff] [blame] | 545 | case TYPE_VPN: |
| 546 | return "VPN"; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 547 | default: |
| 548 | return Integer.toString(type); |
| 549 | } |
| 550 | } |
| 551 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 552 | /** |
| 553 | * Checks if a given type uses the cellular data connection. |
| 554 | * This should be replaced in the future by a network property. |
| 555 | * @param networkType the type to check |
| 556 | * @return a boolean - {@code true} if uses cellular network, else {@code false} |
| 557 | * {@hide} |
| 558 | */ |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 559 | public static boolean isNetworkTypeMobile(int networkType) { |
| 560 | switch (networkType) { |
| 561 | case TYPE_MOBILE: |
| 562 | case TYPE_MOBILE_MMS: |
| 563 | case TYPE_MOBILE_SUPL: |
| 564 | case TYPE_MOBILE_DUN: |
| 565 | case TYPE_MOBILE_HIPRI: |
| 566 | case TYPE_MOBILE_FOTA: |
| 567 | case TYPE_MOBILE_IMS: |
| 568 | case TYPE_MOBILE_CBS: |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 569 | case TYPE_MOBILE_IA: |
Ram | 3e0e3bc | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 570 | case TYPE_MOBILE_EMERGENCY: |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 571 | return true; |
| 572 | default: |
| 573 | return false; |
| 574 | } |
| 575 | } |
| 576 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 577 | /** |
Jeff Sharkey | 7a1c3fc | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 578 | * Checks if the given network type is backed by a Wi-Fi radio. |
| 579 | * |
| 580 | * @hide |
| 581 | */ |
| 582 | public static boolean isNetworkTypeWifi(int networkType) { |
| 583 | switch (networkType) { |
| 584 | case TYPE_WIFI: |
| 585 | case TYPE_WIFI_P2P: |
| 586 | return true; |
| 587 | default: |
| 588 | return false; |
| 589 | } |
| 590 | } |
| 591 | |
| 592 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 593 | * Specifies the preferred network type. When the device has more |
| 594 | * than one type available the preferred network type will be used. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 595 | * |
| 596 | * @param preference the network type to prefer over all others. It is |
| 597 | * unspecified what happens to the old preferred network in the |
| 598 | * overall ordering. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 599 | * @deprecated Functionality has been removed as it no longer makes sense, |
| 600 | * with many more than two networks - we'd need an array to express |
| 601 | * preference. Instead we use dynamic network properties of |
| 602 | * the networks to describe their precedence. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 603 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 604 | public void setNetworkPreference(int preference) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 605 | } |
| 606 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 607 | /** |
| 608 | * Retrieves the current preferred network type. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 609 | * |
| 610 | * @return an integer representing the preferred network type |
| 611 | * |
| 612 | * <p>This method requires the caller to hold the permission |
| 613 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 614 | * @deprecated Functionality has been removed as it no longer makes sense, |
| 615 | * with many more than two networks - we'd need an array to express |
| 616 | * preference. Instead we use dynamic network properties of |
| 617 | * the networks to describe their precedence. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 618 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 619 | public int getNetworkPreference() { |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 620 | return TYPE_NONE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 621 | } |
| 622 | |
Scott Main | 671644c | 2011-10-06 19:02:28 -0700 | [diff] [blame] | 623 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 624 | * Returns details about the currently active default data network. When |
| 625 | * connected, this network is the default route for outgoing connections. |
| 626 | * You should always check {@link NetworkInfo#isConnected()} before initiating |
| 627 | * network traffic. This may return {@code null} when there is no default |
| 628 | * network. |
| 629 | * |
| 630 | * @return a {@link NetworkInfo} object for the current default network |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 631 | * or {@code null} if no default network is currently active |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 632 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 633 | * <p>This method requires the caller to hold the permission |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 634 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 635 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 636 | public NetworkInfo getActiveNetworkInfo() { |
| 637 | try { |
| 638 | return mService.getActiveNetworkInfo(); |
| 639 | } catch (RemoteException e) { |
| 640 | return null; |
| 641 | } |
| 642 | } |
| 643 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 644 | /** |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 645 | * Returns a {@link Network} object corresponding to the currently active |
| 646 | * default data network. In the event that the current active default data |
| 647 | * network disconnects, the returned {@code Network} object will no longer |
| 648 | * be usable. This will return {@code null} when there is no default |
| 649 | * network. |
| 650 | * |
| 651 | * @return a {@link Network} object for the current default network or |
| 652 | * {@code null} if no default network is currently active |
| 653 | * |
| 654 | * <p>This method requires the caller to hold the permission |
| 655 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
| 656 | */ |
| 657 | public Network getActiveNetwork() { |
| 658 | try { |
| 659 | return mService.getActiveNetwork(); |
| 660 | } catch (RemoteException e) { |
| 661 | return null; |
| 662 | } |
| 663 | } |
| 664 | |
| 665 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 666 | * Returns details about the currently active default data network |
| 667 | * for a given uid. This is for internal use only to avoid spying |
| 668 | * other apps. |
| 669 | * |
| 670 | * @return a {@link NetworkInfo} object for the current default network |
| 671 | * for the given uid or {@code null} if no default network is |
| 672 | * available for the specified uid. |
| 673 | * |
| 674 | * <p>This method requires the caller to hold the permission |
| 675 | * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL} |
| 676 | * {@hide} |
| 677 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 678 | public NetworkInfo getActiveNetworkInfoForUid(int uid) { |
| 679 | try { |
| 680 | return mService.getActiveNetworkInfoForUid(uid); |
| 681 | } catch (RemoteException e) { |
| 682 | return null; |
| 683 | } |
| 684 | } |
| 685 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 686 | /** |
| 687 | * Returns connection status information about a particular |
| 688 | * network type. |
| 689 | * |
| 690 | * @param networkType integer specifying which networkType in |
| 691 | * which you're interested. |
| 692 | * @return a {@link NetworkInfo} object for the requested |
| 693 | * network type or {@code null} if the type is not |
| 694 | * supported by the device. |
| 695 | * |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 696 | * <p>This method requires the caller to hold the permission |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 697 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 698 | * |
| 699 | * @deprecated This method does not support multiple connected networks |
| 700 | * of the same type. Use {@link #getAllNetworks} and |
| 701 | * {@link #getNetworkInfo(android.net.Network)} instead. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 702 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 703 | public NetworkInfo getNetworkInfo(int networkType) { |
| 704 | try { |
| 705 | return mService.getNetworkInfo(networkType); |
| 706 | } catch (RemoteException e) { |
| 707 | return null; |
| 708 | } |
| 709 | } |
| 710 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 711 | /** |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 712 | * Returns connection status information about a particular |
| 713 | * Network. |
| 714 | * |
| 715 | * @param network {@link Network} specifying which network |
| 716 | * in which you're interested. |
| 717 | * @return a {@link NetworkInfo} object for the requested |
| 718 | * network or {@code null} if the {@code Network} |
| 719 | * is not valid. |
| 720 | * |
| 721 | * <p>This method requires the caller to hold the permission |
| 722 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
| 723 | */ |
| 724 | public NetworkInfo getNetworkInfo(Network network) { |
| 725 | try { |
| 726 | return mService.getNetworkInfoForNetwork(network); |
| 727 | } catch (RemoteException e) { |
| 728 | return null; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 733 | * Returns connection status information about all network |
| 734 | * types supported by the device. |
| 735 | * |
| 736 | * @return an array of {@link NetworkInfo} objects. Check each |
| 737 | * {@link NetworkInfo#getType} for which type each applies. |
| 738 | * |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 739 | * <p>This method requires the caller to hold the permission |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 740 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 741 | * |
| 742 | * @deprecated This method does not support multiple connected networks |
| 743 | * of the same type. Use {@link #getAllNetworks} and |
| 744 | * {@link #getNetworkInfo(android.net.Network)} instead. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 745 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 746 | public NetworkInfo[] getAllNetworkInfo() { |
| 747 | try { |
| 748 | return mService.getAllNetworkInfo(); |
| 749 | } catch (RemoteException e) { |
| 750 | return null; |
| 751 | } |
| 752 | } |
| 753 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 754 | /** |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 755 | * Returns the {@link Network} object currently serving a given type, or |
| 756 | * null if the given type is not connected. |
| 757 | * |
| 758 | * <p>This method requires the caller to hold the permission |
| 759 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
| 760 | * |
| 761 | * @hide |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 762 | * @deprecated This method does not support multiple connected networks |
| 763 | * of the same type. Use {@link #getAllNetworks} and |
| 764 | * {@link #getNetworkInfo(android.net.Network)} instead. |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 765 | */ |
| 766 | public Network getNetworkForType(int networkType) { |
| 767 | try { |
| 768 | return mService.getNetworkForType(networkType); |
| 769 | } catch (RemoteException e) { |
| 770 | return null; |
| 771 | } |
| 772 | } |
| 773 | |
| 774 | /** |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 775 | * Returns an array of all {@link Network} currently tracked by the |
| 776 | * framework. |
| 777 | * |
| 778 | * @return an array of {@link Network} objects. |
| 779 | * |
| 780 | * <p>This method requires the caller to hold the permission |
| 781 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
| 782 | */ |
| 783 | public Network[] getAllNetworks() { |
| 784 | try { |
| 785 | return mService.getAllNetworks(); |
| 786 | } catch (RemoteException e) { |
| 787 | return null; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | /** |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 792 | * Returns an array of {@link android.net.NetworkCapabilities} objects, representing |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 793 | * the Networks that applications run by the given user will use by default. |
| 794 | * @hide |
| 795 | */ |
| 796 | public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) { |
| 797 | try { |
| 798 | return mService.getDefaultNetworkCapabilitiesForUser(userId); |
| 799 | } catch (RemoteException e) { |
| 800 | return null; |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | /** |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 805 | * Returns details about the Provisioning or currently active default data network. When |
| 806 | * connected, this network is the default route for outgoing connections. |
| 807 | * You should always check {@link NetworkInfo#isConnected()} before initiating |
| 808 | * network traffic. This may return {@code null} when there is no default |
| 809 | * network. |
| 810 | * |
| 811 | * @return a {@link NetworkInfo} object for the current default network |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 812 | * or {@code null} if no default network is currently active |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 813 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 814 | * <p>This method requires the caller to hold the permission |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 815 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
| 816 | * |
| 817 | * {@hide} |
| 818 | */ |
| 819 | public NetworkInfo getProvisioningOrActiveNetworkInfo() { |
| 820 | try { |
| 821 | return mService.getProvisioningOrActiveNetworkInfo(); |
| 822 | } catch (RemoteException e) { |
| 823 | return null; |
| 824 | } |
| 825 | } |
| 826 | |
| 827 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 828 | * Returns the IP information for the current default network. |
| 829 | * |
| 830 | * @return a {@link LinkProperties} object describing the IP info |
| 831 | * for the current default network, or {@code null} if there |
| 832 | * is no current default network. |
| 833 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 834 | * <p>This method requires the caller to hold the permission |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 835 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
| 836 | * {@hide} |
| 837 | */ |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 838 | public LinkProperties getActiveLinkProperties() { |
| 839 | try { |
| 840 | return mService.getActiveLinkProperties(); |
| 841 | } catch (RemoteException e) { |
| 842 | return null; |
| 843 | } |
| 844 | } |
| 845 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 846 | /** |
| 847 | * Returns the IP information for a given network type. |
| 848 | * |
| 849 | * @param networkType the network type of interest. |
| 850 | * @return a {@link LinkProperties} object describing the IP info |
| 851 | * for the given networkType, or {@code null} if there is |
| 852 | * no current default network. |
| 853 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 854 | * <p>This method requires the caller to hold the permission |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 855 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
| 856 | * {@hide} |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 857 | * @deprecated This method does not support multiple connected networks |
| 858 | * of the same type. Use {@link #getAllNetworks}, |
| 859 | * {@link #getNetworkInfo(android.net.Network)}, and |
| 860 | * {@link #getLinkProperties(android.net.Network)} instead. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 861 | */ |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 862 | public LinkProperties getLinkProperties(int networkType) { |
| 863 | try { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 864 | return mService.getLinkPropertiesForType(networkType); |
| 865 | } catch (RemoteException e) { |
| 866 | return null; |
| 867 | } |
| 868 | } |
| 869 | |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 870 | /** |
| 871 | * Get the {@link LinkProperties} for the given {@link Network}. This |
| 872 | * will return {@code null} if the network is unknown. |
| 873 | * |
| 874 | * @param network The {@link Network} object identifying the network in question. |
| 875 | * @return The {@link LinkProperties} for the network, or {@code null}. |
| 876 | **/ |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 877 | public LinkProperties getLinkProperties(Network network) { |
| 878 | try { |
| 879 | return mService.getLinkProperties(network); |
| 880 | } catch (RemoteException e) { |
| 881 | return null; |
| 882 | } |
| 883 | } |
| 884 | |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 885 | /** |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 886 | * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 887 | * will return {@code null} if the network is unknown. |
| 888 | * |
| 889 | * @param network The {@link Network} object identifying the network in question. |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 890 | * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 891 | */ |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 892 | public NetworkCapabilities getNetworkCapabilities(Network network) { |
| 893 | try { |
| 894 | return mService.getNetworkCapabilities(network); |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 895 | } catch (RemoteException e) { |
| 896 | return null; |
| 897 | } |
| 898 | } |
| 899 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 900 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 901 | * Tells the underlying networking system that the caller wants to |
| 902 | * begin using the named feature. The interpretation of {@code feature} |
| 903 | * is completely up to each networking implementation. |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 904 | * <p>This method requires the caller to hold the permission |
| 905 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 906 | * @param networkType specifies which network the request pertains to |
| 907 | * @param feature the name of the feature to be used |
| 908 | * @return an integer value representing the outcome of the request. |
| 909 | * The interpretation of this value is specific to each networking |
| 910 | * implementation+feature combination, except that the value {@code -1} |
| 911 | * always indicates failure. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 912 | * |
| 913 | * @deprecated Deprecated in favor of the cleaner {@link #requestNetwork} api. |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 914 | * @removed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 915 | */ |
| 916 | public int startUsingNetworkFeature(int networkType, String feature) { |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 917 | NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature); |
| 918 | if (netCap == null) { |
| 919 | Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " + |
| 920 | feature); |
| 921 | return PhoneConstants.APN_REQUEST_FAILED; |
| 922 | } |
| 923 | |
| 924 | NetworkRequest request = null; |
| 925 | synchronized (sLegacyRequests) { |
| 926 | LegacyRequest l = sLegacyRequests.get(netCap); |
| 927 | if (l != null) { |
| 928 | Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest); |
| 929 | renewRequestLocked(l); |
| 930 | if (l.currentNetwork != null) { |
| 931 | return PhoneConstants.APN_ALREADY_ACTIVE; |
| 932 | } else { |
| 933 | return PhoneConstants.APN_REQUEST_STARTED; |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | request = requestNetworkForFeatureLocked(netCap); |
| 938 | } |
| 939 | if (request != null) { |
Robert Greenwalt | 257ee5f | 2014-06-20 10:58:45 -0700 | [diff] [blame] | 940 | Log.d(TAG, "starting startUsingNetworkFeature for request " + request); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 941 | return PhoneConstants.APN_REQUEST_STARTED; |
| 942 | } else { |
| 943 | Log.d(TAG, " request Failed"); |
| 944 | return PhoneConstants.APN_REQUEST_FAILED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 945 | } |
| 946 | } |
| 947 | |
| 948 | /** |
| 949 | * Tells the underlying networking system that the caller is finished |
| 950 | * using the named feature. The interpretation of {@code feature} |
| 951 | * is completely up to each networking implementation. |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 952 | * <p>This method requires the caller to hold the permission |
| 953 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 954 | * @param networkType specifies which network the request pertains to |
| 955 | * @param feature the name of the feature that is no longer needed |
| 956 | * @return an integer value representing the outcome of the request. |
| 957 | * The interpretation of this value is specific to each networking |
| 958 | * implementation+feature combination, except that the value {@code -1} |
| 959 | * always indicates failure. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 960 | * |
| 961 | * @deprecated Deprecated in favor of the cleaner {@link #requestNetwork} api. |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 962 | * @removed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 963 | */ |
| 964 | public int stopUsingNetworkFeature(int networkType, String feature) { |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 965 | NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature); |
| 966 | if (netCap == null) { |
| 967 | Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " + |
| 968 | feature); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 969 | return -1; |
| 970 | } |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 971 | |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 972 | if (removeRequestForFeature(netCap)) { |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 973 | Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 974 | } |
| 975 | return 1; |
| 976 | } |
| 977 | |
Lorenzo Colitti | 4077acb | 2014-06-04 12:20:06 +0900 | [diff] [blame] | 978 | /** |
| 979 | * Removes the NET_CAPABILITY_NOT_RESTRICTED capability from the given |
Lorenzo Colitti | ea1984f | 2014-06-04 19:59:21 +0900 | [diff] [blame] | 980 | * NetworkCapabilities object if all the capabilities it provides are |
| 981 | * typically provided by restricted networks. |
| 982 | * |
| 983 | * TODO: consider: |
| 984 | * - Moving to NetworkCapabilities |
| 985 | * - Renaming it to guessRestrictedCapability and make it set the |
| 986 | * restricted capability bit in addition to clearing it. |
Lorenzo Colitti | 4077acb | 2014-06-04 12:20:06 +0900 | [diff] [blame] | 987 | * @hide |
| 988 | */ |
| 989 | public static void maybeMarkCapabilitiesRestricted(NetworkCapabilities nc) { |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 990 | for (int capability : nc.getCapabilities()) { |
| 991 | switch (capability) { |
Lorenzo Colitti | 4077acb | 2014-06-04 12:20:06 +0900 | [diff] [blame] | 992 | case NetworkCapabilities.NET_CAPABILITY_CBS: |
| 993 | case NetworkCapabilities.NET_CAPABILITY_DUN: |
Lorenzo Colitti | ea1984f | 2014-06-04 19:59:21 +0900 | [diff] [blame] | 994 | case NetworkCapabilities.NET_CAPABILITY_EIMS: |
Lorenzo Colitti | 4077acb | 2014-06-04 12:20:06 +0900 | [diff] [blame] | 995 | case NetworkCapabilities.NET_CAPABILITY_FOTA: |
| 996 | case NetworkCapabilities.NET_CAPABILITY_IA: |
| 997 | case NetworkCapabilities.NET_CAPABILITY_IMS: |
Lorenzo Colitti | ea1984f | 2014-06-04 19:59:21 +0900 | [diff] [blame] | 998 | case NetworkCapabilities.NET_CAPABILITY_RCS: |
| 999 | case NetworkCapabilities.NET_CAPABILITY_XCAP: |
Robert Greenwalt | 9ac3dbe | 2014-06-05 16:39:28 -0700 | [diff] [blame] | 1000 | case NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED: //there by default |
Lorenzo Colitti | ea1984f | 2014-06-04 19:59:21 +0900 | [diff] [blame] | 1001 | continue; |
| 1002 | default: |
| 1003 | // At least one capability usually provided by unrestricted |
| 1004 | // networks. Conclude that this network is unrestricted. |
| 1005 | return; |
Lorenzo Colitti | 4077acb | 2014-06-04 12:20:06 +0900 | [diff] [blame] | 1006 | } |
| 1007 | } |
Lorenzo Colitti | ea1984f | 2014-06-04 19:59:21 +0900 | [diff] [blame] | 1008 | // All the capabilities are typically provided by restricted networks. |
| 1009 | // Conclude that this network is restricted. |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 1010 | nc.removeCapability(NetworkCapabilities.NET_CAPABILITY_NOT_RESTRICTED); |
Lorenzo Colitti | 4077acb | 2014-06-04 12:20:06 +0900 | [diff] [blame] | 1011 | } |
| 1012 | |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1013 | private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) { |
| 1014 | if (networkType == TYPE_MOBILE) { |
| 1015 | int cap = -1; |
| 1016 | if ("enableMMS".equals(feature)) { |
| 1017 | cap = NetworkCapabilities.NET_CAPABILITY_MMS; |
| 1018 | } else if ("enableSUPL".equals(feature)) { |
| 1019 | cap = NetworkCapabilities.NET_CAPABILITY_SUPL; |
| 1020 | } else if ("enableDUN".equals(feature) || "enableDUNAlways".equals(feature)) { |
| 1021 | cap = NetworkCapabilities.NET_CAPABILITY_DUN; |
| 1022 | } else if ("enableHIPRI".equals(feature)) { |
| 1023 | cap = NetworkCapabilities.NET_CAPABILITY_INTERNET; |
| 1024 | } else if ("enableFOTA".equals(feature)) { |
| 1025 | cap = NetworkCapabilities.NET_CAPABILITY_FOTA; |
| 1026 | } else if ("enableIMS".equals(feature)) { |
| 1027 | cap = NetworkCapabilities.NET_CAPABILITY_IMS; |
| 1028 | } else if ("enableCBS".equals(feature)) { |
| 1029 | cap = NetworkCapabilities.NET_CAPABILITY_CBS; |
| 1030 | } else { |
| 1031 | return null; |
| 1032 | } |
| 1033 | NetworkCapabilities netCap = new NetworkCapabilities(); |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 1034 | netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(cap); |
Lorenzo Colitti | 4077acb | 2014-06-04 12:20:06 +0900 | [diff] [blame] | 1035 | maybeMarkCapabilitiesRestricted(netCap); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1036 | return netCap; |
| 1037 | } else if (networkType == TYPE_WIFI) { |
| 1038 | if ("p2p".equals(feature)) { |
| 1039 | NetworkCapabilities netCap = new NetworkCapabilities(); |
| 1040 | netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 1041 | netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P); |
Lorenzo Colitti | 4077acb | 2014-06-04 12:20:06 +0900 | [diff] [blame] | 1042 | maybeMarkCapabilitiesRestricted(netCap); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1043 | return netCap; |
| 1044 | } |
| 1045 | } |
| 1046 | return null; |
| 1047 | } |
| 1048 | |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 1049 | /** |
| 1050 | * Guess what the network request was trying to say so that the resulting |
| 1051 | * network is accessible via the legacy (deprecated) API such as |
| 1052 | * requestRouteToHost. |
| 1053 | * This means we should try to be fairly preceise about transport and |
| 1054 | * capability but ignore things such as networkSpecifier. |
| 1055 | * If the request has more than one transport or capability it doesn't |
| 1056 | * match the old legacy requests (they selected only single transport/capability) |
| 1057 | * so this function cannot map the request to a single legacy type and |
| 1058 | * the resulting network will not be available to the legacy APIs. |
| 1059 | * |
| 1060 | * TODO - This should be removed when the legacy APIs are removed. |
| 1061 | */ |
Ye Wen | b87875e | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 1062 | private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) { |
| 1063 | if (netCap == null) { |
| 1064 | return TYPE_NONE; |
| 1065 | } |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 1066 | |
Ye Wen | b87875e | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 1067 | if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { |
| 1068 | return TYPE_NONE; |
| 1069 | } |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 1070 | |
| 1071 | String type = null; |
| 1072 | int result = TYPE_NONE; |
| 1073 | |
Ye Wen | b87875e | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 1074 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) { |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 1075 | type = "enableCBS"; |
| 1076 | result = TYPE_MOBILE_CBS; |
| 1077 | } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) { |
| 1078 | type = "enableIMS"; |
| 1079 | result = TYPE_MOBILE_IMS; |
| 1080 | } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) { |
| 1081 | type = "enableFOTA"; |
| 1082 | result = TYPE_MOBILE_FOTA; |
| 1083 | } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) { |
| 1084 | type = "enableDUN"; |
| 1085 | result = TYPE_MOBILE_DUN; |
| 1086 | } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) { |
| 1087 | type = "enableSUPL"; |
| 1088 | result = TYPE_MOBILE_SUPL; |
| 1089 | } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) { |
| 1090 | type = "enableMMS"; |
| 1091 | result = TYPE_MOBILE_MMS; |
| 1092 | } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) { |
| 1093 | type = "enableHIPRI"; |
| 1094 | result = TYPE_MOBILE_HIPRI; |
Ye Wen | b87875e | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 1095 | } |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 1096 | if (type != null) { |
| 1097 | NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type); |
| 1098 | if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) { |
| 1099 | return result; |
Ye Wen | b87875e | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 1100 | } |
| 1101 | } |
| 1102 | return TYPE_NONE; |
| 1103 | } |
| 1104 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 1105 | private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) { |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1106 | if (netCap == null) return TYPE_NONE; |
| 1107 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) { |
| 1108 | return TYPE_MOBILE_CBS; |
| 1109 | } |
| 1110 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) { |
| 1111 | return TYPE_MOBILE_IMS; |
| 1112 | } |
| 1113 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) { |
| 1114 | return TYPE_MOBILE_FOTA; |
| 1115 | } |
| 1116 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) { |
| 1117 | return TYPE_MOBILE_DUN; |
| 1118 | } |
| 1119 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) { |
| 1120 | return TYPE_MOBILE_SUPL; |
| 1121 | } |
| 1122 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) { |
| 1123 | return TYPE_MOBILE_MMS; |
| 1124 | } |
| 1125 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) { |
| 1126 | return TYPE_MOBILE_HIPRI; |
| 1127 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 1128 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) { |
| 1129 | return TYPE_WIFI_P2P; |
| 1130 | } |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1131 | return TYPE_NONE; |
| 1132 | } |
| 1133 | |
| 1134 | private static class LegacyRequest { |
| 1135 | NetworkCapabilities networkCapabilities; |
| 1136 | NetworkRequest networkRequest; |
| 1137 | int expireSequenceNumber; |
| 1138 | Network currentNetwork; |
| 1139 | int delay = -1; |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1140 | |
| 1141 | private void clearDnsBinding() { |
| 1142 | if (currentNetwork != null) { |
| 1143 | currentNetwork = null; |
| 1144 | setProcessDefaultNetworkForHostResolution(null); |
| 1145 | } |
| 1146 | } |
| 1147 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1148 | NetworkCallback networkCallback = new NetworkCallback() { |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1149 | @Override |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1150 | public void onAvailable(Network network) { |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1151 | currentNetwork = network; |
| 1152 | Log.d(TAG, "startUsingNetworkFeature got Network:" + network); |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 1153 | setProcessDefaultNetworkForHostResolution(network); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1154 | } |
| 1155 | @Override |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1156 | public void onLost(Network network) { |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1157 | if (network.equals(currentNetwork)) clearDnsBinding(); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1158 | Log.d(TAG, "startUsingNetworkFeature lost Network:" + network); |
| 1159 | } |
| 1160 | }; |
| 1161 | } |
| 1162 | |
Robert Greenwalt | fab50167 | 2014-07-23 11:44:01 -0700 | [diff] [blame] | 1163 | private static HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests = |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1164 | new HashMap<NetworkCapabilities, LegacyRequest>(); |
| 1165 | |
| 1166 | private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) { |
| 1167 | synchronized (sLegacyRequests) { |
| 1168 | LegacyRequest l = sLegacyRequests.get(netCap); |
| 1169 | if (l != null) return l.networkRequest; |
| 1170 | } |
| 1171 | return null; |
| 1172 | } |
| 1173 | |
| 1174 | private void renewRequestLocked(LegacyRequest l) { |
| 1175 | l.expireSequenceNumber++; |
| 1176 | Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber); |
| 1177 | sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay); |
| 1178 | } |
| 1179 | |
| 1180 | private void expireRequest(NetworkCapabilities netCap, int sequenceNum) { |
| 1181 | int ourSeqNum = -1; |
| 1182 | synchronized (sLegacyRequests) { |
| 1183 | LegacyRequest l = sLegacyRequests.get(netCap); |
| 1184 | if (l == null) return; |
| 1185 | ourSeqNum = l.expireSequenceNumber; |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1186 | if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1187 | } |
| 1188 | Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum); |
| 1189 | } |
| 1190 | |
| 1191 | private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) { |
| 1192 | int delay = -1; |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 1193 | int type = legacyTypeForNetworkCapabilities(netCap); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1194 | try { |
| 1195 | delay = mService.getRestoreDefaultNetworkDelay(type); |
| 1196 | } catch (RemoteException e) {} |
| 1197 | LegacyRequest l = new LegacyRequest(); |
| 1198 | l.networkCapabilities = netCap; |
| 1199 | l.delay = delay; |
| 1200 | l.expireSequenceNumber = 0; |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1201 | l.networkRequest = sendRequestForNetwork(netCap, l.networkCallback, 0, |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 1202 | REQUEST, type); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1203 | if (l.networkRequest == null) return null; |
| 1204 | sLegacyRequests.put(netCap, l); |
| 1205 | sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay); |
| 1206 | return l.networkRequest; |
| 1207 | } |
| 1208 | |
| 1209 | private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) { |
| 1210 | if (delay >= 0) { |
| 1211 | Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay); |
| 1212 | Message msg = sCallbackHandler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap); |
| 1213 | sCallbackHandler.sendMessageDelayed(msg, delay); |
| 1214 | } |
| 1215 | } |
| 1216 | |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1217 | private boolean removeRequestForFeature(NetworkCapabilities netCap) { |
| 1218 | final LegacyRequest l; |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1219 | synchronized (sLegacyRequests) { |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1220 | l = sLegacyRequests.remove(netCap); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1221 | } |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1222 | if (l == null) return false; |
| 1223 | unregisterNetworkCallback(l.networkCallback); |
| 1224 | l.clearDnsBinding(); |
| 1225 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1226 | } |
| 1227 | |
| 1228 | /** |
| 1229 | * Ensure that a network route exists to deliver traffic to the specified |
| 1230 | * host via the specified network interface. An attempt to add a route that |
| 1231 | * already exists is ignored, but treated as successful. |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 1232 | * <p>This method requires the caller to hold the permission |
| 1233 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1234 | * @param networkType the type of the network over which traffic to the specified |
| 1235 | * host is to be routed |
| 1236 | * @param hostAddress the IP address of the host to which the route is desired |
| 1237 | * @return {@code true} on success, {@code false} on failure |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1238 | * |
| 1239 | * @deprecated Deprecated in favor of the {@link #requestNetwork}, |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 1240 | * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} api. |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 1241 | * @removed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1242 | */ |
| 1243 | public boolean requestRouteToHost(int networkType, int hostAddress) { |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1244 | return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress)); |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 1245 | } |
| 1246 | |
| 1247 | /** |
| 1248 | * Ensure that a network route exists to deliver traffic to the specified |
| 1249 | * host via the specified network interface. An attempt to add a route that |
| 1250 | * already exists is ignored, but treated as successful. |
Jake Hamby | 8f9b33e | 2014-01-15 13:08:03 -0800 | [diff] [blame] | 1251 | * <p>This method requires the caller to hold the permission |
| 1252 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}. |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 1253 | * @param networkType the type of the network over which traffic to the specified |
| 1254 | * host is to be routed |
| 1255 | * @param hostAddress the IP address of the host to which the route is desired |
| 1256 | * @return {@code true} on success, {@code false} on failure |
| 1257 | * @hide |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1258 | * @deprecated Deprecated in favor of the {@link #requestNetwork} and |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 1259 | * {@link #bindProcessToNetwork} api. |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 1260 | * @removed |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 1261 | */ |
| 1262 | public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1263 | try { |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1264 | return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1265 | } catch (RemoteException e) { |
| 1266 | return false; |
| 1267 | } |
| 1268 | } |
| 1269 | |
| 1270 | /** |
| 1271 | * Returns the value of the setting for background data usage. If false, |
| 1272 | * applications should not use the network if the application is not in the |
| 1273 | * foreground. Developers should respect this setting, and check the value |
| 1274 | * of this before performing any background data operations. |
| 1275 | * <p> |
| 1276 | * All applications that have background services that use the network |
| 1277 | * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}. |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1278 | * <p> |
Scott Main | 4cc5333 | 2011-10-06 18:32:43 -0700 | [diff] [blame] | 1279 | * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1280 | * background data depends on several combined factors, and this method will |
| 1281 | * always return {@code true}. Instead, when background data is unavailable, |
| 1282 | * {@link #getActiveNetworkInfo()} will now appear disconnected. |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 1283 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1284 | * @return Whether background data usage is allowed. |
| 1285 | */ |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1286 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1287 | public boolean getBackgroundDataSetting() { |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1288 | // assume that background data is allowed; final authority is |
| 1289 | // NetworkInfo which may be blocked. |
| 1290 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | /** |
| 1294 | * Sets the value of the setting for background data usage. |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1295 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1296 | * @param allowBackgroundData Whether an application should use data while |
| 1297 | * it is in the background. |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1298 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1299 | * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING |
| 1300 | * @see #getBackgroundDataSetting() |
| 1301 | * @hide |
| 1302 | */ |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1303 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1304 | public void setBackgroundDataSetting(boolean allowBackgroundData) { |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1305 | // ignored |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1306 | } |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1307 | |
| 1308 | /** |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1309 | * Return quota status for the current active network, or {@code null} if no |
| 1310 | * network is active. Quota status can change rapidly, so these values |
| 1311 | * shouldn't be cached. |
Jeff Sharkey | 44a3e0d | 2011-10-06 10:50:09 -0700 | [diff] [blame] | 1312 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1313 | * <p>This method requires the caller to hold the permission |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1314 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
| 1315 | * |
Jeff Sharkey | 44a3e0d | 2011-10-06 10:50:09 -0700 | [diff] [blame] | 1316 | * @hide |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1317 | */ |
| 1318 | public NetworkQuotaInfo getActiveNetworkQuotaInfo() { |
| 1319 | try { |
| 1320 | return mService.getActiveNetworkQuotaInfo(); |
| 1321 | } catch (RemoteException e) { |
| 1322 | return null; |
| 1323 | } |
| 1324 | } |
| 1325 | |
| 1326 | /** |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1327 | * @hide |
Robert Greenwalt | afa05c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 1328 | * @deprecated Talk to TelephonyManager directly |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1329 | */ |
| 1330 | public boolean getMobileDataEnabled() { |
Robert Greenwalt | afa05c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 1331 | IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE); |
| 1332 | if (b != null) { |
| 1333 | try { |
| 1334 | ITelephony it = ITelephony.Stub.asInterface(b); |
Wink Saville | 36ffb04 | 2014-12-05 11:10:30 -0800 | [diff] [blame] | 1335 | int subId = SubscriptionManager.getDefaultDataSubId(); |
| 1336 | Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId); |
| 1337 | boolean retVal = it.getDataEnabled(subId); |
| 1338 | Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId |
| 1339 | + " retVal=" + retVal); |
| 1340 | return retVal; |
Robert Greenwalt | afa05c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 1341 | } catch (RemoteException e) { } |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1342 | } |
Wink Saville | 36ffb04 | 2014-12-05 11:10:30 -0800 | [diff] [blame] | 1343 | Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false"); |
Robert Greenwalt | afa05c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 1344 | return false; |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1345 | } |
| 1346 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1347 | /** |
Robert Greenwalt | b248987 | 2014-09-04 16:44:35 -0700 | [diff] [blame] | 1348 | * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener} |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1349 | * to find out when the system default network has gone in to a high power state. |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1350 | */ |
| 1351 | public interface OnNetworkActiveListener { |
| 1352 | /** |
| 1353 | * Called on the main thread of the process to report that the current data network |
| 1354 | * has become active, and it is now a good time to perform any pending network |
| 1355 | * operations. Note that this listener only tells you when the network becomes |
| 1356 | * active; if at any other time you want to know whether it is active (and thus okay |
| 1357 | * to initiate network traffic), you can retrieve its instantaneous state with |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1358 | * {@link ConnectivityManager#isDefaultNetworkActive}. |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1359 | */ |
| 1360 | public void onNetworkActive(); |
| 1361 | } |
| 1362 | |
| 1363 | private INetworkManagementService getNetworkManagementService() { |
| 1364 | synchronized (this) { |
| 1365 | if (mNMService != null) { |
| 1366 | return mNMService; |
| 1367 | } |
| 1368 | IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE); |
| 1369 | mNMService = INetworkManagementService.Stub.asInterface(b); |
| 1370 | return mNMService; |
| 1371 | } |
| 1372 | } |
| 1373 | |
| 1374 | private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener> |
| 1375 | mNetworkActivityListeners |
| 1376 | = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>(); |
| 1377 | |
| 1378 | /** |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1379 | * Start listening to reports when the system's default data network is active, meaning it is |
| 1380 | * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()} |
| 1381 | * to determine the current state of the system's default network after registering the |
| 1382 | * listener. |
| 1383 | * <p> |
| 1384 | * If the process default network has been set with |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 1385 | * {@link ConnectivityManager#bindProcessToNetwork} this function will not |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1386 | * reflect the process's default, but the system default. |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1387 | * |
| 1388 | * @param l The listener to be told when the network is active. |
| 1389 | */ |
Robert Greenwalt | b248987 | 2014-09-04 16:44:35 -0700 | [diff] [blame] | 1390 | public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1391 | INetworkActivityListener rl = new INetworkActivityListener.Stub() { |
| 1392 | @Override |
| 1393 | public void onNetworkActive() throws RemoteException { |
| 1394 | l.onNetworkActive(); |
| 1395 | } |
| 1396 | }; |
| 1397 | |
| 1398 | try { |
| 1399 | getNetworkManagementService().registerNetworkActivityListener(rl); |
| 1400 | mNetworkActivityListeners.put(l, rl); |
| 1401 | } catch (RemoteException e) { |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | /** |
| 1406 | * Remove network active listener previously registered with |
Robert Greenwalt | b248987 | 2014-09-04 16:44:35 -0700 | [diff] [blame] | 1407 | * {@link #addDefaultNetworkActiveListener}. |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1408 | * |
| 1409 | * @param l Previously registered listener. |
| 1410 | */ |
Robert Greenwalt | b248987 | 2014-09-04 16:44:35 -0700 | [diff] [blame] | 1411 | public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1412 | INetworkActivityListener rl = mNetworkActivityListeners.get(l); |
| 1413 | if (rl == null) { |
| 1414 | throw new IllegalArgumentException("Listener not registered: " + l); |
| 1415 | } |
| 1416 | try { |
| 1417 | getNetworkManagementService().unregisterNetworkActivityListener(rl); |
| 1418 | } catch (RemoteException e) { |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | /** |
| 1423 | * Return whether the data network is currently active. An active network means that |
| 1424 | * it is currently in a high power state for performing data transmission. On some |
| 1425 | * types of networks, it may be expensive to move and stay in such a state, so it is |
| 1426 | * more power efficient to batch network traffic together when the radio is already in |
| 1427 | * this state. This method tells you whether right now is currently a good time to |
| 1428 | * initiate network traffic, as the network is already active. |
| 1429 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1430 | public boolean isDefaultNetworkActive() { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1431 | try { |
| 1432 | return getNetworkManagementService().isNetworkActive(); |
| 1433 | } catch (RemoteException e) { |
| 1434 | } |
| 1435 | return false; |
| 1436 | } |
| 1437 | |
| 1438 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1439 | * {@hide} |
| 1440 | */ |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1441 | public ConnectivityManager(IConnectivityManager service) { |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1442 | mService = checkNotNull(service, "missing IConnectivityManager"); |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 1443 | sInstance = this; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1444 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1445 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1446 | /** {@hide} */ |
| 1447 | public static ConnectivityManager from(Context context) { |
| 1448 | return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 1449 | } |
| 1450 | |
Robert Greenwalt | edb4766 | 2014-09-16 17:54:19 -0700 | [diff] [blame] | 1451 | /** {@hide */ |
| 1452 | public static final void enforceTetherChangePermission(Context context) { |
| 1453 | if (context.getResources().getStringArray( |
| 1454 | com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) { |
| 1455 | // Have a provisioning app - must only let system apps (which check this app) |
| 1456 | // turn on tethering |
| 1457 | context.enforceCallingOrSelfPermission( |
| 1458 | android.Manifest.permission.CONNECTIVITY_INTERNAL, "ConnectivityService"); |
| 1459 | } else { |
| 1460 | context.enforceCallingOrSelfPermission( |
| 1461 | android.Manifest.permission.CHANGE_NETWORK_STATE, "ConnectivityService"); |
| 1462 | } |
| 1463 | } |
| 1464 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1465 | /** |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 1466 | * @deprecated - use getSystemService. This is a kludge to support static access in certain |
| 1467 | * situations where a Context pointer is unavailable. |
| 1468 | * @hide |
| 1469 | */ |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 1470 | static ConnectivityManager getInstanceOrNull() { |
| 1471 | return sInstance; |
| 1472 | } |
| 1473 | |
| 1474 | /** |
| 1475 | * @deprecated - use getSystemService. This is a kludge to support static access in certain |
| 1476 | * situations where a Context pointer is unavailable. |
| 1477 | * @hide |
| 1478 | */ |
| 1479 | private static ConnectivityManager getInstance() { |
| 1480 | if (getInstanceOrNull() == null) { |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 1481 | throw new IllegalStateException("No ConnectivityManager yet constructed"); |
| 1482 | } |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 1483 | return getInstanceOrNull(); |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 1484 | } |
| 1485 | |
| 1486 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1487 | * Get the set of tetherable, available interfaces. This list is limited by |
| 1488 | * device configuration and current interface existence. |
| 1489 | * |
| 1490 | * @return an array of 0 or more Strings of tetherable interface names. |
| 1491 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1492 | * <p>This method requires the caller to hold the permission |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1493 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1494 | * {@hide} |
| 1495 | */ |
| 1496 | public String[] getTetherableIfaces() { |
| 1497 | try { |
| 1498 | return mService.getTetherableIfaces(); |
| 1499 | } catch (RemoteException e) { |
| 1500 | return new String[0]; |
| 1501 | } |
| 1502 | } |
| 1503 | |
| 1504 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1505 | * Get the set of tethered interfaces. |
| 1506 | * |
| 1507 | * @return an array of 0 or more String of currently tethered interface names. |
| 1508 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1509 | * <p>This method requires the caller to hold the permission |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1510 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1511 | * {@hide} |
| 1512 | */ |
| 1513 | public String[] getTetheredIfaces() { |
| 1514 | try { |
| 1515 | return mService.getTetheredIfaces(); |
| 1516 | } catch (RemoteException e) { |
| 1517 | return new String[0]; |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1522 | * Get the set of interface names which attempted to tether but |
| 1523 | * failed. Re-attempting to tether may cause them to reset to the Tethered |
| 1524 | * state. Alternatively, causing the interface to be destroyed and recreated |
| 1525 | * may cause them to reset to the available state. |
| 1526 | * {@link ConnectivityManager#getLastTetherError} can be used to get more |
| 1527 | * information on the cause of the errors. |
| 1528 | * |
| 1529 | * @return an array of 0 or more String indicating the interface names |
| 1530 | * which failed to tether. |
| 1531 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1532 | * <p>This method requires the caller to hold the permission |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1533 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1534 | * {@hide} |
| 1535 | */ |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1536 | public String[] getTetheringErroredIfaces() { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1537 | try { |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1538 | return mService.getTetheringErroredIfaces(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1539 | } catch (RemoteException e) { |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1540 | return new String[0]; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | /** |
Robert Greenwalt | 9c7e2c2 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 1545 | * Get the set of tethered dhcp ranges. |
| 1546 | * |
| 1547 | * @return an array of 0 or more {@code String} of tethered dhcp ranges. |
| 1548 | * {@hide} |
| 1549 | */ |
| 1550 | public String[] getTetheredDhcpRanges() { |
| 1551 | try { |
| 1552 | return mService.getTetheredDhcpRanges(); |
| 1553 | } catch (RemoteException e) { |
| 1554 | return new String[0]; |
| 1555 | } |
| 1556 | } |
| 1557 | |
| 1558 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1559 | * Attempt to tether the named interface. This will setup a dhcp server |
| 1560 | * on the interface, forward and NAT IP packets and forward DNS requests |
| 1561 | * to the best active upstream network interface. Note that if no upstream |
| 1562 | * IP network interface is available, dhcp will still run and traffic will be |
| 1563 | * allowed between the tethered devices and this device, though upstream net |
| 1564 | * access will of course fail until an upstream network interface becomes |
| 1565 | * active. |
| 1566 | * |
| 1567 | * @param iface the interface name to tether. |
| 1568 | * @return error a {@code TETHER_ERROR} value indicating success or failure type |
| 1569 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1570 | * <p>This method requires the caller to hold the permission |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1571 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}. |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1572 | * {@hide} |
| 1573 | */ |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1574 | public int tether(String iface) { |
| 1575 | try { |
| 1576 | return mService.tether(iface); |
| 1577 | } catch (RemoteException e) { |
| 1578 | return TETHER_ERROR_SERVICE_UNAVAIL; |
| 1579 | } |
| 1580 | } |
| 1581 | |
| 1582 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1583 | * Stop tethering the named interface. |
| 1584 | * |
| 1585 | * @param iface the interface name to untether. |
| 1586 | * @return error a {@code TETHER_ERROR} value indicating success or failure type |
| 1587 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1588 | * <p>This method requires the caller to hold the permission |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1589 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}. |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1590 | * {@hide} |
| 1591 | */ |
| 1592 | public int untether(String iface) { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1593 | try { |
| 1594 | return mService.untether(iface); |
| 1595 | } catch (RemoteException e) { |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1596 | return TETHER_ERROR_SERVICE_UNAVAIL; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1597 | } |
| 1598 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1599 | |
| 1600 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1601 | * Check if the device allows for tethering. It may be disabled via |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1602 | * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1603 | * due to device configuration. |
| 1604 | * |
| 1605 | * @return a boolean - {@code true} indicating Tethering is supported. |
| 1606 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1607 | * <p>This method requires the caller to hold the permission |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1608 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1609 | * {@hide} |
| 1610 | */ |
| 1611 | public boolean isTetheringSupported() { |
| 1612 | try { |
| 1613 | return mService.isTetheringSupported(); |
| 1614 | } catch (RemoteException e) { |
| 1615 | return false; |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1620 | * Get the list of regular expressions that define any tetherable |
| 1621 | * USB network interfaces. If USB tethering is not supported by the |
| 1622 | * device, this list should be empty. |
| 1623 | * |
| 1624 | * @return an array of 0 or more regular expression Strings defining |
| 1625 | * what interfaces are considered tetherable usb interfaces. |
| 1626 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1627 | * <p>This method requires the caller to hold the permission |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1628 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1629 | * {@hide} |
| 1630 | */ |
| 1631 | public String[] getTetherableUsbRegexs() { |
| 1632 | try { |
| 1633 | return mService.getTetherableUsbRegexs(); |
| 1634 | } catch (RemoteException e) { |
| 1635 | return new String[0]; |
| 1636 | } |
| 1637 | } |
| 1638 | |
| 1639 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1640 | * Get the list of regular expressions that define any tetherable |
| 1641 | * Wifi network interfaces. If Wifi tethering is not supported by the |
| 1642 | * device, this list should be empty. |
| 1643 | * |
| 1644 | * @return an array of 0 or more regular expression Strings defining |
| 1645 | * what interfaces are considered tetherable wifi interfaces. |
| 1646 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1647 | * <p>This method requires the caller to hold the permission |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1648 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1649 | * {@hide} |
| 1650 | */ |
| 1651 | public String[] getTetherableWifiRegexs() { |
| 1652 | try { |
| 1653 | return mService.getTetherableWifiRegexs(); |
| 1654 | } catch (RemoteException e) { |
| 1655 | return new String[0]; |
| 1656 | } |
| 1657 | } |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1658 | |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 1659 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1660 | * Get the list of regular expressions that define any tetherable |
| 1661 | * Bluetooth network interfaces. If Bluetooth tethering is not supported by the |
| 1662 | * device, this list should be empty. |
| 1663 | * |
| 1664 | * @return an array of 0 or more regular expression Strings defining |
| 1665 | * what interfaces are considered tetherable bluetooth interfaces. |
| 1666 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1667 | * <p>This method requires the caller to hold the permission |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1668 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 1669 | * {@hide} |
| 1670 | */ |
| 1671 | public String[] getTetherableBluetoothRegexs() { |
| 1672 | try { |
| 1673 | return mService.getTetherableBluetoothRegexs(); |
| 1674 | } catch (RemoteException e) { |
| 1675 | return new String[0]; |
| 1676 | } |
| 1677 | } |
| 1678 | |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1679 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1680 | * Attempt to both alter the mode of USB and Tethering of USB. A |
| 1681 | * utility method to deal with some of the complexity of USB - will |
| 1682 | * attempt to switch to Rndis and subsequently tether the resulting |
| 1683 | * interface on {@code true} or turn off tethering and switch off |
| 1684 | * Rndis on {@code false}. |
| 1685 | * |
| 1686 | * @param enable a boolean - {@code true} to enable tethering |
| 1687 | * @return error a {@code TETHER_ERROR} value indicating success or failure type |
| 1688 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1689 | * <p>This method requires the caller to hold the permission |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1690 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}. |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1691 | * {@hide} |
| 1692 | */ |
| 1693 | public int setUsbTethering(boolean enable) { |
| 1694 | try { |
| 1695 | return mService.setUsbTethering(enable); |
| 1696 | } catch (RemoteException e) { |
| 1697 | return TETHER_ERROR_SERVICE_UNAVAIL; |
| 1698 | } |
| 1699 | } |
| 1700 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1701 | /** {@hide} */ |
| 1702 | public static final int TETHER_ERROR_NO_ERROR = 0; |
| 1703 | /** {@hide} */ |
| 1704 | public static final int TETHER_ERROR_UNKNOWN_IFACE = 1; |
| 1705 | /** {@hide} */ |
| 1706 | public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2; |
| 1707 | /** {@hide} */ |
| 1708 | public static final int TETHER_ERROR_UNSUPPORTED = 3; |
| 1709 | /** {@hide} */ |
| 1710 | public static final int TETHER_ERROR_UNAVAIL_IFACE = 4; |
| 1711 | /** {@hide} */ |
| 1712 | public static final int TETHER_ERROR_MASTER_ERROR = 5; |
| 1713 | /** {@hide} */ |
| 1714 | public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6; |
| 1715 | /** {@hide} */ |
| 1716 | public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7; |
| 1717 | /** {@hide} */ |
| 1718 | public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8; |
| 1719 | /** {@hide} */ |
| 1720 | public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9; |
| 1721 | /** {@hide} */ |
| 1722 | public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10; |
| 1723 | |
| 1724 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1725 | * Get a more detailed error code after a Tethering or Untethering |
| 1726 | * request asynchronously failed. |
| 1727 | * |
| 1728 | * @param iface The name of the interface of interest |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1729 | * @return error The error code of the last error tethering or untethering the named |
| 1730 | * interface |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1731 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1732 | * <p>This method requires the caller to hold the permission |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1733 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1734 | * {@hide} |
| 1735 | */ |
| 1736 | public int getLastTetherError(String iface) { |
| 1737 | try { |
| 1738 | return mService.getLastTetherError(iface); |
| 1739 | } catch (RemoteException e) { |
| 1740 | return TETHER_ERROR_SERVICE_UNAVAIL; |
| 1741 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1742 | } |
| 1743 | |
| 1744 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1745 | * Report network connectivity status. This is currently used only |
| 1746 | * to alter status bar UI. |
| 1747 | * |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 1748 | * @param networkType The type of network you want to report on |
| 1749 | * @param percentage The quality of the connection 0 is bad, 100 is good |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1750 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1751 | * <p>This method requires the caller to hold the permission |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1752 | * {@link android.Manifest.permission#STATUS_BAR}. |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 1753 | * {@hide} |
| 1754 | */ |
| 1755 | public void reportInetCondition(int networkType, int percentage) { |
| 1756 | try { |
| 1757 | mService.reportInetCondition(networkType, percentage); |
| 1758 | } catch (RemoteException e) { |
| 1759 | } |
| 1760 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1761 | |
| 1762 | /** |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1763 | * Report a problem network to the framework. This provides a hint to the system |
Ye Wen | b87875e | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 1764 | * that there might be connectivity problems on this network and may cause |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1765 | * the framework to re-evaluate network connectivity and/or switch to another |
| 1766 | * network. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1767 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1768 | * @param network The {@link Network} the application was attempting to use |
| 1769 | * or {@code null} to indicate the current default network. |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 1770 | * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both |
| 1771 | * working and non-working connectivity. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1772 | */ |
| 1773 | public void reportBadNetwork(Network network) { |
| 1774 | try { |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 1775 | // One of these will be ignored because it matches system's current state. |
| 1776 | // The other will trigger the necessary reevaluation. |
| 1777 | mService.reportNetworkConnectivity(network, true); |
| 1778 | mService.reportNetworkConnectivity(network, false); |
| 1779 | } catch (RemoteException e) { |
| 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | /** |
| 1784 | * Report to the framework whether a network has working connectivity. |
| 1785 | * This provides a hint to the system that a particular network is providing |
| 1786 | * working connectivity or not. In response the framework may re-evaluate |
| 1787 | * the network's connectivity and might take further action thereafter. |
| 1788 | * |
| 1789 | * @param network The {@link Network} the application was attempting to use |
| 1790 | * or {@code null} to indicate the current default network. |
| 1791 | * @param hasConnectivity {@code true} if the application was able to successfully access the |
| 1792 | * Internet using {@code network} or {@code false} if not. |
| 1793 | */ |
| 1794 | public void reportNetworkConnectivity(Network network, boolean hasConnectivity) { |
| 1795 | try { |
| 1796 | mService.reportNetworkConnectivity(network, hasConnectivity); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1797 | } catch (RemoteException e) { |
| 1798 | } |
| 1799 | } |
| 1800 | |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 1801 | /** {@hide} */ |
| 1802 | public static final int CAPTIVE_PORTAL_APP_RETURN_DISMISSED = 0; |
| 1803 | /** {@hide} */ |
| 1804 | public static final int CAPTIVE_PORTAL_APP_RETURN_UNWANTED = 1; |
| 1805 | /** {@hide} */ |
| 1806 | public static final int CAPTIVE_PORTAL_APP_RETURN_WANTED_AS_IS = 2; |
| 1807 | |
| 1808 | /** |
| 1809 | * Called by an app handling the {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} |
| 1810 | * action to indicate to the system that the captive portal has been |
| 1811 | * dismissed. In response the framework will re-evaluate the network's |
| 1812 | * connectivity and might take further action thereafter. |
| 1813 | * |
| 1814 | * @param network The {@link Network} object passed via |
| 1815 | * {@link #EXTRA_NETWORK} with the |
| 1816 | * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} action. |
| 1817 | * @param actionToken The {@code String} passed via |
| 1818 | * {@link #EXTRA_CAPTIVE_PORTAL_TOKEN} with the |
| 1819 | * {@code ACTION_CAPTIVE_PORTAL_SIGN_IN} action. |
| 1820 | */ |
| 1821 | public void reportCaptivePortalDismissed(Network network, String actionToken) { |
| 1822 | try { |
| 1823 | mService.captivePortalAppResponse(network, CAPTIVE_PORTAL_APP_RETURN_DISMISSED, |
| 1824 | actionToken); |
| 1825 | } catch (RemoteException e) { |
| 1826 | } |
| 1827 | } |
| 1828 | |
| 1829 | /** |
| 1830 | * Called by an app handling the {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} |
| 1831 | * action to indicate that the user does not want to pursue signing in to |
| 1832 | * captive portal and the system should continue to prefer other networks |
| 1833 | * without captive portals for use as the default active data network. The |
| 1834 | * system will not retest the network for a captive portal so as to avoid |
| 1835 | * disturbing the user with further sign in to network notifications. |
| 1836 | * |
| 1837 | * @param network The {@link Network} object passed via |
| 1838 | * {@link #EXTRA_NETWORK} with the |
| 1839 | * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} action. |
| 1840 | * @param actionToken The {@code String} passed via |
| 1841 | * {@link #EXTRA_CAPTIVE_PORTAL_TOKEN} with the |
| 1842 | * {@code ACTION_CAPTIVE_PORTAL_SIGN_IN} action. |
| 1843 | */ |
| 1844 | public void ignoreNetworkWithCaptivePortal(Network network, String actionToken) { |
| 1845 | try { |
| 1846 | mService.captivePortalAppResponse(network, CAPTIVE_PORTAL_APP_RETURN_UNWANTED, |
| 1847 | actionToken); |
| 1848 | } catch (RemoteException e) { |
| 1849 | } |
| 1850 | } |
| 1851 | |
| 1852 | /** |
| 1853 | * Called by an app handling the {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} |
| 1854 | * action to indicate the user wants to use this network as is, even though |
| 1855 | * the captive portal is still in place. The system will treat the network |
| 1856 | * as if it did not have a captive portal when selecting the network to use |
| 1857 | * as the default active data network. This may result in this network |
| 1858 | * becoming the default active data network, which could disrupt network |
| 1859 | * connectivity for apps because the captive portal is still in place. |
| 1860 | * |
| 1861 | * @param network The {@link Network} object passed via |
| 1862 | * {@link #EXTRA_NETWORK} with the |
| 1863 | * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} action. |
| 1864 | * @param actionToken The {@code String} passed via |
| 1865 | * {@link #EXTRA_CAPTIVE_PORTAL_TOKEN} with the |
| 1866 | * {@code ACTION_CAPTIVE_PORTAL_SIGN_IN} action. |
| 1867 | * @hide |
| 1868 | */ |
| 1869 | public void useNetworkWithCaptivePortal(Network network, String actionToken) { |
| 1870 | try { |
| 1871 | mService.captivePortalAppResponse(network, CAPTIVE_PORTAL_APP_RETURN_WANTED_AS_IS, |
| 1872 | actionToken); |
| 1873 | } catch (RemoteException e) { |
| 1874 | } |
| 1875 | } |
| 1876 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1877 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1878 | * Set a network-independent global http proxy. This is not normally what you want |
| 1879 | * for typical HTTP proxies - they are general network dependent. However if you're |
| 1880 | * doing something unusual like general internal filtering this may be useful. On |
| 1881 | * a private network where the proxy is not accessible, you may break HTTP using this. |
| 1882 | * |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 1883 | * @param p The a {@link ProxyInfo} object defining the new global |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1884 | * HTTP proxy. A {@code null} value will clear the global HTTP proxy. |
| 1885 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1886 | * <p>This method requires the caller to hold the permission |
Jason Monk | faf3fd5 | 2014-05-07 18:41:13 -0400 | [diff] [blame] | 1887 | * android.Manifest.permission#CONNECTIVITY_INTERNAL. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1888 | * @hide |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1889 | */ |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 1890 | public void setGlobalProxy(ProxyInfo p) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1891 | try { |
| 1892 | mService.setGlobalProxy(p); |
| 1893 | } catch (RemoteException e) { |
| 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1898 | * Retrieve any network-independent global HTTP proxy. |
| 1899 | * |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 1900 | * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null} |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1901 | * if no global HTTP proxy is set. |
| 1902 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1903 | * <p>This method requires the caller to hold the permission |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1904 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1905 | * @hide |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1906 | */ |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 1907 | public ProxyInfo getGlobalProxy() { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1908 | try { |
| 1909 | return mService.getGlobalProxy(); |
| 1910 | } catch (RemoteException e) { |
| 1911 | return null; |
| 1912 | } |
| 1913 | } |
| 1914 | |
| 1915 | /** |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 1916 | * Get the current default HTTP proxy settings. If a global proxy is set it will be returned, |
| 1917 | * otherwise if this process is bound to a {@link Network} using |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 1918 | * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 1919 | * the default network's proxy is returned. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1920 | * |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 1921 | * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1922 | * HTTP proxy is active. |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1923 | */ |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 1924 | public ProxyInfo getDefaultProxy() { |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 1925 | final Network network = getBoundNetworkForProcess(); |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 1926 | if (network != null) { |
| 1927 | final ProxyInfo globalProxy = getGlobalProxy(); |
| 1928 | if (globalProxy != null) return globalProxy; |
| 1929 | final LinkProperties lp = getLinkProperties(network); |
| 1930 | if (lp != null) return lp.getHttpProxy(); |
| 1931 | return null; |
| 1932 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1933 | try { |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 1934 | return mService.getDefaultProxy(); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1935 | } catch (RemoteException e) { |
| 1936 | return null; |
| 1937 | } |
| 1938 | } |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1939 | |
| 1940 | /** |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1941 | * Returns true if the hardware supports the given network type |
| 1942 | * else it returns false. This doesn't indicate we have coverage |
| 1943 | * or are authorized onto a network, just whether or not the |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1944 | * hardware supports it. For example a GSM phone without a SIM |
| 1945 | * should still return {@code true} for mobile data, but a wifi only |
| 1946 | * tablet would return {@code false}. |
| 1947 | * |
| 1948 | * @param networkType The network type we'd like to check |
| 1949 | * @return {@code true} if supported, else {@code false} |
| 1950 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1951 | * <p>This method requires the caller to hold the permission |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1952 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1953 | * @hide |
| 1954 | */ |
| 1955 | public boolean isNetworkSupported(int networkType) { |
| 1956 | try { |
| 1957 | return mService.isNetworkSupported(networkType); |
| 1958 | } catch (RemoteException e) {} |
| 1959 | return false; |
| 1960 | } |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1961 | |
| 1962 | /** |
| 1963 | * Returns if the currently active data network is metered. A network is |
| 1964 | * classified as metered when the user is sensitive to heavy data usage on |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1965 | * that connection due to monetary costs, data limitations or |
| 1966 | * battery/performance issues. You should check this before doing large |
| 1967 | * data transfers, and warn the user or delay the operation until another |
| 1968 | * network is available. |
| 1969 | * |
| 1970 | * @return {@code true} if large transfers should be avoided, otherwise |
| 1971 | * {@code false}. |
| 1972 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1973 | * <p>This method requires the caller to hold the permission |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1974 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1975 | */ |
| 1976 | public boolean isActiveNetworkMetered() { |
| 1977 | try { |
| 1978 | return mService.isActiveNetworkMetered(); |
| 1979 | } catch (RemoteException e) { |
| 1980 | return false; |
| 1981 | } |
| 1982 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1983 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1984 | /** |
| 1985 | * If the LockdownVpn mechanism is enabled, updates the vpn |
| 1986 | * with a reload of its profile. |
| 1987 | * |
| 1988 | * @return a boolean with {@code} indicating success |
| 1989 | * |
| 1990 | * <p>This method can only be called by the system UID |
| 1991 | * {@hide} |
| 1992 | */ |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 1993 | public boolean updateLockdownVpn() { |
| 1994 | try { |
| 1995 | return mService.updateLockdownVpn(); |
| 1996 | } catch (RemoteException e) { |
| 1997 | return false; |
| 1998 | } |
| 1999 | } |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2000 | |
| 2001 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2002 | * Signal that the captive portal check on the indicated network |
Wink Saville | d747cbc | 2013-08-07 16:22:47 -0700 | [diff] [blame] | 2003 | * is complete and whether its a captive portal or not. |
| 2004 | * |
| 2005 | * @param info the {@link NetworkInfo} object for the networkType |
| 2006 | * in question. |
| 2007 | * @param isCaptivePortal true/false. |
| 2008 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 2009 | * <p>This method requires the caller to hold the permission |
Wink Saville | d747cbc | 2013-08-07 16:22:47 -0700 | [diff] [blame] | 2010 | * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}. |
| 2011 | * {@hide} |
| 2012 | */ |
| 2013 | public void captivePortalCheckCompleted(NetworkInfo info, boolean isCaptivePortal) { |
| 2014 | try { |
| 2015 | mService.captivePortalCheckCompleted(info, isCaptivePortal); |
| 2016 | } catch (RemoteException e) { |
| 2017 | } |
| 2018 | } |
| 2019 | |
| 2020 | /** |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2021 | * Check mobile provisioning. |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2022 | * |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2023 | * @param suggestedTimeOutMs, timeout in milliseconds |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2024 | * |
| 2025 | * @return time out that will be used, maybe less that suggestedTimeOutMs |
| 2026 | * -1 if an error. |
| 2027 | * |
| 2028 | * {@hide} |
| 2029 | */ |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2030 | public int checkMobileProvisioning(int suggestedTimeOutMs) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2031 | int timeOutMs = -1; |
| 2032 | try { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2033 | timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2034 | } catch (RemoteException e) { |
| 2035 | } |
| 2036 | return timeOutMs; |
| 2037 | } |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 2038 | |
| 2039 | /** |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 2040 | * Get the mobile provisioning url. |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 2041 | * {@hide} |
| 2042 | */ |
| 2043 | public String getMobileProvisioningUrl() { |
| 2044 | try { |
| 2045 | return mService.getMobileProvisioningUrl(); |
| 2046 | } catch (RemoteException e) { |
| 2047 | } |
| 2048 | return null; |
| 2049 | } |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 2050 | |
| 2051 | /** |
| 2052 | * Get the mobile redirected provisioning url. |
| 2053 | * {@hide} |
| 2054 | */ |
| 2055 | public String getMobileRedirectedProvisioningUrl() { |
| 2056 | try { |
| 2057 | return mService.getMobileRedirectedProvisioningUrl(); |
| 2058 | } catch (RemoteException e) { |
| 2059 | } |
| 2060 | return null; |
| 2061 | } |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 2062 | |
| 2063 | /** |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2064 | * Set sign in error notification to visible or in visible |
| 2065 | * |
| 2066 | * @param visible |
| 2067 | * @param networkType |
| 2068 | * |
| 2069 | * {@hide} |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 2070 | * @deprecated Doesn't properly deal with multiple connected networks of the same type. |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2071 | */ |
| 2072 | public void setProvisioningNotificationVisible(boolean visible, int networkType, |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 2073 | String action) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2074 | try { |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 2075 | mService.setProvisioningNotificationVisible(visible, networkType, action); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2076 | } catch (RemoteException e) { |
| 2077 | } |
| 2078 | } |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 2079 | |
| 2080 | /** |
| 2081 | * Set the value for enabling/disabling airplane mode |
| 2082 | * |
| 2083 | * @param enable whether to enable airplane mode or not |
| 2084 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 2085 | * <p>This method requires the caller to hold the permission |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 2086 | * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}. |
| 2087 | * @hide |
| 2088 | */ |
| 2089 | public void setAirplaneMode(boolean enable) { |
| 2090 | try { |
| 2091 | mService.setAirplaneMode(enable); |
| 2092 | } catch (RemoteException e) { |
| 2093 | } |
| 2094 | } |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2095 | |
| 2096 | /** {@hide} */ |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2097 | public void registerNetworkFactory(Messenger messenger, String name) { |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2098 | try { |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2099 | mService.registerNetworkFactory(messenger, name); |
| 2100 | } catch (RemoteException e) { } |
| 2101 | } |
| 2102 | |
| 2103 | /** {@hide} */ |
| 2104 | public void unregisterNetworkFactory(Messenger messenger) { |
| 2105 | try { |
| 2106 | mService.unregisterNetworkFactory(messenger); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2107 | } catch (RemoteException e) { } |
| 2108 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2109 | |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 2110 | /** |
| 2111 | * @hide |
| 2112 | * Register a NetworkAgent with ConnectivityService. |
| 2113 | * @return NetID corresponding to NetworkAgent. |
| 2114 | */ |
| 2115 | public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp, |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 2116 | NetworkCapabilities nc, int score, NetworkMisc misc) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2117 | try { |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 2118 | return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc); |
| 2119 | } catch (RemoteException e) { |
| 2120 | return NETID_UNSET; |
| 2121 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2122 | } |
| 2123 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2124 | /** |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2125 | * Base class for NetworkRequest callbacks. Used for notifications about network |
| 2126 | * changes. Should be extended by applications wanting notifications. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2127 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2128 | public static class NetworkCallback { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2129 | /** @hide */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2130 | public static final int PRECHECK = 1; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2131 | /** @hide */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2132 | public static final int AVAILABLE = 2; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2133 | /** @hide */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2134 | public static final int LOSING = 3; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2135 | /** @hide */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2136 | public static final int LOST = 4; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2137 | /** @hide */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2138 | public static final int UNAVAIL = 5; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2139 | /** @hide */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2140 | public static final int CAP_CHANGED = 6; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2141 | /** @hide */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2142 | public static final int PROP_CHANGED = 7; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2143 | /** @hide */ |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2144 | public static final int CANCELED = 8; |
| 2145 | |
| 2146 | /** |
| 2147 | * @hide |
| 2148 | * Called whenever the framework connects to a network that it may use to |
| 2149 | * satisfy this request |
| 2150 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2151 | public void onPreCheck(Network network) {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2152 | |
| 2153 | /** |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2154 | * Called when the framework connects and has declared new network ready for use. |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2155 | * This callback may be called more than once if the {@link Network} that is |
| 2156 | * satisfying the request changes. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2157 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2158 | * @param network The {@link Network} of the satisfying network. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2159 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2160 | public void onAvailable(Network network) {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2161 | |
| 2162 | /** |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2163 | * Called when the network is about to be disconnected. Often paired with an |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2164 | * {@link NetworkCallback#onAvailable} call with the new replacement network |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2165 | * for graceful handover. This may not be called if we have a hard loss |
| 2166 | * (loss without warning). This may be followed by either a |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2167 | * {@link NetworkCallback#onLost} call or a |
| 2168 | * {@link NetworkCallback#onAvailable} call for this network depending |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2169 | * on whether we lose or regain it. |
| 2170 | * |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2171 | * @param network The {@link Network} that is about to be disconnected. |
| 2172 | * @param maxMsToLive The time in ms the framework will attempt to keep the |
| 2173 | * network connected. Note that the network may suffer a |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2174 | * hard loss at any time. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2175 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2176 | public void onLosing(Network network, int maxMsToLive) {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2177 | |
| 2178 | /** |
| 2179 | * Called when the framework has a hard loss of the network or when the |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2180 | * graceful failure ends. |
| 2181 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2182 | * @param network The {@link Network} lost. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2183 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2184 | public void onLost(Network network) {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2185 | |
| 2186 | /** |
| 2187 | * Called if no network is found in the given timeout time. If no timeout is given, |
| 2188 | * this will not be called. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2189 | * @hide |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2190 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2191 | public void onUnavailable() {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2192 | |
| 2193 | /** |
| 2194 | * Called when the network the framework connected to for this request |
| 2195 | * changes capabilities but still satisfies the stated need. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2196 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2197 | * @param network The {@link Network} whose capabilities have changed. |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 2198 | * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this network. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2199 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2200 | public void onCapabilitiesChanged(Network network, |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2201 | NetworkCapabilities networkCapabilities) {} |
| 2202 | |
| 2203 | /** |
| 2204 | * Called when the network the framework connected to for this request |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2205 | * changes {@link LinkProperties}. |
| 2206 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2207 | * @param network The {@link Network} whose link properties have changed. |
| 2208 | * @param linkProperties The new {@link LinkProperties} for this network. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2209 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2210 | public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2211 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2212 | private NetworkRequest networkRequest; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2213 | } |
| 2214 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2215 | private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER; |
| 2216 | /** @hide obj = pair(NetworkRequest, Network) */ |
| 2217 | public static final int CALLBACK_PRECHECK = BASE + 1; |
| 2218 | /** @hide obj = pair(NetworkRequest, Network) */ |
| 2219 | public static final int CALLBACK_AVAILABLE = BASE + 2; |
| 2220 | /** @hide obj = pair(NetworkRequest, Network), arg1 = ttl */ |
| 2221 | public static final int CALLBACK_LOSING = BASE + 3; |
| 2222 | /** @hide obj = pair(NetworkRequest, Network) */ |
| 2223 | public static final int CALLBACK_LOST = BASE + 4; |
| 2224 | /** @hide obj = NetworkRequest */ |
| 2225 | public static final int CALLBACK_UNAVAIL = BASE + 5; |
| 2226 | /** @hide obj = pair(NetworkRequest, Network) */ |
| 2227 | public static final int CALLBACK_CAP_CHANGED = BASE + 6; |
| 2228 | /** @hide obj = pair(NetworkRequest, Network) */ |
| 2229 | public static final int CALLBACK_IP_CHANGED = BASE + 7; |
| 2230 | /** @hide obj = NetworkRequest */ |
| 2231 | public static final int CALLBACK_RELEASED = BASE + 8; |
| 2232 | /** @hide */ |
| 2233 | public static final int CALLBACK_EXIT = BASE + 9; |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2234 | /** @hide obj = NetworkCapabilities, arg1 = seq number */ |
| 2235 | private static final int EXPIRE_LEGACY_REQUEST = BASE + 10; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2236 | |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2237 | private class CallbackHandler extends Handler { |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2238 | private final HashMap<NetworkRequest, NetworkCallback>mCallbackMap; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2239 | private final AtomicInteger mRefCount; |
| 2240 | private static final String TAG = "ConnectivityManager.CallbackHandler"; |
| 2241 | private final ConnectivityManager mCm; |
| 2242 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2243 | CallbackHandler(Looper looper, HashMap<NetworkRequest, NetworkCallback>callbackMap, |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2244 | AtomicInteger refCount, ConnectivityManager cm) { |
| 2245 | super(looper); |
| 2246 | mCallbackMap = callbackMap; |
| 2247 | mRefCount = refCount; |
| 2248 | mCm = cm; |
| 2249 | } |
| 2250 | |
| 2251 | @Override |
| 2252 | public void handleMessage(Message message) { |
| 2253 | Log.d(TAG, "CM callback handler got msg " + message.what); |
| 2254 | switch (message.what) { |
| 2255 | case CALLBACK_PRECHECK: { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2256 | NetworkRequest request = (NetworkRequest)getObject(message, |
| 2257 | NetworkRequest.class); |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2258 | NetworkCallback callbacks = getCallbacks(request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2259 | if (callbacks != null) { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2260 | callbacks.onPreCheck((Network)getObject(message, Network.class)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2261 | } else { |
| 2262 | Log.e(TAG, "callback not found for PRECHECK message"); |
| 2263 | } |
| 2264 | break; |
| 2265 | } |
| 2266 | case CALLBACK_AVAILABLE: { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2267 | NetworkRequest request = (NetworkRequest)getObject(message, |
| 2268 | NetworkRequest.class); |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2269 | NetworkCallback callbacks = getCallbacks(request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2270 | if (callbacks != null) { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2271 | callbacks.onAvailable((Network)getObject(message, Network.class)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2272 | } else { |
| 2273 | Log.e(TAG, "callback not found for AVAILABLE message"); |
| 2274 | } |
| 2275 | break; |
| 2276 | } |
| 2277 | case CALLBACK_LOSING: { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2278 | NetworkRequest request = (NetworkRequest)getObject(message, |
| 2279 | NetworkRequest.class); |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2280 | NetworkCallback callbacks = getCallbacks(request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2281 | if (callbacks != null) { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2282 | callbacks.onLosing((Network)getObject(message, Network.class), |
| 2283 | message.arg1); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2284 | } else { |
| 2285 | Log.e(TAG, "callback not found for LOSING message"); |
| 2286 | } |
| 2287 | break; |
| 2288 | } |
| 2289 | case CALLBACK_LOST: { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2290 | NetworkRequest request = (NetworkRequest)getObject(message, |
| 2291 | NetworkRequest.class); |
| 2292 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2293 | NetworkCallback callbacks = getCallbacks(request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2294 | if (callbacks != null) { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2295 | callbacks.onLost((Network)getObject(message, Network.class)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2296 | } else { |
| 2297 | Log.e(TAG, "callback not found for LOST message"); |
| 2298 | } |
| 2299 | break; |
| 2300 | } |
| 2301 | case CALLBACK_UNAVAIL: { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2302 | NetworkRequest request = (NetworkRequest)getObject(message, |
| 2303 | NetworkRequest.class); |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2304 | NetworkCallback callbacks = null; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2305 | synchronized(mCallbackMap) { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2306 | callbacks = mCallbackMap.get(request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2307 | } |
| 2308 | if (callbacks != null) { |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2309 | callbacks.onUnavailable(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2310 | } else { |
| 2311 | Log.e(TAG, "callback not found for UNAVAIL message"); |
| 2312 | } |
| 2313 | break; |
| 2314 | } |
| 2315 | case CALLBACK_CAP_CHANGED: { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2316 | NetworkRequest request = (NetworkRequest)getObject(message, |
| 2317 | NetworkRequest.class); |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2318 | NetworkCallback callbacks = getCallbacks(request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2319 | if (callbacks != null) { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2320 | Network network = (Network)getObject(message, Network.class); |
| 2321 | NetworkCapabilities cap = (NetworkCapabilities)getObject(message, |
| 2322 | NetworkCapabilities.class); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2323 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2324 | callbacks.onCapabilitiesChanged(network, cap); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2325 | } else { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2326 | Log.e(TAG, "callback not found for CAP_CHANGED message"); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2327 | } |
| 2328 | break; |
| 2329 | } |
| 2330 | case CALLBACK_IP_CHANGED: { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2331 | NetworkRequest request = (NetworkRequest)getObject(message, |
| 2332 | NetworkRequest.class); |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2333 | NetworkCallback callbacks = getCallbacks(request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2334 | if (callbacks != null) { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2335 | Network network = (Network)getObject(message, Network.class); |
| 2336 | LinkProperties lp = (LinkProperties)getObject(message, |
| 2337 | LinkProperties.class); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2338 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2339 | callbacks.onLinkPropertiesChanged(network, lp); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2340 | } else { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2341 | Log.e(TAG, "callback not found for IP_CHANGED message"); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2342 | } |
| 2343 | break; |
| 2344 | } |
| 2345 | case CALLBACK_RELEASED: { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2346 | NetworkRequest req = (NetworkRequest)getObject(message, NetworkRequest.class); |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2347 | NetworkCallback callbacks = null; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2348 | synchronized(mCallbackMap) { |
| 2349 | callbacks = mCallbackMap.remove(req); |
| 2350 | } |
| 2351 | if (callbacks != null) { |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2352 | synchronized(mRefCount) { |
| 2353 | if (mRefCount.decrementAndGet() == 0) { |
| 2354 | getLooper().quit(); |
| 2355 | } |
| 2356 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2357 | } else { |
| 2358 | Log.e(TAG, "callback not found for CANCELED message"); |
| 2359 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2360 | break; |
| 2361 | } |
| 2362 | case CALLBACK_EXIT: { |
| 2363 | Log.d(TAG, "Listener quiting"); |
| 2364 | getLooper().quit(); |
| 2365 | break; |
| 2366 | } |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2367 | case EXPIRE_LEGACY_REQUEST: { |
| 2368 | expireRequest((NetworkCapabilities)message.obj, message.arg1); |
| 2369 | break; |
| 2370 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2371 | } |
| 2372 | } |
| 2373 | |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2374 | private Object getObject(Message msg, Class c) { |
| 2375 | return msg.getData().getParcelable(c.getSimpleName()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2376 | } |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2377 | private NetworkCallback getCallbacks(NetworkRequest req) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2378 | synchronized(mCallbackMap) { |
| 2379 | return mCallbackMap.get(req); |
| 2380 | } |
| 2381 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2382 | } |
| 2383 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2384 | private void incCallbackHandlerRefCount() { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2385 | synchronized(sCallbackRefCount) { |
| 2386 | if (sCallbackRefCount.incrementAndGet() == 1) { |
| 2387 | // TODO - switch this over to a ManagerThread or expire it when done |
| 2388 | HandlerThread callbackThread = new HandlerThread("ConnectivityManager"); |
| 2389 | callbackThread.start(); |
| 2390 | sCallbackHandler = new CallbackHandler(callbackThread.getLooper(), |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2391 | sNetworkCallback, sCallbackRefCount, this); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2392 | } |
| 2393 | } |
| 2394 | } |
| 2395 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2396 | private void decCallbackHandlerRefCount() { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2397 | synchronized(sCallbackRefCount) { |
| 2398 | if (sCallbackRefCount.decrementAndGet() == 0) { |
| 2399 | sCallbackHandler.obtainMessage(CALLBACK_EXIT).sendToTarget(); |
| 2400 | sCallbackHandler = null; |
| 2401 | } |
| 2402 | } |
| 2403 | } |
| 2404 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2405 | static final HashMap<NetworkRequest, NetworkCallback> sNetworkCallback = |
| 2406 | new HashMap<NetworkRequest, NetworkCallback>(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2407 | static final AtomicInteger sCallbackRefCount = new AtomicInteger(0); |
| 2408 | static CallbackHandler sCallbackHandler = null; |
| 2409 | |
| 2410 | private final static int LISTEN = 1; |
| 2411 | private final static int REQUEST = 2; |
| 2412 | |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2413 | private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2414 | NetworkCallback networkCallback, int timeoutSec, int action, |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 2415 | int legacyType) { |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2416 | if (networkCallback == null) { |
| 2417 | throw new IllegalArgumentException("null NetworkCallback"); |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2418 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2419 | if (need == null) throw new IllegalArgumentException("null NetworkCapabilities"); |
| 2420 | try { |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2421 | incCallbackHandlerRefCount(); |
Paul Jensen | 7221cc3 | 2014-06-27 11:05:32 -0400 | [diff] [blame] | 2422 | synchronized(sNetworkCallback) { |
| 2423 | if (action == LISTEN) { |
| 2424 | networkCallback.networkRequest = mService.listenForNetwork(need, |
| 2425 | new Messenger(sCallbackHandler), new Binder()); |
| 2426 | } else { |
| 2427 | networkCallback.networkRequest = mService.requestNetwork(need, |
| 2428 | new Messenger(sCallbackHandler), timeoutSec, new Binder(), legacyType); |
| 2429 | } |
| 2430 | if (networkCallback.networkRequest != null) { |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2431 | sNetworkCallback.put(networkCallback.networkRequest, networkCallback); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2432 | } |
| 2433 | } |
| 2434 | } catch (RemoteException e) {} |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2435 | if (networkCallback.networkRequest == null) decCallbackHandlerRefCount(); |
| 2436 | return networkCallback.networkRequest; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2437 | } |
| 2438 | |
| 2439 | /** |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 2440 | * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2441 | * |
| 2442 | * This {@link NetworkRequest} will live until released via |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2443 | * {@link #unregisterNetworkCallback} or the calling application exits. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2444 | * Status of the request can be followed by listening to the various |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2445 | * callbacks described in {@link NetworkCallback}. The {@link Network} |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2446 | * can be used to direct traffic to the network. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2447 | * |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2448 | * @param request {@link NetworkRequest} describing this request. |
| 2449 | * @param networkCallback The {@link NetworkCallback} to be utilized for this |
| 2450 | * request. Note the callback must not be shared - they |
| 2451 | * uniquely specify this request. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2452 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2453 | public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) { |
| 2454 | sendRequestForNetwork(request.networkCapabilities, networkCallback, 0, |
Ye Wen | b87875e | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 2455 | REQUEST, inferLegacyTypeForNetworkCapabilities(request.networkCapabilities)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2456 | } |
| 2457 | |
| 2458 | /** |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 2459 | * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2460 | * by a timeout. |
| 2461 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2462 | * This function behaves identically to the non-timedout version, but if a suitable |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2463 | * network is not found within the given time (in milliseconds) the |
| 2464 | * {@link NetworkCallback#unavailable} callback is called. The request must |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2465 | * still be released normally by calling {@link releaseNetworkRequest}. |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2466 | * @param request {@link NetworkRequest} describing this request. |
| 2467 | * @param networkCallback The callbacks to be utilized for this request. Note |
| 2468 | * the callbacks must not be shared - they uniquely specify |
| 2469 | * this request. |
| 2470 | * @param timeoutMs The time in milliseconds to attempt looking for a suitable network |
| 2471 | * before {@link NetworkCallback#unavailable} is called. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2472 | * @hide |
| 2473 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2474 | public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback, |
| 2475 | int timeoutMs) { |
| 2476 | sendRequestForNetwork(request.networkCapabilities, networkCallback, timeoutMs, |
Ye Wen | b87875e | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 2477 | REQUEST, inferLegacyTypeForNetworkCapabilities(request.networkCapabilities)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2478 | } |
| 2479 | |
| 2480 | /** |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2481 | * The maximum number of milliseconds the framework will look for a suitable network |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2482 | * during a timeout-equiped call to {@link requestNetwork}. |
| 2483 | * {@hide} |
| 2484 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2485 | public final static int MAX_NETWORK_REQUEST_TIMEOUT_MS = 100 * 60 * 1000; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2486 | |
| 2487 | /** |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2488 | * The lookup key for a {@link Network} object included with the intent after |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2489 | * successfully finding a network for the applications request. Retrieve it with |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2490 | * {@link android.content.Intent#getParcelableExtra(String)}. |
Jeremy Joslin | fcde58f | 2015-02-11 16:51:13 -0800 | [diff] [blame] | 2491 | * <p> |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2492 | * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)} |
| 2493 | * then you must get a ConnectivityManager instance before doing so. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2494 | */ |
Erik Kline | 90e9307 | 2014-11-19 12:12:24 +0900 | [diff] [blame] | 2495 | public static final String EXTRA_NETWORK = "android.net.extra.NETWORK"; |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2496 | |
| 2497 | /** |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2498 | * The lookup key for a {@link NetworkRequest} object included with the intent after |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2499 | * successfully finding a network for the applications request. Retrieve it with |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2500 | * {@link android.content.Intent#getParcelableExtra(String)}. |
| 2501 | */ |
Erik Kline | 90e9307 | 2014-11-19 12:12:24 +0900 | [diff] [blame] | 2502 | public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST"; |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2503 | |
| 2504 | |
| 2505 | /** |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 2506 | * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2507 | * |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2508 | * This function behaves identically to the version that takes a NetworkCallback, but instead |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2509 | * of {@link NetworkCallback} a {@link PendingIntent} is used. This means |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2510 | * the request may outlive the calling application and get called back when a suitable |
| 2511 | * network is found. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2512 | * <p> |
| 2513 | * The operation is an Intent broadcast that goes to a broadcast receiver that |
| 2514 | * you registered with {@link Context#registerReceiver} or through the |
| 2515 | * <receiver> tag in an AndroidManifest.xml file |
| 2516 | * <p> |
| 2517 | * The operation Intent is delivered with two extras, a {@link Network} typed |
Erik Kline | 90e9307 | 2014-11-19 12:12:24 +0900 | [diff] [blame] | 2518 | * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest} |
| 2519 | * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2520 | * the original requests parameters. It is important to create a new, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2521 | * {@link NetworkCallback} based request before completing the processing of the |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2522 | * Intent to reserve the network or it will be released shortly after the Intent |
| 2523 | * is processed. |
| 2524 | * <p> |
| 2525 | * If there is already an request for this Intent registered (with the equality of |
| 2526 | * two Intents defined by {@link Intent#filterEquals}), then it will be removed and |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2527 | * replaced by this one, effectively releasing the previous {@link NetworkRequest}. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2528 | * <p> |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2529 | * The request may be released normally by calling |
| 2530 | * {@link #releaseNetworkRequest(android.app.PendingIntent)}. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2531 | * |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2532 | * @param request {@link NetworkRequest} describing this request. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2533 | * @param operation Action to perform when the network is available (corresponds |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2534 | * to the {@link NetworkCallback#onAvailable} call. Typically |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2535 | * comes from {@link PendingIntent#getBroadcast}. Cannot be null. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2536 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2537 | public void requestNetwork(NetworkRequest request, PendingIntent operation) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2538 | checkPendingIntent(operation); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2539 | try { |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2540 | mService.pendingRequestForNetwork(request.networkCapabilities, operation); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2541 | } catch (RemoteException e) {} |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2542 | } |
| 2543 | |
| 2544 | /** |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2545 | * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} |
| 2546 | * <p> |
| 2547 | * This method has the same behavior as {@link #unregisterNetworkCallback} with respect to |
| 2548 | * releasing network resources and disconnecting. |
| 2549 | * |
| 2550 | * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the |
| 2551 | * PendingIntent passed to |
| 2552 | * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the |
| 2553 | * corresponding NetworkRequest you'd like to remove. Cannot be null. |
| 2554 | */ |
| 2555 | public void releaseNetworkRequest(PendingIntent operation) { |
| 2556 | checkPendingIntent(operation); |
| 2557 | try { |
| 2558 | mService.releasePendingNetworkRequest(operation); |
| 2559 | } catch (RemoteException e) {} |
| 2560 | } |
| 2561 | |
| 2562 | private void checkPendingIntent(PendingIntent intent) { |
| 2563 | if (intent == null) { |
| 2564 | throw new IllegalArgumentException("PendingIntent cannot be null."); |
| 2565 | } |
| 2566 | } |
| 2567 | |
| 2568 | /** |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2569 | * Registers to receive notifications about all networks which satisfy the given |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2570 | * {@link NetworkRequest}. The callbacks will continue to be called until |
| 2571 | * either the application exits or {@link #unregisterNetworkCallback} is called |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2572 | * |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2573 | * @param request {@link NetworkRequest} describing this request. |
| 2574 | * @param networkCallback The {@link NetworkCallback} that the system will call as suitable |
| 2575 | * networks change state. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2576 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2577 | public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) { |
| 2578 | sendRequestForNetwork(request.networkCapabilities, networkCallback, 0, LISTEN, TYPE_NONE); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | /** |
fenglu | db57147 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 2582 | * Requests bandwidth update for a given {@link Network} and returns whether the update request |
| 2583 | * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying |
| 2584 | * network connection for updated bandwidth information. The caller will be notified via |
| 2585 | * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this |
| 2586 | * method assumes that the caller has previously called {@link #registerNetworkCallback} to |
| 2587 | * listen for network changes. |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 2588 | * |
fenglu | ae51919 | 2015-04-27 14:28:04 -0700 | [diff] [blame^] | 2589 | * @param network {@link Network} specifying which network you're interested. |
fenglu | db57147 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 2590 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 2591 | */ |
fenglu | db57147 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 2592 | public boolean requestBandwidthUpdate(Network network) { |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 2593 | try { |
fenglu | db57147 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 2594 | return mService.requestBandwidthUpdate(network); |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 2595 | } catch (RemoteException e) { |
| 2596 | return false; |
| 2597 | } |
| 2598 | } |
| 2599 | |
| 2600 | /** |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2601 | * Unregisters callbacks about and possibly releases networks originating from |
| 2602 | * {@link #requestNetwork} and {@link #registerNetworkCallback} calls. If the |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2603 | * given {@code NetworkCallback} had previously been used with {@code #requestNetwork}, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2604 | * any networks that had been connected to only to satisfy that request will be |
| 2605 | * disconnected. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2606 | * |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2607 | * @param networkCallback The {@link NetworkCallback} used when making the request. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2608 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2609 | public void unregisterNetworkCallback(NetworkCallback networkCallback) { |
| 2610 | if (networkCallback == null || networkCallback.networkRequest == null || |
| 2611 | networkCallback.networkRequest.requestId == REQUEST_ID_UNSET) { |
| 2612 | throw new IllegalArgumentException("Invalid NetworkCallback"); |
| 2613 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2614 | try { |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2615 | mService.releaseNetworkRequest(networkCallback.networkRequest); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2616 | } catch (RemoteException e) {} |
| 2617 | } |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 2618 | |
| 2619 | /** |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2620 | * Informs the system whether it should switch to {@code network} regardless of whether it is |
| 2621 | * validated or not. If {@code accept} is true, and the network was explicitly selected by the |
| 2622 | * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become |
| 2623 | * the system default network regardless of any other network that's currently connected. If |
| 2624 | * {@code always} is true, then the choice is remembered, so that the next time the user |
| 2625 | * connects to this network, the system will switch to it. |
| 2626 | * |
| 2627 | * <p>This method requires the caller to hold the permission |
| 2628 | * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL} |
| 2629 | * |
| 2630 | * @param network The network to accept. |
| 2631 | * @param accept Whether to accept the network even if unvalidated. |
| 2632 | * @param always Whether to remember this choice in the future. |
| 2633 | * |
| 2634 | * @hide |
| 2635 | */ |
| 2636 | public void setAcceptUnvalidated(Network network, boolean accept, boolean always) { |
| 2637 | try { |
| 2638 | mService.setAcceptUnvalidated(network, accept, always); |
| 2639 | } catch (RemoteException e) {} |
| 2640 | } |
| 2641 | |
| 2642 | /** |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 2643 | * Resets all connectivity manager settings back to factory defaults. |
| 2644 | * @hide |
| 2645 | */ |
| 2646 | public void factoryReset() { |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 2647 | try { |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 2648 | mService.factoryReset(); |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 2649 | } catch (RemoteException e) { |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 2650 | } |
| 2651 | } |
| 2652 | |
| 2653 | /** |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 2654 | * Binds the current process to {@code network}. All Sockets created in the future |
| 2655 | * (and not explicitly bound via a bound SocketFactory from |
| 2656 | * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to |
| 2657 | * {@code network}. All host name resolutions will be limited to {@code network} as well. |
| 2658 | * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to |
| 2659 | * work and all host name resolutions will fail. This is by design so an application doesn't |
| 2660 | * accidentally use Sockets it thinks are still bound to a particular {@link Network}. |
| 2661 | * To clear binding pass {@code null} for {@code network}. Using individually bound |
| 2662 | * Sockets created by Network.getSocketFactory().createSocket() and |
| 2663 | * performing network-specific host name resolutions via |
| 2664 | * {@link Network#getAllByName Network.getAllByName} is preferred to calling |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2665 | * {@code bindProcessToNetwork}. |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 2666 | * |
| 2667 | * @param network The {@link Network} to bind the current process to, or {@code null} to clear |
| 2668 | * the current binding. |
| 2669 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
| 2670 | */ |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2671 | public boolean bindProcessToNetwork(Network network) { |
| 2672 | // Forcing callers to call thru non-static function ensures ConnectivityManager |
| 2673 | // instantiated. |
| 2674 | return setProcessDefaultNetwork(network); |
| 2675 | } |
| 2676 | |
| 2677 | /** |
| 2678 | * Binds the current process to {@code network}. All Sockets created in the future |
| 2679 | * (and not explicitly bound via a bound SocketFactory from |
| 2680 | * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to |
| 2681 | * {@code network}. All host name resolutions will be limited to {@code network} as well. |
| 2682 | * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to |
| 2683 | * work and all host name resolutions will fail. This is by design so an application doesn't |
| 2684 | * accidentally use Sockets it thinks are still bound to a particular {@link Network}. |
| 2685 | * To clear binding pass {@code null} for {@code network}. Using individually bound |
| 2686 | * Sockets created by Network.getSocketFactory().createSocket() and |
| 2687 | * performing network-specific host name resolutions via |
| 2688 | * {@link Network#getAllByName Network.getAllByName} is preferred to calling |
| 2689 | * {@code setProcessDefaultNetwork}. |
| 2690 | * |
| 2691 | * @param network The {@link Network} to bind the current process to, or {@code null} to clear |
| 2692 | * the current binding. |
| 2693 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
| 2694 | * @deprecated This function can throw {@link IllegalStateException}. Use |
| 2695 | * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork} |
| 2696 | * is a direct replacement. |
| 2697 | */ |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 2698 | public static boolean setProcessDefaultNetwork(Network network) { |
Paul Jensen | c91b534 | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 2699 | int netId = (network == null) ? NETID_UNSET : network.netId; |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2700 | if (netId == NetworkUtils.getBoundNetworkForProcess()) { |
Paul Jensen | c91b534 | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 2701 | return true; |
| 2702 | } |
| 2703 | if (NetworkUtils.bindProcessToNetwork(netId)) { |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 2704 | // Set HTTP proxy system properties to match network. |
| 2705 | // TODO: Deprecate this static method and replace it with a non-static version. |
Lorenzo Colitti | ec4c555 | 2015-04-22 11:52:48 +0900 | [diff] [blame] | 2706 | try { |
| 2707 | Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy()); |
| 2708 | } catch (SecurityException e) { |
| 2709 | // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy. |
| 2710 | Log.e(TAG, "Can't set proxy properties", e); |
| 2711 | } |
Paul Jensen | c91b534 | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 2712 | // Must flush DNS cache as new network may have different DNS resolutions. |
| 2713 | InetAddress.clearDnsCache(); |
| 2714 | // Must flush socket pool as idle sockets will be bound to previous network and may |
| 2715 | // cause subsequent fetches to be performed on old network. |
| 2716 | NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged(); |
| 2717 | return true; |
| 2718 | } else { |
| 2719 | return false; |
| 2720 | } |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 2721 | } |
| 2722 | |
| 2723 | /** |
| 2724 | * Returns the {@link Network} currently bound to this process via |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2725 | * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound. |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 2726 | * |
| 2727 | * @return {@code Network} to which this process is bound, or {@code null}. |
| 2728 | */ |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2729 | public Network getBoundNetworkForProcess() { |
| 2730 | // Forcing callers to call thru non-static function ensures ConnectivityManager |
| 2731 | // instantiated. |
| 2732 | return getProcessDefaultNetwork(); |
| 2733 | } |
| 2734 | |
| 2735 | /** |
| 2736 | * Returns the {@link Network} currently bound to this process via |
| 2737 | * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound. |
| 2738 | * |
| 2739 | * @return {@code Network} to which this process is bound, or {@code null}. |
| 2740 | * @deprecated Using this function can lead to other functions throwing |
| 2741 | * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead. |
| 2742 | * {@code getBoundNetworkForProcess} is a direct replacement. |
| 2743 | */ |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 2744 | public static Network getProcessDefaultNetwork() { |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2745 | int netId = NetworkUtils.getBoundNetworkForProcess(); |
Paul Jensen | bcc76d3 | 2014-07-11 08:17:29 -0400 | [diff] [blame] | 2746 | if (netId == NETID_UNSET) return null; |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 2747 | return new Network(netId); |
| 2748 | } |
| 2749 | |
| 2750 | /** |
| 2751 | * Binds host resolutions performed by this process to {@code network}. |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2752 | * {@link #bindProcessToNetwork} takes precedence over this setting. |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 2753 | * |
| 2754 | * @param network The {@link Network} to bind host resolutions from the current process to, or |
| 2755 | * {@code null} to clear the current binding. |
| 2756 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
| 2757 | * @hide |
| 2758 | * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}. |
| 2759 | */ |
| 2760 | public static boolean setProcessDefaultNetworkForHostResolution(Network network) { |
Paul Jensen | bcc76d3 | 2014-07-11 08:17:29 -0400 | [diff] [blame] | 2761 | return NetworkUtils.bindProcessToNetworkForHostResolution( |
| 2762 | network == null ? NETID_UNSET : network.netId); |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 2763 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2764 | } |