The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 16 | package android.net; |
| 17 | |
Junyu Lai | a62493f | 2021-01-19 11:10:56 +0000 | [diff] [blame] | 18 | import static android.annotation.SystemApi.Client.MODULE_LIBRARIES; |
paulhu | 56e09df | 2021-03-17 20:30:33 +0800 | [diff] [blame] | 19 | import static android.net.ConnectivitySettingsManager.PRIVATE_DNS_DEFAULT_MODE; |
| 20 | import static android.net.ConnectivitySettingsManager.PRIVATE_DNS_MODE; |
Junyu Lai | a62493f | 2021-01-19 11:10:56 +0000 | [diff] [blame] | 21 | import static android.net.NetworkRequest.Type.BACKGROUND_REQUEST; |
junyulai | ad01079 | 2021-01-11 16:53:38 +0800 | [diff] [blame] | 22 | import static android.net.NetworkRequest.Type.LISTEN; |
junyulai | 1b1c874 | 2021-03-12 20:05:08 +0800 | [diff] [blame] | 23 | import static android.net.NetworkRequest.Type.LISTEN_FOR_BEST; |
junyulai | ad01079 | 2021-01-11 16:53:38 +0800 | [diff] [blame] | 24 | import static android.net.NetworkRequest.Type.REQUEST; |
| 25 | import static android.net.NetworkRequest.Type.TRACK_DEFAULT; |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 26 | import static android.net.NetworkRequest.Type.TRACK_SYSTEM_DEFAULT; |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 27 | import static android.net.QosCallback.QosCallbackRegistrationException; |
junyulai | d05a192 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 28 | |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 29 | import android.annotation.CallbackExecutor; |
Felipe Leme | 3051135 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 30 | import android.annotation.IntDef; |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 31 | import android.annotation.NonNull; |
Robin Lee | e5d5ed5 | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 32 | import android.annotation.Nullable; |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 33 | import android.annotation.RequiresPermission; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 34 | import android.annotation.SdkConstant; |
| 35 | import android.annotation.SdkConstant.SdkConstantType; |
lucaslin | 705c333 | 2021-03-12 17:56:09 +0800 | [diff] [blame] | 36 | import android.annotation.StringDef; |
Junyu Lai | a62493f | 2021-01-19 11:10:56 +0000 | [diff] [blame] | 37 | import android.annotation.SuppressLint; |
Udam Saini | cd64546 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 38 | import android.annotation.SystemApi; |
Jeff Sharkey | 9b39e9a | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 39 | import android.annotation.SystemService; |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 40 | import android.app.PendingIntent; |
Lorenzo Colitti | 79c6f22 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 41 | import android.app.admin.DevicePolicyManager; |
Artur Satayev | 9c2add6 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 42 | import android.compat.annotation.UnsupportedAppUsage; |
Lorenzo Colitti | 79c6f22 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 43 | import android.content.ComponentName; |
lucaslin | 705c333 | 2021-03-12 17:56:09 +0800 | [diff] [blame] | 44 | import android.content.ContentResolver; |
Jeff Sharkey | 2d9e5a5 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 45 | import android.content.Context; |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 46 | import android.content.Intent; |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 47 | import android.net.IpSecManager.UdpEncapsulationSocket; |
| 48 | import android.net.SocketKeepalive.Callback; |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 49 | import android.net.TetheringManager.StartTetheringCallback; |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 50 | import android.net.TetheringManager.TetheringEventCallback; |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 51 | import android.net.TetheringManager.TetheringRequest; |
Roshan Pius | 951c003 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 52 | import android.net.wifi.WifiNetworkSuggestion; |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 53 | import android.os.Binder; |
Mathew Inwood | bdfc1fc | 2018-12-20 15:30:45 +0000 | [diff] [blame] | 54 | import android.os.Build; |
Jeff Sharkey | 39c01eb | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 55 | import android.os.Build.VERSION_CODES; |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 56 | import android.os.Bundle; |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 57 | import android.os.Handler; |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 58 | import android.os.IBinder; |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 59 | import android.os.Looper; |
| 60 | import android.os.Message; |
Robert Greenwalt | 15a4153 | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 61 | import android.os.Messenger; |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 62 | import android.os.ParcelFileDescriptor; |
Cody Kesting | f53a075 | 2020-04-15 12:33:28 -0700 | [diff] [blame] | 63 | import android.os.PersistableBundle; |
Lorenzo Colitti | 2780e4b | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 64 | import android.os.Process; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | import android.os.RemoteException; |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 66 | import android.os.ResultReceiver; |
Hugo Benichi | a590ab8 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 67 | import android.os.ServiceSpecificException; |
Chalard Jean | fa45a68 | 2021-02-25 17:23:40 +0900 | [diff] [blame] | 68 | import android.os.UserHandle; |
Jeff Sharkey | 971cd16 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 69 | import android.provider.Settings; |
Wink Saville | 689f704 | 2014-12-05 11:10:30 -0800 | [diff] [blame] | 70 | import android.telephony.SubscriptionManager; |
Meng Wang | ed6f441 | 2019-11-18 17:10:00 -0800 | [diff] [blame] | 71 | import android.telephony.TelephonyManager; |
lucaslin | 705c333 | 2021-03-12 17:56:09 +0800 | [diff] [blame] | 72 | import android.text.TextUtils; |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 73 | import android.util.ArrayMap; |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 74 | import android.util.Log; |
Lorenzo Colitti | 3f54f10 | 2020-12-12 00:51:11 +0900 | [diff] [blame] | 75 | import android.util.Range; |
Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 76 | import android.util.SparseIntArray; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 77 | |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 78 | import com.android.internal.annotations.GuardedBy; |
Robert Greenwalt | 0c150c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 79 | |
Paul Jensen | 3e2917c | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 80 | import libcore.net.event.NetworkEventDispatcher; |
| 81 | |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 82 | import java.io.IOException; |
| 83 | import java.io.UncheckedIOException; |
Felipe Leme | 3051135 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 84 | import java.lang.annotation.Retention; |
| 85 | import java.lang.annotation.RetentionPolicy; |
Lorenzo Colitti | 3f54f10 | 2020-12-12 00:51:11 +0900 | [diff] [blame] | 86 | import java.net.DatagramSocket; |
Jeremy Klein | 434835a | 2015-12-28 15:11:58 -0800 | [diff] [blame] | 87 | import java.net.InetAddress; |
Jeff Vander Stoep | 39a51e0 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 88 | import java.net.InetSocketAddress; |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 89 | import java.net.Socket; |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 90 | import java.util.ArrayList; |
Lorenzo Colitti | 3f54f10 | 2020-12-12 00:51:11 +0900 | [diff] [blame] | 91 | import java.util.Collection; |
Jeremy Klein | 434835a | 2015-12-28 15:11:58 -0800 | [diff] [blame] | 92 | import java.util.HashMap; |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 93 | import java.util.List; |
| 94 | import java.util.Map; |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 95 | import java.util.Objects; |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 96 | import java.util.concurrent.Executor; |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 97 | import java.util.concurrent.ExecutorService; |
| 98 | import java.util.concurrent.Executors; |
| 99 | import java.util.concurrent.RejectedExecutionException; |
Jeremy Klein | 434835a | 2015-12-28 15:11:58 -0800 | [diff] [blame] | 100 | |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 101 | /** |
| 102 | * Class that answers queries about the state of network connectivity. It also |
Jeff Sharkey | 9b39e9a | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 103 | * notifies applications when network connectivity changes. |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 104 | * <p> |
| 105 | * The primary responsibilities of this class are to: |
| 106 | * <ol> |
| 107 | * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li> |
| 108 | * <li>Send broadcast intents when network connectivity changes</li> |
| 109 | * <li>Attempt to "fail over" to another network when connectivity to a network |
| 110 | * is lost</li> |
| 111 | * <li>Provide an API that allows applications to query the coarse-grained or fine-grained |
| 112 | * state of the available networks</li> |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 113 | * <li>Provide an API that allows applications to request and select networks for their data |
| 114 | * traffic</li> |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 115 | * </ol> |
| 116 | */ |
Jeff Sharkey | 9b39e9a | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 117 | @SystemService(Context.CONNECTIVITY_SERVICE) |
Jeff Sharkey | 921ebf2 | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 118 | public class ConnectivityManager { |
| 119 | private static final String TAG = "ConnectivityManager"; |
Soi, Yoshinari | b8102ef | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 120 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Jeff Sharkey | 921ebf2 | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 121 | |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | /** |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 123 | * A change in network connectivity has occurred. A default connection has either |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 124 | * been established or lost. The NetworkInfo for the affected network is |
| 125 | * sent as an extra; it should be consulted to see what kind of |
| 126 | * connectivity event occurred. |
| 127 | * <p/> |
Mark Lu | 3e422ac | 2016-12-05 10:57:55 -0800 | [diff] [blame] | 128 | * Apps targeting Android 7.0 (API level 24) and higher do not receive this |
| 129 | * broadcast if they declare the broadcast receiver in their manifest. Apps |
| 130 | * will still receive broadcasts if they register their |
| 131 | * {@link android.content.BroadcastReceiver} with |
| 132 | * {@link android.content.Context#registerReceiver Context.registerReceiver()} |
| 133 | * and that context is still valid. |
| 134 | * <p/> |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | * If this is a connection that was the result of failing over from a |
| 136 | * disconnected network, then the FAILOVER_CONNECTION boolean extra is |
| 137 | * set to true. |
| 138 | * <p/> |
| 139 | * For a loss of connectivity, if the connectivity manager is attempting |
| 140 | * to connect (or has already connected) to another network, the |
| 141 | * NetworkInfo for the new network is also passed as an extra. This lets |
| 142 | * any receivers of the broadcast know that they should not necessarily |
| 143 | * tell the user that no data traffic will be possible. Instead, the |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 144 | * receiver should expect another broadcast soon, indicating either that |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | * the failover attempt succeeded (and so there is still overall data |
| 146 | * connectivity), or that the failover attempt failed, meaning that all |
| 147 | * connectivity has been lost. |
| 148 | * <p/> |
| 149 | * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY |
| 150 | * is set to {@code true} if there are no connected networks at all. |
Chalard Jean | 52e2396 | 2018-02-10 05:33:50 +0900 | [diff] [blame] | 151 | * |
| 152 | * @deprecated apps should use the more versatile {@link #requestNetwork}, |
| 153 | * {@link #registerNetworkCallback} or {@link #registerDefaultNetworkCallback} |
| 154 | * functions instead for faster and more detailed updates about the network |
| 155 | * changes they care about. |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 156 | */ |
Jeff Sharkey | ee9275b | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 157 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Chalard Jean | 52e2396 | 2018-02-10 05:33:50 +0900 | [diff] [blame] | 158 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; |
Jeff Sharkey | 66fa968 | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 160 | |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | /** |
Paul Jensen | 60df4aa | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 162 | * The device has connected to a network that has presented a captive |
| 163 | * portal, which is blocking Internet connectivity. The user was presented |
| 164 | * with a notification that network sign in is required, |
| 165 | * and the user invoked the notification's action indicating they |
Paul Jensen | 75e0adb | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 166 | * desire to sign in to the network. Apps handling this activity should |
Paul Jensen | 60df4aa | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 167 | * facilitate signing in to the network. This action includes a |
| 168 | * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents |
| 169 | * the network presenting the captive portal; all communication with the |
| 170 | * captive portal must be done using this {@code Network} object. |
| 171 | * <p/> |
Paul Jensen | 75e0adb | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 172 | * This activity includes a {@link CaptivePortal} extra named |
| 173 | * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different |
| 174 | * outcomes of the captive portal sign in to the system: |
| 175 | * <ul> |
| 176 | * <li> When the app handling this action believes the user has signed in to |
| 177 | * the network and the captive portal has been dismissed, the app should |
| 178 | * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can |
| 179 | * reevaluate the network. If reevaluation finds the network no longer |
| 180 | * subject to a captive portal, the network may become the default active |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 181 | * data network.</li> |
Paul Jensen | 75e0adb | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 182 | * <li> When the app handling this action believes the user explicitly wants |
Paul Jensen | 60df4aa | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 183 | * to ignore the captive portal and the network, the app should call |
Paul Jensen | 75e0adb | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 184 | * {@link CaptivePortal#ignoreNetwork}. </li> |
| 185 | * </ul> |
Paul Jensen | 60df4aa | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 186 | */ |
| 187 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 188 | public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL"; |
| 189 | |
| 190 | /** |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 191 | * The lookup key for a {@link NetworkInfo} object. Retrieve with |
| 192 | * {@link android.content.Intent#getParcelableExtra(String)}. |
Jeff Sharkey | 66fa968 | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 193 | * |
Chalard Jean | 97021a1 | 2019-01-11 16:47:53 +0900 | [diff] [blame] | 194 | * @deprecated The {@link NetworkInfo} object is deprecated, as many of its properties |
| 195 | * can't accurately represent modern network characteristics. |
| 196 | * Please obtain information about networks from the {@link NetworkCapabilities} |
| 197 | * or {@link LinkProperties} objects instead. |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | */ |
Jeff Sharkey | 66fa968 | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 199 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | public static final String EXTRA_NETWORK_INFO = "networkInfo"; |
Jeff Sharkey | 66fa968 | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 201 | |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | /** |
Jeff Sharkey | 47905d1 | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 203 | * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast. |
Jeff Sharkey | 47905d1 | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 204 | * |
| 205 | * @see android.content.Intent#getIntExtra(String, int) |
Chalard Jean | 97021a1 | 2019-01-11 16:47:53 +0900 | [diff] [blame] | 206 | * @deprecated The network type is not rich enough to represent the characteristics |
| 207 | * of modern networks. Please use {@link NetworkCapabilities} instead, |
| 208 | * in particular the transports. |
Jeff Sharkey | 47905d1 | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 209 | */ |
Chalard Jean | 97021a1 | 2019-01-11 16:47:53 +0900 | [diff] [blame] | 210 | @Deprecated |
Jeff Sharkey | 47905d1 | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 211 | public static final String EXTRA_NETWORK_TYPE = "networkType"; |
| 212 | |
| 213 | /** |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 214 | * The lookup key for a boolean that indicates whether a connect event |
| 215 | * is for a network to which the connectivity manager was failing over |
| 216 | * following a disconnect on another network. |
| 217 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
junyulai | 9826e7f | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 218 | * |
| 219 | * @deprecated See {@link NetworkInfo}. |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 220 | */ |
junyulai | 9826e7f | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 221 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | public static final String EXTRA_IS_FAILOVER = "isFailover"; |
| 223 | /** |
| 224 | * The lookup key for a {@link NetworkInfo} object. This is supplied when |
| 225 | * there is another network that it may be possible to connect to. Retrieve with |
| 226 | * {@link android.content.Intent#getParcelableExtra(String)}. |
junyulai | 9826e7f | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 227 | * |
| 228 | * @deprecated See {@link NetworkInfo}. |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 229 | */ |
junyulai | 9826e7f | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 230 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork"; |
| 232 | /** |
| 233 | * The lookup key for a boolean that indicates whether there is a |
| 234 | * complete lack of connectivity, i.e., no network is available. |
| 235 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
| 236 | */ |
| 237 | public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity"; |
| 238 | /** |
| 239 | * The lookup key for a string that indicates why an attempt to connect |
| 240 | * to a network failed. The string has no particular structure. It is |
| 241 | * intended to be used in notifications presented to users. Retrieve |
| 242 | * it with {@link android.content.Intent#getStringExtra(String)}. |
| 243 | */ |
| 244 | public static final String EXTRA_REASON = "reason"; |
| 245 | /** |
| 246 | * The lookup key for a string that provides optionally supplied |
| 247 | * extra information about the network state. The information |
| 248 | * may be passed up from the lower networking layers, and its |
| 249 | * meaning may be specific to a particular network type. Retrieve |
| 250 | * it with {@link android.content.Intent#getStringExtra(String)}. |
junyulai | 9826e7f | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 251 | * |
| 252 | * @deprecated See {@link NetworkInfo#getExtraInfo()}. |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 253 | */ |
junyulai | 9826e7f | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 254 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | public static final String EXTRA_EXTRA_INFO = "extraInfo"; |
Robert Greenwalt | 986c741 | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 256 | /** |
| 257 | * The lookup key for an int that provides information about |
| 258 | * our connection to the internet at large. 0 indicates no connection, |
| 259 | * 100 indicates a great connection. Retrieve it with |
Jeff Sharkey | 921ebf2 | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 260 | * {@link android.content.Intent#getIntExtra(String, int)}. |
Robert Greenwalt | 986c741 | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 261 | * {@hide} |
| 262 | */ |
| 263 | public static final String EXTRA_INET_CONDITION = "inetCondition"; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 264 | /** |
Paul Jensen | 75e0adb | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 265 | * The lookup key for a {@link CaptivePortal} object included with the |
| 266 | * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal} |
| 267 | * object can be used to either indicate to the system that the captive |
| 268 | * portal has been dismissed or that the user does not want to pursue |
| 269 | * signing in to captive portal. Retrieve it with |
| 270 | * {@link android.content.Intent#getParcelableExtra(String)}. |
Paul Jensen | 60df4aa | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 271 | */ |
Paul Jensen | 75e0adb | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 272 | public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL"; |
Jan Nordqvist | 032e267 | 2015-09-22 15:54:32 -0700 | [diff] [blame] | 273 | |
| 274 | /** |
| 275 | * Key for passing a URL to the captive portal login activity. |
| 276 | */ |
| 277 | public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL"; |
| 278 | |
Paul Jensen | 60df4aa | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 279 | /** |
Remi NGUYEN VAN | c249157 | 2018-05-22 10:01:53 +0900 | [diff] [blame] | 280 | * Key for passing a {@link android.net.captiveportal.CaptivePortalProbeSpec} to the captive |
| 281 | * portal login activity. |
| 282 | * {@hide} |
| 283 | */ |
Remi NGUYEN VAN | 70ab67f | 2019-01-17 14:38:31 +0900 | [diff] [blame] | 284 | @SystemApi |
Remi NGUYEN VAN | c249157 | 2018-05-22 10:01:53 +0900 | [diff] [blame] | 285 | public static final String EXTRA_CAPTIVE_PORTAL_PROBE_SPEC = |
| 286 | "android.net.extra.CAPTIVE_PORTAL_PROBE_SPEC"; |
| 287 | |
| 288 | /** |
Hugo Benichi | 454e066 | 2016-12-14 08:23:40 +0900 | [diff] [blame] | 289 | * Key for passing a user agent string to the captive portal login activity. |
| 290 | * {@hide} |
| 291 | */ |
Remi NGUYEN VAN | 70ab67f | 2019-01-17 14:38:31 +0900 | [diff] [blame] | 292 | @SystemApi |
Hugo Benichi | 454e066 | 2016-12-14 08:23:40 +0900 | [diff] [blame] | 293 | public static final String EXTRA_CAPTIVE_PORTAL_USER_AGENT = |
| 294 | "android.net.extra.CAPTIVE_PORTAL_USER_AGENT"; |
| 295 | |
| 296 | /** |
Haoyu Bai | b5da575 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 297 | * Broadcast action to indicate the change of data activity status |
| 298 | * (idle or active) on a network in a recent period. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 299 | * The network becomes active when data transmission is started, or |
| 300 | * idle if there is no data transmission for a period of time. |
Haoyu Bai | b5da575 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 301 | * {@hide} |
| 302 | */ |
| 303 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 304 | public static final String ACTION_DATA_ACTIVITY_CHANGE = |
| 305 | "android.net.conn.DATA_ACTIVITY_CHANGE"; |
Haoyu Bai | b5da575 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 306 | /** |
| 307 | * The lookup key for an enum that indicates the network device type on which this data activity |
| 308 | * change happens. |
| 309 | * {@hide} |
| 310 | */ |
| 311 | public static final String EXTRA_DEVICE_TYPE = "deviceType"; |
| 312 | /** |
| 313 | * The lookup key for a boolean that indicates the device is active or not. {@code true} means |
| 314 | * it is actively sending or receiving data and {@code false} means it is idle. |
| 315 | * {@hide} |
| 316 | */ |
| 317 | public static final String EXTRA_IS_ACTIVE = "isActive"; |
Ashish Sharma | 29f7e0e | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 318 | /** |
| 319 | * The lookup key for a long that contains the timestamp (nanos) of the radio state change. |
| 320 | * {@hide} |
| 321 | */ |
| 322 | public static final String EXTRA_REALTIME_NS = "tsNanos"; |
Haoyu Bai | b5da575 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 323 | |
| 324 | /** |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | * Broadcast Action: The setting for background data usage has changed |
| 326 | * values. Use {@link #getBackgroundDataSetting()} to get the current value. |
| 327 | * <p> |
| 328 | * If an application uses the network in the background, it should listen |
| 329 | * for this broadcast and stop using the background data if the value is |
Jeff Sharkey | 921ebf2 | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 330 | * {@code false}. |
Jeff Sharkey | c958c77 | 2012-01-30 16:29:24 -0800 | [diff] [blame] | 331 | * <p> |
| 332 | * |
| 333 | * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability |
| 334 | * of background data depends on several combined factors, and |
| 335 | * this broadcast is no longer sent. Instead, when background |
| 336 | * data is unavailable, {@link #getActiveNetworkInfo()} will now |
| 337 | * appear disconnected. During first boot after a platform |
| 338 | * upgrade, this broadcast will be sent once if |
| 339 | * {@link #getBackgroundDataSetting()} was {@code false} before |
| 340 | * the upgrade. |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 341 | */ |
| 342 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Jeff Sharkey | c958c77 | 2012-01-30 16:29:24 -0800 | [diff] [blame] | 343 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 344 | public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED = |
| 345 | "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED"; |
| 346 | |
Robert Greenwalt | d3401f9 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 347 | /** |
| 348 | * Broadcast Action: The network connection may not be good |
| 349 | * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and |
| 350 | * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify |
| 351 | * the network and it's condition. |
| 352 | * @hide |
| 353 | */ |
Jeff Sharkey | ee9275b | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 354 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 355 | @UnsupportedAppUsage |
Robert Greenwalt | d3401f9 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 356 | public static final String INET_CONDITION_ACTION = |
| 357 | "android.net.conn.INET_CONDITION_ACTION"; |
| 358 | |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 359 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 360 | * Broadcast Action: A tetherable connection has come or gone. |
| 361 | * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER}, |
Erik Kline | 762fa8e | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 362 | * {@code ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY}, |
| 363 | * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER}, and |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 364 | * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate |
| 365 | * the current state of tethering. Each include a list of |
| 366 | * interface names in that state (may be empty). |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 367 | * @hide |
| 368 | */ |
Jeff Sharkey | ee9275b | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 369 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 370 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 371 | public static final String ACTION_TETHER_STATE_CHANGED = |
markchien | a0f8fd9 | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 372 | TetheringManager.ACTION_TETHER_STATE_CHANGED; |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 373 | |
| 374 | /** |
| 375 | * @hide |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 376 | * gives a String[] listing all the interfaces configured for |
| 377 | * tethering and currently available for tethering. |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 378 | */ |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 379 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
markchien | a0f8fd9 | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 380 | public static final String EXTRA_AVAILABLE_TETHER = TetheringManager.EXTRA_AVAILABLE_TETHER; |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 381 | |
| 382 | /** |
| 383 | * @hide |
Erik Kline | 762fa8e | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 384 | * gives a String[] listing all the interfaces currently in local-only |
| 385 | * mode (ie, has DHCPv4+IPv6-ULA support and no packet forwarding) |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 386 | */ |
markchien | a0f8fd9 | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 387 | public static final String EXTRA_ACTIVE_LOCAL_ONLY = TetheringManager.EXTRA_ACTIVE_LOCAL_ONLY; |
Erik Kline | 762fa8e | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 388 | |
| 389 | /** |
| 390 | * @hide |
| 391 | * gives a String[] listing all the interfaces currently tethered |
| 392 | * (ie, has DHCPv4 support and packets potentially forwarded/NATed) |
| 393 | */ |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 394 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
markchien | a0f8fd9 | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 395 | public static final String EXTRA_ACTIVE_TETHER = TetheringManager.EXTRA_ACTIVE_TETHER; |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 396 | |
| 397 | /** |
| 398 | * @hide |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 399 | * gives a String[] listing all the interfaces we tried to tether and |
| 400 | * failed. Use {@link #getLastTetherError} to find the error code |
| 401 | * for any interfaces listed here. |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 402 | */ |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 403 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
markchien | a0f8fd9 | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 404 | public static final String EXTRA_ERRORED_TETHER = TetheringManager.EXTRA_ERRORED_TETHER; |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 405 | |
| 406 | /** |
Russell Brenner | 30fe264 | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 407 | * Broadcast Action: The captive portal tracker has finished its test. |
| 408 | * Sent only while running Setup Wizard, in lieu of showing a user |
| 409 | * notification. |
| 410 | * @hide |
| 411 | */ |
Jeff Sharkey | ee9275b | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 412 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Russell Brenner | 30fe264 | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 413 | public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED = |
| 414 | "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED"; |
| 415 | /** |
| 416 | * The lookup key for a boolean that indicates whether a captive portal was detected. |
| 417 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
| 418 | * @hide |
| 419 | */ |
| 420 | public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal"; |
| 421 | |
| 422 | /** |
Lorenzo Colitti | 6947c06 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 423 | * Action used to display a dialog that asks the user whether to connect to a network that is |
| 424 | * not validated. This intent is used to start the dialog in settings via startActivity. |
| 425 | * |
| 426 | * @hide |
| 427 | */ |
| 428 | public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED"; |
| 429 | |
| 430 | /** |
Lorenzo Colitti | 199ecfc | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 431 | * Action used to display a dialog that asks the user whether to avoid a network that is no |
| 432 | * longer validated. This intent is used to start the dialog in settings via startActivity. |
| 433 | * |
| 434 | * @hide |
| 435 | */ |
| 436 | public static final String ACTION_PROMPT_LOST_VALIDATION = |
| 437 | "android.net.conn.PROMPT_LOST_VALIDATION"; |
| 438 | |
| 439 | /** |
lucaslin | 2240ef6 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 440 | * Action used to display a dialog that asks the user whether to stay connected to a network |
| 441 | * that has not validated. This intent is used to start the dialog in settings via |
| 442 | * startActivity. |
| 443 | * |
| 444 | * @hide |
| 445 | */ |
| 446 | public static final String ACTION_PROMPT_PARTIAL_CONNECTIVITY = |
| 447 | "android.net.conn.PROMPT_PARTIAL_CONNECTIVITY"; |
| 448 | |
| 449 | /** |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 450 | * Invalid tethering type. |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 451 | * @see #startTethering(int, boolean, OnStartTetheringCallback) |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 452 | * @hide |
| 453 | */ |
markchien | a0f8fd9 | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 454 | public static final int TETHERING_INVALID = TetheringManager.TETHERING_INVALID; |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 455 | |
| 456 | /** |
| 457 | * Wifi tethering type. |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 458 | * @see #startTethering(int, boolean, OnStartTetheringCallback) |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 459 | * @hide |
| 460 | */ |
| 461 | @SystemApi |
Remi NGUYEN VAN | 09f8ed2 | 2021-02-15 18:52:06 +0900 | [diff] [blame] | 462 | public static final int TETHERING_WIFI = 0; |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 463 | |
| 464 | /** |
| 465 | * USB tethering type. |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 466 | * @see #startTethering(int, boolean, OnStartTetheringCallback) |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 467 | * @hide |
| 468 | */ |
| 469 | @SystemApi |
Remi NGUYEN VAN | 09f8ed2 | 2021-02-15 18:52:06 +0900 | [diff] [blame] | 470 | public static final int TETHERING_USB = 1; |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 471 | |
| 472 | /** |
| 473 | * Bluetooth tethering type. |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 474 | * @see #startTethering(int, boolean, OnStartTetheringCallback) |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 475 | * @hide |
| 476 | */ |
| 477 | @SystemApi |
Remi NGUYEN VAN | 09f8ed2 | 2021-02-15 18:52:06 +0900 | [diff] [blame] | 478 | public static final int TETHERING_BLUETOOTH = 2; |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 479 | |
| 480 | /** |
Jimmy Chen | dd31bc4 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 481 | * Wifi P2p tethering type. |
| 482 | * Wifi P2p tethering is set through events automatically, and don't |
| 483 | * need to start from #startTethering(int, boolean, OnStartTetheringCallback). |
| 484 | * @hide |
| 485 | */ |
markchien | a0f8fd9 | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 486 | public static final int TETHERING_WIFI_P2P = TetheringManager.TETHERING_WIFI_P2P; |
Jimmy Chen | dd31bc4 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 487 | |
| 488 | /** |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 489 | * Extra used for communicating with the TetherService. Includes the type of tethering to |
| 490 | * enable if any. |
| 491 | * @hide |
| 492 | */ |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 493 | public static final String EXTRA_ADD_TETHER_TYPE = TetheringConstants.EXTRA_ADD_TETHER_TYPE; |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 494 | |
| 495 | /** |
| 496 | * Extra used for communicating with the TetherService. Includes the type of tethering for |
| 497 | * which to cancel provisioning. |
| 498 | * @hide |
| 499 | */ |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 500 | public static final String EXTRA_REM_TETHER_TYPE = TetheringConstants.EXTRA_REM_TETHER_TYPE; |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 501 | |
| 502 | /** |
| 503 | * Extra used for communicating with the TetherService. True to schedule a recheck of tether |
| 504 | * provisioning. |
| 505 | * @hide |
| 506 | */ |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 507 | public static final String EXTRA_SET_ALARM = TetheringConstants.EXTRA_SET_ALARM; |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 508 | |
| 509 | /** |
| 510 | * Tells the TetherService to run a provision check now. |
| 511 | * @hide |
| 512 | */ |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 513 | public static final String EXTRA_RUN_PROVISION = TetheringConstants.EXTRA_RUN_PROVISION; |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 514 | |
| 515 | /** |
| 516 | * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver} |
| 517 | * which will receive provisioning results. Can be left empty. |
| 518 | * @hide |
| 519 | */ |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 520 | public static final String EXTRA_PROVISION_CALLBACK = |
| 521 | TetheringConstants.EXTRA_PROVISION_CALLBACK; |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 522 | |
| 523 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 524 | * The absence of a connection type. |
Robert Greenwalt | 33cdcdf | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 525 | * @hide |
| 526 | */ |
paulhu | 74357e7 | 2020-01-13 16:46:45 +0800 | [diff] [blame] | 527 | @SystemApi |
Robert Greenwalt | 33cdcdf | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 528 | public static final int TYPE_NONE = -1; |
| 529 | |
| 530 | /** |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 531 | * A Mobile data connection. Devices may support more than one. |
| 532 | * |
| 533 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 534 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
| 535 | * appropriate network. {@see NetworkCapabilities} for supported transports. |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 536 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 537 | @Deprecated |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 538 | public static final int TYPE_MOBILE = 0; |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 539 | |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 540 | /** |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 541 | * A WIFI data connection. Devices may support more than one. |
| 542 | * |
| 543 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 544 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
| 545 | * appropriate network. {@see NetworkCapabilities} for supported transports. |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 546 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 547 | @Deprecated |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 548 | public static final int TYPE_WIFI = 1; |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 549 | |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 550 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 551 | * An MMS-specific Mobile data connection. This network type may use the |
| 552 | * same network interface as {@link #TYPE_MOBILE} or it may use a different |
| 553 | * one. This is used by applications needing to talk to the carrier's |
| 554 | * Multimedia Messaging Service servers. |
Lorenzo Colitti | 21e9a15 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 555 | * |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 556 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or |
Lorenzo Colitti | 7f6a2bf | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 557 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that |
Lorenzo Colitti | 21e9a15 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 558 | * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability. |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 559 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 560 | @Deprecated |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 561 | public static final int TYPE_MOBILE_MMS = 2; |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 562 | |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 563 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 564 | * A SUPL-specific Mobile data connection. This network type may use the |
| 565 | * same network interface as {@link #TYPE_MOBILE} or it may use a different |
| 566 | * one. This is used by applications needing to talk to the carrier's |
| 567 | * Secure User Plane Location servers for help locating the device. |
Lorenzo Colitti | 21e9a15 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 568 | * |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 569 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or |
Lorenzo Colitti | 7f6a2bf | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 570 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that |
Lorenzo Colitti | 21e9a15 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 571 | * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability. |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 572 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 573 | @Deprecated |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 574 | public static final int TYPE_MOBILE_SUPL = 3; |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 575 | |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 576 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 577 | * A DUN-specific Mobile data connection. This network type may use the |
| 578 | * same network interface as {@link #TYPE_MOBILE} or it may use a different |
| 579 | * one. This is sometimes by the system when setting up an upstream connection |
| 580 | * for tethering so that the carrier is aware of DUN traffic. |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 581 | * |
| 582 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or |
| 583 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that |
| 584 | * provides the {@link NetworkCapabilities#NET_CAPABILITY_DUN} capability. |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 585 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 586 | @Deprecated |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 587 | public static final int TYPE_MOBILE_DUN = 4; |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 588 | |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 589 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 590 | * A High Priority Mobile data connection. This network type uses the |
| 591 | * same network interface as {@link #TYPE_MOBILE} but the routing setup |
Lorenzo Colitti | 21e9a15 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 592 | * is different. |
| 593 | * |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 594 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 595 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
| 596 | * appropriate network. {@see NetworkCapabilities} for supported transports. |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 597 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 598 | @Deprecated |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 599 | public static final int TYPE_MOBILE_HIPRI = 5; |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 600 | |
jsh | bfa8172 | 2010-03-11 15:04:43 -0800 | [diff] [blame] | 601 | /** |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 602 | * A WiMAX data connection. |
| 603 | * |
| 604 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 605 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
| 606 | * appropriate network. {@see NetworkCapabilities} for supported transports. |
jsh | bfa8172 | 2010-03-11 15:04:43 -0800 | [diff] [blame] | 607 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 608 | @Deprecated |
jsh | bfa8172 | 2010-03-11 15:04:43 -0800 | [diff] [blame] | 609 | public static final int TYPE_WIMAX = 6; |
Robert Greenwalt | eb123ac | 2010-12-06 13:56:24 -0800 | [diff] [blame] | 610 | |
Jaikumar Ganesh | 0db51a0 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 611 | /** |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 612 | * A Bluetooth data connection. |
| 613 | * |
| 614 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 615 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
| 616 | * appropriate network. {@see NetworkCapabilities} for supported transports. |
Jaikumar Ganesh | 0db51a0 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 617 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 618 | @Deprecated |
Jaikumar Ganesh | 0db51a0 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 619 | public static final int TYPE_BLUETOOTH = 7; |
| 620 | |
Robert Greenwalt | f76c55d | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 621 | /** |
Chiachang Wang | 3b9549f | 2020-07-28 13:53:09 +0800 | [diff] [blame] | 622 | * Fake data connection. This should not be used on shipping devices. |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 623 | * @deprecated This is not used any more. |
Robert Greenwalt | f76c55d | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 624 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 625 | @Deprecated |
Jaikumar Ganesh | 0db51a0 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 626 | public static final int TYPE_DUMMY = 8; |
Wink Saville | b7c92c7 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 627 | |
Robert Greenwalt | f76c55d | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 628 | /** |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 629 | * An Ethernet data connection. |
| 630 | * |
| 631 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 632 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
| 633 | * appropriate network. {@see NetworkCapabilities} for supported transports. |
Robert Greenwalt | f76c55d | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 634 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 635 | @Deprecated |
Robert Greenwalt | 9d07781 | 2011-01-28 14:48:37 -0800 | [diff] [blame] | 636 | public static final int TYPE_ETHERNET = 9; |
Robert Greenwalt | f76c55d | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 637 | |
Wink Saville | b7c92c7 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 638 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 639 | * Over the air Administration. |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 640 | * @deprecated Use {@link NetworkCapabilities} instead. |
Wink Saville | b7c92c7 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 641 | * {@hide} |
| 642 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 643 | @Deprecated |
Chalard Jean | a3b7751 | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 644 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Wink Saville | b7c92c7 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 645 | public static final int TYPE_MOBILE_FOTA = 10; |
| 646 | |
| 647 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 648 | * IP Multimedia Subsystem. |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 649 | * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IMS} instead. |
Wink Saville | b7c92c7 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 650 | * {@hide} |
| 651 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 652 | @Deprecated |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 653 | @UnsupportedAppUsage |
Wink Saville | b7c92c7 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 654 | public static final int TYPE_MOBILE_IMS = 11; |
| 655 | |
| 656 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 657 | * Carrier Branded Services. |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 658 | * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_CBS} instead. |
Wink Saville | b7c92c7 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 659 | * {@hide} |
| 660 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 661 | @Deprecated |
Chalard Jean | a3b7751 | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 662 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Wink Saville | b7c92c7 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 663 | public static final int TYPE_MOBILE_CBS = 12; |
| 664 | |
repo sync | f5de557 | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 665 | /** |
| 666 | * A Wi-Fi p2p connection. Only requesting processes will have access to |
| 667 | * the peers connected. |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 668 | * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_WIFI_P2P} instead. |
repo sync | f5de557 | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 669 | * {@hide} |
| 670 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 671 | @Deprecated |
paulhu | e102b0b | 2020-01-15 15:38:23 +0800 | [diff] [blame] | 672 | @SystemApi |
repo sync | f5de557 | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 673 | public static final int TYPE_WIFI_P2P = 13; |
Wink Saville | b7c92c7 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 674 | |
Wink Saville | 512c220 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 675 | /** |
| 676 | * The network to use for initially attaching to the network |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 677 | * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IA} instead. |
Wink Saville | 512c220 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 678 | * {@hide} |
| 679 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 680 | @Deprecated |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 681 | @UnsupportedAppUsage |
Wink Saville | 512c220 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 682 | public static final int TYPE_MOBILE_IA = 14; |
repo sync | f5de557 | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 683 | |
Lorenzo Colitti | 21e9a15 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 684 | /** |
Robert Greenwalt | b1f7f75 | 2015-07-09 14:49:35 -0700 | [diff] [blame] | 685 | * Emergency PDN connection for emergency services. This |
| 686 | * may include IMS and MMS in emergency situations. |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 687 | * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_EIMS} instead. |
Ram | 693d07a | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 688 | * {@hide} |
| 689 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 690 | @Deprecated |
Chalard Jean | a3b7751 | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 691 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Ram | 693d07a | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 692 | public static final int TYPE_MOBILE_EMERGENCY = 15; |
| 693 | |
Hui Lu | 865b70d | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 694 | /** |
| 695 | * The network that uses proxy to achieve connectivity. |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 696 | * @deprecated Use {@link NetworkCapabilities} instead. |
Hui Lu | 865b70d | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 697 | * {@hide} |
| 698 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 699 | @Deprecated |
Remi NGUYEN VAN | ee660cf | 2020-11-30 19:23:45 +0900 | [diff] [blame] | 700 | @SystemApi |
Hui Lu | 865b70d | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 701 | public static final int TYPE_PROXY = 16; |
Wink Saville | 512c220 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 702 | |
Robert Greenwalt | ce7a144 | 2014-07-07 17:09:01 -0700 | [diff] [blame] | 703 | /** |
| 704 | * A virtual network using one or more native bearers. |
| 705 | * It may or may not be providing security services. |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 706 | * @deprecated Applications should use {@link NetworkCapabilities#TRANSPORT_VPN} instead. |
Robert Greenwalt | ce7a144 | 2014-07-07 17:09:01 -0700 | [diff] [blame] | 707 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 708 | @Deprecated |
Robert Greenwalt | ce7a144 | 2014-07-07 17:09:01 -0700 | [diff] [blame] | 709 | public static final int TYPE_VPN = 17; |
Hui Lu | 865b70d | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 710 | |
Benedict Wong | bdfaa48 | 2018-11-14 17:40:55 -0800 | [diff] [blame] | 711 | /** |
| 712 | * A network that is exclusively meant to be used for testing |
| 713 | * |
| 714 | * @deprecated Use {@link NetworkCapabilities} instead. |
| 715 | * @hide |
| 716 | */ |
| 717 | @Deprecated |
| 718 | public static final int TYPE_TEST = 18; // TODO: Remove this once NetworkTypes are unused. |
Robert Greenwalt | ce7a144 | 2014-07-07 17:09:01 -0700 | [diff] [blame] | 719 | |
Chalard Jean | aea539a | 2020-03-25 01:24:04 +0900 | [diff] [blame] | 720 | /** |
| 721 | * @deprecated Use {@link NetworkCapabilities} instead. |
| 722 | * @hide |
| 723 | */ |
| 724 | @Deprecated |
| 725 | @Retention(RetentionPolicy.SOURCE) |
| 726 | @IntDef(prefix = { "TYPE_" }, value = { |
| 727 | TYPE_NONE, |
| 728 | TYPE_MOBILE, |
| 729 | TYPE_WIFI, |
| 730 | TYPE_MOBILE_MMS, |
| 731 | TYPE_MOBILE_SUPL, |
| 732 | TYPE_MOBILE_DUN, |
| 733 | TYPE_MOBILE_HIPRI, |
| 734 | TYPE_WIMAX, |
| 735 | TYPE_BLUETOOTH, |
| 736 | TYPE_DUMMY, |
| 737 | TYPE_ETHERNET, |
| 738 | TYPE_MOBILE_FOTA, |
| 739 | TYPE_MOBILE_IMS, |
| 740 | TYPE_MOBILE_CBS, |
| 741 | TYPE_WIFI_P2P, |
| 742 | TYPE_MOBILE_IA, |
| 743 | TYPE_MOBILE_EMERGENCY, |
| 744 | TYPE_PROXY, |
| 745 | TYPE_VPN, |
| 746 | TYPE_TEST |
| 747 | }) |
| 748 | public @interface LegacyNetworkType {} |
| 749 | |
Chalard Jean | afaed1a | 2019-11-21 14:48:00 +0900 | [diff] [blame] | 750 | // Deprecated constants for return values of startUsingNetworkFeature. They used to live |
| 751 | // in com.android.internal.telephony.PhoneConstants until they were made inaccessible. |
| 752 | private static final int DEPRECATED_PHONE_CONSTANT_APN_ALREADY_ACTIVE = 0; |
| 753 | private static final int DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED = 1; |
| 754 | private static final int DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED = 3; |
| 755 | |
Robert Greenwalt | ce7a144 | 2014-07-07 17:09:01 -0700 | [diff] [blame] | 756 | /** {@hide} */ |
Benedict Wong | bdfaa48 | 2018-11-14 17:40:55 -0800 | [diff] [blame] | 757 | public static final int MAX_RADIO_TYPE = TYPE_TEST; |
| 758 | |
| 759 | /** {@hide} */ |
| 760 | public static final int MAX_NETWORK_TYPE = TYPE_TEST; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 761 | |
Hugo Benichi | ad353f4 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 762 | private static final int MIN_NETWORK_TYPE = TYPE_MOBILE; |
| 763 | |
Jianzheng Zhou | 028d203 | 2012-11-16 13:45:20 +0800 | [diff] [blame] | 764 | /** |
| 765 | * If you want to set the default network preference,you can directly |
| 766 | * change the networkAttributes array in framework's config.xml. |
| 767 | * |
| 768 | * @deprecated Since we support so many more networks now, the single |
| 769 | * network default network preference can't really express |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 770 | * the hierarchy. Instead, the default is defined by the |
Jianzheng Zhou | 028d203 | 2012-11-16 13:45:20 +0800 | [diff] [blame] | 771 | * networkAttributes in config.xml. You can determine |
Robert Greenwalt | f909cb1 | 2012-12-07 09:56:50 -0800 | [diff] [blame] | 772 | * the current value by calling {@link #getNetworkPreference()} |
Jianzheng Zhou | 028d203 | 2012-11-16 13:45:20 +0800 | [diff] [blame] | 773 | * from an App. |
| 774 | */ |
| 775 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 776 | public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI; |
| 777 | |
Jeff Sharkey | 8c87045 | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 778 | /** |
Robert Greenwalt | 42a0e1e | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 779 | * @hide |
| 780 | */ |
Hugo Benichi | 7ab07a3 | 2017-06-20 14:10:14 +0900 | [diff] [blame] | 781 | public static final int REQUEST_ID_UNSET = 0; |
Robert Greenwalt | bfd1f4c | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 782 | |
Paul Jensen | 5dea435 | 2014-07-11 12:28:19 -0400 | [diff] [blame] | 783 | /** |
Hugo Benichi | bee30fe | 2017-06-17 13:14:12 +0900 | [diff] [blame] | 784 | * Static unique request used as a tombstone for NetworkCallbacks that have been unregistered. |
| 785 | * This allows to distinguish when unregistering NetworkCallbacks those that were never |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 786 | * registered from those that were already unregistered. |
Hugo Benichi | bee30fe | 2017-06-17 13:14:12 +0900 | [diff] [blame] | 787 | * @hide |
| 788 | */ |
Hugo Benichi | 7ab07a3 | 2017-06-20 14:10:14 +0900 | [diff] [blame] | 789 | private static final NetworkRequest ALREADY_UNREGISTERED = |
Hugo Benichi | bee30fe | 2017-06-17 13:14:12 +0900 | [diff] [blame] | 790 | new NetworkRequest.Builder().clearCapabilities().build(); |
| 791 | |
| 792 | /** |
Paul Jensen | 5dea435 | 2014-07-11 12:28:19 -0400 | [diff] [blame] | 793 | * A NetID indicating no Network is selected. |
| 794 | * Keep in sync with bionic/libc/dns/include/resolv_netid.h |
| 795 | * @hide |
| 796 | */ |
| 797 | public static final int NETID_UNSET = 0; |
| 798 | |
Erik Kline | b0be3e6 | 2017-10-30 15:29:44 +0900 | [diff] [blame] | 799 | /** |
| 800 | * Private DNS Mode values. |
| 801 | * |
| 802 | * The "private_dns_mode" global setting stores a String value which is |
| 803 | * expected to be one of the following. |
| 804 | */ |
| 805 | |
| 806 | /** |
| 807 | * @hide |
| 808 | */ |
lucaslin | 705c333 | 2021-03-12 17:56:09 +0800 | [diff] [blame] | 809 | @SystemApi(client = MODULE_LIBRARIES) |
Erik Kline | b0be3e6 | 2017-10-30 15:29:44 +0900 | [diff] [blame] | 810 | public static final String PRIVATE_DNS_MODE_OFF = "off"; |
| 811 | /** |
| 812 | * @hide |
| 813 | */ |
lucaslin | 705c333 | 2021-03-12 17:56:09 +0800 | [diff] [blame] | 814 | @SystemApi(client = MODULE_LIBRARIES) |
Erik Kline | b0be3e6 | 2017-10-30 15:29:44 +0900 | [diff] [blame] | 815 | public static final String PRIVATE_DNS_MODE_OPPORTUNISTIC = "opportunistic"; |
| 816 | /** |
| 817 | * @hide |
| 818 | */ |
lucaslin | 705c333 | 2021-03-12 17:56:09 +0800 | [diff] [blame] | 819 | @SystemApi(client = MODULE_LIBRARIES) |
Erik Kline | b0be3e6 | 2017-10-30 15:29:44 +0900 | [diff] [blame] | 820 | public static final String PRIVATE_DNS_MODE_PROVIDER_HOSTNAME = "hostname"; |
lucaslin | 705c333 | 2021-03-12 17:56:09 +0800 | [diff] [blame] | 821 | |
| 822 | /** @hide */ |
| 823 | @Retention(RetentionPolicy.SOURCE) |
| 824 | @StringDef(value = { |
| 825 | PRIVATE_DNS_MODE_OFF, |
| 826 | PRIVATE_DNS_MODE_OPPORTUNISTIC, |
| 827 | PRIVATE_DNS_MODE_PROVIDER_HOSTNAME, |
| 828 | }) |
| 829 | public @interface PrivateDnsMode {} |
Erik Kline | b0be3e6 | 2017-10-30 15:29:44 +0900 | [diff] [blame] | 830 | |
Sudheer Shanka | 9821556 | 2021-03-23 08:12:28 +0000 | [diff] [blame] | 831 | /** |
| 832 | * Flag to indicate that an app is not subject to any restrictions that could result in its |
| 833 | * network access blocked. |
| 834 | * |
| 835 | * @hide |
| 836 | */ |
| 837 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 838 | public static final int BLOCKED_REASON_NONE = 0; |
| 839 | |
| 840 | /** |
| 841 | * Flag to indicate that an app is subject to Battery saver restrictions that would |
| 842 | * result in its network access being blocked. |
| 843 | * |
| 844 | * @hide |
| 845 | */ |
| 846 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 847 | public static final int BLOCKED_REASON_BATTERY_SAVER = 1 << 0; |
| 848 | |
| 849 | /** |
| 850 | * Flag to indicate that an app is subject to Doze restrictions that would |
| 851 | * result in its network access being blocked. |
| 852 | * |
| 853 | * @hide |
| 854 | */ |
| 855 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 856 | public static final int BLOCKED_REASON_DOZE = 1 << 1; |
| 857 | |
| 858 | /** |
| 859 | * Flag to indicate that an app is subject to App Standby restrictions that would |
| 860 | * result in its network access being blocked. |
| 861 | * |
| 862 | * @hide |
| 863 | */ |
| 864 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 865 | public static final int BLOCKED_REASON_APP_STANDBY = 1 << 2; |
| 866 | |
| 867 | /** |
| 868 | * Flag to indicate that an app is subject to Restricted mode restrictions that would |
| 869 | * result in its network access being blocked. |
| 870 | * |
| 871 | * @hide |
| 872 | */ |
| 873 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 874 | public static final int BLOCKED_REASON_RESTRICTED_MODE = 1 << 3; |
| 875 | |
| 876 | /** |
Lorenzo Colitti | 79c6f22 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 877 | * Flag to indicate that an app is blocked because it is subject to an always-on VPN but the VPN |
| 878 | * is not currently connected. |
| 879 | * |
| 880 | * @see DevicePolicyManager#setAlwaysOnVpnPackage(ComponentName, String, boolean) |
| 881 | * |
| 882 | * @hide |
| 883 | */ |
| 884 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 885 | public static final int BLOCKED_REASON_LOCKDOWN_VPN = 1 << 4; |
| 886 | |
| 887 | /** |
Sudheer Shanka | 9821556 | 2021-03-23 08:12:28 +0000 | [diff] [blame] | 888 | * Flag to indicate that an app is subject to Data saver restrictions that would |
| 889 | * result in its metered network access being blocked. |
| 890 | * |
| 891 | * @hide |
| 892 | */ |
| 893 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 894 | public static final int BLOCKED_METERED_REASON_DATA_SAVER = 1 << 16; |
| 895 | |
| 896 | /** |
| 897 | * Flag to indicate that an app is subject to user restrictions that would |
| 898 | * result in its metered network access being blocked. |
| 899 | * |
| 900 | * @hide |
| 901 | */ |
| 902 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 903 | public static final int BLOCKED_METERED_REASON_USER_RESTRICTED = 1 << 17; |
| 904 | |
| 905 | /** |
| 906 | * Flag to indicate that an app is subject to Device admin restrictions that would |
| 907 | * result in its metered network access being blocked. |
| 908 | * |
| 909 | * @hide |
| 910 | */ |
| 911 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 912 | public static final int BLOCKED_METERED_REASON_ADMIN_DISABLED = 1 << 18; |
| 913 | |
| 914 | /** |
| 915 | * @hide |
| 916 | */ |
| 917 | @Retention(RetentionPolicy.SOURCE) |
| 918 | @IntDef(flag = true, prefix = {"BLOCKED_"}, value = { |
| 919 | BLOCKED_REASON_NONE, |
| 920 | BLOCKED_REASON_BATTERY_SAVER, |
| 921 | BLOCKED_REASON_DOZE, |
| 922 | BLOCKED_REASON_APP_STANDBY, |
| 923 | BLOCKED_REASON_RESTRICTED_MODE, |
Lorenzo Colitti | 42fe223 | 2021-03-25 23:17:36 +0900 | [diff] [blame] | 924 | BLOCKED_REASON_LOCKDOWN_VPN, |
Sudheer Shanka | 9821556 | 2021-03-23 08:12:28 +0000 | [diff] [blame] | 925 | BLOCKED_METERED_REASON_DATA_SAVER, |
| 926 | BLOCKED_METERED_REASON_USER_RESTRICTED, |
| 927 | BLOCKED_METERED_REASON_ADMIN_DISABLED, |
| 928 | }) |
| 929 | public @interface BlockedReason {} |
| 930 | |
Lorenzo Colitti | 79c6f22 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 931 | /** |
| 932 | * Set of blocked reasons that are only applicable on metered networks. |
| 933 | * |
| 934 | * @hide |
| 935 | */ |
| 936 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 937 | public static final int BLOCKED_METERED_REASON_MASK = 0xffff0000; |
| 938 | |
Chalard Jean | a3b7751 | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 939 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Jeff Sharkey | 921ebf2 | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 940 | private final IConnectivityManager mService; |
Lorenzo Colitti | cd67529 | 2021-02-04 17:32:07 +0900 | [diff] [blame] | 941 | |
Paul Jensen | c0618a6 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 942 | /** |
| 943 | * A kludge to facilitate static access where a Context pointer isn't available, like in the |
| 944 | * case of the static set/getProcessDefaultNetwork methods and from the Network class. |
| 945 | * TODO: Remove this after deprecating the static methods in favor of non-static methods or |
| 946 | * methods that take a Context argument. |
| 947 | */ |
| 948 | private static ConnectivityManager sInstance; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 949 | |
Lorenzo Colitti | 2780e4b | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 950 | private final Context mContext; |
| 951 | |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 952 | private final TetheringManager mTetheringManager; |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 953 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 954 | /** |
| 955 | * Tests if a given integer represents a valid network type. |
| 956 | * @param networkType the type to be tested |
| 957 | * @return a boolean. {@code true} if the type is valid, else {@code false} |
Paul Jensen | bbb6109 | 2015-05-06 10:42:25 -0400 | [diff] [blame] | 958 | * @deprecated All APIs accepting a network type are deprecated. There should be no need to |
| 959 | * validate a network type. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 960 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 961 | @Deprecated |
Jeff Sharkey | 21062e7 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 962 | public static boolean isNetworkTypeValid(int networkType) { |
Hugo Benichi | ad353f4 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 963 | return MIN_NETWORK_TYPE <= networkType && networkType <= MAX_NETWORK_TYPE; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 964 | } |
| 965 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 966 | /** |
| 967 | * Returns a non-localized string representing a given network type. |
| 968 | * ONLY used for debugging output. |
| 969 | * @param type the type needing naming |
| 970 | * @return a String for the given type, or a string version of the type ("87") |
| 971 | * if no name is known. |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 972 | * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 973 | * {@hide} |
| 974 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 975 | @Deprecated |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 976 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Jeff Sharkey | 21062e7 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 977 | public static String getNetworkTypeName(int type) { |
| 978 | switch (type) { |
Hugo Benichi | ad353f4 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 979 | case TYPE_NONE: |
| 980 | return "NONE"; |
Jeff Sharkey | 21062e7 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 981 | case TYPE_MOBILE: |
| 982 | return "MOBILE"; |
| 983 | case TYPE_WIFI: |
| 984 | return "WIFI"; |
| 985 | case TYPE_MOBILE_MMS: |
| 986 | return "MOBILE_MMS"; |
| 987 | case TYPE_MOBILE_SUPL: |
| 988 | return "MOBILE_SUPL"; |
| 989 | case TYPE_MOBILE_DUN: |
| 990 | return "MOBILE_DUN"; |
| 991 | case TYPE_MOBILE_HIPRI: |
| 992 | return "MOBILE_HIPRI"; |
| 993 | case TYPE_WIMAX: |
| 994 | return "WIMAX"; |
| 995 | case TYPE_BLUETOOTH: |
| 996 | return "BLUETOOTH"; |
| 997 | case TYPE_DUMMY: |
| 998 | return "DUMMY"; |
| 999 | case TYPE_ETHERNET: |
| 1000 | return "ETHERNET"; |
| 1001 | case TYPE_MOBILE_FOTA: |
| 1002 | return "MOBILE_FOTA"; |
| 1003 | case TYPE_MOBILE_IMS: |
| 1004 | return "MOBILE_IMS"; |
| 1005 | case TYPE_MOBILE_CBS: |
| 1006 | return "MOBILE_CBS"; |
repo sync | f5de557 | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 1007 | case TYPE_WIFI_P2P: |
| 1008 | return "WIFI_P2P"; |
Wink Saville | 512c220 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 1009 | case TYPE_MOBILE_IA: |
| 1010 | return "MOBILE_IA"; |
Ram | 693d07a | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 1011 | case TYPE_MOBILE_EMERGENCY: |
| 1012 | return "MOBILE_EMERGENCY"; |
Hui Lu | 865b70d | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 1013 | case TYPE_PROXY: |
| 1014 | return "PROXY"; |
Erik Kline | 137fadc | 2014-11-19 17:23:41 +0900 | [diff] [blame] | 1015 | case TYPE_VPN: |
| 1016 | return "VPN"; |
Jeff Sharkey | 21062e7 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1017 | default: |
| 1018 | return Integer.toString(type); |
| 1019 | } |
| 1020 | } |
| 1021 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1022 | /** |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 1023 | * @hide |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 1024 | */ |
lucaslin | 7fc930b | 2021-03-17 14:16:01 +0800 | [diff] [blame] | 1025 | @SystemApi(client = MODULE_LIBRARIES) |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 1026 | public void systemReady() { |
| 1027 | try { |
| 1028 | mService.systemReady(); |
| 1029 | } catch (RemoteException e) { |
| 1030 | throw e.rethrowFromSystemServer(); |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1035 | * Checks if a given type uses the cellular data connection. |
| 1036 | * This should be replaced in the future by a network property. |
| 1037 | * @param networkType the type to check |
| 1038 | * @return a boolean - {@code true} if uses cellular network, else {@code false} |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 1039 | * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1040 | * {@hide} |
| 1041 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 1042 | @Deprecated |
Chalard Jean | a3b7751 | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 1043 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Jeff Sharkey | 21062e7 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1044 | public static boolean isNetworkTypeMobile(int networkType) { |
| 1045 | switch (networkType) { |
| 1046 | case TYPE_MOBILE: |
| 1047 | case TYPE_MOBILE_MMS: |
| 1048 | case TYPE_MOBILE_SUPL: |
| 1049 | case TYPE_MOBILE_DUN: |
| 1050 | case TYPE_MOBILE_HIPRI: |
| 1051 | case TYPE_MOBILE_FOTA: |
| 1052 | case TYPE_MOBILE_IMS: |
| 1053 | case TYPE_MOBILE_CBS: |
Wink Saville | 512c220 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 1054 | case TYPE_MOBILE_IA: |
Ram | 693d07a | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 1055 | case TYPE_MOBILE_EMERGENCY: |
Jeff Sharkey | 21062e7 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 1056 | return true; |
| 1057 | default: |
| 1058 | return false; |
| 1059 | } |
| 1060 | } |
| 1061 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1062 | /** |
Jeff Sharkey | 79f3e02 | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 1063 | * Checks if the given network type is backed by a Wi-Fi radio. |
| 1064 | * |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 1065 | * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead. |
Jeff Sharkey | 79f3e02 | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 1066 | * @hide |
| 1067 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 1068 | @Deprecated |
Jeff Sharkey | 79f3e02 | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 1069 | public static boolean isNetworkTypeWifi(int networkType) { |
| 1070 | switch (networkType) { |
| 1071 | case TYPE_WIFI: |
| 1072 | case TYPE_WIFI_P2P: |
| 1073 | return true; |
| 1074 | default: |
| 1075 | return false; |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | /** |
Chalard Jean | fa45a68 | 2021-02-25 17:23:40 +0900 | [diff] [blame] | 1080 | * Preference for {@link #setNetworkPreferenceForUser(UserHandle, int, Executor, Runnable)}. |
| 1081 | * Specify that the traffic for this user should by follow the default rules. |
| 1082 | * @hide |
| 1083 | */ |
Chalard Jean | b43f396 | 2021-03-17 14:33:24 +0900 | [diff] [blame] | 1084 | @SystemApi(client = MODULE_LIBRARIES) |
Chalard Jean | fa45a68 | 2021-02-25 17:23:40 +0900 | [diff] [blame] | 1085 | public static final int PROFILE_NETWORK_PREFERENCE_DEFAULT = 0; |
| 1086 | |
| 1087 | /** |
| 1088 | * Preference for {@link #setNetworkPreferenceForUser(UserHandle, int, Executor, Runnable)}. |
| 1089 | * Specify that the traffic for this user should by default go on a network with |
| 1090 | * {@link NetworkCapabilities#NET_CAPABILITY_ENTERPRISE}, and on the system default network |
| 1091 | * if no such network is available. |
| 1092 | * @hide |
| 1093 | */ |
Chalard Jean | b43f396 | 2021-03-17 14:33:24 +0900 | [diff] [blame] | 1094 | @SystemApi(client = MODULE_LIBRARIES) |
Chalard Jean | fa45a68 | 2021-02-25 17:23:40 +0900 | [diff] [blame] | 1095 | public static final int PROFILE_NETWORK_PREFERENCE_ENTERPRISE = 1; |
| 1096 | |
| 1097 | /** @hide */ |
| 1098 | @Retention(RetentionPolicy.SOURCE) |
| 1099 | @IntDef(value = { |
| 1100 | PROFILE_NETWORK_PREFERENCE_DEFAULT, |
| 1101 | PROFILE_NETWORK_PREFERENCE_ENTERPRISE |
| 1102 | }) |
| 1103 | public @interface ProfileNetworkPreference { |
| 1104 | } |
| 1105 | |
| 1106 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1107 | * Specifies the preferred network type. When the device has more |
| 1108 | * than one type available the preferred network type will be used. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1109 | * |
| 1110 | * @param preference the network type to prefer over all others. It is |
| 1111 | * unspecified what happens to the old preferred network in the |
| 1112 | * overall ordering. |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1113 | * @deprecated Functionality has been removed as it no longer makes sense, |
| 1114 | * with many more than two networks - we'd need an array to express |
| 1115 | * preference. Instead we use dynamic network properties of |
| 1116 | * the networks to describe their precedence. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1117 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 1118 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1119 | public void setNetworkPreference(int preference) { |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1120 | } |
| 1121 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1122 | /** |
| 1123 | * Retrieves the current preferred network type. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1124 | * |
| 1125 | * @return an integer representing the preferred network type |
| 1126 | * |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1127 | * @deprecated Functionality has been removed as it no longer makes sense, |
| 1128 | * with many more than two networks - we'd need an array to express |
| 1129 | * preference. Instead we use dynamic network properties of |
| 1130 | * the networks to describe their precedence. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1131 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 1132 | @Deprecated |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1133 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1134 | public int getNetworkPreference() { |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1135 | return TYPE_NONE; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1136 | } |
| 1137 | |
Scott Main | f58b7d8 | 2011-10-06 19:02:28 -0700 | [diff] [blame] | 1138 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1139 | * Returns details about the currently active default data network. When |
| 1140 | * connected, this network is the default route for outgoing connections. |
| 1141 | * You should always check {@link NetworkInfo#isConnected()} before initiating |
| 1142 | * network traffic. This may return {@code null} when there is no default |
| 1143 | * network. |
Chalard Jean | 96d10a7 | 2018-03-29 17:45:24 +0900 | [diff] [blame] | 1144 | * Note that if the default network is a VPN, this method will return the |
| 1145 | * NetworkInfo for one of its underlying networks instead, or null if the |
| 1146 | * VPN agent did not specify any. Apps interested in learning about VPNs |
| 1147 | * should use {@link #getNetworkInfo(android.net.Network)} instead. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1148 | * |
| 1149 | * @return a {@link NetworkInfo} object for the current default network |
Paul Jensen | bd2d378 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1150 | * or {@code null} if no default network is currently active |
junyulai | 9826e7f | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 1151 | * @deprecated See {@link NetworkInfo}. |
Jeff Sharkey | d00b130 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1152 | */ |
junyulai | 9826e7f | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 1153 | @Deprecated |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1154 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1155 | @Nullable |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1156 | public NetworkInfo getActiveNetworkInfo() { |
| 1157 | try { |
| 1158 | return mService.getActiveNetworkInfo(); |
| 1159 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1160 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1161 | } |
| 1162 | } |
| 1163 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1164 | /** |
Paul Jensen | 1f56738 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1165 | * Returns a {@link Network} object corresponding to the currently active |
| 1166 | * default data network. In the event that the current active default data |
| 1167 | * network disconnects, the returned {@code Network} object will no longer |
| 1168 | * be usable. This will return {@code null} when there is no default |
| 1169 | * network. |
| 1170 | * |
| 1171 | * @return a {@link Network} object for the current default network or |
| 1172 | * {@code null} if no default network is currently active |
Paul Jensen | 1f56738 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1173 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1174 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1175 | @Nullable |
Paul Jensen | 1f56738 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1176 | public Network getActiveNetwork() { |
| 1177 | try { |
| 1178 | return mService.getActiveNetwork(); |
| 1179 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1180 | throw e.rethrowFromSystemServer(); |
Paul Jensen | 1f56738 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1181 | } |
| 1182 | } |
| 1183 | |
| 1184 | /** |
Robin Lee | 5b52bef | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1185 | * Returns a {@link Network} object corresponding to the currently active |
| 1186 | * default data network for a specific UID. In the event that the default data |
| 1187 | * network disconnects, the returned {@code Network} object will no longer |
| 1188 | * be usable. This will return {@code null} when there is no default |
| 1189 | * network for the UID. |
Robin Lee | 5b52bef | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1190 | * |
| 1191 | * @return a {@link Network} object for the current default network for the |
| 1192 | * given UID or {@code null} if no default network is currently active |
lifr | 42a60c2 | 2021-03-11 20:11:09 +0800 | [diff] [blame] | 1193 | * TODO: b/183465229 Cleanup getActiveNetworkForUid once b/165835257 is fixed |
Robin Lee | 5b52bef | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1194 | */ |
paulhu | 8e96a75 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1195 | @RequiresPermission(android.Manifest.permission.NETWORK_STACK) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1196 | @Nullable |
Robin Lee | 5b52bef | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1197 | public Network getActiveNetworkForUid(int uid) { |
Jeff Sharkey | 7dbf83d | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1198 | return getActiveNetworkForUid(uid, false); |
| 1199 | } |
| 1200 | |
| 1201 | /** {@hide} */ |
| 1202 | public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) { |
Robin Lee | 5b52bef | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1203 | try { |
Jeff Sharkey | 7dbf83d | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1204 | return mService.getActiveNetworkForUid(uid, ignoreBlocked); |
Robin Lee | 5b52bef | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1205 | } catch (RemoteException e) { |
| 1206 | throw e.rethrowFromSystemServer(); |
| 1207 | } |
| 1208 | } |
| 1209 | |
| 1210 | /** |
Lorenzo Colitti | 3f54f10 | 2020-12-12 00:51:11 +0900 | [diff] [blame] | 1211 | * Adds or removes a requirement for given UID ranges to use the VPN. |
| 1212 | * |
| 1213 | * If set to {@code true}, informs the system that the UIDs in the specified ranges must not |
| 1214 | * have any connectivity except if a VPN is connected and applies to the UIDs, or if the UIDs |
| 1215 | * otherwise have permission to bypass the VPN (e.g., because they have the |
| 1216 | * {@link android.Manifest.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS} permission, or when |
| 1217 | * using a socket protected by a method such as {@link VpnService#protect(DatagramSocket)}. If |
| 1218 | * set to {@code false}, a previously-added restriction is removed. |
| 1219 | * <p> |
| 1220 | * Each of the UID ranges specified by this method is added and removed as is, and no processing |
| 1221 | * is performed on the ranges to de-duplicate, merge, split, or intersect them. In order to |
| 1222 | * remove a previously-added range, the exact range must be removed as is. |
| 1223 | * <p> |
| 1224 | * The changes are applied asynchronously and may not have been applied by the time the method |
| 1225 | * returns. Apps will be notified about any changes that apply to them via |
| 1226 | * {@link NetworkCallback#onBlockedStatusChanged} callbacks called after the changes take |
| 1227 | * effect. |
| 1228 | * <p> |
| 1229 | * This method should be called only by the VPN code. |
| 1230 | * |
| 1231 | * @param ranges the UID ranges to restrict |
| 1232 | * @param requireVpn whether the specified UID ranges must use a VPN |
| 1233 | * |
Lorenzo Colitti | 3f54f10 | 2020-12-12 00:51:11 +0900 | [diff] [blame] | 1234 | * @hide |
| 1235 | */ |
| 1236 | @RequiresPermission(anyOf = { |
| 1237 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
lucaslin | e257bce | 2021-03-22 11:51:27 +0800 | [diff] [blame] | 1238 | android.Manifest.permission.NETWORK_STACK, |
| 1239 | android.Manifest.permission.NETWORK_SETTINGS}) |
| 1240 | @SystemApi(client = MODULE_LIBRARIES) |
Lorenzo Colitti | 3f54f10 | 2020-12-12 00:51:11 +0900 | [diff] [blame] | 1241 | public void setRequireVpnForUids(boolean requireVpn, |
| 1242 | @NonNull Collection<Range<Integer>> ranges) { |
| 1243 | Objects.requireNonNull(ranges); |
| 1244 | // The Range class is not parcelable. Convert to UidRange, which is what is used internally. |
| 1245 | // This method is not necessarily expected to be used outside the system server, so |
| 1246 | // parceling may not be necessary, but it could be used out-of-process, e.g., by the network |
| 1247 | // stack process, or by tests. |
| 1248 | UidRange[] rangesArray = new UidRange[ranges.size()]; |
| 1249 | int index = 0; |
| 1250 | for (Range<Integer> range : ranges) { |
| 1251 | rangesArray[index++] = new UidRange(range.getLower(), range.getUpper()); |
| 1252 | } |
| 1253 | try { |
| 1254 | mService.setRequireVpnForUids(requireVpn, rangesArray); |
| 1255 | } catch (RemoteException e) { |
| 1256 | throw e.rethrowFromSystemServer(); |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | /** |
Lorenzo Colitti | bcd692f | 2021-01-15 01:29:01 +0900 | [diff] [blame] | 1261 | * Informs ConnectivityService of whether the legacy lockdown VPN, as implemented by |
| 1262 | * LockdownVpnTracker, is in use. This is deprecated for new devices starting from Android 12 |
| 1263 | * but is still supported for backwards compatibility. |
| 1264 | * <p> |
| 1265 | * This type of VPN is assumed always to use the system default network, and must always declare |
| 1266 | * exactly one underlying network, which is the network that was the default when the VPN |
| 1267 | * connected. |
| 1268 | * <p> |
| 1269 | * Calling this method with {@code true} enables legacy behaviour, specifically: |
| 1270 | * <ul> |
| 1271 | * <li>Any VPN that applies to userId 0 behaves specially with respect to deprecated |
| 1272 | * {@link #CONNECTIVITY_ACTION} broadcasts. Any such broadcasts will have the state in the |
| 1273 | * {@link #EXTRA_NETWORK_INFO} replaced by state of the VPN network. Also, any time the VPN |
| 1274 | * connects, a {@link #CONNECTIVITY_ACTION} broadcast will be sent for the network |
| 1275 | * underlying the VPN.</li> |
| 1276 | * <li>Deprecated APIs that return {@link NetworkInfo} objects will have their state |
| 1277 | * similarly replaced by the VPN network state.</li> |
| 1278 | * <li>Information on current network interfaces passed to NetworkStatsService will not |
| 1279 | * include any VPN interfaces.</li> |
| 1280 | * </ul> |
| 1281 | * |
| 1282 | * @param enabled whether legacy lockdown VPN is enabled or disabled |
| 1283 | * |
Lorenzo Colitti | bcd692f | 2021-01-15 01:29:01 +0900 | [diff] [blame] | 1284 | * @hide |
| 1285 | */ |
| 1286 | @RequiresPermission(anyOf = { |
| 1287 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
lucaslin | e257bce | 2021-03-22 11:51:27 +0800 | [diff] [blame] | 1288 | android.Manifest.permission.NETWORK_STACK, |
Lorenzo Colitti | bcd692f | 2021-01-15 01:29:01 +0900 | [diff] [blame] | 1289 | android.Manifest.permission.NETWORK_SETTINGS}) |
lucaslin | e257bce | 2021-03-22 11:51:27 +0800 | [diff] [blame] | 1290 | @SystemApi(client = MODULE_LIBRARIES) |
Lorenzo Colitti | bcd692f | 2021-01-15 01:29:01 +0900 | [diff] [blame] | 1291 | public void setLegacyLockdownVpnEnabled(boolean enabled) { |
| 1292 | try { |
| 1293 | mService.setLegacyLockdownVpnEnabled(enabled); |
| 1294 | } catch (RemoteException e) { |
| 1295 | throw e.rethrowFromSystemServer(); |
| 1296 | } |
| 1297 | } |
| 1298 | |
| 1299 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1300 | * Returns details about the currently active default data network |
| 1301 | * for a given uid. This is for internal use only to avoid spying |
| 1302 | * other apps. |
| 1303 | * |
| 1304 | * @return a {@link NetworkInfo} object for the current default network |
| 1305 | * for the given uid or {@code null} if no default network is |
| 1306 | * available for the specified uid. |
| 1307 | * |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1308 | * {@hide} |
| 1309 | */ |
paulhu | 8e96a75 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1310 | @RequiresPermission(android.Manifest.permission.NETWORK_STACK) |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 1311 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Jeff Sharkey | 921ebf2 | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1312 | public NetworkInfo getActiveNetworkInfoForUid(int uid) { |
Jeff Sharkey | 7dbf83d | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1313 | return getActiveNetworkInfoForUid(uid, false); |
| 1314 | } |
| 1315 | |
| 1316 | /** {@hide} */ |
| 1317 | public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) { |
Jeff Sharkey | 921ebf2 | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1318 | try { |
Jeff Sharkey | 7dbf83d | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1319 | return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked); |
Jeff Sharkey | 921ebf2 | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1320 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1321 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 921ebf2 | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1322 | } |
| 1323 | } |
| 1324 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1325 | /** |
| 1326 | * Returns connection status information about a particular |
| 1327 | * network type. |
| 1328 | * |
| 1329 | * @param networkType integer specifying which networkType in |
| 1330 | * which you're interested. |
| 1331 | * @return a {@link NetworkInfo} object for the requested |
| 1332 | * network type or {@code null} if the type is not |
Chalard Jean | 96d10a7 | 2018-03-29 17:45:24 +0900 | [diff] [blame] | 1333 | * supported by the device. If {@code networkType} is |
| 1334 | * TYPE_VPN and a VPN is active for the calling app, |
| 1335 | * then this method will try to return one of the |
| 1336 | * underlying networks for the VPN or null if the |
| 1337 | * VPN agent didn't specify any. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1338 | * |
Paul Jensen | dda8e59 | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 1339 | * @deprecated This method does not support multiple connected networks |
| 1340 | * of the same type. Use {@link #getAllNetworks} and |
| 1341 | * {@link #getNetworkInfo(android.net.Network)} instead. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1342 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 1343 | @Deprecated |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1344 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1345 | @Nullable |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1346 | public NetworkInfo getNetworkInfo(int networkType) { |
| 1347 | try { |
| 1348 | return mService.getNetworkInfo(networkType); |
| 1349 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1350 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1351 | } |
| 1352 | } |
| 1353 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1354 | /** |
Robert Greenwalt | 02fe11e | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1355 | * Returns connection status information about a particular |
| 1356 | * Network. |
| 1357 | * |
| 1358 | * @param network {@link Network} specifying which network |
| 1359 | * in which you're interested. |
| 1360 | * @return a {@link NetworkInfo} object for the requested |
| 1361 | * network or {@code null} if the {@code Network} |
| 1362 | * is not valid. |
junyulai | 9826e7f | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 1363 | * @deprecated See {@link NetworkInfo}. |
Robert Greenwalt | 02fe11e | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1364 | */ |
junyulai | 9826e7f | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 1365 | @Deprecated |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1366 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1367 | @Nullable |
| 1368 | public NetworkInfo getNetworkInfo(@Nullable Network network) { |
Jeff Sharkey | 7dbf83d | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1369 | return getNetworkInfoForUid(network, Process.myUid(), false); |
| 1370 | } |
| 1371 | |
| 1372 | /** {@hide} */ |
| 1373 | public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) { |
Robert Greenwalt | 02fe11e | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1374 | try { |
Jeff Sharkey | 7dbf83d | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1375 | return mService.getNetworkInfoForUid(network, uid, ignoreBlocked); |
Robert Greenwalt | 02fe11e | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1376 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1377 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 02fe11e | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1378 | } |
| 1379 | } |
| 1380 | |
| 1381 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1382 | * Returns connection status information about all network |
| 1383 | * types supported by the device. |
| 1384 | * |
| 1385 | * @return an array of {@link NetworkInfo} objects. Check each |
| 1386 | * {@link NetworkInfo#getType} for which type each applies. |
| 1387 | * |
Paul Jensen | dda8e59 | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 1388 | * @deprecated This method does not support multiple connected networks |
| 1389 | * of the same type. Use {@link #getAllNetworks} and |
| 1390 | * {@link #getNetworkInfo(android.net.Network)} instead. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1391 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 1392 | @Deprecated |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1393 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1394 | @NonNull |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1395 | public NetworkInfo[] getAllNetworkInfo() { |
| 1396 | try { |
| 1397 | return mService.getAllNetworkInfo(); |
| 1398 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1399 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1400 | } |
| 1401 | } |
| 1402 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1403 | /** |
junyulai | ebd1516 | 2021-03-03 12:09:05 +0800 | [diff] [blame] | 1404 | * Return a list of {@link NetworkStateSnapshot}s, one for each network that is currently |
| 1405 | * connected. |
| 1406 | * @hide |
| 1407 | */ |
| 1408 | @SystemApi(client = MODULE_LIBRARIES) |
| 1409 | @RequiresPermission(anyOf = { |
| 1410 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 1411 | android.Manifest.permission.NETWORK_STACK, |
| 1412 | android.Manifest.permission.NETWORK_SETTINGS}) |
| 1413 | @NonNull |
| 1414 | public List<NetworkStateSnapshot> getAllNetworkStateSnapshot() { |
| 1415 | try { |
| 1416 | return mService.getAllNetworkStateSnapshot(); |
| 1417 | } catch (RemoteException e) { |
| 1418 | throw e.rethrowFromSystemServer(); |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | /** |
Lorenzo Colitti | 860a7aa | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1423 | * Returns the {@link Network} object currently serving a given type, or |
| 1424 | * null if the given type is not connected. |
| 1425 | * |
Lorenzo Colitti | 860a7aa | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1426 | * @hide |
Paul Jensen | dda8e59 | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 1427 | * @deprecated This method does not support multiple connected networks |
| 1428 | * of the same type. Use {@link #getAllNetworks} and |
| 1429 | * {@link #getNetworkInfo(android.net.Network)} instead. |
Lorenzo Colitti | 860a7aa | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1430 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 1431 | @Deprecated |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1432 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 1433 | @UnsupportedAppUsage |
Lorenzo Colitti | 860a7aa | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1434 | public Network getNetworkForType(int networkType) { |
| 1435 | try { |
| 1436 | return mService.getNetworkForType(networkType); |
| 1437 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1438 | throw e.rethrowFromSystemServer(); |
Lorenzo Colitti | 860a7aa | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1439 | } |
| 1440 | } |
| 1441 | |
| 1442 | /** |
Robert Greenwalt | 02fe11e | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1443 | * Returns an array of all {@link Network} currently tracked by the |
| 1444 | * framework. |
Paul Jensen | 08f9dbb | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1445 | * |
| 1446 | * @return an array of {@link Network} objects. |
Robert Greenwalt | 02fe11e | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1447 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1448 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1449 | @NonNull |
Robert Greenwalt | 02fe11e | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1450 | public Network[] getAllNetworks() { |
| 1451 | try { |
| 1452 | return mService.getAllNetworks(); |
| 1453 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1454 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 02fe11e | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | /** |
Roshan Pius | 951c003 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 1459 | * Returns an array of {@link NetworkCapabilities} objects, representing |
Lorenzo Colitti | 79bd2e2 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1460 | * the Networks that applications run by the given user will use by default. |
| 1461 | * @hide |
| 1462 | */ |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 1463 | @UnsupportedAppUsage |
Lorenzo Colitti | 79bd2e2 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1464 | public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) { |
| 1465 | try { |
Qingxi Li | b274810 | 2020-01-08 12:51:49 -0800 | [diff] [blame] | 1466 | return mService.getDefaultNetworkCapabilitiesForUser( |
Roshan Pius | aa24fde | 2020-12-17 14:53:09 -0800 | [diff] [blame] | 1467 | userId, mContext.getOpPackageName(), getAttributionTag()); |
Lorenzo Colitti | 79bd2e2 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1468 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1469 | throw e.rethrowFromSystemServer(); |
Lorenzo Colitti | 79bd2e2 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1474 | * Returns the IP information for the current default network. |
| 1475 | * |
| 1476 | * @return a {@link LinkProperties} object describing the IP info |
| 1477 | * for the current default network, or {@code null} if there |
| 1478 | * is no current default network. |
| 1479 | * |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1480 | * {@hide} |
Chalard Jean | 97021a1 | 2019-01-11 16:47:53 +0900 | [diff] [blame] | 1481 | * @deprecated please use {@link #getLinkProperties(Network)} on the return |
| 1482 | * value of {@link #getActiveNetwork()} instead. In particular, |
| 1483 | * this method will return non-null LinkProperties even if the |
| 1484 | * app is blocked by policy from using this network. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1485 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1486 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 97021a1 | 2019-01-11 16:47:53 +0900 | [diff] [blame] | 1487 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 109783091) |
Robert Greenwalt | 9f0ee4f | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1488 | public LinkProperties getActiveLinkProperties() { |
| 1489 | try { |
| 1490 | return mService.getActiveLinkProperties(); |
| 1491 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1492 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 9f0ee4f | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1493 | } |
| 1494 | } |
| 1495 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1496 | /** |
| 1497 | * Returns the IP information for a given network type. |
| 1498 | * |
| 1499 | * @param networkType the network type of interest. |
| 1500 | * @return a {@link LinkProperties} object describing the IP info |
| 1501 | * for the given networkType, or {@code null} if there is |
| 1502 | * no current default network. |
| 1503 | * |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1504 | * {@hide} |
Paul Jensen | dda8e59 | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 1505 | * @deprecated This method does not support multiple connected networks |
| 1506 | * of the same type. Use {@link #getAllNetworks}, |
| 1507 | * {@link #getNetworkInfo(android.net.Network)}, and |
| 1508 | * {@link #getLinkProperties(android.net.Network)} instead. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1509 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 1510 | @Deprecated |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1511 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | a3b7751 | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 1512 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Robert Greenwalt | 9f0ee4f | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1513 | public LinkProperties getLinkProperties(int networkType) { |
| 1514 | try { |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1515 | return mService.getLinkPropertiesForType(networkType); |
| 1516 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1517 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1518 | } |
| 1519 | } |
| 1520 | |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1521 | /** |
| 1522 | * Get the {@link LinkProperties} for the given {@link Network}. This |
| 1523 | * will return {@code null} if the network is unknown. |
| 1524 | * |
| 1525 | * @param network The {@link Network} object identifying the network in question. |
| 1526 | * @return The {@link LinkProperties} for the network, or {@code null}. |
Paul Jensen | 08f9dbb | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1527 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1528 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1529 | @Nullable |
| 1530 | public LinkProperties getLinkProperties(@Nullable Network network) { |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1531 | try { |
| 1532 | return mService.getLinkProperties(network); |
| 1533 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1534 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1535 | } |
| 1536 | } |
| 1537 | |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1538 | /** |
Roshan Pius | 951c003 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 1539 | * Get the {@link NetworkCapabilities} for the given {@link Network}. This |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1540 | * will return {@code null} if the network is unknown. |
| 1541 | * |
Roshan Pius | 951c003 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 1542 | * This will remove any location sensitive data in {@link TransportInfo} embedded in |
| 1543 | * {@link NetworkCapabilities#getTransportInfo()}. Some transport info instances like |
| 1544 | * {@link android.net.wifi.WifiInfo} contain location sensitive information. Retrieving |
| 1545 | * this location sensitive information (subject to app's location permissions) will be |
| 1546 | * noted by system. To include any location sensitive data in {@link TransportInfo}, |
| 1547 | * use a {@link NetworkCallback} with |
| 1548 | * {@link NetworkCallback#FLAG_INCLUDE_LOCATION_INFO} flag. |
| 1549 | * |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1550 | * @param network The {@link Network} object identifying the network in question. |
Roshan Pius | 951c003 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 1551 | * @return The {@link NetworkCapabilities} for the network, or {@code null}. |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1552 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1553 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1554 | @Nullable |
| 1555 | public NetworkCapabilities getNetworkCapabilities(@Nullable Network network) { |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1556 | try { |
Roshan Pius | aa24fde | 2020-12-17 14:53:09 -0800 | [diff] [blame] | 1557 | return mService.getNetworkCapabilities( |
| 1558 | network, mContext.getOpPackageName(), getAttributionTag()); |
Robert Greenwalt | 9f0ee4f | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1559 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1560 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 9f0ee4f | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1561 | } |
| 1562 | } |
| 1563 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1564 | /** |
Remi NGUYEN VAN | 035f653 | 2019-03-20 14:22:49 +0900 | [diff] [blame] | 1565 | * Gets a URL that can be used for resolving whether a captive portal is present. |
Udam Saini | cd64546 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 1566 | * 1. This URL should respond with a 204 response to a GET request to indicate no captive |
| 1567 | * portal is present. |
| 1568 | * 2. This URL must be HTTP as redirect responses are used to find captive portal |
| 1569 | * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects. |
| 1570 | * |
Remi NGUYEN VAN | 035f653 | 2019-03-20 14:22:49 +0900 | [diff] [blame] | 1571 | * The system network validation may be using different strategies to detect captive portals, |
| 1572 | * so this method does not necessarily return a URL used by the system. It only returns a URL |
| 1573 | * that may be relevant for other components trying to detect captive portals. |
paulhu | 8e96a75 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1574 | * |
Udam Saini | cd64546 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 1575 | * @hide |
paulhu | 8e96a75 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1576 | * @deprecated This API returns URL which is not guaranteed to be one of the URLs used by the |
| 1577 | * system. |
Udam Saini | cd64546 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 1578 | */ |
paulhu | 8e96a75 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1579 | @Deprecated |
Udam Saini | cd64546 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 1580 | @SystemApi |
paulhu | 8e96a75 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1581 | @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS) |
Udam Saini | cd64546 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 1582 | public String getCaptivePortalServerUrl() { |
| 1583 | try { |
| 1584 | return mService.getCaptivePortalServerUrl(); |
| 1585 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1586 | throw e.rethrowFromSystemServer(); |
Udam Saini | cd64546 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 1587 | } |
| 1588 | } |
| 1589 | |
| 1590 | /** |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1591 | * Tells the underlying networking system that the caller wants to |
| 1592 | * begin using the named feature. The interpretation of {@code feature} |
| 1593 | * is completely up to each networking implementation. |
Lorenzo Colitti | 0b40c87 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 1594 | * |
| 1595 | * <p>This method requires the caller to hold either the |
| 1596 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 1597 | * or the ability to modify system settings as determined by |
| 1598 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 1599 | * |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1600 | * @param networkType specifies which network the request pertains to |
| 1601 | * @param feature the name of the feature to be used |
| 1602 | * @return an integer value representing the outcome of the request. |
| 1603 | * The interpretation of this value is specific to each networking |
| 1604 | * implementation+feature combination, except that the value {@code -1} |
| 1605 | * always indicates failure. |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1606 | * |
Lorenzo Colitti | 7f6a2bf | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 1607 | * @deprecated Deprecated in favor of the cleaner |
| 1608 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API. |
Dianne Hackborn | 18c1d83 | 2015-07-31 10:35:34 -0700 | [diff] [blame] | 1609 | * In {@link VERSION_CODES#M}, and above, this method is unsupported and will |
Lorenzo Colitti | 2780e4b | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1610 | * throw {@code UnsupportedOperationException} if called. |
Lorenzo Colitti | 84d7f07 | 2016-12-09 18:39:30 +0900 | [diff] [blame] | 1611 | * @removed |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1612 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 1613 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1614 | public int startUsingNetworkFeature(int networkType, String feature) { |
Lorenzo Colitti | 2780e4b | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1615 | checkLegacyRoutingApiAccess(); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1616 | NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature); |
| 1617 | if (netCap == null) { |
| 1618 | Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " + |
| 1619 | feature); |
Chalard Jean | afaed1a | 2019-11-21 14:48:00 +0900 | [diff] [blame] | 1620 | return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED; |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1621 | } |
| 1622 | |
| 1623 | NetworkRequest request = null; |
| 1624 | synchronized (sLegacyRequests) { |
| 1625 | LegacyRequest l = sLegacyRequests.get(netCap); |
| 1626 | if (l != null) { |
| 1627 | Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest); |
| 1628 | renewRequestLocked(l); |
| 1629 | if (l.currentNetwork != null) { |
Chalard Jean | afaed1a | 2019-11-21 14:48:00 +0900 | [diff] [blame] | 1630 | return DEPRECATED_PHONE_CONSTANT_APN_ALREADY_ACTIVE; |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1631 | } else { |
Chalard Jean | afaed1a | 2019-11-21 14:48:00 +0900 | [diff] [blame] | 1632 | return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED; |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1633 | } |
| 1634 | } |
| 1635 | |
| 1636 | request = requestNetworkForFeatureLocked(netCap); |
| 1637 | } |
| 1638 | if (request != null) { |
Robert Greenwalt | b840173 | 2014-06-20 10:58:45 -0700 | [diff] [blame] | 1639 | Log.d(TAG, "starting startUsingNetworkFeature for request " + request); |
Chalard Jean | afaed1a | 2019-11-21 14:48:00 +0900 | [diff] [blame] | 1640 | return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED; |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1641 | } else { |
| 1642 | Log.d(TAG, " request Failed"); |
Chalard Jean | afaed1a | 2019-11-21 14:48:00 +0900 | [diff] [blame] | 1643 | return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1644 | } |
| 1645 | } |
| 1646 | |
| 1647 | /** |
| 1648 | * Tells the underlying networking system that the caller is finished |
| 1649 | * using the named feature. The interpretation of {@code feature} |
| 1650 | * is completely up to each networking implementation. |
Lorenzo Colitti | 0b40c87 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 1651 | * |
| 1652 | * <p>This method requires the caller to hold either the |
| 1653 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 1654 | * or the ability to modify system settings as determined by |
| 1655 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 1656 | * |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1657 | * @param networkType specifies which network the request pertains to |
| 1658 | * @param feature the name of the feature that is no longer needed |
| 1659 | * @return an integer value representing the outcome of the request. |
| 1660 | * The interpretation of this value is specific to each networking |
| 1661 | * implementation+feature combination, except that the value {@code -1} |
| 1662 | * always indicates failure. |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1663 | * |
Lorenzo Colitti | 15874cd | 2016-04-13 22:00:02 +0900 | [diff] [blame] | 1664 | * @deprecated Deprecated in favor of the cleaner |
| 1665 | * {@link #unregisterNetworkCallback(NetworkCallback)} API. |
Dianne Hackborn | 18c1d83 | 2015-07-31 10:35:34 -0700 | [diff] [blame] | 1666 | * In {@link VERSION_CODES#M}, and above, this method is unsupported and will |
Lorenzo Colitti | 2780e4b | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1667 | * throw {@code UnsupportedOperationException} if called. |
Lorenzo Colitti | 84d7f07 | 2016-12-09 18:39:30 +0900 | [diff] [blame] | 1668 | * @removed |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1669 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 1670 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1671 | public int stopUsingNetworkFeature(int networkType, String feature) { |
Lorenzo Colitti | 2780e4b | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1672 | checkLegacyRoutingApiAccess(); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1673 | NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature); |
| 1674 | if (netCap == null) { |
| 1675 | Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " + |
| 1676 | feature); |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1677 | return -1; |
| 1678 | } |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1679 | |
Paul Jensen | 49f74a3 | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1680 | if (removeRequestForFeature(netCap)) { |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1681 | Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1682 | } |
| 1683 | return 1; |
| 1684 | } |
| 1685 | |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 1686 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1687 | private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) { |
| 1688 | if (networkType == TYPE_MOBILE) { |
Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 1689 | switch (feature) { |
| 1690 | case "enableCBS": |
| 1691 | return networkCapabilitiesForType(TYPE_MOBILE_CBS); |
| 1692 | case "enableDUN": |
| 1693 | case "enableDUNAlways": |
| 1694 | return networkCapabilitiesForType(TYPE_MOBILE_DUN); |
| 1695 | case "enableFOTA": |
| 1696 | return networkCapabilitiesForType(TYPE_MOBILE_FOTA); |
| 1697 | case "enableHIPRI": |
| 1698 | return networkCapabilitiesForType(TYPE_MOBILE_HIPRI); |
| 1699 | case "enableIMS": |
| 1700 | return networkCapabilitiesForType(TYPE_MOBILE_IMS); |
| 1701 | case "enableMMS": |
| 1702 | return networkCapabilitiesForType(TYPE_MOBILE_MMS); |
| 1703 | case "enableSUPL": |
| 1704 | return networkCapabilitiesForType(TYPE_MOBILE_SUPL); |
| 1705 | default: |
| 1706 | return null; |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1707 | } |
Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 1708 | } else if (networkType == TYPE_WIFI && "p2p".equals(feature)) { |
| 1709 | return networkCapabilitiesForType(TYPE_WIFI_P2P); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1710 | } |
| 1711 | return null; |
| 1712 | } |
| 1713 | |
Robert Greenwalt | 802c110 | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 1714 | private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) { |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1715 | if (netCap == null) return TYPE_NONE; |
| 1716 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) { |
| 1717 | return TYPE_MOBILE_CBS; |
| 1718 | } |
| 1719 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) { |
| 1720 | return TYPE_MOBILE_IMS; |
| 1721 | } |
| 1722 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) { |
| 1723 | return TYPE_MOBILE_FOTA; |
| 1724 | } |
| 1725 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) { |
| 1726 | return TYPE_MOBILE_DUN; |
| 1727 | } |
| 1728 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) { |
| 1729 | return TYPE_MOBILE_SUPL; |
| 1730 | } |
| 1731 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) { |
| 1732 | return TYPE_MOBILE_MMS; |
| 1733 | } |
| 1734 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) { |
| 1735 | return TYPE_MOBILE_HIPRI; |
| 1736 | } |
Robert Greenwalt | 802c110 | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 1737 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) { |
| 1738 | return TYPE_WIFI_P2P; |
| 1739 | } |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1740 | return TYPE_NONE; |
| 1741 | } |
| 1742 | |
| 1743 | private static class LegacyRequest { |
| 1744 | NetworkCapabilities networkCapabilities; |
| 1745 | NetworkRequest networkRequest; |
| 1746 | int expireSequenceNumber; |
| 1747 | Network currentNetwork; |
| 1748 | int delay = -1; |
Paul Jensen | 49f74a3 | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1749 | |
| 1750 | private void clearDnsBinding() { |
| 1751 | if (currentNetwork != null) { |
| 1752 | currentNetwork = null; |
| 1753 | setProcessDefaultNetworkForHostResolution(null); |
| 1754 | } |
| 1755 | } |
| 1756 | |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1757 | NetworkCallback networkCallback = new NetworkCallback() { |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1758 | @Override |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1759 | public void onAvailable(Network network) { |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1760 | currentNetwork = network; |
| 1761 | Log.d(TAG, "startUsingNetworkFeature got Network:" + network); |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 1762 | setProcessDefaultNetworkForHostResolution(network); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1763 | } |
| 1764 | @Override |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1765 | public void onLost(Network network) { |
Paul Jensen | 49f74a3 | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1766 | if (network.equals(currentNetwork)) clearDnsBinding(); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1767 | Log.d(TAG, "startUsingNetworkFeature lost Network:" + network); |
| 1768 | } |
| 1769 | }; |
| 1770 | } |
| 1771 | |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 1772 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 1773 | private static final HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests = |
| 1774 | new HashMap<>(); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1775 | |
| 1776 | private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) { |
| 1777 | synchronized (sLegacyRequests) { |
| 1778 | LegacyRequest l = sLegacyRequests.get(netCap); |
| 1779 | if (l != null) return l.networkRequest; |
| 1780 | } |
| 1781 | return null; |
| 1782 | } |
| 1783 | |
| 1784 | private void renewRequestLocked(LegacyRequest l) { |
| 1785 | l.expireSequenceNumber++; |
| 1786 | Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber); |
| 1787 | sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay); |
| 1788 | } |
| 1789 | |
| 1790 | private void expireRequest(NetworkCapabilities netCap, int sequenceNum) { |
| 1791 | int ourSeqNum = -1; |
| 1792 | synchronized (sLegacyRequests) { |
| 1793 | LegacyRequest l = sLegacyRequests.get(netCap); |
| 1794 | if (l == null) return; |
| 1795 | ourSeqNum = l.expireSequenceNumber; |
Paul Jensen | 49f74a3 | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1796 | if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1797 | } |
| 1798 | Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum); |
| 1799 | } |
| 1800 | |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 1801 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1802 | private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) { |
| 1803 | int delay = -1; |
Robert Greenwalt | 802c110 | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 1804 | int type = legacyTypeForNetworkCapabilities(netCap); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1805 | try { |
| 1806 | delay = mService.getRestoreDefaultNetworkDelay(type); |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1807 | } catch (RemoteException e) { |
| 1808 | throw e.rethrowFromSystemServer(); |
| 1809 | } |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1810 | LegacyRequest l = new LegacyRequest(); |
| 1811 | l.networkCapabilities = netCap; |
| 1812 | l.delay = delay; |
| 1813 | l.expireSequenceNumber = 0; |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 1814 | l.networkRequest = sendRequestForNetwork( |
| 1815 | netCap, l.networkCallback, 0, REQUEST, type, getDefaultHandler()); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1816 | if (l.networkRequest == null) return null; |
| 1817 | sLegacyRequests.put(netCap, l); |
| 1818 | sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay); |
| 1819 | return l.networkRequest; |
| 1820 | } |
| 1821 | |
| 1822 | private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) { |
| 1823 | if (delay >= 0) { |
| 1824 | Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay); |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 1825 | CallbackHandler handler = getDefaultHandler(); |
Hugo Benichi | bc4ac97 | 2017-02-03 14:18:44 +0900 | [diff] [blame] | 1826 | Message msg = handler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap); |
| 1827 | handler.sendMessageDelayed(msg, delay); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1828 | } |
| 1829 | } |
| 1830 | |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 1831 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Paul Jensen | 49f74a3 | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1832 | private boolean removeRequestForFeature(NetworkCapabilities netCap) { |
| 1833 | final LegacyRequest l; |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1834 | synchronized (sLegacyRequests) { |
Paul Jensen | 49f74a3 | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1835 | l = sLegacyRequests.remove(netCap); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1836 | } |
Paul Jensen | 49f74a3 | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1837 | if (l == null) return false; |
| 1838 | unregisterNetworkCallback(l.networkCallback); |
| 1839 | l.clearDnsBinding(); |
| 1840 | return true; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1841 | } |
| 1842 | |
Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 1843 | private static final SparseIntArray sLegacyTypeToTransport = new SparseIntArray(); |
| 1844 | static { |
| 1845 | sLegacyTypeToTransport.put(TYPE_MOBILE, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1846 | sLegacyTypeToTransport.put(TYPE_MOBILE_CBS, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1847 | sLegacyTypeToTransport.put(TYPE_MOBILE_DUN, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1848 | sLegacyTypeToTransport.put(TYPE_MOBILE_FOTA, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1849 | sLegacyTypeToTransport.put(TYPE_MOBILE_HIPRI, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1850 | sLegacyTypeToTransport.put(TYPE_MOBILE_IMS, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1851 | sLegacyTypeToTransport.put(TYPE_MOBILE_MMS, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1852 | sLegacyTypeToTransport.put(TYPE_MOBILE_SUPL, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1853 | sLegacyTypeToTransport.put(TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI); |
| 1854 | sLegacyTypeToTransport.put(TYPE_WIFI_P2P, NetworkCapabilities.TRANSPORT_WIFI); |
| 1855 | sLegacyTypeToTransport.put(TYPE_BLUETOOTH, NetworkCapabilities.TRANSPORT_BLUETOOTH); |
| 1856 | sLegacyTypeToTransport.put(TYPE_ETHERNET, NetworkCapabilities.TRANSPORT_ETHERNET); |
| 1857 | } |
| 1858 | |
| 1859 | private static final SparseIntArray sLegacyTypeToCapability = new SparseIntArray(); |
| 1860 | static { |
| 1861 | sLegacyTypeToCapability.put(TYPE_MOBILE_CBS, NetworkCapabilities.NET_CAPABILITY_CBS); |
| 1862 | sLegacyTypeToCapability.put(TYPE_MOBILE_DUN, NetworkCapabilities.NET_CAPABILITY_DUN); |
| 1863 | sLegacyTypeToCapability.put(TYPE_MOBILE_FOTA, NetworkCapabilities.NET_CAPABILITY_FOTA); |
| 1864 | sLegacyTypeToCapability.put(TYPE_MOBILE_IMS, NetworkCapabilities.NET_CAPABILITY_IMS); |
| 1865 | sLegacyTypeToCapability.put(TYPE_MOBILE_MMS, NetworkCapabilities.NET_CAPABILITY_MMS); |
| 1866 | sLegacyTypeToCapability.put(TYPE_MOBILE_SUPL, NetworkCapabilities.NET_CAPABILITY_SUPL); |
| 1867 | sLegacyTypeToCapability.put(TYPE_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P); |
| 1868 | } |
| 1869 | |
| 1870 | /** |
| 1871 | * Given a legacy type (TYPE_WIFI, ...) returns a NetworkCapabilities |
| 1872 | * instance suitable for registering a request or callback. Throws an |
| 1873 | * IllegalArgumentException if no mapping from the legacy type to |
| 1874 | * NetworkCapabilities is known. |
| 1875 | * |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 1876 | * @deprecated Types are deprecated. Use {@link NetworkCallback} or {@link NetworkRequest} |
| 1877 | * to find the network instead. |
Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 1878 | * @hide |
| 1879 | */ |
| 1880 | public static NetworkCapabilities networkCapabilitiesForType(int type) { |
| 1881 | final NetworkCapabilities nc = new NetworkCapabilities(); |
| 1882 | |
| 1883 | // Map from type to transports. |
| 1884 | final int NOT_FOUND = -1; |
| 1885 | final int transport = sLegacyTypeToTransport.get(type, NOT_FOUND); |
Remi NGUYEN VAN | 81f6842 | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 1886 | if (transport == NOT_FOUND) { |
| 1887 | throw new IllegalArgumentException("unknown legacy type: " + type); |
| 1888 | } |
Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 1889 | nc.addTransportType(transport); |
| 1890 | |
| 1891 | // Map from type to capabilities. |
| 1892 | nc.addCapability(sLegacyTypeToCapability.get( |
| 1893 | type, NetworkCapabilities.NET_CAPABILITY_INTERNET)); |
| 1894 | nc.maybeMarkCapabilitiesRestricted(); |
| 1895 | return nc; |
| 1896 | } |
| 1897 | |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1898 | /** @hide */ |
| 1899 | public static class PacketKeepaliveCallback { |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 1900 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Artur Satayev | 56cb6bb | 2019-11-04 17:50:59 +0000 | [diff] [blame] | 1901 | public PacketKeepaliveCallback() { |
| 1902 | } |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1903 | /** The requested keepalive was successfully started. */ |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 1904 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1905 | public void onStarted() {} |
| 1906 | /** The keepalive was successfully stopped. */ |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 1907 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1908 | public void onStopped() {} |
| 1909 | /** An error occurred. */ |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 1910 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1911 | public void onError(int error) {} |
| 1912 | } |
| 1913 | |
| 1914 | /** |
| 1915 | * Allows applications to request that the system periodically send specific packets on their |
| 1916 | * behalf, using hardware offload to save battery power. |
| 1917 | * |
| 1918 | * To request that the system send keepalives, call one of the methods that return a |
| 1919 | * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive}, |
| 1920 | * passing in a non-null callback. If the callback is successfully started, the callback's |
| 1921 | * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called, |
| 1922 | * specifying one of the {@code ERROR_*} constants in this class. |
| 1923 | * |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 1924 | * To stop an existing keepalive, call {@link PacketKeepalive#stop}. The system will call |
| 1925 | * {@link PacketKeepaliveCallback#onStopped} if the operation was successful or |
| 1926 | * {@link PacketKeepaliveCallback#onError} if an error occurred. |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1927 | * |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 1928 | * @deprecated Use {@link SocketKeepalive} instead. |
| 1929 | * |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1930 | * @hide |
| 1931 | */ |
| 1932 | public class PacketKeepalive { |
| 1933 | |
| 1934 | private static final String TAG = "PacketKeepalive"; |
| 1935 | |
| 1936 | /** @hide */ |
| 1937 | public static final int SUCCESS = 0; |
| 1938 | |
| 1939 | /** @hide */ |
| 1940 | public static final int NO_KEEPALIVE = -1; |
| 1941 | |
| 1942 | /** @hide */ |
| 1943 | public static final int BINDER_DIED = -10; |
| 1944 | |
| 1945 | /** The specified {@code Network} is not connected. */ |
| 1946 | public static final int ERROR_INVALID_NETWORK = -20; |
| 1947 | /** The specified IP addresses are invalid. For example, the specified source IP address is |
| 1948 | * not configured on the specified {@code Network}. */ |
| 1949 | public static final int ERROR_INVALID_IP_ADDRESS = -21; |
| 1950 | /** The requested port is invalid. */ |
| 1951 | public static final int ERROR_INVALID_PORT = -22; |
| 1952 | /** The packet length is invalid (e.g., too long). */ |
| 1953 | public static final int ERROR_INVALID_LENGTH = -23; |
| 1954 | /** The packet transmission interval is invalid (e.g., too short). */ |
| 1955 | public static final int ERROR_INVALID_INTERVAL = -24; |
| 1956 | |
| 1957 | /** The hardware does not support this request. */ |
| 1958 | public static final int ERROR_HARDWARE_UNSUPPORTED = -30; |
Lorenzo Colitti | 723f82f | 2015-09-08 16:46:36 +0900 | [diff] [blame] | 1959 | /** The hardware returned an error. */ |
| 1960 | public static final int ERROR_HARDWARE_ERROR = -31; |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1961 | |
Nathan Harold | 0990bc8 | 2018-02-14 13:09:45 -0800 | [diff] [blame] | 1962 | /** The NAT-T destination port for IPsec */ |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1963 | public static final int NATT_PORT = 4500; |
| 1964 | |
Nathan Harold | 0990bc8 | 2018-02-14 13:09:45 -0800 | [diff] [blame] | 1965 | /** The minimum interval in seconds between keepalive packet transmissions */ |
| 1966 | public static final int MIN_INTERVAL = 10; |
| 1967 | |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1968 | private final Network mNetwork; |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 1969 | private final ISocketKeepaliveCallback mCallback; |
| 1970 | private final ExecutorService mExecutor; |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1971 | |
| 1972 | private volatile Integer mSlot; |
| 1973 | |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 1974 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1975 | public void stop() { |
| 1976 | try { |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 1977 | mExecutor.execute(() -> { |
| 1978 | try { |
| 1979 | if (mSlot != null) { |
| 1980 | mService.stopKeepalive(mNetwork, mSlot); |
| 1981 | } |
| 1982 | } catch (RemoteException e) { |
| 1983 | Log.e(TAG, "Error stopping packet keepalive: ", e); |
| 1984 | throw e.rethrowFromSystemServer(); |
| 1985 | } |
| 1986 | }); |
| 1987 | } catch (RejectedExecutionException e) { |
| 1988 | // The internal executor has already stopped due to previous event. |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1989 | } |
| 1990 | } |
| 1991 | |
| 1992 | private PacketKeepalive(Network network, PacketKeepaliveCallback callback) { |
Remi NGUYEN VAN | 81f6842 | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 1993 | Objects.requireNonNull(network, "network cannot be null"); |
| 1994 | Objects.requireNonNull(callback, "callback cannot be null"); |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1995 | mNetwork = network; |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 1996 | mExecutor = Executors.newSingleThreadExecutor(); |
| 1997 | mCallback = new ISocketKeepaliveCallback.Stub() { |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1998 | @Override |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 1999 | public void onStarted(int slot) { |
lucaslin | be80138 | 2020-12-30 11:54:55 +0800 | [diff] [blame] | 2000 | final long token = Binder.clearCallingIdentity(); |
| 2001 | try { |
| 2002 | mExecutor.execute(() -> { |
| 2003 | mSlot = slot; |
| 2004 | callback.onStarted(); |
| 2005 | }); |
| 2006 | } finally { |
| 2007 | Binder.restoreCallingIdentity(token); |
| 2008 | } |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2009 | } |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 2010 | |
| 2011 | @Override |
| 2012 | public void onStopped() { |
lucaslin | be80138 | 2020-12-30 11:54:55 +0800 | [diff] [blame] | 2013 | final long token = Binder.clearCallingIdentity(); |
| 2014 | try { |
| 2015 | mExecutor.execute(() -> { |
| 2016 | mSlot = null; |
| 2017 | callback.onStopped(); |
| 2018 | }); |
| 2019 | } finally { |
| 2020 | Binder.restoreCallingIdentity(token); |
| 2021 | } |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 2022 | mExecutor.shutdown(); |
| 2023 | } |
| 2024 | |
| 2025 | @Override |
| 2026 | public void onError(int error) { |
lucaslin | be80138 | 2020-12-30 11:54:55 +0800 | [diff] [blame] | 2027 | final long token = Binder.clearCallingIdentity(); |
| 2028 | try { |
| 2029 | mExecutor.execute(() -> { |
| 2030 | mSlot = null; |
| 2031 | callback.onError(error); |
| 2032 | }); |
| 2033 | } finally { |
| 2034 | Binder.restoreCallingIdentity(token); |
| 2035 | } |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 2036 | mExecutor.shutdown(); |
| 2037 | } |
| 2038 | |
| 2039 | @Override |
| 2040 | public void onDataReceived() { |
| 2041 | // PacketKeepalive is only used for Nat-T keepalive and as such does not invoke |
| 2042 | // this callback when data is received. |
| 2043 | } |
| 2044 | }; |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2045 | } |
| 2046 | } |
| 2047 | |
| 2048 | /** |
| 2049 | * Starts an IPsec NAT-T keepalive packet with the specified parameters. |
| 2050 | * |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 2051 | * @deprecated Use {@link #createSocketKeepalive} instead. |
| 2052 | * |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2053 | * @hide |
| 2054 | */ |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 2055 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2056 | public PacketKeepalive startNattKeepalive( |
| 2057 | Network network, int intervalSeconds, PacketKeepaliveCallback callback, |
| 2058 | InetAddress srcAddr, int srcPort, InetAddress dstAddr) { |
| 2059 | final PacketKeepalive k = new PacketKeepalive(network, callback); |
| 2060 | try { |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 2061 | mService.startNattKeepalive(network, intervalSeconds, k.mCallback, |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2062 | srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress()); |
| 2063 | } catch (RemoteException e) { |
| 2064 | Log.e(TAG, "Error starting packet keepalive: ", e); |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 2065 | throw e.rethrowFromSystemServer(); |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 2066 | } |
| 2067 | return k; |
| 2068 | } |
| 2069 | |
Chiachang Wang | 619319a | 2021-01-15 11:06:21 +0800 | [diff] [blame] | 2070 | // Construct an invalid fd. |
| 2071 | private ParcelFileDescriptor createInvalidFd() { |
| 2072 | final int invalidFd = -1; |
| 2073 | return ParcelFileDescriptor.adoptFd(invalidFd); |
| 2074 | } |
| 2075 | |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2076 | /** |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 2077 | * Request that keepalives be started on a IPsec NAT-T socket. |
| 2078 | * |
| 2079 | * @param network The {@link Network} the socket is on. |
| 2080 | * @param socket The socket that needs to be kept alive. |
| 2081 | * @param source The source address of the {@link UdpEncapsulationSocket}. |
| 2082 | * @param destination The destination address of the {@link UdpEncapsulationSocket}. |
| 2083 | * @param executor The executor on which callback will be invoked. The provided {@link Executor} |
| 2084 | * must run callback sequentially, otherwise the order of callbacks cannot be |
| 2085 | * guaranteed. |
| 2086 | * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive |
| 2087 | * changes. Must be extended by applications that use this API. |
| 2088 | * |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 2089 | * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the |
| 2090 | * given socket. |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 2091 | **/ |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 2092 | public @NonNull SocketKeepalive createSocketKeepalive(@NonNull Network network, |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 2093 | @NonNull UdpEncapsulationSocket socket, |
| 2094 | @NonNull InetAddress source, |
| 2095 | @NonNull InetAddress destination, |
| 2096 | @NonNull @CallbackExecutor Executor executor, |
| 2097 | @NonNull Callback callback) { |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 2098 | ParcelFileDescriptor dup; |
| 2099 | try { |
junyulai | 828dad1 | 2019-03-27 11:00:37 +0800 | [diff] [blame] | 2100 | // Dup is needed here as the pfd inside the socket is owned by the IpSecService, |
| 2101 | // which cannot be obtained by the app process. |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 2102 | dup = ParcelFileDescriptor.dup(socket.getFileDescriptor()); |
| 2103 | } catch (IOException ignored) { |
| 2104 | // Construct an invalid fd, so that if the user later calls start(), it will fail with |
| 2105 | // ERROR_INVALID_SOCKET. |
Chiachang Wang | 619319a | 2021-01-15 11:06:21 +0800 | [diff] [blame] | 2106 | dup = createInvalidFd(); |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 2107 | } |
| 2108 | return new NattSocketKeepalive(mService, network, dup, socket.getResourceId(), source, |
| 2109 | destination, executor, callback); |
junyulai | d05a192 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 2110 | } |
| 2111 | |
| 2112 | /** |
| 2113 | * Request that keepalives be started on a IPsec NAT-T socket file descriptor. Directly called |
| 2114 | * by system apps which don't use IpSecService to create {@link UdpEncapsulationSocket}. |
| 2115 | * |
| 2116 | * @param network The {@link Network} the socket is on. |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 2117 | * @param pfd The {@link ParcelFileDescriptor} that needs to be kept alive. The provided |
| 2118 | * {@link ParcelFileDescriptor} must be bound to a port and the keepalives will be sent |
| 2119 | * from that port. |
junyulai | d05a192 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 2120 | * @param source The source address of the {@link UdpEncapsulationSocket}. |
| 2121 | * @param destination The destination address of the {@link UdpEncapsulationSocket}. The |
| 2122 | * keepalive packets will always be sent to port 4500 of the given {@code destination}. |
| 2123 | * @param executor The executor on which callback will be invoked. The provided {@link Executor} |
| 2124 | * must run callback sequentially, otherwise the order of callbacks cannot be |
| 2125 | * guaranteed. |
| 2126 | * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive |
| 2127 | * changes. Must be extended by applications that use this API. |
| 2128 | * |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 2129 | * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the |
| 2130 | * given socket. |
junyulai | d05a192 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 2131 | * @hide |
| 2132 | */ |
| 2133 | @SystemApi |
| 2134 | @RequiresPermission(android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD) |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 2135 | public @NonNull SocketKeepalive createNattKeepalive(@NonNull Network network, |
| 2136 | @NonNull ParcelFileDescriptor pfd, |
junyulai | d05a192 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 2137 | @NonNull InetAddress source, |
| 2138 | @NonNull InetAddress destination, |
| 2139 | @NonNull @CallbackExecutor Executor executor, |
| 2140 | @NonNull Callback callback) { |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 2141 | ParcelFileDescriptor dup; |
| 2142 | try { |
junyulai | 828dad1 | 2019-03-27 11:00:37 +0800 | [diff] [blame] | 2143 | // TODO: Consider remove unnecessary dup. |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 2144 | dup = pfd.dup(); |
| 2145 | } catch (IOException ignored) { |
| 2146 | // Construct an invalid fd, so that if the user later calls start(), it will fail with |
| 2147 | // ERROR_INVALID_SOCKET. |
Chiachang Wang | 619319a | 2021-01-15 11:06:21 +0800 | [diff] [blame] | 2148 | dup = createInvalidFd(); |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 2149 | } |
| 2150 | return new NattSocketKeepalive(mService, network, dup, |
Remi NGUYEN VAN | ce35550 | 2021-03-11 10:56:49 +0000 | [diff] [blame] | 2151 | -1 /* Unused */, source, destination, executor, callback); |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 2152 | } |
| 2153 | |
| 2154 | /** |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 2155 | * Request that keepalives be started on a TCP socket. |
| 2156 | * The socket must be established. |
| 2157 | * |
| 2158 | * @param network The {@link Network} the socket is on. |
| 2159 | * @param socket The socket that needs to be kept alive. |
| 2160 | * @param executor The executor on which callback will be invoked. This implementation assumes |
| 2161 | * the provided {@link Executor} runs the callbacks in sequence with no |
| 2162 | * concurrency. Failing this, no guarantee of correctness can be made. It is |
| 2163 | * the responsibility of the caller to ensure the executor provides this |
| 2164 | * guarantee. A simple way of creating such an executor is with the standard |
| 2165 | * tool {@code Executors.newSingleThreadExecutor}. |
| 2166 | * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive |
| 2167 | * changes. Must be extended by applications that use this API. |
| 2168 | * |
| 2169 | * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the |
| 2170 | * given socket. |
| 2171 | * @hide |
| 2172 | */ |
| 2173 | @SystemApi |
| 2174 | @RequiresPermission(android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD) |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 2175 | public @NonNull SocketKeepalive createSocketKeepalive(@NonNull Network network, |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 2176 | @NonNull Socket socket, |
| 2177 | @NonNull Executor executor, |
| 2178 | @NonNull Callback callback) { |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 2179 | ParcelFileDescriptor dup; |
| 2180 | try { |
| 2181 | dup = ParcelFileDescriptor.fromSocket(socket); |
| 2182 | } catch (UncheckedIOException ignored) { |
| 2183 | // Construct an invalid fd, so that if the user later calls start(), it will fail with |
| 2184 | // ERROR_INVALID_SOCKET. |
Chiachang Wang | 619319a | 2021-01-15 11:06:21 +0800 | [diff] [blame] | 2185 | dup = createInvalidFd(); |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 2186 | } |
| 2187 | return new TcpSocketKeepalive(mService, network, dup, executor, callback); |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 2188 | } |
| 2189 | |
| 2190 | /** |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2191 | * Ensure that a network route exists to deliver traffic to the specified |
| 2192 | * host via the specified network interface. An attempt to add a route that |
| 2193 | * already exists is ignored, but treated as successful. |
Lorenzo Colitti | 0b40c87 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 2194 | * |
| 2195 | * <p>This method requires the caller to hold either the |
| 2196 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2197 | * or the ability to modify system settings as determined by |
| 2198 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 2199 | * |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2200 | * @param networkType the type of the network over which traffic to the specified |
| 2201 | * host is to be routed |
| 2202 | * @param hostAddress the IP address of the host to which the route is desired |
| 2203 | * @return {@code true} on success, {@code false} on failure |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2204 | * |
Lorenzo Colitti | 7f6a2bf | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 2205 | * @deprecated Deprecated in favor of the |
| 2206 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, |
| 2207 | * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API. |
Dianne Hackborn | 18c1d83 | 2015-07-31 10:35:34 -0700 | [diff] [blame] | 2208 | * In {@link VERSION_CODES#M}, and above, this method is unsupported and will |
Lorenzo Colitti | 2780e4b | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 2209 | * throw {@code UnsupportedOperationException} if called. |
Lorenzo Colitti | 84d7f07 | 2016-12-09 18:39:30 +0900 | [diff] [blame] | 2210 | * @removed |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2211 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 2212 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2213 | public boolean requestRouteToHost(int networkType, int hostAddress) { |
Sreeram Ramachandran | c06ec73 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 2214 | return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress)); |
Robert Greenwalt | 7fe44cb | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 2215 | } |
| 2216 | |
| 2217 | /** |
| 2218 | * Ensure that a network route exists to deliver traffic to the specified |
| 2219 | * host via the specified network interface. An attempt to add a route that |
| 2220 | * already exists is ignored, but treated as successful. |
Lorenzo Colitti | 0b40c87 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 2221 | * |
| 2222 | * <p>This method requires the caller to hold either the |
| 2223 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2224 | * or the ability to modify system settings as determined by |
| 2225 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 2226 | * |
Robert Greenwalt | 7fe44cb | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 2227 | * @param networkType the type of the network over which traffic to the specified |
| 2228 | * host is to be routed |
| 2229 | * @param hostAddress the IP address of the host to which the route is desired |
| 2230 | * @return {@code true} on success, {@code false} on failure |
| 2231 | * @hide |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2232 | * @deprecated Deprecated in favor of the {@link #requestNetwork} and |
Lorenzo Colitti | 7f6a2bf | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 2233 | * {@link #bindProcessToNetwork} API. |
Robert Greenwalt | 7fe44cb | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 2234 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 2235 | @Deprecated |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2236 | @UnsupportedAppUsage |
lucaslin | e257bce | 2021-03-22 11:51:27 +0800 | [diff] [blame] | 2237 | @SystemApi(client = MODULE_LIBRARIES) |
Robert Greenwalt | 7fe44cb | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 2238 | public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) { |
Lorenzo Colitti | 2780e4b | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 2239 | checkLegacyRoutingApiAccess(); |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2240 | try { |
Philip P. Moltmann | 7bc33df | 2020-03-26 11:50:35 -0700 | [diff] [blame] | 2241 | return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress(), |
| 2242 | mContext.getOpPackageName(), getAttributionTag()); |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2243 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2244 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2245 | } |
| 2246 | } |
| 2247 | |
| 2248 | /** |
Philip P. Moltmann | 7bc33df | 2020-03-26 11:50:35 -0700 | [diff] [blame] | 2249 | * @return the context's attribution tag |
| 2250 | */ |
| 2251 | // TODO: Remove method and replace with direct call once R code is pushed to AOSP |
| 2252 | private @Nullable String getAttributionTag() { |
Roshan Pius | aa24fde | 2020-12-17 14:53:09 -0800 | [diff] [blame] | 2253 | return mContext.getAttributionTag(); |
Philip P. Moltmann | 7bc33df | 2020-03-26 11:50:35 -0700 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | /** |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2257 | * Returns the value of the setting for background data usage. If false, |
| 2258 | * applications should not use the network if the application is not in the |
| 2259 | * foreground. Developers should respect this setting, and check the value |
| 2260 | * of this before performing any background data operations. |
| 2261 | * <p> |
| 2262 | * All applications that have background services that use the network |
| 2263 | * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}. |
Jeff Sharkey | 39c01eb | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2264 | * <p> |
Scott Main | 5058914 | 2011-10-06 18:32:43 -0700 | [diff] [blame] | 2265 | * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of |
Jeff Sharkey | 39c01eb | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2266 | * background data depends on several combined factors, and this method will |
| 2267 | * always return {@code true}. Instead, when background data is unavailable, |
| 2268 | * {@link #getActiveNetworkInfo()} will now appear disconnected. |
Danica Chang | 9656705 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2269 | * |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2270 | * @return Whether background data usage is allowed. |
| 2271 | */ |
Jeff Sharkey | 39c01eb | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2272 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2273 | public boolean getBackgroundDataSetting() { |
Jeff Sharkey | 39c01eb | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2274 | // assume that background data is allowed; final authority is |
| 2275 | // NetworkInfo which may be blocked. |
| 2276 | return true; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2277 | } |
| 2278 | |
| 2279 | /** |
| 2280 | * Sets the value of the setting for background data usage. |
Robert Greenwalt | 1b21f6c | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2281 | * |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2282 | * @param allowBackgroundData Whether an application should use data while |
| 2283 | * it is in the background. |
Robert Greenwalt | 1b21f6c | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2284 | * |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2285 | * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING |
| 2286 | * @see #getBackgroundDataSetting() |
| 2287 | * @hide |
| 2288 | */ |
Jeff Sharkey | 39c01eb | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2289 | @Deprecated |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2290 | @UnsupportedAppUsage |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2291 | public void setBackgroundDataSetting(boolean allowBackgroundData) { |
Jeff Sharkey | 39c01eb | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2292 | // ignored |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2293 | } |
Robert Greenwalt | 1b21f6c | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2294 | |
Jeff Sharkey | 66fa968 | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 2295 | /** |
Robert Greenwalt | 1b21f6c | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2296 | * @hide |
Robert Greenwalt | 0c150c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 2297 | * @deprecated Talk to TelephonyManager directly |
Robert Greenwalt | 1b21f6c | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2298 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 2299 | @Deprecated |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2300 | @UnsupportedAppUsage |
Robert Greenwalt | 1b21f6c | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2301 | public boolean getMobileDataEnabled() { |
Meng Wang | ed6f441 | 2019-11-18 17:10:00 -0800 | [diff] [blame] | 2302 | TelephonyManager tm = mContext.getSystemService(TelephonyManager.class); |
| 2303 | if (tm != null) { |
| 2304 | int subId = SubscriptionManager.getDefaultDataSubscriptionId(); |
| 2305 | Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId); |
| 2306 | boolean retVal = tm.createForSubscriptionId(subId).isDataEnabled(); |
| 2307 | Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId |
| 2308 | + " retVal=" + retVal); |
| 2309 | return retVal; |
Robert Greenwalt | 1b21f6c | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2310 | } |
Wink Saville | 689f704 | 2014-12-05 11:10:30 -0800 | [diff] [blame] | 2311 | Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false"); |
Robert Greenwalt | 0c150c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 2312 | return false; |
Robert Greenwalt | 1b21f6c | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2313 | } |
| 2314 | |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2315 | /** |
Robert Greenwalt | ad35b13 | 2014-09-04 16:44:35 -0700 | [diff] [blame] | 2316 | * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener} |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2317 | * to find out when the system default network has gone in to a high power state. |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2318 | */ |
| 2319 | public interface OnNetworkActiveListener { |
| 2320 | /** |
| 2321 | * Called on the main thread of the process to report that the current data network |
| 2322 | * has become active, and it is now a good time to perform any pending network |
| 2323 | * operations. Note that this listener only tells you when the network becomes |
| 2324 | * active; if at any other time you want to know whether it is active (and thus okay |
| 2325 | * to initiate network traffic), you can retrieve its instantaneous state with |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2326 | * {@link ConnectivityManager#isDefaultNetworkActive}. |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2327 | */ |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 2328 | void onNetworkActive(); |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2329 | } |
| 2330 | |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2331 | private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener> |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 2332 | mNetworkActivityListeners = new ArrayMap<>(); |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2333 | |
| 2334 | /** |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2335 | * Start listening to reports when the system's default data network is active, meaning it is |
| 2336 | * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()} |
| 2337 | * to determine the current state of the system's default network after registering the |
| 2338 | * listener. |
| 2339 | * <p> |
| 2340 | * If the process default network has been set with |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2341 | * {@link ConnectivityManager#bindProcessToNetwork} this function will not |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2342 | * reflect the process's default, but the system default. |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2343 | * |
| 2344 | * @param l The listener to be told when the network is active. |
| 2345 | */ |
Robert Greenwalt | ad35b13 | 2014-09-04 16:44:35 -0700 | [diff] [blame] | 2346 | public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) { |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2347 | INetworkActivityListener rl = new INetworkActivityListener.Stub() { |
| 2348 | @Override |
| 2349 | public void onNetworkActive() throws RemoteException { |
| 2350 | l.onNetworkActive(); |
| 2351 | } |
| 2352 | }; |
| 2353 | |
| 2354 | try { |
lucaslin | 1193a5d | 2021-01-21 02:04:15 +0800 | [diff] [blame] | 2355 | mService.registerNetworkActivityListener(rl); |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2356 | mNetworkActivityListeners.put(l, rl); |
| 2357 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2358 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2359 | } |
| 2360 | } |
| 2361 | |
| 2362 | /** |
| 2363 | * Remove network active listener previously registered with |
Robert Greenwalt | ad35b13 | 2014-09-04 16:44:35 -0700 | [diff] [blame] | 2364 | * {@link #addDefaultNetworkActiveListener}. |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2365 | * |
| 2366 | * @param l Previously registered listener. |
| 2367 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 2368 | public void removeDefaultNetworkActiveListener(@NonNull OnNetworkActiveListener l) { |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2369 | INetworkActivityListener rl = mNetworkActivityListeners.get(l); |
Remi NGUYEN VAN | 81f6842 | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 2370 | if (rl == null) { |
| 2371 | throw new IllegalArgumentException("Listener was not registered."); |
| 2372 | } |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2373 | try { |
lucaslin | 1193a5d | 2021-01-21 02:04:15 +0800 | [diff] [blame] | 2374 | mService.registerNetworkActivityListener(rl); |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2375 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2376 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2377 | } |
| 2378 | } |
| 2379 | |
| 2380 | /** |
| 2381 | * Return whether the data network is currently active. An active network means that |
| 2382 | * it is currently in a high power state for performing data transmission. On some |
| 2383 | * types of networks, it may be expensive to move and stay in such a state, so it is |
| 2384 | * more power efficient to batch network traffic together when the radio is already in |
| 2385 | * this state. This method tells you whether right now is currently a good time to |
| 2386 | * initiate network traffic, as the network is already active. |
| 2387 | */ |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2388 | public boolean isDefaultNetworkActive() { |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2389 | try { |
lucaslin | 1193a5d | 2021-01-21 02:04:15 +0800 | [diff] [blame] | 2390 | return mService.isDefaultNetworkActive(); |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2391 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2392 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2393 | } |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2394 | } |
| 2395 | |
| 2396 | /** |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2397 | * {@hide} |
| 2398 | */ |
Lorenzo Colitti | 2780e4b | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 2399 | public ConnectivityManager(Context context, IConnectivityManager service) { |
Remi NGUYEN VAN | 81f6842 | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 2400 | mContext = Objects.requireNonNull(context, "missing context"); |
| 2401 | mService = Objects.requireNonNull(service, "missing IConnectivityManager"); |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2402 | mTetheringManager = (TetheringManager) mContext.getSystemService(Context.TETHERING_SERVICE); |
Paul Jensen | c0618a6 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 2403 | sInstance = this; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2404 | } |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2405 | |
Jeff Sharkey | 2d9e5a5 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2406 | /** {@hide} */ |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2407 | @UnsupportedAppUsage |
Jeff Sharkey | 2d9e5a5 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2408 | public static ConnectivityManager from(Context context) { |
| 2409 | return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 2410 | } |
| 2411 | |
Remi NGUYEN VAN | 8539129 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2412 | /** @hide */ |
| 2413 | public NetworkRequest getDefaultRequest() { |
| 2414 | try { |
| 2415 | // This is not racy as the default request is final in ConnectivityService. |
| 2416 | return mService.getDefaultRequest(); |
| 2417 | } catch (RemoteException e) { |
| 2418 | throw e.rethrowFromSystemServer(); |
| 2419 | } |
| 2420 | } |
| 2421 | |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2422 | /** |
Philip P. Moltmann | 7bc33df | 2020-03-26 11:50:35 -0700 | [diff] [blame] | 2423 | * Check if the package is a allowed to write settings. This also accounts that such an access |
| 2424 | * happened. |
| 2425 | * |
| 2426 | * @return {@code true} iff the package is allowed to write settings. |
| 2427 | */ |
| 2428 | // TODO: Remove method and replace with direct call once R code is pushed to AOSP |
| 2429 | private static boolean checkAndNoteWriteSettingsOperation(@NonNull Context context, int uid, |
| 2430 | @NonNull String callingPackage, @Nullable String callingAttributionTag, |
| 2431 | boolean throwException) { |
| 2432 | return Settings.checkAndNoteWriteSettingsOperation(context, uid, callingPackage, |
| 2433 | throwException); |
| 2434 | } |
| 2435 | |
| 2436 | /** |
Paul Jensen | c0618a6 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 2437 | * @deprecated - use getSystemService. This is a kludge to support static access in certain |
| 2438 | * situations where a Context pointer is unavailable. |
| 2439 | * @hide |
| 2440 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 2441 | @Deprecated |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2442 | static ConnectivityManager getInstanceOrNull() { |
| 2443 | return sInstance; |
| 2444 | } |
| 2445 | |
| 2446 | /** |
| 2447 | * @deprecated - use getSystemService. This is a kludge to support static access in certain |
| 2448 | * situations where a Context pointer is unavailable. |
| 2449 | * @hide |
| 2450 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 2451 | @Deprecated |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2452 | @UnsupportedAppUsage |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2453 | private static ConnectivityManager getInstance() { |
| 2454 | if (getInstanceOrNull() == null) { |
Paul Jensen | c0618a6 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 2455 | throw new IllegalStateException("No ConnectivityManager yet constructed"); |
| 2456 | } |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2457 | return getInstanceOrNull(); |
Paul Jensen | c0618a6 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 2458 | } |
| 2459 | |
| 2460 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2461 | * Get the set of tetherable, available interfaces. This list is limited by |
| 2462 | * device configuration and current interface existence. |
| 2463 | * |
| 2464 | * @return an array of 0 or more Strings of tetherable interface names. |
| 2465 | * |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2466 | * @deprecated Use {@link TetheringEventCallback#onTetherableInterfacesChanged(List)} instead. |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2467 | * {@hide} |
| 2468 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 2469 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2470 | @UnsupportedAppUsage |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2471 | @Deprecated |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2472 | public String[] getTetherableIfaces() { |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2473 | return mTetheringManager.getTetherableIfaces(); |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2474 | } |
| 2475 | |
| 2476 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2477 | * Get the set of tethered interfaces. |
| 2478 | * |
| 2479 | * @return an array of 0 or more String of currently tethered interface names. |
| 2480 | * |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2481 | * @deprecated Use {@link TetheringEventCallback#onTetherableInterfacesChanged(List)} instead. |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2482 | * {@hide} |
| 2483 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 2484 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2485 | @UnsupportedAppUsage |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2486 | @Deprecated |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2487 | public String[] getTetheredIfaces() { |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2488 | return mTetheringManager.getTetheredIfaces(); |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2489 | } |
| 2490 | |
| 2491 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2492 | * Get the set of interface names which attempted to tether but |
| 2493 | * failed. Re-attempting to tether may cause them to reset to the Tethered |
| 2494 | * state. Alternatively, causing the interface to be destroyed and recreated |
| 2495 | * may cause them to reset to the available state. |
| 2496 | * {@link ConnectivityManager#getLastTetherError} can be used to get more |
| 2497 | * information on the cause of the errors. |
| 2498 | * |
| 2499 | * @return an array of 0 or more String indicating the interface names |
| 2500 | * which failed to tether. |
| 2501 | * |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2502 | * @deprecated Use {@link TetheringEventCallback#onError(String, int)} instead. |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2503 | * {@hide} |
| 2504 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 2505 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2506 | @UnsupportedAppUsage |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2507 | @Deprecated |
Robert Greenwalt | 4283ded | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2508 | public String[] getTetheringErroredIfaces() { |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2509 | return mTetheringManager.getTetheringErroredIfaces(); |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2510 | } |
| 2511 | |
| 2512 | /** |
Robert Greenwalt | e594a76 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 2513 | * Get the set of tethered dhcp ranges. |
| 2514 | * |
markchien | 2e6ba52 | 2020-02-14 11:55:48 +0800 | [diff] [blame] | 2515 | * @deprecated This method is not supported. |
| 2516 | * TODO: remove this function when all of clients are removed. |
Robert Greenwalt | e594a76 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 2517 | * {@hide} |
| 2518 | */ |
paulhu | 8e96a75 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 2519 | @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS) |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2520 | @Deprecated |
Robert Greenwalt | e594a76 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 2521 | public String[] getTetheredDhcpRanges() { |
markchien | 2e6ba52 | 2020-02-14 11:55:48 +0800 | [diff] [blame] | 2522 | throw new UnsupportedOperationException("getTetheredDhcpRanges is not supported"); |
Robert Greenwalt | e594a76 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 2523 | } |
| 2524 | |
| 2525 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2526 | * Attempt to tether the named interface. This will setup a dhcp server |
| 2527 | * on the interface, forward and NAT IP packets and forward DNS requests |
| 2528 | * to the best active upstream network interface. Note that if no upstream |
| 2529 | * IP network interface is available, dhcp will still run and traffic will be |
| 2530 | * allowed between the tethered devices and this device, though upstream net |
| 2531 | * access will of course fail until an upstream network interface becomes |
| 2532 | * active. |
Lorenzo Colitti | 0b40c87 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 2533 | * |
| 2534 | * <p>This method requires the caller to hold either the |
| 2535 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2536 | * or the ability to modify system settings as determined by |
| 2537 | * {@link android.provider.Settings.System#canWrite}.</p> |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2538 | * |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2539 | * <p>WARNING: New clients should not use this function. The only usages should be in PanService |
| 2540 | * and WifiStateMachine which need direct access. All other clients should use |
| 2541 | * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning |
| 2542 | * logic.</p> |
| 2543 | * |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2544 | * @param iface the interface name to tether. |
| 2545 | * @return error a {@code TETHER_ERROR} value indicating success or failure type |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2546 | * @deprecated Use {@link TetheringManager#startTethering} instead |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2547 | * |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2548 | * {@hide} |
| 2549 | */ |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 2550 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2551 | @Deprecated |
Robert Greenwalt | 4283ded | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2552 | public int tether(String iface) { |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2553 | return mTetheringManager.tether(iface); |
Robert Greenwalt | 4283ded | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2554 | } |
| 2555 | |
| 2556 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2557 | * Stop tethering the named interface. |
Lorenzo Colitti | 0b40c87 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 2558 | * |
| 2559 | * <p>This method requires the caller to hold either the |
| 2560 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2561 | * or the ability to modify system settings as determined by |
| 2562 | * {@link android.provider.Settings.System#canWrite}.</p> |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2563 | * |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2564 | * <p>WARNING: New clients should not use this function. The only usages should be in PanService |
| 2565 | * and WifiStateMachine which need direct access. All other clients should use |
| 2566 | * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning |
| 2567 | * logic.</p> |
| 2568 | * |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2569 | * @param iface the interface name to untether. |
| 2570 | * @return error a {@code TETHER_ERROR} value indicating success or failure type |
| 2571 | * |
Robert Greenwalt | 4283ded | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2572 | * {@hide} |
| 2573 | */ |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2574 | @UnsupportedAppUsage |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2575 | @Deprecated |
Robert Greenwalt | 4283ded | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2576 | public int untether(String iface) { |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2577 | return mTetheringManager.untether(iface); |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2578 | } |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2579 | |
| 2580 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2581 | * Check if the device allows for tethering. It may be disabled via |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2582 | * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2583 | * due to device configuration. |
| 2584 | * |
Chalard Jean | ffacaef | 2017-09-26 15:45:18 +0900 | [diff] [blame] | 2585 | * <p>If this app does not have permission to use this API, it will always |
| 2586 | * return false rather than throw an exception.</p> |
| 2587 | * |
| 2588 | * <p>If the device has a hotspot provisioning app, the caller is required to hold the |
| 2589 | * {@link android.Manifest.permission.TETHER_PRIVILEGED} permission.</p> |
| 2590 | * |
| 2591 | * <p>Otherwise, this method requires the caller to hold the ability to modify system |
| 2592 | * settings as determined by {@link android.provider.Settings.System#canWrite}.</p> |
| 2593 | * |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2594 | * @return a boolean - {@code true} indicating Tethering is supported. |
| 2595 | * |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2596 | * @deprecated Use {@link TetheringEventCallback#onTetheringSupported(boolean)} instead. |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2597 | * {@hide} |
| 2598 | */ |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2599 | @SystemApi |
Chalard Jean | ffacaef | 2017-09-26 15:45:18 +0900 | [diff] [blame] | 2600 | @RequiresPermission(anyOf = {android.Manifest.permission.TETHER_PRIVILEGED, |
| 2601 | android.Manifest.permission.WRITE_SETTINGS}) |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2602 | public boolean isTetheringSupported() { |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2603 | return mTetheringManager.isTetheringSupported(); |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2604 | } |
| 2605 | |
| 2606 | /** |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2607 | * Callback for use with {@link #startTethering} to find out whether tethering succeeded. |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2608 | * |
| 2609 | * @deprecated Use {@link TetheringManager.StartTetheringCallback} instead. |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2610 | * @hide |
| 2611 | */ |
| 2612 | @SystemApi |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2613 | @Deprecated |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2614 | public static abstract class OnStartTetheringCallback { |
| 2615 | /** |
| 2616 | * Called when tethering has been successfully started. |
| 2617 | */ |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 2618 | public void onTetheringStarted() {} |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2619 | |
| 2620 | /** |
| 2621 | * Called when starting tethering failed. |
| 2622 | */ |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 2623 | public void onTetheringFailed() {} |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2624 | } |
| 2625 | |
| 2626 | /** |
| 2627 | * Convenient overload for |
| 2628 | * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null |
| 2629 | * handler to run on the current thread's {@link Looper}. |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2630 | * |
| 2631 | * @deprecated Use {@link TetheringManager#startTethering} instead. |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2632 | * @hide |
| 2633 | */ |
| 2634 | @SystemApi |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2635 | @Deprecated |
Udam Saini | 8f7d6a7 | 2017-06-07 12:06:28 -0700 | [diff] [blame] | 2636 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2637 | public void startTethering(int type, boolean showProvisioningUi, |
| 2638 | final OnStartTetheringCallback callback) { |
| 2639 | startTethering(type, showProvisioningUi, callback, null); |
| 2640 | } |
| 2641 | |
| 2642 | /** |
| 2643 | * Runs tether provisioning for the given type if needed and then starts tethering if |
| 2644 | * the check succeeds. If no carrier provisioning is required for tethering, tethering is |
| 2645 | * enabled immediately. If provisioning fails, tethering will not be enabled. It also |
| 2646 | * schedules tether provisioning re-checks if appropriate. |
| 2647 | * |
| 2648 | * @param type The type of tethering to start. Must be one of |
| 2649 | * {@link ConnectivityManager.TETHERING_WIFI}, |
| 2650 | * {@link ConnectivityManager.TETHERING_USB}, or |
| 2651 | * {@link ConnectivityManager.TETHERING_BLUETOOTH}. |
| 2652 | * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there |
| 2653 | * is one. This should be true the first time this function is called and also any time |
| 2654 | * the user can see this UI. It gives users information from their carrier about the |
| 2655 | * check failing and how they can sign up for tethering if possible. |
| 2656 | * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller |
| 2657 | * of the result of trying to tether. |
| 2658 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2659 | * |
| 2660 | * @deprecated Use {@link TetheringManager#startTethering} instead. |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2661 | * @hide |
| 2662 | */ |
| 2663 | @SystemApi |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2664 | @Deprecated |
Jeff Sharkey | 9b39e9a | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 2665 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2666 | public void startTethering(int type, boolean showProvisioningUi, |
| 2667 | final OnStartTetheringCallback callback, Handler handler) { |
Remi NGUYEN VAN | 81f6842 | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 2668 | Objects.requireNonNull(callback, "OnStartTetheringCallback cannot be null."); |
Jeremy Klein | 35e99ea | 2016-03-12 16:29:54 -0800 | [diff] [blame] | 2669 | |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2670 | final Executor executor = new Executor() { |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2671 | @Override |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2672 | public void execute(Runnable command) { |
| 2673 | if (handler == null) { |
| 2674 | command.run(); |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2675 | } else { |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2676 | handler.post(command); |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2677 | } |
| 2678 | } |
| 2679 | }; |
Jeremy Klein | 35e99ea | 2016-03-12 16:29:54 -0800 | [diff] [blame] | 2680 | |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2681 | final StartTetheringCallback tetheringCallback = new StartTetheringCallback() { |
| 2682 | @Override |
| 2683 | public void onTetheringStarted() { |
| 2684 | callback.onTetheringStarted(); |
| 2685 | } |
| 2686 | |
| 2687 | @Override |
markchien | f47d834 | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2688 | public void onTetheringFailed(final int error) { |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2689 | callback.onTetheringFailed(); |
| 2690 | } |
| 2691 | }; |
| 2692 | |
| 2693 | final TetheringRequest request = new TetheringRequest.Builder(type) |
markchien | f47d834 | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2694 | .setShouldShowEntitlementUi(showProvisioningUi).build(); |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2695 | |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2696 | mTetheringManager.startTethering(request, executor, tetheringCallback); |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2697 | } |
| 2698 | |
| 2699 | /** |
| 2700 | * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if |
| 2701 | * applicable. |
| 2702 | * |
| 2703 | * @param type The type of tethering to stop. Must be one of |
| 2704 | * {@link ConnectivityManager.TETHERING_WIFI}, |
| 2705 | * {@link ConnectivityManager.TETHERING_USB}, or |
| 2706 | * {@link ConnectivityManager.TETHERING_BLUETOOTH}. |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2707 | * |
| 2708 | * @deprecated Use {@link TetheringManager#stopTethering} instead. |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2709 | * @hide |
| 2710 | */ |
| 2711 | @SystemApi |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2712 | @Deprecated |
Jeff Sharkey | 9b39e9a | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 2713 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2714 | public void stopTethering(int type) { |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2715 | mTetheringManager.stopTethering(type); |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | /** |
markchien | 4ef53e8 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2719 | * Callback for use with {@link registerTetheringEventCallback} to find out tethering |
| 2720 | * upstream status. |
| 2721 | * |
Nathan Harold | 74f0fb8 | 2020-01-23 18:03:46 -0800 | [diff] [blame] | 2722 | * @deprecated Use {@link TetheringManager#OnTetheringEventCallback} instead. |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2723 | * @hide |
markchien | 4ef53e8 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2724 | */ |
| 2725 | @SystemApi |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2726 | @Deprecated |
markchien | 4ef53e8 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2727 | public abstract static class OnTetheringEventCallback { |
| 2728 | |
| 2729 | /** |
| 2730 | * Called when tethering upstream changed. This can be called multiple times and can be |
| 2731 | * called any time. |
| 2732 | * |
| 2733 | * @param network the {@link Network} of tethering upstream. Null means tethering doesn't |
| 2734 | * have any upstream. |
| 2735 | */ |
| 2736 | public void onUpstreamChanged(@Nullable Network network) {} |
| 2737 | } |
| 2738 | |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2739 | @GuardedBy("mTetheringEventCallbacks") |
| 2740 | private final ArrayMap<OnTetheringEventCallback, TetheringEventCallback> |
| 2741 | mTetheringEventCallbacks = new ArrayMap<>(); |
| 2742 | |
markchien | 4ef53e8 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2743 | /** |
| 2744 | * Start listening to tethering change events. Any new added callback will receive the last |
markchien | 42e2209 | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 2745 | * tethering status right away. If callback is registered when tethering has no upstream or |
markchien | 4ef53e8 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2746 | * disabled, {@link OnTetheringEventCallback#onUpstreamChanged} will immediately be called |
| 2747 | * with a null argument. The same callback object cannot be registered twice. |
| 2748 | * |
| 2749 | * @param executor the executor on which callback will be invoked. |
| 2750 | * @param callback the callback to be called when tethering has change events. |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2751 | * |
Nathan Harold | 74f0fb8 | 2020-01-23 18:03:46 -0800 | [diff] [blame] | 2752 | * @deprecated Use {@link TetheringManager#registerTetheringEventCallback} instead. |
markchien | 4ef53e8 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2753 | * @hide |
| 2754 | */ |
| 2755 | @SystemApi |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2756 | @Deprecated |
markchien | 4ef53e8 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2757 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
| 2758 | public void registerTetheringEventCallback( |
| 2759 | @NonNull @CallbackExecutor Executor executor, |
| 2760 | @NonNull final OnTetheringEventCallback callback) { |
Remi NGUYEN VAN | 81f6842 | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 2761 | Objects.requireNonNull(callback, "OnTetheringEventCallback cannot be null."); |
markchien | 4ef53e8 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2762 | |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2763 | final TetheringEventCallback tetherCallback = |
| 2764 | new TetheringEventCallback() { |
| 2765 | @Override |
| 2766 | public void onUpstreamChanged(@Nullable Network network) { |
| 2767 | callback.onUpstreamChanged(network); |
| 2768 | } |
| 2769 | }; |
| 2770 | |
| 2771 | synchronized (mTetheringEventCallbacks) { |
| 2772 | mTetheringEventCallbacks.put(callback, tetherCallback); |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2773 | mTetheringManager.registerTetheringEventCallback(executor, tetherCallback); |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2774 | } |
markchien | 4ef53e8 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2775 | } |
| 2776 | |
| 2777 | /** |
| 2778 | * Remove tethering event callback previously registered with |
| 2779 | * {@link #registerTetheringEventCallback}. |
| 2780 | * |
| 2781 | * @param callback previously registered callback. |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2782 | * |
| 2783 | * @deprecated Use {@link TetheringManager#unregisterTetheringEventCallback} instead. |
markchien | 4ef53e8 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2784 | * @hide |
| 2785 | */ |
| 2786 | @SystemApi |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2787 | @Deprecated |
markchien | 4ef53e8 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2788 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
| 2789 | public void unregisterTetheringEventCallback( |
| 2790 | @NonNull final OnTetheringEventCallback callback) { |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2791 | Objects.requireNonNull(callback, "The callback must be non-null"); |
| 2792 | synchronized (mTetheringEventCallbacks) { |
| 2793 | final TetheringEventCallback tetherCallback = |
| 2794 | mTetheringEventCallbacks.remove(callback); |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2795 | mTetheringManager.unregisterTetheringEventCallback(tetherCallback); |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2796 | } |
markchien | 4ef53e8 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2797 | } |
| 2798 | |
| 2799 | |
| 2800 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2801 | * Get the list of regular expressions that define any tetherable |
| 2802 | * USB network interfaces. If USB tethering is not supported by the |
| 2803 | * device, this list should be empty. |
| 2804 | * |
| 2805 | * @return an array of 0 or more regular expression Strings defining |
| 2806 | * what interfaces are considered tetherable usb interfaces. |
| 2807 | * |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2808 | * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged} instead. |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2809 | * {@hide} |
| 2810 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 2811 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2812 | @UnsupportedAppUsage |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2813 | @Deprecated |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2814 | public String[] getTetherableUsbRegexs() { |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2815 | return mTetheringManager.getTetherableUsbRegexs(); |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2816 | } |
| 2817 | |
| 2818 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2819 | * Get the list of regular expressions that define any tetherable |
| 2820 | * Wifi network interfaces. If Wifi tethering is not supported by the |
| 2821 | * device, this list should be empty. |
| 2822 | * |
| 2823 | * @return an array of 0 or more regular expression Strings defining |
| 2824 | * what interfaces are considered tetherable wifi interfaces. |
| 2825 | * |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2826 | * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged} instead. |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2827 | * {@hide} |
| 2828 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 2829 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2830 | @UnsupportedAppUsage |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2831 | @Deprecated |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2832 | public String[] getTetherableWifiRegexs() { |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2833 | return mTetheringManager.getTetherableWifiRegexs(); |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2834 | } |
Robert Greenwalt | 4283ded | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2835 | |
Danica Chang | 9656705 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2836 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2837 | * Get the list of regular expressions that define any tetherable |
| 2838 | * Bluetooth network interfaces. If Bluetooth tethering is not supported by the |
| 2839 | * device, this list should be empty. |
| 2840 | * |
| 2841 | * @return an array of 0 or more regular expression Strings defining |
| 2842 | * what interfaces are considered tetherable bluetooth interfaces. |
| 2843 | * |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2844 | * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged( |
| 2845 | *TetheringManager.TetheringInterfaceRegexps)} instead. |
Danica Chang | 9656705 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2846 | * {@hide} |
| 2847 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 2848 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2849 | @UnsupportedAppUsage |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2850 | @Deprecated |
Danica Chang | 9656705 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2851 | public String[] getTetherableBluetoothRegexs() { |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2852 | return mTetheringManager.getTetherableBluetoothRegexs(); |
Danica Chang | 9656705 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2853 | } |
| 2854 | |
Mike Lockwood | ed4a174 | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2855 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2856 | * Attempt to both alter the mode of USB and Tethering of USB. A |
| 2857 | * utility method to deal with some of the complexity of USB - will |
| 2858 | * attempt to switch to Rndis and subsequently tether the resulting |
| 2859 | * interface on {@code true} or turn off tethering and switch off |
| 2860 | * Rndis on {@code false}. |
Lorenzo Colitti | 0b40c87 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 2861 | * |
| 2862 | * <p>This method requires the caller to hold either the |
| 2863 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2864 | * or the ability to modify system settings as determined by |
| 2865 | * {@link android.provider.Settings.System#canWrite}.</p> |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2866 | * |
| 2867 | * @param enable a boolean - {@code true} to enable tethering |
| 2868 | * @return error a {@code TETHER_ERROR} value indicating success or failure type |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2869 | * @deprecated Use {@link TetheringManager#startTethering} instead |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2870 | * |
Mike Lockwood | ed4a174 | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2871 | * {@hide} |
| 2872 | */ |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2873 | @UnsupportedAppUsage |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2874 | @Deprecated |
Mike Lockwood | ed4a174 | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2875 | public int setUsbTethering(boolean enable) { |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2876 | return mTetheringManager.setUsbTethering(enable); |
Mike Lockwood | ed4a174 | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2877 | } |
| 2878 | |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2879 | /** |
| 2880 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_NO_ERROR}. |
| 2881 | * {@hide} |
| 2882 | */ |
markchien | 0f45bb9 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 2883 | @SystemApi |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2884 | @Deprecated |
Remi NGUYEN VAN | 09f8ed2 | 2021-02-15 18:52:06 +0900 | [diff] [blame] | 2885 | public static final int TETHER_ERROR_NO_ERROR = 0; |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2886 | /** |
| 2887 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNKNOWN_IFACE}. |
| 2888 | * {@hide} |
| 2889 | */ |
| 2890 | @Deprecated |
| 2891 | public static final int TETHER_ERROR_UNKNOWN_IFACE = |
| 2892 | TetheringManager.TETHER_ERROR_UNKNOWN_IFACE; |
| 2893 | /** |
| 2894 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_SERVICE_UNAVAIL}. |
| 2895 | * {@hide} |
| 2896 | */ |
| 2897 | @Deprecated |
| 2898 | public static final int TETHER_ERROR_SERVICE_UNAVAIL = |
| 2899 | TetheringManager.TETHER_ERROR_SERVICE_UNAVAIL; |
| 2900 | /** |
| 2901 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNSUPPORTED}. |
| 2902 | * {@hide} |
| 2903 | */ |
| 2904 | @Deprecated |
| 2905 | public static final int TETHER_ERROR_UNSUPPORTED = TetheringManager.TETHER_ERROR_UNSUPPORTED; |
| 2906 | /** |
| 2907 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNAVAIL_IFACE}. |
| 2908 | * {@hide} |
| 2909 | */ |
| 2910 | @Deprecated |
| 2911 | public static final int TETHER_ERROR_UNAVAIL_IFACE = |
| 2912 | TetheringManager.TETHER_ERROR_UNAVAIL_IFACE; |
| 2913 | /** |
markchien | f47d834 | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2914 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_INTERNAL_ERROR}. |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2915 | * {@hide} |
| 2916 | */ |
| 2917 | @Deprecated |
markchien | f47d834 | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2918 | public static final int TETHER_ERROR_MASTER_ERROR = |
| 2919 | TetheringManager.TETHER_ERROR_INTERNAL_ERROR; |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2920 | /** |
| 2921 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_TETHER_IFACE_ERROR}. |
| 2922 | * {@hide} |
| 2923 | */ |
| 2924 | @Deprecated |
| 2925 | public static final int TETHER_ERROR_TETHER_IFACE_ERROR = |
| 2926 | TetheringManager.TETHER_ERROR_TETHER_IFACE_ERROR; |
| 2927 | /** |
| 2928 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNTETHER_IFACE_ERROR}. |
| 2929 | * {@hide} |
| 2930 | */ |
| 2931 | @Deprecated |
| 2932 | public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = |
| 2933 | TetheringManager.TETHER_ERROR_UNTETHER_IFACE_ERROR; |
| 2934 | /** |
markchien | f47d834 | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2935 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_ENABLE_FORWARDING_ERROR}. |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2936 | * {@hide} |
| 2937 | */ |
| 2938 | @Deprecated |
| 2939 | public static final int TETHER_ERROR_ENABLE_NAT_ERROR = |
markchien | f47d834 | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2940 | TetheringManager.TETHER_ERROR_ENABLE_FORWARDING_ERROR; |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2941 | /** |
markchien | f47d834 | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2942 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_DISABLE_FORWARDING_ERROR}. |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2943 | * {@hide} |
| 2944 | */ |
| 2945 | @Deprecated |
| 2946 | public static final int TETHER_ERROR_DISABLE_NAT_ERROR = |
markchien | f47d834 | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2947 | TetheringManager.TETHER_ERROR_DISABLE_FORWARDING_ERROR; |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2948 | /** |
| 2949 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_IFACE_CFG_ERROR}. |
| 2950 | * {@hide} |
| 2951 | */ |
| 2952 | @Deprecated |
| 2953 | public static final int TETHER_ERROR_IFACE_CFG_ERROR = |
| 2954 | TetheringManager.TETHER_ERROR_IFACE_CFG_ERROR; |
| 2955 | /** |
markchien | f47d834 | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2956 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_PROVISIONING_FAILED}. |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2957 | * {@hide} |
| 2958 | */ |
markchien | 0f45bb9 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 2959 | @SystemApi |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2960 | @Deprecated |
Remi NGUYEN VAN | 09f8ed2 | 2021-02-15 18:52:06 +0900 | [diff] [blame] | 2961 | public static final int TETHER_ERROR_PROVISION_FAILED = 11; |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2962 | /** |
| 2963 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_DHCPSERVER_ERROR}. |
| 2964 | * {@hide} |
| 2965 | */ |
| 2966 | @Deprecated |
| 2967 | public static final int TETHER_ERROR_DHCPSERVER_ERROR = |
| 2968 | TetheringManager.TETHER_ERROR_DHCPSERVER_ERROR; |
| 2969 | /** |
| 2970 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_ENTITLEMENT_UNKNOWN}. |
| 2971 | * {@hide} |
| 2972 | */ |
markchien | 0f45bb9 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 2973 | @SystemApi |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2974 | @Deprecated |
Remi NGUYEN VAN | 09f8ed2 | 2021-02-15 18:52:06 +0900 | [diff] [blame] | 2975 | public static final int TETHER_ERROR_ENTITLEMENT_UNKONWN = 13; |
Robert Greenwalt | 4283ded | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2976 | |
| 2977 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2978 | * Get a more detailed error code after a Tethering or Untethering |
| 2979 | * request asynchronously failed. |
| 2980 | * |
| 2981 | * @param iface The name of the interface of interest |
Robert Greenwalt | 4283ded | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2982 | * @return error The error code of the last error tethering or untethering the named |
| 2983 | * interface |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2984 | * |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2985 | * @deprecated Use {@link TetheringEventCallback#onError(String, int)} instead. |
Robert Greenwalt | 4283ded | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2986 | * {@hide} |
| 2987 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 2988 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 2989 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2990 | @Deprecated |
Robert Greenwalt | 4283ded | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2991 | public int getLastTetherError(String iface) { |
markchien | f47d834 | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2992 | int error = mTetheringManager.getLastTetherError(iface); |
| 2993 | if (error == TetheringManager.TETHER_ERROR_UNKNOWN_TYPE) { |
| 2994 | // TETHER_ERROR_UNKNOWN_TYPE was introduced with TetheringManager and has never been |
| 2995 | // returned by ConnectivityManager. Convert it to the legacy TETHER_ERROR_UNKNOWN_IFACE |
| 2996 | // instead. |
| 2997 | error = TetheringManager.TETHER_ERROR_UNKNOWN_IFACE; |
| 2998 | } |
| 2999 | return error; |
Robert Greenwalt | 93dc104 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3000 | } |
| 3001 | |
markchien | bf5ab01 | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 3002 | /** @hide */ |
| 3003 | @Retention(RetentionPolicy.SOURCE) |
| 3004 | @IntDef(value = { |
| 3005 | TETHER_ERROR_NO_ERROR, |
| 3006 | TETHER_ERROR_PROVISION_FAILED, |
| 3007 | TETHER_ERROR_ENTITLEMENT_UNKONWN, |
| 3008 | }) |
| 3009 | public @interface EntitlementResultCode { |
| 3010 | } |
| 3011 | |
Robert Greenwalt | 93dc104 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 3012 | /** |
markchien | bf5ab01 | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 3013 | * Callback for use with {@link #getLatestTetheringEntitlementResult} to find out whether |
markchien | 0f45bb9 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 3014 | * entitlement succeeded. |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 3015 | * |
| 3016 | * @deprecated Use {@link TetheringManager#OnTetheringEntitlementResultListener} instead. |
markchien | 0f45bb9 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 3017 | * @hide |
| 3018 | */ |
| 3019 | @SystemApi |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 3020 | @Deprecated |
markchien | bf5ab01 | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 3021 | public interface OnTetheringEntitlementResultListener { |
| 3022 | /** |
| 3023 | * Called to notify entitlement result. |
| 3024 | * |
| 3025 | * @param resultCode an int value of entitlement result. It may be one of |
| 3026 | * {@link #TETHER_ERROR_NO_ERROR}, |
| 3027 | * {@link #TETHER_ERROR_PROVISION_FAILED}, or |
| 3028 | * {@link #TETHER_ERROR_ENTITLEMENT_UNKONWN}. |
| 3029 | */ |
Jeremy Klein | 7e7c742 | 2019-03-12 13:32:08 -0700 | [diff] [blame] | 3030 | void onTetheringEntitlementResult(@EntitlementResultCode int resultCode); |
markchien | bf5ab01 | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 3031 | } |
| 3032 | |
| 3033 | /** |
markchien | 0f45bb9 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 3034 | * Get the last value of the entitlement check on this downstream. If the cached value is |
| 3035 | * {@link #TETHER_ERROR_NO_ERROR} or showEntitlementUi argument is false, it just return the |
| 3036 | * cached value. Otherwise, a UI-based entitlement check would be performed. It is not |
| 3037 | * guaranteed that the UI-based entitlement check will complete in any specific time period |
| 3038 | * and may in fact never complete. Any successful entitlement check the platform performs for |
| 3039 | * any reason will update the cached value. |
| 3040 | * |
| 3041 | * @param type the downstream type of tethering. Must be one of |
| 3042 | * {@link #TETHERING_WIFI}, |
| 3043 | * {@link #TETHERING_USB}, or |
| 3044 | * {@link #TETHERING_BLUETOOTH}. |
| 3045 | * @param showEntitlementUi a boolean indicating whether to run UI-based entitlement check. |
markchien | bf5ab01 | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 3046 | * @param executor the executor on which callback will be invoked. |
| 3047 | * @param listener an {@link OnTetheringEntitlementResultListener} which will be called to |
| 3048 | * notify the caller of the result of entitlement check. The listener may be called zero |
| 3049 | * or one time. |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 3050 | * @deprecated Use {@link TetheringManager#requestLatestTetheringEntitlementResult} instead. |
markchien | 0f45bb9 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 3051 | * {@hide} |
| 3052 | */ |
| 3053 | @SystemApi |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 3054 | @Deprecated |
markchien | 0f45bb9 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 3055 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
markchien | bf5ab01 | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 3056 | public void getLatestTetheringEntitlementResult(int type, boolean showEntitlementUi, |
| 3057 | @NonNull @CallbackExecutor Executor executor, |
| 3058 | @NonNull final OnTetheringEntitlementResultListener listener) { |
Remi NGUYEN VAN | 81f6842 | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 3059 | Objects.requireNonNull(listener, "TetheringEntitlementResultListener cannot be null."); |
markchien | bf5ab01 | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 3060 | ResultReceiver wrappedListener = new ResultReceiver(null) { |
| 3061 | @Override |
| 3062 | protected void onReceiveResult(int resultCode, Bundle resultData) { |
lucaslin | c6170bb | 2021-03-04 09:38:21 +0800 | [diff] [blame] | 3063 | final long token = Binder.clearCallingIdentity(); |
| 3064 | try { |
| 3065 | executor.execute(() -> { |
| 3066 | listener.onTetheringEntitlementResult(resultCode); |
| 3067 | }); |
| 3068 | } finally { |
| 3069 | Binder.restoreCallingIdentity(token); |
| 3070 | } |
markchien | bf5ab01 | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 3071 | } |
| 3072 | }; |
| 3073 | |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 3074 | mTetheringManager.requestLatestTetheringEntitlementResult(type, wrappedListener, |
markchien | 5776f96 | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 3075 | showEntitlementUi); |
markchien | bf5ab01 | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 3076 | } |
| 3077 | |
| 3078 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 3079 | * Report network connectivity status. This is currently used only |
| 3080 | * to alter status bar UI. |
Paul Jensen | 08f9dbb | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 3081 | * <p>This method requires the caller to hold the permission |
| 3082 | * {@link android.Manifest.permission#STATUS_BAR}. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 3083 | * |
Robert Greenwalt | 986c741 | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3084 | * @param networkType The type of network you want to report on |
| 3085 | * @param percentage The quality of the connection 0 is bad, 100 is good |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 3086 | * @deprecated Types are deprecated. Use {@link #reportNetworkConnectivity} instead. |
Robert Greenwalt | 986c741 | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3087 | * {@hide} |
| 3088 | */ |
| 3089 | public void reportInetCondition(int networkType, int percentage) { |
Soi, Yoshinari | b8102ef | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 3090 | printStackTrace(); |
Robert Greenwalt | 986c741 | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3091 | try { |
| 3092 | mService.reportInetCondition(networkType, percentage); |
| 3093 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3094 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 986c741 | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 3095 | } |
| 3096 | } |
Robert Greenwalt | c3c5f86 | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3097 | |
| 3098 | /** |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3099 | * Report a problem network to the framework. This provides a hint to the system |
Ye Wen | ca7abab | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 3100 | * that there might be connectivity problems on this network and may cause |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3101 | * the framework to re-evaluate network connectivity and/or switch to another |
| 3102 | * network. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3103 | * |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3104 | * @param network The {@link Network} the application was attempting to use |
| 3105 | * or {@code null} to indicate the current default network. |
Paul Jensen | b95d795 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 3106 | * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both |
| 3107 | * working and non-working connectivity. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3108 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 3109 | @Deprecated |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 3110 | public void reportBadNetwork(@Nullable Network network) { |
Soi, Yoshinari | b8102ef | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 3111 | printStackTrace(); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3112 | try { |
Paul Jensen | b95d795 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 3113 | // One of these will be ignored because it matches system's current state. |
| 3114 | // The other will trigger the necessary reevaluation. |
| 3115 | mService.reportNetworkConnectivity(network, true); |
| 3116 | mService.reportNetworkConnectivity(network, false); |
| 3117 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3118 | throw e.rethrowFromSystemServer(); |
Paul Jensen | b95d795 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 3119 | } |
| 3120 | } |
| 3121 | |
| 3122 | /** |
| 3123 | * Report to the framework whether a network has working connectivity. |
| 3124 | * This provides a hint to the system that a particular network is providing |
| 3125 | * working connectivity or not. In response the framework may re-evaluate |
| 3126 | * the network's connectivity and might take further action thereafter. |
| 3127 | * |
| 3128 | * @param network The {@link Network} the application was attempting to use |
| 3129 | * or {@code null} to indicate the current default network. |
| 3130 | * @param hasConnectivity {@code true} if the application was able to successfully access the |
| 3131 | * Internet using {@code network} or {@code false} if not. |
| 3132 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 3133 | public void reportNetworkConnectivity(@Nullable Network network, boolean hasConnectivity) { |
Soi, Yoshinari | b8102ef | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 3134 | printStackTrace(); |
Paul Jensen | b95d795 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 3135 | try { |
| 3136 | mService.reportNetworkConnectivity(network, hasConnectivity); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3137 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3138 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3139 | } |
| 3140 | } |
| 3141 | |
| 3142 | /** |
Chalard Jean | 48d60ea | 2021-03-17 17:03:34 +0900 | [diff] [blame^] | 3143 | * Set a network-independent global HTTP proxy. |
Paul Jensen | 08f9dbb | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 3144 | * |
Chalard Jean | 48d60ea | 2021-03-17 17:03:34 +0900 | [diff] [blame^] | 3145 | * This sets an HTTP proxy that applies to all networks and overrides any network-specific |
| 3146 | * proxy. If set, HTTP libraries that are proxy-aware will use this global proxy when |
| 3147 | * accessing any network, regardless of what the settings for that network are. |
| 3148 | * |
| 3149 | * Note that HTTP proxies are by nature typically network-dependent, and setting a global |
| 3150 | * proxy is likely to break networking on multiple networks. This method is only meant |
| 3151 | * for device policy clients looking to do general internal filtering or similar use cases. |
| 3152 | * |
| 3153 | * {@see #getGlobalProxy} |
| 3154 | * {@see LinkProperties#getHttpProxy} |
| 3155 | * |
| 3156 | * @param p A {@link ProxyInfo} object defining the new global HTTP proxy. Calling this |
| 3157 | * method with a {@code null} value will clear the global HTTP proxy. |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3158 | * @hide |
Robert Greenwalt | c3c5f86 | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3159 | */ |
Chalard Jean | 48d60ea | 2021-03-17 17:03:34 +0900 | [diff] [blame^] | 3160 | // Used by Device Policy Manager to set the global proxy. |
Chiachang Wang | 4d51357 | 2021-03-18 09:44:34 +0800 | [diff] [blame] | 3161 | @SystemApi(client = MODULE_LIBRARIES) |
paulhu | 8e96a75 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 3162 | @RequiresPermission(android.Manifest.permission.NETWORK_STACK) |
Chalard Jean | 48d60ea | 2021-03-17 17:03:34 +0900 | [diff] [blame^] | 3163 | public void setGlobalProxy(@Nullable final ProxyInfo p) { |
Robert Greenwalt | c3c5f86 | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3164 | try { |
| 3165 | mService.setGlobalProxy(p); |
| 3166 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3167 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | c3c5f86 | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3168 | } |
| 3169 | } |
| 3170 | |
| 3171 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 3172 | * Retrieve any network-independent global HTTP proxy. |
| 3173 | * |
Jason Monk | 4d5e20f | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3174 | * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null} |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 3175 | * if no global HTTP proxy is set. |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3176 | * @hide |
Robert Greenwalt | c3c5f86 | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3177 | */ |
Chiachang Wang | 4d51357 | 2021-03-18 09:44:34 +0800 | [diff] [blame] | 3178 | @SystemApi(client = MODULE_LIBRARIES) |
| 3179 | @Nullable |
Jason Monk | 4d5e20f | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3180 | public ProxyInfo getGlobalProxy() { |
Robert Greenwalt | c3c5f86 | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3181 | try { |
| 3182 | return mService.getGlobalProxy(); |
| 3183 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3184 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | c3c5f86 | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3185 | } |
| 3186 | } |
| 3187 | |
| 3188 | /** |
Paul Jensen | db93dd9 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 3189 | * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a |
| 3190 | * network-specific HTTP proxy. If {@code network} is null, the |
| 3191 | * network-specific proxy returned is the proxy of the default active |
| 3192 | * network. |
| 3193 | * |
| 3194 | * @return {@link ProxyInfo} for the current global HTTP proxy, or if no |
| 3195 | * global HTTP proxy is set, {@code ProxyInfo} for {@code network}, |
| 3196 | * or when {@code network} is {@code null}, |
| 3197 | * the {@code ProxyInfo} for the default active network. Returns |
| 3198 | * {@code null} when no proxy applies or the caller doesn't have |
| 3199 | * permission to use {@code network}. |
| 3200 | * @hide |
| 3201 | */ |
| 3202 | public ProxyInfo getProxyForNetwork(Network network) { |
| 3203 | try { |
| 3204 | return mService.getProxyForNetwork(network); |
| 3205 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3206 | throw e.rethrowFromSystemServer(); |
Paul Jensen | db93dd9 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 3207 | } |
| 3208 | } |
| 3209 | |
| 3210 | /** |
Paul Jensen | c0618a6 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 3211 | * Get the current default HTTP proxy settings. If a global proxy is set it will be returned, |
| 3212 | * otherwise if this process is bound to a {@link Network} using |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 3213 | * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise |
Paul Jensen | c0618a6 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 3214 | * the default network's proxy is returned. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 3215 | * |
Jason Monk | 4d5e20f | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3216 | * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 3217 | * HTTP proxy is active. |
Robert Greenwalt | c3c5f86 | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3218 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 3219 | @Nullable |
Paul Jensen | c0618a6 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 3220 | public ProxyInfo getDefaultProxy() { |
Paul Jensen | db93dd9 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 3221 | return getProxyForNetwork(getBoundNetworkForProcess()); |
Robert Greenwalt | c3c5f86 | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3222 | } |
Robert Greenwalt | 34848c0 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 3223 | |
| 3224 | /** |
Robert Greenwalt | 0114f6e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 3225 | * Returns true if the hardware supports the given network type |
| 3226 | * else it returns false. This doesn't indicate we have coverage |
| 3227 | * or are authorized onto a network, just whether or not the |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 3228 | * hardware supports it. For example a GSM phone without a SIM |
| 3229 | * should still return {@code true} for mobile data, but a wifi only |
| 3230 | * tablet would return {@code false}. |
| 3231 | * |
| 3232 | * @param networkType The network type we'd like to check |
| 3233 | * @return {@code true} if supported, else {@code false} |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 3234 | * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead. |
Robert Greenwalt | 0114f6e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 3235 | * @hide |
| 3236 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 3237 | @Deprecated |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 3238 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | a3b7751 | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 3239 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Robert Greenwalt | 0114f6e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 3240 | public boolean isNetworkSupported(int networkType) { |
| 3241 | try { |
| 3242 | return mService.isNetworkSupported(networkType); |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3243 | } catch (RemoteException e) { |
| 3244 | throw e.rethrowFromSystemServer(); |
| 3245 | } |
Robert Greenwalt | 0114f6e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 3246 | } |
Jeff Sharkey | d00b130 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 3247 | |
| 3248 | /** |
| 3249 | * Returns if the currently active data network is metered. A network is |
| 3250 | * classified as metered when the user is sensitive to heavy data usage on |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 3251 | * that connection due to monetary costs, data limitations or |
| 3252 | * battery/performance issues. You should check this before doing large |
| 3253 | * data transfers, and warn the user or delay the operation until another |
| 3254 | * network is available. |
| 3255 | * |
| 3256 | * @return {@code true} if large transfers should be avoided, otherwise |
| 3257 | * {@code false}. |
Jeff Sharkey | d00b130 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 3258 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 3259 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Jeff Sharkey | d00b130 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 3260 | public boolean isActiveNetworkMetered() { |
| 3261 | try { |
| 3262 | return mService.isActiveNetworkMetered(); |
| 3263 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3264 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | d00b130 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 3265 | } |
| 3266 | } |
Jeff Sharkey | ebcc797 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3267 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 3268 | /** |
Sarah Chin | cabcbff | 2020-11-25 12:15:14 -0800 | [diff] [blame] | 3269 | * Set sign in error notification to visible or invisible |
Wink Saville | 9a1a7ef | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3270 | * |
Sarah Chin | cabcbff | 2020-11-25 12:15:14 -0800 | [diff] [blame] | 3271 | * @hide |
Paul Jensen | dda8e59 | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 3272 | * @deprecated Doesn't properly deal with multiple connected networks of the same type. |
Wink Saville | 9a1a7ef | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3273 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 3274 | @Deprecated |
Wink Saville | 9a1a7ef | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3275 | public void setProvisioningNotificationVisible(boolean visible, int networkType, |
Paul Jensen | ebeaecd | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3276 | String action) { |
Wink Saville | 9a1a7ef | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3277 | try { |
Paul Jensen | ebeaecd | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3278 | mService.setProvisioningNotificationVisible(visible, networkType, action); |
Wink Saville | 9a1a7ef | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3279 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3280 | throw e.rethrowFromSystemServer(); |
Wink Saville | 9a1a7ef | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3281 | } |
| 3282 | } |
Yuhao Zheng | b77f15d | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3283 | |
| 3284 | /** |
| 3285 | * Set the value for enabling/disabling airplane mode |
| 3286 | * |
| 3287 | * @param enable whether to enable airplane mode or not |
| 3288 | * |
Yuhao Zheng | b77f15d | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3289 | * @hide |
| 3290 | */ |
Lorenzo Colitti | c7da00d | 2018-10-09 18:55:11 +0900 | [diff] [blame] | 3291 | @RequiresPermission(anyOf = { |
Edward Savage-Jones | d472369 | 2019-11-26 13:18:08 +0100 | [diff] [blame] | 3292 | android.Manifest.permission.NETWORK_AIRPLANE_MODE, |
Lorenzo Colitti | c7da00d | 2018-10-09 18:55:11 +0900 | [diff] [blame] | 3293 | android.Manifest.permission.NETWORK_SETTINGS, |
| 3294 | android.Manifest.permission.NETWORK_SETUP_WIZARD, |
| 3295 | android.Manifest.permission.NETWORK_STACK}) |
Lorenzo Colitti | 0bfef02 | 2018-10-09 18:50:32 +0900 | [diff] [blame] | 3296 | @SystemApi |
Yuhao Zheng | b77f15d | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3297 | public void setAirplaneMode(boolean enable) { |
| 3298 | try { |
| 3299 | mService.setAirplaneMode(enable); |
| 3300 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3301 | throw e.rethrowFromSystemServer(); |
Yuhao Zheng | b77f15d | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3302 | } |
| 3303 | } |
Robert Greenwalt | 7e45d11 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3304 | |
Lorenzo Colitti | 6702d4b | 2020-01-08 00:04:09 +0900 | [diff] [blame] | 3305 | /** |
| 3306 | * Registers the specified {@link NetworkProvider}. |
| 3307 | * Each listener must only be registered once. The listener can be unregistered with |
| 3308 | * {@link #unregisterNetworkProvider}. |
| 3309 | * |
| 3310 | * @param provider the provider to register |
| 3311 | * @return the ID of the provider. This ID must be used by the provider when registering |
| 3312 | * {@link android.net.NetworkAgent}s. |
| 3313 | * @hide |
| 3314 | */ |
| 3315 | @SystemApi |
paulhu | b6ba8e8 | 2020-03-04 09:43:41 +0800 | [diff] [blame] | 3316 | @RequiresPermission(anyOf = { |
| 3317 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 3318 | android.Manifest.permission.NETWORK_FACTORY}) |
Lorenzo Colitti | 6702d4b | 2020-01-08 00:04:09 +0900 | [diff] [blame] | 3319 | public int registerNetworkProvider(@NonNull NetworkProvider provider) { |
| 3320 | if (provider.getProviderId() != NetworkProvider.ID_NONE) { |
Lorenzo Colitti | 6702d4b | 2020-01-08 00:04:09 +0900 | [diff] [blame] | 3321 | throw new IllegalStateException("NetworkProviders can only be registered once"); |
| 3322 | } |
| 3323 | |
| 3324 | try { |
| 3325 | int providerId = mService.registerNetworkProvider(provider.getMessenger(), |
| 3326 | provider.getName()); |
| 3327 | provider.setProviderId(providerId); |
| 3328 | } catch (RemoteException e) { |
| 3329 | throw e.rethrowFromSystemServer(); |
| 3330 | } |
| 3331 | return provider.getProviderId(); |
| 3332 | } |
| 3333 | |
| 3334 | /** |
| 3335 | * Unregisters the specified NetworkProvider. |
| 3336 | * |
| 3337 | * @param provider the provider to unregister |
| 3338 | * @hide |
| 3339 | */ |
| 3340 | @SystemApi |
paulhu | b6ba8e8 | 2020-03-04 09:43:41 +0800 | [diff] [blame] | 3341 | @RequiresPermission(anyOf = { |
| 3342 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 3343 | android.Manifest.permission.NETWORK_FACTORY}) |
Lorenzo Colitti | 6702d4b | 2020-01-08 00:04:09 +0900 | [diff] [blame] | 3344 | public void unregisterNetworkProvider(@NonNull NetworkProvider provider) { |
| 3345 | try { |
| 3346 | mService.unregisterNetworkProvider(provider.getMessenger()); |
| 3347 | } catch (RemoteException e) { |
| 3348 | throw e.rethrowFromSystemServer(); |
| 3349 | } |
| 3350 | provider.setProviderId(NetworkProvider.ID_NONE); |
| 3351 | } |
| 3352 | |
| 3353 | |
| 3354 | /** @hide exposed via the NetworkProvider class. */ |
paulhu | b6ba8e8 | 2020-03-04 09:43:41 +0800 | [diff] [blame] | 3355 | @RequiresPermission(anyOf = { |
| 3356 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 3357 | android.Manifest.permission.NETWORK_FACTORY}) |
Lorenzo Colitti | 6702d4b | 2020-01-08 00:04:09 +0900 | [diff] [blame] | 3358 | public void declareNetworkRequestUnfulfillable(@NonNull NetworkRequest request) { |
| 3359 | try { |
| 3360 | mService.declareNetworkRequestUnfulfillable(request); |
| 3361 | } catch (RemoteException e) { |
| 3362 | throw e.rethrowFromSystemServer(); |
| 3363 | } |
| 3364 | } |
| 3365 | |
Paul Jensen | 1f56738 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 3366 | /** |
| 3367 | * @hide |
| 3368 | * Register a NetworkAgent with ConnectivityService. |
Chalard Jean | f78c964 | 2019-12-13 19:47:12 +0900 | [diff] [blame] | 3369 | * @return Network corresponding to NetworkAgent. |
Paul Jensen | 1f56738 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 3370 | */ |
paulhu | b6ba8e8 | 2020-03-04 09:43:41 +0800 | [diff] [blame] | 3371 | @RequiresPermission(anyOf = { |
| 3372 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 3373 | android.Manifest.permission.NETWORK_FACTORY}) |
Remi NGUYEN VAN | 77b0c1c | 2020-12-23 12:45:08 +0900 | [diff] [blame] | 3374 | public Network registerNetworkAgent(INetworkAgent na, NetworkInfo ni, LinkProperties lp, |
Chalard Jean | 2801857 | 2020-12-21 18:36:52 +0900 | [diff] [blame] | 3375 | NetworkCapabilities nc, @NonNull NetworkScore score, NetworkAgentConfig config, |
| 3376 | int providerId) { |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3377 | try { |
Remi NGUYEN VAN | 77b0c1c | 2020-12-23 12:45:08 +0900 | [diff] [blame] | 3378 | return mService.registerNetworkAgent(na, ni, lp, nc, score, config, providerId); |
Paul Jensen | 1f56738 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 3379 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3380 | throw e.rethrowFromSystemServer(); |
Paul Jensen | 1f56738 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 3381 | } |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3382 | } |
| 3383 | |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3384 | /** |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 3385 | * Base class for {@code NetworkRequest} callbacks. Used for notifications about network |
| 3386 | * changes. Should be extended by applications wanting notifications. |
| 3387 | * |
| 3388 | * A {@code NetworkCallback} is registered by calling |
| 3389 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, |
| 3390 | * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)}, |
Hugo Benichi | cbfbb37 | 2018-02-07 21:17:43 +0900 | [diff] [blame] | 3391 | * or {@link #registerDefaultNetworkCallback(NetworkCallback)}. A {@code NetworkCallback} is |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 3392 | * unregistered by calling {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 3393 | * A {@code NetworkCallback} should be registered at most once at any time. |
| 3394 | * A {@code NetworkCallback} that has been unregistered can be registered again. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3395 | */ |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3396 | public static class NetworkCallback { |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3397 | /** |
Roshan Pius | 951c003 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 3398 | * No flags associated with this callback. |
| 3399 | * @hide |
| 3400 | */ |
| 3401 | public static final int FLAG_NONE = 0; |
| 3402 | /** |
| 3403 | * Use this flag to include any location sensitive data in {@link NetworkCapabilities} sent |
| 3404 | * via {@link #onCapabilitiesChanged(Network, NetworkCapabilities)}. |
| 3405 | * <p> |
| 3406 | * These include: |
| 3407 | * <li> Some transport info instances (retrieved via |
| 3408 | * {@link NetworkCapabilities#getTransportInfo()}) like {@link android.net.wifi.WifiInfo} |
| 3409 | * contain location sensitive information. |
| 3410 | * <li> OwnerUid (retrieved via {@link NetworkCapabilities#getOwnerUid()} is location |
| 3411 | * sensitive for wifi suggestor apps (i.e using {@link WifiNetworkSuggestion}).</li> |
| 3412 | * </p> |
| 3413 | * <p> |
| 3414 | * Note: |
| 3415 | * <li> Retrieving this location sensitive information (subject to app's location |
| 3416 | * permissions) will be noted by system. </li> |
| 3417 | * <li> Without this flag any {@link NetworkCapabilities} provided via the callback does |
| 3418 | * not include location sensitive info. |
| 3419 | * </p> |
| 3420 | */ |
| 3421 | public static final int FLAG_INCLUDE_LOCATION_INFO = 1 << 0; |
| 3422 | |
| 3423 | /** @hide */ |
| 3424 | @Retention(RetentionPolicy.SOURCE) |
| 3425 | @IntDef(flag = true, prefix = "FLAG_", value = { |
| 3426 | FLAG_NONE, |
| 3427 | FLAG_INCLUDE_LOCATION_INFO |
| 3428 | }) |
| 3429 | public @interface Flag { } |
| 3430 | |
| 3431 | /** |
| 3432 | * All the valid flags for error checking. |
| 3433 | */ |
| 3434 | private static final int VALID_FLAGS = FLAG_INCLUDE_LOCATION_INFO; |
| 3435 | |
| 3436 | public NetworkCallback() { |
| 3437 | this(FLAG_NONE); |
| 3438 | } |
| 3439 | |
| 3440 | public NetworkCallback(@Flag int flags) { |
Remi NGUYEN VAN | 81f6842 | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 3441 | if ((flags & VALID_FLAGS) != flags) { |
| 3442 | throw new IllegalArgumentException("Invalid flags"); |
| 3443 | } |
Roshan Pius | 951c003 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 3444 | mFlags = flags; |
| 3445 | } |
| 3446 | |
| 3447 | /** |
Lorenzo Colitti | 1457459 | 2015-04-24 12:23:24 +0900 | [diff] [blame] | 3448 | * Called when the framework connects to a new network to evaluate whether it satisfies this |
| 3449 | * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable} |
| 3450 | * callback. There is no guarantee that this new network will satisfy any requests, or that |
| 3451 | * the network will stay connected for longer than the time necessary to evaluate it. |
| 3452 | * <p> |
| 3453 | * Most applications <b>should not</b> act on this callback, and should instead use |
| 3454 | * {@link #onAvailable}. This callback is intended for use by applications that can assist |
| 3455 | * the framework in properly evaluating the network — for example, an application that |
| 3456 | * can automatically log in to a captive portal without user intervention. |
| 3457 | * |
| 3458 | * @param network The {@link Network} of the network that is being evaluated. |
Lorenzo Colitti | 3a9df1a | 2015-06-11 14:27:17 +0900 | [diff] [blame] | 3459 | * |
| 3460 | * @hide |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3461 | */ |
paulhu | 1a40765 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3462 | public void onPreCheck(@NonNull Network network) {} |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3463 | |
| 3464 | /** |
Lorenzo Colitti | 1457459 | 2015-04-24 12:23:24 +0900 | [diff] [blame] | 3465 | * Called when the framework connects and has declared a new network ready for use. |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3466 | * This callback may be called more than once if the {@link Network} that is |
| 3467 | * satisfying the request changes. |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3468 | * |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3469 | * @param network The {@link Network} of the satisfying network. |
Chalard Jean | a23bc9e | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3470 | * @param networkCapabilities The {@link NetworkCapabilities} of the satisfying network. |
| 3471 | * @param linkProperties The {@link LinkProperties} of the satisfying network. |
junyulai | f2c67e4 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3472 | * @param blocked Whether access to the {@link Network} is blocked due to system policy. |
Chalard Jean | a23bc9e | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3473 | * @hide |
| 3474 | */ |
Lorenzo Colitti | 79c6f22 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 3475 | public final void onAvailable(@NonNull Network network, |
paulhu | 1a40765 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3476 | @NonNull NetworkCapabilities networkCapabilities, |
Lorenzo Colitti | 79c6f22 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 3477 | @NonNull LinkProperties linkProperties, @BlockedReason int blocked) { |
Chalard Jean | a23bc9e | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3478 | // Internally only this method is called when a new network is available, and |
| 3479 | // it calls the callback in the same way and order that older versions used |
| 3480 | // to call so as not to change the behavior. |
Lorenzo Colitti | 79c6f22 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 3481 | onAvailable(network, networkCapabilities, linkProperties, blocked != 0); |
| 3482 | onBlockedStatusChanged(network, blocked); |
| 3483 | } |
| 3484 | |
| 3485 | /** |
| 3486 | * Legacy variant of onAvailable that takes a boolean blocked reason. |
| 3487 | * |
| 3488 | * This method has never been public API, but it's not final, so there may be apps that |
| 3489 | * implemented it and rely on it being called. Do our best not to break them. |
| 3490 | * Note: such apps will also get a second call to onBlockedStatusChanged immediately after |
| 3491 | * this method is called. There does not seem to be a way to avoid this. |
| 3492 | * TODO: add a compat check to move apps off this method, and eventually stop calling it. |
| 3493 | * |
| 3494 | * @hide |
| 3495 | */ |
| 3496 | public void onAvailable(@NonNull Network network, |
| 3497 | @NonNull NetworkCapabilities networkCapabilities, |
| 3498 | @NonNull LinkProperties linkProperties, boolean blocked) { |
Chalard Jean | a23bc9e | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3499 | onAvailable(network); |
| 3500 | if (!networkCapabilities.hasCapability( |
| 3501 | NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)) { |
| 3502 | onNetworkSuspended(network); |
| 3503 | } |
| 3504 | onCapabilitiesChanged(network, networkCapabilities); |
| 3505 | onLinkPropertiesChanged(network, linkProperties); |
Lorenzo Colitti | 79c6f22 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 3506 | // No call to onBlockedStatusChanged here. That is done by the caller. |
Chalard Jean | a23bc9e | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3507 | } |
| 3508 | |
| 3509 | /** |
| 3510 | * Called when the framework connects and has declared a new network ready for use. |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3511 | * |
| 3512 | * <p>For callbacks registered with {@link #registerNetworkCallback}, multiple networks may |
| 3513 | * be available at the same time, and onAvailable will be called for each of these as they |
| 3514 | * appear. |
| 3515 | * |
| 3516 | * <p>For callbacks registered with {@link #requestNetwork} and |
| 3517 | * {@link #registerDefaultNetworkCallback}, this means the network passed as an argument |
| 3518 | * is the new best network for this request and is now tracked by this callback ; this |
| 3519 | * callback will no longer receive method calls about other networks that may have been |
| 3520 | * passed to this method previously. The previously-best network may have disconnected, or |
| 3521 | * it may still be around and the newly-best network may simply be better. |
| 3522 | * |
| 3523 | * <p>Starting with {@link android.os.Build.VERSION_CODES#O}, this will always immediately |
| 3524 | * be followed by a call to {@link #onCapabilitiesChanged(Network, NetworkCapabilities)} |
| 3525 | * then by a call to {@link #onLinkPropertiesChanged(Network, LinkProperties)}, and a call |
| 3526 | * to {@link #onBlockedStatusChanged(Network, boolean)}. |
| 3527 | * |
| 3528 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 3529 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 3530 | * this callback as this is prone to race conditions (there is no guarantee the objects |
| 3531 | * returned by these methods will be current). Instead, wait for a call to |
| 3532 | * {@link #onCapabilitiesChanged(Network, NetworkCapabilities)} and |
| 3533 | * {@link #onLinkPropertiesChanged(Network, LinkProperties)} whose arguments are guaranteed |
| 3534 | * to be well-ordered with respect to other callbacks. |
Chalard Jean | a23bc9e | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3535 | * |
| 3536 | * @param network The {@link Network} of the satisfying network. |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3537 | */ |
paulhu | 1a40765 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3538 | public void onAvailable(@NonNull Network network) {} |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3539 | |
| 3540 | /** |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3541 | * Called when the network is about to be lost, typically because there are no outstanding |
| 3542 | * requests left for it. This may be paired with a {@link NetworkCallback#onAvailable} call |
| 3543 | * with the new replacement network for graceful handover. This method is not guaranteed |
| 3544 | * to be called before {@link NetworkCallback#onLost} is called, for example in case a |
| 3545 | * network is suddenly disconnected. |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3546 | * |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3547 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 3548 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 3549 | * this callback as this is prone to race conditions ; calling these methods while in a |
| 3550 | * callback may return an outdated or even a null object. |
| 3551 | * |
| 3552 | * @param network The {@link Network} that is about to be lost. |
| 3553 | * @param maxMsToLive The time in milliseconds the system intends to keep the network |
| 3554 | * connected for graceful handover; note that the network may still |
| 3555 | * suffer a hard loss at any time. |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3556 | */ |
paulhu | 1a40765 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3557 | public void onLosing(@NonNull Network network, int maxMsToLive) {} |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3558 | |
| 3559 | /** |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3560 | * Called when a network disconnects or otherwise no longer satisfies this request or |
| 3561 | * callback. |
| 3562 | * |
| 3563 | * <p>If the callback was registered with requestNetwork() or |
| 3564 | * registerDefaultNetworkCallback(), it will only be invoked against the last network |
| 3565 | * returned by onAvailable() when that network is lost and no other network satisfies |
| 3566 | * the criteria of the request. |
| 3567 | * |
| 3568 | * <p>If the callback was registered with registerNetworkCallback() it will be called for |
| 3569 | * each network which no longer satisfies the criteria of the callback. |
| 3570 | * |
| 3571 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 3572 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 3573 | * this callback as this is prone to race conditions ; calling these methods while in a |
| 3574 | * callback may return an outdated or even a null object. |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3575 | * |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3576 | * @param network The {@link Network} lost. |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3577 | */ |
paulhu | 1a40765 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3578 | public void onLost(@NonNull Network network) {} |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3579 | |
| 3580 | /** |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3581 | * Called if no network is found within the timeout time specified in |
Etan Cohen | fbfdd84 | 2019-01-08 12:09:18 -0800 | [diff] [blame] | 3582 | * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} call or if the |
| 3583 | * requested network request cannot be fulfilled (whether or not a timeout was |
| 3584 | * specified). When this callback is invoked the associated |
Etan Cohen | f67bde9 | 2017-03-01 12:47:28 -0800 | [diff] [blame] | 3585 | * {@link NetworkRequest} will have already been removed and released, as if |
| 3586 | * {@link #unregisterNetworkCallback(NetworkCallback)} had been called. |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3587 | */ |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3588 | public void onUnavailable() {} |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3589 | |
| 3590 | /** |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3591 | * Called when the network corresponding to this request changes capabilities but still |
| 3592 | * satisfies the requested criteria. |
| 3593 | * |
| 3594 | * <p>Starting with {@link android.os.Build.VERSION_CODES#O} this method is guaranteed |
| 3595 | * to be called immediately after {@link #onAvailable}. |
| 3596 | * |
| 3597 | * <p>Do NOT call {@link #getLinkProperties(Network)} or other synchronous |
| 3598 | * ConnectivityManager methods in this callback as this is prone to race conditions : |
| 3599 | * calling these methods while in a callback may return an outdated or even a null object. |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3600 | * |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3601 | * @param network The {@link Network} whose capabilities have changed. |
Roshan Pius | 951c003 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 3602 | * @param networkCapabilities The new {@link NetworkCapabilities} for this |
Chalard Jean | a23bc9e | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3603 | * network. |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3604 | */ |
paulhu | 1a40765 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3605 | public void onCapabilitiesChanged(@NonNull Network network, |
| 3606 | @NonNull NetworkCapabilities networkCapabilities) {} |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3607 | |
| 3608 | /** |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3609 | * Called when the network corresponding to this request changes {@link LinkProperties}. |
| 3610 | * |
| 3611 | * <p>Starting with {@link android.os.Build.VERSION_CODES#O} this method is guaranteed |
| 3612 | * to be called immediately after {@link #onAvailable}. |
| 3613 | * |
| 3614 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or other synchronous |
| 3615 | * ConnectivityManager methods in this callback as this is prone to race conditions : |
| 3616 | * calling these methods while in a callback may return an outdated or even a null object. |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3617 | * |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3618 | * @param network The {@link Network} whose link properties have changed. |
| 3619 | * @param linkProperties The new {@link LinkProperties} for this network. |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3620 | */ |
paulhu | 1a40765 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3621 | public void onLinkPropertiesChanged(@NonNull Network network, |
| 3622 | @NonNull LinkProperties linkProperties) {} |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3623 | |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3624 | /** |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3625 | * Called when the network the framework connected to for this request suspends data |
| 3626 | * transmission temporarily. |
| 3627 | * |
| 3628 | * <p>This generally means that while the TCP connections are still live temporarily |
| 3629 | * network data fails to transfer. To give a specific example, this is used on cellular |
| 3630 | * networks to mask temporary outages when driving through a tunnel, etc. In general this |
| 3631 | * means read operations on sockets on this network will block once the buffers are |
| 3632 | * drained, and write operations will block once the buffers are full. |
| 3633 | * |
| 3634 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 3635 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 3636 | * this callback as this is prone to race conditions (there is no guarantee the objects |
| 3637 | * returned by these methods will be current). |
| 3638 | * |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3639 | * @hide |
| 3640 | */ |
paulhu | 1a40765 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3641 | public void onNetworkSuspended(@NonNull Network network) {} |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3642 | |
| 3643 | /** |
| 3644 | * Called when the network the framework connected to for this request |
Chalard Jean | a23bc9e | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3645 | * returns from a {@link NetworkInfo.State#SUSPENDED} state. This should always be |
| 3646 | * preceded by a matching {@link NetworkCallback#onNetworkSuspended} call. |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3647 | |
| 3648 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 3649 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 3650 | * this callback as this is prone to race conditions : calling these methods while in a |
| 3651 | * callback may return an outdated or even a null object. |
| 3652 | * |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3653 | * @hide |
| 3654 | */ |
paulhu | 1a40765 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3655 | public void onNetworkResumed(@NonNull Network network) {} |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3656 | |
junyulai | f2c67e4 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3657 | /** |
| 3658 | * Called when access to the specified network is blocked or unblocked. |
| 3659 | * |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3660 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 3661 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 3662 | * this callback as this is prone to race conditions : calling these methods while in a |
| 3663 | * callback may return an outdated or even a null object. |
| 3664 | * |
junyulai | f2c67e4 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3665 | * @param network The {@link Network} whose blocked status has changed. |
| 3666 | * @param blocked The blocked status of this {@link Network}. |
| 3667 | */ |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 3668 | public void onBlockedStatusChanged(@NonNull Network network, boolean blocked) {} |
junyulai | f2c67e4 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3669 | |
Lorenzo Colitti | 79c6f22 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 3670 | /** |
Lorenzo Colitti | 42fe223 | 2021-03-25 23:17:36 +0900 | [diff] [blame] | 3671 | * Called when access to the specified network is blocked or unblocked, or the reason for |
| 3672 | * access being blocked changes. |
Lorenzo Colitti | 79c6f22 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 3673 | * |
| 3674 | * If a NetworkCallback object implements this method, |
| 3675 | * {@link #onBlockedStatusChanged(Network, boolean)} will not be called. |
| 3676 | * |
| 3677 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 3678 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 3679 | * this callback as this is prone to race conditions : calling these methods while in a |
| 3680 | * callback may return an outdated or even a null object. |
| 3681 | * |
| 3682 | * @param network The {@link Network} whose blocked status has changed. |
| 3683 | * @param blocked The blocked status of this {@link Network}. |
| 3684 | * @hide |
| 3685 | */ |
| 3686 | @SystemApi(client = MODULE_LIBRARIES) |
| 3687 | public void onBlockedStatusChanged(@NonNull Network network, @BlockedReason int blocked) { |
| 3688 | onBlockedStatusChanged(network, blocked != 0); |
| 3689 | } |
| 3690 | |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3691 | private NetworkRequest networkRequest; |
Roshan Pius | 951c003 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 3692 | private final int mFlags; |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3693 | } |
| 3694 | |
Hugo Benichi | a590ab8 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 3695 | /** |
| 3696 | * Constant error codes used by ConnectivityService to communicate about failures and errors |
| 3697 | * across a Binder boundary. |
| 3698 | * @hide |
| 3699 | */ |
| 3700 | public interface Errors { |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 3701 | int TOO_MANY_REQUESTS = 1; |
Hugo Benichi | a590ab8 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 3702 | } |
| 3703 | |
| 3704 | /** @hide */ |
| 3705 | public static class TooManyRequestsException extends RuntimeException {} |
| 3706 | |
| 3707 | private static RuntimeException convertServiceException(ServiceSpecificException e) { |
| 3708 | switch (e.errorCode) { |
| 3709 | case Errors.TOO_MANY_REQUESTS: |
| 3710 | return new TooManyRequestsException(); |
| 3711 | default: |
| 3712 | Log.w(TAG, "Unknown service error code " + e.errorCode); |
| 3713 | return new RuntimeException(e); |
| 3714 | } |
| 3715 | } |
| 3716 | |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3717 | /** @hide */ |
Remi NGUYEN VAN | 1fb66ad | 2021-03-12 15:24:06 +0900 | [diff] [blame] | 3718 | public static final int CALLBACK_PRECHECK = 1; |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3719 | /** @hide */ |
Remi NGUYEN VAN | 1fb66ad | 2021-03-12 15:24:06 +0900 | [diff] [blame] | 3720 | public static final int CALLBACK_AVAILABLE = 2; |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3721 | /** @hide arg1 = TTL */ |
Remi NGUYEN VAN | 1fb66ad | 2021-03-12 15:24:06 +0900 | [diff] [blame] | 3722 | public static final int CALLBACK_LOSING = 3; |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3723 | /** @hide */ |
Remi NGUYEN VAN | 1fb66ad | 2021-03-12 15:24:06 +0900 | [diff] [blame] | 3724 | public static final int CALLBACK_LOST = 4; |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3725 | /** @hide */ |
Remi NGUYEN VAN | 1fb66ad | 2021-03-12 15:24:06 +0900 | [diff] [blame] | 3726 | public static final int CALLBACK_UNAVAIL = 5; |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3727 | /** @hide */ |
Remi NGUYEN VAN | 1fb66ad | 2021-03-12 15:24:06 +0900 | [diff] [blame] | 3728 | public static final int CALLBACK_CAP_CHANGED = 6; |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3729 | /** @hide */ |
Remi NGUYEN VAN | 1fb66ad | 2021-03-12 15:24:06 +0900 | [diff] [blame] | 3730 | public static final int CALLBACK_IP_CHANGED = 7; |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 3731 | /** @hide obj = NetworkCapabilities, arg1 = seq number */ |
Remi NGUYEN VAN | 1fb66ad | 2021-03-12 15:24:06 +0900 | [diff] [blame] | 3732 | private static final int EXPIRE_LEGACY_REQUEST = 8; |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3733 | /** @hide */ |
Remi NGUYEN VAN | 1fb66ad | 2021-03-12 15:24:06 +0900 | [diff] [blame] | 3734 | public static final int CALLBACK_SUSPENDED = 9; |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3735 | /** @hide */ |
Remi NGUYEN VAN | 1fb66ad | 2021-03-12 15:24:06 +0900 | [diff] [blame] | 3736 | public static final int CALLBACK_RESUMED = 10; |
junyulai | f2c67e4 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3737 | /** @hide */ |
Remi NGUYEN VAN | 1fb66ad | 2021-03-12 15:24:06 +0900 | [diff] [blame] | 3738 | public static final int CALLBACK_BLK_CHANGED = 11; |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3739 | |
Erik Kline | 155a59a | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3740 | /** @hide */ |
| 3741 | public static String getCallbackName(int whichCallback) { |
| 3742 | switch (whichCallback) { |
| 3743 | case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK"; |
| 3744 | case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE"; |
| 3745 | case CALLBACK_LOSING: return "CALLBACK_LOSING"; |
| 3746 | case CALLBACK_LOST: return "CALLBACK_LOST"; |
| 3747 | case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL"; |
| 3748 | case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED"; |
| 3749 | case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED"; |
Erik Kline | 155a59a | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3750 | case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST"; |
| 3751 | case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED"; |
| 3752 | case CALLBACK_RESUMED: return "CALLBACK_RESUMED"; |
junyulai | f2c67e4 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3753 | case CALLBACK_BLK_CHANGED: return "CALLBACK_BLK_CHANGED"; |
Erik Kline | 155a59a | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3754 | default: |
| 3755 | return Integer.toString(whichCallback); |
| 3756 | } |
| 3757 | } |
| 3758 | |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 3759 | private class CallbackHandler extends Handler { |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3760 | private static final String TAG = "ConnectivityManager.CallbackHandler"; |
Robert Greenwalt | 72877c2 | 2015-09-03 16:41:45 -0700 | [diff] [blame] | 3761 | private static final boolean DBG = false; |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3762 | |
Hugo Benichi | 0b42baf | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3763 | CallbackHandler(Looper looper) { |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3764 | super(looper); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3765 | } |
| 3766 | |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3767 | CallbackHandler(Handler handler) { |
Remi NGUYEN VAN | 81f6842 | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 3768 | this(Objects.requireNonNull(handler, "Handler cannot be null.").getLooper()); |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3769 | } |
| 3770 | |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3771 | @Override |
| 3772 | public void handleMessage(Message message) { |
Hugo Benichi | b6c2406 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3773 | if (message.what == EXPIRE_LEGACY_REQUEST) { |
| 3774 | expireRequest((NetworkCapabilities) message.obj, message.arg1); |
| 3775 | return; |
| 3776 | } |
| 3777 | |
| 3778 | final NetworkRequest request = getObject(message, NetworkRequest.class); |
| 3779 | final Network network = getObject(message, Network.class); |
| 3780 | final NetworkCallback callback; |
| 3781 | synchronized (sCallbacks) { |
| 3782 | callback = sCallbacks.get(request); |
Etan Cohen | b58e366 | 2019-05-21 12:06:04 -0700 | [diff] [blame] | 3783 | if (callback == null) { |
| 3784 | Log.w(TAG, |
| 3785 | "callback not found for " + getCallbackName(message.what) + " message"); |
| 3786 | return; |
| 3787 | } |
Etan Cohen | 6cb6599 | 2019-04-16 15:07:55 -0700 | [diff] [blame] | 3788 | if (message.what == CALLBACK_UNAVAIL) { |
| 3789 | sCallbacks.remove(request); |
| 3790 | callback.networkRequest = ALREADY_UNREGISTERED; |
| 3791 | } |
Hugo Benichi | b6c2406 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3792 | } |
Lorenzo Colitti | b027e6e | 2016-03-01 22:56:37 +0900 | [diff] [blame] | 3793 | if (DBG) { |
Hugo Benichi | 8d96292 | 2017-03-22 17:07:57 +0900 | [diff] [blame] | 3794 | Log.d(TAG, getCallbackName(message.what) + " for network " + network); |
Lorenzo Colitti | b027e6e | 2016-03-01 22:56:37 +0900 | [diff] [blame] | 3795 | } |
Hugo Benichi | b6c2406 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3796 | |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3797 | switch (message.what) { |
| 3798 | case CALLBACK_PRECHECK: { |
Hugo Benichi | b6c2406 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3799 | callback.onPreCheck(network); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3800 | break; |
| 3801 | } |
| 3802 | case CALLBACK_AVAILABLE: { |
Chalard Jean | a23bc9e | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3803 | NetworkCapabilities cap = getObject(message, NetworkCapabilities.class); |
| 3804 | LinkProperties lp = getObject(message, LinkProperties.class); |
Lorenzo Colitti | 79c6f22 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 3805 | callback.onAvailable(network, cap, lp, message.arg1); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3806 | break; |
| 3807 | } |
| 3808 | case CALLBACK_LOSING: { |
Hugo Benichi | b6c2406 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3809 | callback.onLosing(network, message.arg1); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3810 | break; |
| 3811 | } |
| 3812 | case CALLBACK_LOST: { |
Hugo Benichi | b6c2406 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3813 | callback.onLost(network); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3814 | break; |
| 3815 | } |
| 3816 | case CALLBACK_UNAVAIL: { |
Hugo Benichi | b6c2406 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3817 | callback.onUnavailable(); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3818 | break; |
| 3819 | } |
| 3820 | case CALLBACK_CAP_CHANGED: { |
Hugo Benichi | b6c2406 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3821 | NetworkCapabilities cap = getObject(message, NetworkCapabilities.class); |
| 3822 | callback.onCapabilitiesChanged(network, cap); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3823 | break; |
| 3824 | } |
| 3825 | case CALLBACK_IP_CHANGED: { |
Hugo Benichi | b6c2406 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3826 | LinkProperties lp = getObject(message, LinkProperties.class); |
| 3827 | callback.onLinkPropertiesChanged(network, lp); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3828 | break; |
| 3829 | } |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3830 | case CALLBACK_SUSPENDED: { |
Hugo Benichi | b6c2406 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3831 | callback.onNetworkSuspended(network); |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3832 | break; |
| 3833 | } |
| 3834 | case CALLBACK_RESUMED: { |
Hugo Benichi | b6c2406 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3835 | callback.onNetworkResumed(network); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 3836 | break; |
| 3837 | } |
junyulai | f2c67e4 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3838 | case CALLBACK_BLK_CHANGED: { |
Lorenzo Colitti | 79c6f22 | 2021-03-18 00:54:57 +0900 | [diff] [blame] | 3839 | callback.onBlockedStatusChanged(network, message.arg1); |
junyulai | f2c67e4 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3840 | } |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3841 | } |
| 3842 | } |
| 3843 | |
Hugo Benichi | 0b42baf | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3844 | private <T> T getObject(Message msg, Class<T> c) { |
| 3845 | return (T) msg.getData().getParcelable(c.getSimpleName()); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3846 | } |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3847 | } |
| 3848 | |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3849 | private CallbackHandler getDefaultHandler() { |
Hugo Benichi | 3b41f05 | 2016-07-07 10:15:56 +0900 | [diff] [blame] | 3850 | synchronized (sCallbacks) { |
| 3851 | if (sCallbackHandler == null) { |
| 3852 | sCallbackHandler = new CallbackHandler(ConnectivityThread.getInstanceLooper()); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3853 | } |
Hugo Benichi | 3b41f05 | 2016-07-07 10:15:56 +0900 | [diff] [blame] | 3854 | return sCallbackHandler; |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3855 | } |
| 3856 | } |
| 3857 | |
Hugo Benichi | bc4ac97 | 2017-02-03 14:18:44 +0900 | [diff] [blame] | 3858 | private static final HashMap<NetworkRequest, NetworkCallback> sCallbacks = new HashMap<>(); |
| 3859 | private static CallbackHandler sCallbackHandler; |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3860 | |
Lorenzo Colitti | 2a49c5d | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 3861 | private NetworkRequest sendRequestForNetwork(int asUid, NetworkCapabilities need, |
| 3862 | NetworkCallback callback, int timeoutMs, NetworkRequest.Type reqType, int legacyType, |
| 3863 | CallbackHandler handler) { |
Soi, Yoshinari | b8102ef | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 3864 | printStackTrace(); |
Hugo Benichi | bc1104b | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 3865 | checkCallbackNotNull(callback); |
Remi NGUYEN VAN | 81f6842 | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 3866 | if (reqType != TRACK_DEFAULT && reqType != TRACK_SYSTEM_DEFAULT && need == null) { |
| 3867 | throw new IllegalArgumentException("null NetworkCapabilities"); |
| 3868 | } |
Hugo Benichi | 0b42baf | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3869 | final NetworkRequest request; |
Roshan Pius | bc7e37d | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 3870 | final String callingPackageName = mContext.getOpPackageName(); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3871 | try { |
Hugo Benichi | 0b42baf | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3872 | synchronized(sCallbacks) { |
Hugo Benichi | bee30fe | 2017-06-17 13:14:12 +0900 | [diff] [blame] | 3873 | if (callback.networkRequest != null |
| 3874 | && callback.networkRequest != ALREADY_UNREGISTERED) { |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 3875 | // TODO: throw exception instead and enforce 1:1 mapping of callbacks |
| 3876 | // and requests (http://b/20701525). |
| 3877 | Log.e(TAG, "NetworkCallback was already registered"); |
| 3878 | } |
Hugo Benichi | 3b41f05 | 2016-07-07 10:15:56 +0900 | [diff] [blame] | 3879 | Messenger messenger = new Messenger(handler); |
Hugo Benichi | 0b42baf | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3880 | Binder binder = new Binder(); |
Roshan Pius | 951c003 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 3881 | final int callbackFlags = callback.mFlags; |
junyulai | ad01079 | 2021-01-11 16:53:38 +0800 | [diff] [blame] | 3882 | if (reqType == LISTEN) { |
Roshan Pius | bc7e37d | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 3883 | request = mService.listenForNetwork( |
Roshan Pius | 951c003 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 3884 | need, messenger, binder, callbackFlags, callingPackageName, |
Roshan Pius | aa24fde | 2020-12-17 14:53:09 -0800 | [diff] [blame] | 3885 | getAttributionTag()); |
Paul Jensen | bb42768 | 2014-06-27 11:05:32 -0400 | [diff] [blame] | 3886 | } else { |
Hugo Benichi | 0b42baf | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3887 | request = mService.requestNetwork( |
Lorenzo Colitti | 2a49c5d | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 3888 | asUid, need, reqType.ordinal(), messenger, timeoutMs, binder, |
| 3889 | legacyType, callbackFlags, callingPackageName, getAttributionTag()); |
Paul Jensen | bb42768 | 2014-06-27 11:05:32 -0400 | [diff] [blame] | 3890 | } |
Hugo Benichi | 0b42baf | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3891 | if (request != null) { |
Hugo Benichi | 3b41f05 | 2016-07-07 10:15:56 +0900 | [diff] [blame] | 3892 | sCallbacks.put(request, callback); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3893 | } |
Hugo Benichi | 3b41f05 | 2016-07-07 10:15:56 +0900 | [diff] [blame] | 3894 | callback.networkRequest = request; |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3895 | } |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3896 | } catch (RemoteException e) { |
| 3897 | throw e.rethrowFromSystemServer(); |
Hugo Benichi | a590ab8 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 3898 | } catch (ServiceSpecificException e) { |
| 3899 | throw convertServiceException(e); |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3900 | } |
Hugo Benichi | 0b42baf | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3901 | return request; |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3902 | } |
| 3903 | |
Lorenzo Colitti | 2a49c5d | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 3904 | private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, NetworkCallback callback, |
| 3905 | int timeoutMs, NetworkRequest.Type reqType, int legacyType, CallbackHandler handler) { |
| 3906 | return sendRequestForNetwork(Process.INVALID_UID, need, callback, timeoutMs, reqType, |
| 3907 | legacyType, handler); |
| 3908 | } |
| 3909 | |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3910 | /** |
Erik Kline | 23bf99c | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 3911 | * Helper function to request a network with a particular legacy type. |
Lorenzo Colitti | 6653c4d | 2015-11-25 12:00:52 +0900 | [diff] [blame] | 3912 | * |
markchien | fac84a2 | 2020-03-18 21:16:15 +0800 | [diff] [blame] | 3913 | * This API is only for use in internal system code that requests networks with legacy type and |
| 3914 | * relies on CONNECTIVITY_ACTION broadcasts instead of NetworkCallbacks. New caller should use |
markchien | d6eeffd | 2020-01-14 00:55:21 +0800 | [diff] [blame] | 3915 | * {@link #requestNetwork(NetworkRequest, NetworkCallback, Handler)} instead. |
Lorenzo Colitti | 6653c4d | 2015-11-25 12:00:52 +0900 | [diff] [blame] | 3916 | * |
markchien | d6eeffd | 2020-01-14 00:55:21 +0800 | [diff] [blame] | 3917 | * @param request {@link NetworkRequest} describing this request. |
markchien | d6eeffd | 2020-01-14 00:55:21 +0800 | [diff] [blame] | 3918 | * @param timeoutMs The time in milliseconds to attempt looking for a suitable network |
| 3919 | * before {@link NetworkCallback#onUnavailable()} is called. The timeout must |
| 3920 | * be a positive value (i.e. >0). |
| 3921 | * @param legacyType to specify the network type(#TYPE_*). |
| 3922 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
markchien | fac84a2 | 2020-03-18 21:16:15 +0800 | [diff] [blame] | 3923 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 3924 | * the callback must not be shared - it uniquely specifies this request. |
Lorenzo Colitti | 6653c4d | 2015-11-25 12:00:52 +0900 | [diff] [blame] | 3925 | * |
| 3926 | * @hide |
| 3927 | */ |
markchien | d6eeffd | 2020-01-14 00:55:21 +0800 | [diff] [blame] | 3928 | @SystemApi |
markchien | fac84a2 | 2020-03-18 21:16:15 +0800 | [diff] [blame] | 3929 | @RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 3930 | public void requestNetwork(@NonNull NetworkRequest request, |
markchien | fac84a2 | 2020-03-18 21:16:15 +0800 | [diff] [blame] | 3931 | int timeoutMs, int legacyType, @NonNull Handler handler, |
| 3932 | @NonNull NetworkCallback networkCallback) { |
| 3933 | if (legacyType == TYPE_NONE) { |
| 3934 | throw new IllegalArgumentException("TYPE_NONE is meaningless legacy type"); |
| 3935 | } |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3936 | CallbackHandler cbHandler = new CallbackHandler(handler); |
| 3937 | NetworkCapabilities nc = request.networkCapabilities; |
| 3938 | sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, legacyType, cbHandler); |
Lorenzo Colitti | 6653c4d | 2015-11-25 12:00:52 +0900 | [diff] [blame] | 3939 | } |
| 3940 | |
| 3941 | /** |
Roshan Pius | 951c003 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 3942 | * Request a network to satisfy a set of {@link NetworkCapabilities}. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3943 | * |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3944 | * <p>This method will attempt to find the best network that matches the passed |
| 3945 | * {@link NetworkRequest}, and to bring up one that does if none currently satisfies the |
| 3946 | * criteria. The platform will evaluate which network is the best at its own discretion. |
| 3947 | * Throughput, latency, cost per byte, policy, user preference and other considerations |
| 3948 | * may be factored in the decision of what is considered the best network. |
| 3949 | * |
| 3950 | * <p>As long as this request is outstanding, the platform will try to maintain the best network |
| 3951 | * matching this request, while always attempting to match the request to a better network if |
| 3952 | * possible. If a better match is found, the platform will switch this request to the now-best |
| 3953 | * network and inform the app of the newly best network by invoking |
| 3954 | * {@link NetworkCallback#onAvailable(Network)} on the provided callback. Note that the platform |
| 3955 | * will not try to maintain any other network than the best one currently matching the request: |
| 3956 | * a network not matching any network request may be disconnected at any time. |
| 3957 | * |
| 3958 | * <p>For example, an application could use this method to obtain a connected cellular network |
| 3959 | * even if the device currently has a data connection over Ethernet. This may cause the cellular |
| 3960 | * radio to consume additional power. Or, an application could inform the system that it wants |
| 3961 | * a network supporting sending MMSes and have the system let it know about the currently best |
| 3962 | * MMS-supporting network through the provided {@link NetworkCallback}. |
| 3963 | * |
| 3964 | * <p>The status of the request can be followed by listening to the various callbacks described |
| 3965 | * in {@link NetworkCallback}. The {@link Network} object passed to the callback methods can be |
| 3966 | * used to direct traffic to the network (although accessing some networks may be subject to |
| 3967 | * holding specific permissions). Callers will learn about the specific characteristics of the |
| 3968 | * network through |
| 3969 | * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)} and |
| 3970 | * {@link NetworkCallback#onLinkPropertiesChanged(Network, LinkProperties)}. The methods of the |
| 3971 | * provided {@link NetworkCallback} will only be invoked due to changes in the best network |
| 3972 | * matching the request at any given time; therefore when a better network matching the request |
| 3973 | * becomes available, the {@link NetworkCallback#onAvailable(Network)} method is called |
| 3974 | * with the new network after which no further updates are given about the previously-best |
| 3975 | * network, unless it becomes the best again at some later time. All callbacks are invoked |
| 3976 | * in order on the same thread, which by default is a thread created by the framework running |
| 3977 | * in the app. |
| 3978 | * {@see #requestNetwork(NetworkRequest, NetworkCallback, Handler)} to change where the |
| 3979 | * callbacks are invoked. |
| 3980 | * |
| 3981 | * <p>This{@link NetworkRequest} will live until released via |
| 3982 | * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits, at |
| 3983 | * which point the system may let go of the network at any time. |
| 3984 | * |
| 3985 | * <p>A version of this method which takes a timeout is |
| 3986 | * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}, that an app can use to only |
| 3987 | * wait for a limited amount of time for the network to become unavailable. |
| 3988 | * |
Paul Jensen | ee52d23 | 2015-06-16 15:11:58 -0400 | [diff] [blame] | 3989 | * <p>It is presently unsupported to request a network with mutable |
| 3990 | * {@link NetworkCapabilities} such as |
| 3991 | * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or |
| 3992 | * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL} |
| 3993 | * as these {@code NetworkCapabilities} represent states that a particular |
| 3994 | * network may never attain, and whether a network will attain these states |
| 3995 | * is unknown prior to bringing up the network so the framework does not |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3996 | * know how to go about satisfying a request with these capabilities. |
Lorenzo Colitti | 0b40c87 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 3997 | * |
| 3998 | * <p>This method requires the caller to hold either the |
| 3999 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 4000 | * or the ability to modify system settings as determined by |
| 4001 | * {@link android.provider.Settings.System#canWrite}.</p> |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4002 | * |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4003 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4004 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4005 | * all variants of this method, of {@link #registerNetworkCallback} as well as |
| 4006 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4007 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4008 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4009 | * make sure to unregister the callbacks with |
| 4010 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 4011 | * |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 4012 | * @param request {@link NetworkRequest} describing this request. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4013 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 4014 | * the callback must not be shared - it uniquely specifies this request. |
| 4015 | * The callback is invoked on the default internal Handler. |
Chalard Jean | 31740e4 | 2019-05-13 15:13:58 +0900 | [diff] [blame] | 4016 | * @throws IllegalArgumentException if {@code request} contains invalid network capabilities. |
| 4017 | * @throws SecurityException if missing the appropriate permissions. |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4018 | * @throws RuntimeException if the app already has too many callbacks registered. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4019 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4020 | public void requestNetwork(@NonNull NetworkRequest request, |
| 4021 | @NonNull NetworkCallback networkCallback) { |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4022 | requestNetwork(request, networkCallback, getDefaultHandler()); |
| 4023 | } |
| 4024 | |
| 4025 | /** |
Roshan Pius | 951c003 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 4026 | * Request a network to satisfy a set of {@link NetworkCapabilities}. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4027 | * |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 4028 | * This method behaves identically to {@link #requestNetwork(NetworkRequest, NetworkCallback)} |
| 4029 | * but runs all the callbacks on the passed Handler. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4030 | * |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 4031 | * <p>This method has the same permission requirements as |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4032 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations, |
| 4033 | * and throws the same exceptions in the same conditions. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4034 | * |
| 4035 | * @param request {@link NetworkRequest} describing this request. |
| 4036 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 4037 | * the callback must not be shared - it uniquely specifies this request. |
| 4038 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4039 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4040 | public void requestNetwork(@NonNull NetworkRequest request, |
| 4041 | @NonNull NetworkCallback networkCallback, @NonNull Handler handler) { |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4042 | CallbackHandler cbHandler = new CallbackHandler(handler); |
markchien | fac84a2 | 2020-03-18 21:16:15 +0800 | [diff] [blame] | 4043 | NetworkCapabilities nc = request.networkCapabilities; |
| 4044 | sendRequestForNetwork(nc, networkCallback, 0, REQUEST, TYPE_NONE, cbHandler); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4045 | } |
| 4046 | |
| 4047 | /** |
Roshan Pius | 951c003 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 4048 | * Request a network to satisfy a set of {@link NetworkCapabilities}, limited |
Etan Cohen | f67bde9 | 2017-03-01 12:47:28 -0800 | [diff] [blame] | 4049 | * by a timeout. |
| 4050 | * |
| 4051 | * This function behaves identically to the non-timed-out version |
| 4052 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network |
| 4053 | * is not found within the given time (in milliseconds) the |
| 4054 | * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be |
| 4055 | * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does |
| 4056 | * not have to be released if timed-out (it is automatically released). Unregistering a |
| 4057 | * request that timed out is not an error. |
| 4058 | * |
| 4059 | * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small |
| 4060 | * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided |
| 4061 | * for that purpose. Calling this method will attempt to bring up the requested network. |
| 4062 | * |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 4063 | * <p>This method has the same permission requirements as |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4064 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations, |
| 4065 | * and throws the same exceptions in the same conditions. |
Etan Cohen | f67bde9 | 2017-03-01 12:47:28 -0800 | [diff] [blame] | 4066 | * |
| 4067 | * @param request {@link NetworkRequest} describing this request. |
Lorenzo Colitti | 139a48c | 2017-04-28 00:56:30 +0900 | [diff] [blame] | 4068 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 4069 | * the callback must not be shared - it uniquely specifies this request. |
Etan Cohen | f67bde9 | 2017-03-01 12:47:28 -0800 | [diff] [blame] | 4070 | * @param timeoutMs The time in milliseconds to attempt looking for a suitable network |
| 4071 | * before {@link NetworkCallback#onUnavailable()} is called. The timeout must |
| 4072 | * be a positive value (i.e. >0). |
Etan Cohen | f67bde9 | 2017-03-01 12:47:28 -0800 | [diff] [blame] | 4073 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4074 | public void requestNetwork(@NonNull NetworkRequest request, |
| 4075 | @NonNull NetworkCallback networkCallback, int timeoutMs) { |
Hugo Benichi | bc1104b | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 4076 | checkTimeout(timeoutMs); |
markchien | fac84a2 | 2020-03-18 21:16:15 +0800 | [diff] [blame] | 4077 | NetworkCapabilities nc = request.networkCapabilities; |
| 4078 | sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, TYPE_NONE, |
| 4079 | getDefaultHandler()); |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4080 | } |
| 4081 | |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4082 | /** |
Roshan Pius | 951c003 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 4083 | * Request a network to satisfy a set of {@link NetworkCapabilities}, limited |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4084 | * by a timeout. |
| 4085 | * |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 4086 | * This method behaves identically to |
| 4087 | * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} but runs all the callbacks |
| 4088 | * on the passed Handler. |
Etan Cohen | f67bde9 | 2017-03-01 12:47:28 -0800 | [diff] [blame] | 4089 | * |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 4090 | * <p>This method has the same permission requirements as |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4091 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations, |
| 4092 | * and throws the same exceptions in the same conditions. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4093 | * |
| 4094 | * @param request {@link NetworkRequest} describing this request. |
Etan Cohen | f67bde9 | 2017-03-01 12:47:28 -0800 | [diff] [blame] | 4095 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 4096 | * the callback must not be shared - it uniquely specifies this request. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4097 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
Lorenzo Colitti | 139a48c | 2017-04-28 00:56:30 +0900 | [diff] [blame] | 4098 | * @param timeoutMs The time in milliseconds to attempt looking for a suitable network |
| 4099 | * before {@link NetworkCallback#onUnavailable} is called. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4100 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4101 | public void requestNetwork(@NonNull NetworkRequest request, |
| 4102 | @NonNull NetworkCallback networkCallback, @NonNull Handler handler, int timeoutMs) { |
Hugo Benichi | bc1104b | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 4103 | checkTimeout(timeoutMs); |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4104 | CallbackHandler cbHandler = new CallbackHandler(handler); |
markchien | fac84a2 | 2020-03-18 21:16:15 +0800 | [diff] [blame] | 4105 | NetworkCapabilities nc = request.networkCapabilities; |
| 4106 | sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, TYPE_NONE, cbHandler); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4107 | } |
| 4108 | |
| 4109 | /** |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 4110 | * The lookup key for a {@link Network} object included with the intent after |
Jeremy Joslin | 60d379b | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4111 | * successfully finding a network for the applications request. Retrieve it with |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 4112 | * {@link android.content.Intent#getParcelableExtra(String)}. |
Jeremy Joslin | c530263 | 2015-02-11 16:51:13 -0800 | [diff] [blame] | 4113 | * <p> |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 4114 | * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)} |
| 4115 | * then you must get a ConnectivityManager instance before doing so. |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 4116 | */ |
Erik Kline | 8a82621 | 2014-11-19 12:12:24 +0900 | [diff] [blame] | 4117 | public static final String EXTRA_NETWORK = "android.net.extra.NETWORK"; |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 4118 | |
| 4119 | /** |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 4120 | * The lookup key for a {@link NetworkRequest} object included with the intent after |
Jeremy Joslin | 60d379b | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4121 | * successfully finding a network for the applications request. Retrieve it with |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 4122 | * {@link android.content.Intent#getParcelableExtra(String)}. |
| 4123 | */ |
Erik Kline | 8a82621 | 2014-11-19 12:12:24 +0900 | [diff] [blame] | 4124 | public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST"; |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 4125 | |
| 4126 | |
| 4127 | /** |
Roshan Pius | 951c003 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 4128 | * Request a network to satisfy a set of {@link NetworkCapabilities}. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4129 | * |
Jeremy Joslin | 60d379b | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4130 | * This function behaves identically to the version that takes a NetworkCallback, but instead |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 4131 | * of {@link NetworkCallback} a {@link PendingIntent} is used. This means |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 4132 | * the request may outlive the calling application and get called back when a suitable |
| 4133 | * network is found. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4134 | * <p> |
| 4135 | * The operation is an Intent broadcast that goes to a broadcast receiver that |
| 4136 | * you registered with {@link Context#registerReceiver} or through the |
| 4137 | * <receiver> tag in an AndroidManifest.xml file |
| 4138 | * <p> |
| 4139 | * The operation Intent is delivered with two extras, a {@link Network} typed |
Erik Kline | 8a82621 | 2014-11-19 12:12:24 +0900 | [diff] [blame] | 4140 | * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest} |
| 4141 | * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4142 | * the original requests parameters. It is important to create a new, |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 4143 | * {@link NetworkCallback} based request before completing the processing of the |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4144 | * Intent to reserve the network or it will be released shortly after the Intent |
| 4145 | * is processed. |
| 4146 | * <p> |
Paul Jensen | c8873fc | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 4147 | * If there is already a request for this Intent registered (with the equality of |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4148 | * two Intents defined by {@link Intent#filterEquals}), then it will be removed and |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 4149 | * replaced by this one, effectively releasing the previous {@link NetworkRequest}. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4150 | * <p> |
Jeremy Joslin | 60d379b | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4151 | * The request may be released normally by calling |
| 4152 | * {@link #releaseNetworkRequest(android.app.PendingIntent)}. |
Paul Jensen | ee52d23 | 2015-06-16 15:11:58 -0400 | [diff] [blame] | 4153 | * <p>It is presently unsupported to request a network with either |
| 4154 | * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or |
| 4155 | * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL} |
| 4156 | * as these {@code NetworkCapabilities} represent states that a particular |
| 4157 | * network may never attain, and whether a network will attain these states |
| 4158 | * is unknown prior to bringing up the network so the framework does not |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 4159 | * know how to go about satisfying a request with these capabilities. |
Lorenzo Colitti | 0b40c87 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 4160 | * |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4161 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4162 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4163 | * all variants of this method, of {@link #registerNetworkCallback} as well as |
| 4164 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4165 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4166 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4167 | * make sure to unregister the callbacks with {@link #unregisterNetworkCallback(PendingIntent)} |
| 4168 | * or {@link #releaseNetworkRequest(PendingIntent)}. |
| 4169 | * |
Lorenzo Colitti | 0b40c87 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 4170 | * <p>This method requires the caller to hold either the |
| 4171 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 4172 | * or the ability to modify system settings as determined by |
| 4173 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 4174 | * |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 4175 | * @param request {@link NetworkRequest} describing this request. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4176 | * @param operation Action to perform when the network is available (corresponds |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 4177 | * to the {@link NetworkCallback#onAvailable} call. Typically |
Jeremy Joslin | 60d379b | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4178 | * comes from {@link PendingIntent#getBroadcast}. Cannot be null. |
Chalard Jean | 31740e4 | 2019-05-13 15:13:58 +0900 | [diff] [blame] | 4179 | * @throws IllegalArgumentException if {@code request} contains invalid network capabilities. |
| 4180 | * @throws SecurityException if missing the appropriate permissions. |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4181 | * @throws RuntimeException if the app already has too many callbacks registered. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4182 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4183 | public void requestNetwork(@NonNull NetworkRequest request, |
| 4184 | @NonNull PendingIntent operation) { |
Soi, Yoshinari | b8102ef | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 4185 | printStackTrace(); |
Hugo Benichi | bc1104b | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 4186 | checkPendingIntentNotNull(operation); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4187 | try { |
Roshan Pius | bc7e37d | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 4188 | mService.pendingRequestForNetwork( |
Philip P. Moltmann | 7bc33df | 2020-03-26 11:50:35 -0700 | [diff] [blame] | 4189 | request.networkCapabilities, operation, mContext.getOpPackageName(), |
| 4190 | getAttributionTag()); |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4191 | } catch (RemoteException e) { |
| 4192 | throw e.rethrowFromSystemServer(); |
Hugo Benichi | a590ab8 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 4193 | } catch (ServiceSpecificException e) { |
| 4194 | throw convertServiceException(e); |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4195 | } |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4196 | } |
| 4197 | |
| 4198 | /** |
Jeremy Joslin | 60d379b | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4199 | * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} |
| 4200 | * <p> |
Lorenzo Colitti | 15874cd | 2016-04-13 22:00:02 +0900 | [diff] [blame] | 4201 | * This method has the same behavior as |
| 4202 | * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to |
Jeremy Joslin | 60d379b | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4203 | * releasing network resources and disconnecting. |
| 4204 | * |
| 4205 | * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the |
| 4206 | * PendingIntent passed to |
| 4207 | * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the |
| 4208 | * corresponding NetworkRequest you'd like to remove. Cannot be null. |
| 4209 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4210 | public void releaseNetworkRequest(@NonNull PendingIntent operation) { |
Soi, Yoshinari | b8102ef | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 4211 | printStackTrace(); |
Hugo Benichi | bc1104b | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 4212 | checkPendingIntentNotNull(operation); |
Jeremy Joslin | 60d379b | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4213 | try { |
| 4214 | mService.releasePendingNetworkRequest(operation); |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4215 | } catch (RemoteException e) { |
| 4216 | throw e.rethrowFromSystemServer(); |
| 4217 | } |
Jeremy Joslin | 60d379b | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4218 | } |
| 4219 | |
Hugo Benichi | bc1104b | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 4220 | private static void checkPendingIntentNotNull(PendingIntent intent) { |
Remi NGUYEN VAN | 81f6842 | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 4221 | Objects.requireNonNull(intent, "PendingIntent cannot be null."); |
Hugo Benichi | bc1104b | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 4222 | } |
| 4223 | |
| 4224 | private static void checkCallbackNotNull(NetworkCallback callback) { |
Remi NGUYEN VAN | 81f6842 | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 4225 | Objects.requireNonNull(callback, "null NetworkCallback"); |
Hugo Benichi | bc1104b | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 4226 | } |
| 4227 | |
| 4228 | private static void checkTimeout(int timeoutMs) { |
Remi NGUYEN VAN | 81f6842 | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 4229 | if (timeoutMs <= 0) { |
| 4230 | throw new IllegalArgumentException("timeoutMs must be strictly positive."); |
| 4231 | } |
Jeremy Joslin | 60d379b | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4232 | } |
| 4233 | |
| 4234 | /** |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4235 | * Registers to receive notifications about all networks which satisfy the given |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 4236 | * {@link NetworkRequest}. The callbacks will continue to be called until |
Chiachang Wang | 3b723c2 | 2019-02-27 17:14:50 +0800 | [diff] [blame] | 4237 | * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is |
| 4238 | * called. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4239 | * |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4240 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4241 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4242 | * all variants of this method, of {@link #requestNetwork} as well as |
| 4243 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4244 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4245 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4246 | * make sure to unregister the callbacks with |
| 4247 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 4248 | * |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 4249 | * @param request {@link NetworkRequest} describing this request. |
| 4250 | * @param networkCallback The {@link NetworkCallback} that the system will call as suitable |
| 4251 | * networks change state. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4252 | * The callback is invoked on the default internal Handler. |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4253 | * @throws RuntimeException if the app already has too many callbacks registered. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4254 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 4255 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4256 | public void registerNetworkCallback(@NonNull NetworkRequest request, |
| 4257 | @NonNull NetworkCallback networkCallback) { |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4258 | registerNetworkCallback(request, networkCallback, getDefaultHandler()); |
| 4259 | } |
| 4260 | |
| 4261 | /** |
| 4262 | * Registers to receive notifications about all networks which satisfy the given |
| 4263 | * {@link NetworkRequest}. The callbacks will continue to be called until |
Chiachang Wang | 3b723c2 | 2019-02-27 17:14:50 +0800 | [diff] [blame] | 4264 | * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is |
| 4265 | * called. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4266 | * |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4267 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4268 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4269 | * all variants of this method, of {@link #requestNetwork} as well as |
| 4270 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4271 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4272 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4273 | * make sure to unregister the callbacks with |
| 4274 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 4275 | * |
| 4276 | * |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4277 | * @param request {@link NetworkRequest} describing this request. |
| 4278 | * @param networkCallback The {@link NetworkCallback} that the system will call as suitable |
| 4279 | * networks change state. |
| 4280 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4281 | * @throws RuntimeException if the app already has too many callbacks registered. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4282 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 4283 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4284 | public void registerNetworkCallback(@NonNull NetworkRequest request, |
| 4285 | @NonNull NetworkCallback networkCallback, @NonNull Handler handler) { |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4286 | CallbackHandler cbHandler = new CallbackHandler(handler); |
| 4287 | NetworkCapabilities nc = request.networkCapabilities; |
| 4288 | sendRequestForNetwork(nc, networkCallback, 0, LISTEN, TYPE_NONE, cbHandler); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4289 | } |
| 4290 | |
| 4291 | /** |
Paul Jensen | c8873fc | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 4292 | * Registers a PendingIntent to be sent when a network is available which satisfies the given |
| 4293 | * {@link NetworkRequest}. |
| 4294 | * |
| 4295 | * This function behaves identically to the version that takes a NetworkCallback, but instead |
| 4296 | * of {@link NetworkCallback} a {@link PendingIntent} is used. This means |
| 4297 | * the request may outlive the calling application and get called back when a suitable |
| 4298 | * network is found. |
| 4299 | * <p> |
| 4300 | * The operation is an Intent broadcast that goes to a broadcast receiver that |
| 4301 | * you registered with {@link Context#registerReceiver} or through the |
| 4302 | * <receiver> tag in an AndroidManifest.xml file |
| 4303 | * <p> |
| 4304 | * The operation Intent is delivered with two extras, a {@link Network} typed |
| 4305 | * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest} |
| 4306 | * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing |
| 4307 | * the original requests parameters. |
| 4308 | * <p> |
| 4309 | * If there is already a request for this Intent registered (with the equality of |
| 4310 | * two Intents defined by {@link Intent#filterEquals}), then it will be removed and |
| 4311 | * replaced by this one, effectively releasing the previous {@link NetworkRequest}. |
| 4312 | * <p> |
| 4313 | * The request may be released normally by calling |
Paul Jensen | 169f662 | 2015-06-30 14:29:18 -0400 | [diff] [blame] | 4314 | * {@link #unregisterNetworkCallback(android.app.PendingIntent)}. |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4315 | * |
| 4316 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4317 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4318 | * all variants of this method, of {@link #requestNetwork} as well as |
| 4319 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4320 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4321 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4322 | * make sure to unregister the callbacks with {@link #unregisterNetworkCallback(PendingIntent)} |
| 4323 | * or {@link #releaseNetworkRequest(PendingIntent)}. |
| 4324 | * |
Paul Jensen | c8873fc | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 4325 | * @param request {@link NetworkRequest} describing this request. |
| 4326 | * @param operation Action to perform when the network is available (corresponds |
| 4327 | * to the {@link NetworkCallback#onAvailable} call. Typically |
| 4328 | * comes from {@link PendingIntent#getBroadcast}. Cannot be null. |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4329 | * @throws RuntimeException if the app already has too many callbacks registered. |
Paul Jensen | c8873fc | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 4330 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 4331 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4332 | public void registerNetworkCallback(@NonNull NetworkRequest request, |
| 4333 | @NonNull PendingIntent operation) { |
Soi, Yoshinari | b8102ef | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 4334 | printStackTrace(); |
Hugo Benichi | bc1104b | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 4335 | checkPendingIntentNotNull(operation); |
Paul Jensen | c8873fc | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 4336 | try { |
Roshan Pius | bc7e37d | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 4337 | mService.pendingListenForNetwork( |
Roshan Pius | aa24fde | 2020-12-17 14:53:09 -0800 | [diff] [blame] | 4338 | request.networkCapabilities, operation, mContext.getOpPackageName(), |
| 4339 | getAttributionTag()); |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4340 | } catch (RemoteException e) { |
| 4341 | throw e.rethrowFromSystemServer(); |
Hugo Benichi | a590ab8 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 4342 | } catch (ServiceSpecificException e) { |
| 4343 | throw convertServiceException(e); |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4344 | } |
Paul Jensen | c8873fc | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 4345 | } |
| 4346 | |
| 4347 | /** |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 4348 | * Registers to receive notifications about changes in the application's default network. This |
| 4349 | * may be a physical network or a virtual network, such as a VPN that applies to the |
| 4350 | * application. The callbacks will continue to be called until either the application exits or |
Lorenzo Colitti | 15874cd | 2016-04-13 22:00:02 +0900 | [diff] [blame] | 4351 | * {@link #unregisterNetworkCallback(NetworkCallback)} is called. |
Erik Kline | 23bf99c | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 4352 | * |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4353 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4354 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4355 | * all variants of this method, of {@link #requestNetwork} as well as |
| 4356 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4357 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4358 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4359 | * make sure to unregister the callbacks with |
| 4360 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 4361 | * |
Erik Kline | 23bf99c | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 4362 | * @param networkCallback The {@link NetworkCallback} that the system will call as the |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 4363 | * application's default network changes. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4364 | * The callback is invoked on the default internal Handler. |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4365 | * @throws RuntimeException if the app already has too many callbacks registered. |
Erik Kline | 23bf99c | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 4366 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 4367 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4368 | public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback) { |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4369 | registerDefaultNetworkCallback(networkCallback, getDefaultHandler()); |
| 4370 | } |
| 4371 | |
| 4372 | /** |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 4373 | * Registers to receive notifications about changes in the application's default network. This |
| 4374 | * may be a physical network or a virtual network, such as a VPN that applies to the |
| 4375 | * application. The callbacks will continue to be called until either the application exits or |
| 4376 | * {@link #unregisterNetworkCallback(NetworkCallback)} is called. |
| 4377 | * |
| 4378 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4379 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4380 | * all variants of this method, of {@link #requestNetwork} as well as |
| 4381 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4382 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4383 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4384 | * make sure to unregister the callbacks with |
| 4385 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 4386 | * |
| 4387 | * @param networkCallback The {@link NetworkCallback} that the system will call as the |
| 4388 | * application's default network changes. |
| 4389 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
| 4390 | * @throws RuntimeException if the app already has too many callbacks registered. |
| 4391 | */ |
| 4392 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 4393 | public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback, |
| 4394 | @NonNull Handler handler) { |
Lorenzo Colitti | 2a49c5d | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 4395 | registerDefaultNetworkCallbackAsUid(Process.INVALID_UID, networkCallback, handler); |
| 4396 | } |
| 4397 | |
| 4398 | /** |
| 4399 | * Registers to receive notifications about changes in the default network for the specified |
| 4400 | * UID. This may be a physical network or a virtual network, such as a VPN that applies to the |
| 4401 | * UID. The callbacks will continue to be called until either the application exits or |
| 4402 | * {@link #unregisterNetworkCallback(NetworkCallback)} is called. |
| 4403 | * |
| 4404 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4405 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4406 | * all variants of this method, of {@link #requestNetwork} as well as |
| 4407 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4408 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4409 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4410 | * make sure to unregister the callbacks with |
| 4411 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 4412 | * |
| 4413 | * @param uid the UID for which to track default network changes. |
| 4414 | * @param networkCallback The {@link NetworkCallback} that the system will call as the |
| 4415 | * UID's default network changes. |
| 4416 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
| 4417 | * @throws RuntimeException if the app already has too many callbacks registered. |
| 4418 | * @hide |
| 4419 | */ |
Lorenzo Colitti | 92ed8b9 | 2021-03-22 18:23:21 +0900 | [diff] [blame] | 4420 | @SystemApi(client = MODULE_LIBRARIES) |
Lorenzo Colitti | 2a49c5d | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 4421 | @SuppressLint({"ExecutorRegistration", "PairedRegistration"}) |
| 4422 | @RequiresPermission(anyOf = { |
| 4423 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 4424 | android.Manifest.permission.NETWORK_SETTINGS}) |
| 4425 | public void registerDefaultNetworkCallbackAsUid(int uid, |
| 4426 | @NonNull NetworkCallback networkCallback, @NonNull Handler handler) { |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 4427 | CallbackHandler cbHandler = new CallbackHandler(handler); |
Lorenzo Colitti | 2a49c5d | 2021-03-12 22:48:07 +0900 | [diff] [blame] | 4428 | sendRequestForNetwork(uid, null /* need */, networkCallback, 0 /* timeoutMs */, |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 4429 | TRACK_DEFAULT, TYPE_NONE, cbHandler); |
| 4430 | } |
| 4431 | |
| 4432 | /** |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4433 | * Registers to receive notifications about changes in the system default network. The callbacks |
| 4434 | * will continue to be called until either the application exits or |
| 4435 | * {@link #unregisterNetworkCallback(NetworkCallback)} is called. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4436 | * |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 4437 | * This method should not be used to determine networking state seen by applications, because in |
| 4438 | * many cases, most or even all application traffic may not use the default network directly, |
| 4439 | * and traffic from different applications may go on different networks by default. As an |
| 4440 | * example, if a VPN is connected, traffic from all applications might be sent through the VPN |
| 4441 | * and not onto the system default network. Applications or system components desiring to do |
| 4442 | * determine network state as seen by applications should use other methods such as |
| 4443 | * {@link #registerDefaultNetworkCallback(NetworkCallback, Handler)}. |
| 4444 | * |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4445 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4446 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4447 | * all variants of this method, of {@link #requestNetwork} as well as |
| 4448 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4449 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4450 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4451 | * make sure to unregister the callbacks with |
| 4452 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 4453 | * |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4454 | * @param networkCallback The {@link NetworkCallback} that the system will call as the |
| 4455 | * system default network changes. |
| 4456 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4457 | * @throws RuntimeException if the app already has too many callbacks registered. |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 4458 | * |
| 4459 | * @hide |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4460 | */ |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 4461 | @SystemApi(client = MODULE_LIBRARIES) |
| 4462 | @SuppressLint({"ExecutorRegistration", "PairedRegistration"}) |
| 4463 | @RequiresPermission(anyOf = { |
| 4464 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 4465 | android.Manifest.permission.NETWORK_SETTINGS}) |
| 4466 | public void registerSystemDefaultNetworkCallback(@NonNull NetworkCallback networkCallback, |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4467 | @NonNull Handler handler) { |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4468 | CallbackHandler cbHandler = new CallbackHandler(handler); |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 4469 | sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0, |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 4470 | TRACK_SYSTEM_DEFAULT, TYPE_NONE, cbHandler); |
Erik Kline | 23bf99c | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 4471 | } |
| 4472 | |
| 4473 | /** |
junyulai | 8eb13a2 | 2021-03-15 11:48:48 +0800 | [diff] [blame] | 4474 | * Registers to receive notifications about the best matching network which satisfy the given |
| 4475 | * {@link NetworkRequest}. The callbacks will continue to be called until |
| 4476 | * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is |
| 4477 | * called. |
| 4478 | * |
| 4479 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4480 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4481 | * {@link #registerNetworkCallback} and its variants and {@link #requestNetwork} as well as |
| 4482 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4483 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4484 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4485 | * make sure to unregister the callbacks with |
| 4486 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 4487 | * |
| 4488 | * |
| 4489 | * @param request {@link NetworkRequest} describing this request. |
| 4490 | * @param networkCallback The {@link NetworkCallback} that the system will call as suitable |
| 4491 | * networks change state. |
| 4492 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
| 4493 | * @throws RuntimeException if the app already has too many callbacks registered. |
junyulai | 7514e31 | 2021-03-05 15:51:17 +0800 | [diff] [blame] | 4494 | */ |
junyulai | 7514e31 | 2021-03-05 15:51:17 +0800 | [diff] [blame] | 4495 | @SuppressLint("ExecutorRegistration") |
| 4496 | public void registerBestMatchingNetworkCallback(@NonNull NetworkRequest request, |
| 4497 | @NonNull NetworkCallback networkCallback, @NonNull Handler handler) { |
| 4498 | final NetworkCapabilities nc = request.networkCapabilities; |
| 4499 | final CallbackHandler cbHandler = new CallbackHandler(handler); |
junyulai | 1b1c874 | 2021-03-12 20:05:08 +0800 | [diff] [blame] | 4500 | sendRequestForNetwork(nc, networkCallback, 0, LISTEN_FOR_BEST, TYPE_NONE, cbHandler); |
junyulai | 7514e31 | 2021-03-05 15:51:17 +0800 | [diff] [blame] | 4501 | } |
| 4502 | |
| 4503 | /** |
fenglu | b00f488 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 4504 | * Requests bandwidth update for a given {@link Network} and returns whether the update request |
| 4505 | * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying |
| 4506 | * network connection for updated bandwidth information. The caller will be notified via |
| 4507 | * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this |
Lorenzo Colitti | 15874cd | 2016-04-13 22:00:02 +0900 | [diff] [blame] | 4508 | * method assumes that the caller has previously called |
| 4509 | * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network |
| 4510 | * changes. |
fenglu | 3f35740 | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 4511 | * |
fenglu | ea2d928 | 2015-04-27 14:28:04 -0700 | [diff] [blame] | 4512 | * @param network {@link Network} specifying which network you're interested. |
fenglu | b00f488 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 4513 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
fenglu | 3f35740 | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 4514 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4515 | public boolean requestBandwidthUpdate(@NonNull Network network) { |
fenglu | 3f35740 | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 4516 | try { |
fenglu | b00f488 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 4517 | return mService.requestBandwidthUpdate(network); |
fenglu | 3f35740 | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 4518 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4519 | throw e.rethrowFromSystemServer(); |
fenglu | 3f35740 | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 4520 | } |
| 4521 | } |
| 4522 | |
| 4523 | /** |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 4524 | * Unregisters a {@code NetworkCallback} and possibly releases networks originating from |
Lorenzo Colitti | 15874cd | 2016-04-13 22:00:02 +0900 | [diff] [blame] | 4525 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and |
| 4526 | * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls. |
| 4527 | * If the given {@code NetworkCallback} had previously been used with |
Lorenzo Colitti | 7f6a2bf | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 4528 | * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request |
| 4529 | * will be disconnected. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4530 | * |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 4531 | * Notifications that would have triggered that {@code NetworkCallback} will immediately stop |
| 4532 | * triggering it as soon as this call returns. |
| 4533 | * |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 4534 | * @param networkCallback The {@link NetworkCallback} used when making the request. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4535 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4536 | public void unregisterNetworkCallback(@NonNull NetworkCallback networkCallback) { |
Soi, Yoshinari | b8102ef | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 4537 | printStackTrace(); |
Hugo Benichi | bc1104b | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 4538 | checkCallbackNotNull(networkCallback); |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 4539 | final List<NetworkRequest> reqs = new ArrayList<>(); |
| 4540 | // Find all requests associated to this callback and stop callback triggers immediately. |
| 4541 | // Callback is reusable immediately. http://b/20701525, http://b/35921499. |
| 4542 | synchronized (sCallbacks) { |
Remi NGUYEN VAN | 81f6842 | 2021-03-15 07:31:54 +0000 | [diff] [blame] | 4543 | if (networkCallback.networkRequest == null) { |
| 4544 | throw new IllegalArgumentException("NetworkCallback was not registered"); |
| 4545 | } |
Etan Cohen | 6cb6599 | 2019-04-16 15:07:55 -0700 | [diff] [blame] | 4546 | if (networkCallback.networkRequest == ALREADY_UNREGISTERED) { |
| 4547 | Log.d(TAG, "NetworkCallback was already unregistered"); |
| 4548 | return; |
| 4549 | } |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 4550 | for (Map.Entry<NetworkRequest, NetworkCallback> e : sCallbacks.entrySet()) { |
| 4551 | if (e.getValue() == networkCallback) { |
| 4552 | reqs.add(e.getKey()); |
| 4553 | } |
| 4554 | } |
| 4555 | // TODO: throw exception if callback was registered more than once (http://b/20701525). |
| 4556 | for (NetworkRequest r : reqs) { |
| 4557 | try { |
| 4558 | mService.releaseNetworkRequest(r); |
| 4559 | } catch (RemoteException e) { |
| 4560 | throw e.rethrowFromSystemServer(); |
| 4561 | } |
| 4562 | // Only remove mapping if rpc was successful. |
| 4563 | sCallbacks.remove(r); |
| 4564 | } |
Hugo Benichi | bee30fe | 2017-06-17 13:14:12 +0900 | [diff] [blame] | 4565 | networkCallback.networkRequest = ALREADY_UNREGISTERED; |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4566 | } |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4567 | } |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4568 | |
| 4569 | /** |
Paul Jensen | 169f662 | 2015-06-30 14:29:18 -0400 | [diff] [blame] | 4570 | * Unregisters a callback previously registered via |
| 4571 | * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}. |
| 4572 | * |
| 4573 | * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the |
| 4574 | * PendingIntent passed to |
| 4575 | * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}. |
| 4576 | * Cannot be null. |
| 4577 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4578 | public void unregisterNetworkCallback(@NonNull PendingIntent operation) { |
Paul Jensen | 169f662 | 2015-06-30 14:29:18 -0400 | [diff] [blame] | 4579 | releaseNetworkRequest(operation); |
| 4580 | } |
| 4581 | |
| 4582 | /** |
Lorenzo Colitti | 6947c06 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 4583 | * Informs the system whether it should switch to {@code network} regardless of whether it is |
| 4584 | * validated or not. If {@code accept} is true, and the network was explicitly selected by the |
| 4585 | * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become |
| 4586 | * the system default network regardless of any other network that's currently connected. If |
| 4587 | * {@code always} is true, then the choice is remembered, so that the next time the user |
| 4588 | * connects to this network, the system will switch to it. |
| 4589 | * |
Lorenzo Colitti | 6947c06 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 4590 | * @param network The network to accept. |
| 4591 | * @param accept Whether to accept the network even if unvalidated. |
| 4592 | * @param always Whether to remember this choice in the future. |
| 4593 | * |
| 4594 | * @hide |
| 4595 | */ |
Chiachang Wang | 4d51357 | 2021-03-18 09:44:34 +0800 | [diff] [blame] | 4596 | @SystemApi(client = MODULE_LIBRARIES) |
| 4597 | @RequiresPermission(anyOf = { |
| 4598 | android.Manifest.permission.NETWORK_SETTINGS, |
| 4599 | android.Manifest.permission.NETWORK_SETUP_WIZARD, |
| 4600 | android.Manifest.permission.NETWORK_STACK, |
| 4601 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) |
| 4602 | public void setAcceptUnvalidated(@NonNull Network network, boolean accept, boolean always) { |
Lorenzo Colitti | 6947c06 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 4603 | try { |
| 4604 | mService.setAcceptUnvalidated(network, accept, always); |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4605 | } catch (RemoteException e) { |
| 4606 | throw e.rethrowFromSystemServer(); |
| 4607 | } |
Lorenzo Colitti | 6947c06 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 4608 | } |
| 4609 | |
| 4610 | /** |
lucaslin | 2240ef6 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 4611 | * Informs the system whether it should consider the network as validated even if it only has |
| 4612 | * partial connectivity. If {@code accept} is true, then the network will be considered as |
| 4613 | * validated even if connectivity is only partial. If {@code always} is true, then the choice |
| 4614 | * is remembered, so that the next time the user connects to this network, the system will |
| 4615 | * switch to it. |
| 4616 | * |
| 4617 | * @param network The network to accept. |
| 4618 | * @param accept Whether to consider the network as validated even if it has partial |
| 4619 | * connectivity. |
| 4620 | * @param always Whether to remember this choice in the future. |
| 4621 | * |
| 4622 | * @hide |
| 4623 | */ |
Chiachang Wang | 4d51357 | 2021-03-18 09:44:34 +0800 | [diff] [blame] | 4624 | @SystemApi(client = MODULE_LIBRARIES) |
| 4625 | @RequiresPermission(anyOf = { |
| 4626 | android.Manifest.permission.NETWORK_SETTINGS, |
| 4627 | android.Manifest.permission.NETWORK_SETUP_WIZARD, |
| 4628 | android.Manifest.permission.NETWORK_STACK, |
| 4629 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) |
| 4630 | public void setAcceptPartialConnectivity(@NonNull Network network, boolean accept, |
| 4631 | boolean always) { |
lucaslin | 2240ef6 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 4632 | try { |
| 4633 | mService.setAcceptPartialConnectivity(network, accept, always); |
| 4634 | } catch (RemoteException e) { |
| 4635 | throw e.rethrowFromSystemServer(); |
| 4636 | } |
| 4637 | } |
| 4638 | |
| 4639 | /** |
Lorenzo Colitti | c65750c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 4640 | * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is |
| 4641 | * only meaningful if the system is configured not to penalize such networks, e.g., if the |
| 4642 | * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code |
| 4643 | * NETWORK_AVOID_BAD_WIFI setting is unset}. |
| 4644 | * |
Lorenzo Colitti | c65750c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 4645 | * @param network The network to accept. |
| 4646 | * |
| 4647 | * @hide |
| 4648 | */ |
Chiachang Wang | 4d51357 | 2021-03-18 09:44:34 +0800 | [diff] [blame] | 4649 | @SystemApi(client = MODULE_LIBRARIES) |
| 4650 | @RequiresPermission(anyOf = { |
| 4651 | android.Manifest.permission.NETWORK_SETTINGS, |
| 4652 | android.Manifest.permission.NETWORK_SETUP_WIZARD, |
| 4653 | android.Manifest.permission.NETWORK_STACK, |
| 4654 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) |
| 4655 | public void setAvoidUnvalidated(@NonNull Network network) { |
Lorenzo Colitti | c65750c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 4656 | try { |
| 4657 | mService.setAvoidUnvalidated(network); |
| 4658 | } catch (RemoteException e) { |
| 4659 | throw e.rethrowFromSystemServer(); |
| 4660 | } |
| 4661 | } |
| 4662 | |
| 4663 | /** |
Lorenzo Colitti | 500dbae | 2017-04-27 14:30:21 +0900 | [diff] [blame] | 4664 | * Requests that the system open the captive portal app on the specified network. |
| 4665 | * |
Remi NGUYEN VAN | 4cf96ab | 2021-03-16 18:06:06 +0900 | [diff] [blame] | 4666 | * <p>This is to be used on networks where a captive portal was detected, as per |
| 4667 | * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}. |
| 4668 | * |
Lorenzo Colitti | 500dbae | 2017-04-27 14:30:21 +0900 | [diff] [blame] | 4669 | * @param network The network to log into. |
| 4670 | * |
| 4671 | * @hide |
| 4672 | */ |
Remi NGUYEN VAN | 4cf96ab | 2021-03-16 18:06:06 +0900 | [diff] [blame] | 4673 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
| 4674 | @RequiresPermission(anyOf = { |
| 4675 | android.Manifest.permission.NETWORK_SETTINGS, |
| 4676 | android.Manifest.permission.NETWORK_STACK, |
| 4677 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK |
| 4678 | }) |
| 4679 | public void startCaptivePortalApp(@NonNull Network network) { |
Lorenzo Colitti | 500dbae | 2017-04-27 14:30:21 +0900 | [diff] [blame] | 4680 | try { |
| 4681 | mService.startCaptivePortalApp(network); |
| 4682 | } catch (RemoteException e) { |
| 4683 | throw e.rethrowFromSystemServer(); |
| 4684 | } |
| 4685 | } |
| 4686 | |
| 4687 | /** |
Remi NGUYEN VAN | 94ff95b | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 4688 | * Requests that the system open the captive portal app with the specified extras. |
| 4689 | * |
| 4690 | * <p>This endpoint is exclusively for use by the NetworkStack and is protected by the |
| 4691 | * corresponding permission. |
Remi NGUYEN VAN | eab0f54 | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 4692 | * @param network Network on which the captive portal was detected. |
Remi NGUYEN VAN | 94ff95b | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 4693 | * @param appExtras Extras to include in the app start intent. |
| 4694 | * @hide |
| 4695 | */ |
| 4696 | @SystemApi |
Remi NGUYEN VAN | 94ff95b | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 4697 | @RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK) |
paulhu | cbbc3db | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 4698 | public void startCaptivePortalApp(@NonNull Network network, @NonNull Bundle appExtras) { |
Remi NGUYEN VAN | 94ff95b | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 4699 | try { |
Remi NGUYEN VAN | eab0f54 | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 4700 | mService.startCaptivePortalAppInternal(network, appExtras); |
Remi NGUYEN VAN | 94ff95b | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 4701 | } catch (RemoteException e) { |
| 4702 | throw e.rethrowFromSystemServer(); |
| 4703 | } |
| 4704 | } |
| 4705 | |
| 4706 | /** |
Remi NGUYEN VAN | 27de63e | 2019-01-20 20:35:06 +0900 | [diff] [blame] | 4707 | * Determine whether the device is configured to avoid bad wifi. |
| 4708 | * @hide |
| 4709 | */ |
| 4710 | @SystemApi |
Remi NGUYEN VAN | 1fb7cab | 2019-03-22 11:14:13 +0900 | [diff] [blame] | 4711 | @RequiresPermission(anyOf = { |
| 4712 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 4713 | android.Manifest.permission.NETWORK_STACK}) |
| 4714 | public boolean shouldAvoidBadWifi() { |
Remi NGUYEN VAN | 27de63e | 2019-01-20 20:35:06 +0900 | [diff] [blame] | 4715 | try { |
Remi NGUYEN VAN | 1fb7cab | 2019-03-22 11:14:13 +0900 | [diff] [blame] | 4716 | return mService.shouldAvoidBadWifi(); |
Remi NGUYEN VAN | 27de63e | 2019-01-20 20:35:06 +0900 | [diff] [blame] | 4717 | } catch (RemoteException e) { |
| 4718 | throw e.rethrowFromSystemServer(); |
| 4719 | } |
| 4720 | } |
| 4721 | |
| 4722 | /** |
Lorenzo Colitti | 48a2a32 | 2017-01-24 18:08:41 +0900 | [diff] [blame] | 4723 | * It is acceptable to briefly use multipath data to provide seamless connectivity for |
| 4724 | * time-sensitive user-facing operations when the system default network is temporarily |
Lorenzo Colitti | 139a48c | 2017-04-28 00:56:30 +0900 | [diff] [blame] | 4725 | * unresponsive. The amount of data should be limited (less than one megabyte for every call to |
| 4726 | * this method), and the operation should be infrequent to ensure that data usage is limited. |
Lorenzo Colitti | 48a2a32 | 2017-01-24 18:08:41 +0900 | [diff] [blame] | 4727 | * |
| 4728 | * An example of such an operation might be a time-sensitive foreground activity, such as a |
| 4729 | * voice command, that the user is performing while walking out of range of a Wi-Fi network. |
| 4730 | */ |
| 4731 | public static final int MULTIPATH_PREFERENCE_HANDOVER = 1 << 0; |
| 4732 | |
| 4733 | /** |
| 4734 | * It is acceptable to use small amounts of multipath data on an ongoing basis to provide |
| 4735 | * a backup channel for traffic that is primarily going over another network. |
| 4736 | * |
| 4737 | * An example might be maintaining backup connections to peers or servers for the purpose of |
| 4738 | * fast fallback if the default network is temporarily unresponsive or disconnects. The traffic |
| 4739 | * on backup paths should be negligible compared to the traffic on the main path. |
| 4740 | */ |
| 4741 | public static final int MULTIPATH_PREFERENCE_RELIABILITY = 1 << 1; |
| 4742 | |
| 4743 | /** |
| 4744 | * It is acceptable to use metered data to improve network latency and performance. |
| 4745 | */ |
| 4746 | public static final int MULTIPATH_PREFERENCE_PERFORMANCE = 1 << 2; |
| 4747 | |
| 4748 | /** |
| 4749 | * Return value to use for unmetered networks. On such networks we currently set all the flags |
| 4750 | * to true. |
| 4751 | * @hide |
| 4752 | */ |
| 4753 | public static final int MULTIPATH_PREFERENCE_UNMETERED = |
| 4754 | MULTIPATH_PREFERENCE_HANDOVER | |
| 4755 | MULTIPATH_PREFERENCE_RELIABILITY | |
| 4756 | MULTIPATH_PREFERENCE_PERFORMANCE; |
| 4757 | |
| 4758 | /** @hide */ |
| 4759 | @Retention(RetentionPolicy.SOURCE) |
| 4760 | @IntDef(flag = true, value = { |
| 4761 | MULTIPATH_PREFERENCE_HANDOVER, |
| 4762 | MULTIPATH_PREFERENCE_RELIABILITY, |
| 4763 | MULTIPATH_PREFERENCE_PERFORMANCE, |
| 4764 | }) |
| 4765 | public @interface MultipathPreference { |
| 4766 | } |
| 4767 | |
| 4768 | /** |
| 4769 | * Provides a hint to the calling application on whether it is desirable to use the |
| 4770 | * multinetwork APIs (e.g., {@link Network#openConnection}, {@link Network#bindSocket}, etc.) |
| 4771 | * for multipath data transfer on this network when it is not the system default network. |
| 4772 | * Applications desiring to use multipath network protocols should call this method before |
| 4773 | * each such operation. |
Lorenzo Colitti | 48a2a32 | 2017-01-24 18:08:41 +0900 | [diff] [blame] | 4774 | * |
| 4775 | * @param network The network on which the application desires to use multipath data. |
| 4776 | * If {@code null}, this method will return the a preference that will generally |
| 4777 | * apply to metered networks. |
| 4778 | * @return a bitwise OR of zero or more of the {@code MULTIPATH_PREFERENCE_*} constants. |
| 4779 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 4780 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4781 | public @MultipathPreference int getMultipathPreference(@Nullable Network network) { |
Lorenzo Colitti | 48a2a32 | 2017-01-24 18:08:41 +0900 | [diff] [blame] | 4782 | try { |
| 4783 | return mService.getMultipathPreference(network); |
| 4784 | } catch (RemoteException e) { |
| 4785 | throw e.rethrowFromSystemServer(); |
| 4786 | } |
| 4787 | } |
| 4788 | |
| 4789 | /** |
Stuart Scott | 0f835ac | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 4790 | * Resets all connectivity manager settings back to factory defaults. |
| 4791 | * @hide |
| 4792 | */ |
Chiachang Wang | 4d51357 | 2021-03-18 09:44:34 +0800 | [diff] [blame] | 4793 | @SystemApi(client = MODULE_LIBRARIES) |
| 4794 | @RequiresPermission(anyOf = { |
| 4795 | android.Manifest.permission.NETWORK_SETTINGS, |
| 4796 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK}) |
Stuart Scott | 0f835ac | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 4797 | public void factoryReset() { |
Stuart Scott | 0f835ac | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 4798 | try { |
Stuart Scott | d546364 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4799 | mService.factoryReset(); |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 4800 | mTetheringManager.stopAllTethering(); |
Stuart Scott | 0f835ac | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 4801 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4802 | throw e.rethrowFromSystemServer(); |
Stuart Scott | 0f835ac | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 4803 | } |
| 4804 | } |
| 4805 | |
| 4806 | /** |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4807 | * Binds the current process to {@code network}. All Sockets created in the future |
| 4808 | * (and not explicitly bound via a bound SocketFactory from |
| 4809 | * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to |
| 4810 | * {@code network}. All host name resolutions will be limited to {@code network} as well. |
| 4811 | * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to |
| 4812 | * work and all host name resolutions will fail. This is by design so an application doesn't |
| 4813 | * accidentally use Sockets it thinks are still bound to a particular {@link Network}. |
| 4814 | * To clear binding pass {@code null} for {@code network}. Using individually bound |
| 4815 | * Sockets created by Network.getSocketFactory().createSocket() and |
| 4816 | * performing network-specific host name resolutions via |
| 4817 | * {@link Network#getAllByName Network.getAllByName} is preferred to calling |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 4818 | * {@code bindProcessToNetwork}. |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4819 | * |
| 4820 | * @param network The {@link Network} to bind the current process to, or {@code null} to clear |
| 4821 | * the current binding. |
| 4822 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
| 4823 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4824 | public boolean bindProcessToNetwork(@Nullable Network network) { |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 4825 | // Forcing callers to call through non-static function ensures ConnectivityManager |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 4826 | // instantiated. |
| 4827 | return setProcessDefaultNetwork(network); |
| 4828 | } |
| 4829 | |
| 4830 | /** |
| 4831 | * Binds the current process to {@code network}. All Sockets created in the future |
| 4832 | * (and not explicitly bound via a bound SocketFactory from |
| 4833 | * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to |
| 4834 | * {@code network}. All host name resolutions will be limited to {@code network} as well. |
| 4835 | * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to |
| 4836 | * work and all host name resolutions will fail. This is by design so an application doesn't |
| 4837 | * accidentally use Sockets it thinks are still bound to a particular {@link Network}. |
| 4838 | * To clear binding pass {@code null} for {@code network}. Using individually bound |
| 4839 | * Sockets created by Network.getSocketFactory().createSocket() and |
| 4840 | * performing network-specific host name resolutions via |
| 4841 | * {@link Network#getAllByName Network.getAllByName} is preferred to calling |
| 4842 | * {@code setProcessDefaultNetwork}. |
| 4843 | * |
| 4844 | * @param network The {@link Network} to bind the current process to, or {@code null} to clear |
| 4845 | * the current binding. |
| 4846 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
| 4847 | * @deprecated This function can throw {@link IllegalStateException}. Use |
| 4848 | * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork} |
| 4849 | * is a direct replacement. |
| 4850 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 4851 | @Deprecated |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4852 | public static boolean setProcessDefaultNetwork(@Nullable Network network) { |
Paul Jensen | 3e2917c | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 4853 | int netId = (network == null) ? NETID_UNSET : network.netId; |
Lorenzo Colitti | 3c766eb | 2019-01-31 13:08:24 +0900 | [diff] [blame] | 4854 | boolean isSameNetId = (netId == NetworkUtils.getBoundNetworkForProcess()); |
| 4855 | |
Lorenzo Colitti | 3a1cb9d | 2019-01-30 23:04:54 +0900 | [diff] [blame] | 4856 | if (netId != NETID_UNSET) { |
| 4857 | netId = network.getNetIdForResolv(); |
Paul Jensen | 3e2917c | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 4858 | } |
Lorenzo Colitti | 3c766eb | 2019-01-31 13:08:24 +0900 | [diff] [blame] | 4859 | |
| 4860 | if (!NetworkUtils.bindProcessToNetwork(netId)) { |
| 4861 | return false; |
| 4862 | } |
| 4863 | |
| 4864 | if (!isSameNetId) { |
Paul Jensen | c0618a6 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 4865 | // Set HTTP proxy system properties to match network. |
| 4866 | // TODO: Deprecate this static method and replace it with a non-static version. |
Lorenzo Colitti | 41b1fbc | 2015-04-22 11:52:48 +0900 | [diff] [blame] | 4867 | try { |
Remi NGUYEN VAN | b33335c | 2021-02-03 10:18:20 +0900 | [diff] [blame] | 4868 | Proxy.setHttpProxyConfiguration(getInstance().getDefaultProxy()); |
Lorenzo Colitti | 41b1fbc | 2015-04-22 11:52:48 +0900 | [diff] [blame] | 4869 | } catch (SecurityException e) { |
| 4870 | // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy. |
| 4871 | Log.e(TAG, "Can't set proxy properties", e); |
| 4872 | } |
Paul Jensen | 3e2917c | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 4873 | // Must flush DNS cache as new network may have different DNS resolutions. |
Remi NGUYEN VAN | e1b04f6 | 2021-03-18 23:27:19 +0900 | [diff] [blame] | 4874 | InetAddressCompat.clearDnsCache(); |
Paul Jensen | 3e2917c | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 4875 | // Must flush socket pool as idle sockets will be bound to previous network and may |
| 4876 | // cause subsequent fetches to be performed on old network. |
| 4877 | NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged(); |
Paul Jensen | 3e2917c | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 4878 | } |
Lorenzo Colitti | 3c766eb | 2019-01-31 13:08:24 +0900 | [diff] [blame] | 4879 | |
| 4880 | return true; |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4881 | } |
| 4882 | |
| 4883 | /** |
| 4884 | * Returns the {@link Network} currently bound to this process via |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 4885 | * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound. |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4886 | * |
| 4887 | * @return {@code Network} to which this process is bound, or {@code null}. |
| 4888 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4889 | @Nullable |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 4890 | public Network getBoundNetworkForProcess() { |
| 4891 | // Forcing callers to call thru non-static function ensures ConnectivityManager |
| 4892 | // instantiated. |
| 4893 | return getProcessDefaultNetwork(); |
| 4894 | } |
| 4895 | |
| 4896 | /** |
| 4897 | * Returns the {@link Network} currently bound to this process via |
| 4898 | * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound. |
| 4899 | * |
| 4900 | * @return {@code Network} to which this process is bound, or {@code null}. |
| 4901 | * @deprecated Using this function can lead to other functions throwing |
| 4902 | * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead. |
| 4903 | * {@code getBoundNetworkForProcess} is a direct replacement. |
| 4904 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 4905 | @Deprecated |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4906 | @Nullable |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4907 | public static Network getProcessDefaultNetwork() { |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 4908 | int netId = NetworkUtils.getBoundNetworkForProcess(); |
Paul Jensen | 65743a2 | 2014-07-11 08:17:29 -0400 | [diff] [blame] | 4909 | if (netId == NETID_UNSET) return null; |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4910 | return new Network(netId); |
| 4911 | } |
| 4912 | |
Lorenzo Colitti | 2780e4b | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 4913 | private void unsupportedStartingFrom(int version) { |
| 4914 | if (Process.myUid() == Process.SYSTEM_UID) { |
Lorenzo Colitti | 2386291 | 2018-09-28 11:31:55 +0900 | [diff] [blame] | 4915 | // The getApplicationInfo() call we make below is not supported in system context. Let |
| 4916 | // the call through here, and rely on the fact that ConnectivityService will refuse to |
| 4917 | // allow the system to use these APIs anyway. |
Lorenzo Colitti | 2780e4b | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 4918 | return; |
| 4919 | } |
| 4920 | |
| 4921 | if (mContext.getApplicationInfo().targetSdkVersion >= version) { |
| 4922 | throw new UnsupportedOperationException( |
| 4923 | "This method is not supported in target SDK version " + version + " and above"); |
| 4924 | } |
| 4925 | } |
| 4926 | |
| 4927 | // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature, |
| 4928 | // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException. |
Lifu Tang | 0a922fd | 2016-01-07 23:20:38 -0800 | [diff] [blame] | 4929 | // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and |
Lorenzo Colitti | 2780e4b | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 4930 | // remove these exemptions. Note that this check is not secure, and apps can still access these |
| 4931 | // functions by accessing ConnectivityService directly. However, it should be clear that doing |
| 4932 | // so is unsupported and may break in the future. http://b/22728205 |
| 4933 | private void checkLegacyRoutingApiAccess() { |
Dianne Hackborn | 18c1d83 | 2015-07-31 10:35:34 -0700 | [diff] [blame] | 4934 | unsupportedStartingFrom(VERSION_CODES.M); |
Lorenzo Colitti | 2780e4b | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 4935 | } |
| 4936 | |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4937 | /** |
| 4938 | * Binds host resolutions performed by this process to {@code network}. |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 4939 | * {@link #bindProcessToNetwork} takes precedence over this setting. |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4940 | * |
| 4941 | * @param network The {@link Network} to bind host resolutions from the current process to, or |
| 4942 | * {@code null} to clear the current binding. |
| 4943 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
| 4944 | * @hide |
| 4945 | * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}. |
| 4946 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 4947 | @Deprecated |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 4948 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4949 | public static boolean setProcessDefaultNetworkForHostResolution(Network network) { |
Paul Jensen | 65743a2 | 2014-07-11 08:17:29 -0400 | [diff] [blame] | 4950 | return NetworkUtils.bindProcessToNetworkForHostResolution( |
Erik Kline | 767b7f2 | 2018-04-27 22:48:33 +0900 | [diff] [blame] | 4951 | (network == null) ? NETID_UNSET : network.getNetIdForResolv()); |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4952 | } |
Felipe Leme | 3051135 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 4953 | |
| 4954 | /** |
| 4955 | * Device is not restricting metered network activity while application is running on |
| 4956 | * background. |
| 4957 | */ |
| 4958 | public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1; |
| 4959 | |
| 4960 | /** |
| 4961 | * Device is restricting metered network activity while application is running on background, |
| 4962 | * but application is allowed to bypass it. |
| 4963 | * <p> |
| 4964 | * In this state, application should take action to mitigate metered network access. |
| 4965 | * For example, a music streaming application should switch to a low-bandwidth bitrate. |
| 4966 | */ |
| 4967 | public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2; |
| 4968 | |
| 4969 | /** |
| 4970 | * Device is restricting metered network activity while application is running on background. |
Felipe Leme | d34c9af | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 4971 | * <p> |
Felipe Leme | 3051135 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 4972 | * In this state, application should not try to use the network while running on background, |
| 4973 | * because it would be denied. |
| 4974 | */ |
| 4975 | public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3; |
| 4976 | |
Felipe Leme | d34c9af | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 4977 | /** |
| 4978 | * A change in the background metered network activity restriction has occurred. |
| 4979 | * <p> |
| 4980 | * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction |
| 4981 | * applies to them. |
| 4982 | * <p> |
| 4983 | * This is only sent to registered receivers, not manifest receivers. |
| 4984 | */ |
| 4985 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 4986 | public static final String ACTION_RESTRICT_BACKGROUND_CHANGED = |
| 4987 | "android.net.conn.RESTRICT_BACKGROUND_CHANGED"; |
| 4988 | |
Felipe Leme | 7e4c185 | 2016-01-25 11:48:04 -0800 | [diff] [blame] | 4989 | /** @hide */ |
| 4990 | @Retention(RetentionPolicy.SOURCE) |
Felipe Leme | 3051135 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 4991 | @IntDef(flag = false, value = { |
| 4992 | RESTRICT_BACKGROUND_STATUS_DISABLED, |
| 4993 | RESTRICT_BACKGROUND_STATUS_WHITELISTED, |
| 4994 | RESTRICT_BACKGROUND_STATUS_ENABLED, |
| 4995 | }) |
Felipe Leme | 3051135 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 4996 | public @interface RestrictBackgroundStatus { |
| 4997 | } |
| 4998 | |
Felipe Leme | 3051135 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 4999 | /** |
| 5000 | * Determines if the calling application is subject to metered network restrictions while |
| 5001 | * running on background. |
Felipe Leme | 3edc616 | 2016-05-16 13:57:19 -0700 | [diff] [blame] | 5002 | * |
| 5003 | * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED}, |
| 5004 | * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED}, |
| 5005 | * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED} |
Felipe Leme | 3051135 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 5006 | */ |
| 5007 | public @RestrictBackgroundStatus int getRestrictBackgroundStatus() { |
| 5008 | try { |
Remi NGUYEN VAN | e2139a0 | 2021-03-18 14:23:12 +0900 | [diff] [blame] | 5009 | return mService.getRestrictBackgroundStatusByCaller(); |
Felipe Leme | 3051135 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 5010 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 5011 | throw e.rethrowFromSystemServer(); |
Felipe Leme | 3051135 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 5012 | } |
| 5013 | } |
Ricky Wai | 7097cc9 | 2018-01-23 04:09:45 +0000 | [diff] [blame] | 5014 | |
| 5015 | /** |
| 5016 | * The network watchlist is a list of domains and IP addresses that are associated with |
Ricky Wai | 04baf11 | 2018-03-20 14:20:54 +0000 | [diff] [blame] | 5017 | * potentially harmful apps. This method returns the SHA-256 of the watchlist config file |
| 5018 | * currently used by the system for validation purposes. |
Ricky Wai | 7097cc9 | 2018-01-23 04:09:45 +0000 | [diff] [blame] | 5019 | * |
| 5020 | * @return Hash of network watchlist config file. Null if config does not exist. |
| 5021 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 5022 | @Nullable |
Ricky Wai | 7097cc9 | 2018-01-23 04:09:45 +0000 | [diff] [blame] | 5023 | public byte[] getNetworkWatchlistConfigHash() { |
| 5024 | try { |
| 5025 | return mService.getNetworkWatchlistConfigHash(); |
| 5026 | } catch (RemoteException e) { |
| 5027 | Log.e(TAG, "Unable to get watchlist config hash"); |
| 5028 | throw e.rethrowFromSystemServer(); |
| 5029 | } |
| 5030 | } |
Jeff Vander Stoep | 39a51e0 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 5031 | |
| 5032 | /** |
| 5033 | * Returns the {@code uid} of the owner of a network connection. |
| 5034 | * |
Benedict Wong | 0bd4bba | 2020-01-20 22:14:59 -0800 | [diff] [blame] | 5035 | * @param protocol The protocol of the connection. Only {@code IPPROTO_TCP} and {@code |
| 5036 | * IPPROTO_UDP} currently supported. |
Jeff Vander Stoep | 39a51e0 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 5037 | * @param local The local {@link InetSocketAddress} of a connection. |
| 5038 | * @param remote The remote {@link InetSocketAddress} of a connection. |
Jeff Vander Stoep | 39a51e0 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 5039 | * @return {@code uid} if the connection is found and the app has permission to observe it |
Benedict Wong | 0bd4bba | 2020-01-20 22:14:59 -0800 | [diff] [blame] | 5040 | * (e.g., if it is associated with the calling VPN app's VpnService tunnel) or {@link |
| 5041 | * android.os.Process#INVALID_UID} if the connection is not found. |
| 5042 | * @throws {@link SecurityException} if the caller is not the active VpnService for the current |
| 5043 | * user. |
| 5044 | * @throws {@link IllegalArgumentException} if an unsupported protocol is requested. |
Jeff Vander Stoep | 39a51e0 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 5045 | */ |
Benedict Wong | 0bd4bba | 2020-01-20 22:14:59 -0800 | [diff] [blame] | 5046 | public int getConnectionOwnerUid( |
| 5047 | int protocol, @NonNull InetSocketAddress local, @NonNull InetSocketAddress remote) { |
Jeff Vander Stoep | 39a51e0 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 5048 | ConnectionInfo connectionInfo = new ConnectionInfo(protocol, local, remote); |
| 5049 | try { |
| 5050 | return mService.getConnectionOwnerUid(connectionInfo); |
| 5051 | } catch (RemoteException e) { |
| 5052 | throw e.rethrowFromSystemServer(); |
| 5053 | } |
| 5054 | } |
Soi, Yoshinari | b8102ef | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 5055 | |
| 5056 | private void printStackTrace() { |
| 5057 | if (DEBUG) { |
| 5058 | final StackTraceElement[] callStack = Thread.currentThread().getStackTrace(); |
| 5059 | final StringBuffer sb = new StringBuffer(); |
| 5060 | for (int i = 3; i < callStack.length; i++) { |
| 5061 | final String stackTrace = callStack[i].toString(); |
| 5062 | if (stackTrace == null || stackTrace.contains("android.os")) { |
| 5063 | break; |
| 5064 | } |
| 5065 | sb.append(" [").append(stackTrace).append("]"); |
| 5066 | } |
| 5067 | Log.d(TAG, "StackLog:" + sb.toString()); |
| 5068 | } |
| 5069 | } |
Cody Kesting | f53a075 | 2020-04-15 12:33:28 -0700 | [diff] [blame] | 5070 | |
Remi NGUYEN VAN | 5f40642 | 2021-01-15 23:02:47 +0900 | [diff] [blame] | 5071 | /** @hide */ |
| 5072 | public TestNetworkManager startOrGetTestNetworkManager() { |
| 5073 | final IBinder tnBinder; |
| 5074 | try { |
| 5075 | tnBinder = mService.startOrGetTestNetworkService(); |
| 5076 | } catch (RemoteException e) { |
| 5077 | throw e.rethrowFromSystemServer(); |
| 5078 | } |
| 5079 | |
| 5080 | return new TestNetworkManager(ITestNetworkManager.Stub.asInterface(tnBinder)); |
| 5081 | } |
| 5082 | |
Remi NGUYEN VAN | 5f40642 | 2021-01-15 23:02:47 +0900 | [diff] [blame] | 5083 | /** @hide */ |
| 5084 | public ConnectivityDiagnosticsManager createDiagnosticsManager() { |
| 5085 | return new ConnectivityDiagnosticsManager(mContext, mService); |
| 5086 | } |
| 5087 | |
Cody Kesting | f53a075 | 2020-04-15 12:33:28 -0700 | [diff] [blame] | 5088 | /** |
| 5089 | * Simulates a Data Stall for the specified Network. |
| 5090 | * |
Remi NGUYEN VAN | 761c7ad | 2021-01-12 18:40:04 +0900 | [diff] [blame] | 5091 | * <p>This method should only be used for tests. |
| 5092 | * |
Cody Kesting | f53a075 | 2020-04-15 12:33:28 -0700 | [diff] [blame] | 5093 | * <p>The caller must be the owner of the specified Network. |
| 5094 | * |
| 5095 | * @param detectionMethod The detection method used to identify the Data Stall. |
| 5096 | * @param timestampMillis The timestamp at which the stall 'occurred', in milliseconds. |
| 5097 | * @param network The Network for which a Data Stall is being simluated. |
| 5098 | * @param extras The PersistableBundle of extras included in the Data Stall notification. |
| 5099 | * @throws SecurityException if the caller is not the owner of the given network. |
| 5100 | * @hide |
| 5101 | */ |
Remi NGUYEN VAN | 761c7ad | 2021-01-12 18:40:04 +0900 | [diff] [blame] | 5102 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
Cody Kesting | f53a075 | 2020-04-15 12:33:28 -0700 | [diff] [blame] | 5103 | @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_TEST_NETWORKS, |
| 5104 | android.Manifest.permission.NETWORK_STACK}) |
| 5105 | public void simulateDataStall(int detectionMethod, long timestampMillis, |
| 5106 | @NonNull Network network, @NonNull PersistableBundle extras) { |
| 5107 | try { |
| 5108 | mService.simulateDataStall(detectionMethod, timestampMillis, network, extras); |
| 5109 | } catch (RemoteException e) { |
| 5110 | e.rethrowFromSystemServer(); |
| 5111 | } |
| 5112 | } |
James Mattis | 356a879 | 2020-10-28 21:48:54 -0700 | [diff] [blame] | 5113 | |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 5114 | @NonNull |
| 5115 | private final List<QosCallbackConnection> mQosCallbackConnections = new ArrayList<>(); |
| 5116 | |
| 5117 | /** |
| 5118 | * Registers a {@link QosSocketInfo} with an associated {@link QosCallback}. The callback will |
| 5119 | * receive available QoS events related to the {@link Network} and local ip + port |
| 5120 | * specified within socketInfo. |
| 5121 | * <p/> |
| 5122 | * The same {@link QosCallback} must be unregistered before being registered a second time, |
| 5123 | * otherwise {@link QosCallbackRegistrationException} is thrown. |
| 5124 | * <p/> |
| 5125 | * This API does not, in itself, require any permission if called with a network that is not |
| 5126 | * restricted. However, the underlying implementation currently only supports the IMS network, |
| 5127 | * which is always restricted. That means non-preinstalled callers can't possibly find this API |
| 5128 | * useful, because they'd never be called back on networks that they would have access to. |
| 5129 | * |
| 5130 | * @throws SecurityException if {@link QosSocketInfo#getNetwork()} is restricted and the app is |
| 5131 | * missing CONNECTIVITY_USE_RESTRICTED_NETWORKS permission. |
| 5132 | * @throws QosCallback.QosCallbackRegistrationException if qosCallback is already registered. |
| 5133 | * @throws RuntimeException if the app already has too many callbacks registered. |
| 5134 | * |
| 5135 | * Exceptions after the time of registration is passed through |
| 5136 | * {@link QosCallback#onError(QosCallbackException)}. see: {@link QosCallbackException}. |
| 5137 | * |
| 5138 | * @param socketInfo the socket information used to match QoS events |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 5139 | * @param executor The executor on which the callback will be invoked. The provided |
| 5140 | * {@link Executor} must run callback sequentially, otherwise the order of |
Daniel Bright | 7ee5f52 | 2021-03-10 11:51:50 -0800 | [diff] [blame] | 5141 | * callbacks cannot be guaranteed.onQosCallbackRegistered |
| 5142 | * @param callback receives qos events that satisfy socketInfo |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 5143 | * |
| 5144 | * @hide |
| 5145 | */ |
| 5146 | @SystemApi |
| 5147 | public void registerQosCallback(@NonNull final QosSocketInfo socketInfo, |
Daniel Bright | 7ee5f52 | 2021-03-10 11:51:50 -0800 | [diff] [blame] | 5148 | @CallbackExecutor @NonNull final Executor executor, |
| 5149 | @NonNull final QosCallback callback) { |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 5150 | Objects.requireNonNull(socketInfo, "socketInfo must be non-null"); |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 5151 | Objects.requireNonNull(executor, "executor must be non-null"); |
Daniel Bright | 7ee5f52 | 2021-03-10 11:51:50 -0800 | [diff] [blame] | 5152 | Objects.requireNonNull(callback, "callback must be non-null"); |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 5153 | |
| 5154 | try { |
| 5155 | synchronized (mQosCallbackConnections) { |
| 5156 | if (getQosCallbackConnection(callback) == null) { |
| 5157 | final QosCallbackConnection connection = |
| 5158 | new QosCallbackConnection(this, callback, executor); |
| 5159 | mQosCallbackConnections.add(connection); |
| 5160 | mService.registerQosSocketCallback(socketInfo, connection); |
| 5161 | } else { |
| 5162 | Log.e(TAG, "registerQosCallback: Callback already registered"); |
| 5163 | throw new QosCallbackRegistrationException(); |
| 5164 | } |
| 5165 | } |
| 5166 | } catch (final RemoteException e) { |
| 5167 | Log.e(TAG, "registerQosCallback: Error while registering ", e); |
| 5168 | |
| 5169 | // The same unregister method method is called for consistency even though nothing |
| 5170 | // will be sent to the ConnectivityService since the callback was never successfully |
| 5171 | // registered. |
| 5172 | unregisterQosCallback(callback); |
| 5173 | e.rethrowFromSystemServer(); |
| 5174 | } catch (final ServiceSpecificException e) { |
| 5175 | Log.e(TAG, "registerQosCallback: Error while registering ", e); |
| 5176 | unregisterQosCallback(callback); |
| 5177 | throw convertServiceException(e); |
| 5178 | } |
| 5179 | } |
| 5180 | |
| 5181 | /** |
| 5182 | * Unregisters the given {@link QosCallback}. The {@link QosCallback} will no longer receive |
| 5183 | * events once unregistered and can be registered a second time. |
| 5184 | * <p/> |
| 5185 | * If the {@link QosCallback} does not have an active registration, it is a no-op. |
| 5186 | * |
| 5187 | * @param callback the callback being unregistered |
| 5188 | * |
| 5189 | * @hide |
| 5190 | */ |
| 5191 | @SystemApi |
| 5192 | public void unregisterQosCallback(@NonNull final QosCallback callback) { |
| 5193 | Objects.requireNonNull(callback, "The callback must be non-null"); |
| 5194 | try { |
| 5195 | synchronized (mQosCallbackConnections) { |
| 5196 | final QosCallbackConnection connection = getQosCallbackConnection(callback); |
| 5197 | if (connection != null) { |
| 5198 | connection.stopReceivingMessages(); |
| 5199 | mService.unregisterQosCallback(connection); |
| 5200 | mQosCallbackConnections.remove(connection); |
| 5201 | } else { |
| 5202 | Log.d(TAG, "unregisterQosCallback: Callback not registered"); |
| 5203 | } |
| 5204 | } |
| 5205 | } catch (final RemoteException e) { |
| 5206 | Log.e(TAG, "unregisterQosCallback: Error while unregistering ", e); |
| 5207 | e.rethrowFromSystemServer(); |
| 5208 | } |
| 5209 | } |
| 5210 | |
| 5211 | /** |
| 5212 | * Gets the connection related to the callback. |
| 5213 | * |
| 5214 | * @param callback the callback to look up |
| 5215 | * @return the related connection |
| 5216 | */ |
| 5217 | @Nullable |
| 5218 | private QosCallbackConnection getQosCallbackConnection(final QosCallback callback) { |
| 5219 | for (final QosCallbackConnection connection : mQosCallbackConnections) { |
| 5220 | // Checking by reference here is intentional |
| 5221 | if (connection.getCallback() == callback) { |
| 5222 | return connection; |
| 5223 | } |
| 5224 | } |
| 5225 | return null; |
| 5226 | } |
Junyu Lai | a62493f | 2021-01-19 11:10:56 +0000 | [diff] [blame] | 5227 | |
| 5228 | /** |
Roshan Pius | 951c003 | 2020-12-22 15:10:42 -0800 | [diff] [blame] | 5229 | * Request a network to satisfy a set of {@link NetworkCapabilities}, but |
Junyu Lai | a62493f | 2021-01-19 11:10:56 +0000 | [diff] [blame] | 5230 | * does not cause any networks to retain the NET_CAPABILITY_FOREGROUND capability. This can |
| 5231 | * be used to request that the system provide a network without causing the network to be |
| 5232 | * in the foreground. |
| 5233 | * |
| 5234 | * <p>This method will attempt to find the best network that matches the passed |
| 5235 | * {@link NetworkRequest}, and to bring up one that does if none currently satisfies the |
| 5236 | * criteria. The platform will evaluate which network is the best at its own discretion. |
| 5237 | * Throughput, latency, cost per byte, policy, user preference and other considerations |
| 5238 | * may be factored in the decision of what is considered the best network. |
| 5239 | * |
| 5240 | * <p>As long as this request is outstanding, the platform will try to maintain the best network |
| 5241 | * matching this request, while always attempting to match the request to a better network if |
| 5242 | * possible. If a better match is found, the platform will switch this request to the now-best |
| 5243 | * network and inform the app of the newly best network by invoking |
| 5244 | * {@link NetworkCallback#onAvailable(Network)} on the provided callback. Note that the platform |
| 5245 | * will not try to maintain any other network than the best one currently matching the request: |
| 5246 | * a network not matching any network request may be disconnected at any time. |
| 5247 | * |
| 5248 | * <p>For example, an application could use this method to obtain a connected cellular network |
| 5249 | * even if the device currently has a data connection over Ethernet. This may cause the cellular |
| 5250 | * radio to consume additional power. Or, an application could inform the system that it wants |
| 5251 | * a network supporting sending MMSes and have the system let it know about the currently best |
| 5252 | * MMS-supporting network through the provided {@link NetworkCallback}. |
| 5253 | * |
| 5254 | * <p>The status of the request can be followed by listening to the various callbacks described |
| 5255 | * in {@link NetworkCallback}. The {@link Network} object passed to the callback methods can be |
| 5256 | * used to direct traffic to the network (although accessing some networks may be subject to |
| 5257 | * holding specific permissions). Callers will learn about the specific characteristics of the |
| 5258 | * network through |
| 5259 | * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)} and |
| 5260 | * {@link NetworkCallback#onLinkPropertiesChanged(Network, LinkProperties)}. The methods of the |
| 5261 | * provided {@link NetworkCallback} will only be invoked due to changes in the best network |
| 5262 | * matching the request at any given time; therefore when a better network matching the request |
| 5263 | * becomes available, the {@link NetworkCallback#onAvailable(Network)} method is called |
| 5264 | * with the new network after which no further updates are given about the previously-best |
| 5265 | * network, unless it becomes the best again at some later time. All callbacks are invoked |
| 5266 | * in order on the same thread, which by default is a thread created by the framework running |
| 5267 | * in the app. |
| 5268 | * |
| 5269 | * <p>This{@link NetworkRequest} will live until released via |
| 5270 | * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits, at |
| 5271 | * which point the system may let go of the network at any time. |
| 5272 | * |
| 5273 | * <p>It is presently unsupported to request a network with mutable |
| 5274 | * {@link NetworkCapabilities} such as |
| 5275 | * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or |
| 5276 | * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL} |
| 5277 | * as these {@code NetworkCapabilities} represent states that a particular |
| 5278 | * network may never attain, and whether a network will attain these states |
| 5279 | * is unknown prior to bringing up the network so the framework does not |
| 5280 | * know how to go about satisfying a request with these capabilities. |
| 5281 | * |
| 5282 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 5283 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 5284 | * all variants of this method, of {@link #registerNetworkCallback} as well as |
| 5285 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 5286 | * Requesting a network with this method will count toward this limit. If this limit is |
| 5287 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 5288 | * make sure to unregister the callbacks with |
| 5289 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 5290 | * |
| 5291 | * @param request {@link NetworkRequest} describing this request. |
| 5292 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
| 5293 | * If null, the callback is invoked on the default internal Handler. |
| 5294 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 5295 | * the callback must not be shared - it uniquely specifies this request. |
| 5296 | * @throws IllegalArgumentException if {@code request} contains invalid network capabilities. |
| 5297 | * @throws SecurityException if missing the appropriate permissions. |
| 5298 | * @throws RuntimeException if the app already has too many callbacks registered. |
| 5299 | * |
| 5300 | * @hide |
| 5301 | */ |
| 5302 | @SystemApi(client = MODULE_LIBRARIES) |
| 5303 | @SuppressLint("ExecutorRegistration") |
| 5304 | @RequiresPermission(anyOf = { |
| 5305 | android.Manifest.permission.NETWORK_SETTINGS, |
| 5306 | android.Manifest.permission.NETWORK_STACK, |
| 5307 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK |
| 5308 | }) |
| 5309 | public void requestBackgroundNetwork(@NonNull NetworkRequest request, |
junyulai | 962bdb8 | 2021-03-09 20:49:48 +0800 | [diff] [blame] | 5310 | @NonNull Handler handler, @NonNull NetworkCallback networkCallback) { |
Junyu Lai | a62493f | 2021-01-19 11:10:56 +0000 | [diff] [blame] | 5311 | final NetworkCapabilities nc = request.networkCapabilities; |
| 5312 | sendRequestForNetwork(nc, networkCallback, 0, BACKGROUND_REQUEST, |
junyulai | 962bdb8 | 2021-03-09 20:49:48 +0800 | [diff] [blame] | 5313 | TYPE_NONE, new CallbackHandler(handler)); |
Junyu Lai | a62493f | 2021-01-19 11:10:56 +0000 | [diff] [blame] | 5314 | } |
James Mattis | 45d8184 | 2021-01-10 14:24:24 -0800 | [diff] [blame] | 5315 | |
| 5316 | /** |
James Mattis | 45d8184 | 2021-01-10 14:24:24 -0800 | [diff] [blame] | 5317 | * Used by automotive devices to set the network preferences used to direct traffic at an |
| 5318 | * application level as per the given OemNetworkPreferences. An example use-case would be an |
| 5319 | * automotive OEM wanting to provide connectivity for applications critical to the usage of a |
| 5320 | * vehicle via a particular network. |
| 5321 | * |
| 5322 | * Calling this will overwrite the existing preference. |
| 5323 | * |
| 5324 | * @param preference {@link OemNetworkPreferences} The application network preference to be set. |
| 5325 | * @param executor the executor on which listener will be invoked. |
| 5326 | * @param listener {@link OnSetOemNetworkPreferenceListener} optional listener used to |
| 5327 | * communicate completion of setOemNetworkPreference(). This will only be |
| 5328 | * called once upon successful completion of setOemNetworkPreference(). |
| 5329 | * @throws IllegalArgumentException if {@code preference} contains invalid preference values. |
| 5330 | * @throws SecurityException if missing the appropriate permissions. |
| 5331 | * @throws UnsupportedOperationException if called on a non-automotive device. |
James Mattis | da32cfe | 2021-01-26 16:23:52 -0800 | [diff] [blame] | 5332 | * @hide |
James Mattis | 45d8184 | 2021-01-10 14:24:24 -0800 | [diff] [blame] | 5333 | */ |
James Mattis | da32cfe | 2021-01-26 16:23:52 -0800 | [diff] [blame] | 5334 | @SystemApi |
James Mattis | 8378aec | 2021-01-26 14:05:36 -0800 | [diff] [blame] | 5335 | @RequiresPermission(android.Manifest.permission.CONTROL_OEM_PAID_NETWORK_PREFERENCE) |
James Mattis | da32cfe | 2021-01-26 16:23:52 -0800 | [diff] [blame] | 5336 | public void setOemNetworkPreference(@NonNull final OemNetworkPreferences preference, |
James Mattis | 45d8184 | 2021-01-10 14:24:24 -0800 | [diff] [blame] | 5337 | @Nullable @CallbackExecutor final Executor executor, |
Chalard Jean | 6010c00 | 2021-03-03 16:37:13 +0900 | [diff] [blame] | 5338 | @Nullable final Runnable listener) { |
James Mattis | 45d8184 | 2021-01-10 14:24:24 -0800 | [diff] [blame] | 5339 | Objects.requireNonNull(preference, "OemNetworkPreferences must be non-null"); |
| 5340 | if (null != listener) { |
| 5341 | Objects.requireNonNull(executor, "Executor must be non-null"); |
| 5342 | } |
Chalard Jean | 6010c00 | 2021-03-03 16:37:13 +0900 | [diff] [blame] | 5343 | final IOnCompleteListener listenerInternal = listener == null ? null : |
| 5344 | new IOnCompleteListener.Stub() { |
James Mattis | 45d8184 | 2021-01-10 14:24:24 -0800 | [diff] [blame] | 5345 | @Override |
| 5346 | public void onComplete() { |
Chalard Jean | 6010c00 | 2021-03-03 16:37:13 +0900 | [diff] [blame] | 5347 | executor.execute(listener::run); |
James Mattis | 45d8184 | 2021-01-10 14:24:24 -0800 | [diff] [blame] | 5348 | } |
| 5349 | }; |
| 5350 | |
| 5351 | try { |
| 5352 | mService.setOemNetworkPreference(preference, listenerInternal); |
| 5353 | } catch (RemoteException e) { |
| 5354 | Log.e(TAG, "setOemNetworkPreference() failed for preference: " + preference.toString()); |
| 5355 | throw e.rethrowFromSystemServer(); |
| 5356 | } |
| 5357 | } |
lucaslin | 30e70a8 | 2021-03-12 00:46:33 +0800 | [diff] [blame] | 5358 | |
Chalard Jean | fa45a68 | 2021-02-25 17:23:40 +0900 | [diff] [blame] | 5359 | /** |
| 5360 | * Request that a user profile is put by default on a network matching a given preference. |
| 5361 | * |
| 5362 | * See the documentation for the individual preferences for a description of the supported |
| 5363 | * behaviors. |
| 5364 | * |
| 5365 | * @param profile the profile concerned. |
| 5366 | * @param preference the preference for this profile. |
| 5367 | * @param executor an executor to execute the listener on. Optional if listener is null. |
| 5368 | * @param listener an optional listener to listen for completion of the operation. |
| 5369 | * @throws IllegalArgumentException if {@code profile} is not a valid user profile. |
| 5370 | * @throws SecurityException if missing the appropriate permissions. |
| 5371 | * @hide |
| 5372 | */ |
Chalard Jean | 6010c00 | 2021-03-03 16:37:13 +0900 | [diff] [blame] | 5373 | // This function is for establishing per-profile default networking and can only be called by |
| 5374 | // the device policy manager, running as the system server. It would make no sense to call it |
| 5375 | // on a context for a user because it does not establish a setting on behalf of a user, rather |
| 5376 | // it establishes a setting for a user on behalf of the DPM. |
| 5377 | @SuppressLint({"UserHandle"}) |
| 5378 | @SystemApi(client = MODULE_LIBRARIES) |
Chalard Jean | fa45a68 | 2021-02-25 17:23:40 +0900 | [diff] [blame] | 5379 | @RequiresPermission(android.Manifest.permission.NETWORK_STACK) |
| 5380 | public void setProfileNetworkPreference(@NonNull final UserHandle profile, |
| 5381 | @ProfileNetworkPreference final int preference, |
| 5382 | @Nullable @CallbackExecutor final Executor executor, |
| 5383 | @Nullable final Runnable listener) { |
| 5384 | if (null != listener) { |
| 5385 | Objects.requireNonNull(executor, "Pass a non-null executor, or a null listener"); |
| 5386 | } |
| 5387 | final IOnCompleteListener proxy; |
| 5388 | if (null == listener) { |
| 5389 | proxy = null; |
| 5390 | } else { |
| 5391 | proxy = new IOnCompleteListener.Stub() { |
| 5392 | @Override |
| 5393 | public void onComplete() { |
| 5394 | executor.execute(listener::run); |
| 5395 | } |
| 5396 | }; |
| 5397 | } |
| 5398 | try { |
| 5399 | mService.setProfileNetworkPreference(profile, preference, proxy); |
| 5400 | } catch (RemoteException e) { |
| 5401 | throw e.rethrowFromSystemServer(); |
| 5402 | } |
| 5403 | } |
| 5404 | |
lucaslin | 30e70a8 | 2021-03-12 00:46:33 +0800 | [diff] [blame] | 5405 | // The first network ID of IPSec tunnel interface. |
lucaslin | 0cdcea1 | 2021-03-15 17:24:12 +0800 | [diff] [blame] | 5406 | private static final int TUN_INTF_NETID_START = 0xFC00; // 0xFC00 = 64512 |
lucaslin | 30e70a8 | 2021-03-12 00:46:33 +0800 | [diff] [blame] | 5407 | // The network ID range of IPSec tunnel interface. |
lucaslin | 0cdcea1 | 2021-03-15 17:24:12 +0800 | [diff] [blame] | 5408 | private static final int TUN_INTF_NETID_RANGE = 0x0400; // 0x0400 = 1024 |
lucaslin | 30e70a8 | 2021-03-12 00:46:33 +0800 | [diff] [blame] | 5409 | |
| 5410 | /** |
| 5411 | * Get the network ID range reserved for IPSec tunnel interfaces. |
| 5412 | * |
| 5413 | * @return A Range which indicates the network ID range of IPSec tunnel interface. |
| 5414 | * @hide |
| 5415 | */ |
| 5416 | @SystemApi(client = MODULE_LIBRARIES) |
| 5417 | @NonNull |
| 5418 | public static Range<Integer> getIpSecNetIdRange() { |
| 5419 | return new Range(TUN_INTF_NETID_START, TUN_INTF_NETID_START + TUN_INTF_NETID_RANGE - 1); |
| 5420 | } |
lucaslin | 705c333 | 2021-03-12 17:56:09 +0800 | [diff] [blame] | 5421 | |
| 5422 | /** |
| 5423 | * Get private DNS mode from settings. |
| 5424 | * |
lucaslin | 7abe7e0 | 2021-03-17 14:53:35 +0800 | [diff] [blame] | 5425 | * @param context The Context to query the private DNS mode from settings. |
lucaslin | 705c333 | 2021-03-12 17:56:09 +0800 | [diff] [blame] | 5426 | * @return A string of private DNS mode as one of the PRIVATE_DNS_MODE_* constants. |
| 5427 | * |
| 5428 | * @hide |
| 5429 | */ |
| 5430 | @SystemApi(client = MODULE_LIBRARIES) |
| 5431 | @NonNull |
| 5432 | @PrivateDnsMode |
lucaslin | 20f0488 | 2021-03-16 17:11:14 +0800 | [diff] [blame] | 5433 | public static String getPrivateDnsMode(@NonNull Context context) { |
| 5434 | final ContentResolver cr = context.getContentResolver(); |
lucaslin | 705c333 | 2021-03-12 17:56:09 +0800 | [diff] [blame] | 5435 | String mode = Settings.Global.getString(cr, PRIVATE_DNS_MODE); |
| 5436 | if (TextUtils.isEmpty(mode)) mode = Settings.Global.getString(cr, PRIVATE_DNS_DEFAULT_MODE); |
| 5437 | // If both PRIVATE_DNS_MODE and PRIVATE_DNS_DEFAULT_MODE are not set, choose |
| 5438 | // PRIVATE_DNS_MODE_OPPORTUNISTIC as default mode. |
| 5439 | if (TextUtils.isEmpty(mode)) mode = PRIVATE_DNS_MODE_OPPORTUNISTIC; |
| 5440 | return mode; |
| 5441 | } |
paulhu | 89a762a | 2021-03-25 12:36:56 +0800 | [diff] [blame] | 5442 | |
| 5443 | /** |
| 5444 | * Set private DNS mode to settings. |
| 5445 | * |
| 5446 | * @param context The {@link Context} to set the private DNS mode. |
| 5447 | * @param mode The private dns mode. This should be one of the PRIVATE_DNS_MODE_* constants. |
| 5448 | * |
| 5449 | * @hide |
| 5450 | */ |
| 5451 | @SystemApi(client = MODULE_LIBRARIES) |
| 5452 | public static void setPrivateDnsMode(@NonNull Context context, |
| 5453 | @NonNull @PrivateDnsMode String mode) { |
| 5454 | if (!(mode == PRIVATE_DNS_MODE_OFF |
| 5455 | || mode == PRIVATE_DNS_MODE_OPPORTUNISTIC |
| 5456 | || mode == PRIVATE_DNS_MODE_PROVIDER_HOSTNAME)) { |
| 5457 | throw new IllegalArgumentException("Invalid private dns mode"); |
| 5458 | } |
| 5459 | Settings.Global.putString(context.getContentResolver(), PRIVATE_DNS_MODE, mode); |
| 5460 | } |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5461 | } |