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