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