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     public void captivePortalCheckComplete() {
    124         // not implemented
    125     }
    126 
    127     /**
    128      * Record the detailed state of a network, and if it is a
    129      * change from the previous state, send a notification to
    130      * any listeners.
    131      * @param state the new {@code DetailedState}
    132      * @param reason a {@code String} indicating a reason for the state change,
    133      * if one was supplied. May be {@code null}.
    134      * @param extraInfo optional {@code String} providing extra information about the state change
    135      */
    136     private void setDetailedState(NetworkInfo.DetailedState state, String reason,
    137             String extraInfo) {
    138         if (DBG) log("setDetailed state, old ="
    139                 + mNetworkInfo.getDetailedState() + " and new state=" + state);
    140         mNetworkInfo.setDetailedState(state, reason, extraInfo);
    141         Message msg = mTarget.obtainMessage(EVENT_STATE_CHANGED, mNetworkInfo);
    142         msg.sendToTarget();
    143     }
    144 
    145     public void setTeardownRequested(boolean isRequested) {
    146         mTeardownRequested = isRequested;
    147     }
    148 
    149     public boolean isTeardownRequested() {
    150         return mTeardownRequested;
    151     }
    152 
    153     /**
    154      * Re-enable mobile data connectivity after a {@link #teardown()}.
    155      * TODO - make async and always get a notification?
    156      */
    157     public boolean reconnect() {
    158         setDetailedState(NetworkInfo.DetailedState.CONNECTING, "enabled", null);
    159         setDetailedState(NetworkInfo.DetailedState.CONNECTED, "enabled", null);
    160         setTeardownRequested(false);
    161         return true;
    162     }
    163 
    164     /**
    165      * Turn on or off the mobile radio. No connectivity will be possible while the
    166      * radio is off. The operation is a no-op if the radio is already in the desired state.
    167      * @param turnOn {@code true} if the radio should be turned on, {@code false} if
    168      */
    169     public boolean setRadio(boolean turnOn) {
    170         return true;
    171     }
    172 
    173     @Override
    174     public void setUserDataEnable(boolean enabled) {
    175         // ignored
    176     }
    177 
    178     @Override
    179     public void setPolicyDataEnable(boolean enabled) {
    180         // ignored
    181     }
    182 
    183     @Override
    184     public String toString() {
    185         StringBuffer sb = new StringBuffer("Dummy data state: none, dummy!");
    186         return sb.toString();
    187     }
    188 
    189     /**
    190      * @see android.net.NetworkStateTracker#getLinkProperties()
    191      */
    192     public LinkProperties getLinkProperties() {
    193         return new LinkProperties(mLinkProperties);
    194     }
    195 
    196     /**
    197      * @see android.net.NetworkStateTracker#getLinkCapabilities()
    198      */
    199     public LinkCapabilities getLinkCapabilities() {
    200         return new LinkCapabilities(mLinkCapabilities);
    201     }
    202 
    203     public void setDependencyMet(boolean met) {
    204         // not supported on this network
    205     }
    206 
    207     @Override
    208     public void addStackedLink(LinkProperties link) {
    209         mLinkProperties.addStackedLink(link);
    210     }
    211 
    212     @Override
    213     public void removeStackedLink(LinkProperties link) {
    214         mLinkProperties.removeStackedLink(link);
    215     }
    216 
    217     @Override
    218     public void supplyMessenger(Messenger messenger) {
    219         // not supported on this network
    220     }
    221 
    222     static private void log(String s) {
    223         Slog.d(TAG, s);
    224     }
    225 
    226     static private void loge(String s) {
    227         Slog.e(TAG, s);
    228     }
    229 }
    230