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