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