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 | */ |
| 16 | |
| 17 | package android.net; |
| 18 | |
| 19 | import android.os.Parcelable; |
| 20 | import android.os.Parcel; |
| 21 | |
Jeff Sharkey | 836ecb5 | 2013-01-03 11:21:24 -0800 | [diff] [blame] | 22 | import com.android.internal.annotations.VisibleForTesting; |
| 23 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | import java.util.EnumMap; |
| 25 | |
| 26 | /** |
Scott Main | 671644c | 2011-10-06 19:02:28 -0700 | [diff] [blame] | 27 | * Describes the status of a network interface. |
| 28 | * <p>Use {@link ConnectivityManager#getActiveNetworkInfo()} to get an instance that represents |
| 29 | * the current network connection. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 30 | */ |
| 31 | public class NetworkInfo implements Parcelable { |
| 32 | |
| 33 | /** |
| 34 | * Coarse-grained network state. This is probably what most applications should |
| 35 | * use, rather than {@link android.net.NetworkInfo.DetailedState DetailedState}. |
| 36 | * The mapping between the two is as follows: |
| 37 | * <br/><br/> |
| 38 | * <table> |
| 39 | * <tr><td><b>Detailed state</b></td><td><b>Coarse-grained state</b></td></tr> |
| 40 | * <tr><td><code>IDLE</code></td><td><code>DISCONNECTED</code></td></tr> |
| 41 | * <tr><td><code>SCANNING</code></td><td><code>CONNECTING</code></td></tr> |
| 42 | * <tr><td><code>CONNECTING</code></td><td><code>CONNECTING</code></td></tr> |
| 43 | * <tr><td><code>AUTHENTICATING</code></td><td><code>CONNECTING</code></td></tr> |
Scott Main | 671644c | 2011-10-06 19:02:28 -0700 | [diff] [blame] | 44 | * <tr><td><code>CONNECTED</code></td><td><code>CONNECTED</code></td></tr> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 45 | * <tr><td><code>DISCONNECTING</code></td><td><code>DISCONNECTING</code></td></tr> |
| 46 | * <tr><td><code>DISCONNECTED</code></td><td><code>DISCONNECTED</code></td></tr> |
| 47 | * <tr><td><code>UNAVAILABLE</code></td><td><code>DISCONNECTED</code></td></tr> |
| 48 | * <tr><td><code>FAILED</code></td><td><code>DISCONNECTED</code></td></tr> |
| 49 | * </table> |
| 50 | */ |
| 51 | public enum State { |
| 52 | CONNECTING, CONNECTED, SUSPENDED, DISCONNECTING, DISCONNECTED, UNKNOWN |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * The fine-grained state of a network connection. This level of detail |
| 57 | * is probably of interest to few applications. Most should use |
| 58 | * {@link android.net.NetworkInfo.State State} instead. |
| 59 | */ |
| 60 | public enum DetailedState { |
| 61 | /** Ready to start data connection setup. */ |
| 62 | IDLE, |
| 63 | /** Searching for an available access point. */ |
| 64 | SCANNING, |
| 65 | /** Currently setting up data connection. */ |
| 66 | CONNECTING, |
| 67 | /** Network link established, performing authentication. */ |
| 68 | AUTHENTICATING, |
| 69 | /** Awaiting response from DHCP server in order to assign IP address information. */ |
| 70 | OBTAINING_IPADDR, |
| 71 | /** IP traffic should be available. */ |
| 72 | CONNECTED, |
| 73 | /** IP traffic is suspended */ |
| 74 | SUSPENDED, |
| 75 | /** Currently tearing down data connection. */ |
| 76 | DISCONNECTING, |
| 77 | /** IP traffic not available. */ |
| 78 | DISCONNECTED, |
| 79 | /** Attempt to connect failed. */ |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 80 | FAILED, |
| 81 | /** Access to this network is blocked. */ |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 82 | BLOCKED, |
| 83 | /** Link has poor connectivity. */ |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 84 | VERIFYING_POOR_LINK, |
| 85 | /** Checking if network is a captive portal */ |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 86 | CAPTIVE_PORTAL_CHECK |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | /** |
| 90 | * This is the map described in the Javadoc comment above. The positions |
| 91 | * of the elements of the array must correspond to the ordinal values |
| 92 | * of <code>DetailedState</code>. |
| 93 | */ |
| 94 | private static final EnumMap<DetailedState, State> stateMap = |
| 95 | new EnumMap<DetailedState, State>(DetailedState.class); |
| 96 | |
| 97 | static { |
| 98 | stateMap.put(DetailedState.IDLE, State.DISCONNECTED); |
| 99 | stateMap.put(DetailedState.SCANNING, State.DISCONNECTED); |
| 100 | stateMap.put(DetailedState.CONNECTING, State.CONNECTING); |
| 101 | stateMap.put(DetailedState.AUTHENTICATING, State.CONNECTING); |
| 102 | stateMap.put(DetailedState.OBTAINING_IPADDR, State.CONNECTING); |
Irfan Sheriff | 07573b3 | 2012-01-27 21:00:19 -0800 | [diff] [blame] | 103 | stateMap.put(DetailedState.VERIFYING_POOR_LINK, State.CONNECTING); |
Irfan Sheriff | da6da09 | 2012-08-16 12:49:23 -0700 | [diff] [blame] | 104 | stateMap.put(DetailedState.CAPTIVE_PORTAL_CHECK, State.CONNECTING); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 105 | stateMap.put(DetailedState.CONNECTED, State.CONNECTED); |
| 106 | stateMap.put(DetailedState.SUSPENDED, State.SUSPENDED); |
| 107 | stateMap.put(DetailedState.DISCONNECTING, State.DISCONNECTING); |
| 108 | stateMap.put(DetailedState.DISCONNECTED, State.DISCONNECTED); |
| 109 | stateMap.put(DetailedState.FAILED, State.DISCONNECTED); |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 110 | stateMap.put(DetailedState.BLOCKED, State.DISCONNECTED); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 111 | } |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 112 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 113 | private int mNetworkType; |
| 114 | private int mSubtype; |
| 115 | private String mTypeName; |
| 116 | private String mSubtypeName; |
| 117 | private State mState; |
| 118 | private DetailedState mDetailedState; |
| 119 | private String mReason; |
| 120 | private String mExtraInfo; |
| 121 | private boolean mIsFailover; |
| 122 | private boolean mIsRoaming; |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 123 | private boolean mIsConnectedToProvisioningNetwork; |
| 124 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 125 | /** |
| 126 | * Indicates whether network connectivity is possible: |
| 127 | */ |
| 128 | private boolean mIsAvailable; |
| 129 | |
| 130 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 131 | * @param type network type |
Eric Shienbrood | 2ecc74f | 2009-04-14 15:40:20 -0700 | [diff] [blame] | 132 | * @deprecated |
| 133 | * @hide because this constructor was only meant for internal use (and |
| 134 | * has now been superseded by the package-private constructor below). |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 135 | */ |
| 136 | public NetworkInfo(int type) {} |
| 137 | |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 138 | /** |
| 139 | * @hide |
| 140 | */ |
| 141 | public NetworkInfo(int type, int subtype, String typeName, String subtypeName) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 142 | if (!ConnectivityManager.isNetworkTypeValid(type)) { |
| 143 | throw new IllegalArgumentException("Invalid network type: " + type); |
| 144 | } |
| 145 | mNetworkType = type; |
| 146 | mSubtype = subtype; |
| 147 | mTypeName = typeName; |
| 148 | mSubtypeName = subtypeName; |
| 149 | setDetailedState(DetailedState.IDLE, null, null); |
| 150 | mState = State.UNKNOWN; |
Robert Greenwalt | 8206ff3 | 2009-09-10 15:06:20 -0700 | [diff] [blame] | 151 | mIsAvailable = false; // until we're told otherwise, assume unavailable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 152 | mIsRoaming = false; |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 153 | mIsConnectedToProvisioningNetwork = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 156 | /** {@hide} */ |
| 157 | public NetworkInfo(NetworkInfo source) { |
| 158 | if (source != null) { |
Narayan Kamath | 12b28ce | 2013-10-11 13:43:30 +0100 | [diff] [blame] | 159 | synchronized (source) { |
| 160 | mNetworkType = source.mNetworkType; |
| 161 | mSubtype = source.mSubtype; |
| 162 | mTypeName = source.mTypeName; |
| 163 | mSubtypeName = source.mSubtypeName; |
| 164 | mState = source.mState; |
| 165 | mDetailedState = source.mDetailedState; |
| 166 | mReason = source.mReason; |
| 167 | mExtraInfo = source.mExtraInfo; |
| 168 | mIsFailover = source.mIsFailover; |
| 169 | mIsRoaming = source.mIsRoaming; |
| 170 | mIsAvailable = source.mIsAvailable; |
| 171 | mIsConnectedToProvisioningNetwork = source.mIsConnectedToProvisioningNetwork; |
| 172 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 173 | } |
| 174 | } |
| 175 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 176 | /** |
Scott Main | 671644c | 2011-10-06 19:02:28 -0700 | [diff] [blame] | 177 | * Reports the type of network to which the |
| 178 | * info in this {@code NetworkInfo} pertains. |
| 179 | * @return one of {@link ConnectivityManager#TYPE_MOBILE}, {@link |
| 180 | * ConnectivityManager#TYPE_WIFI}, {@link ConnectivityManager#TYPE_WIMAX}, {@link |
| 181 | * ConnectivityManager#TYPE_ETHERNET}, {@link ConnectivityManager#TYPE_BLUETOOTH}, or other |
| 182 | * types defined by {@link ConnectivityManager} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | */ |
| 184 | public int getType() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 185 | synchronized (this) { |
| 186 | return mNetworkType; |
| 187 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Return a network-type-specific integer describing the subtype |
| 192 | * of the network. |
| 193 | * @return the network subtype |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 194 | */ |
| 195 | public int getSubtype() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 196 | synchronized (this) { |
| 197 | return mSubtype; |
| 198 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void setSubtype(int subtype, String subtypeName) { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 202 | synchronized (this) { |
| 203 | mSubtype = subtype; |
| 204 | mSubtypeName = subtypeName; |
| 205 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | /** |
| 209 | * Return a human-readable name describe the type of the network, |
| 210 | * for example "WIFI" or "MOBILE". |
| 211 | * @return the name of the network type |
| 212 | */ |
| 213 | public String getTypeName() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 214 | synchronized (this) { |
| 215 | return mTypeName; |
| 216 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Return a human-readable name describing the subtype of the network. |
| 221 | * @return the name of the network subtype |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | */ |
| 223 | public String getSubtypeName() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 224 | synchronized (this) { |
| 225 | return mSubtypeName; |
| 226 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 227 | } |
| 228 | |
| 229 | /** |
| 230 | * Indicates whether network connectivity exists or is in the process |
| 231 | * of being established. This is good for applications that need to |
| 232 | * do anything related to the network other than read or write data. |
| 233 | * For the latter, call {@link #isConnected()} instead, which guarantees |
| 234 | * that the network is fully usable. |
| 235 | * @return {@code true} if network connectivity exists or is in the process |
| 236 | * of being established, {@code false} otherwise. |
| 237 | */ |
| 238 | public boolean isConnectedOrConnecting() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 239 | synchronized (this) { |
| 240 | return mState == State.CONNECTED || mState == State.CONNECTING; |
| 241 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /** |
| 245 | * Indicates whether network connectivity exists and it is possible to establish |
| 246 | * connections and pass data. |
Scott Main | 671644c | 2011-10-06 19:02:28 -0700 | [diff] [blame] | 247 | * <p>Always call this before attempting to perform data transactions. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 248 | * @return {@code true} if network connectivity exists, {@code false} otherwise. |
| 249 | */ |
| 250 | public boolean isConnected() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 251 | synchronized (this) { |
| 252 | return mState == State.CONNECTED; |
| 253 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /** |
| 257 | * Indicates whether network connectivity is possible. A network is unavailable |
| 258 | * when a persistent or semi-persistent condition prevents the possibility |
| 259 | * of connecting to that network. Examples include |
| 260 | * <ul> |
| 261 | * <li>The device is out of the coverage area for any network of this type.</li> |
| 262 | * <li>The device is on a network other than the home network (i.e., roaming), and |
| 263 | * data roaming has been disabled.</li> |
| 264 | * <li>The device's radio is turned off, e.g., because airplane mode is enabled.</li> |
| 265 | * </ul> |
| 266 | * @return {@code true} if the network is available, {@code false} otherwise |
| 267 | */ |
| 268 | public boolean isAvailable() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 269 | synchronized (this) { |
| 270 | return mIsAvailable; |
| 271 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 272 | } |
| 273 | |
| 274 | /** |
| 275 | * Sets if the network is available, ie, if the connectivity is possible. |
| 276 | * @param isAvailable the new availability value. |
| 277 | * |
| 278 | * @hide |
| 279 | */ |
| 280 | public void setIsAvailable(boolean isAvailable) { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 281 | synchronized (this) { |
| 282 | mIsAvailable = isAvailable; |
| 283 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | /** |
| 287 | * Indicates whether the current attempt to connect to the network |
| 288 | * resulted from the ConnectivityManager trying to fail over to this |
| 289 | * network following a disconnect from another network. |
| 290 | * @return {@code true} if this is a failover attempt, {@code false} |
| 291 | * otherwise. |
| 292 | */ |
| 293 | public boolean isFailover() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 294 | synchronized (this) { |
| 295 | return mIsFailover; |
| 296 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Set the failover boolean. |
| 301 | * @param isFailover {@code true} to mark the current connection attempt |
| 302 | * as a failover. |
| 303 | * @hide |
| 304 | */ |
| 305 | public void setFailover(boolean isFailover) { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 306 | synchronized (this) { |
| 307 | mIsFailover = isFailover; |
| 308 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Indicates whether the device is currently roaming on this network. |
| 313 | * When {@code true}, it suggests that use of data on this network |
| 314 | * may incur extra costs. |
| 315 | * @return {@code true} if roaming is in effect, {@code false} otherwise. |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 316 | */ |
| 317 | public boolean isRoaming() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 318 | synchronized (this) { |
| 319 | return mIsRoaming; |
| 320 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 321 | } |
| 322 | |
Jeff Sharkey | 836ecb5 | 2013-01-03 11:21:24 -0800 | [diff] [blame] | 323 | /** {@hide} */ |
| 324 | @VisibleForTesting |
| 325 | public void setRoaming(boolean isRoaming) { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 326 | synchronized (this) { |
| 327 | mIsRoaming = isRoaming; |
| 328 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 331 | /** {@hide} */ |
| 332 | @VisibleForTesting |
| 333 | public boolean isConnectedToProvisioningNetwork() { |
| 334 | synchronized (this) { |
| 335 | return mIsConnectedToProvisioningNetwork; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | /** {@hide} */ |
| 340 | @VisibleForTesting |
| 341 | public void setIsConnectedToProvisioningNetwork(boolean val) { |
| 342 | synchronized (this) { |
| 343 | mIsConnectedToProvisioningNetwork = val; |
| 344 | } |
| 345 | } |
| 346 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 347 | /** |
| 348 | * Reports the current coarse-grained state of the network. |
| 349 | * @return the coarse-grained state |
| 350 | */ |
| 351 | public State getState() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 352 | synchronized (this) { |
| 353 | return mState; |
| 354 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 355 | } |
| 356 | |
| 357 | /** |
| 358 | * Reports the current fine-grained state of the network. |
| 359 | * @return the fine-grained state |
| 360 | */ |
| 361 | public DetailedState getDetailedState() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 362 | synchronized (this) { |
| 363 | return mDetailedState; |
| 364 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 365 | } |
| 366 | |
| 367 | /** |
| 368 | * Sets the fine-grained state of the network. |
| 369 | * @param detailedState the {@link DetailedState}. |
| 370 | * @param reason a {@code String} indicating the reason for the state change, |
| 371 | * if one was supplied. May be {@code null}. |
| 372 | * @param extraInfo an optional {@code String} providing addditional network state |
| 373 | * information passed up from the lower networking layers. |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 374 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 375 | */ |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 376 | public void setDetailedState(DetailedState detailedState, String reason, String extraInfo) { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 377 | synchronized (this) { |
| 378 | this.mDetailedState = detailedState; |
| 379 | this.mState = stateMap.get(detailedState); |
| 380 | this.mReason = reason; |
| 381 | this.mExtraInfo = extraInfo; |
| 382 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | /** |
Doug Zongker | d60ae7f | 2011-11-03 12:45:42 -0700 | [diff] [blame] | 386 | * Set the extraInfo field. |
| 387 | * @param extraInfo an optional {@code String} providing addditional network state |
| 388 | * information passed up from the lower networking layers. |
| 389 | * @hide |
| 390 | */ |
| 391 | public void setExtraInfo(String extraInfo) { |
| 392 | synchronized (this) { |
| 393 | this.mExtraInfo = extraInfo; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 398 | * Report the reason an attempt to establish connectivity failed, |
| 399 | * if one is available. |
| 400 | * @return the reason for failure, or null if not available |
| 401 | */ |
| 402 | public String getReason() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 403 | synchronized (this) { |
| 404 | return mReason; |
| 405 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | /** |
| 409 | * Report the extra information about the network state, if any was |
| 410 | * provided by the lower networking layers., |
| 411 | * if one is available. |
| 412 | * @return the extra information, or null if not available |
| 413 | */ |
| 414 | public String getExtraInfo() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 415 | synchronized (this) { |
| 416 | return mExtraInfo; |
| 417 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | @Override |
| 421 | public String toString() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 422 | synchronized (this) { |
| 423 | StringBuilder builder = new StringBuilder("NetworkInfo: "); |
| 424 | builder.append("type: ").append(getTypeName()).append("[").append(getSubtypeName()). |
| 425 | append("], state: ").append(mState).append("/").append(mDetailedState). |
| 426 | append(", reason: ").append(mReason == null ? "(unspecified)" : mReason). |
| 427 | append(", extra: ").append(mExtraInfo == null ? "(none)" : mExtraInfo). |
| 428 | append(", roaming: ").append(mIsRoaming). |
| 429 | append(", failover: ").append(mIsFailover). |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 430 | append(", isAvailable: ").append(mIsAvailable). |
| 431 | append(", isConnectedToProvisioningNetwork: "). |
| 432 | append(mIsConnectedToProvisioningNetwork); |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 433 | return builder.toString(); |
| 434 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /** |
| 438 | * Implement the Parcelable interface |
| 439 | * @hide |
| 440 | */ |
| 441 | public int describeContents() { |
| 442 | return 0; |
| 443 | } |
| 444 | |
| 445 | /** |
| 446 | * Implement the Parcelable interface. |
| 447 | * @hide |
| 448 | */ |
| 449 | public void writeToParcel(Parcel dest, int flags) { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 450 | synchronized (this) { |
| 451 | dest.writeInt(mNetworkType); |
| 452 | dest.writeInt(mSubtype); |
| 453 | dest.writeString(mTypeName); |
| 454 | dest.writeString(mSubtypeName); |
| 455 | dest.writeString(mState.name()); |
| 456 | dest.writeString(mDetailedState.name()); |
| 457 | dest.writeInt(mIsFailover ? 1 : 0); |
| 458 | dest.writeInt(mIsAvailable ? 1 : 0); |
| 459 | dest.writeInt(mIsRoaming ? 1 : 0); |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 460 | dest.writeInt(mIsConnectedToProvisioningNetwork ? 1 : 0); |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 461 | dest.writeString(mReason); |
| 462 | dest.writeString(mExtraInfo); |
| 463 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | /** |
| 467 | * Implement the Parcelable interface. |
| 468 | * @hide |
| 469 | */ |
| 470 | public static final Creator<NetworkInfo> CREATOR = |
| 471 | new Creator<NetworkInfo>() { |
| 472 | public NetworkInfo createFromParcel(Parcel in) { |
| 473 | int netType = in.readInt(); |
| 474 | int subtype = in.readInt(); |
| 475 | String typeName = in.readString(); |
| 476 | String subtypeName = in.readString(); |
| 477 | NetworkInfo netInfo = new NetworkInfo(netType, subtype, typeName, subtypeName); |
| 478 | netInfo.mState = State.valueOf(in.readString()); |
| 479 | netInfo.mDetailedState = DetailedState.valueOf(in.readString()); |
| 480 | netInfo.mIsFailover = in.readInt() != 0; |
| 481 | netInfo.mIsAvailable = in.readInt() != 0; |
| 482 | netInfo.mIsRoaming = in.readInt() != 0; |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 483 | netInfo.mIsConnectedToProvisioningNetwork = in.readInt() != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 484 | netInfo.mReason = in.readString(); |
| 485 | netInfo.mExtraInfo = in.readString(); |
| 486 | return netInfo; |
| 487 | } |
| 488 | |
| 489 | public NetworkInfo[] newArray(int size) { |
| 490 | return new NetworkInfo[size]; |
| 491 | } |
| 492 | }; |
| 493 | } |