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