Tethering.java revision 6143f5f7392fb0d3e5702a46a2415bd0ecb6efb4
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;
226fdd0c6274c81b337ad35b70480f881daf7354c3Danica Changimport android.bluetooth.BluetoothPan;
23d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.content.BroadcastReceiver;
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;
29c4308f01c965571dc2354107c3574df113e397eeMike Lockwoodimport android.hardware.usb.UsbManager;
30d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.net.ConnectivityManager;
3165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwaltimport android.net.InterfaceConfiguration;
322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.net.IConnectivityManager;
33d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.net.INetworkManagementEventObserver;
34ed1264093234b1f1354cd5f669eb82a50bb56869Robert Greenwaltimport android.net.LinkAddress;
3505d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwaltimport android.net.LinkProperties;
362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.net.NetworkInfo;
37e590373ea71251cfffc8f22f011e2e6335dce716Robert Greenwaltimport android.net.NetworkUtils;
382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.os.Binder;
39030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwaltimport android.os.Environment;
406eef5c687677b5965f769c62f50edda25abb431eMike Lockwoodimport android.os.Handler;
417eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwaltimport android.os.HandlerThread;
42d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.os.IBinder;
43d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.os.INetworkManagementService;
44dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwaltimport android.os.Looper;
452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.os.Message;
46d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.os.RemoteException;
47d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.os.ServiceManager;
48d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.provider.Settings;
49d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.util.Log;
50d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport com.android.internal.telephony.Phone;
5264c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Savilleimport com.android.internal.util.IState;
5364c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Savilleimport com.android.internal.util.State;
5464c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Savilleimport com.android.internal.util.StateMachine;
552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.io.FileDescriptor;
572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.io.PrintWriter;
5804808c294027f8bc318643a94c85a999257d7f52Robert Greenwaltimport java.net.InetAddress;
59d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport java.util.ArrayList;
60ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwaltimport java.util.Collection;
612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.util.HashMap;
62ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwaltimport java.util.Iterator;
636fdd0c6274c81b337ad35b70480f881daf7354c3Danica Changimport java.util.LinkedList;
642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.util.Set;
65d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt/**
66d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * @hide
672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt *
682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt * Timeout
692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt *
702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt * TODO - look for parent classes and code sharing
71d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt */
7265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
73d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltpublic class Tethering extends INetworkManagementEventObserver.Stub {
74d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
75d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    private Context mContext;
76924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt    private final static String TAG = "Tethering";
771039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt    private final static boolean DEBUG = true;
78d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
79c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    private boolean mBooted = false;
80c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    //used to remember if we got connected before boot finished
81c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    private boolean mDeferedUsbConnection = false;
82d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    // TODO - remove both of these - should be part of interface inspection/selection stuff
842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mTetherableUsbRegexs;
852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mTetherableWifiRegexs;
866fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    private String[] mTetherableBluetoothRegexs;
87ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    private Collection<Integer> mUpstreamIfaceTypes;
88ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
89ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    private static final Integer MOBILE_TYPE = new Integer(ConnectivityManager.TYPE_MOBILE);
90ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    private static final Integer HIPRI_TYPE = new Integer(ConnectivityManager.TYPE_MOBILE_HIPRI);
91ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    private static final Integer DUN_TYPE = new Integer(ConnectivityManager.TYPE_MOBILE_DUN);
92ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
93ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    // if we have to connect to mobile, what APN type should we use?  Calculated by examining the
94ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    // upstream type list and the DUN_REQUIRED secure-setting
95ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    private int mPreferredUpstreamMobileApn = ConnectivityManager.TYPE_NONE;
962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
97c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh    private INetworkManagementService mNMService;
987eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt    private Looper mLooper;
997eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt    private HandlerThread mThread;
100dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt
101030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private HashMap<String, TetherInterfaceSM> mIfaces; // all tethered/tetherable ifaces
102d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
103d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    private BroadcastReceiver mStateReceiver;
104d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
105bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String USB_NEAR_IFACE_ADDR      = "192.168.42.129";
106b979f79158f9c470fa09ff3b96d72db274262201Robert Greenwalt    private static final int USB_PREFIX_LENGTH        = 24;
107bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt
108707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    // USB is  192.168.42.1 and 255.255.255.0
109707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    // Wifi is 192.168.43.1 and 255.255.255.0
110707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    // BT is limited to max default of 5 connections. 192.168.44.1 to 192.168.48.1
111707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    // with 255.255.255.0
112d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt
1132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mDhcpRange;
114c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh    private static final String[] DHCP_DEFAULT_RANGE = {
115c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh        "192.168.42.2", "192.168.42.254", "192.168.43.2", "192.168.43.254",
116c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh        "192.168.44.2", "192.168.44.254", "192.168.45.2", "192.168.45.254",
117c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh        "192.168.46.2", "192.168.46.254", "192.168.47.2", "192.168.47.254",
118c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh        "192.168.48.2", "192.168.48.254",
119c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh    };
1202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mDnsServers;
122d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private static final String DNS_DEFAULT_SERVER1 = "8.8.8.8";
123df255c2283b2bcf2d5e70d095a4d2333c1cb80a3Robert Greenwalt    private static final String DNS_DEFAULT_SERVER2 = "8.8.4.4";
1242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
12564c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville    private StateMachine mTetherMasterSM;
126a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
127a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private Notification mTetheredNotification;
1282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
129030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    // whether we can tether is the && of these two - they come in as separate
130030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    // broadcasts so track them so we can decide what to do when either changes
131030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private boolean mUsbMassStorageOff;  // track the status of USB Mass Storage
132030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private boolean mUsbConnected;       // track the status of USB connection
133030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
134c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh    public Tethering(Context context, INetworkManagementService nmService, Looper looper) {
135d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        mContext = context;
136c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh        mNMService = nmService;
137dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        mLooper = looper;
138d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
1392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mIfaces = new HashMap<String, TetherInterfaceSM>();
140d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
1417eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        // make our own thread so we don't anr the system
1427eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        mThread = new HandlerThread("Tethering");
1437eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        mThread.start();
1447eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        mLooper = mThread.getLooper();
145dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        mTetherMasterSM = new TetherMasterSM("TetherMaster", mLooper);
1462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mTetherMasterSM.start();
1472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
148bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        mStateReceiver = new StateReceiver();
149d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        IntentFilter filter = new IntentFilter();
150770126a678ccc9328a89407ffc82f4d998b25427Mike Lockwood        filter.addAction(UsbManager.ACTION_USB_STATE);
1512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
152c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt        filter.addAction(Intent.ACTION_BOOT_COMPLETED);
153bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        mContext.registerReceiver(mStateReceiver, filter);
154bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt
155bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        filter = new IntentFilter();
156030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        filter.addAction(Intent.ACTION_MEDIA_SHARED);
157030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
158bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        filter.addDataScheme("file");
159bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        mContext.registerReceiver(mStateReceiver, filter);
160bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt
161030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        mUsbMassStorageOff = !Environment.MEDIA_SHARED.equals(
162030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                Environment.getExternalStorageState());
1632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mDhcpRange = context.getResources().getStringArray(
1652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                com.android.internal.R.array.config_tether_dhcp_range);
166bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt        if ((mDhcpRange.length == 0) || (mDhcpRange.length % 2 ==1)) {
167c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh            mDhcpRange = DHCP_DEFAULT_RANGE;
1682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
1692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mTetherableUsbRegexs = context.getResources().getStringArray(
1712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                com.android.internal.R.array.config_tether_usb_regexs);
1722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mTetherableWifiRegexs = context.getResources().getStringArray(
1732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                com.android.internal.R.array.config_tether_wifi_regexs);
1746fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        mTetherableBluetoothRegexs = context.getResources().getStringArray(
1756fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                com.android.internal.R.array.config_tether_bluetooth_regexs);
176ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        int ifaceTypes[] = context.getResources().getIntArray(
177ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                com.android.internal.R.array.config_tether_upstream_types);
178ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        mUpstreamIfaceTypes = new ArrayList();
179ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        for (int i : ifaceTypes) {
180ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            mUpstreamIfaceTypes.add(new Integer(i));
181ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        }
182ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
183ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        // check if the upstream type list needs to be modified due to secure-settings
184ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        checkDunRequired();
1852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // TODO - remove and rely on real notifications of the current iface
1872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mDnsServers = new String[2];
188d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        mDnsServers[0] = DNS_DEFAULT_SERVER1;
189d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        mDnsServers[1] = DNS_DEFAULT_SERVER2;
190d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
191d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
1926143f5f7392fb0d3e5702a46a2415bd0ecb6efb4Mike J. Chen    public void interfaceLinkStateChanged(String iface, boolean up) {
1936143f5f7392fb0d3e5702a46a2415bd0ecb6efb4Mike J. Chen    }
1946143f5f7392fb0d3e5702a46a2415bd0ecb6efb4Mike J. Chen
1956143f5f7392fb0d3e5702a46a2415bd0ecb6efb4Mike J. Chen    public void interfaceStatusChanged(String iface, boolean up) {
1966143f5f7392fb0d3e5702a46a2415bd0ecb6efb4Mike J. Chen        if (DEBUG) Log.d(TAG, "interfaceStatusChanged " + iface + ", " + up);
1972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        boolean found = false;
19865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean usb = false;
199a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (isWifi(iface)) {
200a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
201a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        } else if (isUsb(iface)) {
202a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
203a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            usb = true;
2046fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        } else if (isBluetooth(iface)) {
2056fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            found = true;
2062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
2072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (found == false) return;
2082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
2092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
2102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
2116143f5f7392fb0d3e5702a46a2415bd0ecb6efb4Mike J. Chen            if (up) {
2122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm == null) {
213dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt                    sm = new TetherInterfaceSM(iface, mLooper, usb);
2142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    mIfaces.put(iface, sm);
2152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    sm.start();
2162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
2172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            } else {
2182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm != null) {
2196a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(TetherInterfaceSM.CMD_INTERFACE_DOWN);
2202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    mIfaces.remove(iface);
2212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
2222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
2232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
224d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
225d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
226a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private boolean isUsb(String iface) {
227a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        for (String regex : mTetherableUsbRegexs) {
228a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (iface.matches(regex)) return true;
229a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
230a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        return false;
231a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
232a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
233a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    public boolean isWifi(String iface) {
234a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        for (String regex : mTetherableWifiRegexs) {
235a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (iface.matches(regex)) return true;
236a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
237a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        return false;
238a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
239a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
2406fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    public boolean isBluetooth(String iface) {
2416fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        for (String regex : mTetherableBluetoothRegexs) {
2426fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            if (iface.matches(regex)) return true;
2436fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        }
2446fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        return false;
2456fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    }
2462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void interfaceAdded(String iface) {
2472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        boolean found = false;
24865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean usb = false;
249a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (isWifi(iface)) {
250a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
2512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
252a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (isUsb(iface)) {
253a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
254a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            usb = true;
255d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
2566fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        if (isBluetooth(iface)) {
2576fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            found = true;
2586fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        }
2592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (found == false) {
260924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt            if (DEBUG) Log.d(TAG, iface + " is not a tetherable iface, ignoring");
261d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            return;
262d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
26365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
2642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
2652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
2662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (sm != null) {
267924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "active iface (" + iface + ") reported as added, ignoring");
2682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return;
2692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
270dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            sm = new TetherInterfaceSM(iface, mLooper, usb);
2712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaces.put(iface, sm);
2722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm.start();
2732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
274924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) Log.d(TAG, "interfaceAdded :" + iface);
275d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
276d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
2772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void interfaceRemoved(String iface) {
2782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
2792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
2802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (sm == null) {
281924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) {
282924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                    Log.e(TAG, "attempting to remove unknown iface (" + iface + "), ignoring");
283924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                }
2842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return;
2852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
2866a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            sm.sendMessage(TetherInterfaceSM.CMD_INTERFACE_DOWN);
2872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaces.remove(iface);
288d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
289d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
290d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
2915a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int tether(String iface) {
292d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Log.d(TAG, "Tethering " + iface);
2932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        TetherInterfaceSM sm = null;
2942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
2952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm = mIfaces.get(iface);
296d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
2972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm == null) {
2982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Tether an unknown iface :" + iface + ", ignoring");
2995a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
300d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3015a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        if (!sm.isAvailable() && !sm.isErrored()) {
3022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Tether an unavailable iface :" + iface + ", ignoring");
3035a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
304d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3056a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        sm.sendMessage(TetherInterfaceSM.CMD_TETHER_REQUESTED);
3065a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return ConnectivityManager.TETHER_ERROR_NO_ERROR;
307d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
308d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3095a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int untether(String iface) {
310d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Log.d(TAG, "Untethering " + iface);
3112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        TetherInterfaceSM sm = null;
3122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
3132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm = mIfaces.get(iface);
3142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
3152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm == null) {
3162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Untether an unknown iface :" + iface + ", ignoring");
3175a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
318d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm.isErrored()) {
3202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Untethered an errored iface :" + iface + ", ignoring");
3215a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
322d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3236a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        sm.sendMessage(TetherInterfaceSM.CMD_TETHER_UNREQUESTED);
3245a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return ConnectivityManager.TETHER_ERROR_NO_ERROR;
3255a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
3265a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
3275a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int getLastTetherError(String iface) {
3285a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        TetherInterfaceSM sm = null;
3295a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        synchronized (mIfaces) {
3305a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            sm = mIfaces.get(iface);
3315a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
3325a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        if (sm == null) {
3335a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            Log.e(TAG, "Tried to getLastTetherError on an unknown iface :" + iface + ", ignoring");
3345a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
3355a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
3365a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return sm.getLastError();
3372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
338d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private void sendTetherStateChangedBroadcast() {
3402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
341c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh        IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b);
342d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        try {
343c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh            if (!cm.isTetheringSupported()) return;
3442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        } catch (RemoteException e) {
3452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return;
346d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
347d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> availableList = new ArrayList<String>();
3492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> activeList = new ArrayList<String>();
3502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> erroredList = new ArrayList<String>();
3512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
352a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        boolean wifiTethered = false;
353a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        boolean usbTethered = false;
3546fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        boolean bluetoothTethered = false;
355a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
3562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
3572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set ifaces = mIfaces.keySet();
3582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object iface : ifaces) {
3592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(iface);
3602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm != null) {
3616fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                    if (sm.isErrored()) {
3622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        erroredList.add((String)iface);
3632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    } else if (sm.isAvailable()) {
3642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        availableList.add((String)iface);
3652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    } else if (sm.isTethered()) {
366a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        if (isUsb((String)iface)) {
367a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            usbTethered = true;
368a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        } else if (isWifi((String)iface)) {
369a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            wifiTethered = true;
3706fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                      } else if (isBluetooth((String)iface)) {
3716fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                            bluetoothTethered = true;
372a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        }
3732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        activeList.add((String)iface);
3742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    }
3752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
376d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            }
377d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
378d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Intent broadcast = new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
379d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        broadcast.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
3802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_AVAILABLE_TETHER,
3812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                availableList);
3822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ACTIVE_TETHER, activeList);
3832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ERRORED_TETHER,
3842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                erroredList);
3852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mContext.sendStickyBroadcast(broadcast);
386924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) {
387924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt            Log.d(TAG, "sendTetherStateChangedBroadcast " + availableList.size() + ", " +
388924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                    activeList.size() + ", " + erroredList.size());
389924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        }
390a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
391a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (usbTethered) {
3926fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            if (wifiTethered || bluetoothTethered) {
393a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general);
394a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            } else {
395a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_usb);
396a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            }
397a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        } else if (wifiTethered) {
3986fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            if (bluetoothTethered) {
3996fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general);
4006fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            } else {
4016fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_wifi);
4026fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            }
4036fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        } else if (bluetoothTethered) {
4046fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_bluetooth);
405a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        } else {
406a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            clearTetheredNotification();
407a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
408a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
409a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
410a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private void showTetheredNotification(int icon) {
411a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        NotificationManager notificationManager =
412a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
413a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (notificationManager == null) {
414a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            return;
415a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
416a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
417db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt        if (mTetheredNotification != null) {
418db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            if (mTetheredNotification.icon == icon) {
419db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt                return;
420db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            }
421db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            notificationManager.cancel(mTetheredNotification.icon);
422db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt        }
423db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt
424a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        Intent intent = new Intent();
425a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        intent.setClassName("com.android.settings", "com.android.settings.TetherSettings");
426a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
427a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
428a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);
429a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
430a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        Resources r = Resources.getSystem();
431a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        CharSequence title = r.getText(com.android.internal.R.string.tethered_notification_title);
432a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        CharSequence message = r.getText(com.android.internal.R.string.
433a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                tethered_notification_message);
434a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
4356fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        if (mTetheredNotification == null) {
436a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification = new Notification();
437a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification.when = 0;
438a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
439a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.icon = icon;
440a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.defaults &= ~Notification.DEFAULT_SOUND;
441a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.flags = Notification.FLAG_ONGOING_EVENT;
442a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.tickerText = title;
443a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.setLatestEventInfo(mContext, title, message, pi);
444a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
445a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        notificationManager.notify(mTetheredNotification.icon, mTetheredNotification);
446a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
447a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
448a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private void clearTetheredNotification() {
449a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        NotificationManager notificationManager =
450a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
451a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (notificationManager != null && mTetheredNotification != null) {
452a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            notificationManager.cancel(mTetheredNotification.icon);
453a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification = null;
454a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
4555a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
4565a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
457030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private void updateUsbStatus() {
458030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        boolean enable = mUsbConnected && mUsbMassStorageOff;
459030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
460030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        if (mBooted) {
461030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            enableUsbIfaces(enable);
462030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        }
463030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    }
464030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
4652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private class StateReceiver extends BroadcastReceiver {
466d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        public void onReceive(Context content, Intent intent) {
4672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            String action = intent.getAction();
468770126a678ccc9328a89407ffc82f4d998b25427Mike Lockwood            if (action.equals(UsbManager.ACTION_USB_STATE)) {
469b92df0f66926252cf60cdda7812bb1d50ce9378eMike Lockwood                mUsbConnected = intent.getExtras().getBoolean(UsbManager.USB_CONNECTED);
470b92df0f66926252cf60cdda7812bb1d50ce9378eMike Lockwood                updateUsbStatus();
471030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
472030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                mUsbMassStorageOff = false;
473030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                updateUsbStatus();
474030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            }
475030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
476030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                mUsbMassStorageOff = true;
477030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                updateUsbStatus();
4782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
479924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "Tethering got CONNECTIVITY_ACTION");
4806a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED);
481c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt            } else if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
482c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                mBooted = true;
483030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                updateUsbStatus();
484d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            }
485d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
486d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
487d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
48865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    // used on cable insert/remove
489d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private void enableUsbIfaces(boolean enable) {
4903c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood        // add/remove USB interfaces when USB is connected/disconnected
4913c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood        for (String intf : mTetherableUsbRegexs) {
4923c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            if (enable) {
4933c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                interfaceAdded(intf);
4943c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            } else {
4953c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                interfaceRemoved(intf);
4963c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            }
4973c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood        }
4983c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood
49965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        String[] ifaces = new String[0];
50065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        try {
501c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh            ifaces = mNMService.listInterfaces();
50265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        } catch (Exception e) {
5033c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            Log.e(TAG, "Error listing Interfaces", e);
50465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            return;
50565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
50665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        for (String iface : ifaces) {
507a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (isUsb(iface)) {
508a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                if (enable) {
509a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    interfaceAdded(iface);
510a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                } else {
511a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    interfaceRemoved(iface);
51265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
51365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
51465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
51565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    }
51665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
5176fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    // toggled when we enter/leave the fully tethered state
518d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private boolean enableUsbRndis(boolean enabled) {
519924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) Log.d(TAG, "enableUsbRndis(" + enabled + ")");
52065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
5213c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood        UsbManager usbManager = (UsbManager)mContext.getSystemService(Context.USB_SERVICE);
5223c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood        if (usbManager == null) {
5233c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            Log.d(TAG, "could not get UsbManager");
5243c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            return false;
5253c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood        }
52665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        try {
52765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            if (enabled) {
528f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood                usbManager.setCurrentFunction(UsbManager.USB_FUNCTION_RNDIS, false);
52965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            } else {
530f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood                usbManager.setCurrentFunction(null, false);
53165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
53265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        } catch (Exception e) {
5333c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            Log.e(TAG, "Error toggling usb RNDIS", e);
53465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            return false;
53565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
53665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        return true;
53765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    }
53865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
53965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    // configured when we start tethering and unconfig'd on error or conclusion
540d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private boolean configureUsbIface(boolean enabled) {
541924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) Log.d(TAG, "configureUsbIface(" + enabled + ")");
54265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
5433c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood        if (enabled) {
5443c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            // must enable RNDIS first to create the interface
5453c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            enableUsbRndis(enabled);
54665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
5473c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood
5483c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood        try {
5493c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            // bring toggle the interfaces
5503c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            String[] ifaces = new String[0];
5513c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            try {
5523c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                ifaces = mNMService.listInterfaces();
5533c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            } catch (Exception e) {
5543c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                Log.e(TAG, "Error listing Interfaces", e);
5553c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                return false;
5563c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            }
5573c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            for (String iface : ifaces) {
5583c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                if (isUsb(iface)) {
5593c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                    InterfaceConfiguration ifcg = null;
5603c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                    try {
5613c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                        ifcg = mNMService.getInterfaceConfig(iface);
5623c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                        if (ifcg != null) {
5633c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                            InetAddress addr = NetworkUtils.numericToInetAddress(USB_NEAR_IFACE_ADDR);
5643c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                            ifcg.addr = new LinkAddress(addr, USB_PREFIX_LENGTH);
5653c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                            if (enabled) {
5663c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                                ifcg.interfaceFlags = ifcg.interfaceFlags.replace("down", "up");
5673c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                            } else {
5683c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                                ifcg.interfaceFlags = ifcg.interfaceFlags.replace("up", "down");
5693c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                            }
5703c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                            ifcg.interfaceFlags = ifcg.interfaceFlags.replace("running", "");
5713c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                            ifcg.interfaceFlags = ifcg.interfaceFlags.replace("  "," ");
5723c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                            mNMService.setInterfaceConfig(iface, ifcg);
57365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
5743c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                    } catch (Exception e) {
5753c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                        Log.e(TAG, "Error configuring interface " + iface, e);
5763c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                        return false;
57765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    }
57865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
5793c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood             }
5803c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood        } finally {
5813c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            if (!enabled) {
5823c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                enableUsbRndis(false);
58365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
58465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
58565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
58665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        return true;
58765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    }
58865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
5892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetherableUsbRegexs() {
5902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return mTetherableUsbRegexs;
5912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
5922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
5932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetherableWifiRegexs() {
5942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return mTetherableWifiRegexs;
5952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
5962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
5976fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    public String[] getTetherableBluetoothRegexs() {
5986fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        return mTetherableBluetoothRegexs;
5996fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    }
6006fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang
601ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    public int[] getUpstreamIfaceTypes() {
602ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        int values[] = new int[mUpstreamIfaceTypes.size()];
603ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        Iterator<Integer> iterator = mUpstreamIfaceTypes.iterator();
604ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        for (int i=0; i < mUpstreamIfaceTypes.size(); i++) {
605ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            values[i] = iterator.next();
606ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        }
607ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        return values;
608c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    }
609c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt
610ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    public void checkDunRequired() {
611ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        int requiredApn = ((Settings.Secure.getInt(mContext.getContentResolver(),
612ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                Settings.Secure.TETHER_DUN_REQUIRED, 0) == 1) ?
613ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                ConnectivityManager.TYPE_MOBILE_DUN :
614ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                ConnectivityManager.TYPE_MOBILE_HIPRI);
615ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        if (mPreferredUpstreamMobileApn != requiredApn) {
616ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            if (requiredApn == ConnectivityManager.TYPE_MOBILE_DUN) {
617ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                while (mUpstreamIfaceTypes.contains(MOBILE_TYPE)) {
618ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    mUpstreamIfaceTypes.remove(MOBILE_TYPE);
619ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                }
620ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                while (mUpstreamIfaceTypes.contains(HIPRI_TYPE)) {
621ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    mUpstreamIfaceTypes.remove(HIPRI_TYPE);
622ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                }
623ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (mUpstreamIfaceTypes.contains(DUN_TYPE) == false) {
624ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    mUpstreamIfaceTypes.add(DUN_TYPE);
625ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                }
626ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            } else {
627ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                while (mUpstreamIfaceTypes.contains(DUN_TYPE)) {
628ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    mUpstreamIfaceTypes.remove(DUN_TYPE);
629ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                }
630ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (mUpstreamIfaceTypes.contains(MOBILE_TYPE) == false) {
631ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    mUpstreamIfaceTypes.add(MOBILE_TYPE);
632ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                }
633ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (mUpstreamIfaceTypes.contains(HIPRI_TYPE) == false) {
634ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    mUpstreamIfaceTypes.add(HIPRI_TYPE);
635ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                }
636ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            }
637ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            mPreferredUpstreamMobileApn = requiredApn;
638ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        }
639c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    }
640c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt
6412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetheredIfaces() {
6422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> list = new ArrayList<String>();
6432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
6442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set keys = mIfaces.keySet();
6452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object key : keys) {
6462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
6472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm.isTethered()) {
6482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    list.add((String)key);
6492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
6502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
6512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String[] retVal = new String[list.size()];
6532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        for (int i=0; i < list.size(); i++) {
6542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            retVal[i] = list.get(i);
6552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return retVal;
6572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
6582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetherableIfaces() {
6602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> list = new ArrayList<String>();
6612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
6622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set keys = mIfaces.keySet();
6632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object key : keys) {
6642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
6652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm.isAvailable()) {
6662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    list.add((String)key);
6672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
6682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
6692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String[] retVal = new String[list.size()];
6712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        for (int i=0; i < list.size(); i++) {
6722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            retVal[i] = list.get(i);
6732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return retVal;
6752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
6762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6775a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public String[] getErroredIfaces() {
6785a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        ArrayList<String> list = new ArrayList<String>();
6795a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        synchronized (mIfaces) {
6805a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            Set keys = mIfaces.keySet();
6815a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            for (Object key : keys) {
6825a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
6835a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                if (sm.isErrored()) {
6845a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    list.add((String)key);
6855a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                }
6865a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            }
6875a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
6885a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        String[] retVal = new String[list.size()];
6895a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        for (int i= 0; i< list.size(); i++) {
6905a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            retVal[i] = list.get(i);
6915a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
6925a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return retVal;
6935a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
6945a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
69564c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville    class TetherInterfaceSM extends StateMachine {
6962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it's not in tether mode
6976a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_MODE_DEAD            =  1;
6982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // request from the user that it wants to tether
6996a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_REQUESTED            =  2;
7002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // request from the user that it wants to untether
7016a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_UNREQUESTED          =  3;
7022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification that this interface is down
7036a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_INTERFACE_DOWN              =  4;
7042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification that this interface is up
7056a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_INTERFACE_UP                =  5;
7062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had an error turning on cellular dun
7076a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_CELL_DUN_ERROR              =  6;
7082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble enabling IP Forwarding
7096a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_IP_FORWARDING_ENABLE_ERROR  =  7;
7102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble disabling IP Forwarding
7116a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_IP_FORWARDING_DISABLE_ERROR =  8;
7122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble staring tethering
7136a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_START_TETHERING_ERROR       =  9;
7142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble stopping tethering
7156a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_STOP_TETHERING_ERROR        = 10;
7162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble setting the DNS forwarders
7176a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_SET_DNS_FORWARDERS_ERROR    = 11;
7186a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // the upstream connection has changed
719951749ba2e014566553d17c512e947472951a060Wink Saville        static final int CMD_TETHER_CONNECTION_CHANGED   = 12;
7202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
72164c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mDefaultState;
7222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
72364c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mInitialState;
72464c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mStartingState;
72564c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mTetheredState;
7262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
72764c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mUnavailableState;
7282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private boolean mAvailable;
7302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private boolean mTethered;
7315a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        int mLastError;
7322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String mIfaceName;
7346a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        String mMyUpstreamIfaceName;  // may change over time
7356a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
73665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean mUsb;
7372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
738dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        TetherInterfaceSM(String name, Looper looper, boolean usb) {
739dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            super(name, looper);
7402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaceName = name;
74165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            mUsb = usb;
7425a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
7432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mInitialState = new InitialState();
7452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mInitialState);
7462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStartingState = new StartingState();
7472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStartingState);
7482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mTetheredState = new TetheredState();
7492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mTetheredState);
7502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mUnavailableState = new UnavailableState();
7512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mUnavailableState);
7522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            setInitialState(mInitialState);
7542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public String toString() {
7572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            String res = new String();
7582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            res += mIfaceName + " - ";
75964c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville            IState current = getCurrentState();
7602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mInitialState) res += "InitialState";
7612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mStartingState) res += "StartingState";
7622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mTetheredState) res += "TetheredState";
7632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mUnavailableState) res += "UnavailableState";
7642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (mAvailable) res += " - Available";
7652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (mTethered) res += " - Tethered";
7665a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            res += " - lastError =" + mLastError;
7672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return res;
7682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7705a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        public synchronized int getLastError() {
7715a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return mLastError;
7725a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
7735a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
7745a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        private synchronized void setLastError(int error) {
7755a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            mLastError = error;
7765a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
7775a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            if (isErrored()) {
7785a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                if (mUsb) {
7795a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    // note everything's been unwound by this point so nothing to do on
7805a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    // further error..
7815a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    Tethering.this.configureUsbIface(false);
7825a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                }
7835a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            }
7845a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
7855a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
7862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // synchronized between this getter and the following setter
7872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public synchronized boolean isAvailable() {
7882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return mAvailable;
7892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private synchronized void setAvailable(boolean available) {
7922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mAvailable = available;
7932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // synchronized between this getter and the following setter
7962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public synchronized boolean isTethered() {
7972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return mTethered;
7982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
8002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private synchronized void setTethered(boolean tethered) {
8012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mTethered = tethered;
8022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
8042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // synchronized between this getter and the following setter
8052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public synchronized boolean isErrored() {
8065a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return (mLastError != ConnectivityManager.TETHER_ERROR_NO_ERROR);
8072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
80964c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class InitialState extends State {
8102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
8112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
8122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(true);
8132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(false);
8142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
8152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
8172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
8182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
819924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "InitialState.processMessage what=" + message.what);
8202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
8212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
8222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_REQUESTED:
8235a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
8246a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_REQUESTED,
8256a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
8262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mStartingState);
8272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
8292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mUnavailableState);
8302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
8322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
8332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
8352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
8362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
83964c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class StartingState extends State {
8402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
8412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
8422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
84365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                if (mUsb) {
844d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    if (!Tethering.this.configureUsbIface(true)) {
8456a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
8466a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
8475a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastError(ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
8485a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
849951749ba2e014566553d17c512e947472951a060Wink Saville                        transitionTo(mInitialState);
85065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        return;
85165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    }
85265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
8532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
8546a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
855951749ba2e014566553d17c512e947472951a060Wink Saville                // Skipping StartingState
856951749ba2e014566553d17c512e947472951a060Wink Saville                transitionTo(mTetheredState);
8572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
8592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
860924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "StartingState.processMessage what=" + message.what);
8612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
8622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
8632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    // maybe a parent class?
8642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_UNREQUESTED:
8656a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
8666a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
86765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        if (mUsb) {
868d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                            if (!Tethering.this.configureUsbIface(false)) {
8695a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                setLastErrorAndTransitionToInitialState(
8705a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
87165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                                break;
87265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
87365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
8742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
8752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_CELL_DUN_ERROR:
8772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_ENABLE_ERROR:
8782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_DISABLE_ERROR:
8792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_START_TETHERING_ERROR:
8802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_STOP_TETHERING_ERROR:
8812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_SET_DNS_FORWARDERS_ERROR:
8825a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastErrorAndTransitionToInitialState(
8835a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                ConnectivityManager.TETHER_ERROR_MASTER_ERROR);
8842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
8866a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
8876a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
8882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mUnavailableState);
8892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
8912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
8922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
8932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
8942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
89764c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class TetheredState extends State {
8982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
8992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
9002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
901c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.tetherInterface(mIfaceName);
9022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {
9035a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    setLastError(ConnectivityManager.TETHER_ERROR_TETHER_IFACE_ERROR);
9045a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
905951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mInitialState);
9062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    return;
9072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
908924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "Tethered " + mIfaceName);
9092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
9102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(true);
9112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
9122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
9132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
9142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
915924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "TetheredState.processMessage what=" + message.what);
9162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
9172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean error = false;
9182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
9192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_UNREQUESTED:
9202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
9216a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (mMyUpstreamIfaceName != null) {
9225a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            try {
923c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                                mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName);
924b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mMyUpstreamIfaceName = null;
9256a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
9266a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
927c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                                    mNMService.untetherInterface(mIfaceName);
9286a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
9295a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
9306a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastErrorAndTransitionToInitialState(
9316a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                        ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR);
9326a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                break;
9336a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
9342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
9352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        try {
936c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                            mNMService.untetherInterface(mIfaceName);
9372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } catch (Exception e) {
9385a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
9395a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR);
9402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
9412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
9426a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
9436a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
9442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (message.what == CMD_TETHER_UNREQUESTED) {
94565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            if (mUsb) {
946d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                                if (!Tethering.this.configureUsbIface(false)) {
9475a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    setLastError(
9485a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                            ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
94965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                                }
95065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
9515a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            transitionTo(mInitialState);
9522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } else if (message.what == CMD_INTERFACE_DOWN) {
9532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            transitionTo(mUnavailableState);
9542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
955924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                        if (DEBUG) Log.d(TAG, "Untethered " + mIfaceName);
9562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
9576a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_TETHER_CONNECTION_CHANGED:
9586a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        String newUpstreamIfaceName = (String)(message.obj);
9591039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        if ((mMyUpstreamIfaceName == null && newUpstreamIfaceName == null) ||
9601039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                                (mMyUpstreamIfaceName != null &&
9611039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                                mMyUpstreamIfaceName.equals(newUpstreamIfaceName))) {
9621039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                            if (DEBUG) Log.d(TAG, "Connection changed noop - dropping");
9631039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                            break;
9641039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        }
9656a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (mMyUpstreamIfaceName != null) {
9666a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            try {
967c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                                mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName);
968b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mMyUpstreamIfaceName = null;
9696a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
9706a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
971c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                                    mNMService.untetherInterface(mIfaceName);
9726a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
9736a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
9746a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastErrorAndTransitionToInitialState(
9756a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                        ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR);
9766a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                break;
9776a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
9786a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        }
9796a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (newUpstreamIfaceName != null) {
9806a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            try {
981c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                                mNMService.enableNat(mIfaceName, newUpstreamIfaceName);
9826a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
9836a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
984c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                                    mNMService.untetherInterface(mIfaceName);
9856a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
9866a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
9876a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastError(ConnectivityManager.TETHER_ERROR_ENABLE_NAT_ERROR);
9886a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                transitionTo(mInitialState);
9896a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                return true;
9906a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
9916a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        }
9926a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mMyUpstreamIfaceName = newUpstreamIfaceName;
9936a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        break;
9942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_CELL_DUN_ERROR:
9952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_ENABLE_ERROR:
9962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_DISABLE_ERROR:
9972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_START_TETHERING_ERROR:
9982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_STOP_TETHERING_ERROR:
9992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_SET_DNS_FORWARDERS_ERROR:
10002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        error = true;
10012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        // fall through
10022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_DEAD:
10036a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (mMyUpstreamIfaceName != null) {
10045a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            try {
1005c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                                mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName);
1006b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mMyUpstreamIfaceName = null;
10076a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
10086a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
1009c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                                    mNMService.untetherInterface(mIfaceName);
10106a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
10115a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
10126a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastErrorAndTransitionToInitialState(
10136a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                        ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR);
10146a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                break;
10156a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
10162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
10172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        try {
1018c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                            mNMService.untetherInterface(mIfaceName);
10192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } catch (Exception e) {
10205a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
10215a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR);
10222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
10232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
10242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (error) {
10255a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
10265a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_MASTER_ERROR);
10272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
10282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
1029924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                        if (DEBUG) Log.d(TAG, "Tether lost upstream connection " + mIfaceName);
10302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        sendTetherStateChangedBroadcast();
103165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        if (mUsb) {
1032d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                            if (!Tethering.this.configureUsbIface(false)) {
10335a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                setLastError(ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
103465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
103565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
10362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
10372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
10392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
10402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
10422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
10432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
10442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
10452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
104664c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class UnavailableState extends State {
10472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
10482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
10492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
10505a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
10512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(false);
10522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
10532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
10542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
10552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
10562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
10572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
10582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_UP:
10592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
10602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
10622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
10632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
10652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
10662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
10672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
10682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10695a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        void setLastErrorAndTransitionToInitialState(int error) {
10705a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            setLastError(error);
10715a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            transitionTo(mInitialState);
10722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
107365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
10742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
10752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
107664c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville    class TetherMasterSM extends StateMachine {
10772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // an interface SM has requested Tethering
10782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        static final int CMD_TETHER_MODE_REQUESTED   = 1;
10792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // an interface SM has unrequested Tethering
10802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        static final int CMD_TETHER_MODE_UNREQUESTED = 2;
10816a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // upstream connection change - do the right thing
10826a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_UPSTREAM_CHANGED        = 3;
10832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // we received notice that the cellular DUN connection is up
10846a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_CELL_CONNECTION_RENEW   = 4;
10856a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // we don't have a valid upstream conn, check again after a delay
1086951749ba2e014566553d17c512e947472951a060Wink Saville        static final int CMD_RETRY_UPSTREAM          = 5;
10872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // This indicates what a timeout event relates to.  A state that
10892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // sends itself a delayed timeout event and handles incoming timeout events
10902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // should inc this when it is entered and whenever it sends a new timeout event.
10912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // We do not flush the old ones.
10922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private int mSequenceNumber;
10932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
109464c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mInitialState;
109564c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mTetherModeAliveState;
10962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
109764c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mSetIpForwardingEnabledErrorState;
109864c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mSetIpForwardingDisabledErrorState;
109964c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mStartTetheringErrorState;
110064c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mStopTetheringErrorState;
110164c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mSetDnsForwardersErrorState;
11022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private ArrayList mNotifyList;
11042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11051039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt        private int mCurrentConnectionSequence;
1106ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        private int mMobileApnReserved = ConnectivityManager.TYPE_NONE;
11072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1108b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt        private String mUpstreamIfaceName = null;
1109b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt
11106a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        private static final int UPSTREAM_SETTLE_TIME_MS     = 10000;
11116a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        private static final int CELL_CONNECTION_RENEW_MS    = 40000;
11122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1113dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        TetherMasterSM(String name, Looper looper) {
1114dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            super(name, looper);
11152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            //Add states
11172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mInitialState = new InitialState();
11182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mInitialState);
11192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mTetherModeAliveState = new TetherModeAliveState();
11202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mTetherModeAliveState);
11212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetIpForwardingEnabledErrorState = new SetIpForwardingEnabledErrorState();
11232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetIpForwardingEnabledErrorState);
11242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetIpForwardingDisabledErrorState = new SetIpForwardingDisabledErrorState();
11252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetIpForwardingDisabledErrorState);
11262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStartTetheringErrorState = new StartTetheringErrorState();
11272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStartTetheringErrorState);
11282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStopTetheringErrorState = new StopTetheringErrorState();
11292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStopTetheringErrorState);
11302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetDnsForwardersErrorState = new SetDnsForwardersErrorState();
11312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetDnsForwardersErrorState);
11322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mNotifyList = new ArrayList();
11342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            setInitialState(mInitialState);
11352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
11362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
113764c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class TetherMasterUtilState extends State {
11386a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected final static boolean TRY_TO_SETUP_MOBILE_CONNECTION = true;
11396a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected final static boolean WAIT_FOR_NETWORK_TO_SETTLE     = false;
11406a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
1141d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            @Override
1142d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            public boolean processMessage(Message m) {
1143d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return false;
1144d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
1145ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            protected String enableString(int apnType) {
1146ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                switch (apnType) {
1147ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                case ConnectivityManager.TYPE_MOBILE_DUN:
1148ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    return Phone.FEATURE_ENABLE_DUN_ALWAYS;
1149ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                case ConnectivityManager.TYPE_MOBILE:
1150ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                case ConnectivityManager.TYPE_MOBILE_HIPRI:
1151ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    return Phone.FEATURE_ENABLE_HIPRI;
1152ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                }
1153ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                return null;
1154ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            }
1155ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            protected boolean turnOnUpstreamMobileConnection(int apnType) {
11561039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                boolean retValue = true;
1157ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (apnType == ConnectivityManager.TYPE_NONE) return false;
1158ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (apnType != mMobileApnReserved) turnOffUpstreamMobileConnection();
1159d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
1160c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b);
11611039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                int result = Phone.APN_REQUEST_FAILED;
1162ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                String enableString = enableString(apnType);
1163ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (enableString == null) return false;
1164d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1165c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    result = cm.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
1166ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                            enableString, new Binder());
1167d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1168d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
11691039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                switch (result) {
11706a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                case Phone.APN_ALREADY_ACTIVE:
11716a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                case Phone.APN_REQUEST_STARTED:
1172ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    mMobileApnReserved = apnType;
11731039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    Message m = obtainMessage(CMD_CELL_CONNECTION_RENEW);
11741039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    m.arg1 = ++mCurrentConnectionSequence;
11751039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    sendMessageDelayed(m, CELL_CONNECTION_RENEW_MS);
11766a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    break;
11776a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                case Phone.APN_REQUEST_FAILED:
11786a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                default:
11791039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    retValue = false;
11806a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    break;
11816a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
11826a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
1183d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return retValue;
1184d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
1185ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            protected boolean turnOffUpstreamMobileConnection() {
1186ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (mMobileApnReserved != ConnectivityManager.TYPE_NONE) {
11876a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
1188c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b);
11896a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    try {
1190c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                        cm.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
1191ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                                enableString(mMobileApnReserved));
11926a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    } catch (Exception e) {
11936a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        return false;
11946a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    }
1195ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    mMobileApnReserved = ConnectivityManager.TYPE_NONE;
1196d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1197d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1198d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
11996a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected boolean turnOnMasterTetherSettings() {
1200d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1201c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.setIpForwardingEnabled(true);
1202d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1203951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mSetIpForwardingEnabledErrorState);
1204d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1205d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1206d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1207c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.startTethering(mDhcpRange);
1208d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1209547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                    try {
1210c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                        mNMService.stopTethering();
1211c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                        mNMService.startTethering(mDhcpRange);
1212547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                    } catch (Exception ee) {
1213547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                        transitionTo(mStartTetheringErrorState);
1214547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                        return false;
1215547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                    }
1216d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1217d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1218c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.setDnsForwarders(mDnsServers);
1219d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1220951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mSetDnsForwardersErrorState);
1221d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1222d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1223d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1224d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
12256a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected boolean turnOffMasterTetherSettings() {
1226d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1227c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.stopTethering();
1228d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1229d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    transitionTo(mStopTetheringErrorState);
1230d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1231d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1232d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1233c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.setIpForwardingEnabled(false);
1234d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1235d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    transitionTo(mSetIpForwardingDisabledErrorState);
1236d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1237d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1238d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                transitionTo(mInitialState);
1239d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1240d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
1241ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
1242ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            protected void chooseUpstreamType(boolean tryCell) {
124305d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
124405d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b);
1245ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                int upType = ConnectivityManager.TYPE_NONE;
1246ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                String iface = null;
124705d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt
1248ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                for (Integer netType : mUpstreamIfaceTypes) {
1249ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    NetworkInfo info = null;
1250ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    try {
1251ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                        info = cm.getNetworkInfo(netType.intValue());
1252ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    } catch (RemoteException e) { }
1253ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    if ((info != null) && info.isConnected()) {
1254ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                        upType = netType.intValue();
1255ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                        break;
1256c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    }
1257c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                }
12581039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt
1259924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) {
1260ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    Log.d(TAG, "chooseUpstreamType(" + tryCell + "), preferredApn ="
1261ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                            + mPreferredUpstreamMobileApn + ", got type=" + upType);
1262924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                }
1263ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
1264ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                // if we're on DUN, put our own grab on it
1265ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (upType == ConnectivityManager.TYPE_MOBILE_DUN ||
1266ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                        upType == ConnectivityManager.TYPE_MOBILE_HIPRI) {
1267ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    turnOnUpstreamMobileConnection(upType);
12686a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
1269ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
1270ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (upType == ConnectivityManager.TYPE_NONE) {
1271ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    boolean tryAgainLater = true;
1272ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    if ((tryCell == TRY_TO_SETUP_MOBILE_CONNECTION) &&
1273ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                            (turnOnUpstreamMobileConnection(mPreferredUpstreamMobileApn) == true)) {
1274ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                        // we think mobile should be coming up - don't set a retry
1275ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                        tryAgainLater = false;
12761039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    }
1277ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    if (tryAgainLater) {
12781039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        sendMessageDelayed(CMD_RETRY_UPSTREAM, UPSTREAM_SETTLE_TIME_MS);
127977e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                    }
1280ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                } else {
1281ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    LinkProperties linkProperties = null;
1282ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    try {
1283ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                        linkProperties = cm.getLinkProperties(upType);
1284ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    } catch (RemoteException e) { }
1285ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    if (linkProperties != null) iface = linkProperties.getInterfaceName();
12866a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
1287b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                notifyTetheredOfNewUpstreamIface(iface);
12886a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            }
1289ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
1290b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt            protected void notifyTetheredOfNewUpstreamIface(String ifaceName) {
1291924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "notifying tethered with iface =" + ifaceName);
1292b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                mUpstreamIfaceName = ifaceName;
12936a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                for (Object o : mNotifyList) {
12946a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    TetherInterfaceSM sm = (TetherInterfaceSM)o;
12956a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED,
12966a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            ifaceName);
12972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
12982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
12992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
13002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
13016a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        class InitialState extends TetherMasterUtilState {
13022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
13042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
1307924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "MasterInitialState.processMessage what=" + message.what);
13082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
13092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
13102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
1311ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                        checkDunRequired();
13122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
1313924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                        if (DEBUG) Log.d(TAG, "Tether Mode requested by " + who.toString());
13142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        mNotifyList.add(who);
13156a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        transitionTo(mTetherModeAliveState);
13162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_UNREQUESTED:
13182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        who = (TetherInterfaceSM)message.obj;
1319924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                        if (DEBUG) Log.d(TAG, "Tether Mode unrequested by " + who.toString());
13202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        int index = mNotifyList.indexOf(who);
13212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (index != -1) {
13226a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            mNotifyList.remove(who);
13232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
13242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
13262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
13272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
13292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
13302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
13322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1333d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        class TetherModeAliveState extends TetherMasterUtilState {
133419b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt            boolean mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE;
13352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
133719b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt                mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE; // better try something first pass
133819b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt                                                        // or crazy tests cases will fail
13396a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                chooseUpstreamType(mTryCell);
13406a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                mTryCell = !mTryCell;
13416a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                turnOnMasterTetherSettings(); // may transition us out
13426a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            }
13436a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            @Override
13446a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            public void exit() {
1345ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                turnOffUpstreamMobileConnection();
1346b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                notifyTetheredOfNewUpstreamIface(null);
13472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
1350924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "TetherModeAliveState.processMessage what=" + message.what);
13512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
13522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
13532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
13542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
13552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        mNotifyList.add(who);
1356b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                        who.sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED,
1357b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mUpstreamIfaceName);
13582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_UNREQUESTED:
13602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        who = (TetherInterfaceSM)message.obj;
13612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        int index = mNotifyList.indexOf(who);
13622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (index != -1) {
13632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            mNotifyList.remove(index);
13642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            if (mNotifyList.isEmpty()) {
1365d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                                turnOffMasterTetherSettings(); // transitions appropriately
13662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            }
13672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
13682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13696a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_UPSTREAM_CHANGED:
137005d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                        // need to try DUN immediately if Wifi goes down
137105d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                        mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE;
13726a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        chooseUpstreamType(mTryCell);
13736a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTryCell = !mTryCell;
13742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13756a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_CELL_CONNECTION_RENEW:
13766a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        // make sure we're still using a requested connection - may have found
13776a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        // wifi or something since then.
13781039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        if (mCurrentConnectionSequence == message.arg1) {
1379924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                            if (DEBUG) {
1380924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                                Log.d(TAG, "renewing mobile connection - requeuing for another " +
1381924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                                        CELL_CONNECTION_RENEW_MS + "ms");
1382924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                            }
1383ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                            turnOnUpstreamMobileConnection(mMobileApnReserved);
13846a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        }
138565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        break;
138601758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                    case CMD_RETRY_UPSTREAM:
138701758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                        chooseUpstreamType(mTryCell);
138801758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                        mTryCell = !mTryCell;
138901758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                        break;
139001758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                    default:
139101758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                        retValue = false;
139201758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                        break;
13932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
13942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
13952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
13972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
139864c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class ErrorState extends State {
13992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            int mErrorNotification;
14002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
14022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
14032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
14042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
14052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
14066a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        who.sendMessage(mErrorNotification);
14072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
14082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
14092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                       retValue = false;
14102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
14112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
14122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            void notify(int msgType) {
14142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                mErrorNotification = msgType;
14152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                for (Object o : mNotifyList) {
14162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    TetherInterfaceSM sm = (TetherInterfaceSM)o;
14176a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(msgType);
14182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
14192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetIpForwardingEnabledErrorState extends ErrorState {
14232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setIpForwardingEnabled");
14262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_IP_FORWARDING_ENABLE_ERROR);
14272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetIpForwardingDisabledErrorState extends ErrorState {
14312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setIpForwardingDisabled");
14342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_IP_FORWARDING_DISABLE_ERROR);
14352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class StartTetheringErrorState extends ErrorState {
14392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in startTethering");
14422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_START_TETHERING_ERROR);
14432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
1444c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.setIpForwardingEnabled(false);
14452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
14462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class StopTetheringErrorState extends ErrorState {
14502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in stopTethering");
14532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_STOP_TETHERING_ERROR);
14542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
1455c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.setIpForwardingEnabled(false);
14562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
14572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetDnsForwardersErrorState extends ErrorState {
14612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setDnsForwarders");
14642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_SET_DNS_FORWARDERS_ERROR);
14652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
1466c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.stopTethering();
14672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
14682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
1469c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.setIpForwardingEnabled(false);
14702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
14712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
1472d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
1473d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
1474d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
14752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
14762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (mContext.checkCallingOrSelfPermission(
14772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
14782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            pw.println("Permission Denial: can't dump ConnectivityService.Tether " +
14792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    "from from pid=" + Binder.getCallingPid() + ", uid=" +
14802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    Binder.getCallingUid());
14812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    return;
14822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        pw.println();
14852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        pw.println("Tether state:");
14862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
14872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object o : mIfaces.values()) {
14882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                pw.println(" "+o.toString());
14892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
1490d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
14912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        pw.println();
14922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return;
1493d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
1494d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt}
1495