NetworkInfo.java revision 07573b32494acbabd21979d8b9584c1ed3f7a6ad
165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian/*
265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * Copyright (C) 2008 The Android Open Source Project
365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian *
465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * you may not use this file except in compliance with the License.
665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * You may obtain a copy of the License at
765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian *
865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian *
1065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * Unless required by applicable law or agreed to in writing, software
1165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
1265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * See the License for the specific language governing permissions and
1465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * limitations under the License.
1565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian */
1665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianpackage android.net;
1865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianimport android.os.Parcelable;
207c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurentimport android.os.Parcel;
217c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
22d1d8f23dde239289ba8c0de0944baaca3586c880Glenn Kastenimport java.util.EnumMap;
2365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
247c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent/**
25d2dcb08808aed82218b258726fa66f8e095a3f9cGlenn Kasten * Describes the status of a network interface.
2664760240f931714858a59c1579f07264d7182ba2Dima Zavin * <p>Use {@link ConnectivityManager#getActiveNetworkInfo()} to get an instance that represents
277394a4f358fa9908a9f0a7c954b65c399f4268e6Dima Zavin * the current network connection.
28fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin */
297c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurentpublic class NetworkInfo implements Parcelable {
307c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
317c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    /**
32fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin     * Coarse-grained network state. This is probably what most applications should
3365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * use, rather than {@link android.net.NetworkInfo.DetailedState DetailedState}.
3465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * The mapping between the two is as follows:
3565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * <br/><br/>
3665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * <table>
37d2dcb08808aed82218b258726fa66f8e095a3f9cGlenn Kasten     * <tr><td><b>Detailed state</b></td><td><b>Coarse-grained state</b></td></tr>
38d2dcb08808aed82218b258726fa66f8e095a3f9cGlenn Kasten     * <tr><td><code>IDLE</code></td><td><code>DISCONNECTED</code></td></tr>
39d2dcb08808aed82218b258726fa66f8e095a3f9cGlenn Kasten     * <tr><td><code>SCANNING</code></td><td><code>CONNECTING</code></td></tr>
40fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin     * <tr><td><code>CONNECTING</code></td><td><code>CONNECTING</code></td></tr>
41de070137f11d346fba77605bd76a44c040a618fcEric Laurent     * <tr><td><code>AUTHENTICATING</code></td><td><code>CONNECTING</code></td></tr>
4265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * <tr><td><code>CONNECTED</code></td><td><code>CONNECTED</code></td></tr>
43d2dcb08808aed82218b258726fa66f8e095a3f9cGlenn Kasten     * <tr><td><code>DISCONNECTING</code></td><td><code>DISCONNECTING</code></td></tr>
4465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * <tr><td><code>DISCONNECTED</code></td><td><code>DISCONNECTED</code></td></tr>
4565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * <tr><td><code>UNAVAILABLE</code></td><td><code>DISCONNECTED</code></td></tr>
46d2dcb08808aed82218b258726fa66f8e095a3f9cGlenn Kasten     * <tr><td><code>FAILED</code></td><td><code>DISCONNECTED</code></td></tr>
47d2dcb08808aed82218b258726fa66f8e095a3f9cGlenn Kasten     * </table>
4865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     */
4965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    public enum State {
5065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        CONNECTING, CONNECTED, SUSPENDED, DISCONNECTING, DISCONNECTED, UNKNOWN
5165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
5265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    /**
5465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * The fine-grained state of a network connection. This level of detail
55fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin     * is probably of interest to few applications. Most should use
56fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin     * {@link android.net.NetworkInfo.State State} instead.
5765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     */
58fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    public enum DetailedState {
59fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        /** Ready to start data connection setup. */
60de070137f11d346fba77605bd76a44c040a618fcEric Laurent        IDLE,
61f78aee70d15daf4690de7e7b4983ee68b0d1381dGlenn Kasten        /** Searching for an available access point. */
62fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        SCANNING,
63fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        /** Currently setting up data connection. */
64fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        CONNECTING,
6565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        /** Network link established, performing authentication. */
6658f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten        AUTHENTICATING,
6765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        /** Awaiting response from DHCP server in order to assign IP address information. */
680ca3cf94c0dfc173ad7886ae162c4b67067539f6Eric Laurent        OBTAINING_IPADDR,
690ca3cf94c0dfc173ad7886ae162c4b67067539f6Eric Laurent        /** IP traffic should be available. */
70de070137f11d346fba77605bd76a44c040a618fcEric Laurent        CONNECTED,
71fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        /** IP traffic is suspended */
72de070137f11d346fba77605bd76a44c040a618fcEric Laurent        SUSPENDED,
73de070137f11d346fba77605bd76a44c040a618fcEric Laurent        /** Currently tearing down data connection. */
74fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        DISCONNECTING,
75de070137f11d346fba77605bd76a44c040a618fcEric Laurent        /** IP traffic not available. */
7665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        DISCONNECTED,
77eba51fb3a361f67a6a64d5a16eba6084fe27d60eGlenn Kasten        /** Attempt to connect failed. */
7865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        FAILED,
7958f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten        /** Access to this network is blocked. */
8065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        BLOCKED,
81fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        /** Link has poor connectivity. */
82de9719b3ec71472e6bf75117152176af51d1a515Glenn Kasten        VERIFYING_POOR_LINK
837c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
8465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
8565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    /**
8665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * This is the map described in the Javadoc comment above. The positions
87fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin     * of the elements of the array must correspond to the ordinal values
8865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * of <code>DetailedState</code>.
8965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     */
9083844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent    private static final EnumMap<DetailedState, State> stateMap =
9183844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent        new EnumMap<DetailedState, State>(DetailedState.class);
9283844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent
9383844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent    static {
9483844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent        stateMap.put(DetailedState.IDLE, State.DISCONNECTED);
9583844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent        stateMap.put(DetailedState.SCANNING, State.DISCONNECTED);
9665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        stateMap.put(DetailedState.CONNECTING, State.CONNECTING);
97fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        stateMap.put(DetailedState.AUTHENTICATING, State.CONNECTING);
986374252107fd6539397598195ea6defd5870fafbEric Laurent        stateMap.put(DetailedState.OBTAINING_IPADDR, State.CONNECTING);
99de070137f11d346fba77605bd76a44c040a618fcEric Laurent        stateMap.put(DetailedState.VERIFYING_POOR_LINK, State.CONNECTING);
100de070137f11d346fba77605bd76a44c040a618fcEric Laurent        stateMap.put(DetailedState.CONNECTED, State.CONNECTED);
101de070137f11d346fba77605bd76a44c040a618fcEric Laurent        stateMap.put(DetailedState.SUSPENDED, State.SUSPENDED);
1027c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        stateMap.put(DetailedState.DISCONNECTING, State.DISCONNECTING);
103de070137f11d346fba77605bd76a44c040a618fcEric Laurent        stateMap.put(DetailedState.DISCONNECTED, State.DISCONNECTED);
104de070137f11d346fba77605bd76a44c040a618fcEric Laurent        stateMap.put(DetailedState.FAILED, State.DISCONNECTED);
105de070137f11d346fba77605bd76a44c040a618fcEric Laurent        stateMap.put(DetailedState.BLOCKED, State.DISCONNECTED);
106de070137f11d346fba77605bd76a44c040a618fcEric Laurent    }
107db7c079f284f6e91266f6653ae0ec198b1c5006eEric Laurent
108fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten    private int mNetworkType;
109de070137f11d346fba77605bd76a44c040a618fcEric Laurent    private int mSubtype;
11057dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent    private String mTypeName;
11157dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent    private String mSubtypeName;
11257dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent    private State mState;
11365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    private DetailedState mDetailedState;
11465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    private String mReason;
11565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    private String mExtraInfo;
11665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    private boolean mIsFailover;
11765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    private boolean mIsRoaming;
11865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    /**
11965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * Indicates whether network connectivity is possible:
12065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     */
12165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    private boolean mIsAvailable;
12265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
123fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    /**
124fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin     * @param type network type
125fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin     * @deprecated
12665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * @hide because this constructor was only meant for internal use (and
127fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin     * has now been superseded by the package-private constructor below).
128fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin     */
129fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    public NetworkInfo(int type) {}
130fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
131fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    /**
132de070137f11d346fba77605bd76a44c040a618fcEric Laurent     * @hide
133de070137f11d346fba77605bd76a44c040a618fcEric Laurent     */
134de070137f11d346fba77605bd76a44c040a618fcEric Laurent    public NetworkInfo(int type, int subtype, String typeName, String subtypeName) {
135fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        if (!ConnectivityManager.isNetworkTypeValid(type)) {
13665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            throw new IllegalArgumentException("Invalid network type: " + type);
13765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
13865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mNetworkType = type;
13965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mSubtype = subtype;
14065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mTypeName = typeName;
14165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mSubtypeName = subtypeName;
14265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        setDetailedState(DetailedState.IDLE, null, null);
14365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mState = State.UNKNOWN;
14465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mIsAvailable = false; // until we're told otherwise, assume unavailable
14565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mIsRoaming = false;
14665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
14765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
14865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    /** {@hide} */
14965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    public NetworkInfo(NetworkInfo source) {
15065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (source != null) {
15165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mNetworkType = source.mNetworkType;
15265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mSubtype = source.mSubtype;
15365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mTypeName = source.mTypeName;
15465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mSubtypeName = source.mSubtypeName;
15565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mState = source.mState;
15665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mDetailedState = source.mDetailedState;
15765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mReason = source.mReason;
15865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mExtraInfo = source.mExtraInfo;
15965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mIsFailover = source.mIsFailover;
16065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mIsRoaming = source.mIsRoaming;
16165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mIsAvailable = source.mIsAvailable;
16265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
16365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
16465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
16565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    /**
16665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * Reports the type of network to which the
16765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * info in this {@code NetworkInfo} pertains.
16865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * @return one of {@link ConnectivityManager#TYPE_MOBILE}, {@link
16965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * ConnectivityManager#TYPE_WIFI}, {@link ConnectivityManager#TYPE_WIMAX}, {@link
17065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * ConnectivityManager#TYPE_ETHERNET},  {@link ConnectivityManager#TYPE_BLUETOOTH}, or other
17165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * types defined by {@link ConnectivityManager}
17265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     */
17365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    public int getType() {
1743d2f877c1cb4e4ae4ddde7f57f4353de9341f11bGlenn Kasten        synchronized (this) {
1753d2f877c1cb4e4ae4ddde7f57f4353de9341f11bGlenn Kasten            return mNetworkType;
17665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
17772ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten    }
17872ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten
17972ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten    /**
18072ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten     * Return a network-type-specific integer describing the subtype
18165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * of the network.
18265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * @return the network subtype
18365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     */
18465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    public int getSubtype() {
18565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        synchronized (this) {
18665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return mSubtype;
18765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
18865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
18965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
19065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    void setSubtype(int subtype, String subtypeName) {
19165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        synchronized (this) {
19265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mSubtype = subtype;
19365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mSubtypeName = subtypeName;
19465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
19565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
19665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
19765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    /**
19865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * Return a human-readable name describe the type of the network,
19965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * for example "WIFI" or "MOBILE".
20065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * @return the name of the network type
20165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     */
20265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    public String getTypeName() {
20365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        synchronized (this) {
2043d2f877c1cb4e4ae4ddde7f57f4353de9341f11bGlenn Kasten            return mTypeName;
205fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten        }
20665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
20765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
20865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    /**
20965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * Return a human-readable name describing the subtype of the network.
210fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten     * @return the name of the network subtype
21165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     */
21272ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten    public String getSubtypeName() {
21365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        synchronized (this) {
21465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return mSubtypeName;
21565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
21665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
21772ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten
21865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    /**
21965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * Indicates whether network connectivity exists or is in the process
22065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * of being established. This is good for applications that need to
22165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * do anything related to the network other than read or write data.
22265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * For the latter, call {@link #isConnected()} instead, which guarantees
22365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * that the network is fully usable.
22465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * @return {@code true} if network connectivity exists or is in the process
22565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * of being established, {@code false} otherwise.
22665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     */
22765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    public boolean isConnectedOrConnecting() {
22865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        synchronized (this) {
22965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return mState == State.CONNECTED || mState == State.CONNECTING;
23065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
23165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
23265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
23365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    /**
2347c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     * Indicates whether network connectivity exists and it is possible to establish
2357c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     * connections and pass data.
2369fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten     * <p>Always call this before attempting to perform data transactions.
2379fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten     * @return {@code true} if network connectivity exists, {@code false} otherwise.
2389fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten     */
2399fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten    public boolean isConnected() {
2409fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten        synchronized (this) {
2419fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten            return mState == State.CONNECTED;
2429fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten        }
2439fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten    }
2449fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten
2459fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten    /**
2469fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten     * Indicates whether network connectivity is possible. A network is unavailable
2479fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten     * when a persistent or semi-persistent condition prevents the possibility
2489fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten     * of connecting to that network. Examples include
2499fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten     * <ul>
2509fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten     * <li>The device is out of the coverage area for any network of this type.</li>
2519fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten     * <li>The device is on a network other than the home network (i.e., roaming), and
2529fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten     * data roaming has been disabled.</li>
2539fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten     * <li>The device's radio is turned off, e.g., because airplane mode is enabled.</li>
2549fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten     * </ul>
2559fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten     * @return {@code true} if the network is available, {@code false} otherwise
2569fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten     */
2579fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten    public boolean isAvailable() {
2589fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten        synchronized (this) {
2599fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten            return mIsAvailable;
2609fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten        }
2619fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten    }
2629fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten
2637c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    /**
2647c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     * Sets if the network is available, ie, if the connectivity is possible.
2657c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     * @param isAvailable the new availability value.
2667c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     *
2677c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     * @hide
2687c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     */
2697c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    public void setIsAvailable(boolean isAvailable) {
2707c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        synchronized (this) {
2719fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten            mIsAvailable = isAvailable;
2729fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten        }
2739fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten    }
2749fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten
2759fda4b87441fe17d90d8144639c9de6d9022c3c0Glenn Kasten    /**
2767c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     * Indicates whether the current attempt to connect to the network
2777c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     * resulted from the ConnectivityManager trying to fail over to this
2787c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     * network following a disconnect from another network.
2797c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     * @return {@code true} if this is a failover attempt, {@code false}
2807c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     * otherwise.
2817c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     */
28281872a2ce65653ae980621ba2907da56a83ba9a7Glenn Kasten    public boolean isFailover() {
28381872a2ce65653ae980621ba2907da56a83ba9a7Glenn Kasten        synchronized (this) {
28481872a2ce65653ae980621ba2907da56a83ba9a7Glenn Kasten            return mIsFailover;
2857c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        }
2867c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
2877c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
2888dad0e31c2366c501bd1d600261d0af35a6ca786Glenn Kasten    /**
2897c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     * Set the failover boolean.
29081872a2ce65653ae980621ba2907da56a83ba9a7Glenn Kasten     * @param isFailover {@code true} to mark the current connection attempt
2917c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     * as a failover.
2927c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     * @hide
2937c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     */
2947c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    public void setFailover(boolean isFailover) {
2957c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        synchronized (this) {
2967c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            mIsFailover = isFailover;
2977c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        }
2987c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
2997c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
3007c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    /**
3017c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     * Indicates whether the device is currently roaming on this network.
3027c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     * When {@code true}, it suggests that use of data on this network
3037c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     * may incur extra costs.
3047c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     * @return {@code true} if roaming is in effect, {@code false} otherwise.
3057c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     */
3067c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    public boolean isRoaming() {
3077c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        synchronized (this) {
30865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return mIsRoaming;
30965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
31065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
31165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
312eda6c364c253ba97ee45a3adeb8c2b45db1f81dbEric Laurent    void setRoaming(boolean isRoaming) {
313eda6c364c253ba97ee45a3adeb8c2b45db1f81dbEric Laurent        synchronized (this) {
314e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten            mIsRoaming = isRoaming;
315e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten        }
316fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    }
317fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
3187c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    /**
3197c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent     * Reports the current coarse-grained state of the network.
32065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     * @return the coarse-grained state
32165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian     */
32265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    public State getState() {
32365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        synchronized (this) {
32465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            return mState;
325        }
326    }
327
328    /**
329     * Reports the current fine-grained state of the network.
330     * @return the fine-grained state
331     */
332    public DetailedState getDetailedState() {
333        synchronized (this) {
334            return mDetailedState;
335        }
336    }
337
338    /**
339     * Sets the fine-grained state of the network.
340     * @param detailedState the {@link DetailedState}.
341     * @param reason a {@code String} indicating the reason for the state change,
342     * if one was supplied. May be {@code null}.
343     * @param extraInfo an optional {@code String} providing addditional network state
344     * information passed up from the lower networking layers.
345     * @hide
346     */
347    public void setDetailedState(DetailedState detailedState, String reason, String extraInfo) {
348        synchronized (this) {
349            this.mDetailedState = detailedState;
350            this.mState = stateMap.get(detailedState);
351            this.mReason = reason;
352            this.mExtraInfo = extraInfo;
353        }
354    }
355
356    /**
357     * Set the extraInfo field.
358     * @param extraInfo an optional {@code String} providing addditional network state
359     * information passed up from the lower networking layers.
360     * @hide
361     */
362    public void setExtraInfo(String extraInfo) {
363        synchronized (this) {
364            this.mExtraInfo = extraInfo;
365        }
366    }
367
368    /**
369     * Report the reason an attempt to establish connectivity failed,
370     * if one is available.
371     * @return the reason for failure, or null if not available
372     */
373    public String getReason() {
374        synchronized (this) {
375            return mReason;
376        }
377    }
378
379    /**
380     * Report the extra information about the network state, if any was
381     * provided by the lower networking layers.,
382     * if one is available.
383     * @return the extra information, or null if not available
384     */
385    public String getExtraInfo() {
386        synchronized (this) {
387            return mExtraInfo;
388        }
389    }
390
391    @Override
392    public String toString() {
393        synchronized (this) {
394            StringBuilder builder = new StringBuilder("NetworkInfo: ");
395            builder.append("type: ").append(getTypeName()).append("[").append(getSubtypeName()).
396            append("], state: ").append(mState).append("/").append(mDetailedState).
397            append(", reason: ").append(mReason == null ? "(unspecified)" : mReason).
398            append(", extra: ").append(mExtraInfo == null ? "(none)" : mExtraInfo).
399            append(", roaming: ").append(mIsRoaming).
400            append(", failover: ").append(mIsFailover).
401            append(", isAvailable: ").append(mIsAvailable);
402            return builder.toString();
403        }
404    }
405
406    /**
407     * Implement the Parcelable interface
408     * @hide
409     */
410    public int describeContents() {
411        return 0;
412    }
413
414    /**
415     * Implement the Parcelable interface.
416     * @hide
417     */
418    public void writeToParcel(Parcel dest, int flags) {
419        synchronized (this) {
420            dest.writeInt(mNetworkType);
421            dest.writeInt(mSubtype);
422            dest.writeString(mTypeName);
423            dest.writeString(mSubtypeName);
424            dest.writeString(mState.name());
425            dest.writeString(mDetailedState.name());
426            dest.writeInt(mIsFailover ? 1 : 0);
427            dest.writeInt(mIsAvailable ? 1 : 0);
428            dest.writeInt(mIsRoaming ? 1 : 0);
429            dest.writeString(mReason);
430            dest.writeString(mExtraInfo);
431        }
432    }
433
434    /**
435     * Implement the Parcelable interface.
436     * @hide
437     */
438    public static final Creator<NetworkInfo> CREATOR =
439        new Creator<NetworkInfo>() {
440            public NetworkInfo createFromParcel(Parcel in) {
441                int netType = in.readInt();
442                int subtype = in.readInt();
443                String typeName = in.readString();
444                String subtypeName = in.readString();
445                NetworkInfo netInfo = new NetworkInfo(netType, subtype, typeName, subtypeName);
446                netInfo.mState = State.valueOf(in.readString());
447                netInfo.mDetailedState = DetailedState.valueOf(in.readString());
448                netInfo.mIsFailover = in.readInt() != 0;
449                netInfo.mIsAvailable = in.readInt() != 0;
450                netInfo.mIsRoaming = in.readInt() != 0;
451                netInfo.mReason = in.readString();
452                netInfo.mExtraInfo = in.readString();
453                return netInfo;
454            }
455
456            public NetworkInfo[] newArray(int size) {
457                return new NetworkInfo[size];
458            }
459        };
460}
461