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