1d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt/*
2d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * Copyright (C) 2010 The Android Open Source Project
3d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt *
4d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * Licensed under the Apache License, Version 2.0 (the "License");
5d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * you may not use this file except in compliance with the License.
6d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * You may obtain a copy of the License at
7d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt *
8d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt *      http://www.apache.org/licenses/LICENSE-2.0
9d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt *
10d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * Unless required by applicable law or agreed to in writing, software
11d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * distributed under the License is distributed on an "AS IS" BASIS,
12d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * See the License for the specific language governing permissions and
14d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * limitations under the License.
15d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt */
16d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
17d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltpackage com.android.server.connectivity;
18d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
19d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.app.Notification;
20d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.app.NotificationManager;
21d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.app.PendingIntent;
22d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.content.BroadcastReceiver;
23d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.content.Context;
24d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.content.Intent;
25d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.content.IntentFilter;
262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.content.pm.PackageManager;
27d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.content.res.Resources;
28c4308f01c965571dc2354107c3574df113e397eeMike Lockwoodimport android.hardware.usb.UsbManager;
29d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.net.ConnectivityManager;
302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.net.IConnectivityManager;
31d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.net.INetworkManagementEventObserver;
32367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkeyimport android.net.INetworkStatsService;
33cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkeyimport android.net.InterfaceConfiguration;
34ed1264093234b1f1354cd5f669eb82a50bb56869Robert Greenwaltimport android.net.LinkAddress;
3505d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwaltimport android.net.LinkProperties;
362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.net.NetworkInfo;
37e590373ea71251cfffc8f22f011e2e6335dce716Robert Greenwaltimport android.net.NetworkUtils;
382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.os.Binder;
397eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwaltimport android.os.HandlerThread;
40d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.os.IBinder;
41d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.os.INetworkManagementService;
42dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwaltimport android.os.Looper;
432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.os.Message;
44d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.os.RemoteException;
45d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.os.ServiceManager;
465ac72a29593ab9a20337a2225df52bdf4754be02Dianne Hackbornimport android.os.UserHandle;
47d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.provider.Settings;
48d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.util.Log;
49d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport com.android.internal.telephony.Phone;
51a639b311e93ad14d9ee5c2b2c215ed2d86c32d2aWink Savilleimport com.android.internal.telephony.PhoneConstants;
5264c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Savilleimport com.android.internal.util.IState;
5364c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Savilleimport com.android.internal.util.State;
5464c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Savilleimport com.android.internal.util.StateMachine;
55cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkeyimport com.google.android.collect.Lists;
562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.io.FileDescriptor;
582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.io.PrintWriter;
5904808c294027f8bc318643a94c85a999257d7f52Robert Greenwaltimport java.net.InetAddress;
604f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwaltimport java.net.Inet4Address;
61d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport java.util.ArrayList;
62ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwaltimport java.util.Collection;
632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.util.HashMap;
64ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwaltimport java.util.Iterator;
652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.util.Set;
66cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey
67d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt/**
68d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * @hide
692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt *
702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt * Timeout
712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt *
722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt * TODO - look for parent classes and code sharing
73d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt */
74d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltpublic class Tethering extends INetworkManagementEventObserver.Stub {
75d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
76d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    private Context mContext;
77924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt    private final static String TAG = "Tethering";
78c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville    private final static boolean DBG = true;
79fd1be2bc1ebfccf9d111dbe3451293d6e31e4af1Robert Greenwalt    private final static boolean VDBG = false;
80d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    // TODO - remove both of these - should be part of interface inspection/selection stuff
822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mTetherableUsbRegexs;
832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mTetherableWifiRegexs;
846fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    private String[] mTetherableBluetoothRegexs;
85ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    private Collection<Integer> mUpstreamIfaceTypes;
86ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
87b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt    // used to synchronize public access to members
88b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt    private Object mPublicSync;
89b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt
90ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    private static final Integer MOBILE_TYPE = new Integer(ConnectivityManager.TYPE_MOBILE);
91ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    private static final Integer HIPRI_TYPE = new Integer(ConnectivityManager.TYPE_MOBILE_HIPRI);
92ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    private static final Integer DUN_TYPE = new Integer(ConnectivityManager.TYPE_MOBILE_DUN);
93ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
94ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    // if we have to connect to mobile, what APN type should we use?  Calculated by examining the
95ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    // upstream type list and the DUN_REQUIRED secure-setting
96ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    private int mPreferredUpstreamMobileApn = ConnectivityManager.TYPE_NONE;
972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
98367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey    private final INetworkManagementService mNMService;
99367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey    private final INetworkStatsService mStatsService;
1003b28e9a3daede9eac44faeb736ab4c8386ddd089Robert Greenwalt    private final IConnectivityManager mConnService;
1017eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt    private Looper mLooper;
1027eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt    private HandlerThread mThread;
103dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt
104030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private HashMap<String, TetherInterfaceSM> mIfaces; // all tethered/tetherable ifaces
105d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
106d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    private BroadcastReceiver mStateReceiver;
107d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
108bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String USB_NEAR_IFACE_ADDR      = "192.168.42.129";
109b979f79158f9c470fa09ff3b96d72db274262201Robert Greenwalt    private static final int USB_PREFIX_LENGTH        = 24;
110bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt
111707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    // USB is  192.168.42.1 and 255.255.255.0
112707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    // Wifi is 192.168.43.1 and 255.255.255.0
113707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    // BT is limited to max default of 5 connections. 192.168.44.1 to 192.168.48.1
114707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    // with 255.255.255.0
115d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt
1162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mDhcpRange;
117c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh    private static final String[] DHCP_DEFAULT_RANGE = {
118c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh        "192.168.42.2", "192.168.42.254", "192.168.43.2", "192.168.43.254",
119c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh        "192.168.44.2", "192.168.44.254", "192.168.45.2", "192.168.45.254",
120c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh        "192.168.46.2", "192.168.46.254", "192.168.47.2", "192.168.47.254",
121c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh        "192.168.48.2", "192.168.48.254",
122c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh    };
1232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
124bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt    private String[] mDefaultDnsServers;
125d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private static final String DNS_DEFAULT_SERVER1 = "8.8.8.8";
126df255c2283b2bcf2d5e70d095a4d2333c1cb80a3Robert Greenwalt    private static final String DNS_DEFAULT_SERVER2 = "8.8.4.4";
1272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
12864c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville    private StateMachine mTetherMasterSM;
129a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
130a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private Notification mTetheredNotification;
1312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1326c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood    private boolean mRndisEnabled;       // track the RNDIS function enabled state
1336c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood    private boolean mUsbTetherRequested; // true if USB tethering should be started
1346c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                                         // when RNDIS is enabled
135030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
136367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey    public Tethering(Context context, INetworkManagementService nmService,
1373b28e9a3daede9eac44faeb736ab4c8386ddd089Robert Greenwalt            INetworkStatsService statsService, IConnectivityManager connService, Looper looper) {
138d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        mContext = context;
139c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh        mNMService = nmService;
140367d15ab1a33b6159447fa8542d4fa8ff148371cJeff Sharkey        mStatsService = statsService;
1413b28e9a3daede9eac44faeb736ab4c8386ddd089Robert Greenwalt        mConnService = connService;
142dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        mLooper = looper;
143d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
144b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        mPublicSync = new Object();
145b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt
1462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mIfaces = new HashMap<String, TetherInterfaceSM>();
147d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
1487eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        // make our own thread so we don't anr the system
1497eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        mThread = new HandlerThread("Tethering");
1507eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        mThread.start();
1517eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        mLooper = mThread.getLooper();
152dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        mTetherMasterSM = new TetherMasterSM("TetherMaster", mLooper);
1532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mTetherMasterSM.start();
1542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
155bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        mStateReceiver = new StateReceiver();
156d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        IntentFilter filter = new IntentFilter();
157770126a678ccc9328a89407ffc82f4d998b25427Mike Lockwood        filter.addAction(UsbManager.ACTION_USB_STATE);
1582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
159bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        mContext.registerReceiver(mStateReceiver, filter);
160bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt
161bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        filter = new IntentFilter();
162030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        filter.addAction(Intent.ACTION_MEDIA_SHARED);
163030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
164bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        filter.addDataScheme("file");
165bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        mContext.registerReceiver(mStateReceiver, filter);
166bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt
1672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mDhcpRange = context.getResources().getStringArray(
1682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                com.android.internal.R.array.config_tether_dhcp_range);
169bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt        if ((mDhcpRange.length == 0) || (mDhcpRange.length % 2 ==1)) {
170c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh            mDhcpRange = DHCP_DEFAULT_RANGE;
1712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
1722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
17349348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt        // load device config info
17449348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt        updateConfiguration();
17549348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt
17649348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt        // TODO - remove and rely on real notifications of the current iface
177bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt        mDefaultDnsServers = new String[2];
178bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt        mDefaultDnsServers[0] = DNS_DEFAULT_SERVER1;
179bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt        mDefaultDnsServers[1] = DNS_DEFAULT_SERVER2;
18049348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt    }
18149348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt
18249348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt    void updateConfiguration() {
183b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        String[] tetherableUsbRegexs = mContext.getResources().getStringArray(
1842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                com.android.internal.R.array.config_tether_usb_regexs);
185b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        String[] tetherableWifiRegexs = mContext.getResources().getStringArray(
1862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                com.android.internal.R.array.config_tether_wifi_regexs);
187b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        String[] tetherableBluetoothRegexs = mContext.getResources().getStringArray(
1886fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                com.android.internal.R.array.config_tether_bluetooth_regexs);
18949348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt
19049348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt        int ifaceTypes[] = mContext.getResources().getIntArray(
191ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                com.android.internal.R.array.config_tether_upstream_types);
192b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        Collection<Integer> upstreamIfaceTypes = new ArrayList();
193ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        for (int i : ifaceTypes) {
194b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            upstreamIfaceTypes.add(new Integer(i));
195b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        }
196b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt
197b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
198b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            mTetherableUsbRegexs = tetherableUsbRegexs;
199b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            mTetherableWifiRegexs = tetherableWifiRegexs;
200b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            mTetherableBluetoothRegexs = tetherableBluetoothRegexs;
201b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            mUpstreamIfaceTypes = upstreamIfaceTypes;
202ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        }
203ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
204ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        // check if the upstream type list needs to be modified due to secure-settings
205ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        checkDunRequired();
206d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
207d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
2086143f5f7392fb0d3e5702a46a2415bd0ecb6efb4Mike J. Chen    public void interfaceStatusChanged(String iface, boolean up) {
209c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville        if (VDBG) Log.d(TAG, "interfaceStatusChanged " + iface + ", " + up);
2102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        boolean found = false;
21165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean usb = false;
212b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
213b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            if (isWifi(iface)) {
214b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                found = true;
215b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            } else if (isUsb(iface)) {
216b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                found = true;
217b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                usb = true;
218b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            } else if (isBluetooth(iface)) {
219b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                found = true;
220b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
221b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            if (found == false) return;
2222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
2232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
2246143f5f7392fb0d3e5702a46a2415bd0ecb6efb4Mike J. Chen            if (up) {
2252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm == null) {
226dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt                    sm = new TetherInterfaceSM(iface, mLooper, usb);
2272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    mIfaces.put(iface, sm);
2282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    sm.start();
2292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
2302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            } else {
231485861c409f9aaca2dac97ea82a66c2cb380ebadMike Lockwood                if (isUsb(iface)) {
232485861c409f9aaca2dac97ea82a66c2cb380ebadMike Lockwood                    // ignore usb0 down after enabling RNDIS
233485861c409f9aaca2dac97ea82a66c2cb380ebadMike Lockwood                    // we will handle disconnect in interfaceRemoved instead
234fd1be2bc1ebfccf9d111dbe3451293d6e31e4af1Robert Greenwalt                    if (VDBG) Log.d(TAG, "ignore interface down for " + iface);
235485861c409f9aaca2dac97ea82a66c2cb380ebadMike Lockwood                } else if (sm != null) {
2366a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(TetherInterfaceSM.CMD_INTERFACE_DOWN);
2372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    mIfaces.remove(iface);
2382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
2392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
2402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
241d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
242d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
243f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen    public void interfaceLinkStateChanged(String iface, boolean up) {
244c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville        if (VDBG) Log.d(TAG, "interfaceLinkStateChanged " + iface + ", " + up);
24523eb297c771bd996fe8559178feb1a07be43ad72Irfan Sheriff        interfaceStatusChanged(iface, up);
246f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen    }
247f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen
248a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private boolean isUsb(String iface) {
249b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
250b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            for (String regex : mTetherableUsbRegexs) {
251b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                if (iface.matches(regex)) return true;
252b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
253b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            return false;
254a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
255a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
256a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
257a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    public boolean isWifi(String iface) {
258b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
259b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            for (String regex : mTetherableWifiRegexs) {
260b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                if (iface.matches(regex)) return true;
261b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
262b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            return false;
263a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
264a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
265a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
2666fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    public boolean isBluetooth(String iface) {
267b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
268b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            for (String regex : mTetherableBluetoothRegexs) {
269b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                if (iface.matches(regex)) return true;
270b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
271b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            return false;
2726fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        }
2736fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    }
2746c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood
2752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void interfaceAdded(String iface) {
276c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville        if (VDBG) Log.d(TAG, "interfaceAdded " + iface);
2772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        boolean found = false;
27865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean usb = false;
279b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
280b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            if (isWifi(iface)) {
281b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                found = true;
282b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
283b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            if (isUsb(iface)) {
284b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                found = true;
285b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                usb = true;
286b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
287b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            if (isBluetooth(iface)) {
288b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                found = true;
289b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
290b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            if (found == false) {
291b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                if (VDBG) Log.d(TAG, iface + " is not a tetherable iface, ignoring");
292b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                return;
293b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
29465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
2952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
2962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (sm != null) {
297c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville                if (VDBG) Log.d(TAG, "active iface (" + iface + ") reported as added, ignoring");
2982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return;
2992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
300dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            sm = new TetherInterfaceSM(iface, mLooper, usb);
3012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaces.put(iface, sm);
3022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm.start();
3032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
304d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
305d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void interfaceRemoved(String iface) {
307c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville        if (VDBG) Log.d(TAG, "interfaceRemoved " + iface);
308b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
3092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
3102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (sm == null) {
311c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville                if (VDBG) {
312924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                    Log.e(TAG, "attempting to remove unknown iface (" + iface + "), ignoring");
313924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                }
3142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return;
3152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
3166a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            sm.sendMessage(TetherInterfaceSM.CMD_INTERFACE_DOWN);
3172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaces.remove(iface);
318d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
319d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
320d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
32112b933d0d9252decaae9fee2456bb1e1cd94c085JP Abgrall    public void limitReached(String limitName, String iface) {}
32212b933d0d9252decaae9fee2456bb1e1cd94c085JP Abgrall
323db3c8678e5cbdfec011afaf25bde2091152c30adHaoyu Bai    public void interfaceClassDataActivityChanged(String label, boolean active) {}
324db3c8678e5cbdfec011afaf25bde2091152c30adHaoyu Bai
3255a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int tether(String iface) {
326c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville        if (DBG) Log.d(TAG, "Tethering " + iface);
3272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        TetherInterfaceSM sm = null;
328b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
3292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm = mIfaces.get(iface);
330d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm == null) {
3322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Tether an unknown iface :" + iface + ", ignoring");
3335a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
334d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3355a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        if (!sm.isAvailable() && !sm.isErrored()) {
3362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Tether an unavailable iface :" + iface + ", ignoring");
3375a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
338d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3396a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        sm.sendMessage(TetherInterfaceSM.CMD_TETHER_REQUESTED);
3405a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return ConnectivityManager.TETHER_ERROR_NO_ERROR;
341d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
342d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3435a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int untether(String iface) {
344c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville        if (DBG) Log.d(TAG, "Untethering " + iface);
3452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        TetherInterfaceSM sm = null;
346b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
3472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm = mIfaces.get(iface);
3482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
3492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm == null) {
3502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Untether an unknown iface :" + iface + ", ignoring");
3515a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
352d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm.isErrored()) {
3542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Untethered an errored iface :" + iface + ", ignoring");
3555a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
356d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3576a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        sm.sendMessage(TetherInterfaceSM.CMD_TETHER_UNREQUESTED);
3585a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return ConnectivityManager.TETHER_ERROR_NO_ERROR;
3595a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
3605a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
3615a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int getLastTetherError(String iface) {
3625a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        TetherInterfaceSM sm = null;
363b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
3645a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            sm = mIfaces.get(iface);
365b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            if (sm == null) {
366b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                Log.e(TAG, "Tried to getLastTetherError on an unknown iface :" + iface +
367b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        ", ignoring");
368b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
369b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
370b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            return sm.getLastError();
3715a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
3722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
373d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
374b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt    // TODO - move all private methods used only by the state machine into the state machine
375b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt    // to clarify what needs synchronized protection.
3762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private void sendTetherStateChangedBroadcast() {
377d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        try {
3783b28e9a3daede9eac44faeb736ab4c8386ddd089Robert Greenwalt            if (!mConnService.isTetheringSupported()) return;
3792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        } catch (RemoteException e) {
3802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return;
381d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
382d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> availableList = new ArrayList<String>();
3842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> activeList = new ArrayList<String>();
3852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> erroredList = new ArrayList<String>();
3862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
387a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        boolean wifiTethered = false;
388a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        boolean usbTethered = false;
3896fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        boolean bluetoothTethered = false;
390a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
391b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
3922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set ifaces = mIfaces.keySet();
3932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object iface : ifaces) {
3942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(iface);
3952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm != null) {
3966fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                    if (sm.isErrored()) {
3972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        erroredList.add((String)iface);
3982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    } else if (sm.isAvailable()) {
3992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        availableList.add((String)iface);
4002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    } else if (sm.isTethered()) {
401a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        if (isUsb((String)iface)) {
402a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            usbTethered = true;
403a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        } else if (isWifi((String)iface)) {
404a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            wifiTethered = true;
4056fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                      } else if (isBluetooth((String)iface)) {
4066fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                            bluetoothTethered = true;
407a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        }
4082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        activeList.add((String)iface);
4092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    }
4102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
411d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            }
412d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
413d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Intent broadcast = new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
41498c79e53c0f24e98910a1ab130ec77d2ae707ed4Robert Greenwalt        broadcast.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING |
41598c79e53c0f24e98910a1ab130ec77d2ae707ed4Robert Greenwalt                Intent.FLAG_RECEIVER_REGISTERED_ONLY_BEFORE_BOOT);
4162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_AVAILABLE_TETHER,
4172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                availableList);
4182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ACTIVE_TETHER, activeList);
4192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ERRORED_TETHER,
4202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                erroredList);
4215ac72a29593ab9a20337a2225df52bdf4754be02Dianne Hackborn        mContext.sendStickyBroadcastAsUser(broadcast, UserHandle.ALL);
422fd1be2bc1ebfccf9d111dbe3451293d6e31e4af1Robert Greenwalt        if (DBG) {
423924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt            Log.d(TAG, "sendTetherStateChangedBroadcast " + availableList.size() + ", " +
424924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                    activeList.size() + ", " + erroredList.size());
425924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        }
426a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
427a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (usbTethered) {
4286fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            if (wifiTethered || bluetoothTethered) {
429a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general);
430a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            } else {
431a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_usb);
432a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            }
433a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        } else if (wifiTethered) {
4346fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            if (bluetoothTethered) {
4356fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general);
4366fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            } else {
4376fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_wifi);
4386fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            }
4396fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        } else if (bluetoothTethered) {
4406fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_bluetooth);
441a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        } else {
442a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            clearTetheredNotification();
443a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
444a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
445a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
446a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private void showTetheredNotification(int icon) {
447a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        NotificationManager notificationManager =
448a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
449a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (notificationManager == null) {
450a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            return;
451a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
452a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
453db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt        if (mTetheredNotification != null) {
454db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            if (mTetheredNotification.icon == icon) {
455db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt                return;
456db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            }
4573cab6b0f1deff1ee4b4b2e048935f2201d2cbc0dRobert Greenwalt            notificationManager.cancelAsUser(null, mTetheredNotification.icon,
4583cab6b0f1deff1ee4b4b2e048935f2201d2cbc0dRobert Greenwalt                    UserHandle.ALL);
459db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt        }
460db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt
461a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        Intent intent = new Intent();
462a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        intent.setClassName("com.android.settings", "com.android.settings.TetherSettings");
463a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
464a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
46550cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn        PendingIntent pi = PendingIntent.getActivityAsUser(mContext, 0, intent, 0,
46650cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn                null, UserHandle.CURRENT);
467a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
468a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        Resources r = Resources.getSystem();
469a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        CharSequence title = r.getText(com.android.internal.R.string.tethered_notification_title);
470a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        CharSequence message = r.getText(com.android.internal.R.string.
471a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                tethered_notification_message);
472a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
4736fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        if (mTetheredNotification == null) {
474a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification = new Notification();
475a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification.when = 0;
476a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
477a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.icon = icon;
478a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.defaults &= ~Notification.DEFAULT_SOUND;
479a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.flags = Notification.FLAG_ONGOING_EVENT;
480a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.tickerText = title;
481a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.setLatestEventInfo(mContext, title, message, pi);
482a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
48350cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn        notificationManager.notifyAsUser(null, mTetheredNotification.icon,
48450cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn                mTetheredNotification, UserHandle.ALL);
485a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
486a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
487a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private void clearTetheredNotification() {
488a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        NotificationManager notificationManager =
489a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
490a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (notificationManager != null && mTetheredNotification != null) {
49150cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn            notificationManager.cancelAsUser(null, mTetheredNotification.icon,
49250cdf7c3069eb2cf82acbad73c322b7a5f3af4b1Dianne Hackborn                    UserHandle.ALL);
493a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification = null;
494a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
4955a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
4965a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
4972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private class StateReceiver extends BroadcastReceiver {
498d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        public void onReceive(Context content, Intent intent) {
4992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            String action = intent.getAction();
500770126a678ccc9328a89407ffc82f4d998b25427Mike Lockwood            if (action.equals(UsbManager.ACTION_USB_STATE)) {
501b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                synchronized (Tethering.this.mPublicSync) {
5026c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    boolean usbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false);
5036c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    mRndisEnabled = intent.getBooleanExtra(UsbManager.USB_FUNCTION_RNDIS, false);
5046c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    // start tethering if we have a request pending
5056c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    if (usbConnected && mRndisEnabled && mUsbTetherRequested) {
5066c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                        tetherUsb(true);
5076c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    }
5086c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    mUsbTetherRequested = false;
5096c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                }
5102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
511adfb0d0cf1fed4b42e65fb35cdc342ab99e9c6a5Robert Greenwalt                NetworkInfo networkInfo = (NetworkInfo)intent.getParcelableExtra(
512adfb0d0cf1fed4b42e65fb35cdc342ab99e9c6a5Robert Greenwalt                        ConnectivityManager.EXTRA_NETWORK_INFO);
513adfb0d0cf1fed4b42e65fb35cdc342ab99e9c6a5Robert Greenwalt                if (networkInfo != null &&
514adfb0d0cf1fed4b42e65fb35cdc342ab99e9c6a5Robert Greenwalt                        networkInfo.getDetailedState() != NetworkInfo.DetailedState.FAILED) {
515adfb0d0cf1fed4b42e65fb35cdc342ab99e9c6a5Robert Greenwalt                    if (VDBG) Log.d(TAG, "Tethering got CONNECTIVITY_ACTION");
516adfb0d0cf1fed4b42e65fb35cdc342ab99e9c6a5Robert Greenwalt                    mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED);
517adfb0d0cf1fed4b42e65fb35cdc342ab99e9c6a5Robert Greenwalt                }
518d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            }
519d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
520d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
521d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
5226c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood    private void tetherUsb(boolean enable) {
523c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville        if (VDBG) Log.d(TAG, "tetherUsb " + enable);
5243c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood
52565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        String[] ifaces = new String[0];
52665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        try {
527c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh            ifaces = mNMService.listInterfaces();
52865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        } catch (Exception e) {
5293c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            Log.e(TAG, "Error listing Interfaces", e);
53065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            return;
53165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
53265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        for (String iface : ifaces) {
533a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (isUsb(iface)) {
5346c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                int result = (enable ? tether(iface) : untether(iface));
5356c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                if (result == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
5366c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    return;
53765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
53865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
53965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
5406c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood        Log.e(TAG, "unable start or stop USB tethering");
54165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    }
54265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
54365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    // configured when we start tethering and unconfig'd on error or conclusion
544d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private boolean configureUsbIface(boolean enabled) {
545c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville        if (VDBG) Log.d(TAG, "configureUsbIface(" + enabled + ")");
54665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
5476c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood        // toggle the USB interfaces
5486c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood        String[] ifaces = new String[0];
5493c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood        try {
5506c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood            ifaces = mNMService.listInterfaces();
5516c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood        } catch (Exception e) {
5526c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood            Log.e(TAG, "Error listing Interfaces", e);
5536c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood            return false;
5546c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood        }
5556c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood        for (String iface : ifaces) {
5566c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood            if (isUsb(iface)) {
5576c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                InterfaceConfiguration ifcg = null;
5586c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                try {
5596c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    ifcg = mNMService.getInterfaceConfig(iface);
5606c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    if (ifcg != null) {
5616c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                        InetAddress addr = NetworkUtils.numericToInetAddress(USB_NEAR_IFACE_ADDR);
562ddba10622dfd3122cf99c795706754e0d41858c3Jeff Sharkey                        ifcg.setLinkAddress(new LinkAddress(addr, USB_PREFIX_LENGTH));
5636c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                        if (enabled) {
564ddba10622dfd3122cf99c795706754e0d41858c3Jeff Sharkey                            ifcg.setInterfaceUp();
5656c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                        } else {
566ddba10622dfd3122cf99c795706754e0d41858c3Jeff Sharkey                            ifcg.setInterfaceDown();
56765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
568ddba10622dfd3122cf99c795706754e0d41858c3Jeff Sharkey                        ifcg.clearFlag("running");
5696c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                        mNMService.setInterfaceConfig(iface, ifcg);
57065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    }
5716c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                } catch (Exception e) {
5726c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    Log.e(TAG, "Error configuring interface " + iface, e);
5736c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    return false;
57465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
57565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
5766c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood         }
57765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
57865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        return true;
57965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    }
58065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
581b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt    // TODO - return copies so people can't tamper
5822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetherableUsbRegexs() {
5832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return mTetherableUsbRegexs;
5842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
5852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
5862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetherableWifiRegexs() {
5872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return mTetherableWifiRegexs;
5882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
5892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
5906fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    public String[] getTetherableBluetoothRegexs() {
5916fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        return mTetherableBluetoothRegexs;
5926fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    }
5936fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang
5946c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood    public int setUsbTethering(boolean enable) {
595c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville        if (VDBG) Log.d(TAG, "setUsbTethering(" + enable + ")");
5966c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood        UsbManager usbManager = (UsbManager)mContext.getSystemService(Context.USB_SERVICE);
5976c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood
598b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
5996c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood            if (enable) {
6006c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                if (mRndisEnabled) {
6016c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    tetherUsb(true);
6026c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                } else {
6036c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    mUsbTetherRequested = true;
6046c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    usbManager.setCurrentFunction(UsbManager.USB_FUNCTION_RNDIS, false);
6056c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                }
6066c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood            } else {
6076c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                tetherUsb(false);
6086c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                if (mRndisEnabled) {
6096c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                    usbManager.setCurrentFunction(null, false);
6106c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                }
6116c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood                mUsbTetherRequested = false;
6126c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood            }
6136c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood        }
6146c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood        return ConnectivityManager.TETHER_ERROR_NO_ERROR;
6156c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood    }
6166c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood
617ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    public int[] getUpstreamIfaceTypes() {
618b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        int values[];
619b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
620b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            updateConfiguration();
621b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            values = new int[mUpstreamIfaceTypes.size()];
622b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            Iterator<Integer> iterator = mUpstreamIfaceTypes.iterator();
623b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            for (int i=0; i < mUpstreamIfaceTypes.size(); i++) {
624b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                values[i] = iterator.next();
625b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
626ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        }
627ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        return values;
628c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    }
629c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt
630ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    public void checkDunRequired() {
631bf6f6f9de72c9fd15e6bda9f228c05a9b37d6324Jeff Brown        int secureSetting = Settings.Global.getInt(mContext.getContentResolver(),
632bf6f6f9de72c9fd15e6bda9f228c05a9b37d6324Jeff Brown                Settings.Global.TETHER_DUN_REQUIRED, 2);
633b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
634b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            // 2 = not set, 0 = DUN not required, 1 = DUN required
635b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            if (secureSetting != 2) {
636b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                int requiredApn = (secureSetting == 1 ?
637b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        ConnectivityManager.TYPE_MOBILE_DUN :
638b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        ConnectivityManager.TYPE_MOBILE_HIPRI);
639b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                if (requiredApn == ConnectivityManager.TYPE_MOBILE_DUN) {
640b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    while (mUpstreamIfaceTypes.contains(MOBILE_TYPE)) {
641b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        mUpstreamIfaceTypes.remove(MOBILE_TYPE);
642b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    }
643b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    while (mUpstreamIfaceTypes.contains(HIPRI_TYPE)) {
644b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        mUpstreamIfaceTypes.remove(HIPRI_TYPE);
645b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    }
646b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    if (mUpstreamIfaceTypes.contains(DUN_TYPE) == false) {
647b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        mUpstreamIfaceTypes.add(DUN_TYPE);
648b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    }
649b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                } else {
650b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    while (mUpstreamIfaceTypes.contains(DUN_TYPE)) {
651b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        mUpstreamIfaceTypes.remove(DUN_TYPE);
652b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    }
653b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    if (mUpstreamIfaceTypes.contains(MOBILE_TYPE) == false) {
654b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        mUpstreamIfaceTypes.add(MOBILE_TYPE);
655b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    }
656b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    if (mUpstreamIfaceTypes.contains(HIPRI_TYPE) == false) {
657b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        mUpstreamIfaceTypes.add(HIPRI_TYPE);
658b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    }
659ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                }
660b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
661b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            if (mUpstreamIfaceTypes.contains(DUN_TYPE)) {
662b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                mPreferredUpstreamMobileApn = ConnectivityManager.TYPE_MOBILE_DUN;
663ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            } else {
664b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                mPreferredUpstreamMobileApn = ConnectivityManager.TYPE_MOBILE_HIPRI;
665ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            }
66649348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt        }
667c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    }
668c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt
669b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt    // TODO review API - maybe return ArrayList<String> here and below?
6702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetheredIfaces() {
6712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> list = new ArrayList<String>();
672b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
6732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set keys = mIfaces.keySet();
6742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object key : keys) {
6752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
6762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm.isTethered()) {
6772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    list.add((String)key);
6782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
6792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
6802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String[] retVal = new String[list.size()];
6822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        for (int i=0; i < list.size(); i++) {
6832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            retVal[i] = list.get(i);
6842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return retVal;
6862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
6872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
688cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey    public String[] getTetheredIfacePairs() {
689cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey        final ArrayList<String> list = Lists.newArrayList();
690b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
691cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey            for (TetherInterfaceSM sm : mIfaces.values()) {
692cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey                if (sm.isTethered()) {
693cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey                    list.add(sm.mMyUpstreamIfaceName);
694cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey                    list.add(sm.mIfaceName);
695cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey                }
696cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey            }
697cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey        }
698cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey        return list.toArray(new String[list.size()]);
699cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey    }
700cdd02c5d76d3dd4e21b5bb922d7fcfb86efec85fJeff Sharkey
7012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetherableIfaces() {
7022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> list = new ArrayList<String>();
703b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
7042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set keys = mIfaces.keySet();
7052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object key : keys) {
7062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
7072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm.isAvailable()) {
7082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    list.add((String)key);
7092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
7102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
7112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String[] retVal = new String[list.size()];
7132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        for (int i=0; i < list.size(); i++) {
7142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            retVal[i] = list.get(i);
7152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return retVal;
7172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
7182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7195a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public String[] getErroredIfaces() {
7205a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        ArrayList<String> list = new ArrayList<String>();
721b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
7225a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            Set keys = mIfaces.keySet();
7235a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            for (Object key : keys) {
7245a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
7255a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                if (sm.isErrored()) {
7265a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    list.add((String)key);
7275a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                }
7285a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            }
7295a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
7305a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        String[] retVal = new String[list.size()];
7315a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        for (int i= 0; i< list.size(); i++) {
7325a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            retVal[i] = list.get(i);
7335a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
7345a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return retVal;
7355a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
7365a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
7371ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo    //TODO: Temporary handling upstream change triggered without
7381ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo    //      CONNECTIVITY_ACTION. Only to accomodate interface
7391ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo    //      switch during HO.
7401ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo    //      @see bug/4455071
7411ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo    public void handleTetherIfaceChange() {
7421ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED);
7431ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo    }
7441ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo
74564c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville    class TetherInterfaceSM extends StateMachine {
7462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it's not in tether mode
7476a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_MODE_DEAD            =  1;
7482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // request from the user that it wants to tether
7496a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_REQUESTED            =  2;
7502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // request from the user that it wants to untether
7516a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_UNREQUESTED          =  3;
7522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification that this interface is down
7536a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_INTERFACE_DOWN              =  4;
7542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification that this interface is up
7556a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_INTERFACE_UP                =  5;
7562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had an error turning on cellular dun
7576a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_CELL_DUN_ERROR              =  6;
7582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble enabling IP Forwarding
7596a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_IP_FORWARDING_ENABLE_ERROR  =  7;
7602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble disabling IP Forwarding
7616a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_IP_FORWARDING_DISABLE_ERROR =  8;
7622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble staring tethering
7636a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_START_TETHERING_ERROR       =  9;
7642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble stopping tethering
7656a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_STOP_TETHERING_ERROR        = 10;
7662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble setting the DNS forwarders
7676a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_SET_DNS_FORWARDERS_ERROR    = 11;
7686a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // the upstream connection has changed
769951749ba2e014566553d17c512e947472951a060Wink Saville        static final int CMD_TETHER_CONNECTION_CHANGED   = 12;
7702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
77164c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mDefaultState;
7722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
77364c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mInitialState;
77464c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mStartingState;
77564c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mTetheredState;
7762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
77764c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mUnavailableState;
7782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private boolean mAvailable;
7802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private boolean mTethered;
7815a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        int mLastError;
7822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String mIfaceName;
7846a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        String mMyUpstreamIfaceName;  // may change over time
7856a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
78665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean mUsb;
7872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
788dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        TetherInterfaceSM(String name, Looper looper, boolean usb) {
789dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            super(name, looper);
7902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaceName = name;
79165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            mUsb = usb;
7925a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
7932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mInitialState = new InitialState();
7952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mInitialState);
7962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStartingState = new StartingState();
7972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStartingState);
7982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mTetheredState = new TetheredState();
7992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mTetheredState);
8002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mUnavailableState = new UnavailableState();
8012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mUnavailableState);
8022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
8032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            setInitialState(mInitialState);
8042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
8062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public String toString() {
8072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            String res = new String();
8082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            res += mIfaceName + " - ";
80964c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville            IState current = getCurrentState();
8102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mInitialState) res += "InitialState";
8112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mStartingState) res += "StartingState";
8122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mTetheredState) res += "TetheredState";
8132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mUnavailableState) res += "UnavailableState";
8142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (mAvailable) res += " - Available";
8152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (mTethered) res += " - Tethered";
8165a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            res += " - lastError =" + mLastError;
8172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return res;
8182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
820b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        public int getLastError() {
821b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            synchronized (Tethering.this.mPublicSync) {
822b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                return mLastError;
823b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
8245a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
8255a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
826b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        private void setLastError(int error) {
827b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            synchronized (Tethering.this.mPublicSync) {
828b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                mLastError = error;
8295a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
830b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                if (isErrored()) {
831b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    if (mUsb) {
832b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        // note everything's been unwound by this point so nothing to do on
833b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        // further error..
834b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        Tethering.this.configureUsbIface(false);
835b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    }
8365a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                }
8375a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            }
8385a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
8395a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
840b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        public boolean isAvailable() {
841b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            synchronized (Tethering.this.mPublicSync) {
842b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                return mAvailable;
843b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
8442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
846b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        private void setAvailable(boolean available) {
847b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            synchronized (Tethering.this.mPublicSync) {
848b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                mAvailable = available;
849b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
8502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
852b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        public boolean isTethered() {
853b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            synchronized (Tethering.this.mPublicSync) {
854b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                return mTethered;
855