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