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