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