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