Tethering.java revision 7eae25021d3aa71c7616ea766f9a4d5af3b1772b
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;
68d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    private final String TAG = "Tethering";
69d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
70c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    private boolean mBooted = false;
71c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    //used to remember if we got connected before boot finished
72c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    private boolean mDeferedUsbConnection = false;
73d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    // TODO - remove both of these - should be part of interface inspection/selection stuff
752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mTetherableUsbRegexs;
762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mTetherableWifiRegexs;
77c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    private String[] mUpstreamIfaceRegexs;
782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
797eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt    private Looper mLooper;
807eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt    private HandlerThread mThread;
81dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt
82030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private HashMap<String, TetherInterfaceSM> mIfaces; // all tethered/tetherable ifaces
83d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
84d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    private BroadcastReceiver mStateReceiver;
85d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
86bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String USB_NEAR_IFACE_ADDR      = "192.168.42.129";
87bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String USB_NETMASK              = "255.255.255.0";
88bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt
89bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    // FYI - the default wifi is 192.168.43.1 and 255.255.255.0
90d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt
912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mDhcpRange;
92bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String DHCP_DEFAULT_RANGE1_START = "192.168.42.2";
93bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String DHCP_DEFAULT_RANGE1_STOP  = "192.168.42.254";
94bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String DHCP_DEFAULT_RANGE2_START = "192.168.43.2";
95bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String DHCP_DEFAULT_RANGE2_STOP  = "192.168.43.254";
962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mDnsServers;
98d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private static final String DNS_DEFAULT_SERVER1 = "8.8.8.8";
99d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private static final String DNS_DEFAULT_SERVER2 = "4.2.2.2";
1002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
101030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private boolean mDunRequired;  // configuration info - must use DUN apn on 3g
102030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
103a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private HierarchicalStateMachine mTetherMasterSM;
104a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
105a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private Notification mTetheredNotification;
1062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
107030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    // whether we can tether is the && of these two - they come in as separate
108030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    // broadcasts so track them so we can decide what to do when either changes
109030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private boolean mUsbMassStorageOff;  // track the status of USB Mass Storage
110030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private boolean mUsbConnected;       // track the status of USB connection
111030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
112dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt    public Tethering(Context context, Looper looper) {
113d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Log.d(TAG, "Tethering starting");
114d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        mContext = context;
115dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        mLooper = looper;
116d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
117d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        // register for notifications from NetworkManagement Service
118d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
119d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
120d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        try {
121d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            service.registerObserver(this);
122d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        } catch (RemoteException e) {
123d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            Log.e(TAG, "Error registering observer :" + e);
124d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
125d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
1262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mIfaces = new HashMap<String, TetherInterfaceSM>();
127d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
1287eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        // make our own thread so we don't anr the system
1297eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        mThread = new HandlerThread("Tethering");
1307eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        mThread.start();
1317eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        mLooper = mThread.getLooper();
132dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        mTetherMasterSM = new TetherMasterSM("TetherMaster", mLooper);
1332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mTetherMasterSM.start();
1342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
135bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        mStateReceiver = new StateReceiver();
136d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        IntentFilter filter = new IntentFilter();
1371cb3cb1a94342e03b54fabfaf361c9e2e26f23feMike Lockwood        filter.addAction(Intent.ACTION_BATTERY_CHANGED);
1382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
139c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt        filter.addAction(Intent.ACTION_BOOT_COMPLETED);
140bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        mContext.registerReceiver(mStateReceiver, filter);
141bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt
142bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        filter = new IntentFilter();
143030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        filter.addAction(Intent.ACTION_MEDIA_SHARED);
144030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
145bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        filter.addDataScheme("file");
146bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        mContext.registerReceiver(mStateReceiver, filter);
147bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt
148030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        mUsbMassStorageOff = !Environment.MEDIA_SHARED.equals(
149030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                Environment.getExternalStorageState());
1502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mDhcpRange = context.getResources().getStringArray(
1522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                com.android.internal.R.array.config_tether_dhcp_range);
153bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt        if ((mDhcpRange.length == 0) || (mDhcpRange.length % 2 ==1)) {
154bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt            mDhcpRange = new String[4];
155bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt            mDhcpRange[0] = DHCP_DEFAULT_RANGE1_START;
156bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt            mDhcpRange[1] = DHCP_DEFAULT_RANGE1_STOP;
157bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt            mDhcpRange[2] = DHCP_DEFAULT_RANGE2_START;
158bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt            mDhcpRange[3] = DHCP_DEFAULT_RANGE2_STOP;
1592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
160c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt        mDunRequired = context.getResources().getBoolean(
161c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                com.android.internal.R.bool.config_tether_dun_required);
1622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mTetherableUsbRegexs = context.getResources().getStringArray(
1642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                com.android.internal.R.array.config_tether_usb_regexs);
1652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mTetherableWifiRegexs = context.getResources().getStringArray(
1662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                com.android.internal.R.array.config_tether_wifi_regexs);
167c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt        mUpstreamIfaceRegexs = context.getResources().getStringArray(
168c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                com.android.internal.R.array.config_tether_upstream_regexs);
1692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // TODO - remove and rely on real notifications of the current iface
1712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mDnsServers = new String[2];
172d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        mDnsServers[0] = DNS_DEFAULT_SERVER1;
173d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        mDnsServers[1] = DNS_DEFAULT_SERVER2;
174d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
175d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
1762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void interfaceLinkStatusChanged(String iface, boolean link) {
177d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Log.d(TAG, "interfaceLinkStatusChanged " + iface + ", " + link);
1782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        boolean found = false;
17965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean usb = false;
180a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (isWifi(iface)) {
181a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
182a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        } else if (isUsb(iface)) {
183a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
184a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            usb = true;
1852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
1862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (found == false) return;
1872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
1892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
1902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (link) {
1912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm == null) {
192dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt                    sm = new TetherInterfaceSM(iface, mLooper, usb);
1932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    mIfaces.put(iface, sm);
1942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    sm.start();
1952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
1962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            } else {
1972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm != null) {
1986a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(TetherInterfaceSM.CMD_INTERFACE_DOWN);
1992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    mIfaces.remove(iface);
2002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
2012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
2022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
203d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
204d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
205a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private boolean isUsb(String iface) {
206a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        for (String regex : mTetherableUsbRegexs) {
207a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (iface.matches(regex)) return true;
208a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
209a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        return false;
210a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
211a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
212a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    public boolean isWifi(String iface) {
213a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        for (String regex : mTetherableWifiRegexs) {
214a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (iface.matches(regex)) return true;
215a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
216a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        return false;
217a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
218a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
2192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void interfaceAdded(String iface) {
22065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
22165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
2222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        boolean found = false;
22365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean usb = false;
224a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (isWifi(iface)) {
225a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
2262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
227a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (isUsb(iface)) {
228a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
229a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            usb = true;
230d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
2312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (found == false) {
2322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.d(TAG, iface + " is not a tetherable iface, ignoring");
233d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            return;
234d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
23565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
2362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
2372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
2382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (sm != null) {
2392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "active iface (" + iface + ") reported as added, ignoring");
2402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return;
2412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
242dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            sm = new TetherInterfaceSM(iface, mLooper, usb);
2432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaces.put(iface, sm);
2442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm.start();
2452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
246d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Log.d(TAG, "interfaceAdded :" + iface);
247d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
248d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
2492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void interfaceRemoved(String iface) {
2502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
2512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
2522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (sm == null) {
2532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "attempting to remove unknown iface (" + iface + "), ignoring");
2542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return;
2552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
2566a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            sm.sendMessage(TetherInterfaceSM.CMD_INTERFACE_DOWN);
2572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaces.remove(iface);
258d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
259d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
260d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
2615a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int tether(String iface) {
262d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Log.d(TAG, "Tethering " + iface);
2632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        TetherInterfaceSM sm = null;
2642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
2652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm = mIfaces.get(iface);
266d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
2672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm == null) {
2682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Tether an unknown iface :" + iface + ", ignoring");
2695a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
270d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
2715a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        if (!sm.isAvailable() && !sm.isErrored()) {
2722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Tether an unavailable iface :" + iface + ", ignoring");
2735a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
274d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
2756a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        sm.sendMessage(TetherInterfaceSM.CMD_TETHER_REQUESTED);
2765a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return ConnectivityManager.TETHER_ERROR_NO_ERROR;
277d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
278d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
2795a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int untether(String iface) {
280d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Log.d(TAG, "Untethering " + iface);
2812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        TetherInterfaceSM sm = null;
2822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
2832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm = mIfaces.get(iface);
2842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
2852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm == null) {
2862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Untether an unknown iface :" + iface + ", ignoring");
2875a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
288d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
2892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm.isErrored()) {
2902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Untethered an errored iface :" + iface + ", ignoring");
2915a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
292d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
2936a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        sm.sendMessage(TetherInterfaceSM.CMD_TETHER_UNREQUESTED);
2945a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return ConnectivityManager.TETHER_ERROR_NO_ERROR;
2955a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
2965a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
2975a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int getLastTetherError(String iface) {
2985a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        TetherInterfaceSM sm = null;
2995a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        synchronized (mIfaces) {
3005a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            sm = mIfaces.get(iface);
3015a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
3025a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        if (sm == null) {
3035a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            Log.e(TAG, "Tried to getLastTetherError on an unknown iface :" + iface + ", ignoring");
3045a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
3055a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
3065a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return sm.getLastError();
3072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
308d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private void sendTetherStateChangedBroadcast() {
3102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
3112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
312d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        try {
3132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (!service.isTetheringSupported()) return;
3142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        } catch (RemoteException e) {
3152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return;
316d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
317d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> availableList = new ArrayList<String>();
3192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> activeList = new ArrayList<String>();
3202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> erroredList = new ArrayList<String>();
3212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
322a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        boolean wifiTethered = false;
323a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        boolean usbTethered = false;
324a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
3252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
3262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set ifaces = mIfaces.keySet();
3272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object iface : ifaces) {
3282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(iface);
3292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm != null) {
3302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    if(sm.isErrored()) {
3312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        erroredList.add((String)iface);
3322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    } else if (sm.isAvailable()) {
3332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        availableList.add((String)iface);
3342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    } else if (sm.isTethered()) {
335a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        if (isUsb((String)iface)) {
336a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            usbTethered = true;
337a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        } else if (isWifi((String)iface)) {
338a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            wifiTethered = true;
339a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        }
3402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        activeList.add((String)iface);
3412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    }
3422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
343d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            }
344d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
345d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Intent broadcast = new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
346d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        broadcast.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
3472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_AVAILABLE_TETHER,
3482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                availableList);
3492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ACTIVE_TETHER, activeList);
3502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ERRORED_TETHER,
3512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                erroredList);
3522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mContext.sendStickyBroadcast(broadcast);
353d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        Log.d(TAG, "sendTetherStateChangedBroadcast " + availableList.size() + ", " +
354d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                activeList.size() + ", " + erroredList.size());
355a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
356a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (usbTethered) {
357a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (wifiTethered) {
358a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general);
359a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            } else {
360a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_usb);
361a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            }
362a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        } else if (wifiTethered) {
363a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_wifi);
364a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        } else {
365a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            clearTetheredNotification();
366a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
367a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
368a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
369a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private void showTetheredNotification(int icon) {
370a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        NotificationManager notificationManager =
371a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
372a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (notificationManager == null) {
373a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            return;
374a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
375a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
376db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt        if (mTetheredNotification != null) {
377db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            if (mTetheredNotification.icon == icon) {
378db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt                return;
379db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            }
380db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            notificationManager.cancel(mTetheredNotification.icon);
381db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt        }
382db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt
383a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        Intent intent = new Intent();
384a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        intent.setClassName("com.android.settings", "com.android.settings.TetherSettings");
385a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
386a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
387a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);
388a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
389a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        Resources r = Resources.getSystem();
390a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        CharSequence title = r.getText(com.android.internal.R.string.tethered_notification_title);
391a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        CharSequence message = r.getText(com.android.internal.R.string.
392a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                tethered_notification_message);
393a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
394a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if(mTetheredNotification == null) {
395a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification = new Notification();
396a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification.when = 0;
397a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
398a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.icon = icon;
399a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.defaults &= ~Notification.DEFAULT_SOUND;
400a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.flags = Notification.FLAG_ONGOING_EVENT;
401a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.tickerText = title;
402a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.setLatestEventInfo(mContext, title, message, pi);
403a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
404a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        notificationManager.notify(mTetheredNotification.icon, mTetheredNotification);
405a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
406a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
407a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private void clearTetheredNotification() {
408a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        NotificationManager notificationManager =
409a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
410a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (notificationManager != null && mTetheredNotification != null) {
411a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            notificationManager.cancel(mTetheredNotification.icon);
412a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification = null;
413a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
4145a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
4155a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
416030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private void updateUsbStatus() {
417030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        boolean enable = mUsbConnected && mUsbMassStorageOff;
418030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
419030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        if (mBooted) {
420030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            enableUsbIfaces(enable);
421030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        }
422030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    }
423030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
4242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private class StateReceiver extends BroadcastReceiver {
425d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        public void onReceive(Context content, Intent intent) {
4262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            String action = intent.getAction();
4271cb3cb1a94342e03b54fabfaf361c9e2e26f23feMike Lockwood            if (action.equals(Intent.ACTION_BATTERY_CHANGED)) {
428030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                mUsbConnected = (intent.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1)
4291cb3cb1a94342e03b54fabfaf361c9e2e26f23feMike Lockwood                        == BatteryManager.BATTERY_PLUGGED_USB);
430030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                Tethering.this.updateUsbStatus();
431030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
432030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                mUsbMassStorageOff = false;
433030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                updateUsbStatus();
434030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            }
435030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
436030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                mUsbMassStorageOff = true;
437030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                updateUsbStatus();
4382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
4396a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                Log.d(TAG, "Tethering got CONNECTIVITY_ACTION");
4406a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED);
441c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt            } else if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
442c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                mBooted = true;
443030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                updateUsbStatus();
444d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            }
445d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
446d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
447d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
44865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    // used on cable insert/remove
449d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private void enableUsbIfaces(boolean enable) {
45065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
45165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
45265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        String[] ifaces = new String[0];
45365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        try {
45465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            ifaces = service.listInterfaces();
45565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        } catch (Exception e) {
45665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            Log.e(TAG, "Error listing Interfaces :" + e);
45765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            return;
45865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
45965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        for (String iface : ifaces) {
460a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (isUsb(iface)) {
461a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                if (enable) {
462a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    interfaceAdded(iface);
463a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                } else {
464a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    interfaceRemoved(iface);
46565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
46665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
46765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
46865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    }
46965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
47065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    // toggled when we enter/leave the fully teathered state
471d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private boolean enableUsbRndis(boolean enabled) {
472d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        Log.d(TAG, "enableUsbRndis(" + enabled + ")");
47365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
47465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
47565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
47665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        try {
47765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            if (enabled) {
47865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                synchronized (this) {
47965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    if (!service.isUsbRNDISStarted()) {
48065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        service.startUsbRNDIS();
48165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    }
48265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
48365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            } else {
48465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                if (service.isUsbRNDISStarted()) {
48565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    service.stopUsbRNDIS();
48665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
48765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
48865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        } catch (Exception e) {
48965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            Log.e(TAG, "Error toggling usb RNDIS :" + e);
49065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            return false;
49165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
49265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        return true;
49365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    }
49465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
49565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    // configured when we start tethering and unconfig'd on error or conclusion
496d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private boolean configureUsbIface(boolean enabled) {
497d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        Log.d(TAG, "configureUsbIface(" + enabled + ")");
49865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
49965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
50065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
50165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
50265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        // bring toggle the interfaces
50365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        String[] ifaces = new String[0];
50465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        try {
50565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            ifaces = service.listInterfaces();
50665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        } catch (Exception e) {
50765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            Log.e(TAG, "Error listing Interfaces :" + e);
50865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            return false;
50965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
51065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        for (String iface : ifaces) {
511a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (isUsb(iface)) {
512a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                InterfaceConfiguration ifcg = null;
513a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                try {
514a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    ifcg = service.getInterfaceConfig(iface);
515a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    if (ifcg != null) {
516bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt                        String[] addr = USB_NEAR_IFACE_ADDR.split("\\.");
517bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt                        ifcg.ipAddr = (Integer.parseInt(addr[0]) << 24) +
518bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt                                (Integer.parseInt(addr[1]) << 16) +
519bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt                                (Integer.parseInt(addr[2]) << 8) +
520bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt                                (Integer.parseInt(addr[3]));
521bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt                        addr = USB_NETMASK.split("\\.");
522bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt                        ifcg.netmask = (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]));
526a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        if (enabled) {
527a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            ifcg.interfaceFlags = ifcg.interfaceFlags.replace("down", "up");
528a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        } else {
529a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            ifcg.interfaceFlags = ifcg.interfaceFlags.replace("up", "down");
53065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
531a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        ifcg.interfaceFlags = ifcg.interfaceFlags.replace("running", "");
532a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        ifcg.interfaceFlags = ifcg.interfaceFlags.replace("  "," ");
533a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        service.setInterfaceConfig(iface, ifcg);
53465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    }
535a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                } catch (Exception e) {
536a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    Log.e(TAG, "Error configuring interface " + iface + ", :" + e);
537a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    return false;
53865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
53965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
54065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
54165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
54265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        return true;
54365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    }
54465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
5452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetherableUsbRegexs() {
5462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return mTetherableUsbRegexs;
5472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
5482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
5492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetherableWifiRegexs() {
5502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return mTetherableWifiRegexs;
5512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
5522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
553c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    public String[] getUpstreamIfaceRegexs() {
554c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt        return mUpstreamIfaceRegexs;
555c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    }
556c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt
557c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    public boolean isDunRequired() {
558c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt        return mDunRequired;
559c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    }
560c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt
5612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetheredIfaces() {
5622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> list = new ArrayList<String>();
5632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
5642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set keys = mIfaces.keySet();
5652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object key : keys) {
5662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
5672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm.isTethered()) {
5682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    list.add((String)key);
5692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
5702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
5712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
5722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String[] retVal = new String[list.size()];
5732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        for (int i=0; i < list.size(); i++) {
5742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            retVal[i] = list.get(i);
5752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
5762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return retVal;
5772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
5782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
5792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetherableIfaces() {
5802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> list = new ArrayList<String>();
5812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
5822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set keys = mIfaces.keySet();
5832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object key : keys) {
5842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
5852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm.isAvailable()) {
5862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    list.add((String)key);
5872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
5882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
5892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
5902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String[] retVal = new String[list.size()];
5912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        for (int i=0; i < list.size(); i++) {
5922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            retVal[i] = list.get(i);
5932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
5942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return retVal;
5952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
5962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
5975a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public String[] getErroredIfaces() {
5985a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        ArrayList<String> list = new ArrayList<String>();
5995a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        synchronized (mIfaces) {
6005a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            Set keys = mIfaces.keySet();
6015a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            for (Object key : keys) {
6025a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
6035a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                if (sm.isErrored()) {
6045a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    list.add((String)key);
6055a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                }
6065a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            }
6075a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
6085a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        String[] retVal = new String[list.size()];
6095a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        for (int i= 0; i< list.size(); i++) {
6105a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            retVal[i] = list.get(i);
6115a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
6125a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return retVal;
6135a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
6145a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
6152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    class TetherInterfaceSM extends HierarchicalStateMachine {
6172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it's not in tether mode
6186a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_MODE_DEAD            =  1;
6192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // request from the user that it wants to tether
6206a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_REQUESTED            =  2;
6212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // request from the user that it wants to untether
6226a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_UNREQUESTED          =  3;
6232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification that this interface is down
6246a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_INTERFACE_DOWN              =  4;
6252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification that this interface is up
6266a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_INTERFACE_UP                =  5;
6272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had an error turning on cellular dun
6286a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_CELL_DUN_ERROR              =  6;
6292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble enabling IP Forwarding
6306a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_IP_FORWARDING_ENABLE_ERROR  =  7;
6312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble disabling IP Forwarding
6326a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_IP_FORWARDING_DISABLE_ERROR =  8;
6332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble staring tethering
6346a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_START_TETHERING_ERROR       =  9;
6352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble stopping tethering
6366a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_STOP_TETHERING_ERROR        = 10;
6372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble setting the DNS forwarders
6386a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_SET_DNS_FORWARDERS_ERROR    = 11;
6396a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // the upstream connection has changed
640951749ba2e014566553d17c512e947472951a060Wink Saville        static final int CMD_TETHER_CONNECTION_CHANGED   = 12;
6412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mDefaultState;
6432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mInitialState;
6452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mStartingState;
6462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mTetheredState;
6472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mUnavailableState;
6492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private boolean mAvailable;
6512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private boolean mTethered;
6525a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        int mLastError;
6532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String mIfaceName;
6556a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        String mMyUpstreamIfaceName;  // may change over time
6566a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
65765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean mUsb;
6582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
659dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        TetherInterfaceSM(String name, Looper looper, boolean usb) {
660dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            super(name, looper);
6612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaceName = name;
66265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            mUsb = usb;
6635a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
6642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mInitialState = new InitialState();
6662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mInitialState);
6672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStartingState = new StartingState();
6682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStartingState);
6692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mTetheredState = new TetheredState();
6702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mTetheredState);
6712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mUnavailableState = new UnavailableState();
6722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mUnavailableState);
6732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            setInitialState(mInitialState);
6752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public String toString() {
6782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            String res = new String();
6792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            res += mIfaceName + " - ";
6802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            HierarchicalState current = getCurrentState();
6812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mInitialState) res += "InitialState";
6822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mStartingState) res += "StartingState";
6832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mTetheredState) res += "TetheredState";
6842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mUnavailableState) res += "UnavailableState";
6852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (mAvailable) res += " - Available";
6862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (mTethered) res += " - Tethered";
6875a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            res += " - lastError =" + mLastError;
6882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return res;
6892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6915a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        public synchronized int getLastError() {
6925a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return mLastError;
6935a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
6945a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
6955a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        private synchronized void setLastError(int error) {
6965a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            mLastError = error;
6975a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
6985a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            if (isErrored()) {
6995a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                if (mUsb) {
7005a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    // note everything's been unwound by this point so nothing to do on
7015a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    // further error..
7025a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    Tethering.this.configureUsbIface(false);
7035a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                }
7045a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            }
7055a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
7065a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
7072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // synchronized between this getter and the following setter
7082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public synchronized boolean isAvailable() {
7092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return mAvailable;
7102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private synchronized void setAvailable(boolean available) {
7132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mAvailable = available;
7142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // synchronized between this getter and the following setter
7172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public synchronized boolean isTethered() {
7182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return mTethered;
7192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private synchronized void setTethered(boolean tethered) {
7222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mTethered = tethered;
7232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // synchronized between this getter and the following setter
7262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public synchronized boolean isErrored() {
7275a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return (mLastError != ConnectivityManager.TETHER_ERROR_NO_ERROR);
7282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class InitialState extends HierarchicalState {
7312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
7322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
7332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(true);
7342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(false);
7352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
7362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
7372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
7392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
7402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.d(TAG, "InitialState.processMessage what=" + message.what);
7412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
7422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
7432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_REQUESTED:
7445a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
7456a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_REQUESTED,
7466a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
7472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mStartingState);
7482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
7492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
7502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mUnavailableState);
7512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
7522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
7532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
7542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
7552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
7562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
7572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
7582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class StartingState extends HierarchicalState {
7612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
7622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
7632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
76465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                if (mUsb) {
765d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    if (!Tethering.this.configureUsbIface(true)) {
7666a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
7676a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
7685a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastError(ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
7695a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
770951749ba2e014566553d17c512e947472951a060Wink Saville                        transitionTo(mInitialState);
77165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        return;
77265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    }
77365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
7742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
7756a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
776951749ba2e014566553d17c512e947472951a060Wink Saville                // Skipping StartingState
777951749ba2e014566553d17c512e947472951a060Wink Saville                transitionTo(mTetheredState);
7782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
7792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
7802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
7812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.d(TAG, "StartingState.processMessage what=" + message.what);
7822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
7832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
7842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    // maybe a parent class?
7852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_UNREQUESTED:
7866a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
7876a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
78865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        if (mUsb) {
789d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                            if (!Tethering.this.configureUsbIface(false)) {
7905a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                setLastErrorAndTransitionToInitialState(
7915a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
79265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                                break;
79365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
79465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
7952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
7962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
7972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_CELL_DUN_ERROR:
7982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_ENABLE_ERROR:
7992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_DISABLE_ERROR:
8002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_START_TETHERING_ERROR:
8012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_STOP_TETHERING_ERROR:
8022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_SET_DNS_FORWARDERS_ERROR:
8035a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastErrorAndTransitionToInitialState(
8045a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                ConnectivityManager.TETHER_ERROR_MASTER_ERROR);
8052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
8076a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
8086a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
8092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mUnavailableState);
8102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
8122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
8132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
8142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
8152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
8182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class TetheredState extends HierarchicalState {
8192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
8202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
8212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
8222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                INetworkManagementService service =
8232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        INetworkManagementService.Stub.asInterface(b);
8242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
8252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    service.tetherInterface(mIfaceName);
8262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {
8275a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    setLastError(ConnectivityManager.TETHER_ERROR_TETHER_IFACE_ERROR);
8285a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
829951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mInitialState);
8302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    return;
8312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
832d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                if (mUsb) Tethering.this.enableUsbRndis(true);
8332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.d(TAG, "Tethered " + mIfaceName);
8342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
8352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(true);
8362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
8372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
83965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            public void exit() {
840d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                if (mUsb) Tethering.this.enableUsbRndis(false);
84165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
84265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            @Override
8432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
8442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.d(TAG, "TetheredState.processMessage what=" + message.what);
8452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
8462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean error = false;
8472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
8482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_UNREQUESTED:
8492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
8502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
8512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        INetworkManagementService service =
8522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                                INetworkManagementService.Stub.asInterface(b);
8536a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (mMyUpstreamIfaceName != null) {
8545a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            try {
8556a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                service.disableNat(mIfaceName, mMyUpstreamIfaceName);
856b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mMyUpstreamIfaceName = null;
8576a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
8586a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
8596a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                    service.untetherInterface(mIfaceName);
8606a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
8615a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
8626a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastErrorAndTransitionToInitialState(
8636a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                        ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR);
8646a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                break;
8656a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
8662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
8672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        try {
8682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            service.untetherInterface(mIfaceName);
8692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } catch (Exception e) {
8705a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
8715a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR);
8722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
8732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
8746a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
8756a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
8762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (message.what == CMD_TETHER_UNREQUESTED) {
87765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            if (mUsb) {
878d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                                if (!Tethering.this.configureUsbIface(false)) {
8795a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    setLastError(
8805a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                            ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
88165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                                }
88265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
8835a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            transitionTo(mInitialState);
8842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } else if (message.what == CMD_INTERFACE_DOWN) {
8852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            transitionTo(mUnavailableState);
8862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
8872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        Log.d(TAG, "Untethered " + mIfaceName);
8882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8896a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_TETHER_CONNECTION_CHANGED:
8906a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        String newUpstreamIfaceName = (String)(message.obj);
8916a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
8926a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        service = INetworkManagementService.Stub.asInterface(b);
8936a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
8946a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (mMyUpstreamIfaceName != null) {
8956a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            try {
8966a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                service.disableNat(mIfaceName, mMyUpstreamIfaceName);
897b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mMyUpstreamIfaceName = null;
8986a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
8996a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
9006a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                    service.untetherInterface(mIfaceName);
9016a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
9026a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
9036a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastErrorAndTransitionToInitialState(
9046a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                        ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR);
9056a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                break;
9066a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
9076a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        }
9086a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (newUpstreamIfaceName != null) {
9096a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            try {
9106a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                service.enableNat(mIfaceName, newUpstreamIfaceName);
9116a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
9126a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
9136a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                    service.untetherInterface(mIfaceName);
9146a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
9156a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
9166a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastError(ConnectivityManager.TETHER_ERROR_ENABLE_NAT_ERROR);
9176a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                transitionTo(mInitialState);
9186a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                return true;
9196a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
9206a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        }
9216a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mMyUpstreamIfaceName = newUpstreamIfaceName;
9226a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        break;
9232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_CELL_DUN_ERROR:
9242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_ENABLE_ERROR:
9252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_DISABLE_ERROR:
9262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_START_TETHERING_ERROR:
9272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_STOP_TETHERING_ERROR:
9282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_SET_DNS_FORWARDERS_ERROR:
9292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        error = true;
9302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        // fall through
9312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_DEAD:
9322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
9332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        service = INetworkManagementService.Stub.asInterface(b);
9346a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (mMyUpstreamIfaceName != null) {
9355a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            try {
9366a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                service.disableNat(mIfaceName, mMyUpstreamIfaceName);
937b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mMyUpstreamIfaceName = null;
9386a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
9396a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
9406a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                    service.untetherInterface(mIfaceName);
9416a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
9425a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
9436a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastErrorAndTransitionToInitialState(
9446a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                        ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR);
9456a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                break;
9466a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
9472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
9482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        try {
9492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            service.untetherInterface(mIfaceName);
9502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } catch (Exception e) {
9515a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
9525a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR);
9532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
9542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
9552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (error) {
9565a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
9575a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_MASTER_ERROR);
9582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
9592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
9602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        Log.d(TAG, "Tether lost upstream connection " + mIfaceName);
9612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        sendTetherStateChangedBroadcast();
96265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        if (mUsb) {
963d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                            if (!Tethering.this.configureUsbIface(false)) {
9645a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                setLastError(ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
96565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
96665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
9672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
9682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
9692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
9702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
9712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
9722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
9732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
9742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
9752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
9762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
9772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class UnavailableState extends HierarchicalState {
9782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
9792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
9802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
9815a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
9822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(false);
9832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
9842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
9852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
9862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
9872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
9882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
9892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_UP:
9902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
9912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
9922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
9932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
9942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
9952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
9962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
9972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
9982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
9992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10005a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        void setLastErrorAndTransitionToInitialState(int error) {
10015a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            setLastError(error);
10025a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            transitionTo(mInitialState);
10032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
100465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
10052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
10062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    class TetherMasterSM extends HierarchicalStateMachine {
10082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // an interface SM has requested Tethering
10092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        static final int CMD_TETHER_MODE_REQUESTED   = 1;
10102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // an interface SM has unrequested Tethering
10112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        static final int CMD_TETHER_MODE_UNREQUESTED = 2;
10126a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // upstream connection change - do the right thing
10136a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_UPSTREAM_CHANGED        = 3;
10142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // we received notice that the cellular DUN connection is up
10156a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_CELL_CONNECTION_RENEW   = 4;
10166a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // we don't have a valid upstream conn, check again after a delay
1017951749ba2e014566553d17c512e947472951a060Wink Saville        static final int CMD_RETRY_UPSTREAM          = 5;
10182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // This indicates what a timeout event relates to.  A state that
10202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // sends itself a delayed timeout event and handles incoming timeout events
10212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // should inc this when it is entered and whenever it sends a new timeout event.
10222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // We do not flush the old ones.
10232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private int mSequenceNumber;
10242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mInitialState;
10262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mTetherModeAliveState;
10272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mSetIpForwardingEnabledErrorState;
10292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mSetIpForwardingDisabledErrorState;
10302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mStartTetheringErrorState;
10312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mStopTetheringErrorState;
10322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mSetDnsForwardersErrorState;
10332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private ArrayList mNotifyList;
10352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10366a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        private boolean mConnectionRequested = false;
10372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1038b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt        private String mUpstreamIfaceName = null;
1039b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt
10406a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        private static final int UPSTREAM_SETTLE_TIME_MS     = 10000;
10416a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        private static final int CELL_CONNECTION_RENEW_MS    = 40000;
10422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1043dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        TetherMasterSM(String name, Looper looper) {
1044dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            super(name, looper);
10452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            //Add states
10472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mInitialState = new InitialState();
10482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mInitialState);
10492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mTetherModeAliveState = new TetherModeAliveState();
10502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mTetherModeAliveState);
10512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetIpForwardingEnabledErrorState = new SetIpForwardingEnabledErrorState();
10532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetIpForwardingEnabledErrorState);
10542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetIpForwardingDisabledErrorState = new SetIpForwardingDisabledErrorState();
10552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetIpForwardingDisabledErrorState);
10562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStartTetheringErrorState = new StartTetheringErrorState();
10572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStartTetheringErrorState);
10582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStopTetheringErrorState = new StopTetheringErrorState();
10592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStopTetheringErrorState);
10602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetDnsForwardersErrorState = new SetDnsForwardersErrorState();
10612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetDnsForwardersErrorState);
10622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mNotifyList = new ArrayList();
10642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            setInitialState(mInitialState);
10652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
10662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1067d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        class TetherMasterUtilState extends HierarchicalState {
10686a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected final static boolean TRY_TO_SETUP_MOBILE_CONNECTION = true;
10696a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected final static boolean WAIT_FOR_NETWORK_TO_SETTLE     = false;
10706a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
1071d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            @Override
1072d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            public boolean processMessage(Message m) {
1073d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return false;
1074d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
10756a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected int turnOnMobileConnection() {
1076d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
1077d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
1078d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                int retValue = Phone.APN_REQUEST_FAILED;
1079d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1080d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    retValue = service.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
108177e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                            (mDunRequired ? Phone.FEATURE_ENABLE_DUN : Phone.FEATURE_ENABLE_HIPRI),
1082c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            new Binder());
1083d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1084d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
10856a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                switch (retValue) {
10866a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                case Phone.APN_ALREADY_ACTIVE:
10876a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                case Phone.APN_REQUEST_STARTED:
10886a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sendMessageDelayed(CMD_CELL_CONNECTION_RENEW, CELL_CONNECTION_RENEW_MS);
10896a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    mConnectionRequested = true;
10906a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    break;
10916a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                case Phone.APN_REQUEST_FAILED:
10926a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                default:
10936a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    mConnectionRequested = false;
10946a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    break;
10956a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
10966a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
1097d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return retValue;
1098d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
10996a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected boolean turnOffMobileConnection() {
11006a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                if (mConnectionRequested) {
11016a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
11026a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    IConnectivityManager service =
11036a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            IConnectivityManager.Stub.asInterface(b);
11046a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    try {
11056a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        service.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
110677e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                (mDunRequired? Phone.FEATURE_ENABLE_DUN :
110777e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                             Phone.FEATURE_ENABLE_HIPRI));
11086a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    } catch (Exception e) {
11096a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        return false;
11106a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    }
11116a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    mConnectionRequested = false;
1112d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1113d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1114d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
11156a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected boolean turnOnMasterTetherSettings() {
1116d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1117d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                INetworkManagementService service =
1118d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                        INetworkManagementService.Stub.asInterface(b);
1119d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1120d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    service.setIpForwardingEnabled(true);
1121d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1122951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mSetIpForwardingEnabledErrorState);
1123d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1124d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1125d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1126bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt                    service.startTethering(mDhcpRange);
1127d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1128951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mStartTetheringErrorState);
1129d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1130d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1131d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1132d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    service.setDnsForwarders(mDnsServers);
1133d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1134951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mSetDnsForwardersErrorState);
1135d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1136d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1137d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1138d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
11396a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected boolean turnOffMasterTetherSettings() {
1140d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1141d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                INetworkManagementService service =
1142d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                        INetworkManagementService.Stub.asInterface(b);
1143d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1144d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    service.stopTethering();
1145d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1146d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    transitionTo(mStopTetheringErrorState);
1147d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1148d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1149d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1150d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    service.setIpForwardingEnabled(false);
1151d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1152d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    transitionTo(mSetIpForwardingDisabledErrorState);
1153d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1154d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1155d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                transitionTo(mInitialState);
1156d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1157d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
11586a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected String findActiveUpstreamIface() {
1159c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                // check for what iface we can use - if none found switch to error.
1160c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1161c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
1162c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt
1163c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                String[] ifaces = new String[0];
1164c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                try {
1165c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    ifaces = service.listInterfaces();
1166c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                } catch (Exception e) {
1167c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    Log.e(TAG, "Error listing Interfaces :" + e);
1168c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    return null;
1169c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                }
11706a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
1171c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                for (String iface : ifaces) {
1172c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    for (String regex : mUpstreamIfaceRegexs) {
1173c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                        if (iface.matches(regex)) {
1174c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            // verify it is up!
1175c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            InterfaceConfiguration ifcg = null;
1176c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            try {
1177c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                                ifcg = service.getInterfaceConfig(iface);
1178c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            } catch (Exception e) {
1179c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                                Log.e(TAG, "Error getting iface config :" + e);
1180c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                                // ignore - try next
1181c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                                continue;
1182c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            }
1183c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            if (ifcg.interfaceFlags.contains("up")) {
1184c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                                return iface;
1185c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            }
1186c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                        }
1187c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    }
1188c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                }
1189c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                return null;
1190c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt            }
11916a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected void chooseUpstreamType(boolean tryCell) {
11926a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                // decide if the current upstream is good or not and if not
11936a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                // do something about it (start up DUN if required or HiPri if not)
11946a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                String iface = findActiveUpstreamIface();
11956a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
11966a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b);
11976a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                mConnectionRequested = false;
11986a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                Log.d(TAG, "chooseUpstreamType(" + tryCell + "),  dunRequired ="
11996a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        + mDunRequired + ", iface=" + iface);
120077e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                if (iface != null) {
12016a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    try {
120277e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                        if (mDunRequired) {
120377e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                            // check if Dun is on - we can use that
120477e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                            NetworkInfo info = cm.getNetworkInfo(
120577e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                    ConnectivityManager.TYPE_MOBILE_DUN);
120677e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                            if (info.isConnected()) {
120777e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                Log.d(TAG, "setting dun ifacename =" + iface);
120877e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                // even if we're already connected - it may be somebody else's
120977e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                // refcount, so add our own
121077e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                turnOnMobileConnection();
121177e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                            } else {
121277e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                // verify the iface is not the default mobile - can't use that!
121377e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                info = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
121477e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                if (info.isConnected()) {
121577e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                    iface = null; // can't accept this one
121677e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                }
121777e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                            }
121877e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                        } else {
12196a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            Log.d(TAG, "checking if hipri brought us this connection");
12206a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            NetworkInfo info = cm.getNetworkInfo(
12216a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                    ConnectivityManager.TYPE_MOBILE_HIPRI);
12226a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            if (info.isConnected()) {
12236a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                Log.d(TAG, "yes - hipri in use");
122477e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                // even if we're already connected - it may be sombody else's
122577e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                // refcount, so add our own
12266a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                turnOnMobileConnection();
12272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            }
12282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
122977e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                    } catch (RemoteException e) {
123077e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                        Log.e(TAG, "RemoteException calling ConnectivityManager " + e);
123177e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                        iface = null;
12326a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    }
12336a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
123477e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                // may have been set to null in the if above
123577e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                if (iface == null ) {
123677e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                    if (tryCell == TRY_TO_SETUP_MOBILE_CONNECTION) {
123777e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                        turnOnMobileConnection();
123877e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                    }
12396a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    // wait for things to settle and retry
12406a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sendMessageDelayed(CMD_RETRY_UPSTREAM, UPSTREAM_SETTLE_TIME_MS);
12416a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
1242b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                notifyTetheredOfNewUpstreamIface(iface);
12436a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            }
1244b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt            protected void notifyTetheredOfNewUpstreamIface(String ifaceName) {
12456a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                Log.d(TAG, "notifying tethered with iface =" + ifaceName);
1246b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                mUpstreamIfaceName = ifaceName;
12476a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                for (Object o : mNotifyList) {
12486a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    TetherInterfaceSM sm = (TetherInterfaceSM)o;
12496a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED,
12506a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            ifaceName);
12512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
12522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
12532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
12542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
12556a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        class InitialState extends TetherMasterUtilState {
12562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
12572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
12586a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                mConnectionRequested = false;
12592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
12602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
12612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
12626a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                Log.d(TAG, "MasterInitialState.processMessage what=" + message.what);
12632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
12642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
12652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
12662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
12676a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        Log.d(TAG, "Tether Mode requested by " + who.toString());
12682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        mNotifyList.add(who);
12696a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        transitionTo(mTetherModeAliveState);
12702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
12712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_UNREQUESTED:
12722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        who = (TetherInterfaceSM)message.obj;
12736a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        Log.d(TAG, "Tether Mode unrequested by " + who.toString());
12742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        int index = mNotifyList.indexOf(who);
12752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (index != -1) {
12766a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            mNotifyList.remove(who);
12772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
12782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
12792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
12802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
12812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
12822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
12832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
12842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
12852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
12862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1287d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        class TetherModeAliveState extends TetherMasterUtilState {
12886a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            boolean mTryCell = WAIT_FOR_NETWORK_TO_SETTLE;
12892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
12902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
12916a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                mTryCell = WAIT_FOR_NETWORK_TO_SETTLE;  // first pass lets just see what we have.
12926a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                chooseUpstreamType(mTryCell);
12936a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                mTryCell = !mTryCell;
12946a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                turnOnMasterTetherSettings(); // may transition us out
12956a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            }
12966a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            @Override
12976a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            public void exit() {
12986a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                turnOffMobileConnection();
1299b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                notifyTetheredOfNewUpstreamIface(null);
13002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
13032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.d(TAG, "TetherModeAliveState.processMessage what=" + message.what);
13042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
13052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
13062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
13072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
13082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        mNotifyList.add(who);
1309b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                        who.sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED,
1310b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mUpstreamIfaceName);
13112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_UNREQUESTED:
13132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        who = (TetherInterfaceSM)message.obj;
13142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        int index = mNotifyList.indexOf(who);
13152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (index != -1) {
13162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            mNotifyList.remove(index);
13172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            if (mNotifyList.isEmpty()) {
1318d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                                turnOffMasterTetherSettings(); // transitions appropriately
13192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            }
13202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
13212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13226a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_UPSTREAM_CHANGED:
13236a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTryCell = WAIT_FOR_NETWORK_TO_SETTLE;
13246a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        chooseUpstreamType(mTryCell);
13256a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTryCell = !mTryCell;
13262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13276a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_CELL_CONNECTION_RENEW:
13286a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        // make sure we're still using a requested connection - may have found
13296a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        // wifi or something since then.
13306a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (mConnectionRequested) {
13316a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            Log.d(TAG, "renewing mobile connection - requeuing for another " +
13326a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                    CELL_CONNECTION_RENEW_MS + "ms");
13336a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            turnOnMobileConnection();
13346a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        }
133565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        break;
13366a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                   case CMD_RETRY_UPSTREAM:
13376a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                       chooseUpstreamType(mTryCell);
13386a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                       mTryCell = !mTryCell;
13396a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                       break;
13406a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                   default:
13412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                       retValue = false;
13422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                       break;
13432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
13442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
13452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
13472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
13482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class ErrorState extends HierarchicalState {
13492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            int mErrorNotification;
13502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
13522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
13532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
13542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
13552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
13566a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        who.sendMessage(mErrorNotification);
13572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
13592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                       retValue = false;
13602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
13612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
13622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            void notify(int msgType) {
13642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                mErrorNotification = msgType;
13652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                for (Object o : mNotifyList) {
13662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    TetherInterfaceSM sm = (TetherInterfaceSM)o;
13676a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(msgType);
13682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
13692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
13712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
13722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetIpForwardingEnabledErrorState extends ErrorState {
13732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
13752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setIpForwardingEnabled");
13762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_IP_FORWARDING_ENABLE_ERROR);
13772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
13792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
13802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetIpForwardingDisabledErrorState extends ErrorState {
13812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
13832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setIpForwardingDisabled");
13842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_IP_FORWARDING_DISABLE_ERROR);
13852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
13872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
13882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class StartTetheringErrorState extends ErrorState {
13892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
13912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in startTethering");
13922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_START_TETHERING_ERROR);
13932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
13942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                INetworkManagementService service =
13952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        INetworkManagementService.Stub.asInterface(b);
13962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
13972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    service.setIpForwardingEnabled(false);
13982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
13992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class StopTetheringErrorState extends ErrorState {
14032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in stopTethering");
14062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_STOP_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 SetDnsForwardersErrorState extends ErrorState {
14172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setDnsForwarders");
14202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_SET_DNS_FORWARDERS_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.stopTethering();
14262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
14272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
14282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    service.setIpForwardingEnabled(false);
14292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
14302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
1431d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
1432d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
1433d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
14342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
14352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (mContext.checkCallingOrSelfPermission(
14362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
14372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            pw.println("Permission Denial: can't dump ConnectivityService.Tether " +
14382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    "from from pid=" + Binder.getCallingPid() + ", uid=" +
14392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    Binder.getCallingUid());
14402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    return;
14412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        pw.println();
14442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        pw.println("Tether state:");
14452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
14462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object o : mIfaces.values()) {
14472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                pw.println(" "+o.toString());
14482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
1449d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
14502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        pw.println();
14512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return;
1452d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
1453d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt}
1454