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