The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 16 | package android.net; |
| 17 | |
junyulai | 215b877 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 18 | import static android.net.IpSecManager.INVALID_RESOURCE_ID; |
| 19 | |
junyulai | 48eac1d4 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 20 | import android.annotation.CallbackExecutor; |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 21 | import android.annotation.IntDef; |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 22 | import android.annotation.NonNull; |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 24 | import android.annotation.RequiresPermission; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | import android.annotation.SdkConstant; |
| 26 | import android.annotation.SdkConstant.SdkConstantType; |
Udam Saini | b7c2487 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 27 | import android.annotation.SystemApi; |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 28 | import android.annotation.SystemService; |
Remi NGUYEN VAN | 7731c5b | 2019-01-17 14:38:31 +0900 | [diff] [blame] | 29 | import android.annotation.TestApi; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 30 | import android.app.PendingIntent; |
Artur Satayev | 2695800 | 2019-12-10 17:47:52 +0000 | [diff] [blame] | 31 | import android.compat.annotation.UnsupportedAppUsage; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 32 | import android.content.Context; |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 33 | import android.content.Intent; |
junyulai | 48eac1d4 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 34 | import android.net.IpSecManager.UdpEncapsulationSocket; |
| 35 | import android.net.SocketKeepalive.Callback; |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 36 | import android.net.TetheringManager.StartTetheringCallback; |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 37 | import android.net.TetheringManager.TetheringEventCallback; |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 38 | import android.net.TetheringManager.TetheringRequest; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 39 | import android.os.Binder; |
Mathew Inwood | 55418ea | 2018-12-20 15:30:45 +0000 | [diff] [blame] | 40 | import android.os.Build; |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 41 | import android.os.Build.VERSION_CODES; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 42 | import android.os.Bundle; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 43 | import android.os.Handler; |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 44 | import android.os.IBinder; |
| 45 | import android.os.INetworkActivityListener; |
| 46 | import android.os.INetworkManagementService; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 47 | import android.os.Looper; |
| 48 | import android.os.Message; |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 49 | import android.os.Messenger; |
junyulai | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 50 | import android.os.ParcelFileDescriptor; |
Automerger Merge Worker | 42f8135 | 2020-05-12 18:48:04 +0000 | [diff] [blame] | 51 | import android.os.PersistableBundle; |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 52 | import android.os.Process; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 53 | import android.os.RemoteException; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 54 | import android.os.ResultReceiver; |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 55 | import android.os.ServiceManager; |
Hugo Benichi | cb88323 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 56 | import android.os.ServiceSpecificException; |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 57 | import android.provider.Settings; |
Wink Saville | 36ffb04 | 2014-12-05 11:10:30 -0800 | [diff] [blame] | 58 | import android.telephony.SubscriptionManager; |
Meng Wang | a73bed8 | 2019-11-18 17:10:00 -0800 | [diff] [blame] | 59 | import android.telephony.TelephonyManager; |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 60 | import android.util.ArrayMap; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 61 | import android.util.Log; |
Erik Kline | 35bf06c | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 62 | import android.util.SparseIntArray; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 63 | |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 64 | import com.android.internal.annotations.GuardedBy; |
Hugo Benichi | dafed3d | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 65 | import com.android.internal.util.Preconditions; |
| 66 | import com.android.internal.util.Protocol; |
Robert Greenwalt | afa05c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 67 | |
Paul Jensen | c91b534 | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 68 | import libcore.net.event.NetworkEventDispatcher; |
| 69 | |
junyulai | 215b877 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 70 | import java.io.FileDescriptor; |
junyulai | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 71 | import java.io.IOException; |
| 72 | import java.io.UncheckedIOException; |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 73 | import java.lang.annotation.Retention; |
| 74 | import java.lang.annotation.RetentionPolicy; |
Jeremy Klein | d42209d | 2015-12-28 15:11:58 -0800 | [diff] [blame] | 75 | import java.net.InetAddress; |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 76 | import java.net.InetSocketAddress; |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 77 | import java.net.Socket; |
Hugo Benichi | dafed3d | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 78 | import java.util.ArrayList; |
Jeremy Klein | d42209d | 2015-12-28 15:11:58 -0800 | [diff] [blame] | 79 | import java.util.HashMap; |
Hugo Benichi | dafed3d | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 80 | import java.util.List; |
| 81 | import java.util.Map; |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 82 | import java.util.Objects; |
junyulai | 48eac1d4 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 83 | import java.util.concurrent.Executor; |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 84 | import java.util.concurrent.ExecutorService; |
| 85 | import java.util.concurrent.Executors; |
| 86 | import java.util.concurrent.RejectedExecutionException; |
Jeremy Klein | d42209d | 2015-12-28 15:11:58 -0800 | [diff] [blame] | 87 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 88 | /** |
| 89 | * Class that answers queries about the state of network connectivity. It also |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 90 | * notifies applications when network connectivity changes. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 91 | * <p> |
| 92 | * The primary responsibilities of this class are to: |
| 93 | * <ol> |
| 94 | * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li> |
| 95 | * <li>Send broadcast intents when network connectivity changes</li> |
| 96 | * <li>Attempt to "fail over" to another network when connectivity to a network |
| 97 | * is lost</li> |
| 98 | * <li>Provide an API that allows applications to query the coarse-grained or fine-grained |
| 99 | * state of the available networks</li> |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 100 | * <li>Provide an API that allows applications to request and select networks for their data |
| 101 | * traffic</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | * </ol> |
| 103 | */ |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 104 | @SystemService(Context.CONNECTIVITY_SERVICE) |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 105 | public class ConnectivityManager { |
| 106 | private static final String TAG = "ConnectivityManager"; |
Soi, Yoshinari | dee2aa4 | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 107 | private static final boolean DEBUG = Log.isLoggable(TAG, Log.DEBUG); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 108 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 109 | /** |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 110 | * A change in network connectivity has occurred. A default connection has either |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | * been established or lost. The NetworkInfo for the affected network is |
| 112 | * sent as an extra; it should be consulted to see what kind of |
| 113 | * connectivity event occurred. |
| 114 | * <p/> |
Mark Lu | 33ec106 | 2016-12-05 10:57:55 -0800 | [diff] [blame] | 115 | * Apps targeting Android 7.0 (API level 24) and higher do not receive this |
| 116 | * broadcast if they declare the broadcast receiver in their manifest. Apps |
| 117 | * will still receive broadcasts if they register their |
| 118 | * {@link android.content.BroadcastReceiver} with |
| 119 | * {@link android.content.Context#registerReceiver Context.registerReceiver()} |
| 120 | * and that context is still valid. |
| 121 | * <p/> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 122 | * If this is a connection that was the result of failing over from a |
| 123 | * disconnected network, then the FAILOVER_CONNECTION boolean extra is |
| 124 | * set to true. |
| 125 | * <p/> |
| 126 | * For a loss of connectivity, if the connectivity manager is attempting |
| 127 | * to connect (or has already connected) to another network, the |
| 128 | * NetworkInfo for the new network is also passed as an extra. This lets |
| 129 | * any receivers of the broadcast know that they should not necessarily |
| 130 | * tell the user that no data traffic will be possible. Instead, the |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 131 | * receiver should expect another broadcast soon, indicating either that |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 132 | * the failover attempt succeeded (and so there is still overall data |
| 133 | * connectivity), or that the failover attempt failed, meaning that all |
| 134 | * connectivity has been lost. |
| 135 | * <p/> |
| 136 | * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY |
| 137 | * is set to {@code true} if there are no connected networks at all. |
Chalard Jean | 054cd16 | 2018-02-10 05:33:50 +0900 | [diff] [blame] | 138 | * |
| 139 | * @deprecated apps should use the more versatile {@link #requestNetwork}, |
| 140 | * {@link #registerNetworkCallback} or {@link #registerDefaultNetworkCallback} |
| 141 | * functions instead for faster and more detailed updates about the network |
| 142 | * changes they care about. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | */ |
Jeff Sharkey | 4fa63b2 | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 144 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Chalard Jean | 054cd16 | 2018-02-10 05:33:50 +0900 | [diff] [blame] | 145 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 147 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | /** |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 149 | * The device has connected to a network that has presented a captive |
| 150 | * portal, which is blocking Internet connectivity. The user was presented |
| 151 | * with a notification that network sign in is required, |
| 152 | * and the user invoked the notification's action indicating they |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 153 | * desire to sign in to the network. Apps handling this activity should |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 154 | * facilitate signing in to the network. This action includes a |
| 155 | * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents |
| 156 | * the network presenting the captive portal; all communication with the |
| 157 | * captive portal must be done using this {@code Network} object. |
| 158 | * <p/> |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 159 | * This activity includes a {@link CaptivePortal} extra named |
| 160 | * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different |
| 161 | * outcomes of the captive portal sign in to the system: |
| 162 | * <ul> |
| 163 | * <li> When the app handling this action believes the user has signed in to |
| 164 | * the network and the captive portal has been dismissed, the app should |
| 165 | * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can |
| 166 | * reevaluate the network. If reevaluation finds the network no longer |
| 167 | * subject to a captive portal, the network may become the default active |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 168 | * data network.</li> |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 169 | * <li> When the app handling this action believes the user explicitly wants |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 170 | * to ignore the captive portal and the network, the app should call |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 171 | * {@link CaptivePortal#ignoreNetwork}. </li> |
| 172 | * </ul> |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 173 | */ |
| 174 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 175 | public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL"; |
| 176 | |
| 177 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | * The lookup key for a {@link NetworkInfo} object. Retrieve with |
| 179 | * {@link android.content.Intent#getParcelableExtra(String)}. |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 180 | * |
Chalard Jean | eb1ea88 | 2019-01-11 16:47:53 +0900 | [diff] [blame] | 181 | * @deprecated The {@link NetworkInfo} object is deprecated, as many of its properties |
| 182 | * can't accurately represent modern network characteristics. |
| 183 | * Please obtain information about networks from the {@link NetworkCapabilities} |
| 184 | * or {@link LinkProperties} objects instead. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 185 | */ |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 186 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 187 | public static final String EXTRA_NETWORK_INFO = "networkInfo"; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 188 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 189 | /** |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 190 | * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast. |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 191 | * |
| 192 | * @see android.content.Intent#getIntExtra(String, int) |
Chalard Jean | eb1ea88 | 2019-01-11 16:47:53 +0900 | [diff] [blame] | 193 | * @deprecated The network type is not rich enough to represent the characteristics |
| 194 | * of modern networks. Please use {@link NetworkCapabilities} instead, |
| 195 | * in particular the transports. |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 196 | */ |
Chalard Jean | eb1ea88 | 2019-01-11 16:47:53 +0900 | [diff] [blame] | 197 | @Deprecated |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 198 | public static final String EXTRA_NETWORK_TYPE = "networkType"; |
| 199 | |
| 200 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | * The lookup key for a boolean that indicates whether a connect event |
| 202 | * is for a network to which the connectivity manager was failing over |
| 203 | * following a disconnect on another network. |
| 204 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
junyulai | 3822c8a | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 205 | * |
| 206 | * @deprecated See {@link NetworkInfo}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 207 | */ |
junyulai | 3822c8a | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 208 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 209 | public static final String EXTRA_IS_FAILOVER = "isFailover"; |
| 210 | /** |
| 211 | * The lookup key for a {@link NetworkInfo} object. This is supplied when |
| 212 | * there is another network that it may be possible to connect to. Retrieve with |
| 213 | * {@link android.content.Intent#getParcelableExtra(String)}. |
junyulai | 3822c8a | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 214 | * |
| 215 | * @deprecated See {@link NetworkInfo}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 216 | */ |
junyulai | 3822c8a | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 217 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 218 | public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork"; |
| 219 | /** |
| 220 | * The lookup key for a boolean that indicates whether there is a |
| 221 | * complete lack of connectivity, i.e., no network is available. |
| 222 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
| 223 | */ |
| 224 | public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity"; |
| 225 | /** |
| 226 | * The lookup key for a string that indicates why an attempt to connect |
| 227 | * to a network failed. The string has no particular structure. It is |
| 228 | * intended to be used in notifications presented to users. Retrieve |
| 229 | * it with {@link android.content.Intent#getStringExtra(String)}. |
| 230 | */ |
| 231 | public static final String EXTRA_REASON = "reason"; |
| 232 | /** |
| 233 | * The lookup key for a string that provides optionally supplied |
| 234 | * extra information about the network state. The information |
| 235 | * may be passed up from the lower networking layers, and its |
| 236 | * meaning may be specific to a particular network type. Retrieve |
| 237 | * it with {@link android.content.Intent#getStringExtra(String)}. |
junyulai | 3822c8a | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 238 | * |
| 239 | * @deprecated See {@link NetworkInfo#getExtraInfo()}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 240 | */ |
junyulai | 3822c8a | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 241 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | public static final String EXTRA_EXTRA_INFO = "extraInfo"; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 243 | /** |
| 244 | * The lookup key for an int that provides information about |
| 245 | * our connection to the internet at large. 0 indicates no connection, |
| 246 | * 100 indicates a great connection. Retrieve it with |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 247 | * {@link android.content.Intent#getIntExtra(String, int)}. |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 248 | * {@hide} |
| 249 | */ |
| 250 | public static final String EXTRA_INET_CONDITION = "inetCondition"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 251 | /** |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 252 | * The lookup key for a {@link CaptivePortal} object included with the |
| 253 | * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal} |
| 254 | * object can be used to either indicate to the system that the captive |
| 255 | * portal has been dismissed or that the user does not want to pursue |
| 256 | * signing in to captive portal. Retrieve it with |
| 257 | * {@link android.content.Intent#getParcelableExtra(String)}. |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 258 | */ |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 259 | public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL"; |
Jan Nordqvist | 52eb29f | 2015-09-22 15:54:32 -0700 | [diff] [blame] | 260 | |
| 261 | /** |
| 262 | * Key for passing a URL to the captive portal login activity. |
| 263 | */ |
| 264 | public static final String EXTRA_CAPTIVE_PORTAL_URL = "android.net.extra.CAPTIVE_PORTAL_URL"; |
| 265 | |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 266 | /** |
Remi NGUYEN VAN | 8255c2d | 2018-05-22 10:01:53 +0900 | [diff] [blame] | 267 | * Key for passing a {@link android.net.captiveportal.CaptivePortalProbeSpec} to the captive |
| 268 | * portal login activity. |
| 269 | * {@hide} |
| 270 | */ |
Remi NGUYEN VAN | 7731c5b | 2019-01-17 14:38:31 +0900 | [diff] [blame] | 271 | @SystemApi |
| 272 | @TestApi |
Remi NGUYEN VAN | 8255c2d | 2018-05-22 10:01:53 +0900 | [diff] [blame] | 273 | public static final String EXTRA_CAPTIVE_PORTAL_PROBE_SPEC = |
| 274 | "android.net.extra.CAPTIVE_PORTAL_PROBE_SPEC"; |
| 275 | |
| 276 | /** |
Hugo Benichi | cdf3ba4 | 2016-12-14 08:23:40 +0900 | [diff] [blame] | 277 | * Key for passing a user agent string to the captive portal login activity. |
| 278 | * {@hide} |
| 279 | */ |
Remi NGUYEN VAN | 7731c5b | 2019-01-17 14:38:31 +0900 | [diff] [blame] | 280 | @SystemApi |
| 281 | @TestApi |
Hugo Benichi | cdf3ba4 | 2016-12-14 08:23:40 +0900 | [diff] [blame] | 282 | public static final String EXTRA_CAPTIVE_PORTAL_USER_AGENT = |
| 283 | "android.net.extra.CAPTIVE_PORTAL_USER_AGENT"; |
| 284 | |
| 285 | /** |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 286 | * Broadcast action to indicate the change of data activity status |
| 287 | * (idle or active) on a network in a recent period. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 288 | * The network becomes active when data transmission is started, or |
| 289 | * idle if there is no data transmission for a period of time. |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 290 | * {@hide} |
| 291 | */ |
| 292 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 293 | public static final String ACTION_DATA_ACTIVITY_CHANGE = |
| 294 | "android.net.conn.DATA_ACTIVITY_CHANGE"; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 295 | /** |
| 296 | * The lookup key for an enum that indicates the network device type on which this data activity |
| 297 | * change happens. |
| 298 | * {@hide} |
| 299 | */ |
| 300 | public static final String EXTRA_DEVICE_TYPE = "deviceType"; |
| 301 | /** |
| 302 | * The lookup key for a boolean that indicates the device is active or not. {@code true} means |
| 303 | * it is actively sending or receiving data and {@code false} means it is idle. |
| 304 | * {@hide} |
| 305 | */ |
| 306 | public static final String EXTRA_IS_ACTIVE = "isActive"; |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 307 | /** |
| 308 | * The lookup key for a long that contains the timestamp (nanos) of the radio state change. |
| 309 | * {@hide} |
| 310 | */ |
| 311 | public static final String EXTRA_REALTIME_NS = "tsNanos"; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 312 | |
| 313 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 314 | * Broadcast Action: The setting for background data usage has changed |
| 315 | * values. Use {@link #getBackgroundDataSetting()} to get the current value. |
| 316 | * <p> |
| 317 | * If an application uses the network in the background, it should listen |
| 318 | * for this broadcast and stop using the background data if the value is |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 319 | * {@code false}. |
Jeff Sharkey | 54ee2ad | 2012-01-30 16:29:24 -0800 | [diff] [blame] | 320 | * <p> |
| 321 | * |
| 322 | * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability |
| 323 | * of background data depends on several combined factors, and |
| 324 | * this broadcast is no longer sent. Instead, when background |
| 325 | * data is unavailable, {@link #getActiveNetworkInfo()} will now |
| 326 | * appear disconnected. During first boot after a platform |
| 327 | * upgrade, this broadcast will be sent once if |
| 328 | * {@link #getBackgroundDataSetting()} was {@code false} before |
| 329 | * the upgrade. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 330 | */ |
| 331 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Jeff Sharkey | 54ee2ad | 2012-01-30 16:29:24 -0800 | [diff] [blame] | 332 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED = |
| 334 | "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED"; |
| 335 | |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 336 | /** |
| 337 | * Broadcast Action: The network connection may not be good |
| 338 | * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and |
| 339 | * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify |
| 340 | * the network and it's condition. |
| 341 | * @hide |
| 342 | */ |
Jeff Sharkey | 4fa63b2 | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 343 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 344 | @UnsupportedAppUsage |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 345 | public static final String INET_CONDITION_ACTION = |
| 346 | "android.net.conn.INET_CONDITION_ACTION"; |
| 347 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 348 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 349 | * Broadcast Action: A tetherable connection has come or gone. |
| 350 | * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER}, |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 351 | * {@code ConnectivityManager.EXTRA_ACTIVE_LOCAL_ONLY}, |
| 352 | * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER}, and |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 353 | * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate |
| 354 | * the current state of tethering. Each include a list of |
| 355 | * interface names in that state (may be empty). |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 356 | * @hide |
| 357 | */ |
Jeff Sharkey | 4fa63b2 | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 358 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 359 | @UnsupportedAppUsage |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 360 | public static final String ACTION_TETHER_STATE_CHANGED = |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 361 | TetheringManager.ACTION_TETHER_STATE_CHANGED; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 362 | |
| 363 | /** |
| 364 | * @hide |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 365 | * gives a String[] listing all the interfaces configured for |
| 366 | * tethering and currently available for tethering. |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 367 | */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 368 | @UnsupportedAppUsage |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 369 | public static final String EXTRA_AVAILABLE_TETHER = TetheringManager.EXTRA_AVAILABLE_TETHER; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 370 | |
| 371 | /** |
| 372 | * @hide |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 373 | * gives a String[] listing all the interfaces currently in local-only |
| 374 | * mode (ie, has DHCPv4+IPv6-ULA support and no packet forwarding) |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 375 | */ |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 376 | public static final String EXTRA_ACTIVE_LOCAL_ONLY = TetheringManager.EXTRA_ACTIVE_LOCAL_ONLY; |
Erik Kline | 8351faa | 2017-04-17 16:47:23 +0900 | [diff] [blame] | 377 | |
| 378 | /** |
| 379 | * @hide |
| 380 | * gives a String[] listing all the interfaces currently tethered |
| 381 | * (ie, has DHCPv4 support and packets potentially forwarded/NATed) |
| 382 | */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 383 | @UnsupportedAppUsage |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 384 | public static final String EXTRA_ACTIVE_TETHER = TetheringManager.EXTRA_ACTIVE_TETHER; |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 385 | |
| 386 | /** |
| 387 | * @hide |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 388 | * gives a String[] listing all the interfaces we tried to tether and |
| 389 | * failed. Use {@link #getLastTetherError} to find the error code |
| 390 | * for any interfaces listed here. |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 391 | */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 392 | @UnsupportedAppUsage |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 393 | public static final String EXTRA_ERRORED_TETHER = TetheringManager.EXTRA_ERRORED_TETHER; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 394 | |
| 395 | /** |
Russell Brenner | 108da0c | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 396 | * Broadcast Action: The captive portal tracker has finished its test. |
| 397 | * Sent only while running Setup Wizard, in lieu of showing a user |
| 398 | * notification. |
| 399 | * @hide |
| 400 | */ |
Jeff Sharkey | 4fa63b2 | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 401 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Russell Brenner | 108da0c | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 402 | public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED = |
| 403 | "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED"; |
| 404 | /** |
| 405 | * The lookup key for a boolean that indicates whether a captive portal was detected. |
| 406 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
| 407 | * @hide |
| 408 | */ |
| 409 | public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal"; |
| 410 | |
| 411 | /** |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 412 | * Action used to display a dialog that asks the user whether to connect to a network that is |
| 413 | * not validated. This intent is used to start the dialog in settings via startActivity. |
| 414 | * |
| 415 | * @hide |
| 416 | */ |
| 417 | public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED"; |
| 418 | |
| 419 | /** |
Lorenzo Colitti | 9be58c5 | 2016-09-15 14:02:29 +0900 | [diff] [blame] | 420 | * Action used to display a dialog that asks the user whether to avoid a network that is no |
| 421 | * longer validated. This intent is used to start the dialog in settings via startActivity. |
| 422 | * |
| 423 | * @hide |
| 424 | */ |
| 425 | public static final String ACTION_PROMPT_LOST_VALIDATION = |
| 426 | "android.net.conn.PROMPT_LOST_VALIDATION"; |
| 427 | |
| 428 | /** |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 429 | * Action used to display a dialog that asks the user whether to stay connected to a network |
| 430 | * that has not validated. This intent is used to start the dialog in settings via |
| 431 | * startActivity. |
| 432 | * |
| 433 | * @hide |
| 434 | */ |
| 435 | public static final String ACTION_PROMPT_PARTIAL_CONNECTIVITY = |
| 436 | "android.net.conn.PROMPT_PARTIAL_CONNECTIVITY"; |
| 437 | |
| 438 | /** |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 439 | * Invalid tethering type. |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 440 | * @see #startTethering(int, boolean, OnStartTetheringCallback) |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 441 | * @hide |
| 442 | */ |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 443 | public static final int TETHERING_INVALID = TetheringManager.TETHERING_INVALID; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 444 | |
| 445 | /** |
| 446 | * Wifi tethering type. |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 447 | * @see #startTethering(int, boolean, OnStartTetheringCallback) |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 448 | * @hide |
| 449 | */ |
| 450 | @SystemApi |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 451 | public static final int TETHERING_WIFI = TetheringManager.TETHERING_WIFI; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 452 | |
| 453 | /** |
| 454 | * USB tethering type. |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 455 | * @see #startTethering(int, boolean, OnStartTetheringCallback) |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 456 | * @hide |
| 457 | */ |
| 458 | @SystemApi |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 459 | public static final int TETHERING_USB = TetheringManager.TETHERING_USB; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 460 | |
| 461 | /** |
| 462 | * Bluetooth tethering type. |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 463 | * @see #startTethering(int, boolean, OnStartTetheringCallback) |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 464 | * @hide |
| 465 | */ |
| 466 | @SystemApi |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 467 | public static final int TETHERING_BLUETOOTH = TetheringManager.TETHERING_BLUETOOTH; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 468 | |
| 469 | /** |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 470 | * Wifi P2p tethering type. |
| 471 | * Wifi P2p tethering is set through events automatically, and don't |
| 472 | * need to start from #startTethering(int, boolean, OnStartTetheringCallback). |
| 473 | * @hide |
| 474 | */ |
markchien | 9e44cde | 2019-12-25 19:40:32 +0800 | [diff] [blame] | 475 | public static final int TETHERING_WIFI_P2P = TetheringManager.TETHERING_WIFI_P2P; |
Jimmy Chen | bcd86d0 | 2019-07-15 18:03:23 +0800 | [diff] [blame] | 476 | |
| 477 | /** |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 478 | * Extra used for communicating with the TetherService. Includes the type of tethering to |
| 479 | * enable if any. |
| 480 | * @hide |
| 481 | */ |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 482 | public static final String EXTRA_ADD_TETHER_TYPE = TetheringConstants.EXTRA_ADD_TETHER_TYPE; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 483 | |
| 484 | /** |
| 485 | * Extra used for communicating with the TetherService. Includes the type of tethering for |
| 486 | * which to cancel provisioning. |
| 487 | * @hide |
| 488 | */ |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 489 | public static final String EXTRA_REM_TETHER_TYPE = TetheringConstants.EXTRA_REM_TETHER_TYPE; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 490 | |
| 491 | /** |
| 492 | * Extra used for communicating with the TetherService. True to schedule a recheck of tether |
| 493 | * provisioning. |
| 494 | * @hide |
| 495 | */ |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 496 | public static final String EXTRA_SET_ALARM = TetheringConstants.EXTRA_SET_ALARM; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 497 | |
| 498 | /** |
| 499 | * Tells the TetherService to run a provision check now. |
| 500 | * @hide |
| 501 | */ |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 502 | public static final String EXTRA_RUN_PROVISION = TetheringConstants.EXTRA_RUN_PROVISION; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 503 | |
| 504 | /** |
| 505 | * Extra used for communicating with the TetherService. Contains the {@link ResultReceiver} |
| 506 | * which will receive provisioning results. Can be left empty. |
| 507 | * @hide |
| 508 | */ |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 509 | public static final String EXTRA_PROVISION_CALLBACK = |
| 510 | TetheringConstants.EXTRA_PROVISION_CALLBACK; |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 511 | |
| 512 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 513 | * The absence of a connection type. |
Robert Greenwalt | ccf83af1 | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 514 | * @hide |
| 515 | */ |
paulhu | 4dc238b5 | 2020-01-13 16:46:45 +0800 | [diff] [blame] | 516 | @SystemApi |
Robert Greenwalt | ccf83af1 | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 517 | public static final int TYPE_NONE = -1; |
| 518 | |
| 519 | /** |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 520 | * A Mobile data connection. Devices may support more than one. |
| 521 | * |
| 522 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 523 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
| 524 | * appropriate network. {@see NetworkCapabilities} for supported transports. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 525 | */ |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 526 | @Deprecated |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 527 | public static final int TYPE_MOBILE = 0; |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 528 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 529 | /** |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 530 | * A WIFI data connection. Devices may support more than one. |
| 531 | * |
| 532 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 533 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
| 534 | * appropriate network. {@see NetworkCapabilities} for supported transports. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 535 | */ |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 536 | @Deprecated |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 537 | public static final int TYPE_WIFI = 1; |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 538 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 539 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 540 | * An MMS-specific Mobile data connection. This network type may use the |
| 541 | * same network interface as {@link #TYPE_MOBILE} or it may use a different |
| 542 | * one. This is used by applications needing to talk to the carrier's |
| 543 | * Multimedia Messaging Service servers. |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 544 | * |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 545 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or |
Lorenzo Colitti | 2ea89e5 | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 546 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 547 | * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 548 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 549 | @Deprecated |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 550 | public static final int TYPE_MOBILE_MMS = 2; |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 551 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 552 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 553 | * A SUPL-specific Mobile data connection. This network type may use the |
| 554 | * same network interface as {@link #TYPE_MOBILE} or it may use a different |
| 555 | * one. This is used by applications needing to talk to the carrier's |
| 556 | * Secure User Plane Location servers for help locating the device. |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 557 | * |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 558 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or |
Lorenzo Colitti | 2ea89e5 | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 559 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 560 | * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 561 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 562 | @Deprecated |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 563 | public static final int TYPE_MOBILE_SUPL = 3; |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 564 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 565 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 566 | * A DUN-specific Mobile data connection. This network type may use the |
| 567 | * same network interface as {@link #TYPE_MOBILE} or it may use a different |
| 568 | * one. This is sometimes by the system when setting up an upstream connection |
| 569 | * for tethering so that the carrier is aware of DUN traffic. |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 570 | * |
| 571 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasCapability} or |
| 572 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that |
| 573 | * provides the {@link NetworkCapabilities#NET_CAPABILITY_DUN} capability. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 574 | */ |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 575 | @Deprecated |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 576 | public static final int TYPE_MOBILE_DUN = 4; |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 577 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 578 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 579 | * A High Priority Mobile data connection. This network type uses the |
| 580 | * same network interface as {@link #TYPE_MOBILE} but the routing setup |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 581 | * is different. |
| 582 | * |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 583 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 584 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
| 585 | * appropriate network. {@see NetworkCapabilities} for supported transports. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 586 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 587 | @Deprecated |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 588 | public static final int TYPE_MOBILE_HIPRI = 5; |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 589 | |
jsh | 8214deb | 2010-03-11 15:04:43 -0800 | [diff] [blame] | 590 | /** |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 591 | * A WiMAX data connection. |
| 592 | * |
| 593 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 594 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
| 595 | * appropriate network. {@see NetworkCapabilities} for supported transports. |
jsh | 8214deb | 2010-03-11 15:04:43 -0800 | [diff] [blame] | 596 | */ |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 597 | @Deprecated |
jsh | 8214deb | 2010-03-11 15:04:43 -0800 | [diff] [blame] | 598 | public static final int TYPE_WIMAX = 6; |
Robert Greenwalt | da3d5e6 | 2010-12-06 13:56:24 -0800 | [diff] [blame] | 599 | |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 600 | /** |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 601 | * A Bluetooth data connection. |
| 602 | * |
| 603 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 604 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
| 605 | * appropriate network. {@see NetworkCapabilities} for supported transports. |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 606 | */ |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 607 | @Deprecated |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 608 | public static final int TYPE_BLUETOOTH = 7; |
| 609 | |
Robert Greenwalt | 6081084 | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 610 | /** |
| 611 | * Dummy data connection. This should not be used on shipping devices. |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 612 | * @deprecated This is not used any more. |
Robert Greenwalt | 6081084 | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 613 | */ |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 614 | @Deprecated |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 615 | public static final int TYPE_DUMMY = 8; |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 616 | |
Robert Greenwalt | 6081084 | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 617 | /** |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 618 | * An Ethernet data connection. |
| 619 | * |
| 620 | * @deprecated Applications should instead use {@link NetworkCapabilities#hasTransport} or |
| 621 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request an |
| 622 | * appropriate network. {@see NetworkCapabilities} for supported transports. |
Robert Greenwalt | 6081084 | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 623 | */ |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 624 | @Deprecated |
Robert Greenwalt | e12aec9 | 2011-01-28 14:48:37 -0800 | [diff] [blame] | 625 | public static final int TYPE_ETHERNET = 9; |
Robert Greenwalt | 6081084 | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 626 | |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 627 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 628 | * Over the air Administration. |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 629 | * @deprecated Use {@link NetworkCapabilities} instead. |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 630 | * {@hide} |
| 631 | */ |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 632 | @Deprecated |
Chalard Jean | 0bb53dbb | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 633 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 634 | public static final int TYPE_MOBILE_FOTA = 10; |
| 635 | |
| 636 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 637 | * IP Multimedia Subsystem. |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 638 | * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IMS} instead. |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 639 | * {@hide} |
| 640 | */ |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 641 | @Deprecated |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 642 | @UnsupportedAppUsage |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 643 | public static final int TYPE_MOBILE_IMS = 11; |
| 644 | |
| 645 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 646 | * Carrier Branded Services. |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 647 | * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_CBS} instead. |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 648 | * {@hide} |
| 649 | */ |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 650 | @Deprecated |
Chalard Jean | 0bb53dbb | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 651 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 652 | public static final int TYPE_MOBILE_CBS = 12; |
| 653 | |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 654 | /** |
| 655 | * A Wi-Fi p2p connection. Only requesting processes will have access to |
| 656 | * the peers connected. |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 657 | * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_WIFI_P2P} instead. |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 658 | * {@hide} |
| 659 | */ |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 660 | @Deprecated |
paulhu | 5ae1d2c | 2020-01-15 15:38:23 +0800 | [diff] [blame] | 661 | @SystemApi |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 662 | public static final int TYPE_WIFI_P2P = 13; |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 663 | |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 664 | /** |
| 665 | * The network to use for initially attaching to the network |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 666 | * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_IA} instead. |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 667 | * {@hide} |
| 668 | */ |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 669 | @Deprecated |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 670 | @UnsupportedAppUsage |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 671 | public static final int TYPE_MOBILE_IA = 14; |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 672 | |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 673 | /** |
Robert Greenwalt | 4bd4389 | 2015-07-09 14:49:35 -0700 | [diff] [blame] | 674 | * Emergency PDN connection for emergency services. This |
| 675 | * may include IMS and MMS in emergency situations. |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 676 | * @deprecated Use {@link NetworkCapabilities#NET_CAPABILITY_EIMS} instead. |
Ram | 3e0e3bc | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 677 | * {@hide} |
| 678 | */ |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 679 | @Deprecated |
Chalard Jean | 0bb53dbb | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 680 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Ram | 3e0e3bc | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 681 | public static final int TYPE_MOBILE_EMERGENCY = 15; |
| 682 | |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 683 | /** |
| 684 | * The network that uses proxy to achieve connectivity. |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 685 | * @deprecated Use {@link NetworkCapabilities} instead. |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 686 | * {@hide} |
| 687 | */ |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 688 | @Deprecated |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 689 | @UnsupportedAppUsage |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 690 | public static final int TYPE_PROXY = 16; |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 691 | |
Robert Greenwalt | 8283f88 | 2014-07-07 17:09:01 -0700 | [diff] [blame] | 692 | /** |
| 693 | * A virtual network using one or more native bearers. |
| 694 | * It may or may not be providing security services. |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 695 | * @deprecated Applications should use {@link NetworkCapabilities#TRANSPORT_VPN} instead. |
Robert Greenwalt | 8283f88 | 2014-07-07 17:09:01 -0700 | [diff] [blame] | 696 | */ |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 697 | @Deprecated |
Robert Greenwalt | 8283f88 | 2014-07-07 17:09:01 -0700 | [diff] [blame] | 698 | public static final int TYPE_VPN = 17; |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 699 | |
Benedict Wong | 89ce5e3 | 2018-11-14 17:40:55 -0800 | [diff] [blame] | 700 | /** |
| 701 | * A network that is exclusively meant to be used for testing |
| 702 | * |
| 703 | * @deprecated Use {@link NetworkCapabilities} instead. |
| 704 | * @hide |
| 705 | */ |
| 706 | @Deprecated |
| 707 | public static final int TYPE_TEST = 18; // TODO: Remove this once NetworkTypes are unused. |
Robert Greenwalt | 8283f88 | 2014-07-07 17:09:01 -0700 | [diff] [blame] | 708 | |
Chalard Jean | 1d2d35d | 2020-03-25 01:24:04 +0900 | [diff] [blame] | 709 | /** |
| 710 | * @deprecated Use {@link NetworkCapabilities} instead. |
| 711 | * @hide |
| 712 | */ |
| 713 | @Deprecated |
| 714 | @Retention(RetentionPolicy.SOURCE) |
| 715 | @IntDef(prefix = { "TYPE_" }, value = { |
| 716 | TYPE_NONE, |
| 717 | TYPE_MOBILE, |
| 718 | TYPE_WIFI, |
| 719 | TYPE_MOBILE_MMS, |
| 720 | TYPE_MOBILE_SUPL, |
| 721 | TYPE_MOBILE_DUN, |
| 722 | TYPE_MOBILE_HIPRI, |
| 723 | TYPE_WIMAX, |
| 724 | TYPE_BLUETOOTH, |
| 725 | TYPE_DUMMY, |
| 726 | TYPE_ETHERNET, |
| 727 | TYPE_MOBILE_FOTA, |
| 728 | TYPE_MOBILE_IMS, |
| 729 | TYPE_MOBILE_CBS, |
| 730 | TYPE_WIFI_P2P, |
| 731 | TYPE_MOBILE_IA, |
| 732 | TYPE_MOBILE_EMERGENCY, |
| 733 | TYPE_PROXY, |
| 734 | TYPE_VPN, |
| 735 | TYPE_TEST |
| 736 | }) |
| 737 | public @interface LegacyNetworkType {} |
| 738 | |
Chalard Jean | c06d788 | 2019-11-21 14:48:00 +0900 | [diff] [blame] | 739 | // Deprecated constants for return values of startUsingNetworkFeature. They used to live |
| 740 | // in com.android.internal.telephony.PhoneConstants until they were made inaccessible. |
| 741 | private static final int DEPRECATED_PHONE_CONSTANT_APN_ALREADY_ACTIVE = 0; |
| 742 | private static final int DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED = 1; |
| 743 | private static final int DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED = 3; |
| 744 | |
Robert Greenwalt | 8283f88 | 2014-07-07 17:09:01 -0700 | [diff] [blame] | 745 | /** {@hide} */ |
Benedict Wong | 89ce5e3 | 2018-11-14 17:40:55 -0800 | [diff] [blame] | 746 | public static final int MAX_RADIO_TYPE = TYPE_TEST; |
| 747 | |
| 748 | /** {@hide} */ |
| 749 | public static final int MAX_NETWORK_TYPE = TYPE_TEST; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 750 | |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 751 | private static final int MIN_NETWORK_TYPE = TYPE_MOBILE; |
| 752 | |
Jianzheng Zhou | dcf03f3 | 2012-11-16 13:45:20 +0800 | [diff] [blame] | 753 | /** |
| 754 | * If you want to set the default network preference,you can directly |
| 755 | * change the networkAttributes array in framework's config.xml. |
| 756 | * |
| 757 | * @deprecated Since we support so many more networks now, the single |
| 758 | * network default network preference can't really express |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 759 | * the hierarchy. Instead, the default is defined by the |
Jianzheng Zhou | dcf03f3 | 2012-11-16 13:45:20 +0800 | [diff] [blame] | 760 | * networkAttributes in config.xml. You can determine |
Robert Greenwalt | 4c8b748 | 2012-12-07 09:56:50 -0800 | [diff] [blame] | 761 | * the current value by calling {@link #getNetworkPreference()} |
Jianzheng Zhou | dcf03f3 | 2012-11-16 13:45:20 +0800 | [diff] [blame] | 762 | * from an App. |
| 763 | */ |
| 764 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 765 | public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI; |
| 766 | |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 767 | /** |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 768 | * @hide |
| 769 | */ |
Hugo Benichi | a5c1f7f | 2017-06-20 14:10:14 +0900 | [diff] [blame] | 770 | public static final int REQUEST_ID_UNSET = 0; |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 771 | |
Paul Jensen | 5d59e78 | 2014-07-11 12:28:19 -0400 | [diff] [blame] | 772 | /** |
Hugo Benichi | 31c176d | 2017-06-17 13:14:12 +0900 | [diff] [blame] | 773 | * Static unique request used as a tombstone for NetworkCallbacks that have been unregistered. |
| 774 | * This allows to distinguish when unregistering NetworkCallbacks those that were never |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 775 | * registered from those that were already unregistered. |
Hugo Benichi | 31c176d | 2017-06-17 13:14:12 +0900 | [diff] [blame] | 776 | * @hide |
| 777 | */ |
Hugo Benichi | a5c1f7f | 2017-06-20 14:10:14 +0900 | [diff] [blame] | 778 | private static final NetworkRequest ALREADY_UNREGISTERED = |
Hugo Benichi | 31c176d | 2017-06-17 13:14:12 +0900 | [diff] [blame] | 779 | new NetworkRequest.Builder().clearCapabilities().build(); |
| 780 | |
| 781 | /** |
Paul Jensen | 5d59e78 | 2014-07-11 12:28:19 -0400 | [diff] [blame] | 782 | * A NetID indicating no Network is selected. |
| 783 | * Keep in sync with bionic/libc/dns/include/resolv_netid.h |
| 784 | * @hide |
| 785 | */ |
| 786 | public static final int NETID_UNSET = 0; |
| 787 | |
Erik Kline | 4d09223 | 2017-10-30 15:29:44 +0900 | [diff] [blame] | 788 | /** |
| 789 | * Private DNS Mode values. |
| 790 | * |
| 791 | * The "private_dns_mode" global setting stores a String value which is |
| 792 | * expected to be one of the following. |
| 793 | */ |
| 794 | |
| 795 | /** |
| 796 | * @hide |
| 797 | */ |
| 798 | public static final String PRIVATE_DNS_MODE_OFF = "off"; |
| 799 | /** |
| 800 | * @hide |
| 801 | */ |
| 802 | public static final String PRIVATE_DNS_MODE_OPPORTUNISTIC = "opportunistic"; |
| 803 | /** |
| 804 | * @hide |
| 805 | */ |
| 806 | public static final String PRIVATE_DNS_MODE_PROVIDER_HOSTNAME = "hostname"; |
| 807 | /** |
| 808 | * The default Private DNS mode. |
| 809 | * |
| 810 | * This may change from release to release or may become dependent upon |
| 811 | * the capabilities of the underlying platform. |
| 812 | * |
| 813 | * @hide |
| 814 | */ |
Erik Kline | 1984179 | 2018-05-16 16:41:57 +0900 | [diff] [blame] | 815 | public static final String PRIVATE_DNS_DEFAULT_MODE_FALLBACK = PRIVATE_DNS_MODE_OPPORTUNISTIC; |
Erik Kline | 4d09223 | 2017-10-30 15:29:44 +0900 | [diff] [blame] | 816 | |
Chalard Jean | 0bb53dbb | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 817 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 818 | private final IConnectivityManager mService; |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 819 | /** |
| 820 | * A kludge to facilitate static access where a Context pointer isn't available, like in the |
| 821 | * case of the static set/getProcessDefaultNetwork methods and from the Network class. |
| 822 | * TODO: Remove this after deprecating the static methods in favor of non-static methods or |
| 823 | * methods that take a Context argument. |
| 824 | */ |
| 825 | private static ConnectivityManager sInstance; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 826 | |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 827 | private final Context mContext; |
| 828 | |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 829 | private INetworkManagementService mNMService; |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 830 | private INetworkPolicyManager mNPManager; |
Automerger Merge Worker | 6112c82 | 2020-03-06 00:38:43 +0000 | [diff] [blame] | 831 | private final TetheringManager mTetheringManager; |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 832 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 833 | /** |
| 834 | * Tests if a given integer represents a valid network type. |
| 835 | * @param networkType the type to be tested |
| 836 | * @return a boolean. {@code true} if the type is valid, else {@code false} |
Paul Jensen | 9e59e12 | 2015-05-06 10:42:25 -0400 | [diff] [blame] | 837 | * @deprecated All APIs accepting a network type are deprecated. There should be no need to |
| 838 | * validate a network type. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 839 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 840 | @Deprecated |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 841 | public static boolean isNetworkTypeValid(int networkType) { |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 842 | return MIN_NETWORK_TYPE <= networkType && networkType <= MAX_NETWORK_TYPE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 843 | } |
| 844 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 845 | /** |
| 846 | * Returns a non-localized string representing a given network type. |
| 847 | * ONLY used for debugging output. |
| 848 | * @param type the type needing naming |
| 849 | * @return a String for the given type, or a string version of the type ("87") |
| 850 | * if no name is known. |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 851 | * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 852 | * {@hide} |
| 853 | */ |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 854 | @Deprecated |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 855 | @UnsupportedAppUsage |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 856 | public static String getNetworkTypeName(int type) { |
| 857 | switch (type) { |
Hugo Benichi | 16f0a94 | 2017-06-20 14:07:59 +0900 | [diff] [blame] | 858 | case TYPE_NONE: |
| 859 | return "NONE"; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 860 | case TYPE_MOBILE: |
| 861 | return "MOBILE"; |
| 862 | case TYPE_WIFI: |
| 863 | return "WIFI"; |
| 864 | case TYPE_MOBILE_MMS: |
| 865 | return "MOBILE_MMS"; |
| 866 | case TYPE_MOBILE_SUPL: |
| 867 | return "MOBILE_SUPL"; |
| 868 | case TYPE_MOBILE_DUN: |
| 869 | return "MOBILE_DUN"; |
| 870 | case TYPE_MOBILE_HIPRI: |
| 871 | return "MOBILE_HIPRI"; |
| 872 | case TYPE_WIMAX: |
| 873 | return "WIMAX"; |
| 874 | case TYPE_BLUETOOTH: |
| 875 | return "BLUETOOTH"; |
| 876 | case TYPE_DUMMY: |
| 877 | return "DUMMY"; |
| 878 | case TYPE_ETHERNET: |
| 879 | return "ETHERNET"; |
| 880 | case TYPE_MOBILE_FOTA: |
| 881 | return "MOBILE_FOTA"; |
| 882 | case TYPE_MOBILE_IMS: |
| 883 | return "MOBILE_IMS"; |
| 884 | case TYPE_MOBILE_CBS: |
| 885 | return "MOBILE_CBS"; |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 886 | case TYPE_WIFI_P2P: |
| 887 | return "WIFI_P2P"; |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 888 | case TYPE_MOBILE_IA: |
| 889 | return "MOBILE_IA"; |
Ram | 3e0e3bc | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 890 | case TYPE_MOBILE_EMERGENCY: |
| 891 | return "MOBILE_EMERGENCY"; |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 892 | case TYPE_PROXY: |
| 893 | return "PROXY"; |
Erik Kline | 37fbfa1 | 2014-11-19 17:23:41 +0900 | [diff] [blame] | 894 | case TYPE_VPN: |
| 895 | return "VPN"; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 896 | default: |
| 897 | return Integer.toString(type); |
| 898 | } |
| 899 | } |
| 900 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 901 | /** |
| 902 | * Checks if a given type uses the cellular data connection. |
| 903 | * This should be replaced in the future by a network property. |
| 904 | * @param networkType the type to check |
| 905 | * @return a boolean - {@code true} if uses cellular network, else {@code false} |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 906 | * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 907 | * {@hide} |
| 908 | */ |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 909 | @Deprecated |
Chalard Jean | 0bb53dbb | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 910 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 911 | public static boolean isNetworkTypeMobile(int networkType) { |
| 912 | switch (networkType) { |
| 913 | case TYPE_MOBILE: |
| 914 | case TYPE_MOBILE_MMS: |
| 915 | case TYPE_MOBILE_SUPL: |
| 916 | case TYPE_MOBILE_DUN: |
| 917 | case TYPE_MOBILE_HIPRI: |
| 918 | case TYPE_MOBILE_FOTA: |
| 919 | case TYPE_MOBILE_IMS: |
| 920 | case TYPE_MOBILE_CBS: |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 921 | case TYPE_MOBILE_IA: |
Ram | 3e0e3bc | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 922 | case TYPE_MOBILE_EMERGENCY: |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 923 | return true; |
| 924 | default: |
| 925 | return false; |
| 926 | } |
| 927 | } |
| 928 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 929 | /** |
Jeff Sharkey | 7a1c3fc | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 930 | * Checks if the given network type is backed by a Wi-Fi radio. |
| 931 | * |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 932 | * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead. |
Jeff Sharkey | 7a1c3fc | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 933 | * @hide |
| 934 | */ |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 935 | @Deprecated |
Jeff Sharkey | 7a1c3fc | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 936 | public static boolean isNetworkTypeWifi(int networkType) { |
| 937 | switch (networkType) { |
| 938 | case TYPE_WIFI: |
| 939 | case TYPE_WIFI_P2P: |
| 940 | return true; |
| 941 | default: |
| 942 | return false; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 947 | * Specifies the preferred network type. When the device has more |
| 948 | * than one type available the preferred network type will be used. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 949 | * |
| 950 | * @param preference the network type to prefer over all others. It is |
| 951 | * unspecified what happens to the old preferred network in the |
| 952 | * overall ordering. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 953 | * @deprecated Functionality has been removed as it no longer makes sense, |
| 954 | * with many more than two networks - we'd need an array to express |
| 955 | * preference. Instead we use dynamic network properties of |
| 956 | * the networks to describe their precedence. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 957 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 958 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 959 | public void setNetworkPreference(int preference) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 960 | } |
| 961 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 962 | /** |
| 963 | * Retrieves the current preferred network type. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 964 | * |
| 965 | * @return an integer representing the preferred network type |
| 966 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 967 | * @deprecated Functionality has been removed as it no longer makes sense, |
| 968 | * with many more than two networks - we'd need an array to express |
| 969 | * preference. Instead we use dynamic network properties of |
| 970 | * the networks to describe their precedence. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 971 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 972 | @Deprecated |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 973 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 974 | public int getNetworkPreference() { |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 975 | return TYPE_NONE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 976 | } |
| 977 | |
Scott Main | 671644c | 2011-10-06 19:02:28 -0700 | [diff] [blame] | 978 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 979 | * Returns details about the currently active default data network. When |
| 980 | * connected, this network is the default route for outgoing connections. |
| 981 | * You should always check {@link NetworkInfo#isConnected()} before initiating |
| 982 | * network traffic. This may return {@code null} when there is no default |
| 983 | * network. |
Chalard Jean | 5a041d1 | 2018-03-29 17:45:24 +0900 | [diff] [blame] | 984 | * Note that if the default network is a VPN, this method will return the |
| 985 | * NetworkInfo for one of its underlying networks instead, or null if the |
| 986 | * VPN agent did not specify any. Apps interested in learning about VPNs |
| 987 | * should use {@link #getNetworkInfo(android.net.Network)} instead. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 988 | * |
| 989 | * @return a {@link NetworkInfo} object for the current default network |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 990 | * or {@code null} if no default network is currently active |
junyulai | 3822c8a | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 991 | * @deprecated See {@link NetworkInfo}. |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 992 | */ |
junyulai | 3822c8a | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 993 | @Deprecated |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 994 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 995 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 996 | public NetworkInfo getActiveNetworkInfo() { |
| 997 | try { |
| 998 | return mService.getActiveNetworkInfo(); |
| 999 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1000 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1001 | } |
| 1002 | } |
| 1003 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1004 | /** |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1005 | * Returns a {@link Network} object corresponding to the currently active |
| 1006 | * default data network. In the event that the current active default data |
| 1007 | * network disconnects, the returned {@code Network} object will no longer |
| 1008 | * be usable. This will return {@code null} when there is no default |
| 1009 | * network. |
| 1010 | * |
| 1011 | * @return a {@link Network} object for the current default network or |
| 1012 | * {@code null} if no default network is currently active |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1013 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1014 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1015 | @Nullable |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1016 | public Network getActiveNetwork() { |
| 1017 | try { |
| 1018 | return mService.getActiveNetwork(); |
| 1019 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1020 | throw e.rethrowFromSystemServer(); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1021 | } |
| 1022 | } |
| 1023 | |
| 1024 | /** |
Robin Lee | d2baf79 | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1025 | * Returns a {@link Network} object corresponding to the currently active |
| 1026 | * default data network for a specific UID. In the event that the default data |
| 1027 | * network disconnects, the returned {@code Network} object will no longer |
| 1028 | * be usable. This will return {@code null} when there is no default |
| 1029 | * network for the UID. |
Robin Lee | d2baf79 | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1030 | * |
| 1031 | * @return a {@link Network} object for the current default network for the |
| 1032 | * given UID or {@code null} if no default network is currently active |
| 1033 | * |
| 1034 | * @hide |
| 1035 | */ |
paulhu | a6af6b6 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1036 | @RequiresPermission(android.Manifest.permission.NETWORK_STACK) |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1037 | @Nullable |
Robin Lee | d2baf79 | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1038 | public Network getActiveNetworkForUid(int uid) { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1039 | return getActiveNetworkForUid(uid, false); |
| 1040 | } |
| 1041 | |
| 1042 | /** {@hide} */ |
| 1043 | public Network getActiveNetworkForUid(int uid, boolean ignoreBlocked) { |
Robin Lee | d2baf79 | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1044 | try { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1045 | return mService.getActiveNetworkForUid(uid, ignoreBlocked); |
Robin Lee | d2baf79 | 2016-03-24 12:07:00 +0000 | [diff] [blame] | 1046 | } catch (RemoteException e) { |
| 1047 | throw e.rethrowFromSystemServer(); |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | /** |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 1052 | * Checks if a VPN app supports always-on mode. |
| 1053 | * |
| 1054 | * In order to support the always-on feature, an app has to |
| 1055 | * <ul> |
| 1056 | * <li>target {@link VERSION_CODES#N API 24} or above, and |
Charles He | c57a01c | 2017-08-15 15:30:22 +0100 | [diff] [blame] | 1057 | * <li>not opt out through the {@link VpnService#SERVICE_META_DATA_SUPPORTS_ALWAYS_ON} |
| 1058 | * meta-data field. |
Charles He | 3673863 | 2017-05-15 17:07:18 +0100 | [diff] [blame] | 1059 | * </ul> |
| 1060 | * |
| 1061 | * @param userId The identifier of the user for whom the VPN app is installed. |
| 1062 | * @param vpnPackage The canonical package name of the VPN app. |
| 1063 | * @return {@code true} if and only if the VPN app exists and supports always-on mode. |
| 1064 | * @hide |
| 1065 | */ |
| 1066 | public boolean isAlwaysOnVpnPackageSupportedForUser(int userId, @Nullable String vpnPackage) { |
| 1067 | try { |
| 1068 | return mService.isAlwaysOnVpnPackageSupported(userId, vpnPackage); |
| 1069 | } catch (RemoteException e) { |
| 1070 | throw e.rethrowFromSystemServer(); |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | /** |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 1075 | * Configures an always-on VPN connection through a specific application. |
| 1076 | * This connection is automatically granted and persisted after a reboot. |
| 1077 | * |
| 1078 | * <p>The designated package should declare a {@link VpnService} in its |
| 1079 | * manifest guarded by {@link android.Manifest.permission.BIND_VPN_SERVICE}, |
| 1080 | * otherwise the call will fail. |
| 1081 | * |
| 1082 | * @param userId The identifier of the user to set an always-on VPN for. |
| 1083 | * @param vpnPackage The package name for an installed VPN app on the device, or {@code null} |
| 1084 | * to remove an existing always-on VPN configuration. |
Robin Lee | dc67971 | 2016-05-03 13:23:03 +0100 | [diff] [blame] | 1085 | * @param lockdownEnabled {@code true} to disallow networking when the VPN is not connected or |
| 1086 | * {@code false} otherwise. |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 1087 | * @param lockdownWhitelist The list of packages that are allowed to access network directly |
| 1088 | * when VPN is in lockdown mode but is not running. Non-existent packages are ignored so |
| 1089 | * this method must be called when a package that should be whitelisted is installed or |
| 1090 | * uninstalled. |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 1091 | * @return {@code true} if the package is set as always-on VPN controller; |
| 1092 | * {@code false} otherwise. |
| 1093 | * @hide |
| 1094 | */ |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 1095 | @RequiresPermission(android.Manifest.permission.CONTROL_ALWAYS_ON_VPN) |
Robin Lee | dc67971 | 2016-05-03 13:23:03 +0100 | [diff] [blame] | 1096 | public boolean setAlwaysOnVpnPackageForUser(int userId, @Nullable String vpnPackage, |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 1097 | boolean lockdownEnabled, @Nullable List<String> lockdownWhitelist) { |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 1098 | try { |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 1099 | return mService.setAlwaysOnVpnPackage( |
| 1100 | userId, vpnPackage, lockdownEnabled, lockdownWhitelist); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 1101 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1102 | throw e.rethrowFromSystemServer(); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 1103 | } |
| 1104 | } |
| 1105 | |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 1106 | /** |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 1107 | * Returns the package name of the currently set always-on VPN application. |
| 1108 | * If there is no always-on VPN set, or the VPN is provided by the system instead |
| 1109 | * of by an app, {@code null} will be returned. |
| 1110 | * |
| 1111 | * @return Package name of VPN controller responsible for always-on VPN, |
| 1112 | * or {@code null} if none is set. |
| 1113 | * @hide |
| 1114 | */ |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 1115 | @RequiresPermission(android.Manifest.permission.CONTROL_ALWAYS_ON_VPN) |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 1116 | public String getAlwaysOnVpnPackageForUser(int userId) { |
| 1117 | try { |
| 1118 | return mService.getAlwaysOnVpnPackage(userId); |
| 1119 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1120 | throw e.rethrowFromSystemServer(); |
Robin Lee | 244ce8e | 2016-01-05 18:03:46 +0000 | [diff] [blame] | 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | /** |
Pavel Grafov | a462bcb | 2019-01-25 08:50:06 +0000 | [diff] [blame] | 1125 | * @return whether always-on VPN is in lockdown mode. |
| 1126 | * |
| 1127 | * @hide |
| 1128 | **/ |
| 1129 | @RequiresPermission(android.Manifest.permission.CONTROL_ALWAYS_ON_VPN) |
| 1130 | public boolean isVpnLockdownEnabled(int userId) { |
| 1131 | try { |
| 1132 | return mService.isVpnLockdownEnabled(userId); |
| 1133 | } catch (RemoteException e) { |
| 1134 | throw e.rethrowFromSystemServer(); |
| 1135 | } |
| 1136 | |
| 1137 | } |
| 1138 | |
| 1139 | /** |
| 1140 | * @return the list of packages that are allowed to access network when always-on VPN is in |
| 1141 | * lockdown mode but not connected. Returns {@code null} when VPN lockdown is not active. |
| 1142 | * |
| 1143 | * @hide |
| 1144 | **/ |
| 1145 | @RequiresPermission(android.Manifest.permission.CONTROL_ALWAYS_ON_VPN) |
| 1146 | public List<String> getVpnLockdownWhitelist(int userId) { |
| 1147 | try { |
| 1148 | return mService.getVpnLockdownWhitelist(userId); |
| 1149 | } catch (RemoteException e) { |
| 1150 | throw e.rethrowFromSystemServer(); |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1155 | * Returns details about the currently active default data network |
| 1156 | * for a given uid. This is for internal use only to avoid spying |
| 1157 | * other apps. |
| 1158 | * |
| 1159 | * @return a {@link NetworkInfo} object for the current default network |
| 1160 | * for the given uid or {@code null} if no default network is |
| 1161 | * available for the specified uid. |
| 1162 | * |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1163 | * {@hide} |
| 1164 | */ |
paulhu | a6af6b6 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1165 | @RequiresPermission(android.Manifest.permission.NETWORK_STACK) |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 1166 | @UnsupportedAppUsage |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1167 | public NetworkInfo getActiveNetworkInfoForUid(int uid) { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1168 | return getActiveNetworkInfoForUid(uid, false); |
| 1169 | } |
| 1170 | |
| 1171 | /** {@hide} */ |
| 1172 | public NetworkInfo getActiveNetworkInfoForUid(int uid, boolean ignoreBlocked) { |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1173 | try { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1174 | return mService.getActiveNetworkInfoForUid(uid, ignoreBlocked); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1175 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1176 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 1177 | } |
| 1178 | } |
| 1179 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1180 | /** |
| 1181 | * Returns connection status information about a particular |
| 1182 | * network type. |
| 1183 | * |
| 1184 | * @param networkType integer specifying which networkType in |
| 1185 | * which you're interested. |
| 1186 | * @return a {@link NetworkInfo} object for the requested |
| 1187 | * network type or {@code null} if the type is not |
Chalard Jean | 5a041d1 | 2018-03-29 17:45:24 +0900 | [diff] [blame] | 1188 | * supported by the device. If {@code networkType} is |
| 1189 | * TYPE_VPN and a VPN is active for the calling app, |
| 1190 | * then this method will try to return one of the |
| 1191 | * underlying networks for the VPN or null if the |
| 1192 | * VPN agent didn't specify any. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1193 | * |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 1194 | * @deprecated This method does not support multiple connected networks |
| 1195 | * of the same type. Use {@link #getAllNetworks} and |
| 1196 | * {@link #getNetworkInfo(android.net.Network)} instead. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1197 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 1198 | @Deprecated |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1199 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1200 | @Nullable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1201 | public NetworkInfo getNetworkInfo(int networkType) { |
| 1202 | try { |
| 1203 | return mService.getNetworkInfo(networkType); |
| 1204 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1205 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1206 | } |
| 1207 | } |
| 1208 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1209 | /** |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1210 | * Returns connection status information about a particular |
| 1211 | * Network. |
| 1212 | * |
| 1213 | * @param network {@link Network} specifying which network |
| 1214 | * in which you're interested. |
| 1215 | * @return a {@link NetworkInfo} object for the requested |
| 1216 | * network or {@code null} if the {@code Network} |
| 1217 | * is not valid. |
junyulai | 3822c8a | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 1218 | * @deprecated See {@link NetworkInfo}. |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1219 | */ |
junyulai | 3822c8a | 2018-12-13 12:47:51 +0800 | [diff] [blame] | 1220 | @Deprecated |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1221 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1222 | @Nullable |
| 1223 | public NetworkInfo getNetworkInfo(@Nullable Network network) { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1224 | return getNetworkInfoForUid(network, Process.myUid(), false); |
| 1225 | } |
| 1226 | |
| 1227 | /** {@hide} */ |
| 1228 | public NetworkInfo getNetworkInfoForUid(Network network, int uid, boolean ignoreBlocked) { |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1229 | try { |
Jeff Sharkey | 1b6519b | 2016-04-28 15:33:18 -0600 | [diff] [blame] | 1230 | return mService.getNetworkInfoForUid(network, uid, ignoreBlocked); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1231 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1232 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1233 | } |
| 1234 | } |
| 1235 | |
| 1236 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1237 | * Returns connection status information about all network |
| 1238 | * types supported by the device. |
| 1239 | * |
| 1240 | * @return an array of {@link NetworkInfo} objects. Check each |
| 1241 | * {@link NetworkInfo#getType} for which type each applies. |
| 1242 | * |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 1243 | * @deprecated This method does not support multiple connected networks |
| 1244 | * of the same type. Use {@link #getAllNetworks} and |
| 1245 | * {@link #getNetworkInfo(android.net.Network)} instead. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1246 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 1247 | @Deprecated |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1248 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1249 | @NonNull |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1250 | public NetworkInfo[] getAllNetworkInfo() { |
| 1251 | try { |
| 1252 | return mService.getAllNetworkInfo(); |
| 1253 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1254 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1255 | } |
| 1256 | } |
| 1257 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1258 | /** |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1259 | * Returns the {@link Network} object currently serving a given type, or |
| 1260 | * null if the given type is not connected. |
| 1261 | * |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1262 | * @hide |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 1263 | * @deprecated This method does not support multiple connected networks |
| 1264 | * of the same type. Use {@link #getAllNetworks} and |
| 1265 | * {@link #getNetworkInfo(android.net.Network)} instead. |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1266 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 1267 | @Deprecated |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1268 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 1269 | @UnsupportedAppUsage |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1270 | public Network getNetworkForType(int networkType) { |
| 1271 | try { |
| 1272 | return mService.getNetworkForType(networkType); |
| 1273 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1274 | throw e.rethrowFromSystemServer(); |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | /** |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1279 | * Returns an array of all {@link Network} currently tracked by the |
| 1280 | * framework. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1281 | * |
| 1282 | * @return an array of {@link Network} objects. |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1283 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1284 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1285 | @NonNull |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1286 | public Network[] getAllNetworks() { |
| 1287 | try { |
| 1288 | return mService.getAllNetworks(); |
| 1289 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1290 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 1291 | } |
| 1292 | } |
| 1293 | |
| 1294 | /** |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 1295 | * Returns an array of {@link android.net.NetworkCapabilities} objects, representing |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1296 | * the Networks that applications run by the given user will use by default. |
| 1297 | * @hide |
| 1298 | */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 1299 | @UnsupportedAppUsage |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1300 | public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) { |
| 1301 | try { |
Qingxi Li | 9c5d8b9 | 2020-01-08 12:51:49 -0800 | [diff] [blame] | 1302 | return mService.getDefaultNetworkCapabilitiesForUser( |
| 1303 | userId, mContext.getOpPackageName()); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1304 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1305 | throw e.rethrowFromSystemServer(); |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 1306 | } |
| 1307 | } |
| 1308 | |
| 1309 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1310 | * Returns the IP information for the current default network. |
| 1311 | * |
| 1312 | * @return a {@link LinkProperties} object describing the IP info |
| 1313 | * for the current default network, or {@code null} if there |
| 1314 | * is no current default network. |
| 1315 | * |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1316 | * {@hide} |
Chalard Jean | eb1ea88 | 2019-01-11 16:47:53 +0900 | [diff] [blame] | 1317 | * @deprecated please use {@link #getLinkProperties(Network)} on the return |
| 1318 | * value of {@link #getActiveNetwork()} instead. In particular, |
| 1319 | * this method will return non-null LinkProperties even if the |
| 1320 | * app is blocked by policy from using this network. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1321 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1322 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | eb1ea88 | 2019-01-11 16:47:53 +0900 | [diff] [blame] | 1323 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 109783091) |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1324 | public LinkProperties getActiveLinkProperties() { |
| 1325 | try { |
| 1326 | return mService.getActiveLinkProperties(); |
| 1327 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1328 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1329 | } |
| 1330 | } |
| 1331 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1332 | /** |
| 1333 | * Returns the IP information for a given network type. |
| 1334 | * |
| 1335 | * @param networkType the network type of interest. |
| 1336 | * @return a {@link LinkProperties} object describing the IP info |
| 1337 | * for the given networkType, or {@code null} if there is |
| 1338 | * no current default network. |
| 1339 | * |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1340 | * {@hide} |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 1341 | * @deprecated This method does not support multiple connected networks |
| 1342 | * of the same type. Use {@link #getAllNetworks}, |
| 1343 | * {@link #getNetworkInfo(android.net.Network)}, and |
| 1344 | * {@link #getLinkProperties(android.net.Network)} instead. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1345 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 1346 | @Deprecated |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1347 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 0bb53dbb | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 1348 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1349 | public LinkProperties getLinkProperties(int networkType) { |
| 1350 | try { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1351 | return mService.getLinkPropertiesForType(networkType); |
| 1352 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1353 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1354 | } |
| 1355 | } |
| 1356 | |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1357 | /** |
| 1358 | * Get the {@link LinkProperties} for the given {@link Network}. This |
| 1359 | * will return {@code null} if the network is unknown. |
| 1360 | * |
| 1361 | * @param network The {@link Network} object identifying the network in question. |
| 1362 | * @return The {@link LinkProperties} for the network, or {@code null}. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1363 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1364 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1365 | @Nullable |
| 1366 | public LinkProperties getLinkProperties(@Nullable Network network) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1367 | try { |
| 1368 | return mService.getLinkProperties(network); |
| 1369 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1370 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1371 | } |
| 1372 | } |
| 1373 | |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1374 | /** |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 1375 | * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1376 | * will return {@code null} if the network is unknown. |
| 1377 | * |
| 1378 | * @param network The {@link Network} object identifying the network in question. |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 1379 | * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1380 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 1381 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 1382 | @Nullable |
| 1383 | public NetworkCapabilities getNetworkCapabilities(@Nullable Network network) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1384 | try { |
Qingxi Li | 9c5d8b9 | 2020-01-08 12:51:49 -0800 | [diff] [blame] | 1385 | return mService.getNetworkCapabilities(network, mContext.getOpPackageName()); |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1386 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1387 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 1388 | } |
| 1389 | } |
| 1390 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1391 | /** |
Remi NGUYEN VAN | 0bd294c | 2019-03-20 14:22:49 +0900 | [diff] [blame] | 1392 | * Gets a URL that can be used for resolving whether a captive portal is present. |
Udam Saini | b7c2487 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 1393 | * 1. This URL should respond with a 204 response to a GET request to indicate no captive |
| 1394 | * portal is present. |
| 1395 | * 2. This URL must be HTTP as redirect responses are used to find captive portal |
| 1396 | * sign-in pages. Captive portals cannot respond to HTTPS requests with redirects. |
| 1397 | * |
Remi NGUYEN VAN | 0bd294c | 2019-03-20 14:22:49 +0900 | [diff] [blame] | 1398 | * The system network validation may be using different strategies to detect captive portals, |
| 1399 | * so this method does not necessarily return a URL used by the system. It only returns a URL |
| 1400 | * that may be relevant for other components trying to detect captive portals. |
paulhu | a6af6b6 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1401 | * |
Udam Saini | b7c2487 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 1402 | * @hide |
paulhu | a6af6b6 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1403 | * @deprecated This API returns URL which is not guaranteed to be one of the URLs used by the |
| 1404 | * system. |
Udam Saini | b7c2487 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 1405 | */ |
paulhu | a6af6b6 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1406 | @Deprecated |
Udam Saini | b7c2487 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 1407 | @SystemApi |
paulhu | a6af6b6 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 1408 | @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS) |
Udam Saini | b7c2487 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 1409 | public String getCaptivePortalServerUrl() { |
| 1410 | try { |
| 1411 | return mService.getCaptivePortalServerUrl(); |
| 1412 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1413 | throw e.rethrowFromSystemServer(); |
Udam Saini | b7c2487 | 2016-01-04 12:16:14 -0800 | [diff] [blame] | 1414 | } |
| 1415 | } |
| 1416 | |
| 1417 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1418 | * Tells the underlying networking system that the caller wants to |
| 1419 | * begin using the named feature. The interpretation of {@code feature} |
| 1420 | * is completely up to each networking implementation. |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 1421 | * |
| 1422 | * <p>This method requires the caller to hold either the |
| 1423 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 1424 | * or the ability to modify system settings as determined by |
| 1425 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 1426 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1427 | * @param networkType specifies which network the request pertains to |
| 1428 | * @param feature the name of the feature to be used |
| 1429 | * @return an integer value representing the outcome of the request. |
| 1430 | * The interpretation of this value is specific to each networking |
| 1431 | * implementation+feature combination, except that the value {@code -1} |
| 1432 | * always indicates failure. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1433 | * |
Lorenzo Colitti | 2ea89e5 | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 1434 | * @deprecated Deprecated in favor of the cleaner |
| 1435 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API. |
Dianne Hackborn | 692a244 | 2015-07-31 10:35:34 -0700 | [diff] [blame] | 1436 | * In {@link VERSION_CODES#M}, and above, this method is unsupported and will |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1437 | * throw {@code UnsupportedOperationException} if called. |
Lorenzo Colitti | 2187df7 | 2016-12-09 18:39:30 +0900 | [diff] [blame] | 1438 | * @removed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1439 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 1440 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1441 | public int startUsingNetworkFeature(int networkType, String feature) { |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1442 | checkLegacyRoutingApiAccess(); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1443 | NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature); |
| 1444 | if (netCap == null) { |
| 1445 | Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " + |
| 1446 | feature); |
Chalard Jean | c06d788 | 2019-11-21 14:48:00 +0900 | [diff] [blame] | 1447 | return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED; |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1448 | } |
| 1449 | |
| 1450 | NetworkRequest request = null; |
| 1451 | synchronized (sLegacyRequests) { |
| 1452 | LegacyRequest l = sLegacyRequests.get(netCap); |
| 1453 | if (l != null) { |
| 1454 | Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest); |
| 1455 | renewRequestLocked(l); |
| 1456 | if (l.currentNetwork != null) { |
Chalard Jean | c06d788 | 2019-11-21 14:48:00 +0900 | [diff] [blame] | 1457 | return DEPRECATED_PHONE_CONSTANT_APN_ALREADY_ACTIVE; |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1458 | } else { |
Chalard Jean | c06d788 | 2019-11-21 14:48:00 +0900 | [diff] [blame] | 1459 | return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED; |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1460 | } |
| 1461 | } |
| 1462 | |
| 1463 | request = requestNetworkForFeatureLocked(netCap); |
| 1464 | } |
| 1465 | if (request != null) { |
Robert Greenwalt | 257ee5f | 2014-06-20 10:58:45 -0700 | [diff] [blame] | 1466 | Log.d(TAG, "starting startUsingNetworkFeature for request " + request); |
Chalard Jean | c06d788 | 2019-11-21 14:48:00 +0900 | [diff] [blame] | 1467 | return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_STARTED; |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1468 | } else { |
| 1469 | Log.d(TAG, " request Failed"); |
Chalard Jean | c06d788 | 2019-11-21 14:48:00 +0900 | [diff] [blame] | 1470 | return DEPRECATED_PHONE_CONSTANT_APN_REQUEST_FAILED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1471 | } |
| 1472 | } |
| 1473 | |
| 1474 | /** |
| 1475 | * Tells the underlying networking system that the caller is finished |
| 1476 | * using the named feature. The interpretation of {@code feature} |
| 1477 | * is completely up to each networking implementation. |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 1478 | * |
| 1479 | * <p>This method requires the caller to hold either the |
| 1480 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 1481 | * or the ability to modify system settings as determined by |
| 1482 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 1483 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1484 | * @param networkType specifies which network the request pertains to |
| 1485 | * @param feature the name of the feature that is no longer needed |
| 1486 | * @return an integer value representing the outcome of the request. |
| 1487 | * The interpretation of this value is specific to each networking |
| 1488 | * implementation+feature combination, except that the value {@code -1} |
| 1489 | * always indicates failure. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1490 | * |
Lorenzo Colitti | 88bc0bb | 2016-04-13 22:00:02 +0900 | [diff] [blame] | 1491 | * @deprecated Deprecated in favor of the cleaner |
| 1492 | * {@link #unregisterNetworkCallback(NetworkCallback)} API. |
Dianne Hackborn | 692a244 | 2015-07-31 10:35:34 -0700 | [diff] [blame] | 1493 | * In {@link VERSION_CODES#M}, and above, this method is unsupported and will |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1494 | * throw {@code UnsupportedOperationException} if called. |
Lorenzo Colitti | 2187df7 | 2016-12-09 18:39:30 +0900 | [diff] [blame] | 1495 | * @removed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1496 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 1497 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1498 | public int stopUsingNetworkFeature(int networkType, String feature) { |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1499 | checkLegacyRoutingApiAccess(); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1500 | NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature); |
| 1501 | if (netCap == null) { |
| 1502 | Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " + |
| 1503 | feature); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1504 | return -1; |
| 1505 | } |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1506 | |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1507 | if (removeRequestForFeature(netCap)) { |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1508 | Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1509 | } |
| 1510 | return 1; |
| 1511 | } |
| 1512 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 1513 | @UnsupportedAppUsage |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1514 | private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) { |
| 1515 | if (networkType == TYPE_MOBILE) { |
Erik Kline | 35bf06c | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 1516 | switch (feature) { |
| 1517 | case "enableCBS": |
| 1518 | return networkCapabilitiesForType(TYPE_MOBILE_CBS); |
| 1519 | case "enableDUN": |
| 1520 | case "enableDUNAlways": |
| 1521 | return networkCapabilitiesForType(TYPE_MOBILE_DUN); |
| 1522 | case "enableFOTA": |
| 1523 | return networkCapabilitiesForType(TYPE_MOBILE_FOTA); |
| 1524 | case "enableHIPRI": |
| 1525 | return networkCapabilitiesForType(TYPE_MOBILE_HIPRI); |
| 1526 | case "enableIMS": |
| 1527 | return networkCapabilitiesForType(TYPE_MOBILE_IMS); |
| 1528 | case "enableMMS": |
| 1529 | return networkCapabilitiesForType(TYPE_MOBILE_MMS); |
| 1530 | case "enableSUPL": |
| 1531 | return networkCapabilitiesForType(TYPE_MOBILE_SUPL); |
| 1532 | default: |
| 1533 | return null; |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1534 | } |
Erik Kline | 35bf06c | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 1535 | } else if (networkType == TYPE_WIFI && "p2p".equals(feature)) { |
| 1536 | return networkCapabilitiesForType(TYPE_WIFI_P2P); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1537 | } |
| 1538 | return null; |
| 1539 | } |
| 1540 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 1541 | private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) { |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1542 | if (netCap == null) return TYPE_NONE; |
| 1543 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) { |
| 1544 | return TYPE_MOBILE_CBS; |
| 1545 | } |
| 1546 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) { |
| 1547 | return TYPE_MOBILE_IMS; |
| 1548 | } |
| 1549 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) { |
| 1550 | return TYPE_MOBILE_FOTA; |
| 1551 | } |
| 1552 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) { |
| 1553 | return TYPE_MOBILE_DUN; |
| 1554 | } |
| 1555 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) { |
| 1556 | return TYPE_MOBILE_SUPL; |
| 1557 | } |
| 1558 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) { |
| 1559 | return TYPE_MOBILE_MMS; |
| 1560 | } |
| 1561 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) { |
| 1562 | return TYPE_MOBILE_HIPRI; |
| 1563 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 1564 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) { |
| 1565 | return TYPE_WIFI_P2P; |
| 1566 | } |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1567 | return TYPE_NONE; |
| 1568 | } |
| 1569 | |
| 1570 | private static class LegacyRequest { |
| 1571 | NetworkCapabilities networkCapabilities; |
| 1572 | NetworkRequest networkRequest; |
| 1573 | int expireSequenceNumber; |
| 1574 | Network currentNetwork; |
| 1575 | int delay = -1; |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1576 | |
| 1577 | private void clearDnsBinding() { |
| 1578 | if (currentNetwork != null) { |
| 1579 | currentNetwork = null; |
| 1580 | setProcessDefaultNetworkForHostResolution(null); |
| 1581 | } |
| 1582 | } |
| 1583 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1584 | NetworkCallback networkCallback = new NetworkCallback() { |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1585 | @Override |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1586 | public void onAvailable(Network network) { |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1587 | currentNetwork = network; |
| 1588 | Log.d(TAG, "startUsingNetworkFeature got Network:" + network); |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 1589 | setProcessDefaultNetworkForHostResolution(network); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1590 | } |
| 1591 | @Override |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1592 | public void onLost(Network network) { |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1593 | if (network.equals(currentNetwork)) clearDnsBinding(); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1594 | Log.d(TAG, "startUsingNetworkFeature lost Network:" + network); |
| 1595 | } |
| 1596 | }; |
| 1597 | } |
| 1598 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 1599 | @UnsupportedAppUsage |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 1600 | private static final HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests = |
| 1601 | new HashMap<>(); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1602 | |
| 1603 | private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) { |
| 1604 | synchronized (sLegacyRequests) { |
| 1605 | LegacyRequest l = sLegacyRequests.get(netCap); |
| 1606 | if (l != null) return l.networkRequest; |
| 1607 | } |
| 1608 | return null; |
| 1609 | } |
| 1610 | |
| 1611 | private void renewRequestLocked(LegacyRequest l) { |
| 1612 | l.expireSequenceNumber++; |
| 1613 | Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber); |
| 1614 | sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay); |
| 1615 | } |
| 1616 | |
| 1617 | private void expireRequest(NetworkCapabilities netCap, int sequenceNum) { |
| 1618 | int ourSeqNum = -1; |
| 1619 | synchronized (sLegacyRequests) { |
| 1620 | LegacyRequest l = sLegacyRequests.get(netCap); |
| 1621 | if (l == null) return; |
| 1622 | ourSeqNum = l.expireSequenceNumber; |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1623 | if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1624 | } |
| 1625 | Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum); |
| 1626 | } |
| 1627 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 1628 | @UnsupportedAppUsage |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1629 | private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) { |
| 1630 | int delay = -1; |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 1631 | int type = legacyTypeForNetworkCapabilities(netCap); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1632 | try { |
| 1633 | delay = mService.getRestoreDefaultNetworkDelay(type); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 1634 | } catch (RemoteException e) { |
| 1635 | throw e.rethrowFromSystemServer(); |
| 1636 | } |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1637 | LegacyRequest l = new LegacyRequest(); |
| 1638 | l.networkCapabilities = netCap; |
| 1639 | l.delay = delay; |
| 1640 | l.expireSequenceNumber = 0; |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 1641 | l.networkRequest = sendRequestForNetwork( |
| 1642 | netCap, l.networkCallback, 0, REQUEST, type, getDefaultHandler()); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1643 | if (l.networkRequest == null) return null; |
| 1644 | sLegacyRequests.put(netCap, l); |
| 1645 | sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay); |
| 1646 | return l.networkRequest; |
| 1647 | } |
| 1648 | |
| 1649 | private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) { |
| 1650 | if (delay >= 0) { |
| 1651 | Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay); |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 1652 | CallbackHandler handler = getDefaultHandler(); |
Hugo Benichi | 6f260f3 | 2017-02-03 14:18:44 +0900 | [diff] [blame] | 1653 | Message msg = handler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap); |
| 1654 | handler.sendMessageDelayed(msg, delay); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1655 | } |
| 1656 | } |
| 1657 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 1658 | @UnsupportedAppUsage |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1659 | private boolean removeRequestForFeature(NetworkCapabilities netCap) { |
| 1660 | final LegacyRequest l; |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1661 | synchronized (sLegacyRequests) { |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1662 | l = sLegacyRequests.remove(netCap); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1663 | } |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1664 | if (l == null) return false; |
| 1665 | unregisterNetworkCallback(l.networkCallback); |
| 1666 | l.clearDnsBinding(); |
| 1667 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1668 | } |
| 1669 | |
Erik Kline | 35bf06c | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 1670 | private static final SparseIntArray sLegacyTypeToTransport = new SparseIntArray(); |
| 1671 | static { |
| 1672 | sLegacyTypeToTransport.put(TYPE_MOBILE, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1673 | sLegacyTypeToTransport.put(TYPE_MOBILE_CBS, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1674 | sLegacyTypeToTransport.put(TYPE_MOBILE_DUN, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1675 | sLegacyTypeToTransport.put(TYPE_MOBILE_FOTA, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1676 | sLegacyTypeToTransport.put(TYPE_MOBILE_HIPRI, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1677 | sLegacyTypeToTransport.put(TYPE_MOBILE_IMS, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1678 | sLegacyTypeToTransport.put(TYPE_MOBILE_MMS, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1679 | sLegacyTypeToTransport.put(TYPE_MOBILE_SUPL, NetworkCapabilities.TRANSPORT_CELLULAR); |
| 1680 | sLegacyTypeToTransport.put(TYPE_WIFI, NetworkCapabilities.TRANSPORT_WIFI); |
| 1681 | sLegacyTypeToTransport.put(TYPE_WIFI_P2P, NetworkCapabilities.TRANSPORT_WIFI); |
| 1682 | sLegacyTypeToTransport.put(TYPE_BLUETOOTH, NetworkCapabilities.TRANSPORT_BLUETOOTH); |
| 1683 | sLegacyTypeToTransport.put(TYPE_ETHERNET, NetworkCapabilities.TRANSPORT_ETHERNET); |
| 1684 | } |
| 1685 | |
| 1686 | private static final SparseIntArray sLegacyTypeToCapability = new SparseIntArray(); |
| 1687 | static { |
| 1688 | sLegacyTypeToCapability.put(TYPE_MOBILE_CBS, NetworkCapabilities.NET_CAPABILITY_CBS); |
| 1689 | sLegacyTypeToCapability.put(TYPE_MOBILE_DUN, NetworkCapabilities.NET_CAPABILITY_DUN); |
| 1690 | sLegacyTypeToCapability.put(TYPE_MOBILE_FOTA, NetworkCapabilities.NET_CAPABILITY_FOTA); |
| 1691 | sLegacyTypeToCapability.put(TYPE_MOBILE_IMS, NetworkCapabilities.NET_CAPABILITY_IMS); |
| 1692 | sLegacyTypeToCapability.put(TYPE_MOBILE_MMS, NetworkCapabilities.NET_CAPABILITY_MMS); |
| 1693 | sLegacyTypeToCapability.put(TYPE_MOBILE_SUPL, NetworkCapabilities.NET_CAPABILITY_SUPL); |
| 1694 | sLegacyTypeToCapability.put(TYPE_WIFI_P2P, NetworkCapabilities.NET_CAPABILITY_WIFI_P2P); |
| 1695 | } |
| 1696 | |
| 1697 | /** |
| 1698 | * Given a legacy type (TYPE_WIFI, ...) returns a NetworkCapabilities |
| 1699 | * instance suitable for registering a request or callback. Throws an |
| 1700 | * IllegalArgumentException if no mapping from the legacy type to |
| 1701 | * NetworkCapabilities is known. |
| 1702 | * |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 1703 | * @deprecated Types are deprecated. Use {@link NetworkCallback} or {@link NetworkRequest} |
| 1704 | * to find the network instead. |
Erik Kline | 35bf06c | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 1705 | * @hide |
| 1706 | */ |
| 1707 | public static NetworkCapabilities networkCapabilitiesForType(int type) { |
| 1708 | final NetworkCapabilities nc = new NetworkCapabilities(); |
| 1709 | |
| 1710 | // Map from type to transports. |
| 1711 | final int NOT_FOUND = -1; |
| 1712 | final int transport = sLegacyTypeToTransport.get(type, NOT_FOUND); |
Hugo Benichi | e767851 | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 1713 | Preconditions.checkArgument(transport != NOT_FOUND, "unknown legacy type: " + type); |
Erik Kline | 35bf06c | 2017-01-26 18:08:28 +0900 | [diff] [blame] | 1714 | nc.addTransportType(transport); |
| 1715 | |
| 1716 | // Map from type to capabilities. |
| 1717 | nc.addCapability(sLegacyTypeToCapability.get( |
| 1718 | type, NetworkCapabilities.NET_CAPABILITY_INTERNET)); |
| 1719 | nc.maybeMarkCapabilitiesRestricted(); |
| 1720 | return nc; |
| 1721 | } |
| 1722 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1723 | /** @hide */ |
| 1724 | public static class PacketKeepaliveCallback { |
Artur Satayev | 751e551 | 2019-11-15 19:12:49 +0000 | [diff] [blame] | 1725 | @UnsupportedAppUsage |
| 1726 | public PacketKeepaliveCallback() { |
| 1727 | } |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1728 | /** The requested keepalive was successfully started. */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 1729 | @UnsupportedAppUsage |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1730 | public void onStarted() {} |
| 1731 | /** The keepalive was successfully stopped. */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 1732 | @UnsupportedAppUsage |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1733 | public void onStopped() {} |
| 1734 | /** An error occurred. */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 1735 | @UnsupportedAppUsage |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1736 | public void onError(int error) {} |
| 1737 | } |
| 1738 | |
| 1739 | /** |
| 1740 | * Allows applications to request that the system periodically send specific packets on their |
| 1741 | * behalf, using hardware offload to save battery power. |
| 1742 | * |
| 1743 | * To request that the system send keepalives, call one of the methods that return a |
| 1744 | * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive}, |
| 1745 | * passing in a non-null callback. If the callback is successfully started, the callback's |
| 1746 | * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called, |
| 1747 | * specifying one of the {@code ERROR_*} constants in this class. |
| 1748 | * |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 1749 | * To stop an existing keepalive, call {@link PacketKeepalive#stop}. The system will call |
| 1750 | * {@link PacketKeepaliveCallback#onStopped} if the operation was successful or |
| 1751 | * {@link PacketKeepaliveCallback#onError} if an error occurred. |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1752 | * |
junyulai | 48eac1d4 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 1753 | * @deprecated Use {@link SocketKeepalive} instead. |
| 1754 | * |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1755 | * @hide |
| 1756 | */ |
| 1757 | public class PacketKeepalive { |
| 1758 | |
| 1759 | private static final String TAG = "PacketKeepalive"; |
| 1760 | |
| 1761 | /** @hide */ |
| 1762 | public static final int SUCCESS = 0; |
| 1763 | |
| 1764 | /** @hide */ |
| 1765 | public static final int NO_KEEPALIVE = -1; |
| 1766 | |
| 1767 | /** @hide */ |
| 1768 | public static final int BINDER_DIED = -10; |
| 1769 | |
| 1770 | /** The specified {@code Network} is not connected. */ |
| 1771 | public static final int ERROR_INVALID_NETWORK = -20; |
| 1772 | /** The specified IP addresses are invalid. For example, the specified source IP address is |
| 1773 | * not configured on the specified {@code Network}. */ |
| 1774 | public static final int ERROR_INVALID_IP_ADDRESS = -21; |
| 1775 | /** The requested port is invalid. */ |
| 1776 | public static final int ERROR_INVALID_PORT = -22; |
| 1777 | /** The packet length is invalid (e.g., too long). */ |
| 1778 | public static final int ERROR_INVALID_LENGTH = -23; |
| 1779 | /** The packet transmission interval is invalid (e.g., too short). */ |
| 1780 | public static final int ERROR_INVALID_INTERVAL = -24; |
| 1781 | |
| 1782 | /** The hardware does not support this request. */ |
| 1783 | public static final int ERROR_HARDWARE_UNSUPPORTED = -30; |
Lorenzo Colitti | 9d1284e | 2015-09-08 16:46:36 +0900 | [diff] [blame] | 1784 | /** The hardware returned an error. */ |
| 1785 | public static final int ERROR_HARDWARE_ERROR = -31; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1786 | |
Nathan Harold | 63dd813 | 2018-02-14 13:09:45 -0800 | [diff] [blame] | 1787 | /** The NAT-T destination port for IPsec */ |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1788 | public static final int NATT_PORT = 4500; |
| 1789 | |
Nathan Harold | 63dd813 | 2018-02-14 13:09:45 -0800 | [diff] [blame] | 1790 | /** The minimum interval in seconds between keepalive packet transmissions */ |
| 1791 | public static final int MIN_INTERVAL = 10; |
| 1792 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1793 | private final Network mNetwork; |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 1794 | private final ISocketKeepaliveCallback mCallback; |
| 1795 | private final ExecutorService mExecutor; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1796 | |
| 1797 | private volatile Integer mSlot; |
| 1798 | |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 1799 | @UnsupportedAppUsage |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1800 | public void stop() { |
| 1801 | try { |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 1802 | mExecutor.execute(() -> { |
| 1803 | try { |
| 1804 | if (mSlot != null) { |
| 1805 | mService.stopKeepalive(mNetwork, mSlot); |
| 1806 | } |
| 1807 | } catch (RemoteException e) { |
| 1808 | Log.e(TAG, "Error stopping packet keepalive: ", e); |
| 1809 | throw e.rethrowFromSystemServer(); |
| 1810 | } |
| 1811 | }); |
| 1812 | } catch (RejectedExecutionException e) { |
| 1813 | // The internal executor has already stopped due to previous event. |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1814 | } |
| 1815 | } |
| 1816 | |
| 1817 | private PacketKeepalive(Network network, PacketKeepaliveCallback callback) { |
Hugo Benichi | dafed3d | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 1818 | Preconditions.checkNotNull(network, "network cannot be null"); |
| 1819 | Preconditions.checkNotNull(callback, "callback cannot be null"); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1820 | mNetwork = network; |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 1821 | mExecutor = Executors.newSingleThreadExecutor(); |
| 1822 | mCallback = new ISocketKeepaliveCallback.Stub() { |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1823 | @Override |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 1824 | public void onStarted(int slot) { |
| 1825 | Binder.withCleanCallingIdentity(() -> |
| 1826 | mExecutor.execute(() -> { |
| 1827 | mSlot = slot; |
| 1828 | callback.onStarted(); |
| 1829 | })); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1830 | } |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 1831 | |
| 1832 | @Override |
| 1833 | public void onStopped() { |
| 1834 | Binder.withCleanCallingIdentity(() -> |
| 1835 | mExecutor.execute(() -> { |
| 1836 | mSlot = null; |
| 1837 | callback.onStopped(); |
| 1838 | })); |
| 1839 | mExecutor.shutdown(); |
| 1840 | } |
| 1841 | |
| 1842 | @Override |
| 1843 | public void onError(int error) { |
| 1844 | Binder.withCleanCallingIdentity(() -> |
| 1845 | mExecutor.execute(() -> { |
| 1846 | mSlot = null; |
| 1847 | callback.onError(error); |
| 1848 | })); |
| 1849 | mExecutor.shutdown(); |
| 1850 | } |
| 1851 | |
| 1852 | @Override |
| 1853 | public void onDataReceived() { |
| 1854 | // PacketKeepalive is only used for Nat-T keepalive and as such does not invoke |
| 1855 | // this callback when data is received. |
| 1856 | } |
| 1857 | }; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1858 | } |
| 1859 | } |
| 1860 | |
| 1861 | /** |
| 1862 | * Starts an IPsec NAT-T keepalive packet with the specified parameters. |
| 1863 | * |
junyulai | 48eac1d4 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 1864 | * @deprecated Use {@link #createSocketKeepalive} instead. |
| 1865 | * |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1866 | * @hide |
| 1867 | */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 1868 | @UnsupportedAppUsage |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1869 | public PacketKeepalive startNattKeepalive( |
| 1870 | Network network, int intervalSeconds, PacketKeepaliveCallback callback, |
| 1871 | InetAddress srcAddr, int srcPort, InetAddress dstAddr) { |
| 1872 | final PacketKeepalive k = new PacketKeepalive(network, callback); |
| 1873 | try { |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 1874 | mService.startNattKeepalive(network, intervalSeconds, k.mCallback, |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1875 | srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress()); |
| 1876 | } catch (RemoteException e) { |
| 1877 | Log.e(TAG, "Error starting packet keepalive: ", e); |
junyulai | 7c46917 | 2019-01-16 20:23:34 +0800 | [diff] [blame] | 1878 | throw e.rethrowFromSystemServer(); |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1879 | } |
| 1880 | return k; |
| 1881 | } |
| 1882 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1883 | /** |
junyulai | 48eac1d4 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 1884 | * Request that keepalives be started on a IPsec NAT-T socket. |
| 1885 | * |
| 1886 | * @param network The {@link Network} the socket is on. |
| 1887 | * @param socket The socket that needs to be kept alive. |
| 1888 | * @param source The source address of the {@link UdpEncapsulationSocket}. |
| 1889 | * @param destination The destination address of the {@link UdpEncapsulationSocket}. |
| 1890 | * @param executor The executor on which callback will be invoked. The provided {@link Executor} |
| 1891 | * must run callback sequentially, otherwise the order of callbacks cannot be |
| 1892 | * guaranteed. |
| 1893 | * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive |
| 1894 | * changes. Must be extended by applications that use this API. |
| 1895 | * |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 1896 | * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the |
| 1897 | * given socket. |
junyulai | 48eac1d4 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 1898 | **/ |
junyulai | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 1899 | public @NonNull SocketKeepalive createSocketKeepalive(@NonNull Network network, |
junyulai | 48eac1d4 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 1900 | @NonNull UdpEncapsulationSocket socket, |
| 1901 | @NonNull InetAddress source, |
| 1902 | @NonNull InetAddress destination, |
| 1903 | @NonNull @CallbackExecutor Executor executor, |
| 1904 | @NonNull Callback callback) { |
junyulai | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 1905 | ParcelFileDescriptor dup; |
| 1906 | try { |
junyulai | 1517815 | 2019-03-27 11:00:37 +0800 | [diff] [blame] | 1907 | // Dup is needed here as the pfd inside the socket is owned by the IpSecService, |
| 1908 | // which cannot be obtained by the app process. |
junyulai | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 1909 | dup = ParcelFileDescriptor.dup(socket.getFileDescriptor()); |
| 1910 | } catch (IOException ignored) { |
| 1911 | // Construct an invalid fd, so that if the user later calls start(), it will fail with |
| 1912 | // ERROR_INVALID_SOCKET. |
| 1913 | dup = new ParcelFileDescriptor(new FileDescriptor()); |
| 1914 | } |
| 1915 | return new NattSocketKeepalive(mService, network, dup, socket.getResourceId(), source, |
| 1916 | destination, executor, callback); |
junyulai | 215b877 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 1917 | } |
| 1918 | |
| 1919 | /** |
| 1920 | * Request that keepalives be started on a IPsec NAT-T socket file descriptor. Directly called |
| 1921 | * by system apps which don't use IpSecService to create {@link UdpEncapsulationSocket}. |
| 1922 | * |
| 1923 | * @param network The {@link Network} the socket is on. |
junyulai | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 1924 | * @param pfd The {@link ParcelFileDescriptor} that needs to be kept alive. The provided |
| 1925 | * {@link ParcelFileDescriptor} must be bound to a port and the keepalives will be sent |
| 1926 | * from that port. |
junyulai | 215b877 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 1927 | * @param source The source address of the {@link UdpEncapsulationSocket}. |
| 1928 | * @param destination The destination address of the {@link UdpEncapsulationSocket}. The |
| 1929 | * keepalive packets will always be sent to port 4500 of the given {@code destination}. |
| 1930 | * @param executor The executor on which callback will be invoked. The provided {@link Executor} |
| 1931 | * must run callback sequentially, otherwise the order of callbacks cannot be |
| 1932 | * guaranteed. |
| 1933 | * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive |
| 1934 | * changes. Must be extended by applications that use this API. |
| 1935 | * |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 1936 | * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the |
| 1937 | * given socket. |
junyulai | 215b877 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 1938 | * @hide |
| 1939 | */ |
| 1940 | @SystemApi |
| 1941 | @RequiresPermission(android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD) |
junyulai | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 1942 | public @NonNull SocketKeepalive createNattKeepalive(@NonNull Network network, |
| 1943 | @NonNull ParcelFileDescriptor pfd, |
junyulai | 215b877 | 2019-01-15 11:32:44 +0800 | [diff] [blame] | 1944 | @NonNull InetAddress source, |
| 1945 | @NonNull InetAddress destination, |
| 1946 | @NonNull @CallbackExecutor Executor executor, |
| 1947 | @NonNull Callback callback) { |
junyulai | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 1948 | ParcelFileDescriptor dup; |
| 1949 | try { |
junyulai | 1517815 | 2019-03-27 11:00:37 +0800 | [diff] [blame] | 1950 | // TODO: Consider remove unnecessary dup. |
junyulai | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 1951 | dup = pfd.dup(); |
| 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. |
| 1955 | dup = new ParcelFileDescriptor(new FileDescriptor()); |
| 1956 | } |
| 1957 | return new NattSocketKeepalive(mService, network, dup, |
| 1958 | INVALID_RESOURCE_ID /* Unused */, source, destination, executor, callback); |
junyulai | 48eac1d4 | 2018-12-27 17:25:29 +0800 | [diff] [blame] | 1959 | } |
| 1960 | |
| 1961 | /** |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 1962 | * Request that keepalives be started on a TCP socket. |
| 1963 | * The socket must be established. |
| 1964 | * |
| 1965 | * @param network The {@link Network} the socket is on. |
| 1966 | * @param socket The socket that needs to be kept alive. |
| 1967 | * @param executor The executor on which callback will be invoked. This implementation assumes |
| 1968 | * the provided {@link Executor} runs the callbacks in sequence with no |
| 1969 | * concurrency. Failing this, no guarantee of correctness can be made. It is |
| 1970 | * the responsibility of the caller to ensure the executor provides this |
| 1971 | * guarantee. A simple way of creating such an executor is with the standard |
| 1972 | * tool {@code Executors.newSingleThreadExecutor}. |
| 1973 | * @param callback A {@link SocketKeepalive.Callback}. Used for notifications about keepalive |
| 1974 | * changes. Must be extended by applications that use this API. |
| 1975 | * |
| 1976 | * @return A {@link SocketKeepalive} object that can be used to control the keepalive on the |
| 1977 | * given socket. |
| 1978 | * @hide |
| 1979 | */ |
| 1980 | @SystemApi |
| 1981 | @RequiresPermission(android.Manifest.permission.PACKET_KEEPALIVE_OFFLOAD) |
junyulai | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 1982 | public @NonNull SocketKeepalive createSocketKeepalive(@NonNull Network network, |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 1983 | @NonNull Socket socket, |
| 1984 | @NonNull Executor executor, |
| 1985 | @NonNull Callback callback) { |
junyulai | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 1986 | ParcelFileDescriptor dup; |
| 1987 | try { |
| 1988 | dup = ParcelFileDescriptor.fromSocket(socket); |
| 1989 | } catch (UncheckedIOException ignored) { |
| 1990 | // Construct an invalid fd, so that if the user later calls start(), it will fail with |
| 1991 | // ERROR_INVALID_SOCKET. |
| 1992 | dup = new ParcelFileDescriptor(new FileDescriptor()); |
| 1993 | } |
| 1994 | return new TcpSocketKeepalive(mService, network, dup, executor, callback); |
junyulai | 352dc2f | 2019-01-08 20:04:33 +0800 | [diff] [blame] | 1995 | } |
| 1996 | |
| 1997 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1998 | * Ensure that a network route exists to deliver traffic to the specified |
| 1999 | * host via the specified network interface. An attempt to add a route that |
| 2000 | * already exists is ignored, but treated as successful. |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 2001 | * |
| 2002 | * <p>This method requires the caller to hold either the |
| 2003 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2004 | * or the ability to modify system settings as determined by |
| 2005 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 2006 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2007 | * @param networkType the type of the network over which traffic to the specified |
| 2008 | * host is to be routed |
| 2009 | * @param hostAddress the IP address of the host to which the route is desired |
| 2010 | * @return {@code true} on success, {@code false} on failure |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2011 | * |
Lorenzo Colitti | 2ea89e5 | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 2012 | * @deprecated Deprecated in favor of the |
| 2013 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, |
| 2014 | * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API. |
Dianne Hackborn | 692a244 | 2015-07-31 10:35:34 -0700 | [diff] [blame] | 2015 | * In {@link VERSION_CODES#M}, and above, this method is unsupported and will |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 2016 | * throw {@code UnsupportedOperationException} if called. |
Lorenzo Colitti | 2187df7 | 2016-12-09 18:39:30 +0900 | [diff] [blame] | 2017 | * @removed |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2018 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 2019 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2020 | public boolean requestRouteToHost(int networkType, int hostAddress) { |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 2021 | return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress)); |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 2022 | } |
| 2023 | |
| 2024 | /** |
| 2025 | * Ensure that a network route exists to deliver traffic to the specified |
| 2026 | * host via the specified network interface. An attempt to add a route that |
| 2027 | * already exists is ignored, but treated as successful. |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 2028 | * |
| 2029 | * <p>This method requires the caller to hold either the |
| 2030 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2031 | * or the ability to modify system settings as determined by |
| 2032 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 2033 | * |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 2034 | * @param networkType the type of the network over which traffic to the specified |
| 2035 | * host is to be routed |
| 2036 | * @param hostAddress the IP address of the host to which the route is desired |
| 2037 | * @return {@code true} on success, {@code false} on failure |
| 2038 | * @hide |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2039 | * @deprecated Deprecated in favor of the {@link #requestNetwork} and |
Lorenzo Colitti | 2ea89e5 | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 2040 | * {@link #bindProcessToNetwork} API. |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 2041 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 2042 | @Deprecated |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2043 | @UnsupportedAppUsage |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 2044 | public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) { |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 2045 | checkLegacyRoutingApiAccess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2046 | try { |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 2047 | return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2048 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2049 | throw e.rethrowFromSystemServer(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2050 | } |
| 2051 | } |
| 2052 | |
| 2053 | /** |
| 2054 | * Returns the value of the setting for background data usage. If false, |
| 2055 | * applications should not use the network if the application is not in the |
| 2056 | * foreground. Developers should respect this setting, and check the value |
| 2057 | * of this before performing any background data operations. |
| 2058 | * <p> |
| 2059 | * All applications that have background services that use the network |
| 2060 | * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}. |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2061 | * <p> |
Scott Main | 4cc5333 | 2011-10-06 18:32:43 -0700 | [diff] [blame] | 2062 | * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2063 | * background data depends on several combined factors, and this method will |
| 2064 | * always return {@code true}. Instead, when background data is unavailable, |
| 2065 | * {@link #getActiveNetworkInfo()} will now appear disconnected. |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2066 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2067 | * @return Whether background data usage is allowed. |
| 2068 | */ |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2069 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2070 | public boolean getBackgroundDataSetting() { |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2071 | // assume that background data is allowed; final authority is |
| 2072 | // NetworkInfo which may be blocked. |
| 2073 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2074 | } |
| 2075 | |
| 2076 | /** |
| 2077 | * Sets the value of the setting for background data usage. |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2078 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2079 | * @param allowBackgroundData Whether an application should use data while |
| 2080 | * it is in the background. |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2081 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2082 | * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING |
| 2083 | * @see #getBackgroundDataSetting() |
| 2084 | * @hide |
| 2085 | */ |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2086 | @Deprecated |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2087 | @UnsupportedAppUsage |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2088 | public void setBackgroundDataSetting(boolean allowBackgroundData) { |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 2089 | // ignored |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2090 | } |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2091 | |
Jeff Sharkey | 43d2a17 | 2017-07-12 10:50:42 -0600 | [diff] [blame] | 2092 | /** {@hide} */ |
| 2093 | @Deprecated |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2094 | @UnsupportedAppUsage |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 2095 | public NetworkQuotaInfo getActiveNetworkQuotaInfo() { |
| 2096 | try { |
| 2097 | return mService.getActiveNetworkQuotaInfo(); |
| 2098 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2099 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 2100 | } |
| 2101 | } |
| 2102 | |
| 2103 | /** |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2104 | * @hide |
Robert Greenwalt | afa05c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 2105 | * @deprecated Talk to TelephonyManager directly |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2106 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 2107 | @Deprecated |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2108 | @UnsupportedAppUsage |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2109 | public boolean getMobileDataEnabled() { |
Meng Wang | a73bed8 | 2019-11-18 17:10:00 -0800 | [diff] [blame] | 2110 | TelephonyManager tm = mContext.getSystemService(TelephonyManager.class); |
| 2111 | if (tm != null) { |
| 2112 | int subId = SubscriptionManager.getDefaultDataSubscriptionId(); |
| 2113 | Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId); |
| 2114 | boolean retVal = tm.createForSubscriptionId(subId).isDataEnabled(); |
| 2115 | Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId |
| 2116 | + " retVal=" + retVal); |
| 2117 | return retVal; |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2118 | } |
Wink Saville | 36ffb04 | 2014-12-05 11:10:30 -0800 | [diff] [blame] | 2119 | Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false"); |
Robert Greenwalt | afa05c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 2120 | return false; |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 2121 | } |
| 2122 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2123 | /** |
Robert Greenwalt | b248987 | 2014-09-04 16:44:35 -0700 | [diff] [blame] | 2124 | * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener} |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2125 | * to find out when the system default network has gone in to a high power state. |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2126 | */ |
| 2127 | public interface OnNetworkActiveListener { |
| 2128 | /** |
| 2129 | * Called on the main thread of the process to report that the current data network |
| 2130 | * has become active, and it is now a good time to perform any pending network |
| 2131 | * operations. Note that this listener only tells you when the network becomes |
| 2132 | * active; if at any other time you want to know whether it is active (and thus okay |
| 2133 | * to initiate network traffic), you can retrieve its instantaneous state with |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2134 | * {@link ConnectivityManager#isDefaultNetworkActive}. |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2135 | */ |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 2136 | void onNetworkActive(); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2137 | } |
| 2138 | |
| 2139 | private INetworkManagementService getNetworkManagementService() { |
| 2140 | synchronized (this) { |
| 2141 | if (mNMService != null) { |
| 2142 | return mNMService; |
| 2143 | } |
| 2144 | IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE); |
| 2145 | mNMService = INetworkManagementService.Stub.asInterface(b); |
| 2146 | return mNMService; |
| 2147 | } |
| 2148 | } |
| 2149 | |
| 2150 | private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener> |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 2151 | mNetworkActivityListeners = new ArrayMap<>(); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2152 | |
| 2153 | /** |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2154 | * Start listening to reports when the system's default data network is active, meaning it is |
| 2155 | * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()} |
| 2156 | * to determine the current state of the system's default network after registering the |
| 2157 | * listener. |
| 2158 | * <p> |
| 2159 | * If the process default network has been set with |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2160 | * {@link ConnectivityManager#bindProcessToNetwork} this function will not |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2161 | * reflect the process's default, but the system default. |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2162 | * |
| 2163 | * @param l The listener to be told when the network is active. |
| 2164 | */ |
Robert Greenwalt | b248987 | 2014-09-04 16:44:35 -0700 | [diff] [blame] | 2165 | public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2166 | INetworkActivityListener rl = new INetworkActivityListener.Stub() { |
| 2167 | @Override |
| 2168 | public void onNetworkActive() throws RemoteException { |
| 2169 | l.onNetworkActive(); |
| 2170 | } |
| 2171 | }; |
| 2172 | |
| 2173 | try { |
| 2174 | getNetworkManagementService().registerNetworkActivityListener(rl); |
| 2175 | mNetworkActivityListeners.put(l, rl); |
| 2176 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2177 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2178 | } |
| 2179 | } |
| 2180 | |
| 2181 | /** |
| 2182 | * Remove network active listener previously registered with |
Robert Greenwalt | b248987 | 2014-09-04 16:44:35 -0700 | [diff] [blame] | 2183 | * {@link #addDefaultNetworkActiveListener}. |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2184 | * |
| 2185 | * @param l Previously registered listener. |
| 2186 | */ |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 2187 | public void removeDefaultNetworkActiveListener(@NonNull OnNetworkActiveListener l) { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2188 | INetworkActivityListener rl = mNetworkActivityListeners.get(l); |
Hugo Benichi | e767851 | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 2189 | Preconditions.checkArgument(rl != null, "Listener was not registered."); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2190 | try { |
| 2191 | getNetworkManagementService().unregisterNetworkActivityListener(rl); |
| 2192 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2193 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2194 | } |
| 2195 | } |
| 2196 | |
| 2197 | /** |
| 2198 | * Return whether the data network is currently active. An active network means that |
| 2199 | * it is currently in a high power state for performing data transmission. On some |
| 2200 | * types of networks, it may be expensive to move and stay in such a state, so it is |
| 2201 | * more power efficient to batch network traffic together when the radio is already in |
| 2202 | * this state. This method tells you whether right now is currently a good time to |
| 2203 | * initiate network traffic, as the network is already active. |
| 2204 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2205 | public boolean isDefaultNetworkActive() { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2206 | try { |
| 2207 | return getNetworkManagementService().isNetworkActive(); |
| 2208 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2209 | throw e.rethrowFromSystemServer(); |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2210 | } |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2211 | } |
| 2212 | |
| 2213 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2214 | * {@hide} |
| 2215 | */ |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 2216 | public ConnectivityManager(Context context, IConnectivityManager service) { |
Hugo Benichi | dafed3d | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 2217 | mContext = Preconditions.checkNotNull(context, "missing context"); |
| 2218 | mService = Preconditions.checkNotNull(service, "missing IConnectivityManager"); |
Automerger Merge Worker | 6112c82 | 2020-03-06 00:38:43 +0000 | [diff] [blame] | 2219 | mTetheringManager = (TetheringManager) mContext.getSystemService(Context.TETHERING_SERVICE); |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 2220 | sInstance = this; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2221 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2222 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2223 | /** {@hide} */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2224 | @UnsupportedAppUsage |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 2225 | public static ConnectivityManager from(Context context) { |
| 2226 | return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 2227 | } |
| 2228 | |
Remi NGUYEN VAN | e67b0c3 | 2018-12-27 16:43:56 +0900 | [diff] [blame] | 2229 | /** @hide */ |
| 2230 | public NetworkRequest getDefaultRequest() { |
| 2231 | try { |
| 2232 | // This is not racy as the default request is final in ConnectivityService. |
| 2233 | return mService.getDefaultRequest(); |
| 2234 | } catch (RemoteException e) { |
| 2235 | throw e.rethrowFromSystemServer(); |
| 2236 | } |
| 2237 | } |
| 2238 | |
Lorenzo Colitti | fbe9b1a | 2016-07-28 17:14:11 +0900 | [diff] [blame] | 2239 | /* TODO: These permissions checks don't belong in client-side code. Move them to |
| 2240 | * services.jar, possibly in com.android.server.net. */ |
| 2241 | |
| 2242 | /** {@hide} */ |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 2243 | public static final void enforceChangePermission(Context context) { |
| 2244 | int uid = Binder.getCallingUid(); |
| 2245 | Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings |
| 2246 | .getPackageNameForUid(context, uid), true /* throwException */); |
| 2247 | } |
| 2248 | |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 2249 | /** {@hide} */ |
| 2250 | public static final void enforceTetherChangePermission(Context context, String callingPkg) { |
Hugo Benichi | e767851 | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 2251 | Preconditions.checkNotNull(context, "Context cannot be null"); |
| 2252 | Preconditions.checkNotNull(callingPkg, "callingPkg cannot be null"); |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 2253 | |
Robert Greenwalt | edb4766 | 2014-09-16 17:54:19 -0700 | [diff] [blame] | 2254 | if (context.getResources().getStringArray( |
| 2255 | com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) { |
| 2256 | // Have a provisioning app - must only let system apps (which check this app) |
| 2257 | // turn on tethering |
| 2258 | context.enforceCallingOrSelfPermission( |
Jeremy Klein | d42209d | 2015-12-28 15:11:58 -0800 | [diff] [blame] | 2259 | android.Manifest.permission.TETHER_PRIVILEGED, "ConnectivityService"); |
Robert Greenwalt | edb4766 | 2014-09-16 17:54:19 -0700 | [diff] [blame] | 2260 | } else { |
Billy Lau | a7238a3 | 2015-08-01 12:45:02 +0100 | [diff] [blame] | 2261 | int uid = Binder.getCallingUid(); |
Tetsutoki Shiozawa | 335d2ed | 2016-03-16 23:30:57 +0900 | [diff] [blame] | 2262 | // If callingPkg's uid is not same as Binder.getCallingUid(), |
| 2263 | // AppOpsService throws SecurityException. |
| 2264 | Settings.checkAndNoteWriteSettingsOperation(context, uid, callingPkg, |
| 2265 | true /* throwException */); |
Robert Greenwalt | edb4766 | 2014-09-16 17:54:19 -0700 | [diff] [blame] | 2266 | } |
| 2267 | } |
| 2268 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2269 | /** |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 2270 | * @deprecated - use getSystemService. This is a kludge to support static access in certain |
| 2271 | * situations where a Context pointer is unavailable. |
| 2272 | * @hide |
| 2273 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 2274 | @Deprecated |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2275 | static ConnectivityManager getInstanceOrNull() { |
| 2276 | return sInstance; |
| 2277 | } |
| 2278 | |
| 2279 | /** |
| 2280 | * @deprecated - use getSystemService. This is a kludge to support static access in certain |
| 2281 | * situations where a Context pointer is unavailable. |
| 2282 | * @hide |
| 2283 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 2284 | @Deprecated |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2285 | @UnsupportedAppUsage |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2286 | private static ConnectivityManager getInstance() { |
| 2287 | if (getInstanceOrNull() == null) { |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 2288 | throw new IllegalStateException("No ConnectivityManager yet constructed"); |
| 2289 | } |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2290 | return getInstanceOrNull(); |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 2291 | } |
| 2292 | |
| 2293 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2294 | * Get the set of tetherable, available interfaces. This list is limited by |
| 2295 | * device configuration and current interface existence. |
| 2296 | * |
| 2297 | * @return an array of 0 or more Strings of tetherable interface names. |
| 2298 | * |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2299 | * @deprecated Use {@link TetheringEventCallback#onTetherableInterfacesChanged(List)} instead. |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2300 | * {@hide} |
| 2301 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 2302 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2303 | @UnsupportedAppUsage |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2304 | @Deprecated |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2305 | public String[] getTetherableIfaces() { |
Automerger Merge Worker | 6112c82 | 2020-03-06 00:38:43 +0000 | [diff] [blame] | 2306 | return mTetheringManager.getTetherableIfaces(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2307 | } |
| 2308 | |
| 2309 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2310 | * Get the set of tethered interfaces. |
| 2311 | * |
| 2312 | * @return an array of 0 or more String of currently tethered interface names. |
| 2313 | * |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2314 | * @deprecated Use {@link TetheringEventCallback#onTetherableInterfacesChanged(List)} instead. |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2315 | * {@hide} |
| 2316 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 2317 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2318 | @UnsupportedAppUsage |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2319 | @Deprecated |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2320 | public String[] getTetheredIfaces() { |
Automerger Merge Worker | 6112c82 | 2020-03-06 00:38:43 +0000 | [diff] [blame] | 2321 | return mTetheringManager.getTetheredIfaces(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2322 | } |
| 2323 | |
| 2324 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2325 | * Get the set of interface names which attempted to tether but |
| 2326 | * failed. Re-attempting to tether may cause them to reset to the Tethered |
| 2327 | * state. Alternatively, causing the interface to be destroyed and recreated |
| 2328 | * may cause them to reset to the available state. |
| 2329 | * {@link ConnectivityManager#getLastTetherError} can be used to get more |
| 2330 | * information on the cause of the errors. |
| 2331 | * |
| 2332 | * @return an array of 0 or more String indicating the interface names |
| 2333 | * which failed to tether. |
| 2334 | * |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2335 | * @deprecated Use {@link TetheringEventCallback#onError(String, int)} instead. |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2336 | * {@hide} |
| 2337 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 2338 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2339 | @UnsupportedAppUsage |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2340 | @Deprecated |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2341 | public String[] getTetheringErroredIfaces() { |
Automerger Merge Worker | 6112c82 | 2020-03-06 00:38:43 +0000 | [diff] [blame] | 2342 | return mTetheringManager.getTetheringErroredIfaces(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2343 | } |
| 2344 | |
| 2345 | /** |
Robert Greenwalt | 9c7e2c2 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 2346 | * Get the set of tethered dhcp ranges. |
| 2347 | * |
markchien | 2c15370 | 2020-02-06 19:23:26 +0800 | [diff] [blame] | 2348 | * @deprecated This method is not supported. |
| 2349 | * TODO: remove this function when all of clients are removed. |
Robert Greenwalt | 9c7e2c2 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 2350 | * {@hide} |
| 2351 | */ |
paulhu | a6af6b6 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 2352 | @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS) |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2353 | @Deprecated |
Robert Greenwalt | 9c7e2c2 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 2354 | public String[] getTetheredDhcpRanges() { |
markchien | 2c15370 | 2020-02-06 19:23:26 +0800 | [diff] [blame] | 2355 | throw new UnsupportedOperationException("getTetheredDhcpRanges is not supported"); |
Robert Greenwalt | 9c7e2c2 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 2356 | } |
| 2357 | |
| 2358 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2359 | * Attempt to tether the named interface. This will setup a dhcp server |
| 2360 | * on the interface, forward and NAT IP packets and forward DNS requests |
| 2361 | * to the best active upstream network interface. Note that if no upstream |
| 2362 | * IP network interface is available, dhcp will still run and traffic will be |
| 2363 | * allowed between the tethered devices and this device, though upstream net |
| 2364 | * access will of course fail until an upstream network interface becomes |
| 2365 | * active. |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 2366 | * |
| 2367 | * <p>This method requires the caller to hold either the |
| 2368 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2369 | * or the ability to modify system settings as determined by |
| 2370 | * {@link android.provider.Settings.System#canWrite}.</p> |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2371 | * |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2372 | * <p>WARNING: New clients should not use this function. The only usages should be in PanService |
| 2373 | * and WifiStateMachine which need direct access. All other clients should use |
| 2374 | * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning |
| 2375 | * logic.</p> |
| 2376 | * |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2377 | * @param iface the interface name to tether. |
| 2378 | * @return error a {@code TETHER_ERROR} value indicating success or failure type |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2379 | * @deprecated Use {@link TetheringManager#startTethering} instead |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2380 | * |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2381 | * {@hide} |
| 2382 | */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2383 | @UnsupportedAppUsage |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2384 | @Deprecated |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2385 | public int tether(String iface) { |
Automerger Merge Worker | 6112c82 | 2020-03-06 00:38:43 +0000 | [diff] [blame] | 2386 | return mTetheringManager.tether(iface); |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2387 | } |
| 2388 | |
| 2389 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2390 | * Stop tethering the named interface. |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 2391 | * |
| 2392 | * <p>This method requires the caller to hold either the |
| 2393 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2394 | * or the ability to modify system settings as determined by |
| 2395 | * {@link android.provider.Settings.System#canWrite}.</p> |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2396 | * |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2397 | * <p>WARNING: New clients should not use this function. The only usages should be in PanService |
| 2398 | * and WifiStateMachine which need direct access. All other clients should use |
| 2399 | * {@link #startTethering} and {@link #stopTethering} which encapsulate proper provisioning |
| 2400 | * logic.</p> |
| 2401 | * |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2402 | * @param iface the interface name to untether. |
| 2403 | * @return error a {@code TETHER_ERROR} value indicating success or failure type |
| 2404 | * |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2405 | * {@hide} |
| 2406 | */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2407 | @UnsupportedAppUsage |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2408 | @Deprecated |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2409 | public int untether(String iface) { |
Automerger Merge Worker | 6112c82 | 2020-03-06 00:38:43 +0000 | [diff] [blame] | 2410 | return mTetheringManager.untether(iface); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 2411 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2412 | |
| 2413 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2414 | * Check if the device allows for tethering. It may be disabled via |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 2415 | * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2416 | * due to device configuration. |
| 2417 | * |
Chalard Jean | 8f76fc3 | 2017-09-26 15:45:18 +0900 | [diff] [blame] | 2418 | * <p>If this app does not have permission to use this API, it will always |
| 2419 | * return false rather than throw an exception.</p> |
| 2420 | * |
| 2421 | * <p>If the device has a hotspot provisioning app, the caller is required to hold the |
| 2422 | * {@link android.Manifest.permission.TETHER_PRIVILEGED} permission.</p> |
| 2423 | * |
| 2424 | * <p>Otherwise, this method requires the caller to hold the ability to modify system |
| 2425 | * settings as determined by {@link android.provider.Settings.System#canWrite}.</p> |
| 2426 | * |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2427 | * @return a boolean - {@code true} indicating Tethering is supported. |
| 2428 | * |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2429 | * @deprecated Use {@link TetheringEventCallback#onTetheringSupported(boolean)} instead. |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2430 | * {@hide} |
| 2431 | */ |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2432 | @SystemApi |
Chalard Jean | 8f76fc3 | 2017-09-26 15:45:18 +0900 | [diff] [blame] | 2433 | @RequiresPermission(anyOf = {android.Manifest.permission.TETHER_PRIVILEGED, |
| 2434 | android.Manifest.permission.WRITE_SETTINGS}) |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2435 | public boolean isTetheringSupported() { |
Automerger Merge Worker | 6112c82 | 2020-03-06 00:38:43 +0000 | [diff] [blame] | 2436 | return mTetheringManager.isTetheringSupported(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2437 | } |
| 2438 | |
| 2439 | /** |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2440 | * Callback for use with {@link #startTethering} to find out whether tethering succeeded. |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2441 | * |
| 2442 | * @deprecated Use {@link TetheringManager.StartTetheringCallback} instead. |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2443 | * @hide |
| 2444 | */ |
| 2445 | @SystemApi |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2446 | @Deprecated |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2447 | public static abstract class OnStartTetheringCallback { |
| 2448 | /** |
| 2449 | * Called when tethering has been successfully started. |
| 2450 | */ |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 2451 | public void onTetheringStarted() {} |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2452 | |
| 2453 | /** |
| 2454 | * Called when starting tethering failed. |
| 2455 | */ |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 2456 | public void onTetheringFailed() {} |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2457 | } |
| 2458 | |
| 2459 | /** |
| 2460 | * Convenient overload for |
| 2461 | * {@link #startTethering(int, boolean, OnStartTetheringCallback, Handler)} which passes a null |
| 2462 | * handler to run on the current thread's {@link Looper}. |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2463 | * |
| 2464 | * @deprecated Use {@link TetheringManager#startTethering} instead. |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2465 | * @hide |
| 2466 | */ |
| 2467 | @SystemApi |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2468 | @Deprecated |
Udam Saini | 0e94c36 | 2017-06-07 12:06:28 -0700 | [diff] [blame] | 2469 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2470 | public void startTethering(int type, boolean showProvisioningUi, |
| 2471 | final OnStartTetheringCallback callback) { |
| 2472 | startTethering(type, showProvisioningUi, callback, null); |
| 2473 | } |
| 2474 | |
| 2475 | /** |
| 2476 | * Runs tether provisioning for the given type if needed and then starts tethering if |
| 2477 | * the check succeeds. If no carrier provisioning is required for tethering, tethering is |
| 2478 | * enabled immediately. If provisioning fails, tethering will not be enabled. It also |
| 2479 | * schedules tether provisioning re-checks if appropriate. |
| 2480 | * |
| 2481 | * @param type The type of tethering to start. Must be one of |
| 2482 | * {@link ConnectivityManager.TETHERING_WIFI}, |
| 2483 | * {@link ConnectivityManager.TETHERING_USB}, or |
| 2484 | * {@link ConnectivityManager.TETHERING_BLUETOOTH}. |
| 2485 | * @param showProvisioningUi a boolean indicating to show the provisioning app UI if there |
| 2486 | * is one. This should be true the first time this function is called and also any time |
| 2487 | * the user can see this UI. It gives users information from their carrier about the |
| 2488 | * check failing and how they can sign up for tethering if possible. |
| 2489 | * @param callback an {@link OnStartTetheringCallback} which will be called to notify the caller |
| 2490 | * of the result of trying to tether. |
| 2491 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2492 | * |
| 2493 | * @deprecated Use {@link TetheringManager#startTethering} instead. |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2494 | * @hide |
| 2495 | */ |
| 2496 | @SystemApi |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2497 | @Deprecated |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 2498 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2499 | public void startTethering(int type, boolean showProvisioningUi, |
| 2500 | final OnStartTetheringCallback callback, Handler handler) { |
Hugo Benichi | dafed3d | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 2501 | Preconditions.checkNotNull(callback, "OnStartTetheringCallback cannot be null."); |
Jeremy Klein | 5f277e1 | 2016-03-12 16:29:54 -0800 | [diff] [blame] | 2502 | |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2503 | final Executor executor = new Executor() { |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2504 | @Override |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2505 | public void execute(Runnable command) { |
| 2506 | if (handler == null) { |
| 2507 | command.run(); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2508 | } else { |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2509 | handler.post(command); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2510 | } |
| 2511 | } |
| 2512 | }; |
Jeremy Klein | 5f277e1 | 2016-03-12 16:29:54 -0800 | [diff] [blame] | 2513 | |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2514 | final StartTetheringCallback tetheringCallback = new StartTetheringCallback() { |
| 2515 | @Override |
| 2516 | public void onTetheringStarted() { |
| 2517 | callback.onTetheringStarted(); |
| 2518 | } |
| 2519 | |
| 2520 | @Override |
markchien | 62a625d | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2521 | public void onTetheringFailed(final int error) { |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2522 | callback.onTetheringFailed(); |
| 2523 | } |
| 2524 | }; |
| 2525 | |
| 2526 | final TetheringRequest request = new TetheringRequest.Builder(type) |
markchien | 62a625d | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2527 | .setShouldShowEntitlementUi(showProvisioningUi).build(); |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2528 | |
Automerger Merge Worker | 6112c82 | 2020-03-06 00:38:43 +0000 | [diff] [blame] | 2529 | mTetheringManager.startTethering(request, executor, tetheringCallback); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2530 | } |
| 2531 | |
| 2532 | /** |
| 2533 | * Stops tethering for the given type. Also cancels any provisioning rechecks for that type if |
| 2534 | * applicable. |
| 2535 | * |
| 2536 | * @param type The type of tethering to stop. Must be one of |
| 2537 | * {@link ConnectivityManager.TETHERING_WIFI}, |
| 2538 | * {@link ConnectivityManager.TETHERING_USB}, or |
| 2539 | * {@link ConnectivityManager.TETHERING_BLUETOOTH}. |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2540 | * |
| 2541 | * @deprecated Use {@link TetheringManager#stopTethering} instead. |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2542 | * @hide |
| 2543 | */ |
| 2544 | @SystemApi |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2545 | @Deprecated |
Jeff Sharkey | d86b8fe | 2017-06-02 17:36:26 -0600 | [diff] [blame] | 2546 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2547 | public void stopTethering(int type) { |
Automerger Merge Worker | 6112c82 | 2020-03-06 00:38:43 +0000 | [diff] [blame] | 2548 | mTetheringManager.stopTethering(type); |
Jeremy Klein | 36c7aa0 | 2016-01-22 14:11:45 -0800 | [diff] [blame] | 2549 | } |
| 2550 | |
| 2551 | /** |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2552 | * Callback for use with {@link registerTetheringEventCallback} to find out tethering |
| 2553 | * upstream status. |
| 2554 | * |
Nathan Harold | edf6b3d | 2020-01-23 18:03:46 -0800 | [diff] [blame] | 2555 | * @deprecated Use {@link TetheringManager#OnTetheringEventCallback} instead. |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2556 | * @hide |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2557 | */ |
| 2558 | @SystemApi |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2559 | @Deprecated |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2560 | public abstract static class OnTetheringEventCallback { |
| 2561 | |
| 2562 | /** |
| 2563 | * Called when tethering upstream changed. This can be called multiple times and can be |
| 2564 | * called any time. |
| 2565 | * |
| 2566 | * @param network the {@link Network} of tethering upstream. Null means tethering doesn't |
| 2567 | * have any upstream. |
| 2568 | */ |
| 2569 | public void onUpstreamChanged(@Nullable Network network) {} |
| 2570 | } |
| 2571 | |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2572 | @GuardedBy("mTetheringEventCallbacks") |
| 2573 | private final ArrayMap<OnTetheringEventCallback, TetheringEventCallback> |
| 2574 | mTetheringEventCallbacks = new ArrayMap<>(); |
| 2575 | |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2576 | /** |
| 2577 | * Start listening to tethering change events. Any new added callback will receive the last |
markchien | 9f246bd | 2019-04-03 10:43:09 +0800 | [diff] [blame] | 2578 | * tethering status right away. If callback is registered when tethering has no upstream or |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2579 | * disabled, {@link OnTetheringEventCallback#onUpstreamChanged} will immediately be called |
| 2580 | * with a null argument. The same callback object cannot be registered twice. |
| 2581 | * |
| 2582 | * @param executor the executor on which callback will be invoked. |
| 2583 | * @param callback the callback to be called when tethering has change events. |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2584 | * |
Nathan Harold | edf6b3d | 2020-01-23 18:03:46 -0800 | [diff] [blame] | 2585 | * @deprecated Use {@link TetheringManager#registerTetheringEventCallback} instead. |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2586 | * @hide |
| 2587 | */ |
| 2588 | @SystemApi |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2589 | @Deprecated |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2590 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
| 2591 | public void registerTetheringEventCallback( |
| 2592 | @NonNull @CallbackExecutor Executor executor, |
| 2593 | @NonNull final OnTetheringEventCallback callback) { |
| 2594 | Preconditions.checkNotNull(callback, "OnTetheringEventCallback cannot be null."); |
| 2595 | |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2596 | final TetheringEventCallback tetherCallback = |
| 2597 | new TetheringEventCallback() { |
| 2598 | @Override |
| 2599 | public void onUpstreamChanged(@Nullable Network network) { |
| 2600 | callback.onUpstreamChanged(network); |
| 2601 | } |
| 2602 | }; |
| 2603 | |
| 2604 | synchronized (mTetheringEventCallbacks) { |
| 2605 | mTetheringEventCallbacks.put(callback, tetherCallback); |
Automerger Merge Worker | 6112c82 | 2020-03-06 00:38:43 +0000 | [diff] [blame] | 2606 | mTetheringManager.registerTetheringEventCallback(executor, tetherCallback); |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2607 | } |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2608 | } |
| 2609 | |
| 2610 | /** |
| 2611 | * Remove tethering event callback previously registered with |
| 2612 | * {@link #registerTetheringEventCallback}. |
| 2613 | * |
| 2614 | * @param callback previously registered callback. |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2615 | * |
| 2616 | * @deprecated Use {@link TetheringManager#unregisterTetheringEventCallback} instead. |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2617 | * @hide |
| 2618 | */ |
| 2619 | @SystemApi |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2620 | @Deprecated |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2621 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
| 2622 | public void unregisterTetheringEventCallback( |
| 2623 | @NonNull final OnTetheringEventCallback callback) { |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2624 | Objects.requireNonNull(callback, "The callback must be non-null"); |
| 2625 | synchronized (mTetheringEventCallbacks) { |
| 2626 | final TetheringEventCallback tetherCallback = |
| 2627 | mTetheringEventCallbacks.remove(callback); |
Automerger Merge Worker | 6112c82 | 2020-03-06 00:38:43 +0000 | [diff] [blame] | 2628 | mTetheringManager.unregisterTetheringEventCallback(tetherCallback); |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2629 | } |
markchien | 26299ed | 2019-02-27 14:56:11 +0800 | [diff] [blame] | 2630 | } |
| 2631 | |
| 2632 | |
| 2633 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2634 | * Get the list of regular expressions that define any tetherable |
| 2635 | * USB network interfaces. If USB tethering is not supported by the |
| 2636 | * device, this list should be empty. |
| 2637 | * |
| 2638 | * @return an array of 0 or more regular expression Strings defining |
| 2639 | * what interfaces are considered tetherable usb interfaces. |
| 2640 | * |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2641 | * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged} instead. |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2642 | * {@hide} |
| 2643 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 2644 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2645 | @UnsupportedAppUsage |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2646 | @Deprecated |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2647 | public String[] getTetherableUsbRegexs() { |
Automerger Merge Worker | 6112c82 | 2020-03-06 00:38:43 +0000 | [diff] [blame] | 2648 | return mTetheringManager.getTetherableUsbRegexs(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2649 | } |
| 2650 | |
| 2651 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2652 | * Get the list of regular expressions that define any tetherable |
| 2653 | * Wifi network interfaces. If Wifi tethering is not supported by the |
| 2654 | * device, this list should be empty. |
| 2655 | * |
| 2656 | * @return an array of 0 or more regular expression Strings defining |
| 2657 | * what interfaces are considered tetherable wifi interfaces. |
| 2658 | * |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2659 | * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged} instead. |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2660 | * {@hide} |
| 2661 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 2662 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2663 | @UnsupportedAppUsage |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2664 | @Deprecated |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2665 | public String[] getTetherableWifiRegexs() { |
Automerger Merge Worker | 6112c82 | 2020-03-06 00:38:43 +0000 | [diff] [blame] | 2666 | return mTetheringManager.getTetherableWifiRegexs(); |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 2667 | } |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2668 | |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2669 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2670 | * Get the list of regular expressions that define any tetherable |
| 2671 | * Bluetooth network interfaces. If Bluetooth tethering is not supported by the |
| 2672 | * device, this list should be empty. |
| 2673 | * |
| 2674 | * @return an array of 0 or more regular expression Strings defining |
| 2675 | * what interfaces are considered tetherable bluetooth interfaces. |
| 2676 | * |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2677 | * @deprecated Use {@link TetheringEventCallback#onTetherableInterfaceRegexpsChanged( |
| 2678 | *TetheringManager.TetheringInterfaceRegexps)} instead. |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2679 | * {@hide} |
| 2680 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 2681 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2682 | @UnsupportedAppUsage |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2683 | @Deprecated |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2684 | public String[] getTetherableBluetoothRegexs() { |
Automerger Merge Worker | 6112c82 | 2020-03-06 00:38:43 +0000 | [diff] [blame] | 2685 | return mTetheringManager.getTetherableBluetoothRegexs(); |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 2686 | } |
| 2687 | |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2688 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2689 | * Attempt to both alter the mode of USB and Tethering of USB. A |
| 2690 | * utility method to deal with some of the complexity of USB - will |
| 2691 | * attempt to switch to Rndis and subsequently tether the resulting |
| 2692 | * interface on {@code true} or turn off tethering and switch off |
| 2693 | * Rndis on {@code false}. |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 2694 | * |
| 2695 | * <p>This method requires the caller to hold either the |
| 2696 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 2697 | * or the ability to modify system settings as determined by |
| 2698 | * {@link android.provider.Settings.System#canWrite}.</p> |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2699 | * |
| 2700 | * @param enable a boolean - {@code true} to enable tethering |
| 2701 | * @return error a {@code TETHER_ERROR} value indicating success or failure type |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2702 | * @deprecated Use {@link TetheringManager#startTethering} instead |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2703 | * |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2704 | * {@hide} |
| 2705 | */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2706 | @UnsupportedAppUsage |
markchien | e8b9d75 | 2020-01-20 19:31:56 +0800 | [diff] [blame] | 2707 | @Deprecated |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2708 | public int setUsbTethering(boolean enable) { |
Automerger Merge Worker | 6112c82 | 2020-03-06 00:38:43 +0000 | [diff] [blame] | 2709 | return mTetheringManager.setUsbTethering(enable); |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 2710 | } |
| 2711 | |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2712 | /** |
| 2713 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_NO_ERROR}. |
| 2714 | * {@hide} |
| 2715 | */ |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 2716 | @SystemApi |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2717 | @Deprecated |
| 2718 | public static final int TETHER_ERROR_NO_ERROR = TetheringManager.TETHER_ERROR_NO_ERROR; |
| 2719 | /** |
| 2720 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNKNOWN_IFACE}. |
| 2721 | * {@hide} |
| 2722 | */ |
| 2723 | @Deprecated |
| 2724 | public static final int TETHER_ERROR_UNKNOWN_IFACE = |
| 2725 | TetheringManager.TETHER_ERROR_UNKNOWN_IFACE; |
| 2726 | /** |
| 2727 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_SERVICE_UNAVAIL}. |
| 2728 | * {@hide} |
| 2729 | */ |
| 2730 | @Deprecated |
| 2731 | public static final int TETHER_ERROR_SERVICE_UNAVAIL = |
| 2732 | TetheringManager.TETHER_ERROR_SERVICE_UNAVAIL; |
| 2733 | /** |
| 2734 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNSUPPORTED}. |
| 2735 | * {@hide} |
| 2736 | */ |
| 2737 | @Deprecated |
| 2738 | public static final int TETHER_ERROR_UNSUPPORTED = TetheringManager.TETHER_ERROR_UNSUPPORTED; |
| 2739 | /** |
| 2740 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNAVAIL_IFACE}. |
| 2741 | * {@hide} |
| 2742 | */ |
| 2743 | @Deprecated |
| 2744 | public static final int TETHER_ERROR_UNAVAIL_IFACE = |
| 2745 | TetheringManager.TETHER_ERROR_UNAVAIL_IFACE; |
| 2746 | /** |
markchien | 62a625d | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2747 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_INTERNAL_ERROR}. |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2748 | * {@hide} |
| 2749 | */ |
| 2750 | @Deprecated |
markchien | 62a625d | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2751 | public static final int TETHER_ERROR_MASTER_ERROR = |
| 2752 | TetheringManager.TETHER_ERROR_INTERNAL_ERROR; |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2753 | /** |
| 2754 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_TETHER_IFACE_ERROR}. |
| 2755 | * {@hide} |
| 2756 | */ |
| 2757 | @Deprecated |
| 2758 | public static final int TETHER_ERROR_TETHER_IFACE_ERROR = |
| 2759 | TetheringManager.TETHER_ERROR_TETHER_IFACE_ERROR; |
| 2760 | /** |
| 2761 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_UNTETHER_IFACE_ERROR}. |
| 2762 | * {@hide} |
| 2763 | */ |
| 2764 | @Deprecated |
| 2765 | public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = |
| 2766 | TetheringManager.TETHER_ERROR_UNTETHER_IFACE_ERROR; |
| 2767 | /** |
markchien | 62a625d | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2768 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_ENABLE_FORWARDING_ERROR}. |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2769 | * {@hide} |
| 2770 | */ |
| 2771 | @Deprecated |
| 2772 | public static final int TETHER_ERROR_ENABLE_NAT_ERROR = |
markchien | 62a625d | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2773 | TetheringManager.TETHER_ERROR_ENABLE_FORWARDING_ERROR; |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2774 | /** |
markchien | 62a625d | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2775 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_DISABLE_FORWARDING_ERROR}. |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2776 | * {@hide} |
| 2777 | */ |
| 2778 | @Deprecated |
| 2779 | public static final int TETHER_ERROR_DISABLE_NAT_ERROR = |
markchien | 62a625d | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2780 | TetheringManager.TETHER_ERROR_DISABLE_FORWARDING_ERROR; |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2781 | /** |
| 2782 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_IFACE_CFG_ERROR}. |
| 2783 | * {@hide} |
| 2784 | */ |
| 2785 | @Deprecated |
| 2786 | public static final int TETHER_ERROR_IFACE_CFG_ERROR = |
| 2787 | TetheringManager.TETHER_ERROR_IFACE_CFG_ERROR; |
| 2788 | /** |
markchien | 62a625d | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2789 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_PROVISIONING_FAILED}. |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2790 | * {@hide} |
| 2791 | */ |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 2792 | @SystemApi |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2793 | @Deprecated |
| 2794 | public static final int TETHER_ERROR_PROVISION_FAILED = |
markchien | 62a625d | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2795 | TetheringManager.TETHER_ERROR_PROVISIONING_FAILED; |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2796 | /** |
| 2797 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_DHCPSERVER_ERROR}. |
| 2798 | * {@hide} |
| 2799 | */ |
| 2800 | @Deprecated |
| 2801 | public static final int TETHER_ERROR_DHCPSERVER_ERROR = |
| 2802 | TetheringManager.TETHER_ERROR_DHCPSERVER_ERROR; |
| 2803 | /** |
| 2804 | * @deprecated Use {@link TetheringManager#TETHER_ERROR_ENTITLEMENT_UNKNOWN}. |
| 2805 | * {@hide} |
| 2806 | */ |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 2807 | @SystemApi |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2808 | @Deprecated |
| 2809 | public static final int TETHER_ERROR_ENTITLEMENT_UNKONWN = |
| 2810 | TetheringManager.TETHER_ERROR_ENTITLEMENT_UNKNOWN; |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2811 | |
| 2812 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2813 | * Get a more detailed error code after a Tethering or Untethering |
| 2814 | * request asynchronously failed. |
| 2815 | * |
| 2816 | * @param iface The name of the interface of interest |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2817 | * @return error The error code of the last error tethering or untethering the named |
| 2818 | * interface |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2819 | * |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2820 | * @deprecated Use {@link TetheringEventCallback#onError(String, int)} instead. |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2821 | * {@hide} |
| 2822 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 2823 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 2824 | @UnsupportedAppUsage |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2825 | @Deprecated |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 2826 | public int getLastTetherError(String iface) { |
markchien | 62a625d | 2020-03-19 13:37:43 +0800 | [diff] [blame] | 2827 | int error = mTetheringManager.getLastTetherError(iface); |
| 2828 | if (error == TetheringManager.TETHER_ERROR_UNKNOWN_TYPE) { |
| 2829 | // TETHER_ERROR_UNKNOWN_TYPE was introduced with TetheringManager and has never been |
| 2830 | // returned by ConnectivityManager. Convert it to the legacy TETHER_ERROR_UNKNOWN_IFACE |
| 2831 | // instead. |
| 2832 | error = TetheringManager.TETHER_ERROR_UNKNOWN_IFACE; |
| 2833 | } |
| 2834 | return error; |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2835 | } |
| 2836 | |
markchien | 9554abf | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 2837 | /** @hide */ |
| 2838 | @Retention(RetentionPolicy.SOURCE) |
| 2839 | @IntDef(value = { |
| 2840 | TETHER_ERROR_NO_ERROR, |
| 2841 | TETHER_ERROR_PROVISION_FAILED, |
| 2842 | TETHER_ERROR_ENTITLEMENT_UNKONWN, |
| 2843 | }) |
| 2844 | public @interface EntitlementResultCode { |
| 2845 | } |
| 2846 | |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 2847 | /** |
markchien | 9554abf | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 2848 | * Callback for use with {@link #getLatestTetheringEntitlementResult} to find out whether |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 2849 | * entitlement succeeded. |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2850 | * |
| 2851 | * @deprecated Use {@link TetheringManager#OnTetheringEntitlementResultListener} instead. |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 2852 | * @hide |
| 2853 | */ |
| 2854 | @SystemApi |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2855 | @Deprecated |
markchien | 9554abf | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 2856 | public interface OnTetheringEntitlementResultListener { |
| 2857 | /** |
| 2858 | * Called to notify entitlement result. |
| 2859 | * |
| 2860 | * @param resultCode an int value of entitlement result. It may be one of |
| 2861 | * {@link #TETHER_ERROR_NO_ERROR}, |
| 2862 | * {@link #TETHER_ERROR_PROVISION_FAILED}, or |
| 2863 | * {@link #TETHER_ERROR_ENTITLEMENT_UNKONWN}. |
| 2864 | */ |
Jeremy Klein | 9f2e0f0 | 2019-03-12 13:32:08 -0700 | [diff] [blame] | 2865 | void onTetheringEntitlementResult(@EntitlementResultCode int resultCode); |
markchien | 9554abf | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 2866 | } |
| 2867 | |
| 2868 | /** |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 2869 | * Get the last value of the entitlement check on this downstream. If the cached value is |
| 2870 | * {@link #TETHER_ERROR_NO_ERROR} or showEntitlementUi argument is false, it just return the |
| 2871 | * cached value. Otherwise, a UI-based entitlement check would be performed. It is not |
| 2872 | * guaranteed that the UI-based entitlement check will complete in any specific time period |
| 2873 | * and may in fact never complete. Any successful entitlement check the platform performs for |
| 2874 | * any reason will update the cached value. |
| 2875 | * |
| 2876 | * @param type the downstream type of tethering. Must be one of |
| 2877 | * {@link #TETHERING_WIFI}, |
| 2878 | * {@link #TETHERING_USB}, or |
| 2879 | * {@link #TETHERING_BLUETOOTH}. |
| 2880 | * @param showEntitlementUi a boolean indicating whether to run UI-based entitlement check. |
markchien | 9554abf | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 2881 | * @param executor the executor on which callback will be invoked. |
| 2882 | * @param listener an {@link OnTetheringEntitlementResultListener} which will be called to |
| 2883 | * notify the caller of the result of entitlement check. The listener may be called zero |
| 2884 | * or one time. |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2885 | * @deprecated Use {@link TetheringManager#requestLatestTetheringEntitlementResult} instead. |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 2886 | * {@hide} |
| 2887 | */ |
| 2888 | @SystemApi |
markchien | 40898ca | 2020-01-21 13:11:06 +0800 | [diff] [blame] | 2889 | @Deprecated |
markchien | f273127 | 2019-01-16 17:44:13 +0800 | [diff] [blame] | 2890 | @RequiresPermission(android.Manifest.permission.TETHER_PRIVILEGED) |
markchien | 9554abf | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 2891 | public void getLatestTetheringEntitlementResult(int type, boolean showEntitlementUi, |
| 2892 | @NonNull @CallbackExecutor Executor executor, |
| 2893 | @NonNull final OnTetheringEntitlementResultListener listener) { |
| 2894 | Preconditions.checkNotNull(listener, "TetheringEntitlementResultListener cannot be null."); |
| 2895 | ResultReceiver wrappedListener = new ResultReceiver(null) { |
| 2896 | @Override |
| 2897 | protected void onReceiveResult(int resultCode, Bundle resultData) { |
| 2898 | Binder.withCleanCallingIdentity(() -> |
| 2899 | executor.execute(() -> { |
Jeremy Klein | 9f2e0f0 | 2019-03-12 13:32:08 -0700 | [diff] [blame] | 2900 | listener.onTetheringEntitlementResult(resultCode); |
markchien | 9554abf | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 2901 | })); |
| 2902 | } |
| 2903 | }; |
| 2904 | |
Automerger Merge Worker | 6112c82 | 2020-03-06 00:38:43 +0000 | [diff] [blame] | 2905 | mTetheringManager.requestLatestTetheringEntitlementResult(type, wrappedListener, |
markchien | ae8aa64 | 2019-12-16 20:15:20 +0800 | [diff] [blame] | 2906 | showEntitlementUi); |
markchien | 9554abf | 2019-03-06 16:25:00 +0800 | [diff] [blame] | 2907 | } |
| 2908 | |
| 2909 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2910 | * Report network connectivity status. This is currently used only |
| 2911 | * to alter status bar UI. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 2912 | * <p>This method requires the caller to hold the permission |
| 2913 | * {@link android.Manifest.permission#STATUS_BAR}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2914 | * |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2915 | * @param networkType The type of network you want to report on |
| 2916 | * @param percentage The quality of the connection 0 is bad, 100 is good |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 2917 | * @deprecated Types are deprecated. Use {@link #reportNetworkConnectivity} instead. |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2918 | * {@hide} |
| 2919 | */ |
| 2920 | public void reportInetCondition(int networkType, int percentage) { |
Soi, Yoshinari | dee2aa4 | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 2921 | printStackTrace(); |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2922 | try { |
| 2923 | mService.reportInetCondition(networkType, percentage); |
| 2924 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2925 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 2926 | } |
| 2927 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2928 | |
| 2929 | /** |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2930 | * Report a problem network to the framework. This provides a hint to the system |
Ye Wen | b87875e | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 2931 | * that there might be connectivity problems on this network and may cause |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2932 | * the framework to re-evaluate network connectivity and/or switch to another |
| 2933 | * network. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2934 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2935 | * @param network The {@link Network} the application was attempting to use |
| 2936 | * or {@code null} to indicate the current default network. |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 2937 | * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both |
| 2938 | * working and non-working connectivity. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2939 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 2940 | @Deprecated |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 2941 | public void reportBadNetwork(@Nullable Network network) { |
Soi, Yoshinari | dee2aa4 | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 2942 | printStackTrace(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2943 | try { |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 2944 | // One of these will be ignored because it matches system's current state. |
| 2945 | // The other will trigger the necessary reevaluation. |
| 2946 | mService.reportNetworkConnectivity(network, true); |
| 2947 | mService.reportNetworkConnectivity(network, false); |
| 2948 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2949 | throw e.rethrowFromSystemServer(); |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 2950 | } |
| 2951 | } |
| 2952 | |
| 2953 | /** |
| 2954 | * Report to the framework whether a network has working connectivity. |
| 2955 | * This provides a hint to the system that a particular network is providing |
| 2956 | * working connectivity or not. In response the framework may re-evaluate |
| 2957 | * the network's connectivity and might take further action thereafter. |
| 2958 | * |
| 2959 | * @param network The {@link Network} the application was attempting to use |
| 2960 | * or {@code null} to indicate the current default network. |
| 2961 | * @param hasConnectivity {@code true} if the application was able to successfully access the |
| 2962 | * Internet using {@code network} or {@code false} if not. |
| 2963 | */ |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 2964 | public void reportNetworkConnectivity(@Nullable Network network, boolean hasConnectivity) { |
Soi, Yoshinari | dee2aa4 | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 2965 | printStackTrace(); |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 2966 | try { |
| 2967 | mService.reportNetworkConnectivity(network, hasConnectivity); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2968 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2969 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2970 | } |
| 2971 | } |
| 2972 | |
| 2973 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2974 | * Set a network-independent global http proxy. This is not normally what you want |
| 2975 | * for typical HTTP proxies - they are general network dependent. However if you're |
| 2976 | * doing something unusual like general internal filtering this may be useful. On |
| 2977 | * a private network where the proxy is not accessible, you may break HTTP using this. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 2978 | * |
| 2979 | * @param p A {@link ProxyInfo} object defining the new global |
| 2980 | * HTTP proxy. A {@code null} value will clear the global HTTP proxy. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2981 | * @hide |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2982 | */ |
paulhu | a6af6b6 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 2983 | @RequiresPermission(android.Manifest.permission.NETWORK_STACK) |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2984 | public void setGlobalProxy(ProxyInfo p) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2985 | try { |
| 2986 | mService.setGlobalProxy(p); |
| 2987 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 2988 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2989 | } |
| 2990 | } |
| 2991 | |
| 2992 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2993 | * Retrieve any network-independent global HTTP proxy. |
| 2994 | * |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2995 | * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null} |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2996 | * if no global HTTP proxy is set. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2997 | * @hide |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 2998 | */ |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 2999 | public ProxyInfo getGlobalProxy() { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3000 | try { |
| 3001 | return mService.getGlobalProxy(); |
| 3002 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3003 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3004 | } |
| 3005 | } |
| 3006 | |
| 3007 | /** |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 3008 | * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a |
| 3009 | * network-specific HTTP proxy. If {@code network} is null, the |
| 3010 | * network-specific proxy returned is the proxy of the default active |
| 3011 | * network. |
| 3012 | * |
| 3013 | * @return {@link ProxyInfo} for the current global HTTP proxy, or if no |
| 3014 | * global HTTP proxy is set, {@code ProxyInfo} for {@code network}, |
| 3015 | * or when {@code network} is {@code null}, |
| 3016 | * the {@code ProxyInfo} for the default active network. Returns |
| 3017 | * {@code null} when no proxy applies or the caller doesn't have |
| 3018 | * permission to use {@code network}. |
| 3019 | * @hide |
| 3020 | */ |
| 3021 | public ProxyInfo getProxyForNetwork(Network network) { |
| 3022 | try { |
| 3023 | return mService.getProxyForNetwork(network); |
| 3024 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3025 | throw e.rethrowFromSystemServer(); |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 3026 | } |
| 3027 | } |
| 3028 | |
| 3029 | /** |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 3030 | * Get the current default HTTP proxy settings. If a global proxy is set it will be returned, |
| 3031 | * otherwise if this process is bound to a {@link Network} using |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 3032 | * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 3033 | * the default network's proxy is returned. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 3034 | * |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 3035 | * @return the {@link ProxyInfo} for the current HTTP proxy, or {@code null} if no |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 3036 | * HTTP proxy is active. |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3037 | */ |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 3038 | @Nullable |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 3039 | public ProxyInfo getDefaultProxy() { |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 3040 | return getProxyForNetwork(getBoundNetworkForProcess()); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 3041 | } |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 3042 | |
| 3043 | /** |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 3044 | * Returns true if the hardware supports the given network type |
| 3045 | * else it returns false. This doesn't indicate we have coverage |
| 3046 | * or are authorized onto a network, just whether or not the |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 3047 | * hardware supports it. For example a GSM phone without a SIM |
| 3048 | * should still return {@code true} for mobile data, but a wifi only |
| 3049 | * tablet would return {@code false}. |
| 3050 | * |
| 3051 | * @param networkType The network type we'd like to check |
| 3052 | * @return {@code true} if supported, else {@code false} |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 3053 | * @deprecated Types are deprecated. Use {@link NetworkCapabilities} instead. |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 3054 | * @hide |
| 3055 | */ |
Chalard Jean | 6b1da6e | 2018-03-08 13:54:53 +0900 | [diff] [blame] | 3056 | @Deprecated |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 3057 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 0bb53dbb | 2019-04-09 15:46:21 +0900 | [diff] [blame] | 3058 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562) |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 3059 | public boolean isNetworkSupported(int networkType) { |
| 3060 | try { |
| 3061 | return mService.isNetworkSupported(networkType); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3062 | } catch (RemoteException e) { |
| 3063 | throw e.rethrowFromSystemServer(); |
| 3064 | } |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 3065 | } |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 3066 | |
| 3067 | /** |
| 3068 | * Returns if the currently active data network is metered. A network is |
| 3069 | * classified as metered when the user is sensitive to heavy data usage on |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 3070 | * that connection due to monetary costs, data limitations or |
| 3071 | * battery/performance issues. You should check this before doing large |
| 3072 | * data transfers, and warn the user or delay the operation until another |
| 3073 | * network is available. |
| 3074 | * |
| 3075 | * @return {@code true} if large transfers should be avoided, otherwise |
| 3076 | * {@code false}. |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 3077 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 3078 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 3079 | public boolean isActiveNetworkMetered() { |
| 3080 | try { |
| 3081 | return mService.isActiveNetworkMetered(); |
| 3082 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3083 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 3084 | } |
| 3085 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3086 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 3087 | /** |
| 3088 | * If the LockdownVpn mechanism is enabled, updates the vpn |
| 3089 | * with a reload of its profile. |
| 3090 | * |
| 3091 | * @return a boolean with {@code} indicating success |
| 3092 | * |
| 3093 | * <p>This method can only be called by the system UID |
| 3094 | * {@hide} |
| 3095 | */ |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3096 | public boolean updateLockdownVpn() { |
| 3097 | try { |
| 3098 | return mService.updateLockdownVpn(); |
| 3099 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3100 | throw e.rethrowFromSystemServer(); |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 3101 | } |
| 3102 | } |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 3103 | |
| 3104 | /** |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3105 | * Check mobile provisioning. |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3106 | * |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3107 | * @param suggestedTimeOutMs, timeout in milliseconds |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3108 | * |
| 3109 | * @return time out that will be used, maybe less that suggestedTimeOutMs |
| 3110 | * -1 if an error. |
| 3111 | * |
| 3112 | * {@hide} |
| 3113 | */ |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3114 | public int checkMobileProvisioning(int suggestedTimeOutMs) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3115 | int timeOutMs = -1; |
| 3116 | try { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3117 | timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3118 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3119 | throw e.rethrowFromSystemServer(); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 3120 | } |
| 3121 | return timeOutMs; |
| 3122 | } |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3123 | |
| 3124 | /** |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 3125 | * Get the mobile provisioning url. |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3126 | * {@hide} |
| 3127 | */ |
paulhu | a6af6b6 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 3128 | @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS) |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3129 | public String getMobileProvisioningUrl() { |
| 3130 | try { |
| 3131 | return mService.getMobileProvisioningUrl(); |
| 3132 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3133 | throw e.rethrowFromSystemServer(); |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3134 | } |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 3135 | } |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 3136 | |
| 3137 | /** |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3138 | * Set sign in error notification to visible or in visible |
| 3139 | * |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3140 | * {@hide} |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 3141 | * @deprecated Doesn't properly deal with multiple connected networks of the same type. |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3142 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 3143 | @Deprecated |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3144 | public void setProvisioningNotificationVisible(boolean visible, int networkType, |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3145 | String action) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3146 | try { |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 3147 | mService.setProvisioningNotificationVisible(visible, networkType, action); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3148 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3149 | throw e.rethrowFromSystemServer(); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 3150 | } |
| 3151 | } |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3152 | |
| 3153 | /** |
| 3154 | * Set the value for enabling/disabling airplane mode |
| 3155 | * |
| 3156 | * @param enable whether to enable airplane mode or not |
| 3157 | * |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3158 | * @hide |
| 3159 | */ |
Lorenzo Colitti | f5845d1 | 2018-10-09 18:55:11 +0900 | [diff] [blame] | 3160 | @RequiresPermission(anyOf = { |
Edward Savage-Jones | affb229 | 2019-11-26 13:18:08 +0100 | [diff] [blame] | 3161 | android.Manifest.permission.NETWORK_AIRPLANE_MODE, |
Lorenzo Colitti | f5845d1 | 2018-10-09 18:55:11 +0900 | [diff] [blame] | 3162 | android.Manifest.permission.NETWORK_SETTINGS, |
| 3163 | android.Manifest.permission.NETWORK_SETUP_WIZARD, |
| 3164 | android.Manifest.permission.NETWORK_STACK}) |
Lorenzo Colitti | 85eca48 | 2018-10-09 18:50:32 +0900 | [diff] [blame] | 3165 | @SystemApi |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3166 | public void setAirplaneMode(boolean enable) { |
| 3167 | try { |
| 3168 | mService.setAirplaneMode(enable); |
| 3169 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3170 | throw e.rethrowFromSystemServer(); |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 3171 | } |
| 3172 | } |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3173 | |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 3174 | /** {@hide} - returns the factory serial number */ |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 3175 | @UnsupportedAppUsage |
Automerger Merge Worker | 8a8e8a1 | 2020-03-04 18:04:01 +0000 | [diff] [blame] | 3176 | @RequiresPermission(anyOf = { |
| 3177 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 3178 | android.Manifest.permission.NETWORK_FACTORY}) |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 3179 | public int registerNetworkFactory(Messenger messenger, String name) { |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3180 | try { |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 3181 | return mService.registerNetworkFactory(messenger, name); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3182 | } catch (RemoteException e) { |
| 3183 | throw e.rethrowFromSystemServer(); |
| 3184 | } |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3185 | } |
| 3186 | |
| 3187 | /** {@hide} */ |
Mathew Inwood | 55418ea | 2018-12-20 15:30:45 +0000 | [diff] [blame] | 3188 | @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023) |
Automerger Merge Worker | 8a8e8a1 | 2020-03-04 18:04:01 +0000 | [diff] [blame] | 3189 | @RequiresPermission(anyOf = { |
| 3190 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 3191 | android.Manifest.permission.NETWORK_FACTORY}) |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 3192 | public void unregisterNetworkFactory(Messenger messenger) { |
| 3193 | try { |
| 3194 | mService.unregisterNetworkFactory(messenger); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3195 | } catch (RemoteException e) { |
| 3196 | throw e.rethrowFromSystemServer(); |
| 3197 | } |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 3198 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3199 | |
Lorenzo Colitti | ae5cb71 | 2020-01-08 00:04:09 +0900 | [diff] [blame] | 3200 | /** |
| 3201 | * Registers the specified {@link NetworkProvider}. |
| 3202 | * Each listener must only be registered once. The listener can be unregistered with |
| 3203 | * {@link #unregisterNetworkProvider}. |
| 3204 | * |
| 3205 | * @param provider the provider to register |
| 3206 | * @return the ID of the provider. This ID must be used by the provider when registering |
| 3207 | * {@link android.net.NetworkAgent}s. |
| 3208 | * @hide |
| 3209 | */ |
| 3210 | @SystemApi |
Automerger Merge Worker | 8a8e8a1 | 2020-03-04 18:04:01 +0000 | [diff] [blame] | 3211 | @RequiresPermission(anyOf = { |
| 3212 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 3213 | android.Manifest.permission.NETWORK_FACTORY}) |
Lorenzo Colitti | ae5cb71 | 2020-01-08 00:04:09 +0900 | [diff] [blame] | 3214 | public int registerNetworkProvider(@NonNull NetworkProvider provider) { |
| 3215 | if (provider.getProviderId() != NetworkProvider.ID_NONE) { |
Lorenzo Colitti | ae5cb71 | 2020-01-08 00:04:09 +0900 | [diff] [blame] | 3216 | throw new IllegalStateException("NetworkProviders can only be registered once"); |
| 3217 | } |
| 3218 | |
| 3219 | try { |
| 3220 | int providerId = mService.registerNetworkProvider(provider.getMessenger(), |
| 3221 | provider.getName()); |
| 3222 | provider.setProviderId(providerId); |
| 3223 | } catch (RemoteException e) { |
| 3224 | throw e.rethrowFromSystemServer(); |
| 3225 | } |
| 3226 | return provider.getProviderId(); |
| 3227 | } |
| 3228 | |
| 3229 | /** |
| 3230 | * Unregisters the specified NetworkProvider. |
| 3231 | * |
| 3232 | * @param provider the provider to unregister |
| 3233 | * @hide |
| 3234 | */ |
| 3235 | @SystemApi |
Automerger Merge Worker | 8a8e8a1 | 2020-03-04 18:04:01 +0000 | [diff] [blame] | 3236 | @RequiresPermission(anyOf = { |
| 3237 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 3238 | android.Manifest.permission.NETWORK_FACTORY}) |
Lorenzo Colitti | ae5cb71 | 2020-01-08 00:04:09 +0900 | [diff] [blame] | 3239 | public void unregisterNetworkProvider(@NonNull NetworkProvider provider) { |
| 3240 | try { |
| 3241 | mService.unregisterNetworkProvider(provider.getMessenger()); |
| 3242 | } catch (RemoteException e) { |
| 3243 | throw e.rethrowFromSystemServer(); |
| 3244 | } |
| 3245 | provider.setProviderId(NetworkProvider.ID_NONE); |
| 3246 | } |
| 3247 | |
| 3248 | |
| 3249 | /** @hide exposed via the NetworkProvider class. */ |
Automerger Merge Worker | 8a8e8a1 | 2020-03-04 18:04:01 +0000 | [diff] [blame] | 3250 | @RequiresPermission(anyOf = { |
| 3251 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 3252 | android.Manifest.permission.NETWORK_FACTORY}) |
Lorenzo Colitti | ae5cb71 | 2020-01-08 00:04:09 +0900 | [diff] [blame] | 3253 | public void declareNetworkRequestUnfulfillable(@NonNull NetworkRequest request) { |
| 3254 | try { |
| 3255 | mService.declareNetworkRequestUnfulfillable(request); |
| 3256 | } catch (RemoteException e) { |
| 3257 | throw e.rethrowFromSystemServer(); |
| 3258 | } |
| 3259 | } |
| 3260 | |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 3261 | // TODO : remove this method. It is a stopgap measure to help sheperding a number |
| 3262 | // of dependent changes that would conflict throughout the automerger graph. Having this |
| 3263 | // temporarily helps with the process of going through with all these dependent changes across |
| 3264 | // the entire tree. |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 3265 | /** |
| 3266 | * @hide |
| 3267 | * Register a NetworkAgent with ConnectivityService. |
Chalard Jean | a0e2aa12 | 2019-12-13 19:47:12 +0900 | [diff] [blame] | 3268 | * @return Network corresponding to NetworkAgent. |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 3269 | */ |
Automerger Merge Worker | 8a8e8a1 | 2020-03-04 18:04:01 +0000 | [diff] [blame] | 3270 | @RequiresPermission(anyOf = { |
| 3271 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 3272 | android.Manifest.permission.NETWORK_FACTORY}) |
Chalard Jean | a0e2aa12 | 2019-12-13 19:47:12 +0900 | [diff] [blame] | 3273 | public Network registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp, |
Lorenzo Colitti | d969656 | 2020-01-12 22:28:37 +0900 | [diff] [blame] | 3274 | NetworkCapabilities nc, int score, NetworkAgentConfig config) { |
Chalard Jean | a11593c | 2020-02-21 19:37:21 +0900 | [diff] [blame] | 3275 | return registerNetworkAgent(messenger, ni, lp, nc, score, config, NetworkProvider.ID_NONE); |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 3276 | } |
| 3277 | |
| 3278 | /** |
| 3279 | * @hide |
| 3280 | * Register a NetworkAgent with ConnectivityService. |
Chalard Jean | a0e2aa12 | 2019-12-13 19:47:12 +0900 | [diff] [blame] | 3281 | * @return Network corresponding to NetworkAgent. |
Chalard Jean | 05ab681 | 2018-05-02 21:14:54 +0900 | [diff] [blame] | 3282 | */ |
Automerger Merge Worker | 8a8e8a1 | 2020-03-04 18:04:01 +0000 | [diff] [blame] | 3283 | @RequiresPermission(anyOf = { |
| 3284 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 3285 | android.Manifest.permission.NETWORK_FACTORY}) |
Chalard Jean | a0e2aa12 | 2019-12-13 19:47:12 +0900 | [diff] [blame] | 3286 | public Network registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp, |
Chalard Jean | a11593c | 2020-02-21 19:37:21 +0900 | [diff] [blame] | 3287 | NetworkCapabilities nc, int score, NetworkAgentConfig config, int providerId) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3288 | try { |
Lorenzo Colitti | d969656 | 2020-01-12 22:28:37 +0900 | [diff] [blame] | 3289 | return mService.registerNetworkAgent(messenger, ni, lp, nc, score, config, providerId); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 3290 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3291 | throw e.rethrowFromSystemServer(); |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 3292 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3293 | } |
| 3294 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3295 | /** |
Hugo Benichi | dafed3d | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 3296 | * Base class for {@code NetworkRequest} callbacks. Used for notifications about network |
| 3297 | * changes. Should be extended by applications wanting notifications. |
| 3298 | * |
| 3299 | * A {@code NetworkCallback} is registered by calling |
| 3300 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, |
| 3301 | * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)}, |
Hugo Benichi | ca867dc | 2018-02-07 21:17:43 +0900 | [diff] [blame] | 3302 | * or {@link #registerDefaultNetworkCallback(NetworkCallback)}. A {@code NetworkCallback} is |
Hugo Benichi | dafed3d | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 3303 | * unregistered by calling {@link #unregisterNetworkCallback(NetworkCallback)}. |
| 3304 | * A {@code NetworkCallback} should be registered at most once at any time. |
| 3305 | * A {@code NetworkCallback} that has been unregistered can be registered again. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3306 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3307 | public static class NetworkCallback { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3308 | /** |
Lorenzo Colitti | 0708693 | 2015-04-24 12:23:24 +0900 | [diff] [blame] | 3309 | * Called when the framework connects to a new network to evaluate whether it satisfies this |
| 3310 | * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable} |
| 3311 | * callback. There is no guarantee that this new network will satisfy any requests, or that |
| 3312 | * the network will stay connected for longer than the time necessary to evaluate it. |
| 3313 | * <p> |
| 3314 | * Most applications <b>should not</b> act on this callback, and should instead use |
| 3315 | * {@link #onAvailable}. This callback is intended for use by applications that can assist |
| 3316 | * the framework in properly evaluating the network — for example, an application that |
| 3317 | * can automatically log in to a captive portal without user intervention. |
| 3318 | * |
| 3319 | * @param network The {@link Network} of the network that is being evaluated. |
Lorenzo Colitti | 6627612 | 2015-06-11 14:27:17 +0900 | [diff] [blame] | 3320 | * |
| 3321 | * @hide |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3322 | */ |
paulhu | 3d67f53 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3323 | public void onPreCheck(@NonNull Network network) {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3324 | |
| 3325 | /** |
Lorenzo Colitti | 0708693 | 2015-04-24 12:23:24 +0900 | [diff] [blame] | 3326 | * Called when the framework connects and has declared a new network ready for use. |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3327 | * This callback may be called more than once if the {@link Network} that is |
| 3328 | * satisfying the request changes. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3329 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3330 | * @param network The {@link Network} of the satisfying network. |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3331 | * @param networkCapabilities The {@link NetworkCapabilities} of the satisfying network. |
| 3332 | * @param linkProperties The {@link LinkProperties} of the satisfying network. |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3333 | * @param blocked Whether access to the {@link Network} is blocked due to system policy. |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3334 | * @hide |
| 3335 | */ |
paulhu | 3d67f53 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3336 | public void onAvailable(@NonNull Network network, |
| 3337 | @NonNull NetworkCapabilities networkCapabilities, |
| 3338 | @NonNull LinkProperties linkProperties, boolean blocked) { |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3339 | // Internally only this method is called when a new network is available, and |
| 3340 | // it calls the callback in the same way and order that older versions used |
| 3341 | // to call so as not to change the behavior. |
| 3342 | onAvailable(network); |
| 3343 | if (!networkCapabilities.hasCapability( |
| 3344 | NetworkCapabilities.NET_CAPABILITY_NOT_SUSPENDED)) { |
| 3345 | onNetworkSuspended(network); |
| 3346 | } |
| 3347 | onCapabilitiesChanged(network, networkCapabilities); |
| 3348 | onLinkPropertiesChanged(network, linkProperties); |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3349 | onBlockedStatusChanged(network, blocked); |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3350 | } |
| 3351 | |
| 3352 | /** |
| 3353 | * Called when the framework connects and has declared a new network ready for use. |
Chalard Jean | 599ce75 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3354 | * |
| 3355 | * <p>For callbacks registered with {@link #registerNetworkCallback}, multiple networks may |
| 3356 | * be available at the same time, and onAvailable will be called for each of these as they |
| 3357 | * appear. |
| 3358 | * |
| 3359 | * <p>For callbacks registered with {@link #requestNetwork} and |
| 3360 | * {@link #registerDefaultNetworkCallback}, this means the network passed as an argument |
| 3361 | * is the new best network for this request and is now tracked by this callback ; this |
| 3362 | * callback will no longer receive method calls about other networks that may have been |
| 3363 | * passed to this method previously. The previously-best network may have disconnected, or |
| 3364 | * it may still be around and the newly-best network may simply be better. |
| 3365 | * |
| 3366 | * <p>Starting with {@link android.os.Build.VERSION_CODES#O}, this will always immediately |
| 3367 | * be followed by a call to {@link #onCapabilitiesChanged(Network, NetworkCapabilities)} |
| 3368 | * then by a call to {@link #onLinkPropertiesChanged(Network, LinkProperties)}, and a call |
| 3369 | * to {@link #onBlockedStatusChanged(Network, boolean)}. |
| 3370 | * |
| 3371 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 3372 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 3373 | * this callback as this is prone to race conditions (there is no guarantee the objects |
| 3374 | * returned by these methods will be current). Instead, wait for a call to |
| 3375 | * {@link #onCapabilitiesChanged(Network, NetworkCapabilities)} and |
| 3376 | * {@link #onLinkPropertiesChanged(Network, LinkProperties)} whose arguments are guaranteed |
| 3377 | * to be well-ordered with respect to other callbacks. |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3378 | * |
| 3379 | * @param network The {@link Network} of the satisfying network. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3380 | */ |
paulhu | 3d67f53 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3381 | public void onAvailable(@NonNull Network network) {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3382 | |
| 3383 | /** |
Chalard Jean | 599ce75 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3384 | * Called when the network is about to be lost, typically because there are no outstanding |
| 3385 | * requests left for it. This may be paired with a {@link NetworkCallback#onAvailable} call |
| 3386 | * with the new replacement network for graceful handover. This method is not guaranteed |
| 3387 | * to be called before {@link NetworkCallback#onLost} is called, for example in case a |
| 3388 | * network is suddenly disconnected. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3389 | * |
Chalard Jean | 599ce75 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3390 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 3391 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 3392 | * this callback as this is prone to race conditions ; calling these methods while in a |
| 3393 | * callback may return an outdated or even a null object. |
| 3394 | * |
| 3395 | * @param network The {@link Network} that is about to be lost. |
| 3396 | * @param maxMsToLive The time in milliseconds the system intends to keep the network |
| 3397 | * connected for graceful handover; note that the network may still |
| 3398 | * suffer a hard loss at any time. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3399 | */ |
paulhu | 3d67f53 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3400 | public void onLosing(@NonNull Network network, int maxMsToLive) {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3401 | |
| 3402 | /** |
Chalard Jean | 599ce75 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3403 | * Called when a network disconnects or otherwise no longer satisfies this request or |
| 3404 | * callback. |
| 3405 | * |
| 3406 | * <p>If the callback was registered with requestNetwork() or |
| 3407 | * registerDefaultNetworkCallback(), it will only be invoked against the last network |
| 3408 | * returned by onAvailable() when that network is lost and no other network satisfies |
| 3409 | * the criteria of the request. |
| 3410 | * |
| 3411 | * <p>If the callback was registered with registerNetworkCallback() it will be called for |
| 3412 | * each network which no longer satisfies the criteria of the callback. |
| 3413 | * |
| 3414 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 3415 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 3416 | * this callback as this is prone to race conditions ; calling these methods while in a |
| 3417 | * callback may return an outdated or even a null object. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3418 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3419 | * @param network The {@link Network} lost. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3420 | */ |
paulhu | 3d67f53 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3421 | public void onLost(@NonNull Network network) {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3422 | |
| 3423 | /** |
Chalard Jean | 599ce75 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3424 | * Called if no network is found within the timeout time specified in |
Etan Cohen | ddb720a | 2019-01-08 12:09:18 -0800 | [diff] [blame] | 3425 | * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} call or if the |
| 3426 | * requested network request cannot be fulfilled (whether or not a timeout was |
| 3427 | * specified). When this callback is invoked the associated |
Etan Cohen | aebf17e | 2017-03-01 12:47:28 -0800 | [diff] [blame] | 3428 | * {@link NetworkRequest} will have already been removed and released, as if |
| 3429 | * {@link #unregisterNetworkCallback(NetworkCallback)} had been called. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3430 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3431 | public void onUnavailable() {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3432 | |
| 3433 | /** |
Chalard Jean | 599ce75 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3434 | * Called when the network corresponding to this request changes capabilities but still |
| 3435 | * satisfies the requested criteria. |
| 3436 | * |
| 3437 | * <p>Starting with {@link android.os.Build.VERSION_CODES#O} this method is guaranteed |
| 3438 | * to be called immediately after {@link #onAvailable}. |
| 3439 | * |
| 3440 | * <p>Do NOT call {@link #getLinkProperties(Network)} or other synchronous |
| 3441 | * ConnectivityManager methods in this callback as this is prone to race conditions : |
| 3442 | * calling these methods while in a callback may return an outdated or even a null object. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3443 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3444 | * @param network The {@link Network} whose capabilities have changed. |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3445 | * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this |
| 3446 | * network. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3447 | */ |
paulhu | 3d67f53 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3448 | public void onCapabilitiesChanged(@NonNull Network network, |
| 3449 | @NonNull NetworkCapabilities networkCapabilities) {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3450 | |
| 3451 | /** |
Chalard Jean | 599ce75 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3452 | * Called when the network corresponding to this request changes {@link LinkProperties}. |
| 3453 | * |
| 3454 | * <p>Starting with {@link android.os.Build.VERSION_CODES#O} this method is guaranteed |
| 3455 | * to be called immediately after {@link #onAvailable}. |
| 3456 | * |
| 3457 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or other synchronous |
| 3458 | * ConnectivityManager methods in this callback as this is prone to race conditions : |
| 3459 | * calling these methods while in a callback may return an outdated or even a null object. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3460 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3461 | * @param network The {@link Network} whose link properties have changed. |
| 3462 | * @param linkProperties The new {@link LinkProperties} for this network. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3463 | */ |
paulhu | 3d67f53 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3464 | public void onLinkPropertiesChanged(@NonNull Network network, |
| 3465 | @NonNull LinkProperties linkProperties) {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3466 | |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3467 | /** |
Chalard Jean | 599ce75 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3468 | * Called when the network the framework connected to for this request suspends data |
| 3469 | * transmission temporarily. |
| 3470 | * |
| 3471 | * <p>This generally means that while the TCP connections are still live temporarily |
| 3472 | * network data fails to transfer. To give a specific example, this is used on cellular |
| 3473 | * networks to mask temporary outages when driving through a tunnel, etc. In general this |
| 3474 | * means read operations on sockets on this network will block once the buffers are |
| 3475 | * drained, and write operations will block once the buffers are full. |
| 3476 | * |
| 3477 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 3478 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 3479 | * this callback as this is prone to race conditions (there is no guarantee the objects |
| 3480 | * returned by these methods will be current). |
| 3481 | * |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3482 | * @hide |
| 3483 | */ |
paulhu | 3d67f53 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3484 | public void onNetworkSuspended(@NonNull Network network) {} |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3485 | |
| 3486 | /** |
| 3487 | * Called when the network the framework connected to for this request |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3488 | * returns from a {@link NetworkInfo.State#SUSPENDED} state. This should always be |
| 3489 | * preceded by a matching {@link NetworkCallback#onNetworkSuspended} call. |
Chalard Jean | 599ce75 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3490 | |
| 3491 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 3492 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 3493 | * this callback as this is prone to race conditions : calling these methods while in a |
| 3494 | * callback may return an outdated or even a null object. |
| 3495 | * |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3496 | * @hide |
| 3497 | */ |
paulhu | 3d67f53 | 2019-03-22 16:35:06 +0800 | [diff] [blame] | 3498 | public void onNetworkResumed(@NonNull Network network) {} |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3499 | |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3500 | /** |
| 3501 | * Called when access to the specified network is blocked or unblocked. |
| 3502 | * |
Chalard Jean | 599ce75 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3503 | * <p>Do NOT call {@link #getNetworkCapabilities(Network)} or |
| 3504 | * {@link #getLinkProperties(Network)} or other synchronous ConnectivityManager methods in |
| 3505 | * this callback as this is prone to race conditions : calling these methods while in a |
| 3506 | * callback may return an outdated or even a null object. |
| 3507 | * |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3508 | * @param network The {@link Network} whose blocked status has changed. |
| 3509 | * @param blocked The blocked status of this {@link Network}. |
| 3510 | */ |
junyulai | 0c66697 | 2019-03-04 22:45:36 +0800 | [diff] [blame] | 3511 | public void onBlockedStatusChanged(@NonNull Network network, boolean blocked) {} |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3512 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3513 | private NetworkRequest networkRequest; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 3514 | } |
| 3515 | |
Hugo Benichi | cb88323 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 3516 | /** |
| 3517 | * Constant error codes used by ConnectivityService to communicate about failures and errors |
| 3518 | * across a Binder boundary. |
| 3519 | * @hide |
| 3520 | */ |
| 3521 | public interface Errors { |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 3522 | int TOO_MANY_REQUESTS = 1; |
Hugo Benichi | cb88323 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 3523 | } |
| 3524 | |
| 3525 | /** @hide */ |
| 3526 | public static class TooManyRequestsException extends RuntimeException {} |
| 3527 | |
| 3528 | private static RuntimeException convertServiceException(ServiceSpecificException e) { |
| 3529 | switch (e.errorCode) { |
| 3530 | case Errors.TOO_MANY_REQUESTS: |
| 3531 | return new TooManyRequestsException(); |
| 3532 | default: |
| 3533 | Log.w(TAG, "Unknown service error code " + e.errorCode); |
| 3534 | return new RuntimeException(e); |
| 3535 | } |
| 3536 | } |
| 3537 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3538 | private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3539 | /** @hide */ |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3540 | public static final int CALLBACK_PRECHECK = BASE + 1; |
| 3541 | /** @hide */ |
| 3542 | public static final int CALLBACK_AVAILABLE = BASE + 2; |
| 3543 | /** @hide arg1 = TTL */ |
| 3544 | public static final int CALLBACK_LOSING = BASE + 3; |
| 3545 | /** @hide */ |
| 3546 | public static final int CALLBACK_LOST = BASE + 4; |
| 3547 | /** @hide */ |
| 3548 | public static final int CALLBACK_UNAVAIL = BASE + 5; |
| 3549 | /** @hide */ |
| 3550 | public static final int CALLBACK_CAP_CHANGED = BASE + 6; |
| 3551 | /** @hide */ |
| 3552 | public static final int CALLBACK_IP_CHANGED = BASE + 7; |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 3553 | /** @hide obj = NetworkCapabilities, arg1 = seq number */ |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 3554 | private static final int EXPIRE_LEGACY_REQUEST = BASE + 8; |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3555 | /** @hide */ |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 3556 | public static final int CALLBACK_SUSPENDED = BASE + 9; |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3557 | /** @hide */ |
Hugo Benichi | dba33db | 2017-03-23 22:40:44 +0900 | [diff] [blame] | 3558 | public static final int CALLBACK_RESUMED = BASE + 10; |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3559 | /** @hide */ |
| 3560 | public static final int CALLBACK_BLK_CHANGED = BASE + 11; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3561 | |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3562 | /** @hide */ |
| 3563 | public static String getCallbackName(int whichCallback) { |
| 3564 | switch (whichCallback) { |
| 3565 | case CALLBACK_PRECHECK: return "CALLBACK_PRECHECK"; |
| 3566 | case CALLBACK_AVAILABLE: return "CALLBACK_AVAILABLE"; |
| 3567 | case CALLBACK_LOSING: return "CALLBACK_LOSING"; |
| 3568 | case CALLBACK_LOST: return "CALLBACK_LOST"; |
| 3569 | case CALLBACK_UNAVAIL: return "CALLBACK_UNAVAIL"; |
| 3570 | case CALLBACK_CAP_CHANGED: return "CALLBACK_CAP_CHANGED"; |
| 3571 | case CALLBACK_IP_CHANGED: return "CALLBACK_IP_CHANGED"; |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3572 | case EXPIRE_LEGACY_REQUEST: return "EXPIRE_LEGACY_REQUEST"; |
| 3573 | case CALLBACK_SUSPENDED: return "CALLBACK_SUSPENDED"; |
| 3574 | case CALLBACK_RESUMED: return "CALLBACK_RESUMED"; |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3575 | case CALLBACK_BLK_CHANGED: return "CALLBACK_BLK_CHANGED"; |
Erik Kline | 57faba9 | 2015-11-25 12:49:38 +0900 | [diff] [blame] | 3576 | default: |
| 3577 | return Integer.toString(whichCallback); |
| 3578 | } |
| 3579 | } |
| 3580 | |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 3581 | private class CallbackHandler extends Handler { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3582 | private static final String TAG = "ConnectivityManager.CallbackHandler"; |
Robert Greenwalt | a9ebeef | 2015-09-03 16:41:45 -0700 | [diff] [blame] | 3583 | private static final boolean DBG = false; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3584 | |
Hugo Benichi | d42650f | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3585 | CallbackHandler(Looper looper) { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3586 | super(looper); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3587 | } |
| 3588 | |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3589 | CallbackHandler(Handler handler) { |
Hugo Benichi | e767851 | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 3590 | this(Preconditions.checkNotNull(handler, "Handler cannot be null.").getLooper()); |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3591 | } |
| 3592 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3593 | @Override |
| 3594 | public void handleMessage(Message message) { |
Hugo Benichi | 2c68452 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3595 | if (message.what == EXPIRE_LEGACY_REQUEST) { |
| 3596 | expireRequest((NetworkCapabilities) message.obj, message.arg1); |
| 3597 | return; |
| 3598 | } |
| 3599 | |
| 3600 | final NetworkRequest request = getObject(message, NetworkRequest.class); |
| 3601 | final Network network = getObject(message, Network.class); |
| 3602 | final NetworkCallback callback; |
| 3603 | synchronized (sCallbacks) { |
| 3604 | callback = sCallbacks.get(request); |
Etan Cohen | 4a62d1d | 2019-05-21 12:06:04 -0700 | [diff] [blame] | 3605 | if (callback == null) { |
| 3606 | Log.w(TAG, |
| 3607 | "callback not found for " + getCallbackName(message.what) + " message"); |
| 3608 | return; |
| 3609 | } |
Etan Cohen | 9b09a16 | 2019-04-16 15:07:55 -0700 | [diff] [blame] | 3610 | if (message.what == CALLBACK_UNAVAIL) { |
| 3611 | sCallbacks.remove(request); |
| 3612 | callback.networkRequest = ALREADY_UNREGISTERED; |
| 3613 | } |
Hugo Benichi | 2c68452 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3614 | } |
Lorenzo Colitti | fcfa7d9 | 2016-03-01 22:56:37 +0900 | [diff] [blame] | 3615 | if (DBG) { |
Hugo Benichi | a038568 | 2017-03-22 17:07:57 +0900 | [diff] [blame] | 3616 | Log.d(TAG, getCallbackName(message.what) + " for network " + network); |
Lorenzo Colitti | fcfa7d9 | 2016-03-01 22:56:37 +0900 | [diff] [blame] | 3617 | } |
Hugo Benichi | 2c68452 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3618 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3619 | switch (message.what) { |
| 3620 | case CALLBACK_PRECHECK: { |
Hugo Benichi | 2c68452 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3621 | callback.onPreCheck(network); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3622 | break; |
| 3623 | } |
| 3624 | case CALLBACK_AVAILABLE: { |
Chalard Jean | 804b8fb | 2018-01-30 22:41:41 +0900 | [diff] [blame] | 3625 | NetworkCapabilities cap = getObject(message, NetworkCapabilities.class); |
| 3626 | LinkProperties lp = getObject(message, LinkProperties.class); |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3627 | callback.onAvailable(network, cap, lp, message.arg1 != 0); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3628 | break; |
| 3629 | } |
| 3630 | case CALLBACK_LOSING: { |
Hugo Benichi | 2c68452 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3631 | callback.onLosing(network, message.arg1); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3632 | break; |
| 3633 | } |
| 3634 | case CALLBACK_LOST: { |
Hugo Benichi | 2c68452 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3635 | callback.onLost(network); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3636 | break; |
| 3637 | } |
| 3638 | case CALLBACK_UNAVAIL: { |
Hugo Benichi | 2c68452 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3639 | callback.onUnavailable(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3640 | break; |
| 3641 | } |
| 3642 | case CALLBACK_CAP_CHANGED: { |
Hugo Benichi | 2c68452 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3643 | NetworkCapabilities cap = getObject(message, NetworkCapabilities.class); |
| 3644 | callback.onCapabilitiesChanged(network, cap); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3645 | break; |
| 3646 | } |
| 3647 | case CALLBACK_IP_CHANGED: { |
Hugo Benichi | 2c68452 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3648 | LinkProperties lp = getObject(message, LinkProperties.class); |
| 3649 | callback.onLinkPropertiesChanged(network, lp); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3650 | break; |
| 3651 | } |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3652 | case CALLBACK_SUSPENDED: { |
Hugo Benichi | 2c68452 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3653 | callback.onNetworkSuspended(network); |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 3654 | break; |
| 3655 | } |
| 3656 | case CALLBACK_RESUMED: { |
Hugo Benichi | 2c68452 | 2017-05-09 14:36:02 +0900 | [diff] [blame] | 3657 | callback.onNetworkResumed(network); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 3658 | break; |
| 3659 | } |
junyulai | 05986c6 | 2018-08-07 19:50:45 +0800 | [diff] [blame] | 3660 | case CALLBACK_BLK_CHANGED: { |
| 3661 | boolean blocked = message.arg1 != 0; |
| 3662 | callback.onBlockedStatusChanged(network, blocked); |
| 3663 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3664 | } |
| 3665 | } |
| 3666 | |
Hugo Benichi | d42650f | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3667 | private <T> T getObject(Message msg, Class<T> c) { |
| 3668 | return (T) msg.getData().getParcelable(c.getSimpleName()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3669 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3670 | } |
| 3671 | |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3672 | private CallbackHandler getDefaultHandler() { |
Hugo Benichi | 7724cdd | 2016-07-07 10:15:56 +0900 | [diff] [blame] | 3673 | synchronized (sCallbacks) { |
| 3674 | if (sCallbackHandler == null) { |
| 3675 | sCallbackHandler = new CallbackHandler(ConnectivityThread.getInstanceLooper()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3676 | } |
Hugo Benichi | 7724cdd | 2016-07-07 10:15:56 +0900 | [diff] [blame] | 3677 | return sCallbackHandler; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3678 | } |
| 3679 | } |
| 3680 | |
Hugo Benichi | 6f260f3 | 2017-02-03 14:18:44 +0900 | [diff] [blame] | 3681 | private static final HashMap<NetworkRequest, NetworkCallback> sCallbacks = new HashMap<>(); |
| 3682 | private static CallbackHandler sCallbackHandler; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3683 | |
Hugo Benichi | 6f260f3 | 2017-02-03 14:18:44 +0900 | [diff] [blame] | 3684 | private static final int LISTEN = 1; |
| 3685 | private static final int REQUEST = 2; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3686 | |
Hugo Benichi | 6f260f3 | 2017-02-03 14:18:44 +0900 | [diff] [blame] | 3687 | private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, NetworkCallback callback, |
| 3688 | int timeoutMs, int action, int legacyType, CallbackHandler handler) { |
Soi, Yoshinari | dee2aa4 | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 3689 | printStackTrace(); |
Hugo Benichi | e767851 | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 3690 | checkCallbackNotNull(callback); |
Hugo Benichi | dafed3d | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 3691 | Preconditions.checkArgument(action == REQUEST || need != null, "null NetworkCapabilities"); |
Hugo Benichi | d42650f | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3692 | final NetworkRequest request; |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 3693 | final String callingPackageName = mContext.getOpPackageName(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3694 | try { |
Hugo Benichi | d42650f | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3695 | synchronized(sCallbacks) { |
Hugo Benichi | 31c176d | 2017-06-17 13:14:12 +0900 | [diff] [blame] | 3696 | if (callback.networkRequest != null |
| 3697 | && callback.networkRequest != ALREADY_UNREGISTERED) { |
Hugo Benichi | dafed3d | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 3698 | // TODO: throw exception instead and enforce 1:1 mapping of callbacks |
| 3699 | // and requests (http://b/20701525). |
| 3700 | Log.e(TAG, "NetworkCallback was already registered"); |
| 3701 | } |
Hugo Benichi | 7724cdd | 2016-07-07 10:15:56 +0900 | [diff] [blame] | 3702 | Messenger messenger = new Messenger(handler); |
Hugo Benichi | d42650f | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3703 | Binder binder = new Binder(); |
Paul Jensen | 7221cc3 | 2014-06-27 11:05:32 -0400 | [diff] [blame] | 3704 | if (action == LISTEN) { |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 3705 | request = mService.listenForNetwork( |
| 3706 | need, messenger, binder, callingPackageName); |
Paul Jensen | 7221cc3 | 2014-06-27 11:05:32 -0400 | [diff] [blame] | 3707 | } else { |
Hugo Benichi | d42650f | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3708 | request = mService.requestNetwork( |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 3709 | need, messenger, timeoutMs, binder, legacyType, callingPackageName); |
Paul Jensen | 7221cc3 | 2014-06-27 11:05:32 -0400 | [diff] [blame] | 3710 | } |
Hugo Benichi | d42650f | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3711 | if (request != null) { |
Hugo Benichi | 7724cdd | 2016-07-07 10:15:56 +0900 | [diff] [blame] | 3712 | sCallbacks.put(request, callback); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3713 | } |
Hugo Benichi | 7724cdd | 2016-07-07 10:15:56 +0900 | [diff] [blame] | 3714 | callback.networkRequest = request; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3715 | } |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3716 | } catch (RemoteException e) { |
| 3717 | throw e.rethrowFromSystemServer(); |
Hugo Benichi | cb88323 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 3718 | } catch (ServiceSpecificException e) { |
| 3719 | throw convertServiceException(e); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3720 | } |
Hugo Benichi | d42650f | 2016-07-06 22:53:17 +0900 | [diff] [blame] | 3721 | return request; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3722 | } |
| 3723 | |
| 3724 | /** |
Erik Kline | a2d2940 | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 3725 | * Helper function to request a network with a particular legacy type. |
Lorenzo Colitti | 7de289f | 2015-11-25 12:00:52 +0900 | [diff] [blame] | 3726 | * |
markchien | 97c029e | 2020-03-18 21:16:15 +0800 | [diff] [blame] | 3727 | * This API is only for use in internal system code that requests networks with legacy type and |
| 3728 | * relies on CONNECTIVITY_ACTION broadcasts instead of NetworkCallbacks. New caller should use |
markchien | ace1d34 | 2020-01-14 12:54:40 +0800 | [diff] [blame] | 3729 | * {@link #requestNetwork(NetworkRequest, NetworkCallback, Handler)} instead. |
Lorenzo Colitti | 7de289f | 2015-11-25 12:00:52 +0900 | [diff] [blame] | 3730 | * |
markchien | ace1d34 | 2020-01-14 12:54:40 +0800 | [diff] [blame] | 3731 | * @param request {@link NetworkRequest} describing this request. |
markchien | ace1d34 | 2020-01-14 12:54:40 +0800 | [diff] [blame] | 3732 | * @param timeoutMs The time in milliseconds to attempt looking for a suitable network |
| 3733 | * before {@link NetworkCallback#onUnavailable()} is called. The timeout must |
| 3734 | * be a positive value (i.e. >0). |
| 3735 | * @param legacyType to specify the network type(#TYPE_*). |
| 3736 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
markchien | 97c029e | 2020-03-18 21:16:15 +0800 | [diff] [blame] | 3737 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 3738 | * the callback must not be shared - it uniquely specifies this request. |
Lorenzo Colitti | 7de289f | 2015-11-25 12:00:52 +0900 | [diff] [blame] | 3739 | * |
| 3740 | * @hide |
| 3741 | */ |
markchien | ace1d34 | 2020-01-14 12:54:40 +0800 | [diff] [blame] | 3742 | @SystemApi |
markchien | 97c029e | 2020-03-18 21:16:15 +0800 | [diff] [blame] | 3743 | @RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK) |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 3744 | public void requestNetwork(@NonNull NetworkRequest request, |
markchien | 97c029e | 2020-03-18 21:16:15 +0800 | [diff] [blame] | 3745 | int timeoutMs, int legacyType, @NonNull Handler handler, |
| 3746 | @NonNull NetworkCallback networkCallback) { |
| 3747 | if (legacyType == TYPE_NONE) { |
| 3748 | throw new IllegalArgumentException("TYPE_NONE is meaningless legacy type"); |
| 3749 | } |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3750 | CallbackHandler cbHandler = new CallbackHandler(handler); |
| 3751 | NetworkCapabilities nc = request.networkCapabilities; |
| 3752 | sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, legacyType, cbHandler); |
Lorenzo Colitti | 7de289f | 2015-11-25 12:00:52 +0900 | [diff] [blame] | 3753 | } |
| 3754 | |
| 3755 | /** |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 3756 | * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3757 | * |
Chalard Jean | 599ce75 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3758 | * <p>This method will attempt to find the best network that matches the passed |
| 3759 | * {@link NetworkRequest}, and to bring up one that does if none currently satisfies the |
| 3760 | * criteria. The platform will evaluate which network is the best at its own discretion. |
| 3761 | * Throughput, latency, cost per byte, policy, user preference and other considerations |
| 3762 | * may be factored in the decision of what is considered the best network. |
| 3763 | * |
| 3764 | * <p>As long as this request is outstanding, the platform will try to maintain the best network |
| 3765 | * matching this request, while always attempting to match the request to a better network if |
| 3766 | * possible. If a better match is found, the platform will switch this request to the now-best |
| 3767 | * network and inform the app of the newly best network by invoking |
| 3768 | * {@link NetworkCallback#onAvailable(Network)} on the provided callback. Note that the platform |
| 3769 | * will not try to maintain any other network than the best one currently matching the request: |
| 3770 | * a network not matching any network request may be disconnected at any time. |
| 3771 | * |
| 3772 | * <p>For example, an application could use this method to obtain a connected cellular network |
| 3773 | * even if the device currently has a data connection over Ethernet. This may cause the cellular |
| 3774 | * radio to consume additional power. Or, an application could inform the system that it wants |
| 3775 | * a network supporting sending MMSes and have the system let it know about the currently best |
| 3776 | * MMS-supporting network through the provided {@link NetworkCallback}. |
| 3777 | * |
| 3778 | * <p>The status of the request can be followed by listening to the various callbacks described |
| 3779 | * in {@link NetworkCallback}. The {@link Network} object passed to the callback methods can be |
| 3780 | * used to direct traffic to the network (although accessing some networks may be subject to |
| 3781 | * holding specific permissions). Callers will learn about the specific characteristics of the |
| 3782 | * network through |
| 3783 | * {@link NetworkCallback#onCapabilitiesChanged(Network, NetworkCapabilities)} and |
| 3784 | * {@link NetworkCallback#onLinkPropertiesChanged(Network, LinkProperties)}. The methods of the |
| 3785 | * provided {@link NetworkCallback} will only be invoked due to changes in the best network |
| 3786 | * matching the request at any given time; therefore when a better network matching the request |
| 3787 | * becomes available, the {@link NetworkCallback#onAvailable(Network)} method is called |
| 3788 | * with the new network after which no further updates are given about the previously-best |
| 3789 | * network, unless it becomes the best again at some later time. All callbacks are invoked |
| 3790 | * in order on the same thread, which by default is a thread created by the framework running |
| 3791 | * in the app. |
| 3792 | * {@see #requestNetwork(NetworkRequest, NetworkCallback, Handler)} to change where the |
| 3793 | * callbacks are invoked. |
| 3794 | * |
| 3795 | * <p>This{@link NetworkRequest} will live until released via |
| 3796 | * {@link #unregisterNetworkCallback(NetworkCallback)} or the calling application exits, at |
| 3797 | * which point the system may let go of the network at any time. |
| 3798 | * |
| 3799 | * <p>A version of this method which takes a timeout is |
| 3800 | * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)}, that an app can use to only |
| 3801 | * wait for a limited amount of time for the network to become unavailable. |
| 3802 | * |
Paul Jensen | bb2e0e9 | 2015-06-16 15:11:58 -0400 | [diff] [blame] | 3803 | * <p>It is presently unsupported to request a network with mutable |
| 3804 | * {@link NetworkCapabilities} such as |
| 3805 | * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or |
| 3806 | * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL} |
| 3807 | * as these {@code NetworkCapabilities} represent states that a particular |
| 3808 | * network may never attain, and whether a network will attain these states |
| 3809 | * is unknown prior to bringing up the network so the framework does not |
Chalard Jean | 599ce75 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3810 | * know how to go about satisfying a request with these capabilities. |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 3811 | * |
| 3812 | * <p>This method requires the caller to hold either the |
| 3813 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 3814 | * or the ability to modify system settings as determined by |
| 3815 | * {@link android.provider.Settings.System#canWrite}.</p> |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3816 | * |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3817 | * @param request {@link NetworkRequest} describing this request. |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3818 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 3819 | * the callback must not be shared - it uniquely specifies this request. |
| 3820 | * The callback is invoked on the default internal Handler. |
Chalard Jean | 92c8b67 | 2019-05-13 15:13:58 +0900 | [diff] [blame] | 3821 | * @throws IllegalArgumentException if {@code request} contains invalid network capabilities. |
| 3822 | * @throws SecurityException if missing the appropriate permissions. |
| 3823 | * @throws RuntimeException if request limit per UID is exceeded. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3824 | */ |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 3825 | public void requestNetwork(@NonNull NetworkRequest request, |
| 3826 | @NonNull NetworkCallback networkCallback) { |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3827 | requestNetwork(request, networkCallback, getDefaultHandler()); |
| 3828 | } |
| 3829 | |
| 3830 | /** |
| 3831 | * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}. |
| 3832 | * |
Chalard Jean | 599ce75 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3833 | * This method behaves identically to {@link #requestNetwork(NetworkRequest, NetworkCallback)} |
| 3834 | * but runs all the callbacks on the passed Handler. |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3835 | * |
Chalard Jean | 599ce75 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3836 | * <p>This method has the same permission requirements as |
| 3837 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and throws the same exceptions in |
| 3838 | * the same conditions. |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3839 | * |
| 3840 | * @param request {@link NetworkRequest} describing this request. |
| 3841 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 3842 | * the callback must not be shared - it uniquely specifies this request. |
| 3843 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3844 | */ |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 3845 | public void requestNetwork(@NonNull NetworkRequest request, |
| 3846 | @NonNull NetworkCallback networkCallback, @NonNull Handler handler) { |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3847 | CallbackHandler cbHandler = new CallbackHandler(handler); |
markchien | 97c029e | 2020-03-18 21:16:15 +0800 | [diff] [blame] | 3848 | NetworkCapabilities nc = request.networkCapabilities; |
| 3849 | sendRequestForNetwork(nc, networkCallback, 0, REQUEST, TYPE_NONE, cbHandler); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3850 | } |
| 3851 | |
| 3852 | /** |
Etan Cohen | aebf17e | 2017-03-01 12:47:28 -0800 | [diff] [blame] | 3853 | * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited |
| 3854 | * by a timeout. |
| 3855 | * |
| 3856 | * This function behaves identically to the non-timed-out version |
| 3857 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, but if a suitable network |
| 3858 | * is not found within the given time (in milliseconds) the |
| 3859 | * {@link NetworkCallback#onUnavailable()} callback is called. The request can still be |
| 3860 | * released normally by calling {@link #unregisterNetworkCallback(NetworkCallback)} but does |
| 3861 | * not have to be released if timed-out (it is automatically released). Unregistering a |
| 3862 | * request that timed out is not an error. |
| 3863 | * |
| 3864 | * <p>Do not use this method to poll for the existence of specific networks (e.g. with a small |
| 3865 | * timeout) - {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} is provided |
| 3866 | * for that purpose. Calling this method will attempt to bring up the requested network. |
| 3867 | * |
Chalard Jean | 599ce75 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3868 | * <p>This method has the same permission requirements as |
| 3869 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and throws the same exceptions in |
| 3870 | * the same conditions. |
Etan Cohen | aebf17e | 2017-03-01 12:47:28 -0800 | [diff] [blame] | 3871 | * |
| 3872 | * @param request {@link NetworkRequest} describing this request. |
Lorenzo Colitti | 15fd439 | 2017-04-28 00:56:30 +0900 | [diff] [blame] | 3873 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 3874 | * the callback must not be shared - it uniquely specifies this request. |
Etan Cohen | aebf17e | 2017-03-01 12:47:28 -0800 | [diff] [blame] | 3875 | * @param timeoutMs The time in milliseconds to attempt looking for a suitable network |
| 3876 | * before {@link NetworkCallback#onUnavailable()} is called. The timeout must |
| 3877 | * be a positive value (i.e. >0). |
Etan Cohen | aebf17e | 2017-03-01 12:47:28 -0800 | [diff] [blame] | 3878 | */ |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 3879 | public void requestNetwork(@NonNull NetworkRequest request, |
| 3880 | @NonNull NetworkCallback networkCallback, int timeoutMs) { |
Hugo Benichi | e767851 | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 3881 | checkTimeout(timeoutMs); |
markchien | 97c029e | 2020-03-18 21:16:15 +0800 | [diff] [blame] | 3882 | NetworkCapabilities nc = request.networkCapabilities; |
| 3883 | sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, TYPE_NONE, |
| 3884 | getDefaultHandler()); |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3885 | } |
| 3886 | |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3887 | /** |
| 3888 | * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}, limited |
| 3889 | * by a timeout. |
| 3890 | * |
Chalard Jean | 599ce75 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3891 | * This method behaves identically to |
| 3892 | * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} but runs all the callbacks |
| 3893 | * on the passed Handler. |
Etan Cohen | aebf17e | 2017-03-01 12:47:28 -0800 | [diff] [blame] | 3894 | * |
Chalard Jean | 599ce75 | 2019-08-30 16:27:28 +0900 | [diff] [blame] | 3895 | * <p>This method has the same permission requirements as |
| 3896 | * {@link #requestNetwork(NetworkRequest, NetworkCallback, int)} and throws the same exceptions |
| 3897 | * in the same conditions. |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3898 | * |
| 3899 | * @param request {@link NetworkRequest} describing this request. |
Etan Cohen | aebf17e | 2017-03-01 12:47:28 -0800 | [diff] [blame] | 3900 | * @param networkCallback The {@link NetworkCallback} to be utilized for this request. Note |
| 3901 | * the callback must not be shared - it uniquely specifies this request. |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3902 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
Lorenzo Colitti | 15fd439 | 2017-04-28 00:56:30 +0900 | [diff] [blame] | 3903 | * @param timeoutMs The time in milliseconds to attempt looking for a suitable network |
| 3904 | * before {@link NetworkCallback#onUnavailable} is called. |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3905 | */ |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 3906 | public void requestNetwork(@NonNull NetworkRequest request, |
| 3907 | @NonNull NetworkCallback networkCallback, @NonNull Handler handler, int timeoutMs) { |
Hugo Benichi | e767851 | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 3908 | checkTimeout(timeoutMs); |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 3909 | CallbackHandler cbHandler = new CallbackHandler(handler); |
markchien | 97c029e | 2020-03-18 21:16:15 +0800 | [diff] [blame] | 3910 | NetworkCapabilities nc = request.networkCapabilities; |
| 3911 | sendRequestForNetwork(nc, networkCallback, timeoutMs, REQUEST, TYPE_NONE, cbHandler); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3912 | } |
| 3913 | |
| 3914 | /** |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3915 | * The lookup key for a {@link Network} object included with the intent after |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3916 | * successfully finding a network for the applications request. Retrieve it with |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3917 | * {@link android.content.Intent#getParcelableExtra(String)}. |
Jeremy Joslin | fcde58f | 2015-02-11 16:51:13 -0800 | [diff] [blame] | 3918 | * <p> |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 3919 | * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)} |
| 3920 | * then you must get a ConnectivityManager instance before doing so. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3921 | */ |
Erik Kline | 90e9307 | 2014-11-19 12:12:24 +0900 | [diff] [blame] | 3922 | public static final String EXTRA_NETWORK = "android.net.extra.NETWORK"; |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3923 | |
| 3924 | /** |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3925 | * The lookup key for a {@link NetworkRequest} object included with the intent after |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3926 | * successfully finding a network for the applications request. Retrieve it with |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3927 | * {@link android.content.Intent#getParcelableExtra(String)}. |
| 3928 | */ |
Erik Kline | 90e9307 | 2014-11-19 12:12:24 +0900 | [diff] [blame] | 3929 | public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST"; |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3930 | |
| 3931 | |
| 3932 | /** |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 3933 | * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3934 | * |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3935 | * This function behaves identically to the version that takes a NetworkCallback, but instead |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3936 | * of {@link NetworkCallback} a {@link PendingIntent} is used. This means |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3937 | * the request may outlive the calling application and get called back when a suitable |
| 3938 | * network is found. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3939 | * <p> |
| 3940 | * The operation is an Intent broadcast that goes to a broadcast receiver that |
| 3941 | * you registered with {@link Context#registerReceiver} or through the |
| 3942 | * <receiver> tag in an AndroidManifest.xml file |
| 3943 | * <p> |
| 3944 | * The operation Intent is delivered with two extras, a {@link Network} typed |
Erik Kline | 90e9307 | 2014-11-19 12:12:24 +0900 | [diff] [blame] | 3945 | * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest} |
| 3946 | * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3947 | * the original requests parameters. It is important to create a new, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3948 | * {@link NetworkCallback} based request before completing the processing of the |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3949 | * Intent to reserve the network or it will be released shortly after the Intent |
| 3950 | * is processed. |
| 3951 | * <p> |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 3952 | * If there is already a request for this Intent registered (with the equality of |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3953 | * two Intents defined by {@link Intent#filterEquals}), then it will be removed and |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 3954 | * replaced by this one, effectively releasing the previous {@link NetworkRequest}. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3955 | * <p> |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3956 | * The request may be released normally by calling |
| 3957 | * {@link #releaseNetworkRequest(android.app.PendingIntent)}. |
Paul Jensen | bb2e0e9 | 2015-06-16 15:11:58 -0400 | [diff] [blame] | 3958 | * <p>It is presently unsupported to request a network with either |
| 3959 | * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or |
| 3960 | * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL} |
| 3961 | * as these {@code NetworkCapabilities} represent states that a particular |
| 3962 | * network may never attain, and whether a network will attain these states |
| 3963 | * is unknown prior to bringing up the network so the framework does not |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 3964 | * know how to go about satisfying a request with these capabilities. |
Lorenzo Colitti | d542705 | 2015-10-15 16:29:00 +0900 | [diff] [blame] | 3965 | * |
| 3966 | * <p>This method requires the caller to hold either the |
| 3967 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE} permission |
| 3968 | * or the ability to modify system settings as determined by |
| 3969 | * {@link android.provider.Settings.System#canWrite}.</p> |
| 3970 | * |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3971 | * @param request {@link NetworkRequest} describing this request. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3972 | * @param operation Action to perform when the network is available (corresponds |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 3973 | * to the {@link NetworkCallback#onAvailable} call. Typically |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3974 | * comes from {@link PendingIntent#getBroadcast}. Cannot be null. |
Chalard Jean | 92c8b67 | 2019-05-13 15:13:58 +0900 | [diff] [blame] | 3975 | * @throws IllegalArgumentException if {@code request} contains invalid network capabilities. |
| 3976 | * @throws SecurityException if missing the appropriate permissions. |
| 3977 | * @throws RuntimeException if request limit per UID is exceeded. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3978 | */ |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 3979 | public void requestNetwork(@NonNull NetworkRequest request, |
| 3980 | @NonNull PendingIntent operation) { |
Soi, Yoshinari | dee2aa4 | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 3981 | printStackTrace(); |
Hugo Benichi | e767851 | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 3982 | checkPendingIntentNotNull(operation); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3983 | try { |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 3984 | mService.pendingRequestForNetwork( |
Qingxi Li | 9c5d8b9 | 2020-01-08 12:51:49 -0800 | [diff] [blame] | 3985 | request.networkCapabilities, operation, mContext.getOpPackageName()); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3986 | } catch (RemoteException e) { |
| 3987 | throw e.rethrowFromSystemServer(); |
Hugo Benichi | cb88323 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 3988 | } catch (ServiceSpecificException e) { |
| 3989 | throw convertServiceException(e); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 3990 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 3991 | } |
| 3992 | |
| 3993 | /** |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3994 | * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} |
| 3995 | * <p> |
Lorenzo Colitti | 88bc0bb | 2016-04-13 22:00:02 +0900 | [diff] [blame] | 3996 | * This method has the same behavior as |
| 3997 | * {@link #unregisterNetworkCallback(android.app.PendingIntent)} with respect to |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 3998 | * releasing network resources and disconnecting. |
| 3999 | * |
| 4000 | * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the |
| 4001 | * PendingIntent passed to |
| 4002 | * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the |
| 4003 | * corresponding NetworkRequest you'd like to remove. Cannot be null. |
| 4004 | */ |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4005 | public void releaseNetworkRequest(@NonNull PendingIntent operation) { |
Soi, Yoshinari | dee2aa4 | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 4006 | printStackTrace(); |
Hugo Benichi | e767851 | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 4007 | checkPendingIntentNotNull(operation); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4008 | try { |
| 4009 | mService.releasePendingNetworkRequest(operation); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4010 | } catch (RemoteException e) { |
| 4011 | throw e.rethrowFromSystemServer(); |
| 4012 | } |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4013 | } |
| 4014 | |
Hugo Benichi | e767851 | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 4015 | private static void checkPendingIntentNotNull(PendingIntent intent) { |
| 4016 | Preconditions.checkNotNull(intent, "PendingIntent cannot be null."); |
| 4017 | } |
| 4018 | |
| 4019 | private static void checkCallbackNotNull(NetworkCallback callback) { |
| 4020 | Preconditions.checkNotNull(callback, "null NetworkCallback"); |
| 4021 | } |
| 4022 | |
| 4023 | private static void checkTimeout(int timeoutMs) { |
| 4024 | Preconditions.checkArgumentPositive(timeoutMs, "timeoutMs must be strictly positive."); |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 4025 | } |
| 4026 | |
| 4027 | /** |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4028 | * Registers to receive notifications about all networks which satisfy the given |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 4029 | * {@link NetworkRequest}. The callbacks will continue to be called until |
Chiachang Wang | 9088225 | 2019-02-27 17:14:50 +0800 | [diff] [blame] | 4030 | * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is |
| 4031 | * called. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4032 | * |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 4033 | * @param request {@link NetworkRequest} describing this request. |
| 4034 | * @param networkCallback The {@link NetworkCallback} that the system will call as suitable |
| 4035 | * networks change state. |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4036 | * The callback is invoked on the default internal Handler. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4037 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 4038 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4039 | public void registerNetworkCallback(@NonNull NetworkRequest request, |
| 4040 | @NonNull NetworkCallback networkCallback) { |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4041 | registerNetworkCallback(request, networkCallback, getDefaultHandler()); |
| 4042 | } |
| 4043 | |
| 4044 | /** |
| 4045 | * Registers to receive notifications about all networks which satisfy the given |
| 4046 | * {@link NetworkRequest}. The callbacks will continue to be called until |
Chiachang Wang | 9088225 | 2019-02-27 17:14:50 +0800 | [diff] [blame] | 4047 | * either the application exits or {@link #unregisterNetworkCallback(NetworkCallback)} is |
| 4048 | * called. |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4049 | * |
| 4050 | * @param request {@link NetworkRequest} describing this request. |
| 4051 | * @param networkCallback The {@link NetworkCallback} that the system will call as suitable |
| 4052 | * networks change state. |
| 4053 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4054 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 4055 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4056 | public void registerNetworkCallback(@NonNull NetworkRequest request, |
| 4057 | @NonNull NetworkCallback networkCallback, @NonNull Handler handler) { |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4058 | CallbackHandler cbHandler = new CallbackHandler(handler); |
| 4059 | NetworkCapabilities nc = request.networkCapabilities; |
| 4060 | sendRequestForNetwork(nc, networkCallback, 0, LISTEN, TYPE_NONE, cbHandler); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4061 | } |
| 4062 | |
| 4063 | /** |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 4064 | * Registers a PendingIntent to be sent when a network is available which satisfies the given |
| 4065 | * {@link NetworkRequest}. |
| 4066 | * |
| 4067 | * This function behaves identically to the version that takes a NetworkCallback, but instead |
| 4068 | * of {@link NetworkCallback} a {@link PendingIntent} is used. This means |
| 4069 | * the request may outlive the calling application and get called back when a suitable |
| 4070 | * network is found. |
| 4071 | * <p> |
| 4072 | * The operation is an Intent broadcast that goes to a broadcast receiver that |
| 4073 | * you registered with {@link Context#registerReceiver} or through the |
| 4074 | * <receiver> tag in an AndroidManifest.xml file |
| 4075 | * <p> |
| 4076 | * The operation Intent is delivered with two extras, a {@link Network} typed |
| 4077 | * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest} |
| 4078 | * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing |
| 4079 | * the original requests parameters. |
| 4080 | * <p> |
| 4081 | * If there is already a request for this Intent registered (with the equality of |
| 4082 | * two Intents defined by {@link Intent#filterEquals}), then it will be removed and |
| 4083 | * replaced by this one, effectively releasing the previous {@link NetworkRequest}. |
| 4084 | * <p> |
| 4085 | * The request may be released normally by calling |
Paul Jensen | f2c1cfe | 2015-06-30 14:29:18 -0400 | [diff] [blame] | 4086 | * {@link #unregisterNetworkCallback(android.app.PendingIntent)}. |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 4087 | * @param request {@link NetworkRequest} describing this request. |
| 4088 | * @param operation Action to perform when the network is available (corresponds |
| 4089 | * to the {@link NetworkCallback#onAvailable} call. Typically |
| 4090 | * comes from {@link PendingIntent#getBroadcast}. Cannot be null. |
| 4091 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 4092 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4093 | public void registerNetworkCallback(@NonNull NetworkRequest request, |
| 4094 | @NonNull PendingIntent operation) { |
Soi, Yoshinari | dee2aa4 | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 4095 | printStackTrace(); |
Hugo Benichi | e767851 | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 4096 | checkPendingIntentNotNull(operation); |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 4097 | try { |
Roshan Pius | e38acab | 2020-01-16 12:17:17 -0800 | [diff] [blame] | 4098 | mService.pendingListenForNetwork( |
Qingxi Li | 9c5d8b9 | 2020-01-08 12:51:49 -0800 | [diff] [blame] | 4099 | request.networkCapabilities, operation, mContext.getOpPackageName()); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4100 | } catch (RemoteException e) { |
| 4101 | throw e.rethrowFromSystemServer(); |
Hugo Benichi | cb88323 | 2017-05-11 13:16:17 +0900 | [diff] [blame] | 4102 | } catch (ServiceSpecificException e) { |
| 4103 | throw convertServiceException(e); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4104 | } |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 4105 | } |
| 4106 | |
| 4107 | /** |
Lorenzo Colitti | 88bc0bb | 2016-04-13 22:00:02 +0900 | [diff] [blame] | 4108 | * Registers to receive notifications about changes in the system default network. The callbacks |
| 4109 | * will continue to be called until either the application exits or |
| 4110 | * {@link #unregisterNetworkCallback(NetworkCallback)} is called. |
Erik Kline | a2d2940 | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 4111 | * |
| 4112 | * @param networkCallback The {@link NetworkCallback} that the system will call as the |
| 4113 | * system default network changes. |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4114 | * The callback is invoked on the default internal Handler. |
Erik Kline | a2d2940 | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 4115 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 4116 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4117 | public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback) { |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4118 | registerDefaultNetworkCallback(networkCallback, getDefaultHandler()); |
| 4119 | } |
| 4120 | |
| 4121 | /** |
| 4122 | * Registers to receive notifications about changes in the system default network. The callbacks |
| 4123 | * will continue to be called until either the application exits or |
| 4124 | * {@link #unregisterNetworkCallback(NetworkCallback)} is called. |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4125 | * |
| 4126 | * @param networkCallback The {@link NetworkCallback} that the system will call as the |
| 4127 | * system default network changes. |
| 4128 | * @param handler {@link Handler} to specify the thread upon which the callback will be invoked. |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4129 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 4130 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4131 | public void registerDefaultNetworkCallback(@NonNull NetworkCallback networkCallback, |
| 4132 | @NonNull Handler handler) { |
Erik Kline | a2d2940 | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 4133 | // This works because if the NetworkCapabilities are null, |
| 4134 | // ConnectivityService takes them from the default request. |
| 4135 | // |
| 4136 | // Since the capabilities are exactly the same as the default request's |
| 4137 | // capabilities, this request is guaranteed, at all times, to be |
| 4138 | // satisfied by the same network, if any, that satisfies the default |
| 4139 | // request, i.e., the system default network. |
Hugo Benichi | 2583ef0 | 2017-02-02 17:02:36 +0900 | [diff] [blame] | 4140 | CallbackHandler cbHandler = new CallbackHandler(handler); |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 4141 | sendRequestForNetwork(null /* NetworkCapabilities need */, networkCallback, 0, |
| 4142 | REQUEST, TYPE_NONE, cbHandler); |
Erik Kline | a2d2940 | 2016-03-16 15:31:39 +0900 | [diff] [blame] | 4143 | } |
| 4144 | |
| 4145 | /** |
fenglu | db57147 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 4146 | * Requests bandwidth update for a given {@link Network} and returns whether the update request |
| 4147 | * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying |
| 4148 | * network connection for updated bandwidth information. The caller will be notified via |
| 4149 | * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this |
Lorenzo Colitti | 88bc0bb | 2016-04-13 22:00:02 +0900 | [diff] [blame] | 4150 | * method assumes that the caller has previously called |
| 4151 | * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} to listen for network |
| 4152 | * changes. |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 4153 | * |
fenglu | ae51919 | 2015-04-27 14:28:04 -0700 | [diff] [blame] | 4154 | * @param network {@link Network} specifying which network you're interested. |
fenglu | db57147 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 4155 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 4156 | */ |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4157 | public boolean requestBandwidthUpdate(@NonNull Network network) { |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 4158 | try { |
fenglu | db57147 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 4159 | return mService.requestBandwidthUpdate(network); |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 4160 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4161 | throw e.rethrowFromSystemServer(); |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 4162 | } |
| 4163 | } |
| 4164 | |
| 4165 | /** |
Hugo Benichi | dafed3d | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 4166 | * Unregisters a {@code NetworkCallback} and possibly releases networks originating from |
Lorenzo Colitti | 88bc0bb | 2016-04-13 22:00:02 +0900 | [diff] [blame] | 4167 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and |
| 4168 | * {@link #registerNetworkCallback(NetworkRequest, NetworkCallback)} calls. |
| 4169 | * If the given {@code NetworkCallback} had previously been used with |
Lorenzo Colitti | 2ea89e5 | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 4170 | * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request |
| 4171 | * will be disconnected. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4172 | * |
Hugo Benichi | dafed3d | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 4173 | * Notifications that would have triggered that {@code NetworkCallback} will immediately stop |
| 4174 | * triggering it as soon as this call returns. |
| 4175 | * |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 4176 | * @param networkCallback The {@link NetworkCallback} used when making the request. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4177 | */ |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4178 | public void unregisterNetworkCallback(@NonNull NetworkCallback networkCallback) { |
Soi, Yoshinari | dee2aa4 | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 4179 | printStackTrace(); |
Hugo Benichi | e767851 | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 4180 | checkCallbackNotNull(networkCallback); |
Hugo Benichi | dafed3d | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 4181 | final List<NetworkRequest> reqs = new ArrayList<>(); |
| 4182 | // Find all requests associated to this callback and stop callback triggers immediately. |
| 4183 | // Callback is reusable immediately. http://b/20701525, http://b/35921499. |
| 4184 | synchronized (sCallbacks) { |
Hugo Benichi | 31c176d | 2017-06-17 13:14:12 +0900 | [diff] [blame] | 4185 | Preconditions.checkArgument(networkCallback.networkRequest != null, |
| 4186 | "NetworkCallback was not registered"); |
Etan Cohen | 9b09a16 | 2019-04-16 15:07:55 -0700 | [diff] [blame] | 4187 | if (networkCallback.networkRequest == ALREADY_UNREGISTERED) { |
| 4188 | Log.d(TAG, "NetworkCallback was already unregistered"); |
| 4189 | return; |
| 4190 | } |
Hugo Benichi | dafed3d | 2017-03-06 09:17:06 +0900 | [diff] [blame] | 4191 | for (Map.Entry<NetworkRequest, NetworkCallback> e : sCallbacks.entrySet()) { |
| 4192 | if (e.getValue() == networkCallback) { |
| 4193 | reqs.add(e.getKey()); |
| 4194 | } |
| 4195 | } |
| 4196 | // TODO: throw exception if callback was registered more than once (http://b/20701525). |
| 4197 | for (NetworkRequest r : reqs) { |
| 4198 | try { |
| 4199 | mService.releaseNetworkRequest(r); |
| 4200 | } catch (RemoteException e) { |
| 4201 | throw e.rethrowFromSystemServer(); |
| 4202 | } |
| 4203 | // Only remove mapping if rpc was successful. |
| 4204 | sCallbacks.remove(r); |
| 4205 | } |
Hugo Benichi | 31c176d | 2017-06-17 13:14:12 +0900 | [diff] [blame] | 4206 | networkCallback.networkRequest = ALREADY_UNREGISTERED; |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4207 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 4208 | } |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4209 | |
| 4210 | /** |
Paul Jensen | f2c1cfe | 2015-06-30 14:29:18 -0400 | [diff] [blame] | 4211 | * Unregisters a callback previously registered via |
| 4212 | * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}. |
| 4213 | * |
| 4214 | * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the |
| 4215 | * PendingIntent passed to |
| 4216 | * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}. |
| 4217 | * Cannot be null. |
| 4218 | */ |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4219 | public void unregisterNetworkCallback(@NonNull PendingIntent operation) { |
Hugo Benichi | e767851 | 2017-05-09 15:19:01 +0900 | [diff] [blame] | 4220 | checkPendingIntentNotNull(operation); |
Paul Jensen | f2c1cfe | 2015-06-30 14:29:18 -0400 | [diff] [blame] | 4221 | releaseNetworkRequest(operation); |
| 4222 | } |
| 4223 | |
| 4224 | /** |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 4225 | * Informs the system whether it should switch to {@code network} regardless of whether it is |
| 4226 | * validated or not. If {@code accept} is true, and the network was explicitly selected by the |
| 4227 | * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become |
| 4228 | * the system default network regardless of any other network that's currently connected. If |
| 4229 | * {@code always} is true, then the choice is remembered, so that the next time the user |
| 4230 | * connects to this network, the system will switch to it. |
| 4231 | * |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 4232 | * @param network The network to accept. |
| 4233 | * @param accept Whether to accept the network even if unvalidated. |
| 4234 | * @param always Whether to remember this choice in the future. |
| 4235 | * |
| 4236 | * @hide |
| 4237 | */ |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 4238 | @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS) |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 4239 | public void setAcceptUnvalidated(Network network, boolean accept, boolean always) { |
| 4240 | try { |
| 4241 | mService.setAcceptUnvalidated(network, accept, always); |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4242 | } catch (RemoteException e) { |
| 4243 | throw e.rethrowFromSystemServer(); |
| 4244 | } |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 4245 | } |
| 4246 | |
| 4247 | /** |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 4248 | * Informs the system whether it should consider the network as validated even if it only has |
| 4249 | * partial connectivity. If {@code accept} is true, then the network will be considered as |
| 4250 | * validated even if connectivity is only partial. If {@code always} is true, then the choice |
| 4251 | * is remembered, so that the next time the user connects to this network, the system will |
| 4252 | * switch to it. |
| 4253 | * |
| 4254 | * @param network The network to accept. |
| 4255 | * @param accept Whether to consider the network as validated even if it has partial |
| 4256 | * connectivity. |
| 4257 | * @param always Whether to remember this choice in the future. |
| 4258 | * |
| 4259 | * @hide |
| 4260 | */ |
| 4261 | @RequiresPermission(android.Manifest.permission.NETWORK_STACK) |
| 4262 | public void setAcceptPartialConnectivity(Network network, boolean accept, boolean always) { |
| 4263 | try { |
| 4264 | mService.setAcceptPartialConnectivity(network, accept, always); |
| 4265 | } catch (RemoteException e) { |
| 4266 | throw e.rethrowFromSystemServer(); |
| 4267 | } |
| 4268 | } |
| 4269 | |
| 4270 | /** |
Lorenzo Colitti | 165c51c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 4271 | * Informs the system to penalize {@code network}'s score when it becomes unvalidated. This is |
| 4272 | * only meaningful if the system is configured not to penalize such networks, e.g., if the |
| 4273 | * {@code config_networkAvoidBadWifi} configuration variable is set to 0 and the {@code |
| 4274 | * NETWORK_AVOID_BAD_WIFI setting is unset}. |
| 4275 | * |
Lorenzo Colitti | 165c51c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 4276 | * @param network The network to accept. |
| 4277 | * |
| 4278 | * @hide |
| 4279 | */ |
lucaslin | e252a74 | 2019-03-12 13:08:03 +0800 | [diff] [blame] | 4280 | @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS) |
Lorenzo Colitti | 165c51c | 2016-09-19 01:00:19 +0900 | [diff] [blame] | 4281 | public void setAvoidUnvalidated(Network network) { |
| 4282 | try { |
| 4283 | mService.setAvoidUnvalidated(network); |
| 4284 | } catch (RemoteException e) { |
| 4285 | throw e.rethrowFromSystemServer(); |
| 4286 | } |
| 4287 | } |
| 4288 | |
| 4289 | /** |
Lorenzo Colitti | 4734cdb | 2017-04-27 14:30:21 +0900 | [diff] [blame] | 4290 | * Requests that the system open the captive portal app on the specified network. |
| 4291 | * |
| 4292 | * @param network The network to log into. |
| 4293 | * |
| 4294 | * @hide |
| 4295 | */ |
paulhu | a6af6b6 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 4296 | @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS) |
Lorenzo Colitti | 4734cdb | 2017-04-27 14:30:21 +0900 | [diff] [blame] | 4297 | public void startCaptivePortalApp(Network network) { |
| 4298 | try { |
| 4299 | mService.startCaptivePortalApp(network); |
| 4300 | } catch (RemoteException e) { |
| 4301 | throw e.rethrowFromSystemServer(); |
| 4302 | } |
| 4303 | } |
| 4304 | |
| 4305 | /** |
Remi NGUYEN VAN | dc48356 | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 4306 | * Requests that the system open the captive portal app with the specified extras. |
| 4307 | * |
| 4308 | * <p>This endpoint is exclusively for use by the NetworkStack and is protected by the |
| 4309 | * corresponding permission. |
Remi NGUYEN VAN | cfff01e | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 4310 | * @param network Network on which the captive portal was detected. |
Remi NGUYEN VAN | dc48356 | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 4311 | * @param appExtras Extras to include in the app start intent. |
| 4312 | * @hide |
| 4313 | */ |
| 4314 | @SystemApi |
| 4315 | @TestApi |
| 4316 | @RequiresPermission(NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK) |
paulhu | d9736de | 2019-03-08 16:35:20 +0800 | [diff] [blame] | 4317 | public void startCaptivePortalApp(@NonNull Network network, @NonNull Bundle appExtras) { |
Remi NGUYEN VAN | dc48356 | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 4318 | try { |
Remi NGUYEN VAN | cfff01e | 2019-02-13 20:58:59 +0900 | [diff] [blame] | 4319 | mService.startCaptivePortalAppInternal(network, appExtras); |
Remi NGUYEN VAN | dc48356 | 2019-02-04 11:32:20 +0900 | [diff] [blame] | 4320 | } catch (RemoteException e) { |
| 4321 | throw e.rethrowFromSystemServer(); |
| 4322 | } |
| 4323 | } |
| 4324 | |
| 4325 | /** |
Remi NGUYEN VAN | 97f69c2 | 2019-01-20 20:35:06 +0900 | [diff] [blame] | 4326 | * Determine whether the device is configured to avoid bad wifi. |
| 4327 | * @hide |
| 4328 | */ |
| 4329 | @SystemApi |
Remi NGUYEN VAN | e2365d6 | 2019-03-22 11:14:13 +0900 | [diff] [blame] | 4330 | @RequiresPermission(anyOf = { |
| 4331 | NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK, |
| 4332 | android.Manifest.permission.NETWORK_STACK}) |
| 4333 | public boolean shouldAvoidBadWifi() { |
Remi NGUYEN VAN | 97f69c2 | 2019-01-20 20:35:06 +0900 | [diff] [blame] | 4334 | try { |
Remi NGUYEN VAN | e2365d6 | 2019-03-22 11:14:13 +0900 | [diff] [blame] | 4335 | return mService.shouldAvoidBadWifi(); |
Remi NGUYEN VAN | 97f69c2 | 2019-01-20 20:35:06 +0900 | [diff] [blame] | 4336 | } catch (RemoteException e) { |
| 4337 | throw e.rethrowFromSystemServer(); |
| 4338 | } |
| 4339 | } |
| 4340 | |
| 4341 | /** |
Lorenzo Colitti | 2de4925 | 2017-01-24 18:08:41 +0900 | [diff] [blame] | 4342 | * It is acceptable to briefly use multipath data to provide seamless connectivity for |
| 4343 | * time-sensitive user-facing operations when the system default network is temporarily |
Lorenzo Colitti | 15fd439 | 2017-04-28 00:56:30 +0900 | [diff] [blame] | 4344 | * unresponsive. The amount of data should be limited (less than one megabyte for every call to |
| 4345 | * this method), and the operation should be infrequent to ensure that data usage is limited. |
Lorenzo Colitti | 2de4925 | 2017-01-24 18:08:41 +0900 | [diff] [blame] | 4346 | * |
| 4347 | * An example of such an operation might be a time-sensitive foreground activity, such as a |
| 4348 | * voice command, that the user is performing while walking out of range of a Wi-Fi network. |
| 4349 | */ |
| 4350 | public static final int MULTIPATH_PREFERENCE_HANDOVER = 1 << 0; |
| 4351 | |
| 4352 | /** |
| 4353 | * It is acceptable to use small amounts of multipath data on an ongoing basis to provide |
| 4354 | * a backup channel for traffic that is primarily going over another network. |
| 4355 | * |
| 4356 | * An example might be maintaining backup connections to peers or servers for the purpose of |
| 4357 | * fast fallback if the default network is temporarily unresponsive or disconnects. The traffic |
| 4358 | * on backup paths should be negligible compared to the traffic on the main path. |
| 4359 | */ |
| 4360 | public static final int MULTIPATH_PREFERENCE_RELIABILITY = 1 << 1; |
| 4361 | |
| 4362 | /** |
| 4363 | * It is acceptable to use metered data to improve network latency and performance. |
| 4364 | */ |
| 4365 | public static final int MULTIPATH_PREFERENCE_PERFORMANCE = 1 << 2; |
| 4366 | |
| 4367 | /** |
| 4368 | * Return value to use for unmetered networks. On such networks we currently set all the flags |
| 4369 | * to true. |
| 4370 | * @hide |
| 4371 | */ |
| 4372 | public static final int MULTIPATH_PREFERENCE_UNMETERED = |
| 4373 | MULTIPATH_PREFERENCE_HANDOVER | |
| 4374 | MULTIPATH_PREFERENCE_RELIABILITY | |
| 4375 | MULTIPATH_PREFERENCE_PERFORMANCE; |
| 4376 | |
| 4377 | /** @hide */ |
| 4378 | @Retention(RetentionPolicy.SOURCE) |
| 4379 | @IntDef(flag = true, value = { |
| 4380 | MULTIPATH_PREFERENCE_HANDOVER, |
| 4381 | MULTIPATH_PREFERENCE_RELIABILITY, |
| 4382 | MULTIPATH_PREFERENCE_PERFORMANCE, |
| 4383 | }) |
| 4384 | public @interface MultipathPreference { |
| 4385 | } |
| 4386 | |
| 4387 | /** |
| 4388 | * Provides a hint to the calling application on whether it is desirable to use the |
| 4389 | * multinetwork APIs (e.g., {@link Network#openConnection}, {@link Network#bindSocket}, etc.) |
| 4390 | * for multipath data transfer on this network when it is not the system default network. |
| 4391 | * Applications desiring to use multipath network protocols should call this method before |
| 4392 | * each such operation. |
Lorenzo Colitti | 2de4925 | 2017-01-24 18:08:41 +0900 | [diff] [blame] | 4393 | * |
| 4394 | * @param network The network on which the application desires to use multipath data. |
| 4395 | * If {@code null}, this method will return the a preference that will generally |
| 4396 | * apply to metered networks. |
| 4397 | * @return a bitwise OR of zero or more of the {@code MULTIPATH_PREFERENCE_*} constants. |
| 4398 | */ |
Jeff Sharkey | 30e06bb | 2017-04-24 11:18:03 -0600 | [diff] [blame] | 4399 | @RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE) |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4400 | public @MultipathPreference int getMultipathPreference(@Nullable Network network) { |
Lorenzo Colitti | 2de4925 | 2017-01-24 18:08:41 +0900 | [diff] [blame] | 4401 | try { |
| 4402 | return mService.getMultipathPreference(network); |
| 4403 | } catch (RemoteException e) { |
| 4404 | throw e.rethrowFromSystemServer(); |
| 4405 | } |
| 4406 | } |
| 4407 | |
| 4408 | /** |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 4409 | * Resets all connectivity manager settings back to factory defaults. |
| 4410 | * @hide |
| 4411 | */ |
paulhu | a6af6b6 | 2019-08-12 16:25:11 +0800 | [diff] [blame] | 4412 | @RequiresPermission(android.Manifest.permission.NETWORK_SETTINGS) |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 4413 | public void factoryReset() { |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 4414 | try { |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 4415 | mService.factoryReset(); |
Automerger Merge Worker | 6112c82 | 2020-03-06 00:38:43 +0000 | [diff] [blame] | 4416 | mTetheringManager.stopAllTethering(); |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 4417 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4418 | throw e.rethrowFromSystemServer(); |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 4419 | } |
| 4420 | } |
| 4421 | |
| 4422 | /** |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4423 | * Binds the current process to {@code network}. All Sockets created in the future |
| 4424 | * (and not explicitly bound via a bound SocketFactory from |
| 4425 | * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to |
| 4426 | * {@code network}. All host name resolutions will be limited to {@code network} as well. |
| 4427 | * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to |
| 4428 | * work and all host name resolutions will fail. This is by design so an application doesn't |
| 4429 | * accidentally use Sockets it thinks are still bound to a particular {@link Network}. |
| 4430 | * To clear binding pass {@code null} for {@code network}. Using individually bound |
| 4431 | * Sockets created by Network.getSocketFactory().createSocket() and |
| 4432 | * performing network-specific host name resolutions via |
| 4433 | * {@link Network#getAllByName Network.getAllByName} is preferred to calling |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 4434 | * {@code bindProcessToNetwork}. |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4435 | * |
| 4436 | * @param network The {@link Network} to bind the current process to, or {@code null} to clear |
| 4437 | * the current binding. |
| 4438 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
| 4439 | */ |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4440 | public boolean bindProcessToNetwork(@Nullable Network network) { |
Chalard Jean | 4d66011 | 2018-06-04 16:52:49 +0900 | [diff] [blame] | 4441 | // Forcing callers to call through non-static function ensures ConnectivityManager |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 4442 | // instantiated. |
| 4443 | return setProcessDefaultNetwork(network); |
| 4444 | } |
| 4445 | |
| 4446 | /** |
| 4447 | * Binds the current process to {@code network}. All Sockets created in the future |
| 4448 | * (and not explicitly bound via a bound SocketFactory from |
| 4449 | * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to |
| 4450 | * {@code network}. All host name resolutions will be limited to {@code network} as well. |
| 4451 | * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to |
| 4452 | * work and all host name resolutions will fail. This is by design so an application doesn't |
| 4453 | * accidentally use Sockets it thinks are still bound to a particular {@link Network}. |
| 4454 | * To clear binding pass {@code null} for {@code network}. Using individually bound |
| 4455 | * Sockets created by Network.getSocketFactory().createSocket() and |
| 4456 | * performing network-specific host name resolutions via |
| 4457 | * {@link Network#getAllByName Network.getAllByName} is preferred to calling |
| 4458 | * {@code setProcessDefaultNetwork}. |
| 4459 | * |
| 4460 | * @param network The {@link Network} to bind the current process to, or {@code null} to clear |
| 4461 | * the current binding. |
| 4462 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
| 4463 | * @deprecated This function can throw {@link IllegalStateException}. Use |
| 4464 | * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork} |
| 4465 | * is a direct replacement. |
| 4466 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 4467 | @Deprecated |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4468 | public static boolean setProcessDefaultNetwork(@Nullable Network network) { |
Paul Jensen | c91b534 | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 4469 | int netId = (network == null) ? NETID_UNSET : network.netId; |
Lorenzo Colitti | e5ca116 | 2019-01-31 13:08:24 +0900 | [diff] [blame] | 4470 | boolean isSameNetId = (netId == NetworkUtils.getBoundNetworkForProcess()); |
| 4471 | |
Lorenzo Colitti | 65ceeb4 | 2019-01-30 23:04:54 +0900 | [diff] [blame] | 4472 | if (netId != NETID_UNSET) { |
| 4473 | netId = network.getNetIdForResolv(); |
Paul Jensen | c91b534 | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 4474 | } |
Lorenzo Colitti | e5ca116 | 2019-01-31 13:08:24 +0900 | [diff] [blame] | 4475 | |
| 4476 | if (!NetworkUtils.bindProcessToNetwork(netId)) { |
| 4477 | return false; |
| 4478 | } |
| 4479 | |
| 4480 | if (!isSameNetId) { |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 4481 | // Set HTTP proxy system properties to match network. |
| 4482 | // TODO: Deprecate this static method and replace it with a non-static version. |
Lorenzo Colitti | ec4c555 | 2015-04-22 11:52:48 +0900 | [diff] [blame] | 4483 | try { |
| 4484 | Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy()); |
| 4485 | } catch (SecurityException e) { |
| 4486 | // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy. |
| 4487 | Log.e(TAG, "Can't set proxy properties", e); |
| 4488 | } |
Paul Jensen | c91b534 | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 4489 | // Must flush DNS cache as new network may have different DNS resolutions. |
| 4490 | InetAddress.clearDnsCache(); |
| 4491 | // Must flush socket pool as idle sockets will be bound to previous network and may |
| 4492 | // cause subsequent fetches to be performed on old network. |
| 4493 | NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged(); |
Paul Jensen | c91b534 | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 4494 | } |
Lorenzo Colitti | e5ca116 | 2019-01-31 13:08:24 +0900 | [diff] [blame] | 4495 | |
| 4496 | return true; |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4497 | } |
| 4498 | |
| 4499 | /** |
| 4500 | * Returns the {@link Network} currently bound to this process via |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 4501 | * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound. |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4502 | * |
| 4503 | * @return {@code Network} to which this process is bound, or {@code null}. |
| 4504 | */ |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4505 | @Nullable |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 4506 | public Network getBoundNetworkForProcess() { |
| 4507 | // Forcing callers to call thru non-static function ensures ConnectivityManager |
| 4508 | // instantiated. |
| 4509 | return getProcessDefaultNetwork(); |
| 4510 | } |
| 4511 | |
| 4512 | /** |
| 4513 | * Returns the {@link Network} currently bound to this process via |
| 4514 | * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound. |
| 4515 | * |
| 4516 | * @return {@code Network} to which this process is bound, or {@code null}. |
| 4517 | * @deprecated Using this function can lead to other functions throwing |
| 4518 | * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead. |
| 4519 | * {@code getBoundNetworkForProcess} is a direct replacement. |
| 4520 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 4521 | @Deprecated |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4522 | @Nullable |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4523 | public static Network getProcessDefaultNetwork() { |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 4524 | int netId = NetworkUtils.getBoundNetworkForProcess(); |
Paul Jensen | bcc76d3 | 2014-07-11 08:17:29 -0400 | [diff] [blame] | 4525 | if (netId == NETID_UNSET) return null; |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4526 | return new Network(netId); |
| 4527 | } |
| 4528 | |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 4529 | private void unsupportedStartingFrom(int version) { |
| 4530 | if (Process.myUid() == Process.SYSTEM_UID) { |
Lorenzo Colitti | 2e31a7c | 2018-09-28 11:31:55 +0900 | [diff] [blame] | 4531 | // The getApplicationInfo() call we make below is not supported in system context. Let |
| 4532 | // the call through here, and rely on the fact that ConnectivityService will refuse to |
| 4533 | // allow the system to use these APIs anyway. |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 4534 | return; |
| 4535 | } |
| 4536 | |
| 4537 | if (mContext.getApplicationInfo().targetSdkVersion >= version) { |
| 4538 | throw new UnsupportedOperationException( |
| 4539 | "This method is not supported in target SDK version " + version + " and above"); |
| 4540 | } |
| 4541 | } |
| 4542 | |
| 4543 | // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature, |
| 4544 | // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException. |
Lifu Tang | 30f95a7 | 2016-01-07 23:20:38 -0800 | [diff] [blame] | 4545 | // TODO: convert the existing system users (Tethering, GnssLocationProvider) to the new APIs and |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 4546 | // remove these exemptions. Note that this check is not secure, and apps can still access these |
| 4547 | // functions by accessing ConnectivityService directly. However, it should be clear that doing |
| 4548 | // so is unsupported and may break in the future. http://b/22728205 |
| 4549 | private void checkLegacyRoutingApiAccess() { |
Dianne Hackborn | 692a244 | 2015-07-31 10:35:34 -0700 | [diff] [blame] | 4550 | unsupportedStartingFrom(VERSION_CODES.M); |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 4551 | } |
| 4552 | |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4553 | /** |
| 4554 | * Binds host resolutions performed by this process to {@code network}. |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 4555 | * {@link #bindProcessToNetwork} takes precedence over this setting. |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4556 | * |
| 4557 | * @param network The {@link Network} to bind host resolutions from the current process to, or |
| 4558 | * {@code null} to clear the current binding. |
| 4559 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
| 4560 | * @hide |
| 4561 | * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}. |
| 4562 | */ |
Aurimas Liutikas | 514c5ef | 2016-05-24 15:22:55 -0700 | [diff] [blame] | 4563 | @Deprecated |
Mathew Inwood | fa3a746 | 2018-08-08 14:52:47 +0100 | [diff] [blame] | 4564 | @UnsupportedAppUsage |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4565 | public static boolean setProcessDefaultNetworkForHostResolution(Network network) { |
Paul Jensen | bcc76d3 | 2014-07-11 08:17:29 -0400 | [diff] [blame] | 4566 | return NetworkUtils.bindProcessToNetworkForHostResolution( |
Erik Kline | f4fa982 | 2018-04-27 22:48:33 +0900 | [diff] [blame] | 4567 | (network == null) ? NETID_UNSET : network.getNetIdForResolv()); |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 4568 | } |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 4569 | |
| 4570 | /** |
| 4571 | * Device is not restricting metered network activity while application is running on |
| 4572 | * background. |
| 4573 | */ |
| 4574 | public static final int RESTRICT_BACKGROUND_STATUS_DISABLED = 1; |
| 4575 | |
| 4576 | /** |
| 4577 | * Device is restricting metered network activity while application is running on background, |
| 4578 | * but application is allowed to bypass it. |
| 4579 | * <p> |
| 4580 | * In this state, application should take action to mitigate metered network access. |
| 4581 | * For example, a music streaming application should switch to a low-bandwidth bitrate. |
| 4582 | */ |
| 4583 | public static final int RESTRICT_BACKGROUND_STATUS_WHITELISTED = 2; |
| 4584 | |
| 4585 | /** |
| 4586 | * Device is restricting metered network activity while application is running on background. |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 4587 | * <p> |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 4588 | * In this state, application should not try to use the network while running on background, |
| 4589 | * because it would be denied. |
| 4590 | */ |
| 4591 | public static final int RESTRICT_BACKGROUND_STATUS_ENABLED = 3; |
| 4592 | |
Felipe Leme | 9778f76 | 2016-01-27 14:46:39 -0800 | [diff] [blame] | 4593 | /** |
| 4594 | * A change in the background metered network activity restriction has occurred. |
| 4595 | * <p> |
| 4596 | * Applications should call {@link #getRestrictBackgroundStatus()} to check if the restriction |
| 4597 | * applies to them. |
| 4598 | * <p> |
| 4599 | * This is only sent to registered receivers, not manifest receivers. |
| 4600 | */ |
| 4601 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 4602 | public static final String ACTION_RESTRICT_BACKGROUND_CHANGED = |
| 4603 | "android.net.conn.RESTRICT_BACKGROUND_CHANGED"; |
| 4604 | |
Felipe Leme | ecfccea | 2016-01-25 11:48:04 -0800 | [diff] [blame] | 4605 | /** @hide */ |
| 4606 | @Retention(RetentionPolicy.SOURCE) |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 4607 | @IntDef(flag = false, value = { |
| 4608 | RESTRICT_BACKGROUND_STATUS_DISABLED, |
| 4609 | RESTRICT_BACKGROUND_STATUS_WHITELISTED, |
| 4610 | RESTRICT_BACKGROUND_STATUS_ENABLED, |
| 4611 | }) |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 4612 | public @interface RestrictBackgroundStatus { |
| 4613 | } |
| 4614 | |
| 4615 | private INetworkPolicyManager getNetworkPolicyManager() { |
| 4616 | synchronized (this) { |
| 4617 | if (mNPManager != null) { |
| 4618 | return mNPManager; |
| 4619 | } |
| 4620 | mNPManager = INetworkPolicyManager.Stub.asInterface(ServiceManager |
| 4621 | .getService(Context.NETWORK_POLICY_SERVICE)); |
| 4622 | return mNPManager; |
| 4623 | } |
| 4624 | } |
| 4625 | |
| 4626 | /** |
| 4627 | * Determines if the calling application is subject to metered network restrictions while |
| 4628 | * running on background. |
Felipe Leme | c9c7be5 | 2016-05-16 13:57:19 -0700 | [diff] [blame] | 4629 | * |
| 4630 | * @return {@link #RESTRICT_BACKGROUND_STATUS_DISABLED}, |
| 4631 | * {@link #RESTRICT_BACKGROUND_STATUS_ENABLED}, |
| 4632 | * or {@link #RESTRICT_BACKGROUND_STATUS_WHITELISTED} |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 4633 | */ |
| 4634 | public @RestrictBackgroundStatus int getRestrictBackgroundStatus() { |
| 4635 | try { |
| 4636 | return getNetworkPolicyManager().getRestrictBackgroundByCaller(); |
| 4637 | } catch (RemoteException e) { |
Jeff Sharkey | c53962d | 2016-03-01 19:27:23 -0700 | [diff] [blame] | 4638 | throw e.rethrowFromSystemServer(); |
Felipe Leme | 1b10323 | 2016-01-22 09:44:57 -0800 | [diff] [blame] | 4639 | } |
| 4640 | } |
Ricky Wai | 44dcbde | 2018-01-23 04:09:45 +0000 | [diff] [blame] | 4641 | |
| 4642 | /** |
| 4643 | * The network watchlist is a list of domains and IP addresses that are associated with |
Ricky Wai | a86d5d5 | 2018-03-20 14:20:54 +0000 | [diff] [blame] | 4644 | * potentially harmful apps. This method returns the SHA-256 of the watchlist config file |
| 4645 | * currently used by the system for validation purposes. |
Ricky Wai | 44dcbde | 2018-01-23 04:09:45 +0000 | [diff] [blame] | 4646 | * |
| 4647 | * @return Hash of network watchlist config file. Null if config does not exist. |
| 4648 | */ |
Chalard Jean | 50bea3d | 2019-01-07 19:26:34 +0900 | [diff] [blame] | 4649 | @Nullable |
Ricky Wai | 44dcbde | 2018-01-23 04:09:45 +0000 | [diff] [blame] | 4650 | public byte[] getNetworkWatchlistConfigHash() { |
| 4651 | try { |
| 4652 | return mService.getNetworkWatchlistConfigHash(); |
| 4653 | } catch (RemoteException e) { |
| 4654 | Log.e(TAG, "Unable to get watchlist config hash"); |
| 4655 | throw e.rethrowFromSystemServer(); |
| 4656 | } |
| 4657 | } |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 4658 | |
| 4659 | /** |
| 4660 | * Returns the {@code uid} of the owner of a network connection. |
| 4661 | * |
Benedict Wong | 5d50ce8 | 2020-01-20 22:14:59 -0800 | [diff] [blame] | 4662 | * @param protocol The protocol of the connection. Only {@code IPPROTO_TCP} and {@code |
| 4663 | * IPPROTO_UDP} currently supported. |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 4664 | * @param local The local {@link InetSocketAddress} of a connection. |
| 4665 | * @param remote The remote {@link InetSocketAddress} of a connection. |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 4666 | * @return {@code uid} if the connection is found and the app has permission to observe it |
Benedict Wong | 5d50ce8 | 2020-01-20 22:14:59 -0800 | [diff] [blame] | 4667 | * (e.g., if it is associated with the calling VPN app's VpnService tunnel) or {@link |
| 4668 | * android.os.Process#INVALID_UID} if the connection is not found. |
| 4669 | * @throws {@link SecurityException} if the caller is not the active VpnService for the current |
| 4670 | * user. |
| 4671 | * @throws {@link IllegalArgumentException} if an unsupported protocol is requested. |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 4672 | */ |
Benedict Wong | 5d50ce8 | 2020-01-20 22:14:59 -0800 | [diff] [blame] | 4673 | public int getConnectionOwnerUid( |
| 4674 | int protocol, @NonNull InetSocketAddress local, @NonNull InetSocketAddress remote) { |
Jeff Vander Stoep | 0ac2c09 | 2018-07-23 10:57:53 -0700 | [diff] [blame] | 4675 | ConnectionInfo connectionInfo = new ConnectionInfo(protocol, local, remote); |
| 4676 | try { |
| 4677 | return mService.getConnectionOwnerUid(connectionInfo); |
| 4678 | } catch (RemoteException e) { |
| 4679 | throw e.rethrowFromSystemServer(); |
| 4680 | } |
| 4681 | } |
Soi, Yoshinari | dee2aa4 | 2015-11-12 12:09:02 +0900 | [diff] [blame] | 4682 | |
| 4683 | private void printStackTrace() { |
| 4684 | if (DEBUG) { |
| 4685 | final StackTraceElement[] callStack = Thread.currentThread().getStackTrace(); |
| 4686 | final StringBuffer sb = new StringBuffer(); |
| 4687 | for (int i = 3; i < callStack.length; i++) { |
| 4688 | final String stackTrace = callStack[i].toString(); |
| 4689 | if (stackTrace == null || stackTrace.contains("android.os")) { |
| 4690 | break; |
| 4691 | } |
| 4692 | sb.append(" [").append(stackTrace).append("]"); |
| 4693 | } |
| 4694 | Log.d(TAG, "StackLog:" + sb.toString()); |
| 4695 | } |
| 4696 | } |
Automerger Merge Worker | 42f8135 | 2020-05-12 18:48:04 +0000 | [diff] [blame] | 4697 | |
| 4698 | /** |
| 4699 | * Simulates a Data Stall for the specified Network. |
| 4700 | * |
| 4701 | * <p>The caller must be the owner of the specified Network. |
| 4702 | * |
| 4703 | * @param detectionMethod The detection method used to identify the Data Stall. |
| 4704 | * @param timestampMillis The timestamp at which the stall 'occurred', in milliseconds. |
| 4705 | * @param network The Network for which a Data Stall is being simluated. |
| 4706 | * @param extras The PersistableBundle of extras included in the Data Stall notification. |
| 4707 | * @throws SecurityException if the caller is not the owner of the given network. |
| 4708 | * @hide |
| 4709 | */ |
| 4710 | @TestApi |
| 4711 | @RequiresPermission(anyOf = {android.Manifest.permission.MANAGE_TEST_NETWORKS, |
| 4712 | android.Manifest.permission.NETWORK_STACK}) |
| 4713 | public void simulateDataStall(int detectionMethod, long timestampMillis, |
| 4714 | @NonNull Network network, @NonNull PersistableBundle extras) { |
| 4715 | try { |
| 4716 | mService.simulateDataStall(detectionMethod, timestampMillis, network, extras); |
| 4717 | } catch (RemoteException e) { |
| 4718 | e.rethrowFromSystemServer(); |
| 4719 | } |
| 4720 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 4721 | } |