Tethering.java revision 3cab6b0f1deff1ee4b4b2e048935f2201d2cbc0d
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;
855b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
8562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
858b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        private void setTethered(boolean tethered) {
859b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            synchronized (Tethering.this.mPublicSync) {
860b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                mTethered = tethered;
861b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
8622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
864b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        public boolean isErrored() {
865b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            synchronized (Tethering.this.mPublicSync) {
866b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                return (mLastError != ConnectivityManager.TETHER_ERROR_NO_ERROR);
867b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
8682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
87064c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class InitialState extends State {
8712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
8722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
8732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(true);
8742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(false);
8752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
8762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
8782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
8792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
880fd1be2bc1ebfccf9d111dbe3451293d6e31e4af1Robert Greenwalt                if (DBG) Log.d(TAG, "InitialState.processMessage what=" + message.what);
8812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
8822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
8832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_REQUESTED:
8845a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
8856a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_REQUESTED,
8866a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
8872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mStartingState);
8882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
8902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mUnavailableState);
8912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
8932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
8942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
8962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
8972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
90064c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class StartingState extends State {
9012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
9022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
9032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
90465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                if (mUsb) {
905d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    if (!Tethering.this.configureUsbIface(true)) {
9066a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
9076a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
9085a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastError(ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
9095a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
910951749ba2e014566553d17c512e947472951a060Wink Saville                        transitionTo(mInitialState);
91165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        return;
91265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    }
91365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
9142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
9156a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
916951749ba2e014566553d17c512e947472951a060Wink Saville                // Skipping StartingState
917951749ba2e014566553d17c512e947472951a060Wink Saville                transitionTo(mTetheredState);
9182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
9192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
9202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
921fd1be2bc1ebfccf9d111dbe3451293d6e31e4af1Robert Greenwalt                if (DBG) Log.d(TAG, "StartingState.processMessage what=" + message.what);
9222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
9232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
9242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    // maybe a parent class?
9252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_UNREQUESTED:
9266a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
9276a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
92865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        if (mUsb) {
929d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                            if (!Tethering.this.configureUsbIface(false)) {
9305a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                setLastErrorAndTransitionToInitialState(
9315a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
93265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                                break;
93365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
93465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
9352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
9362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
9372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_CELL_DUN_ERROR:
9382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_ENABLE_ERROR:
9392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_DISABLE_ERROR:
9402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_START_TETHERING_ERROR:
9412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_STOP_TETHERING_ERROR:
9422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_SET_DNS_FORWARDERS_ERROR:
9435a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastErrorAndTransitionToInitialState(
9445a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                ConnectivityManager.TETHER_ERROR_MASTER_ERROR);
9452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
9462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
9476a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
9486a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
9492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mUnavailableState);
9502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
9512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
9522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
9532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
9542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
9552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
9562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
9572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
95864c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class TetheredState extends State {
9592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
9602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
9612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
962c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.tetherInterface(mIfaceName);
9632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {
9643b28e9a3daede9eac44faeb736ab4c8386ddd089Robert Greenwalt                    Log.e(TAG, "Error Tethering: " + e.toString());
9655a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    setLastError(ConnectivityManager.TETHER_ERROR_TETHER_IFACE_ERROR);
9665a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
967951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mInitialState);
9682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    return;
9692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
970c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville                if (DBG) Log.d(TAG, "Tethered " + mIfaceName);
9712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
9722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(true);
9732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
9742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
975731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt
976b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            private void cleanupUpstream() {
977731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                if (mMyUpstreamIfaceName != null) {
978731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                    // note that we don't care about errors here.
979731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                    // sometimes interfaces are gone before we get
980731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                    // to remove their rules, which generates errors.
981731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                    // just do the best we can.
982731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                    try {
983731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                        // about to tear down NAT; gather remaining statistics
984731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                        mStatsService.forceUpdate();
985731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                    } catch (Exception e) {
986731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                        if (VDBG) Log.e(TAG, "Exception in forceUpdate: " + e.toString());
987731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                    }
988731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                    try {
989731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                        mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName);
990731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                    } catch (Exception e) {
991731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                        if (VDBG) Log.e(TAG, "Exception in disableNat: " + e.toString());
992731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                    }
993731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                    mMyUpstreamIfaceName = null;
994731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                }
995731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                return;
996731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt            }
997731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt
9982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
9992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
1000fd1be2bc1ebfccf9d111dbe3451293d6e31e4af1Robert Greenwalt                if (DBG) Log.d(TAG, "TetheredState.processMessage what=" + message.what);
10012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
10022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean error = false;
10032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
10042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_UNREQUESTED:
10052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
1006731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                        cleanupUpstream();
10072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        try {
1008c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                            mNMService.untetherInterface(mIfaceName);
10092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } catch (Exception e) {
10105a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
10115a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR);
10122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
10132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
10146a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
10156a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
10162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (message.what == CMD_TETHER_UNREQUESTED) {
101765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            if (mUsb) {
1018d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                                if (!Tethering.this.configureUsbIface(false)) {
10195a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    setLastError(
10205a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                            ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
102165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                                }
102265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
10235a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            transitionTo(mInitialState);
10242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } else if (message.what == CMD_INTERFACE_DOWN) {
10252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            transitionTo(mUnavailableState);
10262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
1027c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville                        if (DBG) Log.d(TAG, "Untethered " + mIfaceName);
10282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10296a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_TETHER_CONNECTION_CHANGED:
10306a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        String newUpstreamIfaceName = (String)(message.obj);
10311039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        if ((mMyUpstreamIfaceName == null && newUpstreamIfaceName == null) ||
10321039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                                (mMyUpstreamIfaceName != null &&
10331039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                                mMyUpstreamIfaceName.equals(newUpstreamIfaceName))) {
1034c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville                            if (VDBG) Log.d(TAG, "Connection changed noop - dropping");
10351039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                            break;
10361039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        }
1037731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                        cleanupUpstream();
10386a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (newUpstreamIfaceName != null) {
10396a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            try {
1040c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                                mNMService.enableNat(mIfaceName, newUpstreamIfaceName);
10416a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
10423b28e9a3daede9eac44faeb736ab4c8386ddd089Robert Greenwalt                                Log.e(TAG, "Exception enabling Nat: " + e.toString());
10436a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
1044c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                                    mNMService.untetherInterface(mIfaceName);
10456a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
10466a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
10476a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastError(ConnectivityManager.TETHER_ERROR_ENABLE_NAT_ERROR);
10486a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                transitionTo(mInitialState);
10496a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                return true;
10506a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
10516a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        }
10526a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mMyUpstreamIfaceName = newUpstreamIfaceName;
10536a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        break;
10542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_CELL_DUN_ERROR:
10552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_ENABLE_ERROR:
10562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_DISABLE_ERROR:
10572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_START_TETHERING_ERROR:
10582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_STOP_TETHERING_ERROR:
10592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_SET_DNS_FORWARDERS_ERROR:
10602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        error = true;
10612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        // fall through
10622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_DEAD:
1063731026c2df0daff1a2ff5a4c1cf44f3891995dd8Robert Greenwalt                        cleanupUpstream();
10642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        try {
1065c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                            mNMService.untetherInterface(mIfaceName);
10662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } catch (Exception e) {
10675a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
10685a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR);
10692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
10702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
10712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (error) {
10725a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
10735a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_MASTER_ERROR);
10742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
10752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
1076fd1be2bc1ebfccf9d111dbe3451293d6e31e4af1Robert Greenwalt                        if (DBG) Log.d(TAG, "Tether lost upstream connection " + mIfaceName);
10772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        sendTetherStateChangedBroadcast();
107865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        if (mUsb) {
1079d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                            if (!Tethering.this.configureUsbIface(false)) {
10805a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                setLastError(ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
108165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
108265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
10832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
10842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
10862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
10872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
10892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
10902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
10912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
10922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
109364c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class UnavailableState extends State {
10942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
10952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
10962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
10975a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
10982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(false);
10992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
11002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
11012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
11022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
11032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
11042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
11052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_UP:
11062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
11072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
11082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
11092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
11102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
11112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
11122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
11132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
11142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
11152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11165a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        void setLastErrorAndTransitionToInitialState(int error) {
11175a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            setLastError(error);
11185a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            transitionTo(mInitialState);
11192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
112065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
11212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
11222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
112364c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville    class TetherMasterSM extends StateMachine {
11242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // an interface SM has requested Tethering
11252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        static final int CMD_TETHER_MODE_REQUESTED   = 1;
11262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // an interface SM has unrequested Tethering
11272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        static final int CMD_TETHER_MODE_UNREQUESTED = 2;
11286a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // upstream connection change - do the right thing
11296a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_UPSTREAM_CHANGED        = 3;
11302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // we received notice that the cellular DUN connection is up
11316a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_CELL_CONNECTION_RENEW   = 4;
11326a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // we don't have a valid upstream conn, check again after a delay
1133951749ba2e014566553d17c512e947472951a060Wink Saville        static final int CMD_RETRY_UPSTREAM          = 5;
11342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // This indicates what a timeout event relates to.  A state that
11362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // sends itself a delayed timeout event and handles incoming timeout events
11372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // should inc this when it is entered and whenever it sends a new timeout event.
11382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // We do not flush the old ones.
11392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private int mSequenceNumber;
11402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
114164c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mInitialState;
114264c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mTetherModeAliveState;
11432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
114464c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mSetIpForwardingEnabledErrorState;
114564c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mSetIpForwardingDisabledErrorState;
114664c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mStartTetheringErrorState;
114764c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mStopTetheringErrorState;
114864c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mSetDnsForwardersErrorState;
11492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
115068ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt        private ArrayList<TetherInterfaceSM> mNotifyList;
11512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11521039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt        private int mCurrentConnectionSequence;
1153ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        private int mMobileApnReserved = ConnectivityManager.TYPE_NONE;
11542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1155b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt        private String mUpstreamIfaceName = null;
1156b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt
11576a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        private static final int UPSTREAM_SETTLE_TIME_MS     = 10000;
11586a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        private static final int CELL_CONNECTION_RENEW_MS    = 40000;
11592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1160dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        TetherMasterSM(String name, Looper looper) {
1161dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            super(name, looper);
11622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            //Add states
11642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mInitialState = new InitialState();
11652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mInitialState);
11662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mTetherModeAliveState = new TetherModeAliveState();
11672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mTetherModeAliveState);
11682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetIpForwardingEnabledErrorState = new SetIpForwardingEnabledErrorState();
11702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetIpForwardingEnabledErrorState);
11712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetIpForwardingDisabledErrorState = new SetIpForwardingDisabledErrorState();
11722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetIpForwardingDisabledErrorState);
11732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStartTetheringErrorState = new StartTetheringErrorState();
11742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStartTetheringErrorState);
11752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStopTetheringErrorState = new StopTetheringErrorState();
11762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStopTetheringErrorState);
11772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetDnsForwardersErrorState = new SetDnsForwardersErrorState();
11782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetDnsForwardersErrorState);
11792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
118068ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt            mNotifyList = new ArrayList<TetherInterfaceSM>();
11812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            setInitialState(mInitialState);
11822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
11832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
118464c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class TetherMasterUtilState extends State {
11856a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected final static boolean TRY_TO_SETUP_MOBILE_CONNECTION = true;
11866a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected final static boolean WAIT_FOR_NETWORK_TO_SETTLE     = false;
11876a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
1188d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            @Override
1189d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            public boolean processMessage(Message m) {
1190d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return false;
1191d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
1192ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            protected String enableString(int apnType) {
1193ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                switch (apnType) {
1194ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                case ConnectivityManager.TYPE_MOBILE_DUN:
1195ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    return Phone.FEATURE_ENABLE_DUN_ALWAYS;
1196ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                case ConnectivityManager.TYPE_MOBILE:
1197ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                case ConnectivityManager.TYPE_MOBILE_HIPRI:
1198ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    return Phone.FEATURE_ENABLE_HIPRI;
1199ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                }
1200ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                return null;
1201ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            }
1202ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            protected boolean turnOnUpstreamMobileConnection(int apnType) {
12031039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                boolean retValue = true;
1204ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (apnType == ConnectivityManager.TYPE_NONE) return false;
1205ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (apnType != mMobileApnReserved) turnOffUpstreamMobileConnection();
1206a639b311e93ad14d9ee5c2b2c215ed2d86c32d2aWink Saville                int result = PhoneConstants.APN_REQUEST_FAILED;
1207ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                String enableString = enableString(apnType);
1208ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (enableString == null) return false;
1209d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
12103b28e9a3daede9eac44faeb736ab4c8386ddd089Robert Greenwalt                    result = mConnService.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
1211ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                            enableString, new Binder());
1212d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1213d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
12141039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                switch (result) {
1215a639b311e93ad14d9ee5c2b2c215ed2d86c32d2aWink Saville                case PhoneConstants.APN_ALREADY_ACTIVE:
1216a639b311e93ad14d9ee5c2b2c215ed2d86c32d2aWink Saville                case PhoneConstants.APN_REQUEST_STARTED:
1217ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    mMobileApnReserved = apnType;
12181039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    Message m = obtainMessage(CMD_CELL_CONNECTION_RENEW);
12191039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    m.arg1 = ++mCurrentConnectionSequence;
12201039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    sendMessageDelayed(m, CELL_CONNECTION_RENEW_MS);
12216a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    break;
1222a639b311e93ad14d9ee5c2b2c215ed2d86c32d2aWink Saville                case PhoneConstants.APN_REQUEST_FAILED:
12236a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                default:
12241039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    retValue = false;
12256a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    break;
12266a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
12276a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
1228d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return retValue;
1229d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
1230ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            protected boolean turnOffUpstreamMobileConnection() {
1231c9dac1185d6944e4bdb851a06ae07ea3621042bfRobert Greenwalt                // ignore pending renewal requests
1232c9dac1185d6944e4bdb851a06ae07ea3621042bfRobert Greenwalt                ++mCurrentConnectionSequence;
1233ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (mMobileApnReserved != ConnectivityManager.TYPE_NONE) {
12346a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    try {
12353b28e9a3daede9eac44faeb736ab4c8386ddd089Robert Greenwalt                        mConnService.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
1236ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                                enableString(mMobileApnReserved));
12376a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    } catch (Exception e) {
12386a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        return false;
12396a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    }
1240ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    mMobileApnReserved = ConnectivityManager.TYPE_NONE;
1241d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1242d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1243d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
12446a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected boolean turnOnMasterTetherSettings() {
1245d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1246c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.setIpForwardingEnabled(true);
1247d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1248951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mSetIpForwardingEnabledErrorState);
1249d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1250d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1251d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1252c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.startTethering(mDhcpRange);
1253d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1254547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                    try {
1255c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                        mNMService.stopTethering();
1256c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                        mNMService.startTethering(mDhcpRange);
1257547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                    } catch (Exception ee) {
1258547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                        transitionTo(mStartTetheringErrorState);
1259547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                        return false;
1260547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                    }
1261d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1262d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1263bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                    mNMService.setDnsForwarders(mDefaultDnsServers);
1264d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1265951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mSetDnsForwardersErrorState);
1266d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1267d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1268d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1269d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
12706a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected boolean turnOffMasterTetherSettings() {
1271d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1272c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.stopTethering();
1273d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1274d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    transitionTo(mStopTetheringErrorState);
1275d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1276d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1277d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1278c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.setIpForwardingEnabled(false);
1279d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1280d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    transitionTo(mSetIpForwardingDisabledErrorState);
1281d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1282d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1283d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                transitionTo(mInitialState);
1284d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1285d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
1286ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
1287ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            protected void chooseUpstreamType(boolean tryCell) {
1288ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                int upType = ConnectivityManager.TYPE_NONE;
1289ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                String iface = null;
129005d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt
129149348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt                updateConfiguration();
129249348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt
1293b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                synchronized (mPublicSync) {
1294b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    if (VDBG) {
1295b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        Log.d(TAG, "chooseUpstreamType has upstream iface types:");
1296b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        for (Integer netType : mUpstreamIfaceTypes) {
1297b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                            Log.d(TAG, " " + netType);
1298b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        }
129949348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt                    }
130049348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt
1301b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                    for (Integer netType : mUpstreamIfaceTypes) {
1302b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        NetworkInfo info = null;
1303b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        try {
1304b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                            info = mConnService.getNetworkInfo(netType.intValue());
1305b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        } catch (RemoteException e) { }
1306b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        if ((info != null) && info.isConnected()) {
1307b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                            upType = netType.intValue();
1308b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                            break;
1309b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                        }
1310c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    }
1311c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                }
13121039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt
1313fd1be2bc1ebfccf9d111dbe3451293d6e31e4af1Robert Greenwalt                if (DBG) {
1314ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    Log.d(TAG, "chooseUpstreamType(" + tryCell + "), preferredApn ="
1315ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                            + mPreferredUpstreamMobileApn + ", got type=" + upType);
1316924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                }
1317ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
1318ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                // if we're on DUN, put our own grab on it
1319ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (upType == ConnectivityManager.TYPE_MOBILE_DUN ||
1320ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                        upType == ConnectivityManager.TYPE_MOBILE_HIPRI) {
1321ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    turnOnUpstreamMobileConnection(upType);
1322c9dac1185d6944e4bdb851a06ae07ea3621042bfRobert Greenwalt                } else if (upType != ConnectivityManager.TYPE_NONE) {
1323c9dac1185d6944e4bdb851a06ae07ea3621042bfRobert Greenwalt                    /* If we've found an active upstream connection that's not DUN/HIPRI
1324c9dac1185d6944e4bdb851a06ae07ea3621042bfRobert Greenwalt                     * we should stop any outstanding DUN/HIPRI start requests.
1325c9dac1185d6944e4bdb851a06ae07ea3621042bfRobert Greenwalt                     *
1326c9dac1185d6944e4bdb851a06ae07ea3621042bfRobert Greenwalt                     * If we found NONE we don't want to do this as we want any previous
1327c9dac1185d6944e4bdb851a06ae07ea3621042bfRobert Greenwalt                     * requests to keep trying to bring up something we can use.
1328c9dac1185d6944e4bdb851a06ae07ea3621042bfRobert Greenwalt                     */
1329c9dac1185d6944e4bdb851a06ae07ea3621042bfRobert Greenwalt                    turnOffUpstreamMobileConnection();
13306a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
1331ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
1332ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (upType == ConnectivityManager.TYPE_NONE) {
1333ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    boolean tryAgainLater = true;
1334ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    if ((tryCell == TRY_TO_SETUP_MOBILE_CONNECTION) &&
1335ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                            (turnOnUpstreamMobileConnection(mPreferredUpstreamMobileApn) == true)) {
1336ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                        // we think mobile should be coming up - don't set a retry
1337ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                        tryAgainLater = false;
13381039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    }
1339ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    if (tryAgainLater) {
13401039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        sendMessageDelayed(CMD_RETRY_UPSTREAM, UPSTREAM_SETTLE_TIME_MS);
134177e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                    }
1342ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                } else {
1343ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    LinkProperties linkProperties = null;
1344ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    try {
13453b28e9a3daede9eac44faeb736ab4c8386ddd089Robert Greenwalt                        linkProperties = mConnService.getLinkProperties(upType);
1346ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    } catch (RemoteException e) { }
1347bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                    if (linkProperties != null) {
1348bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                        iface = linkProperties.getInterfaceName();
1349bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                        String[] dnsServers = mDefaultDnsServers;
1350bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                        Collection<InetAddress> dnses = linkProperties.getDnses();
1351bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                        if (dnses != null) {
13524f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                            // we currently only handle IPv4
13534f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                            ArrayList<InetAddress> v4Dnses =
13544f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                                    new ArrayList<InetAddress>(dnses.size());
13554f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                            for (InetAddress dnsAddress : dnses) {
13564f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                                if (dnsAddress instanceof Inet4Address) {
13574f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                                    v4Dnses.add(dnsAddress);
13584f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                                }
13594f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                            }
13604f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                            if (v4Dnses.size() > 0) {
13614f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                                dnsServers = NetworkUtils.makeStrings(v4Dnses);
13624f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                            }
1363bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                        }
1364bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                        try {
1365bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                            mNMService.setDnsForwarders(dnsServers);
1366bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                        } catch (Exception e) {
1367bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                            transitionTo(mSetDnsForwardersErrorState);
1368bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                        }
1369bb4fd0b4442301a7c64b52a1241a9d0cd4a75d0bRobert Greenwalt                    }
13706a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
1371b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                notifyTetheredOfNewUpstreamIface(iface);
13726a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            }
1373ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
1374b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt            protected void notifyTetheredOfNewUpstreamIface(String ifaceName) {
1375fd1be2bc1ebfccf9d111dbe3451293d6e31e4af1Robert Greenwalt                if (DBG) Log.d(TAG, "notifying tethered with iface =" + ifaceName);
1376b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                mUpstreamIfaceName = ifaceName;
137768ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                for (TetherInterfaceSM sm : mNotifyList) {
13786a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED,
13796a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            ifaceName);
13802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
13812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
13832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
13846a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        class InitialState extends TetherMasterUtilState {
13852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
13872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
1390fd1be2bc1ebfccf9d111dbe3451293d6e31e4af1Robert Greenwalt                if (DBG) Log.d(TAG, "MasterInitialState.processMessage what=" + message.what);
13912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
13922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
13932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
13942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
139568ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                        if (VDBG) Log.d(TAG, "Tether Mode requested by " + who);
13962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        mNotifyList.add(who);
13976a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        transitionTo(mTetherModeAliveState);
13982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_UNREQUESTED:
14002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        who = (TetherInterfaceSM)message.obj;
140168ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                        if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who);
14022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        int index = mNotifyList.indexOf(who);
14032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (index != -1) {
14046a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            mNotifyList.remove(who);
14052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
14062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
14072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
14082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
14092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
14102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
14112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
14122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1415d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        class TetherModeAliveState extends TetherMasterUtilState {
141619b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt            boolean mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE;
14172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14194f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt                turnOnMasterTetherSettings(); // may transition us out
14204f74d552d54fd26bc377f333fccb671d20519b3eRobert Greenwalt
142119b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt                mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE; // better try something first pass
142219b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt                                                        // or crazy tests cases will fail
14236a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                chooseUpstreamType(mTryCell);
14246a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                mTryCell = !mTryCell;
14256a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            }
14266a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            @Override
14276a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            public void exit() {
1428ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                turnOffUpstreamMobileConnection();
1429b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                notifyTetheredOfNewUpstreamIface(null);
14302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
1433fd1be2bc1ebfccf9d111dbe3451293d6e31e4af1Robert Greenwalt                if (DBG) Log.d(TAG, "TetherModeAliveState.processMessage what=" + message.what);
14342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
14352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
14362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
14372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
143868ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                        if (VDBG) Log.d(TAG, "Tether Mode requested by " + who);
14392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        mNotifyList.add(who);
1440b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                        who.sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED,
1441b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mUpstreamIfaceName);
14422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
14432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_UNREQUESTED:
14442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        who = (TetherInterfaceSM)message.obj;
144568ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                        if (VDBG) Log.d(TAG, "Tether Mode unrequested by " + who);
14462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        int index = mNotifyList.indexOf(who);
14472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (index != -1) {
144868ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                            if (DBG) Log.d(TAG, "TetherModeAlive removing notifyee " + who);
14492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            mNotifyList.remove(index);
14502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            if (mNotifyList.isEmpty()) {
1451d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                                turnOffMasterTetherSettings(); // transitions appropriately
145268ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                            } else {
145368ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                                if (DBG) {
145468ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                                    Log.d(TAG, "TetherModeAlive still has " + mNotifyList.size() +
145568ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                                            " live requests:");
145668ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                                    for (Object o : mNotifyList) Log.d(TAG, "  " + o);
145768ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                                }
14582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            }
145968ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                        } else {
146068ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                           Log.e(TAG, "TetherModeAliveState UNREQUESTED has unknown who: " + who);
14612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
14622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
14636a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_UPSTREAM_CHANGED:
146405d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                        // need to try DUN immediately if Wifi goes down
146505d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                        mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE;
14666a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        chooseUpstreamType(mTryCell);
14676a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTryCell = !mTryCell;
14682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
14696a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_CELL_CONNECTION_RENEW:
14706a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        // make sure we're still using a requested connection - may have found
14716a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        // wifi or something since then.
14721039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        if (mCurrentConnectionSequence == message.arg1) {
1473c9acde9aa6cf21598640aeebb7d908f1926a48ddWink Saville                            if (VDBG) {
1474924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                                Log.d(TAG, "renewing mobile connection - requeuing for another " +
1475924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                                        CELL_CONNECTION_RENEW_MS + "ms");
1476924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                            }
1477ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                            turnOnUpstreamMobileConnection(mMobileApnReserved);
14786a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        }
147965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        break;
148001758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                    case CMD_RETRY_UPSTREAM:
148101758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                        chooseUpstreamType(mTryCell);
148201758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                        mTryCell = !mTryCell;
148301758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                        break;
148401758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                    default:
148501758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                        retValue = false;
148601758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                        break;
14872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
14882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
14892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
149264c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class ErrorState extends State {
14932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            int mErrorNotification;
14942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
14962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
14972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
14982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
14992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
15006a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        who.sendMessage(mErrorNotification);
15012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
15022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
15032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                       retValue = false;
15042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
15052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
15062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
15072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            void notify(int msgType) {
15082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                mErrorNotification = msgType;
15092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                for (Object o : mNotifyList) {
15102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    TetherInterfaceSM sm = (TetherInterfaceSM)o;
15116a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(msgType);
15122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
15132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
15142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
15152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
15162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetIpForwardingEnabledErrorState extends ErrorState {
15172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
15182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
15192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setIpForwardingEnabled");
15202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_IP_FORWARDING_ENABLE_ERROR);
15212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
15222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
15232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
15242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetIpForwardingDisabledErrorState extends ErrorState {
15252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
15262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
15272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setIpForwardingDisabled");
15282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_IP_FORWARDING_DISABLE_ERROR);
15292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
15302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
15312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
15322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class StartTetheringErrorState extends ErrorState {
15332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
15342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
15352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in startTethering");
15362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_START_TETHERING_ERROR);
15372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
1538c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.setIpForwardingEnabled(false);
15392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
15402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
15412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
15422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
15432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class StopTetheringErrorState extends ErrorState {
15442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
15452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
15462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in stopTethering");
15472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_STOP_TETHERING_ERROR);
15482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
1549c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.setIpForwardingEnabled(false);
15502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
15512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
15522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
15532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
15542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetDnsForwardersErrorState extends ErrorState {
15552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
15562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
15572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setDnsForwarders");
15582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_SET_DNS_FORWARDERS_ERROR);
15592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
1560c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.stopTethering();
15612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
15622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
1563c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.setIpForwardingEnabled(false);
15642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
15652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
1566d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
1567d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
1568d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
15692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
15702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (mContext.checkCallingOrSelfPermission(
15712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
15722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            pw.println("Permission Denial: can't dump ConnectivityService.Tether " +
15732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    "from from pid=" + Binder.getCallingPid() + ", uid=" +
15742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    Binder.getCallingUid());
15752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    return;
15762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
15772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1578b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt        synchronized (mPublicSync) {
1579b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            pw.println("mUpstreamIfaceTypes: ");
1580b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            for (Integer netType : mUpstreamIfaceTypes) {
1581b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt                pw.println(" " + netType);
1582b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            }
158349348e77b69ec8b7ba17072bdce066d6ad842276Robert Greenwalt
1584b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            pw.println();
1585b445362bd67cf596cfdd39db2dbe8f42cf9a119aRobert Greenwalt            pw.println("Tether state:");
15862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object o : mIfaces.values()) {
158768ea9b08ef2d909a063dd78a866926cbb842e9d4Robert Greenwalt                pw.println(" " + o);
15882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
1589d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
15902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        pw.println();
15912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return;
1592d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
1593d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt}
1594