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