blob: a5d059e54790b04e794a1604f7ab73d7d0eacaaf [file] [log] [blame]
Robert Greenwaltda3d5e62010-12-06 13:56:24 -08001/*
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;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -070022import android.os.Messenger;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -080023import 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 */
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -070032public class DummyDataStateTracker extends BaseNetworkStateTracker {
Robert Greenwaltda3d5e62010-12-06 13:56:24 -080033
34 private static final String TAG = "DummyDataStateTracker";
35 private static final boolean DBG = true;
36 private static final boolean VDBG = false;
37
Robert Greenwaltda3d5e62010-12-06 13:56:24 -080038 private boolean mTeardownRequested = false;
39 private Handler mTarget;
Robert Greenwaltda3d5e62010-12-06 13:56:24 -080040 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
Robert Greenwaltda3d5e62010-12-06 13:56:24 -080067 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
Robert Greenwaltda3d5e62010-12-06 13:56:24 -080079 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
Wink Savilled747cbc2013-08-07 16:22:47 -0700119 @Override
Wink Savilled747cbc2013-08-07 16:22:47 -0700120 public void captivePortalCheckCompleted(boolean isCaptivePortal) {
121 // not implemented
122 }
123
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800124 /**
125 * Record the detailed state of a network, and if it is a
126 * change from the previous state, send a notification to
127 * any listeners.
Jeff Smitha45746e2012-07-19 14:19:24 -0500128 * @param state the new {@code DetailedState}
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800129 * @param reason a {@code String} indicating a reason for the state change,
130 * if one was supplied. May be {@code null}.
131 * @param extraInfo optional {@code String} providing extra information about the state change
132 */
133 private void setDetailedState(NetworkInfo.DetailedState state, String reason,
134 String extraInfo) {
135 if (DBG) log("setDetailed state, old ="
136 + mNetworkInfo.getDetailedState() + " and new state=" + state);
137 mNetworkInfo.setDetailedState(state, reason, extraInfo);
138 Message msg = mTarget.obtainMessage(EVENT_STATE_CHANGED, mNetworkInfo);
139 msg.sendToTarget();
140 }
141
142 public void setTeardownRequested(boolean isRequested) {
143 mTeardownRequested = isRequested;
144 }
145
146 public boolean isTeardownRequested() {
147 return mTeardownRequested;
148 }
149
150 /**
151 * Re-enable mobile data connectivity after a {@link #teardown()}.
152 * TODO - make async and always get a notification?
153 */
154 public boolean reconnect() {
155 setDetailedState(NetworkInfo.DetailedState.CONNECTING, "enabled", null);
156 setDetailedState(NetworkInfo.DetailedState.CONNECTED, "enabled", null);
157 setTeardownRequested(false);
158 return true;
159 }
160
161 /**
162 * Turn on or off the mobile radio. No connectivity will be possible while the
163 * radio is off. The operation is a no-op if the radio is already in the desired state.
164 * @param turnOn {@code true} if the radio should be turned on, {@code false} if
165 */
166 public boolean setRadio(boolean turnOn) {
167 return true;
168 }
169
Jeff Sharkey8e28b7d2011-08-19 02:24:24 -0700170 @Override
171 public void setUserDataEnable(boolean enabled) {
172 // ignored
173 }
174
175 @Override
176 public void setPolicyDataEnable(boolean enabled) {
177 // ignored
Wink Savillee7982682010-12-07 10:31:02 -0800178 }
179
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800180 @Override
181 public String toString() {
182 StringBuffer sb = new StringBuffer("Dummy data state: none, dummy!");
183 return sb.toString();
184 }
185
186 /**
187 * @see android.net.NetworkStateTracker#getLinkProperties()
188 */
189 public LinkProperties getLinkProperties() {
190 return new LinkProperties(mLinkProperties);
191 }
192
193 /**
194 * @see android.net.NetworkStateTracker#getLinkCapabilities()
195 */
196 public LinkCapabilities getLinkCapabilities() {
197 return new LinkCapabilities(mLinkCapabilities);
198 }
199
Robert Greenwaltd55a6b42011-03-25 13:09:25 -0700200 public void setDependencyMet(boolean met) {
201 // not supported on this network
202 }
203
Lorenzo Colitti69edd642013-03-07 11:01:12 -0800204 @Override
205 public void addStackedLink(LinkProperties link) {
206 mLinkProperties.addStackedLink(link);
207 }
208
209 @Override
210 public void removeStackedLink(LinkProperties link) {
211 mLinkProperties.removeStackedLink(link);
212 }
213
Robert Greenwalt665e1ae2012-08-21 19:27:00 -0700214 @Override
215 public void supplyMessenger(Messenger messenger) {
216 // not supported on this network
217 }
218
Robert Greenwaltda3d5e62010-12-06 13:56:24 -0800219 static private void log(String s) {
220 Slog.d(TAG, s);
221 }
222
223 static private void loge(String s) {
224 Slog.e(TAG, s);
225 }
226}