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; |
| 19 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | import android.annotation.SdkConstant; |
| 21 | import android.annotation.SdkConstant.SdkConstantType; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 22 | import android.app.PendingIntent; |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 23 | import android.content.Context; |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 24 | import android.content.Intent; |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 25 | import android.content.pm.PackageManager; |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 26 | import android.net.NetworkUtils; |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 27 | import android.os.Binder; |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 28 | import android.os.Build.VERSION_CODES; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 29 | import android.os.Handler; |
| 30 | import android.os.HandlerThread; |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 31 | import android.os.IBinder; |
| 32 | import android.os.INetworkActivityListener; |
| 33 | import android.os.INetworkManagementService; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 34 | import android.os.Looper; |
| 35 | import android.os.Message; |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 36 | import android.os.Messenger; |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 37 | import android.os.Process; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 38 | import android.os.RemoteException; |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 39 | import android.os.ServiceManager; |
Jeff Sharkey | 961e304 | 2011-08-29 16:02:57 -0700 | [diff] [blame] | 40 | import android.provider.Settings; |
Wink Saville | 36ffb04 | 2014-12-05 11:10:30 -0800 | [diff] [blame] | 41 | import android.telephony.SubscriptionManager; |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 42 | import android.util.ArrayMap; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 43 | import android.util.Log; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 44 | |
Robert Greenwalt | afa05c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 45 | import com.android.internal.telephony.ITelephony; |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 46 | import com.android.internal.telephony.PhoneConstants; |
Robert Greenwalt | afa05c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 47 | import com.android.internal.util.Protocol; |
| 48 | |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 49 | import java.net.InetAddress; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 50 | import java.util.concurrent.atomic.AtomicInteger; |
| 51 | import java.util.HashMap; |
| 52 | |
Paul Jensen | c91b534 | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 53 | import libcore.net.event.NetworkEventDispatcher; |
| 54 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | /** |
| 56 | * Class that answers queries about the state of network connectivity. It also |
| 57 | * notifies applications when network connectivity changes. Get an instance |
| 58 | * of this class by calling |
| 59 | * {@link android.content.Context#getSystemService(String) Context.getSystemService(Context.CONNECTIVITY_SERVICE)}. |
| 60 | * <p> |
| 61 | * The primary responsibilities of this class are to: |
| 62 | * <ol> |
| 63 | * <li>Monitor network connections (Wi-Fi, GPRS, UMTS, etc.)</li> |
| 64 | * <li>Send broadcast intents when network connectivity changes</li> |
| 65 | * <li>Attempt to "fail over" to another network when connectivity to a network |
| 66 | * is lost</li> |
| 67 | * <li>Provide an API that allows applications to query the coarse-grained or fine-grained |
| 68 | * state of the available networks</li> |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 69 | * <li>Provide an API that allows applications to request and select networks for their data |
| 70 | * traffic</li> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 71 | * </ol> |
| 72 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 73 | public class ConnectivityManager { |
| 74 | private static final String TAG = "ConnectivityManager"; |
| 75 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 76 | /** |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 77 | * 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] | 78 | * been established or lost. The NetworkInfo for the affected network is |
| 79 | * sent as an extra; it should be consulted to see what kind of |
| 80 | * connectivity event occurred. |
| 81 | * <p/> |
| 82 | * If this is a connection that was the result of failing over from a |
| 83 | * disconnected network, then the FAILOVER_CONNECTION boolean extra is |
| 84 | * set to true. |
| 85 | * <p/> |
| 86 | * For a loss of connectivity, if the connectivity manager is attempting |
| 87 | * to connect (or has already connected) to another network, the |
| 88 | * NetworkInfo for the new network is also passed as an extra. This lets |
| 89 | * any receivers of the broadcast know that they should not necessarily |
| 90 | * tell the user that no data traffic will be possible. Instead, the |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 91 | * receiver should expect another broadcast soon, indicating either that |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | * the failover attempt succeeded (and so there is still overall data |
| 93 | * connectivity), or that the failover attempt failed, meaning that all |
| 94 | * connectivity has been lost. |
| 95 | * <p/> |
| 96 | * For a disconnect event, the boolean extra EXTRA_NO_CONNECTIVITY |
| 97 | * is set to {@code true} if there are no connected networks at all. |
| 98 | */ |
Jeff Sharkey | 4fa63b2 | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 99 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 100 | public static final String CONNECTIVITY_ACTION = "android.net.conn.CONNECTIVITY_CHANGE"; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 101 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 102 | /** |
Robert Greenwalt | e94a6ff | 2015-09-01 08:23:04 -0700 | [diff] [blame] | 103 | * A temporary hack until SUPL system can get off the legacy APIS. |
| 104 | * They do too many network requests and the long list of apps listening |
| 105 | * and waking due to the CONNECTIVITY_ACTION bcast makes it expensive. |
| 106 | * Use this bcast intent instead for SUPL requests. |
| 107 | * @hide |
| 108 | */ |
| 109 | public static final String CONNECTIVITY_ACTION_SUPL = |
| 110 | "android.net.conn.CONNECTIVITY_CHANGE_SUPL"; |
| 111 | |
| 112 | /** |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 113 | * The device has connected to a network that has presented a captive |
| 114 | * portal, which is blocking Internet connectivity. The user was presented |
| 115 | * with a notification that network sign in is required, |
| 116 | * and the user invoked the notification's action indicating they |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 117 | * desire to sign in to the network. Apps handling this activity should |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 118 | * facilitate signing in to the network. This action includes a |
| 119 | * {@link Network} typed extra called {@link #EXTRA_NETWORK} that represents |
| 120 | * the network presenting the captive portal; all communication with the |
| 121 | * captive portal must be done using this {@code Network} object. |
| 122 | * <p/> |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 123 | * This activity includes a {@link CaptivePortal} extra named |
| 124 | * {@link #EXTRA_CAPTIVE_PORTAL} that can be used to indicate different |
| 125 | * outcomes of the captive portal sign in to the system: |
| 126 | * <ul> |
| 127 | * <li> When the app handling this action believes the user has signed in to |
| 128 | * the network and the captive portal has been dismissed, the app should |
| 129 | * call {@link CaptivePortal#reportCaptivePortalDismissed} so the system can |
| 130 | * reevaluate the network. If reevaluation finds the network no longer |
| 131 | * subject to a captive portal, the network may become the default active |
| 132 | * data network. </li> |
| 133 | * <li> When the app handling this action believes the user explicitly wants |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 134 | * to ignore the captive portal and the network, the app should call |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 135 | * {@link CaptivePortal#ignoreNetwork}. </li> |
| 136 | * </ul> |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 137 | */ |
| 138 | @SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION) |
| 139 | public static final String ACTION_CAPTIVE_PORTAL_SIGN_IN = "android.net.conn.CAPTIVE_PORTAL"; |
| 140 | |
| 141 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | * The lookup key for a {@link NetworkInfo} object. Retrieve with |
| 143 | * {@link android.content.Intent#getParcelableExtra(String)}. |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 144 | * |
| 145 | * @deprecated Since {@link NetworkInfo} can vary based on UID, applications |
| 146 | * should always obtain network information through |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 147 | * {@link #getActiveNetworkInfo()}. |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 148 | * @see #EXTRA_NETWORK_TYPE |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 149 | */ |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 150 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 151 | public static final String EXTRA_NETWORK_INFO = "networkInfo"; |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 152 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | /** |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 154 | * Network type which triggered a {@link #CONNECTIVITY_ACTION} broadcast. |
Jeff Sharkey | 75fbb4b | 2012-08-06 11:41:50 -0700 | [diff] [blame] | 155 | * |
| 156 | * @see android.content.Intent#getIntExtra(String, int) |
| 157 | */ |
| 158 | public static final String EXTRA_NETWORK_TYPE = "networkType"; |
| 159 | |
| 160 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | * The lookup key for a boolean that indicates whether a connect event |
| 162 | * is for a network to which the connectivity manager was failing over |
| 163 | * following a disconnect on another network. |
| 164 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
| 165 | */ |
| 166 | public static final String EXTRA_IS_FAILOVER = "isFailover"; |
| 167 | /** |
| 168 | * The lookup key for a {@link NetworkInfo} object. This is supplied when |
| 169 | * there is another network that it may be possible to connect to. Retrieve with |
| 170 | * {@link android.content.Intent#getParcelableExtra(String)}. |
| 171 | */ |
| 172 | public static final String EXTRA_OTHER_NETWORK_INFO = "otherNetwork"; |
| 173 | /** |
| 174 | * The lookup key for a boolean that indicates whether there is a |
| 175 | * complete lack of connectivity, i.e., no network is available. |
| 176 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
| 177 | */ |
| 178 | public static final String EXTRA_NO_CONNECTIVITY = "noConnectivity"; |
| 179 | /** |
| 180 | * The lookup key for a string that indicates why an attempt to connect |
| 181 | * to a network failed. The string has no particular structure. It is |
| 182 | * intended to be used in notifications presented to users. Retrieve |
| 183 | * it with {@link android.content.Intent#getStringExtra(String)}. |
| 184 | */ |
| 185 | public static final String EXTRA_REASON = "reason"; |
| 186 | /** |
| 187 | * The lookup key for a string that provides optionally supplied |
| 188 | * extra information about the network state. The information |
| 189 | * may be passed up from the lower networking layers, and its |
| 190 | * meaning may be specific to a particular network type. Retrieve |
| 191 | * it with {@link android.content.Intent#getStringExtra(String)}. |
| 192 | */ |
| 193 | public static final String EXTRA_EXTRA_INFO = "extraInfo"; |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 194 | /** |
| 195 | * The lookup key for an int that provides information about |
| 196 | * our connection to the internet at large. 0 indicates no connection, |
| 197 | * 100 indicates a great connection. Retrieve it with |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 198 | * {@link android.content.Intent#getIntExtra(String, int)}. |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 199 | * {@hide} |
| 200 | */ |
| 201 | public static final String EXTRA_INET_CONDITION = "inetCondition"; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 202 | /** |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 203 | * The lookup key for a {@link CaptivePortal} object included with the |
| 204 | * {@link #ACTION_CAPTIVE_PORTAL_SIGN_IN} intent. The {@code CaptivePortal} |
| 205 | * object can be used to either indicate to the system that the captive |
| 206 | * portal has been dismissed or that the user does not want to pursue |
| 207 | * signing in to captive portal. Retrieve it with |
| 208 | * {@link android.content.Intent#getParcelableExtra(String)}. |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 209 | */ |
Paul Jensen | 49e3edf | 2015-05-22 10:50:39 -0400 | [diff] [blame] | 210 | public static final String EXTRA_CAPTIVE_PORTAL = "android.net.extra.CAPTIVE_PORTAL"; |
Paul Jensen | 25a217c | 2015-02-27 22:55:47 -0500 | [diff] [blame] | 211 | /** |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 212 | * Broadcast action to indicate the change of data activity status |
| 213 | * (idle or active) on a network in a recent period. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 214 | * The network becomes active when data transmission is started, or |
| 215 | * idle if there is no data transmission for a period of time. |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 216 | * {@hide} |
| 217 | */ |
| 218 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
| 219 | public static final String ACTION_DATA_ACTIVITY_CHANGE = "android.net.conn.DATA_ACTIVITY_CHANGE"; |
| 220 | /** |
| 221 | * The lookup key for an enum that indicates the network device type on which this data activity |
| 222 | * change happens. |
| 223 | * {@hide} |
| 224 | */ |
| 225 | public static final String EXTRA_DEVICE_TYPE = "deviceType"; |
| 226 | /** |
| 227 | * The lookup key for a boolean that indicates the device is active or not. {@code true} means |
| 228 | * it is actively sending or receiving data and {@code false} means it is idle. |
| 229 | * {@hide} |
| 230 | */ |
| 231 | public static final String EXTRA_IS_ACTIVE = "isActive"; |
Ashish Sharma | 0535a9f | 2014-03-12 18:42:23 -0700 | [diff] [blame] | 232 | /** |
| 233 | * The lookup key for a long that contains the timestamp (nanos) of the radio state change. |
| 234 | * {@hide} |
| 235 | */ |
| 236 | public static final String EXTRA_REALTIME_NS = "tsNanos"; |
Haoyu Bai | db3c867 | 2012-06-20 14:29:57 -0700 | [diff] [blame] | 237 | |
| 238 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 239 | * Broadcast Action: The setting for background data usage has changed |
| 240 | * values. Use {@link #getBackgroundDataSetting()} to get the current value. |
| 241 | * <p> |
| 242 | * If an application uses the network in the background, it should listen |
| 243 | * 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] | 244 | * {@code false}. |
Jeff Sharkey | 54ee2ad | 2012-01-30 16:29:24 -0800 | [diff] [blame] | 245 | * <p> |
| 246 | * |
| 247 | * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability |
| 248 | * of background data depends on several combined factors, and |
| 249 | * this broadcast is no longer sent. Instead, when background |
| 250 | * data is unavailable, {@link #getActiveNetworkInfo()} will now |
| 251 | * appear disconnected. During first boot after a platform |
| 252 | * upgrade, this broadcast will be sent once if |
| 253 | * {@link #getBackgroundDataSetting()} was {@code false} before |
| 254 | * the upgrade. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 255 | */ |
| 256 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Jeff Sharkey | 54ee2ad | 2012-01-30 16:29:24 -0800 | [diff] [blame] | 257 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | public static final String ACTION_BACKGROUND_DATA_SETTING_CHANGED = |
| 259 | "android.net.conn.BACKGROUND_DATA_SETTING_CHANGED"; |
| 260 | |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 261 | /** |
| 262 | * Broadcast Action: The network connection may not be good |
| 263 | * uses {@code ConnectivityManager.EXTRA_INET_CONDITION} and |
| 264 | * {@code ConnectivityManager.EXTRA_NETWORK_INFO} to specify |
| 265 | * the network and it's condition. |
| 266 | * @hide |
| 267 | */ |
Jeff Sharkey | 4fa63b2 | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 268 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Robert Greenwalt | 1e9aac2 | 2010-09-15 17:36:33 -0700 | [diff] [blame] | 269 | public static final String INET_CONDITION_ACTION = |
| 270 | "android.net.conn.INET_CONDITION_ACTION"; |
| 271 | |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 272 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 273 | * Broadcast Action: A tetherable connection has come or gone. |
| 274 | * Uses {@code ConnectivityManager.EXTRA_AVAILABLE_TETHER}, |
| 275 | * {@code ConnectivityManager.EXTRA_ACTIVE_TETHER} and |
| 276 | * {@code ConnectivityManager.EXTRA_ERRORED_TETHER} to indicate |
| 277 | * the current state of tethering. Each include a list of |
| 278 | * interface names in that state (may be empty). |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 279 | * @hide |
| 280 | */ |
Jeff Sharkey | 4fa63b2 | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 281 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 282 | public static final String ACTION_TETHER_STATE_CHANGED = |
| 283 | "android.net.conn.TETHER_STATE_CHANGED"; |
| 284 | |
| 285 | /** |
| 286 | * @hide |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 287 | * gives a String[] listing all the interfaces configured for |
| 288 | * tethering and currently available for tethering. |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 289 | */ |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 290 | public static final String EXTRA_AVAILABLE_TETHER = "availableArray"; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 291 | |
| 292 | /** |
| 293 | * @hide |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 294 | * gives a String[] listing all the interfaces currently tethered |
| 295 | * (ie, has dhcp support and packets potentially forwarded/NATed) |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 296 | */ |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 297 | public static final String EXTRA_ACTIVE_TETHER = "activeArray"; |
| 298 | |
| 299 | /** |
| 300 | * @hide |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 301 | * gives a String[] listing all the interfaces we tried to tether and |
| 302 | * failed. Use {@link #getLastTetherError} to find the error code |
| 303 | * for any interfaces listed here. |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 304 | */ |
| 305 | public static final String EXTRA_ERRORED_TETHER = "erroredArray"; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 306 | |
| 307 | /** |
Russell Brenner | 108da0c | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 308 | * Broadcast Action: The captive portal tracker has finished its test. |
| 309 | * Sent only while running Setup Wizard, in lieu of showing a user |
| 310 | * notification. |
| 311 | * @hide |
| 312 | */ |
Jeff Sharkey | 4fa63b2 | 2013-02-20 18:21:19 -0800 | [diff] [blame] | 313 | @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION) |
Russell Brenner | 108da0c | 2013-02-12 10:03:14 -0800 | [diff] [blame] | 314 | public static final String ACTION_CAPTIVE_PORTAL_TEST_COMPLETED = |
| 315 | "android.net.conn.CAPTIVE_PORTAL_TEST_COMPLETED"; |
| 316 | /** |
| 317 | * The lookup key for a boolean that indicates whether a captive portal was detected. |
| 318 | * Retrieve it with {@link android.content.Intent#getBooleanExtra(String,boolean)}. |
| 319 | * @hide |
| 320 | */ |
| 321 | public static final String EXTRA_IS_CAPTIVE_PORTAL = "captivePortal"; |
| 322 | |
| 323 | /** |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 324 | * Action used to display a dialog that asks the user whether to connect to a network that is |
| 325 | * not validated. This intent is used to start the dialog in settings via startActivity. |
| 326 | * |
| 327 | * @hide |
| 328 | */ |
| 329 | public static final String ACTION_PROMPT_UNVALIDATED = "android.net.conn.PROMPT_UNVALIDATED"; |
| 330 | |
| 331 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 332 | * The absence of a connection type. |
Robert Greenwalt | ccf83af1 | 2011-06-02 17:30:47 -0700 | [diff] [blame] | 333 | * @hide |
| 334 | */ |
| 335 | public static final int TYPE_NONE = -1; |
| 336 | |
| 337 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 338 | * The Mobile data connection. When active, all data traffic |
| 339 | * will use this network type's interface by default |
| 340 | * (it has a default route) |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 341 | */ |
| 342 | public static final int TYPE_MOBILE = 0; |
| 343 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 344 | * The WIFI data connection. When active, all data traffic |
| 345 | * will use this network type's interface by default |
| 346 | * (it has a default route). |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 347 | */ |
| 348 | public static final int TYPE_WIFI = 1; |
| 349 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 350 | * An MMS-specific Mobile data connection. This network type may use the |
| 351 | * same network interface as {@link #TYPE_MOBILE} or it may use a different |
| 352 | * one. This is used by applications needing to talk to the carrier's |
| 353 | * Multimedia Messaging Service servers. |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 354 | * |
Lorenzo Colitti | 2ea89e5 | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 355 | * @deprecated Applications should instead use |
| 356 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 357 | * provides the {@link NetworkCapabilities#NET_CAPABILITY_MMS} capability. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 358 | */ |
| 359 | public static final int TYPE_MOBILE_MMS = 2; |
| 360 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 361 | * A SUPL-specific Mobile data connection. This network type may use the |
| 362 | * same network interface as {@link #TYPE_MOBILE} or it may use a different |
| 363 | * one. This is used by applications needing to talk to the carrier's |
| 364 | * Secure User Plane Location servers for help locating the device. |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 365 | * |
Lorenzo Colitti | 2ea89e5 | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 366 | * @deprecated Applications should instead use |
| 367 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 368 | * provides the {@link NetworkCapabilities#NET_CAPABILITY_SUPL} capability. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 369 | */ |
| 370 | public static final int TYPE_MOBILE_SUPL = 3; |
| 371 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 372 | * A DUN-specific Mobile data connection. This network type may use the |
| 373 | * same network interface as {@link #TYPE_MOBILE} or it may use a different |
| 374 | * one. This is sometimes by the system when setting up an upstream connection |
| 375 | * for tethering so that the carrier is aware of DUN traffic. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 376 | */ |
| 377 | public static final int TYPE_MOBILE_DUN = 4; |
| 378 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 379 | * A High Priority Mobile data connection. This network type uses the |
| 380 | * same network interface as {@link #TYPE_MOBILE} but the routing setup |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 381 | * is different. |
| 382 | * |
Lorenzo Colitti | 2ea89e5 | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 383 | * @deprecated Applications should instead use |
| 384 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} to request a network that |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 385 | * uses the {@link NetworkCapabilities#TRANSPORT_CELLULAR} transport. |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 386 | */ |
| 387 | public static final int TYPE_MOBILE_HIPRI = 5; |
jsh | 8214deb | 2010-03-11 15:04:43 -0800 | [diff] [blame] | 388 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 389 | * The WiMAX data connection. When active, all data traffic |
| 390 | * will use this network type's interface by default |
| 391 | * (it has a default route). |
jsh | 8214deb | 2010-03-11 15:04:43 -0800 | [diff] [blame] | 392 | */ |
| 393 | public static final int TYPE_WIMAX = 6; |
Robert Greenwalt | da3d5e6 | 2010-12-06 13:56:24 -0800 | [diff] [blame] | 394 | |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 395 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 396 | * The Bluetooth data connection. When active, all data traffic |
| 397 | * will use this network type's interface by default |
| 398 | * (it has a default route). |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 399 | */ |
| 400 | public static final int TYPE_BLUETOOTH = 7; |
| 401 | |
Robert Greenwalt | 6081084 | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 402 | /** |
| 403 | * Dummy data connection. This should not be used on shipping devices. |
| 404 | */ |
Jaikumar Ganesh | 15c7439 | 2010-12-21 22:31:44 -0800 | [diff] [blame] | 405 | public static final int TYPE_DUMMY = 8; |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 406 | |
Robert Greenwalt | 6081084 | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 407 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 408 | * The Ethernet data connection. When active, all data traffic |
| 409 | * will use this network type's interface by default |
| 410 | * (it has a default route). |
Robert Greenwalt | 6081084 | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 411 | */ |
Robert Greenwalt | e12aec9 | 2011-01-28 14:48:37 -0800 | [diff] [blame] | 412 | public static final int TYPE_ETHERNET = 9; |
Robert Greenwalt | 6081084 | 2011-04-22 15:28:18 -0700 | [diff] [blame] | 413 | |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 414 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 415 | * Over the air Administration. |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 416 | * {@hide} |
| 417 | */ |
| 418 | public static final int TYPE_MOBILE_FOTA = 10; |
| 419 | |
| 420 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 421 | * IP Multimedia Subsystem. |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 422 | * {@hide} |
| 423 | */ |
| 424 | public static final int TYPE_MOBILE_IMS = 11; |
| 425 | |
| 426 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 427 | * Carrier Branded Services. |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 428 | * {@hide} |
| 429 | */ |
| 430 | public static final int TYPE_MOBILE_CBS = 12; |
| 431 | |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 432 | /** |
| 433 | * A Wi-Fi p2p connection. Only requesting processes will have access to |
| 434 | * the peers connected. |
| 435 | * {@hide} |
| 436 | */ |
| 437 | public static final int TYPE_WIFI_P2P = 13; |
Wink Saville | 9d7d628 | 2011-03-12 14:52:01 -0800 | [diff] [blame] | 438 | |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 439 | /** |
| 440 | * The network to use for initially attaching to the network |
| 441 | * {@hide} |
| 442 | */ |
| 443 | public static final int TYPE_MOBILE_IA = 14; |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 444 | |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 445 | /** |
Robert Greenwalt | 4bd4389 | 2015-07-09 14:49:35 -0700 | [diff] [blame] | 446 | * Emergency PDN connection for emergency services. This |
| 447 | * may include IMS and MMS in emergency situations. |
Ram | 3e0e3bc | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 448 | * {@hide} |
| 449 | */ |
| 450 | public static final int TYPE_MOBILE_EMERGENCY = 15; |
| 451 | |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 452 | /** |
| 453 | * The network that uses proxy to achieve connectivity. |
| 454 | * {@hide} |
| 455 | */ |
| 456 | public static final int TYPE_PROXY = 16; |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 457 | |
Robert Greenwalt | 8283f88 | 2014-07-07 17:09:01 -0700 | [diff] [blame] | 458 | /** |
| 459 | * A virtual network using one or more native bearers. |
| 460 | * It may or may not be providing security services. |
| 461 | */ |
| 462 | public static final int TYPE_VPN = 17; |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 463 | |
| 464 | /** {@hide} */ |
Robert Greenwalt | 8283f88 | 2014-07-07 17:09:01 -0700 | [diff] [blame] | 465 | public static final int MAX_RADIO_TYPE = TYPE_VPN; |
| 466 | |
| 467 | /** {@hide} */ |
| 468 | public static final int MAX_NETWORK_TYPE = TYPE_VPN; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | |
Jianzheng Zhou | dcf03f3 | 2012-11-16 13:45:20 +0800 | [diff] [blame] | 470 | /** |
| 471 | * If you want to set the default network preference,you can directly |
| 472 | * change the networkAttributes array in framework's config.xml. |
| 473 | * |
| 474 | * @deprecated Since we support so many more networks now, the single |
| 475 | * network default network preference can't really express |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 476 | * the hierarchy. Instead, the default is defined by the |
Jianzheng Zhou | dcf03f3 | 2012-11-16 13:45:20 +0800 | [diff] [blame] | 477 | * networkAttributes in config.xml. You can determine |
Robert Greenwalt | 4c8b748 | 2012-12-07 09:56:50 -0800 | [diff] [blame] | 478 | * the current value by calling {@link #getNetworkPreference()} |
Jianzheng Zhou | dcf03f3 | 2012-11-16 13:45:20 +0800 | [diff] [blame] | 479 | * from an App. |
| 480 | */ |
| 481 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 482 | public static final int DEFAULT_NETWORK_PREFERENCE = TYPE_WIFI; |
| 483 | |
Jeff Sharkey | 625239a | 2012-09-26 22:03:49 -0700 | [diff] [blame] | 484 | /** |
Robert Greenwalt | 9ba9c58 | 2014-03-19 17:56:12 -0700 | [diff] [blame] | 485 | * @hide |
| 486 | */ |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 487 | public final static int REQUEST_ID_UNSET = 0; |
| 488 | |
Paul Jensen | 5d59e78 | 2014-07-11 12:28:19 -0400 | [diff] [blame] | 489 | /** |
| 490 | * A NetID indicating no Network is selected. |
| 491 | * Keep in sync with bionic/libc/dns/include/resolv_netid.h |
| 492 | * @hide |
| 493 | */ |
| 494 | public static final int NETID_UNSET = 0; |
| 495 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 496 | private final IConnectivityManager mService; |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 497 | /** |
| 498 | * A kludge to facilitate static access where a Context pointer isn't available, like in the |
| 499 | * case of the static set/getProcessDefaultNetwork methods and from the Network class. |
| 500 | * TODO: Remove this after deprecating the static methods in favor of non-static methods or |
| 501 | * methods that take a Context argument. |
| 502 | */ |
| 503 | private static ConnectivityManager sInstance; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 504 | |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 505 | private final Context mContext; |
| 506 | |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 507 | private INetworkManagementService mNMService; |
| 508 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 509 | /** |
| 510 | * Tests if a given integer represents a valid network type. |
| 511 | * @param networkType the type to be tested |
| 512 | * @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] | 513 | * @deprecated All APIs accepting a network type are deprecated. There should be no need to |
| 514 | * validate a network type. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 515 | */ |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 516 | public static boolean isNetworkTypeValid(int networkType) { |
Robert Greenwalt | 42acef3 | 2009-08-12 16:08:25 -0700 | [diff] [blame] | 517 | return networkType >= 0 && networkType <= MAX_NETWORK_TYPE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 518 | } |
| 519 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 520 | /** |
| 521 | * Returns a non-localized string representing a given network type. |
| 522 | * ONLY used for debugging output. |
| 523 | * @param type the type needing naming |
| 524 | * @return a String for the given type, or a string version of the type ("87") |
| 525 | * if no name is known. |
| 526 | * {@hide} |
| 527 | */ |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 528 | public static String getNetworkTypeName(int type) { |
| 529 | switch (type) { |
| 530 | case TYPE_MOBILE: |
| 531 | return "MOBILE"; |
| 532 | case TYPE_WIFI: |
| 533 | return "WIFI"; |
| 534 | case TYPE_MOBILE_MMS: |
| 535 | return "MOBILE_MMS"; |
| 536 | case TYPE_MOBILE_SUPL: |
| 537 | return "MOBILE_SUPL"; |
| 538 | case TYPE_MOBILE_DUN: |
| 539 | return "MOBILE_DUN"; |
| 540 | case TYPE_MOBILE_HIPRI: |
| 541 | return "MOBILE_HIPRI"; |
| 542 | case TYPE_WIMAX: |
| 543 | return "WIMAX"; |
| 544 | case TYPE_BLUETOOTH: |
| 545 | return "BLUETOOTH"; |
| 546 | case TYPE_DUMMY: |
| 547 | return "DUMMY"; |
| 548 | case TYPE_ETHERNET: |
| 549 | return "ETHERNET"; |
| 550 | case TYPE_MOBILE_FOTA: |
| 551 | return "MOBILE_FOTA"; |
| 552 | case TYPE_MOBILE_IMS: |
| 553 | return "MOBILE_IMS"; |
| 554 | case TYPE_MOBILE_CBS: |
| 555 | return "MOBILE_CBS"; |
repo sync | aea743a | 2011-07-29 23:55:49 -0700 | [diff] [blame] | 556 | case TYPE_WIFI_P2P: |
| 557 | return "WIFI_P2P"; |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 558 | case TYPE_MOBILE_IA: |
| 559 | return "MOBILE_IA"; |
Ram | 3e0e3bc | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 560 | case TYPE_MOBILE_EMERGENCY: |
| 561 | return "MOBILE_EMERGENCY"; |
Hui Lu | 1c5624a | 2014-01-15 11:05:36 -0500 | [diff] [blame] | 562 | case TYPE_PROXY: |
| 563 | return "PROXY"; |
Erik Kline | 37fbfa1 | 2014-11-19 17:23:41 +0900 | [diff] [blame] | 564 | case TYPE_VPN: |
| 565 | return "VPN"; |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 566 | default: |
| 567 | return Integer.toString(type); |
| 568 | } |
| 569 | } |
| 570 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 571 | /** |
| 572 | * Checks if a given type uses the cellular data connection. |
| 573 | * This should be replaced in the future by a network property. |
| 574 | * @param networkType the type to check |
| 575 | * @return a boolean - {@code true} if uses cellular network, else {@code false} |
| 576 | * {@hide} |
| 577 | */ |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 578 | public static boolean isNetworkTypeMobile(int networkType) { |
| 579 | switch (networkType) { |
| 580 | case TYPE_MOBILE: |
| 581 | case TYPE_MOBILE_MMS: |
| 582 | case TYPE_MOBILE_SUPL: |
| 583 | case TYPE_MOBILE_DUN: |
| 584 | case TYPE_MOBILE_HIPRI: |
| 585 | case TYPE_MOBILE_FOTA: |
| 586 | case TYPE_MOBILE_IMS: |
| 587 | case TYPE_MOBILE_CBS: |
Wink Saville | 5e56bc5 | 2013-07-29 15:00:57 -0700 | [diff] [blame] | 588 | case TYPE_MOBILE_IA: |
Ram | 3e0e3bc | 2014-06-26 11:03:44 -0700 | [diff] [blame] | 589 | case TYPE_MOBILE_EMERGENCY: |
Jeff Sharkey | d2a4587 | 2011-05-28 20:56:34 -0700 | [diff] [blame] | 590 | return true; |
| 591 | default: |
| 592 | return false; |
| 593 | } |
| 594 | } |
| 595 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 596 | /** |
Jeff Sharkey | 7a1c3fc | 2013-06-04 12:29:00 -0700 | [diff] [blame] | 597 | * Checks if the given network type is backed by a Wi-Fi radio. |
| 598 | * |
| 599 | * @hide |
| 600 | */ |
| 601 | public static boolean isNetworkTypeWifi(int networkType) { |
| 602 | switch (networkType) { |
| 603 | case TYPE_WIFI: |
| 604 | case TYPE_WIFI_P2P: |
| 605 | return true; |
| 606 | default: |
| 607 | return false; |
| 608 | } |
| 609 | } |
| 610 | |
| 611 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 612 | * Specifies the preferred network type. When the device has more |
| 613 | * than one type available the preferred network type will be used. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 614 | * |
| 615 | * @param preference the network type to prefer over all others. It is |
| 616 | * unspecified what happens to the old preferred network in the |
| 617 | * overall ordering. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 618 | * @deprecated Functionality has been removed as it no longer makes sense, |
| 619 | * with many more than two networks - we'd need an array to express |
| 620 | * preference. Instead we use dynamic network properties of |
| 621 | * the networks to describe their precedence. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 622 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 623 | public void setNetworkPreference(int preference) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 624 | } |
| 625 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 626 | /** |
| 627 | * Retrieves the current preferred network type. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 628 | * <p>This method requires the caller to hold the permission |
| 629 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 630 | * |
| 631 | * @return an integer representing the preferred network type |
| 632 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 633 | * @deprecated Functionality has been removed as it no longer makes sense, |
| 634 | * with many more than two networks - we'd need an array to express |
| 635 | * preference. Instead we use dynamic network properties of |
| 636 | * the networks to describe their precedence. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 637 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 638 | public int getNetworkPreference() { |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 639 | return TYPE_NONE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 640 | } |
| 641 | |
Scott Main | 671644c | 2011-10-06 19:02:28 -0700 | [diff] [blame] | 642 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 643 | * Returns details about the currently active default data network. When |
| 644 | * connected, this network is the default route for outgoing connections. |
| 645 | * You should always check {@link NetworkInfo#isConnected()} before initiating |
| 646 | * network traffic. This may return {@code null} when there is no default |
| 647 | * network. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 648 | * <p>This method requires the caller to hold the permission |
| 649 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 650 | * |
| 651 | * @return a {@link NetworkInfo} object for the current default network |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 652 | * or {@code null} if no default network is currently active |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 653 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 654 | public NetworkInfo getActiveNetworkInfo() { |
| 655 | try { |
| 656 | return mService.getActiveNetworkInfo(); |
| 657 | } catch (RemoteException e) { |
| 658 | return null; |
| 659 | } |
| 660 | } |
| 661 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 662 | /** |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 663 | * Returns a {@link Network} object corresponding to the currently active |
| 664 | * default data network. In the event that the current active default data |
| 665 | * network disconnects, the returned {@code Network} object will no longer |
| 666 | * be usable. This will return {@code null} when there is no default |
| 667 | * network. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 668 | * <p>This method requires the caller to hold the permission |
| 669 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 670 | * |
| 671 | * @return a {@link Network} object for the current default network or |
| 672 | * {@code null} if no default network is currently active |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 673 | */ |
| 674 | public Network getActiveNetwork() { |
| 675 | try { |
| 676 | return mService.getActiveNetwork(); |
| 677 | } catch (RemoteException e) { |
| 678 | return null; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 683 | * Returns details about the currently active default data network |
| 684 | * for a given uid. This is for internal use only to avoid spying |
| 685 | * other apps. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 686 | * <p>This method requires the caller to hold the permission |
| 687 | * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL} |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 688 | * |
| 689 | * @return a {@link NetworkInfo} object for the current default network |
| 690 | * for the given uid or {@code null} if no default network is |
| 691 | * available for the specified uid. |
| 692 | * |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 693 | * {@hide} |
| 694 | */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 695 | public NetworkInfo getActiveNetworkInfoForUid(int uid) { |
| 696 | try { |
| 697 | return mService.getActiveNetworkInfoForUid(uid); |
| 698 | } catch (RemoteException e) { |
| 699 | return null; |
| 700 | } |
| 701 | } |
| 702 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 703 | /** |
| 704 | * Returns connection status information about a particular |
| 705 | * network type. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 706 | * <p>This method requires the caller to hold the permission |
| 707 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 708 | * |
| 709 | * @param networkType integer specifying which networkType in |
| 710 | * which you're interested. |
| 711 | * @return a {@link NetworkInfo} object for the requested |
| 712 | * network type or {@code null} if the type is not |
| 713 | * supported by the device. |
| 714 | * |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 715 | * @deprecated This method does not support multiple connected networks |
| 716 | * of the same type. Use {@link #getAllNetworks} and |
| 717 | * {@link #getNetworkInfo(android.net.Network)} instead. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 718 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 719 | public NetworkInfo getNetworkInfo(int networkType) { |
| 720 | try { |
| 721 | return mService.getNetworkInfo(networkType); |
| 722 | } catch (RemoteException e) { |
| 723 | return null; |
| 724 | } |
| 725 | } |
| 726 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 727 | /** |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 728 | * Returns connection status information about a particular |
| 729 | * Network. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 730 | * <p>This method requires the caller to hold the permission |
| 731 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 732 | * |
| 733 | * @param network {@link Network} specifying which network |
| 734 | * in which you're interested. |
| 735 | * @return a {@link NetworkInfo} object for the requested |
| 736 | * network or {@code null} if the {@code Network} |
| 737 | * is not valid. |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 738 | */ |
| 739 | public NetworkInfo getNetworkInfo(Network network) { |
| 740 | try { |
| 741 | return mService.getNetworkInfoForNetwork(network); |
| 742 | } catch (RemoteException e) { |
| 743 | return null; |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 748 | * Returns connection status information about all network |
| 749 | * types supported by the device. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 750 | * <p>This method requires the caller to hold the permission |
| 751 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 752 | * |
| 753 | * @return an array of {@link NetworkInfo} objects. Check each |
| 754 | * {@link NetworkInfo#getType} for which type each applies. |
| 755 | * |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 756 | * @deprecated This method does not support multiple connected networks |
| 757 | * of the same type. Use {@link #getAllNetworks} and |
| 758 | * {@link #getNetworkInfo(android.net.Network)} instead. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 759 | */ |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 760 | public NetworkInfo[] getAllNetworkInfo() { |
| 761 | try { |
| 762 | return mService.getAllNetworkInfo(); |
| 763 | } catch (RemoteException e) { |
| 764 | return null; |
| 765 | } |
| 766 | } |
| 767 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 768 | /** |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 769 | * Returns the {@link Network} object currently serving a given type, or |
| 770 | * null if the given type is not connected. |
| 771 | * |
| 772 | * <p>This method requires the caller to hold the permission |
| 773 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
| 774 | * |
| 775 | * @hide |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 776 | * @deprecated This method does not support multiple connected networks |
| 777 | * of the same type. Use {@link #getAllNetworks} and |
| 778 | * {@link #getNetworkInfo(android.net.Network)} instead. |
Lorenzo Colitti | b57edc5 | 2014-08-22 17:10:50 -0700 | [diff] [blame] | 779 | */ |
| 780 | public Network getNetworkForType(int networkType) { |
| 781 | try { |
| 782 | return mService.getNetworkForType(networkType); |
| 783 | } catch (RemoteException e) { |
| 784 | return null; |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | /** |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 789 | * Returns an array of all {@link Network} currently tracked by the |
| 790 | * framework. |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 791 | * <p>This method requires the caller to hold the permission |
| 792 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 793 | * |
| 794 | * @return an array of {@link Network} objects. |
Robert Greenwalt | 73b6cbae | 2014-06-23 11:40:00 -0700 | [diff] [blame] | 795 | */ |
| 796 | public Network[] getAllNetworks() { |
| 797 | try { |
| 798 | return mService.getAllNetworks(); |
| 799 | } catch (RemoteException e) { |
| 800 | return null; |
| 801 | } |
| 802 | } |
| 803 | |
| 804 | /** |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 805 | * Returns an array of {@link android.net.NetworkCapabilities} objects, representing |
Lorenzo Colitti | 403aa26 | 2014-11-28 11:21:30 +0900 | [diff] [blame] | 806 | * the Networks that applications run by the given user will use by default. |
| 807 | * @hide |
| 808 | */ |
| 809 | public NetworkCapabilities[] getDefaultNetworkCapabilitiesForUser(int userId) { |
| 810 | try { |
| 811 | return mService.getDefaultNetworkCapabilitiesForUser(userId); |
| 812 | } catch (RemoteException e) { |
| 813 | return null; |
| 814 | } |
| 815 | } |
| 816 | |
| 817 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 818 | * Returns the IP information for the current default network. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 819 | * <p>This method requires the caller to hold the permission |
| 820 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 821 | * |
| 822 | * @return a {@link LinkProperties} object describing the IP info |
| 823 | * for the current default network, or {@code null} if there |
| 824 | * is no current default network. |
| 825 | * |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 826 | * {@hide} |
| 827 | */ |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 828 | public LinkProperties getActiveLinkProperties() { |
| 829 | try { |
| 830 | return mService.getActiveLinkProperties(); |
| 831 | } catch (RemoteException e) { |
| 832 | return null; |
| 833 | } |
| 834 | } |
| 835 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 836 | /** |
| 837 | * Returns the IP information for a given network type. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 838 | * <p>This method requires the caller to hold the permission |
| 839 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 840 | * |
| 841 | * @param networkType the network type of interest. |
| 842 | * @return a {@link LinkProperties} object describing the IP info |
| 843 | * for the given networkType, or {@code null} if there is |
| 844 | * no current default network. |
| 845 | * |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 846 | * {@hide} |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 847 | * @deprecated This method does not support multiple connected networks |
| 848 | * of the same type. Use {@link #getAllNetworks}, |
| 849 | * {@link #getNetworkInfo(android.net.Network)}, and |
| 850 | * {@link #getLinkProperties(android.net.Network)} instead. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 851 | */ |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 852 | public LinkProperties getLinkProperties(int networkType) { |
| 853 | try { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 854 | return mService.getLinkPropertiesForType(networkType); |
| 855 | } catch (RemoteException e) { |
| 856 | return null; |
| 857 | } |
| 858 | } |
| 859 | |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 860 | /** |
| 861 | * Get the {@link LinkProperties} for the given {@link Network}. This |
| 862 | * will return {@code null} if the network is unknown. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 863 | * <p>This method requires the caller to hold the permission |
| 864 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 865 | * |
| 866 | * @param network The {@link Network} object identifying the network in question. |
| 867 | * @return The {@link LinkProperties} for the network, or {@code null}. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 868 | */ |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 869 | public LinkProperties getLinkProperties(Network network) { |
| 870 | try { |
| 871 | return mService.getLinkProperties(network); |
| 872 | } catch (RemoteException e) { |
| 873 | return null; |
| 874 | } |
| 875 | } |
| 876 | |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 877 | /** |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 878 | * Get the {@link android.net.NetworkCapabilities} for the given {@link Network}. This |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 879 | * will return {@code null} if the network is unknown. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 880 | * <p>This method requires the caller to hold the permission |
| 881 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 882 | * |
| 883 | * @param network The {@link Network} object identifying the network in question. |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 884 | * @return The {@link android.net.NetworkCapabilities} for the network, or {@code null}. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 885 | */ |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 886 | public NetworkCapabilities getNetworkCapabilities(Network network) { |
| 887 | try { |
| 888 | return mService.getNetworkCapabilities(network); |
Robert Greenwalt | d192dad | 2010-09-14 09:18:02 -0700 | [diff] [blame] | 889 | } catch (RemoteException e) { |
| 890 | return null; |
| 891 | } |
| 892 | } |
| 893 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 894 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 895 | * Tells the underlying networking system that the caller wants to |
| 896 | * begin using the named feature. The interpretation of {@code feature} |
| 897 | * is completely up to each networking implementation. |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 898 | * <p>This method requires the caller to hold the permission |
| 899 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 900 | * @param networkType specifies which network the request pertains to |
| 901 | * @param feature the name of the feature to be used |
| 902 | * @return an integer value representing the outcome of the request. |
| 903 | * The interpretation of this value is specific to each networking |
| 904 | * implementation+feature combination, except that the value {@code -1} |
| 905 | * always indicates failure. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 906 | * |
Lorenzo Colitti | 2ea89e5 | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 907 | * @deprecated Deprecated in favor of the cleaner |
| 908 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} API. |
Dianne Hackborn | 692a244 | 2015-07-31 10:35:34 -0700 | [diff] [blame] | 909 | * 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] | 910 | * throw {@code UnsupportedOperationException} if called. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 911 | */ |
| 912 | public int startUsingNetworkFeature(int networkType, String feature) { |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 913 | checkLegacyRoutingApiAccess(); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 914 | NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature); |
| 915 | if (netCap == null) { |
| 916 | Log.d(TAG, "Can't satisfy startUsingNetworkFeature for " + networkType + ", " + |
| 917 | feature); |
| 918 | return PhoneConstants.APN_REQUEST_FAILED; |
| 919 | } |
| 920 | |
| 921 | NetworkRequest request = null; |
| 922 | synchronized (sLegacyRequests) { |
| 923 | LegacyRequest l = sLegacyRequests.get(netCap); |
| 924 | if (l != null) { |
| 925 | Log.d(TAG, "renewing startUsingNetworkFeature request " + l.networkRequest); |
| 926 | renewRequestLocked(l); |
| 927 | if (l.currentNetwork != null) { |
| 928 | return PhoneConstants.APN_ALREADY_ACTIVE; |
| 929 | } else { |
| 930 | return PhoneConstants.APN_REQUEST_STARTED; |
| 931 | } |
| 932 | } |
| 933 | |
| 934 | request = requestNetworkForFeatureLocked(netCap); |
| 935 | } |
| 936 | if (request != null) { |
Robert Greenwalt | 257ee5f | 2014-06-20 10:58:45 -0700 | [diff] [blame] | 937 | Log.d(TAG, "starting startUsingNetworkFeature for request " + request); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 938 | return PhoneConstants.APN_REQUEST_STARTED; |
| 939 | } else { |
| 940 | Log.d(TAG, " request Failed"); |
| 941 | return PhoneConstants.APN_REQUEST_FAILED; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 942 | } |
| 943 | } |
| 944 | |
| 945 | /** |
| 946 | * Tells the underlying networking system that the caller is finished |
| 947 | * using the named feature. The interpretation of {@code feature} |
| 948 | * is completely up to each networking implementation. |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 949 | * <p>This method requires the caller to hold the permission |
| 950 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 951 | * @param networkType specifies which network the request pertains to |
| 952 | * @param feature the name of the feature that is no longer needed |
| 953 | * @return an integer value representing the outcome of the request. |
| 954 | * The interpretation of this value is specific to each networking |
| 955 | * implementation+feature combination, except that the value {@code -1} |
| 956 | * always indicates failure. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 957 | * |
Robert Greenwalt | a36c074 | 2015-07-28 11:41:31 -0700 | [diff] [blame] | 958 | * @deprecated Deprecated in favor of the cleaner {@link #unregisterNetworkCallback} API. |
Dianne Hackborn | 692a244 | 2015-07-31 10:35:34 -0700 | [diff] [blame] | 959 | * 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] | 960 | * throw {@code UnsupportedOperationException} if called. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 961 | */ |
| 962 | public int stopUsingNetworkFeature(int networkType, String feature) { |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 963 | checkLegacyRoutingApiAccess(); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 964 | NetworkCapabilities netCap = networkCapabilitiesForFeature(networkType, feature); |
| 965 | if (netCap == null) { |
| 966 | Log.d(TAG, "Can't satisfy stopUsingNetworkFeature for " + networkType + ", " + |
| 967 | feature); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 968 | return -1; |
| 969 | } |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 970 | |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 971 | if (removeRequestForFeature(netCap)) { |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 972 | Log.d(TAG, "stopUsingNetworkFeature for " + networkType + ", " + feature); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 973 | } |
| 974 | return 1; |
| 975 | } |
| 976 | |
| 977 | private NetworkCapabilities networkCapabilitiesForFeature(int networkType, String feature) { |
| 978 | if (networkType == TYPE_MOBILE) { |
| 979 | int cap = -1; |
| 980 | if ("enableMMS".equals(feature)) { |
| 981 | cap = NetworkCapabilities.NET_CAPABILITY_MMS; |
| 982 | } else if ("enableSUPL".equals(feature)) { |
| 983 | cap = NetworkCapabilities.NET_CAPABILITY_SUPL; |
| 984 | } else if ("enableDUN".equals(feature) || "enableDUNAlways".equals(feature)) { |
| 985 | cap = NetworkCapabilities.NET_CAPABILITY_DUN; |
| 986 | } else if ("enableHIPRI".equals(feature)) { |
| 987 | cap = NetworkCapabilities.NET_CAPABILITY_INTERNET; |
| 988 | } else if ("enableFOTA".equals(feature)) { |
| 989 | cap = NetworkCapabilities.NET_CAPABILITY_FOTA; |
| 990 | } else if ("enableIMS".equals(feature)) { |
| 991 | cap = NetworkCapabilities.NET_CAPABILITY_IMS; |
| 992 | } else if ("enableCBS".equals(feature)) { |
| 993 | cap = NetworkCapabilities.NET_CAPABILITY_CBS; |
| 994 | } else { |
| 995 | return null; |
| 996 | } |
| 997 | NetworkCapabilities netCap = new NetworkCapabilities(); |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 998 | netCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR).addCapability(cap); |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 999 | netCap.maybeMarkCapabilitiesRestricted(); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1000 | return netCap; |
| 1001 | } else if (networkType == TYPE_WIFI) { |
| 1002 | if ("p2p".equals(feature)) { |
| 1003 | NetworkCapabilities netCap = new NetworkCapabilities(); |
| 1004 | netCap.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); |
Robert Greenwalt | 7569f18 | 2014-06-08 16:42:59 -0700 | [diff] [blame] | 1005 | netCap.addCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P); |
Paul Jensen | 487ffe7 | 2015-07-24 15:57:11 -0400 | [diff] [blame] | 1006 | netCap.maybeMarkCapabilitiesRestricted(); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1007 | return netCap; |
| 1008 | } |
| 1009 | } |
| 1010 | return null; |
| 1011 | } |
| 1012 | |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 1013 | /** |
| 1014 | * Guess what the network request was trying to say so that the resulting |
| 1015 | * network is accessible via the legacy (deprecated) API such as |
| 1016 | * requestRouteToHost. |
| 1017 | * This means we should try to be fairly preceise about transport and |
| 1018 | * capability but ignore things such as networkSpecifier. |
| 1019 | * If the request has more than one transport or capability it doesn't |
| 1020 | * match the old legacy requests (they selected only single transport/capability) |
| 1021 | * so this function cannot map the request to a single legacy type and |
| 1022 | * the resulting network will not be available to the legacy APIs. |
| 1023 | * |
| 1024 | * TODO - This should be removed when the legacy APIs are removed. |
| 1025 | */ |
Ye Wen | b87875e | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 1026 | private int inferLegacyTypeForNetworkCapabilities(NetworkCapabilities netCap) { |
| 1027 | if (netCap == null) { |
| 1028 | return TYPE_NONE; |
| 1029 | } |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 1030 | |
Ye Wen | b87875e | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 1031 | if (!netCap.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)) { |
| 1032 | return TYPE_NONE; |
| 1033 | } |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 1034 | |
| 1035 | String type = null; |
| 1036 | int result = TYPE_NONE; |
| 1037 | |
Ye Wen | b87875e | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 1038 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) { |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 1039 | type = "enableCBS"; |
| 1040 | result = TYPE_MOBILE_CBS; |
| 1041 | } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) { |
| 1042 | type = "enableIMS"; |
| 1043 | result = TYPE_MOBILE_IMS; |
| 1044 | } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) { |
| 1045 | type = "enableFOTA"; |
| 1046 | result = TYPE_MOBILE_FOTA; |
| 1047 | } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) { |
| 1048 | type = "enableDUN"; |
| 1049 | result = TYPE_MOBILE_DUN; |
| 1050 | } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) { |
Robert Greenwalt | 74ab4fa | 2015-08-28 12:37:54 -0700 | [diff] [blame] | 1051 | type = "enableSUPL"; |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 1052 | result = TYPE_MOBILE_SUPL; |
Robert Greenwalt | 74ab4fa | 2015-08-28 12:37:54 -0700 | [diff] [blame] | 1053 | // back out this hack for mms as they no longer need this and it's causing |
| 1054 | // device slowdowns - b/23350688 (note, supl still needs this) |
| 1055 | //} else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) { |
| 1056 | // type = "enableMMS"; |
| 1057 | // result = TYPE_MOBILE_MMS; |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 1058 | } else if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) { |
| 1059 | type = "enableHIPRI"; |
| 1060 | result = TYPE_MOBILE_HIPRI; |
Ye Wen | b87875e | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 1061 | } |
Robert Greenwalt | 06314e4 | 2014-10-29 14:04:06 -0700 | [diff] [blame] | 1062 | if (type != null) { |
| 1063 | NetworkCapabilities testCap = networkCapabilitiesForFeature(TYPE_MOBILE, type); |
| 1064 | if (testCap.equalsNetCapabilities(netCap) && testCap.equalsTransportTypes(netCap)) { |
| 1065 | return result; |
Ye Wen | b87875e | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 1066 | } |
| 1067 | } |
| 1068 | return TYPE_NONE; |
| 1069 | } |
| 1070 | |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 1071 | private int legacyTypeForNetworkCapabilities(NetworkCapabilities netCap) { |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1072 | if (netCap == null) return TYPE_NONE; |
| 1073 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_CBS)) { |
| 1074 | return TYPE_MOBILE_CBS; |
| 1075 | } |
| 1076 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_IMS)) { |
| 1077 | return TYPE_MOBILE_IMS; |
| 1078 | } |
| 1079 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_FOTA)) { |
| 1080 | return TYPE_MOBILE_FOTA; |
| 1081 | } |
| 1082 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_DUN)) { |
| 1083 | return TYPE_MOBILE_DUN; |
| 1084 | } |
| 1085 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_SUPL)) { |
| 1086 | return TYPE_MOBILE_SUPL; |
| 1087 | } |
| 1088 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_MMS)) { |
| 1089 | return TYPE_MOBILE_MMS; |
| 1090 | } |
| 1091 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) { |
| 1092 | return TYPE_MOBILE_HIPRI; |
| 1093 | } |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 1094 | if (netCap.hasCapability(NetworkCapabilities.NET_CAPABILITY_WIFI_P2P)) { |
| 1095 | return TYPE_WIFI_P2P; |
| 1096 | } |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1097 | return TYPE_NONE; |
| 1098 | } |
| 1099 | |
| 1100 | private static class LegacyRequest { |
| 1101 | NetworkCapabilities networkCapabilities; |
| 1102 | NetworkRequest networkRequest; |
| 1103 | int expireSequenceNumber; |
| 1104 | Network currentNetwork; |
| 1105 | int delay = -1; |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1106 | |
| 1107 | private void clearDnsBinding() { |
| 1108 | if (currentNetwork != null) { |
| 1109 | currentNetwork = null; |
| 1110 | setProcessDefaultNetworkForHostResolution(null); |
| 1111 | } |
| 1112 | } |
| 1113 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1114 | NetworkCallback networkCallback = new NetworkCallback() { |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1115 | @Override |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1116 | public void onAvailable(Network network) { |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1117 | currentNetwork = network; |
| 1118 | Log.d(TAG, "startUsingNetworkFeature got Network:" + network); |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 1119 | setProcessDefaultNetworkForHostResolution(network); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1120 | } |
| 1121 | @Override |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1122 | public void onLost(Network network) { |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1123 | if (network.equals(currentNetwork)) clearDnsBinding(); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1124 | Log.d(TAG, "startUsingNetworkFeature lost Network:" + network); |
| 1125 | } |
| 1126 | }; |
| 1127 | } |
| 1128 | |
Robert Greenwalt | fab50167 | 2014-07-23 11:44:01 -0700 | [diff] [blame] | 1129 | private static HashMap<NetworkCapabilities, LegacyRequest> sLegacyRequests = |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1130 | new HashMap<NetworkCapabilities, LegacyRequest>(); |
| 1131 | |
| 1132 | private NetworkRequest findRequestForFeature(NetworkCapabilities netCap) { |
| 1133 | synchronized (sLegacyRequests) { |
| 1134 | LegacyRequest l = sLegacyRequests.get(netCap); |
| 1135 | if (l != null) return l.networkRequest; |
| 1136 | } |
| 1137 | return null; |
| 1138 | } |
| 1139 | |
| 1140 | private void renewRequestLocked(LegacyRequest l) { |
| 1141 | l.expireSequenceNumber++; |
| 1142 | Log.d(TAG, "renewing request to seqNum " + l.expireSequenceNumber); |
| 1143 | sendExpireMsgForFeature(l.networkCapabilities, l.expireSequenceNumber, l.delay); |
| 1144 | } |
| 1145 | |
| 1146 | private void expireRequest(NetworkCapabilities netCap, int sequenceNum) { |
| 1147 | int ourSeqNum = -1; |
| 1148 | synchronized (sLegacyRequests) { |
| 1149 | LegacyRequest l = sLegacyRequests.get(netCap); |
| 1150 | if (l == null) return; |
| 1151 | ourSeqNum = l.expireSequenceNumber; |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1152 | if (l.expireSequenceNumber == sequenceNum) removeRequestForFeature(netCap); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1153 | } |
| 1154 | Log.d(TAG, "expireRequest with " + ourSeqNum + ", " + sequenceNum); |
| 1155 | } |
| 1156 | |
| 1157 | private NetworkRequest requestNetworkForFeatureLocked(NetworkCapabilities netCap) { |
| 1158 | int delay = -1; |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 1159 | int type = legacyTypeForNetworkCapabilities(netCap); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1160 | try { |
| 1161 | delay = mService.getRestoreDefaultNetworkDelay(type); |
| 1162 | } catch (RemoteException e) {} |
| 1163 | LegacyRequest l = new LegacyRequest(); |
| 1164 | l.networkCapabilities = netCap; |
| 1165 | l.delay = delay; |
| 1166 | l.expireSequenceNumber = 0; |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1167 | l.networkRequest = sendRequestForNetwork(netCap, l.networkCallback, 0, |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 1168 | REQUEST, type); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1169 | if (l.networkRequest == null) return null; |
| 1170 | sLegacyRequests.put(netCap, l); |
| 1171 | sendExpireMsgForFeature(netCap, l.expireSequenceNumber, delay); |
| 1172 | return l.networkRequest; |
| 1173 | } |
| 1174 | |
| 1175 | private void sendExpireMsgForFeature(NetworkCapabilities netCap, int seqNum, int delay) { |
| 1176 | if (delay >= 0) { |
| 1177 | Log.d(TAG, "sending expire msg with seqNum " + seqNum + " and delay " + delay); |
| 1178 | Message msg = sCallbackHandler.obtainMessage(EXPIRE_LEGACY_REQUEST, seqNum, 0, netCap); |
| 1179 | sCallbackHandler.sendMessageDelayed(msg, delay); |
| 1180 | } |
| 1181 | } |
| 1182 | |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1183 | private boolean removeRequestForFeature(NetworkCapabilities netCap) { |
| 1184 | final LegacyRequest l; |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1185 | synchronized (sLegacyRequests) { |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1186 | l = sLegacyRequests.remove(netCap); |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 1187 | } |
Paul Jensen | 9ffb53c | 2014-12-17 10:39:34 -0500 | [diff] [blame] | 1188 | if (l == null) return false; |
| 1189 | unregisterNetworkCallback(l.networkCallback); |
| 1190 | l.clearDnsBinding(); |
| 1191 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1192 | } |
| 1193 | |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1194 | /** @hide */ |
| 1195 | public static class PacketKeepaliveCallback { |
| 1196 | /** The requested keepalive was successfully started. */ |
| 1197 | public void onStarted() {} |
| 1198 | /** The keepalive was successfully stopped. */ |
| 1199 | public void onStopped() {} |
| 1200 | /** An error occurred. */ |
| 1201 | public void onError(int error) {} |
| 1202 | } |
| 1203 | |
| 1204 | /** |
| 1205 | * Allows applications to request that the system periodically send specific packets on their |
| 1206 | * behalf, using hardware offload to save battery power. |
| 1207 | * |
| 1208 | * To request that the system send keepalives, call one of the methods that return a |
| 1209 | * {@link ConnectivityManager.PacketKeepalive} object, such as {@link #startNattKeepalive}, |
| 1210 | * passing in a non-null callback. If the callback is successfully started, the callback's |
| 1211 | * {@code onStarted} method will be called. If an error occurs, {@code onError} will be called, |
| 1212 | * specifying one of the {@code ERROR_*} constants in this class. |
| 1213 | * |
| 1214 | * To stop an existing keepalive, call {@link stop}. The system will call {@code onStopped} if |
| 1215 | * the operation was successfull or {@code onError} if an error occurred. |
| 1216 | * |
| 1217 | * @hide |
| 1218 | */ |
| 1219 | public class PacketKeepalive { |
| 1220 | |
| 1221 | private static final String TAG = "PacketKeepalive"; |
| 1222 | |
| 1223 | /** @hide */ |
| 1224 | public static final int SUCCESS = 0; |
| 1225 | |
| 1226 | /** @hide */ |
| 1227 | public static final int NO_KEEPALIVE = -1; |
| 1228 | |
| 1229 | /** @hide */ |
| 1230 | public static final int BINDER_DIED = -10; |
| 1231 | |
| 1232 | /** The specified {@code Network} is not connected. */ |
| 1233 | public static final int ERROR_INVALID_NETWORK = -20; |
| 1234 | /** The specified IP addresses are invalid. For example, the specified source IP address is |
| 1235 | * not configured on the specified {@code Network}. */ |
| 1236 | public static final int ERROR_INVALID_IP_ADDRESS = -21; |
| 1237 | /** The requested port is invalid. */ |
| 1238 | public static final int ERROR_INVALID_PORT = -22; |
| 1239 | /** The packet length is invalid (e.g., too long). */ |
| 1240 | public static final int ERROR_INVALID_LENGTH = -23; |
| 1241 | /** The packet transmission interval is invalid (e.g., too short). */ |
| 1242 | public static final int ERROR_INVALID_INTERVAL = -24; |
| 1243 | |
| 1244 | /** The hardware does not support this request. */ |
| 1245 | public static final int ERROR_HARDWARE_UNSUPPORTED = -30; |
Lorenzo Colitti | 9d1284e | 2015-09-08 16:46:36 +0900 | [diff] [blame] | 1246 | /** The hardware returned an error. */ |
| 1247 | public static final int ERROR_HARDWARE_ERROR = -31; |
Lorenzo Colitti | 8bf977d | 2015-06-15 14:29:22 +0900 | [diff] [blame] | 1248 | |
| 1249 | public static final int NATT_PORT = 4500; |
| 1250 | |
| 1251 | private final Network mNetwork; |
| 1252 | private final PacketKeepaliveCallback mCallback; |
| 1253 | private final Looper mLooper; |
| 1254 | private final Messenger mMessenger; |
| 1255 | |
| 1256 | private volatile Integer mSlot; |
| 1257 | |
| 1258 | void stopLooper() { |
| 1259 | mLooper.quit(); |
| 1260 | } |
| 1261 | |
| 1262 | public void stop() { |
| 1263 | try { |
| 1264 | mService.stopKeepalive(mNetwork, mSlot); |
| 1265 | } catch (RemoteException e) { |
| 1266 | Log.e(TAG, "Error stopping packet keepalive: ", e); |
| 1267 | stopLooper(); |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | private PacketKeepalive(Network network, PacketKeepaliveCallback callback) { |
| 1272 | checkNotNull(network, "network cannot be null"); |
| 1273 | checkNotNull(callback, "callback cannot be null"); |
| 1274 | mNetwork = network; |
| 1275 | mCallback = callback; |
| 1276 | HandlerThread thread = new HandlerThread(TAG); |
| 1277 | thread.start(); |
| 1278 | mLooper = thread.getLooper(); |
| 1279 | mMessenger = new Messenger(new Handler(mLooper) { |
| 1280 | @Override |
| 1281 | public void handleMessage(Message message) { |
| 1282 | switch (message.what) { |
| 1283 | case NetworkAgent.EVENT_PACKET_KEEPALIVE: |
| 1284 | int error = message.arg2; |
| 1285 | try { |
| 1286 | if (error == SUCCESS) { |
| 1287 | if (mSlot == null) { |
| 1288 | mSlot = message.arg1; |
| 1289 | mCallback.onStarted(); |
| 1290 | } else { |
| 1291 | mSlot = null; |
| 1292 | stopLooper(); |
| 1293 | mCallback.onStopped(); |
| 1294 | } |
| 1295 | } else { |
| 1296 | stopLooper(); |
| 1297 | mCallback.onError(error); |
| 1298 | } |
| 1299 | } catch (Exception e) { |
| 1300 | Log.e(TAG, "Exception in keepalive callback(" + error + ")", e); |
| 1301 | } |
| 1302 | break; |
| 1303 | default: |
| 1304 | Log.e(TAG, "Unhandled message " + Integer.toHexString(message.what)); |
| 1305 | break; |
| 1306 | } |
| 1307 | } |
| 1308 | }); |
| 1309 | } |
| 1310 | } |
| 1311 | |
| 1312 | /** |
| 1313 | * Starts an IPsec NAT-T keepalive packet with the specified parameters. |
| 1314 | * |
| 1315 | * @hide |
| 1316 | */ |
| 1317 | public PacketKeepalive startNattKeepalive( |
| 1318 | Network network, int intervalSeconds, PacketKeepaliveCallback callback, |
| 1319 | InetAddress srcAddr, int srcPort, InetAddress dstAddr) { |
| 1320 | final PacketKeepalive k = new PacketKeepalive(network, callback); |
| 1321 | try { |
| 1322 | mService.startNattKeepalive(network, intervalSeconds, k.mMessenger, new Binder(), |
| 1323 | srcAddr.getHostAddress(), srcPort, dstAddr.getHostAddress()); |
| 1324 | } catch (RemoteException e) { |
| 1325 | Log.e(TAG, "Error starting packet keepalive: ", e); |
| 1326 | k.stopLooper(); |
| 1327 | return null; |
| 1328 | } |
| 1329 | return k; |
| 1330 | } |
| 1331 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1332 | /** |
| 1333 | * Ensure that a network route exists to deliver traffic to the specified |
| 1334 | * host via the specified network interface. An attempt to add a route that |
| 1335 | * already exists is ignored, but treated as successful. |
Nicolas Falliere | 9530e3a | 2012-06-18 17:21:06 -0700 | [diff] [blame] | 1336 | * <p>This method requires the caller to hold the permission |
| 1337 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1338 | * @param networkType the type of the network over which traffic to the specified |
| 1339 | * host is to be routed |
| 1340 | * @param hostAddress the IP address of the host to which the route is desired |
| 1341 | * @return {@code true} on success, {@code false} on failure |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1342 | * |
Lorenzo Colitti | 2ea89e5 | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 1343 | * @deprecated Deprecated in favor of the |
| 1344 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)}, |
| 1345 | * {@link #bindProcessToNetwork} and {@link Network#getSocketFactory} API. |
Dianne Hackborn | 692a244 | 2015-07-31 10:35:34 -0700 | [diff] [blame] | 1346 | * 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] | 1347 | * throw {@code UnsupportedOperationException} if called. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1348 | */ |
| 1349 | public boolean requestRouteToHost(int networkType, int hostAddress) { |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1350 | return requestRouteToHostAddress(networkType, NetworkUtils.intToInetAddress(hostAddress)); |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | /** |
| 1354 | * Ensure that a network route exists to deliver traffic to the specified |
| 1355 | * host via the specified network interface. An attempt to add a route that |
| 1356 | * already exists is ignored, but treated as successful. |
Jake Hamby | 8f9b33e | 2014-01-15 13:08:03 -0800 | [diff] [blame] | 1357 | * <p>This method requires the caller to hold the permission |
| 1358 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}. |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 1359 | * @param networkType the type of the network over which traffic to the specified |
| 1360 | * host is to be routed |
| 1361 | * @param hostAddress the IP address of the host to which the route is desired |
| 1362 | * @return {@code true} on success, {@code false} on failure |
| 1363 | * @hide |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1364 | * @deprecated Deprecated in favor of the {@link #requestNetwork} and |
Lorenzo Colitti | 2ea89e5 | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 1365 | * {@link #bindProcessToNetwork} API. |
Robert Greenwalt | 585ac0f | 2010-08-27 09:24:29 -0700 | [diff] [blame] | 1366 | */ |
| 1367 | public boolean requestRouteToHostAddress(int networkType, InetAddress hostAddress) { |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1368 | checkLegacyRoutingApiAccess(); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1369 | try { |
Sreeram Ramachandran | 03666c7 | 2014-07-19 23:21:46 -0700 | [diff] [blame] | 1370 | return mService.requestRouteToHostAddress(networkType, hostAddress.getAddress()); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1371 | } catch (RemoteException e) { |
| 1372 | return false; |
| 1373 | } |
| 1374 | } |
| 1375 | |
| 1376 | /** |
| 1377 | * Returns the value of the setting for background data usage. If false, |
| 1378 | * applications should not use the network if the application is not in the |
| 1379 | * foreground. Developers should respect this setting, and check the value |
| 1380 | * of this before performing any background data operations. |
| 1381 | * <p> |
| 1382 | * All applications that have background services that use the network |
| 1383 | * should listen to {@link #ACTION_BACKGROUND_DATA_SETTING_CHANGED}. |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1384 | * <p> |
Scott Main | 4cc5333 | 2011-10-06 18:32:43 -0700 | [diff] [blame] | 1385 | * @deprecated As of {@link VERSION_CODES#ICE_CREAM_SANDWICH}, availability of |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1386 | * background data depends on several combined factors, and this method will |
| 1387 | * always return {@code true}. Instead, when background data is unavailable, |
| 1388 | * {@link #getActiveNetworkInfo()} will now appear disconnected. |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 1389 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1390 | * @return Whether background data usage is allowed. |
| 1391 | */ |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1392 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1393 | public boolean getBackgroundDataSetting() { |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1394 | // assume that background data is allowed; final authority is |
| 1395 | // NetworkInfo which may be blocked. |
| 1396 | return true; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | /** |
| 1400 | * Sets the value of the setting for background data usage. |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1401 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1402 | * @param allowBackgroundData Whether an application should use data while |
| 1403 | * it is in the background. |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1404 | * |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1405 | * @attr ref android.Manifest.permission#CHANGE_BACKGROUND_DATA_SETTING |
| 1406 | * @see #getBackgroundDataSetting() |
| 1407 | * @hide |
| 1408 | */ |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1409 | @Deprecated |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1410 | public void setBackgroundDataSetting(boolean allowBackgroundData) { |
Jeff Sharkey | 3a844fc | 2011-08-16 14:37:57 -0700 | [diff] [blame] | 1411 | // ignored |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1412 | } |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1413 | |
| 1414 | /** |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1415 | * Return quota status for the current active network, or {@code null} if no |
| 1416 | * network is active. Quota status can change rapidly, so these values |
| 1417 | * shouldn't be cached. |
Jeff Sharkey | 44a3e0d | 2011-10-06 10:50:09 -0700 | [diff] [blame] | 1418 | * |
Paul Jensen | 0d719ca | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 1419 | * <p>This method requires the caller to hold the permission |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1420 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
| 1421 | * |
Jeff Sharkey | 44a3e0d | 2011-10-06 10:50:09 -0700 | [diff] [blame] | 1422 | * @hide |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1423 | */ |
| 1424 | public NetworkQuotaInfo getActiveNetworkQuotaInfo() { |
| 1425 | try { |
| 1426 | return mService.getActiveNetworkQuotaInfo(); |
| 1427 | } catch (RemoteException e) { |
| 1428 | return null; |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | /** |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1433 | * @hide |
Robert Greenwalt | afa05c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 1434 | * @deprecated Talk to TelephonyManager directly |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1435 | */ |
| 1436 | public boolean getMobileDataEnabled() { |
Robert Greenwalt | afa05c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 1437 | IBinder b = ServiceManager.getService(Context.TELEPHONY_SERVICE); |
| 1438 | if (b != null) { |
| 1439 | try { |
| 1440 | ITelephony it = ITelephony.Stub.asInterface(b); |
Wink Saville | 36ffb04 | 2014-12-05 11:10:30 -0800 | [diff] [blame] | 1441 | int subId = SubscriptionManager.getDefaultDataSubId(); |
| 1442 | Log.d("ConnectivityManager", "getMobileDataEnabled()+ subId=" + subId); |
| 1443 | boolean retVal = it.getDataEnabled(subId); |
| 1444 | Log.d("ConnectivityManager", "getMobileDataEnabled()- subId=" + subId |
| 1445 | + " retVal=" + retVal); |
| 1446 | return retVal; |
Robert Greenwalt | afa05c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 1447 | } catch (RemoteException e) { } |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1448 | } |
Wink Saville | 36ffb04 | 2014-12-05 11:10:30 -0800 | [diff] [blame] | 1449 | Log.d("ConnectivityManager", "getMobileDataEnabled()- remote exception retVal=false"); |
Robert Greenwalt | afa05c0 | 2014-05-21 20:04:36 -0700 | [diff] [blame] | 1450 | return false; |
Robert Greenwalt | c03fa50 | 2010-02-23 18:58:05 -0800 | [diff] [blame] | 1451 | } |
| 1452 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1453 | /** |
Robert Greenwalt | b248987 | 2014-09-04 16:44:35 -0700 | [diff] [blame] | 1454 | * Callback for use with {@link ConnectivityManager#addDefaultNetworkActiveListener} |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1455 | * 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] | 1456 | */ |
| 1457 | public interface OnNetworkActiveListener { |
| 1458 | /** |
| 1459 | * Called on the main thread of the process to report that the current data network |
| 1460 | * has become active, and it is now a good time to perform any pending network |
| 1461 | * operations. Note that this listener only tells you when the network becomes |
| 1462 | * active; if at any other time you want to know whether it is active (and thus okay |
| 1463 | * to initiate network traffic), you can retrieve its instantaneous state with |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1464 | * {@link ConnectivityManager#isDefaultNetworkActive}. |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1465 | */ |
| 1466 | public void onNetworkActive(); |
| 1467 | } |
| 1468 | |
| 1469 | private INetworkManagementService getNetworkManagementService() { |
| 1470 | synchronized (this) { |
| 1471 | if (mNMService != null) { |
| 1472 | return mNMService; |
| 1473 | } |
| 1474 | IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE); |
| 1475 | mNMService = INetworkManagementService.Stub.asInterface(b); |
| 1476 | return mNMService; |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | private final ArrayMap<OnNetworkActiveListener, INetworkActivityListener> |
| 1481 | mNetworkActivityListeners |
| 1482 | = new ArrayMap<OnNetworkActiveListener, INetworkActivityListener>(); |
| 1483 | |
| 1484 | /** |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1485 | * Start listening to reports when the system's default data network is active, meaning it is |
| 1486 | * a good time to perform network traffic. Use {@link #isDefaultNetworkActive()} |
| 1487 | * to determine the current state of the system's default network after registering the |
| 1488 | * listener. |
| 1489 | * <p> |
| 1490 | * If the process default network has been set with |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 1491 | * {@link ConnectivityManager#bindProcessToNetwork} this function will not |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1492 | * reflect the process's default, but the system default. |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1493 | * |
| 1494 | * @param l The listener to be told when the network is active. |
| 1495 | */ |
Robert Greenwalt | b248987 | 2014-09-04 16:44:35 -0700 | [diff] [blame] | 1496 | public void addDefaultNetworkActiveListener(final OnNetworkActiveListener l) { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1497 | INetworkActivityListener rl = new INetworkActivityListener.Stub() { |
| 1498 | @Override |
| 1499 | public void onNetworkActive() throws RemoteException { |
| 1500 | l.onNetworkActive(); |
| 1501 | } |
| 1502 | }; |
| 1503 | |
| 1504 | try { |
| 1505 | getNetworkManagementService().registerNetworkActivityListener(rl); |
| 1506 | mNetworkActivityListeners.put(l, rl); |
| 1507 | } catch (RemoteException e) { |
| 1508 | } |
| 1509 | } |
| 1510 | |
| 1511 | /** |
| 1512 | * Remove network active listener previously registered with |
Robert Greenwalt | b248987 | 2014-09-04 16:44:35 -0700 | [diff] [blame] | 1513 | * {@link #addDefaultNetworkActiveListener}. |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1514 | * |
| 1515 | * @param l Previously registered listener. |
| 1516 | */ |
Robert Greenwalt | b248987 | 2014-09-04 16:44:35 -0700 | [diff] [blame] | 1517 | public void removeDefaultNetworkActiveListener(OnNetworkActiveListener l) { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1518 | INetworkActivityListener rl = mNetworkActivityListeners.get(l); |
| 1519 | if (rl == null) { |
| 1520 | throw new IllegalArgumentException("Listener not registered: " + l); |
| 1521 | } |
| 1522 | try { |
| 1523 | getNetworkManagementService().unregisterNetworkActivityListener(rl); |
| 1524 | } catch (RemoteException e) { |
| 1525 | } |
| 1526 | } |
| 1527 | |
| 1528 | /** |
| 1529 | * Return whether the data network is currently active. An active network means that |
| 1530 | * it is currently in a high power state for performing data transmission. On some |
| 1531 | * types of networks, it may be expensive to move and stay in such a state, so it is |
| 1532 | * more power efficient to batch network traffic together when the radio is already in |
| 1533 | * this state. This method tells you whether right now is currently a good time to |
| 1534 | * initiate network traffic, as the network is already active. |
| 1535 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 1536 | public boolean isDefaultNetworkActive() { |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1537 | try { |
| 1538 | return getNetworkManagementService().isNetworkActive(); |
| 1539 | } catch (RemoteException e) { |
| 1540 | } |
| 1541 | return false; |
| 1542 | } |
| 1543 | |
| 1544 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1545 | * {@hide} |
| 1546 | */ |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 1547 | public ConnectivityManager(Context context, IConnectivityManager service) { |
| 1548 | mContext = checkNotNull(context, "missing context"); |
Jeff Sharkey | f0ceede | 2011-08-02 17:22:34 -0700 | [diff] [blame] | 1549 | mService = checkNotNull(service, "missing IConnectivityManager"); |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 1550 | sInstance = this; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1551 | } |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1552 | |
Jeff Sharkey | 8fc27e8 | 2012-04-04 20:40:58 -0700 | [diff] [blame] | 1553 | /** {@hide} */ |
| 1554 | public static ConnectivityManager from(Context context) { |
| 1555 | return (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); |
| 1556 | } |
| 1557 | |
Robert Greenwalt | edb4766 | 2014-09-16 17:54:19 -0700 | [diff] [blame] | 1558 | /** {@hide */ |
| 1559 | public static final void enforceTetherChangePermission(Context context) { |
| 1560 | if (context.getResources().getStringArray( |
| 1561 | com.android.internal.R.array.config_mobile_hotspot_provision_app).length == 2) { |
| 1562 | // Have a provisioning app - must only let system apps (which check this app) |
| 1563 | // turn on tethering |
| 1564 | context.enforceCallingOrSelfPermission( |
| 1565 | android.Manifest.permission.CONNECTIVITY_INTERNAL, "ConnectivityService"); |
| 1566 | } else { |
Billy Lau | a7238a3 | 2015-08-01 12:45:02 +0100 | [diff] [blame] | 1567 | int uid = Binder.getCallingUid(); |
| 1568 | Settings.checkAndNoteChangeNetworkStateOperation(context, uid, Settings |
| 1569 | .getPackageNameForUid(context, uid), true); |
Robert Greenwalt | edb4766 | 2014-09-16 17:54:19 -0700 | [diff] [blame] | 1570 | } |
| 1571 | } |
| 1572 | |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1573 | /** |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 1574 | * @deprecated - use getSystemService. This is a kludge to support static access in certain |
| 1575 | * situations where a Context pointer is unavailable. |
| 1576 | * @hide |
| 1577 | */ |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 1578 | static ConnectivityManager getInstanceOrNull() { |
| 1579 | return sInstance; |
| 1580 | } |
| 1581 | |
| 1582 | /** |
| 1583 | * @deprecated - use getSystemService. This is a kludge to support static access in certain |
| 1584 | * situations where a Context pointer is unavailable. |
| 1585 | * @hide |
| 1586 | */ |
| 1587 | private static ConnectivityManager getInstance() { |
| 1588 | if (getInstanceOrNull() == null) { |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 1589 | throw new IllegalStateException("No ConnectivityManager yet constructed"); |
| 1590 | } |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 1591 | return getInstanceOrNull(); |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 1592 | } |
| 1593 | |
| 1594 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1595 | * Get the set of tetherable, available interfaces. This list is limited by |
| 1596 | * device configuration and current interface existence. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1597 | * <p>This method requires the caller to hold the permission |
| 1598 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1599 | * |
| 1600 | * @return an array of 0 or more Strings of tetherable interface names. |
| 1601 | * |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1602 | * {@hide} |
| 1603 | */ |
| 1604 | public String[] getTetherableIfaces() { |
| 1605 | try { |
| 1606 | return mService.getTetherableIfaces(); |
| 1607 | } catch (RemoteException e) { |
| 1608 | return new String[0]; |
| 1609 | } |
| 1610 | } |
| 1611 | |
| 1612 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1613 | * Get the set of tethered interfaces. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1614 | * <p>This method requires the caller to hold the permission |
| 1615 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1616 | * |
| 1617 | * @return an array of 0 or more String of currently tethered interface names. |
| 1618 | * |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1619 | * {@hide} |
| 1620 | */ |
| 1621 | public String[] getTetheredIfaces() { |
| 1622 | try { |
| 1623 | return mService.getTetheredIfaces(); |
| 1624 | } catch (RemoteException e) { |
| 1625 | return new String[0]; |
| 1626 | } |
| 1627 | } |
| 1628 | |
| 1629 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1630 | * Get the set of interface names which attempted to tether but |
| 1631 | * failed. Re-attempting to tether may cause them to reset to the Tethered |
| 1632 | * state. Alternatively, causing the interface to be destroyed and recreated |
| 1633 | * may cause them to reset to the available state. |
| 1634 | * {@link ConnectivityManager#getLastTetherError} can be used to get more |
| 1635 | * information on the cause of the errors. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1636 | * <p>This method requires the caller to hold the permission |
| 1637 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1638 | * |
| 1639 | * @return an array of 0 or more String indicating the interface names |
| 1640 | * which failed to tether. |
| 1641 | * |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1642 | * {@hide} |
| 1643 | */ |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1644 | public String[] getTetheringErroredIfaces() { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1645 | try { |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1646 | return mService.getTetheringErroredIfaces(); |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1647 | } catch (RemoteException e) { |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1648 | return new String[0]; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1649 | } |
| 1650 | } |
| 1651 | |
| 1652 | /** |
Robert Greenwalt | 9c7e2c2 | 2014-06-23 14:53:42 -0700 | [diff] [blame] | 1653 | * Get the set of tethered dhcp ranges. |
| 1654 | * |
| 1655 | * @return an array of 0 or more {@code String} of tethered dhcp ranges. |
| 1656 | * {@hide} |
| 1657 | */ |
| 1658 | public String[] getTetheredDhcpRanges() { |
| 1659 | try { |
| 1660 | return mService.getTetheredDhcpRanges(); |
| 1661 | } catch (RemoteException e) { |
| 1662 | return new String[0]; |
| 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1667 | * Attempt to tether the named interface. This will setup a dhcp server |
| 1668 | * on the interface, forward and NAT IP packets and forward DNS requests |
| 1669 | * to the best active upstream network interface. Note that if no upstream |
| 1670 | * IP network interface is available, dhcp will still run and traffic will be |
| 1671 | * allowed between the tethered devices and this device, though upstream net |
| 1672 | * access will of course fail until an upstream network interface becomes |
| 1673 | * active. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1674 | * <p>This method requires the caller to hold the permission |
| 1675 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1676 | * |
| 1677 | * @param iface the interface name to tether. |
| 1678 | * @return error a {@code TETHER_ERROR} value indicating success or failure type |
| 1679 | * |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1680 | * {@hide} |
| 1681 | */ |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1682 | public int tether(String iface) { |
| 1683 | try { |
| 1684 | return mService.tether(iface); |
| 1685 | } catch (RemoteException e) { |
| 1686 | return TETHER_ERROR_SERVICE_UNAVAIL; |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1691 | * Stop tethering the named interface. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1692 | * <p>This method requires the caller to hold the permission |
| 1693 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1694 | * |
| 1695 | * @param iface the interface name to untether. |
| 1696 | * @return error a {@code TETHER_ERROR} value indicating success or failure type |
| 1697 | * |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1698 | * {@hide} |
| 1699 | */ |
| 1700 | public int untether(String iface) { |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1701 | try { |
| 1702 | return mService.untether(iface); |
| 1703 | } catch (RemoteException e) { |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1704 | return TETHER_ERROR_SERVICE_UNAVAIL; |
Robert Greenwalt | d0e18ff | 2010-01-26 11:40:34 -0800 | [diff] [blame] | 1705 | } |
| 1706 | } |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1707 | |
| 1708 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1709 | * Check if the device allows for tethering. It may be disabled via |
Dianne Hackborn | 77b987f | 2014-02-26 16:20:52 -0800 | [diff] [blame] | 1710 | * {@code ro.tether.denied} system property, Settings.TETHER_SUPPORTED or |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1711 | * due to device configuration. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1712 | * <p>This method requires the caller to hold the permission |
| 1713 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1714 | * |
| 1715 | * @return a boolean - {@code true} indicating Tethering is supported. |
| 1716 | * |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1717 | * {@hide} |
| 1718 | */ |
| 1719 | public boolean isTetheringSupported() { |
| 1720 | try { |
| 1721 | return mService.isTetheringSupported(); |
| 1722 | } catch (RemoteException e) { |
| 1723 | return false; |
| 1724 | } |
| 1725 | } |
| 1726 | |
| 1727 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1728 | * Get the list of regular expressions that define any tetherable |
| 1729 | * USB network interfaces. If USB tethering is not supported by the |
| 1730 | * device, this list should be empty. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1731 | * <p>This method requires the caller to hold the permission |
| 1732 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1733 | * |
| 1734 | * @return an array of 0 or more regular expression Strings defining |
| 1735 | * what interfaces are considered tetherable usb interfaces. |
| 1736 | * |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1737 | * {@hide} |
| 1738 | */ |
| 1739 | public String[] getTetherableUsbRegexs() { |
| 1740 | try { |
| 1741 | return mService.getTetherableUsbRegexs(); |
| 1742 | } catch (RemoteException e) { |
| 1743 | return new String[0]; |
| 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1748 | * Get the list of regular expressions that define any tetherable |
| 1749 | * Wifi network interfaces. If Wifi tethering is not supported by the |
| 1750 | * device, this list should be empty. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1751 | * <p>This method requires the caller to hold the permission |
| 1752 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1753 | * |
| 1754 | * @return an array of 0 or more regular expression Strings defining |
| 1755 | * what interfaces are considered tetherable wifi interfaces. |
| 1756 | * |
Robert Greenwalt | 2a091d7 | 2010-02-11 18:18:40 -0800 | [diff] [blame] | 1757 | * {@hide} |
| 1758 | */ |
| 1759 | public String[] getTetherableWifiRegexs() { |
| 1760 | try { |
| 1761 | return mService.getTetherableWifiRegexs(); |
| 1762 | } catch (RemoteException e) { |
| 1763 | return new String[0]; |
| 1764 | } |
| 1765 | } |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1766 | |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 1767 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1768 | * Get the list of regular expressions that define any tetherable |
| 1769 | * Bluetooth network interfaces. If Bluetooth tethering is not supported by the |
| 1770 | * device, this list should be empty. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1771 | * <p>This method requires the caller to hold the permission |
| 1772 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1773 | * |
| 1774 | * @return an array of 0 or more regular expression Strings defining |
| 1775 | * what interfaces are considered tetherable bluetooth interfaces. |
| 1776 | * |
Danica Chang | 6fdd0c6 | 2010-08-11 14:54:43 -0700 | [diff] [blame] | 1777 | * {@hide} |
| 1778 | */ |
| 1779 | public String[] getTetherableBluetoothRegexs() { |
| 1780 | try { |
| 1781 | return mService.getTetherableBluetoothRegexs(); |
| 1782 | } catch (RemoteException e) { |
| 1783 | return new String[0]; |
| 1784 | } |
| 1785 | } |
| 1786 | |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1787 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1788 | * Attempt to both alter the mode of USB and Tethering of USB. A |
| 1789 | * utility method to deal with some of the complexity of USB - will |
| 1790 | * attempt to switch to Rndis and subsequently tether the resulting |
| 1791 | * interface on {@code true} or turn off tethering and switch off |
| 1792 | * Rndis on {@code false}. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1793 | * <p>This method requires the caller to hold the permission |
| 1794 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1795 | * |
| 1796 | * @param enable a boolean - {@code true} to enable tethering |
| 1797 | * @return error a {@code TETHER_ERROR} value indicating success or failure type |
| 1798 | * |
Mike Lockwood | 6c2260b | 2011-07-19 13:04:47 -0700 | [diff] [blame] | 1799 | * {@hide} |
| 1800 | */ |
| 1801 | public int setUsbTethering(boolean enable) { |
| 1802 | try { |
| 1803 | return mService.setUsbTethering(enable); |
| 1804 | } catch (RemoteException e) { |
| 1805 | return TETHER_ERROR_SERVICE_UNAVAIL; |
| 1806 | } |
| 1807 | } |
| 1808 | |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1809 | /** {@hide} */ |
| 1810 | public static final int TETHER_ERROR_NO_ERROR = 0; |
| 1811 | /** {@hide} */ |
| 1812 | public static final int TETHER_ERROR_UNKNOWN_IFACE = 1; |
| 1813 | /** {@hide} */ |
| 1814 | public static final int TETHER_ERROR_SERVICE_UNAVAIL = 2; |
| 1815 | /** {@hide} */ |
| 1816 | public static final int TETHER_ERROR_UNSUPPORTED = 3; |
| 1817 | /** {@hide} */ |
| 1818 | public static final int TETHER_ERROR_UNAVAIL_IFACE = 4; |
| 1819 | /** {@hide} */ |
| 1820 | public static final int TETHER_ERROR_MASTER_ERROR = 5; |
| 1821 | /** {@hide} */ |
| 1822 | public static final int TETHER_ERROR_TETHER_IFACE_ERROR = 6; |
| 1823 | /** {@hide} */ |
| 1824 | public static final int TETHER_ERROR_UNTETHER_IFACE_ERROR = 7; |
| 1825 | /** {@hide} */ |
| 1826 | public static final int TETHER_ERROR_ENABLE_NAT_ERROR = 8; |
| 1827 | /** {@hide} */ |
| 1828 | public static final int TETHER_ERROR_DISABLE_NAT_ERROR = 9; |
| 1829 | /** {@hide} */ |
| 1830 | public static final int TETHER_ERROR_IFACE_CFG_ERROR = 10; |
| 1831 | |
| 1832 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1833 | * Get a more detailed error code after a Tethering or Untethering |
| 1834 | * request asynchronously failed. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1835 | * <p>This method requires the caller to hold the permission |
| 1836 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1837 | * |
| 1838 | * @param iface The name of the interface of interest |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1839 | * @return error The error code of the last error tethering or untethering the named |
| 1840 | * interface |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1841 | * |
Robert Greenwalt | 5a73506 | 2010-03-02 17:25:02 -0800 | [diff] [blame] | 1842 | * {@hide} |
| 1843 | */ |
| 1844 | public int getLastTetherError(String iface) { |
| 1845 | try { |
| 1846 | return mService.getLastTetherError(iface); |
| 1847 | } catch (RemoteException e) { |
| 1848 | return TETHER_ERROR_SERVICE_UNAVAIL; |
| 1849 | } |
Robert Greenwalt | 14f2ef4 | 2010-06-15 12:19:37 -0700 | [diff] [blame] | 1850 | } |
| 1851 | |
| 1852 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1853 | * Report network connectivity status. This is currently used only |
| 1854 | * to alter status bar UI. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1855 | * <p>This method requires the caller to hold the permission |
| 1856 | * {@link android.Manifest.permission#STATUS_BAR}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1857 | * |
Robert Greenwalt | d7085fc | 2010-09-08 15:24:47 -0700 | [diff] [blame] | 1858 | * @param networkType The type of network you want to report on |
| 1859 | * @param percentage The quality of the connection 0 is bad, 100 is good |
| 1860 | * {@hide} |
| 1861 | */ |
| 1862 | public void reportInetCondition(int networkType, int percentage) { |
| 1863 | try { |
| 1864 | mService.reportInetCondition(networkType, percentage); |
| 1865 | } catch (RemoteException e) { |
| 1866 | } |
| 1867 | } |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1868 | |
| 1869 | /** |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1870 | * 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] | 1871 | * that there might be connectivity problems on this network and may cause |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1872 | * the framework to re-evaluate network connectivity and/or switch to another |
| 1873 | * network. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1874 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1875 | * @param network The {@link Network} the application was attempting to use |
| 1876 | * or {@code null} to indicate the current default network. |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 1877 | * @deprecated Use {@link #reportNetworkConnectivity} which allows reporting both |
| 1878 | * working and non-working connectivity. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1879 | */ |
| 1880 | public void reportBadNetwork(Network network) { |
| 1881 | try { |
Paul Jensen | bfd17b7 | 2015-04-07 12:43:13 -0400 | [diff] [blame] | 1882 | // One of these will be ignored because it matches system's current state. |
| 1883 | // The other will trigger the necessary reevaluation. |
| 1884 | mService.reportNetworkConnectivity(network, true); |
| 1885 | mService.reportNetworkConnectivity(network, false); |
| 1886 | } catch (RemoteException e) { |
| 1887 | } |
| 1888 | } |
| 1889 | |
| 1890 | /** |
| 1891 | * Report to the framework whether a network has working connectivity. |
| 1892 | * This provides a hint to the system that a particular network is providing |
| 1893 | * working connectivity or not. In response the framework may re-evaluate |
| 1894 | * the network's connectivity and might take further action thereafter. |
| 1895 | * |
| 1896 | * @param network The {@link Network} the application was attempting to use |
| 1897 | * or {@code null} to indicate the current default network. |
| 1898 | * @param hasConnectivity {@code true} if the application was able to successfully access the |
| 1899 | * Internet using {@code network} or {@code false} if not. |
| 1900 | */ |
| 1901 | public void reportNetworkConnectivity(Network network, boolean hasConnectivity) { |
| 1902 | try { |
| 1903 | mService.reportNetworkConnectivity(network, hasConnectivity); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 1904 | } catch (RemoteException e) { |
| 1905 | } |
| 1906 | } |
| 1907 | |
| 1908 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1909 | * Set a network-independent global http proxy. This is not normally what you want |
| 1910 | * for typical HTTP proxies - they are general network dependent. However if you're |
| 1911 | * doing something unusual like general internal filtering this may be useful. On |
| 1912 | * 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] | 1913 | * <p>This method requires the caller to hold the permission |
Jason Monk | faf3fd5 | 2014-05-07 18:41:13 -0400 | [diff] [blame] | 1914 | * android.Manifest.permission#CONNECTIVITY_INTERNAL. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1915 | * |
| 1916 | * @param p A {@link ProxyInfo} object defining the new global |
| 1917 | * HTTP proxy. A {@code null} value will clear the global HTTP proxy. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1918 | * @hide |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1919 | */ |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 1920 | public void setGlobalProxy(ProxyInfo p) { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1921 | try { |
| 1922 | mService.setGlobalProxy(p); |
| 1923 | } catch (RemoteException e) { |
| 1924 | } |
| 1925 | } |
| 1926 | |
| 1927 | /** |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1928 | * Retrieve any network-independent global HTTP proxy. |
| 1929 | * |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 1930 | * @return {@link ProxyInfo} for the current global HTTP proxy or {@code null} |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1931 | * if no global HTTP proxy is set. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 1932 | * @hide |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1933 | */ |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 1934 | public ProxyInfo getGlobalProxy() { |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1935 | try { |
| 1936 | return mService.getGlobalProxy(); |
| 1937 | } catch (RemoteException e) { |
| 1938 | return null; |
| 1939 | } |
| 1940 | } |
| 1941 | |
| 1942 | /** |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 1943 | * Retrieve the global HTTP proxy, or if no global HTTP proxy is set, a |
| 1944 | * network-specific HTTP proxy. If {@code network} is null, the |
| 1945 | * network-specific proxy returned is the proxy of the default active |
| 1946 | * network. |
| 1947 | * |
| 1948 | * @return {@link ProxyInfo} for the current global HTTP proxy, or if no |
| 1949 | * global HTTP proxy is set, {@code ProxyInfo} for {@code network}, |
| 1950 | * or when {@code network} is {@code null}, |
| 1951 | * the {@code ProxyInfo} for the default active network. Returns |
| 1952 | * {@code null} when no proxy applies or the caller doesn't have |
| 1953 | * permission to use {@code network}. |
| 1954 | * @hide |
| 1955 | */ |
| 1956 | public ProxyInfo getProxyForNetwork(Network network) { |
| 1957 | try { |
| 1958 | return mService.getProxyForNetwork(network); |
| 1959 | } catch (RemoteException e) { |
| 1960 | return null; |
| 1961 | } |
| 1962 | } |
| 1963 | |
| 1964 | /** |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 1965 | * Get the current default HTTP proxy settings. If a global proxy is set it will be returned, |
| 1966 | * otherwise if this process is bound to a {@link Network} using |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 1967 | * {@link #bindProcessToNetwork} then that {@code Network}'s proxy is returned, otherwise |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 1968 | * the default network's proxy is returned. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1969 | * |
Jason Monk | 207900c | 2014-04-25 15:00:09 -0400 | [diff] [blame] | 1970 | * @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] | 1971 | * HTTP proxy is active. |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1972 | */ |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 1973 | public ProxyInfo getDefaultProxy() { |
Paul Jensen | cee9b51 | 2015-05-06 07:32:40 -0400 | [diff] [blame] | 1974 | return getProxyForNetwork(getBoundNetworkForProcess()); |
Robert Greenwalt | 434203a | 2010-10-11 16:00:27 -0700 | [diff] [blame] | 1975 | } |
Robert Greenwalt | d55a6b4 | 2011-03-25 13:09:25 -0700 | [diff] [blame] | 1976 | |
| 1977 | /** |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1978 | * Returns true if the hardware supports the given network type |
| 1979 | * else it returns false. This doesn't indicate we have coverage |
| 1980 | * or are authorized onto a network, just whether or not the |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1981 | * hardware supports it. For example a GSM phone without a SIM |
| 1982 | * should still return {@code true} for mobile data, but a wifi only |
| 1983 | * tablet would return {@code false}. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 1984 | * <p>This method requires the caller to hold the permission |
| 1985 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 1986 | * |
| 1987 | * @param networkType The network type we'd like to check |
| 1988 | * @return {@code true} if supported, else {@code false} |
| 1989 | * |
Robert Greenwalt | 9b2886e | 2011-08-31 11:46:42 -0700 | [diff] [blame] | 1990 | * @hide |
| 1991 | */ |
| 1992 | public boolean isNetworkSupported(int networkType) { |
| 1993 | try { |
| 1994 | return mService.isNetworkSupported(networkType); |
| 1995 | } catch (RemoteException e) {} |
| 1996 | return false; |
| 1997 | } |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 1998 | |
| 1999 | /** |
| 2000 | * Returns if the currently active data network is metered. A network is |
| 2001 | * 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] | 2002 | * that connection due to monetary costs, data limitations or |
| 2003 | * battery/performance issues. You should check this before doing large |
| 2004 | * data transfers, and warn the user or delay the operation until another |
| 2005 | * network is available. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 2006 | * <p>This method requires the caller to hold the permission |
| 2007 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2008 | * |
| 2009 | * @return {@code true} if large transfers should be avoided, otherwise |
| 2010 | * {@code false}. |
Jeff Sharkey | 9f7cbf0 | 2012-04-12 18:34:54 -0700 | [diff] [blame] | 2011 | */ |
| 2012 | public boolean isActiveNetworkMetered() { |
| 2013 | try { |
| 2014 | return mService.isActiveNetworkMetered(); |
| 2015 | } catch (RemoteException e) { |
| 2016 | return false; |
| 2017 | } |
| 2018 | } |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2019 | |
Robert Greenwalt | 5a6bdc4 | 2013-02-15 10:56:35 -0800 | [diff] [blame] | 2020 | /** |
| 2021 | * If the LockdownVpn mechanism is enabled, updates the vpn |
| 2022 | * with a reload of its profile. |
| 2023 | * |
| 2024 | * @return a boolean with {@code} indicating success |
| 2025 | * |
| 2026 | * <p>This method can only be called by the system UID |
| 2027 | * {@hide} |
| 2028 | */ |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 2029 | public boolean updateLockdownVpn() { |
| 2030 | try { |
| 2031 | return mService.updateLockdownVpn(); |
| 2032 | } catch (RemoteException e) { |
| 2033 | return false; |
| 2034 | } |
| 2035 | } |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 2036 | |
| 2037 | /** |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2038 | * Check mobile provisioning. |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2039 | * |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2040 | * @param suggestedTimeOutMs, timeout in milliseconds |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2041 | * |
| 2042 | * @return time out that will be used, maybe less that suggestedTimeOutMs |
| 2043 | * -1 if an error. |
| 2044 | * |
| 2045 | * {@hide} |
| 2046 | */ |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2047 | public int checkMobileProvisioning(int suggestedTimeOutMs) { |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2048 | int timeOutMs = -1; |
| 2049 | try { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2050 | timeOutMs = mService.checkMobileProvisioning(suggestedTimeOutMs); |
Wink Saville | ab9321d | 2013-06-29 21:10:57 -0700 | [diff] [blame] | 2051 | } catch (RemoteException e) { |
| 2052 | } |
| 2053 | return timeOutMs; |
| 2054 | } |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 2055 | |
| 2056 | /** |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 2057 | * Get the mobile provisioning url. |
Robert Greenwalt | e182bfe | 2013-07-16 12:06:09 -0700 | [diff] [blame] | 2058 | * {@hide} |
| 2059 | */ |
| 2060 | public String getMobileProvisioningUrl() { |
| 2061 | try { |
| 2062 | return mService.getMobileProvisioningUrl(); |
| 2063 | } catch (RemoteException e) { |
| 2064 | } |
| 2065 | return null; |
| 2066 | } |
Wink Saville | 42d4f08 | 2013-07-20 20:31:59 -0700 | [diff] [blame] | 2067 | |
| 2068 | /** |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2069 | * Set sign in error notification to visible or in visible |
| 2070 | * |
| 2071 | * @param visible |
| 2072 | * @param networkType |
| 2073 | * |
| 2074 | * {@hide} |
Paul Jensen | 3541e9f | 2015-03-18 12:23:02 -0400 | [diff] [blame] | 2075 | * @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] | 2076 | */ |
| 2077 | public void setProvisioningNotificationVisible(boolean visible, int networkType, |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 2078 | String action) { |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2079 | try { |
Paul Jensen | 89e0f09 | 2014-09-15 15:59:36 -0400 | [diff] [blame] | 2080 | mService.setProvisioningNotificationVisible(visible, networkType, action); |
Wink Saville | 948282b | 2013-08-29 08:55:16 -0700 | [diff] [blame] | 2081 | } catch (RemoteException e) { |
| 2082 | } |
| 2083 | } |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 2084 | |
| 2085 | /** |
| 2086 | * Set the value for enabling/disabling airplane mode |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 2087 | * <p>This method requires the caller to hold the permission |
| 2088 | * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL}. |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 2089 | * |
| 2090 | * @param enable whether to enable airplane mode or not |
| 2091 | * |
Yuhao Zheng | 5cd1a0e | 2013-09-09 17:00:04 -0700 | [diff] [blame] | 2092 | * @hide |
| 2093 | */ |
| 2094 | public void setAirplaneMode(boolean enable) { |
| 2095 | try { |
| 2096 | mService.setAirplaneMode(enable); |
| 2097 | } catch (RemoteException e) { |
| 2098 | } |
| 2099 | } |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2100 | |
| 2101 | /** {@hide} */ |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2102 | public void registerNetworkFactory(Messenger messenger, String name) { |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2103 | try { |
Robert Greenwalt | a67be03 | 2014-05-16 15:49:14 -0700 | [diff] [blame] | 2104 | mService.registerNetworkFactory(messenger, name); |
| 2105 | } catch (RemoteException e) { } |
| 2106 | } |
| 2107 | |
| 2108 | /** {@hide} */ |
| 2109 | public void unregisterNetworkFactory(Messenger messenger) { |
| 2110 | try { |
| 2111 | mService.unregisterNetworkFactory(messenger); |
Robert Greenwalt | e049c23 | 2014-04-11 15:53:27 -0700 | [diff] [blame] | 2112 | } catch (RemoteException e) { } |
| 2113 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2114 | |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 2115 | /** |
| 2116 | * @hide |
| 2117 | * Register a NetworkAgent with ConnectivityService. |
| 2118 | * @return NetID corresponding to NetworkAgent. |
| 2119 | */ |
| 2120 | public int registerNetworkAgent(Messenger messenger, NetworkInfo ni, LinkProperties lp, |
Sreeram Ramachandran | 8cd33ed | 2014-07-23 15:23:15 -0700 | [diff] [blame] | 2121 | NetworkCapabilities nc, int score, NetworkMisc misc) { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2122 | try { |
Paul Jensen | 31a94f4 | 2015-02-13 14:18:39 -0500 | [diff] [blame] | 2123 | return mService.registerNetworkAgent(messenger, ni, lp, nc, score, misc); |
| 2124 | } catch (RemoteException e) { |
| 2125 | return NETID_UNSET; |
| 2126 | } |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2127 | } |
| 2128 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2129 | /** |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2130 | * Base class for NetworkRequest callbacks. Used for notifications about network |
| 2131 | * changes. Should be extended by applications wanting notifications. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2132 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2133 | public static class NetworkCallback { |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2134 | /** |
Lorenzo Colitti | 0708693 | 2015-04-24 12:23:24 +0900 | [diff] [blame] | 2135 | * Called when the framework connects to a new network to evaluate whether it satisfies this |
| 2136 | * request. If evaluation succeeds, this callback may be followed by an {@link #onAvailable} |
| 2137 | * callback. There is no guarantee that this new network will satisfy any requests, or that |
| 2138 | * the network will stay connected for longer than the time necessary to evaluate it. |
| 2139 | * <p> |
| 2140 | * Most applications <b>should not</b> act on this callback, and should instead use |
| 2141 | * {@link #onAvailable}. This callback is intended for use by applications that can assist |
| 2142 | * the framework in properly evaluating the network — for example, an application that |
| 2143 | * can automatically log in to a captive portal without user intervention. |
| 2144 | * |
| 2145 | * @param network The {@link Network} of the network that is being evaluated. |
Lorenzo Colitti | 6627612 | 2015-06-11 14:27:17 +0900 | [diff] [blame] | 2146 | * |
| 2147 | * @hide |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2148 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2149 | public void onPreCheck(Network network) {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2150 | |
| 2151 | /** |
Lorenzo Colitti | 0708693 | 2015-04-24 12:23:24 +0900 | [diff] [blame] | 2152 | * 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] | 2153 | * This callback may be called more than once if the {@link Network} that is |
| 2154 | * satisfying the request changes. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2155 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2156 | * @param network The {@link Network} of the satisfying network. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2157 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2158 | public void onAvailable(Network network) {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2159 | |
| 2160 | /** |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2161 | * 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] | 2162 | * {@link NetworkCallback#onAvailable} call with the new replacement network |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2163 | * for graceful handover. This may not be called if we have a hard loss |
| 2164 | * (loss without warning). This may be followed by either a |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2165 | * {@link NetworkCallback#onLost} call or a |
| 2166 | * {@link NetworkCallback#onAvailable} call for this network depending |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2167 | * on whether we lose or regain it. |
| 2168 | * |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2169 | * @param network The {@link Network} that is about to be disconnected. |
| 2170 | * @param maxMsToLive The time in ms the framework will attempt to keep the |
| 2171 | * network connected. Note that the network may suffer a |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2172 | * hard loss at any time. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2173 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2174 | public void onLosing(Network network, int maxMsToLive) {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2175 | |
| 2176 | /** |
| 2177 | * 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] | 2178 | * graceful failure ends. |
| 2179 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2180 | * @param network The {@link Network} lost. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2181 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2182 | public void onLost(Network network) {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2183 | |
| 2184 | /** |
| 2185 | * Called if no network is found in the given timeout time. If no timeout is given, |
| 2186 | * this will not be called. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2187 | * @hide |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2188 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2189 | public void onUnavailable() {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2190 | |
| 2191 | /** |
| 2192 | * Called when the network the framework connected to for this request |
| 2193 | * changes capabilities but still satisfies the stated need. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2194 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2195 | * @param network The {@link Network} whose capabilities have changed. |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 2196 | * @param networkCapabilities The new {@link android.net.NetworkCapabilities} for this network. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2197 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2198 | public void onCapabilitiesChanged(Network network, |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2199 | NetworkCapabilities networkCapabilities) {} |
| 2200 | |
| 2201 | /** |
| 2202 | * Called when the network the framework connected to for this request |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2203 | * changes {@link LinkProperties}. |
| 2204 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2205 | * @param network The {@link Network} whose link properties have changed. |
| 2206 | * @param linkProperties The new {@link LinkProperties} for this network. |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2207 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2208 | public void onLinkPropertiesChanged(Network network, LinkProperties linkProperties) {} |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2209 | |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 2210 | /** |
| 2211 | * Called when the network the framework connected to for this request |
| 2212 | * goes into {@link NetworkInfo.DetailedState.SUSPENDED}. |
| 2213 | * This generally means that while the TCP connections are still live, |
| 2214 | * temporarily network data fails to transfer. Specifically this is used |
| 2215 | * on cellular networks to mask temporary outages when driving through |
| 2216 | * a tunnel, etc. |
| 2217 | * @hide |
| 2218 | */ |
| 2219 | public void onNetworkSuspended(Network network) {} |
| 2220 | |
| 2221 | /** |
| 2222 | * Called when the network the framework connected to for this request |
| 2223 | * returns from a {@link NetworkInfo.DetailedState.SUSPENDED} state. |
| 2224 | * This should always be preceeded by a matching {@code onNetworkSuspended} |
| 2225 | * call. |
| 2226 | * @hide |
| 2227 | */ |
| 2228 | public void onNetworkResumed(Network network) {} |
| 2229 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2230 | private NetworkRequest networkRequest; |
Robert Greenwalt | 7b81602 | 2014-04-18 15:25:25 -0700 | [diff] [blame] | 2231 | } |
| 2232 | |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2233 | private static final int BASE = Protocol.BASE_CONNECTIVITY_MANAGER; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2234 | /** @hide */ |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 2235 | public static final int CALLBACK_PRECHECK = BASE + 1; |
| 2236 | /** @hide */ |
| 2237 | public static final int CALLBACK_AVAILABLE = BASE + 2; |
| 2238 | /** @hide arg1 = TTL */ |
| 2239 | public static final int CALLBACK_LOSING = BASE + 3; |
| 2240 | /** @hide */ |
| 2241 | public static final int CALLBACK_LOST = BASE + 4; |
| 2242 | /** @hide */ |
| 2243 | public static final int CALLBACK_UNAVAIL = BASE + 5; |
| 2244 | /** @hide */ |
| 2245 | public static final int CALLBACK_CAP_CHANGED = BASE + 6; |
| 2246 | /** @hide */ |
| 2247 | public static final int CALLBACK_IP_CHANGED = BASE + 7; |
| 2248 | /** @hide */ |
| 2249 | public static final int CALLBACK_RELEASED = BASE + 8; |
| 2250 | /** @hide */ |
| 2251 | public static final int CALLBACK_EXIT = BASE + 9; |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2252 | /** @hide obj = NetworkCapabilities, arg1 = seq number */ |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 2253 | private static final int EXPIRE_LEGACY_REQUEST = BASE + 10; |
| 2254 | /** @hide */ |
| 2255 | public static final int CALLBACK_SUSPENDED = BASE + 11; |
| 2256 | /** @hide */ |
| 2257 | public static final int CALLBACK_RESUMED = BASE + 12; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2258 | |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2259 | private class CallbackHandler extends Handler { |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2260 | private final HashMap<NetworkRequest, NetworkCallback>mCallbackMap; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2261 | private final AtomicInteger mRefCount; |
| 2262 | private static final String TAG = "ConnectivityManager.CallbackHandler"; |
| 2263 | private final ConnectivityManager mCm; |
Robert Greenwalt | a9ebeef | 2015-09-03 16:41:45 -0700 | [diff] [blame] | 2264 | private static final boolean DBG = false; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2265 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2266 | CallbackHandler(Looper looper, HashMap<NetworkRequest, NetworkCallback>callbackMap, |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2267 | AtomicInteger refCount, ConnectivityManager cm) { |
| 2268 | super(looper); |
| 2269 | mCallbackMap = callbackMap; |
| 2270 | mRefCount = refCount; |
| 2271 | mCm = cm; |
| 2272 | } |
| 2273 | |
| 2274 | @Override |
| 2275 | public void handleMessage(Message message) { |
Robert Greenwalt | a9ebeef | 2015-09-03 16:41:45 -0700 | [diff] [blame] | 2276 | if (DBG) Log.d(TAG, "CM callback handler got msg " + message.what); |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2277 | NetworkRequest request = (NetworkRequest) getObject(message, NetworkRequest.class); |
| 2278 | Network network = (Network) getObject(message, Network.class); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2279 | switch (message.what) { |
| 2280 | case CALLBACK_PRECHECK: { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2281 | NetworkCallback callback = getCallback(request, "PRECHECK"); |
| 2282 | if (callback != null) { |
| 2283 | callback.onPreCheck(network); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2284 | } |
| 2285 | break; |
| 2286 | } |
| 2287 | case CALLBACK_AVAILABLE: { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2288 | NetworkCallback callback = getCallback(request, "AVAILABLE"); |
| 2289 | if (callback != null) { |
| 2290 | callback.onAvailable(network); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2291 | } |
| 2292 | break; |
| 2293 | } |
| 2294 | case CALLBACK_LOSING: { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2295 | NetworkCallback callback = getCallback(request, "LOSING"); |
| 2296 | if (callback != null) { |
| 2297 | callback.onLosing(network, message.arg1); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2298 | } |
| 2299 | break; |
| 2300 | } |
| 2301 | case CALLBACK_LOST: { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2302 | NetworkCallback callback = getCallback(request, "LOST"); |
| 2303 | if (callback != null) { |
| 2304 | callback.onLost(network); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2305 | } |
| 2306 | break; |
| 2307 | } |
| 2308 | case CALLBACK_UNAVAIL: { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2309 | NetworkCallback callback = getCallback(request, "UNAVAIL"); |
| 2310 | if (callback != null) { |
| 2311 | callback.onUnavailable(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2312 | } |
| 2313 | break; |
| 2314 | } |
| 2315 | case CALLBACK_CAP_CHANGED: { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2316 | NetworkCallback callback = getCallback(request, "CAP_CHANGED"); |
| 2317 | if (callback != null) { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2318 | NetworkCapabilities cap = (NetworkCapabilities)getObject(message, |
| 2319 | NetworkCapabilities.class); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2320 | |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2321 | callback.onCapabilitiesChanged(network, cap); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2322 | } |
| 2323 | break; |
| 2324 | } |
| 2325 | case CALLBACK_IP_CHANGED: { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2326 | NetworkCallback callback = getCallback(request, "IP_CHANGED"); |
| 2327 | if (callback != null) { |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2328 | LinkProperties lp = (LinkProperties)getObject(message, |
| 2329 | LinkProperties.class); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2330 | |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2331 | callback.onLinkPropertiesChanged(network, lp); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2332 | } |
| 2333 | break; |
| 2334 | } |
Robert Greenwalt | 8d48252 | 2015-06-24 13:23:42 -0700 | [diff] [blame] | 2335 | case CALLBACK_SUSPENDED: { |
| 2336 | NetworkCallback callback = getCallback(request, "SUSPENDED"); |
| 2337 | if (callback != null) { |
| 2338 | callback.onNetworkSuspended(network); |
| 2339 | } |
| 2340 | break; |
| 2341 | } |
| 2342 | case CALLBACK_RESUMED: { |
| 2343 | NetworkCallback callback = getCallback(request, "RESUMED"); |
| 2344 | if (callback != null) { |
| 2345 | callback.onNetworkResumed(network); |
| 2346 | } |
| 2347 | break; |
| 2348 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2349 | case CALLBACK_RELEASED: { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2350 | NetworkCallback callback = null; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2351 | synchronized(mCallbackMap) { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2352 | callback = mCallbackMap.remove(request); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2353 | } |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2354 | if (callback != null) { |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2355 | synchronized(mRefCount) { |
| 2356 | if (mRefCount.decrementAndGet() == 0) { |
| 2357 | getLooper().quit(); |
| 2358 | } |
| 2359 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2360 | } else { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2361 | Log.e(TAG, "callback not found for RELEASED message"); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2362 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2363 | break; |
| 2364 | } |
| 2365 | case CALLBACK_EXIT: { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2366 | Log.d(TAG, "Listener quitting"); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2367 | getLooper().quit(); |
| 2368 | break; |
| 2369 | } |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2370 | case EXPIRE_LEGACY_REQUEST: { |
| 2371 | expireRequest((NetworkCapabilities)message.obj, message.arg1); |
| 2372 | break; |
| 2373 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2374 | } |
| 2375 | } |
| 2376 | |
Robert Greenwalt | a848c1c | 2014-09-30 16:50:07 -0700 | [diff] [blame] | 2377 | private Object getObject(Message msg, Class c) { |
| 2378 | return msg.getData().getParcelable(c.getSimpleName()); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2379 | } |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2380 | |
| 2381 | private NetworkCallback getCallback(NetworkRequest req, String name) { |
| 2382 | NetworkCallback callback; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2383 | synchronized(mCallbackMap) { |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2384 | callback = mCallbackMap.get(req); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2385 | } |
Lorenzo Colitti | db95a60 | 2015-04-24 15:21:21 +0900 | [diff] [blame] | 2386 | if (callback == null) { |
| 2387 | Log.e(TAG, "callback not found for " + name + " message"); |
| 2388 | } |
| 2389 | return callback; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2390 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2391 | } |
| 2392 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2393 | private void incCallbackHandlerRefCount() { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2394 | synchronized(sCallbackRefCount) { |
| 2395 | if (sCallbackRefCount.incrementAndGet() == 1) { |
| 2396 | // TODO - switch this over to a ManagerThread or expire it when done |
| 2397 | HandlerThread callbackThread = new HandlerThread("ConnectivityManager"); |
| 2398 | callbackThread.start(); |
| 2399 | sCallbackHandler = new CallbackHandler(callbackThread.getLooper(), |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2400 | sNetworkCallback, sCallbackRefCount, this); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2401 | } |
| 2402 | } |
| 2403 | } |
| 2404 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2405 | private void decCallbackHandlerRefCount() { |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2406 | synchronized(sCallbackRefCount) { |
| 2407 | if (sCallbackRefCount.decrementAndGet() == 0) { |
| 2408 | sCallbackHandler.obtainMessage(CALLBACK_EXIT).sendToTarget(); |
| 2409 | sCallbackHandler = null; |
| 2410 | } |
| 2411 | } |
| 2412 | } |
| 2413 | |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2414 | static final HashMap<NetworkRequest, NetworkCallback> sNetworkCallback = |
| 2415 | new HashMap<NetworkRequest, NetworkCallback>(); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2416 | static final AtomicInteger sCallbackRefCount = new AtomicInteger(0); |
| 2417 | static CallbackHandler sCallbackHandler = null; |
| 2418 | |
| 2419 | private final static int LISTEN = 1; |
| 2420 | private final static int REQUEST = 2; |
| 2421 | |
Robert Greenwalt | 562cc54 | 2014-05-15 18:07:26 -0700 | [diff] [blame] | 2422 | private NetworkRequest sendRequestForNetwork(NetworkCapabilities need, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2423 | NetworkCallback networkCallback, int timeoutSec, int action, |
Robert Greenwalt | 32aa65a | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 2424 | int legacyType) { |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2425 | if (networkCallback == null) { |
| 2426 | throw new IllegalArgumentException("null NetworkCallback"); |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2427 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2428 | if (need == null) throw new IllegalArgumentException("null NetworkCapabilities"); |
| 2429 | try { |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2430 | incCallbackHandlerRefCount(); |
Paul Jensen | 7221cc3 | 2014-06-27 11:05:32 -0400 | [diff] [blame] | 2431 | synchronized(sNetworkCallback) { |
| 2432 | if (action == LISTEN) { |
| 2433 | networkCallback.networkRequest = mService.listenForNetwork(need, |
| 2434 | new Messenger(sCallbackHandler), new Binder()); |
| 2435 | } else { |
| 2436 | networkCallback.networkRequest = mService.requestNetwork(need, |
| 2437 | new Messenger(sCallbackHandler), timeoutSec, new Binder(), legacyType); |
| 2438 | } |
| 2439 | if (networkCallback.networkRequest != null) { |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2440 | sNetworkCallback.put(networkCallback.networkRequest, networkCallback); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2441 | } |
| 2442 | } |
| 2443 | } catch (RemoteException e) {} |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2444 | if (networkCallback.networkRequest == null) decCallbackHandlerRefCount(); |
| 2445 | return networkCallback.networkRequest; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2446 | } |
| 2447 | |
| 2448 | /** |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 2449 | * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2450 | * |
| 2451 | * This {@link NetworkRequest} will live until released via |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2452 | * {@link #unregisterNetworkCallback} or the calling application exits. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2453 | * Status of the request can be followed by listening to the various |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2454 | * callbacks described in {@link NetworkCallback}. The {@link Network} |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2455 | * can be used to direct traffic to the network. |
Paul Jensen | bb2e0e9 | 2015-06-16 15:11:58 -0400 | [diff] [blame] | 2456 | * <p>It is presently unsupported to request a network with mutable |
| 2457 | * {@link NetworkCapabilities} such as |
| 2458 | * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or |
| 2459 | * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL} |
| 2460 | * as these {@code NetworkCapabilities} represent states that a particular |
| 2461 | * network may never attain, and whether a network will attain these states |
| 2462 | * is unknown prior to bringing up the network so the framework does not |
| 2463 | * know how to go about satisfing a request with these capabilities. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 2464 | * <p>This method requires the caller to hold the permission |
| 2465 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2466 | * |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2467 | * @param request {@link NetworkRequest} describing this request. |
| 2468 | * @param networkCallback The {@link NetworkCallback} to be utilized for this |
| 2469 | * request. Note the callback must not be shared - they |
| 2470 | * uniquely specify this request. |
Paul Jensen | bb2e0e9 | 2015-06-16 15:11:58 -0400 | [diff] [blame] | 2471 | * @throws IllegalArgumentException if {@code request} specifies any mutable |
| 2472 | * {@code NetworkCapabilities}. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2473 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2474 | public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback) { |
| 2475 | sendRequestForNetwork(request.networkCapabilities, networkCallback, 0, |
Ye Wen | b87875e | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 2476 | REQUEST, inferLegacyTypeForNetworkCapabilities(request.networkCapabilities)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | /** |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 2480 | * 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] | 2481 | * by a timeout. |
| 2482 | * |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2483 | * This function behaves identically to the non-timedout version, but if a suitable |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2484 | * network is not found within the given time (in milliseconds) the |
| 2485 | * {@link NetworkCallback#unavailable} callback is called. The request must |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2486 | * still be released normally by calling {@link releaseNetworkRequest}. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 2487 | * <p>This method requires the caller to hold the permission |
| 2488 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}. |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2489 | * @param request {@link NetworkRequest} describing this request. |
| 2490 | * @param networkCallback The callbacks to be utilized for this request. Note |
| 2491 | * the callbacks must not be shared - they uniquely specify |
| 2492 | * this request. |
| 2493 | * @param timeoutMs The time in milliseconds to attempt looking for a suitable network |
| 2494 | * before {@link NetworkCallback#unavailable} is called. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2495 | * @hide |
| 2496 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2497 | public void requestNetwork(NetworkRequest request, NetworkCallback networkCallback, |
| 2498 | int timeoutMs) { |
| 2499 | sendRequestForNetwork(request.networkCapabilities, networkCallback, timeoutMs, |
Ye Wen | b87875e | 2014-07-21 14:19:01 -0700 | [diff] [blame] | 2500 | REQUEST, inferLegacyTypeForNetworkCapabilities(request.networkCapabilities)); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2501 | } |
| 2502 | |
| 2503 | /** |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2504 | * The maximum number of milliseconds the framework will look for a suitable network |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2505 | * during a timeout-equiped call to {@link requestNetwork}. |
| 2506 | * {@hide} |
| 2507 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2508 | public final static int MAX_NETWORK_REQUEST_TIMEOUT_MS = 100 * 60 * 1000; |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2509 | |
| 2510 | /** |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2511 | * 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] | 2512 | * successfully finding a network for the applications request. Retrieve it with |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2513 | * {@link android.content.Intent#getParcelableExtra(String)}. |
Jeremy Joslin | fcde58f | 2015-02-11 16:51:13 -0800 | [diff] [blame] | 2514 | * <p> |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2515 | * Note that if you intend to invoke {@link Network#openConnection(java.net.URL)} |
| 2516 | * then you must get a ConnectivityManager instance before doing so. |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2517 | */ |
Erik Kline | 90e9307 | 2014-11-19 12:12:24 +0900 | [diff] [blame] | 2518 | public static final String EXTRA_NETWORK = "android.net.extra.NETWORK"; |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2519 | |
| 2520 | /** |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2521 | * 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] | 2522 | * successfully finding a network for the applications request. Retrieve it with |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2523 | * {@link android.content.Intent#getParcelableExtra(String)}. |
| 2524 | */ |
Erik Kline | 90e9307 | 2014-11-19 12:12:24 +0900 | [diff] [blame] | 2525 | public static final String EXTRA_NETWORK_REQUEST = "android.net.extra.NETWORK_REQUEST"; |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2526 | |
| 2527 | |
| 2528 | /** |
Lorenzo Colitti | e285b43 | 2015-04-23 15:32:42 +0900 | [diff] [blame] | 2529 | * Request a network to satisfy a set of {@link android.net.NetworkCapabilities}. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2530 | * |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2531 | * 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] | 2532 | * of {@link NetworkCallback} a {@link PendingIntent} is used. This means |
Robert Greenwalt | d19c41c | 2014-05-18 23:07:25 -0700 | [diff] [blame] | 2533 | * the request may outlive the calling application and get called back when a suitable |
| 2534 | * network is found. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2535 | * <p> |
| 2536 | * The operation is an Intent broadcast that goes to a broadcast receiver that |
| 2537 | * you registered with {@link Context#registerReceiver} or through the |
| 2538 | * <receiver> tag in an AndroidManifest.xml file |
| 2539 | * <p> |
| 2540 | * The operation Intent is delivered with two extras, a {@link Network} typed |
Erik Kline | 90e9307 | 2014-11-19 12:12:24 +0900 | [diff] [blame] | 2541 | * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest} |
| 2542 | * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2543 | * the original requests parameters. It is important to create a new, |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2544 | * {@link NetworkCallback} based request before completing the processing of the |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2545 | * Intent to reserve the network or it will be released shortly after the Intent |
| 2546 | * is processed. |
| 2547 | * <p> |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 2548 | * 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] | 2549 | * 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] | 2550 | * replaced by this one, effectively releasing the previous {@link NetworkRequest}. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2551 | * <p> |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2552 | * The request may be released normally by calling |
| 2553 | * {@link #releaseNetworkRequest(android.app.PendingIntent)}. |
Paul Jensen | bb2e0e9 | 2015-06-16 15:11:58 -0400 | [diff] [blame] | 2554 | * <p>It is presently unsupported to request a network with either |
| 2555 | * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or |
| 2556 | * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL} |
| 2557 | * as these {@code NetworkCapabilities} represent states that a particular |
| 2558 | * network may never attain, and whether a network will attain these states |
| 2559 | * is unknown prior to bringing up the network so the framework does not |
| 2560 | * know how to go about satisfing a request with these capabilities. |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 2561 | * <p>This method requires the caller to hold the permission |
| 2562 | * {@link android.Manifest.permission#CHANGE_NETWORK_STATE}. |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2563 | * @param request {@link NetworkRequest} describing this request. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2564 | * @param operation Action to perform when the network is available (corresponds |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2565 | * to the {@link NetworkCallback#onAvailable} call. Typically |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2566 | * comes from {@link PendingIntent#getBroadcast}. Cannot be null. |
Paul Jensen | bb2e0e9 | 2015-06-16 15:11:58 -0400 | [diff] [blame] | 2567 | * @throws IllegalArgumentException if {@code request} contains either |
| 2568 | * {@link NetworkCapabilities#NET_CAPABILITY_VALIDATED} or |
| 2569 | * {@link NetworkCapabilities#NET_CAPABILITY_CAPTIVE_PORTAL}. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2570 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2571 | public void requestNetwork(NetworkRequest request, PendingIntent operation) { |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2572 | checkPendingIntent(operation); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2573 | try { |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2574 | mService.pendingRequestForNetwork(request.networkCapabilities, operation); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2575 | } catch (RemoteException e) {} |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2576 | } |
| 2577 | |
| 2578 | /** |
Jeremy Joslin | 46e3ac8 | 2014-11-05 10:32:09 -0800 | [diff] [blame] | 2579 | * Removes a request made via {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} |
| 2580 | * <p> |
| 2581 | * This method has the same behavior as {@link #unregisterNetworkCallback} with respect to |
| 2582 | * releasing network resources and disconnecting. |
| 2583 | * |
| 2584 | * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the |
| 2585 | * PendingIntent passed to |
| 2586 | * {@link #requestNetwork(NetworkRequest, android.app.PendingIntent)} with the |
| 2587 | * corresponding NetworkRequest you'd like to remove. Cannot be null. |
| 2588 | */ |
| 2589 | public void releaseNetworkRequest(PendingIntent operation) { |
| 2590 | checkPendingIntent(operation); |
| 2591 | try { |
| 2592 | mService.releasePendingNetworkRequest(operation); |
| 2593 | } catch (RemoteException e) {} |
| 2594 | } |
| 2595 | |
| 2596 | private void checkPendingIntent(PendingIntent intent) { |
| 2597 | if (intent == null) { |
| 2598 | throw new IllegalArgumentException("PendingIntent cannot be null."); |
| 2599 | } |
| 2600 | } |
| 2601 | |
| 2602 | /** |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2603 | * Registers to receive notifications about all networks which satisfy the given |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2604 | * {@link NetworkRequest}. The callbacks will continue to be called until |
| 2605 | * either the application exits or {@link #unregisterNetworkCallback} is called |
Paul Jensen | b274892 | 2015-05-06 11:10:18 -0400 | [diff] [blame] | 2606 | * <p>This method requires the caller to hold the permission |
| 2607 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2608 | * |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2609 | * @param request {@link NetworkRequest} describing this request. |
| 2610 | * @param networkCallback The {@link NetworkCallback} that the system will call as suitable |
| 2611 | * networks change state. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2612 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2613 | public void registerNetworkCallback(NetworkRequest request, NetworkCallback networkCallback) { |
| 2614 | sendRequestForNetwork(request.networkCapabilities, networkCallback, 0, LISTEN, TYPE_NONE); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2615 | } |
| 2616 | |
| 2617 | /** |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 2618 | * Registers a PendingIntent to be sent when a network is available which satisfies the given |
| 2619 | * {@link NetworkRequest}. |
| 2620 | * |
| 2621 | * This function behaves identically to the version that takes a NetworkCallback, but instead |
| 2622 | * of {@link NetworkCallback} a {@link PendingIntent} is used. This means |
| 2623 | * the request may outlive the calling application and get called back when a suitable |
| 2624 | * network is found. |
| 2625 | * <p> |
| 2626 | * The operation is an Intent broadcast that goes to a broadcast receiver that |
| 2627 | * you registered with {@link Context#registerReceiver} or through the |
| 2628 | * <receiver> tag in an AndroidManifest.xml file |
| 2629 | * <p> |
| 2630 | * The operation Intent is delivered with two extras, a {@link Network} typed |
| 2631 | * extra called {@link #EXTRA_NETWORK} and a {@link NetworkRequest} |
| 2632 | * typed extra called {@link #EXTRA_NETWORK_REQUEST} containing |
| 2633 | * the original requests parameters. |
| 2634 | * <p> |
| 2635 | * If there is already a request for this Intent registered (with the equality of |
| 2636 | * two Intents defined by {@link Intent#filterEquals}), then it will be removed and |
| 2637 | * replaced by this one, effectively releasing the previous {@link NetworkRequest}. |
| 2638 | * <p> |
| 2639 | * The request may be released normally by calling |
Paul Jensen | f2c1cfe | 2015-06-30 14:29:18 -0400 | [diff] [blame] | 2640 | * {@link #unregisterNetworkCallback(android.app.PendingIntent)}. |
Paul Jensen | 694f2b8 | 2015-06-17 14:15:39 -0400 | [diff] [blame] | 2641 | * <p>This method requires the caller to hold the permission |
| 2642 | * {@link android.Manifest.permission#ACCESS_NETWORK_STATE}. |
| 2643 | * @param request {@link NetworkRequest} describing this request. |
| 2644 | * @param operation Action to perform when the network is available (corresponds |
| 2645 | * to the {@link NetworkCallback#onAvailable} call. Typically |
| 2646 | * comes from {@link PendingIntent#getBroadcast}. Cannot be null. |
| 2647 | */ |
| 2648 | public void registerNetworkCallback(NetworkRequest request, PendingIntent operation) { |
| 2649 | checkPendingIntent(operation); |
| 2650 | try { |
| 2651 | mService.pendingListenForNetwork(request.networkCapabilities, operation); |
| 2652 | } catch (RemoteException e) {} |
| 2653 | } |
| 2654 | |
| 2655 | /** |
fenglu | db57147 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 2656 | * Requests bandwidth update for a given {@link Network} and returns whether the update request |
| 2657 | * is accepted by ConnectivityService. Once accepted, ConnectivityService will poll underlying |
| 2658 | * network connection for updated bandwidth information. The caller will be notified via |
| 2659 | * {@link ConnectivityManager.NetworkCallback} if there is an update. Notice that this |
| 2660 | * method assumes that the caller has previously called {@link #registerNetworkCallback} to |
| 2661 | * listen for network changes. |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 2662 | * |
fenglu | ae51919 | 2015-04-27 14:28:04 -0700 | [diff] [blame] | 2663 | * @param network {@link Network} specifying which network you're interested. |
fenglu | db57147 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 2664 | * @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] | 2665 | */ |
fenglu | db57147 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 2666 | public boolean requestBandwidthUpdate(Network network) { |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 2667 | try { |
fenglu | db57147 | 2015-04-21 17:12:05 -0700 | [diff] [blame] | 2668 | return mService.requestBandwidthUpdate(network); |
fenglu | b15e72b | 2015-03-20 11:29:56 -0700 | [diff] [blame] | 2669 | } catch (RemoteException e) { |
| 2670 | return false; |
| 2671 | } |
| 2672 | } |
| 2673 | |
| 2674 | /** |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2675 | * Unregisters callbacks about and possibly releases networks originating from |
Lorenzo Colitti | 2ea89e5 | 2015-04-24 17:03:31 +0900 | [diff] [blame] | 2676 | * {@link #requestNetwork(NetworkRequest, NetworkCallback)} and {@link #registerNetworkCallback} |
| 2677 | * calls. If the given {@code NetworkCallback} had previously been used with |
| 2678 | * {@code #requestNetwork}, any networks that had been connected to only to satisfy that request |
| 2679 | * will be disconnected. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2680 | * |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2681 | * @param networkCallback The {@link NetworkCallback} used when making the request. |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2682 | */ |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2683 | public void unregisterNetworkCallback(NetworkCallback networkCallback) { |
| 2684 | if (networkCallback == null || networkCallback.networkRequest == null || |
| 2685 | networkCallback.networkRequest.requestId == REQUEST_ID_UNSET) { |
| 2686 | throw new IllegalArgumentException("Invalid NetworkCallback"); |
| 2687 | } |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2688 | try { |
Robert Greenwalt | 6078b50 | 2014-06-11 16:05:07 -0700 | [diff] [blame] | 2689 | mService.releaseNetworkRequest(networkCallback.networkRequest); |
Robert Greenwalt | 9258c64 | 2014-03-26 16:47:06 -0700 | [diff] [blame] | 2690 | } catch (RemoteException e) {} |
| 2691 | } |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 2692 | |
| 2693 | /** |
Paul Jensen | f2c1cfe | 2015-06-30 14:29:18 -0400 | [diff] [blame] | 2694 | * Unregisters a callback previously registered via |
| 2695 | * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}. |
| 2696 | * |
| 2697 | * @param operation A PendingIntent equal (as defined by {@link Intent#filterEquals}) to the |
| 2698 | * PendingIntent passed to |
| 2699 | * {@link #registerNetworkCallback(NetworkRequest, android.app.PendingIntent)}. |
| 2700 | * Cannot be null. |
| 2701 | */ |
| 2702 | public void unregisterNetworkCallback(PendingIntent operation) { |
| 2703 | releaseNetworkRequest(operation); |
| 2704 | } |
| 2705 | |
| 2706 | /** |
Lorenzo Colitti | e03c3c7 | 2015-04-03 16:38:52 +0900 | [diff] [blame] | 2707 | * Informs the system whether it should switch to {@code network} regardless of whether it is |
| 2708 | * validated or not. If {@code accept} is true, and the network was explicitly selected by the |
| 2709 | * user (e.g., by selecting a Wi-Fi network in the Settings app), then the network will become |
| 2710 | * the system default network regardless of any other network that's currently connected. If |
| 2711 | * {@code always} is true, then the choice is remembered, so that the next time the user |
| 2712 | * connects to this network, the system will switch to it. |
| 2713 | * |
| 2714 | * <p>This method requires the caller to hold the permission |
| 2715 | * {@link android.Manifest.permission#CONNECTIVITY_INTERNAL} |
| 2716 | * |
| 2717 | * @param network The network to accept. |
| 2718 | * @param accept Whether to accept the network even if unvalidated. |
| 2719 | * @param always Whether to remember this choice in the future. |
| 2720 | * |
| 2721 | * @hide |
| 2722 | */ |
| 2723 | public void setAcceptUnvalidated(Network network, boolean accept, boolean always) { |
| 2724 | try { |
| 2725 | mService.setAcceptUnvalidated(network, accept, always); |
| 2726 | } catch (RemoteException e) {} |
| 2727 | } |
| 2728 | |
| 2729 | /** |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 2730 | * Resets all connectivity manager settings back to factory defaults. |
| 2731 | * @hide |
| 2732 | */ |
| 2733 | public void factoryReset() { |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 2734 | try { |
Stuart Scott | f1fb397 | 2015-04-02 18:00:02 -0700 | [diff] [blame] | 2735 | mService.factoryReset(); |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 2736 | } catch (RemoteException e) { |
Stuart Scott | 984dc85 | 2015-03-30 13:17:11 -0700 | [diff] [blame] | 2737 | } |
| 2738 | } |
| 2739 | |
| 2740 | /** |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 2741 | * Binds the current process to {@code network}. All Sockets created in the future |
| 2742 | * (and not explicitly bound via a bound SocketFactory from |
| 2743 | * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to |
| 2744 | * {@code network}. All host name resolutions will be limited to {@code network} as well. |
| 2745 | * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to |
| 2746 | * work and all host name resolutions will fail. This is by design so an application doesn't |
| 2747 | * accidentally use Sockets it thinks are still bound to a particular {@link Network}. |
| 2748 | * To clear binding pass {@code null} for {@code network}. Using individually bound |
| 2749 | * Sockets created by Network.getSocketFactory().createSocket() and |
| 2750 | * performing network-specific host name resolutions via |
| 2751 | * {@link Network#getAllByName Network.getAllByName} is preferred to calling |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2752 | * {@code bindProcessToNetwork}. |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 2753 | * |
| 2754 | * @param network The {@link Network} to bind the current process to, or {@code null} to clear |
| 2755 | * the current binding. |
| 2756 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
| 2757 | */ |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2758 | public boolean bindProcessToNetwork(Network network) { |
| 2759 | // Forcing callers to call thru non-static function ensures ConnectivityManager |
| 2760 | // instantiated. |
| 2761 | return setProcessDefaultNetwork(network); |
| 2762 | } |
| 2763 | |
| 2764 | /** |
| 2765 | * Binds the current process to {@code network}. All Sockets created in the future |
| 2766 | * (and not explicitly bound via a bound SocketFactory from |
| 2767 | * {@link Network#getSocketFactory() Network.getSocketFactory()}) will be bound to |
| 2768 | * {@code network}. All host name resolutions will be limited to {@code network} as well. |
| 2769 | * Note that if {@code network} ever disconnects, all Sockets created in this way will cease to |
| 2770 | * work and all host name resolutions will fail. This is by design so an application doesn't |
| 2771 | * accidentally use Sockets it thinks are still bound to a particular {@link Network}. |
| 2772 | * To clear binding pass {@code null} for {@code network}. Using individually bound |
| 2773 | * Sockets created by Network.getSocketFactory().createSocket() and |
| 2774 | * performing network-specific host name resolutions via |
| 2775 | * {@link Network#getAllByName Network.getAllByName} is preferred to calling |
| 2776 | * {@code setProcessDefaultNetwork}. |
| 2777 | * |
| 2778 | * @param network The {@link Network} to bind the current process to, or {@code null} to clear |
| 2779 | * the current binding. |
| 2780 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
| 2781 | * @deprecated This function can throw {@link IllegalStateException}. Use |
| 2782 | * {@link #bindProcessToNetwork} instead. {@code bindProcessToNetwork} |
| 2783 | * is a direct replacement. |
| 2784 | */ |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 2785 | public static boolean setProcessDefaultNetwork(Network network) { |
Paul Jensen | c91b534 | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 2786 | int netId = (network == null) ? NETID_UNSET : network.netId; |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2787 | if (netId == NetworkUtils.getBoundNetworkForProcess()) { |
Paul Jensen | c91b534 | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 2788 | return true; |
| 2789 | } |
| 2790 | if (NetworkUtils.bindProcessToNetwork(netId)) { |
Paul Jensen | e0bef71 | 2014-12-10 15:12:18 -0500 | [diff] [blame] | 2791 | // Set HTTP proxy system properties to match network. |
| 2792 | // 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] | 2793 | try { |
| 2794 | Proxy.setHttpProxySystemProperty(getInstance().getDefaultProxy()); |
| 2795 | } catch (SecurityException e) { |
| 2796 | // The process doesn't have ACCESS_NETWORK_STATE, so we can't fetch the proxy. |
| 2797 | Log.e(TAG, "Can't set proxy properties", e); |
| 2798 | } |
Paul Jensen | c91b534 | 2014-08-27 12:38:45 -0400 | [diff] [blame] | 2799 | // Must flush DNS cache as new network may have different DNS resolutions. |
| 2800 | InetAddress.clearDnsCache(); |
| 2801 | // Must flush socket pool as idle sockets will be bound to previous network and may |
| 2802 | // cause subsequent fetches to be performed on old network. |
| 2803 | NetworkEventDispatcher.getInstance().onNetworkConfigurationChanged(); |
| 2804 | return true; |
| 2805 | } else { |
| 2806 | return false; |
| 2807 | } |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | /** |
| 2811 | * Returns the {@link Network} currently bound to this process via |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2812 | * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound. |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 2813 | * |
| 2814 | * @return {@code Network} to which this process is bound, or {@code null}. |
| 2815 | */ |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2816 | public Network getBoundNetworkForProcess() { |
| 2817 | // Forcing callers to call thru non-static function ensures ConnectivityManager |
| 2818 | // instantiated. |
| 2819 | return getProcessDefaultNetwork(); |
| 2820 | } |
| 2821 | |
| 2822 | /** |
| 2823 | * Returns the {@link Network} currently bound to this process via |
| 2824 | * {@link #bindProcessToNetwork}, or {@code null} if no {@link Network} is explicitly bound. |
| 2825 | * |
| 2826 | * @return {@code Network} to which this process is bound, or {@code null}. |
| 2827 | * @deprecated Using this function can lead to other functions throwing |
| 2828 | * {@link IllegalStateException}. Use {@link #getBoundNetworkForProcess} instead. |
| 2829 | * {@code getBoundNetworkForProcess} is a direct replacement. |
| 2830 | */ |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 2831 | public static Network getProcessDefaultNetwork() { |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2832 | int netId = NetworkUtils.getBoundNetworkForProcess(); |
Paul Jensen | bcc76d3 | 2014-07-11 08:17:29 -0400 | [diff] [blame] | 2833 | if (netId == NETID_UNSET) return null; |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 2834 | return new Network(netId); |
| 2835 | } |
| 2836 | |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 2837 | private void unsupportedStartingFrom(int version) { |
| 2838 | if (Process.myUid() == Process.SYSTEM_UID) { |
| 2839 | // The getApplicationInfo() call we make below is not supported in system context, and |
| 2840 | // we want to allow the system to use these APIs anyway. |
| 2841 | return; |
| 2842 | } |
| 2843 | |
| 2844 | if (mContext.getApplicationInfo().targetSdkVersion >= version) { |
| 2845 | throw new UnsupportedOperationException( |
| 2846 | "This method is not supported in target SDK version " + version + " and above"); |
| 2847 | } |
| 2848 | } |
| 2849 | |
| 2850 | // Checks whether the calling app can use the legacy routing API (startUsingNetworkFeature, |
| 2851 | // stopUsingNetworkFeature, requestRouteToHost), and if not throw UnsupportedOperationException. |
| 2852 | // TODO: convert the existing system users (Tethering, GpsLocationProvider) to the new APIs and |
| 2853 | // remove these exemptions. Note that this check is not secure, and apps can still access these |
| 2854 | // functions by accessing ConnectivityService directly. However, it should be clear that doing |
| 2855 | // so is unsupported and may break in the future. http://b/22728205 |
| 2856 | private void checkLegacyRoutingApiAccess() { |
| 2857 | if (mContext.checkCallingOrSelfPermission("com.android.permission.INJECT_OMADM_SETTINGS") |
| 2858 | == PackageManager.PERMISSION_GRANTED) { |
| 2859 | return; |
| 2860 | } |
| 2861 | |
Dianne Hackborn | 692a244 | 2015-07-31 10:35:34 -0700 | [diff] [blame] | 2862 | unsupportedStartingFrom(VERSION_CODES.M); |
Lorenzo Colitti | ffc42b0 | 2015-07-29 11:41:21 +0900 | [diff] [blame] | 2863 | } |
| 2864 | |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 2865 | /** |
| 2866 | * Binds host resolutions performed by this process to {@code network}. |
Paul Jensen | 72db88e | 2015-03-10 10:54:12 -0400 | [diff] [blame] | 2867 | * {@link #bindProcessToNetwork} takes precedence over this setting. |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 2868 | * |
| 2869 | * @param network The {@link Network} to bind host resolutions from the current process to, or |
| 2870 | * {@code null} to clear the current binding. |
| 2871 | * @return {@code true} on success, {@code false} if the {@link Network} is no longer valid. |
| 2872 | * @hide |
| 2873 | * @deprecated This is strictly for legacy usage to support {@link #startUsingNetworkFeature}. |
| 2874 | */ |
| 2875 | public static boolean setProcessDefaultNetworkForHostResolution(Network network) { |
Paul Jensen | bcc76d3 | 2014-07-11 08:17:29 -0400 | [diff] [blame] | 2876 | return NetworkUtils.bindProcessToNetworkForHostResolution( |
| 2877 | network == null ? NETID_UNSET : network.netId); |
Paul Jensen | 6d3ff9e | 2014-05-29 10:12:39 -0400 | [diff] [blame] | 2878 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 2879 | } |