Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [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 | */ |
| 16 | package android.net; |
| 17 | |
| 18 | import static android.annotation.SystemApi.Client.MODULE_LIBRARIES; |
Junyu Lai | c3dc5b6 | 2023-09-06 19:10:02 +0800 | [diff] [blame] | 19 | import static android.content.pm.ApplicationInfo.FLAG_PERSISTENT; |
| 20 | import static android.content.pm.ApplicationInfo.FLAG_SYSTEM; |
Sooraj Sasindran | f4a58dc | 2022-01-21 13:37:08 -0800 | [diff] [blame] | 21 | import static android.net.NetworkCapabilities.NET_ENTERPRISE_ID_1; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 22 | import static android.net.NetworkRequest.Type.BACKGROUND_REQUEST; |
| 23 | import static android.net.NetworkRequest.Type.LISTEN; |
junyulai | 7664f62 | 2021-03-12 20:05:08 +0800 | [diff] [blame] | 24 | import static android.net.NetworkRequest.Type.LISTEN_FOR_BEST; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 25 | import static android.net.NetworkRequest.Type.REQUEST; |
| 26 | import static android.net.NetworkRequest.Type.TRACK_DEFAULT; |
Lorenzo Colitti | a77d05e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 27 | import static android.net.NetworkRequest.Type.TRACK_SYSTEM_DEFAULT; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 28 | import static android.net.QosCallback.QosCallbackRegistrationException; |
| 29 | |
Junyu Lai | c3dc5b6 | 2023-09-06 19:10:02 +0800 | [diff] [blame] | 30 | import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE; |
| 31 | |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 32 | import android.annotation.CallbackExecutor; |
Junyu Lai | df21036 | 2023-10-24 02:47:50 +0000 | [diff] [blame] | 33 | import android.annotation.FlaggedApi; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 34 | import android.annotation.IntDef; |
| 35 | import android.annotation.NonNull; |
| 36 | import android.annotation.Nullable; |
Chalard Jean | 2fb66f1 | 2023-08-25 12:50:37 +0900 | [diff] [blame] | 37 | import android.annotation.RequiresApi; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 38 | import android.annotation.RequiresPermission; |
| 39 | import android.annotation.SdkConstant; |
| 40 | import android.annotation.SdkConstant.SdkConstantType; |
| 41 | import android.annotation.SuppressLint; |
| 42 | import android.annotation.SystemApi; |
| 43 | import android.annotation.SystemService; |
Junyu Lai | c3dc5b6 | 2023-09-06 19:10:02 +0800 | [diff] [blame] | 44 | import android.annotation.TargetApi; |
| 45 | import android.app.Application; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 46 | import android.app.PendingIntent; |
Lorenzo Colitti | 8ad5812 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 47 | import android.app.admin.DevicePolicyManager; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 48 | import android.compat.annotation.UnsupportedAppUsage; |
Junyu Lai | c3dc5b6 | 2023-09-06 19:10:02 +0800 | [diff] [blame] | 49 | import android.content.BroadcastReceiver; |
Lorenzo Colitti | 8ad5812 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 50 | import android.content.ComponentName; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 51 | import android.content.Context; |
| 52 | import android.content.Intent; |
Junyu Lai | c3dc5b6 | 2023-09-06 19:10:02 +0800 | [diff] [blame] | 53 | import android.content.IntentFilter; |
Remi NGUYEN VAN | 564f7f8 | 2021-04-08 16:26:20 +0900 | [diff] [blame] | 54 | import android.net.ConnectivityDiagnosticsManager.DataStallReport.DetectionMethod; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 55 | import android.net.IpSecManager.UdpEncapsulationSocket; |
| 56 | import android.net.SocketKeepalive.Callback; |
| 57 | import android.net.TetheringManager.StartTetheringCallback; |
| 58 | import android.net.TetheringManager.TetheringEventCallback; |
| 59 | import android.net.TetheringManager.TetheringRequest; |
| 60 | import android.os.Binder; |
| 61 | import android.os.Build; |
| 62 | import android.os.Build.VERSION_CODES; |
| 63 | import android.os.Bundle; |
| 64 | import android.os.Handler; |
| 65 | import android.os.IBinder; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 66 | import android.os.Looper; |
| 67 | import android.os.Message; |
| 68 | import android.os.Messenger; |
| 69 | import android.os.ParcelFileDescriptor; |
| 70 | import android.os.PersistableBundle; |
| 71 | import android.os.Process; |
| 72 | import android.os.RemoteException; |
| 73 | import android.os.ResultReceiver; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 74 | import android.os.ServiceSpecificException; |
Chalard Jean | ad565e2 | 2021-02-25 17:23:40 +0900 | [diff] [blame] | 75 | import android.os.UserHandle; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 76 | import android.provider.Settings; |
| 77 | import android.telephony.SubscriptionManager; |
| 78 | import android.telephony.TelephonyManager; |
| 79 | import android.util.ArrayMap; |
| 80 | import android.util.Log; |
| 81 | import android.util.Range; |
| 82 | import android.util.SparseIntArray; |
| 83 | |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 84 | import com.android.internal.annotations.GuardedBy; |
Junyu Lai | c3dc5b6 | 2023-09-06 19:10:02 +0800 | [diff] [blame] | 85 | import com.android.internal.annotations.VisibleForTesting; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 86 | |
| 87 | import libcore.net.event.NetworkEventDispatcher; |
| 88 | |
| 89 | import java.io.IOException; |
| 90 | import java.io.UncheckedIOException; |
| 91 | import java.lang.annotation.Retention; |
| 92 | import java.lang.annotation.RetentionPolicy; |
| 93 | import java.net.DatagramSocket; |
| 94 | import java.net.InetAddress; |
| 95 | import java.net.InetSocketAddress; |
| 96 | import java.net.Socket; |
| 97 | import java.util.ArrayList; |
| 98 | import java.util.Collection; |
| 99 | import java.util.HashMap; |
| 100 | import java.util.List; |
| 101 | import java.util.Map; |
| 102 | import java.util.Objects; |
| 103 | import java.util.concurrent.Executor; |
| 104 | import java.util.concurrent.ExecutorService; |
| 105 | import java.util.concurrent.Executors; |
| 106 | import java.util.concurrent.RejectedExecutionException; |
Junyu Lai | c3dc5b6 | 2023-09-06 19:10:02 +0800 | [diff] [blame] | 107 | import java.util.concurrent.atomic.AtomicBoolean; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Class that answers queries about the state of network connectivity. It also |
| 111 | * notifies applications when network connectivity changes. |
| 112 | * <p> |
| 113 | * The primary responsibilities of this class are to: |
| 114 | * <ol> |
| 115 | * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li> |
| 116 | * <li>Send broadcast intents when network connectivity changes</li> |
| 117 | * <li>Attempt to "fail over" to another network when connectivity to a network |
| 118 | * is lost</li> |
| 119 | * <li>Provide an API that allows applications to query the coarse-grained or fine-grained |
| 120 | * state of the available networks</li> |
| 121 | * <li>Provide an API that allows applications to request and select networks for their data |
| 122 | * traffic</li> |
| 123 | * </ol> |
| 124 | */ |
| 125 | @SystemService(Context.CONNECTIVITY_SERVICE) |
| 126 | public class ConnectivityManager { |
| 127 | private static final String TAG = "ConnectivityManager"; |
| 128 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
| 129 | |
Junyu Lai | df21036 | 2023-10-24 02:47:50 +0000 | [diff] [blame] | 130 | // TODO : remove this class when udc-mainline-prod is abandoned and android.net.flags.Flags is |
| 131 | // available here |
| 132 | /** @hide */ |
| 133 | public static class Flags { |
| 134 | static final String SET_DATA_SAVER_VIA_CM = |
| 135 | "com.android.net.flags.set_data_saver_via_cm"; |
Junyu Lai | bb59480 | 2023-09-04 11:37:03 +0800 | [diff] [blame] | 136 | static final String SUPPORT_IS_UID_NETWORKING_BLOCKED = |
| 137 | "com.android.net.flags.support_is_uid_networking_blocked"; |
Suprabh Shukla | 2d893b6 | 2023-11-06 08:47:40 -0800 | [diff] [blame] | 138 | static final String BASIC_BACKGROUND_RESTRICTIONS_ENABLED = |
| 139 | "com.android.net.flags.basic_background_restrictions_enabled"; |
Junyu Lai | df21036 | 2023-10-24 02:47:50 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 142 | /** |
| 143 | * A change in network connectivity has occurred. A default connection has either |
| 144 | * been established or lost. The NetworkInfo for the affected network is |
| 145 | * sent as an extra; it should be consulted to see what kind of |
| 146 | * connectivity event occurred. |
| 147 | * <p/> |
| 148 | * Apps targeting Android 7.0 (API level 24) and higher do not receive this |
| 149 | * broadcast if they declare the broadcast receiver in their manifest. Apps |
| 150 | * will still receive broadcasts if they register their |
| 151 | * {@link android.content.BroadcastReceiver} with |
| 152 | * {@link android.content.Context#registerReceiver Context.registerReceiver()} |
| 153 | * and that context is still valid. |
| 154 | * <p/> |
| 155 | * If this is a connection that was the result of failing over from a |
| 156 | * disconnected network, then the FAILOVER_CONNECTION boolean extra is |
| 157 | * set to true. |
| 158 | * <p/> |
| 159 | * For a loss of connectivity, if the connectivity manager is attempting |
| 160 | * to connect (or has already connected) to another network, the |
| 161 | * NetworkInfo for the new network is also passed as an extra. This lets |
| 162 | * any receivers of the broadcast know that they should not necessarily |
| 163 | * tell the user that no data traffic will be possible. Instead, the |
| 164 | * receiver should expect another broadcast soon, indicating either that |
| 165 | * the failover attempt succeeded (and so there is still overall data |
| 166 | * connectivity), or that the failover attempt failed, meaning that all |
| 167 | * connectivity has been lost. |
| 168 | * <p/> |
| 169 | * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY |
| 170 | * is set to {@code true} if there are no connected networks at all. |
Chalard Jean | 025f40b | 2021-10-04 18:33:36 +0900 | [diff] [blame] | 171 | * <p /> |
| 172 | * Note that this broadcast is deprecated and generally tries to implement backwards |
| 173 | * compatibility with older versions of Android. As such, it may not reflect new |
| 174 | * capabilities of the system, like multiple networks being connected at the same |
| 175 | * time, the details of newer technology, or changes in tethering state. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 176 | * |
| 177 | * @deprecated apps should use the more versatile {@link #requestNetwork}, |
| 178 | * {@link #registerNetworkCallback} or {@link #registerDefaultNetworkCallback} |
| 179 | * functions instead for faster and more detailed updates about the network |
| 180 | * changes they care about. |
| 181 | */ |
| 182 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 183 | @Deprecated |
| 184 | public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; |
| 185 | |
| 186 | /** |
| 187 | * The device has connected to a network that has presented a captive |
| 188 | * portal, which is blocking Internet connectivity. The user was presented |
| 189 | * with a notification that network sign in is required, |
| 190 | * and the user invoked the notification's action indicating they |
| 191 | * desire to sign in to the network. Apps handling this activity should |
| 192 | * facilitate signing in to the network. This action includes a |
| 193 | * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents |
| 194 | * the network presenting the captive portal; all communication with the |
| 195 | * captive portal must be done using this {@code Network} object. |
| 196 | * <p/> |
| 197 | * This activity includes a {@link CaptivePortal} extra named |
| 198 | * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different |
| 199 | * outcomes of the captive portal sign in to the system: |
| 200 | * <ul> |
| 201 | * <li> When the app handling this action believes the user has signed in to |
| 202 | * the network and the captive portal has been dismissed, the app should |
| 203 | * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can |
| 204 | * reevaluate the network. If reevaluation finds the network no longer |
| 205 | * subject to a captive portal, the network may become the default active |
| 206 | * data network.</li> |
| 207 | * <li> When the app handling this action believes the user explicitly wants |
| 208 | * to ignore the captive portal and the network, the app should call |
| 209 | * {@link CaptivePortal#ignoreNetwork}. </li> |
| 210 | * </ul> |
| 211 | */ |
| 212 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 213 | public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL"; |
| 214 | |
| 215 | /** |
| 216 | * The lookup key for a {@link NetworkInfo} object. Retrieve with |
| 217 | * {@link android.content.Intent#getParcelableExtra(String)}. |
| 218 | * |
| 219 | * @deprecated The {@link NetworkInfo} object is deprecated, as many of its properties |
| 220 | * can't accurately represent modern network characteristics. |
| 221 | * Please obtain information about networks from the {@link NetworkCapabilities} |
| 222 | * or {@link LinkProperties} objects instead. |
| 223 | */ |
| 224 | @Deprecated |
| 225 | public static final String EXTRA_NETWORK_INFO = "networkInfo"; |
| 226 | |
| 227 | /** |
| 228 | * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast. |
| 229 | * |
| 230 | * @see android.content.Intent#getIntExtra(String, int) |
| 231 | * @deprecated The network type is not rich enough to represent the characteristics |
| 232 | * of modern networks. Please use {@link NetworkCapabilities} instead, |
| 233 | * in particular the transports. |
| 234 | */ |
| 235 | @Deprecated |
| 236 | public static final String EXTRA_NETWORK_TYPE = "networkType"; |
| 237 | |
| 238 | /** |
| 239 | * The lookup key for a boolean that indicates whether a connect event |
| 240 | * is for a network to which the connectivity manager was failing over |
| 241 | * following a disconnect on another network. |
| 242 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
| 243 | * |
| 244 | * @deprecated See {@link NetworkInfo}. |
| 245 | */ |
| 246 | @Deprecated |
| 247 | public static final String EXTRA_IS_FAILOVER = "isFailover"; |
| 248 | /** |
| 249 | * The lookup key for a {@link NetworkInfo} object. This is supplied when |
| 250 | * there is another network that it may be possible to connect to. Retrieve with |
| 251 | * {@link android.content.Intent#getParcelableExtra(String)}. |
| 252 | * |
| 253 | * @deprecated See {@link NetworkInfo}. |
| 254 | */ |
| 255 | @Deprecated |
| 256 | public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork"; |
| 257 | /** |
| 258 | * The lookup key for a boolean that indicates whether there is a |
| 259 | * complete lack of connectivity, i.e., no network is available. |
| 260 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
| 261 | */ |
| 262 | public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity"; |
| 263 | /** |
| 264 | * The lookup key for a string that indicates why an attempt to connect |
| 265 | * to a network failed. The string has no particular structure. It is |
| 266 | * intended to be used in notifications presented to users. Retrieve |
| 267 | * it with {@link android.content.Intent#getStringExtra(String)}. |
| 268 | */ |
| 269 | public static final String EXTRA_REASON = "reason"; |
| 270 | /** |
| 271 | * The lookup key for a string that provides optionally supplied |
| 272 | * extra information about the network state. The information |
| 273 | * may be passed up from the lower networking layers, and its |
| 274 | * meaning may be specific to a particular network type. Retrieve |
| 275 | * it with {@link android.content.Intent#getStringExtra(String)}. |
| 276 | * |
| 277 | * @deprecated See {@link NetworkInfo#getExtraInfo()}. |
| 278 | */ |
| 279 | @Deprecated |
| 280 | public static final String EXTRA_EXTRA_INFO = "extraInfo"; |
| 281 | /** |
| 282 | * The lookup key for an int that provides information about |
| 283 | * our connection to the internet at large. 0 indicates no connection, |
| 284 | * 100 indicates a great connection. Retrieve it with |
| 285 | * {@link android.content.Intent#getIntExtra(String, int)}. |
| 286 | * {@hide} |
| 287 | */ |
| 288 | public static final String EXTRA_INET_CONDITION = "inetCondition"; |
| 289 | /** |
| 290 | * The lookup key for a {@link CaptivePortal} object included with the |
| 291 | * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal} |
| 292 | * object can be used to either indicate to the system that the captive |
| 293 | * portal has been dismissed or that the user does not want to pursue |
| 294 | * signing in to captive portal. Retrieve it with |
| 295 | * {@link android.content.Intent#getParcelableExtra(String)}. |
| 296 | */ |
| 297 | public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL"; |
| 298 | |
| 299 | /** |
| 300 | * Key for passing a URL to the captive portal login activity. |
| 301 | */ |
| 302 | public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL"; |
| 303 | |
| 304 | /** |
| 305 | * Key for passing a {@link android.net.captiveportal.CaptivePortalProbeSpec} to the captive |
| 306 | * portal login activity. |
| 307 | * {@hide} |
| 308 | */ |
| 309 | @SystemApi |
| 310 | public static final String EXTRA_CAPTIVE_PORTAL_PROBE_SPEC = |
| 311 | "android.net.extra.CAPTIVE_PORTAL_PROBE_SPEC"; |
| 312 | |
| 313 | /** |
| 314 | * Key for passing a user agent string to the captive portal login activity. |
| 315 | * {@hide} |
| 316 | */ |
| 317 | @SystemApi |
| 318 | public static final String EXTRA_CAPTIVE_PORTAL_USER_AGENT = |
| 319 | "android.net.extra.CAPTIVE_PORTAL_USER_AGENT"; |
| 320 | |
| 321 | /** |
| 322 | * Broadcast action to indicate the change of data activity status |
| 323 | * (idle or active) on a network in a recent period. |
| 324 | * The network becomes active when data transmission is started, or |
| 325 | * idle if there is no data transmission for a period of time. |
| 326 | * {@hide} |
| 327 | */ |
| 328 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 329 | public static final String ACTION_DATA_ACTIVITY_CHANGE = |
| 330 | "android.net.conn.DATA_ACTIVITY_CHANGE"; |
| 331 | /** |
| 332 | * The lookup key for an enum that indicates the network device type on which this data activity |
| 333 | * change happens. |
| 334 | * {@hide} |
| 335 | */ |
| 336 | public static final String EXTRA_DEVICE_TYPE = "deviceType"; |
| 337 | /** |
| 338 | * The lookup key for a boolean that indicates the device is active or not. {@code true} means |
| 339 | * it is actively sending or receiving data and {@code false} means it is idle. |
| 340 | * {@hide} |
| 341 | */ |
| 342 | public static final String EXTRA_IS_ACTIVE = "isActive"; |
| 343 | /** |
| 344 | * The lookup key for a long that contains the timestamp (nanos) of the radio state change. |
| 345 | * {@hide} |
| 346 | */ |
| 347 | public static final String EXTRA_REALTIME_NS = "tsNanos"; |
| 348 | |
| 349 | /** |
| 350 | * Broadcast Action: The setting for background data usage has changed |
| 351 | * values. Use {@link #getBackgroundDataSetting()} to get the current value. |
| 352 | * <p> |
| 353 | * If an application uses the network in the background, it should listen |
| 354 | * for this broadcast and stop using the background data if the value is |
| 355 | * {@code false}. |
| 356 | * <p> |
| 357 | * |
| 358 | * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability |
| 359 | * of background data depends on several combined factors, and |
| 360 | * this broadcast is no longer sent. Instead, when background |
| 361 | * data is unavailable, {@link #getActiveNetworkInfo()} will now |
| 362 | * appear disconnected. During first boot after a platform |
| 363 | * upgrade, this broadcast will be sent once if |
| 364 | * {@link #getBackgroundDataSetting()} was {@code false} before |
| 365 | * the upgrade. |
| 366 | */ |
| 367 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 368 | @Deprecated |
| 369 | public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED = |
| 370 | "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED"; |
| 371 | |
| 372 | /** |
| 373 | * Broadcast Action: The network connection may not be good |
| 374 | * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and |
| 375 | * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify |
| 376 | * the network and it's condition. |
| 377 | * @hide |
| 378 | */ |
| 379 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 380 | @UnsupportedAppUsage |
| 381 | public static final String INET_CONDITION_ACTION = |
| 382 | "android.net.conn.INET_CONDITION_ACTION"; |
| 383 | |
| 384 | /** |
| 385 | * Broadcast Action: A tetherable connection has come or gone. |
| 386 | * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER}, |
| 387 | * {@code ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY}, |
| 388 | * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER}, and |
| 389 | * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate |
| 390 | * the current state of tethering. Each include a list of |
| 391 | * interface names in that state (may be empty). |
| 392 | * @hide |
| 393 | */ |
| 394 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 395 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 396 | public static final String ACTION_TETHER_STATE_CHANGED = |
| 397 | TetheringManager.ACTION_TETHER_STATE_CHANGED; |
| 398 | |
| 399 | /** |
| 400 | * @hide |
| 401 | * gives a String[] listing all the interfaces configured for |
| 402 | * tethering and currently available for tethering. |
| 403 | */ |
| 404 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 405 | public static final String EXTRA_AVAILABLE_TETHER = TetheringManager.EXTRA_AVAILABLE_TETHER; |
| 406 | |
| 407 | /** |
| 408 | * @hide |
| 409 | * gives a String[] listing all the interfaces currently in local-only |
| 410 | * mode (ie, has DHCPv4+IPv6-ULA support and no packet forwarding) |
| 411 | */ |
| 412 | public static final String EXTRA_ACTIVE_LOCAL_ONLY = TetheringManager.EXTRA_ACTIVE_LOCAL_ONLY; |
| 413 | |
| 414 | /** |
| 415 | * @hide |
| 416 | * gives a String[] listing all the interfaces currently tethered |
| 417 | * (ie, has DHCPv4 support and packets potentially forwarded/NATed) |
| 418 | */ |
| 419 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 420 | public static final String EXTRA_ACTIVE_TETHER = TetheringManager.EXTRA_ACTIVE_TETHER; |
| 421 | |
| 422 | /** |
| 423 | * @hide |
| 424 | * gives a String[] listing all the interfaces we tried to tether and |
| 425 | * failed. Use {@link #getLastTetherError} to find the error code |
| 426 | * for any interfaces listed here. |
| 427 | */ |
| 428 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 429 | public static final String EXTRA_ERRORED_TETHER = TetheringManager.EXTRA_ERRORED_TETHER; |
| 430 | |
| 431 | /** |
| 432 | * Broadcast Action: The captive portal tracker has finished its test. |
| 433 | * Sent only while running Setup Wizard, in lieu of showing a user |
| 434 | * notification. |
| 435 | * @hide |
| 436 | */ |
| 437 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 438 | public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED = |
| 439 | "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED"; |
| 440 | /** |
| 441 | * The lookup key for a boolean that indicates whether a captive portal was detected. |
| 442 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
| 443 | * @hide |
| 444 | */ |
| 445 | public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal"; |
| 446 | |
| 447 | /** |
| 448 | * Action used to display a dialog that asks the user whether to connect to a network that is |
| 449 | * not validated. This intent is used to start the dialog in settings via startActivity. |
| 450 | * |
lucaslin | 8bee2fd | 2021-04-21 10:43:15 +0800 | [diff] [blame] | 451 | * This action includes a {@link Network} typed extra which is called |
| 452 | * {@link ConnectivityManager#EXTRA_NETWORK} that represents the network which is unvalidated. |
| 453 | * |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 454 | * @hide |
| 455 | */ |
lucaslin | cf6d450 | 2021-03-04 17:09:51 +0800 | [diff] [blame] | 456 | @SystemApi(client = MODULE_LIBRARIES) |
| 457 | public static final String ACTION_PROMPT_UNVALIDATED = "android.net.action.PROMPT_UNVALIDATED"; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 458 | |
| 459 | /** |
| 460 | * Action used to display a dialog that asks the user whether to avoid a network that is no |
| 461 | * longer validated. This intent is used to start the dialog in settings via startActivity. |
| 462 | * |
lucaslin | 8bee2fd | 2021-04-21 10:43:15 +0800 | [diff] [blame] | 463 | * This action includes a {@link Network} typed extra which is called |
| 464 | * {@link ConnectivityManager#EXTRA_NETWORK} that represents the network which is no longer |
| 465 | * validated. |
| 466 | * |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 467 | * @hide |
| 468 | */ |
lucaslin | cf6d450 | 2021-03-04 17:09:51 +0800 | [diff] [blame] | 469 | @SystemApi(client = MODULE_LIBRARIES) |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 470 | public static final String ACTION_PROMPT_LOST_VALIDATION = |
lucaslin | cf6d450 | 2021-03-04 17:09:51 +0800 | [diff] [blame] | 471 | "android.net.action.PROMPT_LOST_VALIDATION"; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 472 | |
| 473 | /** |
| 474 | * Action used to display a dialog that asks the user whether to stay connected to a network |
| 475 | * that has not validated. This intent is used to start the dialog in settings via |
| 476 | * startActivity. |
| 477 | * |
lucaslin | 8bee2fd | 2021-04-21 10:43:15 +0800 | [diff] [blame] | 478 | * This action includes a {@link Network} typed extra which is called |
| 479 | * {@link ConnectivityManager#EXTRA_NETWORK} that represents the network which has partial |
| 480 | * connectivity. |
| 481 | * |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 482 | * @hide |
| 483 | */ |
lucaslin | cf6d450 | 2021-03-04 17:09:51 +0800 | [diff] [blame] | 484 | @SystemApi(client = MODULE_LIBRARIES) |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 485 | public static final String ACTION_PROMPT_PARTIAL_CONNECTIVITY = |
lucaslin | cf6d450 | 2021-03-04 17:09:51 +0800 | [diff] [blame] | 486 | "android.net.action.PROMPT_PARTIAL_CONNECTIVITY"; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 487 | |
| 488 | /** |
paulhu | b49c842 | 2021-04-07 16:18:13 +0800 | [diff] [blame] | 489 | * Clear DNS Cache Action: This is broadcast when networks have changed and old |
| 490 | * DNS entries should be cleared. |
| 491 | * @hide |
| 492 | */ |
| 493 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 494 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 495 | public static final String ACTION_CLEAR_DNS_CACHE = "android.net.action.CLEAR_DNS_CACHE"; |
| 496 | |
| 497 | /** |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 498 | * Invalid tethering type. |
| 499 | * @see #startTethering(int, boolean, OnStartTetheringCallback) |
| 500 | * @hide |
| 501 | */ |
| 502 | public static final int TETHERING_INVALID = TetheringManager.TETHERING_INVALID; |
| 503 | |
| 504 | /** |
| 505 | * Wifi tethering type. |
| 506 | * @see #startTethering(int, boolean, OnStartTetheringCallback) |
| 507 | * @hide |
| 508 | */ |
| 509 | @SystemApi |
Remi NGUYEN VAN | 71ced8e | 2021-02-15 18:52:06 +0900 | [diff] [blame] | 510 | public static final int TETHERING_WIFI = 0; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 511 | |
| 512 | /** |
| 513 | * USB tethering type. |
| 514 | * @see #startTethering(int, boolean, OnStartTetheringCallback) |
| 515 | * @hide |
| 516 | */ |
| 517 | @SystemApi |
Remi NGUYEN VAN | 71ced8e | 2021-02-15 18:52:06 +0900 | [diff] [blame] | 518 | public static final int TETHERING_USB = 1; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 519 | |
| 520 | /** |
| 521 | * Bluetooth tethering type. |
| 522 | * @see #startTethering(int, boolean, OnStartTetheringCallback) |
| 523 | * @hide |
| 524 | */ |
| 525 | @SystemApi |
Remi NGUYEN VAN | 71ced8e | 2021-02-15 18:52:06 +0900 | [diff] [blame] | 526 | public static final int TETHERING_BLUETOOTH = 2; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 527 | |
| 528 | /** |
| 529 | * Wifi P2p tethering type. |
| 530 | * Wifi P2p tethering is set through events automatically, and don't |
| 531 | * need to start from #startTethering(int, boolean, OnStartTetheringCallback). |
| 532 | * @hide |
| 533 | */ |
| 534 | public static final int TETHERING_WIFI_P2P = TetheringManager.TETHERING_WIFI_P2P; |
| 535 | |
| 536 | /** |
| 537 | * Extra used for communicating with the TetherService. Includes the type of tethering to |
| 538 | * enable if any. |
| 539 | * @hide |
| 540 | */ |
| 541 | public static final String EXTRA_ADD_TETHER_TYPE = TetheringConstants.EXTRA_ADD_TETHER_TYPE; |
| 542 | |
| 543 | /** |
| 544 | * Extra used for communicating with the TetherService. Includes the type of tethering for |
| 545 | * which to cancel provisioning. |
| 546 | * @hide |
| 547 | */ |
| 548 | public static final String EXTRA_REM_TETHER_TYPE = TetheringConstants.EXTRA_REM_TETHER_TYPE; |
| 549 | |
| 550 | /** |
| 551 | * Extra used for communicating with the TetherService. True to schedule a recheck of tether |
| 552 | * provisioning. |
| 553 | * @hide |
| 554 | */ |
| 555 | public static final String EXTRA_SET_ALARM = TetheringConstants.EXTRA_SET_ALARM; |
| 556 | |
| 557 | /** |
| 558 | * Tells the TetherService to run a provision check now. |
| 559 | * @hide |
| 560 | */ |
| 561 | public static final String EXTRA_RUN_PROVISION = TetheringConstants.EXTRA_RUN_PROVISION; |
| 562 | |
| 563 | /** |
| 564 | * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver} |
| 565 | * which will receive provisioning results. Can be left empty. |
| 566 | * @hide |
| 567 | */ |
| 568 | public static final String EXTRA_PROVISION_CALLBACK = |
| 569 | TetheringConstants.EXTRA_PROVISION_CALLBACK; |
| 570 | |
| 571 | /** |
| 572 | * The absence of a connection type. |
| 573 | * @hide |
| 574 | */ |
| 575 | @SystemApi |
| 576 | public static final int TYPE_NONE = -1; |
| 577 | |
| 578 | /** |
| 579 | * A Mobile data connection. Devices may support more than one. |
| 580 | * |
| 581 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 582 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
chiachangwang | 9473c59 | 2022-07-15 02:25:52 +0000 | [diff] [blame] | 583 | * appropriate network. See {@link NetworkCapabilities} for supported transports. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 584 | */ |
| 585 | @Deprecated |
| 586 | public static final int TYPE_MOBILE = 0; |
| 587 | |
| 588 | /** |
| 589 | * A WIFI data connection. Devices may support more than one. |
| 590 | * |
| 591 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 592 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
chiachangwang | 9473c59 | 2022-07-15 02:25:52 +0000 | [diff] [blame] | 593 | * appropriate network. See {@link NetworkCapabilities} for supported transports. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 594 | */ |
| 595 | @Deprecated |
| 596 | public static final int TYPE_WIFI = 1; |
| 597 | |
| 598 | /** |
| 599 | * An MMS-specific Mobile data connection. This network type may use the |
| 600 | * same network interface as {@link #TYPE_MOBILE} or it may use a different |
| 601 | * one. This is used by applications needing to talk to the carrier's |
| 602 | * Multimedia Messaging Service servers. |
| 603 | * |
| 604 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or |
| 605 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that |
| 606 | * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability. |
| 607 | */ |
| 608 | @Deprecated |
| 609 | public static final int TYPE_MOBILE_MMS = 2; |
| 610 | |
| 611 | /** |
| 612 | * A SUPL-specific Mobile data connection. This network type may use the |
| 613 | * same network interface as {@link #TYPE_MOBILE} or it may use a different |
| 614 | * one. This is used by applications needing to talk to the carrier's |
| 615 | * Secure User Plane Location servers for help locating the device. |
| 616 | * |
| 617 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or |
| 618 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that |
| 619 | * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability. |
| 620 | */ |
| 621 | @Deprecated |
| 622 | public static final int TYPE_MOBILE_SUPL = 3; |
| 623 | |
| 624 | /** |
| 625 | * A DUN-specific Mobile data connection. This network type may use the |
| 626 | * same network interface as {@link #TYPE_MOBILE} or it may use a different |
| 627 | * one. This is sometimes by the system when setting up an upstream connection |
| 628 | * for tethering so that the carrier is aware of DUN traffic. |
| 629 | * |
| 630 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or |
| 631 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that |
| 632 | * provides the {@link NetworkCapabilities#NET_CAPABILITY_DUN} capability. |
| 633 | */ |
| 634 | @Deprecated |
| 635 | public static final int TYPE_MOBILE_DUN = 4; |
| 636 | |
| 637 | /** |
| 638 | * A High Priority Mobile data connection. This network type uses the |
| 639 | * same network interface as {@link #TYPE_MOBILE} but the routing setup |
| 640 | * is different. |
| 641 | * |
| 642 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 643 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
chiachangwang | 9473c59 | 2022-07-15 02:25:52 +0000 | [diff] [blame] | 644 | * appropriate network. See {@link NetworkCapabilities} for supported transports. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 645 | */ |
| 646 | @Deprecated |
| 647 | public static final int TYPE_MOBILE_HIPRI = 5; |
| 648 | |
| 649 | /** |
| 650 | * A WiMAX data connection. |
| 651 | * |
| 652 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 653 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
chiachangwang | 9473c59 | 2022-07-15 02:25:52 +0000 | [diff] [blame] | 654 | * appropriate network. See {@link NetworkCapabilities} for supported transports. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 655 | */ |
| 656 | @Deprecated |
| 657 | public static final int TYPE_WIMAX = 6; |
| 658 | |
| 659 | /** |
| 660 | * A Bluetooth data connection. |
| 661 | * |
| 662 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 663 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
chiachangwang | 9473c59 | 2022-07-15 02:25:52 +0000 | [diff] [blame] | 664 | * appropriate network. See {@link NetworkCapabilities} for supported transports. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 665 | */ |
| 666 | @Deprecated |
| 667 | public static final int TYPE_BLUETOOTH = 7; |
| 668 | |
| 669 | /** |
| 670 | * Fake data connection. This should not be used on shipping devices. |
| 671 | * @deprecated This is not used any more. |
| 672 | */ |
| 673 | @Deprecated |
| 674 | public static final int TYPE_DUMMY = 8; |
| 675 | |
| 676 | /** |
| 677 | * An Ethernet data connection. |
| 678 | * |
| 679 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 680 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
chiachangwang | 9473c59 | 2022-07-15 02:25:52 +0000 | [diff] [blame] | 681 | * appropriate network. See {@link NetworkCapabilities} for supported transports. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 682 | */ |
| 683 | @Deprecated |
| 684 | public static final int TYPE_ETHERNET = 9; |
| 685 | |
| 686 | /** |
| 687 | * Over the air Administration. |
| 688 | * @deprecated Use {@link NetworkCapabilities} instead. |
| 689 | * {@hide} |
| 690 | */ |
| 691 | @Deprecated |
| 692 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
| 693 | public static final int TYPE_MOBILE_FOTA = 10; |
| 694 | |
| 695 | /** |
| 696 | * IP Multimedia Subsystem. |
| 697 | * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IMS} instead. |
| 698 | * {@hide} |
| 699 | */ |
| 700 | @Deprecated |
| 701 | @UnsupportedAppUsage |
| 702 | public static final int TYPE_MOBILE_IMS = 11; |
| 703 | |
| 704 | /** |
| 705 | * Carrier Branded Services. |
| 706 | * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_CBS} instead. |
| 707 | * {@hide} |
| 708 | */ |
| 709 | @Deprecated |
| 710 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
| 711 | public static final int TYPE_MOBILE_CBS = 12; |
| 712 | |
| 713 | /** |
| 714 | * A Wi-Fi p2p connection. Only requesting processes will have access to |
| 715 | * the peers connected. |
| 716 | * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_WIFI_P2P} instead. |
| 717 | * {@hide} |
| 718 | */ |
| 719 | @Deprecated |
| 720 | @SystemApi |
| 721 | public static final int TYPE_WIFI_P2P = 13; |
| 722 | |
| 723 | /** |
| 724 | * The network to use for initially attaching to the network |
| 725 | * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IA} instead. |
| 726 | * {@hide} |
| 727 | */ |
| 728 | @Deprecated |
| 729 | @UnsupportedAppUsage |
| 730 | public static final int TYPE_MOBILE_IA = 14; |
| 731 | |
| 732 | /** |
| 733 | * Emergency PDN connection for emergency services. This |
| 734 | * may include IMS and MMS in emergency situations. |
| 735 | * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_EIMS} instead. |
| 736 | * {@hide} |
| 737 | */ |
| 738 | @Deprecated |
| 739 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
| 740 | public static final int TYPE_MOBILE_EMERGENCY = 15; |
| 741 | |
| 742 | /** |
| 743 | * The network that uses proxy to achieve connectivity. |
| 744 | * @deprecated Use {@link NetworkCapabilities} instead. |
| 745 | * {@hide} |
| 746 | */ |
| 747 | @Deprecated |
| 748 | @SystemApi |
| 749 | public static final int TYPE_PROXY = 16; |
| 750 | |
| 751 | /** |
| 752 | * A virtual network using one or more native bearers. |
| 753 | * It may or may not be providing security services. |
| 754 | * @deprecated Applications should use {@link NetworkCapabilities#TRANSPORT_VPN} instead. |
| 755 | */ |
| 756 | @Deprecated |
| 757 | public static final int TYPE_VPN = 17; |
| 758 | |
| 759 | /** |
| 760 | * A network that is exclusively meant to be used for testing |
| 761 | * |
| 762 | * @deprecated Use {@link NetworkCapabilities} instead. |
| 763 | * @hide |
| 764 | */ |
| 765 | @Deprecated |
| 766 | public static final int TYPE_TEST = 18; // TODO: Remove this once NetworkTypes are unused. |
| 767 | |
| 768 | /** |
| 769 | * @deprecated Use {@link NetworkCapabilities} instead. |
| 770 | * @hide |
| 771 | */ |
| 772 | @Deprecated |
| 773 | @Retention(RetentionPolicy.SOURCE) |
| 774 | @IntDef(prefix = { "TYPE_" }, value = { |
| 775 | TYPE_NONE, |
| 776 | TYPE_MOBILE, |
| 777 | TYPE_WIFI, |
| 778 | TYPE_MOBILE_MMS, |
| 779 | TYPE_MOBILE_SUPL, |
| 780 | TYPE_MOBILE_DUN, |
| 781 | TYPE_MOBILE_HIPRI, |
| 782 | TYPE_WIMAX, |
| 783 | TYPE_BLUETOOTH, |
| 784 | TYPE_DUMMY, |
| 785 | TYPE_ETHERNET, |
| 786 | TYPE_MOBILE_FOTA, |
| 787 | TYPE_MOBILE_IMS, |
| 788 | TYPE_MOBILE_CBS, |
| 789 | TYPE_WIFI_P2P, |
| 790 | TYPE_MOBILE_IA, |
| 791 | TYPE_MOBILE_EMERGENCY, |
| 792 | TYPE_PROXY, |
| 793 | TYPE_VPN, |
| 794 | TYPE_TEST |
| 795 | }) |
| 796 | public @interface LegacyNetworkType {} |
| 797 | |
| 798 | // Deprecated constants for return values of startUsingNetworkFeature. They used to live |
| 799 | // in com.android.internal.telephony.PhoneConstants until they were made inaccessible. |
| 800 | private static final int DEPRECATED_PHONE_CONSTANT_APN_ALREADY_ACTIVE = 0; |
| 801 | private static final int DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED = 1; |
| 802 | private static final int DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED = 3; |
| 803 | |
| 804 | /** {@hide} */ |
| 805 | public static final int MAX_RADIO_TYPE = TYPE_TEST; |
| 806 | |
| 807 | /** {@hide} */ |
| 808 | public static final int MAX_NETWORK_TYPE = TYPE_TEST; |
| 809 | |
| 810 | private static final int MIN_NETWORK_TYPE = TYPE_MOBILE; |
| 811 | |
| 812 | /** |
| 813 | * If you want to set the default network preference,you can directly |
| 814 | * change the networkAttributes array in framework's config.xml. |
| 815 | * |
| 816 | * @deprecated Since we support so many more networks now, the single |
| 817 | * network default network preference can't really express |
| 818 | * the hierarchy. Instead, the default is defined by the |
| 819 | * networkAttributes in config.xml. You can determine |
| 820 | * the current value by calling {@link #getNetworkPreference()} |
| 821 | * from an App. |
| 822 | */ |
| 823 | @Deprecated |
| 824 | public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI; |
| 825 | |
| 826 | /** |
| 827 | * @hide |
| 828 | */ |
| 829 | public static final int REQUEST_ID_UNSET = 0; |
| 830 | |
| 831 | /** |
| 832 | * Static unique request used as a tombstone for NetworkCallbacks that have been unregistered. |
| 833 | * This allows to distinguish when unregistering NetworkCallbacks those that were never |
| 834 | * registered from those that were already unregistered. |
| 835 | * @hide |
| 836 | */ |
| 837 | private static final NetworkRequest ALREADY_UNREGISTERED = |
| 838 | new NetworkRequest.Builder().clearCapabilities().build(); |
| 839 | |
| 840 | /** |
| 841 | * A NetID indicating no Network is selected. |
| 842 | * Keep in sync with bionic/libc/dns/include/resolv_netid.h |
| 843 | * @hide |
| 844 | */ |
| 845 | public static final int NETID_UNSET = 0; |
| 846 | |
| 847 | /** |
Sudheer Shanka | 1d0d9b4 | 2021-03-23 08:12:28 +0000 | [diff] [blame] | 848 | * Flag to indicate that an app is not subject to any restrictions that could result in its |
| 849 | * network access blocked. |
| 850 | * |
| 851 | * @hide |
| 852 | */ |
| 853 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 854 | public static final int BLOCKED_REASON_NONE = 0; |
| 855 | |
| 856 | /** |
| 857 | * Flag to indicate that an app is subject to Battery saver restrictions that would |
| 858 | * result in its network access being blocked. |
| 859 | * |
| 860 | * @hide |
| 861 | */ |
| 862 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 863 | public static final int BLOCKED_REASON_BATTERY_SAVER = 1 << 0; |
| 864 | |
| 865 | /** |
| 866 | * Flag to indicate that an app is subject to Doze restrictions that would |
| 867 | * result in its network access being blocked. |
| 868 | * |
| 869 | * @hide |
| 870 | */ |
| 871 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 872 | public static final int BLOCKED_REASON_DOZE = 1 << 1; |
| 873 | |
| 874 | /** |
| 875 | * Flag to indicate that an app is subject to App Standby restrictions that would |
| 876 | * result in its network access being blocked. |
| 877 | * |
| 878 | * @hide |
| 879 | */ |
| 880 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 881 | public static final int BLOCKED_REASON_APP_STANDBY = 1 << 2; |
| 882 | |
| 883 | /** |
| 884 | * Flag to indicate that an app is subject to Restricted mode restrictions that would |
| 885 | * result in its network access being blocked. |
| 886 | * |
| 887 | * @hide |
| 888 | */ |
| 889 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 890 | public static final int BLOCKED_REASON_RESTRICTED_MODE = 1 << 3; |
| 891 | |
| 892 | /** |
Lorenzo Colitti | 8ad5812 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 893 | * Flag to indicate that an app is blocked because it is subject to an always-on VPN but the VPN |
| 894 | * is not currently connected. |
| 895 | * |
| 896 | * @see DevicePolicyManager#setAlwaysOnVpnPackage(ComponentName, String, boolean) |
| 897 | * |
| 898 | * @hide |
| 899 | */ |
| 900 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 901 | public static final int BLOCKED_REASON_LOCKDOWN_VPN = 1 << 4; |
| 902 | |
| 903 | /** |
Robert Horvath | 2dac948 | 2021-11-15 15:49:37 +0100 | [diff] [blame] | 904 | * Flag to indicate that an app is subject to Low Power Standby restrictions that would |
| 905 | * result in its network access being blocked. |
| 906 | * |
| 907 | * @hide |
| 908 | */ |
| 909 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 910 | public static final int BLOCKED_REASON_LOW_POWER_STANDBY = 1 << 5; |
| 911 | |
| 912 | /** |
Suprabh Shukla | 2d893b6 | 2023-11-06 08:47:40 -0800 | [diff] [blame] | 913 | * Flag to indicate that an app is subject to default background restrictions that would |
| 914 | * result in its network access being blocked. |
| 915 | * |
| 916 | * @hide |
| 917 | */ |
| 918 | @FlaggedApi(Flags.BASIC_BACKGROUND_RESTRICTIONS_ENABLED) |
| 919 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 920 | public static final int BLOCKED_REASON_APP_BACKGROUND = 1 << 6; |
| 921 | |
| 922 | /** |
Sudheer Shanka | 1d0d9b4 | 2021-03-23 08:12:28 +0000 | [diff] [blame] | 923 | * Flag to indicate that an app is subject to Data saver restrictions that would |
| 924 | * result in its metered network access being blocked. |
| 925 | * |
| 926 | * @hide |
| 927 | */ |
| 928 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 929 | public static final int BLOCKED_METERED_REASON_DATA_SAVER = 1 << 16; |
| 930 | |
| 931 | /** |
| 932 | * Flag to indicate that an app is subject to user restrictions that would |
| 933 | * result in its metered network access being blocked. |
| 934 | * |
| 935 | * @hide |
| 936 | */ |
| 937 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 938 | public static final int BLOCKED_METERED_REASON_USER_RESTRICTED = 1 << 17; |
| 939 | |
| 940 | /** |
| 941 | * Flag to indicate that an app is subject to Device admin restrictions that would |
| 942 | * result in its metered network access being blocked. |
| 943 | * |
| 944 | * @hide |
| 945 | */ |
| 946 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 947 | public static final int BLOCKED_METERED_REASON_ADMIN_DISABLED = 1 << 18; |
| 948 | |
| 949 | /** |
| 950 | * @hide |
| 951 | */ |
| 952 | @Retention(RetentionPolicy.SOURCE) |
| 953 | @IntDef(flag = true, prefix = {"BLOCKED_"}, value = { |
| 954 | BLOCKED_REASON_NONE, |
| 955 | BLOCKED_REASON_BATTERY_SAVER, |
| 956 | BLOCKED_REASON_DOZE, |
| 957 | BLOCKED_REASON_APP_STANDBY, |
| 958 | BLOCKED_REASON_RESTRICTED_MODE, |
Lorenzo Colitti | a1bd6f6 | 2021-03-25 23:17:36 +0900 | [diff] [blame] | 959 | BLOCKED_REASON_LOCKDOWN_VPN, |
Robert Horvath | 2dac948 | 2021-11-15 15:49:37 +0100 | [diff] [blame] | 960 | BLOCKED_REASON_LOW_POWER_STANDBY, |
Suprabh Shukla | 2d893b6 | 2023-11-06 08:47:40 -0800 | [diff] [blame] | 961 | BLOCKED_REASON_APP_BACKGROUND, |
Sudheer Shanka | 1d0d9b4 | 2021-03-23 08:12:28 +0000 | [diff] [blame] | 962 | BLOCKED_METERED_REASON_DATA_SAVER, |
| 963 | BLOCKED_METERED_REASON_USER_RESTRICTED, |
| 964 | BLOCKED_METERED_REASON_ADMIN_DISABLED, |
| 965 | }) |
| 966 | public @interface BlockedReason {} |
| 967 | |
Lorenzo Colitti | 8ad5812 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 968 | /** |
| 969 | * Set of blocked reasons that are only applicable on metered networks. |
| 970 | * |
| 971 | * @hide |
| 972 | */ |
| 973 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 974 | public static final int BLOCKED_METERED_REASON_MASK = 0xffff0000; |
| 975 | |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 976 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
| 977 | private final IConnectivityManager mService; |
Lorenzo Colitti | 842075e | 2021-02-04 17:32:07 +0900 | [diff] [blame] | 978 | |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 979 | /** |
markchien | e1561fa | 2021-12-09 22:00:56 +0800 | [diff] [blame] | 980 | * Firewall chain for device idle (doze mode). |
| 981 | * Allowlist of apps that have network access in device idle. |
| 982 | * @hide |
| 983 | */ |
| 984 | @SystemApi(client = MODULE_LIBRARIES) |
| 985 | public static final int FIREWALL_CHAIN_DOZABLE = 1; |
| 986 | |
| 987 | /** |
| 988 | * Firewall chain used for app standby. |
| 989 | * Denylist of apps that do not have network access. |
| 990 | * @hide |
| 991 | */ |
| 992 | @SystemApi(client = MODULE_LIBRARIES) |
| 993 | public static final int FIREWALL_CHAIN_STANDBY = 2; |
| 994 | |
| 995 | /** |
| 996 | * Firewall chain used for battery saver. |
| 997 | * Allowlist of apps that have network access when battery saver is on. |
| 998 | * @hide |
| 999 | */ |
| 1000 | @SystemApi(client = MODULE_LIBRARIES) |
| 1001 | public static final int FIREWALL_CHAIN_POWERSAVE = 3; |
| 1002 | |
| 1003 | /** |
| 1004 | * Firewall chain used for restricted networking mode. |
| 1005 | * Allowlist of apps that have access in restricted networking mode. |
| 1006 | * @hide |
| 1007 | */ |
| 1008 | @SystemApi(client = MODULE_LIBRARIES) |
| 1009 | public static final int FIREWALL_CHAIN_RESTRICTED = 4; |
| 1010 | |
Robert Horvath | 34cba14 | 2022-01-27 19:52:43 +0100 | [diff] [blame] | 1011 | /** |
| 1012 | * Firewall chain used for low power standby. |
| 1013 | * Allowlist of apps that have access in low power standby. |
| 1014 | * @hide |
| 1015 | */ |
| 1016 | @SystemApi(client = MODULE_LIBRARIES) |
| 1017 | public static final int FIREWALL_CHAIN_LOW_POWER_STANDBY = 5; |
| 1018 | |
Motomu Utsumi | b08654c | 2022-05-11 05:56:26 +0000 | [diff] [blame] | 1019 | /** |
Suprabh Shukla | 2d893b6 | 2023-11-06 08:47:40 -0800 | [diff] [blame] | 1020 | * Firewall chain used for always-on default background restrictions. |
| 1021 | * Allowlist of apps that have access because either they are in the foreground or they are |
| 1022 | * exempted for specific situations while in the background. |
| 1023 | * @hide |
| 1024 | */ |
| 1025 | @FlaggedApi(Flags.BASIC_BACKGROUND_RESTRICTIONS_ENABLED) |
| 1026 | @SystemApi(client = MODULE_LIBRARIES) |
| 1027 | public static final int FIREWALL_CHAIN_BACKGROUND = 6; |
| 1028 | |
| 1029 | /** |
Motomu Utsumi | d980149 | 2022-06-01 13:57:27 +0000 | [diff] [blame] | 1030 | * Firewall chain used for OEM-specific application restrictions. |
Lorenzo Colitti | f683c40 | 2022-08-03 10:40:00 +0900 | [diff] [blame] | 1031 | * |
| 1032 | * Denylist of apps that will not have network access due to OEM-specific restrictions. If an |
| 1033 | * app UID is placed on this chain, and the chain is enabled, the app's packets will be dropped. |
| 1034 | * |
| 1035 | * All the {@code FIREWALL_CHAIN_OEM_DENY_x} chains are equivalent, and each one is |
| 1036 | * independent of the others. The chains can be enabled and disabled independently, and apps can |
| 1037 | * be added and removed from each chain independently. |
| 1038 | * |
| 1039 | * @see #FIREWALL_CHAIN_OEM_DENY_2 |
| 1040 | * @see #FIREWALL_CHAIN_OEM_DENY_3 |
Motomu Utsumi | d980149 | 2022-06-01 13:57:27 +0000 | [diff] [blame] | 1041 | * @hide |
| 1042 | */ |
Motomu Utsumi | 62385c8 | 2022-06-12 11:37:19 +0000 | [diff] [blame] | 1043 | @SystemApi(client = MODULE_LIBRARIES) |
Motomu Utsumi | d980149 | 2022-06-01 13:57:27 +0000 | [diff] [blame] | 1044 | public static final int FIREWALL_CHAIN_OEM_DENY_1 = 7; |
| 1045 | |
| 1046 | /** |
| 1047 | * Firewall chain used for OEM-specific application restrictions. |
Lorenzo Colitti | f683c40 | 2022-08-03 10:40:00 +0900 | [diff] [blame] | 1048 | * |
| 1049 | * Denylist of apps that will not have network access due to OEM-specific restrictions. If an |
| 1050 | * app UID is placed on this chain, and the chain is enabled, the app's packets will be dropped. |
| 1051 | * |
| 1052 | * All the {@code FIREWALL_CHAIN_OEM_DENY_x} chains are equivalent, and each one is |
| 1053 | * independent of the others. The chains can be enabled and disabled independently, and apps can |
| 1054 | * be added and removed from each chain independently. |
| 1055 | * |
| 1056 | * @see #FIREWALL_CHAIN_OEM_DENY_1 |
| 1057 | * @see #FIREWALL_CHAIN_OEM_DENY_3 |
Motomu Utsumi | d980149 | 2022-06-01 13:57:27 +0000 | [diff] [blame] | 1058 | * @hide |
| 1059 | */ |
Motomu Utsumi | 62385c8 | 2022-06-12 11:37:19 +0000 | [diff] [blame] | 1060 | @SystemApi(client = MODULE_LIBRARIES) |
Motomu Utsumi | d980149 | 2022-06-01 13:57:27 +0000 | [diff] [blame] | 1061 | public static final int FIREWALL_CHAIN_OEM_DENY_2 = 8; |
| 1062 | |
Motomu Utsumi | 1d9054b | 2022-06-06 07:44:05 +0000 | [diff] [blame] | 1063 | /** |
| 1064 | * Firewall chain used for OEM-specific application restrictions. |
Lorenzo Colitti | f683c40 | 2022-08-03 10:40:00 +0900 | [diff] [blame] | 1065 | * |
| 1066 | * Denylist of apps that will not have network access due to OEM-specific restrictions. If an |
| 1067 | * app UID is placed on this chain, and the chain is enabled, the app's packets will be dropped. |
| 1068 | * |
| 1069 | * All the {@code FIREWALL_CHAIN_OEM_DENY_x} chains are equivalent, and each one is |
| 1070 | * independent of the others. The chains can be enabled and disabled independently, and apps can |
| 1071 | * be added and removed from each chain independently. |
| 1072 | * |
| 1073 | * @see #FIREWALL_CHAIN_OEM_DENY_1 |
| 1074 | * @see #FIREWALL_CHAIN_OEM_DENY_2 |
Motomu Utsumi | 1d9054b | 2022-06-06 07:44:05 +0000 | [diff] [blame] | 1075 | * @hide |
| 1076 | */ |
Motomu Utsumi | 62385c8 | 2022-06-12 11:37:19 +0000 | [diff] [blame] | 1077 | @SystemApi(client = MODULE_LIBRARIES) |
Motomu Utsumi | 1d9054b | 2022-06-06 07:44:05 +0000 | [diff] [blame] | 1078 | public static final int FIREWALL_CHAIN_OEM_DENY_3 = 9; |
| 1079 | |
markchien | e1561fa | 2021-12-09 22:00:56 +0800 | [diff] [blame] | 1080 | /** @hide */ |
| 1081 | @Retention(RetentionPolicy.SOURCE) |
| 1082 | @IntDef(flag = false, prefix = "FIREWALL_CHAIN_", value = { |
| 1083 | FIREWALL_CHAIN_DOZABLE, |
| 1084 | FIREWALL_CHAIN_STANDBY, |
| 1085 | FIREWALL_CHAIN_POWERSAVE, |
Robert Horvath | 34cba14 | 2022-01-27 19:52:43 +0100 | [diff] [blame] | 1086 | FIREWALL_CHAIN_RESTRICTED, |
Motomu Utsumi | b08654c | 2022-05-11 05:56:26 +0000 | [diff] [blame] | 1087 | FIREWALL_CHAIN_LOW_POWER_STANDBY, |
Suprabh Shukla | 2d893b6 | 2023-11-06 08:47:40 -0800 | [diff] [blame] | 1088 | FIREWALL_CHAIN_BACKGROUND, |
Motomu Utsumi | d980149 | 2022-06-01 13:57:27 +0000 | [diff] [blame] | 1089 | FIREWALL_CHAIN_OEM_DENY_1, |
Motomu Utsumi | 1d9054b | 2022-06-06 07:44:05 +0000 | [diff] [blame] | 1090 | FIREWALL_CHAIN_OEM_DENY_2, |
| 1091 | FIREWALL_CHAIN_OEM_DENY_3 |
markchien | e1561fa | 2021-12-09 22:00:56 +0800 | [diff] [blame] | 1092 | }) |
| 1093 | public @interface FirewallChain {} |
| 1094 | |
| 1095 | /** |
markchien | 011a7f5 | 2022-03-29 01:07:22 +0800 | [diff] [blame] | 1096 | * A firewall rule which allows or drops packets depending on existing policy. |
| 1097 | * Used by {@link #setUidFirewallRule(int, int, int)} to follow existing policy to handle |
| 1098 | * specific uid's packets in specific firewall chain. |
markchien | 3c04e66 | 2022-03-22 16:29:56 +0800 | [diff] [blame] | 1099 | * @hide |
| 1100 | */ |
| 1101 | @SystemApi(client = MODULE_LIBRARIES) |
| 1102 | public static final int FIREWALL_RULE_DEFAULT = 0; |
| 1103 | |
| 1104 | /** |
markchien | 011a7f5 | 2022-03-29 01:07:22 +0800 | [diff] [blame] | 1105 | * A firewall rule which allows packets. Used by {@link #setUidFirewallRule(int, int, int)} to |
| 1106 | * allow specific uid's packets in specific firewall chain. |
markchien | 3c04e66 | 2022-03-22 16:29:56 +0800 | [diff] [blame] | 1107 | * @hide |
| 1108 | */ |
| 1109 | @SystemApi(client = MODULE_LIBRARIES) |
| 1110 | public static final int FIREWALL_RULE_ALLOW = 1; |
| 1111 | |
| 1112 | /** |
markchien | 011a7f5 | 2022-03-29 01:07:22 +0800 | [diff] [blame] | 1113 | * A firewall rule which drops packets. Used by {@link #setUidFirewallRule(int, int, int)} to |
| 1114 | * drop specific uid's packets in specific firewall chain. |
markchien | 3c04e66 | 2022-03-22 16:29:56 +0800 | [diff] [blame] | 1115 | * @hide |
| 1116 | */ |
| 1117 | @SystemApi(client = MODULE_LIBRARIES) |
| 1118 | public static final int FIREWALL_RULE_DENY = 2; |
| 1119 | |
| 1120 | /** @hide */ |
| 1121 | @Retention(RetentionPolicy.SOURCE) |
| 1122 | @IntDef(flag = false, prefix = "FIREWALL_RULE_", value = { |
| 1123 | FIREWALL_RULE_DEFAULT, |
| 1124 | FIREWALL_RULE_ALLOW, |
| 1125 | FIREWALL_RULE_DENY |
| 1126 | }) |
| 1127 | public @interface FirewallRule {} |
| 1128 | |
| 1129 | /** |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1130 | * A kludge to facilitate static access where a Context pointer isn't available, like in the |
| 1131 | * case of the static set/getProcessDefaultNetwork methods and from the Network class. |
| 1132 | * TODO: Remove this after deprecating the static methods in favor of non-static methods or |
| 1133 | * methods that take a Context argument. |
| 1134 | */ |
| 1135 | private static ConnectivityManager sInstance; |
| 1136 | |
| 1137 | private final Context mContext; |
| 1138 | |
Remi NGUYEN VAN | e4d1cd9 | 2021-06-17 16:27:31 +0900 | [diff] [blame] | 1139 | @GuardedBy("mTetheringEventCallbacks") |
| 1140 | private TetheringManager mTetheringManager; |
| 1141 | |
| 1142 | private TetheringManager getTetheringManager() { |
| 1143 | synchronized (mTetheringEventCallbacks) { |
| 1144 | if (mTetheringManager == null) { |
| 1145 | mTetheringManager = mContext.getSystemService(TetheringManager.class); |
| 1146 | } |
| 1147 | return mTetheringManager; |
| 1148 | } |
| 1149 | } |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1150 | |
| 1151 | /** |
| 1152 | * Tests if a given integer represents a valid network type. |
| 1153 | * @param networkType the type to be tested |
Chalard Jean | 0c7ebe9 | 2022-08-03 14:45:47 +0900 | [diff] [blame] | 1154 | * @return {@code true} if the type is valid, else {@code false} |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1155 | * @deprecated All APIs accepting a network type are deprecated. There should be no need to |
| 1156 | * validate a network type. |
| 1157 | */ |
| 1158 | @Deprecated |
| 1159 | public static boolean isNetworkTypeValid(int networkType) { |
| 1160 | return MIN_NETWORK_TYPE <= networkType && networkType <= MAX_NETWORK_TYPE; |
| 1161 | } |
| 1162 | |
| 1163 | /** |
| 1164 | * Returns a non-localized string representing a given network type. |
| 1165 | * ONLY used for debugging output. |
| 1166 | * @param type the type needing naming |
| 1167 | * @return a String for the given type, or a string version of the type ("87") |
| 1168 | * if no name is known. |
| 1169 | * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead. |
| 1170 | * {@hide} |
| 1171 | */ |
| 1172 | @Deprecated |
| 1173 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1174 | public static String getNetworkTypeName(int type) { |
| 1175 | switch (type) { |
| 1176 | case TYPE_NONE: |
| 1177 | return "NONE"; |
| 1178 | case TYPE_MOBILE: |
| 1179 | return "MOBILE"; |
| 1180 | case TYPE_WIFI: |
| 1181 | return "WIFI"; |
| 1182 | case TYPE_MOBILE_MMS: |
| 1183 | return "MOBILE_MMS"; |
| 1184 | case TYPE_MOBILE_SUPL: |
| 1185 | return "MOBILE_SUPL"; |
| 1186 | case TYPE_MOBILE_DUN: |
| 1187 | return "MOBILE_DUN"; |
| 1188 | case TYPE_MOBILE_HIPRI: |
| 1189 | return "MOBILE_HIPRI"; |
| 1190 | case TYPE_WIMAX: |
| 1191 | return "WIMAX"; |
| 1192 | case TYPE_BLUETOOTH: |
| 1193 | return "BLUETOOTH"; |
| 1194 | case TYPE_DUMMY: |
| 1195 | return "DUMMY"; |
| 1196 | case TYPE_ETHERNET: |
| 1197 | return "ETHERNET"; |
| 1198 | case TYPE_MOBILE_FOTA: |
| 1199 | return "MOBILE_FOTA"; |
| 1200 | case TYPE_MOBILE_IMS: |
| 1201 | return "MOBILE_IMS"; |
| 1202 | case TYPE_MOBILE_CBS: |
| 1203 | return "MOBILE_CBS"; |
| 1204 | case TYPE_WIFI_P2P: |
| 1205 | return "WIFI_P2P"; |
| 1206 | case TYPE_MOBILE_IA: |
| 1207 | return "MOBILE_IA"; |
| 1208 | case TYPE_MOBILE_EMERGENCY: |
| 1209 | return "MOBILE_EMERGENCY"; |
| 1210 | case TYPE_PROXY: |
| 1211 | return "PROXY"; |
| 1212 | case TYPE_VPN: |
| 1213 | return "VPN"; |
Junyu Lai | c9f1ca6 | 2022-07-25 16:31:59 +0800 | [diff] [blame] | 1214 | case TYPE_TEST: |
| 1215 | return "TEST"; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1216 | default: |
| 1217 | return Integer.toString(type); |
| 1218 | } |
| 1219 | } |
| 1220 | |
| 1221 | /** |
| 1222 | * @hide |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1223 | */ |
lucaslin | 10774b7 | 2021-03-17 14:16:01 +0800 | [diff] [blame] | 1224 | @SystemApi(client = MODULE_LIBRARIES) |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1225 | public void systemReady() { |
| 1226 | try { |
| 1227 | mService.systemReady(); |
| 1228 | } catch (RemoteException e) { |
| 1229 | throw e.rethrowFromSystemServer(); |
| 1230 | } |
| 1231 | } |
| 1232 | |
| 1233 | /** |
| 1234 | * Checks if a given type uses the cellular data connection. |
| 1235 | * This should be replaced in the future by a network property. |
| 1236 | * @param networkType the type to check |
| 1237 | * @return a boolean - {@code true} if uses cellular network, else {@code false} |
| 1238 | * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead. |
| 1239 | * {@hide} |
| 1240 | */ |
| 1241 | @Deprecated |
| 1242 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
| 1243 | public static boolean isNetworkTypeMobile(int networkType) { |
| 1244 | switch (networkType) { |
| 1245 | case TYPE_MOBILE: |
| 1246 | case TYPE_MOBILE_MMS: |
| 1247 | case TYPE_MOBILE_SUPL: |
| 1248 | case TYPE_MOBILE_DUN: |
| 1249 | case TYPE_MOBILE_HIPRI: |
| 1250 | case TYPE_MOBILE_FOTA: |
| 1251 | case TYPE_MOBILE_IMS: |
| 1252 | case TYPE_MOBILE_CBS: |
| 1253 | case TYPE_MOBILE_IA: |
| 1254 | case TYPE_MOBILE_EMERGENCY: |
| 1255 | return true; |
| 1256 | default: |
| 1257 | return false; |
| 1258 | } |
| 1259 | } |
| 1260 | |
| 1261 | /** |
| 1262 | * Checks if the given network type is backed by a Wi-Fi radio. |
| 1263 | * |
| 1264 | * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead. |
| 1265 | * @hide |
| 1266 | */ |
| 1267 | @Deprecated |
| 1268 | public static boolean isNetworkTypeWifi(int networkType) { |
| 1269 | switch (networkType) { |
| 1270 | case TYPE_WIFI: |
| 1271 | case TYPE_WIFI_P2P: |
| 1272 | return true; |
| 1273 | default: |
| 1274 | return false; |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | /** |
Junyu Lai | 35665cc | 2022-12-19 17:37:48 +0800 | [diff] [blame] | 1279 | * Preference for {@link ProfileNetworkPreference.Builder#setPreference(int)}. |
chiachangwang | 9473c59 | 2022-07-15 02:25:52 +0000 | [diff] [blame] | 1280 | * See {@link #setProfileNetworkPreferences(UserHandle, List, Executor, Runnable)} |
Junyu Lai | 35665cc | 2022-12-19 17:37:48 +0800 | [diff] [blame] | 1281 | * Specify that the traffic for this user should by follow the default rules: |
| 1282 | * applications in the profile designated by the UserHandle behave like any |
| 1283 | * other application and use the system default network as their default |
| 1284 | * network. Compare other PROFILE_NETWORK_PREFERENCE_* settings. |
Chalard Jean | ad565e2 | 2021-02-25 17:23:40 +0900 | [diff] [blame] | 1285 | * @hide |
| 1286 | */ |
Chalard Jean | bef6b09 | 2021-03-17 14:33:24 +0900 | [diff] [blame] | 1287 | @SystemApi(client = MODULE_LIBRARIES) |
Chalard Jean | ad565e2 | 2021-02-25 17:23:40 +0900 | [diff] [blame] | 1288 | public static final int PROFILE_NETWORK_PREFERENCE_DEFAULT = 0; |
| 1289 | |
| 1290 | /** |
Junyu Lai | 35665cc | 2022-12-19 17:37:48 +0800 | [diff] [blame] | 1291 | * Preference for {@link ProfileNetworkPreference.Builder#setPreference(int)}. |
chiachangwang | 9473c59 | 2022-07-15 02:25:52 +0000 | [diff] [blame] | 1292 | * See {@link #setProfileNetworkPreferences(UserHandle, List, Executor, Runnable)} |
Chalard Jean | ad565e2 | 2021-02-25 17:23:40 +0900 | [diff] [blame] | 1293 | * Specify that the traffic for this user should by default go on a network with |
| 1294 | * {@link NetworkCapabilities#NET_CAPABILITY_ENTERPRISE}, and on the system default network |
| 1295 | * if no such network is available. |
| 1296 | * @hide |
| 1297 | */ |
Chalard Jean | bef6b09 | 2021-03-17 14:33:24 +0900 | [diff] [blame] | 1298 | @SystemApi(client = MODULE_LIBRARIES) |
Chalard Jean | ad565e2 | 2021-02-25 17:23:40 +0900 | [diff] [blame] | 1299 | public static final int PROFILE_NETWORK_PREFERENCE_ENTERPRISE = 1; |
| 1300 | |
Sooraj Sasindran | 06baf4c | 2021-11-29 12:21:09 -0800 | [diff] [blame] | 1301 | /** |
Junyu Lai | 35665cc | 2022-12-19 17:37:48 +0800 | [diff] [blame] | 1302 | * Preference for {@link ProfileNetworkPreference.Builder#setPreference(int)}. |
chiachangwang | 9473c59 | 2022-07-15 02:25:52 +0000 | [diff] [blame] | 1303 | * See {@link #setProfileNetworkPreferences(UserHandle, List, Executor, Runnable)} |
Sooraj Sasindran | 06baf4c | 2021-11-29 12:21:09 -0800 | [diff] [blame] | 1304 | * Specify that the traffic for this user should by default go on a network with |
| 1305 | * {@link NetworkCapabilities#NET_CAPABILITY_ENTERPRISE} and if no such network is available |
Junyu Lai | 35665cc | 2022-12-19 17:37:48 +0800 | [diff] [blame] | 1306 | * should not have a default network at all (that is, network accesses that |
| 1307 | * do not specify a network explicitly terminate with an error), even if there |
| 1308 | * is a system default network available to apps outside this preference. |
| 1309 | * The apps can still use a non-enterprise network if they request it explicitly |
| 1310 | * provided that specific network doesn't require any specific permission they |
| 1311 | * do not hold. |
Sooraj Sasindran | 06baf4c | 2021-11-29 12:21:09 -0800 | [diff] [blame] | 1312 | * @hide |
| 1313 | */ |
| 1314 | @SystemApi(client = MODULE_LIBRARIES) |
| 1315 | public static final int PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK = 2; |
| 1316 | |
Junyu Lai | 35665cc | 2022-12-19 17:37:48 +0800 | [diff] [blame] | 1317 | /** |
| 1318 | * Preference for {@link ProfileNetworkPreference.Builder#setPreference(int)}. |
| 1319 | * See {@link #setProfileNetworkPreferences(UserHandle, List, Executor, Runnable)} |
| 1320 | * Specify that the traffic for this user should by default go on a network with |
| 1321 | * {@link NetworkCapabilities#NET_CAPABILITY_ENTERPRISE}. |
| 1322 | * If there is no such network, the apps will have no default |
| 1323 | * network at all, even if there are available non-enterprise networks on the |
| 1324 | * device (that is, network accesses that do not specify a network explicitly |
| 1325 | * terminate with an error). Additionally, the designated apps should be |
| 1326 | * blocked from using any non-enterprise network even if they specify it |
| 1327 | * explicitly, unless they hold specific privilege overriding this (see |
| 1328 | * {@link android.Manifest.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS}). |
| 1329 | * @hide |
| 1330 | */ |
| 1331 | @SystemApi(client = MODULE_LIBRARIES) |
| 1332 | public static final int PROFILE_NETWORK_PREFERENCE_ENTERPRISE_BLOCKING = 3; |
| 1333 | |
Chalard Jean | ad565e2 | 2021-02-25 17:23:40 +0900 | [diff] [blame] | 1334 | /** @hide */ |
| 1335 | @Retention(RetentionPolicy.SOURCE) |
| 1336 | @IntDef(value = { |
| 1337 | PROFILE_NETWORK_PREFERENCE_DEFAULT, |
Sooraj Sasindran | 06baf4c | 2021-11-29 12:21:09 -0800 | [diff] [blame] | 1338 | PROFILE_NETWORK_PREFERENCE_ENTERPRISE, |
| 1339 | PROFILE_NETWORK_PREFERENCE_ENTERPRISE_NO_FALLBACK |
Chalard Jean | ad565e2 | 2021-02-25 17:23:40 +0900 | [diff] [blame] | 1340 | }) |
Sooraj Sasindran | e7aee27 | 2021-11-24 20:26:55 -0800 | [diff] [blame] | 1341 | public @interface ProfileNetworkPreferencePolicy { |
Chalard Jean | ad565e2 | 2021-02-25 17:23:40 +0900 | [diff] [blame] | 1342 | } |
| 1343 | |
| 1344 | /** |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1345 | * Specifies the preferred network type. When the device has more |
| 1346 | * than one type available the preferred network type will be used. |
| 1347 | * |
| 1348 | * @param preference the network type to prefer over all others. It is |
| 1349 | * unspecified what happens to the old preferred network in the |
| 1350 | * overall ordering. |
| 1351 | * @deprecated Functionality has been removed as it no longer makes sense, |
| 1352 | * with many more than two networks - we'd need an array to express |
| 1353 | * preference. Instead we use dynamic network properties of |
| 1354 | * the networks to describe their precedence. |
| 1355 | */ |
| 1356 | @Deprecated |
| 1357 | public void setNetworkPreference(int preference) { |
| 1358 | } |
| 1359 | |
| 1360 | /** |
| 1361 | * Retrieves the current preferred network type. |
| 1362 | * |
| 1363 | * @return an integer representing the preferred network type |
| 1364 | * |
| 1365 | * @deprecated Functionality has been removed as it no longer makes sense, |
| 1366 | * with many more than two networks - we'd need an array to express |
| 1367 | * preference. Instead we use dynamic network properties of |
| 1368 | * the networks to describe their precedence. |
| 1369 | */ |
| 1370 | @Deprecated |
| 1371 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 1372 | public int getNetworkPreference() { |
| 1373 | return TYPE_NONE; |
| 1374 | } |
| 1375 | |
| 1376 | /** |
| 1377 | * Returns details about the currently active default data network. When |
| 1378 | * connected, this network is the default route for outgoing connections. |
| 1379 | * You should always check {@link NetworkInfo#isConnected()} before initiating |
| 1380 | * network traffic. This may return {@code null} when there is no default |
| 1381 | * network. |
| 1382 | * Note that if the default network is a VPN, this method will return the |
| 1383 | * NetworkInfo for one of its underlying networks instead, or null if the |
| 1384 | * VPN agent did not specify any. Apps interested in learning about VPNs |
| 1385 | * should use {@link #getNetworkInfo(android.net.Network)} instead. |
| 1386 | * |
| 1387 | * @return a {@link NetworkInfo} object for the current default network |
| 1388 | * or {@code null} if no default network is currently active |
| 1389 | * @deprecated See {@link NetworkInfo}. |
| 1390 | */ |
| 1391 | @Deprecated |
| 1392 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 1393 | @Nullable |
| 1394 | public NetworkInfo getActiveNetworkInfo() { |
| 1395 | try { |
| 1396 | return mService.getActiveNetworkInfo(); |
| 1397 | } catch (RemoteException e) { |
| 1398 | throw e.rethrowFromSystemServer(); |
| 1399 | } |
| 1400 | } |
| 1401 | |
| 1402 | /** |
| 1403 | * Returns a {@link Network} object corresponding to the currently active |
| 1404 | * default data network. In the event that the current active default data |
| 1405 | * network disconnects, the returned {@code Network} object will no longer |
| 1406 | * be usable. This will return {@code null} when there is no default |
Chalard Jean | 0d05151 | 2021-09-28 15:31:15 +0900 | [diff] [blame] | 1407 | * network, or when the default network is blocked. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1408 | * |
| 1409 | * @return a {@link Network} object for the current default network or |
Chalard Jean | 0d05151 | 2021-09-28 15:31:15 +0900 | [diff] [blame] | 1410 | * {@code null} if no default network is currently active or if |
| 1411 | * the default network is blocked for the caller |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1412 | */ |
| 1413 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 1414 | @Nullable |
| 1415 | public Network getActiveNetwork() { |
| 1416 | try { |
| 1417 | return mService.getActiveNetwork(); |
| 1418 | } catch (RemoteException e) { |
| 1419 | throw e.rethrowFromSystemServer(); |
| 1420 | } |
| 1421 | } |
| 1422 | |
| 1423 | /** |
| 1424 | * Returns a {@link Network} object corresponding to the currently active |
| 1425 | * default data network for a specific UID. In the event that the default data |
| 1426 | * network disconnects, the returned {@code Network} object will no longer |
| 1427 | * be usable. This will return {@code null} when there is no default |
| 1428 | * network for the UID. |
| 1429 | * |
| 1430 | * @return a {@link Network} object for the current default network for the |
| 1431 | * given UID or {@code null} if no default network is currently active |
lifr | db7d676 | 2021-03-30 21:04:53 +0800 | [diff] [blame] | 1432 | * |
| 1433 | * @hide |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1434 | */ |
| 1435 | @RequiresPermission(android.Manifest.permission.NETWORK_STACK) |
| 1436 | @Nullable |
| 1437 | public Network getActiveNetworkForUid(int uid) { |
| 1438 | return getActiveNetworkForUid(uid, false); |
| 1439 | } |
| 1440 | |
| 1441 | /** {@hide} */ |
| 1442 | public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) { |
| 1443 | try { |
| 1444 | return mService.getActiveNetworkForUid(uid, ignoreBlocked); |
| 1445 | } catch (RemoteException e) { |
| 1446 | throw e.rethrowFromSystemServer(); |
| 1447 | } |
| 1448 | } |
| 1449 | |
lucaslin | 3ba7cc2 | 2022-12-19 02:35:33 +0000 | [diff] [blame] | 1450 | private static UidRange[] getUidRangeArray(@NonNull Collection<Range<Integer>> ranges) { |
| 1451 | Objects.requireNonNull(ranges); |
| 1452 | final UidRange[] rangesArray = new UidRange[ranges.size()]; |
| 1453 | int index = 0; |
| 1454 | for (Range<Integer> range : ranges) { |
| 1455 | rangesArray[index++] = new UidRange(range.getLower(), range.getUpper()); |
| 1456 | } |
| 1457 | |
| 1458 | return rangesArray; |
| 1459 | } |
| 1460 | |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1461 | /** |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1462 | * Adds or removes a requirement for given UID ranges to use the VPN. |
| 1463 | * |
| 1464 | * If set to {@code true}, informs the system that the UIDs in the specified ranges must not |
| 1465 | * have any connectivity except if a VPN is connected and applies to the UIDs, or if the UIDs |
| 1466 | * otherwise have permission to bypass the VPN (e.g., because they have the |
| 1467 | * {@link android.Manifest.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS} permission, or when |
| 1468 | * using a socket protected by a method such as {@link VpnService#protect(DatagramSocket)}. If |
| 1469 | * set to {@code false}, a previously-added restriction is removed. |
| 1470 | * <p> |
| 1471 | * Each of the UID ranges specified by this method is added and removed as is, and no processing |
| 1472 | * is performed on the ranges to de-duplicate, merge, split, or intersect them. In order to |
| 1473 | * remove a previously-added range, the exact range must be removed as is. |
| 1474 | * <p> |
| 1475 | * The changes are applied asynchronously and may not have been applied by the time the method |
| 1476 | * returns. Apps will be notified about any changes that apply to them via |
| 1477 | * {@link NetworkCallback#onBlockedStatusChanged} callbacks called after the changes take |
| 1478 | * effect. |
| 1479 | * <p> |
lucaslin | 3ba7cc2 | 2022-12-19 02:35:33 +0000 | [diff] [blame] | 1480 | * This method will block the specified UIDs from accessing non-VPN networks, but does not |
| 1481 | * affect what the UIDs get as their default network. |
| 1482 | * Compare {@link #setVpnDefaultForUids(String, Collection)}, which declares that the UIDs |
| 1483 | * should only have a VPN as their default network, but does not block them from accessing other |
| 1484 | * networks if they request them explicitly with the {@link Network} API. |
| 1485 | * <p> |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1486 | * This method should be called only by the VPN code. |
| 1487 | * |
| 1488 | * @param ranges the UID ranges to restrict |
| 1489 | * @param requireVpn whether the specified UID ranges must use a VPN |
| 1490 | * |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1491 | * @hide |
| 1492 | */ |
| 1493 | @RequiresPermission(anyOf = { |
| 1494 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
lucaslin | 97fb10a | 2021-03-22 11:51:27 +0800 | [diff] [blame] | 1495 | android.Manifest.permission.NETWORK_STACK, |
| 1496 | android.Manifest.permission.NETWORK_SETTINGS}) |
| 1497 | @SystemApi(client = MODULE_LIBRARIES) |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1498 | public void setRequireVpnForUids(boolean requireVpn, |
| 1499 | @NonNull Collection<Range<Integer>> ranges) { |
| 1500 | Objects.requireNonNull(ranges); |
| 1501 | // The Range class is not parcelable. Convert to UidRange, which is what is used internally. |
| 1502 | // This method is not necessarily expected to be used outside the system server, so |
| 1503 | // parceling may not be necessary, but it could be used out-of-process, e.g., by the network |
| 1504 | // stack process, or by tests. |
lucaslin | 3ba7cc2 | 2022-12-19 02:35:33 +0000 | [diff] [blame] | 1505 | final UidRange[] rangesArray = getUidRangeArray(ranges); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1506 | try { |
| 1507 | mService.setRequireVpnForUids(requireVpn, rangesArray); |
| 1508 | } catch (RemoteException e) { |
| 1509 | throw e.rethrowFromSystemServer(); |
| 1510 | } |
| 1511 | } |
| 1512 | |
| 1513 | /** |
lucaslin | 3ba7cc2 | 2022-12-19 02:35:33 +0000 | [diff] [blame] | 1514 | * Inform the system that this VPN session should manage the passed UIDs. |
| 1515 | * |
| 1516 | * A VPN with the specified session ID may call this method to inform the system that the UIDs |
| 1517 | * in the specified range are subject to a VPN. |
| 1518 | * When this is called, the system will only choose a VPN for the default network of the UIDs in |
| 1519 | * the specified ranges. |
| 1520 | * |
| 1521 | * This method declares that the UIDs in the range will only have a VPN for their default |
| 1522 | * network, but does not block the UIDs from accessing other networks (permissions allowing) by |
| 1523 | * explicitly requesting it with the {@link Network} API. |
| 1524 | * Compare {@link #setRequireVpnForUids(boolean, Collection)}, which does not affect what |
| 1525 | * network the UIDs get as default, but will block them from accessing non-VPN networks. |
| 1526 | * |
| 1527 | * @param session The VPN session which manages the passed UIDs. |
| 1528 | * @param ranges The uid ranges which will treat VPN as their only default network. |
| 1529 | * |
| 1530 | * @hide |
| 1531 | */ |
| 1532 | @RequiresPermission(anyOf = { |
| 1533 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 1534 | android.Manifest.permission.NETWORK_STACK, |
| 1535 | android.Manifest.permission.NETWORK_SETTINGS}) |
| 1536 | @SystemApi(client = MODULE_LIBRARIES) |
| 1537 | public void setVpnDefaultForUids(@NonNull String session, |
| 1538 | @NonNull Collection<Range<Integer>> ranges) { |
| 1539 | Objects.requireNonNull(ranges); |
| 1540 | final UidRange[] rangesArray = getUidRangeArray(ranges); |
| 1541 | try { |
| 1542 | mService.setVpnNetworkPreference(session, rangesArray); |
| 1543 | } catch (RemoteException e) { |
| 1544 | throw e.rethrowFromSystemServer(); |
| 1545 | } |
| 1546 | } |
| 1547 | |
| 1548 | /** |
chiachangwang | e0192a7 | 2023-02-06 13:25:01 +0000 | [diff] [blame] | 1549 | * Temporarily set automaticOnOff keeplaive TCP polling alarm timer to 1 second. |
| 1550 | * |
| 1551 | * TODO: Remove this when the TCP polling design is replaced with callback. |
Jean Chalard | 17cbf06 | 2023-02-13 05:07:48 +0000 | [diff] [blame] | 1552 | * @param timeMs The time of expiry, with System.currentTimeMillis() base. The value should be |
| 1553 | * set no more than 5 minutes in the future. |
chiachangwang | e0192a7 | 2023-02-06 13:25:01 +0000 | [diff] [blame] | 1554 | * @hide |
| 1555 | */ |
| 1556 | public void setTestLowTcpPollingTimerForKeepalive(long timeMs) { |
| 1557 | try { |
| 1558 | mService.setTestLowTcpPollingTimerForKeepalive(timeMs); |
| 1559 | } catch (RemoteException e) { |
| 1560 | throw e.rethrowFromSystemServer(); |
| 1561 | } |
| 1562 | } |
| 1563 | |
| 1564 | /** |
Lorenzo Colitti | c71cff8 | 2021-01-15 01:29:01 +0900 | [diff] [blame] | 1565 | * Informs ConnectivityService of whether the legacy lockdown VPN, as implemented by |
| 1566 | * LockdownVpnTracker, is in use. This is deprecated for new devices starting from Android 12 |
| 1567 | * but is still supported for backwards compatibility. |
| 1568 | * <p> |
| 1569 | * This type of VPN is assumed always to use the system default network, and must always declare |
| 1570 | * exactly one underlying network, which is the network that was the default when the VPN |
| 1571 | * connected. |
| 1572 | * <p> |
| 1573 | * Calling this method with {@code true} enables legacy behaviour, specifically: |
| 1574 | * <ul> |
| 1575 | * <li>Any VPN that applies to userId 0 behaves specially with respect to deprecated |
| 1576 | * {@link #CONNECTIVITY_ACTION} broadcasts. Any such broadcasts will have the state in the |
| 1577 | * {@link #EXTRA_NETWORK_INFO} replaced by state of the VPN network. Also, any time the VPN |
| 1578 | * connects, a {@link #CONNECTIVITY_ACTION} broadcast will be sent for the network |
| 1579 | * underlying the VPN.</li> |
| 1580 | * <li>Deprecated APIs that return {@link NetworkInfo} objects will have their state |
| 1581 | * similarly replaced by the VPN network state.</li> |
| 1582 | * <li>Information on current network interfaces passed to NetworkStatsService will not |
| 1583 | * include any VPN interfaces.</li> |
| 1584 | * </ul> |
| 1585 | * |
| 1586 | * @param enabled whether legacy lockdown VPN is enabled or disabled |
| 1587 | * |
Lorenzo Colitti | c71cff8 | 2021-01-15 01:29:01 +0900 | [diff] [blame] | 1588 | * @hide |
| 1589 | */ |
| 1590 | @RequiresPermission(anyOf = { |
| 1591 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
lucaslin | 97fb10a | 2021-03-22 11:51:27 +0800 | [diff] [blame] | 1592 | android.Manifest.permission.NETWORK_STACK, |
Lorenzo Colitti | c71cff8 | 2021-01-15 01:29:01 +0900 | [diff] [blame] | 1593 | android.Manifest.permission.NETWORK_SETTINGS}) |
lucaslin | 97fb10a | 2021-03-22 11:51:27 +0800 | [diff] [blame] | 1594 | @SystemApi(client = MODULE_LIBRARIES) |
Lorenzo Colitti | c71cff8 | 2021-01-15 01:29:01 +0900 | [diff] [blame] | 1595 | public void setLegacyLockdownVpnEnabled(boolean enabled) { |
| 1596 | try { |
| 1597 | mService.setLegacyLockdownVpnEnabled(enabled); |
| 1598 | } catch (RemoteException e) { |
| 1599 | throw e.rethrowFromSystemServer(); |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | /** |
Chalard Jean | 0c7ebe9 | 2022-08-03 14:45:47 +0900 | [diff] [blame] | 1604 | * Returns details about the currently active default data network for a given uid. |
| 1605 | * This is for privileged use only to avoid spying on other apps. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1606 | * |
| 1607 | * @return a {@link NetworkInfo} object for the current default network |
| 1608 | * for the given uid or {@code null} if no default network is |
| 1609 | * available for the specified uid. |
| 1610 | * |
| 1611 | * {@hide} |
| 1612 | */ |
| 1613 | @RequiresPermission(android.Manifest.permission.NETWORK_STACK) |
| 1614 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 1615 | public NetworkInfo getActiveNetworkInfoForUid(int uid) { |
| 1616 | return getActiveNetworkInfoForUid(uid, false); |
| 1617 | } |
| 1618 | |
| 1619 | /** {@hide} */ |
| 1620 | public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) { |
| 1621 | try { |
| 1622 | return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked); |
| 1623 | } catch (RemoteException e) { |
| 1624 | throw e.rethrowFromSystemServer(); |
| 1625 | } |
| 1626 | } |
| 1627 | |
| 1628 | /** |
Chalard Jean | 0c7ebe9 | 2022-08-03 14:45:47 +0900 | [diff] [blame] | 1629 | * Returns connection status information about a particular network type. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1630 | * |
| 1631 | * @param networkType integer specifying which networkType in |
| 1632 | * which you're interested. |
| 1633 | * @return a {@link NetworkInfo} object for the requested |
| 1634 | * network type or {@code null} if the type is not |
| 1635 | * supported by the device. If {@code networkType} is |
| 1636 | * TYPE_VPN and a VPN is active for the calling app, |
| 1637 | * then this method will try to return one of the |
| 1638 | * underlying networks for the VPN or null if the |
| 1639 | * VPN agent didn't specify any. |
| 1640 | * |
| 1641 | * @deprecated This method does not support multiple connected networks |
| 1642 | * of the same type. Use {@link #getAllNetworks} and |
| 1643 | * {@link #getNetworkInfo(android.net.Network)} instead. |
| 1644 | */ |
| 1645 | @Deprecated |
| 1646 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 1647 | @Nullable |
| 1648 | public NetworkInfo getNetworkInfo(int networkType) { |
| 1649 | try { |
| 1650 | return mService.getNetworkInfo(networkType); |
| 1651 | } catch (RemoteException e) { |
| 1652 | throw e.rethrowFromSystemServer(); |
| 1653 | } |
| 1654 | } |
| 1655 | |
| 1656 | /** |
Chalard Jean | 0c7ebe9 | 2022-08-03 14:45:47 +0900 | [diff] [blame] | 1657 | * Returns connection status information about a particular Network. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1658 | * |
| 1659 | * @param network {@link Network} specifying which network |
| 1660 | * in which you're interested. |
| 1661 | * @return a {@link NetworkInfo} object for the requested |
| 1662 | * network or {@code null} if the {@code Network} |
| 1663 | * is not valid. |
| 1664 | * @deprecated See {@link NetworkInfo}. |
| 1665 | */ |
| 1666 | @Deprecated |
| 1667 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 1668 | @Nullable |
| 1669 | public NetworkInfo getNetworkInfo(@Nullable Network network) { |
| 1670 | return getNetworkInfoForUid(network, Process.myUid(), false); |
| 1671 | } |
| 1672 | |
| 1673 | /** {@hide} */ |
| 1674 | public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) { |
| 1675 | try { |
| 1676 | return mService.getNetworkInfoForUid(network, uid, ignoreBlocked); |
| 1677 | } catch (RemoteException e) { |
| 1678 | throw e.rethrowFromSystemServer(); |
| 1679 | } |
| 1680 | } |
| 1681 | |
| 1682 | /** |
Chalard Jean | 0c7ebe9 | 2022-08-03 14:45:47 +0900 | [diff] [blame] | 1683 | * Returns connection status information about all network types supported by the device. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1684 | * |
| 1685 | * @return an array of {@link NetworkInfo} objects. Check each |
| 1686 | * {@link NetworkInfo#getType} for which type each applies. |
| 1687 | * |
| 1688 | * @deprecated This method does not support multiple connected networks |
| 1689 | * of the same type. Use {@link #getAllNetworks} and |
| 1690 | * {@link #getNetworkInfo(android.net.Network)} instead. |
| 1691 | */ |
| 1692 | @Deprecated |
| 1693 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 1694 | @NonNull |
| 1695 | public NetworkInfo[] getAllNetworkInfo() { |
| 1696 | try { |
| 1697 | return mService.getAllNetworkInfo(); |
| 1698 | } catch (RemoteException e) { |
| 1699 | throw e.rethrowFromSystemServer(); |
| 1700 | } |
| 1701 | } |
| 1702 | |
| 1703 | /** |
junyulai | b121137 | 2021-03-03 12:09:05 +0800 | [diff] [blame] | 1704 | * Return a list of {@link NetworkStateSnapshot}s, one for each network that is currently |
| 1705 | * connected. |
| 1706 | * @hide |
| 1707 | */ |
| 1708 | @SystemApi(client = MODULE_LIBRARIES) |
| 1709 | @RequiresPermission(anyOf = { |
| 1710 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 1711 | android.Manifest.permission.NETWORK_STACK, |
| 1712 | android.Manifest.permission.NETWORK_SETTINGS}) |
| 1713 | @NonNull |
Aaron Huang | ab615e5 | 2021-04-17 13:46:25 +0800 | [diff] [blame] | 1714 | public List<NetworkStateSnapshot> getAllNetworkStateSnapshots() { |
junyulai | b121137 | 2021-03-03 12:09:05 +0800 | [diff] [blame] | 1715 | try { |
Aaron Huang | ab615e5 | 2021-04-17 13:46:25 +0800 | [diff] [blame] | 1716 | return mService.getAllNetworkStateSnapshots(); |
junyulai | b121137 | 2021-03-03 12:09:05 +0800 | [diff] [blame] | 1717 | } catch (RemoteException e) { |
| 1718 | throw e.rethrowFromSystemServer(); |
| 1719 | } |
| 1720 | } |
| 1721 | |
| 1722 | /** |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1723 | * Returns the {@link Network} object currently serving a given type, or |
| 1724 | * null if the given type is not connected. |
| 1725 | * |
| 1726 | * @hide |
| 1727 | * @deprecated This method does not support multiple connected networks |
| 1728 | * of the same type. Use {@link #getAllNetworks} and |
| 1729 | * {@link #getNetworkInfo(android.net.Network)} instead. |
| 1730 | */ |
| 1731 | @Deprecated |
| 1732 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 1733 | @UnsupportedAppUsage |
| 1734 | public Network getNetworkForType(int networkType) { |
| 1735 | try { |
| 1736 | return mService.getNetworkForType(networkType); |
| 1737 | } catch (RemoteException e) { |
| 1738 | throw e.rethrowFromSystemServer(); |
| 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | /** |
Chalard Jean | 0c7ebe9 | 2022-08-03 14:45:47 +0900 | [diff] [blame] | 1743 | * Returns an array of all {@link Network} currently tracked by the framework. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1744 | * |
Lorenzo Colitti | 86714b1 | 2021-05-17 20:31:21 +0900 | [diff] [blame] | 1745 | * @deprecated This method does not provide any notification of network state changes, forcing |
| 1746 | * apps to call it repeatedly. This is inefficient and prone to race conditions. |
| 1747 | * Apps should use methods such as |
| 1748 | * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} instead. |
| 1749 | * Apps that desire to obtain information about networks that do not apply to them |
| 1750 | * can use {@link NetworkRequest.Builder#setIncludeOtherUidNetworks}. |
| 1751 | * |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1752 | * @return an array of {@link Network} objects. |
| 1753 | */ |
| 1754 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 1755 | @NonNull |
Lorenzo Colitti | 86714b1 | 2021-05-17 20:31:21 +0900 | [diff] [blame] | 1756 | @Deprecated |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1757 | public Network[] getAllNetworks() { |
| 1758 | try { |
| 1759 | return mService.getAllNetworks(); |
| 1760 | } catch (RemoteException e) { |
| 1761 | throw e.rethrowFromSystemServer(); |
| 1762 | } |
| 1763 | } |
| 1764 | |
| 1765 | /** |
Roshan Pius | e08bc18 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 1766 | * Returns an array of {@link NetworkCapabilities} objects, representing |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1767 | * the Networks that applications run by the given user will use by default. |
| 1768 | * @hide |
| 1769 | */ |
| 1770 | @UnsupportedAppUsage |
| 1771 | public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) { |
| 1772 | try { |
| 1773 | return mService.getDefaultNetworkCapabilitiesForUser( |
Roshan Pius | a8a477b | 2020-12-17 14:53:09 -0800 | [diff] [blame] | 1774 | userId, mContext.getOpPackageName(), getAttributionTag()); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1775 | } catch (RemoteException e) { |
| 1776 | throw e.rethrowFromSystemServer(); |
| 1777 | } |
| 1778 | } |
| 1779 | |
| 1780 | /** |
| 1781 | * Returns the IP information for the current default network. |
| 1782 | * |
| 1783 | * @return a {@link LinkProperties} object describing the IP info |
| 1784 | * for the current default network, or {@code null} if there |
| 1785 | * is no current default network. |
| 1786 | * |
| 1787 | * {@hide} |
| 1788 | * @deprecated please use {@link #getLinkProperties(Network)} on the return |
| 1789 | * value of {@link #getActiveNetwork()} instead. In particular, |
| 1790 | * this method will return non-null LinkProperties even if the |
| 1791 | * app is blocked by policy from using this network. |
| 1792 | */ |
| 1793 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 1794 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 109783091) |
| 1795 | public LinkProperties getActiveLinkProperties() { |
| 1796 | try { |
| 1797 | return mService.getActiveLinkProperties(); |
| 1798 | } catch (RemoteException e) { |
| 1799 | throw e.rethrowFromSystemServer(); |
| 1800 | } |
| 1801 | } |
| 1802 | |
| 1803 | /** |
| 1804 | * Returns the IP information for a given network type. |
| 1805 | * |
| 1806 | * @param networkType the network type of interest. |
| 1807 | * @return a {@link LinkProperties} object describing the IP info |
| 1808 | * for the given networkType, or {@code null} if there is |
| 1809 | * no current default network. |
| 1810 | * |
| 1811 | * {@hide} |
| 1812 | * @deprecated This method does not support multiple connected networks |
| 1813 | * of the same type. Use {@link #getAllNetworks}, |
| 1814 | * {@link #getNetworkInfo(android.net.Network)}, and |
| 1815 | * {@link #getLinkProperties(android.net.Network)} instead. |
| 1816 | */ |
| 1817 | @Deprecated |
| 1818 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 1819 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
| 1820 | public LinkProperties getLinkProperties(int networkType) { |
| 1821 | try { |
| 1822 | return mService.getLinkPropertiesForType(networkType); |
| 1823 | } catch (RemoteException e) { |
| 1824 | throw e.rethrowFromSystemServer(); |
| 1825 | } |
| 1826 | } |
| 1827 | |
| 1828 | /** |
| 1829 | * Get the {@link LinkProperties} for the given {@link Network}. This |
| 1830 | * will return {@code null} if the network is unknown. |
| 1831 | * |
| 1832 | * @param network The {@link Network} object identifying the network in question. |
| 1833 | * @return The {@link LinkProperties} for the network, or {@code null}. |
| 1834 | */ |
| 1835 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 1836 | @Nullable |
| 1837 | public LinkProperties getLinkProperties(@Nullable Network network) { |
| 1838 | try { |
| 1839 | return mService.getLinkProperties(network); |
| 1840 | } catch (RemoteException e) { |
| 1841 | throw e.rethrowFromSystemServer(); |
| 1842 | } |
| 1843 | } |
| 1844 | |
| 1845 | /** |
lucaslin | c582d50 | 2022-01-27 09:07:00 +0800 | [diff] [blame] | 1846 | * Redact {@link LinkProperties} for a given package |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1847 | * |
lucaslin | c582d50 | 2022-01-27 09:07:00 +0800 | [diff] [blame] | 1848 | * Returns an instance of the given {@link LinkProperties} appropriately redacted to send to the |
| 1849 | * given package, considering its permissions. |
| 1850 | * |
| 1851 | * @param lp A {@link LinkProperties} which will be redacted. |
| 1852 | * @param uid The target uid. |
| 1853 | * @param packageName The name of the package, for appops logging. |
| 1854 | * @return A redacted {@link LinkProperties} which is appropriate to send to the given uid, |
| 1855 | * or null if the uid lacks the ACCESS_NETWORK_STATE permission. |
| 1856 | * @hide |
| 1857 | */ |
| 1858 | @RequiresPermission(anyOf = { |
| 1859 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 1860 | android.Manifest.permission.NETWORK_STACK, |
| 1861 | android.Manifest.permission.NETWORK_SETTINGS}) |
| 1862 | @SystemApi(client = MODULE_LIBRARIES) |
| 1863 | @Nullable |
lucaslin | d2b0613 | 2022-03-02 10:56:57 +0800 | [diff] [blame] | 1864 | public LinkProperties getRedactedLinkPropertiesForPackage(@NonNull LinkProperties lp, int uid, |
lucaslin | c582d50 | 2022-01-27 09:07:00 +0800 | [diff] [blame] | 1865 | @NonNull String packageName) { |
| 1866 | try { |
lucaslin | d2b0613 | 2022-03-02 10:56:57 +0800 | [diff] [blame] | 1867 | return mService.getRedactedLinkPropertiesForPackage( |
lucaslin | c582d50 | 2022-01-27 09:07:00 +0800 | [diff] [blame] | 1868 | lp, uid, packageName, getAttributionTag()); |
| 1869 | } catch (RemoteException e) { |
| 1870 | throw e.rethrowFromSystemServer(); |
| 1871 | } |
| 1872 | } |
| 1873 | |
| 1874 | /** |
| 1875 | * Get the {@link NetworkCapabilities} for the given {@link Network}, or null. |
| 1876 | * |
| 1877 | * This will remove any location sensitive data in the returned {@link NetworkCapabilities}. |
| 1878 | * Some {@link TransportInfo} instances like {@link android.net.wifi.WifiInfo} contain location |
| 1879 | * sensitive information. To retrieve this location sensitive information (subject to |
| 1880 | * the caller's location permissions), use a {@link NetworkCallback} with the |
| 1881 | * {@link NetworkCallback#FLAG_INCLUDE_LOCATION_INFO} flag instead. |
| 1882 | * |
| 1883 | * This method returns {@code null} if the network is unknown or if the |network| argument |
| 1884 | * is null. |
Roshan Pius | e08bc18 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 1885 | * |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1886 | * @param network The {@link Network} object identifying the network in question. |
Roshan Pius | e08bc18 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 1887 | * @return The {@link NetworkCapabilities} for the network, or {@code null}. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1888 | */ |
| 1889 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 1890 | @Nullable |
| 1891 | public NetworkCapabilities getNetworkCapabilities(@Nullable Network network) { |
| 1892 | try { |
Roshan Pius | a8a477b | 2020-12-17 14:53:09 -0800 | [diff] [blame] | 1893 | return mService.getNetworkCapabilities( |
| 1894 | network, mContext.getOpPackageName(), getAttributionTag()); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1895 | } catch (RemoteException e) { |
| 1896 | throw e.rethrowFromSystemServer(); |
| 1897 | } |
| 1898 | } |
| 1899 | |
| 1900 | /** |
lucaslin | c582d50 | 2022-01-27 09:07:00 +0800 | [diff] [blame] | 1901 | * Redact {@link NetworkCapabilities} for a given package. |
| 1902 | * |
| 1903 | * Returns an instance of {@link NetworkCapabilities} that is appropriately redacted to send |
lucaslin | d2b0613 | 2022-03-02 10:56:57 +0800 | [diff] [blame] | 1904 | * to the given package, considering its permissions. If the passed capabilities contain |
| 1905 | * location-sensitive information, they will be redacted to the correct degree for the location |
| 1906 | * permissions of the app (COARSE or FINE), and will blame the UID accordingly for retrieving |
| 1907 | * that level of location. If the UID holds no location permission, the returned object will |
| 1908 | * contain no location-sensitive information and the UID is not blamed. |
lucaslin | c582d50 | 2022-01-27 09:07:00 +0800 | [diff] [blame] | 1909 | * |
| 1910 | * @param nc A {@link NetworkCapabilities} instance which will be redacted. |
| 1911 | * @param uid The target uid. |
| 1912 | * @param packageName The name of the package, for appops logging. |
| 1913 | * @return A redacted {@link NetworkCapabilities} which is appropriate to send to the given uid, |
| 1914 | * or null if the uid lacks the ACCESS_NETWORK_STATE permission. |
| 1915 | * @hide |
| 1916 | */ |
| 1917 | @RequiresPermission(anyOf = { |
| 1918 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 1919 | android.Manifest.permission.NETWORK_STACK, |
| 1920 | android.Manifest.permission.NETWORK_SETTINGS}) |
| 1921 | @SystemApi(client = MODULE_LIBRARIES) |
| 1922 | @Nullable |
lucaslin | d2b0613 | 2022-03-02 10:56:57 +0800 | [diff] [blame] | 1923 | public NetworkCapabilities getRedactedNetworkCapabilitiesForPackage( |
lucaslin | c582d50 | 2022-01-27 09:07:00 +0800 | [diff] [blame] | 1924 | @NonNull NetworkCapabilities nc, |
| 1925 | int uid, @NonNull String packageName) { |
| 1926 | try { |
lucaslin | d2b0613 | 2022-03-02 10:56:57 +0800 | [diff] [blame] | 1927 | return mService.getRedactedNetworkCapabilitiesForPackage(nc, uid, packageName, |
lucaslin | c582d50 | 2022-01-27 09:07:00 +0800 | [diff] [blame] | 1928 | getAttributionTag()); |
| 1929 | } catch (RemoteException e) { |
| 1930 | throw e.rethrowFromSystemServer(); |
| 1931 | } |
| 1932 | } |
| 1933 | |
| 1934 | /** |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1935 | * Gets a URL that can be used for resolving whether a captive portal is present. |
| 1936 | * 1. This URL should respond with a 204 response to a GET request to indicate no captive |
| 1937 | * portal is present. |
| 1938 | * 2. This URL must be HTTP as redirect responses are used to find captive portal |
| 1939 | * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects. |
| 1940 | * |
| 1941 | * The system network validation may be using different strategies to detect captive portals, |
| 1942 | * so this method does not necessarily return a URL used by the system. It only returns a URL |
| 1943 | * that may be relevant for other components trying to detect captive portals. |
| 1944 | * |
| 1945 | * @hide |
Chalard Jean | 0c7ebe9 | 2022-08-03 14:45:47 +0900 | [diff] [blame] | 1946 | * @deprecated This API returns a URL which is not guaranteed to be one of the URLs used by the |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 1947 | * system. |
| 1948 | */ |
| 1949 | @Deprecated |
| 1950 | @SystemApi |
| 1951 | @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS) |
| 1952 | public String getCaptivePortalServerUrl() { |
| 1953 | try { |
| 1954 | return mService.getCaptivePortalServerUrl(); |
| 1955 | } catch (RemoteException e) { |
| 1956 | throw e.rethrowFromSystemServer(); |
| 1957 | } |
| 1958 | } |
| 1959 | |
| 1960 | /** |
| 1961 | * Tells the underlying networking system that the caller wants to |
| 1962 | * begin using the named feature. The interpretation of {@code feature} |
| 1963 | * is completely up to each networking implementation. |
| 1964 | * |
| 1965 | * <p>This method requires the caller to hold either the |
| 1966 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 1967 | * or the ability to modify system settings as determined by |
| 1968 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 1969 | * |
| 1970 | * @param networkType specifies which network the request pertains to |
| 1971 | * @param feature the name of the feature to be used |
| 1972 | * @return an integer value representing the outcome of the request. |
| 1973 | * The interpretation of this value is specific to each networking |
| 1974 | * implementation+feature combination, except that the value {@code -1} |
| 1975 | * always indicates failure. |
| 1976 | * |
| 1977 | * @deprecated Deprecated in favor of the cleaner |
| 1978 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API. |
| 1979 | * In {@link VERSION_CODES#M}, and above, this method is unsupported and will |
| 1980 | * throw {@code UnsupportedOperationException} if called. |
| 1981 | * @removed |
| 1982 | */ |
| 1983 | @Deprecated |
| 1984 | public int startUsingNetworkFeature(int networkType, String feature) { |
| 1985 | checkLegacyRoutingApiAccess(); |
| 1986 | NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature); |
| 1987 | if (netCap == null) { |
| 1988 | Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " + |
| 1989 | feature); |
| 1990 | return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED; |
| 1991 | } |
| 1992 | |
| 1993 | NetworkRequest request = null; |
| 1994 | synchronized (sLegacyRequests) { |
| 1995 | LegacyRequest l = sLegacyRequests.get(netCap); |
| 1996 | if (l != null) { |
| 1997 | Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest); |
| 1998 | renewRequestLocked(l); |
| 1999 | if (l.currentNetwork != null) { |
| 2000 | return DEPRECATED_PHONE_CONSTANT_APN_ALREADY_ACTIVE; |
| 2001 | } else { |
| 2002 | return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED; |
| 2003 | } |
| 2004 | } |
| 2005 | |
| 2006 | request = requestNetworkForFeatureLocked(netCap); |
| 2007 | } |
| 2008 | if (request != null) { |
| 2009 | Log.d(TAG, "starting startUsingNetworkFeature for request " + request); |
| 2010 | return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED; |
| 2011 | } else { |
| 2012 | Log.d(TAG, " request Failed"); |
| 2013 | return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED; |
| 2014 | } |
| 2015 | } |
| 2016 | |
| 2017 | /** |
| 2018 | * Tells the underlying networking system that the caller is finished |
| 2019 | * using the named feature. The interpretation of {@code feature} |
| 2020 | * is completely up to each networking implementation. |
| 2021 | * |
| 2022 | * <p>This method requires the caller to hold either the |
| 2023 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2024 | * or the ability to modify system settings as determined by |
| 2025 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 2026 | * |
| 2027 | * @param networkType specifies which network the request pertains to |
| 2028 | * @param feature the name of the feature that is no longer needed |
| 2029 | * @return an integer value representing the outcome of the request. |
| 2030 | * The interpretation of this value is specific to each networking |
| 2031 | * implementation+feature combination, except that the value {@code -1} |
| 2032 | * always indicates failure. |
| 2033 | * |
| 2034 | * @deprecated Deprecated in favor of the cleaner |
| 2035 | * {@link #unregisterNetworkCallback(NetworkCallback)} API. |
| 2036 | * In {@link VERSION_CODES#M}, and above, this method is unsupported and will |
| 2037 | * throw {@code UnsupportedOperationException} if called. |
| 2038 | * @removed |
| 2039 | */ |
| 2040 | @Deprecated |
| 2041 | public int stopUsingNetworkFeature(int networkType, String feature) { |
| 2042 | checkLegacyRoutingApiAccess(); |
| 2043 | NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature); |
| 2044 | if (netCap == null) { |
| 2045 | Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " + |
| 2046 | feature); |
| 2047 | return -1; |
| 2048 | } |
| 2049 | |
| 2050 | if (removeRequestForFeature(netCap)) { |
| 2051 | Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature); |
| 2052 | } |
| 2053 | return 1; |
| 2054 | } |
| 2055 | |
| 2056 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 2057 | private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) { |
| 2058 | if (networkType == TYPE_MOBILE) { |
| 2059 | switch (feature) { |
| 2060 | case "enableCBS": |
| 2061 | return networkCapabilitiesForType(TYPE_MOBILE_CBS); |
| 2062 | case "enableDUN": |
| 2063 | case "enableDUNAlways": |
| 2064 | return networkCapabilitiesForType(TYPE_MOBILE_DUN); |
| 2065 | case "enableFOTA": |
| 2066 | return networkCapabilitiesForType(TYPE_MOBILE_FOTA); |
| 2067 | case "enableHIPRI": |
| 2068 | return networkCapabilitiesForType(TYPE_MOBILE_HIPRI); |
| 2069 | case "enableIMS": |
| 2070 | return networkCapabilitiesForType(TYPE_MOBILE_IMS); |
| 2071 | case "enableMMS": |
| 2072 | return networkCapabilitiesForType(TYPE_MOBILE_MMS); |
| 2073 | case "enableSUPL": |
| 2074 | return networkCapabilitiesForType(TYPE_MOBILE_SUPL); |
| 2075 | default: |
| 2076 | return null; |
| 2077 | } |
| 2078 | } else if (networkType == TYPE_WIFI && "p2p".equals(feature)) { |
| 2079 | return networkCapabilitiesForType(TYPE_WIFI_P2P); |
| 2080 | } |
| 2081 | return null; |
| 2082 | } |
| 2083 | |
| 2084 | private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) { |
| 2085 | if (netCap == null) return TYPE_NONE; |
| 2086 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) { |
| 2087 | return TYPE_MOBILE_CBS; |
| 2088 | } |
| 2089 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) { |
| 2090 | return TYPE_MOBILE_IMS; |
| 2091 | } |
| 2092 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) { |
| 2093 | return TYPE_MOBILE_FOTA; |
| 2094 | } |
| 2095 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) { |
| 2096 | return TYPE_MOBILE_DUN; |
| 2097 | } |
| 2098 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) { |
| 2099 | return TYPE_MOBILE_SUPL; |
| 2100 | } |
| 2101 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) { |
| 2102 | return TYPE_MOBILE_MMS; |
| 2103 | } |
| 2104 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) { |
| 2105 | return TYPE_MOBILE_HIPRI; |
| 2106 | } |
| 2107 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) { |
| 2108 | return TYPE_WIFI_P2P; |
| 2109 | } |
| 2110 | return TYPE_NONE; |
| 2111 | } |
| 2112 | |
| 2113 | private static class LegacyRequest { |
| 2114 | NetworkCapabilities networkCapabilities; |
| 2115 | NetworkRequest networkRequest; |
| 2116 | int expireSequenceNumber; |
| 2117 | Network currentNetwork; |
| 2118 | int delay = -1; |
| 2119 | |
| 2120 | private void clearDnsBinding() { |
| 2121 | if (currentNetwork != null) { |
| 2122 | currentNetwork = null; |
| 2123 | setProcessDefaultNetworkForHostResolution(null); |
| 2124 | } |
| 2125 | } |
| 2126 | |
| 2127 | NetworkCallback networkCallback = new NetworkCallback() { |
| 2128 | @Override |
| 2129 | public void onAvailable(Network network) { |
| 2130 | currentNetwork = network; |
| 2131 | Log.d(TAG, "startUsingNetworkFeature got Network:" + network); |
| 2132 | setProcessDefaultNetworkForHostResolution(network); |
| 2133 | } |
| 2134 | @Override |
| 2135 | public void onLost(Network network) { |
| 2136 | if (network.equals(currentNetwork)) clearDnsBinding(); |
| 2137 | Log.d(TAG, "startUsingNetworkFeature lost Network:" + network); |
| 2138 | } |
| 2139 | }; |
| 2140 | } |
| 2141 | |
| 2142 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 2143 | private static final HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests = |
| 2144 | new HashMap<>(); |
| 2145 | |
| 2146 | private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) { |
| 2147 | synchronized (sLegacyRequests) { |
| 2148 | LegacyRequest l = sLegacyRequests.get(netCap); |
| 2149 | if (l != null) return l.networkRequest; |
| 2150 | } |
| 2151 | return null; |
| 2152 | } |
| 2153 | |
| 2154 | private void renewRequestLocked(LegacyRequest l) { |
| 2155 | l.expireSequenceNumber++; |
| 2156 | Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber); |
| 2157 | sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay); |
| 2158 | } |
| 2159 | |
| 2160 | private void expireRequest(NetworkCapabilities netCap, int sequenceNum) { |
| 2161 | int ourSeqNum = -1; |
| 2162 | synchronized (sLegacyRequests) { |
| 2163 | LegacyRequest l = sLegacyRequests.get(netCap); |
| 2164 | if (l == null) return; |
| 2165 | ourSeqNum = l.expireSequenceNumber; |
| 2166 | if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap); |
| 2167 | } |
| 2168 | Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum); |
| 2169 | } |
| 2170 | |
| 2171 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 2172 | private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) { |
| 2173 | int delay = -1; |
| 2174 | int type = legacyTypeForNetworkCapabilities(netCap); |
| 2175 | try { |
| 2176 | delay = mService.getRestoreDefaultNetworkDelay(type); |
| 2177 | } catch (RemoteException e) { |
| 2178 | throw e.rethrowFromSystemServer(); |
| 2179 | } |
| 2180 | LegacyRequest l = new LegacyRequest(); |
| 2181 | l.networkCapabilities = netCap; |
| 2182 | l.delay = delay; |
| 2183 | l.expireSequenceNumber = 0; |
| 2184 | l.networkRequest = sendRequestForNetwork( |
| 2185 | netCap, l.networkCallback, 0, REQUEST, type, getDefaultHandler()); |
| 2186 | if (l.networkRequest == null) return null; |
| 2187 | sLegacyRequests.put(netCap, l); |
| 2188 | sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay); |
| 2189 | return l.networkRequest; |
| 2190 | } |
| 2191 | |
| 2192 | private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) { |
| 2193 | if (delay >= 0) { |
| 2194 | Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay); |
| 2195 | CallbackHandler handler = getDefaultHandler(); |
| 2196 | Message msg = handler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap); |
| 2197 | handler.sendMessageDelayed(msg, delay); |
| 2198 | } |
| 2199 | } |
| 2200 | |
| 2201 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 2202 | private boolean removeRequestForFeature(NetworkCapabilities netCap) { |
| 2203 | final LegacyRequest l; |
| 2204 | synchronized (sLegacyRequests) { |
| 2205 | l = sLegacyRequests.remove(netCap); |
| 2206 | } |
| 2207 | if (l == null) return false; |
| 2208 | unregisterNetworkCallback(l.networkCallback); |
| 2209 | l.clearDnsBinding(); |
| 2210 | return true; |
| 2211 | } |
| 2212 | |
| 2213 | private static final SparseIntArray sLegacyTypeToTransport = new SparseIntArray(); |
| 2214 | static { |
| 2215 | sLegacyTypeToTransport.put(TYPE_MOBILE, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 2216 | sLegacyTypeToTransport.put(TYPE_MOBILE_CBS, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 2217 | sLegacyTypeToTransport.put(TYPE_MOBILE_DUN, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 2218 | sLegacyTypeToTransport.put(TYPE_MOBILE_FOTA, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 2219 | sLegacyTypeToTransport.put(TYPE_MOBILE_HIPRI, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 2220 | sLegacyTypeToTransport.put(TYPE_MOBILE_IMS, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 2221 | sLegacyTypeToTransport.put(TYPE_MOBILE_MMS, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 2222 | sLegacyTypeToTransport.put(TYPE_MOBILE_SUPL, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 2223 | sLegacyTypeToTransport.put(TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI); |
| 2224 | sLegacyTypeToTransport.put(TYPE_WIFI_P2P, NetworkCapabilities.TRANSPORT_WIFI); |
| 2225 | sLegacyTypeToTransport.put(TYPE_BLUETOOTH, NetworkCapabilities.TRANSPORT_BLUETOOTH); |
| 2226 | sLegacyTypeToTransport.put(TYPE_ETHERNET, NetworkCapabilities.TRANSPORT_ETHERNET); |
| 2227 | } |
| 2228 | |
| 2229 | private static final SparseIntArray sLegacyTypeToCapability = new SparseIntArray(); |
| 2230 | static { |
| 2231 | sLegacyTypeToCapability.put(TYPE_MOBILE_CBS, NetworkCapabilities.NET_CAPABILITY_CBS); |
| 2232 | sLegacyTypeToCapability.put(TYPE_MOBILE_DUN, NetworkCapabilities.NET_CAPABILITY_DUN); |
| 2233 | sLegacyTypeToCapability.put(TYPE_MOBILE_FOTA, NetworkCapabilities.NET_CAPABILITY_FOTA); |
| 2234 | sLegacyTypeToCapability.put(TYPE_MOBILE_IMS, NetworkCapabilities.NET_CAPABILITY_IMS); |
| 2235 | sLegacyTypeToCapability.put(TYPE_MOBILE_MMS, NetworkCapabilities.NET_CAPABILITY_MMS); |
| 2236 | sLegacyTypeToCapability.put(TYPE_MOBILE_SUPL, NetworkCapabilities.NET_CAPABILITY_SUPL); |
| 2237 | sLegacyTypeToCapability.put(TYPE_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P); |
| 2238 | } |
| 2239 | |
| 2240 | /** |
| 2241 | * Given a legacy type (TYPE_WIFI, ...) returns a NetworkCapabilities |
| 2242 | * instance suitable for registering a request or callback. Throws an |
| 2243 | * IllegalArgumentException if no mapping from the legacy type to |
| 2244 | * NetworkCapabilities is known. |
| 2245 | * |
| 2246 | * @deprecated Types are deprecated. Use {@link NetworkCallback} or {@link NetworkRequest} |
| 2247 | * to find the network instead. |
| 2248 | * @hide |
| 2249 | */ |
| 2250 | public static NetworkCapabilities networkCapabilitiesForType(int type) { |
| 2251 | final NetworkCapabilities nc = new NetworkCapabilities(); |
| 2252 | |
| 2253 | // Map from type to transports. |
| 2254 | final int NOT_FOUND = -1; |
| 2255 | final int transport = sLegacyTypeToTransport.get(type, NOT_FOUND); |
Remi NGUYEN VAN | 1818dbb | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 2256 | if (transport == NOT_FOUND) { |
| 2257 | throw new IllegalArgumentException("unknown legacy type: " + type); |
| 2258 | } |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2259 | nc.addTransportType(transport); |
| 2260 | |
| 2261 | // Map from type to capabilities. |
| 2262 | nc.addCapability(sLegacyTypeToCapability.get( |
| 2263 | type, NetworkCapabilities.NET_CAPABILITY_INTERNET)); |
| 2264 | nc.maybeMarkCapabilitiesRestricted(); |
| 2265 | return nc; |
| 2266 | } |
| 2267 | |
| 2268 | /** @hide */ |
| 2269 | public static class PacketKeepaliveCallback { |
| 2270 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 2271 | public PacketKeepaliveCallback() { |
| 2272 | } |
| 2273 | /** The requested keepalive was successfully started. */ |
| 2274 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 2275 | public void onStarted() {} |
Chalard Jean | bdb8282 | 2023-01-19 23:14:02 +0900 | [diff] [blame] | 2276 | /** The keepalive was resumed after being paused by the system. */ |
| 2277 | public void onResumed() {} |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2278 | /** The keepalive was successfully stopped. */ |
| 2279 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 2280 | public void onStopped() {} |
Chalard Jean | bdb8282 | 2023-01-19 23:14:02 +0900 | [diff] [blame] | 2281 | /** The keepalive was paused automatically by the system. */ |
| 2282 | public void onPaused() {} |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2283 | /** An error occurred. */ |
| 2284 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 2285 | public void onError(int error) {} |
| 2286 | } |
| 2287 | |
| 2288 | /** |
| 2289 | * Allows applications to request that the system periodically send specific packets on their |
| 2290 | * behalf, using hardware offload to save battery power. |
| 2291 | * |
| 2292 | * To request that the system send keepalives, call one of the methods that return a |
| 2293 | * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive}, |
| 2294 | * passing in a non-null callback. If the callback is successfully started, the callback's |
| 2295 | * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called, |
| 2296 | * specifying one of the {@code ERROR_*} constants in this class. |
| 2297 | * |
| 2298 | * To stop an existing keepalive, call {@link PacketKeepalive#stop}. The system will call |
| 2299 | * {@link PacketKeepaliveCallback#onStopped} if the operation was successful or |
| 2300 | * {@link PacketKeepaliveCallback#onError} if an error occurred. |
| 2301 | * |
| 2302 | * @deprecated Use {@link SocketKeepalive} instead. |
| 2303 | * |
| 2304 | * @hide |
| 2305 | */ |
| 2306 | public class PacketKeepalive { |
| 2307 | |
| 2308 | private static final String TAG = "PacketKeepalive"; |
| 2309 | |
| 2310 | /** @hide */ |
| 2311 | public static final int SUCCESS = 0; |
| 2312 | |
| 2313 | /** @hide */ |
| 2314 | public static final int NO_KEEPALIVE = -1; |
| 2315 | |
| 2316 | /** @hide */ |
| 2317 | public static final int BINDER_DIED = -10; |
| 2318 | |
| 2319 | /** The specified {@code Network} is not connected. */ |
| 2320 | public static final int ERROR_INVALID_NETWORK = -20; |
| 2321 | /** The specified IP addresses are invalid. For example, the specified source IP address is |
| 2322 | * not configured on the specified {@code Network}. */ |
| 2323 | public static final int ERROR_INVALID_IP_ADDRESS = -21; |
| 2324 | /** The requested port is invalid. */ |
| 2325 | public static final int ERROR_INVALID_PORT = -22; |
| 2326 | /** The packet length is invalid (e.g., too long). */ |
| 2327 | public static final int ERROR_INVALID_LENGTH = -23; |
| 2328 | /** The packet transmission interval is invalid (e.g., too short). */ |
| 2329 | public static final int ERROR_INVALID_INTERVAL = -24; |
| 2330 | |
| 2331 | /** The hardware does not support this request. */ |
| 2332 | public static final int ERROR_HARDWARE_UNSUPPORTED = -30; |
| 2333 | /** The hardware returned an error. */ |
| 2334 | public static final int ERROR_HARDWARE_ERROR = -31; |
| 2335 | |
| 2336 | /** The NAT-T destination port for IPsec */ |
| 2337 | public static final int NATT_PORT = 4500; |
| 2338 | |
| 2339 | /** The minimum interval in seconds between keepalive packet transmissions */ |
| 2340 | public static final int MIN_INTERVAL = 10; |
| 2341 | |
| 2342 | private final Network mNetwork; |
| 2343 | private final ISocketKeepaliveCallback mCallback; |
| 2344 | private final ExecutorService mExecutor; |
| 2345 | |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2346 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 2347 | public void stop() { |
| 2348 | try { |
| 2349 | mExecutor.execute(() -> { |
| 2350 | try { |
Chalard Jean | f0b261e | 2023-02-03 22:11:20 +0900 | [diff] [blame] | 2351 | mService.stopKeepalive(mCallback); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2352 | } catch (RemoteException e) { |
| 2353 | Log.e(TAG, "Error stopping packet keepalive: ", e); |
| 2354 | throw e.rethrowFromSystemServer(); |
| 2355 | } |
| 2356 | }); |
| 2357 | } catch (RejectedExecutionException e) { |
| 2358 | // The internal executor has already stopped due to previous event. |
| 2359 | } |
| 2360 | } |
| 2361 | |
| 2362 | private PacketKeepalive(Network network, PacketKeepaliveCallback callback) { |
Remi NGUYEN VAN | 1818dbb | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 2363 | Objects.requireNonNull(network, "network cannot be null"); |
| 2364 | Objects.requireNonNull(callback, "callback cannot be null"); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2365 | mNetwork = network; |
| 2366 | mExecutor = Executors.newSingleThreadExecutor(); |
| 2367 | mCallback = new ISocketKeepaliveCallback.Stub() { |
| 2368 | @Override |
Chalard Jean | f0b261e | 2023-02-03 22:11:20 +0900 | [diff] [blame] | 2369 | public void onStarted() { |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2370 | final long token = Binder.clearCallingIdentity(); |
| 2371 | try { |
| 2372 | mExecutor.execute(() -> { |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2373 | callback.onStarted(); |
| 2374 | }); |
| 2375 | } finally { |
| 2376 | Binder.restoreCallingIdentity(token); |
| 2377 | } |
| 2378 | } |
| 2379 | |
| 2380 | @Override |
Chalard Jean | bdb8282 | 2023-01-19 23:14:02 +0900 | [diff] [blame] | 2381 | public void onResumed() { |
| 2382 | final long token = Binder.clearCallingIdentity(); |
| 2383 | try { |
| 2384 | mExecutor.execute(() -> { |
| 2385 | callback.onResumed(); |
| 2386 | }); |
| 2387 | } finally { |
| 2388 | Binder.restoreCallingIdentity(token); |
| 2389 | } |
| 2390 | } |
| 2391 | |
| 2392 | @Override |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2393 | public void onStopped() { |
| 2394 | final long token = Binder.clearCallingIdentity(); |
| 2395 | try { |
| 2396 | mExecutor.execute(() -> { |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2397 | callback.onStopped(); |
| 2398 | }); |
| 2399 | } finally { |
| 2400 | Binder.restoreCallingIdentity(token); |
| 2401 | } |
| 2402 | mExecutor.shutdown(); |
| 2403 | } |
| 2404 | |
| 2405 | @Override |
Chalard Jean | bdb8282 | 2023-01-19 23:14:02 +0900 | [diff] [blame] | 2406 | public void onPaused() { |
| 2407 | final long token = Binder.clearCallingIdentity(); |
| 2408 | try { |
| 2409 | mExecutor.execute(() -> { |
| 2410 | callback.onPaused(); |
| 2411 | }); |
| 2412 | } finally { |
| 2413 | Binder.restoreCallingIdentity(token); |
| 2414 | } |
| 2415 | mExecutor.shutdown(); |
| 2416 | } |
| 2417 | |
| 2418 | @Override |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2419 | public void onError(int error) { |
| 2420 | final long token = Binder.clearCallingIdentity(); |
| 2421 | try { |
| 2422 | mExecutor.execute(() -> { |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2423 | callback.onError(error); |
| 2424 | }); |
| 2425 | } finally { |
| 2426 | Binder.restoreCallingIdentity(token); |
| 2427 | } |
| 2428 | mExecutor.shutdown(); |
| 2429 | } |
| 2430 | |
| 2431 | @Override |
| 2432 | public void onDataReceived() { |
| 2433 | // PacketKeepalive is only used for Nat-T keepalive and as such does not invoke |
| 2434 | // this callback when data is received. |
| 2435 | } |
| 2436 | }; |
| 2437 | } |
| 2438 | } |
| 2439 | |
| 2440 | /** |
| 2441 | * Starts an IPsec NAT-T keepalive packet with the specified parameters. |
| 2442 | * |
| 2443 | * @deprecated Use {@link #createSocketKeepalive} instead. |
| 2444 | * |
| 2445 | * @hide |
| 2446 | */ |
| 2447 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 2448 | public PacketKeepalive startNattKeepalive( |
| 2449 | Network network, int intervalSeconds, PacketKeepaliveCallback callback, |
| 2450 | InetAddress srcAddr, int srcPort, InetAddress dstAddr) { |
| 2451 | final PacketKeepalive k = new PacketKeepalive(network, callback); |
| 2452 | try { |
| 2453 | mService.startNattKeepalive(network, intervalSeconds, k.mCallback, |
| 2454 | srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress()); |
| 2455 | } catch (RemoteException e) { |
| 2456 | Log.e(TAG, "Error starting packet keepalive: ", e); |
| 2457 | throw e.rethrowFromSystemServer(); |
| 2458 | } |
| 2459 | return k; |
| 2460 | } |
| 2461 | |
| 2462 | // Construct an invalid fd. |
| 2463 | private ParcelFileDescriptor createInvalidFd() { |
| 2464 | final int invalidFd = -1; |
| 2465 | return ParcelFileDescriptor.adoptFd(invalidFd); |
| 2466 | } |
| 2467 | |
| 2468 | /** |
| 2469 | * Request that keepalives be started on a IPsec NAT-T socket. |
| 2470 | * |
| 2471 | * @param network The {@link Network} the socket is on. |
| 2472 | * @param socket The socket that needs to be kept alive. |
| 2473 | * @param source The source address of the {@link UdpEncapsulationSocket}. |
| 2474 | * @param destination The destination address of the {@link UdpEncapsulationSocket}. |
| 2475 | * @param executor The executor on which callback will be invoked. The provided {@link Executor} |
| 2476 | * must run callback sequentially, otherwise the order of callbacks cannot be |
| 2477 | * guaranteed. |
| 2478 | * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive |
| 2479 | * changes. Must be extended by applications that use this API. |
| 2480 | * |
| 2481 | * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the |
| 2482 | * given socket. |
| 2483 | **/ |
| 2484 | public @NonNull SocketKeepalive createSocketKeepalive(@NonNull Network network, |
| 2485 | @NonNull UdpEncapsulationSocket socket, |
| 2486 | @NonNull InetAddress source, |
| 2487 | @NonNull InetAddress destination, |
| 2488 | @NonNull @CallbackExecutor Executor executor, |
| 2489 | @NonNull Callback callback) { |
| 2490 | ParcelFileDescriptor dup; |
| 2491 | try { |
| 2492 | // Dup is needed here as the pfd inside the socket is owned by the IpSecService, |
| 2493 | // which cannot be obtained by the app process. |
| 2494 | dup = ParcelFileDescriptor.dup(socket.getFileDescriptor()); |
| 2495 | } catch (IOException ignored) { |
| 2496 | // Construct an invalid fd, so that if the user later calls start(), it will fail with |
| 2497 | // ERROR_INVALID_SOCKET. |
| 2498 | dup = createInvalidFd(); |
| 2499 | } |
| 2500 | return new NattSocketKeepalive(mService, network, dup, socket.getResourceId(), source, |
| 2501 | destination, executor, callback); |
| 2502 | } |
| 2503 | |
| 2504 | /** |
| 2505 | * Request that keepalives be started on a IPsec NAT-T socket file descriptor. Directly called |
| 2506 | * by system apps which don't use IpSecService to create {@link UdpEncapsulationSocket}. |
| 2507 | * |
| 2508 | * @param network The {@link Network} the socket is on. |
| 2509 | * @param pfd The {@link ParcelFileDescriptor} that needs to be kept alive. The provided |
| 2510 | * {@link ParcelFileDescriptor} must be bound to a port and the keepalives will be sent |
| 2511 | * from that port. |
| 2512 | * @param source The source address of the {@link UdpEncapsulationSocket}. |
| 2513 | * @param destination The destination address of the {@link UdpEncapsulationSocket}. The |
| 2514 | * keepalive packets will always be sent to port 4500 of the given {@code destination}. |
| 2515 | * @param executor The executor on which callback will be invoked. The provided {@link Executor} |
| 2516 | * must run callback sequentially, otherwise the order of callbacks cannot be |
| 2517 | * guaranteed. |
| 2518 | * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive |
| 2519 | * changes. Must be extended by applications that use this API. |
| 2520 | * |
| 2521 | * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the |
| 2522 | * given socket. |
| 2523 | * @hide |
| 2524 | */ |
| 2525 | @SystemApi |
| 2526 | @RequiresPermission(android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD) |
| 2527 | public @NonNull SocketKeepalive createNattKeepalive(@NonNull Network network, |
| 2528 | @NonNull ParcelFileDescriptor pfd, |
| 2529 | @NonNull InetAddress source, |
| 2530 | @NonNull InetAddress destination, |
| 2531 | @NonNull @CallbackExecutor Executor executor, |
| 2532 | @NonNull Callback callback) { |
| 2533 | ParcelFileDescriptor dup; |
| 2534 | try { |
| 2535 | // TODO: Consider remove unnecessary dup. |
| 2536 | dup = pfd.dup(); |
| 2537 | } catch (IOException ignored) { |
| 2538 | // Construct an invalid fd, so that if the user later calls start(), it will fail with |
| 2539 | // ERROR_INVALID_SOCKET. |
| 2540 | dup = createInvalidFd(); |
| 2541 | } |
| 2542 | return new NattSocketKeepalive(mService, network, dup, |
Remi NGUYEN VAN | a29be5c | 2021-03-11 10:56:49 +0000 | [diff] [blame] | 2543 | -1 /* Unused */, source, destination, executor, callback); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2544 | } |
| 2545 | |
| 2546 | /** |
Chalard Jean | 0c7ebe9 | 2022-08-03 14:45:47 +0900 | [diff] [blame] | 2547 | * Request that keepalives be started on a TCP socket. The socket must be established. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2548 | * |
| 2549 | * @param network The {@link Network} the socket is on. |
| 2550 | * @param socket The socket that needs to be kept alive. |
| 2551 | * @param executor The executor on which callback will be invoked. This implementation assumes |
| 2552 | * the provided {@link Executor} runs the callbacks in sequence with no |
| 2553 | * concurrency. Failing this, no guarantee of correctness can be made. It is |
| 2554 | * the responsibility of the caller to ensure the executor provides this |
| 2555 | * guarantee. A simple way of creating such an executor is with the standard |
| 2556 | * tool {@code Executors.newSingleThreadExecutor}. |
| 2557 | * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive |
| 2558 | * changes. Must be extended by applications that use this API. |
| 2559 | * |
| 2560 | * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the |
| 2561 | * given socket. |
| 2562 | * @hide |
| 2563 | */ |
| 2564 | @SystemApi |
| 2565 | @RequiresPermission(android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD) |
| 2566 | public @NonNull SocketKeepalive createSocketKeepalive(@NonNull Network network, |
| 2567 | @NonNull Socket socket, |
Sherri Lin | 443b718 | 2023-02-08 04:49:29 +0100 | [diff] [blame] | 2568 | @NonNull @CallbackExecutor Executor executor, |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2569 | @NonNull Callback callback) { |
| 2570 | ParcelFileDescriptor dup; |
| 2571 | try { |
| 2572 | dup = ParcelFileDescriptor.fromSocket(socket); |
| 2573 | } catch (UncheckedIOException ignored) { |
| 2574 | // Construct an invalid fd, so that if the user later calls start(), it will fail with |
| 2575 | // ERROR_INVALID_SOCKET. |
| 2576 | dup = createInvalidFd(); |
| 2577 | } |
| 2578 | return new TcpSocketKeepalive(mService, network, dup, executor, callback); |
| 2579 | } |
| 2580 | |
| 2581 | /** |
Remi NGUYEN VAN | bee2ee1 | 2023-02-20 20:10:09 +0900 | [diff] [blame] | 2582 | * Get the supported keepalive count for each transport configured in resource overlays. |
| 2583 | * |
| 2584 | * @return An array of supported keepalive count for each transport type. |
| 2585 | * @hide |
| 2586 | */ |
| 2587 | @RequiresPermission(anyOf = { android.Manifest.permission.NETWORK_SETTINGS, |
| 2588 | // CTS 13 used QUERY_ALL_PACKAGES to get the resource value, which was implemented |
| 2589 | // as below in KeepaliveUtils. Also allow that permission so that KeepaliveUtils can |
| 2590 | // use this method and avoid breaking released CTS. Apps that have this permission |
| 2591 | // can query the resource themselves anyway. |
| 2592 | android.Manifest.permission.QUERY_ALL_PACKAGES }) |
| 2593 | public int[] getSupportedKeepalives() { |
| 2594 | try { |
| 2595 | return mService.getSupportedKeepalives(); |
| 2596 | } catch (RemoteException e) { |
| 2597 | throw e.rethrowFromSystemServer(); |
| 2598 | } |
| 2599 | } |
| 2600 | |
| 2601 | /** |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2602 | * Ensure that a network route exists to deliver traffic to the specified |
| 2603 | * host via the specified network interface. An attempt to add a route that |
| 2604 | * already exists is ignored, but treated as successful. |
| 2605 | * |
| 2606 | * <p>This method requires the caller to hold either the |
| 2607 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2608 | * or the ability to modify system settings as determined by |
| 2609 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 2610 | * |
| 2611 | * @param networkType the type of the network over which traffic to the specified |
| 2612 | * host is to be routed |
| 2613 | * @param hostAddress the IP address of the host to which the route is desired |
| 2614 | * @return {@code true} on success, {@code false} on failure |
| 2615 | * |
| 2616 | * @deprecated Deprecated in favor of the |
| 2617 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, |
| 2618 | * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API. |
| 2619 | * In {@link VERSION_CODES#M}, and above, this method is unsupported and will |
| 2620 | * throw {@code UnsupportedOperationException} if called. |
| 2621 | * @removed |
| 2622 | */ |
| 2623 | @Deprecated |
| 2624 | public boolean requestRouteToHost(int networkType, int hostAddress) { |
| 2625 | return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress)); |
| 2626 | } |
| 2627 | |
| 2628 | /** |
| 2629 | * Ensure that a network route exists to deliver traffic to the specified |
| 2630 | * host via the specified network interface. An attempt to add a route that |
| 2631 | * already exists is ignored, but treated as successful. |
| 2632 | * |
| 2633 | * <p>This method requires the caller to hold either the |
| 2634 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2635 | * or the ability to modify system settings as determined by |
| 2636 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 2637 | * |
| 2638 | * @param networkType the type of the network over which traffic to the specified |
| 2639 | * host is to be routed |
| 2640 | * @param hostAddress the IP address of the host to which the route is desired |
| 2641 | * @return {@code true} on success, {@code false} on failure |
| 2642 | * @hide |
| 2643 | * @deprecated Deprecated in favor of the {@link #requestNetwork} and |
| 2644 | * {@link #bindProcessToNetwork} API. |
| 2645 | */ |
| 2646 | @Deprecated |
| 2647 | @UnsupportedAppUsage |
lucaslin | 97fb10a | 2021-03-22 11:51:27 +0800 | [diff] [blame] | 2648 | @SystemApi(client = MODULE_LIBRARIES) |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2649 | public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) { |
| 2650 | checkLegacyRoutingApiAccess(); |
| 2651 | try { |
| 2652 | return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress(), |
| 2653 | mContext.getOpPackageName(), getAttributionTag()); |
| 2654 | } catch (RemoteException e) { |
| 2655 | throw e.rethrowFromSystemServer(); |
| 2656 | } |
| 2657 | } |
| 2658 | |
| 2659 | /** |
| 2660 | * @return the context's attribution tag |
| 2661 | */ |
| 2662 | // TODO: Remove method and replace with direct call once R code is pushed to AOSP |
| 2663 | private @Nullable String getAttributionTag() { |
Remi NGUYEN VAN | a522fc2 | 2021-02-01 10:25:24 +0000 | [diff] [blame] | 2664 | return mContext.getAttributionTag(); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2665 | } |
| 2666 | |
| 2667 | /** |
| 2668 | * Returns the value of the setting for background data usage. If false, |
| 2669 | * applications should not use the network if the application is not in the |
| 2670 | * foreground. Developers should respect this setting, and check the value |
| 2671 | * of this before performing any background data operations. |
| 2672 | * <p> |
| 2673 | * All applications that have background services that use the network |
| 2674 | * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}. |
| 2675 | * <p> |
| 2676 | * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of |
| 2677 | * background data depends on several combined factors, and this method will |
| 2678 | * always return {@code true}. Instead, when background data is unavailable, |
| 2679 | * {@link #getActiveNetworkInfo()} will now appear disconnected. |
| 2680 | * |
| 2681 | * @return Whether background data usage is allowed. |
| 2682 | */ |
| 2683 | @Deprecated |
| 2684 | public boolean getBackgroundDataSetting() { |
| 2685 | // assume that background data is allowed; final authority is |
| 2686 | // NetworkInfo which may be blocked. |
| 2687 | return true; |
| 2688 | } |
| 2689 | |
| 2690 | /** |
| 2691 | * Sets the value of the setting for background data usage. |
| 2692 | * |
| 2693 | * @param allowBackgroundData Whether an application should use data while |
| 2694 | * it is in the background. |
| 2695 | * |
| 2696 | * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING |
| 2697 | * @see #getBackgroundDataSetting() |
| 2698 | * @hide |
| 2699 | */ |
| 2700 | @Deprecated |
| 2701 | @UnsupportedAppUsage |
| 2702 | public void setBackgroundDataSetting(boolean allowBackgroundData) { |
| 2703 | // ignored |
| 2704 | } |
| 2705 | |
| 2706 | /** |
| 2707 | * @hide |
| 2708 | * @deprecated Talk to TelephonyManager directly |
| 2709 | */ |
| 2710 | @Deprecated |
| 2711 | @UnsupportedAppUsage |
| 2712 | public boolean getMobileDataEnabled() { |
| 2713 | TelephonyManager tm = mContext.getSystemService(TelephonyManager.class); |
| 2714 | if (tm != null) { |
| 2715 | int subId = SubscriptionManager.getDefaultDataSubscriptionId(); |
| 2716 | Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId); |
| 2717 | boolean retVal = tm.createForSubscriptionId(subId).isDataEnabled(); |
| 2718 | Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId |
| 2719 | + " retVal=" + retVal); |
| 2720 | return retVal; |
| 2721 | } |
| 2722 | Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false"); |
| 2723 | return false; |
| 2724 | } |
| 2725 | |
| 2726 | /** |
| 2727 | * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener} |
| 2728 | * to find out when the system default network has gone in to a high power state. |
| 2729 | */ |
| 2730 | public interface OnNetworkActiveListener { |
| 2731 | /** |
| 2732 | * Called on the main thread of the process to report that the current data network |
| 2733 | * has become active, and it is now a good time to perform any pending network |
| 2734 | * operations. Note that this listener only tells you when the network becomes |
| 2735 | * active; if at any other time you want to know whether it is active (and thus okay |
| 2736 | * to initiate network traffic), you can retrieve its instantaneous state with |
| 2737 | * {@link ConnectivityManager#isDefaultNetworkActive}. |
| 2738 | */ |
| 2739 | void onNetworkActive(); |
| 2740 | } |
| 2741 | |
Chiachang Wang | 2de4168 | 2021-09-23 10:46:03 +0800 | [diff] [blame] | 2742 | @GuardedBy("mNetworkActivityListeners") |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2743 | private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener> |
| 2744 | mNetworkActivityListeners = new ArrayMap<>(); |
| 2745 | |
| 2746 | /** |
| 2747 | * Start listening to reports when the system's default data network is active, meaning it is |
| 2748 | * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()} |
| 2749 | * to determine the current state of the system's default network after registering the |
| 2750 | * listener. |
| 2751 | * <p> |
| 2752 | * If the process default network has been set with |
| 2753 | * {@link ConnectivityManager#bindProcessToNetwork} this function will not |
| 2754 | * reflect the process's default, but the system default. |
| 2755 | * |
| 2756 | * @param l The listener to be told when the network is active. |
| 2757 | */ |
| 2758 | public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) { |
Chiachang Wang | 2de4168 | 2021-09-23 10:46:03 +0800 | [diff] [blame] | 2759 | final INetworkActivityListener rl = new INetworkActivityListener.Stub() { |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2760 | @Override |
| 2761 | public void onNetworkActive() throws RemoteException { |
| 2762 | l.onNetworkActive(); |
| 2763 | } |
| 2764 | }; |
| 2765 | |
Chiachang Wang | 2de4168 | 2021-09-23 10:46:03 +0800 | [diff] [blame] | 2766 | synchronized (mNetworkActivityListeners) { |
| 2767 | try { |
| 2768 | mService.registerNetworkActivityListener(rl); |
| 2769 | mNetworkActivityListeners.put(l, rl); |
| 2770 | } catch (RemoteException e) { |
| 2771 | throw e.rethrowFromSystemServer(); |
| 2772 | } |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2773 | } |
| 2774 | } |
| 2775 | |
| 2776 | /** |
| 2777 | * Remove network active listener previously registered with |
| 2778 | * {@link #addDefaultNetworkActiveListener}. |
| 2779 | * |
| 2780 | * @param l Previously registered listener. |
| 2781 | */ |
| 2782 | public void removeDefaultNetworkActiveListener(@NonNull OnNetworkActiveListener l) { |
Chiachang Wang | 2de4168 | 2021-09-23 10:46:03 +0800 | [diff] [blame] | 2783 | synchronized (mNetworkActivityListeners) { |
| 2784 | final INetworkActivityListener rl = mNetworkActivityListeners.get(l); |
| 2785 | if (rl == null) { |
| 2786 | throw new IllegalArgumentException("Listener was not registered."); |
| 2787 | } |
| 2788 | try { |
| 2789 | mService.unregisterNetworkActivityListener(rl); |
| 2790 | mNetworkActivityListeners.remove(l); |
| 2791 | } catch (RemoteException e) { |
| 2792 | throw e.rethrowFromSystemServer(); |
| 2793 | } |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2794 | } |
| 2795 | } |
| 2796 | |
| 2797 | /** |
| 2798 | * Return whether the data network is currently active. An active network means that |
| 2799 | * it is currently in a high power state for performing data transmission. On some |
| 2800 | * types of networks, it may be expensive to move and stay in such a state, so it is |
| 2801 | * more power efficient to batch network traffic together when the radio is already in |
| 2802 | * this state. This method tells you whether right now is currently a good time to |
| 2803 | * initiate network traffic, as the network is already active. |
| 2804 | */ |
| 2805 | public boolean isDefaultNetworkActive() { |
| 2806 | try { |
lucaslin | 709eb84 | 2021-01-21 02:04:15 +0800 | [diff] [blame] | 2807 | return mService.isDefaultNetworkActive(); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2808 | } catch (RemoteException e) { |
| 2809 | throw e.rethrowFromSystemServer(); |
| 2810 | } |
| 2811 | } |
| 2812 | |
| 2813 | /** |
| 2814 | * {@hide} |
| 2815 | */ |
| 2816 | public ConnectivityManager(Context context, IConnectivityManager service) { |
markchien | d201566 | 2022-04-26 18:08:03 +0800 | [diff] [blame] | 2817 | this(context, service, true /* newStatic */); |
| 2818 | } |
| 2819 | |
| 2820 | private ConnectivityManager(Context context, IConnectivityManager service, boolean newStatic) { |
Remi NGUYEN VAN | 1818dbb | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 2821 | mContext = Objects.requireNonNull(context, "missing context"); |
| 2822 | mService = Objects.requireNonNull(service, "missing IConnectivityManager"); |
markchien | d201566 | 2022-04-26 18:08:03 +0800 | [diff] [blame] | 2823 | // sInstance is accessed without a lock, so it may actually be reassigned several times with |
| 2824 | // different ConnectivityManager, but that's still OK considering its usage. |
| 2825 | if (sInstance == null && newStatic) { |
| 2826 | final Context appContext = mContext.getApplicationContext(); |
| 2827 | // Don't create static ConnectivityManager instance again to prevent infinite loop. |
| 2828 | // If the application context is null, we're either in the system process or |
| 2829 | // it's the application context very early in app initialization. In both these |
| 2830 | // cases, the passed-in Context will not be freed, so it's safe to pass it to the |
| 2831 | // service. http://b/27532714 . |
| 2832 | sInstance = new ConnectivityManager(appContext != null ? appContext : context, service, |
| 2833 | false /* newStatic */); |
| 2834 | } |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2835 | } |
| 2836 | |
| 2837 | /** {@hide} */ |
| 2838 | @UnsupportedAppUsage |
| 2839 | public static ConnectivityManager from(Context context) { |
| 2840 | return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 2841 | } |
| 2842 | |
| 2843 | /** @hide */ |
| 2844 | public NetworkRequest getDefaultRequest() { |
| 2845 | try { |
| 2846 | // This is not racy as the default request is final in ConnectivityService. |
| 2847 | return mService.getDefaultRequest(); |
| 2848 | } catch (RemoteException e) { |
| 2849 | throw e.rethrowFromSystemServer(); |
| 2850 | } |
| 2851 | } |
| 2852 | |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2853 | /** |
Chalard Jean | 0c7ebe9 | 2022-08-03 14:45:47 +0900 | [diff] [blame] | 2854 | * Check if the package is allowed to write settings. This also records that such an access |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2855 | * happened. |
| 2856 | * |
| 2857 | * @return {@code true} iff the package is allowed to write settings. |
| 2858 | */ |
| 2859 | // TODO: Remove method and replace with direct call once R code is pushed to AOSP |
| 2860 | private static boolean checkAndNoteWriteSettingsOperation(@NonNull Context context, int uid, |
| 2861 | @NonNull String callingPackage, @Nullable String callingAttributionTag, |
| 2862 | boolean throwException) { |
| 2863 | return Settings.checkAndNoteWriteSettingsOperation(context, uid, callingPackage, |
Remi NGUYEN VAN | a522fc2 | 2021-02-01 10:25:24 +0000 | [diff] [blame] | 2864 | callingAttributionTag, throwException); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2865 | } |
| 2866 | |
| 2867 | /** |
| 2868 | * @deprecated - use getSystemService. This is a kludge to support static access in certain |
| 2869 | * situations where a Context pointer is unavailable. |
| 2870 | * @hide |
| 2871 | */ |
| 2872 | @Deprecated |
| 2873 | static ConnectivityManager getInstanceOrNull() { |
| 2874 | return sInstance; |
| 2875 | } |
| 2876 | |
| 2877 | /** |
| 2878 | * @deprecated - use getSystemService. This is a kludge to support static access in certain |
| 2879 | * situations where a Context pointer is unavailable. |
| 2880 | * @hide |
| 2881 | */ |
| 2882 | @Deprecated |
| 2883 | @UnsupportedAppUsage |
| 2884 | private static ConnectivityManager getInstance() { |
| 2885 | if (getInstanceOrNull() == null) { |
| 2886 | throw new IllegalStateException("No ConnectivityManager yet constructed"); |
| 2887 | } |
| 2888 | return getInstanceOrNull(); |
| 2889 | } |
| 2890 | |
| 2891 | /** |
| 2892 | * Get the set of tetherable, available interfaces. This list is limited by |
| 2893 | * device configuration and current interface existence. |
| 2894 | * |
| 2895 | * @return an array of 0 or more Strings of tetherable interface names. |
| 2896 | * |
| 2897 | * @deprecated Use {@link TetheringEventCallback#onTetherableInterfacesChanged(List)} instead. |
| 2898 | * {@hide} |
| 2899 | */ |
| 2900 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 2901 | @UnsupportedAppUsage |
| 2902 | @Deprecated |
| 2903 | public String[] getTetherableIfaces() { |
Remi NGUYEN VAN | e4d1cd9 | 2021-06-17 16:27:31 +0900 | [diff] [blame] | 2904 | return getTetheringManager().getTetherableIfaces(); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2905 | } |
| 2906 | |
| 2907 | /** |
| 2908 | * Get the set of tethered interfaces. |
| 2909 | * |
| 2910 | * @return an array of 0 or more String of currently tethered interface names. |
| 2911 | * |
| 2912 | * @deprecated Use {@link TetheringEventCallback#onTetherableInterfacesChanged(List)} instead. |
| 2913 | * {@hide} |
| 2914 | */ |
| 2915 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 2916 | @UnsupportedAppUsage |
| 2917 | @Deprecated |
| 2918 | public String[] getTetheredIfaces() { |
Remi NGUYEN VAN | e4d1cd9 | 2021-06-17 16:27:31 +0900 | [diff] [blame] | 2919 | return getTetheringManager().getTetheredIfaces(); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2920 | } |
| 2921 | |
| 2922 | /** |
| 2923 | * Get the set of interface names which attempted to tether but |
| 2924 | * failed. Re-attempting to tether may cause them to reset to the Tethered |
| 2925 | * state. Alternatively, causing the interface to be destroyed and recreated |
| 2926 | * may cause them to reset to the available state. |
| 2927 | * {@link ConnectivityManager#getLastTetherError} can be used to get more |
| 2928 | * information on the cause of the errors. |
| 2929 | * |
| 2930 | * @return an array of 0 or more String indicating the interface names |
| 2931 | * which failed to tether. |
| 2932 | * |
| 2933 | * @deprecated Use {@link TetheringEventCallback#onError(String, int)} instead. |
| 2934 | * {@hide} |
| 2935 | */ |
| 2936 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 2937 | @UnsupportedAppUsage |
| 2938 | @Deprecated |
| 2939 | public String[] getTetheringErroredIfaces() { |
Remi NGUYEN VAN | e4d1cd9 | 2021-06-17 16:27:31 +0900 | [diff] [blame] | 2940 | return getTetheringManager().getTetheringErroredIfaces(); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2941 | } |
| 2942 | |
| 2943 | /** |
| 2944 | * Get the set of tethered dhcp ranges. |
| 2945 | * |
| 2946 | * @deprecated This method is not supported. |
| 2947 | * TODO: remove this function when all of clients are removed. |
| 2948 | * {@hide} |
| 2949 | */ |
| 2950 | @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS) |
| 2951 | @Deprecated |
| 2952 | public String[] getTetheredDhcpRanges() { |
| 2953 | throw new UnsupportedOperationException("getTetheredDhcpRanges is not supported"); |
| 2954 | } |
| 2955 | |
| 2956 | /** |
Chalard Jean | 0c7ebe9 | 2022-08-03 14:45:47 +0900 | [diff] [blame] | 2957 | * Attempt to tether the named interface. This will set up a dhcp server |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2958 | * on the interface, forward and NAT IP packets and forward DNS requests |
| 2959 | * to the best active upstream network interface. Note that if no upstream |
| 2960 | * IP network interface is available, dhcp will still run and traffic will be |
| 2961 | * allowed between the tethered devices and this device, though upstream net |
| 2962 | * access will of course fail until an upstream network interface becomes |
| 2963 | * active. |
| 2964 | * |
| 2965 | * <p>This method requires the caller to hold either the |
| 2966 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2967 | * or the ability to modify system settings as determined by |
| 2968 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 2969 | * |
| 2970 | * <p>WARNING: New clients should not use this function. The only usages should be in PanService |
| 2971 | * and WifiStateMachine which need direct access. All other clients should use |
| 2972 | * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning |
| 2973 | * logic.</p> |
| 2974 | * |
| 2975 | * @param iface the interface name to tether. |
| 2976 | * @return error a {@code TETHER_ERROR} value indicating success or failure type |
| 2977 | * @deprecated Use {@link TetheringManager#startTethering} instead |
| 2978 | * |
| 2979 | * {@hide} |
| 2980 | */ |
| 2981 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 2982 | @Deprecated |
| 2983 | public int tether(String iface) { |
Remi NGUYEN VAN | e4d1cd9 | 2021-06-17 16:27:31 +0900 | [diff] [blame] | 2984 | return getTetheringManager().tether(iface); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 2985 | } |
| 2986 | |
| 2987 | /** |
| 2988 | * Stop tethering the named interface. |
| 2989 | * |
| 2990 | * <p>This method requires the caller to hold either the |
| 2991 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2992 | * or the ability to modify system settings as determined by |
| 2993 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 2994 | * |
| 2995 | * <p>WARNING: New clients should not use this function. The only usages should be in PanService |
| 2996 | * and WifiStateMachine which need direct access. All other clients should use |
| 2997 | * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning |
| 2998 | * logic.</p> |
| 2999 | * |
| 3000 | * @param iface the interface name to untether. |
| 3001 | * @return error a {@code TETHER_ERROR} value indicating success or failure type |
| 3002 | * |
| 3003 | * {@hide} |
| 3004 | */ |
| 3005 | @UnsupportedAppUsage |
| 3006 | @Deprecated |
| 3007 | public int untether(String iface) { |
Remi NGUYEN VAN | e4d1cd9 | 2021-06-17 16:27:31 +0900 | [diff] [blame] | 3008 | return getTetheringManager().untether(iface); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3009 | } |
| 3010 | |
| 3011 | /** |
| 3012 | * Check if the device allows for tethering. It may be disabled via |
| 3013 | * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or |
| 3014 | * due to device configuration. |
| 3015 | * |
| 3016 | * <p>If this app does not have permission to use this API, it will always |
| 3017 | * return false rather than throw an exception.</p> |
| 3018 | * |
| 3019 | * <p>If the device has a hotspot provisioning app, the caller is required to hold the |
| 3020 | * {@link android.Manifest.permission.TETHER_PRIVILEGED} permission.</p> |
| 3021 | * |
| 3022 | * <p>Otherwise, this method requires the caller to hold the ability to modify system |
| 3023 | * settings as determined by {@link android.provider.Settings.System#canWrite}.</p> |
| 3024 | * |
| 3025 | * @return a boolean - {@code true} indicating Tethering is supported. |
| 3026 | * |
| 3027 | * @deprecated Use {@link TetheringEventCallback#onTetheringSupported(boolean)} instead. |
| 3028 | * {@hide} |
| 3029 | */ |
| 3030 | @SystemApi |
| 3031 | @RequiresPermission(anyOf = {android.Manifest.permission.TETHER_PRIVILEGED, |
| 3032 | android.Manifest.permission.WRITE_SETTINGS}) |
| 3033 | public boolean isTetheringSupported() { |
Remi NGUYEN VAN | e4d1cd9 | 2021-06-17 16:27:31 +0900 | [diff] [blame] | 3034 | return getTetheringManager().isTetheringSupported(); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3035 | } |
| 3036 | |
| 3037 | /** |
| 3038 | * Callback for use with {@link #startTethering} to find out whether tethering succeeded. |
| 3039 | * |
| 3040 | * @deprecated Use {@link TetheringManager.StartTetheringCallback} instead. |
| 3041 | * @hide |
| 3042 | */ |
| 3043 | @SystemApi |
| 3044 | @Deprecated |
| 3045 | public static abstract class OnStartTetheringCallback { |
| 3046 | /** |
| 3047 | * Called when tethering has been successfully started. |
| 3048 | */ |
| 3049 | public void onTetheringStarted() {} |
| 3050 | |
| 3051 | /** |
| 3052 | * Called when starting tethering failed. |
| 3053 | */ |
| 3054 | public void onTetheringFailed() {} |
| 3055 | } |
| 3056 | |
| 3057 | /** |
| 3058 | * Convenient overload for |
| 3059 | * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null |
| 3060 | * handler to run on the current thread's {@link Looper}. |
| 3061 | * |
| 3062 | * @deprecated Use {@link TetheringManager#startTethering} instead. |
| 3063 | * @hide |
| 3064 | */ |
| 3065 | @SystemApi |
| 3066 | @Deprecated |
| 3067 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
| 3068 | public void startTethering(int type, boolean showProvisioningUi, |
| 3069 | final OnStartTetheringCallback callback) { |
| 3070 | startTethering(type, showProvisioningUi, callback, null); |
| 3071 | } |
| 3072 | |
| 3073 | /** |
| 3074 | * Runs tether provisioning for the given type if needed and then starts tethering if |
| 3075 | * the check succeeds. If no carrier provisioning is required for tethering, tethering is |
| 3076 | * enabled immediately. If provisioning fails, tethering will not be enabled. It also |
| 3077 | * schedules tether provisioning re-checks if appropriate. |
| 3078 | * |
| 3079 | * @param type The type of tethering to start. Must be one of |
| 3080 | * {@link ConnectivityManager.TETHERING_WIFI}, |
| 3081 | * {@link ConnectivityManager.TETHERING_USB}, or |
| 3082 | * {@link ConnectivityManager.TETHERING_BLUETOOTH}. |
| 3083 | * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there |
| 3084 | * is one. This should be true the first time this function is called and also any time |
| 3085 | * the user can see this UI. It gives users information from their carrier about the |
| 3086 | * check failing and how they can sign up for tethering if possible. |
| 3087 | * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller |
| 3088 | * of the result of trying to tether. |
| 3089 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
| 3090 | * |
| 3091 | * @deprecated Use {@link TetheringManager#startTethering} instead. |
| 3092 | * @hide |
| 3093 | */ |
| 3094 | @SystemApi |
| 3095 | @Deprecated |
| 3096 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
| 3097 | public void startTethering(int type, boolean showProvisioningUi, |
| 3098 | final OnStartTetheringCallback callback, Handler handler) { |
Remi NGUYEN VAN | 1818dbb | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 3099 | Objects.requireNonNull(callback, "OnStartTetheringCallback cannot be null."); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3100 | |
| 3101 | final Executor executor = new Executor() { |
| 3102 | @Override |
| 3103 | public void execute(Runnable command) { |
| 3104 | if (handler == null) { |
| 3105 | command.run(); |
| 3106 | } else { |
| 3107 | handler.post(command); |
| 3108 | } |
| 3109 | } |
| 3110 | }; |
| 3111 | |
| 3112 | final StartTetheringCallback tetheringCallback = new StartTetheringCallback() { |
| 3113 | @Override |
| 3114 | public void onTetheringStarted() { |
| 3115 | callback.onTetheringStarted(); |
| 3116 | } |
| 3117 | |
| 3118 | @Override |
| 3119 | public void onTetheringFailed(final int error) { |
| 3120 | callback.onTetheringFailed(); |
| 3121 | } |
| 3122 | }; |
| 3123 | |
| 3124 | final TetheringRequest request = new TetheringRequest.Builder(type) |
| 3125 | .setShouldShowEntitlementUi(showProvisioningUi).build(); |
| 3126 | |
Remi NGUYEN VAN | e4d1cd9 | 2021-06-17 16:27:31 +0900 | [diff] [blame] | 3127 | getTetheringManager().startTethering(request, executor, tetheringCallback); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3128 | } |
| 3129 | |
| 3130 | /** |
| 3131 | * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if |
| 3132 | * applicable. |
| 3133 | * |
| 3134 | * @param type The type of tethering to stop. Must be one of |
| 3135 | * {@link ConnectivityManager.TETHERING_WIFI}, |
| 3136 | * {@link ConnectivityManager.TETHERING_USB}, or |
| 3137 | * {@link ConnectivityManager.TETHERING_BLUETOOTH}. |
| 3138 | * |
| 3139 | * @deprecated Use {@link TetheringManager#stopTethering} instead. |
| 3140 | * @hide |
| 3141 | */ |
| 3142 | @SystemApi |
| 3143 | @Deprecated |
| 3144 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
| 3145 | public void stopTethering(int type) { |
Remi NGUYEN VAN | e4d1cd9 | 2021-06-17 16:27:31 +0900 | [diff] [blame] | 3146 | getTetheringManager().stopTethering(type); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3147 | } |
| 3148 | |
| 3149 | /** |
| 3150 | * Callback for use with {@link registerTetheringEventCallback} to find out tethering |
| 3151 | * upstream status. |
| 3152 | * |
| 3153 | * @deprecated Use {@link TetheringManager#OnTetheringEventCallback} instead. |
| 3154 | * @hide |
| 3155 | */ |
| 3156 | @SystemApi |
| 3157 | @Deprecated |
| 3158 | public abstract static class OnTetheringEventCallback { |
| 3159 | |
| 3160 | /** |
| 3161 | * Called when tethering upstream changed. This can be called multiple times and can be |
| 3162 | * called any time. |
| 3163 | * |
| 3164 | * @param network the {@link Network} of tethering upstream. Null means tethering doesn't |
| 3165 | * have any upstream. |
| 3166 | */ |
| 3167 | public void onUpstreamChanged(@Nullable Network network) {} |
| 3168 | } |
| 3169 | |
| 3170 | @GuardedBy("mTetheringEventCallbacks") |
| 3171 | private final ArrayMap<OnTetheringEventCallback, TetheringEventCallback> |
| 3172 | mTetheringEventCallbacks = new ArrayMap<>(); |
| 3173 | |
| 3174 | /** |
| 3175 | * Start listening to tethering change events. Any new added callback will receive the last |
| 3176 | * tethering status right away. If callback is registered when tethering has no upstream or |
| 3177 | * disabled, {@link OnTetheringEventCallback#onUpstreamChanged} will immediately be called |
| 3178 | * with a null argument. The same callback object cannot be registered twice. |
| 3179 | * |
| 3180 | * @param executor the executor on which callback will be invoked. |
| 3181 | * @param callback the callback to be called when tethering has change events. |
| 3182 | * |
| 3183 | * @deprecated Use {@link TetheringManager#registerTetheringEventCallback} instead. |
| 3184 | * @hide |
| 3185 | */ |
| 3186 | @SystemApi |
| 3187 | @Deprecated |
| 3188 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
| 3189 | public void registerTetheringEventCallback( |
| 3190 | @NonNull @CallbackExecutor Executor executor, |
| 3191 | @NonNull final OnTetheringEventCallback callback) { |
Remi NGUYEN VAN | 1818dbb | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 3192 | Objects.requireNonNull(callback, "OnTetheringEventCallback cannot be null."); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3193 | |
| 3194 | final TetheringEventCallback tetherCallback = |
| 3195 | new TetheringEventCallback() { |
| 3196 | @Override |
| 3197 | public void onUpstreamChanged(@Nullable Network network) { |
| 3198 | callback.onUpstreamChanged(network); |
| 3199 | } |
| 3200 | }; |
| 3201 | |
| 3202 | synchronized (mTetheringEventCallbacks) { |
| 3203 | mTetheringEventCallbacks.put(callback, tetherCallback); |
Remi NGUYEN VAN | e4d1cd9 | 2021-06-17 16:27:31 +0900 | [diff] [blame] | 3204 | getTetheringManager().registerTetheringEventCallback(executor, tetherCallback); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3205 | } |
| 3206 | } |
| 3207 | |
| 3208 | /** |
| 3209 | * Remove tethering event callback previously registered with |
| 3210 | * {@link #registerTetheringEventCallback}. |
| 3211 | * |
| 3212 | * @param callback previously registered callback. |
| 3213 | * |
| 3214 | * @deprecated Use {@link TetheringManager#unregisterTetheringEventCallback} instead. |
| 3215 | * @hide |
| 3216 | */ |
| 3217 | @SystemApi |
| 3218 | @Deprecated |
| 3219 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
| 3220 | public void unregisterTetheringEventCallback( |
| 3221 | @NonNull final OnTetheringEventCallback callback) { |
| 3222 | Objects.requireNonNull(callback, "The callback must be non-null"); |
| 3223 | synchronized (mTetheringEventCallbacks) { |
| 3224 | final TetheringEventCallback tetherCallback = |
| 3225 | mTetheringEventCallbacks.remove(callback); |
Remi NGUYEN VAN | e4d1cd9 | 2021-06-17 16:27:31 +0900 | [diff] [blame] | 3226 | getTetheringManager().unregisterTetheringEventCallback(tetherCallback); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3227 | } |
| 3228 | } |
| 3229 | |
| 3230 | |
| 3231 | /** |
| 3232 | * Get the list of regular expressions that define any tetherable |
| 3233 | * USB network interfaces. If USB tethering is not supported by the |
| 3234 | * device, this list should be empty. |
| 3235 | * |
| 3236 | * @return an array of 0 or more regular expression Strings defining |
| 3237 | * what interfaces are considered tetherable usb interfaces. |
| 3238 | * |
| 3239 | * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged} instead. |
| 3240 | * {@hide} |
| 3241 | */ |
| 3242 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 3243 | @UnsupportedAppUsage |
| 3244 | @Deprecated |
| 3245 | public String[] getTetherableUsbRegexs() { |
Remi NGUYEN VAN | e4d1cd9 | 2021-06-17 16:27:31 +0900 | [diff] [blame] | 3246 | return getTetheringManager().getTetherableUsbRegexs(); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3247 | } |
| 3248 | |
| 3249 | /** |
| 3250 | * Get the list of regular expressions that define any tetherable |
| 3251 | * Wifi network interfaces. If Wifi tethering is not supported by the |
| 3252 | * device, this list should be empty. |
| 3253 | * |
| 3254 | * @return an array of 0 or more regular expression Strings defining |
| 3255 | * what interfaces are considered tetherable wifi interfaces. |
| 3256 | * |
| 3257 | * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged} instead. |
| 3258 | * {@hide} |
| 3259 | */ |
| 3260 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 3261 | @UnsupportedAppUsage |
| 3262 | @Deprecated |
| 3263 | public String[] getTetherableWifiRegexs() { |
Remi NGUYEN VAN | e4d1cd9 | 2021-06-17 16:27:31 +0900 | [diff] [blame] | 3264 | return getTetheringManager().getTetherableWifiRegexs(); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3265 | } |
| 3266 | |
| 3267 | /** |
| 3268 | * Get the list of regular expressions that define any tetherable |
| 3269 | * Bluetooth network interfaces. If Bluetooth tethering is not supported by the |
| 3270 | * device, this list should be empty. |
| 3271 | * |
| 3272 | * @return an array of 0 or more regular expression Strings defining |
| 3273 | * what interfaces are considered tetherable bluetooth interfaces. |
| 3274 | * |
| 3275 | * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged( |
| 3276 | *TetheringManager.TetheringInterfaceRegexps)} instead. |
| 3277 | * {@hide} |
| 3278 | */ |
| 3279 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 3280 | @UnsupportedAppUsage |
| 3281 | @Deprecated |
| 3282 | public String[] getTetherableBluetoothRegexs() { |
Remi NGUYEN VAN | e4d1cd9 | 2021-06-17 16:27:31 +0900 | [diff] [blame] | 3283 | return getTetheringManager().getTetherableBluetoothRegexs(); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | /** |
| 3287 | * Attempt to both alter the mode of USB and Tethering of USB. A |
| 3288 | * utility method to deal with some of the complexity of USB - will |
| 3289 | * attempt to switch to Rndis and subsequently tether the resulting |
| 3290 | * interface on {@code true} or turn off tethering and switch off |
| 3291 | * Rndis on {@code false}. |
| 3292 | * |
| 3293 | * <p>This method requires the caller to hold either the |
| 3294 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 3295 | * or the ability to modify system settings as determined by |
| 3296 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 3297 | * |
| 3298 | * @param enable a boolean - {@code true} to enable tethering |
| 3299 | * @return error a {@code TETHER_ERROR} value indicating success or failure type |
| 3300 | * @deprecated Use {@link TetheringManager#startTethering} instead |
| 3301 | * |
| 3302 | * {@hide} |
| 3303 | */ |
| 3304 | @UnsupportedAppUsage |
| 3305 | @Deprecated |
| 3306 | public int setUsbTethering(boolean enable) { |
Remi NGUYEN VAN | e4d1cd9 | 2021-06-17 16:27:31 +0900 | [diff] [blame] | 3307 | return getTetheringManager().setUsbTethering(enable); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3308 | } |
| 3309 | |
| 3310 | /** |
| 3311 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_NO_ERROR}. |
| 3312 | * {@hide} |
| 3313 | */ |
| 3314 | @SystemApi |
| 3315 | @Deprecated |
Remi NGUYEN VAN | 71ced8e | 2021-02-15 18:52:06 +0900 | [diff] [blame] | 3316 | public static final int TETHER_ERROR_NO_ERROR = 0; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3317 | /** |
| 3318 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNKNOWN_IFACE}. |
| 3319 | * {@hide} |
| 3320 | */ |
| 3321 | @Deprecated |
| 3322 | public static final int TETHER_ERROR_UNKNOWN_IFACE = |
| 3323 | TetheringManager.TETHER_ERROR_UNKNOWN_IFACE; |
| 3324 | /** |
| 3325 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_SERVICE_UNAVAIL}. |
| 3326 | * {@hide} |
| 3327 | */ |
| 3328 | @Deprecated |
| 3329 | public static final int TETHER_ERROR_SERVICE_UNAVAIL = |
| 3330 | TetheringManager.TETHER_ERROR_SERVICE_UNAVAIL; |
| 3331 | /** |
| 3332 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNSUPPORTED}. |
| 3333 | * {@hide} |
| 3334 | */ |
| 3335 | @Deprecated |
| 3336 | public static final int TETHER_ERROR_UNSUPPORTED = TetheringManager.TETHER_ERROR_UNSUPPORTED; |
| 3337 | /** |
| 3338 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNAVAIL_IFACE}. |
| 3339 | * {@hide} |
| 3340 | */ |
| 3341 | @Deprecated |
| 3342 | public static final int TETHER_ERROR_UNAVAIL_IFACE = |
| 3343 | TetheringManager.TETHER_ERROR_UNAVAIL_IFACE; |
| 3344 | /** |
| 3345 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_INTERNAL_ERROR}. |
| 3346 | * {@hide} |
| 3347 | */ |
| 3348 | @Deprecated |
| 3349 | public static final int TETHER_ERROR_MASTER_ERROR = |
| 3350 | TetheringManager.TETHER_ERROR_INTERNAL_ERROR; |
| 3351 | /** |
| 3352 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_TETHER_IFACE_ERROR}. |
| 3353 | * {@hide} |
| 3354 | */ |
| 3355 | @Deprecated |
| 3356 | public static final int TETHER_ERROR_TETHER_IFACE_ERROR = |
| 3357 | TetheringManager.TETHER_ERROR_TETHER_IFACE_ERROR; |
| 3358 | /** |
| 3359 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNTETHER_IFACE_ERROR}. |
| 3360 | * {@hide} |
| 3361 | */ |
| 3362 | @Deprecated |
| 3363 | public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = |
| 3364 | TetheringManager.TETHER_ERROR_UNTETHER_IFACE_ERROR; |
| 3365 | /** |
| 3366 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_ENABLE_FORWARDING_ERROR}. |
| 3367 | * {@hide} |
| 3368 | */ |
| 3369 | @Deprecated |
| 3370 | public static final int TETHER_ERROR_ENABLE_NAT_ERROR = |
| 3371 | TetheringManager.TETHER_ERROR_ENABLE_FORWARDING_ERROR; |
| 3372 | /** |
| 3373 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_DISABLE_FORWARDING_ERROR}. |
| 3374 | * {@hide} |
| 3375 | */ |
| 3376 | @Deprecated |
| 3377 | public static final int TETHER_ERROR_DISABLE_NAT_ERROR = |
| 3378 | TetheringManager.TETHER_ERROR_DISABLE_FORWARDING_ERROR; |
| 3379 | /** |
| 3380 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_IFACE_CFG_ERROR}. |
| 3381 | * {@hide} |
| 3382 | */ |
| 3383 | @Deprecated |
| 3384 | public static final int TETHER_ERROR_IFACE_CFG_ERROR = |
| 3385 | TetheringManager.TETHER_ERROR_IFACE_CFG_ERROR; |
| 3386 | /** |
| 3387 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_PROVISIONING_FAILED}. |
| 3388 | * {@hide} |
| 3389 | */ |
| 3390 | @SystemApi |
| 3391 | @Deprecated |
Remi NGUYEN VAN | 71ced8e | 2021-02-15 18:52:06 +0900 | [diff] [blame] | 3392 | public static final int TETHER_ERROR_PROVISION_FAILED = 11; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3393 | /** |
| 3394 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_DHCPSERVER_ERROR}. |
| 3395 | * {@hide} |
| 3396 | */ |
| 3397 | @Deprecated |
| 3398 | public static final int TETHER_ERROR_DHCPSERVER_ERROR = |
| 3399 | TetheringManager.TETHER_ERROR_DHCPSERVER_ERROR; |
| 3400 | /** |
| 3401 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_ENTITLEMENT_UNKNOWN}. |
| 3402 | * {@hide} |
| 3403 | */ |
| 3404 | @SystemApi |
| 3405 | @Deprecated |
Remi NGUYEN VAN | 71ced8e | 2021-02-15 18:52:06 +0900 | [diff] [blame] | 3406 | public static final int TETHER_ERROR_ENTITLEMENT_UNKONWN = 13; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3407 | |
| 3408 | /** |
| 3409 | * Get a more detailed error code after a Tethering or Untethering |
| 3410 | * request asynchronously failed. |
| 3411 | * |
| 3412 | * @param iface The name of the interface of interest |
| 3413 | * @return error The error code of the last error tethering or untethering the named |
| 3414 | * interface |
| 3415 | * |
| 3416 | * @deprecated Use {@link TetheringEventCallback#onError(String, int)} instead. |
| 3417 | * {@hide} |
| 3418 | */ |
| 3419 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 3420 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 3421 | @Deprecated |
| 3422 | public int getLastTetherError(String iface) { |
Remi NGUYEN VAN | e4d1cd9 | 2021-06-17 16:27:31 +0900 | [diff] [blame] | 3423 | int error = getTetheringManager().getLastTetherError(iface); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3424 | if (error == TetheringManager.TETHER_ERROR_UNKNOWN_TYPE) { |
| 3425 | // TETHER_ERROR_UNKNOWN_TYPE was introduced with TetheringManager and has never been |
| 3426 | // returned by ConnectivityManager. Convert it to the legacy TETHER_ERROR_UNKNOWN_IFACE |
| 3427 | // instead. |
| 3428 | error = TetheringManager.TETHER_ERROR_UNKNOWN_IFACE; |
| 3429 | } |
| 3430 | return error; |
| 3431 | } |
| 3432 | |
| 3433 | /** @hide */ |
| 3434 | @Retention(RetentionPolicy.SOURCE) |
| 3435 | @IntDef(value = { |
| 3436 | TETHER_ERROR_NO_ERROR, |
| 3437 | TETHER_ERROR_PROVISION_FAILED, |
| 3438 | TETHER_ERROR_ENTITLEMENT_UNKONWN, |
| 3439 | }) |
| 3440 | public @interface EntitlementResultCode { |
| 3441 | } |
| 3442 | |
| 3443 | /** |
| 3444 | * Callback for use with {@link #getLatestTetheringEntitlementResult} to find out whether |
| 3445 | * entitlement succeeded. |
| 3446 | * |
| 3447 | * @deprecated Use {@link TetheringManager#OnTetheringEntitlementResultListener} instead. |
| 3448 | * @hide |
| 3449 | */ |
| 3450 | @SystemApi |
| 3451 | @Deprecated |
| 3452 | public interface OnTetheringEntitlementResultListener { |
| 3453 | /** |
| 3454 | * Called to notify entitlement result. |
| 3455 | * |
| 3456 | * @param resultCode an int value of entitlement result. It may be one of |
| 3457 | * {@link #TETHER_ERROR_NO_ERROR}, |
| 3458 | * {@link #TETHER_ERROR_PROVISION_FAILED}, or |
| 3459 | * {@link #TETHER_ERROR_ENTITLEMENT_UNKONWN}. |
| 3460 | */ |
| 3461 | void onTetheringEntitlementResult(@EntitlementResultCode int resultCode); |
| 3462 | } |
| 3463 | |
| 3464 | /** |
| 3465 | * Get the last value of the entitlement check on this downstream. If the cached value is |
Chalard Jean | 0c7ebe9 | 2022-08-03 14:45:47 +0900 | [diff] [blame] | 3466 | * {@link #TETHER_ERROR_NO_ERROR} or showEntitlementUi argument is false, this just returns the |
| 3467 | * cached value. Otherwise, a UI-based entitlement check will be performed. It is not |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3468 | * guaranteed that the UI-based entitlement check will complete in any specific time period |
Chalard Jean | 0c7ebe9 | 2022-08-03 14:45:47 +0900 | [diff] [blame] | 3469 | * and it may in fact never complete. Any successful entitlement check the platform performs for |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3470 | * any reason will update the cached value. |
| 3471 | * |
| 3472 | * @param type the downstream type of tethering. Must be one of |
| 3473 | * {@link #TETHERING_WIFI}, |
| 3474 | * {@link #TETHERING_USB}, or |
| 3475 | * {@link #TETHERING_BLUETOOTH}. |
| 3476 | * @param showEntitlementUi a boolean indicating whether to run UI-based entitlement check. |
| 3477 | * @param executor the executor on which callback will be invoked. |
| 3478 | * @param listener an {@link OnTetheringEntitlementResultListener} which will be called to |
| 3479 | * notify the caller of the result of entitlement check. The listener may be called zero |
| 3480 | * or one time. |
| 3481 | * @deprecated Use {@link TetheringManager#requestLatestTetheringEntitlementResult} instead. |
| 3482 | * {@hide} |
| 3483 | */ |
| 3484 | @SystemApi |
| 3485 | @Deprecated |
| 3486 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
| 3487 | public void getLatestTetheringEntitlementResult(int type, boolean showEntitlementUi, |
| 3488 | @NonNull @CallbackExecutor Executor executor, |
| 3489 | @NonNull final OnTetheringEntitlementResultListener listener) { |
Remi NGUYEN VAN | 1818dbb | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 3490 | Objects.requireNonNull(listener, "TetheringEntitlementResultListener cannot be null."); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3491 | ResultReceiver wrappedListener = new ResultReceiver(null) { |
| 3492 | @Override |
| 3493 | protected void onReceiveResult(int resultCode, Bundle resultData) { |
lucaslin | eaff72d | 2021-03-04 09:38:21 +0800 | [diff] [blame] | 3494 | final long token = Binder.clearCallingIdentity(); |
| 3495 | try { |
| 3496 | executor.execute(() -> { |
| 3497 | listener.onTetheringEntitlementResult(resultCode); |
| 3498 | }); |
| 3499 | } finally { |
| 3500 | Binder.restoreCallingIdentity(token); |
| 3501 | } |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3502 | } |
| 3503 | }; |
| 3504 | |
Remi NGUYEN VAN | e4d1cd9 | 2021-06-17 16:27:31 +0900 | [diff] [blame] | 3505 | getTetheringManager().requestLatestTetheringEntitlementResult(type, wrappedListener, |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3506 | showEntitlementUi); |
| 3507 | } |
| 3508 | |
| 3509 | /** |
| 3510 | * Report network connectivity status. This is currently used only |
| 3511 | * to alter status bar UI. |
| 3512 | * <p>This method requires the caller to hold the permission |
| 3513 | * {@link android.Manifest.permission#STATUS_BAR}. |
| 3514 | * |
| 3515 | * @param networkType The type of network you want to report on |
| 3516 | * @param percentage The quality of the connection 0 is bad, 100 is good |
| 3517 | * @deprecated Types are deprecated. Use {@link #reportNetworkConnectivity} instead. |
| 3518 | * {@hide} |
| 3519 | */ |
| 3520 | public void reportInetCondition(int networkType, int percentage) { |
| 3521 | printStackTrace(); |
| 3522 | try { |
| 3523 | mService.reportInetCondition(networkType, percentage); |
| 3524 | } catch (RemoteException e) { |
| 3525 | throw e.rethrowFromSystemServer(); |
| 3526 | } |
| 3527 | } |
| 3528 | |
| 3529 | /** |
| 3530 | * Report a problem network to the framework. This provides a hint to the system |
| 3531 | * that there might be connectivity problems on this network and may cause |
| 3532 | * the framework to re-evaluate network connectivity and/or switch to another |
| 3533 | * network. |
| 3534 | * |
| 3535 | * @param network The {@link Network} the application was attempting to use |
| 3536 | * or {@code null} to indicate the current default network. |
| 3537 | * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both |
| 3538 | * working and non-working connectivity. |
| 3539 | */ |
| 3540 | @Deprecated |
| 3541 | public void reportBadNetwork(@Nullable Network network) { |
| 3542 | printStackTrace(); |
| 3543 | try { |
| 3544 | // One of these will be ignored because it matches system's current state. |
| 3545 | // The other will trigger the necessary reevaluation. |
| 3546 | mService.reportNetworkConnectivity(network, true); |
| 3547 | mService.reportNetworkConnectivity(network, false); |
| 3548 | } catch (RemoteException e) { |
| 3549 | throw e.rethrowFromSystemServer(); |
| 3550 | } |
| 3551 | } |
| 3552 | |
| 3553 | /** |
| 3554 | * Report to the framework whether a network has working connectivity. |
| 3555 | * This provides a hint to the system that a particular network is providing |
| 3556 | * working connectivity or not. In response the framework may re-evaluate |
| 3557 | * the network's connectivity and might take further action thereafter. |
| 3558 | * |
| 3559 | * @param network The {@link Network} the application was attempting to use |
| 3560 | * or {@code null} to indicate the current default network. |
| 3561 | * @param hasConnectivity {@code true} if the application was able to successfully access the |
| 3562 | * Internet using {@code network} or {@code false} if not. |
| 3563 | */ |
| 3564 | public void reportNetworkConnectivity(@Nullable Network network, boolean hasConnectivity) { |
| 3565 | printStackTrace(); |
| 3566 | try { |
| 3567 | mService.reportNetworkConnectivity(network, hasConnectivity); |
| 3568 | } catch (RemoteException e) { |
| 3569 | throw e.rethrowFromSystemServer(); |
| 3570 | } |
| 3571 | } |
| 3572 | |
| 3573 | /** |
Chalard Jean | e1ce6ae | 2021-03-17 17:03:34 +0900 | [diff] [blame] | 3574 | * Set a network-independent global HTTP proxy. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3575 | * |
Chalard Jean | e1ce6ae | 2021-03-17 17:03:34 +0900 | [diff] [blame] | 3576 | * This sets an HTTP proxy that applies to all networks and overrides any network-specific |
| 3577 | * proxy. If set, HTTP libraries that are proxy-aware will use this global proxy when |
| 3578 | * accessing any network, regardless of what the settings for that network are. |
| 3579 | * |
| 3580 | * Note that HTTP proxies are by nature typically network-dependent, and setting a global |
| 3581 | * proxy is likely to break networking on multiple networks. This method is only meant |
| 3582 | * for device policy clients looking to do general internal filtering or similar use cases. |
| 3583 | * |
chiachangwang | 9473c59 | 2022-07-15 02:25:52 +0000 | [diff] [blame] | 3584 | * @see #getGlobalProxy |
| 3585 | * @see LinkProperties#getHttpProxy |
Chalard Jean | e1ce6ae | 2021-03-17 17:03:34 +0900 | [diff] [blame] | 3586 | * |
| 3587 | * @param p A {@link ProxyInfo} object defining the new global HTTP proxy. Calling this |
| 3588 | * method with a {@code null} value will clear the global HTTP proxy. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3589 | * @hide |
| 3590 | */ |
Chalard Jean | e1ce6ae | 2021-03-17 17:03:34 +0900 | [diff] [blame] | 3591 | // Used by Device Policy Manager to set the global proxy. |
Chiachang Wang | f9294e7 | 2021-03-18 09:44:34 +0800 | [diff] [blame] | 3592 | @SystemApi(client = MODULE_LIBRARIES) |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3593 | @RequiresPermission(android.Manifest.permission.NETWORK_STACK) |
Chalard Jean | e1ce6ae | 2021-03-17 17:03:34 +0900 | [diff] [blame] | 3594 | public void setGlobalProxy(@Nullable final ProxyInfo p) { |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3595 | try { |
| 3596 | mService.setGlobalProxy(p); |
| 3597 | } catch (RemoteException e) { |
| 3598 | throw e.rethrowFromSystemServer(); |
| 3599 | } |
| 3600 | } |
| 3601 | |
| 3602 | /** |
| 3603 | * Retrieve any network-independent global HTTP proxy. |
| 3604 | * |
| 3605 | * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null} |
| 3606 | * if no global HTTP proxy is set. |
| 3607 | * @hide |
| 3608 | */ |
Chiachang Wang | f9294e7 | 2021-03-18 09:44:34 +0800 | [diff] [blame] | 3609 | @SystemApi(client = MODULE_LIBRARIES) |
| 3610 | @Nullable |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3611 | public ProxyInfo getGlobalProxy() { |
| 3612 | try { |
| 3613 | return mService.getGlobalProxy(); |
| 3614 | } catch (RemoteException e) { |
| 3615 | throw e.rethrowFromSystemServer(); |
| 3616 | } |
| 3617 | } |
| 3618 | |
| 3619 | /** |
| 3620 | * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a |
| 3621 | * network-specific HTTP proxy. If {@code network} is null, the |
| 3622 | * network-specific proxy returned is the proxy of the default active |
| 3623 | * network. |
| 3624 | * |
| 3625 | * @return {@link ProxyInfo} for the current global HTTP proxy, or if no |
| 3626 | * global HTTP proxy is set, {@code ProxyInfo} for {@code network}, |
| 3627 | * or when {@code network} is {@code null}, |
| 3628 | * the {@code ProxyInfo} for the default active network. Returns |
| 3629 | * {@code null} when no proxy applies or the caller doesn't have |
| 3630 | * permission to use {@code network}. |
| 3631 | * @hide |
| 3632 | */ |
| 3633 | public ProxyInfo getProxyForNetwork(Network network) { |
| 3634 | try { |
| 3635 | return mService.getProxyForNetwork(network); |
| 3636 | } catch (RemoteException e) { |
| 3637 | throw e.rethrowFromSystemServer(); |
| 3638 | } |
| 3639 | } |
| 3640 | |
| 3641 | /** |
| 3642 | * Get the current default HTTP proxy settings. If a global proxy is set it will be returned, |
| 3643 | * otherwise if this process is bound to a {@link Network} using |
| 3644 | * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise |
| 3645 | * the default network's proxy is returned. |
| 3646 | * |
| 3647 | * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no |
| 3648 | * HTTP proxy is active. |
| 3649 | */ |
| 3650 | @Nullable |
| 3651 | public ProxyInfo getDefaultProxy() { |
| 3652 | return getProxyForNetwork(getBoundNetworkForProcess()); |
| 3653 | } |
| 3654 | |
| 3655 | /** |
Chalard Jean | 0c7ebe9 | 2022-08-03 14:45:47 +0900 | [diff] [blame] | 3656 | * Returns whether the hardware supports the given network type. |
| 3657 | * |
| 3658 | * This doesn't indicate there is coverage or such a network is available, just whether the |
| 3659 | * hardware supports it. For example a GSM phone without a SIM card will return {@code true} |
| 3660 | * for mobile data, but a WiFi only tablet would return {@code false}. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3661 | * |
| 3662 | * @param networkType The network type we'd like to check |
| 3663 | * @return {@code true} if supported, else {@code false} |
| 3664 | * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead. |
| 3665 | * @hide |
| 3666 | */ |
| 3667 | @Deprecated |
| 3668 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 3669 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
| 3670 | public boolean isNetworkSupported(int networkType) { |
| 3671 | try { |
| 3672 | return mService.isNetworkSupported(networkType); |
| 3673 | } catch (RemoteException e) { |
| 3674 | throw e.rethrowFromSystemServer(); |
| 3675 | } |
| 3676 | } |
| 3677 | |
| 3678 | /** |
| 3679 | * Returns if the currently active data network is metered. A network is |
| 3680 | * classified as metered when the user is sensitive to heavy data usage on |
| 3681 | * that connection due to monetary costs, data limitations or |
| 3682 | * battery/performance issues. You should check this before doing large |
| 3683 | * data transfers, and warn the user or delay the operation until another |
| 3684 | * network is available. |
| 3685 | * |
| 3686 | * @return {@code true} if large transfers should be avoided, otherwise |
| 3687 | * {@code false}. |
| 3688 | */ |
| 3689 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 3690 | public boolean isActiveNetworkMetered() { |
| 3691 | try { |
| 3692 | return mService.isActiveNetworkMetered(); |
| 3693 | } catch (RemoteException e) { |
| 3694 | throw e.rethrowFromSystemServer(); |
| 3695 | } |
| 3696 | } |
| 3697 | |
| 3698 | /** |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3699 | * Set sign in error notification to visible or invisible |
| 3700 | * |
| 3701 | * @hide |
| 3702 | * @deprecated Doesn't properly deal with multiple connected networks of the same type. |
| 3703 | */ |
| 3704 | @Deprecated |
| 3705 | public void setProvisioningNotificationVisible(boolean visible, int networkType, |
| 3706 | String action) { |
| 3707 | try { |
| 3708 | mService.setProvisioningNotificationVisible(visible, networkType, action); |
| 3709 | } catch (RemoteException e) { |
| 3710 | throw e.rethrowFromSystemServer(); |
| 3711 | } |
| 3712 | } |
| 3713 | |
| 3714 | /** |
| 3715 | * Set the value for enabling/disabling airplane mode |
| 3716 | * |
| 3717 | * @param enable whether to enable airplane mode or not |
| 3718 | * |
| 3719 | * @hide |
| 3720 | */ |
| 3721 | @RequiresPermission(anyOf = { |
| 3722 | android.Manifest.permission.NETWORK_AIRPLANE_MODE, |
| 3723 | android.Manifest.permission.NETWORK_SETTINGS, |
| 3724 | android.Manifest.permission.NETWORK_SETUP_WIZARD, |
| 3725 | android.Manifest.permission.NETWORK_STACK}) |
| 3726 | @SystemApi |
| 3727 | public void setAirplaneMode(boolean enable) { |
| 3728 | try { |
| 3729 | mService.setAirplaneMode(enable); |
| 3730 | } catch (RemoteException e) { |
| 3731 | throw e.rethrowFromSystemServer(); |
| 3732 | } |
| 3733 | } |
| 3734 | |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3735 | /** |
| 3736 | * Registers the specified {@link NetworkProvider}. |
| 3737 | * Each listener must only be registered once. The listener can be unregistered with |
| 3738 | * {@link #unregisterNetworkProvider}. |
| 3739 | * |
| 3740 | * @param provider the provider to register |
| 3741 | * @return the ID of the provider. This ID must be used by the provider when registering |
| 3742 | * {@link android.net.NetworkAgent}s. |
| 3743 | * @hide |
| 3744 | */ |
| 3745 | @SystemApi |
| 3746 | @RequiresPermission(anyOf = { |
| 3747 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 3748 | android.Manifest.permission.NETWORK_FACTORY}) |
| 3749 | public int registerNetworkProvider(@NonNull NetworkProvider provider) { |
| 3750 | if (provider.getProviderId() != NetworkProvider.ID_NONE) { |
| 3751 | throw new IllegalStateException("NetworkProviders can only be registered once"); |
| 3752 | } |
| 3753 | |
| 3754 | try { |
| 3755 | int providerId = mService.registerNetworkProvider(provider.getMessenger(), |
| 3756 | provider.getName()); |
| 3757 | provider.setProviderId(providerId); |
| 3758 | } catch (RemoteException e) { |
| 3759 | throw e.rethrowFromSystemServer(); |
| 3760 | } |
| 3761 | return provider.getProviderId(); |
| 3762 | } |
| 3763 | |
| 3764 | /** |
| 3765 | * Unregisters the specified NetworkProvider. |
| 3766 | * |
| 3767 | * @param provider the provider to unregister |
| 3768 | * @hide |
| 3769 | */ |
| 3770 | @SystemApi |
| 3771 | @RequiresPermission(anyOf = { |
| 3772 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 3773 | android.Manifest.permission.NETWORK_FACTORY}) |
| 3774 | public void unregisterNetworkProvider(@NonNull NetworkProvider provider) { |
| 3775 | try { |
| 3776 | mService.unregisterNetworkProvider(provider.getMessenger()); |
| 3777 | } catch (RemoteException e) { |
| 3778 | throw e.rethrowFromSystemServer(); |
| 3779 | } |
| 3780 | provider.setProviderId(NetworkProvider.ID_NONE); |
| 3781 | } |
| 3782 | |
Chalard Jean | d1b498b | 2021-01-05 08:40:09 +0900 | [diff] [blame] | 3783 | /** |
| 3784 | * Register or update a network offer with ConnectivityService. |
| 3785 | * |
| 3786 | * ConnectivityService keeps track of offers made by the various providers and matches |
Chalard Jean | 61e231f | 2021-03-24 17:43:10 +0900 | [diff] [blame] | 3787 | * them to networking requests made by apps or the system. A callback identifies an offer |
| 3788 | * uniquely, and later calls with the same callback update the offer. The provider supplies a |
| 3789 | * score and the capabilities of the network it might be able to bring up ; these act as |
| 3790 | * filters used by ConnectivityService to only send those requests that can be fulfilled by the |
Chalard Jean | d1b498b | 2021-01-05 08:40:09 +0900 | [diff] [blame] | 3791 | * provider. |
| 3792 | * |
| 3793 | * The provider is under no obligation to be able to bring up the network it offers at any |
| 3794 | * given time. Instead, this mechanism is meant to limit requests received by providers |
| 3795 | * to those they actually have a chance to fulfill, as providers don't have a way to compare |
| 3796 | * the quality of the network satisfying a given request to their own offer. |
| 3797 | * |
| 3798 | * An offer can be updated by calling this again with the same callback object. This is |
| 3799 | * similar to calling unofferNetwork and offerNetwork again, but will only update the |
| 3800 | * provider with the changes caused by the changes in the offer. |
| 3801 | * |
| 3802 | * @param provider The provider making this offer. |
| 3803 | * @param score The prospective score of the network. |
| 3804 | * @param caps The prospective capabilities of the network. |
| 3805 | * @param callback The callback to call when this offer is needed or unneeded. |
Chalard Jean | 428b913 | 2021-01-12 10:58:56 +0900 | [diff] [blame] | 3806 | * @hide exposed via the NetworkProvider class. |
Chalard Jean | d1b498b | 2021-01-05 08:40:09 +0900 | [diff] [blame] | 3807 | */ |
| 3808 | @RequiresPermission(anyOf = { |
| 3809 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 3810 | android.Manifest.permission.NETWORK_FACTORY}) |
Chalard Jean | 148dcce | 2021-03-22 22:44:02 +0900 | [diff] [blame] | 3811 | public void offerNetwork(@NonNull final int providerId, |
Chalard Jean | d1b498b | 2021-01-05 08:40:09 +0900 | [diff] [blame] | 3812 | @NonNull final NetworkScore score, @NonNull final NetworkCapabilities caps, |
| 3813 | @NonNull final INetworkOfferCallback callback) { |
| 3814 | try { |
Chalard Jean | 148dcce | 2021-03-22 22:44:02 +0900 | [diff] [blame] | 3815 | mService.offerNetwork(providerId, |
Chalard Jean | d1b498b | 2021-01-05 08:40:09 +0900 | [diff] [blame] | 3816 | Objects.requireNonNull(score, "null score"), |
| 3817 | Objects.requireNonNull(caps, "null caps"), |
| 3818 | Objects.requireNonNull(callback, "null callback")); |
| 3819 | } catch (RemoteException e) { |
| 3820 | throw e.rethrowFromSystemServer(); |
| 3821 | } |
| 3822 | } |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3823 | |
Chalard Jean | d1b498b | 2021-01-05 08:40:09 +0900 | [diff] [blame] | 3824 | /** |
| 3825 | * Withdraw a network offer made with {@link #offerNetwork}. |
| 3826 | * |
| 3827 | * @param callback The callback passed at registration time. This must be the same object |
| 3828 | * that was passed to {@link #offerNetwork} |
Chalard Jean | 428b913 | 2021-01-12 10:58:56 +0900 | [diff] [blame] | 3829 | * @hide exposed via the NetworkProvider class. |
Chalard Jean | d1b498b | 2021-01-05 08:40:09 +0900 | [diff] [blame] | 3830 | */ |
| 3831 | public void unofferNetwork(@NonNull final INetworkOfferCallback callback) { |
| 3832 | try { |
| 3833 | mService.unofferNetwork(Objects.requireNonNull(callback)); |
| 3834 | } catch (RemoteException e) { |
| 3835 | throw e.rethrowFromSystemServer(); |
| 3836 | } |
| 3837 | } |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3838 | /** @hide exposed via the NetworkProvider class. */ |
| 3839 | @RequiresPermission(anyOf = { |
| 3840 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 3841 | android.Manifest.permission.NETWORK_FACTORY}) |
| 3842 | public void declareNetworkRequestUnfulfillable(@NonNull NetworkRequest request) { |
| 3843 | try { |
| 3844 | mService.declareNetworkRequestUnfulfillable(request); |
| 3845 | } catch (RemoteException e) { |
| 3846 | throw e.rethrowFromSystemServer(); |
| 3847 | } |
| 3848 | } |
| 3849 | |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3850 | /** |
| 3851 | * @hide |
| 3852 | * Register a NetworkAgent with ConnectivityService. |
| 3853 | * @return Network corresponding to NetworkAgent. |
| 3854 | */ |
| 3855 | @RequiresPermission(anyOf = { |
| 3856 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 3857 | android.Manifest.permission.NETWORK_FACTORY}) |
Chalard Jean | f9d0e3e | 2023-10-17 13:23:17 +0900 | [diff] [blame] | 3858 | public Network registerNetworkAgent(@NonNull INetworkAgent na, @NonNull NetworkInfo ni, |
| 3859 | @NonNull LinkProperties lp, @NonNull NetworkCapabilities nc, |
| 3860 | @NonNull NetworkScore score, @NonNull NetworkAgentConfig config, int providerId) { |
| 3861 | return registerNetworkAgent(na, ni, lp, nc, null /* localNetworkConfig */, score, config, |
| 3862 | providerId); |
| 3863 | } |
| 3864 | |
| 3865 | /** |
| 3866 | * @hide |
| 3867 | * Register a NetworkAgent with ConnectivityService. |
| 3868 | * @return Network corresponding to NetworkAgent. |
| 3869 | */ |
| 3870 | @RequiresPermission(anyOf = { |
| 3871 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 3872 | android.Manifest.permission.NETWORK_FACTORY}) |
| 3873 | public Network registerNetworkAgent(@NonNull INetworkAgent na, @NonNull NetworkInfo ni, |
| 3874 | @NonNull LinkProperties lp, @NonNull NetworkCapabilities nc, |
| 3875 | @Nullable LocalNetworkConfig localNetworkConfig, @NonNull NetworkScore score, |
| 3876 | @NonNull NetworkAgentConfig config, int providerId) { |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3877 | try { |
Chalard Jean | f9d0e3e | 2023-10-17 13:23:17 +0900 | [diff] [blame] | 3878 | return mService.registerNetworkAgent(na, ni, lp, nc, score, localNetworkConfig, config, |
| 3879 | providerId); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3880 | } catch (RemoteException e) { |
| 3881 | throw e.rethrowFromSystemServer(); |
| 3882 | } |
| 3883 | } |
| 3884 | |
| 3885 | /** |
| 3886 | * Base class for {@code NetworkRequest} callbacks. Used for notifications about network |
| 3887 | * changes. Should be extended by applications wanting notifications. |
| 3888 | * |
| 3889 | * A {@code NetworkCallback} is registered by calling |
| 3890 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, |
| 3891 | * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)}, |
| 3892 | * or {@link #registerDefaultNetworkCallback(NetworkCallback)}. A {@code NetworkCallback} is |
| 3893 | * unregistered by calling {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 3894 | * A {@code NetworkCallback} should be registered at most once at any time. |
| 3895 | * A {@code NetworkCallback} that has been unregistered can be registered again. |
| 3896 | */ |
| 3897 | public static class NetworkCallback { |
| 3898 | /** |
Roshan Pius | e08bc18 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 3899 | * No flags associated with this callback. |
| 3900 | * @hide |
| 3901 | */ |
| 3902 | public static final int FLAG_NONE = 0; |
lucaslin | c582d50 | 2022-01-27 09:07:00 +0800 | [diff] [blame] | 3903 | |
Roshan Pius | e08bc18 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 3904 | /** |
lucaslin | c582d50 | 2022-01-27 09:07:00 +0800 | [diff] [blame] | 3905 | * Inclusion of this flag means location-sensitive redaction requests keeping location info. |
| 3906 | * |
| 3907 | * Some objects like {@link NetworkCapabilities} may contain location-sensitive information. |
| 3908 | * Prior to Android 12, this information is always returned to apps holding the appropriate |
| 3909 | * permission, possibly noting that the app has used location. |
| 3910 | * <p>In Android 12 and above, by default the sent objects do not contain any location |
| 3911 | * information, even if the app holds the necessary permissions, and the system does not |
| 3912 | * take note of location usage by the app. Apps can request that location information is |
| 3913 | * included, in which case the system will check location permission and the location |
| 3914 | * toggle state, and take note of location usage by the app if any such information is |
| 3915 | * returned. |
| 3916 | * |
Roshan Pius | e08bc18 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 3917 | * Use this flag to include any location sensitive data in {@link NetworkCapabilities} sent |
| 3918 | * via {@link #onCapabilitiesChanged(Network, NetworkCapabilities)}. |
| 3919 | * <p> |
| 3920 | * These include: |
| 3921 | * <li> Some transport info instances (retrieved via |
| 3922 | * {@link NetworkCapabilities#getTransportInfo()}) like {@link android.net.wifi.WifiInfo} |
| 3923 | * contain location sensitive information. |
| 3924 | * <li> OwnerUid (retrieved via {@link NetworkCapabilities#getOwnerUid()} is location |
Anton Hansson | df40109 | 2021-10-20 11:27:13 +0100 | [diff] [blame] | 3925 | * sensitive for wifi suggestor apps (i.e using |
| 3926 | * {@link android.net.wifi.WifiNetworkSuggestion WifiNetworkSuggestion}).</li> |
Roshan Pius | e08bc18 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 3927 | * </p> |
| 3928 | * <p> |
| 3929 | * Note: |
| 3930 | * <li> Retrieving this location sensitive information (subject to app's location |
| 3931 | * permissions) will be noted by system. </li> |
| 3932 | * <li> Without this flag any {@link NetworkCapabilities} provided via the callback does |
lucaslin | c582d50 | 2022-01-27 09:07:00 +0800 | [diff] [blame] | 3933 | * not include location sensitive information. |
Roshan Pius | e08bc18 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 3934 | */ |
Roshan Pius | 189d009 | 2021-03-11 21:16:44 -0800 | [diff] [blame] | 3935 | // Note: Some existing fields which are location sensitive may still be included without |
| 3936 | // this flag if the app targets SDK < S (to maintain backwards compatibility). |
Roshan Pius | e08bc18 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 3937 | public static final int FLAG_INCLUDE_LOCATION_INFO = 1 << 0; |
| 3938 | |
| 3939 | /** @hide */ |
| 3940 | @Retention(RetentionPolicy.SOURCE) |
| 3941 | @IntDef(flag = true, prefix = "FLAG_", value = { |
| 3942 | FLAG_NONE, |
| 3943 | FLAG_INCLUDE_LOCATION_INFO |
| 3944 | }) |
| 3945 | public @interface Flag { } |
| 3946 | |
| 3947 | /** |
| 3948 | * All the valid flags for error checking. |
| 3949 | */ |
| 3950 | private static final int VALID_FLAGS = FLAG_INCLUDE_LOCATION_INFO; |
| 3951 | |
| 3952 | public NetworkCallback() { |
| 3953 | this(FLAG_NONE); |
| 3954 | } |
| 3955 | |
| 3956 | public NetworkCallback(@Flag int flags) { |
Remi NGUYEN VAN | 1818dbb | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 3957 | if ((flags & VALID_FLAGS) != flags) { |
| 3958 | throw new IllegalArgumentException("Invalid flags"); |
| 3959 | } |
Roshan Pius | e08bc18 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 3960 | mFlags = flags; |
| 3961 | } |
| 3962 | |
| 3963 | /** |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3964 | * Called when the framework connects to a new network to evaluate whether it satisfies this |
| 3965 | * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable} |
| 3966 | * callback. There is no guarantee that this new network will satisfy any requests, or that |
| 3967 | * the network will stay connected for longer than the time necessary to evaluate it. |
| 3968 | * <p> |
| 3969 | * Most applications <b>should not</b> act on this callback, and should instead use |
| 3970 | * {@link #onAvailable}. This callback is intended for use by applications that can assist |
| 3971 | * the framework in properly evaluating the network — for example, an application that |
| 3972 | * can automatically log in to a captive portal without user intervention. |
| 3973 | * |
| 3974 | * @param network The {@link Network} of the network that is being evaluated. |
| 3975 | * |
| 3976 | * @hide |
| 3977 | */ |
| 3978 | public void onPreCheck(@NonNull Network network) {} |
| 3979 | |
| 3980 | /** |
| 3981 | * Called when the framework connects and has declared a new network ready for use. |
| 3982 | * This callback may be called more than once if the {@link Network} that is |
| 3983 | * satisfying the request changes. |
| 3984 | * |
| 3985 | * @param network The {@link Network} of the satisfying network. |
| 3986 | * @param networkCapabilities The {@link NetworkCapabilities} of the satisfying network. |
| 3987 | * @param linkProperties The {@link LinkProperties} of the satisfying network. |
Chalard Jean | 22350c9 | 2023-10-07 19:21:45 +0900 | [diff] [blame] | 3988 | * @param localInfo The {@link LocalNetworkInfo} of the satisfying network, or null |
| 3989 | * if this network is not a local network. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3990 | * @param blocked Whether access to the {@link Network} is blocked due to system policy. |
| 3991 | * @hide |
| 3992 | */ |
Lorenzo Colitti | 8ad5812 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 3993 | public final void onAvailable(@NonNull Network network, |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3994 | @NonNull NetworkCapabilities networkCapabilities, |
Chalard Jean | 22350c9 | 2023-10-07 19:21:45 +0900 | [diff] [blame] | 3995 | @NonNull LinkProperties linkProperties, |
| 3996 | @Nullable LocalNetworkInfo localInfo, |
| 3997 | @BlockedReason int blocked) { |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 3998 | // Internally only this method is called when a new network is available, and |
| 3999 | // it calls the callback in the same way and order that older versions used |
| 4000 | // to call so as not to change the behavior. |
Lorenzo Colitti | 8ad5812 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 4001 | onAvailable(network, networkCapabilities, linkProperties, blocked != 0); |
Chalard Jean | 22350c9 | 2023-10-07 19:21:45 +0900 | [diff] [blame] | 4002 | if (null != localInfo) onLocalNetworkInfoChanged(network, localInfo); |
Lorenzo Colitti | 8ad5812 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 4003 | onBlockedStatusChanged(network, blocked); |
| 4004 | } |
| 4005 | |
| 4006 | /** |
| 4007 | * Legacy variant of onAvailable that takes a boolean blocked reason. |
| 4008 | * |
| 4009 | * This method has never been public API, but it's not final, so there may be apps that |
| 4010 | * implemented it and rely on it being called. Do our best not to break them. |
| 4011 | * Note: such apps will also get a second call to onBlockedStatusChanged immediately after |
| 4012 | * this method is called. There does not seem to be a way to avoid this. |
| 4013 | * TODO: add a compat check to move apps off this method, and eventually stop calling it. |
| 4014 | * |
| 4015 | * @hide |
| 4016 | */ |
| 4017 | public void onAvailable(@NonNull Network network, |
| 4018 | @NonNull NetworkCapabilities networkCapabilities, |
Chalard Jean | 22350c9 | 2023-10-07 19:21:45 +0900 | [diff] [blame] | 4019 | @NonNull LinkProperties linkProperties, |
| 4020 | boolean blocked) { |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4021 | onAvailable(network); |
| 4022 | if (!networkCapabilities.hasCapability( |
| 4023 | NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)) { |
| 4024 | onNetworkSuspended(network); |
| 4025 | } |
| 4026 | onCapabilitiesChanged(network, networkCapabilities); |
| 4027 | onLinkPropertiesChanged(network, linkProperties); |
Lorenzo Colitti | 8ad5812 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 4028 | // No call to onBlockedStatusChanged here. That is done by the caller. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4029 | } |
| 4030 | |
| 4031 | /** |
| 4032 | * Called when the framework connects and has declared a new network ready for use. |
| 4033 | * |
| 4034 | * <p>For callbacks registered with {@link #registerNetworkCallback}, multiple networks may |
| 4035 | * be available at the same time, and onAvailable will be called for each of these as they |
| 4036 | * appear. |
| 4037 | * |
| 4038 | * <p>For callbacks registered with {@link #requestNetwork} and |
| 4039 | * {@link #registerDefaultNetworkCallback}, this means the network passed as an argument |
| 4040 | * is the new best network for this request and is now tracked by this callback ; this |
| 4041 | * callback will no longer receive method calls about other networks that may have been |
| 4042 | * passed to this method previously. The previously-best network may have disconnected, or |
| 4043 | * it may still be around and the newly-best network may simply be better. |
| 4044 | * |
| 4045 | * <p>Starting with {@link android.os.Build.VERSION_CODES#O}, this will always immediately |
| 4046 | * be followed by a call to {@link #onCapabilitiesChanged(Network, NetworkCapabilities)} |
| 4047 | * then by a call to {@link #onLinkPropertiesChanged(Network, LinkProperties)}, and a call |
| 4048 | * to {@link #onBlockedStatusChanged(Network, boolean)}. |
| 4049 | * |
| 4050 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 4051 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 4052 | * this callback as this is prone to race conditions (there is no guarantee the objects |
| 4053 | * returned by these methods will be current). Instead, wait for a call to |
| 4054 | * {@link #onCapabilitiesChanged(Network, NetworkCapabilities)} and |
| 4055 | * {@link #onLinkPropertiesChanged(Network, LinkProperties)} whose arguments are guaranteed |
| 4056 | * to be well-ordered with respect to other callbacks. |
| 4057 | * |
| 4058 | * @param network The {@link Network} of the satisfying network. |
| 4059 | */ |
| 4060 | public void onAvailable(@NonNull Network network) {} |
| 4061 | |
| 4062 | /** |
| 4063 | * Called when the network is about to be lost, typically because there are no outstanding |
| 4064 | * requests left for it. This may be paired with a {@link NetworkCallback#onAvailable} call |
| 4065 | * with the new replacement network for graceful handover. This method is not guaranteed |
| 4066 | * to be called before {@link NetworkCallback#onLost} is called, for example in case a |
| 4067 | * network is suddenly disconnected. |
| 4068 | * |
| 4069 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 4070 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 4071 | * this callback as this is prone to race conditions ; calling these methods while in a |
| 4072 | * callback may return an outdated or even a null object. |
| 4073 | * |
| 4074 | * @param network The {@link Network} that is about to be lost. |
| 4075 | * @param maxMsToLive The time in milliseconds the system intends to keep the network |
| 4076 | * connected for graceful handover; note that the network may still |
| 4077 | * suffer a hard loss at any time. |
| 4078 | */ |
| 4079 | public void onLosing(@NonNull Network network, int maxMsToLive) {} |
| 4080 | |
| 4081 | /** |
| 4082 | * Called when a network disconnects or otherwise no longer satisfies this request or |
| 4083 | * callback. |
| 4084 | * |
| 4085 | * <p>If the callback was registered with requestNetwork() or |
| 4086 | * registerDefaultNetworkCallback(), it will only be invoked against the last network |
| 4087 | * returned by onAvailable() when that network is lost and no other network satisfies |
| 4088 | * the criteria of the request. |
| 4089 | * |
| 4090 | * <p>If the callback was registered with registerNetworkCallback() it will be called for |
| 4091 | * each network which no longer satisfies the criteria of the callback. |
| 4092 | * |
| 4093 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 4094 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 4095 | * this callback as this is prone to race conditions ; calling these methods while in a |
| 4096 | * callback may return an outdated or even a null object. |
| 4097 | * |
| 4098 | * @param network The {@link Network} lost. |
| 4099 | */ |
| 4100 | public void onLost(@NonNull Network network) {} |
| 4101 | |
| 4102 | /** |
| 4103 | * Called if no network is found within the timeout time specified in |
| 4104 | * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} call or if the |
| 4105 | * requested network request cannot be fulfilled (whether or not a timeout was |
| 4106 | * specified). When this callback is invoked the associated |
| 4107 | * {@link NetworkRequest} will have already been removed and released, as if |
| 4108 | * {@link #unregisterNetworkCallback(NetworkCallback)} had been called. |
| 4109 | */ |
| 4110 | public void onUnavailable() {} |
| 4111 | |
| 4112 | /** |
| 4113 | * Called when the network corresponding to this request changes capabilities but still |
| 4114 | * satisfies the requested criteria. |
| 4115 | * |
| 4116 | * <p>Starting with {@link android.os.Build.VERSION_CODES#O} this method is guaranteed |
| 4117 | * to be called immediately after {@link #onAvailable}. |
| 4118 | * |
| 4119 | * <p>Do NOT call {@link #getLinkProperties(Network)} or other synchronous |
| 4120 | * ConnectivityManager methods in this callback as this is prone to race conditions : |
| 4121 | * calling these methods while in a callback may return an outdated or even a null object. |
| 4122 | * |
| 4123 | * @param network The {@link Network} whose capabilities have changed. |
Roshan Pius | e08bc18 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 4124 | * @param networkCapabilities The new {@link NetworkCapabilities} for this |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4125 | * network. |
| 4126 | */ |
| 4127 | public void onCapabilitiesChanged(@NonNull Network network, |
| 4128 | @NonNull NetworkCapabilities networkCapabilities) {} |
| 4129 | |
| 4130 | /** |
| 4131 | * Called when the network corresponding to this request changes {@link LinkProperties}. |
| 4132 | * |
| 4133 | * <p>Starting with {@link android.os.Build.VERSION_CODES#O} this method is guaranteed |
| 4134 | * to be called immediately after {@link #onAvailable}. |
| 4135 | * |
| 4136 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or other synchronous |
| 4137 | * ConnectivityManager methods in this callback as this is prone to race conditions : |
| 4138 | * calling these methods while in a callback may return an outdated or even a null object. |
| 4139 | * |
| 4140 | * @param network The {@link Network} whose link properties have changed. |
| 4141 | * @param linkProperties The new {@link LinkProperties} for this network. |
| 4142 | */ |
| 4143 | public void onLinkPropertiesChanged(@NonNull Network network, |
| 4144 | @NonNull LinkProperties linkProperties) {} |
| 4145 | |
| 4146 | /** |
Chalard Jean | 22350c9 | 2023-10-07 19:21:45 +0900 | [diff] [blame] | 4147 | * Called when there is a change in the {@link LocalNetworkInfo} for this network. |
| 4148 | * |
| 4149 | * This is only called for local networks, that is those with the |
| 4150 | * NET_CAPABILITY_LOCAL_NETWORK network capability. |
| 4151 | * |
| 4152 | * @param network the {@link Network} whose local network info has changed. |
| 4153 | * @param localNetworkInfo the new {@link LocalNetworkInfo} for this network. |
| 4154 | * @hide |
| 4155 | */ |
| 4156 | public void onLocalNetworkInfoChanged(@NonNull Network network, |
| 4157 | @NonNull LocalNetworkInfo localNetworkInfo) {} |
| 4158 | |
| 4159 | /** |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4160 | * Called when the network the framework connected to for this request suspends data |
| 4161 | * transmission temporarily. |
| 4162 | * |
| 4163 | * <p>This generally means that while the TCP connections are still live temporarily |
| 4164 | * network data fails to transfer. To give a specific example, this is used on cellular |
| 4165 | * networks to mask temporary outages when driving through a tunnel, etc. In general this |
| 4166 | * means read operations on sockets on this network will block once the buffers are |
| 4167 | * drained, and write operations will block once the buffers are full. |
| 4168 | * |
| 4169 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 4170 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 4171 | * this callback as this is prone to race conditions (there is no guarantee the objects |
| 4172 | * returned by these methods will be current). |
| 4173 | * |
| 4174 | * @hide |
| 4175 | */ |
| 4176 | public void onNetworkSuspended(@NonNull Network network) {} |
| 4177 | |
| 4178 | /** |
| 4179 | * Called when the network the framework connected to for this request |
| 4180 | * returns from a {@link NetworkInfo.State#SUSPENDED} state. This should always be |
| 4181 | * preceded by a matching {@link NetworkCallback#onNetworkSuspended} call. |
| 4182 | |
| 4183 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 4184 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 4185 | * this callback as this is prone to race conditions : calling these methods while in a |
| 4186 | * callback may return an outdated or even a null object. |
| 4187 | * |
| 4188 | * @hide |
| 4189 | */ |
| 4190 | public void onNetworkResumed(@NonNull Network network) {} |
| 4191 | |
| 4192 | /** |
| 4193 | * Called when access to the specified network is blocked or unblocked. |
| 4194 | * |
| 4195 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 4196 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 4197 | * this callback as this is prone to race conditions : calling these methods while in a |
| 4198 | * callback may return an outdated or even a null object. |
| 4199 | * |
| 4200 | * @param network The {@link Network} whose blocked status has changed. |
| 4201 | * @param blocked The blocked status of this {@link Network}. |
| 4202 | */ |
| 4203 | public void onBlockedStatusChanged(@NonNull Network network, boolean blocked) {} |
| 4204 | |
Lorenzo Colitti | 8ad5812 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 4205 | /** |
Lorenzo Colitti | a1bd6f6 | 2021-03-25 23:17:36 +0900 | [diff] [blame] | 4206 | * Called when access to the specified network is blocked or unblocked, or the reason for |
| 4207 | * access being blocked changes. |
Lorenzo Colitti | 8ad5812 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 4208 | * |
| 4209 | * If a NetworkCallback object implements this method, |
| 4210 | * {@link #onBlockedStatusChanged(Network, boolean)} will not be called. |
| 4211 | * |
| 4212 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 4213 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 4214 | * this callback as this is prone to race conditions : calling these methods while in a |
| 4215 | * callback may return an outdated or even a null object. |
| 4216 | * |
| 4217 | * @param network The {@link Network} whose blocked status has changed. |
| 4218 | * @param blocked The blocked status of this {@link Network}. |
| 4219 | * @hide |
| 4220 | */ |
| 4221 | @SystemApi(client = MODULE_LIBRARIES) |
| 4222 | public void onBlockedStatusChanged(@NonNull Network network, @BlockedReason int blocked) { |
| 4223 | onBlockedStatusChanged(network, blocked != 0); |
| 4224 | } |
| 4225 | |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4226 | private NetworkRequest networkRequest; |
Roshan Pius | e08bc18 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 4227 | private final int mFlags; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4228 | } |
| 4229 | |
| 4230 | /** |
| 4231 | * Constant error codes used by ConnectivityService to communicate about failures and errors |
| 4232 | * across a Binder boundary. |
| 4233 | * @hide |
| 4234 | */ |
| 4235 | public interface Errors { |
| 4236 | int TOO_MANY_REQUESTS = 1; |
| 4237 | } |
| 4238 | |
| 4239 | /** @hide */ |
| 4240 | public static class TooManyRequestsException extends RuntimeException {} |
| 4241 | |
| 4242 | private static RuntimeException convertServiceException(ServiceSpecificException e) { |
| 4243 | switch (e.errorCode) { |
| 4244 | case Errors.TOO_MANY_REQUESTS: |
| 4245 | return new TooManyRequestsException(); |
| 4246 | default: |
| 4247 | Log.w(TAG, "Unknown service error code " + e.errorCode); |
| 4248 | return new RuntimeException(e); |
| 4249 | } |
| 4250 | } |
| 4251 | |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4252 | /** @hide */ |
Chalard Jean | 22350c9 | 2023-10-07 19:21:45 +0900 | [diff] [blame] | 4253 | public static final int CALLBACK_PRECHECK = 1; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4254 | /** @hide */ |
Chalard Jean | 22350c9 | 2023-10-07 19:21:45 +0900 | [diff] [blame] | 4255 | public static final int CALLBACK_AVAILABLE = 2; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4256 | /** @hide arg1 = TTL */ |
Chalard Jean | 22350c9 | 2023-10-07 19:21:45 +0900 | [diff] [blame] | 4257 | public static final int CALLBACK_LOSING = 3; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4258 | /** @hide */ |
Chalard Jean | 22350c9 | 2023-10-07 19:21:45 +0900 | [diff] [blame] | 4259 | public static final int CALLBACK_LOST = 4; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4260 | /** @hide */ |
Chalard Jean | 22350c9 | 2023-10-07 19:21:45 +0900 | [diff] [blame] | 4261 | public static final int CALLBACK_UNAVAIL = 5; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4262 | /** @hide */ |
Chalard Jean | 22350c9 | 2023-10-07 19:21:45 +0900 | [diff] [blame] | 4263 | public static final int CALLBACK_CAP_CHANGED = 6; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4264 | /** @hide */ |
Chalard Jean | 22350c9 | 2023-10-07 19:21:45 +0900 | [diff] [blame] | 4265 | public static final int CALLBACK_IP_CHANGED = 7; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4266 | /** @hide obj = NetworkCapabilities, arg1 = seq number */ |
Chalard Jean | 22350c9 | 2023-10-07 19:21:45 +0900 | [diff] [blame] | 4267 | private static final int EXPIRE_LEGACY_REQUEST = 8; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4268 | /** @hide */ |
Chalard Jean | 22350c9 | 2023-10-07 19:21:45 +0900 | [diff] [blame] | 4269 | public static final int CALLBACK_SUSPENDED = 9; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4270 | /** @hide */ |
Chalard Jean | 22350c9 | 2023-10-07 19:21:45 +0900 | [diff] [blame] | 4271 | public static final int CALLBACK_RESUMED = 10; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4272 | /** @hide */ |
Chalard Jean | 22350c9 | 2023-10-07 19:21:45 +0900 | [diff] [blame] | 4273 | public static final int CALLBACK_BLK_CHANGED = 11; |
| 4274 | /** @hide */ |
| 4275 | public static final int CALLBACK_LOCAL_NETWORK_INFO_CHANGED = 12; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4276 | |
| 4277 | /** @hide */ |
| 4278 | public static String getCallbackName(int whichCallback) { |
| 4279 | switch (whichCallback) { |
| 4280 | case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK"; |
| 4281 | case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE"; |
| 4282 | case CALLBACK_LOSING: return "CALLBACK_LOSING"; |
| 4283 | case CALLBACK_LOST: return "CALLBACK_LOST"; |
| 4284 | case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL"; |
| 4285 | case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED"; |
| 4286 | case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED"; |
| 4287 | case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST"; |
| 4288 | case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED"; |
| 4289 | case CALLBACK_RESUMED: return "CALLBACK_RESUMED"; |
| 4290 | case CALLBACK_BLK_CHANGED: return "CALLBACK_BLK_CHANGED"; |
Chalard Jean | 22350c9 | 2023-10-07 19:21:45 +0900 | [diff] [blame] | 4291 | case CALLBACK_LOCAL_NETWORK_INFO_CHANGED: return "CALLBACK_LOCAL_NETWORK_INFO_CHANGED"; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4292 | default: |
| 4293 | return Integer.toString(whichCallback); |
| 4294 | } |
| 4295 | } |
| 4296 | |
zhujiatai | 79b0de9 | 2022-09-22 15:44:02 +0800 | [diff] [blame] | 4297 | private static class CallbackHandler extends Handler { |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4298 | private static final String TAG = "ConnectivityManager.CallbackHandler"; |
| 4299 | private static final boolean DBG = false; |
| 4300 | |
| 4301 | CallbackHandler(Looper looper) { |
| 4302 | super(looper); |
| 4303 | } |
| 4304 | |
| 4305 | CallbackHandler(Handler handler) { |
Remi NGUYEN VAN | 1818dbb | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 4306 | this(Objects.requireNonNull(handler, "Handler cannot be null.").getLooper()); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4307 | } |
| 4308 | |
| 4309 | @Override |
| 4310 | public void handleMessage(Message message) { |
| 4311 | if (message.what == EXPIRE_LEGACY_REQUEST) { |
zhujiatai | 79b0de9 | 2022-09-22 15:44:02 +0800 | [diff] [blame] | 4312 | // the sInstance can't be null because to send this message a ConnectivityManager |
| 4313 | // instance must have been created prior to creating the thread on which this |
| 4314 | // Handler is running. |
| 4315 | sInstance.expireRequest((NetworkCapabilities) message.obj, message.arg1); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4316 | return; |
| 4317 | } |
| 4318 | |
| 4319 | final NetworkRequest request = getObject(message, NetworkRequest.class); |
| 4320 | final Network network = getObject(message, Network.class); |
| 4321 | final NetworkCallback callback; |
| 4322 | synchronized (sCallbacks) { |
| 4323 | callback = sCallbacks.get(request); |
| 4324 | if (callback == null) { |
| 4325 | Log.w(TAG, |
| 4326 | "callback not found for " + getCallbackName(message.what) + " message"); |
| 4327 | return; |
| 4328 | } |
| 4329 | if (message.what == CALLBACK_UNAVAIL) { |
| 4330 | sCallbacks.remove(request); |
| 4331 | callback.networkRequest = ALREADY_UNREGISTERED; |
| 4332 | } |
| 4333 | } |
| 4334 | if (DBG) { |
| 4335 | Log.d(TAG, getCallbackName(message.what) + " for network " + network); |
| 4336 | } |
| 4337 | |
| 4338 | switch (message.what) { |
| 4339 | case CALLBACK_PRECHECK: { |
| 4340 | callback.onPreCheck(network); |
| 4341 | break; |
| 4342 | } |
| 4343 | case CALLBACK_AVAILABLE: { |
| 4344 | NetworkCapabilities cap = getObject(message, NetworkCapabilities.class); |
| 4345 | LinkProperties lp = getObject(message, LinkProperties.class); |
Chalard Jean | 22350c9 | 2023-10-07 19:21:45 +0900 | [diff] [blame] | 4346 | LocalNetworkInfo lni = getObject(message, LocalNetworkInfo.class); |
| 4347 | callback.onAvailable(network, cap, lp, lni, message.arg1); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4348 | break; |
| 4349 | } |
| 4350 | case CALLBACK_LOSING: { |
| 4351 | callback.onLosing(network, message.arg1); |
| 4352 | break; |
| 4353 | } |
| 4354 | case CALLBACK_LOST: { |
| 4355 | callback.onLost(network); |
| 4356 | break; |
| 4357 | } |
| 4358 | case CALLBACK_UNAVAIL: { |
| 4359 | callback.onUnavailable(); |
| 4360 | break; |
| 4361 | } |
| 4362 | case CALLBACK_CAP_CHANGED: { |
| 4363 | NetworkCapabilities cap = getObject(message, NetworkCapabilities.class); |
| 4364 | callback.onCapabilitiesChanged(network, cap); |
| 4365 | break; |
| 4366 | } |
| 4367 | case CALLBACK_IP_CHANGED: { |
| 4368 | LinkProperties lp = getObject(message, LinkProperties.class); |
| 4369 | callback.onLinkPropertiesChanged(network, lp); |
| 4370 | break; |
| 4371 | } |
Chalard Jean | 22350c9 | 2023-10-07 19:21:45 +0900 | [diff] [blame] | 4372 | case CALLBACK_LOCAL_NETWORK_INFO_CHANGED: { |
| 4373 | final LocalNetworkInfo info = getObject(message, LocalNetworkInfo.class); |
| 4374 | callback.onLocalNetworkInfoChanged(network, info); |
| 4375 | break; |
| 4376 | } |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4377 | case CALLBACK_SUSPENDED: { |
| 4378 | callback.onNetworkSuspended(network); |
| 4379 | break; |
| 4380 | } |
| 4381 | case CALLBACK_RESUMED: { |
| 4382 | callback.onNetworkResumed(network); |
| 4383 | break; |
| 4384 | } |
| 4385 | case CALLBACK_BLK_CHANGED: { |
Lorenzo Colitti | 8ad5812 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 4386 | callback.onBlockedStatusChanged(network, message.arg1); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4387 | } |
| 4388 | } |
| 4389 | } |
| 4390 | |
| 4391 | private <T> T getObject(Message msg, Class<T> c) { |
| 4392 | return (T) msg.getData().getParcelable(c.getSimpleName()); |
| 4393 | } |
| 4394 | } |
| 4395 | |
| 4396 | private CallbackHandler getDefaultHandler() { |
| 4397 | synchronized (sCallbacks) { |
| 4398 | if (sCallbackHandler == null) { |
| 4399 | sCallbackHandler = new CallbackHandler(ConnectivityThread.getInstanceLooper()); |
| 4400 | } |
| 4401 | return sCallbackHandler; |
| 4402 | } |
| 4403 | } |
| 4404 | |
| 4405 | private static final HashMap<NetworkRequest, NetworkCallback> sCallbacks = new HashMap<>(); |
| 4406 | private static CallbackHandler sCallbackHandler; |
| 4407 | |
Lorenzo Colitti | 5f26b19 | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 4408 | private NetworkRequest sendRequestForNetwork(int asUid, NetworkCapabilities need, |
| 4409 | NetworkCallback callback, int timeoutMs, NetworkRequest.Type reqType, int legacyType, |
| 4410 | CallbackHandler handler) { |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4411 | printStackTrace(); |
| 4412 | checkCallbackNotNull(callback); |
Remi NGUYEN VAN | 1818dbb | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 4413 | if (reqType != TRACK_DEFAULT && reqType != TRACK_SYSTEM_DEFAULT && need == null) { |
| 4414 | throw new IllegalArgumentException("null NetworkCapabilities"); |
| 4415 | } |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4416 | final NetworkRequest request; |
| 4417 | final String callingPackageName = mContext.getOpPackageName(); |
| 4418 | try { |
| 4419 | synchronized(sCallbacks) { |
| 4420 | if (callback.networkRequest != null |
| 4421 | && callback.networkRequest != ALREADY_UNREGISTERED) { |
| 4422 | // TODO: throw exception instead and enforce 1:1 mapping of callbacks |
| 4423 | // and requests (http://b/20701525). |
| 4424 | Log.e(TAG, "NetworkCallback was already registered"); |
| 4425 | } |
| 4426 | Messenger messenger = new Messenger(handler); |
| 4427 | Binder binder = new Binder(); |
Roshan Pius | e08bc18 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 4428 | final int callbackFlags = callback.mFlags; |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4429 | if (reqType == LISTEN) { |
| 4430 | request = mService.listenForNetwork( |
Roshan Pius | e08bc18 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 4431 | need, messenger, binder, callbackFlags, callingPackageName, |
Roshan Pius | a8a477b | 2020-12-17 14:53:09 -0800 | [diff] [blame] | 4432 | getAttributionTag()); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4433 | } else { |
| 4434 | request = mService.requestNetwork( |
Lorenzo Colitti | 5f26b19 | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 4435 | asUid, need, reqType.ordinal(), messenger, timeoutMs, binder, |
| 4436 | legacyType, callbackFlags, callingPackageName, getAttributionTag()); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4437 | } |
| 4438 | if (request != null) { |
| 4439 | sCallbacks.put(request, callback); |
| 4440 | } |
| 4441 | callback.networkRequest = request; |
| 4442 | } |
| 4443 | } catch (RemoteException e) { |
| 4444 | throw e.rethrowFromSystemServer(); |
| 4445 | } catch (ServiceSpecificException e) { |
| 4446 | throw convertServiceException(e); |
| 4447 | } |
| 4448 | return request; |
| 4449 | } |
| 4450 | |
Lorenzo Colitti | 5f26b19 | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 4451 | private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, NetworkCallback callback, |
| 4452 | int timeoutMs, NetworkRequest.Type reqType, int legacyType, CallbackHandler handler) { |
| 4453 | return sendRequestForNetwork(Process.INVALID_UID, need, callback, timeoutMs, reqType, |
| 4454 | legacyType, handler); |
| 4455 | } |
| 4456 | |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4457 | /** |
| 4458 | * Helper function to request a network with a particular legacy type. |
| 4459 | * |
| 4460 | * This API is only for use in internal system code that requests networks with legacy type and |
| 4461 | * relies on CONNECTIVITY_ACTION broadcasts instead of NetworkCallbacks. New caller should use |
| 4462 | * {@link #requestNetwork(NetworkRequest, NetworkCallback, Handler)} instead. |
| 4463 | * |
| 4464 | * @param request {@link NetworkRequest} describing this request. |
| 4465 | * @param timeoutMs The time in milliseconds to attempt looking for a suitable network |
| 4466 | * before {@link NetworkCallback#onUnavailable()} is called. The timeout must |
| 4467 | * be a positive value (i.e. >0). |
| 4468 | * @param legacyType to specify the network type(#TYPE_*). |
| 4469 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
| 4470 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 4471 | * the callback must not be shared - it uniquely specifies this request. |
| 4472 | * |
| 4473 | * @hide |
| 4474 | */ |
| 4475 | @SystemApi |
| 4476 | @RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK) |
| 4477 | public void requestNetwork(@NonNull NetworkRequest request, |
| 4478 | int timeoutMs, int legacyType, @NonNull Handler handler, |
| 4479 | @NonNull NetworkCallback networkCallback) { |
| 4480 | if (legacyType == TYPE_NONE) { |
| 4481 | throw new IllegalArgumentException("TYPE_NONE is meaningless legacy type"); |
| 4482 | } |
| 4483 | CallbackHandler cbHandler = new CallbackHandler(handler); |
| 4484 | NetworkCapabilities nc = request.networkCapabilities; |
| 4485 | sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, legacyType, cbHandler); |
| 4486 | } |
| 4487 | |
| 4488 | /** |
Roshan Pius | e08bc18 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 4489 | * Request a network to satisfy a set of {@link NetworkCapabilities}. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4490 | * |
| 4491 | * <p>This method will attempt to find the best network that matches the passed |
| 4492 | * {@link NetworkRequest}, and to bring up one that does if none currently satisfies the |
| 4493 | * criteria. The platform will evaluate which network is the best at its own discretion. |
| 4494 | * Throughput, latency, cost per byte, policy, user preference and other considerations |
| 4495 | * may be factored in the decision of what is considered the best network. |
| 4496 | * |
| 4497 | * <p>As long as this request is outstanding, the platform will try to maintain the best network |
| 4498 | * matching this request, while always attempting to match the request to a better network if |
| 4499 | * possible. If a better match is found, the platform will switch this request to the now-best |
| 4500 | * network and inform the app of the newly best network by invoking |
| 4501 | * {@link NetworkCallback#onAvailable(Network)} on the provided callback. Note that the platform |
| 4502 | * will not try to maintain any other network than the best one currently matching the request: |
| 4503 | * a network not matching any network request may be disconnected at any time. |
| 4504 | * |
| 4505 | * <p>For example, an application could use this method to obtain a connected cellular network |
| 4506 | * even if the device currently has a data connection over Ethernet. This may cause the cellular |
| 4507 | * radio to consume additional power. Or, an application could inform the system that it wants |
| 4508 | * a network supporting sending MMSes and have the system let it know about the currently best |
| 4509 | * MMS-supporting network through the provided {@link NetworkCallback}. |
| 4510 | * |
| 4511 | * <p>The status of the request can be followed by listening to the various callbacks described |
| 4512 | * in {@link NetworkCallback}. The {@link Network} object passed to the callback methods can be |
| 4513 | * used to direct traffic to the network (although accessing some networks may be subject to |
| 4514 | * holding specific permissions). Callers will learn about the specific characteristics of the |
| 4515 | * network through |
| 4516 | * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)} and |
| 4517 | * {@link NetworkCallback#onLinkPropertiesChanged(Network, LinkProperties)}. The methods of the |
| 4518 | * provided {@link NetworkCallback} will only be invoked due to changes in the best network |
| 4519 | * matching the request at any given time; therefore when a better network matching the request |
| 4520 | * becomes available, the {@link NetworkCallback#onAvailable(Network)} method is called |
| 4521 | * with the new network after which no further updates are given about the previously-best |
| 4522 | * network, unless it becomes the best again at some later time. All callbacks are invoked |
| 4523 | * in order on the same thread, which by default is a thread created by the framework running |
| 4524 | * in the app. |
chiachangwang | 9473c59 | 2022-07-15 02:25:52 +0000 | [diff] [blame] | 4525 | * See {@link #requestNetwork(NetworkRequest, NetworkCallback, Handler)} to change where the |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4526 | * callbacks are invoked. |
| 4527 | * |
| 4528 | * <p>This{@link NetworkRequest} will live until released via |
| 4529 | * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits, at |
| 4530 | * which point the system may let go of the network at any time. |
| 4531 | * |
| 4532 | * <p>A version of this method which takes a timeout is |
| 4533 | * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}, that an app can use to only |
| 4534 | * wait for a limited amount of time for the network to become unavailable. |
| 4535 | * |
| 4536 | * <p>It is presently unsupported to request a network with mutable |
| 4537 | * {@link NetworkCapabilities} such as |
| 4538 | * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or |
| 4539 | * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL} |
| 4540 | * as these {@code NetworkCapabilities} represent states that a particular |
| 4541 | * network may never attain, and whether a network will attain these states |
| 4542 | * is unknown prior to bringing up the network so the framework does not |
| 4543 | * know how to go about satisfying a request with these capabilities. |
| 4544 | * |
| 4545 | * <p>This method requires the caller to hold either the |
| 4546 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 4547 | * or the ability to modify system settings as determined by |
| 4548 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 4549 | * |
| 4550 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4551 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4552 | * all variants of this method, of {@link #registerNetworkCallback} as well as |
| 4553 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4554 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4555 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4556 | * make sure to unregister the callbacks with |
| 4557 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 4558 | * |
| 4559 | * @param request {@link NetworkRequest} describing this request. |
| 4560 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 4561 | * the callback must not be shared - it uniquely specifies this request. |
| 4562 | * The callback is invoked on the default internal Handler. |
| 4563 | * @throws IllegalArgumentException if {@code request} contains invalid network capabilities. |
| 4564 | * @throws SecurityException if missing the appropriate permissions. |
| 4565 | * @throws RuntimeException if the app already has too many callbacks registered. |
| 4566 | */ |
| 4567 | public void requestNetwork(@NonNull NetworkRequest request, |
| 4568 | @NonNull NetworkCallback networkCallback) { |
| 4569 | requestNetwork(request, networkCallback, getDefaultHandler()); |
| 4570 | } |
| 4571 | |
| 4572 | /** |
Roshan Pius | e08bc18 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 4573 | * Request a network to satisfy a set of {@link NetworkCapabilities}. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4574 | * |
| 4575 | * This method behaves identically to {@link #requestNetwork(NetworkRequest, NetworkCallback)} |
| 4576 | * but runs all the callbacks on the passed Handler. |
| 4577 | * |
| 4578 | * <p>This method has the same permission requirements as |
| 4579 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations, |
| 4580 | * and throws the same exceptions in the same conditions. |
| 4581 | * |
| 4582 | * @param request {@link NetworkRequest} describing this request. |
| 4583 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 4584 | * the callback must not be shared - it uniquely specifies this request. |
| 4585 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
| 4586 | */ |
| 4587 | public void requestNetwork(@NonNull NetworkRequest request, |
| 4588 | @NonNull NetworkCallback networkCallback, @NonNull Handler handler) { |
| 4589 | CallbackHandler cbHandler = new CallbackHandler(handler); |
| 4590 | NetworkCapabilities nc = request.networkCapabilities; |
| 4591 | sendRequestForNetwork(nc, networkCallback, 0, REQUEST, TYPE_NONE, cbHandler); |
| 4592 | } |
| 4593 | |
| 4594 | /** |
Roshan Pius | e08bc18 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 4595 | * Request a network to satisfy a set of {@link NetworkCapabilities}, limited |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4596 | * by a timeout. |
| 4597 | * |
| 4598 | * This function behaves identically to the non-timed-out version |
| 4599 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network |
| 4600 | * is not found within the given time (in milliseconds) the |
| 4601 | * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be |
| 4602 | * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does |
| 4603 | * not have to be released if timed-out (it is automatically released). Unregistering a |
| 4604 | * request that timed out is not an error. |
| 4605 | * |
| 4606 | * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small |
| 4607 | * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided |
| 4608 | * for that purpose. Calling this method will attempt to bring up the requested network. |
| 4609 | * |
| 4610 | * <p>This method has the same permission requirements as |
| 4611 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations, |
| 4612 | * and throws the same exceptions in the same conditions. |
| 4613 | * |
| 4614 | * @param request {@link NetworkRequest} describing this request. |
| 4615 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 4616 | * the callback must not be shared - it uniquely specifies this request. |
| 4617 | * @param timeoutMs The time in milliseconds to attempt looking for a suitable network |
| 4618 | * before {@link NetworkCallback#onUnavailable()} is called. The timeout must |
| 4619 | * be a positive value (i.e. >0). |
| 4620 | */ |
| 4621 | public void requestNetwork(@NonNull NetworkRequest request, |
| 4622 | @NonNull NetworkCallback networkCallback, int timeoutMs) { |
| 4623 | checkTimeout(timeoutMs); |
| 4624 | NetworkCapabilities nc = request.networkCapabilities; |
| 4625 | sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, TYPE_NONE, |
| 4626 | getDefaultHandler()); |
| 4627 | } |
| 4628 | |
| 4629 | /** |
Roshan Pius | e08bc18 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 4630 | * Request a network to satisfy a set of {@link NetworkCapabilities}, limited |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4631 | * by a timeout. |
| 4632 | * |
| 4633 | * This method behaves identically to |
| 4634 | * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} but runs all the callbacks |
| 4635 | * on the passed Handler. |
| 4636 | * |
| 4637 | * <p>This method has the same permission requirements as |
| 4638 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations, |
| 4639 | * and throws the same exceptions in the same conditions. |
| 4640 | * |
| 4641 | * @param request {@link NetworkRequest} describing this request. |
| 4642 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 4643 | * the callback must not be shared - it uniquely specifies this request. |
| 4644 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
| 4645 | * @param timeoutMs The time in milliseconds to attempt looking for a suitable network |
| 4646 | * before {@link NetworkCallback#onUnavailable} is called. |
| 4647 | */ |
| 4648 | public void requestNetwork(@NonNull NetworkRequest request, |
| 4649 | @NonNull NetworkCallback networkCallback, @NonNull Handler handler, int timeoutMs) { |
| 4650 | checkTimeout(timeoutMs); |
| 4651 | CallbackHandler cbHandler = new CallbackHandler(handler); |
| 4652 | NetworkCapabilities nc = request.networkCapabilities; |
| 4653 | sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, TYPE_NONE, cbHandler); |
| 4654 | } |
| 4655 | |
| 4656 | /** |
| 4657 | * The lookup key for a {@link Network} object included with the intent after |
| 4658 | * successfully finding a network for the applications request. Retrieve it with |
| 4659 | * {@link android.content.Intent#getParcelableExtra(String)}. |
| 4660 | * <p> |
| 4661 | * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)} |
| 4662 | * then you must get a ConnectivityManager instance before doing so. |
| 4663 | */ |
| 4664 | public static final String EXTRA_NETWORK = "android.net.extra.NETWORK"; |
| 4665 | |
| 4666 | /** |
| 4667 | * The lookup key for a {@link NetworkRequest} object included with the intent after |
| 4668 | * successfully finding a network for the applications request. Retrieve it with |
| 4669 | * {@link android.content.Intent#getParcelableExtra(String)}. |
| 4670 | */ |
| 4671 | public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST"; |
| 4672 | |
| 4673 | |
| 4674 | /** |
Roshan Pius | e08bc18 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 4675 | * Request a network to satisfy a set of {@link NetworkCapabilities}. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4676 | * |
| 4677 | * This function behaves identically to the version that takes a NetworkCallback, but instead |
| 4678 | * of {@link NetworkCallback} a {@link PendingIntent} is used. This means |
| 4679 | * the request may outlive the calling application and get called back when a suitable |
| 4680 | * network is found. |
| 4681 | * <p> |
| 4682 | * The operation is an Intent broadcast that goes to a broadcast receiver that |
| 4683 | * you registered with {@link Context#registerReceiver} or through the |
| 4684 | * <receiver> tag in an AndroidManifest.xml file |
| 4685 | * <p> |
| 4686 | * The operation Intent is delivered with two extras, a {@link Network} typed |
| 4687 | * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest} |
| 4688 | * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing |
| 4689 | * the original requests parameters. It is important to create a new, |
| 4690 | * {@link NetworkCallback} based request before completing the processing of the |
| 4691 | * Intent to reserve the network or it will be released shortly after the Intent |
| 4692 | * is processed. |
| 4693 | * <p> |
| 4694 | * If there is already a request for this Intent registered (with the equality of |
| 4695 | * two Intents defined by {@link Intent#filterEquals}), then it will be removed and |
| 4696 | * replaced by this one, effectively releasing the previous {@link NetworkRequest}. |
| 4697 | * <p> |
| 4698 | * The request may be released normally by calling |
| 4699 | * {@link #releaseNetworkRequest(android.app.PendingIntent)}. |
| 4700 | * <p>It is presently unsupported to request a network with either |
| 4701 | * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or |
| 4702 | * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL} |
| 4703 | * as these {@code NetworkCapabilities} represent states that a particular |
| 4704 | * network may never attain, and whether a network will attain these states |
| 4705 | * is unknown prior to bringing up the network so the framework does not |
| 4706 | * know how to go about satisfying a request with these capabilities. |
| 4707 | * |
| 4708 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4709 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4710 | * all variants of this method, of {@link #registerNetworkCallback} as well as |
| 4711 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4712 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4713 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4714 | * make sure to unregister the callbacks with {@link #unregisterNetworkCallback(PendingIntent)} |
| 4715 | * or {@link #releaseNetworkRequest(PendingIntent)}. |
| 4716 | * |
| 4717 | * <p>This method requires the caller to hold either the |
| 4718 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 4719 | * or the ability to modify system settings as determined by |
| 4720 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 4721 | * |
| 4722 | * @param request {@link NetworkRequest} describing this request. |
| 4723 | * @param operation Action to perform when the network is available (corresponds |
| 4724 | * to the {@link NetworkCallback#onAvailable} call. Typically |
| 4725 | * comes from {@link PendingIntent#getBroadcast}. Cannot be null. |
| 4726 | * @throws IllegalArgumentException if {@code request} contains invalid network capabilities. |
| 4727 | * @throws SecurityException if missing the appropriate permissions. |
| 4728 | * @throws RuntimeException if the app already has too many callbacks registered. |
| 4729 | */ |
| 4730 | public void requestNetwork(@NonNull NetworkRequest request, |
| 4731 | @NonNull PendingIntent operation) { |
| 4732 | printStackTrace(); |
| 4733 | checkPendingIntentNotNull(operation); |
| 4734 | try { |
| 4735 | mService.pendingRequestForNetwork( |
| 4736 | request.networkCapabilities, operation, mContext.getOpPackageName(), |
| 4737 | getAttributionTag()); |
| 4738 | } catch (RemoteException e) { |
| 4739 | throw e.rethrowFromSystemServer(); |
| 4740 | } catch (ServiceSpecificException e) { |
| 4741 | throw convertServiceException(e); |
| 4742 | } |
| 4743 | } |
| 4744 | |
| 4745 | /** |
| 4746 | * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} |
| 4747 | * <p> |
| 4748 | * This method has the same behavior as |
| 4749 | * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to |
| 4750 | * releasing network resources and disconnecting. |
| 4751 | * |
| 4752 | * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the |
| 4753 | * PendingIntent passed to |
| 4754 | * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the |
| 4755 | * corresponding NetworkRequest you'd like to remove. Cannot be null. |
| 4756 | */ |
| 4757 | public void releaseNetworkRequest(@NonNull PendingIntent operation) { |
| 4758 | printStackTrace(); |
| 4759 | checkPendingIntentNotNull(operation); |
| 4760 | try { |
| 4761 | mService.releasePendingNetworkRequest(operation); |
| 4762 | } catch (RemoteException e) { |
| 4763 | throw e.rethrowFromSystemServer(); |
| 4764 | } |
| 4765 | } |
| 4766 | |
| 4767 | private static void checkPendingIntentNotNull(PendingIntent intent) { |
Remi NGUYEN VAN | 1818dbb | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 4768 | Objects.requireNonNull(intent, "PendingIntent cannot be null."); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4769 | } |
| 4770 | |
| 4771 | private static void checkCallbackNotNull(NetworkCallback callback) { |
Remi NGUYEN VAN | 1818dbb | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 4772 | Objects.requireNonNull(callback, "null NetworkCallback"); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4773 | } |
| 4774 | |
| 4775 | private static void checkTimeout(int timeoutMs) { |
Remi NGUYEN VAN | 1818dbb | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 4776 | if (timeoutMs <= 0) { |
| 4777 | throw new IllegalArgumentException("timeoutMs must be strictly positive."); |
| 4778 | } |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4779 | } |
| 4780 | |
| 4781 | /** |
| 4782 | * Registers to receive notifications about all networks which satisfy the given |
| 4783 | * {@link NetworkRequest}. The callbacks will continue to be called until |
| 4784 | * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is |
| 4785 | * called. |
| 4786 | * |
| 4787 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4788 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4789 | * all variants of this method, of {@link #requestNetwork} as well as |
| 4790 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4791 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4792 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4793 | * make sure to unregister the callbacks with |
| 4794 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 4795 | * |
| 4796 | * @param request {@link NetworkRequest} describing this request. |
| 4797 | * @param networkCallback The {@link NetworkCallback} that the system will call as suitable |
| 4798 | * networks change state. |
| 4799 | * The callback is invoked on the default internal Handler. |
| 4800 | * @throws RuntimeException if the app already has too many callbacks registered. |
| 4801 | */ |
| 4802 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 4803 | public void registerNetworkCallback(@NonNull NetworkRequest request, |
| 4804 | @NonNull NetworkCallback networkCallback) { |
| 4805 | registerNetworkCallback(request, networkCallback, getDefaultHandler()); |
| 4806 | } |
| 4807 | |
| 4808 | /** |
| 4809 | * Registers to receive notifications about all networks which satisfy the given |
| 4810 | * {@link NetworkRequest}. The callbacks will continue to be called until |
| 4811 | * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is |
| 4812 | * called. |
| 4813 | * |
| 4814 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4815 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4816 | * all variants of this method, of {@link #requestNetwork} as well as |
| 4817 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4818 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4819 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4820 | * make sure to unregister the callbacks with |
| 4821 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 4822 | * |
| 4823 | * |
| 4824 | * @param request {@link NetworkRequest} describing this request. |
| 4825 | * @param networkCallback The {@link NetworkCallback} that the system will call as suitable |
| 4826 | * networks change state. |
| 4827 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
| 4828 | * @throws RuntimeException if the app already has too many callbacks registered. |
| 4829 | */ |
| 4830 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 4831 | public void registerNetworkCallback(@NonNull NetworkRequest request, |
| 4832 | @NonNull NetworkCallback networkCallback, @NonNull Handler handler) { |
| 4833 | CallbackHandler cbHandler = new CallbackHandler(handler); |
| 4834 | NetworkCapabilities nc = request.networkCapabilities; |
| 4835 | sendRequestForNetwork(nc, networkCallback, 0, LISTEN, TYPE_NONE, cbHandler); |
| 4836 | } |
| 4837 | |
| 4838 | /** |
| 4839 | * Registers a PendingIntent to be sent when a network is available which satisfies the given |
| 4840 | * {@link NetworkRequest}. |
| 4841 | * |
| 4842 | * This function behaves identically to the version that takes a NetworkCallback, but instead |
| 4843 | * of {@link NetworkCallback} a {@link PendingIntent} is used. This means |
| 4844 | * the request may outlive the calling application and get called back when a suitable |
| 4845 | * network is found. |
| 4846 | * <p> |
| 4847 | * The operation is an Intent broadcast that goes to a broadcast receiver that |
| 4848 | * you registered with {@link Context#registerReceiver} or through the |
| 4849 | * <receiver> tag in an AndroidManifest.xml file |
| 4850 | * <p> |
| 4851 | * The operation Intent is delivered with two extras, a {@link Network} typed |
| 4852 | * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest} |
| 4853 | * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing |
| 4854 | * the original requests parameters. |
| 4855 | * <p> |
| 4856 | * If there is already a request for this Intent registered (with the equality of |
| 4857 | * two Intents defined by {@link Intent#filterEquals}), then it will be removed and |
| 4858 | * replaced by this one, effectively releasing the previous {@link NetworkRequest}. |
| 4859 | * <p> |
| 4860 | * The request may be released normally by calling |
| 4861 | * {@link #unregisterNetworkCallback(android.app.PendingIntent)}. |
| 4862 | * |
| 4863 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4864 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4865 | * all variants of this method, of {@link #requestNetwork} as well as |
| 4866 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4867 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4868 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4869 | * make sure to unregister the callbacks with {@link #unregisterNetworkCallback(PendingIntent)} |
| 4870 | * or {@link #releaseNetworkRequest(PendingIntent)}. |
| 4871 | * |
| 4872 | * @param request {@link NetworkRequest} describing this request. |
| 4873 | * @param operation Action to perform when the network is available (corresponds |
| 4874 | * to the {@link NetworkCallback#onAvailable} call. Typically |
| 4875 | * comes from {@link PendingIntent#getBroadcast}. Cannot be null. |
| 4876 | * @throws RuntimeException if the app already has too many callbacks registered. |
| 4877 | */ |
| 4878 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 4879 | public void registerNetworkCallback(@NonNull NetworkRequest request, |
| 4880 | @NonNull PendingIntent operation) { |
| 4881 | printStackTrace(); |
| 4882 | checkPendingIntentNotNull(operation); |
| 4883 | try { |
| 4884 | mService.pendingListenForNetwork( |
Roshan Pius | a8a477b | 2020-12-17 14:53:09 -0800 | [diff] [blame] | 4885 | request.networkCapabilities, operation, mContext.getOpPackageName(), |
| 4886 | getAttributionTag()); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4887 | } catch (RemoteException e) { |
| 4888 | throw e.rethrowFromSystemServer(); |
| 4889 | } catch (ServiceSpecificException e) { |
| 4890 | throw convertServiceException(e); |
| 4891 | } |
| 4892 | } |
| 4893 | |
| 4894 | /** |
Lorenzo Colitti | a77d05e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 4895 | * Registers to receive notifications about changes in the application's default network. This |
| 4896 | * may be a physical network or a virtual network, such as a VPN that applies to the |
| 4897 | * application. The callbacks will continue to be called until either the application exits or |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4898 | * {@link #unregisterNetworkCallback(NetworkCallback)} is called. |
| 4899 | * |
| 4900 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4901 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4902 | * all variants of this method, of {@link #requestNetwork} as well as |
| 4903 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4904 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4905 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4906 | * make sure to unregister the callbacks with |
| 4907 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 4908 | * |
| 4909 | * @param networkCallback The {@link NetworkCallback} that the system will call as the |
Lorenzo Colitti | a77d05e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 4910 | * application's default network changes. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4911 | * The callback is invoked on the default internal Handler. |
| 4912 | * @throws RuntimeException if the app already has too many callbacks registered. |
| 4913 | */ |
| 4914 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 4915 | public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback) { |
| 4916 | registerDefaultNetworkCallback(networkCallback, getDefaultHandler()); |
| 4917 | } |
| 4918 | |
| 4919 | /** |
Lorenzo Colitti | a77d05e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 4920 | * Registers to receive notifications about changes in the application's default network. This |
| 4921 | * may be a physical network or a virtual network, such as a VPN that applies to the |
| 4922 | * application. The callbacks will continue to be called until either the application exits or |
| 4923 | * {@link #unregisterNetworkCallback(NetworkCallback)} is called. |
| 4924 | * |
| 4925 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4926 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4927 | * all variants of this method, of {@link #requestNetwork} as well as |
| 4928 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4929 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4930 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4931 | * make sure to unregister the callbacks with |
| 4932 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 4933 | * |
| 4934 | * @param networkCallback The {@link NetworkCallback} that the system will call as the |
| 4935 | * application's default network changes. |
| 4936 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
| 4937 | * @throws RuntimeException if the app already has too many callbacks registered. |
| 4938 | */ |
| 4939 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 4940 | public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback, |
| 4941 | @NonNull Handler handler) { |
Chiachang Wang | c7d203d | 2021-04-20 15:41:24 +0800 | [diff] [blame] | 4942 | registerDefaultNetworkCallbackForUid(Process.INVALID_UID, networkCallback, handler); |
Lorenzo Colitti | 5f26b19 | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 4943 | } |
| 4944 | |
| 4945 | /** |
| 4946 | * Registers to receive notifications about changes in the default network for the specified |
| 4947 | * UID. This may be a physical network or a virtual network, such as a VPN that applies to the |
| 4948 | * UID. The callbacks will continue to be called until either the application exits or |
| 4949 | * {@link #unregisterNetworkCallback(NetworkCallback)} is called. |
| 4950 | * |
| 4951 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4952 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4953 | * all variants of this method, of {@link #requestNetwork} as well as |
| 4954 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4955 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4956 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4957 | * make sure to unregister the callbacks with |
| 4958 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 4959 | * |
| 4960 | * @param uid the UID for which to track default network changes. |
| 4961 | * @param networkCallback The {@link NetworkCallback} that the system will call as the |
| 4962 | * UID's default network changes. |
| 4963 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
| 4964 | * @throws RuntimeException if the app already has too many callbacks registered. |
| 4965 | * @hide |
| 4966 | */ |
Lorenzo Colitti | b90bdbd | 2021-03-22 18:23:21 +0900 | [diff] [blame] | 4967 | @SystemApi(client = MODULE_LIBRARIES) |
Lorenzo Colitti | 5f26b19 | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 4968 | @SuppressLint({"ExecutorRegistration", "PairedRegistration"}) |
| 4969 | @RequiresPermission(anyOf = { |
| 4970 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 4971 | android.Manifest.permission.NETWORK_SETTINGS}) |
Chiachang Wang | c7d203d | 2021-04-20 15:41:24 +0800 | [diff] [blame] | 4972 | public void registerDefaultNetworkCallbackForUid(int uid, |
Lorenzo Colitti | 5f26b19 | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 4973 | @NonNull NetworkCallback networkCallback, @NonNull Handler handler) { |
Lorenzo Colitti | a77d05e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 4974 | CallbackHandler cbHandler = new CallbackHandler(handler); |
Lorenzo Colitti | 5f26b19 | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 4975 | sendRequestForNetwork(uid, null /* need */, networkCallback, 0 /* timeoutMs */, |
Lorenzo Colitti | a77d05e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 4976 | TRACK_DEFAULT, TYPE_NONE, cbHandler); |
| 4977 | } |
| 4978 | |
| 4979 | /** |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4980 | * Registers to receive notifications about changes in the system default network. The callbacks |
| 4981 | * will continue to be called until either the application exits or |
| 4982 | * {@link #unregisterNetworkCallback(NetworkCallback)} is called. |
| 4983 | * |
Lorenzo Colitti | a77d05e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 4984 | * This method should not be used to determine networking state seen by applications, because in |
| 4985 | * many cases, most or even all application traffic may not use the default network directly, |
| 4986 | * and traffic from different applications may go on different networks by default. As an |
| 4987 | * example, if a VPN is connected, traffic from all applications might be sent through the VPN |
| 4988 | * and not onto the system default network. Applications or system components desiring to do |
| 4989 | * determine network state as seen by applications should use other methods such as |
| 4990 | * {@link #registerDefaultNetworkCallback(NetworkCallback, Handler)}. |
| 4991 | * |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 4992 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4993 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4994 | * all variants of this method, of {@link #requestNetwork} as well as |
| 4995 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4996 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4997 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4998 | * make sure to unregister the callbacks with |
| 4999 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 5000 | * |
| 5001 | * @param networkCallback The {@link NetworkCallback} that the system will call as the |
| 5002 | * system default network changes. |
| 5003 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
| 5004 | * @throws RuntimeException if the app already has too many callbacks registered. |
Lorenzo Colitti | a77d05e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 5005 | * |
| 5006 | * @hide |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5007 | */ |
Lorenzo Colitti | a77d05e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 5008 | @SystemApi(client = MODULE_LIBRARIES) |
| 5009 | @SuppressLint({"ExecutorRegistration", "PairedRegistration"}) |
| 5010 | @RequiresPermission(anyOf = { |
| 5011 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
Junyu Lai | aa4ad8c | 2022-10-28 15:42:00 +0800 | [diff] [blame] | 5012 | android.Manifest.permission.NETWORK_SETTINGS, |
Quang Luong | 98858d6 | 2023-02-11 00:25:24 +0000 | [diff] [blame] | 5013 | android.Manifest.permission.NETWORK_SETUP_WIZARD, |
Junyu Lai | aa4ad8c | 2022-10-28 15:42:00 +0800 | [diff] [blame] | 5014 | android.Manifest.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS}) |
Lorenzo Colitti | a77d05e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 5015 | public void registerSystemDefaultNetworkCallback(@NonNull NetworkCallback networkCallback, |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5016 | @NonNull Handler handler) { |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5017 | CallbackHandler cbHandler = new CallbackHandler(handler); |
| 5018 | sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0, |
Lorenzo Colitti | a77d05e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 5019 | TRACK_SYSTEM_DEFAULT, TYPE_NONE, cbHandler); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5020 | } |
| 5021 | |
| 5022 | /** |
junyulai | bd12306 | 2021-03-15 11:48:48 +0800 | [diff] [blame] | 5023 | * Registers to receive notifications about the best matching network which satisfy the given |
| 5024 | * {@link NetworkRequest}. The callbacks will continue to be called until |
| 5025 | * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is |
| 5026 | * called. |
| 5027 | * |
| 5028 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 5029 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 5030 | * {@link #registerNetworkCallback} and its variants and {@link #requestNetwork} as well as |
| 5031 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 5032 | * Requesting a network with this method will count toward this limit. If this limit is |
| 5033 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 5034 | * make sure to unregister the callbacks with |
| 5035 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 5036 | * |
| 5037 | * |
| 5038 | * @param request {@link NetworkRequest} describing this request. |
| 5039 | * @param networkCallback The {@link NetworkCallback} that the system will call as suitable |
| 5040 | * networks change state. |
| 5041 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
| 5042 | * @throws RuntimeException if the app already has too many callbacks registered. |
junyulai | 5a5c99b | 2021-03-05 15:51:17 +0800 | [diff] [blame] | 5043 | */ |
junyulai | 5a5c99b | 2021-03-05 15:51:17 +0800 | [diff] [blame] | 5044 | @SuppressLint("ExecutorRegistration") |
| 5045 | public void registerBestMatchingNetworkCallback(@NonNull NetworkRequest request, |
| 5046 | @NonNull NetworkCallback networkCallback, @NonNull Handler handler) { |
| 5047 | final NetworkCapabilities nc = request.networkCapabilities; |
| 5048 | final CallbackHandler cbHandler = new CallbackHandler(handler); |
junyulai | 7664f62 | 2021-03-12 20:05:08 +0800 | [diff] [blame] | 5049 | sendRequestForNetwork(nc, networkCallback, 0, LISTEN_FOR_BEST, TYPE_NONE, cbHandler); |
junyulai | 5a5c99b | 2021-03-05 15:51:17 +0800 | [diff] [blame] | 5050 | } |
| 5051 | |
| 5052 | /** |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5053 | * Requests bandwidth update for a given {@link Network} and returns whether the update request |
| 5054 | * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying |
| 5055 | * network connection for updated bandwidth information. The caller will be notified via |
| 5056 | * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this |
| 5057 | * method assumes that the caller has previously called |
| 5058 | * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network |
| 5059 | * changes. |
| 5060 | * |
| 5061 | * @param network {@link Network} specifying which network you're interested. |
| 5062 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
| 5063 | */ |
| 5064 | public boolean requestBandwidthUpdate(@NonNull Network network) { |
| 5065 | try { |
| 5066 | return mService.requestBandwidthUpdate(network); |
| 5067 | } catch (RemoteException e) { |
| 5068 | throw e.rethrowFromSystemServer(); |
| 5069 | } |
| 5070 | } |
| 5071 | |
| 5072 | /** |
| 5073 | * Unregisters a {@code NetworkCallback} and possibly releases networks originating from |
| 5074 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and |
| 5075 | * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls. |
Chalard Jean | 0c7ebe9 | 2022-08-03 14:45:47 +0900 | [diff] [blame] | 5076 | * If the given {@code NetworkCallback} had previously been used with {@code #requestNetwork}, |
| 5077 | * any networks that the device brought up only to satisfy that request will be disconnected. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5078 | * |
| 5079 | * Notifications that would have triggered that {@code NetworkCallback} will immediately stop |
| 5080 | * triggering it as soon as this call returns. |
| 5081 | * |
| 5082 | * @param networkCallback The {@link NetworkCallback} used when making the request. |
| 5083 | */ |
| 5084 | public void unregisterNetworkCallback(@NonNull NetworkCallback networkCallback) { |
| 5085 | printStackTrace(); |
| 5086 | checkCallbackNotNull(networkCallback); |
| 5087 | final List<NetworkRequest> reqs = new ArrayList<>(); |
| 5088 | // Find all requests associated to this callback and stop callback triggers immediately. |
| 5089 | // Callback is reusable immediately. http://b/20701525, http://b/35921499. |
| 5090 | synchronized (sCallbacks) { |
Remi NGUYEN VAN | 1818dbb | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 5091 | if (networkCallback.networkRequest == null) { |
| 5092 | throw new IllegalArgumentException("NetworkCallback was not registered"); |
| 5093 | } |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5094 | if (networkCallback.networkRequest == ALREADY_UNREGISTERED) { |
| 5095 | Log.d(TAG, "NetworkCallback was already unregistered"); |
| 5096 | return; |
| 5097 | } |
| 5098 | for (Map.Entry<NetworkRequest, NetworkCallback> e : sCallbacks.entrySet()) { |
| 5099 | if (e.getValue() == networkCallback) { |
| 5100 | reqs.add(e.getKey()); |
| 5101 | } |
| 5102 | } |
| 5103 | // TODO: throw exception if callback was registered more than once (http://b/20701525). |
| 5104 | for (NetworkRequest r : reqs) { |
| 5105 | try { |
| 5106 | mService.releaseNetworkRequest(r); |
| 5107 | } catch (RemoteException e) { |
| 5108 | throw e.rethrowFromSystemServer(); |
| 5109 | } |
| 5110 | // Only remove mapping if rpc was successful. |
| 5111 | sCallbacks.remove(r); |
| 5112 | } |
| 5113 | networkCallback.networkRequest = ALREADY_UNREGISTERED; |
| 5114 | } |
| 5115 | } |
| 5116 | |
| 5117 | /** |
| 5118 | * Unregisters a callback previously registered via |
| 5119 | * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}. |
| 5120 | * |
| 5121 | * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the |
| 5122 | * PendingIntent passed to |
| 5123 | * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}. |
| 5124 | * Cannot be null. |
| 5125 | */ |
| 5126 | public void unregisterNetworkCallback(@NonNull PendingIntent operation) { |
| 5127 | releaseNetworkRequest(operation); |
| 5128 | } |
| 5129 | |
| 5130 | /** |
| 5131 | * Informs the system whether it should switch to {@code network} regardless of whether it is |
| 5132 | * validated or not. If {@code accept} is true, and the network was explicitly selected by the |
| 5133 | * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become |
| 5134 | * the system default network regardless of any other network that's currently connected. If |
| 5135 | * {@code always} is true, then the choice is remembered, so that the next time the user |
| 5136 | * connects to this network, the system will switch to it. |
| 5137 | * |
| 5138 | * @param network The network to accept. |
| 5139 | * @param accept Whether to accept the network even if unvalidated. |
| 5140 | * @param always Whether to remember this choice in the future. |
| 5141 | * |
| 5142 | * @hide |
| 5143 | */ |
Chiachang Wang | f9294e7 | 2021-03-18 09:44:34 +0800 | [diff] [blame] | 5144 | @SystemApi(client = MODULE_LIBRARIES) |
| 5145 | @RequiresPermission(anyOf = { |
| 5146 | android.Manifest.permission.NETWORK_SETTINGS, |
| 5147 | android.Manifest.permission.NETWORK_SETUP_WIZARD, |
| 5148 | android.Manifest.permission.NETWORK_STACK, |
| 5149 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) |
| 5150 | public void setAcceptUnvalidated(@NonNull Network network, boolean accept, boolean always) { |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5151 | try { |
| 5152 | mService.setAcceptUnvalidated(network, accept, always); |
| 5153 | } catch (RemoteException e) { |
| 5154 | throw e.rethrowFromSystemServer(); |
| 5155 | } |
| 5156 | } |
| 5157 | |
| 5158 | /** |
| 5159 | * Informs the system whether it should consider the network as validated even if it only has |
| 5160 | * partial connectivity. If {@code accept} is true, then the network will be considered as |
| 5161 | * validated even if connectivity is only partial. If {@code always} is true, then the choice |
| 5162 | * is remembered, so that the next time the user connects to this network, the system will |
| 5163 | * switch to it. |
| 5164 | * |
| 5165 | * @param network The network to accept. |
| 5166 | * @param accept Whether to consider the network as validated even if it has partial |
| 5167 | * connectivity. |
| 5168 | * @param always Whether to remember this choice in the future. |
| 5169 | * |
| 5170 | * @hide |
| 5171 | */ |
Chiachang Wang | f9294e7 | 2021-03-18 09:44:34 +0800 | [diff] [blame] | 5172 | @SystemApi(client = MODULE_LIBRARIES) |
| 5173 | @RequiresPermission(anyOf = { |
| 5174 | android.Manifest.permission.NETWORK_SETTINGS, |
| 5175 | android.Manifest.permission.NETWORK_SETUP_WIZARD, |
| 5176 | android.Manifest.permission.NETWORK_STACK, |
| 5177 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) |
| 5178 | public void setAcceptPartialConnectivity(@NonNull Network network, boolean accept, |
| 5179 | boolean always) { |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5180 | try { |
| 5181 | mService.setAcceptPartialConnectivity(network, accept, always); |
| 5182 | } catch (RemoteException e) { |
| 5183 | throw e.rethrowFromSystemServer(); |
| 5184 | } |
| 5185 | } |
| 5186 | |
| 5187 | /** |
| 5188 | * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is |
| 5189 | * only meaningful if the system is configured not to penalize such networks, e.g., if the |
| 5190 | * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code |
| 5191 | * NETWORK_AVOID_BAD_WIFI setting is unset}. |
| 5192 | * |
| 5193 | * @param network The network to accept. |
| 5194 | * |
| 5195 | * @hide |
| 5196 | */ |
Chiachang Wang | f9294e7 | 2021-03-18 09:44:34 +0800 | [diff] [blame] | 5197 | @SystemApi(client = MODULE_LIBRARIES) |
| 5198 | @RequiresPermission(anyOf = { |
| 5199 | android.Manifest.permission.NETWORK_SETTINGS, |
| 5200 | android.Manifest.permission.NETWORK_SETUP_WIZARD, |
| 5201 | android.Manifest.permission.NETWORK_STACK, |
| 5202 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) |
| 5203 | public void setAvoidUnvalidated(@NonNull Network network) { |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5204 | try { |
| 5205 | mService.setAvoidUnvalidated(network); |
| 5206 | } catch (RemoteException e) { |
| 5207 | throw e.rethrowFromSystemServer(); |
| 5208 | } |
| 5209 | } |
| 5210 | |
| 5211 | /** |
Chalard Jean | 0c7ebe9 | 2022-08-03 14:45:47 +0900 | [diff] [blame] | 5212 | * Temporarily allow bad Wi-Fi to override {@code config_networkAvoidBadWifi} configuration. |
Chiachang Wang | 6eac9fb | 2021-06-17 22:11:30 +0800 | [diff] [blame] | 5213 | * |
| 5214 | * @param timeMs The expired current time. The value should be set within a limited time from |
| 5215 | * now. |
| 5216 | * |
| 5217 | * @hide |
| 5218 | */ |
| 5219 | public void setTestAllowBadWifiUntil(long timeMs) { |
| 5220 | try { |
| 5221 | mService.setTestAllowBadWifiUntil(timeMs); |
| 5222 | } catch (RemoteException e) { |
| 5223 | throw e.rethrowFromSystemServer(); |
| 5224 | } |
| 5225 | } |
| 5226 | |
| 5227 | /** |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5228 | * Requests that the system open the captive portal app on the specified network. |
| 5229 | * |
Remi NGUYEN VAN | 8238a76 | 2021-03-16 18:06:06 +0900 | [diff] [blame] | 5230 | * <p>This is to be used on networks where a captive portal was detected, as per |
| 5231 | * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}. |
| 5232 | * |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5233 | * @param network The network to log into. |
| 5234 | * |
| 5235 | * @hide |
| 5236 | */ |
Remi NGUYEN VAN | 8238a76 | 2021-03-16 18:06:06 +0900 | [diff] [blame] | 5237 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 5238 | @RequiresPermission(anyOf = { |
| 5239 | android.Manifest.permission.NETWORK_SETTINGS, |
| 5240 | android.Manifest.permission.NETWORK_STACK, |
| 5241 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK |
| 5242 | }) |
| 5243 | public void startCaptivePortalApp(@NonNull Network network) { |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5244 | try { |
| 5245 | mService.startCaptivePortalApp(network); |
| 5246 | } catch (RemoteException e) { |
| 5247 | throw e.rethrowFromSystemServer(); |
| 5248 | } |
| 5249 | } |
| 5250 | |
| 5251 | /** |
| 5252 | * Requests that the system open the captive portal app with the specified extras. |
| 5253 | * |
| 5254 | * <p>This endpoint is exclusively for use by the NetworkStack and is protected by the |
| 5255 | * corresponding permission. |
| 5256 | * @param network Network on which the captive portal was detected. |
| 5257 | * @param appExtras Extras to include in the app start intent. |
| 5258 | * @hide |
| 5259 | */ |
| 5260 | @SystemApi |
| 5261 | @RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK) |
| 5262 | public void startCaptivePortalApp(@NonNull Network network, @NonNull Bundle appExtras) { |
| 5263 | try { |
| 5264 | mService.startCaptivePortalAppInternal(network, appExtras); |
| 5265 | } catch (RemoteException e) { |
| 5266 | throw e.rethrowFromSystemServer(); |
| 5267 | } |
| 5268 | } |
| 5269 | |
| 5270 | /** |
Chalard Jean | 0c7ebe9 | 2022-08-03 14:45:47 +0900 | [diff] [blame] | 5271 | * Determine whether the device is configured to avoid bad Wi-Fi. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5272 | * @hide |
| 5273 | */ |
| 5274 | @SystemApi |
| 5275 | @RequiresPermission(anyOf = { |
| 5276 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 5277 | android.Manifest.permission.NETWORK_STACK}) |
| 5278 | public boolean shouldAvoidBadWifi() { |
| 5279 | try { |
| 5280 | return mService.shouldAvoidBadWifi(); |
| 5281 | } catch (RemoteException e) { |
| 5282 | throw e.rethrowFromSystemServer(); |
| 5283 | } |
| 5284 | } |
| 5285 | |
| 5286 | /** |
| 5287 | * It is acceptable to briefly use multipath data to provide seamless connectivity for |
| 5288 | * time-sensitive user-facing operations when the system default network is temporarily |
| 5289 | * unresponsive. The amount of data should be limited (less than one megabyte for every call to |
| 5290 | * this method), and the operation should be infrequent to ensure that data usage is limited. |
| 5291 | * |
| 5292 | * An example of such an operation might be a time-sensitive foreground activity, such as a |
| 5293 | * voice command, that the user is performing while walking out of range of a Wi-Fi network. |
| 5294 | */ |
| 5295 | public static final int MULTIPATH_PREFERENCE_HANDOVER = 1 << 0; |
| 5296 | |
| 5297 | /** |
| 5298 | * It is acceptable to use small amounts of multipath data on an ongoing basis to provide |
| 5299 | * a backup channel for traffic that is primarily going over another network. |
| 5300 | * |
| 5301 | * An example might be maintaining backup connections to peers or servers for the purpose of |
| 5302 | * fast fallback if the default network is temporarily unresponsive or disconnects. The traffic |
| 5303 | * on backup paths should be negligible compared to the traffic on the main path. |
| 5304 | */ |
| 5305 | public static final int MULTIPATH_PREFERENCE_RELIABILITY = 1 << 1; |
| 5306 | |
| 5307 | /** |
| 5308 | * It is acceptable to use metered data to improve network latency and performance. |
| 5309 | */ |
| 5310 | public static final int MULTIPATH_PREFERENCE_PERFORMANCE = 1 << 2; |
| 5311 | |
| 5312 | /** |
| 5313 | * Return value to use for unmetered networks. On such networks we currently set all the flags |
| 5314 | * to true. |
| 5315 | * @hide |
| 5316 | */ |
| 5317 | public static final int MULTIPATH_PREFERENCE_UNMETERED = |
| 5318 | MULTIPATH_PREFERENCE_HANDOVER | |
| 5319 | MULTIPATH_PREFERENCE_RELIABILITY | |
| 5320 | MULTIPATH_PREFERENCE_PERFORMANCE; |
| 5321 | |
Aaron Huang | cff2294 | 2021-05-27 16:31:26 +0800 | [diff] [blame] | 5322 | /** @hide */ |
| 5323 | @Retention(RetentionPolicy.SOURCE) |
| 5324 | @IntDef(flag = true, value = { |
| 5325 | MULTIPATH_PREFERENCE_HANDOVER, |
| 5326 | MULTIPATH_PREFERENCE_RELIABILITY, |
| 5327 | MULTIPATH_PREFERENCE_PERFORMANCE, |
| 5328 | }) |
| 5329 | public @interface MultipathPreference { |
| 5330 | } |
| 5331 | |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5332 | /** |
| 5333 | * Provides a hint to the calling application on whether it is desirable to use the |
| 5334 | * multinetwork APIs (e.g., {@link Network#openConnection}, {@link Network#bindSocket}, etc.) |
| 5335 | * for multipath data transfer on this network when it is not the system default network. |
| 5336 | * Applications desiring to use multipath network protocols should call this method before |
| 5337 | * each such operation. |
| 5338 | * |
| 5339 | * @param network The network on which the application desires to use multipath data. |
Chalard Jean | 0c7ebe9 | 2022-08-03 14:45:47 +0900 | [diff] [blame] | 5340 | * If {@code null}, this method will return a preference that will generally |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5341 | * apply to metered networks. |
Chalard Jean | 0c7ebe9 | 2022-08-03 14:45:47 +0900 | [diff] [blame] | 5342 | * @return a bitwise OR of zero or more of the {@code MULTIPATH_PREFERENCE_*} constants. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5343 | */ |
| 5344 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 5345 | public @MultipathPreference int getMultipathPreference(@Nullable Network network) { |
| 5346 | try { |
| 5347 | return mService.getMultipathPreference(network); |
| 5348 | } catch (RemoteException e) { |
| 5349 | throw e.rethrowFromSystemServer(); |
| 5350 | } |
| 5351 | } |
| 5352 | |
| 5353 | /** |
| 5354 | * Resets all connectivity manager settings back to factory defaults. |
| 5355 | * @hide |
| 5356 | */ |
Chiachang Wang | f9294e7 | 2021-03-18 09:44:34 +0800 | [diff] [blame] | 5357 | @SystemApi(client = MODULE_LIBRARIES) |
| 5358 | @RequiresPermission(anyOf = { |
| 5359 | android.Manifest.permission.NETWORK_SETTINGS, |
| 5360 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5361 | public void factoryReset() { |
| 5362 | try { |
| 5363 | mService.factoryReset(); |
Remi NGUYEN VAN | e4d1cd9 | 2021-06-17 16:27:31 +0900 | [diff] [blame] | 5364 | getTetheringManager().stopAllTethering(); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5365 | } catch (RemoteException e) { |
| 5366 | throw e.rethrowFromSystemServer(); |
| 5367 | } |
| 5368 | } |
| 5369 | |
| 5370 | /** |
| 5371 | * Binds the current process to {@code network}. All Sockets created in the future |
| 5372 | * (and not explicitly bound via a bound SocketFactory from |
| 5373 | * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to |
| 5374 | * {@code network}. All host name resolutions will be limited to {@code network} as well. |
| 5375 | * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to |
| 5376 | * work and all host name resolutions will fail. This is by design so an application doesn't |
| 5377 | * accidentally use Sockets it thinks are still bound to a particular {@link Network}. |
| 5378 | * To clear binding pass {@code null} for {@code network}. Using individually bound |
| 5379 | * Sockets created by Network.getSocketFactory().createSocket() and |
| 5380 | * performing network-specific host name resolutions via |
| 5381 | * {@link Network#getAllByName Network.getAllByName} is preferred to calling |
| 5382 | * {@code bindProcessToNetwork}. |
| 5383 | * |
| 5384 | * @param network The {@link Network} to bind the current process to, or {@code null} to clear |
| 5385 | * the current binding. |
| 5386 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
| 5387 | */ |
| 5388 | public boolean bindProcessToNetwork(@Nullable Network network) { |
| 5389 | // Forcing callers to call through non-static function ensures ConnectivityManager |
| 5390 | // instantiated. |
| 5391 | return setProcessDefaultNetwork(network); |
| 5392 | } |
| 5393 | |
| 5394 | /** |
| 5395 | * Binds the current process to {@code network}. All Sockets created in the future |
| 5396 | * (and not explicitly bound via a bound SocketFactory from |
| 5397 | * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to |
| 5398 | * {@code network}. All host name resolutions will be limited to {@code network} as well. |
| 5399 | * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to |
| 5400 | * work and all host name resolutions will fail. This is by design so an application doesn't |
| 5401 | * accidentally use Sockets it thinks are still bound to a particular {@link Network}. |
| 5402 | * To clear binding pass {@code null} for {@code network}. Using individually bound |
| 5403 | * Sockets created by Network.getSocketFactory().createSocket() and |
| 5404 | * performing network-specific host name resolutions via |
| 5405 | * {@link Network#getAllByName Network.getAllByName} is preferred to calling |
| 5406 | * {@code setProcessDefaultNetwork}. |
| 5407 | * |
| 5408 | * @param network The {@link Network} to bind the current process to, or {@code null} to clear |
| 5409 | * the current binding. |
| 5410 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
| 5411 | * @deprecated This function can throw {@link IllegalStateException}. Use |
| 5412 | * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork} |
| 5413 | * is a direct replacement. |
| 5414 | */ |
| 5415 | @Deprecated |
| 5416 | public static boolean setProcessDefaultNetwork(@Nullable Network network) { |
| 5417 | int netId = (network == null) ? NETID_UNSET : network.netId; |
| 5418 | boolean isSameNetId = (netId == NetworkUtils.getBoundNetworkForProcess()); |
| 5419 | |
| 5420 | if (netId != NETID_UNSET) { |
| 5421 | netId = network.getNetIdForResolv(); |
| 5422 | } |
| 5423 | |
| 5424 | if (!NetworkUtils.bindProcessToNetwork(netId)) { |
| 5425 | return false; |
| 5426 | } |
| 5427 | |
| 5428 | if (!isSameNetId) { |
| 5429 | // Set HTTP proxy system properties to match network. |
| 5430 | // TODO: Deprecate this static method and replace it with a non-static version. |
| 5431 | try { |
Remi NGUYEN VAN | 8a831d6 | 2021-02-03 10:18:20 +0900 | [diff] [blame] | 5432 | Proxy.setHttpProxyConfiguration(getInstance().getDefaultProxy()); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5433 | } catch (SecurityException e) { |
| 5434 | // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy. |
| 5435 | Log.e(TAG, "Can't set proxy properties", e); |
| 5436 | } |
| 5437 | // Must flush DNS cache as new network may have different DNS resolutions. |
Remi NGUYEN VAN | b2e919f | 2021-07-02 09:34:36 +0900 | [diff] [blame] | 5438 | InetAddress.clearDnsCache(); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5439 | // Must flush socket pool as idle sockets will be bound to previous network and may |
| 5440 | // cause subsequent fetches to be performed on old network. |
Orion Hodson | 1f4fa9f | 2021-05-25 21:02:02 +0100 | [diff] [blame] | 5441 | NetworkEventDispatcher.getInstance().dispatchNetworkConfigurationChange(); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5442 | } |
| 5443 | |
| 5444 | return true; |
| 5445 | } |
| 5446 | |
| 5447 | /** |
| 5448 | * Returns the {@link Network} currently bound to this process via |
| 5449 | * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound. |
| 5450 | * |
| 5451 | * @return {@code Network} to which this process is bound, or {@code null}. |
| 5452 | */ |
| 5453 | @Nullable |
| 5454 | public Network getBoundNetworkForProcess() { |
Chalard Jean | 0c7ebe9 | 2022-08-03 14:45:47 +0900 | [diff] [blame] | 5455 | // Forcing callers to call through non-static function ensures ConnectivityManager has been |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5456 | // instantiated. |
| 5457 | return getProcessDefaultNetwork(); |
| 5458 | } |
| 5459 | |
| 5460 | /** |
| 5461 | * Returns the {@link Network} currently bound to this process via |
| 5462 | * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound. |
| 5463 | * |
| 5464 | * @return {@code Network} to which this process is bound, or {@code null}. |
| 5465 | * @deprecated Using this function can lead to other functions throwing |
| 5466 | * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead. |
| 5467 | * {@code getBoundNetworkForProcess} is a direct replacement. |
| 5468 | */ |
| 5469 | @Deprecated |
| 5470 | @Nullable |
| 5471 | public static Network getProcessDefaultNetwork() { |
| 5472 | int netId = NetworkUtils.getBoundNetworkForProcess(); |
| 5473 | if (netId == NETID_UNSET) return null; |
| 5474 | return new Network(netId); |
| 5475 | } |
| 5476 | |
| 5477 | private void unsupportedStartingFrom(int version) { |
| 5478 | if (Process.myUid() == Process.SYSTEM_UID) { |
| 5479 | // The getApplicationInfo() call we make below is not supported in system context. Let |
| 5480 | // the call through here, and rely on the fact that ConnectivityService will refuse to |
| 5481 | // allow the system to use these APIs anyway. |
| 5482 | return; |
| 5483 | } |
| 5484 | |
| 5485 | if (mContext.getApplicationInfo().targetSdkVersion >= version) { |
| 5486 | throw new UnsupportedOperationException( |
| 5487 | "This method is not supported in target SDK version " + version + " and above"); |
| 5488 | } |
| 5489 | } |
| 5490 | |
| 5491 | // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature, |
| 5492 | // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException. |
| 5493 | // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and |
| 5494 | // remove these exemptions. Note that this check is not secure, and apps can still access these |
| 5495 | // functions by accessing ConnectivityService directly. However, it should be clear that doing |
| 5496 | // so is unsupported and may break in the future. http://b/22728205 |
| 5497 | private void checkLegacyRoutingApiAccess() { |
| 5498 | unsupportedStartingFrom(VERSION_CODES.M); |
| 5499 | } |
| 5500 | |
| 5501 | /** |
| 5502 | * Binds host resolutions performed by this process to {@code network}. |
| 5503 | * {@link #bindProcessToNetwork} takes precedence over this setting. |
| 5504 | * |
| 5505 | * @param network The {@link Network} to bind host resolutions from the current process to, or |
| 5506 | * {@code null} to clear the current binding. |
| 5507 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
| 5508 | * @hide |
| 5509 | * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}. |
| 5510 | */ |
| 5511 | @Deprecated |
| 5512 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
| 5513 | public static boolean setProcessDefaultNetworkForHostResolution(Network network) { |
| 5514 | return NetworkUtils.bindProcessToNetworkForHostResolution( |
| 5515 | (network == null) ? NETID_UNSET : network.getNetIdForResolv()); |
| 5516 | } |
| 5517 | |
| 5518 | /** |
| 5519 | * Device is not restricting metered network activity while application is running on |
| 5520 | * background. |
| 5521 | */ |
| 5522 | public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1; |
| 5523 | |
| 5524 | /** |
| 5525 | * Device is restricting metered network activity while application is running on background, |
| 5526 | * but application is allowed to bypass it. |
| 5527 | * <p> |
| 5528 | * In this state, application should take action to mitigate metered network access. |
| 5529 | * For example, a music streaming application should switch to a low-bandwidth bitrate. |
| 5530 | */ |
| 5531 | public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2; |
| 5532 | |
| 5533 | /** |
| 5534 | * Device is restricting metered network activity while application is running on background. |
| 5535 | * <p> |
| 5536 | * In this state, application should not try to use the network while running on background, |
| 5537 | * because it would be denied. |
| 5538 | */ |
| 5539 | public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3; |
| 5540 | |
| 5541 | /** |
| 5542 | * A change in the background metered network activity restriction has occurred. |
| 5543 | * <p> |
| 5544 | * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction |
| 5545 | * applies to them. |
| 5546 | * <p> |
| 5547 | * This is only sent to registered receivers, not manifest receivers. |
| 5548 | */ |
| 5549 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 5550 | public static final String ACTION_RESTRICT_BACKGROUND_CHANGED = |
| 5551 | "android.net.conn.RESTRICT_BACKGROUND_CHANGED"; |
| 5552 | |
Aaron Huang | cff2294 | 2021-05-27 16:31:26 +0800 | [diff] [blame] | 5553 | /** @hide */ |
| 5554 | @Retention(RetentionPolicy.SOURCE) |
| 5555 | @IntDef(flag = false, value = { |
| 5556 | RESTRICT_BACKGROUND_STATUS_DISABLED, |
| 5557 | RESTRICT_BACKGROUND_STATUS_WHITELISTED, |
| 5558 | RESTRICT_BACKGROUND_STATUS_ENABLED, |
| 5559 | }) |
| 5560 | public @interface RestrictBackgroundStatus { |
| 5561 | } |
| 5562 | |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5563 | /** |
| 5564 | * Determines if the calling application is subject to metered network restrictions while |
| 5565 | * running on background. |
| 5566 | * |
| 5567 | * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED}, |
| 5568 | * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED}, |
| 5569 | * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED} |
| 5570 | */ |
| 5571 | public @RestrictBackgroundStatus int getRestrictBackgroundStatus() { |
| 5572 | try { |
Remi NGUYEN VAN | 1fdeb50 | 2021-03-18 14:23:12 +0900 | [diff] [blame] | 5573 | return mService.getRestrictBackgroundStatusByCaller(); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5574 | } catch (RemoteException e) { |
| 5575 | throw e.rethrowFromSystemServer(); |
| 5576 | } |
| 5577 | } |
| 5578 | |
| 5579 | /** |
| 5580 | * The network watchlist is a list of domains and IP addresses that are associated with |
| 5581 | * potentially harmful apps. This method returns the SHA-256 of the watchlist config file |
| 5582 | * currently used by the system for validation purposes. |
| 5583 | * |
| 5584 | * @return Hash of network watchlist config file. Null if config does not exist. |
| 5585 | */ |
| 5586 | @Nullable |
| 5587 | public byte[] getNetworkWatchlistConfigHash() { |
| 5588 | try { |
| 5589 | return mService.getNetworkWatchlistConfigHash(); |
| 5590 | } catch (RemoteException e) { |
| 5591 | Log.e(TAG, "Unable to get watchlist config hash"); |
| 5592 | throw e.rethrowFromSystemServer(); |
| 5593 | } |
| 5594 | } |
| 5595 | |
| 5596 | /** |
| 5597 | * Returns the {@code uid} of the owner of a network connection. |
| 5598 | * |
| 5599 | * @param protocol The protocol of the connection. Only {@code IPPROTO_TCP} and {@code |
| 5600 | * IPPROTO_UDP} currently supported. |
| 5601 | * @param local The local {@link InetSocketAddress} of a connection. |
| 5602 | * @param remote The remote {@link InetSocketAddress} of a connection. |
| 5603 | * @return {@code uid} if the connection is found and the app has permission to observe it |
| 5604 | * (e.g., if it is associated with the calling VPN app's VpnService tunnel) or {@link |
| 5605 | * android.os.Process#INVALID_UID} if the connection is not found. |
Sherri Lin | 443b718 | 2023-02-08 04:49:29 +0100 | [diff] [blame] | 5606 | * @throws SecurityException if the caller is not the active VpnService for the current |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5607 | * user. |
Sherri Lin | 443b718 | 2023-02-08 04:49:29 +0100 | [diff] [blame] | 5608 | * @throws IllegalArgumentException if an unsupported protocol is requested. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5609 | */ |
| 5610 | public int getConnectionOwnerUid( |
| 5611 | int protocol, @NonNull InetSocketAddress local, @NonNull InetSocketAddress remote) { |
| 5612 | ConnectionInfo connectionInfo = new ConnectionInfo(protocol, local, remote); |
| 5613 | try { |
| 5614 | return mService.getConnectionOwnerUid(connectionInfo); |
| 5615 | } catch (RemoteException e) { |
| 5616 | throw e.rethrowFromSystemServer(); |
| 5617 | } |
| 5618 | } |
| 5619 | |
| 5620 | private void printStackTrace() { |
| 5621 | if (DEBUG) { |
| 5622 | final StackTraceElement[] callStack = Thread.currentThread().getStackTrace(); |
| 5623 | final StringBuffer sb = new StringBuffer(); |
| 5624 | for (int i = 3; i < callStack.length; i++) { |
| 5625 | final String stackTrace = callStack[i].toString(); |
| 5626 | if (stackTrace == null || stackTrace.contains("android.os")) { |
| 5627 | break; |
| 5628 | } |
| 5629 | sb.append(" [").append(stackTrace).append("]"); |
| 5630 | } |
| 5631 | Log.d(TAG, "StackLog:" + sb.toString()); |
| 5632 | } |
| 5633 | } |
| 5634 | |
Remi NGUYEN VAN | 91444ca | 2021-01-15 23:02:47 +0900 | [diff] [blame] | 5635 | /** @hide */ |
| 5636 | public TestNetworkManager startOrGetTestNetworkManager() { |
| 5637 | final IBinder tnBinder; |
| 5638 | try { |
| 5639 | tnBinder = mService.startOrGetTestNetworkService(); |
| 5640 | } catch (RemoteException e) { |
| 5641 | throw e.rethrowFromSystemServer(); |
| 5642 | } |
| 5643 | |
| 5644 | return new TestNetworkManager(ITestNetworkManager.Stub.asInterface(tnBinder)); |
| 5645 | } |
| 5646 | |
Remi NGUYEN VAN | 91444ca | 2021-01-15 23:02:47 +0900 | [diff] [blame] | 5647 | /** @hide */ |
| 5648 | public ConnectivityDiagnosticsManager createDiagnosticsManager() { |
| 5649 | return new ConnectivityDiagnosticsManager(mContext, mService); |
| 5650 | } |
| 5651 | |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5652 | /** |
| 5653 | * Simulates a Data Stall for the specified Network. |
| 5654 | * |
| 5655 | * <p>This method should only be used for tests. |
| 5656 | * |
Remi NGUYEN VAN | 564f7f8 | 2021-04-08 16:26:20 +0900 | [diff] [blame] | 5657 | * <p>The caller must be the owner of the specified Network. This simulates a data stall to |
| 5658 | * have the system behave as if it had happened, but does not actually stall connectivity. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5659 | * |
| 5660 | * @param detectionMethod The detection method used to identify the Data Stall. |
Remi NGUYEN VAN | 564f7f8 | 2021-04-08 16:26:20 +0900 | [diff] [blame] | 5661 | * See ConnectivityDiagnosticsManager.DataStallReport.DETECTION_METHOD_*. |
| 5662 | * @param timestampMillis The timestamp at which the stall 'occurred', in milliseconds, as per |
| 5663 | * SystemClock.elapsedRealtime. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5664 | * @param network The Network for which a Data Stall is being simluated. |
| 5665 | * @param extras The PersistableBundle of extras included in the Data Stall notification. |
| 5666 | * @throws SecurityException if the caller is not the owner of the given network. |
| 5667 | * @hide |
| 5668 | */ |
| 5669 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 5670 | @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_TEST_NETWORKS, |
| 5671 | android.Manifest.permission.NETWORK_STACK}) |
Remi NGUYEN VAN | 564f7f8 | 2021-04-08 16:26:20 +0900 | [diff] [blame] | 5672 | public void simulateDataStall(@DetectionMethod int detectionMethod, long timestampMillis, |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5673 | @NonNull Network network, @NonNull PersistableBundle extras) { |
| 5674 | try { |
| 5675 | mService.simulateDataStall(detectionMethod, timestampMillis, network, extras); |
| 5676 | } catch (RemoteException e) { |
| 5677 | e.rethrowFromSystemServer(); |
| 5678 | } |
| 5679 | } |
| 5680 | |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5681 | @NonNull |
| 5682 | private final List<QosCallbackConnection> mQosCallbackConnections = new ArrayList<>(); |
| 5683 | |
| 5684 | /** |
| 5685 | * Registers a {@link QosSocketInfo} with an associated {@link QosCallback}. The callback will |
| 5686 | * receive available QoS events related to the {@link Network} and local ip + port |
| 5687 | * specified within socketInfo. |
| 5688 | * <p/> |
| 5689 | * The same {@link QosCallback} must be unregistered before being registered a second time, |
| 5690 | * otherwise {@link QosCallbackRegistrationException} is thrown. |
| 5691 | * <p/> |
| 5692 | * This API does not, in itself, require any permission if called with a network that is not |
| 5693 | * restricted. However, the underlying implementation currently only supports the IMS network, |
| 5694 | * which is always restricted. That means non-preinstalled callers can't possibly find this API |
| 5695 | * useful, because they'd never be called back on networks that they would have access to. |
| 5696 | * |
| 5697 | * @throws SecurityException if {@link QosSocketInfo#getNetwork()} is restricted and the app is |
| 5698 | * missing CONNECTIVITY_USE_RESTRICTED_NETWORKS permission. |
| 5699 | * @throws QosCallback.QosCallbackRegistrationException if qosCallback is already registered. |
| 5700 | * @throws RuntimeException if the app already has too many callbacks registered. |
| 5701 | * |
| 5702 | * Exceptions after the time of registration is passed through |
| 5703 | * {@link QosCallback#onError(QosCallbackException)}. see: {@link QosCallbackException}. |
| 5704 | * |
| 5705 | * @param socketInfo the socket information used to match QoS events |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5706 | * @param executor The executor on which the callback will be invoked. The provided |
| 5707 | * {@link Executor} must run callback sequentially, otherwise the order of |
Daniel Bright | 686d5d2 | 2021-03-10 11:51:50 -0800 | [diff] [blame] | 5708 | * callbacks cannot be guaranteed.onQosCallbackRegistered |
| 5709 | * @param callback receives qos events that satisfy socketInfo |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5710 | * |
| 5711 | * @hide |
| 5712 | */ |
| 5713 | @SystemApi |
| 5714 | public void registerQosCallback(@NonNull final QosSocketInfo socketInfo, |
Daniel Bright | 686d5d2 | 2021-03-10 11:51:50 -0800 | [diff] [blame] | 5715 | @CallbackExecutor @NonNull final Executor executor, |
| 5716 | @NonNull final QosCallback callback) { |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5717 | Objects.requireNonNull(socketInfo, "socketInfo must be non-null"); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5718 | Objects.requireNonNull(executor, "executor must be non-null"); |
Daniel Bright | 686d5d2 | 2021-03-10 11:51:50 -0800 | [diff] [blame] | 5719 | Objects.requireNonNull(callback, "callback must be non-null"); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5720 | |
| 5721 | try { |
| 5722 | synchronized (mQosCallbackConnections) { |
| 5723 | if (getQosCallbackConnection(callback) == null) { |
| 5724 | final QosCallbackConnection connection = |
| 5725 | new QosCallbackConnection(this, callback, executor); |
| 5726 | mQosCallbackConnections.add(connection); |
| 5727 | mService.registerQosSocketCallback(socketInfo, connection); |
| 5728 | } else { |
| 5729 | Log.e(TAG, "registerQosCallback: Callback already registered"); |
| 5730 | throw new QosCallbackRegistrationException(); |
| 5731 | } |
| 5732 | } |
| 5733 | } catch (final RemoteException e) { |
| 5734 | Log.e(TAG, "registerQosCallback: Error while registering ", e); |
| 5735 | |
| 5736 | // The same unregister method method is called for consistency even though nothing |
| 5737 | // will be sent to the ConnectivityService since the callback was never successfully |
| 5738 | // registered. |
| 5739 | unregisterQosCallback(callback); |
| 5740 | e.rethrowFromSystemServer(); |
| 5741 | } catch (final ServiceSpecificException e) { |
| 5742 | Log.e(TAG, "registerQosCallback: Error while registering ", e); |
| 5743 | unregisterQosCallback(callback); |
| 5744 | throw convertServiceException(e); |
| 5745 | } |
| 5746 | } |
| 5747 | |
| 5748 | /** |
| 5749 | * Unregisters the given {@link QosCallback}. The {@link QosCallback} will no longer receive |
| 5750 | * events once unregistered and can be registered a second time. |
| 5751 | * <p/> |
| 5752 | * If the {@link QosCallback} does not have an active registration, it is a no-op. |
| 5753 | * |
| 5754 | * @param callback the callback being unregistered |
| 5755 | * |
| 5756 | * @hide |
| 5757 | */ |
| 5758 | @SystemApi |
| 5759 | public void unregisterQosCallback(@NonNull final QosCallback callback) { |
| 5760 | Objects.requireNonNull(callback, "The callback must be non-null"); |
| 5761 | try { |
| 5762 | synchronized (mQosCallbackConnections) { |
| 5763 | final QosCallbackConnection connection = getQosCallbackConnection(callback); |
| 5764 | if (connection != null) { |
| 5765 | connection.stopReceivingMessages(); |
| 5766 | mService.unregisterQosCallback(connection); |
| 5767 | mQosCallbackConnections.remove(connection); |
| 5768 | } else { |
| 5769 | Log.d(TAG, "unregisterQosCallback: Callback not registered"); |
| 5770 | } |
| 5771 | } |
| 5772 | } catch (final RemoteException e) { |
| 5773 | Log.e(TAG, "unregisterQosCallback: Error while unregistering ", e); |
| 5774 | e.rethrowFromSystemServer(); |
| 5775 | } |
| 5776 | } |
| 5777 | |
| 5778 | /** |
| 5779 | * Gets the connection related to the callback. |
| 5780 | * |
| 5781 | * @param callback the callback to look up |
| 5782 | * @return the related connection |
| 5783 | */ |
| 5784 | @Nullable |
| 5785 | private QosCallbackConnection getQosCallbackConnection(final QosCallback callback) { |
| 5786 | for (final QosCallbackConnection connection : mQosCallbackConnections) { |
| 5787 | // Checking by reference here is intentional |
| 5788 | if (connection.getCallback() == callback) { |
| 5789 | return connection; |
| 5790 | } |
| 5791 | } |
| 5792 | return null; |
| 5793 | } |
| 5794 | |
| 5795 | /** |
Roshan Pius | e08bc18 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 5796 | * Request a network to satisfy a set of {@link NetworkCapabilities}, but |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5797 | * does not cause any networks to retain the NET_CAPABILITY_FOREGROUND capability. This can |
| 5798 | * be used to request that the system provide a network without causing the network to be |
| 5799 | * in the foreground. |
| 5800 | * |
| 5801 | * <p>This method will attempt to find the best network that matches the passed |
| 5802 | * {@link NetworkRequest}, and to bring up one that does if none currently satisfies the |
| 5803 | * criteria. The platform will evaluate which network is the best at its own discretion. |
| 5804 | * Throughput, latency, cost per byte, policy, user preference and other considerations |
| 5805 | * may be factored in the decision of what is considered the best network. |
| 5806 | * |
| 5807 | * <p>As long as this request is outstanding, the platform will try to maintain the best network |
| 5808 | * matching this request, while always attempting to match the request to a better network if |
| 5809 | * possible. If a better match is found, the platform will switch this request to the now-best |
| 5810 | * network and inform the app of the newly best network by invoking |
| 5811 | * {@link NetworkCallback#onAvailable(Network)} on the provided callback. Note that the platform |
| 5812 | * will not try to maintain any other network than the best one currently matching the request: |
| 5813 | * a network not matching any network request may be disconnected at any time. |
| 5814 | * |
| 5815 | * <p>For example, an application could use this method to obtain a connected cellular network |
| 5816 | * even if the device currently has a data connection over Ethernet. This may cause the cellular |
| 5817 | * radio to consume additional power. Or, an application could inform the system that it wants |
| 5818 | * a network supporting sending MMSes and have the system let it know about the currently best |
| 5819 | * MMS-supporting network through the provided {@link NetworkCallback}. |
| 5820 | * |
| 5821 | * <p>The status of the request can be followed by listening to the various callbacks described |
| 5822 | * in {@link NetworkCallback}. The {@link Network} object passed to the callback methods can be |
| 5823 | * used to direct traffic to the network (although accessing some networks may be subject to |
| 5824 | * holding specific permissions). Callers will learn about the specific characteristics of the |
| 5825 | * network through |
| 5826 | * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)} and |
| 5827 | * {@link NetworkCallback#onLinkPropertiesChanged(Network, LinkProperties)}. The methods of the |
| 5828 | * provided {@link NetworkCallback} will only be invoked due to changes in the best network |
| 5829 | * matching the request at any given time; therefore when a better network matching the request |
| 5830 | * becomes available, the {@link NetworkCallback#onAvailable(Network)} method is called |
| 5831 | * with the new network after which no further updates are given about the previously-best |
| 5832 | * network, unless it becomes the best again at some later time. All callbacks are invoked |
| 5833 | * in order on the same thread, which by default is a thread created by the framework running |
| 5834 | * in the app. |
| 5835 | * |
| 5836 | * <p>This{@link NetworkRequest} will live until released via |
| 5837 | * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits, at |
| 5838 | * which point the system may let go of the network at any time. |
| 5839 | * |
| 5840 | * <p>It is presently unsupported to request a network with mutable |
| 5841 | * {@link NetworkCapabilities} such as |
| 5842 | * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or |
| 5843 | * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL} |
| 5844 | * as these {@code NetworkCapabilities} represent states that a particular |
| 5845 | * network may never attain, and whether a network will attain these states |
| 5846 | * is unknown prior to bringing up the network so the framework does not |
| 5847 | * know how to go about satisfying a request with these capabilities. |
| 5848 | * |
| 5849 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 5850 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 5851 | * all variants of this method, of {@link #registerNetworkCallback} as well as |
| 5852 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 5853 | * Requesting a network with this method will count toward this limit. If this limit is |
| 5854 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 5855 | * make sure to unregister the callbacks with |
| 5856 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 5857 | * |
| 5858 | * @param request {@link NetworkRequest} describing this request. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5859 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 5860 | * the callback must not be shared - it uniquely specifies this request. |
junyulai | ca657cb | 2021-04-15 00:39:49 +0800 | [diff] [blame] | 5861 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
| 5862 | * If null, the callback is invoked on the default internal Handler. |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5863 | * @throws IllegalArgumentException if {@code request} contains invalid network capabilities. |
| 5864 | * @throws SecurityException if missing the appropriate permissions. |
| 5865 | * @throws RuntimeException if the app already has too many callbacks registered. |
| 5866 | * |
| 5867 | * @hide |
| 5868 | */ |
| 5869 | @SystemApi(client = MODULE_LIBRARIES) |
| 5870 | @SuppressLint("ExecutorRegistration") |
| 5871 | @RequiresPermission(anyOf = { |
| 5872 | android.Manifest.permission.NETWORK_SETTINGS, |
| 5873 | android.Manifest.permission.NETWORK_STACK, |
| 5874 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK |
| 5875 | }) |
| 5876 | public void requestBackgroundNetwork(@NonNull NetworkRequest request, |
junyulai | ca657cb | 2021-04-15 00:39:49 +0800 | [diff] [blame] | 5877 | @NonNull NetworkCallback networkCallback, |
| 5878 | @SuppressLint("ListenerLast") @NonNull Handler handler) { |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5879 | final NetworkCapabilities nc = request.networkCapabilities; |
| 5880 | sendRequestForNetwork(nc, networkCallback, 0, BACKGROUND_REQUEST, |
junyulai | dbb7046 | 2021-03-09 20:49:48 +0800 | [diff] [blame] | 5881 | TYPE_NONE, new CallbackHandler(handler)); |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 5882 | } |
James Mattis | 12aeab8 | 2021-01-10 14:24:24 -0800 | [diff] [blame] | 5883 | |
| 5884 | /** |
James Mattis | 12aeab8 | 2021-01-10 14:24:24 -0800 | [diff] [blame] | 5885 | * Used by automotive devices to set the network preferences used to direct traffic at an |
| 5886 | * application level as per the given OemNetworkPreferences. An example use-case would be an |
| 5887 | * automotive OEM wanting to provide connectivity for applications critical to the usage of a |
| 5888 | * vehicle via a particular network. |
| 5889 | * |
| 5890 | * Calling this will overwrite the existing preference. |
| 5891 | * |
| 5892 | * @param preference {@link OemNetworkPreferences} The application network preference to be set. |
| 5893 | * @param executor the executor on which listener will be invoked. |
| 5894 | * @param listener {@link OnSetOemNetworkPreferenceListener} optional listener used to |
| 5895 | * communicate completion of setOemNetworkPreference(). This will only be |
| 5896 | * called once upon successful completion of setOemNetworkPreference(). |
| 5897 | * @throws IllegalArgumentException if {@code preference} contains invalid preference values. |
| 5898 | * @throws SecurityException if missing the appropriate permissions. |
| 5899 | * @throws UnsupportedOperationException if called on a non-automotive device. |
James Mattis | 6e2d702 | 2021-01-26 16:23:52 -0800 | [diff] [blame] | 5900 | * @hide |
James Mattis | 12aeab8 | 2021-01-10 14:24:24 -0800 | [diff] [blame] | 5901 | */ |
James Mattis | 6e2d702 | 2021-01-26 16:23:52 -0800 | [diff] [blame] | 5902 | @SystemApi |
James Mattis | a46c144 | 2021-01-26 14:05:36 -0800 | [diff] [blame] | 5903 | @RequiresPermission(android.Manifest.permission.CONTROL_OEM_PAID_NETWORK_PREFERENCE) |
James Mattis | 6e2d702 | 2021-01-26 16:23:52 -0800 | [diff] [blame] | 5904 | public void setOemNetworkPreference(@NonNull final OemNetworkPreferences preference, |
James Mattis | 12aeab8 | 2021-01-10 14:24:24 -0800 | [diff] [blame] | 5905 | @Nullable @CallbackExecutor final Executor executor, |
Chalard Jean | 0a4aefc | 2021-03-03 16:37:13 +0900 | [diff] [blame] | 5906 | @Nullable final Runnable listener) { |
James Mattis | 12aeab8 | 2021-01-10 14:24:24 -0800 | [diff] [blame] | 5907 | Objects.requireNonNull(preference, "OemNetworkPreferences must be non-null"); |
| 5908 | if (null != listener) { |
| 5909 | Objects.requireNonNull(executor, "Executor must be non-null"); |
| 5910 | } |
Chalard Jean | 0a4aefc | 2021-03-03 16:37:13 +0900 | [diff] [blame] | 5911 | final IOnCompleteListener listenerInternal = listener == null ? null : |
| 5912 | new IOnCompleteListener.Stub() { |
James Mattis | 12aeab8 | 2021-01-10 14:24:24 -0800 | [diff] [blame] | 5913 | @Override |
| 5914 | public void onComplete() { |
Chalard Jean | 0a4aefc | 2021-03-03 16:37:13 +0900 | [diff] [blame] | 5915 | executor.execute(listener::run); |
James Mattis | 12aeab8 | 2021-01-10 14:24:24 -0800 | [diff] [blame] | 5916 | } |
| 5917 | }; |
| 5918 | |
| 5919 | try { |
| 5920 | mService.setOemNetworkPreference(preference, listenerInternal); |
| 5921 | } catch (RemoteException e) { |
| 5922 | Log.e(TAG, "setOemNetworkPreference() failed for preference: " + preference.toString()); |
| 5923 | throw e.rethrowFromSystemServer(); |
| 5924 | } |
| 5925 | } |
lucaslin | 5cdbcfb | 2021-03-12 00:46:33 +0800 | [diff] [blame] | 5926 | |
Chalard Jean | ad565e2 | 2021-02-25 17:23:40 +0900 | [diff] [blame] | 5927 | /** |
| 5928 | * Request that a user profile is put by default on a network matching a given preference. |
| 5929 | * |
| 5930 | * See the documentation for the individual preferences for a description of the supported |
| 5931 | * behaviors. |
| 5932 | * |
| 5933 | * @param profile the profile concerned. |
| 5934 | * @param preference the preference for this profile. |
| 5935 | * @param executor an executor to execute the listener on. Optional if listener is null. |
| 5936 | * @param listener an optional listener to listen for completion of the operation. |
| 5937 | * @throws IllegalArgumentException if {@code profile} is not a valid user profile. |
| 5938 | * @throws SecurityException if missing the appropriate permissions. |
Sooraj Sasindran | e7aee27 | 2021-11-24 20:26:55 -0800 | [diff] [blame] | 5939 | * @deprecated Use {@link #setProfileNetworkPreferences(UserHandle, List, Executor, Runnable)} |
| 5940 | * instead as it provides a more flexible API with more options. |
Chalard Jean | ad565e2 | 2021-02-25 17:23:40 +0900 | [diff] [blame] | 5941 | * @hide |
| 5942 | */ |
Chalard Jean | 0a4aefc | 2021-03-03 16:37:13 +0900 | [diff] [blame] | 5943 | // This function is for establishing per-profile default networking and can only be called by |
| 5944 | // the device policy manager, running as the system server. It would make no sense to call it |
| 5945 | // on a context for a user because it does not establish a setting on behalf of a user, rather |
| 5946 | // it establishes a setting for a user on behalf of the DPM. |
| 5947 | @SuppressLint({"UserHandle"}) |
| 5948 | @SystemApi(client = MODULE_LIBRARIES) |
Chalard Jean | ad565e2 | 2021-02-25 17:23:40 +0900 | [diff] [blame] | 5949 | @RequiresPermission(android.Manifest.permission.NETWORK_STACK) |
Sooraj Sasindran | e7aee27 | 2021-11-24 20:26:55 -0800 | [diff] [blame] | 5950 | @Deprecated |
Chalard Jean | ad565e2 | 2021-02-25 17:23:40 +0900 | [diff] [blame] | 5951 | public void setProfileNetworkPreference(@NonNull final UserHandle profile, |
Sooraj Sasindran | e7aee27 | 2021-11-24 20:26:55 -0800 | [diff] [blame] | 5952 | @ProfileNetworkPreferencePolicy final int preference, |
| 5953 | @Nullable @CallbackExecutor final Executor executor, |
| 5954 | @Nullable final Runnable listener) { |
| 5955 | |
| 5956 | ProfileNetworkPreference.Builder preferenceBuilder = |
| 5957 | new ProfileNetworkPreference.Builder(); |
| 5958 | preferenceBuilder.setPreference(preference); |
Sooraj Sasindran | f4a58dc | 2022-01-21 13:37:08 -0800 | [diff] [blame] | 5959 | if (preference != PROFILE_NETWORK_PREFERENCE_DEFAULT) { |
| 5960 | preferenceBuilder.setPreferenceEnterpriseId(NET_ENTERPRISE_ID_1); |
| 5961 | } |
Sooraj Sasindran | e7aee27 | 2021-11-24 20:26:55 -0800 | [diff] [blame] | 5962 | setProfileNetworkPreferences(profile, |
| 5963 | List.of(preferenceBuilder.build()), executor, listener); |
| 5964 | } |
| 5965 | |
| 5966 | /** |
| 5967 | * Set a list of default network selection policies for a user profile. |
| 5968 | * |
| 5969 | * Calling this API with a user handle defines the entire policy for that user handle. |
| 5970 | * It will overwrite any setting previously set for the same user profile, |
| 5971 | * and not affect previously set settings for other handles. |
| 5972 | * |
| 5973 | * Call this API with an empty list to remove settings for this user profile. |
| 5974 | * |
| 5975 | * See {@link ProfileNetworkPreference} for more details on each preference |
| 5976 | * parameter. |
| 5977 | * |
| 5978 | * @param profile the user profile for which the preference is being set. |
| 5979 | * @param profileNetworkPreferences the list of profile network preferences for the |
| 5980 | * provided profile. |
| 5981 | * @param executor an executor to execute the listener on. Optional if listener is null. |
| 5982 | * @param listener an optional listener to listen for completion of the operation. |
| 5983 | * @throws IllegalArgumentException if {@code profile} is not a valid user profile. |
| 5984 | * @throws SecurityException if missing the appropriate permissions. |
| 5985 | * @hide |
| 5986 | */ |
| 5987 | @SystemApi(client = MODULE_LIBRARIES) |
| 5988 | @RequiresPermission(android.Manifest.permission.NETWORK_STACK) |
| 5989 | public void setProfileNetworkPreferences( |
| 5990 | @NonNull final UserHandle profile, |
| 5991 | @NonNull List<ProfileNetworkPreference> profileNetworkPreferences, |
Chalard Jean | ad565e2 | 2021-02-25 17:23:40 +0900 | [diff] [blame] | 5992 | @Nullable @CallbackExecutor final Executor executor, |
| 5993 | @Nullable final Runnable listener) { |
| 5994 | if (null != listener) { |
| 5995 | Objects.requireNonNull(executor, "Pass a non-null executor, or a null listener"); |
| 5996 | } |
| 5997 | final IOnCompleteListener proxy; |
| 5998 | if (null == listener) { |
| 5999 | proxy = null; |
| 6000 | } else { |
| 6001 | proxy = new IOnCompleteListener.Stub() { |
| 6002 | @Override |
| 6003 | public void onComplete() { |
| 6004 | executor.execute(listener::run); |
| 6005 | } |
| 6006 | }; |
| 6007 | } |
| 6008 | try { |
Sooraj Sasindran | e7aee27 | 2021-11-24 20:26:55 -0800 | [diff] [blame] | 6009 | mService.setProfileNetworkPreferences(profile, profileNetworkPreferences, proxy); |
Chalard Jean | ad565e2 | 2021-02-25 17:23:40 +0900 | [diff] [blame] | 6010 | } catch (RemoteException e) { |
| 6011 | throw e.rethrowFromSystemServer(); |
| 6012 | } |
| 6013 | } |
| 6014 | |
lucaslin | 5cdbcfb | 2021-03-12 00:46:33 +0800 | [diff] [blame] | 6015 | // The first network ID of IPSec tunnel interface. |
lucaslin | c296fcc | 2021-03-15 17:24:12 +0800 | [diff] [blame] | 6016 | private static final int TUN_INTF_NETID_START = 0xFC00; // 0xFC00 = 64512 |
lucaslin | 5cdbcfb | 2021-03-12 00:46:33 +0800 | [diff] [blame] | 6017 | // The network ID range of IPSec tunnel interface. |
lucaslin | c296fcc | 2021-03-15 17:24:12 +0800 | [diff] [blame] | 6018 | private static final int TUN_INTF_NETID_RANGE = 0x0400; // 0x0400 = 1024 |
lucaslin | 5cdbcfb | 2021-03-12 00:46:33 +0800 | [diff] [blame] | 6019 | |
| 6020 | /** |
| 6021 | * Get the network ID range reserved for IPSec tunnel interfaces. |
| 6022 | * |
| 6023 | * @return A Range which indicates the network ID range of IPSec tunnel interface. |
| 6024 | * @hide |
| 6025 | */ |
| 6026 | @SystemApi(client = MODULE_LIBRARIES) |
| 6027 | @NonNull |
| 6028 | public static Range<Integer> getIpSecNetIdRange() { |
| 6029 | return new Range(TUN_INTF_NETID_START, TUN_INTF_NETID_START + TUN_INTF_NETID_RANGE - 1); |
| 6030 | } |
markchien | 738ad91 | 2021-12-09 18:15:45 +0800 | [diff] [blame] | 6031 | |
| 6032 | /** |
Junyu Lai | df21036 | 2023-10-24 02:47:50 +0000 | [diff] [blame] | 6033 | * Sets data saver switch. |
| 6034 | * |
| 6035 | * @param enable True if enable. |
| 6036 | * @throws IllegalStateException if failed. |
| 6037 | * @hide |
| 6038 | */ |
| 6039 | @FlaggedApi(Flags.SET_DATA_SAVER_VIA_CM) |
| 6040 | @SystemApi(client = MODULE_LIBRARIES) |
| 6041 | @RequiresPermission(anyOf = { |
| 6042 | android.Manifest.permission.NETWORK_SETTINGS, |
| 6043 | android.Manifest.permission.NETWORK_STACK, |
| 6044 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK |
| 6045 | }) |
| 6046 | public void setDataSaverEnabled(final boolean enable) { |
| 6047 | try { |
| 6048 | mService.setDataSaverEnabled(enable); |
| 6049 | } catch (RemoteException e) { |
| 6050 | throw e.rethrowFromSystemServer(); |
| 6051 | } |
| 6052 | } |
| 6053 | |
| 6054 | /** |
markchien | e46042b | 2022-03-02 18:07:35 +0800 | [diff] [blame] | 6055 | * Adds the specified UID to the list of UIds that are allowed to use data on metered networks |
| 6056 | * even when background data is restricted. The deny list takes precedence over the allow list. |
markchien | 738ad91 | 2021-12-09 18:15:45 +0800 | [diff] [blame] | 6057 | * |
| 6058 | * @param uid uid of target app |
markchien | 68cfadc | 2022-01-14 13:39:54 +0800 | [diff] [blame] | 6059 | * @throws IllegalStateException if updating allow list failed. |
markchien | 738ad91 | 2021-12-09 18:15:45 +0800 | [diff] [blame] | 6060 | * @hide |
| 6061 | */ |
| 6062 | @SystemApi(client = MODULE_LIBRARIES) |
| 6063 | @RequiresPermission(anyOf = { |
| 6064 | android.Manifest.permission.NETWORK_SETTINGS, |
| 6065 | android.Manifest.permission.NETWORK_STACK, |
| 6066 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK |
| 6067 | }) |
markchien | e46042b | 2022-03-02 18:07:35 +0800 | [diff] [blame] | 6068 | public void addUidToMeteredNetworkAllowList(final int uid) { |
markchien | 738ad91 | 2021-12-09 18:15:45 +0800 | [diff] [blame] | 6069 | try { |
markchien | e46042b | 2022-03-02 18:07:35 +0800 | [diff] [blame] | 6070 | mService.updateMeteredNetworkAllowList(uid, true /* add */); |
markchien | 738ad91 | 2021-12-09 18:15:45 +0800 | [diff] [blame] | 6071 | } catch (RemoteException e) { |
| 6072 | throw e.rethrowFromSystemServer(); |
markchien | 738ad91 | 2021-12-09 18:15:45 +0800 | [diff] [blame] | 6073 | } |
| 6074 | } |
| 6075 | |
| 6076 | /** |
markchien | e46042b | 2022-03-02 18:07:35 +0800 | [diff] [blame] | 6077 | * Removes the specified UID from the list of UIDs that are allowed to use background data on |
| 6078 | * metered networks when background data is restricted. The deny list takes precedence over |
| 6079 | * the allow list. |
| 6080 | * |
| 6081 | * @param uid uid of target app |
| 6082 | * @throws IllegalStateException if updating allow list failed. |
| 6083 | * @hide |
| 6084 | */ |
| 6085 | @SystemApi(client = MODULE_LIBRARIES) |
| 6086 | @RequiresPermission(anyOf = { |
| 6087 | android.Manifest.permission.NETWORK_SETTINGS, |
| 6088 | android.Manifest.permission.NETWORK_STACK, |
| 6089 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK |
| 6090 | }) |
| 6091 | public void removeUidFromMeteredNetworkAllowList(final int uid) { |
| 6092 | try { |
| 6093 | mService.updateMeteredNetworkAllowList(uid, false /* remove */); |
| 6094 | } catch (RemoteException e) { |
| 6095 | throw e.rethrowFromSystemServer(); |
| 6096 | } |
| 6097 | } |
| 6098 | |
| 6099 | /** |
| 6100 | * Adds the specified UID to the list of UIDs that are not allowed to use background data on |
| 6101 | * metered networks. Takes precedence over {@link #addUidToMeteredNetworkAllowList}. |
markchien | 738ad91 | 2021-12-09 18:15:45 +0800 | [diff] [blame] | 6102 | * |
| 6103 | * @param uid uid of target app |
markchien | 68cfadc | 2022-01-14 13:39:54 +0800 | [diff] [blame] | 6104 | * @throws IllegalStateException if updating deny list failed. |
markchien | 738ad91 | 2021-12-09 18:15:45 +0800 | [diff] [blame] | 6105 | * @hide |
| 6106 | */ |
| 6107 | @SystemApi(client = MODULE_LIBRARIES) |
| 6108 | @RequiresPermission(anyOf = { |
| 6109 | android.Manifest.permission.NETWORK_SETTINGS, |
| 6110 | android.Manifest.permission.NETWORK_STACK, |
| 6111 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK |
| 6112 | }) |
markchien | e46042b | 2022-03-02 18:07:35 +0800 | [diff] [blame] | 6113 | public void addUidToMeteredNetworkDenyList(final int uid) { |
markchien | 738ad91 | 2021-12-09 18:15:45 +0800 | [diff] [blame] | 6114 | try { |
markchien | e46042b | 2022-03-02 18:07:35 +0800 | [diff] [blame] | 6115 | mService.updateMeteredNetworkDenyList(uid, true /* add */); |
| 6116 | } catch (RemoteException e) { |
| 6117 | throw e.rethrowFromSystemServer(); |
| 6118 | } |
| 6119 | } |
| 6120 | |
| 6121 | /** |
Chalard Jean | 0c7ebe9 | 2022-08-03 14:45:47 +0900 | [diff] [blame] | 6122 | * Removes the specified UID from the list of UIDs that can use background data on metered |
markchien | e46042b | 2022-03-02 18:07:35 +0800 | [diff] [blame] | 6123 | * networks if background data is not restricted. The deny list takes precedence over the |
| 6124 | * allow list. |
| 6125 | * |
| 6126 | * @param uid uid of target app |
| 6127 | * @throws IllegalStateException if updating deny list failed. |
| 6128 | * @hide |
| 6129 | */ |
| 6130 | @SystemApi(client = MODULE_LIBRARIES) |
| 6131 | @RequiresPermission(anyOf = { |
| 6132 | android.Manifest.permission.NETWORK_SETTINGS, |
| 6133 | android.Manifest.permission.NETWORK_STACK, |
| 6134 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK |
| 6135 | }) |
| 6136 | public void removeUidFromMeteredNetworkDenyList(final int uid) { |
| 6137 | try { |
| 6138 | mService.updateMeteredNetworkDenyList(uid, false /* remove */); |
markchien | 738ad91 | 2021-12-09 18:15:45 +0800 | [diff] [blame] | 6139 | } catch (RemoteException e) { |
| 6140 | throw e.rethrowFromSystemServer(); |
markchien | e1561fa | 2021-12-09 22:00:56 +0800 | [diff] [blame] | 6141 | } |
| 6142 | } |
| 6143 | |
| 6144 | /** |
| 6145 | * Sets a firewall rule for the specified UID on the specified chain. |
| 6146 | * |
| 6147 | * @param chain target chain. |
| 6148 | * @param uid uid to allow/deny. |
markchien | 3c04e66 | 2022-03-22 16:29:56 +0800 | [diff] [blame] | 6149 | * @param rule firewall rule to allow/drop packets. |
markchien | 68cfadc | 2022-01-14 13:39:54 +0800 | [diff] [blame] | 6150 | * @throws IllegalStateException if updating firewall rule failed. |
markchien | 3c04e66 | 2022-03-22 16:29:56 +0800 | [diff] [blame] | 6151 | * @throws IllegalArgumentException if {@code rule} is not a valid rule. |
markchien | e1561fa | 2021-12-09 22:00:56 +0800 | [diff] [blame] | 6152 | * @hide |
| 6153 | */ |
| 6154 | @SystemApi(client = MODULE_LIBRARIES) |
| 6155 | @RequiresPermission(anyOf = { |
| 6156 | android.Manifest.permission.NETWORK_SETTINGS, |
| 6157 | android.Manifest.permission.NETWORK_STACK, |
| 6158 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK |
| 6159 | }) |
markchien | 3c04e66 | 2022-03-22 16:29:56 +0800 | [diff] [blame] | 6160 | public void setUidFirewallRule(@FirewallChain final int chain, final int uid, |
| 6161 | @FirewallRule final int rule) { |
markchien | e1561fa | 2021-12-09 22:00:56 +0800 | [diff] [blame] | 6162 | try { |
markchien | 3c04e66 | 2022-03-22 16:29:56 +0800 | [diff] [blame] | 6163 | mService.setUidFirewallRule(chain, uid, rule); |
markchien | e1561fa | 2021-12-09 22:00:56 +0800 | [diff] [blame] | 6164 | } catch (RemoteException e) { |
| 6165 | throw e.rethrowFromSystemServer(); |
markchien | 738ad91 | 2021-12-09 18:15:45 +0800 | [diff] [blame] | 6166 | } |
| 6167 | } |
markchien | 98a6f95 | 2022-01-13 23:43:53 +0800 | [diff] [blame] | 6168 | |
| 6169 | /** |
Motomu Utsumi | 900b806 | 2023-01-19 16:16:49 +0900 | [diff] [blame] | 6170 | * Get firewall rule of specified firewall chain on specified uid. |
| 6171 | * |
| 6172 | * @param chain target chain. |
| 6173 | * @param uid target uid |
| 6174 | * @return either FIREWALL_RULE_ALLOW or FIREWALL_RULE_DENY |
| 6175 | * @throws UnsupportedOperationException if called on pre-T devices. |
| 6176 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 6177 | * cause of the failure. |
| 6178 | * @hide |
| 6179 | */ |
| 6180 | @RequiresPermission(anyOf = { |
| 6181 | android.Manifest.permission.NETWORK_SETTINGS, |
| 6182 | android.Manifest.permission.NETWORK_STACK, |
| 6183 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK |
| 6184 | }) |
| 6185 | public int getUidFirewallRule(@FirewallChain final int chain, final int uid) { |
| 6186 | try { |
| 6187 | return mService.getUidFirewallRule(chain, uid); |
| 6188 | } catch (RemoteException e) { |
| 6189 | throw e.rethrowFromSystemServer(); |
| 6190 | } |
| 6191 | } |
| 6192 | |
| 6193 | /** |
markchien | 98a6f95 | 2022-01-13 23:43:53 +0800 | [diff] [blame] | 6194 | * Enables or disables the specified firewall chain. |
| 6195 | * |
| 6196 | * @param chain target chain. |
| 6197 | * @param enable whether the chain should be enabled. |
Motomu Utsumi | 18b287d | 2022-06-19 10:45:30 +0000 | [diff] [blame] | 6198 | * @throws UnsupportedOperationException if called on pre-T devices. |
markchien | 68cfadc | 2022-01-14 13:39:54 +0800 | [diff] [blame] | 6199 | * @throws IllegalStateException if enabling or disabling the firewall chain failed. |
markchien | 98a6f95 | 2022-01-13 23:43:53 +0800 | [diff] [blame] | 6200 | * @hide |
| 6201 | */ |
| 6202 | @SystemApi(client = MODULE_LIBRARIES) |
| 6203 | @RequiresPermission(anyOf = { |
| 6204 | android.Manifest.permission.NETWORK_SETTINGS, |
| 6205 | android.Manifest.permission.NETWORK_STACK, |
| 6206 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK |
| 6207 | }) |
| 6208 | public void setFirewallChainEnabled(@FirewallChain final int chain, final boolean enable) { |
| 6209 | try { |
| 6210 | mService.setFirewallChainEnabled(chain, enable); |
| 6211 | } catch (RemoteException e) { |
| 6212 | throw e.rethrowFromSystemServer(); |
| 6213 | } |
| 6214 | } |
markchien | 00a0bed | 2022-01-13 23:46:13 +0800 | [diff] [blame] | 6215 | |
| 6216 | /** |
Motomu Utsumi | 25cf86f | 2022-06-27 08:50:19 +0000 | [diff] [blame] | 6217 | * Get the specified firewall chain's status. |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 6218 | * |
| 6219 | * @param chain target chain. |
| 6220 | * @return {@code true} if chain is enabled, {@code false} if chain is disabled. |
| 6221 | * @throws UnsupportedOperationException if called on pre-T devices. |
Motomu Utsumi | be3ff1e | 2022-06-08 10:05:07 +0000 | [diff] [blame] | 6222 | * @throws ServiceSpecificException in case of failure, with an error code indicating the |
| 6223 | * cause of the failure. |
| 6224 | * @hide |
| 6225 | */ |
| 6226 | @RequiresPermission(anyOf = { |
| 6227 | android.Manifest.permission.NETWORK_SETTINGS, |
| 6228 | android.Manifest.permission.NETWORK_STACK, |
| 6229 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK |
| 6230 | }) |
| 6231 | public boolean getFirewallChainEnabled(@FirewallChain final int chain) { |
| 6232 | try { |
| 6233 | return mService.getFirewallChainEnabled(chain); |
| 6234 | } catch (RemoteException e) { |
| 6235 | throw e.rethrowFromSystemServer(); |
| 6236 | } |
| 6237 | } |
| 6238 | |
| 6239 | /** |
markchien | 00a0bed | 2022-01-13 23:46:13 +0800 | [diff] [blame] | 6240 | * Replaces the contents of the specified UID-based firewall chain. |
| 6241 | * |
| 6242 | * @param chain target chain to replace. |
| 6243 | * @param uids The list of UIDs to be placed into chain. |
Motomu Utsumi | 9be2ea0 | 2022-07-05 06:14:59 +0000 | [diff] [blame] | 6244 | * @throws UnsupportedOperationException if called on pre-T devices. |
markchien | 00a0bed | 2022-01-13 23:46:13 +0800 | [diff] [blame] | 6245 | * @throws IllegalArgumentException if {@code chain} is not a valid chain. |
| 6246 | * @hide |
| 6247 | */ |
| 6248 | @SystemApi(client = MODULE_LIBRARIES) |
| 6249 | @RequiresPermission(anyOf = { |
| 6250 | android.Manifest.permission.NETWORK_SETTINGS, |
| 6251 | android.Manifest.permission.NETWORK_STACK, |
| 6252 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK |
| 6253 | }) |
| 6254 | public void replaceFirewallChain(@FirewallChain final int chain, @NonNull final int[] uids) { |
| 6255 | Objects.requireNonNull(uids); |
| 6256 | try { |
| 6257 | mService.replaceFirewallChain(chain, uids); |
| 6258 | } catch (RemoteException e) { |
| 6259 | throw e.rethrowFromSystemServer(); |
| 6260 | } |
| 6261 | } |
Igor Chernyshev | 9dac660 | 2022-12-13 19:28:32 -0800 | [diff] [blame] | 6262 | |
Junyu Lai | e003152 | 2023-08-29 18:32:57 +0800 | [diff] [blame] | 6263 | /** |
Junyu Lai | c3dc5b6 | 2023-09-06 19:10:02 +0800 | [diff] [blame] | 6264 | * Helper class to track data saver status. |
| 6265 | * |
| 6266 | * The class will fetch current data saver status from {@link NetworkPolicyManager} when |
| 6267 | * initialized, and listening for status changed intent to cache the latest status. |
| 6268 | * |
| 6269 | * @hide |
| 6270 | */ |
| 6271 | @TargetApi(Build.VERSION_CODES.TIRAMISU) // RECEIVER_NOT_EXPORTED requires T. |
| 6272 | @VisibleForTesting(visibility = PRIVATE) |
| 6273 | public static class DataSaverStatusTracker extends BroadcastReceiver { |
| 6274 | private static final Object sDataSaverStatusTrackerLock = new Object(); |
| 6275 | |
| 6276 | private static volatile DataSaverStatusTracker sInstance; |
| 6277 | |
| 6278 | /** |
| 6279 | * Gets a static instance of the class. |
| 6280 | * |
| 6281 | * @param context A {@link Context} for initialization. Note that since the data saver |
| 6282 | * status is global on a device, passing any context is equivalent. |
| 6283 | * @return The static instance of a {@link DataSaverStatusTracker}. |
| 6284 | */ |
| 6285 | public static DataSaverStatusTracker getInstance(@NonNull Context context) { |
| 6286 | if (sInstance == null) { |
| 6287 | synchronized (sDataSaverStatusTrackerLock) { |
| 6288 | if (sInstance == null) { |
| 6289 | sInstance = new DataSaverStatusTracker(context); |
| 6290 | } |
| 6291 | } |
| 6292 | } |
| 6293 | return sInstance; |
| 6294 | } |
| 6295 | |
| 6296 | private final NetworkPolicyManager mNpm; |
| 6297 | // The value updates on the caller's binder thread or UI thread. |
| 6298 | private final AtomicBoolean mIsDataSaverEnabled; |
| 6299 | |
| 6300 | @VisibleForTesting(visibility = VisibleForTesting.Visibility.PACKAGE) |
| 6301 | public DataSaverStatusTracker(final Context context) { |
| 6302 | // To avoid leaks, take the application context. |
| 6303 | final Context appContext; |
| 6304 | if (context instanceof Application) { |
| 6305 | appContext = context; |
| 6306 | } else { |
| 6307 | appContext = context.getApplicationContext(); |
| 6308 | } |
| 6309 | |
| 6310 | if ((appContext.getApplicationInfo().flags & FLAG_PERSISTENT) == 0 |
| 6311 | && (appContext.getApplicationInfo().flags & FLAG_SYSTEM) == 0) { |
| 6312 | throw new IllegalStateException("Unexpected caller: " |
| 6313 | + appContext.getApplicationInfo().packageName); |
| 6314 | } |
| 6315 | |
| 6316 | mNpm = appContext.getSystemService(NetworkPolicyManager.class); |
| 6317 | final IntentFilter filter = new IntentFilter( |
| 6318 | ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED); |
| 6319 | // The receiver should not receive broadcasts from other Apps. |
| 6320 | appContext.registerReceiver(this, filter, Context.RECEIVER_NOT_EXPORTED); |
| 6321 | mIsDataSaverEnabled = new AtomicBoolean(); |
| 6322 | updateDataSaverEnabled(); |
| 6323 | } |
| 6324 | |
| 6325 | // Runs on caller's UI thread. |
| 6326 | @Override |
| 6327 | public void onReceive(Context context, Intent intent) { |
| 6328 | if (intent.getAction().equals(ConnectivityManager.ACTION_RESTRICT_BACKGROUND_CHANGED)) { |
| 6329 | updateDataSaverEnabled(); |
| 6330 | } else { |
| 6331 | throw new IllegalStateException("Unexpected intent " + intent); |
| 6332 | } |
| 6333 | } |
| 6334 | |
| 6335 | public boolean getDataSaverEnabled() { |
| 6336 | return mIsDataSaverEnabled.get(); |
| 6337 | } |
| 6338 | |
| 6339 | private void updateDataSaverEnabled() { |
| 6340 | // Uid doesn't really matter, but use a fixed UID to make things clearer. |
| 6341 | final int dataSaverForCallerUid = mNpm.getRestrictBackgroundStatus(Process.SYSTEM_UID); |
| 6342 | mIsDataSaverEnabled.set(dataSaverForCallerUid |
| 6343 | != ConnectivityManager.RESTRICT_BACKGROUND_STATUS_DISABLED); |
| 6344 | } |
| 6345 | } |
| 6346 | |
| 6347 | /** |
| 6348 | * Return whether the network is blocked for the given uid and metered condition. |
Junyu Lai | e003152 | 2023-08-29 18:32:57 +0800 | [diff] [blame] | 6349 | * |
| 6350 | * Similar to {@link NetworkPolicyManager#isUidNetworkingBlocked}, but directly reads the BPF |
| 6351 | * maps and therefore considerably faster. For use by the NetworkStack process only. |
| 6352 | * |
| 6353 | * @param uid The target uid. |
Junyu Lai | c3dc5b6 | 2023-09-06 19:10:02 +0800 | [diff] [blame] | 6354 | * @param isNetworkMetered Whether the target network is metered. |
| 6355 | * |
| 6356 | * @return True if all networking with the given condition is blocked. Otherwise, false. |
Junyu Lai | e003152 | 2023-08-29 18:32:57 +0800 | [diff] [blame] | 6357 | * @throws IllegalStateException if the map cannot be opened. |
| 6358 | * @throws ServiceSpecificException if the read fails. |
| 6359 | * @hide |
| 6360 | */ |
| 6361 | // This isn't protected by a standard Android permission since it can't |
| 6362 | // afford to do IPC for performance reasons. Instead, the access control |
| 6363 | // is provided by linux file group permission AID_NET_BW_ACCT and the |
| 6364 | // selinux context fs_bpf_net*. |
| 6365 | // Only the system server process and the network stack have access. |
Junyu Lai | bb59480 | 2023-09-04 11:37:03 +0800 | [diff] [blame] | 6366 | @FlaggedApi(Flags.SUPPORT_IS_UID_NETWORKING_BLOCKED) |
| 6367 | @SystemApi(client = MODULE_LIBRARIES) |
Junyu Lai | e003152 | 2023-08-29 18:32:57 +0800 | [diff] [blame] | 6368 | @RequiresApi(Build.VERSION_CODES.TIRAMISU) // BPF maps were only mainlined in T |
| 6369 | @RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK) |
Junyu Lai | c3dc5b6 | 2023-09-06 19:10:02 +0800 | [diff] [blame] | 6370 | public boolean isUidNetworkingBlocked(int uid, boolean isNetworkMetered) { |
Junyu Lai | e003152 | 2023-08-29 18:32:57 +0800 | [diff] [blame] | 6371 | final BpfNetMapsReader reader = BpfNetMapsReader.getInstance(); |
| 6372 | |
Junyu Lai | c3dc5b6 | 2023-09-06 19:10:02 +0800 | [diff] [blame] | 6373 | final boolean isDataSaverEnabled; |
| 6374 | // TODO: For U-QPR3+ devices, get data saver status from bpf configuration map directly. |
| 6375 | final DataSaverStatusTracker dataSaverStatusTracker = |
| 6376 | DataSaverStatusTracker.getInstance(mContext); |
| 6377 | isDataSaverEnabled = dataSaverStatusTracker.getDataSaverEnabled(); |
Junyu Lai | e003152 | 2023-08-29 18:32:57 +0800 | [diff] [blame] | 6378 | |
Junyu Lai | c3dc5b6 | 2023-09-06 19:10:02 +0800 | [diff] [blame] | 6379 | return reader.isUidNetworkingBlocked(uid, isNetworkMetered, isDataSaverEnabled); |
Junyu Lai | e003152 | 2023-08-29 18:32:57 +0800 | [diff] [blame] | 6380 | } |
| 6381 | |
Igor Chernyshev | 9dac660 | 2022-12-13 19:28:32 -0800 | [diff] [blame] | 6382 | /** @hide */ |
| 6383 | public IBinder getCompanionDeviceManagerProxyService() { |
| 6384 | try { |
| 6385 | return mService.getCompanionDeviceManagerProxyService(); |
| 6386 | } catch (RemoteException e) { |
| 6387 | throw e.rethrowFromSystemServer(); |
| 6388 | } |
| 6389 | } |
Chalard Jean | 2fb66f1 | 2023-08-25 12:50:37 +0900 | [diff] [blame] | 6390 | |
| 6391 | private static final Object sRoutingCoordinatorManagerLock = new Object(); |
| 6392 | @GuardedBy("sRoutingCoordinatorManagerLock") |
| 6393 | private static RoutingCoordinatorManager sRoutingCoordinatorManager = null; |
| 6394 | /** @hide */ |
| 6395 | @RequiresApi(Build.VERSION_CODES.S) |
| 6396 | public RoutingCoordinatorManager getRoutingCoordinatorManager() { |
| 6397 | try { |
| 6398 | synchronized (sRoutingCoordinatorManagerLock) { |
| 6399 | if (null == sRoutingCoordinatorManager) { |
| 6400 | sRoutingCoordinatorManager = new RoutingCoordinatorManager(mContext, |
| 6401 | IRoutingCoordinator.Stub.asInterface( |
| 6402 | mService.getRoutingCoordinatorService())); |
| 6403 | } |
| 6404 | return sRoutingCoordinatorManager; |
| 6405 | } |
| 6406 | } catch (RemoteException e) { |
| 6407 | throw e.rethrowFromSystemServer(); |
| 6408 | } |
| 6409 | } |
Remi NGUYEN VAN | fbbccbc | 2021-01-15 18:08:24 +0900 | [diff] [blame] | 6410 | } |