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