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