Home | History | Annotate | Download | only in net
      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;
     21 import android.os.Messenger;
     22 
     23 import com.android.internal.util.Preconditions;
     24 
     25 import java.util.concurrent.atomic.AtomicBoolean;
     26 
     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 
     60     protected BaseNetworkStateTracker() {
     61         // By default, let the sub classes construct everything
     62     }
     63 
     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
     80     public void startMonitoring(Context context, Handler target) {
     81         mContext = Preconditions.checkNotNull(context);
     82         mTarget = Preconditions.checkNotNull(target);
     83         startMonitoringInternal();
     84     }
     85 
     86     protected void startMonitoringInternal() {
     87 
     88     }
     89 
     90     @Override
     91     public NetworkInfo getNetworkInfo() {
     92         return new NetworkInfo(mNetworkInfo);
     93     }
     94 
     95     @Override
     96     public LinkProperties getLinkProperties() {
     97         return new LinkProperties(mLinkProperties);
     98     }
     99 
    100     @Override
    101     public LinkCapabilities getLinkCapabilities() {
    102         return new LinkCapabilities(mLinkCapabilities);
    103     }
    104 
    105     @Override
    106     public LinkQualityInfo getLinkQualityInfo() {
    107         return null;
    108     }
    109 
    110     @Override
    111     public void captivePortalCheckComplete() {
    112         // not implemented
    113     }
    114 
    115     @Override
    116     public void captivePortalCheckCompleted(boolean isCaptivePortal) {
    117         // not implemented
    118     }
    119 
    120     @Override
    121     public boolean setRadio(boolean turnOn) {
    122         // Base tracker doesn't handle radios
    123         return true;
    124     }
    125 
    126     @Override
    127     public boolean isAvailable() {
    128         return mNetworkInfo.isAvailable();
    129     }
    130 
    131     @Override
    132     public void setUserDataEnable(boolean enabled) {
    133         // Base tracker doesn't handle enabled flags
    134     }
    135 
    136     @Override
    137     public void setPolicyDataEnable(boolean enabled) {
    138         // Base tracker doesn't handle enabled flags
    139     }
    140 
    141     @Override
    142     public boolean isPrivateDnsRouteSet() {
    143         return mPrivateDnsRouteSet.get();
    144     }
    145 
    146     @Override
    147     public void privateDnsRouteSet(boolean enabled) {
    148         mPrivateDnsRouteSet.set(enabled);
    149     }
    150 
    151     @Override
    152     public boolean isDefaultRouteSet() {
    153         return mDefaultRouteSet.get();
    154     }
    155 
    156     @Override
    157     public void defaultRouteSet(boolean enabled) {
    158         mDefaultRouteSet.set(enabled);
    159     }
    160 
    161     @Override
    162     public boolean isTeardownRequested() {
    163         return mTeardownRequested.get();
    164     }
    165 
    166     @Override
    167     public void setTeardownRequested(boolean isRequested) {
    168         mTeardownRequested.set(isRequested);
    169     }
    170 
    171     @Override
    172     public void setDependencyMet(boolean met) {
    173         // Base tracker doesn't handle dependencies
    174     }
    175 
    176     @Override
    177     public void addStackedLink(LinkProperties link) {
    178         mLinkProperties.addStackedLink(link);
    179     }
    180 
    181     @Override
    182     public void removeStackedLink(LinkProperties link) {
    183         mLinkProperties.removeStackedLink(link);
    184     }
    185 
    186     @Override
    187     public void supplyMessenger(Messenger messenger) {
    188         // not supported on this network
    189     }
    190 
    191     @Override
    192     public String getNetworkInterfaceName() {
    193         if (mLinkProperties != null) {
    194             return mLinkProperties.getInterfaceName();
    195         } else {
    196             return null;
    197         }
    198     }
    199 
    200     @Override
    201     public void startSampling(SamplingDataTracker.SamplingSnapshot s) {
    202         // nothing to do
    203     }
    204 
    205     @Override
    206     public void stopSampling(SamplingDataTracker.SamplingSnapshot s) {
    207         // nothing to do
    208     }
    209 }
    210