Tethering.java revision 924cc94caaab5e9ad0714e0877e871290316e2b1
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.ContentResolver;
24d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.content.Context;
25d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.content.Intent;
26d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.content.IntentFilter;
272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.content.pm.PackageManager;
28d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.content.res.Resources;
29d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.net.ConnectivityManager;
3065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwaltimport android.net.InterfaceConfiguration;
312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.net.IConnectivityManager;
32d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.net.INetworkManagementEventObserver;
332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.net.NetworkInfo;
34bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwaltimport android.net.NetworkUtils;
351cb3cb1a94342e03b54fabfaf361c9e2e26f23feMike Lockwoodimport android.os.BatteryManager;
362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.os.Binder;
37030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwaltimport android.os.Environment;
387eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwaltimport android.os.HandlerThread;
39d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.os.IBinder;
40d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.os.INetworkManagementService;
41dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwaltimport android.os.Looper;
422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.os.Message;
43d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.os.RemoteException;
44d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.os.ServiceManager;
45d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.provider.Settings;
46d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.util.Log;
47d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport com.android.internal.telephony.Phone;
492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport com.android.internal.util.HierarchicalState;
502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport com.android.internal.util.HierarchicalStateMachine;
512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.io.FileDescriptor;
532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.io.PrintWriter;
54d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport java.util.ArrayList;
552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.util.HashMap;
562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.util.Set;
57d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt/**
58d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * @hide
592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt *
602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt * Timeout
612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt *
622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt * TODO - look for parent classes and code sharing
63d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt */
6465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
65d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltpublic class Tethering extends INetworkManagementEventObserver.Stub {
66d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
67d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    private Context mContext;
68924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt    private final static String TAG = "Tethering";
69924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt    private final static boolean DEBUG = false;
70d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
71c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    private boolean mBooted = false;
72c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    //used to remember if we got connected before boot finished
73c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    private boolean mDeferedUsbConnection = false;
74d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    // TODO - remove both of these - should be part of interface inspection/selection stuff
762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mTetherableUsbRegexs;
772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mTetherableWifiRegexs;
78c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    private String[] mUpstreamIfaceRegexs;
792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
807eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt    private Looper mLooper;
817eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt    private HandlerThread mThread;
82dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt
83030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private HashMap<String, TetherInterfaceSM> mIfaces; // all tethered/tetherable ifaces
84d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
85d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    private BroadcastReceiver mStateReceiver;
86d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
87bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String USB_NEAR_IFACE_ADDR      = "192.168.42.129";
88bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String USB_NETMASK              = "255.255.255.0";
89bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt
90bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    // FYI - the default wifi is 192.168.43.1 and 255.255.255.0
91d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt
922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mDhcpRange;
93bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String DHCP_DEFAULT_RANGE1_START = "192.168.42.2";
94bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String DHCP_DEFAULT_RANGE1_STOP  = "192.168.42.254";
95bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String DHCP_DEFAULT_RANGE2_START = "192.168.43.2";
96bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String DHCP_DEFAULT_RANGE2_STOP  = "192.168.43.254";
972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mDnsServers;
99d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private static final String DNS_DEFAULT_SERVER1 = "8.8.8.8";
100d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private static final String DNS_DEFAULT_SERVER2 = "4.2.2.2";
1012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10277b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt    // resampled each time we turn on tethering - used as cache for settings/config-val
103030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private boolean mDunRequired;  // configuration info - must use DUN apn on 3g
104030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
105a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private HierarchicalStateMachine mTetherMasterSM;
106a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
107a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private Notification mTetheredNotification;
1082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
109030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    // whether we can tether is the && of these two - they come in as separate
110030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    // broadcasts so track them so we can decide what to do when either changes
111030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private boolean mUsbMassStorageOff;  // track the status of USB Mass Storage
112030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private boolean mUsbConnected;       // track the status of USB connection
113030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
114dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt    public Tethering(Context context, Looper looper) {
115d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Log.d(TAG, "Tethering starting");
116d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        mContext = context;
117dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        mLooper = looper;
118d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
119d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        // register for notifications from NetworkManagement Service
120d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
121d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
122d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        try {
123d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            service.registerObserver(this);
124d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        } catch (RemoteException e) {
125d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            Log.e(TAG, "Error registering observer :" + e);
126d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
127d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
1282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mIfaces = new HashMap<String, TetherInterfaceSM>();
129d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
1307eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        // make our own thread so we don't anr the system
1317eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        mThread = new HandlerThread("Tethering");
1327eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        mThread.start();
1337eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        mLooper = mThread.getLooper();
134dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        mTetherMasterSM = new TetherMasterSM("TetherMaster", mLooper);
1352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mTetherMasterSM.start();
1362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
137bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        mStateReceiver = new StateReceiver();
138d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        IntentFilter filter = new IntentFilter();
1391cb3cb1a94342e03b54fabfaf361c9e2e26f23feMike Lockwood        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
1402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
141c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt        filter.addAction(Intent.ACTION_BOOT_COMPLETED);
142bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        mContext.registerReceiver(mStateReceiver, filter);
143bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt
144bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        filter = new IntentFilter();
145030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        filter.addAction(Intent.ACTION_MEDIA_SHARED);
146030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
147bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        filter.addDataScheme("file");
148bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        mContext.registerReceiver(mStateReceiver, filter);
149bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt
150030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        mUsbMassStorageOff = !Environment.MEDIA_SHARED.equals(
151030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                Environment.getExternalStorageState());
1522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mDhcpRange = context.getResources().getStringArray(
1542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                com.android.internal.R.array.config_tether_dhcp_range);
155bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt        if ((mDhcpRange.length == 0) || (mDhcpRange.length % 2 ==1)) {
156bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt            mDhcpRange = new String[4];
157bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt            mDhcpRange[0] = DHCP_DEFAULT_RANGE1_START;
158bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt            mDhcpRange[1] = DHCP_DEFAULT_RANGE1_STOP;
159bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt            mDhcpRange[2] = DHCP_DEFAULT_RANGE2_START;
160bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt            mDhcpRange[3] = DHCP_DEFAULT_RANGE2_STOP;
1612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
16277b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt        mDunRequired = false; // resample when we turn on
1632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mTetherableUsbRegexs = context.getResources().getStringArray(
1652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                com.android.internal.R.array.config_tether_usb_regexs);
1662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mTetherableWifiRegexs = context.getResources().getStringArray(
1672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                com.android.internal.R.array.config_tether_wifi_regexs);
168c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt        mUpstreamIfaceRegexs = context.getResources().getStringArray(
169c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                com.android.internal.R.array.config_tether_upstream_regexs);
1702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // TODO - remove and rely on real notifications of the current iface
1722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mDnsServers = new String[2];
173d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        mDnsServers[0] = DNS_DEFAULT_SERVER1;
174d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        mDnsServers[1] = DNS_DEFAULT_SERVER2;
175d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
176d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
1772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void interfaceLinkStatusChanged(String iface, boolean link) {
178924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) Log.d(TAG, "interfaceLinkStatusChanged " + iface + ", " + link);
1792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        boolean found = false;
18065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean usb = false;
181a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (isWifi(iface)) {
182a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
183a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        } else if (isUsb(iface)) {
184a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
185a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            usb = true;
1862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
1872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (found == false) return;
1882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
1902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
1912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (link) {
1922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm == null) {
193dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt                    sm = new TetherInterfaceSM(iface, mLooper, usb);
1942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    mIfaces.put(iface, sm);
1952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    sm.start();
1962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
1972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            } else {
1982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm != null) {
1996a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(TetherInterfaceSM.CMD_INTERFACE_DOWN);
2002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    mIfaces.remove(iface);
2012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
2022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
2032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
204d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
205d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
206a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private boolean isUsb(String iface) {
207a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        for (String regex : mTetherableUsbRegexs) {
208a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (iface.matches(regex)) return true;
209a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
210a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        return false;
211a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
212a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
213a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    public boolean isWifi(String iface) {
214a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        for (String regex : mTetherableWifiRegexs) {
215a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (iface.matches(regex)) return true;
216a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
217a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        return false;
218a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
219a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
2202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void interfaceAdded(String iface) {
22165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
22265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
2232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        boolean found = false;
22465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean usb = false;
225a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (isWifi(iface)) {
226a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
2272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
228a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (isUsb(iface)) {
229a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
230a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            usb = true;
231d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
2322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (found == false) {
233924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt            if (DEBUG) Log.d(TAG, iface + " is not a tetherable iface, ignoring");
234d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            return;
235d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
23665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
2372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
2382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
2392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (sm != null) {
240924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "active iface (" + iface + ") reported as added, ignoring");
2412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return;
2422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
243dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            sm = new TetherInterfaceSM(iface, mLooper, usb);
2442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaces.put(iface, sm);
2452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm.start();
2462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
247924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) Log.d(TAG, "interfaceAdded :" + iface);
248d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
249d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
2502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void interfaceRemoved(String iface) {
2512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
2522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
2532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (sm == null) {
254924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) {
255924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                    Log.e(TAG, "attempting to remove unknown iface (" + iface + "), ignoring");
256924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                }
2572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return;
2582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
2596a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            sm.sendMessage(TetherInterfaceSM.CMD_INTERFACE_DOWN);
2602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaces.remove(iface);
261d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
262d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
263d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
2645a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int tether(String iface) {
265d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Log.d(TAG, "Tethering " + iface);
2662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        TetherInterfaceSM sm = null;
2672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
2682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm = mIfaces.get(iface);
269d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
2702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm == null) {
2712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Tether an unknown iface :" + iface + ", ignoring");
2725a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
273d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
2745a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        if (!sm.isAvailable() && !sm.isErrored()) {
2752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Tether an unavailable iface :" + iface + ", ignoring");
2765a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
277d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
2786a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        sm.sendMessage(TetherInterfaceSM.CMD_TETHER_REQUESTED);
2795a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return ConnectivityManager.TETHER_ERROR_NO_ERROR;
280d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
281d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
2825a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int untether(String iface) {
283d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Log.d(TAG, "Untethering " + iface);
2842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        TetherInterfaceSM sm = null;
2852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
2862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm = mIfaces.get(iface);
2872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
2882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm == null) {
2892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Untether an unknown iface :" + iface + ", ignoring");
2905a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
291d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
2922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm.isErrored()) {
2932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Untethered an errored iface :" + iface + ", ignoring");
2945a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
295d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
2966a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        sm.sendMessage(TetherInterfaceSM.CMD_TETHER_UNREQUESTED);
2975a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return ConnectivityManager.TETHER_ERROR_NO_ERROR;
2985a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
2995a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
3005a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int getLastTetherError(String iface) {
3015a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        TetherInterfaceSM sm = null;
3025a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        synchronized (mIfaces) {
3035a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            sm = mIfaces.get(iface);
3045a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
3055a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        if (sm == null) {
3065a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            Log.e(TAG, "Tried to getLastTetherError on an unknown iface :" + iface + ", ignoring");
3075a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
3085a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
3095a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return sm.getLastError();
3102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
311d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private void sendTetherStateChangedBroadcast() {
3132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
3142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
315d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        try {
3162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (!service.isTetheringSupported()) return;
3172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        } catch (RemoteException e) {
3182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return;
319d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
320d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> availableList = new ArrayList<String>();
3222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> activeList = new ArrayList<String>();
3232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> erroredList = new ArrayList<String>();
3242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
325a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        boolean wifiTethered = false;
326a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        boolean usbTethered = false;
327a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
3282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
3292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set ifaces = mIfaces.keySet();
3302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object iface : ifaces) {
3312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(iface);
3322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm != null) {
3332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    if(sm.isErrored()) {
3342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        erroredList.add((String)iface);
3352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    } else if (sm.isAvailable()) {
3362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        availableList.add((String)iface);
3372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    } else if (sm.isTethered()) {
338a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        if (isUsb((String)iface)) {
339a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            usbTethered = true;
340a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        } else if (isWifi((String)iface)) {
341a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            wifiTethered = true;
342a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        }
3432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        activeList.add((String)iface);
3442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    }
3452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
346d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            }
347d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
348d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Intent broadcast = new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
349d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        broadcast.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
3502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_AVAILABLE_TETHER,
3512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                availableList);
3522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ACTIVE_TETHER, activeList);
3532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ERRORED_TETHER,
3542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                erroredList);
3552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mContext.sendStickyBroadcast(broadcast);
356924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) {
357924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt            Log.d(TAG, "sendTetherStateChangedBroadcast " + availableList.size() + ", " +
358924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                    activeList.size() + ", " + erroredList.size());
359924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        }
360a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
361a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (usbTethered) {
362a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (wifiTethered) {
363a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general);
364a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            } else {
365a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_usb);
366a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            }
367a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        } else if (wifiTethered) {
368a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_wifi);
369a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        } else {
370a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            clearTetheredNotification();
371a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
372a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
373a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
374a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private void showTetheredNotification(int icon) {
375a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        NotificationManager notificationManager =
376a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
377a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (notificationManager == null) {
378a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            return;
379a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
380a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
381db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt        if (mTetheredNotification != null) {
382db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            if (mTetheredNotification.icon == icon) {
383db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt                return;
384db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            }
385db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            notificationManager.cancel(mTetheredNotification.icon);
386db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt        }
387db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt
388a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        Intent intent = new Intent();
389a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        intent.setClassName("com.android.settings", "com.android.settings.TetherSettings");
390a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
391a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
392a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);
393a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
394a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        Resources r = Resources.getSystem();
395a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        CharSequence title = r.getText(com.android.internal.R.string.tethered_notification_title);
396a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        CharSequence message = r.getText(com.android.internal.R.string.
397a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                tethered_notification_message);
398a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
399a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if(mTetheredNotification == null) {
400a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification = new Notification();
401a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification.when = 0;
402a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
403a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.icon = icon;
404a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.defaults &= ~Notification.DEFAULT_SOUND;
405a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.flags = Notification.FLAG_ONGOING_EVENT;
406a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.tickerText = title;
407a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.setLatestEventInfo(mContext, title, message, pi);
408a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
409a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        notificationManager.notify(mTetheredNotification.icon, mTetheredNotification);
410a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
411a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
412a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private void clearTetheredNotification() {
413a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        NotificationManager notificationManager =
414a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
415a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (notificationManager != null && mTetheredNotification != null) {
416a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            notificationManager.cancel(mTetheredNotification.icon);
417a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification = null;
418a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
4195a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
4205a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
421030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private void updateUsbStatus() {
422030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        boolean enable = mUsbConnected && mUsbMassStorageOff;
423030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
424030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        if (mBooted) {
425030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            enableUsbIfaces(enable);
426030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        }
427030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    }
428030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
4292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private class StateReceiver extends BroadcastReceiver {
430d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        public void onReceive(Context content, Intent intent) {
4312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            String action = intent.getAction();
4321cb3cb1a94342e03b54fabfaf361c9e2e26f23feMike Lockwood            if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
433030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                mUsbConnected = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)
4341cb3cb1a94342e03b54fabfaf361c9e2e26f23feMike Lockwood                        == BatteryManager.BATTERY_PLUGGED_USB);
435030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                Tethering.this.updateUsbStatus();
436030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
437030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                mUsbMassStorageOff = false;
438030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                updateUsbStatus();
439030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            }
440030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
441030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                mUsbMassStorageOff = true;
442030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                updateUsbStatus();
4432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
444924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "Tethering got CONNECTIVITY_ACTION");
4456a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED);
446c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt            } else if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
447c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                mBooted = true;
448030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                updateUsbStatus();
449d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            }
450d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
451d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
452d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
45365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    // used on cable insert/remove
454d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private void enableUsbIfaces(boolean enable) {
45565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
45665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
45765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        String[] ifaces = new String[0];
45865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        try {
45965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            ifaces = service.listInterfaces();
46065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        } catch (Exception e) {
46165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            Log.e(TAG, "Error listing Interfaces :" + e);
46265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            return;
46365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
46465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        for (String iface : ifaces) {
465a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (isUsb(iface)) {
466a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                if (enable) {
467a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    interfaceAdded(iface);
468a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                } else {
469a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    interfaceRemoved(iface);
47065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
47165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
47265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
47365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    }
47465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
47565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    // toggled when we enter/leave the fully teathered state
476d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private boolean enableUsbRndis(boolean enabled) {
477924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) Log.d(TAG, "enableUsbRndis(" + enabled + ")");
47865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
47965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
48065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
48165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        try {
48265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            if (enabled) {
48365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                synchronized (this) {
48465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    if (!service.isUsbRNDISStarted()) {
48565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        service.startUsbRNDIS();
48665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    }
48765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
48865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            } else {
48965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                if (service.isUsbRNDISStarted()) {
49065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    service.stopUsbRNDIS();
49165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
49265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
49365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        } catch (Exception e) {
49465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            Log.e(TAG, "Error toggling usb RNDIS :" + e);
49565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            return false;
49665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
49765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        return true;
49865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    }
49965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
50065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    // configured when we start tethering and unconfig'd on error or conclusion
501d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private boolean configureUsbIface(boolean enabled) {
502924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) Log.d(TAG, "configureUsbIface(" + enabled + ")");
50365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
50465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
50565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
50665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
50765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        // bring toggle the interfaces
50865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        String[] ifaces = new String[0];
50965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        try {
51065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            ifaces = service.listInterfaces();
51165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        } catch (Exception e) {
51265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            Log.e(TAG, "Error listing Interfaces :" + e);
51365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            return false;
51465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
51565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        for (String iface : ifaces) {
516a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (isUsb(iface)) {
517a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                InterfaceConfiguration ifcg = null;
518a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                try {
519a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    ifcg = service.getInterfaceConfig(iface);
520a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    if (ifcg != null) {
521bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt                        String[] addr = USB_NEAR_IFACE_ADDR.split("\\.");
522bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt                        ifcg.ipAddr = (Integer.parseInt(addr[0]) << 24) +
523bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt                                (Integer.parseInt(addr[1]) << 16) +
524bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt                                (Integer.parseInt(addr[2]) << 8) +
525bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt                                (Integer.parseInt(addr[3]));
526bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt                        addr = USB_NETMASK.split("\\.");
527bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt                        ifcg.netmask = (Integer.parseInt(addr[0]) << 24) +
528bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt                                (Integer.parseInt(addr[1]) << 16) +
529bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt                                (Integer.parseInt(addr[2]) << 8) +
530bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt                                (Integer.parseInt(addr[3]));
531a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        if (enabled) {
532a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            ifcg.interfaceFlags = ifcg.interfaceFlags.replace("down", "up");
533a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        } else {
534a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            ifcg.interfaceFlags = ifcg.interfaceFlags.replace("up", "down");
53565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
536a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        ifcg.interfaceFlags = ifcg.interfaceFlags.replace("running", "");
537a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        ifcg.interfaceFlags = ifcg.interfaceFlags.replace("  "," ");
538a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        service.setInterfaceConfig(iface, ifcg);
53965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    }
540a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                } catch (Exception e) {
541a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    Log.e(TAG, "Error configuring interface " + iface + ", :" + e);
542a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    return false;
54365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
54465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
54565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
54665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
54765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        return true;
54865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    }
54965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
5502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetherableUsbRegexs() {
5512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return mTetherableUsbRegexs;
5522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
5532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
5542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetherableWifiRegexs() {
5552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return mTetherableWifiRegexs;
5562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
5572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
558c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    public String[] getUpstreamIfaceRegexs() {
559c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt        return mUpstreamIfaceRegexs;
560c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    }
561c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt
562c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    public boolean isDunRequired() {
56377b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt        boolean defaultVal = mContext.getResources().getBoolean(
56477b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt                com.android.internal.R.bool.config_tether_dun_required);
56577b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt        boolean result = (Settings.Secure.getInt(mContext.getContentResolver(),
56677b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt                Settings.Secure.TETHER_DUN_REQUIRED, (defaultVal ? 1 : 0)) == 1);
56777b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt        return result;
568c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    }
569c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt
5702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetheredIfaces() {
5712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> list = new ArrayList<String>();
5722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
5732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set keys = mIfaces.keySet();
5742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object key : keys) {
5752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
5762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm.isTethered()) {
5772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    list.add((String)key);
5782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
5792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
5802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
5812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String[] retVal = new String[list.size()];
5822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        for (int i=0; i < list.size(); i++) {
5832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            retVal[i] = list.get(i);
5842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
5852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return retVal;
5862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
5872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
5882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetherableIfaces() {
5892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> list = new ArrayList<String>();
5902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
5912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set keys = mIfaces.keySet();
5922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object key : keys) {
5932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
5942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm.isAvailable()) {
5952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    list.add((String)key);
5962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
5972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
5982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
5992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String[] retVal = new String[list.size()];
6002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        for (int i=0; i < list.size(); i++) {
6012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            retVal[i] = list.get(i);
6022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return retVal;
6042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
6052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6065a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public String[] getErroredIfaces() {
6075a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        ArrayList<String> list = new ArrayList<String>();
6085a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        synchronized (mIfaces) {
6095a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            Set keys = mIfaces.keySet();
6105a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            for (Object key : keys) {
6115a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
6125a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                if (sm.isErrored()) {
6135a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    list.add((String)key);
6145a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                }
6155a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            }
6165a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
6175a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        String[] retVal = new String[list.size()];
6185a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        for (int i= 0; i< list.size(); i++) {
6195a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            retVal[i] = list.get(i);
6205a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
6215a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return retVal;
6225a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
6235a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
6242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    class TetherInterfaceSM extends HierarchicalStateMachine {
6262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it's not in tether mode
6276a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_MODE_DEAD            =  1;
6282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // request from the user that it wants to tether
6296a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_REQUESTED            =  2;
6302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // request from the user that it wants to untether
6316a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_UNREQUESTED          =  3;
6322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification that this interface is down
6336a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_INTERFACE_DOWN              =  4;
6342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification that this interface is up
6356a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_INTERFACE_UP                =  5;
6362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had an error turning on cellular dun
6376a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_CELL_DUN_ERROR              =  6;
6382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble enabling IP Forwarding
6396a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_IP_FORWARDING_ENABLE_ERROR  =  7;
6402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble disabling IP Forwarding
6416a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_IP_FORWARDING_DISABLE_ERROR =  8;
6422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble staring tethering
6436a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_START_TETHERING_ERROR       =  9;
6442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble stopping tethering
6456a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_STOP_TETHERING_ERROR        = 10;
6462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble setting the DNS forwarders
6476a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_SET_DNS_FORWARDERS_ERROR    = 11;
6486a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // the upstream connection has changed
649951749ba2e014566553d17c512e947472951a060Wink Saville        static final int CMD_TETHER_CONNECTION_CHANGED   = 12;
6502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mDefaultState;
6522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mInitialState;
6542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mStartingState;
6552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mTetheredState;
6562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mUnavailableState;
6582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private boolean mAvailable;
6602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private boolean mTethered;
6615a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        int mLastError;
6622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String mIfaceName;
6646a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        String mMyUpstreamIfaceName;  // may change over time
6656a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
66665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean mUsb;
6672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
668dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        TetherInterfaceSM(String name, Looper looper, boolean usb) {
669dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            super(name, looper);
6702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaceName = name;
67165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            mUsb = usb;
6725a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
6732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mInitialState = new InitialState();
6752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mInitialState);
6762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStartingState = new StartingState();
6772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStartingState);
6782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mTetheredState = new TetheredState();
6792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mTetheredState);
6802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mUnavailableState = new UnavailableState();
6812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mUnavailableState);
6822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            setInitialState(mInitialState);
6842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public String toString() {
6872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            String res = new String();
6882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            res += mIfaceName + " - ";
6892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            HierarchicalState current = getCurrentState();
6902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mInitialState) res += "InitialState";
6912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mStartingState) res += "StartingState";
6922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mTetheredState) res += "TetheredState";
6932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mUnavailableState) res += "UnavailableState";
6942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (mAvailable) res += " - Available";
6952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (mTethered) res += " - Tethered";
6965a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            res += " - lastError =" + mLastError;
6972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return res;
6982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7005a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        public synchronized int getLastError() {
7015a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return mLastError;
7025a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
7035a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
7045a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        private synchronized void setLastError(int error) {
7055a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            mLastError = error;
7065a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
7075a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            if (isErrored()) {
7085a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                if (mUsb) {
7095a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    // note everything's been unwound by this point so nothing to do on
7105a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    // further error..
7115a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    Tethering.this.configureUsbIface(false);
7125a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                }
7135a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            }
7145a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
7155a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
7162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // synchronized between this getter and the following setter
7172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public synchronized boolean isAvailable() {
7182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return mAvailable;
7192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private synchronized void setAvailable(boolean available) {
7222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mAvailable = available;
7232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // synchronized between this getter and the following setter
7262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public synchronized boolean isTethered() {
7272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return mTethered;
7282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private synchronized void setTethered(boolean tethered) {
7312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mTethered = tethered;
7322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // synchronized between this getter and the following setter
7352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public synchronized boolean isErrored() {
7365a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return (mLastError != ConnectivityManager.TETHER_ERROR_NO_ERROR);
7372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class InitialState extends HierarchicalState {
7402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
7412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
7422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(true);
7432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(false);
7442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
7452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
7462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
7482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
749924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "InitialState.processMessage what=" + message.what);
7502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
7512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
7522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_REQUESTED:
7535a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
7546a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_REQUESTED,
7556a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
7562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mStartingState);
7572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
7582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
7592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mUnavailableState);
7602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
7612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
7622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
7632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
7642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
7652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
7662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
7672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class StartingState extends HierarchicalState {
7702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
7712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
7722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
77365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                if (mUsb) {
774d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    if (!Tethering.this.configureUsbIface(true)) {
7756a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
7766a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
7775a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastError(ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
7785a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
779951749ba2e014566553d17c512e947472951a060Wink Saville                        transitionTo(mInitialState);
78065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        return;
78165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    }
78265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
7832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
7846a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
785951749ba2e014566553d17c512e947472951a060Wink Saville                // Skipping StartingState
786951749ba2e014566553d17c512e947472951a060Wink Saville                transitionTo(mTetheredState);
7872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
7882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
7892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
790924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "StartingState.processMessage what=" + message.what);
7912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
7922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
7932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    // maybe a parent class?
7942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_UNREQUESTED:
7956a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
7966a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
79765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        if (mUsb) {
798d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                            if (!Tethering.this.configureUsbIface(false)) {
7995a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                setLastErrorAndTransitionToInitialState(
8005a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
80165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                                break;
80265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
80365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
8042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
8052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_CELL_DUN_ERROR:
8072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_ENABLE_ERROR:
8082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_DISABLE_ERROR:
8092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_START_TETHERING_ERROR:
8102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_STOP_TETHERING_ERROR:
8112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_SET_DNS_FORWARDERS_ERROR:
8125a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastErrorAndTransitionToInitialState(
8135a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                ConnectivityManager.TETHER_ERROR_MASTER_ERROR);
8142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
8166a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
8176a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
8182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mUnavailableState);
8192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
8212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
8222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
8232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
8242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
8272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class TetheredState extends HierarchicalState {
8282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
8292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
8302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
8312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                INetworkManagementService service =
8322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        INetworkManagementService.Stub.asInterface(b);
8332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
8342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    service.tetherInterface(mIfaceName);
8352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {
8365a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    setLastError(ConnectivityManager.TETHER_ERROR_TETHER_IFACE_ERROR);
8375a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
838951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mInitialState);
8392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    return;
8402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
841d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                if (mUsb) Tethering.this.enableUsbRndis(true);
842924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "Tethered " + mIfaceName);
8432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
8442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(true);
8452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
8462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
84865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            public void exit() {
849d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                if (mUsb) Tethering.this.enableUsbRndis(false);
85065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
85165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            @Override
8522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
853924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "TetheredState.processMessage what=" + message.what);
8542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
8552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean error = false;
8562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
8572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_UNREQUESTED:
8582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
8592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
8602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        INetworkManagementService service =
8612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                                INetworkManagementService.Stub.asInterface(b);
8626a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (mMyUpstreamIfaceName != null) {
8635a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            try {
8646a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                service.disableNat(mIfaceName, mMyUpstreamIfaceName);
865b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mMyUpstreamIfaceName = null;
8666a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
8676a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
8686a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                    service.untetherInterface(mIfaceName);
8696a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
8705a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
8716a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastErrorAndTransitionToInitialState(
8726a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                        ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR);
8736a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                break;
8746a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
8752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
8762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        try {
8772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            service.untetherInterface(mIfaceName);
8782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } catch (Exception e) {
8795a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
8805a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR);
8812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
8822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
8836a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
8846a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
8852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (message.what == CMD_TETHER_UNREQUESTED) {
88665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            if (mUsb) {
887d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                                if (!Tethering.this.configureUsbIface(false)) {
8885a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    setLastError(
8895a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                            ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
89065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                                }
89165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
8925a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            transitionTo(mInitialState);
8932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } else if (message.what == CMD_INTERFACE_DOWN) {
8942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            transitionTo(mUnavailableState);
8952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
896924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                        if (DEBUG) Log.d(TAG, "Untethered " + mIfaceName);
8972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8986a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_TETHER_CONNECTION_CHANGED:
8996a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        String newUpstreamIfaceName = (String)(message.obj);
9006a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
9016a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        service = INetworkManagementService.Stub.asInterface(b);
9026a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
9036a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (mMyUpstreamIfaceName != null) {
9046a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            try {
9056a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                service.disableNat(mIfaceName, mMyUpstreamIfaceName);
906b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mMyUpstreamIfaceName = null;
9076a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
9086a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
9096a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                    service.untetherInterface(mIfaceName);
9106a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
9116a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
9126a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastErrorAndTransitionToInitialState(
9136a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                        ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR);
9146a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                break;
9156a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
9166a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        }
9176a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (newUpstreamIfaceName != null) {
9186a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            try {
9196a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                service.enableNat(mIfaceName, newUpstreamIfaceName);
9206a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
9216a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
9226a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                    service.untetherInterface(mIfaceName);
9236a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
9246a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
9256a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastError(ConnectivityManager.TETHER_ERROR_ENABLE_NAT_ERROR);
9266a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                transitionTo(mInitialState);
9276a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                return true;
9286a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
9296a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        }
9306a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mMyUpstreamIfaceName = newUpstreamIfaceName;
9316a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        break;
9322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_CELL_DUN_ERROR:
9332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_ENABLE_ERROR:
9342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_DISABLE_ERROR:
9352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_START_TETHERING_ERROR:
9362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_STOP_TETHERING_ERROR:
9372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_SET_DNS_FORWARDERS_ERROR:
9382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        error = true;
9392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        // fall through
9402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_DEAD:
9412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
9422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        service = INetworkManagementService.Stub.asInterface(b);
9436a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (mMyUpstreamIfaceName != null) {
9445a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            try {
9456a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                service.disableNat(mIfaceName, mMyUpstreamIfaceName);
946b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mMyUpstreamIfaceName = null;
9476a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
9486a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
9496a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                    service.untetherInterface(mIfaceName);
9506a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
9515a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
9526a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastErrorAndTransitionToInitialState(
9536a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                        ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR);
9546a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                break;
9556a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
9562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
9572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        try {
9582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            service.untetherInterface(mIfaceName);
9592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } catch (Exception e) {
9605a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
9615a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR);
9622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
9632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
9642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (error) {
9655a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
9665a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_MASTER_ERROR);
9672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
9682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
969924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                        if (DEBUG) Log.d(TAG, "Tether lost upstream connection " + mIfaceName);
9702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        sendTetherStateChangedBroadcast();
97165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        if (mUsb) {
972d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                            if (!Tethering.this.configureUsbIface(false)) {
9735a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                setLastError(ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
97465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
97565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
9762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
9772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
9782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
9792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
9802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
9812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
9822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
9832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
9842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
9852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
9862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class UnavailableState extends HierarchicalState {
9872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
9882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
9892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
9905a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
9912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(false);
9922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
9932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
9942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
9952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
9962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
9972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
9982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_UP:
9992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
10002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
10022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
10032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
10052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
10062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
10072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
10082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10095a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        void setLastErrorAndTransitionToInitialState(int error) {
10105a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            setLastError(error);
10115a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            transitionTo(mInitialState);
10122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
101365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
10142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
10152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    class TetherMasterSM extends HierarchicalStateMachine {
10172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // an interface SM has requested Tethering
10182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        static final int CMD_TETHER_MODE_REQUESTED   = 1;
10192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // an interface SM has unrequested Tethering
10202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        static final int CMD_TETHER_MODE_UNREQUESTED = 2;
10216a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // upstream connection change - do the right thing
10226a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_UPSTREAM_CHANGED        = 3;
10232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // we received notice that the cellular DUN connection is up
10246a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_CELL_CONNECTION_RENEW   = 4;
10256a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // we don't have a valid upstream conn, check again after a delay
1026951749ba2e014566553d17c512e947472951a060Wink Saville        static final int CMD_RETRY_UPSTREAM          = 5;
10272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // This indicates what a timeout event relates to.  A state that
10292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // sends itself a delayed timeout event and handles incoming timeout events
10302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // should inc this when it is entered and whenever it sends a new timeout event.
10312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // We do not flush the old ones.
10322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private int mSequenceNumber;
10332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mInitialState;
10352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mTetherModeAliveState;
10362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mSetIpForwardingEnabledErrorState;
10382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mSetIpForwardingDisabledErrorState;
10392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mStartTetheringErrorState;
10402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mStopTetheringErrorState;
10412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mSetDnsForwardersErrorState;
10422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private ArrayList mNotifyList;
10442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10456a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        private boolean mConnectionRequested = false;
10462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1047b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt        private String mUpstreamIfaceName = null;
1048b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt
10496a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        private static final int UPSTREAM_SETTLE_TIME_MS     = 10000;
10506a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        private static final int CELL_CONNECTION_RENEW_MS    = 40000;
10512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1052dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        TetherMasterSM(String name, Looper looper) {
1053dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            super(name, looper);
10542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            //Add states
10562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mInitialState = new InitialState();
10572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mInitialState);
10582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mTetherModeAliveState = new TetherModeAliveState();
10592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mTetherModeAliveState);
10602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetIpForwardingEnabledErrorState = new SetIpForwardingEnabledErrorState();
10622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetIpForwardingEnabledErrorState);
10632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetIpForwardingDisabledErrorState = new SetIpForwardingDisabledErrorState();
10642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetIpForwardingDisabledErrorState);
10652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStartTetheringErrorState = new StartTetheringErrorState();
10662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStartTetheringErrorState);
10672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStopTetheringErrorState = new StopTetheringErrorState();
10682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStopTetheringErrorState);
10692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetDnsForwardersErrorState = new SetDnsForwardersErrorState();
10702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetDnsForwardersErrorState);
10712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mNotifyList = new ArrayList();
10732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            setInitialState(mInitialState);
10742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
10752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1076d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        class TetherMasterUtilState extends HierarchicalState {
10776a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected final static boolean TRY_TO_SETUP_MOBILE_CONNECTION = true;
10786a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected final static boolean WAIT_FOR_NETWORK_TO_SETTLE     = false;
10796a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
1080d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            @Override
1081d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            public boolean processMessage(Message m) {
1082d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return false;
1083d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
10846a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected int turnOnMobileConnection() {
1085d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
1086d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
1087d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                int retValue = Phone.APN_REQUEST_FAILED;
1088d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1089d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    retValue = service.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
109077e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                            (mDunRequired ? Phone.FEATURE_ENABLE_DUN : Phone.FEATURE_ENABLE_HIPRI),
1091c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            new Binder());
1092d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1093d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
10946a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                switch (retValue) {
10956a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                case Phone.APN_ALREADY_ACTIVE:
10966a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                case Phone.APN_REQUEST_STARTED:
10976a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sendMessageDelayed(CMD_CELL_CONNECTION_RENEW, CELL_CONNECTION_RENEW_MS);
10986a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    mConnectionRequested = true;
10996a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    break;
11006a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                case Phone.APN_REQUEST_FAILED:
11016a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                default:
11026a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    mConnectionRequested = false;
11036a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    break;
11046a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
11056a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
1106d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return retValue;
1107d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
11086a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected boolean turnOffMobileConnection() {
11096a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                if (mConnectionRequested) {
11106a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
11116a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    IConnectivityManager service =
11126a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            IConnectivityManager.Stub.asInterface(b);
11136a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    try {
11146a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        service.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
111577e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                (mDunRequired? Phone.FEATURE_ENABLE_DUN :
111677e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                             Phone.FEATURE_ENABLE_HIPRI));
11176a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    } catch (Exception e) {
11186a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        return false;
11196a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    }
11206a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    mConnectionRequested = false;
1121d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1122d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1123d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
11246a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected boolean turnOnMasterTetherSettings() {
1125d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1126d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                INetworkManagementService service =
1127d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                        INetworkManagementService.Stub.asInterface(b);
1128d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1129d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    service.setIpForwardingEnabled(true);
1130d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1131951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mSetIpForwardingEnabledErrorState);
1132d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1133d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1134d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1135bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt                    service.startTethering(mDhcpRange);
1136d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1137951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mStartTetheringErrorState);
1138d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1139d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1140d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1141d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    service.setDnsForwarders(mDnsServers);
1142d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1143951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mSetDnsForwardersErrorState);
1144d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1145d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1146d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1147d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
11486a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected boolean turnOffMasterTetherSettings() {
1149d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1150d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                INetworkManagementService service =
1151d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                        INetworkManagementService.Stub.asInterface(b);
1152d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1153d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    service.stopTethering();
1154d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1155d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    transitionTo(mStopTetheringErrorState);
1156d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1157d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1158d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1159d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    service.setIpForwardingEnabled(false);
1160d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1161d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    transitionTo(mSetIpForwardingDisabledErrorState);
1162d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1163d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1164d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                transitionTo(mInitialState);
1165d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1166d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
11676a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected String findActiveUpstreamIface() {
1168c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                // check for what iface we can use - if none found switch to error.
1169c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1170c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
1171c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt
1172c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                String[] ifaces = new String[0];
1173c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                try {
1174c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    ifaces = service.listInterfaces();
1175c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                } catch (Exception e) {
1176c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    Log.e(TAG, "Error listing Interfaces :" + e);
1177c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    return null;
1178c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                }
11796a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
1180c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                for (String iface : ifaces) {
1181c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    for (String regex : mUpstreamIfaceRegexs) {
1182c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                        if (iface.matches(regex)) {
1183c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            // verify it is up!
1184c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            InterfaceConfiguration ifcg = null;
1185c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            try {
1186c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                                ifcg = service.getInterfaceConfig(iface);
1187c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            } catch (Exception e) {
1188c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                                Log.e(TAG, "Error getting iface config :" + e);
1189c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                                // ignore - try next
1190c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                                continue;
1191c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            }
1192c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            if (ifcg.interfaceFlags.contains("up")) {
1193c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                                return iface;
1194c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            }
1195c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                        }
1196c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    }
1197c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                }
1198c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                return null;
1199c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt            }
12006a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected void chooseUpstreamType(boolean tryCell) {
12016a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                // decide if the current upstream is good or not and if not
12026a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                // do something about it (start up DUN if required or HiPri if not)
12036a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                String iface = findActiveUpstreamIface();
12046a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
12056a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b);
12066a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                mConnectionRequested = false;
1207924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) {
1208924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                    Log.d(TAG, "chooseUpstreamType(" + tryCell + "),  dunRequired ="
1209924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                            + mDunRequired + ", iface=" + iface);
1210924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                }
121177e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                if (iface != null) {
12126a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    try {
121377e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                        if (mDunRequired) {
121477e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                            // check if Dun is on - we can use that
121577e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                            NetworkInfo info = cm.getNetworkInfo(
121677e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                    ConnectivityManager.TYPE_MOBILE_DUN);
121777e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                            if (info.isConnected()) {
1218924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                                if (DEBUG) Log.d(TAG, "setting dun ifacename =" + iface);
121977e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                // even if we're already connected - it may be somebody else's
122077e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                // refcount, so add our own
122177e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                turnOnMobileConnection();
122277e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                            } else {
122377e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                // verify the iface is not the default mobile - can't use that!
122477e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                info = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
122577e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                if (info.isConnected()) {
122677e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                    iface = null; // can't accept this one
122777e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                }
122877e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                            }
122977e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                        } else {
1230924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                            if (DEBUG) Log.d(TAG, "checking if hipri brought us this connection");
12316a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            NetworkInfo info = cm.getNetworkInfo(
12326a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                    ConnectivityManager.TYPE_MOBILE_HIPRI);
12336a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            if (info.isConnected()) {
1234924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                                if (DEBUG) Log.d(TAG, "yes - hipri in use");
123577e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                // even if we're already connected - it may be sombody else's
123677e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                // refcount, so add our own
12376a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                turnOnMobileConnection();
12382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            }
12392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
124077e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                    } catch (RemoteException e) {
124177e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                        Log.e(TAG, "RemoteException calling ConnectivityManager " + e);
124277e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                        iface = null;
12436a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    }
12446a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
124577e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                // may have been set to null in the if above
124677e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                if (iface == null ) {
124777e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                    if (tryCell == TRY_TO_SETUP_MOBILE_CONNECTION) {
124877e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                        turnOnMobileConnection();
124977e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                    }
12506a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    // wait for things to settle and retry
12516a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sendMessageDelayed(CMD_RETRY_UPSTREAM, UPSTREAM_SETTLE_TIME_MS);
12526a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
1253b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                notifyTetheredOfNewUpstreamIface(iface);
12546a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            }
1255b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt            protected void notifyTetheredOfNewUpstreamIface(String ifaceName) {
1256924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "notifying tethered with iface =" + ifaceName);
1257b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                mUpstreamIfaceName = ifaceName;
12586a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                for (Object o : mNotifyList) {
12596a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    TetherInterfaceSM sm = (TetherInterfaceSM)o;
12606a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED,
12616a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            ifaceName);
12622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
12632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
12642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
12652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
12666a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        class InitialState extends TetherMasterUtilState {
12672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
12682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
12696a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                mConnectionRequested = false;
12702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
12712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
12722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
1273924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "MasterInitialState.processMessage what=" + message.what);
12742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
12752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
12762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
127777b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt                        mDunRequired = isDunRequired();
12782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
1279924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                        if (DEBUG) Log.d(TAG, "Tether Mode requested by " + who.toString());
12802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        mNotifyList.add(who);
12816a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        transitionTo(mTetherModeAliveState);
12822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
12832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_UNREQUESTED:
12842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        who = (TetherInterfaceSM)message.obj;
1285924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                        if (DEBUG) Log.d(TAG, "Tether Mode unrequested by " + who.toString());
12862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        int index = mNotifyList.indexOf(who);
12872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (index != -1) {
12886a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            mNotifyList.remove(who);
12892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
12902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
12912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
12922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
12932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
12942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
12952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
12962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
12972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
12982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1299d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        class TetherModeAliveState extends TetherMasterUtilState {
13006a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            boolean mTryCell = WAIT_FOR_NETWORK_TO_SETTLE;
13012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
13036a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                mTryCell = WAIT_FOR_NETWORK_TO_SETTLE;  // first pass lets just see what we have.
13046a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                chooseUpstreamType(mTryCell);
13056a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                mTryCell = !mTryCell;
13066a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                turnOnMasterTetherSettings(); // may transition us out
13076a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            }
13086a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            @Override
13096a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            public void exit() {
13106a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                turnOffMobileConnection();
1311b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                notifyTetheredOfNewUpstreamIface(null);
13122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
1315924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "TetherModeAliveState.processMessage what=" + message.what);
13162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
13172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
13182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
13192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
13202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        mNotifyList.add(who);
1321b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                        who.sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED,
1322b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mUpstreamIfaceName);
13232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_UNREQUESTED:
13252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        who = (TetherInterfaceSM)message.obj;
13262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        int index = mNotifyList.indexOf(who);
13272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (index != -1) {
13282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            mNotifyList.remove(index);
13292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            if (mNotifyList.isEmpty()) {
1330d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                                turnOffMasterTetherSettings(); // transitions appropriately
13312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            }
13322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
13332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13346a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_UPSTREAM_CHANGED:
13356a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTryCell = WAIT_FOR_NETWORK_TO_SETTLE;
13366a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        chooseUpstreamType(mTryCell);
13376a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTryCell = !mTryCell;
13382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13396a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_CELL_CONNECTION_RENEW:
13406a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        // make sure we're still using a requested connection - may have found
13416a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        // wifi or something since then.
13426a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (mConnectionRequested) {
1343924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                            if (DEBUG) {
1344924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                                Log.d(TAG, "renewing mobile connection - requeuing for another " +
1345924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                                        CELL_CONNECTION_RENEW_MS + "ms");
1346924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                            }
13476a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            turnOnMobileConnection();
13486a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        }
134965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        break;
13506a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                   case CMD_RETRY_UPSTREAM:
13516a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                       chooseUpstreamType(mTryCell);
13526a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                       mTryCell = !mTryCell;
13536a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                       break;
13546a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                   default:
13552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                       retValue = false;
13562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                       break;
13572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
13582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
13592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
13612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
13622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class ErrorState extends HierarchicalState {
13632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            int mErrorNotification;
13642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
13662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
13672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
13682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
13692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
13706a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        who.sendMessage(mErrorNotification);
13712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
13732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                       retValue = false;
13742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
13752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
13762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            void notify(int msgType) {
13782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                mErrorNotification = msgType;
13792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                for (Object o : mNotifyList) {
13802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    TetherInterfaceSM sm = (TetherInterfaceSM)o;
13816a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(msgType);
13822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
13832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
13852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
13862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetIpForwardingEnabledErrorState extends ErrorState {
13872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
13892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setIpForwardingEnabled");
13902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_IP_FORWARDING_ENABLE_ERROR);
13912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
13932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
13942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetIpForwardingDisabledErrorState extends ErrorState {
13952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
13972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setIpForwardingDisabled");
13982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_IP_FORWARDING_DISABLE_ERROR);
13992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class StartTetheringErrorState extends ErrorState {
14032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in startTethering");
14062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_START_TETHERING_ERROR);
14072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
14082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                INetworkManagementService service =
14092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        INetworkManagementService.Stub.asInterface(b);
14102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
14112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    service.setIpForwardingEnabled(false);
14122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
14132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class StopTetheringErrorState extends ErrorState {
14172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in stopTethering");
14202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_STOP_TETHERING_ERROR);
14212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
14222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                INetworkManagementService service =
14232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                         INetworkManagementService.Stub.asInterface(b);
14242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
14252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    service.setIpForwardingEnabled(false);
14262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
14272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetDnsForwardersErrorState extends ErrorState {
14312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setDnsForwarders");
14342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_SET_DNS_FORWARDERS_ERROR);
14352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
14362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                INetworkManagementService service =
14372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        INetworkManagementService.Stub.asInterface(b);
14382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
14392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    service.stopTethering();
14402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
14412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
14422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    service.setIpForwardingEnabled(false);
14432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
14442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
1445d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
1446d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
1447d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
14482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
14492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (mContext.checkCallingOrSelfPermission(
14502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
14512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            pw.println("Permission Denial: can't dump ConnectivityService.Tether " +
14522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    "from from pid=" + Binder.getCallingPid() + ", uid=" +
14532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    Binder.getCallingUid());
14542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    return;
14552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        pw.println();
14582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        pw.println("Tether state:");
14592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
14602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object o : mIfaces.values()) {
14612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                pw.println(" "+o.toString());
14622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
1463d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
14642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        pw.println();
14652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return;
1466d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
1467d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt}
1468