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     @Deprecated
     61     protected Handler getTargetHandler() {
     62         return mTarget;
     63     }
     64 
     65     protected final void dispatchStateChanged() {
     66         // TODO: include snapshot of other fields when sending
     67         mTarget.obtainMessage(EVENT_STATE_CHANGED, getNetworkInfo()).sendToTarget();
     68     }
     69 
     70     protected final void dispatchConfigurationChanged() {
     71         // TODO: include snapshot of other fields when sending
     72         mTarget.obtainMessage(EVENT_CONFIGURATION_CHANGED, getNetworkInfo()).sendToTarget();
     73     }
     74 
     75     @Override
     76     public final void startMonitoring(Context context, Handler target) {
     77         mContext = Preconditions.checkNotNull(context);
     78         mTarget = Preconditions.checkNotNull(target);
     79         startMonitoringInternal();
     80     }
     81 
     82     protected abstract void startMonitoringInternal();
     83 
     84     @Override
     85     public final NetworkInfo getNetworkInfo() {
     86         return new NetworkInfo(mNetworkInfo);
     87     }
     88 
     89     @Override
     90     public final LinkProperties getLinkProperties() {
     91         return new LinkProperties(mLinkProperties);
     92     }
     93 
     94     @Override
     95     public final LinkCapabilities getLinkCapabilities() {
     96         return new LinkCapabilities(mLinkCapabilities);
     97     }
     98 
     99     @Override
    100     public void captivePortalCheckComplete() {
    101         // not implemented
    102     }
    103 
    104     @Override
    105     public boolean setRadio(boolean turnOn) {
    106         // Base tracker doesn't handle radios
    107         return true;
    108     }
    109 
    110     @Override
    111     public boolean isAvailable() {
    112         return mNetworkInfo.isAvailable();
    113     }
    114 
    115     @Override
    116     public void setUserDataEnable(boolean enabled) {
    117         // Base tracker doesn't handle enabled flags
    118     }
    119 
    120     @Override
    121     public void setPolicyDataEnable(boolean enabled) {
    122         // Base tracker doesn't handle enabled flags
    123     }
    124 
    125     @Override
    126     public boolean isPrivateDnsRouteSet() {
    127         return mPrivateDnsRouteSet.get();
    128     }
    129 
    130     @Override
    131     public void privateDnsRouteSet(boolean enabled) {
    132         mPrivateDnsRouteSet.set(enabled);
    133     }
    134 
    135     @Override
    136     public boolean isDefaultRouteSet() {
    137         return mDefaultRouteSet.get();
    138     }
    139 
    140     @Override
    141     public void defaultRouteSet(boolean enabled) {
    142         mDefaultRouteSet.set(enabled);
    143     }
    144 
    145     @Override
    146     public boolean isTeardownRequested() {
    147         return mTeardownRequested.get();
    148     }
    149 
    150     @Override
    151     public void setTeardownRequested(boolean isRequested) {
    152         mTeardownRequested.set(isRequested);
    153     }
    154 
    155     @Override
    156     public void setDependencyMet(boolean met) {
    157         // Base tracker doesn't handle dependencies
    158     }
    159 
    160     @Override
    161     public void addStackedLink(LinkProperties link) {
    162         mLinkProperties.addStackedLink(link);
    163     }
    164 
    165     @Override
    166     public void removeStackedLink(LinkProperties link) {
    167         mLinkProperties.removeStackedLink(link);
    168     }
    169 
    170     @Override
    171     public void supplyMessenger(Messenger messenger) {
    172         // not supported on this network
    173     }
    174 }
    175