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