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