Home | History | Annotate | Download | only in net
      1 /*
      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 
     17 package android.net;
     18 
     19 import android.content.Context;
     20 import android.os.Handler;
     21 import android.os.Message;
     22 import android.util.Slog;
     23 
     24 /**
     25  * A dummy data state tracker for use when we don't have a real radio
     26  * connection.  useful when bringing up a board or when you have network
     27  * access through other means.
     28  *
     29  * {@hide}
     30  */
     31 public class DummyDataStateTracker implements NetworkStateTracker {
     32 
     33     private static final String TAG = "DummyDataStateTracker";
     34     private static final boolean DBG = true;
     35     private static final boolean VDBG = false;
     36 
     37     private NetworkInfo mNetworkInfo;
     38     private boolean mTeardownRequested = false;
     39     private Handler mTarget;
     40     private Context mContext;
     41     private LinkProperties mLinkProperties;
     42     private LinkCapabilities mLinkCapabilities;
     43     private boolean mPrivateDnsRouteSet = false;
     44     private boolean mDefaultRouteSet = false;
     45 
     46     // DEFAULT and HIPRI are the same connection.  If we're one of these we need to check if
     47     // the other is also disconnected before we reset sockets
     48     private boolean mIsDefaultOrHipri = false;
     49 
     50     /**
     51      * Create a new DummyDataStateTracker
     52      * @param netType the ConnectivityManager network type
     53      * @param tag the name of this network
     54      */
     55     public DummyDataStateTracker(int netType, String tag) {
     56         mNetworkInfo = new NetworkInfo(netType);
     57     }
     58 
     59     /**
     60      * Begin monitoring data connectivity.
     61      *
     62      * @param context is the current Android context
     63      * @param target is the Handler to which to return the events.
     64      */
     65     public void startMonitoring(Context context, Handler target) {
     66         mTarget = target;
     67         mContext = context;
     68     }
     69 
     70     public boolean isPrivateDnsRouteSet() {
     71         return mPrivateDnsRouteSet;
     72     }
     73 
     74     public void privateDnsRouteSet(boolean enabled) {
     75         mPrivateDnsRouteSet = enabled;
     76     }
     77 
     78     public NetworkInfo getNetworkInfo() {
     79         return mNetworkInfo;
     80     }
     81 
     82     public boolean isDefaultRouteSet() {
     83         return mDefaultRouteSet;
     84     }
     85 
     86     public void defaultRouteSet(boolean enabled) {
     87         mDefaultRouteSet = enabled;
     88     }
     89 
     90     /**
     91      * This is not implemented.
     92      */
     93     public void releaseWakeLock() {
     94     }
     95 
     96     /**
     97      * Report whether data connectivity is possible.
     98      */
     99     public boolean isAvailable() {
    100         return true;
    101     }
    102 
    103     /**
    104      * Return the system properties name associated with the tcp buffer sizes
    105      * for this network.
    106      */
    107     public String getTcpBufferSizesPropName() {
    108         return "net.tcp.buffersize.unknown";
    109     }
    110 
    111     /**
    112      * Tear down mobile data connectivity, i.e., disable the ability to create
    113      * mobile data connections.
    114      * TODO - make async and return nothing?
    115      */
    116     public boolean teardown() {
    117         setDetailedState(NetworkInfo.DetailedState.DISCONNECTING, "disabled", null);
    118         setDetailedState(NetworkInfo.DetailedState.DISCONNECTED, "disabled", null);
    119         return true;
    120     }
    121 
    122     /**
    123      * Record the detailed state of a network, and if it is a
    124      * change from the previous state, send a notification to
    125      * any listeners.
    126      * @param state the new @{code DetailedState}
    127      * @param reason a {@code String} indicating a reason for the state change,
    128      * if one was supplied. May be {@code null}.
    129      * @param extraInfo optional {@code String} providing extra information about the state change
    130      */
    131     private void setDetailedState(NetworkInfo.DetailedState state, String reason,
    132             String extraInfo) {
    133         if (DBG) log("setDetailed state, old ="
    134                 + mNetworkInfo.getDetailedState() + " and new state=" + state);
    135         mNetworkInfo.setDetailedState(state, reason, extraInfo);
    136         Message msg = mTarget.obtainMessage(EVENT_STATE_CHANGED, mNetworkInfo);
    137         msg.sendToTarget();
    138     }
    139 
    140     public void setTeardownRequested(boolean isRequested) {
    141         mTeardownRequested = isRequested;
    142     }
    143 
    144     public boolean isTeardownRequested() {
    145         return mTeardownRequested;
    146     }
    147 
    148     /**
    149      * Re-enable mobile data connectivity after a {@link #teardown()}.
    150      * TODO - make async and always get a notification?
    151      */
    152     public boolean reconnect() {
    153         setDetailedState(NetworkInfo.DetailedState.CONNECTING, "enabled", null);
    154         setDetailedState(NetworkInfo.DetailedState.CONNECTED, "enabled", null);
    155         setTeardownRequested(false);
    156         return true;
    157     }
    158 
    159     /**
    160      * Turn on or off the mobile radio. No connectivity will be possible while the
    161      * radio is off. The operation is a no-op if the radio is already in the desired state.
    162      * @param turnOn {@code true} if the radio should be turned on, {@code false} if
    163      */
    164     public boolean setRadio(boolean turnOn) {
    165         return true;
    166     }
    167 
    168     @Override
    169     public void setUserDataEnable(boolean enabled) {
    170         // ignored
    171     }
    172 
    173     @Override
    174     public void setPolicyDataEnable(boolean enabled) {
    175         // ignored
    176     }
    177 
    178     @Override
    179     public String toString() {
    180         StringBuffer sb = new StringBuffer("Dummy data state: none, dummy!");
    181         return sb.toString();
    182     }
    183 
    184     /**
    185      * @see android.net.NetworkStateTracker#getLinkProperties()
    186      */
    187     public LinkProperties getLinkProperties() {
    188         return new LinkProperties(mLinkProperties);
    189     }
    190 
    191     /**
    192      * @see android.net.NetworkStateTracker#getLinkCapabilities()
    193      */
    194     public LinkCapabilities getLinkCapabilities() {
    195         return new LinkCapabilities(mLinkCapabilities);
    196     }
    197 
    198     public void setDependencyMet(boolean met) {
    199         // not supported on this network
    200     }
    201 
    202     static private void log(String s) {
    203         Slog.d(TAG, s);
    204     }
    205 
    206     static private void loge(String s) {
    207         Slog.e(TAG, s);
    208     }
    209 }
    210