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