blob: 804f8eecbbdc9f4de8a348aac10ba5376cd919cf [file] [log] [blame]
Jeff Sharkey899223b2012-08-04 15:24:58 -07001/*
2 * Copyright (C) 2012 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;
Robert Greenwalt665e1ae2012-08-21 19:27:00 -070021import android.os.Messenger;
Jeff Sharkey899223b2012-08-04 15:24:58 -070022
Jeff Sharkey899223b2012-08-04 15:24:58 -070023import java.util.concurrent.atomic.AtomicBoolean;
24
Narayan Kamath98e17972013-08-29 13:40:43 +010025import com.android.internal.util.Preconditions;
26
Jeff Sharkey899223b2012-08-04 15:24:58 -070027/**
28 * Interface to control and observe state of a specific network, hiding
29 * network-specific details from {@link ConnectivityManager}. Surfaces events
30 * through the registered {@link Handler} to enable {@link ConnectivityManager}
31 * to respond to state changes over time.
32 *
33 * @hide
34 */
35public abstract class BaseNetworkStateTracker implements NetworkStateTracker {
36 // TODO: better document threading expectations
37 // TODO: migrate to make NetworkStateTracker abstract class
38
39 public static final String PROP_TCP_BUFFER_UNKNOWN = "net.tcp.buffersize.unknown";
40 public static final String PROP_TCP_BUFFER_WIFI = "net.tcp.buffersize.wifi";
41
42 protected Context mContext;
43 private Handler mTarget;
44
45 protected NetworkInfo mNetworkInfo;
46 protected LinkProperties mLinkProperties;
47 protected LinkCapabilities mLinkCapabilities;
48
49 private AtomicBoolean mTeardownRequested = new AtomicBoolean(false);
50 private AtomicBoolean mPrivateDnsRouteSet = new AtomicBoolean(false);
51 private AtomicBoolean mDefaultRouteSet = new AtomicBoolean(false);
52
53 public BaseNetworkStateTracker(int networkType) {
54 mNetworkInfo = new NetworkInfo(
55 networkType, -1, ConnectivityManager.getNetworkTypeName(networkType), null);
56 mLinkProperties = new LinkProperties();
57 mLinkCapabilities = new LinkCapabilities();
58 }
59
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -070060 protected BaseNetworkStateTracker() {
61 // By default, let the sub classes construct everything
62 }
63
Jeff Sharkey899223b2012-08-04 15:24:58 -070064 @Deprecated
65 protected Handler getTargetHandler() {
66 return mTarget;
67 }
68
69 protected final void dispatchStateChanged() {
70 // TODO: include snapshot of other fields when sending
71 mTarget.obtainMessage(EVENT_STATE_CHANGED, getNetworkInfo()).sendToTarget();
72 }
73
74 protected final void dispatchConfigurationChanged() {
75 // TODO: include snapshot of other fields when sending
76 mTarget.obtainMessage(EVENT_CONFIGURATION_CHANGED, getNetworkInfo()).sendToTarget();
77 }
78
79 @Override
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -070080 public void startMonitoring(Context context, Handler target) {
Jeff Sharkey899223b2012-08-04 15:24:58 -070081 mContext = Preconditions.checkNotNull(context);
82 mTarget = Preconditions.checkNotNull(target);
83 startMonitoringInternal();
84 }
85
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -070086 protected void startMonitoringInternal() {
87
88 }
Jeff Sharkey899223b2012-08-04 15:24:58 -070089
90 @Override
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -070091 public NetworkInfo getNetworkInfo() {
Jeff Sharkey899223b2012-08-04 15:24:58 -070092 return new NetworkInfo(mNetworkInfo);
93 }
94
95 @Override
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -070096 public LinkProperties getLinkProperties() {
Jeff Sharkey899223b2012-08-04 15:24:58 -070097 return new LinkProperties(mLinkProperties);
98 }
99
100 @Override
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -0700101 public LinkCapabilities getLinkCapabilities() {
Jeff Sharkey899223b2012-08-04 15:24:58 -0700102 return new LinkCapabilities(mLinkCapabilities);
103 }
104
105 @Override
Vinit Deshapnde6a2d3252013-09-04 14:11:24 -0700106 public LinkQualityInfo getLinkQualityInfo() {
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -0700107 return null;
108 }
109
110 @Override
Wink Savilled747cbc2013-08-07 16:22:47 -0700111 public void captivePortalCheckCompleted(boolean isCaptivePortal) {
112 // not implemented
113 }
114
115 @Override
Jeff Sharkey899223b2012-08-04 15:24:58 -0700116 public boolean setRadio(boolean turnOn) {
117 // Base tracker doesn't handle radios
118 return true;
119 }
120
121 @Override
122 public boolean isAvailable() {
123 return mNetworkInfo.isAvailable();
124 }
125
126 @Override
127 public void setUserDataEnable(boolean enabled) {
128 // Base tracker doesn't handle enabled flags
129 }
130
131 @Override
132 public void setPolicyDataEnable(boolean enabled) {
133 // Base tracker doesn't handle enabled flags
134 }
135
136 @Override
137 public boolean isPrivateDnsRouteSet() {
138 return mPrivateDnsRouteSet.get();
139 }
140
141 @Override
142 public void privateDnsRouteSet(boolean enabled) {
143 mPrivateDnsRouteSet.set(enabled);
144 }
145
146 @Override
147 public boolean isDefaultRouteSet() {
148 return mDefaultRouteSet.get();
149 }
150
151 @Override
152 public void defaultRouteSet(boolean enabled) {
153 mDefaultRouteSet.set(enabled);
154 }
155
156 @Override
157 public boolean isTeardownRequested() {
158 return mTeardownRequested.get();
159 }
160
161 @Override
162 public void setTeardownRequested(boolean isRequested) {
163 mTeardownRequested.set(isRequested);
164 }
165
166 @Override
167 public void setDependencyMet(boolean met) {
168 // Base tracker doesn't handle dependencies
169 }
Lorenzo Colitti69edd642013-03-07 11:01:12 -0800170
171 @Override
172 public void addStackedLink(LinkProperties link) {
173 mLinkProperties.addStackedLink(link);
174 }
175
176 @Override
177 public void removeStackedLink(LinkProperties link) {
178 mLinkProperties.removeStackedLink(link);
179 }
Robert Greenwalt665e1ae2012-08-21 19:27:00 -0700180
181 @Override
182 public void supplyMessenger(Messenger messenger) {
183 // not supported on this network
184 }
Vinit Deshapnde1f12cb52013-08-21 13:09:01 -0700185
186 @Override
187 public String getNetworkInterfaceName() {
188 if (mLinkProperties != null) {
189 return mLinkProperties.getInterfaceName();
190 } else {
191 return null;
192 }
193 }
194
195 @Override
196 public void startSampling(SamplingDataTracker.SamplingSnapshot s) {
197 // nothing to do
198 }
199
200 @Override
201 public void stopSampling(SamplingDataTracker.SamplingSnapshot s) {
202 // nothing to do
203 }
Jeff Sharkey899223b2012-08-04 15:24:58 -0700204}