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; |
junyulai | d05a192 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 19 | import static android.net.IpSecManager.INVALID_RESOURCE_ID; |
Junyu Lai | a62493f | 2021-01-19 11:10:56 +0000 | [diff] [blame] | 20 | import static android.net.NetworkRequest.Type.BACKGROUND_REQUEST; |
junyulai | ad01079 | 2021-01-11 16:53:38 +0800 | [diff] [blame] | 21 | import static android.net.NetworkRequest.Type.LISTEN; |
| 22 | import static android.net.NetworkRequest.Type.REQUEST; |
junyulai | 7514e31 | 2021-03-05 15:51:17 +0800 | [diff] [blame^] | 23 | import static android.net.NetworkRequest.Type.TRACK_BEST; |
junyulai | ad01079 | 2021-01-11 16:53:38 +0800 | [diff] [blame] | 24 | import static android.net.NetworkRequest.Type.TRACK_DEFAULT; |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 25 | import static android.net.NetworkRequest.Type.TRACK_SYSTEM_DEFAULT; |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 26 | import static android.net.QosCallback.QosCallbackRegistrationException; |
junyulai | d05a192 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 27 | |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 28 | import android.annotation.CallbackExecutor; |
Felipe Leme | 3051135 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 29 | import android.annotation.IntDef; |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 30 | import android.annotation.NonNull; |
Robin Lee | e5d5ed5 | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 31 | import android.annotation.Nullable; |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 32 | import android.annotation.RequiresPermission; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | import android.annotation.SdkConstant; |
| 34 | import android.annotation.SdkConstant.SdkConstantType; |
Junyu Lai | a62493f | 2021-01-19 11:10:56 +0000 | [diff] [blame] | 35 | import android.annotation.SuppressLint; |
Udam Saini | cd64546 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 36 | import android.annotation.SystemApi; |
Jeff Sharkey | 9b39e9a | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 37 | import android.annotation.SystemService; |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 38 | import android.app.PendingIntent; |
Artur Satayev | 9c2add6 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 39 | import android.compat.annotation.UnsupportedAppUsage; |
Jeff Sharkey | 2d9e5a5 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 40 | import android.content.Context; |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 41 | import android.content.Intent; |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 42 | import android.net.IpSecManager.UdpEncapsulationSocket; |
| 43 | import android.net.SocketKeepalive.Callback; |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 44 | import android.net.TetheringManager.StartTetheringCallback; |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 45 | import android.net.TetheringManager.TetheringEventCallback; |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 46 | import android.net.TetheringManager.TetheringRequest; |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 47 | import android.os.Binder; |
Mathew Inwood | bdfc1fc | 2018-12-20 15:30:45 +0000 | [diff] [blame] | 48 | import android.os.Build; |
Jeff Sharkey | 39c01eb | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 49 | import android.os.Build.VERSION_CODES; |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 50 | import android.os.Bundle; |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 51 | import android.os.Handler; |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 52 | import android.os.IBinder; |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 53 | import android.os.Looper; |
| 54 | import android.os.Message; |
Robert Greenwalt | 15a4153 | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 55 | import android.os.Messenger; |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 56 | import android.os.ParcelFileDescriptor; |
Cody Kesting | f53a075 | 2020-04-15 12:33:28 -0700 | [diff] [blame] | 57 | import android.os.PersistableBundle; |
Lorenzo Colitti | 2780e4b | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 58 | import android.os.Process; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 59 | import android.os.RemoteException; |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 60 | import android.os.ResultReceiver; |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 61 | import android.os.ServiceManager; |
Hugo Benichi | a590ab8 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 62 | import android.os.ServiceSpecificException; |
Jeff Sharkey | 971cd16 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 63 | import android.provider.Settings; |
Wink Saville | 689f704 | 2014-12-05 11:10:30 -0800 | [diff] [blame] | 64 | import android.telephony.SubscriptionManager; |
Meng Wang | ed6f441 | 2019-11-18 17:10:00 -0800 | [diff] [blame] | 65 | import android.telephony.TelephonyManager; |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 66 | import android.util.ArrayMap; |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 67 | import android.util.Log; |
Lorenzo Colitti | 3f54f10 | 2020-12-12 00:51:11 +0900 | [diff] [blame] | 68 | import android.util.Range; |
Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 69 | import android.util.SparseIntArray; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | |
Remi NGUYEN VAN | 77b0c1c | 2020-12-23 12:45:08 +0900 | [diff] [blame] | 71 | import com.android.connectivity.aidl.INetworkAgent; |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 72 | import com.android.internal.annotations.GuardedBy; |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 73 | import com.android.internal.util.Preconditions; |
| 74 | import com.android.internal.util.Protocol; |
Robert Greenwalt | 0c150c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 75 | |
Paul Jensen | 3e2917c | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 76 | import libcore.net.event.NetworkEventDispatcher; |
| 77 | |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 78 | import java.io.IOException; |
| 79 | import java.io.UncheckedIOException; |
Felipe Leme | 3051135 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 80 | import java.lang.annotation.Retention; |
| 81 | import java.lang.annotation.RetentionPolicy; |
Lorenzo Colitti | 3f54f10 | 2020-12-12 00:51:11 +0900 | [diff] [blame] | 82 | import java.net.DatagramSocket; |
Jeremy Klein | 434835a | 2015-12-28 15:11:58 -0800 | [diff] [blame] | 83 | import java.net.InetAddress; |
Jeff Vander Stoep | 39a51e0 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 84 | import java.net.InetSocketAddress; |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 85 | import java.net.Socket; |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 86 | import java.util.ArrayList; |
Lorenzo Colitti | 3f54f10 | 2020-12-12 00:51:11 +0900 | [diff] [blame] | 87 | import java.util.Collection; |
Jeremy Klein | 434835a | 2015-12-28 15:11:58 -0800 | [diff] [blame] | 88 | import java.util.HashMap; |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 89 | import java.util.List; |
| 90 | import java.util.Map; |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 91 | import java.util.Objects; |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 92 | import java.util.concurrent.Executor; |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 93 | import java.util.concurrent.ExecutorService; |
| 94 | import java.util.concurrent.Executors; |
| 95 | import java.util.concurrent.RejectedExecutionException; |
Jeremy Klein | 434835a | 2015-12-28 15:11:58 -0800 | [diff] [blame] | 96 | |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | /** |
| 98 | * Class that answers queries about the state of network connectivity. It also |
Jeff Sharkey | 9b39e9a | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 99 | * notifies applications when network connectivity changes. |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | * <p> |
| 101 | * The primary responsibilities of this class are to: |
| 102 | * <ol> |
| 103 | * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li> |
| 104 | * <li>Send broadcast intents when network connectivity changes</li> |
| 105 | * <li>Attempt to "fail over" to another network when connectivity to a network |
| 106 | * is lost</li> |
| 107 | * <li>Provide an API that allows applications to query the coarse-grained or fine-grained |
| 108 | * state of the available networks</li> |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 109 | * <li>Provide an API that allows applications to request and select networks for their data |
| 110 | * traffic</li> |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | * </ol> |
| 112 | */ |
Jeff Sharkey | 9b39e9a | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 113 | @SystemService(Context.CONNECTIVITY_SERVICE) |
Jeff Sharkey | 921ebf2 | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 114 | public class ConnectivityManager { |
| 115 | private static final String TAG = "ConnectivityManager"; |
Soi, Yoshinari | b8102ef | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 116 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Jeff Sharkey | 921ebf2 | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 117 | |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 118 | /** |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 119 | * 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] | 120 | * been established or lost. The NetworkInfo for the affected network is |
| 121 | * sent as an extra; it should be consulted to see what kind of |
| 122 | * connectivity event occurred. |
| 123 | * <p/> |
Mark Lu | 3e422ac | 2016-12-05 10:57:55 -0800 | [diff] [blame] | 124 | * Apps targeting Android 7.0 (API level 24) and higher do not receive this |
| 125 | * broadcast if they declare the broadcast receiver in their manifest. Apps |
| 126 | * will still receive broadcasts if they register their |
| 127 | * {@link android.content.BroadcastReceiver} with |
| 128 | * {@link android.content.Context#registerReceiver Context.registerReceiver()} |
| 129 | * and that context is still valid. |
| 130 | * <p/> |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | * If this is a connection that was the result of failing over from a |
| 132 | * disconnected network, then the FAILOVER_CONNECTION boolean extra is |
| 133 | * set to true. |
| 134 | * <p/> |
| 135 | * For a loss of connectivity, if the connectivity manager is attempting |
| 136 | * to connect (or has already connected) to another network, the |
| 137 | * NetworkInfo for the new network is also passed as an extra. This lets |
| 138 | * any receivers of the broadcast know that they should not necessarily |
| 139 | * tell the user that no data traffic will be possible. Instead, the |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 140 | * receiver should expect another broadcast soon, indicating either that |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 141 | * the failover attempt succeeded (and so there is still overall data |
| 142 | * connectivity), or that the failover attempt failed, meaning that all |
| 143 | * connectivity has been lost. |
| 144 | * <p/> |
| 145 | * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY |
| 146 | * 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] | 147 | * |
| 148 | * @deprecated apps should use the more versatile {@link #requestNetwork}, |
| 149 | * {@link #registerNetworkCallback} or {@link #registerDefaultNetworkCallback} |
| 150 | * functions instead for faster and more detailed updates about the network |
| 151 | * changes they care about. |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | */ |
Jeff Sharkey | ee9275b | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 153 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Chalard Jean | 52e2396 | 2018-02-10 05:33:50 +0900 | [diff] [blame] | 154 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 155 | public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; |
Jeff Sharkey | 66fa968 | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 156 | |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | /** |
Paul Jensen | 60df4aa | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 158 | * The device has connected to a network that has presented a captive |
| 159 | * portal, which is blocking Internet connectivity. The user was presented |
| 160 | * with a notification that network sign in is required, |
| 161 | * and the user invoked the notification's action indicating they |
Paul Jensen | 75e0adb | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 162 | * desire to sign in to the network. Apps handling this activity should |
Paul Jensen | 60df4aa | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 163 | * facilitate signing in to the network. This action includes a |
| 164 | * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents |
| 165 | * the network presenting the captive portal; all communication with the |
| 166 | * captive portal must be done using this {@code Network} object. |
| 167 | * <p/> |
Paul Jensen | 75e0adb | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 168 | * This activity includes a {@link CaptivePortal} extra named |
| 169 | * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different |
| 170 | * outcomes of the captive portal sign in to the system: |
| 171 | * <ul> |
| 172 | * <li> When the app handling this action believes the user has signed in to |
| 173 | * the network and the captive portal has been dismissed, the app should |
| 174 | * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can |
| 175 | * reevaluate the network. If reevaluation finds the network no longer |
| 176 | * subject to a captive portal, the network may become the default active |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 177 | * data network.</li> |
Paul Jensen | 75e0adb | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 178 | * <li> When the app handling this action believes the user explicitly wants |
Paul Jensen | 60df4aa | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 179 | * to ignore the captive portal and the network, the app should call |
Paul Jensen | 75e0adb | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 180 | * {@link CaptivePortal#ignoreNetwork}. </li> |
| 181 | * </ul> |
Paul Jensen | 60df4aa | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 182 | */ |
| 183 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 184 | public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL"; |
| 185 | |
| 186 | /** |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | * The lookup key for a {@link NetworkInfo} object. Retrieve with |
| 188 | * {@link android.content.Intent#getParcelableExtra(String)}. |
Jeff Sharkey | 66fa968 | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 189 | * |
Chalard Jean | 97021a1 | 2019-01-11 16:47:53 +0900 | [diff] [blame] | 190 | * @deprecated The {@link NetworkInfo} object is deprecated, as many of its properties |
| 191 | * can't accurately represent modern network characteristics. |
| 192 | * Please obtain information about networks from the {@link NetworkCapabilities} |
| 193 | * or {@link LinkProperties} objects instead. |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | */ |
Jeff Sharkey | 66fa968 | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 195 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | public static final String EXTRA_NETWORK_INFO = "networkInfo"; |
Jeff Sharkey | 66fa968 | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 197 | |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 198 | /** |
Jeff Sharkey | 47905d1 | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 199 | * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast. |
Jeff Sharkey | 47905d1 | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 200 | * |
| 201 | * @see android.content.Intent#getIntExtra(String, int) |
Chalard Jean | 97021a1 | 2019-01-11 16:47:53 +0900 | [diff] [blame] | 202 | * @deprecated The network type is not rich enough to represent the characteristics |
| 203 | * of modern networks. Please use {@link NetworkCapabilities} instead, |
| 204 | * in particular the transports. |
Jeff Sharkey | 47905d1 | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 205 | */ |
Chalard Jean | 97021a1 | 2019-01-11 16:47:53 +0900 | [diff] [blame] | 206 | @Deprecated |
Jeff Sharkey | 47905d1 | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 207 | public static final String EXTRA_NETWORK_TYPE = "networkType"; |
| 208 | |
| 209 | /** |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | * The lookup key for a boolean that indicates whether a connect event |
| 211 | * is for a network to which the connectivity manager was failing over |
| 212 | * following a disconnect on another network. |
| 213 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
junyulai | 9826e7f | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 214 | * |
| 215 | * @deprecated See {@link NetworkInfo}. |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | */ |
junyulai | 9826e7f | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 217 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | public static final String EXTRA_IS_FAILOVER = "isFailover"; |
| 219 | /** |
| 220 | * The lookup key for a {@link NetworkInfo} object. This is supplied when |
| 221 | * there is another network that it may be possible to connect to. Retrieve with |
| 222 | * {@link android.content.Intent#getParcelableExtra(String)}. |
junyulai | 9826e7f | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 223 | * |
| 224 | * @deprecated See {@link NetworkInfo}. |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | */ |
junyulai | 9826e7f | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 226 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork"; |
| 228 | /** |
| 229 | * The lookup key for a boolean that indicates whether there is a |
| 230 | * complete lack of connectivity, i.e., no network is available. |
| 231 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
| 232 | */ |
| 233 | public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity"; |
| 234 | /** |
| 235 | * The lookup key for a string that indicates why an attempt to connect |
| 236 | * to a network failed. The string has no particular structure. It is |
| 237 | * intended to be used in notifications presented to users. Retrieve |
| 238 | * it with {@link android.content.Intent#getStringExtra(String)}. |
| 239 | */ |
| 240 | public static final String EXTRA_REASON = "reason"; |
| 241 | /** |
| 242 | * The lookup key for a string that provides optionally supplied |
| 243 | * extra information about the network state. The information |
| 244 | * may be passed up from the lower networking layers, and its |
| 245 | * meaning may be specific to a particular network type. Retrieve |
| 246 | * it with {@link android.content.Intent#getStringExtra(String)}. |
junyulai | 9826e7f | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 247 | * |
| 248 | * @deprecated See {@link NetworkInfo#getExtraInfo()}. |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 249 | */ |
junyulai | 9826e7f | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 250 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | public static final String EXTRA_EXTRA_INFO = "extraInfo"; |
Robert Greenwalt | 986c741 | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 252 | /** |
| 253 | * The lookup key for an int that provides information about |
| 254 | * our connection to the internet at large. 0 indicates no connection, |
| 255 | * 100 indicates a great connection. Retrieve it with |
Jeff Sharkey | 921ebf2 | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 256 | * {@link android.content.Intent#getIntExtra(String, int)}. |
Robert Greenwalt | 986c741 | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 257 | * {@hide} |
| 258 | */ |
| 259 | public static final String EXTRA_INET_CONDITION = "inetCondition"; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 260 | /** |
Paul Jensen | 75e0adb | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 261 | * The lookup key for a {@link CaptivePortal} object included with the |
| 262 | * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal} |
| 263 | * object can be used to either indicate to the system that the captive |
| 264 | * portal has been dismissed or that the user does not want to pursue |
| 265 | * signing in to captive portal. Retrieve it with |
| 266 | * {@link android.content.Intent#getParcelableExtra(String)}. |
Paul Jensen | 60df4aa | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 267 | */ |
Paul Jensen | 75e0adb | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 268 | public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL"; |
Jan Nordqvist | 032e267 | 2015-09-22 15:54:32 -0700 | [diff] [blame] | 269 | |
| 270 | /** |
| 271 | * Key for passing a URL to the captive portal login activity. |
| 272 | */ |
| 273 | public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL"; |
| 274 | |
Paul Jensen | 60df4aa | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 275 | /** |
Remi NGUYEN VAN | c249157 | 2018-05-22 10:01:53 +0900 | [diff] [blame] | 276 | * Key for passing a {@link android.net.captiveportal.CaptivePortalProbeSpec} to the captive |
| 277 | * portal login activity. |
| 278 | * {@hide} |
| 279 | */ |
Remi NGUYEN VAN | 70ab67f | 2019-01-17 14:38:31 +0900 | [diff] [blame] | 280 | @SystemApi |
Remi NGUYEN VAN | c249157 | 2018-05-22 10:01:53 +0900 | [diff] [blame] | 281 | public static final String EXTRA_CAPTIVE_PORTAL_PROBE_SPEC = |
| 282 | "android.net.extra.CAPTIVE_PORTAL_PROBE_SPEC"; |
| 283 | |
| 284 | /** |
Hugo Benichi | 454e066 | 2016-12-14 08:23:40 +0900 | [diff] [blame] | 285 | * Key for passing a user agent string to the captive portal login activity. |
| 286 | * {@hide} |
| 287 | */ |
Remi NGUYEN VAN | 70ab67f | 2019-01-17 14:38:31 +0900 | [diff] [blame] | 288 | @SystemApi |
Hugo Benichi | 454e066 | 2016-12-14 08:23:40 +0900 | [diff] [blame] | 289 | public static final String EXTRA_CAPTIVE_PORTAL_USER_AGENT = |
| 290 | "android.net.extra.CAPTIVE_PORTAL_USER_AGENT"; |
| 291 | |
| 292 | /** |
Haoyu Bai | b5da575 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 293 | * Broadcast action to indicate the change of data activity status |
| 294 | * (idle or active) on a network in a recent period. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 295 | * The network becomes active when data transmission is started, or |
| 296 | * idle if there is no data transmission for a period of time. |
Haoyu Bai | b5da575 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 297 | * {@hide} |
| 298 | */ |
| 299 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 300 | public static final String ACTION_DATA_ACTIVITY_CHANGE = |
| 301 | "android.net.conn.DATA_ACTIVITY_CHANGE"; |
Haoyu Bai | b5da575 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 302 | /** |
| 303 | * The lookup key for an enum that indicates the network device type on which this data activity |
| 304 | * change happens. |
| 305 | * {@hide} |
| 306 | */ |
| 307 | public static final String EXTRA_DEVICE_TYPE = "deviceType"; |
| 308 | /** |
| 309 | * The lookup key for a boolean that indicates the device is active or not. {@code true} means |
| 310 | * it is actively sending or receiving data and {@code false} means it is idle. |
| 311 | * {@hide} |
| 312 | */ |
| 313 | public static final String EXTRA_IS_ACTIVE = "isActive"; |
Ashish Sharma | 29f7e0e | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 314 | /** |
| 315 | * The lookup key for a long that contains the timestamp (nanos) of the radio state change. |
| 316 | * {@hide} |
| 317 | */ |
| 318 | public static final String EXTRA_REALTIME_NS = "tsNanos"; |
Haoyu Bai | b5da575 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 319 | |
| 320 | /** |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | * Broadcast Action: The setting for background data usage has changed |
| 322 | * values. Use {@link #getBackgroundDataSetting()} to get the current value. |
| 323 | * <p> |
| 324 | * If an application uses the network in the background, it should listen |
| 325 | * 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] | 326 | * {@code false}. |
Jeff Sharkey | c958c77 | 2012-01-30 16:29:24 -0800 | [diff] [blame] | 327 | * <p> |
| 328 | * |
| 329 | * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability |
| 330 | * of background data depends on several combined factors, and |
| 331 | * this broadcast is no longer sent. Instead, when background |
| 332 | * data is unavailable, {@link #getActiveNetworkInfo()} will now |
| 333 | * appear disconnected. During first boot after a platform |
| 334 | * upgrade, this broadcast will be sent once if |
| 335 | * {@link #getBackgroundDataSetting()} was {@code false} before |
| 336 | * the upgrade. |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 337 | */ |
| 338 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Jeff Sharkey | c958c77 | 2012-01-30 16:29:24 -0800 | [diff] [blame] | 339 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 340 | public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED = |
| 341 | "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED"; |
| 342 | |
Robert Greenwalt | d3401f9 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 343 | /** |
| 344 | * Broadcast Action: The network connection may not be good |
| 345 | * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and |
| 346 | * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify |
| 347 | * the network and it's condition. |
| 348 | * @hide |
| 349 | */ |
Jeff Sharkey | ee9275b | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 350 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 351 | @UnsupportedAppUsage |
Robert Greenwalt | d3401f9 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 352 | public static final String INET_CONDITION_ACTION = |
| 353 | "android.net.conn.INET_CONDITION_ACTION"; |
| 354 | |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 355 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 356 | * Broadcast Action: A tetherable connection has come or gone. |
| 357 | * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER}, |
Erik Kline | 762fa8e | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 358 | * {@code ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY}, |
| 359 | * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER}, and |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 360 | * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate |
| 361 | * the current state of tethering. Each include a list of |
| 362 | * interface names in that state (may be empty). |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 363 | * @hide |
| 364 | */ |
Jeff Sharkey | ee9275b | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 365 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 366 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 367 | public static final String ACTION_TETHER_STATE_CHANGED = |
markchien | a0f8fd9 | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 368 | TetheringManager.ACTION_TETHER_STATE_CHANGED; |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 369 | |
| 370 | /** |
| 371 | * @hide |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 372 | * gives a String[] listing all the interfaces configured for |
| 373 | * tethering and currently available for tethering. |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 374 | */ |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 375 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
markchien | a0f8fd9 | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 376 | public static final String EXTRA_AVAILABLE_TETHER = TetheringManager.EXTRA_AVAILABLE_TETHER; |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 377 | |
| 378 | /** |
| 379 | * @hide |
Erik Kline | 762fa8e | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 380 | * gives a String[] listing all the interfaces currently in local-only |
| 381 | * mode (ie, has DHCPv4+IPv6-ULA support and no packet forwarding) |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 382 | */ |
markchien | a0f8fd9 | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 383 | 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] | 384 | |
| 385 | /** |
| 386 | * @hide |
| 387 | * gives a String[] listing all the interfaces currently tethered |
| 388 | * (ie, has DHCPv4 support and packets potentially forwarded/NATed) |
| 389 | */ |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 390 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
markchien | a0f8fd9 | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 391 | public static final String EXTRA_ACTIVE_TETHER = TetheringManager.EXTRA_ACTIVE_TETHER; |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 392 | |
| 393 | /** |
| 394 | * @hide |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 395 | * gives a String[] listing all the interfaces we tried to tether and |
| 396 | * failed. Use {@link #getLastTetherError} to find the error code |
| 397 | * for any interfaces listed here. |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 398 | */ |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 399 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
markchien | a0f8fd9 | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 400 | public static final String EXTRA_ERRORED_TETHER = TetheringManager.EXTRA_ERRORED_TETHER; |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 401 | |
| 402 | /** |
Russell Brenner | 30fe264 | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 403 | * Broadcast Action: The captive portal tracker has finished its test. |
| 404 | * Sent only while running Setup Wizard, in lieu of showing a user |
| 405 | * notification. |
| 406 | * @hide |
| 407 | */ |
Jeff Sharkey | ee9275b | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 408 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Russell Brenner | 30fe264 | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 409 | public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED = |
| 410 | "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED"; |
| 411 | /** |
| 412 | * The lookup key for a boolean that indicates whether a captive portal was detected. |
| 413 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
| 414 | * @hide |
| 415 | */ |
| 416 | public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal"; |
| 417 | |
| 418 | /** |
Lorenzo Colitti | 6947c06 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 419 | * Action used to display a dialog that asks the user whether to connect to a network that is |
| 420 | * not validated. This intent is used to start the dialog in settings via startActivity. |
| 421 | * |
| 422 | * @hide |
| 423 | */ |
| 424 | public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED"; |
| 425 | |
| 426 | /** |
Lorenzo Colitti | 199ecfc | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 427 | * Action used to display a dialog that asks the user whether to avoid a network that is no |
| 428 | * longer validated. This intent is used to start the dialog in settings via startActivity. |
| 429 | * |
| 430 | * @hide |
| 431 | */ |
| 432 | public static final String ACTION_PROMPT_LOST_VALIDATION = |
| 433 | "android.net.conn.PROMPT_LOST_VALIDATION"; |
| 434 | |
| 435 | /** |
lucaslin | 2240ef6 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 436 | * Action used to display a dialog that asks the user whether to stay connected to a network |
| 437 | * that has not validated. This intent is used to start the dialog in settings via |
| 438 | * startActivity. |
| 439 | * |
| 440 | * @hide |
| 441 | */ |
| 442 | public static final String ACTION_PROMPT_PARTIAL_CONNECTIVITY = |
| 443 | "android.net.conn.PROMPT_PARTIAL_CONNECTIVITY"; |
| 444 | |
| 445 | /** |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 446 | * Invalid tethering type. |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 447 | * @see #startTethering(int, boolean, OnStartTetheringCallback) |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 448 | * @hide |
| 449 | */ |
markchien | a0f8fd9 | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 450 | public static final int TETHERING_INVALID = TetheringManager.TETHERING_INVALID; |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 451 | |
| 452 | /** |
| 453 | * Wifi tethering type. |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 454 | * @see #startTethering(int, boolean, OnStartTetheringCallback) |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 455 | * @hide |
| 456 | */ |
| 457 | @SystemApi |
Remi NGUYEN VAN | 09f8ed2 | 2021-02-15 18:52:06 +0900 | [diff] [blame] | 458 | public static final int TETHERING_WIFI = 0; |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 459 | |
| 460 | /** |
| 461 | * USB tethering type. |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 462 | * @see #startTethering(int, boolean, OnStartTetheringCallback) |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 463 | * @hide |
| 464 | */ |
| 465 | @SystemApi |
Remi NGUYEN VAN | 09f8ed2 | 2021-02-15 18:52:06 +0900 | [diff] [blame] | 466 | public static final int TETHERING_USB = 1; |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 467 | |
| 468 | /** |
| 469 | * Bluetooth tethering type. |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 470 | * @see #startTethering(int, boolean, OnStartTetheringCallback) |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 471 | * @hide |
| 472 | */ |
| 473 | @SystemApi |
Remi NGUYEN VAN | 09f8ed2 | 2021-02-15 18:52:06 +0900 | [diff] [blame] | 474 | public static final int TETHERING_BLUETOOTH = 2; |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 475 | |
| 476 | /** |
Jimmy Chen | dd31bc4 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 477 | * Wifi P2p tethering type. |
| 478 | * Wifi P2p tethering is set through events automatically, and don't |
| 479 | * need to start from #startTethering(int, boolean, OnStartTetheringCallback). |
| 480 | * @hide |
| 481 | */ |
markchien | a0f8fd9 | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 482 | public static final int TETHERING_WIFI_P2P = TetheringManager.TETHERING_WIFI_P2P; |
Jimmy Chen | dd31bc4 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 483 | |
| 484 | /** |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 485 | * Extra used for communicating with the TetherService. Includes the type of tethering to |
| 486 | * enable if any. |
| 487 | * @hide |
| 488 | */ |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 489 | 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] | 490 | |
| 491 | /** |
| 492 | * Extra used for communicating with the TetherService. Includes the type of tethering for |
| 493 | * which to cancel provisioning. |
| 494 | * @hide |
| 495 | */ |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 496 | 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] | 497 | |
| 498 | /** |
| 499 | * Extra used for communicating with the TetherService. True to schedule a recheck of tether |
| 500 | * provisioning. |
| 501 | * @hide |
| 502 | */ |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 503 | public static final String EXTRA_SET_ALARM = TetheringConstants.EXTRA_SET_ALARM; |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 504 | |
| 505 | /** |
| 506 | * Tells the TetherService to run a provision check now. |
| 507 | * @hide |
| 508 | */ |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 509 | public static final String EXTRA_RUN_PROVISION = TetheringConstants.EXTRA_RUN_PROVISION; |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 510 | |
| 511 | /** |
| 512 | * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver} |
| 513 | * which will receive provisioning results. Can be left empty. |
| 514 | * @hide |
| 515 | */ |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 516 | public static final String EXTRA_PROVISION_CALLBACK = |
| 517 | TetheringConstants.EXTRA_PROVISION_CALLBACK; |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 518 | |
| 519 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 520 | * The absence of a connection type. |
Robert Greenwalt | 33cdcdf | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 521 | * @hide |
| 522 | */ |
paulhu | 74357e7 | 2020-01-13 16:46:45 +0800 | [diff] [blame] | 523 | @SystemApi |
Robert Greenwalt | 33cdcdf | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 524 | public static final int TYPE_NONE = -1; |
| 525 | |
| 526 | /** |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 527 | * A Mobile data connection. Devices may support more than one. |
| 528 | * |
| 529 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 530 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
| 531 | * appropriate network. {@see NetworkCapabilities} for supported transports. |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 532 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 533 | @Deprecated |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 534 | public static final int TYPE_MOBILE = 0; |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 535 | |
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 | * A WIFI data connection. Devices may support more than one. |
| 538 | * |
| 539 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 540 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
| 541 | * appropriate network. {@see NetworkCapabilities} for supported transports. |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 542 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 543 | @Deprecated |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 544 | public static final int TYPE_WIFI = 1; |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 545 | |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 546 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 547 | * An MMS-specific Mobile data connection. This network type may use the |
| 548 | * same network interface as {@link #TYPE_MOBILE} or it may use a different |
| 549 | * one. This is used by applications needing to talk to the carrier's |
| 550 | * Multimedia Messaging Service servers. |
Lorenzo Colitti | 21e9a15 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 551 | * |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 552 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or |
Lorenzo Colitti | 7f6a2bf | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 553 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that |
Lorenzo Colitti | 21e9a15 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 554 | * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability. |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 555 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 556 | @Deprecated |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 557 | public static final int TYPE_MOBILE_MMS = 2; |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 558 | |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 559 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 560 | * A SUPL-specific Mobile data connection. This network type may use the |
| 561 | * same network interface as {@link #TYPE_MOBILE} or it may use a different |
| 562 | * one. This is used by applications needing to talk to the carrier's |
| 563 | * Secure User Plane Location servers for help locating the device. |
Lorenzo Colitti | 21e9a15 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 564 | * |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 565 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or |
Lorenzo Colitti | 7f6a2bf | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 566 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that |
Lorenzo Colitti | 21e9a15 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 567 | * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability. |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 568 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 569 | @Deprecated |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 570 | public static final int TYPE_MOBILE_SUPL = 3; |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 571 | |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 572 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 573 | * A DUN-specific Mobile data connection. This network type may use the |
| 574 | * same network interface as {@link #TYPE_MOBILE} or it may use a different |
| 575 | * one. This is sometimes by the system when setting up an upstream connection |
| 576 | * for tethering so that the carrier is aware of DUN traffic. |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 577 | * |
| 578 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or |
| 579 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that |
| 580 | * provides the {@link NetworkCapabilities#NET_CAPABILITY_DUN} capability. |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 581 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 582 | @Deprecated |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 583 | public static final int TYPE_MOBILE_DUN = 4; |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 584 | |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 585 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 586 | * A High Priority Mobile data connection. This network type uses the |
| 587 | * same network interface as {@link #TYPE_MOBILE} but the routing setup |
Lorenzo Colitti | 21e9a15 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 588 | * is different. |
| 589 | * |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 590 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 591 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
| 592 | * appropriate network. {@see NetworkCapabilities} for supported transports. |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 593 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 594 | @Deprecated |
Robert Greenwalt | 2034b91 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 595 | public static final int TYPE_MOBILE_HIPRI = 5; |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 596 | |
jsh | bfa8172 | 2010-03-11 15:04:43 -0800 | [diff] [blame] | 597 | /** |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 598 | * A WiMAX data connection. |
| 599 | * |
| 600 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 601 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
| 602 | * appropriate network. {@see NetworkCapabilities} for supported transports. |
jsh | bfa8172 | 2010-03-11 15:04:43 -0800 | [diff] [blame] | 603 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 604 | @Deprecated |
jsh | bfa8172 | 2010-03-11 15:04:43 -0800 | [diff] [blame] | 605 | public static final int TYPE_WIMAX = 6; |
Robert Greenwalt | eb123ac | 2010-12-06 13:56:24 -0800 | [diff] [blame] | 606 | |
Jaikumar Ganesh | 0db51a0 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 607 | /** |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 608 | * A Bluetooth data connection. |
| 609 | * |
| 610 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 611 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
| 612 | * appropriate network. {@see NetworkCapabilities} for supported transports. |
Jaikumar Ganesh | 0db51a0 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 613 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 614 | @Deprecated |
Jaikumar Ganesh | 0db51a0 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 615 | public static final int TYPE_BLUETOOTH = 7; |
| 616 | |
Robert Greenwalt | f76c55d | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 617 | /** |
Chiachang Wang | 3b9549f | 2020-07-28 13:53:09 +0800 | [diff] [blame] | 618 | * Fake data connection. This should not be used on shipping devices. |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 619 | * @deprecated This is not used any more. |
Robert Greenwalt | f76c55d | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 620 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 621 | @Deprecated |
Jaikumar Ganesh | 0db51a0 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 622 | public static final int TYPE_DUMMY = 8; |
Wink Saville | b7c92c7 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 623 | |
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 | * An Ethernet data connection. |
| 626 | * |
| 627 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 628 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
| 629 | * appropriate network. {@see NetworkCapabilities} for supported transports. |
Robert Greenwalt | f76c55d | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 630 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 631 | @Deprecated |
Robert Greenwalt | 9d07781 | 2011-01-28 14:48:37 -0800 | [diff] [blame] | 632 | public static final int TYPE_ETHERNET = 9; |
Robert Greenwalt | f76c55d | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 633 | |
Wink Saville | b7c92c7 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 634 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 635 | * Over the air Administration. |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 636 | * @deprecated Use {@link NetworkCapabilities} instead. |
Wink Saville | b7c92c7 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 637 | * {@hide} |
| 638 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 639 | @Deprecated |
Chalard Jean | a3b7751 | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 640 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Wink Saville | b7c92c7 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 641 | public static final int TYPE_MOBILE_FOTA = 10; |
| 642 | |
| 643 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 644 | * IP Multimedia Subsystem. |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 645 | * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IMS} instead. |
Wink Saville | b7c92c7 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 646 | * {@hide} |
| 647 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 648 | @Deprecated |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 649 | @UnsupportedAppUsage |
Wink Saville | b7c92c7 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 650 | public static final int TYPE_MOBILE_IMS = 11; |
| 651 | |
| 652 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 653 | * Carrier Branded Services. |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 654 | * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_CBS} instead. |
Wink Saville | b7c92c7 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 655 | * {@hide} |
| 656 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 657 | @Deprecated |
Chalard Jean | a3b7751 | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 658 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Wink Saville | b7c92c7 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 659 | public static final int TYPE_MOBILE_CBS = 12; |
| 660 | |
repo sync | f5de557 | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 661 | /** |
| 662 | * A Wi-Fi p2p connection. Only requesting processes will have access to |
| 663 | * the peers connected. |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 664 | * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_WIFI_P2P} instead. |
repo sync | f5de557 | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 665 | * {@hide} |
| 666 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 667 | @Deprecated |
paulhu | e102b0b | 2020-01-15 15:38:23 +0800 | [diff] [blame] | 668 | @SystemApi |
repo sync | f5de557 | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 669 | public static final int TYPE_WIFI_P2P = 13; |
Wink Saville | b7c92c7 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 670 | |
Wink Saville | 512c220 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 671 | /** |
| 672 | * The network to use for initially attaching to the network |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 673 | * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IA} instead. |
Wink Saville | 512c220 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 674 | * {@hide} |
| 675 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 676 | @Deprecated |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 677 | @UnsupportedAppUsage |
Wink Saville | 512c220 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 678 | public static final int TYPE_MOBILE_IA = 14; |
repo sync | f5de557 | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 679 | |
Lorenzo Colitti | 21e9a15 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 680 | /** |
Robert Greenwalt | b1f7f75 | 2015-07-09 14:49:35 -0700 | [diff] [blame] | 681 | * Emergency PDN connection for emergency services. This |
| 682 | * may include IMS and MMS in emergency situations. |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 683 | * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_EIMS} instead. |
Ram | 693d07a | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 684 | * {@hide} |
| 685 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 686 | @Deprecated |
Chalard Jean | a3b7751 | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 687 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Ram | 693d07a | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 688 | public static final int TYPE_MOBILE_EMERGENCY = 15; |
| 689 | |
Hui Lu | 865b70d | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 690 | /** |
| 691 | * The network that uses proxy to achieve connectivity. |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 692 | * @deprecated Use {@link NetworkCapabilities} instead. |
Hui Lu | 865b70d | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 693 | * {@hide} |
| 694 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 695 | @Deprecated |
Remi NGUYEN VAN | ee660cf | 2020-11-30 19:23:45 +0900 | [diff] [blame] | 696 | @SystemApi |
Hui Lu | 865b70d | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 697 | public static final int TYPE_PROXY = 16; |
Wink Saville | 512c220 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 698 | |
Robert Greenwalt | ce7a144 | 2014-07-07 17:09:01 -0700 | [diff] [blame] | 699 | /** |
| 700 | * A virtual network using one or more native bearers. |
| 701 | * It may or may not be providing security services. |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 702 | * @deprecated Applications should use {@link NetworkCapabilities#TRANSPORT_VPN} instead. |
Robert Greenwalt | ce7a144 | 2014-07-07 17:09:01 -0700 | [diff] [blame] | 703 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 704 | @Deprecated |
Robert Greenwalt | ce7a144 | 2014-07-07 17:09:01 -0700 | [diff] [blame] | 705 | public static final int TYPE_VPN = 17; |
Hui Lu | 865b70d | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 706 | |
Benedict Wong | bdfaa48 | 2018-11-14 17:40:55 -0800 | [diff] [blame] | 707 | /** |
| 708 | * A network that is exclusively meant to be used for testing |
| 709 | * |
| 710 | * @deprecated Use {@link NetworkCapabilities} instead. |
| 711 | * @hide |
| 712 | */ |
| 713 | @Deprecated |
| 714 | 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] | 715 | |
Chalard Jean | aea539a | 2020-03-25 01:24:04 +0900 | [diff] [blame] | 716 | /** |
| 717 | * @deprecated Use {@link NetworkCapabilities} instead. |
| 718 | * @hide |
| 719 | */ |
| 720 | @Deprecated |
| 721 | @Retention(RetentionPolicy.SOURCE) |
| 722 | @IntDef(prefix = { "TYPE_" }, value = { |
| 723 | TYPE_NONE, |
| 724 | TYPE_MOBILE, |
| 725 | TYPE_WIFI, |
| 726 | TYPE_MOBILE_MMS, |
| 727 | TYPE_MOBILE_SUPL, |
| 728 | TYPE_MOBILE_DUN, |
| 729 | TYPE_MOBILE_HIPRI, |
| 730 | TYPE_WIMAX, |
| 731 | TYPE_BLUETOOTH, |
| 732 | TYPE_DUMMY, |
| 733 | TYPE_ETHERNET, |
| 734 | TYPE_MOBILE_FOTA, |
| 735 | TYPE_MOBILE_IMS, |
| 736 | TYPE_MOBILE_CBS, |
| 737 | TYPE_WIFI_P2P, |
| 738 | TYPE_MOBILE_IA, |
| 739 | TYPE_MOBILE_EMERGENCY, |
| 740 | TYPE_PROXY, |
| 741 | TYPE_VPN, |
| 742 | TYPE_TEST |
| 743 | }) |
| 744 | public @interface LegacyNetworkType {} |
| 745 | |
Chalard Jean | afaed1a | 2019-11-21 14:48:00 +0900 | [diff] [blame] | 746 | // Deprecated constants for return values of startUsingNetworkFeature. They used to live |
| 747 | // in com.android.internal.telephony.PhoneConstants until they were made inaccessible. |
| 748 | private static final int DEPRECATED_PHONE_CONSTANT_APN_ALREADY_ACTIVE = 0; |
| 749 | private static final int DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED = 1; |
| 750 | private static final int DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED = 3; |
| 751 | |
Robert Greenwalt | ce7a144 | 2014-07-07 17:09:01 -0700 | [diff] [blame] | 752 | /** {@hide} */ |
Benedict Wong | bdfaa48 | 2018-11-14 17:40:55 -0800 | [diff] [blame] | 753 | public static final int MAX_RADIO_TYPE = TYPE_TEST; |
| 754 | |
| 755 | /** {@hide} */ |
| 756 | public static final int MAX_NETWORK_TYPE = TYPE_TEST; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 757 | |
Hugo Benichi | ad353f4 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 758 | private static final int MIN_NETWORK_TYPE = TYPE_MOBILE; |
| 759 | |
Jianzheng Zhou | 028d203 | 2012-11-16 13:45:20 +0800 | [diff] [blame] | 760 | /** |
| 761 | * If you want to set the default network preference,you can directly |
| 762 | * change the networkAttributes array in framework's config.xml. |
| 763 | * |
| 764 | * @deprecated Since we support so many more networks now, the single |
| 765 | * network default network preference can't really express |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 766 | * the hierarchy. Instead, the default is defined by the |
Jianzheng Zhou | 028d203 | 2012-11-16 13:45:20 +0800 | [diff] [blame] | 767 | * networkAttributes in config.xml. You can determine |
Robert Greenwalt | f909cb1 | 2012-12-07 09:56:50 -0800 | [diff] [blame] | 768 | * the current value by calling {@link #getNetworkPreference()} |
Jianzheng Zhou | 028d203 | 2012-11-16 13:45:20 +0800 | [diff] [blame] | 769 | * from an App. |
| 770 | */ |
| 771 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 772 | public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI; |
| 773 | |
Jeff Sharkey | 8c87045 | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 774 | /** |
Robert Greenwalt | 42a0e1e | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 775 | * @hide |
| 776 | */ |
Hugo Benichi | 7ab07a3 | 2017-06-20 14:10:14 +0900 | [diff] [blame] | 777 | public static final int REQUEST_ID_UNSET = 0; |
Robert Greenwalt | bfd1f4c | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 778 | |
Paul Jensen | 5dea435 | 2014-07-11 12:28:19 -0400 | [diff] [blame] | 779 | /** |
Hugo Benichi | bee30fe | 2017-06-17 13:14:12 +0900 | [diff] [blame] | 780 | * Static unique request used as a tombstone for NetworkCallbacks that have been unregistered. |
| 781 | * This allows to distinguish when unregistering NetworkCallbacks those that were never |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 782 | * registered from those that were already unregistered. |
Hugo Benichi | bee30fe | 2017-06-17 13:14:12 +0900 | [diff] [blame] | 783 | * @hide |
| 784 | */ |
Hugo Benichi | 7ab07a3 | 2017-06-20 14:10:14 +0900 | [diff] [blame] | 785 | private static final NetworkRequest ALREADY_UNREGISTERED = |
Hugo Benichi | bee30fe | 2017-06-17 13:14:12 +0900 | [diff] [blame] | 786 | new NetworkRequest.Builder().clearCapabilities().build(); |
| 787 | |
| 788 | /** |
Paul Jensen | 5dea435 | 2014-07-11 12:28:19 -0400 | [diff] [blame] | 789 | * A NetID indicating no Network is selected. |
| 790 | * Keep in sync with bionic/libc/dns/include/resolv_netid.h |
| 791 | * @hide |
| 792 | */ |
| 793 | public static final int NETID_UNSET = 0; |
| 794 | |
Erik Kline | b0be3e6 | 2017-10-30 15:29:44 +0900 | [diff] [blame] | 795 | /** |
| 796 | * Private DNS Mode values. |
| 797 | * |
| 798 | * The "private_dns_mode" global setting stores a String value which is |
| 799 | * expected to be one of the following. |
| 800 | */ |
| 801 | |
| 802 | /** |
| 803 | * @hide |
| 804 | */ |
| 805 | public static final String PRIVATE_DNS_MODE_OFF = "off"; |
| 806 | /** |
| 807 | * @hide |
| 808 | */ |
| 809 | public static final String PRIVATE_DNS_MODE_OPPORTUNISTIC = "opportunistic"; |
| 810 | /** |
| 811 | * @hide |
| 812 | */ |
| 813 | public static final String PRIVATE_DNS_MODE_PROVIDER_HOSTNAME = "hostname"; |
| 814 | /** |
| 815 | * The default Private DNS mode. |
| 816 | * |
| 817 | * This may change from release to release or may become dependent upon |
| 818 | * the capabilities of the underlying platform. |
| 819 | * |
| 820 | * @hide |
| 821 | */ |
Erik Kline | a7edf6f | 2018-05-16 16:41:57 +0900 | [diff] [blame] | 822 | public static final String PRIVATE_DNS_DEFAULT_MODE_FALLBACK = PRIVATE_DNS_MODE_OPPORTUNISTIC; |
Erik Kline | b0be3e6 | 2017-10-30 15:29:44 +0900 | [diff] [blame] | 823 | |
Chalard Jean | a3b7751 | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 824 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Jeff Sharkey | 921ebf2 | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 825 | private final IConnectivityManager mService; |
Lorenzo Colitti | cd67529 | 2021-02-04 17:32:07 +0900 | [diff] [blame] | 826 | |
Paul Jensen | c0618a6 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 827 | /** |
| 828 | * A kludge to facilitate static access where a Context pointer isn't available, like in the |
| 829 | * case of the static set/getProcessDefaultNetwork methods and from the Network class. |
| 830 | * TODO: Remove this after deprecating the static methods in favor of non-static methods or |
| 831 | * methods that take a Context argument. |
| 832 | */ |
| 833 | private static ConnectivityManager sInstance; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 834 | |
Lorenzo Colitti | 2780e4b | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 835 | private final Context mContext; |
| 836 | |
Felipe Leme | 3051135 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 837 | private INetworkPolicyManager mNPManager; |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 838 | private final TetheringManager mTetheringManager; |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 839 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 840 | /** |
| 841 | * Tests if a given integer represents a valid network type. |
| 842 | * @param networkType the type to be tested |
| 843 | * @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] | 844 | * @deprecated All APIs accepting a network type are deprecated. There should be no need to |
| 845 | * validate a network type. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 846 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 847 | @Deprecated |
Jeff Sharkey | 21062e7 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 848 | public static boolean isNetworkTypeValid(int networkType) { |
Hugo Benichi | ad353f4 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 849 | return MIN_NETWORK_TYPE <= networkType && networkType <= MAX_NETWORK_TYPE; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 850 | } |
| 851 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 852 | /** |
| 853 | * Returns a non-localized string representing a given network type. |
| 854 | * ONLY used for debugging output. |
| 855 | * @param type the type needing naming |
| 856 | * @return a String for the given type, or a string version of the type ("87") |
| 857 | * if no name is known. |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 858 | * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 859 | * {@hide} |
| 860 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 861 | @Deprecated |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 862 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Jeff Sharkey | 21062e7 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 863 | public static String getNetworkTypeName(int type) { |
| 864 | switch (type) { |
Hugo Benichi | ad353f4 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 865 | case TYPE_NONE: |
| 866 | return "NONE"; |
Jeff Sharkey | 21062e7 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 867 | case TYPE_MOBILE: |
| 868 | return "MOBILE"; |
| 869 | case TYPE_WIFI: |
| 870 | return "WIFI"; |
| 871 | case TYPE_MOBILE_MMS: |
| 872 | return "MOBILE_MMS"; |
| 873 | case TYPE_MOBILE_SUPL: |
| 874 | return "MOBILE_SUPL"; |
| 875 | case TYPE_MOBILE_DUN: |
| 876 | return "MOBILE_DUN"; |
| 877 | case TYPE_MOBILE_HIPRI: |
| 878 | return "MOBILE_HIPRI"; |
| 879 | case TYPE_WIMAX: |
| 880 | return "WIMAX"; |
| 881 | case TYPE_BLUETOOTH: |
| 882 | return "BLUETOOTH"; |
| 883 | case TYPE_DUMMY: |
| 884 | return "DUMMY"; |
| 885 | case TYPE_ETHERNET: |
| 886 | return "ETHERNET"; |
| 887 | case TYPE_MOBILE_FOTA: |
| 888 | return "MOBILE_FOTA"; |
| 889 | case TYPE_MOBILE_IMS: |
| 890 | return "MOBILE_IMS"; |
| 891 | case TYPE_MOBILE_CBS: |
| 892 | return "MOBILE_CBS"; |
repo sync | f5de557 | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 893 | case TYPE_WIFI_P2P: |
| 894 | return "WIFI_P2P"; |
Wink Saville | 512c220 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 895 | case TYPE_MOBILE_IA: |
| 896 | return "MOBILE_IA"; |
Ram | 693d07a | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 897 | case TYPE_MOBILE_EMERGENCY: |
| 898 | return "MOBILE_EMERGENCY"; |
Hui Lu | 865b70d | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 899 | case TYPE_PROXY: |
| 900 | return "PROXY"; |
Erik Kline | 137fadc | 2014-11-19 17:23:41 +0900 | [diff] [blame] | 901 | case TYPE_VPN: |
| 902 | return "VPN"; |
Jeff Sharkey | 21062e7 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 903 | default: |
| 904 | return Integer.toString(type); |
| 905 | } |
| 906 | } |
| 907 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 908 | /** |
Aaron Huang | 9601189 | 2020-06-27 07:18:23 +0800 | [diff] [blame] | 909 | * @hide |
| 910 | * TODO: Expose for SystemServer when becomes a module. |
| 911 | */ |
| 912 | public void systemReady() { |
| 913 | try { |
| 914 | mService.systemReady(); |
| 915 | } catch (RemoteException e) { |
| 916 | throw e.rethrowFromSystemServer(); |
| 917 | } |
| 918 | } |
| 919 | |
| 920 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 921 | * Checks if a given type uses the cellular data connection. |
| 922 | * This should be replaced in the future by a network property. |
| 923 | * @param networkType the type to check |
| 924 | * @return a boolean - {@code true} if uses cellular network, else {@code false} |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 925 | * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 926 | * {@hide} |
| 927 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 928 | @Deprecated |
Chalard Jean | a3b7751 | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 929 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Jeff Sharkey | 21062e7 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 930 | public static boolean isNetworkTypeMobile(int networkType) { |
| 931 | switch (networkType) { |
| 932 | case TYPE_MOBILE: |
| 933 | case TYPE_MOBILE_MMS: |
| 934 | case TYPE_MOBILE_SUPL: |
| 935 | case TYPE_MOBILE_DUN: |
| 936 | case TYPE_MOBILE_HIPRI: |
| 937 | case TYPE_MOBILE_FOTA: |
| 938 | case TYPE_MOBILE_IMS: |
| 939 | case TYPE_MOBILE_CBS: |
Wink Saville | 512c220 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 940 | case TYPE_MOBILE_IA: |
Ram | 693d07a | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 941 | case TYPE_MOBILE_EMERGENCY: |
Jeff Sharkey | 21062e7 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 942 | return true; |
| 943 | default: |
| 944 | return false; |
| 945 | } |
| 946 | } |
| 947 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 948 | /** |
Jeff Sharkey | 79f3e02 | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 949 | * Checks if the given network type is backed by a Wi-Fi radio. |
| 950 | * |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 951 | * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead. |
Jeff Sharkey | 79f3e02 | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 952 | * @hide |
| 953 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 954 | @Deprecated |
Jeff Sharkey | 79f3e02 | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 955 | public static boolean isNetworkTypeWifi(int networkType) { |
| 956 | switch (networkType) { |
| 957 | case TYPE_WIFI: |
| 958 | case TYPE_WIFI_P2P: |
| 959 | return true; |
| 960 | default: |
| 961 | return false; |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 966 | * Specifies the preferred network type. When the device has more |
| 967 | * than one type available the preferred network type will be used. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 968 | * |
| 969 | * @param preference the network type to prefer over all others. It is |
| 970 | * unspecified what happens to the old preferred network in the |
| 971 | * overall ordering. |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 972 | * @deprecated Functionality has been removed as it no longer makes sense, |
| 973 | * with many more than two networks - we'd need an array to express |
| 974 | * preference. Instead we use dynamic network properties of |
| 975 | * the networks to describe their precedence. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 976 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 977 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 978 | public void setNetworkPreference(int preference) { |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 979 | } |
| 980 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 981 | /** |
| 982 | * Retrieves the current preferred network type. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 983 | * |
| 984 | * @return an integer representing the preferred network type |
| 985 | * |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 986 | * @deprecated Functionality has been removed as it no longer makes sense, |
| 987 | * with many more than two networks - we'd need an array to express |
| 988 | * preference. Instead we use dynamic network properties of |
| 989 | * the networks to describe their precedence. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 990 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 991 | @Deprecated |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 992 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 993 | public int getNetworkPreference() { |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 994 | return TYPE_NONE; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 995 | } |
| 996 | |
Scott Main | f58b7d8 | 2011-10-06 19:02:28 -0700 | [diff] [blame] | 997 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 998 | * Returns details about the currently active default data network. When |
| 999 | * connected, this network is the default route for outgoing connections. |
| 1000 | * You should always check {@link NetworkInfo#isConnected()} before initiating |
| 1001 | * network traffic. This may return {@code null} when there is no default |
| 1002 | * network. |
Chalard Jean | 96d10a7 | 2018-03-29 17:45:24 +0900 | [diff] [blame] | 1003 | * Note that if the default network is a VPN, this method will return the |
| 1004 | * NetworkInfo for one of its underlying networks instead, or null if the |
| 1005 | * VPN agent did not specify any. Apps interested in learning about VPNs |
| 1006 | * should use {@link #getNetworkInfo(android.net.Network)} instead. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1007 | * |
| 1008 | * @return a {@link NetworkInfo} object for the current default network |
Paul Jensen | bd2d378 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1009 | * or {@code null} if no default network is currently active |
junyulai | 9826e7f | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 1010 | * @deprecated See {@link NetworkInfo}. |
Jeff Sharkey | d00b130 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1011 | */ |
junyulai | 9826e7f | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 1012 | @Deprecated |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1013 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1014 | @Nullable |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1015 | public NetworkInfo getActiveNetworkInfo() { |
| 1016 | try { |
| 1017 | return mService.getActiveNetworkInfo(); |
| 1018 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1019 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1020 | } |
| 1021 | } |
| 1022 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1023 | /** |
Paul Jensen | 1f56738 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1024 | * Returns a {@link Network} object corresponding to the currently active |
| 1025 | * default data network. In the event that the current active default data |
| 1026 | * network disconnects, the returned {@code Network} object will no longer |
| 1027 | * be usable. This will return {@code null} when there is no default |
| 1028 | * network. |
| 1029 | * |
| 1030 | * @return a {@link Network} object for the current default network or |
| 1031 | * {@code null} if no default network is currently active |
Paul Jensen | 1f56738 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1032 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1033 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1034 | @Nullable |
Paul Jensen | 1f56738 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1035 | public Network getActiveNetwork() { |
| 1036 | try { |
| 1037 | return mService.getActiveNetwork(); |
| 1038 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1039 | throw e.rethrowFromSystemServer(); |
Paul Jensen | 1f56738 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | /** |
Robin Lee | 5b52bef | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1044 | * Returns a {@link Network} object corresponding to the currently active |
| 1045 | * default data network for a specific UID. In the event that the default data |
| 1046 | * network disconnects, the returned {@code Network} object will no longer |
| 1047 | * be usable. This will return {@code null} when there is no default |
| 1048 | * network for the UID. |
Robin Lee | 5b52bef | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1049 | * |
| 1050 | * @return a {@link Network} object for the current default network for the |
| 1051 | * given UID or {@code null} if no default network is currently active |
| 1052 | * |
| 1053 | * @hide |
| 1054 | */ |
paulhu | 8e96a75 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1055 | @RequiresPermission(android.Manifest.permission.NETWORK_STACK) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1056 | @Nullable |
Robin Lee | 5b52bef | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1057 | public Network getActiveNetworkForUid(int uid) { |
Jeff Sharkey | 7dbf83d | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1058 | return getActiveNetworkForUid(uid, false); |
| 1059 | } |
| 1060 | |
| 1061 | /** {@hide} */ |
| 1062 | public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) { |
Robin Lee | 5b52bef | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1063 | try { |
Jeff Sharkey | 7dbf83d | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1064 | return mService.getActiveNetworkForUid(uid, ignoreBlocked); |
Robin Lee | 5b52bef | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1065 | } catch (RemoteException e) { |
| 1066 | throw e.rethrowFromSystemServer(); |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | /** |
Lorenzo Colitti | 3f54f10 | 2020-12-12 00:51:11 +0900 | [diff] [blame] | 1071 | * Adds or removes a requirement for given UID ranges to use the VPN. |
| 1072 | * |
| 1073 | * If set to {@code true}, informs the system that the UIDs in the specified ranges must not |
| 1074 | * have any connectivity except if a VPN is connected and applies to the UIDs, or if the UIDs |
| 1075 | * otherwise have permission to bypass the VPN (e.g., because they have the |
| 1076 | * {@link android.Manifest.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS} permission, or when |
| 1077 | * using a socket protected by a method such as {@link VpnService#protect(DatagramSocket)}. If |
| 1078 | * set to {@code false}, a previously-added restriction is removed. |
| 1079 | * <p> |
| 1080 | * Each of the UID ranges specified by this method is added and removed as is, and no processing |
| 1081 | * is performed on the ranges to de-duplicate, merge, split, or intersect them. In order to |
| 1082 | * remove a previously-added range, the exact range must be removed as is. |
| 1083 | * <p> |
| 1084 | * The changes are applied asynchronously and may not have been applied by the time the method |
| 1085 | * returns. Apps will be notified about any changes that apply to them via |
| 1086 | * {@link NetworkCallback#onBlockedStatusChanged} callbacks called after the changes take |
| 1087 | * effect. |
| 1088 | * <p> |
| 1089 | * This method should be called only by the VPN code. |
| 1090 | * |
| 1091 | * @param ranges the UID ranges to restrict |
| 1092 | * @param requireVpn whether the specified UID ranges must use a VPN |
| 1093 | * |
| 1094 | * TODO: expose as @SystemApi. |
| 1095 | * @hide |
| 1096 | */ |
| 1097 | @RequiresPermission(anyOf = { |
| 1098 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 1099 | android.Manifest.permission.NETWORK_STACK}) |
| 1100 | public void setRequireVpnForUids(boolean requireVpn, |
| 1101 | @NonNull Collection<Range<Integer>> ranges) { |
| 1102 | Objects.requireNonNull(ranges); |
| 1103 | // The Range class is not parcelable. Convert to UidRange, which is what is used internally. |
| 1104 | // This method is not necessarily expected to be used outside the system server, so |
| 1105 | // parceling may not be necessary, but it could be used out-of-process, e.g., by the network |
| 1106 | // stack process, or by tests. |
| 1107 | UidRange[] rangesArray = new UidRange[ranges.size()]; |
| 1108 | int index = 0; |
| 1109 | for (Range<Integer> range : ranges) { |
| 1110 | rangesArray[index++] = new UidRange(range.getLower(), range.getUpper()); |
| 1111 | } |
| 1112 | try { |
| 1113 | mService.setRequireVpnForUids(requireVpn, rangesArray); |
| 1114 | } catch (RemoteException e) { |
| 1115 | throw e.rethrowFromSystemServer(); |
| 1116 | } |
| 1117 | } |
| 1118 | |
| 1119 | /** |
Lorenzo Colitti | bcd692f | 2021-01-15 01:29:01 +0900 | [diff] [blame] | 1120 | * Informs ConnectivityService of whether the legacy lockdown VPN, as implemented by |
| 1121 | * LockdownVpnTracker, is in use. This is deprecated for new devices starting from Android 12 |
| 1122 | * but is still supported for backwards compatibility. |
| 1123 | * <p> |
| 1124 | * This type of VPN is assumed always to use the system default network, and must always declare |
| 1125 | * exactly one underlying network, which is the network that was the default when the VPN |
| 1126 | * connected. |
| 1127 | * <p> |
| 1128 | * Calling this method with {@code true} enables legacy behaviour, specifically: |
| 1129 | * <ul> |
| 1130 | * <li>Any VPN that applies to userId 0 behaves specially with respect to deprecated |
| 1131 | * {@link #CONNECTIVITY_ACTION} broadcasts. Any such broadcasts will have the state in the |
| 1132 | * {@link #EXTRA_NETWORK_INFO} replaced by state of the VPN network. Also, any time the VPN |
| 1133 | * connects, a {@link #CONNECTIVITY_ACTION} broadcast will be sent for the network |
| 1134 | * underlying the VPN.</li> |
| 1135 | * <li>Deprecated APIs that return {@link NetworkInfo} objects will have their state |
| 1136 | * similarly replaced by the VPN network state.</li> |
| 1137 | * <li>Information on current network interfaces passed to NetworkStatsService will not |
| 1138 | * include any VPN interfaces.</li> |
| 1139 | * </ul> |
| 1140 | * |
| 1141 | * @param enabled whether legacy lockdown VPN is enabled or disabled |
| 1142 | * |
| 1143 | * TODO: @SystemApi(client = MODULE_LIBRARIES) |
| 1144 | * |
| 1145 | * @hide |
| 1146 | */ |
| 1147 | @RequiresPermission(anyOf = { |
| 1148 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 1149 | android.Manifest.permission.NETWORK_SETTINGS}) |
| 1150 | public void setLegacyLockdownVpnEnabled(boolean enabled) { |
| 1151 | try { |
| 1152 | mService.setLegacyLockdownVpnEnabled(enabled); |
| 1153 | } catch (RemoteException e) { |
| 1154 | throw e.rethrowFromSystemServer(); |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1159 | * Returns details about the currently active default data network |
| 1160 | * for a given uid. This is for internal use only to avoid spying |
| 1161 | * other apps. |
| 1162 | * |
| 1163 | * @return a {@link NetworkInfo} object for the current default network |
| 1164 | * for the given uid or {@code null} if no default network is |
| 1165 | * available for the specified uid. |
| 1166 | * |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1167 | * {@hide} |
| 1168 | */ |
paulhu | 8e96a75 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1169 | @RequiresPermission(android.Manifest.permission.NETWORK_STACK) |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 1170 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Jeff Sharkey | 921ebf2 | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1171 | public NetworkInfo getActiveNetworkInfoForUid(int uid) { |
Jeff Sharkey | 7dbf83d | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1172 | return getActiveNetworkInfoForUid(uid, false); |
| 1173 | } |
| 1174 | |
| 1175 | /** {@hide} */ |
| 1176 | public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) { |
Jeff Sharkey | 921ebf2 | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1177 | try { |
Jeff Sharkey | 7dbf83d | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1178 | return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked); |
Jeff Sharkey | 921ebf2 | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1179 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1180 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 921ebf2 | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1181 | } |
| 1182 | } |
| 1183 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1184 | /** |
| 1185 | * Returns connection status information about a particular |
| 1186 | * network type. |
| 1187 | * |
| 1188 | * @param networkType integer specifying which networkType in |
| 1189 | * which you're interested. |
| 1190 | * @return a {@link NetworkInfo} object for the requested |
| 1191 | * network type or {@code null} if the type is not |
Chalard Jean | 96d10a7 | 2018-03-29 17:45:24 +0900 | [diff] [blame] | 1192 | * supported by the device. If {@code networkType} is |
| 1193 | * TYPE_VPN and a VPN is active for the calling app, |
| 1194 | * then this method will try to return one of the |
| 1195 | * underlying networks for the VPN or null if the |
| 1196 | * VPN agent didn't specify any. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1197 | * |
Paul Jensen | dda8e59 | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 1198 | * @deprecated This method does not support multiple connected networks |
| 1199 | * of the same type. Use {@link #getAllNetworks} and |
| 1200 | * {@link #getNetworkInfo(android.net.Network)} instead. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1201 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 1202 | @Deprecated |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1203 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1204 | @Nullable |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1205 | public NetworkInfo getNetworkInfo(int networkType) { |
| 1206 | try { |
| 1207 | return mService.getNetworkInfo(networkType); |
| 1208 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1209 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1210 | } |
| 1211 | } |
| 1212 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1213 | /** |
Robert Greenwalt | 02fe11e | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1214 | * Returns connection status information about a particular |
| 1215 | * Network. |
| 1216 | * |
| 1217 | * @param network {@link Network} specifying which network |
| 1218 | * in which you're interested. |
| 1219 | * @return a {@link NetworkInfo} object for the requested |
| 1220 | * network or {@code null} if the {@code Network} |
| 1221 | * is not valid. |
junyulai | 9826e7f | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 1222 | * @deprecated See {@link NetworkInfo}. |
Robert Greenwalt | 02fe11e | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1223 | */ |
junyulai | 9826e7f | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 1224 | @Deprecated |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1225 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1226 | @Nullable |
| 1227 | public NetworkInfo getNetworkInfo(@Nullable Network network) { |
Jeff Sharkey | 7dbf83d | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1228 | return getNetworkInfoForUid(network, Process.myUid(), false); |
| 1229 | } |
| 1230 | |
| 1231 | /** {@hide} */ |
| 1232 | public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) { |
Robert Greenwalt | 02fe11e | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1233 | try { |
Jeff Sharkey | 7dbf83d | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1234 | return mService.getNetworkInfoForUid(network, uid, ignoreBlocked); |
Robert Greenwalt | 02fe11e | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1235 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1236 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 02fe11e | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1237 | } |
| 1238 | } |
| 1239 | |
| 1240 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1241 | * Returns connection status information about all network |
| 1242 | * types supported by the device. |
| 1243 | * |
| 1244 | * @return an array of {@link NetworkInfo} objects. Check each |
| 1245 | * {@link NetworkInfo#getType} for which type each applies. |
| 1246 | * |
Paul Jensen | dda8e59 | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 1247 | * @deprecated This method does not support multiple connected networks |
| 1248 | * of the same type. Use {@link #getAllNetworks} and |
| 1249 | * {@link #getNetworkInfo(android.net.Network)} instead. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1250 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 1251 | @Deprecated |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1252 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1253 | @NonNull |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1254 | public NetworkInfo[] getAllNetworkInfo() { |
| 1255 | try { |
| 1256 | return mService.getAllNetworkInfo(); |
| 1257 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1258 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1259 | } |
| 1260 | } |
| 1261 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1262 | /** |
junyulai | ebd1516 | 2021-03-03 12:09:05 +0800 | [diff] [blame] | 1263 | * Return a list of {@link NetworkStateSnapshot}s, one for each network that is currently |
| 1264 | * connected. |
| 1265 | * @hide |
| 1266 | */ |
| 1267 | @SystemApi(client = MODULE_LIBRARIES) |
| 1268 | @RequiresPermission(anyOf = { |
| 1269 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 1270 | android.Manifest.permission.NETWORK_STACK, |
| 1271 | android.Manifest.permission.NETWORK_SETTINGS}) |
| 1272 | @NonNull |
| 1273 | public List<NetworkStateSnapshot> getAllNetworkStateSnapshot() { |
| 1274 | try { |
| 1275 | return mService.getAllNetworkStateSnapshot(); |
| 1276 | } catch (RemoteException e) { |
| 1277 | throw e.rethrowFromSystemServer(); |
| 1278 | } |
| 1279 | } |
| 1280 | |
| 1281 | /** |
Lorenzo Colitti | 860a7aa | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1282 | * Returns the {@link Network} object currently serving a given type, or |
| 1283 | * null if the given type is not connected. |
| 1284 | * |
Lorenzo Colitti | 860a7aa | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1285 | * @hide |
Paul Jensen | dda8e59 | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 1286 | * @deprecated This method does not support multiple connected networks |
| 1287 | * of the same type. Use {@link #getAllNetworks} and |
| 1288 | * {@link #getNetworkInfo(android.net.Network)} instead. |
Lorenzo Colitti | 860a7aa | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1289 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 1290 | @Deprecated |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1291 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 1292 | @UnsupportedAppUsage |
Lorenzo Colitti | 860a7aa | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1293 | public Network getNetworkForType(int networkType) { |
| 1294 | try { |
| 1295 | return mService.getNetworkForType(networkType); |
| 1296 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1297 | throw e.rethrowFromSystemServer(); |
Lorenzo Colitti | 860a7aa | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1298 | } |
| 1299 | } |
| 1300 | |
| 1301 | /** |
Robert Greenwalt | 02fe11e | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1302 | * Returns an array of all {@link Network} currently tracked by the |
| 1303 | * framework. |
Paul Jensen | 08f9dbb | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1304 | * |
| 1305 | * @return an array of {@link Network} objects. |
Robert Greenwalt | 02fe11e | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1306 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1307 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1308 | @NonNull |
Robert Greenwalt | 02fe11e | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1309 | public Network[] getAllNetworks() { |
| 1310 | try { |
| 1311 | return mService.getAllNetworks(); |
| 1312 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1313 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 02fe11e | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | /** |
Lorenzo Colitti | 21e9a15 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 1318 | * Returns an array of {@link android.net.NetworkCapabilities} objects, representing |
Lorenzo Colitti | 79bd2e2 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1319 | * the Networks that applications run by the given user will use by default. |
| 1320 | * @hide |
| 1321 | */ |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 1322 | @UnsupportedAppUsage |
Lorenzo Colitti | 79bd2e2 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1323 | public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) { |
| 1324 | try { |
Qingxi Li | b274810 | 2020-01-08 12:51:49 -0800 | [diff] [blame] | 1325 | return mService.getDefaultNetworkCapabilitiesForUser( |
Roshan Pius | aa24fde | 2020-12-17 14:53:09 -0800 | [diff] [blame] | 1326 | userId, mContext.getOpPackageName(), getAttributionTag()); |
Lorenzo Colitti | 79bd2e2 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1327 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1328 | throw e.rethrowFromSystemServer(); |
Lorenzo Colitti | 79bd2e2 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1333 | * Returns the IP information for the current default network. |
| 1334 | * |
| 1335 | * @return a {@link LinkProperties} object describing the IP info |
| 1336 | * for the current default network, or {@code null} if there |
| 1337 | * is no current default network. |
| 1338 | * |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1339 | * {@hide} |
Chalard Jean | 97021a1 | 2019-01-11 16:47:53 +0900 | [diff] [blame] | 1340 | * @deprecated please use {@link #getLinkProperties(Network)} on the return |
| 1341 | * value of {@link #getActiveNetwork()} instead. In particular, |
| 1342 | * this method will return non-null LinkProperties even if the |
| 1343 | * app is blocked by policy from using this network. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1344 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1345 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 97021a1 | 2019-01-11 16:47:53 +0900 | [diff] [blame] | 1346 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 109783091) |
Robert Greenwalt | 9f0ee4f | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1347 | public LinkProperties getActiveLinkProperties() { |
| 1348 | try { |
| 1349 | return mService.getActiveLinkProperties(); |
| 1350 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1351 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 9f0ee4f | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1352 | } |
| 1353 | } |
| 1354 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1355 | /** |
| 1356 | * Returns the IP information for a given network type. |
| 1357 | * |
| 1358 | * @param networkType the network type of interest. |
| 1359 | * @return a {@link LinkProperties} object describing the IP info |
| 1360 | * for the given networkType, or {@code null} if there is |
| 1361 | * no current default network. |
| 1362 | * |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1363 | * {@hide} |
Paul Jensen | dda8e59 | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 1364 | * @deprecated This method does not support multiple connected networks |
| 1365 | * of the same type. Use {@link #getAllNetworks}, |
| 1366 | * {@link #getNetworkInfo(android.net.Network)}, and |
| 1367 | * {@link #getLinkProperties(android.net.Network)} instead. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1368 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 1369 | @Deprecated |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1370 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | a3b7751 | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 1371 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Robert Greenwalt | 9f0ee4f | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1372 | public LinkProperties getLinkProperties(int networkType) { |
| 1373 | try { |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1374 | return mService.getLinkPropertiesForType(networkType); |
| 1375 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1376 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1377 | } |
| 1378 | } |
| 1379 | |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1380 | /** |
| 1381 | * Get the {@link LinkProperties} for the given {@link Network}. This |
| 1382 | * will return {@code null} if the network is unknown. |
| 1383 | * |
| 1384 | * @param network The {@link Network} object identifying the network in question. |
| 1385 | * @return The {@link LinkProperties} for the network, or {@code null}. |
Paul Jensen | 08f9dbb | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1386 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1387 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1388 | @Nullable |
| 1389 | public LinkProperties getLinkProperties(@Nullable Network network) { |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1390 | try { |
| 1391 | return mService.getLinkProperties(network); |
| 1392 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1393 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1394 | } |
| 1395 | } |
| 1396 | |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1397 | /** |
Lorenzo Colitti | 21e9a15 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 1398 | * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1399 | * will return {@code null} if the network is unknown. |
| 1400 | * |
| 1401 | * @param network The {@link Network} object identifying the network in question. |
Lorenzo Colitti | 21e9a15 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 1402 | * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}. |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1403 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1404 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1405 | @Nullable |
| 1406 | public NetworkCapabilities getNetworkCapabilities(@Nullable Network network) { |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1407 | try { |
Roshan Pius | aa24fde | 2020-12-17 14:53:09 -0800 | [diff] [blame] | 1408 | return mService.getNetworkCapabilities( |
| 1409 | network, mContext.getOpPackageName(), getAttributionTag()); |
Robert Greenwalt | 9f0ee4f | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1410 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1411 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 9f0ee4f | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1412 | } |
| 1413 | } |
| 1414 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1415 | /** |
Remi NGUYEN VAN | 035f653 | 2019-03-20 14:22:49 +0900 | [diff] [blame] | 1416 | * 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] | 1417 | * 1. This URL should respond with a 204 response to a GET request to indicate no captive |
| 1418 | * portal is present. |
| 1419 | * 2. This URL must be HTTP as redirect responses are used to find captive portal |
| 1420 | * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects. |
| 1421 | * |
Remi NGUYEN VAN | 035f653 | 2019-03-20 14:22:49 +0900 | [diff] [blame] | 1422 | * The system network validation may be using different strategies to detect captive portals, |
| 1423 | * so this method does not necessarily return a URL used by the system. It only returns a URL |
| 1424 | * that may be relevant for other components trying to detect captive portals. |
paulhu | 8e96a75 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1425 | * |
Udam Saini | cd64546 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 1426 | * @hide |
paulhu | 8e96a75 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1427 | * @deprecated This API returns URL which is not guaranteed to be one of the URLs used by the |
| 1428 | * system. |
Udam Saini | cd64546 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 1429 | */ |
paulhu | 8e96a75 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1430 | @Deprecated |
Udam Saini | cd64546 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 1431 | @SystemApi |
paulhu | 8e96a75 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1432 | @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS) |
Udam Saini | cd64546 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 1433 | public String getCaptivePortalServerUrl() { |
| 1434 | try { |
| 1435 | return mService.getCaptivePortalServerUrl(); |
| 1436 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1437 | throw e.rethrowFromSystemServer(); |
Udam Saini | cd64546 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 1438 | } |
| 1439 | } |
| 1440 | |
| 1441 | /** |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1442 | * Tells the underlying networking system that the caller wants to |
| 1443 | * begin using the named feature. The interpretation of {@code feature} |
| 1444 | * is completely up to each networking implementation. |
Lorenzo Colitti | 0b40c87 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 1445 | * |
| 1446 | * <p>This method requires the caller to hold either the |
| 1447 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 1448 | * or the ability to modify system settings as determined by |
| 1449 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 1450 | * |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1451 | * @param networkType specifies which network the request pertains to |
| 1452 | * @param feature the name of the feature to be used |
| 1453 | * @return an integer value representing the outcome of the request. |
| 1454 | * The interpretation of this value is specific to each networking |
| 1455 | * implementation+feature combination, except that the value {@code -1} |
| 1456 | * always indicates failure. |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1457 | * |
Lorenzo Colitti | 7f6a2bf | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 1458 | * @deprecated Deprecated in favor of the cleaner |
| 1459 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API. |
Dianne Hackborn | 18c1d83 | 2015-07-31 10:35:34 -0700 | [diff] [blame] | 1460 | * 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] | 1461 | * throw {@code UnsupportedOperationException} if called. |
Lorenzo Colitti | 84d7f07 | 2016-12-09 18:39:30 +0900 | [diff] [blame] | 1462 | * @removed |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1463 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 1464 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1465 | public int startUsingNetworkFeature(int networkType, String feature) { |
Lorenzo Colitti | 2780e4b | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1466 | checkLegacyRoutingApiAccess(); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1467 | NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature); |
| 1468 | if (netCap == null) { |
| 1469 | Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " + |
| 1470 | feature); |
Chalard Jean | afaed1a | 2019-11-21 14:48:00 +0900 | [diff] [blame] | 1471 | return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED; |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1472 | } |
| 1473 | |
| 1474 | NetworkRequest request = null; |
| 1475 | synchronized (sLegacyRequests) { |
| 1476 | LegacyRequest l = sLegacyRequests.get(netCap); |
| 1477 | if (l != null) { |
| 1478 | Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest); |
| 1479 | renewRequestLocked(l); |
| 1480 | if (l.currentNetwork != null) { |
Chalard Jean | afaed1a | 2019-11-21 14:48:00 +0900 | [diff] [blame] | 1481 | return DEPRECATED_PHONE_CONSTANT_APN_ALREADY_ACTIVE; |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1482 | } else { |
Chalard Jean | afaed1a | 2019-11-21 14:48:00 +0900 | [diff] [blame] | 1483 | return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED; |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1484 | } |
| 1485 | } |
| 1486 | |
| 1487 | request = requestNetworkForFeatureLocked(netCap); |
| 1488 | } |
| 1489 | if (request != null) { |
Robert Greenwalt | b840173 | 2014-06-20 10:58:45 -0700 | [diff] [blame] | 1490 | Log.d(TAG, "starting startUsingNetworkFeature for request " + request); |
Chalard Jean | afaed1a | 2019-11-21 14:48:00 +0900 | [diff] [blame] | 1491 | return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED; |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1492 | } else { |
| 1493 | Log.d(TAG, " request Failed"); |
Chalard Jean | afaed1a | 2019-11-21 14:48:00 +0900 | [diff] [blame] | 1494 | return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | /** |
| 1499 | * Tells the underlying networking system that the caller is finished |
| 1500 | * using the named feature. The interpretation of {@code feature} |
| 1501 | * is completely up to each networking implementation. |
Lorenzo Colitti | 0b40c87 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 1502 | * |
| 1503 | * <p>This method requires the caller to hold either the |
| 1504 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 1505 | * or the ability to modify system settings as determined by |
| 1506 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 1507 | * |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1508 | * @param networkType specifies which network the request pertains to |
| 1509 | * @param feature the name of the feature that is no longer needed |
| 1510 | * @return an integer value representing the outcome of the request. |
| 1511 | * The interpretation of this value is specific to each networking |
| 1512 | * implementation+feature combination, except that the value {@code -1} |
| 1513 | * always indicates failure. |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1514 | * |
Lorenzo Colitti | 15874cd | 2016-04-13 22:00:02 +0900 | [diff] [blame] | 1515 | * @deprecated Deprecated in favor of the cleaner |
| 1516 | * {@link #unregisterNetworkCallback(NetworkCallback)} API. |
Dianne Hackborn | 18c1d83 | 2015-07-31 10:35:34 -0700 | [diff] [blame] | 1517 | * 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] | 1518 | * throw {@code UnsupportedOperationException} if called. |
Lorenzo Colitti | 84d7f07 | 2016-12-09 18:39:30 +0900 | [diff] [blame] | 1519 | * @removed |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1520 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 1521 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1522 | public int stopUsingNetworkFeature(int networkType, String feature) { |
Lorenzo Colitti | 2780e4b | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1523 | checkLegacyRoutingApiAccess(); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1524 | NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature); |
| 1525 | if (netCap == null) { |
| 1526 | Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " + |
| 1527 | feature); |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1528 | return -1; |
| 1529 | } |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1530 | |
Paul Jensen | 49f74a3 | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1531 | if (removeRequestForFeature(netCap)) { |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1532 | Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1533 | } |
| 1534 | return 1; |
| 1535 | } |
| 1536 | |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 1537 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1538 | private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) { |
| 1539 | if (networkType == TYPE_MOBILE) { |
Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 1540 | switch (feature) { |
| 1541 | case "enableCBS": |
| 1542 | return networkCapabilitiesForType(TYPE_MOBILE_CBS); |
| 1543 | case "enableDUN": |
| 1544 | case "enableDUNAlways": |
| 1545 | return networkCapabilitiesForType(TYPE_MOBILE_DUN); |
| 1546 | case "enableFOTA": |
| 1547 | return networkCapabilitiesForType(TYPE_MOBILE_FOTA); |
| 1548 | case "enableHIPRI": |
| 1549 | return networkCapabilitiesForType(TYPE_MOBILE_HIPRI); |
| 1550 | case "enableIMS": |
| 1551 | return networkCapabilitiesForType(TYPE_MOBILE_IMS); |
| 1552 | case "enableMMS": |
| 1553 | return networkCapabilitiesForType(TYPE_MOBILE_MMS); |
| 1554 | case "enableSUPL": |
| 1555 | return networkCapabilitiesForType(TYPE_MOBILE_SUPL); |
| 1556 | default: |
| 1557 | return null; |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1558 | } |
Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 1559 | } else if (networkType == TYPE_WIFI && "p2p".equals(feature)) { |
| 1560 | return networkCapabilitiesForType(TYPE_WIFI_P2P); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1561 | } |
| 1562 | return null; |
| 1563 | } |
| 1564 | |
Robert Greenwalt | 802c110 | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 1565 | private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) { |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1566 | if (netCap == null) return TYPE_NONE; |
| 1567 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) { |
| 1568 | return TYPE_MOBILE_CBS; |
| 1569 | } |
| 1570 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) { |
| 1571 | return TYPE_MOBILE_IMS; |
| 1572 | } |
| 1573 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) { |
| 1574 | return TYPE_MOBILE_FOTA; |
| 1575 | } |
| 1576 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) { |
| 1577 | return TYPE_MOBILE_DUN; |
| 1578 | } |
| 1579 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) { |
| 1580 | return TYPE_MOBILE_SUPL; |
| 1581 | } |
| 1582 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) { |
| 1583 | return TYPE_MOBILE_MMS; |
| 1584 | } |
| 1585 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) { |
| 1586 | return TYPE_MOBILE_HIPRI; |
| 1587 | } |
Robert Greenwalt | 802c110 | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 1588 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) { |
| 1589 | return TYPE_WIFI_P2P; |
| 1590 | } |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1591 | return TYPE_NONE; |
| 1592 | } |
| 1593 | |
| 1594 | private static class LegacyRequest { |
| 1595 | NetworkCapabilities networkCapabilities; |
| 1596 | NetworkRequest networkRequest; |
| 1597 | int expireSequenceNumber; |
| 1598 | Network currentNetwork; |
| 1599 | int delay = -1; |
Paul Jensen | 49f74a3 | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1600 | |
| 1601 | private void clearDnsBinding() { |
| 1602 | if (currentNetwork != null) { |
| 1603 | currentNetwork = null; |
| 1604 | setProcessDefaultNetworkForHostResolution(null); |
| 1605 | } |
| 1606 | } |
| 1607 | |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1608 | NetworkCallback networkCallback = new NetworkCallback() { |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1609 | @Override |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1610 | public void onAvailable(Network network) { |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1611 | currentNetwork = network; |
| 1612 | Log.d(TAG, "startUsingNetworkFeature got Network:" + network); |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 1613 | setProcessDefaultNetworkForHostResolution(network); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1614 | } |
| 1615 | @Override |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1616 | public void onLost(Network network) { |
Paul Jensen | 49f74a3 | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1617 | if (network.equals(currentNetwork)) clearDnsBinding(); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1618 | Log.d(TAG, "startUsingNetworkFeature lost Network:" + network); |
| 1619 | } |
| 1620 | }; |
| 1621 | } |
| 1622 | |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 1623 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 1624 | private static final HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests = |
| 1625 | new HashMap<>(); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1626 | |
| 1627 | private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) { |
| 1628 | synchronized (sLegacyRequests) { |
| 1629 | LegacyRequest l = sLegacyRequests.get(netCap); |
| 1630 | if (l != null) return l.networkRequest; |
| 1631 | } |
| 1632 | return null; |
| 1633 | } |
| 1634 | |
| 1635 | private void renewRequestLocked(LegacyRequest l) { |
| 1636 | l.expireSequenceNumber++; |
| 1637 | Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber); |
| 1638 | sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay); |
| 1639 | } |
| 1640 | |
| 1641 | private void expireRequest(NetworkCapabilities netCap, int sequenceNum) { |
| 1642 | int ourSeqNum = -1; |
| 1643 | synchronized (sLegacyRequests) { |
| 1644 | LegacyRequest l = sLegacyRequests.get(netCap); |
| 1645 | if (l == null) return; |
| 1646 | ourSeqNum = l.expireSequenceNumber; |
Paul Jensen | 49f74a3 | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1647 | if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1648 | } |
| 1649 | Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum); |
| 1650 | } |
| 1651 | |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 1652 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1653 | private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) { |
| 1654 | int delay = -1; |
Robert Greenwalt | 802c110 | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 1655 | int type = legacyTypeForNetworkCapabilities(netCap); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1656 | try { |
| 1657 | delay = mService.getRestoreDefaultNetworkDelay(type); |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1658 | } catch (RemoteException e) { |
| 1659 | throw e.rethrowFromSystemServer(); |
| 1660 | } |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1661 | LegacyRequest l = new LegacyRequest(); |
| 1662 | l.networkCapabilities = netCap; |
| 1663 | l.delay = delay; |
| 1664 | l.expireSequenceNumber = 0; |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 1665 | l.networkRequest = sendRequestForNetwork( |
| 1666 | netCap, l.networkCallback, 0, REQUEST, type, getDefaultHandler()); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1667 | if (l.networkRequest == null) return null; |
| 1668 | sLegacyRequests.put(netCap, l); |
| 1669 | sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay); |
| 1670 | return l.networkRequest; |
| 1671 | } |
| 1672 | |
| 1673 | private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) { |
| 1674 | if (delay >= 0) { |
| 1675 | Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay); |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 1676 | CallbackHandler handler = getDefaultHandler(); |
Hugo Benichi | bc4ac97 | 2017-02-03 14:18:44 +0900 | [diff] [blame] | 1677 | Message msg = handler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap); |
| 1678 | handler.sendMessageDelayed(msg, delay); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1679 | } |
| 1680 | } |
| 1681 | |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 1682 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Paul Jensen | 49f74a3 | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1683 | private boolean removeRequestForFeature(NetworkCapabilities netCap) { |
| 1684 | final LegacyRequest l; |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1685 | synchronized (sLegacyRequests) { |
Paul Jensen | 49f74a3 | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1686 | l = sLegacyRequests.remove(netCap); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1687 | } |
Paul Jensen | 49f74a3 | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1688 | if (l == null) return false; |
| 1689 | unregisterNetworkCallback(l.networkCallback); |
| 1690 | l.clearDnsBinding(); |
| 1691 | return true; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1692 | } |
| 1693 | |
Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 1694 | private static final SparseIntArray sLegacyTypeToTransport = new SparseIntArray(); |
| 1695 | static { |
| 1696 | sLegacyTypeToTransport.put(TYPE_MOBILE, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1697 | sLegacyTypeToTransport.put(TYPE_MOBILE_CBS, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1698 | sLegacyTypeToTransport.put(TYPE_MOBILE_DUN, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1699 | sLegacyTypeToTransport.put(TYPE_MOBILE_FOTA, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1700 | sLegacyTypeToTransport.put(TYPE_MOBILE_HIPRI, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1701 | sLegacyTypeToTransport.put(TYPE_MOBILE_IMS, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1702 | sLegacyTypeToTransport.put(TYPE_MOBILE_MMS, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1703 | sLegacyTypeToTransport.put(TYPE_MOBILE_SUPL, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1704 | sLegacyTypeToTransport.put(TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI); |
| 1705 | sLegacyTypeToTransport.put(TYPE_WIFI_P2P, NetworkCapabilities.TRANSPORT_WIFI); |
| 1706 | sLegacyTypeToTransport.put(TYPE_BLUETOOTH, NetworkCapabilities.TRANSPORT_BLUETOOTH); |
| 1707 | sLegacyTypeToTransport.put(TYPE_ETHERNET, NetworkCapabilities.TRANSPORT_ETHERNET); |
| 1708 | } |
| 1709 | |
| 1710 | private static final SparseIntArray sLegacyTypeToCapability = new SparseIntArray(); |
| 1711 | static { |
| 1712 | sLegacyTypeToCapability.put(TYPE_MOBILE_CBS, NetworkCapabilities.NET_CAPABILITY_CBS); |
| 1713 | sLegacyTypeToCapability.put(TYPE_MOBILE_DUN, NetworkCapabilities.NET_CAPABILITY_DUN); |
| 1714 | sLegacyTypeToCapability.put(TYPE_MOBILE_FOTA, NetworkCapabilities.NET_CAPABILITY_FOTA); |
| 1715 | sLegacyTypeToCapability.put(TYPE_MOBILE_IMS, NetworkCapabilities.NET_CAPABILITY_IMS); |
| 1716 | sLegacyTypeToCapability.put(TYPE_MOBILE_MMS, NetworkCapabilities.NET_CAPABILITY_MMS); |
| 1717 | sLegacyTypeToCapability.put(TYPE_MOBILE_SUPL, NetworkCapabilities.NET_CAPABILITY_SUPL); |
| 1718 | sLegacyTypeToCapability.put(TYPE_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P); |
| 1719 | } |
| 1720 | |
| 1721 | /** |
| 1722 | * Given a legacy type (TYPE_WIFI, ...) returns a NetworkCapabilities |
| 1723 | * instance suitable for registering a request or callback. Throws an |
| 1724 | * IllegalArgumentException if no mapping from the legacy type to |
| 1725 | * NetworkCapabilities is known. |
| 1726 | * |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 1727 | * @deprecated Types are deprecated. Use {@link NetworkCallback} or {@link NetworkRequest} |
| 1728 | * to find the network instead. |
Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 1729 | * @hide |
| 1730 | */ |
| 1731 | public static NetworkCapabilities networkCapabilitiesForType(int type) { |
| 1732 | final NetworkCapabilities nc = new NetworkCapabilities(); |
| 1733 | |
| 1734 | // Map from type to transports. |
| 1735 | final int NOT_FOUND = -1; |
| 1736 | final int transport = sLegacyTypeToTransport.get(type, NOT_FOUND); |
Hugo Benichi | bc1104b | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 1737 | Preconditions.checkArgument(transport != NOT_FOUND, "unknown legacy type: " + type); |
Erik Kline | ce55eb1 | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 1738 | nc.addTransportType(transport); |
| 1739 | |
| 1740 | // Map from type to capabilities. |
| 1741 | nc.addCapability(sLegacyTypeToCapability.get( |
| 1742 | type, NetworkCapabilities.NET_CAPABILITY_INTERNET)); |
| 1743 | nc.maybeMarkCapabilitiesRestricted(); |
| 1744 | return nc; |
| 1745 | } |
| 1746 | |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1747 | /** @hide */ |
| 1748 | public static class PacketKeepaliveCallback { |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 1749 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Artur Satayev | 56cb6bb | 2019-11-04 17:50:59 +0000 | [diff] [blame] | 1750 | public PacketKeepaliveCallback() { |
| 1751 | } |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1752 | /** The requested keepalive was successfully started. */ |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 1753 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1754 | public void onStarted() {} |
| 1755 | /** The keepalive was successfully stopped. */ |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 1756 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1757 | public void onStopped() {} |
| 1758 | /** An error occurred. */ |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 1759 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1760 | public void onError(int error) {} |
| 1761 | } |
| 1762 | |
| 1763 | /** |
| 1764 | * Allows applications to request that the system periodically send specific packets on their |
| 1765 | * behalf, using hardware offload to save battery power. |
| 1766 | * |
| 1767 | * To request that the system send keepalives, call one of the methods that return a |
| 1768 | * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive}, |
| 1769 | * passing in a non-null callback. If the callback is successfully started, the callback's |
| 1770 | * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called, |
| 1771 | * specifying one of the {@code ERROR_*} constants in this class. |
| 1772 | * |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 1773 | * To stop an existing keepalive, call {@link PacketKeepalive#stop}. The system will call |
| 1774 | * {@link PacketKeepaliveCallback#onStopped} if the operation was successful or |
| 1775 | * {@link PacketKeepaliveCallback#onError} if an error occurred. |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1776 | * |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 1777 | * @deprecated Use {@link SocketKeepalive} instead. |
| 1778 | * |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1779 | * @hide |
| 1780 | */ |
| 1781 | public class PacketKeepalive { |
| 1782 | |
| 1783 | private static final String TAG = "PacketKeepalive"; |
| 1784 | |
| 1785 | /** @hide */ |
| 1786 | public static final int SUCCESS = 0; |
| 1787 | |
| 1788 | /** @hide */ |
| 1789 | public static final int NO_KEEPALIVE = -1; |
| 1790 | |
| 1791 | /** @hide */ |
| 1792 | public static final int BINDER_DIED = -10; |
| 1793 | |
| 1794 | /** The specified {@code Network} is not connected. */ |
| 1795 | public static final int ERROR_INVALID_NETWORK = -20; |
| 1796 | /** The specified IP addresses are invalid. For example, the specified source IP address is |
| 1797 | * not configured on the specified {@code Network}. */ |
| 1798 | public static final int ERROR_INVALID_IP_ADDRESS = -21; |
| 1799 | /** The requested port is invalid. */ |
| 1800 | public static final int ERROR_INVALID_PORT = -22; |
| 1801 | /** The packet length is invalid (e.g., too long). */ |
| 1802 | public static final int ERROR_INVALID_LENGTH = -23; |
| 1803 | /** The packet transmission interval is invalid (e.g., too short). */ |
| 1804 | public static final int ERROR_INVALID_INTERVAL = -24; |
| 1805 | |
| 1806 | /** The hardware does not support this request. */ |
| 1807 | public static final int ERROR_HARDWARE_UNSUPPORTED = -30; |
Lorenzo Colitti | 723f82f | 2015-09-08 16:46:36 +0900 | [diff] [blame] | 1808 | /** The hardware returned an error. */ |
| 1809 | public static final int ERROR_HARDWARE_ERROR = -31; |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1810 | |
Nathan Harold | 0990bc8 | 2018-02-14 13:09:45 -0800 | [diff] [blame] | 1811 | /** The NAT-T destination port for IPsec */ |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1812 | public static final int NATT_PORT = 4500; |
| 1813 | |
Nathan Harold | 0990bc8 | 2018-02-14 13:09:45 -0800 | [diff] [blame] | 1814 | /** The minimum interval in seconds between keepalive packet transmissions */ |
| 1815 | public static final int MIN_INTERVAL = 10; |
| 1816 | |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1817 | private final Network mNetwork; |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 1818 | private final ISocketKeepaliveCallback mCallback; |
| 1819 | private final ExecutorService mExecutor; |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1820 | |
| 1821 | private volatile Integer mSlot; |
| 1822 | |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 1823 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1824 | public void stop() { |
| 1825 | try { |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 1826 | mExecutor.execute(() -> { |
| 1827 | try { |
| 1828 | if (mSlot != null) { |
| 1829 | mService.stopKeepalive(mNetwork, mSlot); |
| 1830 | } |
| 1831 | } catch (RemoteException e) { |
| 1832 | Log.e(TAG, "Error stopping packet keepalive: ", e); |
| 1833 | throw e.rethrowFromSystemServer(); |
| 1834 | } |
| 1835 | }); |
| 1836 | } catch (RejectedExecutionException e) { |
| 1837 | // The internal executor has already stopped due to previous event. |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1838 | } |
| 1839 | } |
| 1840 | |
| 1841 | private PacketKeepalive(Network network, PacketKeepaliveCallback callback) { |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 1842 | Preconditions.checkNotNull(network, "network cannot be null"); |
| 1843 | Preconditions.checkNotNull(callback, "callback cannot be null"); |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1844 | mNetwork = network; |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 1845 | mExecutor = Executors.newSingleThreadExecutor(); |
| 1846 | mCallback = new ISocketKeepaliveCallback.Stub() { |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1847 | @Override |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 1848 | public void onStarted(int slot) { |
lucaslin | be80138 | 2020-12-30 11:54:55 +0800 | [diff] [blame] | 1849 | final long token = Binder.clearCallingIdentity(); |
| 1850 | try { |
| 1851 | mExecutor.execute(() -> { |
| 1852 | mSlot = slot; |
| 1853 | callback.onStarted(); |
| 1854 | }); |
| 1855 | } finally { |
| 1856 | Binder.restoreCallingIdentity(token); |
| 1857 | } |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1858 | } |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 1859 | |
| 1860 | @Override |
| 1861 | public void onStopped() { |
lucaslin | be80138 | 2020-12-30 11:54:55 +0800 | [diff] [blame] | 1862 | final long token = Binder.clearCallingIdentity(); |
| 1863 | try { |
| 1864 | mExecutor.execute(() -> { |
| 1865 | mSlot = null; |
| 1866 | callback.onStopped(); |
| 1867 | }); |
| 1868 | } finally { |
| 1869 | Binder.restoreCallingIdentity(token); |
| 1870 | } |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 1871 | mExecutor.shutdown(); |
| 1872 | } |
| 1873 | |
| 1874 | @Override |
| 1875 | public void onError(int error) { |
lucaslin | be80138 | 2020-12-30 11:54:55 +0800 | [diff] [blame] | 1876 | final long token = Binder.clearCallingIdentity(); |
| 1877 | try { |
| 1878 | mExecutor.execute(() -> { |
| 1879 | mSlot = null; |
| 1880 | callback.onError(error); |
| 1881 | }); |
| 1882 | } finally { |
| 1883 | Binder.restoreCallingIdentity(token); |
| 1884 | } |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 1885 | mExecutor.shutdown(); |
| 1886 | } |
| 1887 | |
| 1888 | @Override |
| 1889 | public void onDataReceived() { |
| 1890 | // PacketKeepalive is only used for Nat-T keepalive and as such does not invoke |
| 1891 | // this callback when data is received. |
| 1892 | } |
| 1893 | }; |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1894 | } |
| 1895 | } |
| 1896 | |
| 1897 | /** |
| 1898 | * Starts an IPsec NAT-T keepalive packet with the specified parameters. |
| 1899 | * |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 1900 | * @deprecated Use {@link #createSocketKeepalive} instead. |
| 1901 | * |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1902 | * @hide |
| 1903 | */ |
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 PacketKeepalive startNattKeepalive( |
| 1906 | Network network, int intervalSeconds, PacketKeepaliveCallback callback, |
| 1907 | InetAddress srcAddr, int srcPort, InetAddress dstAddr) { |
| 1908 | final PacketKeepalive k = new PacketKeepalive(network, callback); |
| 1909 | try { |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 1910 | mService.startNattKeepalive(network, intervalSeconds, k.mCallback, |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1911 | srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress()); |
| 1912 | } catch (RemoteException e) { |
| 1913 | Log.e(TAG, "Error starting packet keepalive: ", e); |
junyulai | 070f9ff | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 1914 | throw e.rethrowFromSystemServer(); |
Lorenzo Colitti | 0b798a8 | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1915 | } |
| 1916 | return k; |
| 1917 | } |
| 1918 | |
Chiachang Wang | 619319a | 2021-01-15 11:06:21 +0800 | [diff] [blame] | 1919 | // Construct an invalid fd. |
| 1920 | private ParcelFileDescriptor createInvalidFd() { |
| 1921 | final int invalidFd = -1; |
| 1922 | return ParcelFileDescriptor.adoptFd(invalidFd); |
| 1923 | } |
| 1924 | |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1925 | /** |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 1926 | * Request that keepalives be started on a IPsec NAT-T socket. |
| 1927 | * |
| 1928 | * @param network The {@link Network} the socket is on. |
| 1929 | * @param socket The socket that needs to be kept alive. |
| 1930 | * @param source The source address of the {@link UdpEncapsulationSocket}. |
| 1931 | * @param destination The destination address of the {@link UdpEncapsulationSocket}. |
| 1932 | * @param executor The executor on which callback will be invoked. The provided {@link Executor} |
| 1933 | * must run callback sequentially, otherwise the order of callbacks cannot be |
| 1934 | * guaranteed. |
| 1935 | * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive |
| 1936 | * changes. Must be extended by applications that use this API. |
| 1937 | * |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 1938 | * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the |
| 1939 | * given socket. |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 1940 | **/ |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 1941 | public @NonNull SocketKeepalive createSocketKeepalive(@NonNull Network network, |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 1942 | @NonNull UdpEncapsulationSocket socket, |
| 1943 | @NonNull InetAddress source, |
| 1944 | @NonNull InetAddress destination, |
| 1945 | @NonNull @CallbackExecutor Executor executor, |
| 1946 | @NonNull Callback callback) { |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 1947 | ParcelFileDescriptor dup; |
| 1948 | try { |
junyulai | 828dad1 | 2019-03-27 11:00:37 +0800 | [diff] [blame] | 1949 | // Dup is needed here as the pfd inside the socket is owned by the IpSecService, |
| 1950 | // which cannot be obtained by the app process. |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 1951 | dup = ParcelFileDescriptor.dup(socket.getFileDescriptor()); |
| 1952 | } catch (IOException ignored) { |
| 1953 | // Construct an invalid fd, so that if the user later calls start(), it will fail with |
| 1954 | // ERROR_INVALID_SOCKET. |
Chiachang Wang | 619319a | 2021-01-15 11:06:21 +0800 | [diff] [blame] | 1955 | dup = createInvalidFd(); |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 1956 | } |
| 1957 | return new NattSocketKeepalive(mService, network, dup, socket.getResourceId(), source, |
| 1958 | destination, executor, callback); |
junyulai | d05a192 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | /** |
| 1962 | * Request that keepalives be started on a IPsec NAT-T socket file descriptor. Directly called |
| 1963 | * by system apps which don't use IpSecService to create {@link UdpEncapsulationSocket}. |
| 1964 | * |
| 1965 | * @param network The {@link Network} the socket is on. |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 1966 | * @param pfd The {@link ParcelFileDescriptor} that needs to be kept alive. The provided |
| 1967 | * {@link ParcelFileDescriptor} must be bound to a port and the keepalives will be sent |
| 1968 | * from that port. |
junyulai | d05a192 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 1969 | * @param source The source address of the {@link UdpEncapsulationSocket}. |
| 1970 | * @param destination The destination address of the {@link UdpEncapsulationSocket}. The |
| 1971 | * keepalive packets will always be sent to port 4500 of the given {@code destination}. |
| 1972 | * @param executor The executor on which callback will be invoked. The provided {@link Executor} |
| 1973 | * must run callback sequentially, otherwise the order of callbacks cannot be |
| 1974 | * guaranteed. |
| 1975 | * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive |
| 1976 | * changes. Must be extended by applications that use this API. |
| 1977 | * |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 1978 | * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the |
| 1979 | * given socket. |
junyulai | d05a192 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 1980 | * @hide |
| 1981 | */ |
| 1982 | @SystemApi |
| 1983 | @RequiresPermission(android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD) |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 1984 | public @NonNull SocketKeepalive createNattKeepalive(@NonNull Network network, |
| 1985 | @NonNull ParcelFileDescriptor pfd, |
junyulai | d05a192 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 1986 | @NonNull InetAddress source, |
| 1987 | @NonNull InetAddress destination, |
| 1988 | @NonNull @CallbackExecutor Executor executor, |
| 1989 | @NonNull Callback callback) { |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 1990 | ParcelFileDescriptor dup; |
| 1991 | try { |
junyulai | 828dad1 | 2019-03-27 11:00:37 +0800 | [diff] [blame] | 1992 | // TODO: Consider remove unnecessary dup. |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 1993 | dup = pfd.dup(); |
| 1994 | } catch (IOException ignored) { |
| 1995 | // Construct an invalid fd, so that if the user later calls start(), it will fail with |
| 1996 | // ERROR_INVALID_SOCKET. |
Chiachang Wang | 619319a | 2021-01-15 11:06:21 +0800 | [diff] [blame] | 1997 | dup = createInvalidFd(); |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 1998 | } |
| 1999 | return new NattSocketKeepalive(mService, network, dup, |
| 2000 | INVALID_RESOURCE_ID /* Unused */, source, destination, executor, callback); |
junyulai | 4c95b08 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 2001 | } |
| 2002 | |
| 2003 | /** |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 2004 | * Request that keepalives be started on a TCP socket. |
| 2005 | * The socket must be established. |
| 2006 | * |
| 2007 | * @param network The {@link Network} the socket is on. |
| 2008 | * @param socket The socket that needs to be kept alive. |
| 2009 | * @param executor The executor on which callback will be invoked. This implementation assumes |
| 2010 | * the provided {@link Executor} runs the callbacks in sequence with no |
| 2011 | * concurrency. Failing this, no guarantee of correctness can be made. It is |
| 2012 | * the responsibility of the caller to ensure the executor provides this |
| 2013 | * guarantee. A simple way of creating such an executor is with the standard |
| 2014 | * tool {@code Executors.newSingleThreadExecutor}. |
| 2015 | * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive |
| 2016 | * changes. Must be extended by applications that use this API. |
| 2017 | * |
| 2018 | * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the |
| 2019 | * given socket. |
| 2020 | * @hide |
| 2021 | */ |
| 2022 | @SystemApi |
| 2023 | @RequiresPermission(android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD) |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 2024 | public @NonNull SocketKeepalive createSocketKeepalive(@NonNull Network network, |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 2025 | @NonNull Socket socket, |
| 2026 | @NonNull Executor executor, |
| 2027 | @NonNull Callback callback) { |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 2028 | ParcelFileDescriptor dup; |
| 2029 | try { |
| 2030 | dup = ParcelFileDescriptor.fromSocket(socket); |
| 2031 | } catch (UncheckedIOException ignored) { |
| 2032 | // Construct an invalid fd, so that if the user later calls start(), it will fail with |
| 2033 | // ERROR_INVALID_SOCKET. |
Chiachang Wang | 619319a | 2021-01-15 11:06:21 +0800 | [diff] [blame] | 2034 | dup = createInvalidFd(); |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 2035 | } |
| 2036 | return new TcpSocketKeepalive(mService, network, dup, executor, callback); |
junyulai | 0835a1e | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | /** |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2040 | * Ensure that a network route exists to deliver traffic to the specified |
| 2041 | * host via the specified network interface. An attempt to add a route that |
| 2042 | * already exists is ignored, but treated as successful. |
Lorenzo Colitti | 0b40c87 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 2043 | * |
| 2044 | * <p>This method requires the caller to hold either the |
| 2045 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2046 | * or the ability to modify system settings as determined by |
| 2047 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 2048 | * |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2049 | * @param networkType the type of the network over which traffic to the specified |
| 2050 | * host is to be routed |
| 2051 | * @param hostAddress the IP address of the host to which the route is desired |
| 2052 | * @return {@code true} on success, {@code false} on failure |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2053 | * |
Lorenzo Colitti | 7f6a2bf | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 2054 | * @deprecated Deprecated in favor of the |
| 2055 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, |
| 2056 | * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API. |
Dianne Hackborn | 18c1d83 | 2015-07-31 10:35:34 -0700 | [diff] [blame] | 2057 | * 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] | 2058 | * throw {@code UnsupportedOperationException} if called. |
Lorenzo Colitti | 84d7f07 | 2016-12-09 18:39:30 +0900 | [diff] [blame] | 2059 | * @removed |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2060 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 2061 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2062 | public boolean requestRouteToHost(int networkType, int hostAddress) { |
Sreeram Ramachandran | c06ec73 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 2063 | return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress)); |
Robert Greenwalt | 7fe44cb | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 2064 | } |
| 2065 | |
| 2066 | /** |
| 2067 | * Ensure that a network route exists to deliver traffic to the specified |
| 2068 | * host via the specified network interface. An attempt to add a route that |
| 2069 | * already exists is ignored, but treated as successful. |
Lorenzo Colitti | 0b40c87 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 2070 | * |
| 2071 | * <p>This method requires the caller to hold either the |
| 2072 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2073 | * or the ability to modify system settings as determined by |
| 2074 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 2075 | * |
Robert Greenwalt | 7fe44cb | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 2076 | * @param networkType the type of the network over which traffic to the specified |
| 2077 | * host is to be routed |
| 2078 | * @param hostAddress the IP address of the host to which the route is desired |
| 2079 | * @return {@code true} on success, {@code false} on failure |
| 2080 | * @hide |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2081 | * @deprecated Deprecated in favor of the {@link #requestNetwork} and |
Lorenzo Colitti | 7f6a2bf | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 2082 | * {@link #bindProcessToNetwork} API. |
Robert Greenwalt | 7fe44cb | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 2083 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 2084 | @Deprecated |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2085 | @UnsupportedAppUsage |
Robert Greenwalt | 7fe44cb | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 2086 | public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) { |
Lorenzo Colitti | 2780e4b | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 2087 | checkLegacyRoutingApiAccess(); |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2088 | try { |
Philip P. Moltmann | 7bc33df | 2020-03-26 11:50:35 -0700 | [diff] [blame] | 2089 | return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress(), |
| 2090 | mContext.getOpPackageName(), getAttributionTag()); |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2091 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2092 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2093 | } |
| 2094 | } |
| 2095 | |
| 2096 | /** |
Philip P. Moltmann | 7bc33df | 2020-03-26 11:50:35 -0700 | [diff] [blame] | 2097 | * @return the context's attribution tag |
| 2098 | */ |
| 2099 | // TODO: Remove method and replace with direct call once R code is pushed to AOSP |
| 2100 | private @Nullable String getAttributionTag() { |
Roshan Pius | aa24fde | 2020-12-17 14:53:09 -0800 | [diff] [blame] | 2101 | return mContext.getAttributionTag(); |
Philip P. Moltmann | 7bc33df | 2020-03-26 11:50:35 -0700 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | /** |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2105 | * Returns the value of the setting for background data usage. If false, |
| 2106 | * applications should not use the network if the application is not in the |
| 2107 | * foreground. Developers should respect this setting, and check the value |
| 2108 | * of this before performing any background data operations. |
| 2109 | * <p> |
| 2110 | * All applications that have background services that use the network |
| 2111 | * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}. |
Jeff Sharkey | 39c01eb | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2112 | * <p> |
Scott Main | 5058914 | 2011-10-06 18:32:43 -0700 | [diff] [blame] | 2113 | * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of |
Jeff Sharkey | 39c01eb | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2114 | * background data depends on several combined factors, and this method will |
| 2115 | * always return {@code true}. Instead, when background data is unavailable, |
| 2116 | * {@link #getActiveNetworkInfo()} will now appear disconnected. |
Danica Chang | 9656705 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2117 | * |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2118 | * @return Whether background data usage is allowed. |
| 2119 | */ |
Jeff Sharkey | 39c01eb | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2120 | @Deprecated |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2121 | public boolean getBackgroundDataSetting() { |
Jeff Sharkey | 39c01eb | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2122 | // assume that background data is allowed; final authority is |
| 2123 | // NetworkInfo which may be blocked. |
| 2124 | return true; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2125 | } |
| 2126 | |
| 2127 | /** |
| 2128 | * Sets the value of the setting for background data usage. |
Robert Greenwalt | 1b21f6c | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2129 | * |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2130 | * @param allowBackgroundData Whether an application should use data while |
| 2131 | * it is in the background. |
Robert Greenwalt | 1b21f6c | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2132 | * |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2133 | * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING |
| 2134 | * @see #getBackgroundDataSetting() |
| 2135 | * @hide |
| 2136 | */ |
Jeff Sharkey | 39c01eb | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2137 | @Deprecated |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2138 | @UnsupportedAppUsage |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2139 | public void setBackgroundDataSetting(boolean allowBackgroundData) { |
Jeff Sharkey | 39c01eb | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2140 | // ignored |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2141 | } |
Robert Greenwalt | 1b21f6c | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2142 | |
Jeff Sharkey | 66fa968 | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 2143 | /** |
Robert Greenwalt | 1b21f6c | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2144 | * @hide |
Robert Greenwalt | 0c150c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 2145 | * @deprecated Talk to TelephonyManager directly |
Robert Greenwalt | 1b21f6c | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2146 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 2147 | @Deprecated |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2148 | @UnsupportedAppUsage |
Robert Greenwalt | 1b21f6c | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2149 | public boolean getMobileDataEnabled() { |
Meng Wang | ed6f441 | 2019-11-18 17:10:00 -0800 | [diff] [blame] | 2150 | TelephonyManager tm = mContext.getSystemService(TelephonyManager.class); |
| 2151 | if (tm != null) { |
| 2152 | int subId = SubscriptionManager.getDefaultDataSubscriptionId(); |
| 2153 | Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId); |
| 2154 | boolean retVal = tm.createForSubscriptionId(subId).isDataEnabled(); |
| 2155 | Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId |
| 2156 | + " retVal=" + retVal); |
| 2157 | return retVal; |
Robert Greenwalt | 1b21f6c | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2158 | } |
Wink Saville | 689f704 | 2014-12-05 11:10:30 -0800 | [diff] [blame] | 2159 | Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false"); |
Robert Greenwalt | 0c150c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 2160 | return false; |
Robert Greenwalt | 1b21f6c | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2161 | } |
| 2162 | |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2163 | /** |
Robert Greenwalt | ad35b13 | 2014-09-04 16:44:35 -0700 | [diff] [blame] | 2164 | * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener} |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2165 | * 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] | 2166 | */ |
| 2167 | public interface OnNetworkActiveListener { |
| 2168 | /** |
| 2169 | * Called on the main thread of the process to report that the current data network |
| 2170 | * has become active, and it is now a good time to perform any pending network |
| 2171 | * operations. Note that this listener only tells you when the network becomes |
| 2172 | * active; if at any other time you want to know whether it is active (and thus okay |
| 2173 | * to initiate network traffic), you can retrieve its instantaneous state with |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2174 | * {@link ConnectivityManager#isDefaultNetworkActive}. |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2175 | */ |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 2176 | void onNetworkActive(); |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2177 | } |
| 2178 | |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2179 | private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener> |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 2180 | mNetworkActivityListeners = new ArrayMap<>(); |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2181 | |
| 2182 | /** |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2183 | * Start listening to reports when the system's default data network is active, meaning it is |
| 2184 | * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()} |
| 2185 | * to determine the current state of the system's default network after registering the |
| 2186 | * listener. |
| 2187 | * <p> |
| 2188 | * If the process default network has been set with |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2189 | * {@link ConnectivityManager#bindProcessToNetwork} this function will not |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2190 | * reflect the process's default, but the system default. |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2191 | * |
| 2192 | * @param l The listener to be told when the network is active. |
| 2193 | */ |
Robert Greenwalt | ad35b13 | 2014-09-04 16:44:35 -0700 | [diff] [blame] | 2194 | public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) { |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2195 | INetworkActivityListener rl = new INetworkActivityListener.Stub() { |
| 2196 | @Override |
| 2197 | public void onNetworkActive() throws RemoteException { |
| 2198 | l.onNetworkActive(); |
| 2199 | } |
| 2200 | }; |
| 2201 | |
| 2202 | try { |
lucaslin | 1193a5d | 2021-01-21 02:04:15 +0800 | [diff] [blame] | 2203 | mService.registerNetworkActivityListener(rl); |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2204 | mNetworkActivityListeners.put(l, rl); |
| 2205 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2206 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2207 | } |
| 2208 | } |
| 2209 | |
| 2210 | /** |
| 2211 | * Remove network active listener previously registered with |
Robert Greenwalt | ad35b13 | 2014-09-04 16:44:35 -0700 | [diff] [blame] | 2212 | * {@link #addDefaultNetworkActiveListener}. |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2213 | * |
| 2214 | * @param l Previously registered listener. |
| 2215 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 2216 | public void removeDefaultNetworkActiveListener(@NonNull OnNetworkActiveListener l) { |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2217 | INetworkActivityListener rl = mNetworkActivityListeners.get(l); |
Hugo Benichi | bc1104b | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 2218 | Preconditions.checkArgument(rl != null, "Listener was not registered."); |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2219 | try { |
lucaslin | 1193a5d | 2021-01-21 02:04:15 +0800 | [diff] [blame] | 2220 | mService.registerNetworkActivityListener(rl); |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2221 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2222 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2223 | } |
| 2224 | } |
| 2225 | |
| 2226 | /** |
| 2227 | * Return whether the data network is currently active. An active network means that |
| 2228 | * it is currently in a high power state for performing data transmission. On some |
| 2229 | * types of networks, it may be expensive to move and stay in such a state, so it is |
| 2230 | * more power efficient to batch network traffic together when the radio is already in |
| 2231 | * this state. This method tells you whether right now is currently a good time to |
| 2232 | * initiate network traffic, as the network is already active. |
| 2233 | */ |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2234 | public boolean isDefaultNetworkActive() { |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2235 | try { |
lucaslin | 1193a5d | 2021-01-21 02:04:15 +0800 | [diff] [blame] | 2236 | return mService.isDefaultNetworkActive(); |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2237 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2238 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2239 | } |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2240 | } |
| 2241 | |
| 2242 | /** |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2243 | * {@hide} |
| 2244 | */ |
Lorenzo Colitti | 2780e4b | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 2245 | public ConnectivityManager(Context context, IConnectivityManager service) { |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 2246 | mContext = Preconditions.checkNotNull(context, "missing context"); |
| 2247 | mService = Preconditions.checkNotNull(service, "missing IConnectivityManager"); |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2248 | mTetheringManager = (TetheringManager) mContext.getSystemService(Context.TETHERING_SERVICE); |
Paul Jensen | c0618a6 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 2249 | sInstance = this; |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2250 | } |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2251 | |
Jeff Sharkey | 2d9e5a5 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2252 | /** {@hide} */ |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2253 | @UnsupportedAppUsage |
Jeff Sharkey | 2d9e5a5 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2254 | public static ConnectivityManager from(Context context) { |
| 2255 | return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 2256 | } |
| 2257 | |
Remi NGUYEN VAN | 8539129 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2258 | /** @hide */ |
| 2259 | public NetworkRequest getDefaultRequest() { |
| 2260 | try { |
| 2261 | // This is not racy as the default request is final in ConnectivityService. |
| 2262 | return mService.getDefaultRequest(); |
| 2263 | } catch (RemoteException e) { |
| 2264 | throw e.rethrowFromSystemServer(); |
| 2265 | } |
| 2266 | } |
| 2267 | |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2268 | /** |
Philip P. Moltmann | 7bc33df | 2020-03-26 11:50:35 -0700 | [diff] [blame] | 2269 | * Check if the package is a allowed to write settings. This also accounts that such an access |
| 2270 | * happened. |
| 2271 | * |
| 2272 | * @return {@code true} iff the package is allowed to write settings. |
| 2273 | */ |
| 2274 | // TODO: Remove method and replace with direct call once R code is pushed to AOSP |
| 2275 | private static boolean checkAndNoteWriteSettingsOperation(@NonNull Context context, int uid, |
| 2276 | @NonNull String callingPackage, @Nullable String callingAttributionTag, |
| 2277 | boolean throwException) { |
| 2278 | return Settings.checkAndNoteWriteSettingsOperation(context, uid, callingPackage, |
| 2279 | throwException); |
| 2280 | } |
| 2281 | |
| 2282 | /** |
Paul Jensen | c0618a6 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 2283 | * @deprecated - use getSystemService. This is a kludge to support static access in certain |
| 2284 | * situations where a Context pointer is unavailable. |
| 2285 | * @hide |
| 2286 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 2287 | @Deprecated |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2288 | static ConnectivityManager getInstanceOrNull() { |
| 2289 | return sInstance; |
| 2290 | } |
| 2291 | |
| 2292 | /** |
| 2293 | * @deprecated - use getSystemService. This is a kludge to support static access in certain |
| 2294 | * situations where a Context pointer is unavailable. |
| 2295 | * @hide |
| 2296 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 2297 | @Deprecated |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2298 | @UnsupportedAppUsage |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2299 | private static ConnectivityManager getInstance() { |
| 2300 | if (getInstanceOrNull() == null) { |
Paul Jensen | c0618a6 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 2301 | throw new IllegalStateException("No ConnectivityManager yet constructed"); |
| 2302 | } |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2303 | return getInstanceOrNull(); |
Paul Jensen | c0618a6 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 2304 | } |
| 2305 | |
| 2306 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2307 | * Get the set of tetherable, available interfaces. This list is limited by |
| 2308 | * device configuration and current interface existence. |
| 2309 | * |
| 2310 | * @return an array of 0 or more Strings of tetherable interface names. |
| 2311 | * |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2312 | * @deprecated Use {@link TetheringEventCallback#onTetherableInterfacesChanged(List)} instead. |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2313 | * {@hide} |
| 2314 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 2315 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2316 | @UnsupportedAppUsage |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2317 | @Deprecated |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2318 | public String[] getTetherableIfaces() { |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2319 | return mTetheringManager.getTetherableIfaces(); |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2320 | } |
| 2321 | |
| 2322 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2323 | * Get the set of tethered interfaces. |
| 2324 | * |
| 2325 | * @return an array of 0 or more String of currently tethered interface names. |
| 2326 | * |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2327 | * @deprecated Use {@link TetheringEventCallback#onTetherableInterfacesChanged(List)} instead. |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2328 | * {@hide} |
| 2329 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 2330 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2331 | @UnsupportedAppUsage |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2332 | @Deprecated |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2333 | public String[] getTetheredIfaces() { |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2334 | return mTetheringManager.getTetheredIfaces(); |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2335 | } |
| 2336 | |
| 2337 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2338 | * Get the set of interface names which attempted to tether but |
| 2339 | * failed. Re-attempting to tether may cause them to reset to the Tethered |
| 2340 | * state. Alternatively, causing the interface to be destroyed and recreated |
| 2341 | * may cause them to reset to the available state. |
| 2342 | * {@link ConnectivityManager#getLastTetherError} can be used to get more |
| 2343 | * information on the cause of the errors. |
| 2344 | * |
| 2345 | * @return an array of 0 or more String indicating the interface names |
| 2346 | * which failed to tether. |
| 2347 | * |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2348 | * @deprecated Use {@link TetheringEventCallback#onError(String, int)} instead. |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2349 | * {@hide} |
| 2350 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 2351 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2352 | @UnsupportedAppUsage |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2353 | @Deprecated |
Robert Greenwalt | 4283ded | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2354 | public String[] getTetheringErroredIfaces() { |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2355 | return mTetheringManager.getTetheringErroredIfaces(); |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | /** |
Robert Greenwalt | e594a76 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 2359 | * Get the set of tethered dhcp ranges. |
| 2360 | * |
markchien | 2e6ba52 | 2020-02-14 11:55:48 +0800 | [diff] [blame] | 2361 | * @deprecated This method is not supported. |
| 2362 | * TODO: remove this function when all of clients are removed. |
Robert Greenwalt | e594a76 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 2363 | * {@hide} |
| 2364 | */ |
paulhu | 8e96a75 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 2365 | @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS) |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2366 | @Deprecated |
Robert Greenwalt | e594a76 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 2367 | public String[] getTetheredDhcpRanges() { |
markchien | 2e6ba52 | 2020-02-14 11:55:48 +0800 | [diff] [blame] | 2368 | throw new UnsupportedOperationException("getTetheredDhcpRanges is not supported"); |
Robert Greenwalt | e594a76 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 2369 | } |
| 2370 | |
| 2371 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2372 | * Attempt to tether the named interface. This will setup a dhcp server |
| 2373 | * on the interface, forward and NAT IP packets and forward DNS requests |
| 2374 | * to the best active upstream network interface. Note that if no upstream |
| 2375 | * IP network interface is available, dhcp will still run and traffic will be |
| 2376 | * allowed between the tethered devices and this device, though upstream net |
| 2377 | * access will of course fail until an upstream network interface becomes |
| 2378 | * active. |
Lorenzo Colitti | 0b40c87 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 2379 | * |
| 2380 | * <p>This method requires the caller to hold either the |
| 2381 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2382 | * or the ability to modify system settings as determined by |
| 2383 | * {@link android.provider.Settings.System#canWrite}.</p> |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2384 | * |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2385 | * <p>WARNING: New clients should not use this function. The only usages should be in PanService |
| 2386 | * and WifiStateMachine which need direct access. All other clients should use |
| 2387 | * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning |
| 2388 | * logic.</p> |
| 2389 | * |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2390 | * @param iface the interface name to tether. |
| 2391 | * @return error a {@code TETHER_ERROR} value indicating success or failure type |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2392 | * @deprecated Use {@link TetheringManager#startTethering} instead |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2393 | * |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2394 | * {@hide} |
| 2395 | */ |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 2396 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2397 | @Deprecated |
Robert Greenwalt | 4283ded | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2398 | public int tether(String iface) { |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2399 | return mTetheringManager.tether(iface); |
Robert Greenwalt | 4283ded | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2400 | } |
| 2401 | |
| 2402 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2403 | * Stop tethering the named interface. |
Lorenzo Colitti | 0b40c87 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 2404 | * |
| 2405 | * <p>This method requires the caller to hold either the |
| 2406 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2407 | * or the ability to modify system settings as determined by |
| 2408 | * {@link android.provider.Settings.System#canWrite}.</p> |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2409 | * |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2410 | * <p>WARNING: New clients should not use this function. The only usages should be in PanService |
| 2411 | * and WifiStateMachine which need direct access. All other clients should use |
| 2412 | * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning |
| 2413 | * logic.</p> |
| 2414 | * |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2415 | * @param iface the interface name to untether. |
| 2416 | * @return error a {@code TETHER_ERROR} value indicating success or failure type |
| 2417 | * |
Robert Greenwalt | 4283ded | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2418 | * {@hide} |
| 2419 | */ |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2420 | @UnsupportedAppUsage |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2421 | @Deprecated |
Robert Greenwalt | 4283ded | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2422 | public int untether(String iface) { |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2423 | return mTetheringManager.untether(iface); |
Robert Greenwalt | 0c4828c | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2424 | } |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2425 | |
| 2426 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2427 | * Check if the device allows for tethering. It may be disabled via |
Dianne Hackborn | 5ac8816 | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2428 | * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2429 | * due to device configuration. |
| 2430 | * |
Chalard Jean | ffacaef | 2017-09-26 15:45:18 +0900 | [diff] [blame] | 2431 | * <p>If this app does not have permission to use this API, it will always |
| 2432 | * return false rather than throw an exception.</p> |
| 2433 | * |
| 2434 | * <p>If the device has a hotspot provisioning app, the caller is required to hold the |
| 2435 | * {@link android.Manifest.permission.TETHER_PRIVILEGED} permission.</p> |
| 2436 | * |
| 2437 | * <p>Otherwise, this method requires the caller to hold the ability to modify system |
| 2438 | * settings as determined by {@link android.provider.Settings.System#canWrite}.</p> |
| 2439 | * |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2440 | * @return a boolean - {@code true} indicating Tethering is supported. |
| 2441 | * |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2442 | * @deprecated Use {@link TetheringEventCallback#onTetheringSupported(boolean)} instead. |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2443 | * {@hide} |
| 2444 | */ |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2445 | @SystemApi |
Chalard Jean | ffacaef | 2017-09-26 15:45:18 +0900 | [diff] [blame] | 2446 | @RequiresPermission(anyOf = {android.Manifest.permission.TETHER_PRIVILEGED, |
| 2447 | android.Manifest.permission.WRITE_SETTINGS}) |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2448 | public boolean isTetheringSupported() { |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2449 | return mTetheringManager.isTetheringSupported(); |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2450 | } |
| 2451 | |
| 2452 | /** |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2453 | * Callback for use with {@link #startTethering} to find out whether tethering succeeded. |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2454 | * |
| 2455 | * @deprecated Use {@link TetheringManager.StartTetheringCallback} instead. |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2456 | * @hide |
| 2457 | */ |
| 2458 | @SystemApi |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2459 | @Deprecated |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2460 | public static abstract class OnStartTetheringCallback { |
| 2461 | /** |
| 2462 | * Called when tethering has been successfully started. |
| 2463 | */ |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 2464 | public void onTetheringStarted() {} |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2465 | |
| 2466 | /** |
| 2467 | * Called when starting tethering failed. |
| 2468 | */ |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 2469 | public void onTetheringFailed() {} |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2470 | } |
| 2471 | |
| 2472 | /** |
| 2473 | * Convenient overload for |
| 2474 | * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null |
| 2475 | * handler to run on the current thread's {@link Looper}. |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2476 | * |
| 2477 | * @deprecated Use {@link TetheringManager#startTethering} instead. |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2478 | * @hide |
| 2479 | */ |
| 2480 | @SystemApi |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2481 | @Deprecated |
Udam Saini | 8f7d6a7 | 2017-06-07 12:06:28 -0700 | [diff] [blame] | 2482 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2483 | public void startTethering(int type, boolean showProvisioningUi, |
| 2484 | final OnStartTetheringCallback callback) { |
| 2485 | startTethering(type, showProvisioningUi, callback, null); |
| 2486 | } |
| 2487 | |
| 2488 | /** |
| 2489 | * Runs tether provisioning for the given type if needed and then starts tethering if |
| 2490 | * the check succeeds. If no carrier provisioning is required for tethering, tethering is |
| 2491 | * enabled immediately. If provisioning fails, tethering will not be enabled. It also |
| 2492 | * schedules tether provisioning re-checks if appropriate. |
| 2493 | * |
| 2494 | * @param type The type of tethering to start. Must be one of |
| 2495 | * {@link ConnectivityManager.TETHERING_WIFI}, |
| 2496 | * {@link ConnectivityManager.TETHERING_USB}, or |
| 2497 | * {@link ConnectivityManager.TETHERING_BLUETOOTH}. |
| 2498 | * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there |
| 2499 | * is one. This should be true the first time this function is called and also any time |
| 2500 | * the user can see this UI. It gives users information from their carrier about the |
| 2501 | * check failing and how they can sign up for tethering if possible. |
| 2502 | * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller |
| 2503 | * of the result of trying to tether. |
| 2504 | * @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] | 2505 | * |
| 2506 | * @deprecated Use {@link TetheringManager#startTethering} instead. |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2507 | * @hide |
| 2508 | */ |
| 2509 | @SystemApi |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2510 | @Deprecated |
Jeff Sharkey | 9b39e9a | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 2511 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2512 | public void startTethering(int type, boolean showProvisioningUi, |
| 2513 | final OnStartTetheringCallback callback, Handler handler) { |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 2514 | Preconditions.checkNotNull(callback, "OnStartTetheringCallback cannot be null."); |
Jeremy Klein | 35e99ea | 2016-03-12 16:29:54 -0800 | [diff] [blame] | 2515 | |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2516 | final Executor executor = new Executor() { |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2517 | @Override |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2518 | public void execute(Runnable command) { |
| 2519 | if (handler == null) { |
| 2520 | command.run(); |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2521 | } else { |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2522 | handler.post(command); |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2523 | } |
| 2524 | } |
| 2525 | }; |
Jeremy Klein | 35e99ea | 2016-03-12 16:29:54 -0800 | [diff] [blame] | 2526 | |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2527 | final StartTetheringCallback tetheringCallback = new StartTetheringCallback() { |
| 2528 | @Override |
| 2529 | public void onTetheringStarted() { |
| 2530 | callback.onTetheringStarted(); |
| 2531 | } |
| 2532 | |
| 2533 | @Override |
markchien | f47d834 | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2534 | public void onTetheringFailed(final int error) { |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2535 | callback.onTetheringFailed(); |
| 2536 | } |
| 2537 | }; |
| 2538 | |
| 2539 | final TetheringRequest request = new TetheringRequest.Builder(type) |
markchien | f47d834 | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2540 | .setShouldShowEntitlementUi(showProvisioningUi).build(); |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2541 | |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2542 | mTetheringManager.startTethering(request, executor, tetheringCallback); |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2543 | } |
| 2544 | |
| 2545 | /** |
| 2546 | * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if |
| 2547 | * applicable. |
| 2548 | * |
| 2549 | * @param type The type of tethering to stop. Must be one of |
| 2550 | * {@link ConnectivityManager.TETHERING_WIFI}, |
| 2551 | * {@link ConnectivityManager.TETHERING_USB}, or |
| 2552 | * {@link ConnectivityManager.TETHERING_BLUETOOTH}. |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2553 | * |
| 2554 | * @deprecated Use {@link TetheringManager#stopTethering} instead. |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2555 | * @hide |
| 2556 | */ |
| 2557 | @SystemApi |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2558 | @Deprecated |
Jeff Sharkey | 9b39e9a | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 2559 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2560 | public void stopTethering(int type) { |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2561 | mTetheringManager.stopTethering(type); |
Jeremy Klein | 3dabcb9 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2562 | } |
| 2563 | |
| 2564 | /** |
markchien | 4ef53e8 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2565 | * Callback for use with {@link registerTetheringEventCallback} to find out tethering |
| 2566 | * upstream status. |
| 2567 | * |
Nathan Harold | 74f0fb8 | 2020-01-23 18:03:46 -0800 | [diff] [blame] | 2568 | * @deprecated Use {@link TetheringManager#OnTetheringEventCallback} instead. |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2569 | * @hide |
markchien | 4ef53e8 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2570 | */ |
| 2571 | @SystemApi |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2572 | @Deprecated |
markchien | 4ef53e8 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2573 | public abstract static class OnTetheringEventCallback { |
| 2574 | |
| 2575 | /** |
| 2576 | * Called when tethering upstream changed. This can be called multiple times and can be |
| 2577 | * called any time. |
| 2578 | * |
| 2579 | * @param network the {@link Network} of tethering upstream. Null means tethering doesn't |
| 2580 | * have any upstream. |
| 2581 | */ |
| 2582 | public void onUpstreamChanged(@Nullable Network network) {} |
| 2583 | } |
| 2584 | |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2585 | @GuardedBy("mTetheringEventCallbacks") |
| 2586 | private final ArrayMap<OnTetheringEventCallback, TetheringEventCallback> |
| 2587 | mTetheringEventCallbacks = new ArrayMap<>(); |
| 2588 | |
markchien | 4ef53e8 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2589 | /** |
| 2590 | * 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] | 2591 | * 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] | 2592 | * disabled, {@link OnTetheringEventCallback#onUpstreamChanged} will immediately be called |
| 2593 | * with a null argument. The same callback object cannot be registered twice. |
| 2594 | * |
| 2595 | * @param executor the executor on which callback will be invoked. |
| 2596 | * @param callback the callback to be called when tethering has change events. |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2597 | * |
Nathan Harold | 74f0fb8 | 2020-01-23 18:03:46 -0800 | [diff] [blame] | 2598 | * @deprecated Use {@link TetheringManager#registerTetheringEventCallback} instead. |
markchien | 4ef53e8 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2599 | * @hide |
| 2600 | */ |
| 2601 | @SystemApi |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2602 | @Deprecated |
markchien | 4ef53e8 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2603 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
| 2604 | public void registerTetheringEventCallback( |
| 2605 | @NonNull @CallbackExecutor Executor executor, |
| 2606 | @NonNull final OnTetheringEventCallback callback) { |
| 2607 | Preconditions.checkNotNull(callback, "OnTetheringEventCallback cannot be null."); |
| 2608 | |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2609 | final TetheringEventCallback tetherCallback = |
| 2610 | new TetheringEventCallback() { |
| 2611 | @Override |
| 2612 | public void onUpstreamChanged(@Nullable Network network) { |
| 2613 | callback.onUpstreamChanged(network); |
| 2614 | } |
| 2615 | }; |
| 2616 | |
| 2617 | synchronized (mTetheringEventCallbacks) { |
| 2618 | mTetheringEventCallbacks.put(callback, tetherCallback); |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2619 | mTetheringManager.registerTetheringEventCallback(executor, tetherCallback); |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2620 | } |
markchien | 4ef53e8 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2621 | } |
| 2622 | |
| 2623 | /** |
| 2624 | * Remove tethering event callback previously registered with |
| 2625 | * {@link #registerTetheringEventCallback}. |
| 2626 | * |
| 2627 | * @param callback previously registered callback. |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2628 | * |
| 2629 | * @deprecated Use {@link TetheringManager#unregisterTetheringEventCallback} instead. |
markchien | 4ef53e8 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2630 | * @hide |
| 2631 | */ |
| 2632 | @SystemApi |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2633 | @Deprecated |
markchien | 4ef53e8 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2634 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
| 2635 | public void unregisterTetheringEventCallback( |
| 2636 | @NonNull final OnTetheringEventCallback callback) { |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2637 | Objects.requireNonNull(callback, "The callback must be non-null"); |
| 2638 | synchronized (mTetheringEventCallbacks) { |
| 2639 | final TetheringEventCallback tetherCallback = |
| 2640 | mTetheringEventCallbacks.remove(callback); |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2641 | mTetheringManager.unregisterTetheringEventCallback(tetherCallback); |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2642 | } |
markchien | 4ef53e8 | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2643 | } |
| 2644 | |
| 2645 | |
| 2646 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2647 | * Get the list of regular expressions that define any tetherable |
| 2648 | * USB network interfaces. If USB tethering is not supported by the |
| 2649 | * device, this list should be empty. |
| 2650 | * |
| 2651 | * @return an array of 0 or more regular expression Strings defining |
| 2652 | * what interfaces are considered tetherable usb interfaces. |
| 2653 | * |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2654 | * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged} instead. |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2655 | * {@hide} |
| 2656 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 2657 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2658 | @UnsupportedAppUsage |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2659 | @Deprecated |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2660 | public String[] getTetherableUsbRegexs() { |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2661 | return mTetheringManager.getTetherableUsbRegexs(); |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2662 | } |
| 2663 | |
| 2664 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2665 | * Get the list of regular expressions that define any tetherable |
| 2666 | * Wifi network interfaces. If Wifi tethering is not supported by the |
| 2667 | * device, this list should be empty. |
| 2668 | * |
| 2669 | * @return an array of 0 or more regular expression Strings defining |
| 2670 | * what interfaces are considered tetherable wifi interfaces. |
| 2671 | * |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2672 | * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged} instead. |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2673 | * {@hide} |
| 2674 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 2675 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2676 | @UnsupportedAppUsage |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2677 | @Deprecated |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2678 | public String[] getTetherableWifiRegexs() { |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2679 | return mTetheringManager.getTetherableWifiRegexs(); |
Robert Greenwalt | 8e87f12 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2680 | } |
Robert Greenwalt | 4283ded | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2681 | |
Danica Chang | 9656705 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2682 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2683 | * Get the list of regular expressions that define any tetherable |
| 2684 | * Bluetooth network interfaces. If Bluetooth tethering is not supported by the |
| 2685 | * device, this list should be empty. |
| 2686 | * |
| 2687 | * @return an array of 0 or more regular expression Strings defining |
| 2688 | * what interfaces are considered tetherable bluetooth interfaces. |
| 2689 | * |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2690 | * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged( |
| 2691 | *TetheringManager.TetheringInterfaceRegexps)} instead. |
Danica Chang | 9656705 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2692 | * {@hide} |
| 2693 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 2694 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2695 | @UnsupportedAppUsage |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2696 | @Deprecated |
Danica Chang | 9656705 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2697 | public String[] getTetherableBluetoothRegexs() { |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2698 | return mTetheringManager.getTetherableBluetoothRegexs(); |
Danica Chang | 9656705 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2699 | } |
| 2700 | |
Mike Lockwood | ed4a174 | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2701 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2702 | * Attempt to both alter the mode of USB and Tethering of USB. A |
| 2703 | * utility method to deal with some of the complexity of USB - will |
| 2704 | * attempt to switch to Rndis and subsequently tether the resulting |
| 2705 | * interface on {@code true} or turn off tethering and switch off |
| 2706 | * Rndis on {@code false}. |
Lorenzo Colitti | 0b40c87 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 2707 | * |
| 2708 | * <p>This method requires the caller to hold either the |
| 2709 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2710 | * or the ability to modify system settings as determined by |
| 2711 | * {@link android.provider.Settings.System#canWrite}.</p> |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2712 | * |
| 2713 | * @param enable a boolean - {@code true} to enable tethering |
| 2714 | * @return error a {@code TETHER_ERROR} value indicating success or failure type |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2715 | * @deprecated Use {@link TetheringManager#startTethering} instead |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2716 | * |
Mike Lockwood | ed4a174 | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2717 | * {@hide} |
| 2718 | */ |
Mathew Inwood | c5b9bec | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2719 | @UnsupportedAppUsage |
markchien | 91c78e5 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2720 | @Deprecated |
Mike Lockwood | ed4a174 | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2721 | public int setUsbTethering(boolean enable) { |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2722 | return mTetheringManager.setUsbTethering(enable); |
Mike Lockwood | ed4a174 | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2723 | } |
| 2724 | |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2725 | /** |
| 2726 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_NO_ERROR}. |
| 2727 | * {@hide} |
| 2728 | */ |
markchien | 0f45bb9 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 2729 | @SystemApi |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2730 | @Deprecated |
Remi NGUYEN VAN | 09f8ed2 | 2021-02-15 18:52:06 +0900 | [diff] [blame] | 2731 | public static final int TETHER_ERROR_NO_ERROR = 0; |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2732 | /** |
| 2733 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNKNOWN_IFACE}. |
| 2734 | * {@hide} |
| 2735 | */ |
| 2736 | @Deprecated |
| 2737 | public static final int TETHER_ERROR_UNKNOWN_IFACE = |
| 2738 | TetheringManager.TETHER_ERROR_UNKNOWN_IFACE; |
| 2739 | /** |
| 2740 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_SERVICE_UNAVAIL}. |
| 2741 | * {@hide} |
| 2742 | */ |
| 2743 | @Deprecated |
| 2744 | public static final int TETHER_ERROR_SERVICE_UNAVAIL = |
| 2745 | TetheringManager.TETHER_ERROR_SERVICE_UNAVAIL; |
| 2746 | /** |
| 2747 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNSUPPORTED}. |
| 2748 | * {@hide} |
| 2749 | */ |
| 2750 | @Deprecated |
| 2751 | public static final int TETHER_ERROR_UNSUPPORTED = TetheringManager.TETHER_ERROR_UNSUPPORTED; |
| 2752 | /** |
| 2753 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNAVAIL_IFACE}. |
| 2754 | * {@hide} |
| 2755 | */ |
| 2756 | @Deprecated |
| 2757 | public static final int TETHER_ERROR_UNAVAIL_IFACE = |
| 2758 | TetheringManager.TETHER_ERROR_UNAVAIL_IFACE; |
| 2759 | /** |
markchien | f47d834 | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2760 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_INTERNAL_ERROR}. |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2761 | * {@hide} |
| 2762 | */ |
| 2763 | @Deprecated |
markchien | f47d834 | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2764 | public static final int TETHER_ERROR_MASTER_ERROR = |
| 2765 | TetheringManager.TETHER_ERROR_INTERNAL_ERROR; |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2766 | /** |
| 2767 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_TETHER_IFACE_ERROR}. |
| 2768 | * {@hide} |
| 2769 | */ |
| 2770 | @Deprecated |
| 2771 | public static final int TETHER_ERROR_TETHER_IFACE_ERROR = |
| 2772 | TetheringManager.TETHER_ERROR_TETHER_IFACE_ERROR; |
| 2773 | /** |
| 2774 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNTETHER_IFACE_ERROR}. |
| 2775 | * {@hide} |
| 2776 | */ |
| 2777 | @Deprecated |
| 2778 | public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = |
| 2779 | TetheringManager.TETHER_ERROR_UNTETHER_IFACE_ERROR; |
| 2780 | /** |
markchien | f47d834 | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2781 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_ENABLE_FORWARDING_ERROR}. |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2782 | * {@hide} |
| 2783 | */ |
| 2784 | @Deprecated |
| 2785 | public static final int TETHER_ERROR_ENABLE_NAT_ERROR = |
markchien | f47d834 | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2786 | TetheringManager.TETHER_ERROR_ENABLE_FORWARDING_ERROR; |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2787 | /** |
markchien | f47d834 | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2788 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_DISABLE_FORWARDING_ERROR}. |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2789 | * {@hide} |
| 2790 | */ |
| 2791 | @Deprecated |
| 2792 | public static final int TETHER_ERROR_DISABLE_NAT_ERROR = |
markchien | f47d834 | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2793 | TetheringManager.TETHER_ERROR_DISABLE_FORWARDING_ERROR; |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2794 | /** |
| 2795 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_IFACE_CFG_ERROR}. |
| 2796 | * {@hide} |
| 2797 | */ |
| 2798 | @Deprecated |
| 2799 | public static final int TETHER_ERROR_IFACE_CFG_ERROR = |
| 2800 | TetheringManager.TETHER_ERROR_IFACE_CFG_ERROR; |
| 2801 | /** |
markchien | f47d834 | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2802 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_PROVISIONING_FAILED}. |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2803 | * {@hide} |
| 2804 | */ |
markchien | 0f45bb9 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 2805 | @SystemApi |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2806 | @Deprecated |
Remi NGUYEN VAN | 09f8ed2 | 2021-02-15 18:52:06 +0900 | [diff] [blame] | 2807 | public static final int TETHER_ERROR_PROVISION_FAILED = 11; |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2808 | /** |
| 2809 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_DHCPSERVER_ERROR}. |
| 2810 | * {@hide} |
| 2811 | */ |
| 2812 | @Deprecated |
| 2813 | public static final int TETHER_ERROR_DHCPSERVER_ERROR = |
| 2814 | TetheringManager.TETHER_ERROR_DHCPSERVER_ERROR; |
| 2815 | /** |
| 2816 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_ENTITLEMENT_UNKNOWN}. |
| 2817 | * {@hide} |
| 2818 | */ |
markchien | 0f45bb9 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 2819 | @SystemApi |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2820 | @Deprecated |
Remi NGUYEN VAN | 09f8ed2 | 2021-02-15 18:52:06 +0900 | [diff] [blame] | 2821 | public static final int TETHER_ERROR_ENTITLEMENT_UNKONWN = 13; |
Robert Greenwalt | 4283ded | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2822 | |
| 2823 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2824 | * Get a more detailed error code after a Tethering or Untethering |
| 2825 | * request asynchronously failed. |
| 2826 | * |
| 2827 | * @param iface The name of the interface of interest |
Robert Greenwalt | 4283ded | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2828 | * @return error The error code of the last error tethering or untethering the named |
| 2829 | * interface |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2830 | * |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2831 | * @deprecated Use {@link TetheringEventCallback#onError(String, int)} instead. |
Robert Greenwalt | 4283ded | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2832 | * {@hide} |
| 2833 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 2834 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 2835 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2836 | @Deprecated |
Robert Greenwalt | 4283ded | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2837 | public int getLastTetherError(String iface) { |
markchien | f47d834 | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2838 | int error = mTetheringManager.getLastTetherError(iface); |
| 2839 | if (error == TetheringManager.TETHER_ERROR_UNKNOWN_TYPE) { |
| 2840 | // TETHER_ERROR_UNKNOWN_TYPE was introduced with TetheringManager and has never been |
| 2841 | // returned by ConnectivityManager. Convert it to the legacy TETHER_ERROR_UNKNOWN_IFACE |
| 2842 | // instead. |
| 2843 | error = TetheringManager.TETHER_ERROR_UNKNOWN_IFACE; |
| 2844 | } |
| 2845 | return error; |
Robert Greenwalt | 93dc104 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2846 | } |
| 2847 | |
markchien | bf5ab01 | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 2848 | /** @hide */ |
| 2849 | @Retention(RetentionPolicy.SOURCE) |
| 2850 | @IntDef(value = { |
| 2851 | TETHER_ERROR_NO_ERROR, |
| 2852 | TETHER_ERROR_PROVISION_FAILED, |
| 2853 | TETHER_ERROR_ENTITLEMENT_UNKONWN, |
| 2854 | }) |
| 2855 | public @interface EntitlementResultCode { |
| 2856 | } |
| 2857 | |
Robert Greenwalt | 93dc104 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2858 | /** |
markchien | bf5ab01 | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 2859 | * Callback for use with {@link #getLatestTetheringEntitlementResult} to find out whether |
markchien | 0f45bb9 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 2860 | * entitlement succeeded. |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2861 | * |
| 2862 | * @deprecated Use {@link TetheringManager#OnTetheringEntitlementResultListener} instead. |
markchien | 0f45bb9 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 2863 | * @hide |
| 2864 | */ |
| 2865 | @SystemApi |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2866 | @Deprecated |
markchien | bf5ab01 | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 2867 | public interface OnTetheringEntitlementResultListener { |
| 2868 | /** |
| 2869 | * Called to notify entitlement result. |
| 2870 | * |
| 2871 | * @param resultCode an int value of entitlement result. It may be one of |
| 2872 | * {@link #TETHER_ERROR_NO_ERROR}, |
| 2873 | * {@link #TETHER_ERROR_PROVISION_FAILED}, or |
| 2874 | * {@link #TETHER_ERROR_ENTITLEMENT_UNKONWN}. |
| 2875 | */ |
Jeremy Klein | 7e7c742 | 2019-03-12 13:32:08 -0700 | [diff] [blame] | 2876 | void onTetheringEntitlementResult(@EntitlementResultCode int resultCode); |
markchien | bf5ab01 | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 2877 | } |
| 2878 | |
| 2879 | /** |
markchien | 0f45bb9 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 2880 | * Get the last value of the entitlement check on this downstream. If the cached value is |
| 2881 | * {@link #TETHER_ERROR_NO_ERROR} or showEntitlementUi argument is false, it just return the |
| 2882 | * cached value. Otherwise, a UI-based entitlement check would be performed. It is not |
| 2883 | * guaranteed that the UI-based entitlement check will complete in any specific time period |
| 2884 | * and may in fact never complete. Any successful entitlement check the platform performs for |
| 2885 | * any reason will update the cached value. |
| 2886 | * |
| 2887 | * @param type the downstream type of tethering. Must be one of |
| 2888 | * {@link #TETHERING_WIFI}, |
| 2889 | * {@link #TETHERING_USB}, or |
| 2890 | * {@link #TETHERING_BLUETOOTH}. |
| 2891 | * @param showEntitlementUi a boolean indicating whether to run UI-based entitlement check. |
markchien | bf5ab01 | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 2892 | * @param executor the executor on which callback will be invoked. |
| 2893 | * @param listener an {@link OnTetheringEntitlementResultListener} which will be called to |
| 2894 | * notify the caller of the result of entitlement check. The listener may be called zero |
| 2895 | * or one time. |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2896 | * @deprecated Use {@link TetheringManager#requestLatestTetheringEntitlementResult} instead. |
markchien | 0f45bb9 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 2897 | * {@hide} |
| 2898 | */ |
| 2899 | @SystemApi |
markchien | 6ae63e5 | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2900 | @Deprecated |
markchien | 0f45bb9 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 2901 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
markchien | bf5ab01 | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 2902 | public void getLatestTetheringEntitlementResult(int type, boolean showEntitlementUi, |
| 2903 | @NonNull @CallbackExecutor Executor executor, |
| 2904 | @NonNull final OnTetheringEntitlementResultListener listener) { |
| 2905 | Preconditions.checkNotNull(listener, "TetheringEntitlementResultListener cannot be null."); |
| 2906 | ResultReceiver wrappedListener = new ResultReceiver(null) { |
| 2907 | @Override |
| 2908 | protected void onReceiveResult(int resultCode, Bundle resultData) { |
lucaslin | c6170bb | 2021-03-04 09:38:21 +0800 | [diff] [blame] | 2909 | final long token = Binder.clearCallingIdentity(); |
| 2910 | try { |
| 2911 | executor.execute(() -> { |
| 2912 | listener.onTetheringEntitlementResult(resultCode); |
| 2913 | }); |
| 2914 | } finally { |
| 2915 | Binder.restoreCallingIdentity(token); |
| 2916 | } |
markchien | bf5ab01 | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 2917 | } |
| 2918 | }; |
| 2919 | |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 2920 | mTetheringManager.requestLatestTetheringEntitlementResult(type, wrappedListener, |
markchien | 5776f96 | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 2921 | showEntitlementUi); |
markchien | bf5ab01 | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 2922 | } |
| 2923 | |
| 2924 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2925 | * Report network connectivity status. This is currently used only |
| 2926 | * to alter status bar UI. |
Paul Jensen | 08f9dbb | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 2927 | * <p>This method requires the caller to hold the permission |
| 2928 | * {@link android.Manifest.permission#STATUS_BAR}. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2929 | * |
Robert Greenwalt | 986c741 | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2930 | * @param networkType The type of network you want to report on |
| 2931 | * @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] | 2932 | * @deprecated Types are deprecated. Use {@link #reportNetworkConnectivity} instead. |
Robert Greenwalt | 986c741 | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2933 | * {@hide} |
| 2934 | */ |
| 2935 | public void reportInetCondition(int networkType, int percentage) { |
Soi, Yoshinari | b8102ef | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 2936 | printStackTrace(); |
Robert Greenwalt | 986c741 | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2937 | try { |
| 2938 | mService.reportInetCondition(networkType, percentage); |
| 2939 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2940 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 986c741 | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2941 | } |
| 2942 | } |
Robert Greenwalt | c3c5f86 | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2943 | |
| 2944 | /** |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2945 | * 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] | 2946 | * that there might be connectivity problems on this network and may cause |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2947 | * the framework to re-evaluate network connectivity and/or switch to another |
| 2948 | * network. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2949 | * |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2950 | * @param network The {@link Network} the application was attempting to use |
| 2951 | * or {@code null} to indicate the current default network. |
Paul Jensen | b95d795 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 2952 | * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both |
| 2953 | * working and non-working connectivity. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2954 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 2955 | @Deprecated |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 2956 | public void reportBadNetwork(@Nullable Network network) { |
Soi, Yoshinari | b8102ef | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 2957 | printStackTrace(); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2958 | try { |
Paul Jensen | b95d795 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 2959 | // One of these will be ignored because it matches system's current state. |
| 2960 | // The other will trigger the necessary reevaluation. |
| 2961 | mService.reportNetworkConnectivity(network, true); |
| 2962 | mService.reportNetworkConnectivity(network, false); |
| 2963 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2964 | throw e.rethrowFromSystemServer(); |
Paul Jensen | b95d795 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 2965 | } |
| 2966 | } |
| 2967 | |
| 2968 | /** |
| 2969 | * Report to the framework whether a network has working connectivity. |
| 2970 | * This provides a hint to the system that a particular network is providing |
| 2971 | * working connectivity or not. In response the framework may re-evaluate |
| 2972 | * the network's connectivity and might take further action thereafter. |
| 2973 | * |
| 2974 | * @param network The {@link Network} the application was attempting to use |
| 2975 | * or {@code null} to indicate the current default network. |
| 2976 | * @param hasConnectivity {@code true} if the application was able to successfully access the |
| 2977 | * Internet using {@code network} or {@code false} if not. |
| 2978 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 2979 | public void reportNetworkConnectivity(@Nullable Network network, boolean hasConnectivity) { |
Soi, Yoshinari | b8102ef | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 2980 | printStackTrace(); |
Paul Jensen | b95d795 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 2981 | try { |
| 2982 | mService.reportNetworkConnectivity(network, hasConnectivity); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2983 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2984 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2985 | } |
| 2986 | } |
| 2987 | |
| 2988 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2989 | * Set a network-independent global http proxy. This is not normally what you want |
| 2990 | * for typical HTTP proxies - they are general network dependent. However if you're |
| 2991 | * doing something unusual like general internal filtering this may be useful. On |
| 2992 | * a private network where the proxy is not accessible, you may break HTTP using this. |
Paul Jensen | 08f9dbb | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 2993 | * |
| 2994 | * @param p A {@link ProxyInfo} object defining the new global |
| 2995 | * HTTP proxy. A {@code null} value will clear the global HTTP proxy. |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2996 | * @hide |
Robert Greenwalt | c3c5f86 | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2997 | */ |
paulhu | 8e96a75 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 2998 | @RequiresPermission(android.Manifest.permission.NETWORK_STACK) |
Jason Monk | 4d5e20f | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2999 | public void setGlobalProxy(ProxyInfo p) { |
Robert Greenwalt | c3c5f86 | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3000 | try { |
| 3001 | mService.setGlobalProxy(p); |
| 3002 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3003 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | c3c5f86 | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3004 | } |
| 3005 | } |
| 3006 | |
| 3007 | /** |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 3008 | * Retrieve any network-independent global HTTP proxy. |
| 3009 | * |
Jason Monk | 4d5e20f | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3010 | * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null} |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 3011 | * if no global HTTP proxy is set. |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3012 | * @hide |
Robert Greenwalt | c3c5f86 | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3013 | */ |
Jason Monk | 4d5e20f | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3014 | public ProxyInfo getGlobalProxy() { |
Robert Greenwalt | c3c5f86 | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3015 | try { |
| 3016 | return mService.getGlobalProxy(); |
| 3017 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3018 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | c3c5f86 | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3019 | } |
| 3020 | } |
| 3021 | |
| 3022 | /** |
Paul Jensen | db93dd9 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 3023 | * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a |
| 3024 | * network-specific HTTP proxy. If {@code network} is null, the |
| 3025 | * network-specific proxy returned is the proxy of the default active |
| 3026 | * network. |
| 3027 | * |
| 3028 | * @return {@link ProxyInfo} for the current global HTTP proxy, or if no |
| 3029 | * global HTTP proxy is set, {@code ProxyInfo} for {@code network}, |
| 3030 | * or when {@code network} is {@code null}, |
| 3031 | * the {@code ProxyInfo} for the default active network. Returns |
| 3032 | * {@code null} when no proxy applies or the caller doesn't have |
| 3033 | * permission to use {@code network}. |
| 3034 | * @hide |
| 3035 | */ |
| 3036 | public ProxyInfo getProxyForNetwork(Network network) { |
| 3037 | try { |
| 3038 | return mService.getProxyForNetwork(network); |
| 3039 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3040 | throw e.rethrowFromSystemServer(); |
Paul Jensen | db93dd9 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 3041 | } |
| 3042 | } |
| 3043 | |
| 3044 | /** |
Paul Jensen | c0618a6 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 3045 | * Get the current default HTTP proxy settings. If a global proxy is set it will be returned, |
| 3046 | * otherwise if this process is bound to a {@link Network} using |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 3047 | * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise |
Paul Jensen | c0618a6 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 3048 | * the default network's proxy is returned. |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 3049 | * |
Jason Monk | 4d5e20f | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3050 | * @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] | 3051 | * HTTP proxy is active. |
Robert Greenwalt | c3c5f86 | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3052 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 3053 | @Nullable |
Paul Jensen | c0618a6 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 3054 | public ProxyInfo getDefaultProxy() { |
Paul Jensen | db93dd9 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 3055 | return getProxyForNetwork(getBoundNetworkForProcess()); |
Robert Greenwalt | c3c5f86 | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3056 | } |
Robert Greenwalt | 34848c0 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 3057 | |
| 3058 | /** |
Robert Greenwalt | 0114f6e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 3059 | * Returns true if the hardware supports the given network type |
| 3060 | * else it returns false. This doesn't indicate we have coverage |
| 3061 | * or are authorized onto a network, just whether or not the |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 3062 | * hardware supports it. For example a GSM phone without a SIM |
| 3063 | * should still return {@code true} for mobile data, but a wifi only |
| 3064 | * tablet would return {@code false}. |
| 3065 | * |
| 3066 | * @param networkType The network type we'd like to check |
| 3067 | * @return {@code true} if supported, else {@code false} |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 3068 | * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead. |
Robert Greenwalt | 0114f6e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 3069 | * @hide |
| 3070 | */ |
Chalard Jean | 7eaf3b1 | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 3071 | @Deprecated |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 3072 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | a3b7751 | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 3073 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Robert Greenwalt | 0114f6e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 3074 | public boolean isNetworkSupported(int networkType) { |
| 3075 | try { |
| 3076 | return mService.isNetworkSupported(networkType); |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3077 | } catch (RemoteException e) { |
| 3078 | throw e.rethrowFromSystemServer(); |
| 3079 | } |
Robert Greenwalt | 0114f6e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 3080 | } |
Jeff Sharkey | d00b130 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 3081 | |
| 3082 | /** |
| 3083 | * Returns if the currently active data network is metered. A network is |
| 3084 | * 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] | 3085 | * that connection due to monetary costs, data limitations or |
| 3086 | * battery/performance issues. You should check this before doing large |
| 3087 | * data transfers, and warn the user or delay the operation until another |
| 3088 | * network is available. |
| 3089 | * |
| 3090 | * @return {@code true} if large transfers should be avoided, otherwise |
| 3091 | * {@code false}. |
Jeff Sharkey | d00b130 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 3092 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 3093 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Jeff Sharkey | d00b130 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 3094 | public boolean isActiveNetworkMetered() { |
| 3095 | try { |
| 3096 | return mService.isActiveNetworkMetered(); |
| 3097 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3098 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | d00b130 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 3099 | } |
| 3100 | } |
Jeff Sharkey | ebcc797 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3101 | |
Robert Greenwalt | faa4b40 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 3102 | /** |
Sarah Chin | cabcbff | 2020-11-25 12:15:14 -0800 | [diff] [blame] | 3103 | * Set sign in error notification to visible or invisible |
Wink Saville | 9a1a7ef | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3104 | * |
Sarah Chin | cabcbff | 2020-11-25 12:15:14 -0800 | [diff] [blame] | 3105 | * @hide |
Paul Jensen | dda8e59 | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 3106 | * @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] | 3107 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 3108 | @Deprecated |
Wink Saville | 9a1a7ef | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3109 | public void setProvisioningNotificationVisible(boolean visible, int networkType, |
Paul Jensen | ebeaecd | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3110 | String action) { |
Wink Saville | 9a1a7ef | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3111 | try { |
Paul Jensen | ebeaecd | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3112 | mService.setProvisioningNotificationVisible(visible, networkType, action); |
Wink Saville | 9a1a7ef | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3113 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3114 | throw e.rethrowFromSystemServer(); |
Wink Saville | 9a1a7ef | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3115 | } |
| 3116 | } |
Yuhao Zheng | b77f15d | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3117 | |
| 3118 | /** |
| 3119 | * Set the value for enabling/disabling airplane mode |
| 3120 | * |
| 3121 | * @param enable whether to enable airplane mode or not |
| 3122 | * |
Yuhao Zheng | b77f15d | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3123 | * @hide |
| 3124 | */ |
Lorenzo Colitti | c7da00d | 2018-10-09 18:55:11 +0900 | [diff] [blame] | 3125 | @RequiresPermission(anyOf = { |
Edward Savage-Jones | d472369 | 2019-11-26 13:18:08 +0100 | [diff] [blame] | 3126 | android.Manifest.permission.NETWORK_AIRPLANE_MODE, |
Lorenzo Colitti | c7da00d | 2018-10-09 18:55:11 +0900 | [diff] [blame] | 3127 | android.Manifest.permission.NETWORK_SETTINGS, |
| 3128 | android.Manifest.permission.NETWORK_SETUP_WIZARD, |
| 3129 | android.Manifest.permission.NETWORK_STACK}) |
Lorenzo Colitti | 0bfef02 | 2018-10-09 18:50:32 +0900 | [diff] [blame] | 3130 | @SystemApi |
Yuhao Zheng | b77f15d | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3131 | public void setAirplaneMode(boolean enable) { |
| 3132 | try { |
| 3133 | mService.setAirplaneMode(enable); |
| 3134 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3135 | throw e.rethrowFromSystemServer(); |
Yuhao Zheng | b77f15d | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3136 | } |
| 3137 | } |
Robert Greenwalt | 7e45d11 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3138 | |
Lorenzo Colitti | 6702d4b | 2020-01-08 00:04:09 +0900 | [diff] [blame] | 3139 | /** |
| 3140 | * Registers the specified {@link NetworkProvider}. |
| 3141 | * Each listener must only be registered once. The listener can be unregistered with |
| 3142 | * {@link #unregisterNetworkProvider}. |
| 3143 | * |
| 3144 | * @param provider the provider to register |
| 3145 | * @return the ID of the provider. This ID must be used by the provider when registering |
| 3146 | * {@link android.net.NetworkAgent}s. |
| 3147 | * @hide |
| 3148 | */ |
| 3149 | @SystemApi |
paulhu | b6ba8e8 | 2020-03-04 09:43:41 +0800 | [diff] [blame] | 3150 | @RequiresPermission(anyOf = { |
| 3151 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 3152 | android.Manifest.permission.NETWORK_FACTORY}) |
Lorenzo Colitti | 6702d4b | 2020-01-08 00:04:09 +0900 | [diff] [blame] | 3153 | public int registerNetworkProvider(@NonNull NetworkProvider provider) { |
| 3154 | if (provider.getProviderId() != NetworkProvider.ID_NONE) { |
Lorenzo Colitti | 6702d4b | 2020-01-08 00:04:09 +0900 | [diff] [blame] | 3155 | throw new IllegalStateException("NetworkProviders can only be registered once"); |
| 3156 | } |
| 3157 | |
| 3158 | try { |
| 3159 | int providerId = mService.registerNetworkProvider(provider.getMessenger(), |
| 3160 | provider.getName()); |
| 3161 | provider.setProviderId(providerId); |
| 3162 | } catch (RemoteException e) { |
| 3163 | throw e.rethrowFromSystemServer(); |
| 3164 | } |
| 3165 | return provider.getProviderId(); |
| 3166 | } |
| 3167 | |
| 3168 | /** |
| 3169 | * Unregisters the specified NetworkProvider. |
| 3170 | * |
| 3171 | * @param provider the provider to unregister |
| 3172 | * @hide |
| 3173 | */ |
| 3174 | @SystemApi |
paulhu | b6ba8e8 | 2020-03-04 09:43:41 +0800 | [diff] [blame] | 3175 | @RequiresPermission(anyOf = { |
| 3176 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 3177 | android.Manifest.permission.NETWORK_FACTORY}) |
Lorenzo Colitti | 6702d4b | 2020-01-08 00:04:09 +0900 | [diff] [blame] | 3178 | public void unregisterNetworkProvider(@NonNull NetworkProvider provider) { |
| 3179 | try { |
| 3180 | mService.unregisterNetworkProvider(provider.getMessenger()); |
| 3181 | } catch (RemoteException e) { |
| 3182 | throw e.rethrowFromSystemServer(); |
| 3183 | } |
| 3184 | provider.setProviderId(NetworkProvider.ID_NONE); |
| 3185 | } |
| 3186 | |
| 3187 | |
| 3188 | /** @hide exposed via the NetworkProvider class. */ |
paulhu | b6ba8e8 | 2020-03-04 09:43:41 +0800 | [diff] [blame] | 3189 | @RequiresPermission(anyOf = { |
| 3190 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 3191 | android.Manifest.permission.NETWORK_FACTORY}) |
Lorenzo Colitti | 6702d4b | 2020-01-08 00:04:09 +0900 | [diff] [blame] | 3192 | public void declareNetworkRequestUnfulfillable(@NonNull NetworkRequest request) { |
| 3193 | try { |
| 3194 | mService.declareNetworkRequestUnfulfillable(request); |
| 3195 | } catch (RemoteException e) { |
| 3196 | throw e.rethrowFromSystemServer(); |
| 3197 | } |
| 3198 | } |
| 3199 | |
Chalard Jean | 29d06db | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 3200 | // TODO : remove this method. It is a stopgap measure to help sheperding a number |
| 3201 | // of dependent changes that would conflict throughout the automerger graph. Having this |
| 3202 | // temporarily helps with the process of going through with all these dependent changes across |
| 3203 | // the entire tree. |
Paul Jensen | 1f56738 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 3204 | /** |
| 3205 | * @hide |
| 3206 | * Register a NetworkAgent with ConnectivityService. |
Chalard Jean | f78c964 | 2019-12-13 19:47:12 +0900 | [diff] [blame] | 3207 | * @return Network corresponding to NetworkAgent. |
Paul Jensen | 1f56738 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 3208 | */ |
paulhu | b6ba8e8 | 2020-03-04 09:43:41 +0800 | [diff] [blame] | 3209 | @RequiresPermission(anyOf = { |
| 3210 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 3211 | android.Manifest.permission.NETWORK_FACTORY}) |
Remi NGUYEN VAN | 77b0c1c | 2020-12-23 12:45:08 +0900 | [diff] [blame] | 3212 | public Network registerNetworkAgent(INetworkAgent na, NetworkInfo ni, LinkProperties lp, |
Lorenzo Colitti | ab2fed7 | 2020-01-12 22:28:37 +0900 | [diff] [blame] | 3213 | NetworkCapabilities nc, int score, NetworkAgentConfig config) { |
Remi NGUYEN VAN | 77b0c1c | 2020-12-23 12:45:08 +0900 | [diff] [blame] | 3214 | return registerNetworkAgent(na, ni, lp, nc, score, config, NetworkProvider.ID_NONE); |
Chalard Jean | 29d06db | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 3215 | } |
| 3216 | |
| 3217 | /** |
| 3218 | * @hide |
| 3219 | * Register a NetworkAgent with ConnectivityService. |
Chalard Jean | f78c964 | 2019-12-13 19:47:12 +0900 | [diff] [blame] | 3220 | * @return Network corresponding to NetworkAgent. |
Chalard Jean | 29d06db | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 3221 | */ |
paulhu | b6ba8e8 | 2020-03-04 09:43:41 +0800 | [diff] [blame] | 3222 | @RequiresPermission(anyOf = { |
| 3223 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 3224 | android.Manifest.permission.NETWORK_FACTORY}) |
Remi NGUYEN VAN | 77b0c1c | 2020-12-23 12:45:08 +0900 | [diff] [blame] | 3225 | public Network registerNetworkAgent(INetworkAgent na, NetworkInfo ni, LinkProperties lp, |
Lorenzo Colitti | ab2fed7 | 2020-01-12 22:28:37 +0900 | [diff] [blame] | 3226 | NetworkCapabilities nc, int score, NetworkAgentConfig config, int providerId) { |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3227 | try { |
Remi NGUYEN VAN | 77b0c1c | 2020-12-23 12:45:08 +0900 | [diff] [blame] | 3228 | return mService.registerNetworkAgent(na, ni, lp, nc, score, config, providerId); |
Paul Jensen | 1f56738 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 3229 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3230 | throw e.rethrowFromSystemServer(); |
Paul Jensen | 1f56738 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 3231 | } |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3232 | } |
| 3233 | |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3234 | /** |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 3235 | * Base class for {@code NetworkRequest} callbacks. Used for notifications about network |
| 3236 | * changes. Should be extended by applications wanting notifications. |
| 3237 | * |
| 3238 | * A {@code NetworkCallback} is registered by calling |
| 3239 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, |
| 3240 | * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)}, |
Hugo Benichi | cbfbb37 | 2018-02-07 21:17:43 +0900 | [diff] [blame] | 3241 | * or {@link #registerDefaultNetworkCallback(NetworkCallback)}. A {@code NetworkCallback} is |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 3242 | * unregistered by calling {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 3243 | * A {@code NetworkCallback} should be registered at most once at any time. |
| 3244 | * A {@code NetworkCallback} that has been unregistered can be registered again. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3245 | */ |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3246 | public static class NetworkCallback { |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3247 | /** |
Lorenzo Colitti | 1457459 | 2015-04-24 12:23:24 +0900 | [diff] [blame] | 3248 | * Called when the framework connects to a new network to evaluate whether it satisfies this |
| 3249 | * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable} |
| 3250 | * callback. There is no guarantee that this new network will satisfy any requests, or that |
| 3251 | * the network will stay connected for longer than the time necessary to evaluate it. |
| 3252 | * <p> |
| 3253 | * Most applications <b>should not</b> act on this callback, and should instead use |
| 3254 | * {@link #onAvailable}. This callback is intended for use by applications that can assist |
| 3255 | * the framework in properly evaluating the network — for example, an application that |
| 3256 | * can automatically log in to a captive portal without user intervention. |
| 3257 | * |
| 3258 | * @param network The {@link Network} of the network that is being evaluated. |
Lorenzo Colitti | 3a9df1a | 2015-06-11 14:27:17 +0900 | [diff] [blame] | 3259 | * |
| 3260 | * @hide |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3261 | */ |
paulhu | 1a40765 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3262 | public void onPreCheck(@NonNull Network network) {} |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3263 | |
| 3264 | /** |
Lorenzo Colitti | 1457459 | 2015-04-24 12:23:24 +0900 | [diff] [blame] | 3265 | * 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] | 3266 | * This callback may be called more than once if the {@link Network} that is |
| 3267 | * satisfying the request changes. |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3268 | * |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3269 | * @param network The {@link Network} of the satisfying network. |
Chalard Jean | a23bc9e | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3270 | * @param networkCapabilities The {@link NetworkCapabilities} of the satisfying network. |
| 3271 | * @param linkProperties The {@link LinkProperties} of the satisfying network. |
junyulai | f2c67e4 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3272 | * @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] | 3273 | * @hide |
| 3274 | */ |
paulhu | 1a40765 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3275 | public void onAvailable(@NonNull Network network, |
| 3276 | @NonNull NetworkCapabilities networkCapabilities, |
| 3277 | @NonNull LinkProperties linkProperties, boolean blocked) { |
Chalard Jean | a23bc9e | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3278 | // Internally only this method is called when a new network is available, and |
| 3279 | // it calls the callback in the same way and order that older versions used |
| 3280 | // to call so as not to change the behavior. |
| 3281 | onAvailable(network); |
| 3282 | if (!networkCapabilities.hasCapability( |
| 3283 | NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)) { |
| 3284 | onNetworkSuspended(network); |
| 3285 | } |
| 3286 | onCapabilitiesChanged(network, networkCapabilities); |
| 3287 | onLinkPropertiesChanged(network, linkProperties); |
junyulai | f2c67e4 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3288 | onBlockedStatusChanged(network, blocked); |
Chalard Jean | a23bc9e | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3289 | } |
| 3290 | |
| 3291 | /** |
| 3292 | * 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] | 3293 | * |
| 3294 | * <p>For callbacks registered with {@link #registerNetworkCallback}, multiple networks may |
| 3295 | * be available at the same time, and onAvailable will be called for each of these as they |
| 3296 | * appear. |
| 3297 | * |
| 3298 | * <p>For callbacks registered with {@link #requestNetwork} and |
| 3299 | * {@link #registerDefaultNetworkCallback}, this means the network passed as an argument |
| 3300 | * is the new best network for this request and is now tracked by this callback ; this |
| 3301 | * callback will no longer receive method calls about other networks that may have been |
| 3302 | * passed to this method previously. The previously-best network may have disconnected, or |
| 3303 | * it may still be around and the newly-best network may simply be better. |
| 3304 | * |
| 3305 | * <p>Starting with {@link android.os.Build.VERSION_CODES#O}, this will always immediately |
| 3306 | * be followed by a call to {@link #onCapabilitiesChanged(Network, NetworkCapabilities)} |
| 3307 | * then by a call to {@link #onLinkPropertiesChanged(Network, LinkProperties)}, and a call |
| 3308 | * to {@link #onBlockedStatusChanged(Network, boolean)}. |
| 3309 | * |
| 3310 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 3311 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 3312 | * this callback as this is prone to race conditions (there is no guarantee the objects |
| 3313 | * returned by these methods will be current). Instead, wait for a call to |
| 3314 | * {@link #onCapabilitiesChanged(Network, NetworkCapabilities)} and |
| 3315 | * {@link #onLinkPropertiesChanged(Network, LinkProperties)} whose arguments are guaranteed |
| 3316 | * to be well-ordered with respect to other callbacks. |
Chalard Jean | a23bc9e | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3317 | * |
| 3318 | * @param network The {@link Network} of the satisfying network. |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3319 | */ |
paulhu | 1a40765 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3320 | public void onAvailable(@NonNull Network network) {} |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3321 | |
| 3322 | /** |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3323 | * Called when the network is about to be lost, typically because there are no outstanding |
| 3324 | * requests left for it. This may be paired with a {@link NetworkCallback#onAvailable} call |
| 3325 | * with the new replacement network for graceful handover. This method is not guaranteed |
| 3326 | * to be called before {@link NetworkCallback#onLost} is called, for example in case a |
| 3327 | * network is suddenly disconnected. |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3328 | * |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3329 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 3330 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 3331 | * this callback as this is prone to race conditions ; calling these methods while in a |
| 3332 | * callback may return an outdated or even a null object. |
| 3333 | * |
| 3334 | * @param network The {@link Network} that is about to be lost. |
| 3335 | * @param maxMsToLive The time in milliseconds the system intends to keep the network |
| 3336 | * connected for graceful handover; note that the network may still |
| 3337 | * suffer a hard loss at any time. |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3338 | */ |
paulhu | 1a40765 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3339 | public void onLosing(@NonNull Network network, int maxMsToLive) {} |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3340 | |
| 3341 | /** |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3342 | * Called when a network disconnects or otherwise no longer satisfies this request or |
| 3343 | * callback. |
| 3344 | * |
| 3345 | * <p>If the callback was registered with requestNetwork() or |
| 3346 | * registerDefaultNetworkCallback(), it will only be invoked against the last network |
| 3347 | * returned by onAvailable() when that network is lost and no other network satisfies |
| 3348 | * the criteria of the request. |
| 3349 | * |
| 3350 | * <p>If the callback was registered with registerNetworkCallback() it will be called for |
| 3351 | * each network which no longer satisfies the criteria of the callback. |
| 3352 | * |
| 3353 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 3354 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 3355 | * this callback as this is prone to race conditions ; calling these methods while in a |
| 3356 | * callback may return an outdated or even a null object. |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3357 | * |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3358 | * @param network The {@link Network} lost. |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3359 | */ |
paulhu | 1a40765 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3360 | public void onLost(@NonNull Network network) {} |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3361 | |
| 3362 | /** |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3363 | * Called if no network is found within the timeout time specified in |
Etan Cohen | fbfdd84 | 2019-01-08 12:09:18 -0800 | [diff] [blame] | 3364 | * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} call or if the |
| 3365 | * requested network request cannot be fulfilled (whether or not a timeout was |
| 3366 | * specified). When this callback is invoked the associated |
Etan Cohen | f67bde9 | 2017-03-01 12:47:28 -0800 | [diff] [blame] | 3367 | * {@link NetworkRequest} will have already been removed and released, as if |
| 3368 | * {@link #unregisterNetworkCallback(NetworkCallback)} had been called. |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3369 | */ |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3370 | public void onUnavailable() {} |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3371 | |
| 3372 | /** |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3373 | * Called when the network corresponding to this request changes capabilities but still |
| 3374 | * satisfies the requested criteria. |
| 3375 | * |
| 3376 | * <p>Starting with {@link android.os.Build.VERSION_CODES#O} this method is guaranteed |
| 3377 | * to be called immediately after {@link #onAvailable}. |
| 3378 | * |
| 3379 | * <p>Do NOT call {@link #getLinkProperties(Network)} or other synchronous |
| 3380 | * ConnectivityManager methods in this callback as this is prone to race conditions : |
| 3381 | * 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] | 3382 | * |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3383 | * @param network The {@link Network} whose capabilities have changed. |
Chalard Jean | a23bc9e | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3384 | * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this |
| 3385 | * network. |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3386 | */ |
paulhu | 1a40765 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3387 | public void onCapabilitiesChanged(@NonNull Network network, |
| 3388 | @NonNull NetworkCapabilities networkCapabilities) {} |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3389 | |
| 3390 | /** |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3391 | * Called when the network corresponding to this request changes {@link LinkProperties}. |
| 3392 | * |
| 3393 | * <p>Starting with {@link android.os.Build.VERSION_CODES#O} this method is guaranteed |
| 3394 | * to be called immediately after {@link #onAvailable}. |
| 3395 | * |
| 3396 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or other synchronous |
| 3397 | * ConnectivityManager methods in this callback as this is prone to race conditions : |
| 3398 | * 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] | 3399 | * |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3400 | * @param network The {@link Network} whose link properties have changed. |
| 3401 | * @param linkProperties The new {@link LinkProperties} for this network. |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3402 | */ |
paulhu | 1a40765 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3403 | public void onLinkPropertiesChanged(@NonNull Network network, |
| 3404 | @NonNull LinkProperties linkProperties) {} |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3405 | |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3406 | /** |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3407 | * Called when the network the framework connected to for this request suspends data |
| 3408 | * transmission temporarily. |
| 3409 | * |
| 3410 | * <p>This generally means that while the TCP connections are still live temporarily |
| 3411 | * network data fails to transfer. To give a specific example, this is used on cellular |
| 3412 | * networks to mask temporary outages when driving through a tunnel, etc. In general this |
| 3413 | * means read operations on sockets on this network will block once the buffers are |
| 3414 | * drained, and write operations will block once the buffers are full. |
| 3415 | * |
| 3416 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 3417 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 3418 | * this callback as this is prone to race conditions (there is no guarantee the objects |
| 3419 | * returned by these methods will be current). |
| 3420 | * |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3421 | * @hide |
| 3422 | */ |
paulhu | 1a40765 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3423 | public void onNetworkSuspended(@NonNull Network network) {} |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3424 | |
| 3425 | /** |
| 3426 | * Called when the network the framework connected to for this request |
Chalard Jean | a23bc9e | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3427 | * returns from a {@link NetworkInfo.State#SUSPENDED} state. This should always be |
| 3428 | * preceded by a matching {@link NetworkCallback#onNetworkSuspended} call. |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3429 | |
| 3430 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 3431 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 3432 | * this callback as this is prone to race conditions : calling these methods while in a |
| 3433 | * callback may return an outdated or even a null object. |
| 3434 | * |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3435 | * @hide |
| 3436 | */ |
paulhu | 1a40765 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3437 | public void onNetworkResumed(@NonNull Network network) {} |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3438 | |
junyulai | f2c67e4 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3439 | /** |
| 3440 | * Called when access to the specified network is blocked or unblocked. |
| 3441 | * |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3442 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 3443 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 3444 | * this callback as this is prone to race conditions : calling these methods while in a |
| 3445 | * callback may return an outdated or even a null object. |
| 3446 | * |
junyulai | f2c67e4 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3447 | * @param network The {@link Network} whose blocked status has changed. |
| 3448 | * @param blocked The blocked status of this {@link Network}. |
| 3449 | */ |
junyulai | 7e06ad4 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 3450 | public void onBlockedStatusChanged(@NonNull Network network, boolean blocked) {} |
junyulai | f2c67e4 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3451 | |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3452 | private NetworkRequest networkRequest; |
Robert Greenwalt | e20f7a2 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3453 | } |
| 3454 | |
Hugo Benichi | a590ab8 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 3455 | /** |
| 3456 | * Constant error codes used by ConnectivityService to communicate about failures and errors |
| 3457 | * across a Binder boundary. |
| 3458 | * @hide |
| 3459 | */ |
| 3460 | public interface Errors { |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 3461 | int TOO_MANY_REQUESTS = 1; |
Hugo Benichi | a590ab8 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 3462 | } |
| 3463 | |
| 3464 | /** @hide */ |
| 3465 | public static class TooManyRequestsException extends RuntimeException {} |
| 3466 | |
| 3467 | private static RuntimeException convertServiceException(ServiceSpecificException e) { |
| 3468 | switch (e.errorCode) { |
| 3469 | case Errors.TOO_MANY_REQUESTS: |
| 3470 | return new TooManyRequestsException(); |
| 3471 | default: |
| 3472 | Log.w(TAG, "Unknown service error code " + e.errorCode); |
| 3473 | return new RuntimeException(e); |
| 3474 | } |
| 3475 | } |
| 3476 | |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3477 | private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER; |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3478 | /** @hide */ |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3479 | public static final int CALLBACK_PRECHECK = BASE + 1; |
| 3480 | /** @hide */ |
| 3481 | public static final int CALLBACK_AVAILABLE = BASE + 2; |
| 3482 | /** @hide arg1 = TTL */ |
| 3483 | public static final int CALLBACK_LOSING = BASE + 3; |
| 3484 | /** @hide */ |
| 3485 | public static final int CALLBACK_LOST = BASE + 4; |
| 3486 | /** @hide */ |
| 3487 | public static final int CALLBACK_UNAVAIL = BASE + 5; |
| 3488 | /** @hide */ |
| 3489 | public static final int CALLBACK_CAP_CHANGED = BASE + 6; |
| 3490 | /** @hide */ |
| 3491 | public static final int CALLBACK_IP_CHANGED = BASE + 7; |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 3492 | /** @hide obj = NetworkCapabilities, arg1 = seq number */ |
Hugo Benichi | f8a0f9f | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 3493 | private static final int EXPIRE_LEGACY_REQUEST = BASE + 8; |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3494 | /** @hide */ |
Hugo Benichi | f8a0f9f | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 3495 | public static final int CALLBACK_SUSPENDED = BASE + 9; |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3496 | /** @hide */ |
Hugo Benichi | f8a0f9f | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 3497 | public static final int CALLBACK_RESUMED = BASE + 10; |
junyulai | f2c67e4 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3498 | /** @hide */ |
| 3499 | public static final int CALLBACK_BLK_CHANGED = BASE + 11; |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3500 | |
Erik Kline | 155a59a | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3501 | /** @hide */ |
| 3502 | public static String getCallbackName(int whichCallback) { |
| 3503 | switch (whichCallback) { |
| 3504 | case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK"; |
| 3505 | case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE"; |
| 3506 | case CALLBACK_LOSING: return "CALLBACK_LOSING"; |
| 3507 | case CALLBACK_LOST: return "CALLBACK_LOST"; |
| 3508 | case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL"; |
| 3509 | case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED"; |
| 3510 | case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED"; |
Erik Kline | 155a59a | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3511 | case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST"; |
| 3512 | case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED"; |
| 3513 | case CALLBACK_RESUMED: return "CALLBACK_RESUMED"; |
junyulai | f2c67e4 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3514 | case CALLBACK_BLK_CHANGED: return "CALLBACK_BLK_CHANGED"; |
Erik Kline | 155a59a | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3515 | default: |
| 3516 | return Integer.toString(whichCallback); |
| 3517 | } |
| 3518 | } |
| 3519 | |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 3520 | private class CallbackHandler extends Handler { |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3521 | private static final String TAG = "ConnectivityManager.CallbackHandler"; |
Robert Greenwalt | 72877c2 | 2015-09-03 16:41:45 -0700 | [diff] [blame] | 3522 | private static final boolean DBG = false; |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3523 | |
Hugo Benichi | 0b42baf | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3524 | CallbackHandler(Looper looper) { |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3525 | super(looper); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3526 | } |
| 3527 | |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3528 | CallbackHandler(Handler handler) { |
Hugo Benichi | bc1104b | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 3529 | this(Preconditions.checkNotNull(handler, "Handler cannot be null.").getLooper()); |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3530 | } |
| 3531 | |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3532 | @Override |
| 3533 | public void handleMessage(Message message) { |
Hugo Benichi | b6c2406 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3534 | if (message.what == EXPIRE_LEGACY_REQUEST) { |
| 3535 | expireRequest((NetworkCapabilities) message.obj, message.arg1); |
| 3536 | return; |
| 3537 | } |
| 3538 | |
| 3539 | final NetworkRequest request = getObject(message, NetworkRequest.class); |
| 3540 | final Network network = getObject(message, Network.class); |
| 3541 | final NetworkCallback callback; |
| 3542 | synchronized (sCallbacks) { |
| 3543 | callback = sCallbacks.get(request); |
Etan Cohen | b58e366 | 2019-05-21 12:06:04 -0700 | [diff] [blame] | 3544 | if (callback == null) { |
| 3545 | Log.w(TAG, |
| 3546 | "callback not found for " + getCallbackName(message.what) + " message"); |
| 3547 | return; |
| 3548 | } |
Etan Cohen | 6cb6599 | 2019-04-16 15:07:55 -0700 | [diff] [blame] | 3549 | if (message.what == CALLBACK_UNAVAIL) { |
| 3550 | sCallbacks.remove(request); |
| 3551 | callback.networkRequest = ALREADY_UNREGISTERED; |
| 3552 | } |
Hugo Benichi | b6c2406 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3553 | } |
Lorenzo Colitti | b027e6e | 2016-03-01 22:56:37 +0900 | [diff] [blame] | 3554 | if (DBG) { |
Hugo Benichi | 8d96292 | 2017-03-22 17:07:57 +0900 | [diff] [blame] | 3555 | Log.d(TAG, getCallbackName(message.what) + " for network " + network); |
Lorenzo Colitti | b027e6e | 2016-03-01 22:56:37 +0900 | [diff] [blame] | 3556 | } |
Hugo Benichi | b6c2406 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3557 | |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3558 | switch (message.what) { |
| 3559 | case CALLBACK_PRECHECK: { |
Hugo Benichi | b6c2406 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3560 | callback.onPreCheck(network); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3561 | break; |
| 3562 | } |
| 3563 | case CALLBACK_AVAILABLE: { |
Chalard Jean | a23bc9e | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3564 | NetworkCapabilities cap = getObject(message, NetworkCapabilities.class); |
| 3565 | LinkProperties lp = getObject(message, LinkProperties.class); |
junyulai | f2c67e4 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3566 | callback.onAvailable(network, cap, lp, message.arg1 != 0); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3567 | break; |
| 3568 | } |
| 3569 | case CALLBACK_LOSING: { |
Hugo Benichi | b6c2406 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3570 | callback.onLosing(network, message.arg1); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3571 | break; |
| 3572 | } |
| 3573 | case CALLBACK_LOST: { |
Hugo Benichi | b6c2406 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3574 | callback.onLost(network); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3575 | break; |
| 3576 | } |
| 3577 | case CALLBACK_UNAVAIL: { |
Hugo Benichi | b6c2406 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3578 | callback.onUnavailable(); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3579 | break; |
| 3580 | } |
| 3581 | case CALLBACK_CAP_CHANGED: { |
Hugo Benichi | b6c2406 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3582 | NetworkCapabilities cap = getObject(message, NetworkCapabilities.class); |
| 3583 | callback.onCapabilitiesChanged(network, cap); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3584 | break; |
| 3585 | } |
| 3586 | case CALLBACK_IP_CHANGED: { |
Hugo Benichi | b6c2406 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3587 | LinkProperties lp = getObject(message, LinkProperties.class); |
| 3588 | callback.onLinkPropertiesChanged(network, lp); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3589 | break; |
| 3590 | } |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3591 | case CALLBACK_SUSPENDED: { |
Hugo Benichi | b6c2406 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3592 | callback.onNetworkSuspended(network); |
Robert Greenwalt | 7fb8adc | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3593 | break; |
| 3594 | } |
| 3595 | case CALLBACK_RESUMED: { |
Hugo Benichi | b6c2406 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3596 | callback.onNetworkResumed(network); |
Robert Greenwalt | 3dc73e5 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 3597 | break; |
| 3598 | } |
junyulai | f2c67e4 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3599 | case CALLBACK_BLK_CHANGED: { |
| 3600 | boolean blocked = message.arg1 != 0; |
| 3601 | callback.onBlockedStatusChanged(network, blocked); |
| 3602 | } |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3603 | } |
| 3604 | } |
| 3605 | |
Hugo Benichi | 0b42baf | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3606 | private <T> T getObject(Message msg, Class<T> c) { |
| 3607 | return (T) msg.getData().getParcelable(c.getSimpleName()); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3608 | } |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3609 | } |
| 3610 | |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3611 | private CallbackHandler getDefaultHandler() { |
Hugo Benichi | 3b41f05 | 2016-07-07 10:15:56 +0900 | [diff] [blame] | 3612 | synchronized (sCallbacks) { |
| 3613 | if (sCallbackHandler == null) { |
| 3614 | sCallbackHandler = new CallbackHandler(ConnectivityThread.getInstanceLooper()); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3615 | } |
Hugo Benichi | 3b41f05 | 2016-07-07 10:15:56 +0900 | [diff] [blame] | 3616 | return sCallbackHandler; |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3617 | } |
| 3618 | } |
| 3619 | |
Hugo Benichi | bc4ac97 | 2017-02-03 14:18:44 +0900 | [diff] [blame] | 3620 | private static final HashMap<NetworkRequest, NetworkCallback> sCallbacks = new HashMap<>(); |
| 3621 | private static CallbackHandler sCallbackHandler; |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3622 | |
Hugo Benichi | bc4ac97 | 2017-02-03 14:18:44 +0900 | [diff] [blame] | 3623 | private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, NetworkCallback callback, |
junyulai | ad01079 | 2021-01-11 16:53:38 +0800 | [diff] [blame] | 3624 | int timeoutMs, NetworkRequest.Type reqType, int legacyType, CallbackHandler handler) { |
Soi, Yoshinari | b8102ef | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 3625 | printStackTrace(); |
Hugo Benichi | bc1104b | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 3626 | checkCallbackNotNull(callback); |
junyulai | ad01079 | 2021-01-11 16:53:38 +0800 | [diff] [blame] | 3627 | Preconditions.checkArgument( |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 3628 | reqType == TRACK_DEFAULT || reqType == TRACK_SYSTEM_DEFAULT || need != null, |
| 3629 | "null NetworkCapabilities"); |
Hugo Benichi | 0b42baf | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3630 | final NetworkRequest request; |
Roshan Pius | bc7e37d | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 3631 | final String callingPackageName = mContext.getOpPackageName(); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3632 | try { |
Hugo Benichi | 0b42baf | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3633 | synchronized(sCallbacks) { |
Hugo Benichi | bee30fe | 2017-06-17 13:14:12 +0900 | [diff] [blame] | 3634 | if (callback.networkRequest != null |
| 3635 | && callback.networkRequest != ALREADY_UNREGISTERED) { |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 3636 | // TODO: throw exception instead and enforce 1:1 mapping of callbacks |
| 3637 | // and requests (http://b/20701525). |
| 3638 | Log.e(TAG, "NetworkCallback was already registered"); |
| 3639 | } |
Hugo Benichi | 3b41f05 | 2016-07-07 10:15:56 +0900 | [diff] [blame] | 3640 | Messenger messenger = new Messenger(handler); |
Hugo Benichi | 0b42baf | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3641 | Binder binder = new Binder(); |
junyulai | ad01079 | 2021-01-11 16:53:38 +0800 | [diff] [blame] | 3642 | if (reqType == LISTEN) { |
Roshan Pius | bc7e37d | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 3643 | request = mService.listenForNetwork( |
Roshan Pius | aa24fde | 2020-12-17 14:53:09 -0800 | [diff] [blame] | 3644 | need, messenger, binder, callingPackageName, |
| 3645 | getAttributionTag()); |
Paul Jensen | bb42768 | 2014-06-27 11:05:32 -0400 | [diff] [blame] | 3646 | } else { |
Hugo Benichi | 0b42baf | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3647 | request = mService.requestNetwork( |
junyulai | ad01079 | 2021-01-11 16:53:38 +0800 | [diff] [blame] | 3648 | need, reqType.ordinal(), messenger, timeoutMs, binder, legacyType, |
| 3649 | callingPackageName, getAttributionTag()); |
Paul Jensen | bb42768 | 2014-06-27 11:05:32 -0400 | [diff] [blame] | 3650 | } |
Hugo Benichi | 0b42baf | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3651 | if (request != null) { |
Hugo Benichi | 3b41f05 | 2016-07-07 10:15:56 +0900 | [diff] [blame] | 3652 | sCallbacks.put(request, callback); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3653 | } |
Hugo Benichi | 3b41f05 | 2016-07-07 10:15:56 +0900 | [diff] [blame] | 3654 | callback.networkRequest = request; |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3655 | } |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3656 | } catch (RemoteException e) { |
| 3657 | throw e.rethrowFromSystemServer(); |
Hugo Benichi | a590ab8 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 3658 | } catch (ServiceSpecificException e) { |
| 3659 | throw convertServiceException(e); |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3660 | } |
Hugo Benichi | 0b42baf | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3661 | return request; |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3662 | } |
| 3663 | |
| 3664 | /** |
Erik Kline | 23bf99c | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 3665 | * Helper function to request a network with a particular legacy type. |
Lorenzo Colitti | 6653c4d | 2015-11-25 12:00:52 +0900 | [diff] [blame] | 3666 | * |
markchien | fac84a2 | 2020-03-18 21:16:15 +0800 | [diff] [blame] | 3667 | * This API is only for use in internal system code that requests networks with legacy type and |
| 3668 | * relies on CONNECTIVITY_ACTION broadcasts instead of NetworkCallbacks. New caller should use |
markchien | d6eeffd | 2020-01-14 00:55:21 +0800 | [diff] [blame] | 3669 | * {@link #requestNetwork(NetworkRequest, NetworkCallback, Handler)} instead. |
Lorenzo Colitti | 6653c4d | 2015-11-25 12:00:52 +0900 | [diff] [blame] | 3670 | * |
markchien | d6eeffd | 2020-01-14 00:55:21 +0800 | [diff] [blame] | 3671 | * @param request {@link NetworkRequest} describing this request. |
markchien | d6eeffd | 2020-01-14 00:55:21 +0800 | [diff] [blame] | 3672 | * @param timeoutMs The time in milliseconds to attempt looking for a suitable network |
| 3673 | * before {@link NetworkCallback#onUnavailable()} is called. The timeout must |
| 3674 | * be a positive value (i.e. >0). |
| 3675 | * @param legacyType to specify the network type(#TYPE_*). |
| 3676 | * @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] | 3677 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 3678 | * the callback must not be shared - it uniquely specifies this request. |
Lorenzo Colitti | 6653c4d | 2015-11-25 12:00:52 +0900 | [diff] [blame] | 3679 | * |
| 3680 | * @hide |
| 3681 | */ |
markchien | d6eeffd | 2020-01-14 00:55:21 +0800 | [diff] [blame] | 3682 | @SystemApi |
markchien | fac84a2 | 2020-03-18 21:16:15 +0800 | [diff] [blame] | 3683 | @RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 3684 | public void requestNetwork(@NonNull NetworkRequest request, |
markchien | fac84a2 | 2020-03-18 21:16:15 +0800 | [diff] [blame] | 3685 | int timeoutMs, int legacyType, @NonNull Handler handler, |
| 3686 | @NonNull NetworkCallback networkCallback) { |
| 3687 | if (legacyType == TYPE_NONE) { |
| 3688 | throw new IllegalArgumentException("TYPE_NONE is meaningless legacy type"); |
| 3689 | } |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3690 | CallbackHandler cbHandler = new CallbackHandler(handler); |
| 3691 | NetworkCapabilities nc = request.networkCapabilities; |
| 3692 | sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, legacyType, cbHandler); |
Lorenzo Colitti | 6653c4d | 2015-11-25 12:00:52 +0900 | [diff] [blame] | 3693 | } |
| 3694 | |
| 3695 | /** |
Lorenzo Colitti | 21e9a15 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 3696 | * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3697 | * |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3698 | * <p>This method will attempt to find the best network that matches the passed |
| 3699 | * {@link NetworkRequest}, and to bring up one that does if none currently satisfies the |
| 3700 | * criteria. The platform will evaluate which network is the best at its own discretion. |
| 3701 | * Throughput, latency, cost per byte, policy, user preference and other considerations |
| 3702 | * may be factored in the decision of what is considered the best network. |
| 3703 | * |
| 3704 | * <p>As long as this request is outstanding, the platform will try to maintain the best network |
| 3705 | * matching this request, while always attempting to match the request to a better network if |
| 3706 | * possible. If a better match is found, the platform will switch this request to the now-best |
| 3707 | * network and inform the app of the newly best network by invoking |
| 3708 | * {@link NetworkCallback#onAvailable(Network)} on the provided callback. Note that the platform |
| 3709 | * will not try to maintain any other network than the best one currently matching the request: |
| 3710 | * a network not matching any network request may be disconnected at any time. |
| 3711 | * |
| 3712 | * <p>For example, an application could use this method to obtain a connected cellular network |
| 3713 | * even if the device currently has a data connection over Ethernet. This may cause the cellular |
| 3714 | * radio to consume additional power. Or, an application could inform the system that it wants |
| 3715 | * a network supporting sending MMSes and have the system let it know about the currently best |
| 3716 | * MMS-supporting network through the provided {@link NetworkCallback}. |
| 3717 | * |
| 3718 | * <p>The status of the request can be followed by listening to the various callbacks described |
| 3719 | * in {@link NetworkCallback}. The {@link Network} object passed to the callback methods can be |
| 3720 | * used to direct traffic to the network (although accessing some networks may be subject to |
| 3721 | * holding specific permissions). Callers will learn about the specific characteristics of the |
| 3722 | * network through |
| 3723 | * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)} and |
| 3724 | * {@link NetworkCallback#onLinkPropertiesChanged(Network, LinkProperties)}. The methods of the |
| 3725 | * provided {@link NetworkCallback} will only be invoked due to changes in the best network |
| 3726 | * matching the request at any given time; therefore when a better network matching the request |
| 3727 | * becomes available, the {@link NetworkCallback#onAvailable(Network)} method is called |
| 3728 | * with the new network after which no further updates are given about the previously-best |
| 3729 | * network, unless it becomes the best again at some later time. All callbacks are invoked |
| 3730 | * in order on the same thread, which by default is a thread created by the framework running |
| 3731 | * in the app. |
| 3732 | * {@see #requestNetwork(NetworkRequest, NetworkCallback, Handler)} to change where the |
| 3733 | * callbacks are invoked. |
| 3734 | * |
| 3735 | * <p>This{@link NetworkRequest} will live until released via |
| 3736 | * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits, at |
| 3737 | * which point the system may let go of the network at any time. |
| 3738 | * |
| 3739 | * <p>A version of this method which takes a timeout is |
| 3740 | * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}, that an app can use to only |
| 3741 | * wait for a limited amount of time for the network to become unavailable. |
| 3742 | * |
Paul Jensen | ee52d23 | 2015-06-16 15:11:58 -0400 | [diff] [blame] | 3743 | * <p>It is presently unsupported to request a network with mutable |
| 3744 | * {@link NetworkCapabilities} such as |
| 3745 | * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or |
| 3746 | * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL} |
| 3747 | * as these {@code NetworkCapabilities} represent states that a particular |
| 3748 | * network may never attain, and whether a network will attain these states |
| 3749 | * 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] | 3750 | * know how to go about satisfying a request with these capabilities. |
Lorenzo Colitti | 0b40c87 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 3751 | * |
| 3752 | * <p>This method requires the caller to hold either the |
| 3753 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 3754 | * or the ability to modify system settings as determined by |
| 3755 | * {@link android.provider.Settings.System#canWrite}.</p> |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3756 | * |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 3757 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 3758 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 3759 | * all variants of this method, of {@link #registerNetworkCallback} as well as |
| 3760 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 3761 | * Requesting a network with this method will count toward this limit. If this limit is |
| 3762 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 3763 | * make sure to unregister the callbacks with |
| 3764 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 3765 | * |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3766 | * @param request {@link NetworkRequest} describing this request. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3767 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 3768 | * the callback must not be shared - it uniquely specifies this request. |
| 3769 | * The callback is invoked on the default internal Handler. |
Chalard Jean | 31740e4 | 2019-05-13 15:13:58 +0900 | [diff] [blame] | 3770 | * @throws IllegalArgumentException if {@code request} contains invalid network capabilities. |
| 3771 | * @throws SecurityException if missing the appropriate permissions. |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 3772 | * @throws RuntimeException if the app already has too many callbacks registered. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3773 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 3774 | public void requestNetwork(@NonNull NetworkRequest request, |
| 3775 | @NonNull NetworkCallback networkCallback) { |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3776 | requestNetwork(request, networkCallback, getDefaultHandler()); |
| 3777 | } |
| 3778 | |
| 3779 | /** |
| 3780 | * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}. |
| 3781 | * |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3782 | * This method behaves identically to {@link #requestNetwork(NetworkRequest, NetworkCallback)} |
| 3783 | * but runs all the callbacks on the passed Handler. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3784 | * |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3785 | * <p>This method has the same permission requirements as |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 3786 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations, |
| 3787 | * and throws the same exceptions in the same conditions. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3788 | * |
| 3789 | * @param request {@link NetworkRequest} describing this request. |
| 3790 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 3791 | * the callback must not be shared - it uniquely specifies this request. |
| 3792 | * @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] | 3793 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 3794 | public void requestNetwork(@NonNull NetworkRequest request, |
| 3795 | @NonNull NetworkCallback networkCallback, @NonNull Handler handler) { |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3796 | CallbackHandler cbHandler = new CallbackHandler(handler); |
markchien | fac84a2 | 2020-03-18 21:16:15 +0800 | [diff] [blame] | 3797 | NetworkCapabilities nc = request.networkCapabilities; |
| 3798 | sendRequestForNetwork(nc, networkCallback, 0, REQUEST, TYPE_NONE, cbHandler); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3799 | } |
| 3800 | |
| 3801 | /** |
Etan Cohen | f67bde9 | 2017-03-01 12:47:28 -0800 | [diff] [blame] | 3802 | * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited |
| 3803 | * by a timeout. |
| 3804 | * |
| 3805 | * This function behaves identically to the non-timed-out version |
| 3806 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network |
| 3807 | * is not found within the given time (in milliseconds) the |
| 3808 | * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be |
| 3809 | * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does |
| 3810 | * not have to be released if timed-out (it is automatically released). Unregistering a |
| 3811 | * request that timed out is not an error. |
| 3812 | * |
| 3813 | * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small |
| 3814 | * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided |
| 3815 | * for that purpose. Calling this method will attempt to bring up the requested network. |
| 3816 | * |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3817 | * <p>This method has the same permission requirements as |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 3818 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations, |
| 3819 | * and throws the same exceptions in the same conditions. |
Etan Cohen | f67bde9 | 2017-03-01 12:47:28 -0800 | [diff] [blame] | 3820 | * |
| 3821 | * @param request {@link NetworkRequest} describing this request. |
Lorenzo Colitti | 139a48c | 2017-04-28 00:56:30 +0900 | [diff] [blame] | 3822 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 3823 | * the callback must not be shared - it uniquely specifies this request. |
Etan Cohen | f67bde9 | 2017-03-01 12:47:28 -0800 | [diff] [blame] | 3824 | * @param timeoutMs The time in milliseconds to attempt looking for a suitable network |
| 3825 | * before {@link NetworkCallback#onUnavailable()} is called. The timeout must |
| 3826 | * be a positive value (i.e. >0). |
Etan Cohen | f67bde9 | 2017-03-01 12:47:28 -0800 | [diff] [blame] | 3827 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 3828 | public void requestNetwork(@NonNull NetworkRequest request, |
| 3829 | @NonNull NetworkCallback networkCallback, int timeoutMs) { |
Hugo Benichi | bc1104b | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 3830 | checkTimeout(timeoutMs); |
markchien | fac84a2 | 2020-03-18 21:16:15 +0800 | [diff] [blame] | 3831 | NetworkCapabilities nc = request.networkCapabilities; |
| 3832 | sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, TYPE_NONE, |
| 3833 | getDefaultHandler()); |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3834 | } |
| 3835 | |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3836 | /** |
| 3837 | * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited |
| 3838 | * by a timeout. |
| 3839 | * |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3840 | * This method behaves identically to |
| 3841 | * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} but runs all the callbacks |
| 3842 | * on the passed Handler. |
Etan Cohen | f67bde9 | 2017-03-01 12:47:28 -0800 | [diff] [blame] | 3843 | * |
Chalard Jean | 01378b6 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3844 | * <p>This method has the same permission requirements as |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 3845 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, is subject to the same limitations, |
| 3846 | * and throws the same exceptions in the same conditions. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3847 | * |
| 3848 | * @param request {@link NetworkRequest} describing this request. |
Etan Cohen | f67bde9 | 2017-03-01 12:47:28 -0800 | [diff] [blame] | 3849 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 3850 | * the callback must not be shared - it uniquely specifies this request. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3851 | * @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] | 3852 | * @param timeoutMs The time in milliseconds to attempt looking for a suitable network |
| 3853 | * before {@link NetworkCallback#onUnavailable} is called. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3854 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 3855 | public void requestNetwork(@NonNull NetworkRequest request, |
| 3856 | @NonNull NetworkCallback networkCallback, @NonNull Handler handler, int timeoutMs) { |
Hugo Benichi | bc1104b | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 3857 | checkTimeout(timeoutMs); |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3858 | CallbackHandler cbHandler = new CallbackHandler(handler); |
markchien | fac84a2 | 2020-03-18 21:16:15 +0800 | [diff] [blame] | 3859 | NetworkCapabilities nc = request.networkCapabilities; |
| 3860 | sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, TYPE_NONE, cbHandler); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3861 | } |
| 3862 | |
| 3863 | /** |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3864 | * 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] | 3865 | * successfully finding a network for the applications request. Retrieve it with |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3866 | * {@link android.content.Intent#getParcelableExtra(String)}. |
Jeremy Joslin | c530263 | 2015-02-11 16:51:13 -0800 | [diff] [blame] | 3867 | * <p> |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 3868 | * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)} |
| 3869 | * then you must get a ConnectivityManager instance before doing so. |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3870 | */ |
Erik Kline | 8a82621 | 2014-11-19 12:12:24 +0900 | [diff] [blame] | 3871 | public static final String EXTRA_NETWORK = "android.net.extra.NETWORK"; |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3872 | |
| 3873 | /** |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3874 | * 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] | 3875 | * successfully finding a network for the applications request. Retrieve it with |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3876 | * {@link android.content.Intent#getParcelableExtra(String)}. |
| 3877 | */ |
Erik Kline | 8a82621 | 2014-11-19 12:12:24 +0900 | [diff] [blame] | 3878 | public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST"; |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3879 | |
| 3880 | |
| 3881 | /** |
Lorenzo Colitti | 21e9a15 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 3882 | * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3883 | * |
Jeremy Joslin | 60d379b | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3884 | * 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] | 3885 | * of {@link NetworkCallback} a {@link PendingIntent} is used. This means |
Robert Greenwalt | f3017f7 | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3886 | * the request may outlive the calling application and get called back when a suitable |
| 3887 | * network is found. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3888 | * <p> |
| 3889 | * The operation is an Intent broadcast that goes to a broadcast receiver that |
| 3890 | * you registered with {@link Context#registerReceiver} or through the |
| 3891 | * <receiver> tag in an AndroidManifest.xml file |
| 3892 | * <p> |
| 3893 | * The operation Intent is delivered with two extras, a {@link Network} typed |
Erik Kline | 8a82621 | 2014-11-19 12:12:24 +0900 | [diff] [blame] | 3894 | * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest} |
| 3895 | * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3896 | * the original requests parameters. It is important to create a new, |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3897 | * {@link NetworkCallback} based request before completing the processing of the |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3898 | * Intent to reserve the network or it will be released shortly after the Intent |
| 3899 | * is processed. |
| 3900 | * <p> |
Paul Jensen | c8873fc | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 3901 | * 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] | 3902 | * 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] | 3903 | * replaced by this one, effectively releasing the previous {@link NetworkRequest}. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3904 | * <p> |
Jeremy Joslin | 60d379b | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3905 | * The request may be released normally by calling |
| 3906 | * {@link #releaseNetworkRequest(android.app.PendingIntent)}. |
Paul Jensen | ee52d23 | 2015-06-16 15:11:58 -0400 | [diff] [blame] | 3907 | * <p>It is presently unsupported to request a network with either |
| 3908 | * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or |
| 3909 | * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL} |
| 3910 | * as these {@code NetworkCapabilities} represent states that a particular |
| 3911 | * network may never attain, and whether a network will attain these states |
| 3912 | * 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] | 3913 | * know how to go about satisfying a request with these capabilities. |
Lorenzo Colitti | 0b40c87 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 3914 | * |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 3915 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 3916 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 3917 | * all variants of this method, of {@link #registerNetworkCallback} as well as |
| 3918 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 3919 | * Requesting a network with this method will count toward this limit. If this limit is |
| 3920 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 3921 | * make sure to unregister the callbacks with {@link #unregisterNetworkCallback(PendingIntent)} |
| 3922 | * or {@link #releaseNetworkRequest(PendingIntent)}. |
| 3923 | * |
Lorenzo Colitti | 0b40c87 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 3924 | * <p>This method requires the caller to hold either the |
| 3925 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 3926 | * or the ability to modify system settings as determined by |
| 3927 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 3928 | * |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3929 | * @param request {@link NetworkRequest} describing this request. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3930 | * @param operation Action to perform when the network is available (corresponds |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3931 | * to the {@link NetworkCallback#onAvailable} call. Typically |
Jeremy Joslin | 60d379b | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3932 | * comes from {@link PendingIntent#getBroadcast}. Cannot be null. |
Chalard Jean | 31740e4 | 2019-05-13 15:13:58 +0900 | [diff] [blame] | 3933 | * @throws IllegalArgumentException if {@code request} contains invalid network capabilities. |
| 3934 | * @throws SecurityException if missing the appropriate permissions. |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 3935 | * @throws RuntimeException if the app already has too many callbacks registered. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3936 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 3937 | public void requestNetwork(@NonNull NetworkRequest request, |
| 3938 | @NonNull PendingIntent operation) { |
Soi, Yoshinari | b8102ef | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 3939 | printStackTrace(); |
Hugo Benichi | bc1104b | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 3940 | checkPendingIntentNotNull(operation); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3941 | try { |
Roshan Pius | bc7e37d | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 3942 | mService.pendingRequestForNetwork( |
Philip P. Moltmann | 7bc33df | 2020-03-26 11:50:35 -0700 | [diff] [blame] | 3943 | request.networkCapabilities, operation, mContext.getOpPackageName(), |
| 3944 | getAttributionTag()); |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3945 | } catch (RemoteException e) { |
| 3946 | throw e.rethrowFromSystemServer(); |
Hugo Benichi | a590ab8 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 3947 | } catch (ServiceSpecificException e) { |
| 3948 | throw convertServiceException(e); |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3949 | } |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3950 | } |
| 3951 | |
| 3952 | /** |
Jeremy Joslin | 60d379b | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3953 | * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} |
| 3954 | * <p> |
Lorenzo Colitti | 15874cd | 2016-04-13 22:00:02 +0900 | [diff] [blame] | 3955 | * This method has the same behavior as |
| 3956 | * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to |
Jeremy Joslin | 60d379b | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3957 | * releasing network resources and disconnecting. |
| 3958 | * |
| 3959 | * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the |
| 3960 | * PendingIntent passed to |
| 3961 | * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the |
| 3962 | * corresponding NetworkRequest you'd like to remove. Cannot be null. |
| 3963 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 3964 | public void releaseNetworkRequest(@NonNull PendingIntent operation) { |
Soi, Yoshinari | b8102ef | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 3965 | printStackTrace(); |
Hugo Benichi | bc1104b | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 3966 | checkPendingIntentNotNull(operation); |
Jeremy Joslin | 60d379b | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3967 | try { |
| 3968 | mService.releasePendingNetworkRequest(operation); |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3969 | } catch (RemoteException e) { |
| 3970 | throw e.rethrowFromSystemServer(); |
| 3971 | } |
Jeremy Joslin | 60d379b | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3972 | } |
| 3973 | |
Hugo Benichi | bc1104b | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 3974 | private static void checkPendingIntentNotNull(PendingIntent intent) { |
| 3975 | Preconditions.checkNotNull(intent, "PendingIntent cannot be null."); |
| 3976 | } |
| 3977 | |
| 3978 | private static void checkCallbackNotNull(NetworkCallback callback) { |
| 3979 | Preconditions.checkNotNull(callback, "null NetworkCallback"); |
| 3980 | } |
| 3981 | |
| 3982 | private static void checkTimeout(int timeoutMs) { |
| 3983 | Preconditions.checkArgumentPositive(timeoutMs, "timeoutMs must be strictly positive."); |
Jeremy Joslin | 60d379b | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3984 | } |
| 3985 | |
| 3986 | /** |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3987 | * Registers to receive notifications about all networks which satisfy the given |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3988 | * {@link NetworkRequest}. The callbacks will continue to be called until |
Chiachang Wang | 3b723c2 | 2019-02-27 17:14:50 +0800 | [diff] [blame] | 3989 | * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is |
| 3990 | * called. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3991 | * |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 3992 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 3993 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 3994 | * all variants of this method, of {@link #requestNetwork} as well as |
| 3995 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 3996 | * Requesting a network with this method will count toward this limit. If this limit is |
| 3997 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 3998 | * make sure to unregister the callbacks with |
| 3999 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 4000 | * |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 4001 | * @param request {@link NetworkRequest} describing this request. |
| 4002 | * @param networkCallback The {@link NetworkCallback} that the system will call as suitable |
| 4003 | * networks change state. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4004 | * The callback is invoked on the default internal Handler. |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4005 | * @throws RuntimeException if the app already has too many callbacks registered. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4006 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 4007 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4008 | public void registerNetworkCallback(@NonNull NetworkRequest request, |
| 4009 | @NonNull NetworkCallback networkCallback) { |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4010 | registerNetworkCallback(request, networkCallback, getDefaultHandler()); |
| 4011 | } |
| 4012 | |
| 4013 | /** |
| 4014 | * Registers to receive notifications about all networks which satisfy the given |
| 4015 | * {@link NetworkRequest}. The callbacks will continue to be called until |
Chiachang Wang | 3b723c2 | 2019-02-27 17:14:50 +0800 | [diff] [blame] | 4016 | * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is |
| 4017 | * called. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4018 | * |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4019 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4020 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4021 | * all variants of this method, of {@link #requestNetwork} as well as |
| 4022 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4023 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4024 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4025 | * make sure to unregister the callbacks with |
| 4026 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 4027 | * |
| 4028 | * |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4029 | * @param request {@link NetworkRequest} describing this request. |
| 4030 | * @param networkCallback The {@link NetworkCallback} that the system will call as suitable |
| 4031 | * networks change state. |
| 4032 | * @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] | 4033 | * @throws RuntimeException if the app already has too many callbacks registered. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4034 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 4035 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4036 | public void registerNetworkCallback(@NonNull NetworkRequest request, |
| 4037 | @NonNull NetworkCallback networkCallback, @NonNull Handler handler) { |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4038 | CallbackHandler cbHandler = new CallbackHandler(handler); |
| 4039 | NetworkCapabilities nc = request.networkCapabilities; |
| 4040 | sendRequestForNetwork(nc, networkCallback, 0, LISTEN, TYPE_NONE, cbHandler); |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4041 | } |
| 4042 | |
| 4043 | /** |
Paul Jensen | c8873fc | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 4044 | * Registers a PendingIntent to be sent when a network is available which satisfies the given |
| 4045 | * {@link NetworkRequest}. |
| 4046 | * |
| 4047 | * This function behaves identically to the version that takes a NetworkCallback, but instead |
| 4048 | * of {@link NetworkCallback} a {@link PendingIntent} is used. This means |
| 4049 | * the request may outlive the calling application and get called back when a suitable |
| 4050 | * network is found. |
| 4051 | * <p> |
| 4052 | * The operation is an Intent broadcast that goes to a broadcast receiver that |
| 4053 | * you registered with {@link Context#registerReceiver} or through the |
| 4054 | * <receiver> tag in an AndroidManifest.xml file |
| 4055 | * <p> |
| 4056 | * The operation Intent is delivered with two extras, a {@link Network} typed |
| 4057 | * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest} |
| 4058 | * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing |
| 4059 | * the original requests parameters. |
| 4060 | * <p> |
| 4061 | * If there is already a request for this Intent registered (with the equality of |
| 4062 | * two Intents defined by {@link Intent#filterEquals}), then it will be removed and |
| 4063 | * replaced by this one, effectively releasing the previous {@link NetworkRequest}. |
| 4064 | * <p> |
| 4065 | * The request may be released normally by calling |
Paul Jensen | 169f662 | 2015-06-30 14:29:18 -0400 | [diff] [blame] | 4066 | * {@link #unregisterNetworkCallback(android.app.PendingIntent)}. |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4067 | * |
| 4068 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4069 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4070 | * all variants of this method, of {@link #requestNetwork} as well as |
| 4071 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4072 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4073 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4074 | * make sure to unregister the callbacks with {@link #unregisterNetworkCallback(PendingIntent)} |
| 4075 | * or {@link #releaseNetworkRequest(PendingIntent)}. |
| 4076 | * |
Paul Jensen | c8873fc | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 4077 | * @param request {@link NetworkRequest} describing this request. |
| 4078 | * @param operation Action to perform when the network is available (corresponds |
| 4079 | * to the {@link NetworkCallback#onAvailable} call. Typically |
| 4080 | * comes from {@link PendingIntent#getBroadcast}. Cannot be null. |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4081 | * @throws RuntimeException if the app already has too many callbacks registered. |
Paul Jensen | c8873fc | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 4082 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 4083 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4084 | public void registerNetworkCallback(@NonNull NetworkRequest request, |
| 4085 | @NonNull PendingIntent operation) { |
Soi, Yoshinari | b8102ef | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 4086 | printStackTrace(); |
Hugo Benichi | bc1104b | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 4087 | checkPendingIntentNotNull(operation); |
Paul Jensen | c8873fc | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 4088 | try { |
Roshan Pius | bc7e37d | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 4089 | mService.pendingListenForNetwork( |
Roshan Pius | aa24fde | 2020-12-17 14:53:09 -0800 | [diff] [blame] | 4090 | request.networkCapabilities, operation, mContext.getOpPackageName(), |
| 4091 | getAttributionTag()); |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4092 | } catch (RemoteException e) { |
| 4093 | throw e.rethrowFromSystemServer(); |
Hugo Benichi | a590ab8 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 4094 | } catch (ServiceSpecificException e) { |
| 4095 | throw convertServiceException(e); |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4096 | } |
Paul Jensen | c8873fc | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 4097 | } |
| 4098 | |
| 4099 | /** |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 4100 | * Registers to receive notifications about changes in the application's default network. This |
| 4101 | * may be a physical network or a virtual network, such as a VPN that applies to the |
| 4102 | * 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] | 4103 | * {@link #unregisterNetworkCallback(NetworkCallback)} is called. |
Erik Kline | 23bf99c | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 4104 | * |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4105 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4106 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4107 | * all variants of this method, of {@link #requestNetwork} as well as |
| 4108 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4109 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4110 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4111 | * make sure to unregister the callbacks with |
| 4112 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 4113 | * |
Erik Kline | 23bf99c | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 4114 | * @param networkCallback The {@link NetworkCallback} that the system will call as the |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 4115 | * application's default network changes. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4116 | * The callback is invoked on the default internal Handler. |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4117 | * @throws RuntimeException if the app already has too many callbacks registered. |
Erik Kline | 23bf99c | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 4118 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 4119 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4120 | public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback) { |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4121 | registerDefaultNetworkCallback(networkCallback, getDefaultHandler()); |
| 4122 | } |
| 4123 | |
| 4124 | /** |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 4125 | * Registers to receive notifications about changes in the application's default network. This |
| 4126 | * may be a physical network or a virtual network, such as a VPN that applies to the |
| 4127 | * application. The callbacks will continue to be called until either the application exits or |
| 4128 | * {@link #unregisterNetworkCallback(NetworkCallback)} is called. |
| 4129 | * |
| 4130 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4131 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4132 | * all variants of this method, of {@link #requestNetwork} as well as |
| 4133 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4134 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4135 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4136 | * make sure to unregister the callbacks with |
| 4137 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 4138 | * |
| 4139 | * @param networkCallback The {@link NetworkCallback} that the system will call as the |
| 4140 | * application's default network changes. |
| 4141 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
| 4142 | * @throws RuntimeException if the app already has too many callbacks registered. |
| 4143 | */ |
| 4144 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
| 4145 | public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback, |
| 4146 | @NonNull Handler handler) { |
| 4147 | CallbackHandler cbHandler = new CallbackHandler(handler); |
| 4148 | sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0, |
| 4149 | TRACK_DEFAULT, TYPE_NONE, cbHandler); |
| 4150 | } |
| 4151 | |
| 4152 | /** |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4153 | * Registers to receive notifications about changes in the system default network. The callbacks |
| 4154 | * will continue to be called until either the application exits or |
| 4155 | * {@link #unregisterNetworkCallback(NetworkCallback)} is called. |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4156 | * |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 4157 | * This method should not be used to determine networking state seen by applications, because in |
| 4158 | * many cases, most or even all application traffic may not use the default network directly, |
| 4159 | * and traffic from different applications may go on different networks by default. As an |
| 4160 | * example, if a VPN is connected, traffic from all applications might be sent through the VPN |
| 4161 | * and not onto the system default network. Applications or system components desiring to do |
| 4162 | * determine network state as seen by applications should use other methods such as |
| 4163 | * {@link #registerDefaultNetworkCallback(NetworkCallback, Handler)}. |
| 4164 | * |
Chalard Jean | a5ff113 | 2020-05-20 16:11:50 +0900 | [diff] [blame] | 4165 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4166 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4167 | * all variants of this method, of {@link #requestNetwork} as well as |
| 4168 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4169 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4170 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4171 | * make sure to unregister the callbacks with |
| 4172 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 4173 | * |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4174 | * @param networkCallback The {@link NetworkCallback} that the system will call as the |
| 4175 | * system default network changes. |
| 4176 | * @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] | 4177 | * @throws RuntimeException if the app already has too many callbacks registered. |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 4178 | * |
| 4179 | * @hide |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4180 | */ |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 4181 | @SystemApi(client = MODULE_LIBRARIES) |
| 4182 | @SuppressLint({"ExecutorRegistration", "PairedRegistration"}) |
| 4183 | @RequiresPermission(anyOf = { |
| 4184 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 4185 | android.Manifest.permission.NETWORK_SETTINGS}) |
| 4186 | public void registerSystemDefaultNetworkCallback(@NonNull NetworkCallback networkCallback, |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4187 | @NonNull Handler handler) { |
Hugo Benichi | fd44e91 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4188 | CallbackHandler cbHandler = new CallbackHandler(handler); |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 4189 | sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0, |
Lorenzo Colitti | 76b639e | 2021-01-29 20:14:04 +0900 | [diff] [blame] | 4190 | TRACK_SYSTEM_DEFAULT, TYPE_NONE, cbHandler); |
Erik Kline | 23bf99c | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 4191 | } |
| 4192 | |
| 4193 | /** |
junyulai | 7514e31 | 2021-03-05 15:51:17 +0800 | [diff] [blame^] | 4194 | * @hide |
| 4195 | */ |
| 4196 | // TODO: Make it public api. |
| 4197 | @SuppressLint("ExecutorRegistration") |
| 4198 | public void registerBestMatchingNetworkCallback(@NonNull NetworkRequest request, |
| 4199 | @NonNull NetworkCallback networkCallback, @NonNull Handler handler) { |
| 4200 | final NetworkCapabilities nc = request.networkCapabilities; |
| 4201 | final CallbackHandler cbHandler = new CallbackHandler(handler); |
| 4202 | sendRequestForNetwork(nc, networkCallback, 0, TRACK_BEST, TYPE_NONE, cbHandler); |
| 4203 | } |
| 4204 | |
| 4205 | /** |
fenglu | b00f488 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 4206 | * Requests bandwidth update for a given {@link Network} and returns whether the update request |
| 4207 | * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying |
| 4208 | * network connection for updated bandwidth information. The caller will be notified via |
| 4209 | * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this |
Lorenzo Colitti | 15874cd | 2016-04-13 22:00:02 +0900 | [diff] [blame] | 4210 | * method assumes that the caller has previously called |
| 4211 | * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network |
| 4212 | * changes. |
fenglu | 3f35740 | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 4213 | * |
fenglu | ea2d928 | 2015-04-27 14:28:04 -0700 | [diff] [blame] | 4214 | * @param network {@link Network} specifying which network you're interested. |
fenglu | b00f488 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 4215 | * @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] | 4216 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4217 | public boolean requestBandwidthUpdate(@NonNull Network network) { |
fenglu | 3f35740 | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 4218 | try { |
fenglu | b00f488 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 4219 | return mService.requestBandwidthUpdate(network); |
fenglu | 3f35740 | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 4220 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4221 | throw e.rethrowFromSystemServer(); |
fenglu | 3f35740 | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 4222 | } |
| 4223 | } |
| 4224 | |
| 4225 | /** |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 4226 | * Unregisters a {@code NetworkCallback} and possibly releases networks originating from |
Lorenzo Colitti | 15874cd | 2016-04-13 22:00:02 +0900 | [diff] [blame] | 4227 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and |
| 4228 | * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls. |
| 4229 | * If the given {@code NetworkCallback} had previously been used with |
Lorenzo Colitti | 7f6a2bf | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 4230 | * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request |
| 4231 | * will be disconnected. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4232 | * |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 4233 | * Notifications that would have triggered that {@code NetworkCallback} will immediately stop |
| 4234 | * triggering it as soon as this call returns. |
| 4235 | * |
Robert Greenwalt | f57c03c | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 4236 | * @param networkCallback The {@link NetworkCallback} used when making the request. |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4237 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4238 | public void unregisterNetworkCallback(@NonNull NetworkCallback networkCallback) { |
Soi, Yoshinari | b8102ef | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 4239 | printStackTrace(); |
Hugo Benichi | bc1104b | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 4240 | checkCallbackNotNull(networkCallback); |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 4241 | final List<NetworkRequest> reqs = new ArrayList<>(); |
| 4242 | // Find all requests associated to this callback and stop callback triggers immediately. |
| 4243 | // Callback is reusable immediately. http://b/20701525, http://b/35921499. |
| 4244 | synchronized (sCallbacks) { |
Hugo Benichi | bee30fe | 2017-06-17 13:14:12 +0900 | [diff] [blame] | 4245 | Preconditions.checkArgument(networkCallback.networkRequest != null, |
| 4246 | "NetworkCallback was not registered"); |
Etan Cohen | 6cb6599 | 2019-04-16 15:07:55 -0700 | [diff] [blame] | 4247 | if (networkCallback.networkRequest == ALREADY_UNREGISTERED) { |
| 4248 | Log.d(TAG, "NetworkCallback was already unregistered"); |
| 4249 | return; |
| 4250 | } |
Hugo Benichi | 2aa65af | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 4251 | for (Map.Entry<NetworkRequest, NetworkCallback> e : sCallbacks.entrySet()) { |
| 4252 | if (e.getValue() == networkCallback) { |
| 4253 | reqs.add(e.getKey()); |
| 4254 | } |
| 4255 | } |
| 4256 | // TODO: throw exception if callback was registered more than once (http://b/20701525). |
| 4257 | for (NetworkRequest r : reqs) { |
| 4258 | try { |
| 4259 | mService.releaseNetworkRequest(r); |
| 4260 | } catch (RemoteException e) { |
| 4261 | throw e.rethrowFromSystemServer(); |
| 4262 | } |
| 4263 | // Only remove mapping if rpc was successful. |
| 4264 | sCallbacks.remove(r); |
| 4265 | } |
Hugo Benichi | bee30fe | 2017-06-17 13:14:12 +0900 | [diff] [blame] | 4266 | networkCallback.networkRequest = ALREADY_UNREGISTERED; |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4267 | } |
Robert Greenwalt | f99b839 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4268 | } |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4269 | |
| 4270 | /** |
Paul Jensen | 169f662 | 2015-06-30 14:29:18 -0400 | [diff] [blame] | 4271 | * Unregisters a callback previously registered via |
| 4272 | * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}. |
| 4273 | * |
| 4274 | * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the |
| 4275 | * PendingIntent passed to |
| 4276 | * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}. |
| 4277 | * Cannot be null. |
| 4278 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4279 | public void unregisterNetworkCallback(@NonNull PendingIntent operation) { |
Paul Jensen | 169f662 | 2015-06-30 14:29:18 -0400 | [diff] [blame] | 4280 | releaseNetworkRequest(operation); |
| 4281 | } |
| 4282 | |
| 4283 | /** |
Lorenzo Colitti | 6947c06 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 4284 | * Informs the system whether it should switch to {@code network} regardless of whether it is |
| 4285 | * validated or not. If {@code accept} is true, and the network was explicitly selected by the |
| 4286 | * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become |
| 4287 | * the system default network regardless of any other network that's currently connected. If |
| 4288 | * {@code always} is true, then the choice is remembered, so that the next time the user |
| 4289 | * connects to this network, the system will switch to it. |
| 4290 | * |
Lorenzo Colitti | 6947c06 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 4291 | * @param network The network to accept. |
| 4292 | * @param accept Whether to accept the network even if unvalidated. |
| 4293 | * @param always Whether to remember this choice in the future. |
| 4294 | * |
| 4295 | * @hide |
| 4296 | */ |
lucaslin | 2240ef6 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 4297 | @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS) |
Lorenzo Colitti | 6947c06 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 4298 | public void setAcceptUnvalidated(Network network, boolean accept, boolean always) { |
| 4299 | try { |
| 4300 | mService.setAcceptUnvalidated(network, accept, always); |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4301 | } catch (RemoteException e) { |
| 4302 | throw e.rethrowFromSystemServer(); |
| 4303 | } |
Lorenzo Colitti | 6947c06 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 4304 | } |
| 4305 | |
| 4306 | /** |
lucaslin | 2240ef6 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 4307 | * Informs the system whether it should consider the network as validated even if it only has |
| 4308 | * partial connectivity. If {@code accept} is true, then the network will be considered as |
| 4309 | * validated even if connectivity is only partial. If {@code always} is true, then the choice |
| 4310 | * is remembered, so that the next time the user connects to this network, the system will |
| 4311 | * switch to it. |
| 4312 | * |
| 4313 | * @param network The network to accept. |
| 4314 | * @param accept Whether to consider the network as validated even if it has partial |
| 4315 | * connectivity. |
| 4316 | * @param always Whether to remember this choice in the future. |
| 4317 | * |
| 4318 | * @hide |
| 4319 | */ |
| 4320 | @RequiresPermission(android.Manifest.permission.NETWORK_STACK) |
| 4321 | public void setAcceptPartialConnectivity(Network network, boolean accept, boolean always) { |
| 4322 | try { |
| 4323 | mService.setAcceptPartialConnectivity(network, accept, always); |
| 4324 | } catch (RemoteException e) { |
| 4325 | throw e.rethrowFromSystemServer(); |
| 4326 | } |
| 4327 | } |
| 4328 | |
| 4329 | /** |
Lorenzo Colitti | c65750c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 4330 | * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is |
| 4331 | * only meaningful if the system is configured not to penalize such networks, e.g., if the |
| 4332 | * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code |
| 4333 | * NETWORK_AVOID_BAD_WIFI setting is unset}. |
| 4334 | * |
Lorenzo Colitti | c65750c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 4335 | * @param network The network to accept. |
| 4336 | * |
| 4337 | * @hide |
| 4338 | */ |
lucaslin | 2240ef6 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 4339 | @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS) |
Lorenzo Colitti | c65750c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 4340 | public void setAvoidUnvalidated(Network network) { |
| 4341 | try { |
| 4342 | mService.setAvoidUnvalidated(network); |
| 4343 | } catch (RemoteException e) { |
| 4344 | throw e.rethrowFromSystemServer(); |
| 4345 | } |
| 4346 | } |
| 4347 | |
| 4348 | /** |
Lorenzo Colitti | 500dbae | 2017-04-27 14:30:21 +0900 | [diff] [blame] | 4349 | * Requests that the system open the captive portal app on the specified network. |
| 4350 | * |
| 4351 | * @param network The network to log into. |
| 4352 | * |
| 4353 | * @hide |
| 4354 | */ |
paulhu | 8e96a75 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 4355 | @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS) |
Lorenzo Colitti | 500dbae | 2017-04-27 14:30:21 +0900 | [diff] [blame] | 4356 | public void startCaptivePortalApp(Network network) { |
| 4357 | try { |
| 4358 | mService.startCaptivePortalApp(network); |
| 4359 | } catch (RemoteException e) { |
| 4360 | throw e.rethrowFromSystemServer(); |
| 4361 | } |
| 4362 | } |
| 4363 | |
| 4364 | /** |
Remi NGUYEN VAN | 94ff95b | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 4365 | * Requests that the system open the captive portal app with the specified extras. |
| 4366 | * |
| 4367 | * <p>This endpoint is exclusively for use by the NetworkStack and is protected by the |
| 4368 | * corresponding permission. |
Remi NGUYEN VAN | eab0f54 | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 4369 | * @param network Network on which the captive portal was detected. |
Remi NGUYEN VAN | 94ff95b | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 4370 | * @param appExtras Extras to include in the app start intent. |
| 4371 | * @hide |
| 4372 | */ |
| 4373 | @SystemApi |
Remi NGUYEN VAN | 94ff95b | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 4374 | @RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK) |
paulhu | cbbc3db | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 4375 | public void startCaptivePortalApp(@NonNull Network network, @NonNull Bundle appExtras) { |
Remi NGUYEN VAN | 94ff95b | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 4376 | try { |
Remi NGUYEN VAN | eab0f54 | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 4377 | mService.startCaptivePortalAppInternal(network, appExtras); |
Remi NGUYEN VAN | 94ff95b | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 4378 | } catch (RemoteException e) { |
| 4379 | throw e.rethrowFromSystemServer(); |
| 4380 | } |
| 4381 | } |
| 4382 | |
| 4383 | /** |
Remi NGUYEN VAN | 27de63e | 2019-01-20 20:35:06 +0900 | [diff] [blame] | 4384 | * Determine whether the device is configured to avoid bad wifi. |
| 4385 | * @hide |
| 4386 | */ |
| 4387 | @SystemApi |
Remi NGUYEN VAN | 1fb7cab | 2019-03-22 11:14:13 +0900 | [diff] [blame] | 4388 | @RequiresPermission(anyOf = { |
| 4389 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 4390 | android.Manifest.permission.NETWORK_STACK}) |
| 4391 | public boolean shouldAvoidBadWifi() { |
Remi NGUYEN VAN | 27de63e | 2019-01-20 20:35:06 +0900 | [diff] [blame] | 4392 | try { |
Remi NGUYEN VAN | 1fb7cab | 2019-03-22 11:14:13 +0900 | [diff] [blame] | 4393 | return mService.shouldAvoidBadWifi(); |
Remi NGUYEN VAN | 27de63e | 2019-01-20 20:35:06 +0900 | [diff] [blame] | 4394 | } catch (RemoteException e) { |
| 4395 | throw e.rethrowFromSystemServer(); |
| 4396 | } |
| 4397 | } |
| 4398 | |
| 4399 | /** |
Lorenzo Colitti | 48a2a32 | 2017-01-24 18:08:41 +0900 | [diff] [blame] | 4400 | * It is acceptable to briefly use multipath data to provide seamless connectivity for |
| 4401 | * time-sensitive user-facing operations when the system default network is temporarily |
Lorenzo Colitti | 139a48c | 2017-04-28 00:56:30 +0900 | [diff] [blame] | 4402 | * unresponsive. The amount of data should be limited (less than one megabyte for every call to |
| 4403 | * 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] | 4404 | * |
| 4405 | * An example of such an operation might be a time-sensitive foreground activity, such as a |
| 4406 | * voice command, that the user is performing while walking out of range of a Wi-Fi network. |
| 4407 | */ |
| 4408 | public static final int MULTIPATH_PREFERENCE_HANDOVER = 1 << 0; |
| 4409 | |
| 4410 | /** |
| 4411 | * It is acceptable to use small amounts of multipath data on an ongoing basis to provide |
| 4412 | * a backup channel for traffic that is primarily going over another network. |
| 4413 | * |
| 4414 | * An example might be maintaining backup connections to peers or servers for the purpose of |
| 4415 | * fast fallback if the default network is temporarily unresponsive or disconnects. The traffic |
| 4416 | * on backup paths should be negligible compared to the traffic on the main path. |
| 4417 | */ |
| 4418 | public static final int MULTIPATH_PREFERENCE_RELIABILITY = 1 << 1; |
| 4419 | |
| 4420 | /** |
| 4421 | * It is acceptable to use metered data to improve network latency and performance. |
| 4422 | */ |
| 4423 | public static final int MULTIPATH_PREFERENCE_PERFORMANCE = 1 << 2; |
| 4424 | |
| 4425 | /** |
| 4426 | * Return value to use for unmetered networks. On such networks we currently set all the flags |
| 4427 | * to true. |
| 4428 | * @hide |
| 4429 | */ |
| 4430 | public static final int MULTIPATH_PREFERENCE_UNMETERED = |
| 4431 | MULTIPATH_PREFERENCE_HANDOVER | |
| 4432 | MULTIPATH_PREFERENCE_RELIABILITY | |
| 4433 | MULTIPATH_PREFERENCE_PERFORMANCE; |
| 4434 | |
| 4435 | /** @hide */ |
| 4436 | @Retention(RetentionPolicy.SOURCE) |
| 4437 | @IntDef(flag = true, value = { |
| 4438 | MULTIPATH_PREFERENCE_HANDOVER, |
| 4439 | MULTIPATH_PREFERENCE_RELIABILITY, |
| 4440 | MULTIPATH_PREFERENCE_PERFORMANCE, |
| 4441 | }) |
| 4442 | public @interface MultipathPreference { |
| 4443 | } |
| 4444 | |
| 4445 | /** |
| 4446 | * Provides a hint to the calling application on whether it is desirable to use the |
| 4447 | * multinetwork APIs (e.g., {@link Network#openConnection}, {@link Network#bindSocket}, etc.) |
| 4448 | * for multipath data transfer on this network when it is not the system default network. |
| 4449 | * Applications desiring to use multipath network protocols should call this method before |
| 4450 | * each such operation. |
Lorenzo Colitti | 48a2a32 | 2017-01-24 18:08:41 +0900 | [diff] [blame] | 4451 | * |
| 4452 | * @param network The network on which the application desires to use multipath data. |
| 4453 | * If {@code null}, this method will return the a preference that will generally |
| 4454 | * apply to metered networks. |
| 4455 | * @return a bitwise OR of zero or more of the {@code MULTIPATH_PREFERENCE_*} constants. |
| 4456 | */ |
Jeff Sharkey | 656584a | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 4457 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4458 | public @MultipathPreference int getMultipathPreference(@Nullable Network network) { |
Lorenzo Colitti | 48a2a32 | 2017-01-24 18:08:41 +0900 | [diff] [blame] | 4459 | try { |
| 4460 | return mService.getMultipathPreference(network); |
| 4461 | } catch (RemoteException e) { |
| 4462 | throw e.rethrowFromSystemServer(); |
| 4463 | } |
| 4464 | } |
| 4465 | |
| 4466 | /** |
Stuart Scott | 0f835ac | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 4467 | * Resets all connectivity manager settings back to factory defaults. |
| 4468 | * @hide |
| 4469 | */ |
paulhu | 8e96a75 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 4470 | @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS) |
Stuart Scott | 0f835ac | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 4471 | public void factoryReset() { |
Stuart Scott | 0f835ac | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 4472 | try { |
Stuart Scott | d546364 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4473 | mService.factoryReset(); |
Amos Bianchi | a9b415a | 2020-03-04 11:07:38 -0800 | [diff] [blame] | 4474 | mTetheringManager.stopAllTethering(); |
Stuart Scott | 0f835ac | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 4475 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4476 | throw e.rethrowFromSystemServer(); |
Stuart Scott | 0f835ac | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 4477 | } |
| 4478 | } |
| 4479 | |
| 4480 | /** |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4481 | * Binds the current process to {@code network}. All Sockets created in the future |
| 4482 | * (and not explicitly bound via a bound SocketFactory from |
| 4483 | * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to |
| 4484 | * {@code network}. All host name resolutions will be limited to {@code network} as well. |
| 4485 | * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to |
| 4486 | * work and all host name resolutions will fail. This is by design so an application doesn't |
| 4487 | * accidentally use Sockets it thinks are still bound to a particular {@link Network}. |
| 4488 | * To clear binding pass {@code null} for {@code network}. Using individually bound |
| 4489 | * Sockets created by Network.getSocketFactory().createSocket() and |
| 4490 | * performing network-specific host name resolutions via |
| 4491 | * {@link Network#getAllByName Network.getAllByName} is preferred to calling |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 4492 | * {@code bindProcessToNetwork}. |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4493 | * |
| 4494 | * @param network The {@link Network} to bind the current process to, or {@code null} to clear |
| 4495 | * the current binding. |
| 4496 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
| 4497 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4498 | public boolean bindProcessToNetwork(@Nullable Network network) { |
Chalard Jean | 9dd1161 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 4499 | // Forcing callers to call through non-static function ensures ConnectivityManager |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 4500 | // instantiated. |
| 4501 | return setProcessDefaultNetwork(network); |
| 4502 | } |
| 4503 | |
| 4504 | /** |
| 4505 | * Binds the current process to {@code network}. All Sockets created in the future |
| 4506 | * (and not explicitly bound via a bound SocketFactory from |
| 4507 | * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to |
| 4508 | * {@code network}. All host name resolutions will be limited to {@code network} as well. |
| 4509 | * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to |
| 4510 | * work and all host name resolutions will fail. This is by design so an application doesn't |
| 4511 | * accidentally use Sockets it thinks are still bound to a particular {@link Network}. |
| 4512 | * To clear binding pass {@code null} for {@code network}. Using individually bound |
| 4513 | * Sockets created by Network.getSocketFactory().createSocket() and |
| 4514 | * performing network-specific host name resolutions via |
| 4515 | * {@link Network#getAllByName Network.getAllByName} is preferred to calling |
| 4516 | * {@code setProcessDefaultNetwork}. |
| 4517 | * |
| 4518 | * @param network The {@link Network} to bind the current process to, or {@code null} to clear |
| 4519 | * the current binding. |
| 4520 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
| 4521 | * @deprecated This function can throw {@link IllegalStateException}. Use |
| 4522 | * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork} |
| 4523 | * is a direct replacement. |
| 4524 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 4525 | @Deprecated |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4526 | public static boolean setProcessDefaultNetwork(@Nullable Network network) { |
Paul Jensen | 3e2917c | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 4527 | int netId = (network == null) ? NETID_UNSET : network.netId; |
Lorenzo Colitti | 3c766eb | 2019-01-31 13:08:24 +0900 | [diff] [blame] | 4528 | boolean isSameNetId = (netId == NetworkUtils.getBoundNetworkForProcess()); |
| 4529 | |
Lorenzo Colitti | 3a1cb9d | 2019-01-30 23:04:54 +0900 | [diff] [blame] | 4530 | if (netId != NETID_UNSET) { |
| 4531 | netId = network.getNetIdForResolv(); |
Paul Jensen | 3e2917c | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 4532 | } |
Lorenzo Colitti | 3c766eb | 2019-01-31 13:08:24 +0900 | [diff] [blame] | 4533 | |
| 4534 | if (!NetworkUtils.bindProcessToNetwork(netId)) { |
| 4535 | return false; |
| 4536 | } |
| 4537 | |
| 4538 | if (!isSameNetId) { |
Paul Jensen | c0618a6 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 4539 | // Set HTTP proxy system properties to match network. |
| 4540 | // 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] | 4541 | try { |
Remi NGUYEN VAN | b33335c | 2021-02-03 10:18:20 +0900 | [diff] [blame] | 4542 | Proxy.setHttpProxyConfiguration(getInstance().getDefaultProxy()); |
Lorenzo Colitti | 41b1fbc | 2015-04-22 11:52:48 +0900 | [diff] [blame] | 4543 | } catch (SecurityException e) { |
| 4544 | // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy. |
| 4545 | Log.e(TAG, "Can't set proxy properties", e); |
| 4546 | } |
Paul Jensen | 3e2917c | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 4547 | // Must flush DNS cache as new network may have different DNS resolutions. |
| 4548 | InetAddress.clearDnsCache(); |
| 4549 | // Must flush socket pool as idle sockets will be bound to previous network and may |
| 4550 | // cause subsequent fetches to be performed on old network. |
| 4551 | NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged(); |
Paul Jensen | 3e2917c | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 4552 | } |
Lorenzo Colitti | 3c766eb | 2019-01-31 13:08:24 +0900 | [diff] [blame] | 4553 | |
| 4554 | return true; |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4555 | } |
| 4556 | |
| 4557 | /** |
| 4558 | * Returns the {@link Network} currently bound to this process via |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 4559 | * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound. |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4560 | * |
| 4561 | * @return {@code Network} to which this process is bound, or {@code null}. |
| 4562 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4563 | @Nullable |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 4564 | public Network getBoundNetworkForProcess() { |
| 4565 | // Forcing callers to call thru non-static function ensures ConnectivityManager |
| 4566 | // instantiated. |
| 4567 | return getProcessDefaultNetwork(); |
| 4568 | } |
| 4569 | |
| 4570 | /** |
| 4571 | * Returns the {@link Network} currently bound to this process via |
| 4572 | * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound. |
| 4573 | * |
| 4574 | * @return {@code Network} to which this process is bound, or {@code null}. |
| 4575 | * @deprecated Using this function can lead to other functions throwing |
| 4576 | * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead. |
| 4577 | * {@code getBoundNetworkForProcess} is a direct replacement. |
| 4578 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 4579 | @Deprecated |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4580 | @Nullable |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4581 | public static Network getProcessDefaultNetwork() { |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 4582 | int netId = NetworkUtils.getBoundNetworkForProcess(); |
Paul Jensen | 65743a2 | 2014-07-11 08:17:29 -0400 | [diff] [blame] | 4583 | if (netId == NETID_UNSET) return null; |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4584 | return new Network(netId); |
| 4585 | } |
| 4586 | |
Lorenzo Colitti | 2780e4b | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 4587 | private void unsupportedStartingFrom(int version) { |
| 4588 | if (Process.myUid() == Process.SYSTEM_UID) { |
Lorenzo Colitti | 2386291 | 2018-09-28 11:31:55 +0900 | [diff] [blame] | 4589 | // The getApplicationInfo() call we make below is not supported in system context. Let |
| 4590 | // the call through here, and rely on the fact that ConnectivityService will refuse to |
| 4591 | // allow the system to use these APIs anyway. |
Lorenzo Colitti | 2780e4b | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 4592 | return; |
| 4593 | } |
| 4594 | |
| 4595 | if (mContext.getApplicationInfo().targetSdkVersion >= version) { |
| 4596 | throw new UnsupportedOperationException( |
| 4597 | "This method is not supported in target SDK version " + version + " and above"); |
| 4598 | } |
| 4599 | } |
| 4600 | |
| 4601 | // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature, |
| 4602 | // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException. |
Lifu Tang | 0a922fd | 2016-01-07 23:20:38 -0800 | [diff] [blame] | 4603 | // 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] | 4604 | // remove these exemptions. Note that this check is not secure, and apps can still access these |
| 4605 | // functions by accessing ConnectivityService directly. However, it should be clear that doing |
| 4606 | // so is unsupported and may break in the future. http://b/22728205 |
| 4607 | private void checkLegacyRoutingApiAccess() { |
Dianne Hackborn | 18c1d83 | 2015-07-31 10:35:34 -0700 | [diff] [blame] | 4608 | unsupportedStartingFrom(VERSION_CODES.M); |
Lorenzo Colitti | 2780e4b | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 4609 | } |
| 4610 | |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4611 | /** |
| 4612 | * Binds host resolutions performed by this process to {@code network}. |
Paul Jensen | ee2f45d | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 4613 | * {@link #bindProcessToNetwork} takes precedence over this setting. |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4614 | * |
| 4615 | * @param network The {@link Network} to bind host resolutions from the current process to, or |
| 4616 | * {@code null} to clear the current binding. |
| 4617 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
| 4618 | * @hide |
| 4619 | * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}. |
| 4620 | */ |
Aurimas Liutikas | e80a0eb | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 4621 | @Deprecated |
Mathew Inwood | e1a17ba | 2020-11-04 09:29:36 +0000 | [diff] [blame] | 4622 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553) |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4623 | public static boolean setProcessDefaultNetworkForHostResolution(Network network) { |
Paul Jensen | 65743a2 | 2014-07-11 08:17:29 -0400 | [diff] [blame] | 4624 | return NetworkUtils.bindProcessToNetworkForHostResolution( |
Erik Kline | 767b7f2 | 2018-04-27 22:48:33 +0900 | [diff] [blame] | 4625 | (network == null) ? NETID_UNSET : network.getNetIdForResolv()); |
Paul Jensen | 8cdda64 | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4626 | } |
Felipe Leme | 3051135 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 4627 | |
| 4628 | /** |
| 4629 | * Device is not restricting metered network activity while application is running on |
| 4630 | * background. |
| 4631 | */ |
| 4632 | public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1; |
| 4633 | |
| 4634 | /** |
| 4635 | * Device is restricting metered network activity while application is running on background, |
| 4636 | * but application is allowed to bypass it. |
| 4637 | * <p> |
| 4638 | * In this state, application should take action to mitigate metered network access. |
| 4639 | * For example, a music streaming application should switch to a low-bandwidth bitrate. |
| 4640 | */ |
| 4641 | public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2; |
| 4642 | |
| 4643 | /** |
| 4644 | * Device is restricting metered network activity while application is running on background. |
Felipe Leme | d34c9af | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 4645 | * <p> |
Felipe Leme | 3051135 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 4646 | * In this state, application should not try to use the network while running on background, |
| 4647 | * because it would be denied. |
| 4648 | */ |
| 4649 | public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3; |
| 4650 | |
Felipe Leme | d34c9af | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 4651 | /** |
| 4652 | * A change in the background metered network activity restriction has occurred. |
| 4653 | * <p> |
| 4654 | * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction |
| 4655 | * applies to them. |
| 4656 | * <p> |
| 4657 | * This is only sent to registered receivers, not manifest receivers. |
| 4658 | */ |
| 4659 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 4660 | public static final String ACTION_RESTRICT_BACKGROUND_CHANGED = |
| 4661 | "android.net.conn.RESTRICT_BACKGROUND_CHANGED"; |
| 4662 | |
Felipe Leme | 7e4c185 | 2016-01-25 11:48:04 -0800 | [diff] [blame] | 4663 | /** @hide */ |
| 4664 | @Retention(RetentionPolicy.SOURCE) |
Felipe Leme | 3051135 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 4665 | @IntDef(flag = false, value = { |
| 4666 | RESTRICT_BACKGROUND_STATUS_DISABLED, |
| 4667 | RESTRICT_BACKGROUND_STATUS_WHITELISTED, |
| 4668 | RESTRICT_BACKGROUND_STATUS_ENABLED, |
| 4669 | }) |
Felipe Leme | 3051135 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 4670 | public @interface RestrictBackgroundStatus { |
| 4671 | } |
| 4672 | |
| 4673 | private INetworkPolicyManager getNetworkPolicyManager() { |
| 4674 | synchronized (this) { |
| 4675 | if (mNPManager != null) { |
| 4676 | return mNPManager; |
| 4677 | } |
| 4678 | mNPManager = INetworkPolicyManager.Stub.asInterface(ServiceManager |
| 4679 | .getService(Context.NETWORK_POLICY_SERVICE)); |
| 4680 | return mNPManager; |
| 4681 | } |
| 4682 | } |
| 4683 | |
| 4684 | /** |
| 4685 | * Determines if the calling application is subject to metered network restrictions while |
| 4686 | * running on background. |
Felipe Leme | 3edc616 | 2016-05-16 13:57:19 -0700 | [diff] [blame] | 4687 | * |
| 4688 | * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED}, |
| 4689 | * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED}, |
| 4690 | * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED} |
Felipe Leme | 3051135 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 4691 | */ |
| 4692 | public @RestrictBackgroundStatus int getRestrictBackgroundStatus() { |
| 4693 | try { |
| 4694 | return getNetworkPolicyManager().getRestrictBackgroundByCaller(); |
| 4695 | } catch (RemoteException e) { |
Jeff Sharkey | c78f85c | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4696 | throw e.rethrowFromSystemServer(); |
Felipe Leme | 3051135 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 4697 | } |
| 4698 | } |
Ricky Wai | 7097cc9 | 2018-01-23 04:09:45 +0000 | [diff] [blame] | 4699 | |
| 4700 | /** |
| 4701 | * 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] | 4702 | * potentially harmful apps. This method returns the SHA-256 of the watchlist config file |
| 4703 | * currently used by the system for validation purposes. |
Ricky Wai | 7097cc9 | 2018-01-23 04:09:45 +0000 | [diff] [blame] | 4704 | * |
| 4705 | * @return Hash of network watchlist config file. Null if config does not exist. |
| 4706 | */ |
Chalard Jean | 158702d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4707 | @Nullable |
Ricky Wai | 7097cc9 | 2018-01-23 04:09:45 +0000 | [diff] [blame] | 4708 | public byte[] getNetworkWatchlistConfigHash() { |
| 4709 | try { |
| 4710 | return mService.getNetworkWatchlistConfigHash(); |
| 4711 | } catch (RemoteException e) { |
| 4712 | Log.e(TAG, "Unable to get watchlist config hash"); |
| 4713 | throw e.rethrowFromSystemServer(); |
| 4714 | } |
| 4715 | } |
Jeff Vander Stoep | 39a51e0 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 4716 | |
| 4717 | /** |
| 4718 | * Returns the {@code uid} of the owner of a network connection. |
| 4719 | * |
Benedict Wong | 0bd4bba | 2020-01-20 22:14:59 -0800 | [diff] [blame] | 4720 | * @param protocol The protocol of the connection. Only {@code IPPROTO_TCP} and {@code |
| 4721 | * IPPROTO_UDP} currently supported. |
Jeff Vander Stoep | 39a51e0 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 4722 | * @param local The local {@link InetSocketAddress} of a connection. |
| 4723 | * @param remote The remote {@link InetSocketAddress} of a connection. |
Jeff Vander Stoep | 39a51e0 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 4724 | * @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] | 4725 | * (e.g., if it is associated with the calling VPN app's VpnService tunnel) or {@link |
| 4726 | * android.os.Process#INVALID_UID} if the connection is not found. |
| 4727 | * @throws {@link SecurityException} if the caller is not the active VpnService for the current |
| 4728 | * user. |
| 4729 | * @throws {@link IllegalArgumentException} if an unsupported protocol is requested. |
Jeff Vander Stoep | 39a51e0 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 4730 | */ |
Benedict Wong | 0bd4bba | 2020-01-20 22:14:59 -0800 | [diff] [blame] | 4731 | public int getConnectionOwnerUid( |
| 4732 | int protocol, @NonNull InetSocketAddress local, @NonNull InetSocketAddress remote) { |
Jeff Vander Stoep | 39a51e0 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 4733 | ConnectionInfo connectionInfo = new ConnectionInfo(protocol, local, remote); |
| 4734 | try { |
| 4735 | return mService.getConnectionOwnerUid(connectionInfo); |
| 4736 | } catch (RemoteException e) { |
| 4737 | throw e.rethrowFromSystemServer(); |
| 4738 | } |
| 4739 | } |
Soi, Yoshinari | b8102ef | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 4740 | |
| 4741 | private void printStackTrace() { |
| 4742 | if (DEBUG) { |
| 4743 | final StackTraceElement[] callStack = Thread.currentThread().getStackTrace(); |
| 4744 | final StringBuffer sb = new StringBuffer(); |
| 4745 | for (int i = 3; i < callStack.length; i++) { |
| 4746 | final String stackTrace = callStack[i].toString(); |
| 4747 | if (stackTrace == null || stackTrace.contains("android.os")) { |
| 4748 | break; |
| 4749 | } |
| 4750 | sb.append(" [").append(stackTrace).append("]"); |
| 4751 | } |
| 4752 | Log.d(TAG, "StackLog:" + sb.toString()); |
| 4753 | } |
| 4754 | } |
Cody Kesting | f53a075 | 2020-04-15 12:33:28 -0700 | [diff] [blame] | 4755 | |
Remi NGUYEN VAN | 5f40642 | 2021-01-15 23:02:47 +0900 | [diff] [blame] | 4756 | /** @hide */ |
| 4757 | public TestNetworkManager startOrGetTestNetworkManager() { |
| 4758 | final IBinder tnBinder; |
| 4759 | try { |
| 4760 | tnBinder = mService.startOrGetTestNetworkService(); |
| 4761 | } catch (RemoteException e) { |
| 4762 | throw e.rethrowFromSystemServer(); |
| 4763 | } |
| 4764 | |
| 4765 | return new TestNetworkManager(ITestNetworkManager.Stub.asInterface(tnBinder)); |
| 4766 | } |
| 4767 | |
Remi NGUYEN VAN | 5f40642 | 2021-01-15 23:02:47 +0900 | [diff] [blame] | 4768 | /** @hide */ |
| 4769 | public ConnectivityDiagnosticsManager createDiagnosticsManager() { |
| 4770 | return new ConnectivityDiagnosticsManager(mContext, mService); |
| 4771 | } |
| 4772 | |
Cody Kesting | f53a075 | 2020-04-15 12:33:28 -0700 | [diff] [blame] | 4773 | /** |
| 4774 | * Simulates a Data Stall for the specified Network. |
| 4775 | * |
Remi NGUYEN VAN | 761c7ad | 2021-01-12 18:40:04 +0900 | [diff] [blame] | 4776 | * <p>This method should only be used for tests. |
| 4777 | * |
Cody Kesting | f53a075 | 2020-04-15 12:33:28 -0700 | [diff] [blame] | 4778 | * <p>The caller must be the owner of the specified Network. |
| 4779 | * |
| 4780 | * @param detectionMethod The detection method used to identify the Data Stall. |
| 4781 | * @param timestampMillis The timestamp at which the stall 'occurred', in milliseconds. |
| 4782 | * @param network The Network for which a Data Stall is being simluated. |
| 4783 | * @param extras The PersistableBundle of extras included in the Data Stall notification. |
| 4784 | * @throws SecurityException if the caller is not the owner of the given network. |
| 4785 | * @hide |
| 4786 | */ |
Remi NGUYEN VAN | 761c7ad | 2021-01-12 18:40:04 +0900 | [diff] [blame] | 4787 | @SystemApi(client = SystemApi.Client.MODULE_LIBRARIES) |
Cody Kesting | f53a075 | 2020-04-15 12:33:28 -0700 | [diff] [blame] | 4788 | @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_TEST_NETWORKS, |
| 4789 | android.Manifest.permission.NETWORK_STACK}) |
| 4790 | public void simulateDataStall(int detectionMethod, long timestampMillis, |
| 4791 | @NonNull Network network, @NonNull PersistableBundle extras) { |
| 4792 | try { |
| 4793 | mService.simulateDataStall(detectionMethod, timestampMillis, network, extras); |
| 4794 | } catch (RemoteException e) { |
| 4795 | e.rethrowFromSystemServer(); |
| 4796 | } |
| 4797 | } |
James Mattis | 356a879 | 2020-10-28 21:48:54 -0700 | [diff] [blame] | 4798 | |
Daniel Bright | f9e945b | 2020-06-15 16:10:01 -0700 | [diff] [blame] | 4799 | @NonNull |
| 4800 | private final List<QosCallbackConnection> mQosCallbackConnections = new ArrayList<>(); |
| 4801 | |
| 4802 | /** |
| 4803 | * Registers a {@link QosSocketInfo} with an associated {@link QosCallback}. The callback will |
| 4804 | * receive available QoS events related to the {@link Network} and local ip + port |
| 4805 | * specified within socketInfo. |
| 4806 | * <p/> |
| 4807 | * The same {@link QosCallback} must be unregistered before being registered a second time, |
| 4808 | * otherwise {@link QosCallbackRegistrationException} is thrown. |
| 4809 | * <p/> |
| 4810 | * This API does not, in itself, require any permission if called with a network that is not |
| 4811 | * restricted. However, the underlying implementation currently only supports the IMS network, |
| 4812 | * which is always restricted. That means non-preinstalled callers can't possibly find this API |
| 4813 | * useful, because they'd never be called back on networks that they would have access to. |
| 4814 | * |
| 4815 | * @throws SecurityException if {@link QosSocketInfo#getNetwork()} is restricted and the app is |
| 4816 | * missing CONNECTIVITY_USE_RESTRICTED_NETWORKS permission. |
| 4817 | * @throws QosCallback.QosCallbackRegistrationException if qosCallback is already registered. |
| 4818 | * @throws RuntimeException if the app already has too many callbacks registered. |
| 4819 | * |
| 4820 | * Exceptions after the time of registration is passed through |
| 4821 | * {@link QosCallback#onError(QosCallbackException)}. see: {@link QosCallbackException}. |
| 4822 | * |
| 4823 | * @param socketInfo the socket information used to match QoS events |
| 4824 | * @param callback receives qos events that satisfy socketInfo |
| 4825 | * @param executor The executor on which the callback will be invoked. The provided |
| 4826 | * {@link Executor} must run callback sequentially, otherwise the order of |
| 4827 | * callbacks cannot be guaranteed. |
| 4828 | * |
| 4829 | * @hide |
| 4830 | */ |
| 4831 | @SystemApi |
| 4832 | public void registerQosCallback(@NonNull final QosSocketInfo socketInfo, |
| 4833 | @NonNull final QosCallback callback, |
| 4834 | @CallbackExecutor @NonNull final Executor executor) { |
| 4835 | Objects.requireNonNull(socketInfo, "socketInfo must be non-null"); |
| 4836 | Objects.requireNonNull(callback, "callback must be non-null"); |
| 4837 | Objects.requireNonNull(executor, "executor must be non-null"); |
| 4838 | |
| 4839 | try { |
| 4840 | synchronized (mQosCallbackConnections) { |
| 4841 | if (getQosCallbackConnection(callback) == null) { |
| 4842 | final QosCallbackConnection connection = |
| 4843 | new QosCallbackConnection(this, callback, executor); |
| 4844 | mQosCallbackConnections.add(connection); |
| 4845 | mService.registerQosSocketCallback(socketInfo, connection); |
| 4846 | } else { |
| 4847 | Log.e(TAG, "registerQosCallback: Callback already registered"); |
| 4848 | throw new QosCallbackRegistrationException(); |
| 4849 | } |
| 4850 | } |
| 4851 | } catch (final RemoteException e) { |
| 4852 | Log.e(TAG, "registerQosCallback: Error while registering ", e); |
| 4853 | |
| 4854 | // The same unregister method method is called for consistency even though nothing |
| 4855 | // will be sent to the ConnectivityService since the callback was never successfully |
| 4856 | // registered. |
| 4857 | unregisterQosCallback(callback); |
| 4858 | e.rethrowFromSystemServer(); |
| 4859 | } catch (final ServiceSpecificException e) { |
| 4860 | Log.e(TAG, "registerQosCallback: Error while registering ", e); |
| 4861 | unregisterQosCallback(callback); |
| 4862 | throw convertServiceException(e); |
| 4863 | } |
| 4864 | } |
| 4865 | |
| 4866 | /** |
| 4867 | * Unregisters the given {@link QosCallback}. The {@link QosCallback} will no longer receive |
| 4868 | * events once unregistered and can be registered a second time. |
| 4869 | * <p/> |
| 4870 | * If the {@link QosCallback} does not have an active registration, it is a no-op. |
| 4871 | * |
| 4872 | * @param callback the callback being unregistered |
| 4873 | * |
| 4874 | * @hide |
| 4875 | */ |
| 4876 | @SystemApi |
| 4877 | public void unregisterQosCallback(@NonNull final QosCallback callback) { |
| 4878 | Objects.requireNonNull(callback, "The callback must be non-null"); |
| 4879 | try { |
| 4880 | synchronized (mQosCallbackConnections) { |
| 4881 | final QosCallbackConnection connection = getQosCallbackConnection(callback); |
| 4882 | if (connection != null) { |
| 4883 | connection.stopReceivingMessages(); |
| 4884 | mService.unregisterQosCallback(connection); |
| 4885 | mQosCallbackConnections.remove(connection); |
| 4886 | } else { |
| 4887 | Log.d(TAG, "unregisterQosCallback: Callback not registered"); |
| 4888 | } |
| 4889 | } |
| 4890 | } catch (final RemoteException e) { |
| 4891 | Log.e(TAG, "unregisterQosCallback: Error while unregistering ", e); |
| 4892 | e.rethrowFromSystemServer(); |
| 4893 | } |
| 4894 | } |
| 4895 | |
| 4896 | /** |
| 4897 | * Gets the connection related to the callback. |
| 4898 | * |
| 4899 | * @param callback the callback to look up |
| 4900 | * @return the related connection |
| 4901 | */ |
| 4902 | @Nullable |
| 4903 | private QosCallbackConnection getQosCallbackConnection(final QosCallback callback) { |
| 4904 | for (final QosCallbackConnection connection : mQosCallbackConnections) { |
| 4905 | // Checking by reference here is intentional |
| 4906 | if (connection.getCallback() == callback) { |
| 4907 | return connection; |
| 4908 | } |
| 4909 | } |
| 4910 | return null; |
| 4911 | } |
Junyu Lai | a62493f | 2021-01-19 11:10:56 +0000 | [diff] [blame] | 4912 | |
| 4913 | /** |
| 4914 | * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, but |
| 4915 | * does not cause any networks to retain the NET_CAPABILITY_FOREGROUND capability. This can |
| 4916 | * be used to request that the system provide a network without causing the network to be |
| 4917 | * in the foreground. |
| 4918 | * |
| 4919 | * <p>This method will attempt to find the best network that matches the passed |
| 4920 | * {@link NetworkRequest}, and to bring up one that does if none currently satisfies the |
| 4921 | * criteria. The platform will evaluate which network is the best at its own discretion. |
| 4922 | * Throughput, latency, cost per byte, policy, user preference and other considerations |
| 4923 | * may be factored in the decision of what is considered the best network. |
| 4924 | * |
| 4925 | * <p>As long as this request is outstanding, the platform will try to maintain the best network |
| 4926 | * matching this request, while always attempting to match the request to a better network if |
| 4927 | * possible. If a better match is found, the platform will switch this request to the now-best |
| 4928 | * network and inform the app of the newly best network by invoking |
| 4929 | * {@link NetworkCallback#onAvailable(Network)} on the provided callback. Note that the platform |
| 4930 | * will not try to maintain any other network than the best one currently matching the request: |
| 4931 | * a network not matching any network request may be disconnected at any time. |
| 4932 | * |
| 4933 | * <p>For example, an application could use this method to obtain a connected cellular network |
| 4934 | * even if the device currently has a data connection over Ethernet. This may cause the cellular |
| 4935 | * radio to consume additional power. Or, an application could inform the system that it wants |
| 4936 | * a network supporting sending MMSes and have the system let it know about the currently best |
| 4937 | * MMS-supporting network through the provided {@link NetworkCallback}. |
| 4938 | * |
| 4939 | * <p>The status of the request can be followed by listening to the various callbacks described |
| 4940 | * in {@link NetworkCallback}. The {@link Network} object passed to the callback methods can be |
| 4941 | * used to direct traffic to the network (although accessing some networks may be subject to |
| 4942 | * holding specific permissions). Callers will learn about the specific characteristics of the |
| 4943 | * network through |
| 4944 | * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)} and |
| 4945 | * {@link NetworkCallback#onLinkPropertiesChanged(Network, LinkProperties)}. The methods of the |
| 4946 | * provided {@link NetworkCallback} will only be invoked due to changes in the best network |
| 4947 | * matching the request at any given time; therefore when a better network matching the request |
| 4948 | * becomes available, the {@link NetworkCallback#onAvailable(Network)} method is called |
| 4949 | * with the new network after which no further updates are given about the previously-best |
| 4950 | * network, unless it becomes the best again at some later time. All callbacks are invoked |
| 4951 | * in order on the same thread, which by default is a thread created by the framework running |
| 4952 | * in the app. |
| 4953 | * |
| 4954 | * <p>This{@link NetworkRequest} will live until released via |
| 4955 | * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits, at |
| 4956 | * which point the system may let go of the network at any time. |
| 4957 | * |
| 4958 | * <p>It is presently unsupported to request a network with mutable |
| 4959 | * {@link NetworkCapabilities} such as |
| 4960 | * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or |
| 4961 | * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL} |
| 4962 | * as these {@code NetworkCapabilities} represent states that a particular |
| 4963 | * network may never attain, and whether a network will attain these states |
| 4964 | * is unknown prior to bringing up the network so the framework does not |
| 4965 | * know how to go about satisfying a request with these capabilities. |
| 4966 | * |
| 4967 | * <p>To avoid performance issues due to apps leaking callbacks, the system will limit the |
| 4968 | * number of outstanding requests to 100 per app (identified by their UID), shared with |
| 4969 | * all variants of this method, of {@link #registerNetworkCallback} as well as |
| 4970 | * {@link ConnectivityDiagnosticsManager#registerConnectivityDiagnosticsCallback}. |
| 4971 | * Requesting a network with this method will count toward this limit. If this limit is |
| 4972 | * exceeded, an exception will be thrown. To avoid hitting this issue and to conserve resources, |
| 4973 | * make sure to unregister the callbacks with |
| 4974 | * {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 4975 | * |
| 4976 | * @param request {@link NetworkRequest} describing this request. |
| 4977 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
| 4978 | * If null, the callback is invoked on the default internal Handler. |
| 4979 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 4980 | * the callback must not be shared - it uniquely specifies this request. |
| 4981 | * @throws IllegalArgumentException if {@code request} contains invalid network capabilities. |
| 4982 | * @throws SecurityException if missing the appropriate permissions. |
| 4983 | * @throws RuntimeException if the app already has too many callbacks registered. |
| 4984 | * |
| 4985 | * @hide |
| 4986 | */ |
| 4987 | @SystemApi(client = MODULE_LIBRARIES) |
| 4988 | @SuppressLint("ExecutorRegistration") |
| 4989 | @RequiresPermission(anyOf = { |
| 4990 | android.Manifest.permission.NETWORK_SETTINGS, |
| 4991 | android.Manifest.permission.NETWORK_STACK, |
| 4992 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK |
| 4993 | }) |
| 4994 | public void requestBackgroundNetwork(@NonNull NetworkRequest request, |
| 4995 | @Nullable Handler handler, @NonNull NetworkCallback networkCallback) { |
| 4996 | final NetworkCapabilities nc = request.networkCapabilities; |
| 4997 | sendRequestForNetwork(nc, networkCallback, 0, BACKGROUND_REQUEST, |
| 4998 | TYPE_NONE, handler == null ? getDefaultHandler() : new CallbackHandler(handler)); |
| 4999 | } |
James Mattis | 45d8184 | 2021-01-10 14:24:24 -0800 | [diff] [blame] | 5000 | |
| 5001 | /** |
| 5002 | * Listener for {@link #setOemNetworkPreference(OemNetworkPreferences, Executor, |
| 5003 | * OnSetOemNetworkPreferenceListener)}. |
James Mattis | da32cfe | 2021-01-26 16:23:52 -0800 | [diff] [blame] | 5004 | * @hide |
James Mattis | 45d8184 | 2021-01-10 14:24:24 -0800 | [diff] [blame] | 5005 | */ |
James Mattis | da32cfe | 2021-01-26 16:23:52 -0800 | [diff] [blame] | 5006 | @SystemApi |
| 5007 | public interface OnSetOemNetworkPreferenceListener { |
James Mattis | 45d8184 | 2021-01-10 14:24:24 -0800 | [diff] [blame] | 5008 | /** |
| 5009 | * Called when setOemNetworkPreference() successfully completes. |
| 5010 | */ |
| 5011 | void onComplete(); |
| 5012 | } |
| 5013 | |
| 5014 | /** |
| 5015 | * Used by automotive devices to set the network preferences used to direct traffic at an |
| 5016 | * application level as per the given OemNetworkPreferences. An example use-case would be an |
| 5017 | * automotive OEM wanting to provide connectivity for applications critical to the usage of a |
| 5018 | * vehicle via a particular network. |
| 5019 | * |
| 5020 | * Calling this will overwrite the existing preference. |
| 5021 | * |
| 5022 | * @param preference {@link OemNetworkPreferences} The application network preference to be set. |
| 5023 | * @param executor the executor on which listener will be invoked. |
| 5024 | * @param listener {@link OnSetOemNetworkPreferenceListener} optional listener used to |
| 5025 | * communicate completion of setOemNetworkPreference(). This will only be |
| 5026 | * called once upon successful completion of setOemNetworkPreference(). |
| 5027 | * @throws IllegalArgumentException if {@code preference} contains invalid preference values. |
| 5028 | * @throws SecurityException if missing the appropriate permissions. |
| 5029 | * @throws UnsupportedOperationException if called on a non-automotive device. |
James Mattis | da32cfe | 2021-01-26 16:23:52 -0800 | [diff] [blame] | 5030 | * @hide |
James Mattis | 45d8184 | 2021-01-10 14:24:24 -0800 | [diff] [blame] | 5031 | */ |
James Mattis | da32cfe | 2021-01-26 16:23:52 -0800 | [diff] [blame] | 5032 | @SystemApi |
James Mattis | 8378aec | 2021-01-26 14:05:36 -0800 | [diff] [blame] | 5033 | @RequiresPermission(android.Manifest.permission.CONTROL_OEM_PAID_NETWORK_PREFERENCE) |
James Mattis | da32cfe | 2021-01-26 16:23:52 -0800 | [diff] [blame] | 5034 | public void setOemNetworkPreference(@NonNull final OemNetworkPreferences preference, |
James Mattis | 45d8184 | 2021-01-10 14:24:24 -0800 | [diff] [blame] | 5035 | @Nullable @CallbackExecutor final Executor executor, |
| 5036 | @Nullable final OnSetOemNetworkPreferenceListener listener) { |
| 5037 | Objects.requireNonNull(preference, "OemNetworkPreferences must be non-null"); |
| 5038 | if (null != listener) { |
| 5039 | Objects.requireNonNull(executor, "Executor must be non-null"); |
| 5040 | } |
| 5041 | final IOnSetOemNetworkPreferenceListener listenerInternal = listener == null ? null : |
| 5042 | new IOnSetOemNetworkPreferenceListener.Stub() { |
| 5043 | @Override |
| 5044 | public void onComplete() { |
| 5045 | executor.execute(listener::onComplete); |
| 5046 | } |
| 5047 | }; |
| 5048 | |
| 5049 | try { |
| 5050 | mService.setOemNetworkPreference(preference, listenerInternal); |
| 5051 | } catch (RemoteException e) { |
| 5052 | Log.e(TAG, "setOemNetworkPreference() failed for preference: " + preference.toString()); |
| 5053 | throw e.rethrowFromSystemServer(); |
| 5054 | } |
| 5055 | } |
The Android Open Source Project | 28527d2 | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 5056 | } |