EthernetDataTracker.java revision 8e28b7d78232f6cf08739ca0d129cc7f9e650801
11997069436f0d47583a0b700c12ed63132cf3312Benoit Goby/*
21997069436f0d47583a0b700c12ed63132cf3312Benoit Goby * Copyright (C) 2010 The Android Open Source Project
31997069436f0d47583a0b700c12ed63132cf3312Benoit Goby *
41997069436f0d47583a0b700c12ed63132cf3312Benoit Goby * Licensed under the Apache License, Version 2.0 (the "License");
51997069436f0d47583a0b700c12ed63132cf3312Benoit Goby * you may not use this file except in compliance with the License.
61997069436f0d47583a0b700c12ed63132cf3312Benoit Goby * You may obtain a copy of the License at
71997069436f0d47583a0b700c12ed63132cf3312Benoit Goby *
81997069436f0d47583a0b700c12ed63132cf3312Benoit Goby *      http://www.apache.org/licenses/LICENSE-2.0
91997069436f0d47583a0b700c12ed63132cf3312Benoit Goby *
101997069436f0d47583a0b700c12ed63132cf3312Benoit Goby * Unless required by applicable law or agreed to in writing, software
111997069436f0d47583a0b700c12ed63132cf3312Benoit Goby * distributed under the License is distributed on an "AS IS" BASIS,
121997069436f0d47583a0b700c12ed63132cf3312Benoit Goby * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
131997069436f0d47583a0b700c12ed63132cf3312Benoit Goby * See the License for the specific language governing permissions and
141997069436f0d47583a0b700c12ed63132cf3312Benoit Goby * limitations under the License.
151997069436f0d47583a0b700c12ed63132cf3312Benoit Goby */
161997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
171997069436f0d47583a0b700c12ed63132cf3312Benoit Gobypackage android.net;
181997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
191997069436f0d47583a0b700c12ed63132cf3312Benoit Gobyimport android.content.Context;
201997069436f0d47583a0b700c12ed63132cf3312Benoit Gobyimport android.net.NetworkInfo.DetailedState;
211997069436f0d47583a0b700c12ed63132cf3312Benoit Gobyimport android.os.Handler;
221997069436f0d47583a0b700c12ed63132cf3312Benoit Gobyimport android.os.IBinder;
231997069436f0d47583a0b700c12ed63132cf3312Benoit Gobyimport android.os.INetworkManagementService;
241997069436f0d47583a0b700c12ed63132cf3312Benoit Gobyimport android.os.Message;
251997069436f0d47583a0b700c12ed63132cf3312Benoit Gobyimport android.os.RemoteException;
261997069436f0d47583a0b700c12ed63132cf3312Benoit Gobyimport android.os.ServiceManager;
271997069436f0d47583a0b700c12ed63132cf3312Benoit Gobyimport android.util.Log;
281997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
291997069436f0d47583a0b700c12ed63132cf3312Benoit Gobyimport java.util.concurrent.atomic.AtomicBoolean;
301997069436f0d47583a0b700c12ed63132cf3312Benoit Gobyimport java.util.concurrent.atomic.AtomicInteger;
311997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
321997069436f0d47583a0b700c12ed63132cf3312Benoit Goby/**
331997069436f0d47583a0b700c12ed63132cf3312Benoit Goby * This class tracks the data connection associated with Ethernet
341997069436f0d47583a0b700c12ed63132cf3312Benoit Goby * This is a singleton class and an instance will be created by
351997069436f0d47583a0b700c12ed63132cf3312Benoit Goby * ConnectivityService.
361997069436f0d47583a0b700c12ed63132cf3312Benoit Goby * @hide
371997069436f0d47583a0b700c12ed63132cf3312Benoit Goby */
381997069436f0d47583a0b700c12ed63132cf3312Benoit Gobypublic class EthernetDataTracker implements NetworkStateTracker {
391997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    private static final String NETWORKTYPE = "ETHERNET";
401997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    private static final String TAG = "Ethernet";
411997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
421997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    private AtomicBoolean mTeardownRequested = new AtomicBoolean(false);
431997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    private AtomicBoolean mPrivateDnsRouteSet = new AtomicBoolean(false);
441997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    private AtomicInteger mDefaultGatewayAddr = new AtomicInteger(0);
451997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    private AtomicBoolean mDefaultRouteSet = new AtomicBoolean(false);
461997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
47f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen    private static boolean mLinkUp;
481997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    private LinkProperties mLinkProperties;
491997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    private LinkCapabilities mLinkCapabilities;
501997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    private NetworkInfo mNetworkInfo;
511997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    private InterfaceObserver mInterfaceObserver;
521997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
531997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    /* For sending events to connectivity service handler */
541997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    private Handler mCsHandler;
551997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    private Context mContext;
561997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
571997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    private static EthernetDataTracker sInstance;
58c96a667162fab44a250503caccb770109a9cb69aMike J. Chen    private static String sIfaceMatch = "";
591997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    private static String mIface = "";
601997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
611997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    private static class InterfaceObserver extends INetworkManagementEventObserver.Stub {
621997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        private EthernetDataTracker mTracker;
631997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
641997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        InterfaceObserver(EthernetDataTracker tracker) {
651997069436f0d47583a0b700c12ed63132cf3312Benoit Goby            super();
661997069436f0d47583a0b700c12ed63132cf3312Benoit Goby            mTracker = tracker;
671997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        }
681997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
69f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen        public void interfaceStatusChanged(String iface, boolean up) {
70f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen            Log.d(TAG, "Interface status changed: " + iface + (up ? "up" : "down"));
71f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen        }
72f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen
73f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen        public void interfaceLinkStateChanged(String iface, boolean up) {
74f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen            if (mIface.equals(iface) && mLinkUp != up) {
75f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen                Log.d(TAG, "Interface " + iface + " link " + (up ? "up" : "down"));
76f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen                mLinkUp = up;
77f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen
78f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen                // use DHCP
79f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen                if (up) {
80f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen                    mTracker.reconnect();
81f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen                } else {
82f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen                    NetworkUtils.stopDhcp(mIface);
83f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen                    mTracker.mNetworkInfo.setIsAvailable(false);
84f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen                    mTracker.mNetworkInfo.setDetailedState(DetailedState.DISCONNECTED,
85f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen                                                           null, null);
86f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen                }
87f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen            }
881997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        }
891997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
901997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        public void interfaceAdded(String iface) {
911997069436f0d47583a0b700c12ed63132cf3312Benoit Goby            mTracker.interfaceAdded(iface);
921997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        }
931997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
941997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        public void interfaceRemoved(String iface) {
951997069436f0d47583a0b700c12ed63132cf3312Benoit Goby            mTracker.interfaceRemoved(iface);
961997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        }
9712b933d0d9252decaae9fee2456bb1e1cd94c085JP Abgrall
9812b933d0d9252decaae9fee2456bb1e1cd94c085JP Abgrall        public void limitReached(String limitName, String iface) {
9912b933d0d9252decaae9fee2456bb1e1cd94c085JP Abgrall            // Ignored.
10012b933d0d9252decaae9fee2456bb1e1cd94c085JP Abgrall        }
1011997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
1021997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
1031997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    private EthernetDataTracker() {
1041997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        mNetworkInfo = new NetworkInfo(ConnectivityManager.TYPE_ETHERNET, 0, NETWORKTYPE, "");
1051997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        mLinkProperties = new LinkProperties();
1061997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        mLinkCapabilities = new LinkCapabilities();
107f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen        mLinkUp = false;
1081997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
1091997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        mNetworkInfo.setIsAvailable(false);
1101997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        setTeardownRequested(false);
1111997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
1121997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
1131997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    private void interfaceAdded(String iface) {
114c96a667162fab44a250503caccb770109a9cb69aMike J. Chen        if (!iface.matches(sIfaceMatch))
1151997069436f0d47583a0b700c12ed63132cf3312Benoit Goby            return;
1161997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
1171997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        Log.d(TAG, "Adding " + iface);
1181997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
1191997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        synchronized(mIface) {
1201997069436f0d47583a0b700c12ed63132cf3312Benoit Goby            if(!mIface.isEmpty())
1211997069436f0d47583a0b700c12ed63132cf3312Benoit Goby                return;
1221997069436f0d47583a0b700c12ed63132cf3312Benoit Goby            mIface = iface;
1231997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        }
1241997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
1251997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        mNetworkInfo.setIsAvailable(true);
1261997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        Message msg = mCsHandler.obtainMessage(EVENT_CONFIGURATION_CHANGED, mNetworkInfo);
1271997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        msg.sendToTarget();
1281997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
1291997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        runDhcp();
1301997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
1311997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
1321997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    private void interfaceRemoved(String iface) {
1331997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        if (!iface.equals(mIface))
1341997069436f0d47583a0b700c12ed63132cf3312Benoit Goby            return;
1351997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
1361997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        Log.d(TAG, "Removing " + iface);
1371997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
1381997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        NetworkUtils.stopDhcp(mIface);
1391997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
1401997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        mLinkProperties.clear();
1411997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        mNetworkInfo.setIsAvailable(false);
1421997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        mNetworkInfo.setDetailedState(DetailedState.DISCONNECTED, null, null);
1431997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
1441997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        Message msg = mCsHandler.obtainMessage(EVENT_CONFIGURATION_CHANGED, mNetworkInfo);
1451997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        msg.sendToTarget();
1461997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
1471997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        msg = mCsHandler.obtainMessage(EVENT_STATE_CHANGED, mNetworkInfo);
1481997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        msg.sendToTarget();
1491997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
1501997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        mIface = "";
1511997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
1521997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
1531997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    private void runDhcp() {
1541997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        Thread dhcpThread = new Thread(new Runnable() {
1551997069436f0d47583a0b700c12ed63132cf3312Benoit Goby            public void run() {
1561997069436f0d47583a0b700c12ed63132cf3312Benoit Goby                DhcpInfoInternal dhcpInfoInternal = new DhcpInfoInternal();
1571997069436f0d47583a0b700c12ed63132cf3312Benoit Goby                if (!NetworkUtils.runDhcp(mIface, dhcpInfoInternal)) {
1581997069436f0d47583a0b700c12ed63132cf3312Benoit Goby                    Log.e(TAG, "DHCP request error:" + NetworkUtils.getDhcpError());
1591997069436f0d47583a0b700c12ed63132cf3312Benoit Goby                    return;
1601997069436f0d47583a0b700c12ed63132cf3312Benoit Goby                }
1611997069436f0d47583a0b700c12ed63132cf3312Benoit Goby                mLinkProperties = dhcpInfoInternal.makeLinkProperties();
1621997069436f0d47583a0b700c12ed63132cf3312Benoit Goby                mLinkProperties.setInterfaceName(mIface);
1631997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
1641997069436f0d47583a0b700c12ed63132cf3312Benoit Goby                mNetworkInfo.setDetailedState(DetailedState.CONNECTED, null, null);
1651997069436f0d47583a0b700c12ed63132cf3312Benoit Goby                Message msg = mCsHandler.obtainMessage(EVENT_STATE_CHANGED, mNetworkInfo);
1661997069436f0d47583a0b700c12ed63132cf3312Benoit Goby                msg.sendToTarget();
1671997069436f0d47583a0b700c12ed63132cf3312Benoit Goby            }
1681997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        });
1691997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        dhcpThread.start();
1701997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
1711997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
1721997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    public static synchronized EthernetDataTracker getInstance() {
1731997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        if (sInstance == null) sInstance = new EthernetDataTracker();
1741997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        return sInstance;
1751997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
1761997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
1771997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    public Object Clone() throws CloneNotSupportedException {
1781997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        throw new CloneNotSupportedException();
1791997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
1801997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
1811997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    public void setTeardownRequested(boolean isRequested) {
1821997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        mTeardownRequested.set(isRequested);
1831997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
1841997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
1851997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    public boolean isTeardownRequested() {
1861997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        return mTeardownRequested.get();
1871997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
1881997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
1891997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    /**
1901997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * Begin monitoring connectivity
1911997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     */
1921997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    public void startMonitoring(Context context, Handler target) {
1931997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        mContext = context;
1941997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        mCsHandler = target;
1951997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
1961997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        // register for notifications from NetworkManagement Service
1971997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1981997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
199f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen
2001997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        mInterfaceObserver = new InterfaceObserver(this);
201c96a667162fab44a250503caccb770109a9cb69aMike J. Chen
202f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen        // enable and try to connect to an ethernet interface that
203f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen        // already exists
204c96a667162fab44a250503caccb770109a9cb69aMike J. Chen        sIfaceMatch = context.getResources().getString(
205c96a667162fab44a250503caccb770109a9cb69aMike J. Chen            com.android.internal.R.string.config_ethernet_iface_regex);
206c96a667162fab44a250503caccb770109a9cb69aMike J. Chen        try {
207c96a667162fab44a250503caccb770109a9cb69aMike J. Chen            final String[] ifaces = service.listInterfaces();
208c96a667162fab44a250503caccb770109a9cb69aMike J. Chen            for (String iface : ifaces) {
209c96a667162fab44a250503caccb770109a9cb69aMike J. Chen                if (iface.matches(sIfaceMatch)) {
210c96a667162fab44a250503caccb770109a9cb69aMike J. Chen                    mIface = iface;
211f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen                    InterfaceConfiguration config = service.getInterfaceConfig(iface);
212f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen                    mLinkUp = config.isActive();
213c96a667162fab44a250503caccb770109a9cb69aMike J. Chen                    reconnect();
214c96a667162fab44a250503caccb770109a9cb69aMike J. Chen                    break;
215c96a667162fab44a250503caccb770109a9cb69aMike J. Chen                }
216c96a667162fab44a250503caccb770109a9cb69aMike J. Chen            }
217c96a667162fab44a250503caccb770109a9cb69aMike J. Chen        } catch (RemoteException e) {
218c96a667162fab44a250503caccb770109a9cb69aMike J. Chen            Log.e(TAG, "Could not get list of interfaces " + e);
219c96a667162fab44a250503caccb770109a9cb69aMike J. Chen        }
220f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen
221f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen        try {
222f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen            service.registerObserver(mInterfaceObserver);
223f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen        } catch (RemoteException e) {
224f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen            Log.e(TAG, "Could not register InterfaceObserver " + e);
225f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen        }
2261997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
2271997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
2281997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    /**
2291997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * Disable connectivity to a network
2301997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * TODO: do away with return value after making MobileDataStateTracker async
2311997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     */
2321997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    public boolean teardown() {
2331997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        mTeardownRequested.set(true);
2341997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        NetworkUtils.stopDhcp(mIface);
2351997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        return true;
2361997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
2371997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
2381997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    /**
2391997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * Re-enable connectivity to a network after a {@link #teardown()}.
2401997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     */
2411997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    public boolean reconnect() {
2421997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        mTeardownRequested.set(false);
2431997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        runDhcp();
2441997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        return true;
2451997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
2461997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
2471997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    /**
2481997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * Turn the wireless radio off for a network.
2491997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * @param turnOn {@code true} to turn the radio on, {@code false}
2501997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     */
2511997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    public boolean setRadio(boolean turnOn) {
2521997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        return true;
2531997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
2541997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
2551997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    /**
2561997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * @return true - If are we currently tethered with another device.
2571997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     */
2581997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    public synchronized boolean isAvailable() {
2591997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        return mNetworkInfo.isAvailable();
2601997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
2611997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
2621997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    /**
2631997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * Tells the underlying networking system that the caller wants to
2641997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * begin using the named feature. The interpretation of {@code feature}
2651997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * is completely up to each networking implementation.
2661997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * @param feature the name of the feature to be used
2671997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * @param callingPid the process ID of the process that is issuing this request
2681997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * @param callingUid the user ID of the process that is issuing this request
2691997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * @return an integer value representing the outcome of the request.
2701997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * The interpretation of this value is specific to each networking
2711997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * implementation+feature combination, except that the value {@code -1}
2721997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * always indicates failure.
2731997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * TODO: needs to go away
2741997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     */
2751997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    public int startUsingNetworkFeature(String feature, int callingPid, int callingUid) {
2761997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        return -1;
2771997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
2781997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
2791997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    /**
2801997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * Tells the underlying networking system that the caller is finished
2811997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * using the named feature. The interpretation of {@code feature}
2821997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * is completely up to each networking implementation.
2831997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * @param feature the name of the feature that is no longer needed.
2841997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * @param callingPid the process ID of the process that is issuing this request
2851997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * @param callingUid the user ID of the process that is issuing this request
2861997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * @return an integer value representing the outcome of the request.
2871997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * The interpretation of this value is specific to each networking
2881997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * implementation+feature combination, except that the value {@code -1}
2891997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * always indicates failure.
2901997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * TODO: needs to go away
2911997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     */
2921997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    public int stopUsingNetworkFeature(String feature, int callingPid, int callingUid) {
2931997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        return -1;
2941997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
2951997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
2968e28b7d78232f6cf08739ca0d129cc7f9e650801Jeff Sharkey    @Override
2978e28b7d78232f6cf08739ca0d129cc7f9e650801Jeff Sharkey    public void setUserDataEnable(boolean enabled) {
2988e28b7d78232f6cf08739ca0d129cc7f9e650801Jeff Sharkey        Log.w(TAG, "ignoring setUserDataEnable(" + enabled + ")");
2998e28b7d78232f6cf08739ca0d129cc7f9e650801Jeff Sharkey    }
3008e28b7d78232f6cf08739ca0d129cc7f9e650801Jeff Sharkey
3018e28b7d78232f6cf08739ca0d129cc7f9e650801Jeff Sharkey    @Override
3028e28b7d78232f6cf08739ca0d129cc7f9e650801Jeff Sharkey    public void setPolicyDataEnable(boolean enabled) {
3038e28b7d78232f6cf08739ca0d129cc7f9e650801Jeff Sharkey        Log.w(TAG, "ignoring setPolicyDataEnable(" + enabled + ")");
3041997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
3051997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
3061997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    /**
3071997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * Check if private DNS route is set for the network
3081997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     */
3091997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    public boolean isPrivateDnsRouteSet() {
3101997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        return mPrivateDnsRouteSet.get();
3111997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
3121997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
3131997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    /**
3141997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * Set a flag indicating private DNS route is set
3151997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     */
3161997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    public void privateDnsRouteSet(boolean enabled) {
3171997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        mPrivateDnsRouteSet.set(enabled);
3181997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
3191997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
3201997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    /**
3211997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * Fetch NetworkInfo for the network
3221997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     */
3231997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    public synchronized NetworkInfo getNetworkInfo() {
3241997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        return mNetworkInfo;
3251997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
3261997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
3271997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    /**
3281997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * Fetch LinkProperties for the network
3291997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     */
3301997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    public synchronized LinkProperties getLinkProperties() {
3311997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        return new LinkProperties(mLinkProperties);
3321997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
3331997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
3341997069436f0d47583a0b700c12ed63132cf3312Benoit Goby   /**
3351997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * A capability is an Integer/String pair, the capabilities
3361997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * are defined in the class LinkSocket#Key.
3371997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     *
3381997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * @return a copy of this connections capabilities, may be empty but never null.
3391997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     */
3401997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    public LinkCapabilities getLinkCapabilities() {
3411997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        return new LinkCapabilities(mLinkCapabilities);
3421997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
3431997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
3441997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    /**
3451997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * Fetch default gateway address for the network
3461997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     */
3471997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    public int getDefaultGatewayAddr() {
3481997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        return mDefaultGatewayAddr.get();
3491997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
3501997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
3511997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    /**
3521997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * Check if default route is set
3531997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     */
3541997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    public boolean isDefaultRouteSet() {
3551997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        return mDefaultRouteSet.get();
3561997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
3571997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
3581997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    /**
3591997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * Set a flag indicating default route is set for the network
3601997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     */
3611997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    public void defaultRouteSet(boolean enabled) {
3621997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        mDefaultRouteSet.set(enabled);
3631997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
3641997069436f0d47583a0b700c12ed63132cf3312Benoit Goby
3651997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    /**
3661997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * Return the system properties name associated with the tcp buffer sizes
3671997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     * for this network.
3681997069436f0d47583a0b700c12ed63132cf3312Benoit Goby     */
3691997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    public String getTcpBufferSizesPropName() {
3701997069436f0d47583a0b700c12ed63132cf3312Benoit Goby        return "net.tcp.buffersize.wifi";
3711997069436f0d47583a0b700c12ed63132cf3312Benoit Goby    }
372af6eddb1c285a11841a2e1e0e3a41636f6bfd63dRobert Greenwalt
373af6eddb1c285a11841a2e1e0e3a41636f6bfd63dRobert Greenwalt    public void setDependencyMet(boolean met) {
374af6eddb1c285a11841a2e1e0e3a41636f6bfd63dRobert Greenwalt        // not supported on this network
375af6eddb1c285a11841a2e1e0e3a41636f6bfd63dRobert Greenwalt    }
3761997069436f0d47583a0b700c12ed63132cf3312Benoit Goby}
377