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