Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.net; |
| 18 | |
| 19 | import android.content.Context; |
| 20 | import android.os.Handler; |
Robert Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 21 | import android.os.Messenger; |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 22 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 23 | import java.util.concurrent.atomic.AtomicBoolean; |
| 24 | |
Narayan Kamath | 98e1797 | 2013-08-29 13:40:43 +0100 | [diff] [blame] | 25 | import com.android.internal.util.Preconditions; |
| 26 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 27 | /** |
| 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 | */ |
| 35 | public 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 Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 60 | protected BaseNetworkStateTracker() { |
| 61 | // By default, let the sub classes construct everything |
| 62 | } |
| 63 | |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 64 | @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 Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 80 | public void startMonitoring(Context context, Handler target) { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 81 | mContext = Preconditions.checkNotNull(context); |
| 82 | mTarget = Preconditions.checkNotNull(target); |
| 83 | startMonitoringInternal(); |
| 84 | } |
| 85 | |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 86 | protected void startMonitoringInternal() { |
| 87 | |
| 88 | } |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 89 | |
| 90 | @Override |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 91 | public NetworkInfo getNetworkInfo() { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 92 | return new NetworkInfo(mNetworkInfo); |
| 93 | } |
| 94 | |
| 95 | @Override |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 96 | public LinkProperties getLinkProperties() { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 97 | return new LinkProperties(mLinkProperties); |
| 98 | } |
| 99 | |
| 100 | @Override |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 101 | public LinkCapabilities getLinkCapabilities() { |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 102 | return new LinkCapabilities(mLinkCapabilities); |
| 103 | } |
| 104 | |
| 105 | @Override |
Vinit Deshapnde | 6a2d325 | 2013-09-04 14:11:24 -0700 | [diff] [blame] | 106 | public LinkQualityInfo getLinkQualityInfo() { |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 107 | return null; |
| 108 | } |
| 109 | |
| 110 | @Override |
Wink Saville | d747cbc | 2013-08-07 16:22:47 -0700 | [diff] [blame] | 111 | public void captivePortalCheckCompleted(boolean isCaptivePortal) { |
| 112 | // not implemented |
| 113 | } |
| 114 | |
| 115 | @Override |
Jeff Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 116 | 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 Colitti | 69edd64 | 2013-03-07 11:01:12 -0800 | [diff] [blame] | 170 | |
| 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 Greenwalt | 665e1ae | 2012-08-21 19:27:00 -0700 | [diff] [blame] | 180 | |
| 181 | @Override |
| 182 | public void supplyMessenger(Messenger messenger) { |
| 183 | // not supported on this network |
| 184 | } |
Vinit Deshapnde | 1f12cb5 | 2013-08-21 13:09:01 -0700 | [diff] [blame] | 185 | |
| 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 Sharkey | 899223b | 2012-08-04 15:24:58 -0700 | [diff] [blame] | 204 | } |