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