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