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