DummyDataStateTracker.java revision d55a6b498d66d8fc415908ecf63e50f46cce67e8
1/*
2 * Copyright (C) 2010 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
17package android.net;
18
19import android.content.Context;
20import android.os.Handler;
21import android.os.Message;
22import android.net.NetworkInfo.DetailedState;
23import android.net.NetworkInfo;
24import android.net.LinkProperties;
25import android.util.Slog;
26
27/**
28 * A dummy data state tracker for use when we don't have a real radio
29 * connection.  useful when bringing up a board or when you have network
30 * access through other means.
31 *
32 * {@hide}
33 */
34public class DummyDataStateTracker implements NetworkStateTracker {
35
36    private static final String TAG = "DummyDataStateTracker";
37    private static final boolean DBG = true;
38    private static final boolean VDBG = false;
39
40    private NetworkInfo mNetworkInfo;
41    private boolean mTeardownRequested = false;
42    private Handler mTarget;
43    private Context mContext;
44    private LinkProperties mLinkProperties;
45    private LinkCapabilities mLinkCapabilities;
46    private boolean mPrivateDnsRouteSet = false;
47    private boolean mDefaultRouteSet = false;
48
49    // DEFAULT and HIPRI are the same connection.  If we're one of these we need to check if
50    // the other is also disconnected before we reset sockets
51    private boolean mIsDefaultOrHipri = false;
52
53    /**
54     * Create a new DummyDataStateTracker
55     * @param netType the ConnectivityManager network type
56     * @param tag the name of this network
57     */
58    public DummyDataStateTracker(int netType, String tag) {
59        mNetworkInfo = new NetworkInfo(netType);
60    }
61
62    /**
63     * Begin monitoring data connectivity.
64     *
65     * @param context is the current Android context
66     * @param target is the Handler to which to return the events.
67     */
68    public void startMonitoring(Context context, Handler target) {
69        mTarget = target;
70        mContext = context;
71    }
72
73    public boolean isPrivateDnsRouteSet() {
74        return mPrivateDnsRouteSet;
75    }
76
77    public void privateDnsRouteSet(boolean enabled) {
78        mPrivateDnsRouteSet = enabled;
79    }
80
81    public NetworkInfo getNetworkInfo() {
82        return mNetworkInfo;
83    }
84
85    public boolean isDefaultRouteSet() {
86        return mDefaultRouteSet;
87    }
88
89    public void defaultRouteSet(boolean enabled) {
90        mDefaultRouteSet = enabled;
91    }
92
93    /**
94     * This is not implemented.
95     */
96    public void releaseWakeLock() {
97    }
98
99    /**
100     * Report whether data connectivity is possible.
101     */
102    public boolean isAvailable() {
103        return true;
104    }
105
106    /**
107     * Return the system properties name associated with the tcp buffer sizes
108     * for this network.
109     */
110    public String getTcpBufferSizesPropName() {
111        return "net.tcp.buffersize.unknown";
112    }
113
114    /**
115     * Tear down mobile data connectivity, i.e., disable the ability to create
116     * mobile data connections.
117     * TODO - make async and return nothing?
118     */
119    public boolean teardown() {
120        setDetailedState(NetworkInfo.DetailedState.DISCONNECTING, "disabled", null);
121        setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, "disabled", null);
122        return true;
123    }
124
125    /**
126     * Record the detailed state of a network, and if it is a
127     * change from the previous state, send a notification to
128     * any listeners.
129     * @param state the new @{code DetailedState}
130     * @param reason a {@code String} indicating a reason for the state change,
131     * if one was supplied. May be {@code null}.
132     * @param extraInfo optional {@code String} providing extra information about the state change
133     */
134    private void setDetailedState(NetworkInfo.DetailedState state, String reason,
135            String extraInfo) {
136        if (DBG) log("setDetailed state, old ="
137                + mNetworkInfo.getDetailedState() + " and new state=" + state);
138        mNetworkInfo.setDetailedState(state, reason, extraInfo);
139        Message msg = mTarget.obtainMessage(EVENT_STATE_CHANGED, mNetworkInfo);
140        msg.sendToTarget();
141    }
142
143    public void setTeardownRequested(boolean isRequested) {
144        mTeardownRequested = isRequested;
145    }
146
147    public boolean isTeardownRequested() {
148        return mTeardownRequested;
149    }
150
151    /**
152     * Re-enable mobile data connectivity after a {@link #teardown()}.
153     * TODO - make async and always get a notification?
154     */
155    public boolean reconnect() {
156        setDetailedState(NetworkInfo.DetailedState.CONNECTING, "enabled", null);
157        setDetailedState(NetworkInfo.DetailedState.CONNECTED, "enabled", null);
158        setTeardownRequested(false);
159        return true;
160    }
161
162    /**
163     * Turn on or off the mobile radio. No connectivity will be possible while the
164     * radio is off. The operation is a no-op if the radio is already in the desired state.
165     * @param turnOn {@code true} if the radio should be turned on, {@code false} if
166     */
167    public boolean setRadio(boolean turnOn) {
168        return true;
169    }
170
171    public void setDataEnable(boolean enabled) {
172    }
173
174    @Override
175    public String toString() {
176        StringBuffer sb = new StringBuffer("Dummy data state: none, dummy!");
177        return sb.toString();
178    }
179
180    /**
181     * @see android.net.NetworkStateTracker#getLinkProperties()
182     */
183    public LinkProperties getLinkProperties() {
184        return new LinkProperties(mLinkProperties);
185    }
186
187    /**
188     * @see android.net.NetworkStateTracker#getLinkCapabilities()
189     */
190    public LinkCapabilities getLinkCapabilities() {
191        return new LinkCapabilities(mLinkCapabilities);
192    }
193
194    public void setDependencyMet(boolean met) {
195        // not supported on this network
196    }
197
198    static private void log(String s) {
199        Slog.d(TAG, s);
200    }
201
202    static private void loge(String s) {
203        Slog.e(TAG, s);
204    }
205}
206