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