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