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