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