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 | /** |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 131 | * @hide |
| 132 | */ |
| 133 | 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] | 134 | if (!ConnectivityManager.isNetworkTypeValid(type)) { |
| 135 | throw new IllegalArgumentException("Invalid network type: " + type); |
| 136 | } |
| 137 | mNetworkType = type; |
| 138 | mSubtype = subtype; |
| 139 | mTypeName = typeName; |
| 140 | mSubtypeName = subtypeName; |
| 141 | setDetailedState(DetailedState.IDLE, null, null); |
| 142 | mState = State.UNKNOWN; |
Robert Greenwalt | 8206ff3 | 2009-09-10 15:06:20 -0700 | [diff] [blame] | 143 | mIsAvailable = false; // until we're told otherwise, assume unavailable |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 144 | mIsRoaming = false; |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 145 | mIsConnectedToProvisioningNetwork = false; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 148 | /** {@hide} */ |
| 149 | public NetworkInfo(NetworkInfo source) { |
| 150 | if (source != null) { |
Narayan Kamath | 12b28ce | 2013-10-11 13:43:30 +0100 | [diff] [blame] | 151 | synchronized (source) { |
| 152 | mNetworkType = source.mNetworkType; |
| 153 | mSubtype = source.mSubtype; |
| 154 | mTypeName = source.mTypeName; |
| 155 | mSubtypeName = source.mSubtypeName; |
| 156 | mState = source.mState; |
| 157 | mDetailedState = source.mDetailedState; |
| 158 | mReason = source.mReason; |
| 159 | mExtraInfo = source.mExtraInfo; |
| 160 | mIsFailover = source.mIsFailover; |
| 161 | mIsRoaming = source.mIsRoaming; |
| 162 | mIsAvailable = source.mIsAvailable; |
| 163 | mIsConnectedToProvisioningNetwork = source.mIsConnectedToProvisioningNetwork; |
| 164 | } |
Jeff Sharkey | c006f1a | 2011-05-19 17:12:49 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 168 | /** |
Scott Main | 671644c | 2011-10-06 19:02:28 -0700 | [diff] [blame] | 169 | * Reports the type of network to which the |
| 170 | * info in this {@code NetworkInfo} pertains. |
| 171 | * @return one of {@link ConnectivityManager#TYPE_MOBILE}, {@link |
| 172 | * ConnectivityManager#TYPE_WIFI}, {@link ConnectivityManager#TYPE_WIMAX}, {@link |
| 173 | * ConnectivityManager#TYPE_ETHERNET}, {@link ConnectivityManager#TYPE_BLUETOOTH}, or other |
| 174 | * types defined by {@link ConnectivityManager} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | */ |
| 176 | public int getType() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 177 | synchronized (this) { |
| 178 | return mNetworkType; |
| 179 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | /** |
Robert Greenwalt | b90b20b | 2014-06-02 15:32:02 -0700 | [diff] [blame] | 183 | * @hide |
| 184 | */ |
| 185 | public void setType(int type) { |
| 186 | synchronized (this) { |
| 187 | mNetworkType = type; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | * Return a network-type-specific integer describing the subtype |
| 193 | * of the network. |
| 194 | * @return the network subtype |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 195 | */ |
| 196 | public int getSubtype() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 197 | synchronized (this) { |
| 198 | return mSubtype; |
| 199 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Robert Greenwalt | 3192dec | 2014-05-27 13:20:24 -0700 | [diff] [blame] | 202 | /** |
| 203 | * @hide |
| 204 | */ |
| 205 | public void setSubtype(int subtype, String subtypeName) { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 206 | synchronized (this) { |
| 207 | mSubtype = subtype; |
| 208 | mSubtypeName = subtypeName; |
| 209 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Return a human-readable name describe the type of the network, |
| 214 | * for example "WIFI" or "MOBILE". |
| 215 | * @return the name of the network type |
| 216 | */ |
| 217 | public String getTypeName() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 218 | synchronized (this) { |
| 219 | return mTypeName; |
| 220 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | /** |
| 224 | * Return a human-readable name describing the subtype of the network. |
| 225 | * @return the name of the network subtype |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 226 | */ |
| 227 | public String getSubtypeName() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 228 | synchronized (this) { |
| 229 | return mSubtypeName; |
| 230 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Indicates whether network connectivity exists or is in the process |
| 235 | * of being established. This is good for applications that need to |
| 236 | * do anything related to the network other than read or write data. |
| 237 | * For the latter, call {@link #isConnected()} instead, which guarantees |
| 238 | * that the network is fully usable. |
| 239 | * @return {@code true} if network connectivity exists or is in the process |
| 240 | * of being established, {@code false} otherwise. |
| 241 | */ |
| 242 | public boolean isConnectedOrConnecting() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 243 | synchronized (this) { |
| 244 | return mState == State.CONNECTED || mState == State.CONNECTING; |
| 245 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /** |
| 249 | * Indicates whether network connectivity exists and it is possible to establish |
| 250 | * connections and pass data. |
Scott Main | 671644c | 2011-10-06 19:02:28 -0700 | [diff] [blame] | 251 | * <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] | 252 | * @return {@code true} if network connectivity exists, {@code false} otherwise. |
| 253 | */ |
| 254 | public boolean isConnected() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 255 | synchronized (this) { |
| 256 | return mState == State.CONNECTED; |
| 257 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | /** |
| 261 | * Indicates whether network connectivity is possible. A network is unavailable |
| 262 | * when a persistent or semi-persistent condition prevents the possibility |
| 263 | * of connecting to that network. Examples include |
| 264 | * <ul> |
| 265 | * <li>The device is out of the coverage area for any network of this type.</li> |
| 266 | * <li>The device is on a network other than the home network (i.e., roaming), and |
| 267 | * data roaming has been disabled.</li> |
| 268 | * <li>The device's radio is turned off, e.g., because airplane mode is enabled.</li> |
| 269 | * </ul> |
| 270 | * @return {@code true} if the network is available, {@code false} otherwise |
| 271 | */ |
| 272 | public boolean isAvailable() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 273 | synchronized (this) { |
| 274 | return mIsAvailable; |
| 275 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | /** |
| 279 | * Sets if the network is available, ie, if the connectivity is possible. |
| 280 | * @param isAvailable the new availability value. |
| 281 | * |
| 282 | * @hide |
| 283 | */ |
| 284 | public void setIsAvailable(boolean isAvailable) { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 285 | synchronized (this) { |
| 286 | mIsAvailable = isAvailable; |
| 287 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Indicates whether the current attempt to connect to the network |
| 292 | * resulted from the ConnectivityManager trying to fail over to this |
| 293 | * network following a disconnect from another network. |
| 294 | * @return {@code true} if this is a failover attempt, {@code false} |
| 295 | * otherwise. |
| 296 | */ |
| 297 | public boolean isFailover() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 298 | synchronized (this) { |
| 299 | return mIsFailover; |
| 300 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 301 | } |
| 302 | |
| 303 | /** |
| 304 | * Set the failover boolean. |
| 305 | * @param isFailover {@code true} to mark the current connection attempt |
| 306 | * as a failover. |
| 307 | * @hide |
| 308 | */ |
| 309 | public void setFailover(boolean isFailover) { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 310 | synchronized (this) { |
| 311 | mIsFailover = isFailover; |
| 312 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Indicates whether the device is currently roaming on this network. |
| 317 | * When {@code true}, it suggests that use of data on this network |
| 318 | * may incur extra costs. |
| 319 | * @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] | 320 | */ |
| 321 | public boolean isRoaming() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 322 | synchronized (this) { |
| 323 | return mIsRoaming; |
| 324 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 325 | } |
| 326 | |
Jeff Sharkey | 836ecb5 | 2013-01-03 11:21:24 -0800 | [diff] [blame] | 327 | /** {@hide} */ |
| 328 | @VisibleForTesting |
| 329 | public void setRoaming(boolean isRoaming) { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 330 | synchronized (this) { |
| 331 | mIsRoaming = isRoaming; |
| 332 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 333 | } |
| 334 | |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 335 | /** {@hide} */ |
| 336 | @VisibleForTesting |
| 337 | public boolean isConnectedToProvisioningNetwork() { |
| 338 | synchronized (this) { |
| 339 | return mIsConnectedToProvisioningNetwork; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | /** {@hide} */ |
| 344 | @VisibleForTesting |
| 345 | public void setIsConnectedToProvisioningNetwork(boolean val) { |
| 346 | synchronized (this) { |
| 347 | mIsConnectedToProvisioningNetwork = val; |
| 348 | } |
| 349 | } |
| 350 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 351 | /** |
| 352 | * Reports the current coarse-grained state of the network. |
| 353 | * @return the coarse-grained state |
| 354 | */ |
| 355 | public State getState() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 356 | synchronized (this) { |
| 357 | return mState; |
| 358 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 359 | } |
| 360 | |
| 361 | /** |
| 362 | * Reports the current fine-grained state of the network. |
| 363 | * @return the fine-grained state |
| 364 | */ |
| 365 | public DetailedState getDetailedState() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 366 | synchronized (this) { |
| 367 | return mDetailedState; |
| 368 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | /** |
| 372 | * Sets the fine-grained state of the network. |
| 373 | * @param detailedState the {@link DetailedState}. |
| 374 | * @param reason a {@code String} indicating the reason for the state change, |
| 375 | * if one was supplied. May be {@code null}. |
| 376 | * @param extraInfo an optional {@code String} providing addditional network state |
| 377 | * information passed up from the lower networking layers. |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 378 | * @hide |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 379 | */ |
Irfan Sheriff | d649c12 | 2010-06-09 15:39:36 -0700 | [diff] [blame] | 380 | public void setDetailedState(DetailedState detailedState, String reason, String extraInfo) { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 381 | synchronized (this) { |
| 382 | this.mDetailedState = detailedState; |
| 383 | this.mState = stateMap.get(detailedState); |
| 384 | this.mReason = reason; |
| 385 | this.mExtraInfo = extraInfo; |
| 386 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /** |
Doug Zongker | d60ae7f | 2011-11-03 12:45:42 -0700 | [diff] [blame] | 390 | * Set the extraInfo field. |
| 391 | * @param extraInfo an optional {@code String} providing addditional network state |
| 392 | * information passed up from the lower networking layers. |
| 393 | * @hide |
| 394 | */ |
| 395 | public void setExtraInfo(String extraInfo) { |
| 396 | synchronized (this) { |
| 397 | this.mExtraInfo = extraInfo; |
| 398 | } |
| 399 | } |
| 400 | |
| 401 | /** |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 402 | * Report the reason an attempt to establish connectivity failed, |
| 403 | * if one is available. |
| 404 | * @return the reason for failure, or null if not available |
| 405 | */ |
| 406 | public String getReason() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 407 | synchronized (this) { |
| 408 | return mReason; |
| 409 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | /** |
| 413 | * Report the extra information about the network state, if any was |
| 414 | * provided by the lower networking layers., |
| 415 | * if one is available. |
| 416 | * @return the extra information, or null if not available |
| 417 | */ |
| 418 | public String getExtraInfo() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 419 | synchronized (this) { |
| 420 | return mExtraInfo; |
| 421 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 422 | } |
| 423 | |
| 424 | @Override |
| 425 | public String toString() { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 426 | synchronized (this) { |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 427 | StringBuilder builder = new StringBuilder("["); |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 428 | builder.append("type: ").append(getTypeName()).append("[").append(getSubtypeName()). |
| 429 | append("], state: ").append(mState).append("/").append(mDetailedState). |
| 430 | append(", reason: ").append(mReason == null ? "(unspecified)" : mReason). |
| 431 | append(", extra: ").append(mExtraInfo == null ? "(none)" : mExtraInfo). |
| 432 | append(", roaming: ").append(mIsRoaming). |
| 433 | append(", failover: ").append(mIsFailover). |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 434 | append(", isAvailable: ").append(mIsAvailable). |
| 435 | append(", isConnectedToProvisioningNetwork: "). |
Robert Greenwalt | c9c90c7 | 2014-05-13 15:36:27 -0700 | [diff] [blame] | 436 | append(mIsConnectedToProvisioningNetwork). |
| 437 | append("]"); |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 438 | return builder.toString(); |
| 439 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 440 | } |
| 441 | |
| 442 | /** |
| 443 | * Implement the Parcelable interface |
| 444 | * @hide |
| 445 | */ |
| 446 | public int describeContents() { |
| 447 | return 0; |
| 448 | } |
| 449 | |
| 450 | /** |
| 451 | * Implement the Parcelable interface. |
| 452 | * @hide |
| 453 | */ |
| 454 | public void writeToParcel(Parcel dest, int flags) { |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 455 | synchronized (this) { |
| 456 | dest.writeInt(mNetworkType); |
| 457 | dest.writeInt(mSubtype); |
| 458 | dest.writeString(mTypeName); |
| 459 | dest.writeString(mSubtypeName); |
| 460 | dest.writeString(mState.name()); |
| 461 | dest.writeString(mDetailedState.name()); |
| 462 | dest.writeInt(mIsFailover ? 1 : 0); |
| 463 | dest.writeInt(mIsAvailable ? 1 : 0); |
| 464 | dest.writeInt(mIsRoaming ? 1 : 0); |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 465 | dest.writeInt(mIsConnectedToProvisioningNetwork ? 1 : 0); |
Irfan Sheriff | a2a1b91 | 2010-06-07 09:03:04 -0700 | [diff] [blame] | 466 | dest.writeString(mReason); |
| 467 | dest.writeString(mExtraInfo); |
| 468 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | /** |
| 472 | * Implement the Parcelable interface. |
| 473 | * @hide |
| 474 | */ |
| 475 | public static final Creator<NetworkInfo> CREATOR = |
| 476 | new Creator<NetworkInfo>() { |
| 477 | public NetworkInfo createFromParcel(Parcel in) { |
| 478 | int netType = in.readInt(); |
| 479 | int subtype = in.readInt(); |
| 480 | String typeName = in.readString(); |
| 481 | String subtypeName = in.readString(); |
| 482 | NetworkInfo netInfo = new NetworkInfo(netType, subtype, typeName, subtypeName); |
| 483 | netInfo.mState = State.valueOf(in.readString()); |
| 484 | netInfo.mDetailedState = DetailedState.valueOf(in.readString()); |
| 485 | netInfo.mIsFailover = in.readInt() != 0; |
| 486 | netInfo.mIsAvailable = in.readInt() != 0; |
| 487 | netInfo.mIsRoaming = in.readInt() != 0; |
Wink Saville | 67c3821 | 2013-09-05 12:02:25 -0700 | [diff] [blame] | 488 | netInfo.mIsConnectedToProvisioningNetwork = in.readInt() != 0; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 489 | netInfo.mReason = in.readString(); |
| 490 | netInfo.mExtraInfo = in.readString(); |
| 491 | return netInfo; |
| 492 | } |
| 493 | |
| 494 | public NetworkInfo[] newArray(int size) { |
| 495 | return new NetworkInfo[size]; |
| 496 | } |
| 497 | }; |
| 498 | } |