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