Tethering.java revision 23eb297c771bd996fe8559178feb1a07be43ad72
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 interfaceStatusChanged(String iface, boolean up) {
1936143f5f7392fb0d3e5702a46a2415bd0ecb6efb4Mike J. Chen        if (DEBUG) Log.d(TAG, "interfaceStatusChanged " + iface + ", " + up);
1942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        boolean found = false;
19565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean usb = false;
196a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (isWifi(iface)) {
197a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
198a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        } else if (isUsb(iface)) {
199a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
200a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            usb = true;
2016fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        } else if (isBluetooth(iface)) {
2026fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            found = true;
2032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
2042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (found == false) return;
2052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
2062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
2072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
2086143f5f7392fb0d3e5702a46a2415bd0ecb6efb4Mike J. Chen            if (up) {
2092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm == null) {
210dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt                    sm = new TetherInterfaceSM(iface, mLooper, usb);
2112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    mIfaces.put(iface, sm);
2122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    sm.start();
2132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
2142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            } else {
2152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm != null) {
2166a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(TetherInterfaceSM.CMD_INTERFACE_DOWN);
2172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    mIfaces.remove(iface);
2182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
2192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
2202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
221d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
222d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
223f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen    public void interfaceLinkStateChanged(String iface, boolean up) {
22423eb297c771bd996fe8559178feb1a07be43ad72Irfan Sheriff        if (DEBUG) Log.d(TAG, "interfaceLinkStateChanged " + iface + ", " + up);
22523eb297c771bd996fe8559178feb1a07be43ad72Irfan Sheriff        interfaceStatusChanged(iface, up);
226f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen    }
227f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen
228a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private boolean isUsb(String iface) {
229a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        for (String regex : mTetherableUsbRegexs) {
230a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (iface.matches(regex)) return true;
231a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
232a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        return false;
233a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
234a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
235a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    public boolean isWifi(String iface) {
236a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        for (String regex : mTetherableWifiRegexs) {
237a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (iface.matches(regex)) return true;
238a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
239a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        return false;
240a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
241a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
2426fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    public boolean isBluetooth(String iface) {
2436fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        for (String regex : mTetherableBluetoothRegexs) {
2446fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            if (iface.matches(regex)) return true;
2456fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        }
2466fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        return false;
2476fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    }
2482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void interfaceAdded(String iface) {
2492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        boolean found = false;
25065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean usb = false;
251a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (isWifi(iface)) {
252a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
2532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
254a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (isUsb(iface)) {
255a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
256a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            usb = true;
257d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
2586fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        if (isBluetooth(iface)) {
2596fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            found = true;
2606fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        }
2612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (found == false) {
262924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt            if (DEBUG) Log.d(TAG, iface + " is not a tetherable iface, ignoring");
263d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            return;
264d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
26565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
2662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
2672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
2682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (sm != null) {
269924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "active iface (" + iface + ") reported as added, ignoring");
2702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return;
2712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
272dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            sm = new TetherInterfaceSM(iface, mLooper, usb);
2732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaces.put(iface, sm);
2742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm.start();
2752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
276924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) Log.d(TAG, "interfaceAdded :" + iface);
277d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
278d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
2792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void interfaceRemoved(String iface) {
2802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
2812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
2822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (sm == null) {
283924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) {
284924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                    Log.e(TAG, "attempting to remove unknown iface (" + iface + "), ignoring");
285924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                }
2862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return;
2872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
2886a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            sm.sendMessage(TetherInterfaceSM.CMD_INTERFACE_DOWN);
2892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaces.remove(iface);
290d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
291d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
292d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
29312b933d0d9252decaae9fee2456bb1e1cd94c085JP Abgrall    public void limitReached(String limitName, String iface) {}
29412b933d0d9252decaae9fee2456bb1e1cd94c085JP Abgrall
2955a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int tether(String iface) {
296d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Log.d(TAG, "Tethering " + iface);
2972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        TetherInterfaceSM sm = null;
2982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
2992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm = mIfaces.get(iface);
300d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm == null) {
3022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Tether an unknown iface :" + iface + ", ignoring");
3035a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
304d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3055a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        if (!sm.isAvailable() && !sm.isErrored()) {
3062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Tether an unavailable iface :" + iface + ", ignoring");
3075a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
308d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3096a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        sm.sendMessage(TetherInterfaceSM.CMD_TETHER_REQUESTED);
3105a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return ConnectivityManager.TETHER_ERROR_NO_ERROR;
311d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
312d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3135a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int untether(String iface) {
314d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Log.d(TAG, "Untethering " + iface);
3152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        TetherInterfaceSM sm = null;
3162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
3172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm = mIfaces.get(iface);
3182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
3192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm == null) {
3202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Untether an unknown iface :" + iface + ", ignoring");
3215a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
322d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm.isErrored()) {
3242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Untethered an errored iface :" + iface + ", ignoring");
3255a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
326d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3276a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        sm.sendMessage(TetherInterfaceSM.CMD_TETHER_UNREQUESTED);
3285a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return ConnectivityManager.TETHER_ERROR_NO_ERROR;
3295a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
3305a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
3315a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int getLastTetherError(String iface) {
3325a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        TetherInterfaceSM sm = null;
3335a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        synchronized (mIfaces) {
3345a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            sm = mIfaces.get(iface);
3355a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
3365a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        if (sm == null) {
3375a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            Log.e(TAG, "Tried to getLastTetherError on an unknown iface :" + iface + ", ignoring");
3385a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
3395a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
3405a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return sm.getLastError();
3412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
342d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private void sendTetherStateChangedBroadcast() {
3442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
345c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh        IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b);
346d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        try {
347c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh            if (!cm.isTetheringSupported()) return;
3482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        } catch (RemoteException e) {
3492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return;
350d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
351d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> availableList = new ArrayList<String>();
3532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> activeList = new ArrayList<String>();
3542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> erroredList = new ArrayList<String>();
3552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
356a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        boolean wifiTethered = false;
357a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        boolean usbTethered = false;
3586fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        boolean bluetoothTethered = false;
359a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
3602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
3612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set ifaces = mIfaces.keySet();
3622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object iface : ifaces) {
3632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(iface);
3642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm != null) {
3656fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                    if (sm.isErrored()) {
3662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        erroredList.add((String)iface);
3672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    } else if (sm.isAvailable()) {
3682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        availableList.add((String)iface);
3692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    } else if (sm.isTethered()) {
370a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        if (isUsb((String)iface)) {
371a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            usbTethered = true;
372a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        } else if (isWifi((String)iface)) {
373a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            wifiTethered = true;
3746fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                      } else if (isBluetooth((String)iface)) {
3756fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                            bluetoothTethered = true;
376a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        }
3772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        activeList.add((String)iface);
3782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    }
3792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
380d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            }
381d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
382d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Intent broadcast = new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
383d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        broadcast.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
3842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_AVAILABLE_TETHER,
3852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                availableList);
3862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ACTIVE_TETHER, activeList);
3872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ERRORED_TETHER,
3882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                erroredList);
3892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mContext.sendStickyBroadcast(broadcast);
390924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) {
391924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt            Log.d(TAG, "sendTetherStateChangedBroadcast " + availableList.size() + ", " +
392924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                    activeList.size() + ", " + erroredList.size());
393924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        }
394a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
395a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (usbTethered) {
3966fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            if (wifiTethered || bluetoothTethered) {
397a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general);
398a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            } else {
399a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_usb);
400a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            }
401a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        } else if (wifiTethered) {
4026fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            if (bluetoothTethered) {
4036fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general);
4046fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            } else {
4056fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_wifi);
4066fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            }
4076fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        } else if (bluetoothTethered) {
4086fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_bluetooth);
409a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        } else {
410a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            clearTetheredNotification();
411a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
412a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
413a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
414a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private void showTetheredNotification(int icon) {
415a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        NotificationManager notificationManager =
416a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
417a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (notificationManager == null) {
418a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            return;
419a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
420a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
421db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt        if (mTetheredNotification != null) {
422db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            if (mTetheredNotification.icon == icon) {
423db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt                return;
424db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            }
425db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            notificationManager.cancel(mTetheredNotification.icon);
426db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt        }
427db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt
428a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        Intent intent = new Intent();
429a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        intent.setClassName("com.android.settings", "com.android.settings.TetherSettings");
430a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
431a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
432a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);
433a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
434a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        Resources r = Resources.getSystem();
435a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        CharSequence title = r.getText(com.android.internal.R.string.tethered_notification_title);
436a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        CharSequence message = r.getText(com.android.internal.R.string.
437a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                tethered_notification_message);
438a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
4396fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        if (mTetheredNotification == null) {
440a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification = new Notification();
441a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification.when = 0;
442a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
443a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.icon = icon;
444a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.defaults &= ~Notification.DEFAULT_SOUND;
445a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.flags = Notification.FLAG_ONGOING_EVENT;
446a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.tickerText = title;
447a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.setLatestEventInfo(mContext, title, message, pi);
448a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
449a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        notificationManager.notify(mTetheredNotification.icon, mTetheredNotification);
450a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
451a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
452a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private void clearTetheredNotification() {
453a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        NotificationManager notificationManager =
454a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
455a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (notificationManager != null && mTetheredNotification != null) {
456a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            notificationManager.cancel(mTetheredNotification.icon);
457a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification = null;
458a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
4595a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
4605a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
461030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private void updateUsbStatus() {
462030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        boolean enable = mUsbConnected && mUsbMassStorageOff;
463030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
464030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        if (mBooted) {
465030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            enableUsbIfaces(enable);
466030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        }
467030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    }
468030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
4692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private class StateReceiver extends BroadcastReceiver {
470d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        public void onReceive(Context content, Intent intent) {
4712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            String action = intent.getAction();
472770126a678ccc9328a89407ffc82f4d998b25427Mike Lockwood            if (action.equals(UsbManager.ACTION_USB_STATE)) {
473b92df0f66926252cf60cdda7812bb1d50ce9378eMike Lockwood                mUsbConnected = intent.getExtras().getBoolean(UsbManager.USB_CONNECTED);
474b92df0f66926252cf60cdda7812bb1d50ce9378eMike Lockwood                updateUsbStatus();
475030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
476030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                mUsbMassStorageOff = false;
477030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                updateUsbStatus();
478030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            }
479030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
480030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                mUsbMassStorageOff = true;
481030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                updateUsbStatus();
4822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
483924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "Tethering got CONNECTIVITY_ACTION");
4846a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED);
485c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt            } else if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
486c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                mBooted = true;
487030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                updateUsbStatus();
488d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            }
489d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
490d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
491d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
49265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    // used on cable insert/remove
493d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private void enableUsbIfaces(boolean enable) {
4943c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood        // add/remove USB interfaces when USB is connected/disconnected
4953c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood        for (String intf : mTetherableUsbRegexs) {
4963c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            if (enable) {
4973c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                interfaceAdded(intf);
4983c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            } else {
4993c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                interfaceRemoved(intf);
5003c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            }
5013c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood        }
5023c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood
50365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        String[] ifaces = new String[0];
50465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        try {
505c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh            ifaces = mNMService.listInterfaces();
50665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        } catch (Exception e) {
5073c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            Log.e(TAG, "Error listing Interfaces", e);
50865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            return;
50965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
51065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        for (String iface : ifaces) {
511a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (isUsb(iface)) {
512a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                if (enable) {
513a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    interfaceAdded(iface);
514a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                } else {
515a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    interfaceRemoved(iface);
51665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
51765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
51865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
51965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    }
52065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
5216fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    // toggled when we enter/leave the fully tethered state
522d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private boolean enableUsbRndis(boolean enabled) {
523924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) Log.d(TAG, "enableUsbRndis(" + enabled + ")");
52465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
5253c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood        UsbManager usbManager = (UsbManager)mContext.getSystemService(Context.USB_SERVICE);
5263c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood        if (usbManager == null) {
5273c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            Log.d(TAG, "could not get UsbManager");
5283c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            return false;
5293c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood        }
53065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        try {
53165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            if (enabled) {
532f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood                usbManager.setCurrentFunction(UsbManager.USB_FUNCTION_RNDIS, false);
53365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            } else {
534f59717ddb5ef324ee3fdb12b83e7d1b709793d28Mike Lockwood                usbManager.setCurrentFunction(null, false);
53565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
53665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        } catch (Exception e) {
5373c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            Log.e(TAG, "Error toggling usb RNDIS", e);
53865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            return false;
53965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
54065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        return true;
54165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    }
54265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
54365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    // configured when we start tethering and unconfig'd on error or conclusion
544d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private boolean configureUsbIface(boolean enabled) {
545924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) Log.d(TAG, "configureUsbIface(" + enabled + ")");
54665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
5473c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood        if (enabled) {
5483c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            // must enable RNDIS first to create the interface
5493c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            enableUsbRndis(enabled);
55065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
5513c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood
5523c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood        try {
5533c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            // bring toggle the interfaces
5543c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            String[] ifaces = new String[0];
5553c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            try {
5563c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                ifaces = mNMService.listInterfaces();
5573c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            } catch (Exception e) {
5583c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                Log.e(TAG, "Error listing Interfaces", e);
5593c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                return false;
5603c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            }
5613c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            for (String iface : ifaces) {
5623c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                if (isUsb(iface)) {
5633c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                    InterfaceConfiguration ifcg = null;
5643c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                    try {
5653c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                        ifcg = mNMService.getInterfaceConfig(iface);
5663c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                        if (ifcg != null) {
5673c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                            InetAddress addr = NetworkUtils.numericToInetAddress(USB_NEAR_IFACE_ADDR);
5683c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                            ifcg.addr = new LinkAddress(addr, USB_PREFIX_LENGTH);
5693c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                            if (enabled) {
5703c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                                ifcg.interfaceFlags = ifcg.interfaceFlags.replace("down", "up");
5713c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                            } else {
5723c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                                ifcg.interfaceFlags = ifcg.interfaceFlags.replace("up", "down");
5733c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                            }
5743c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                            ifcg.interfaceFlags = ifcg.interfaceFlags.replace("running", "");
5753c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                            ifcg.interfaceFlags = ifcg.interfaceFlags.replace("  "," ");
5763c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                            mNMService.setInterfaceConfig(iface, ifcg);
57765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
5783c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                    } catch (Exception e) {
5793c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                        Log.e(TAG, "Error configuring interface " + iface, e);
5803c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                        return false;
58165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    }
58265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
5833c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood             }
5843c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood        } finally {
5853c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood            if (!enabled) {
5863c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood                enableUsbRndis(false);
58765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
58865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
58965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
59065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        return true;
59165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    }
59265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
5932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetherableUsbRegexs() {
5942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return mTetherableUsbRegexs;
5952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
5962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
5972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetherableWifiRegexs() {
5982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return mTetherableWifiRegexs;
5992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
6002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6016fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    public String[] getTetherableBluetoothRegexs() {
6026fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        return mTetherableBluetoothRegexs;
6036fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    }
6046fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang
605ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    public int[] getUpstreamIfaceTypes() {
606ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        int values[] = new int[mUpstreamIfaceTypes.size()];
607ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        Iterator<Integer> iterator = mUpstreamIfaceTypes.iterator();
608ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        for (int i=0; i < mUpstreamIfaceTypes.size(); i++) {
609ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            values[i] = iterator.next();
610ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        }
611ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        return values;
612c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    }
613c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt
614ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt    public void checkDunRequired() {
615ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        int requiredApn = ((Settings.Secure.getInt(mContext.getContentResolver(),
616ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                Settings.Secure.TETHER_DUN_REQUIRED, 0) == 1) ?
617ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                ConnectivityManager.TYPE_MOBILE_DUN :
618ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                ConnectivityManager.TYPE_MOBILE_HIPRI);
619ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        if (mPreferredUpstreamMobileApn != requiredApn) {
620ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            if (requiredApn == ConnectivityManager.TYPE_MOBILE_DUN) {
621ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                while (mUpstreamIfaceTypes.contains(MOBILE_TYPE)) {
622ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    mUpstreamIfaceTypes.remove(MOBILE_TYPE);
623ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                }
624ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                while (mUpstreamIfaceTypes.contains(HIPRI_TYPE)) {
625ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    mUpstreamIfaceTypes.remove(HIPRI_TYPE);
626ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                }
627ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (mUpstreamIfaceTypes.contains(DUN_TYPE) == false) {
628ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    mUpstreamIfaceTypes.add(DUN_TYPE);
629ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                }
630ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            } else {
631ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                while (mUpstreamIfaceTypes.contains(DUN_TYPE)) {
632ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    mUpstreamIfaceTypes.remove(DUN_TYPE);
633ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                }
634ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (mUpstreamIfaceTypes.contains(MOBILE_TYPE) == false) {
635ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    mUpstreamIfaceTypes.add(MOBILE_TYPE);
636ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                }
637ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (mUpstreamIfaceTypes.contains(HIPRI_TYPE) == false) {
638ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    mUpstreamIfaceTypes.add(HIPRI_TYPE);
639ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                }
640ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            }
641ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            mPreferredUpstreamMobileApn = requiredApn;
642ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        }
643c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    }
644c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt
6452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetheredIfaces() {
6462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> list = new ArrayList<String>();
6472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
6482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set keys = mIfaces.keySet();
6492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object key : keys) {
6502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
6512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm.isTethered()) {
6522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    list.add((String)key);
6532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
6542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
6552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String[] retVal = new String[list.size()];
6572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        for (int i=0; i < list.size(); i++) {
6582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            retVal[i] = list.get(i);
6592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return retVal;
6612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
6622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetherableIfaces() {
6642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> list = new ArrayList<String>();
6652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
6662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set keys = mIfaces.keySet();
6672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object key : keys) {
6682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
6692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm.isAvailable()) {
6702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    list.add((String)key);
6712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
6722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
6732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String[] retVal = new String[list.size()];
6752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        for (int i=0; i < list.size(); i++) {
6762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            retVal[i] = list.get(i);
6772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return retVal;
6792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
6802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6815a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public String[] getErroredIfaces() {
6825a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        ArrayList<String> list = new ArrayList<String>();
6835a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        synchronized (mIfaces) {
6845a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            Set keys = mIfaces.keySet();
6855a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            for (Object key : keys) {
6865a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
6875a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                if (sm.isErrored()) {
6885a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    list.add((String)key);
6895a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                }
6905a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            }
6915a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
6925a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        String[] retVal = new String[list.size()];
6935a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        for (int i= 0; i< list.size(); i++) {
6945a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            retVal[i] = list.get(i);
6955a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
6965a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return retVal;
6975a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
6985a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
6991ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo    //TODO: Temporary handling upstream change triggered without
7001ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo    //      CONNECTIVITY_ACTION. Only to accomodate interface
7011ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo    //      switch during HO.
7021ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo    //      @see bug/4455071
7031ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo    public void handleTetherIfaceChange() {
7041ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED);
7051ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo    }
7061ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo
70764c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville    class TetherInterfaceSM extends StateMachine {
7082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it's not in tether mode
7096a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_MODE_DEAD            =  1;
7102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // request from the user that it wants to tether
7116a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_REQUESTED            =  2;
7122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // request from the user that it wants to untether
7136a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_UNREQUESTED          =  3;
7142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification that this interface is down
7156a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_INTERFACE_DOWN              =  4;
7162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification that this interface is up
7176a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_INTERFACE_UP                =  5;
7182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had an error turning on cellular dun
7196a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_CELL_DUN_ERROR              =  6;
7202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble enabling IP Forwarding
7216a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_IP_FORWARDING_ENABLE_ERROR  =  7;
7222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble disabling IP Forwarding
7236a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_IP_FORWARDING_DISABLE_ERROR =  8;
7242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble staring tethering
7256a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_START_TETHERING_ERROR       =  9;
7262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble stopping tethering
7276a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_STOP_TETHERING_ERROR        = 10;
7282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble setting the DNS forwarders
7296a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_SET_DNS_FORWARDERS_ERROR    = 11;
7306a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // the upstream connection has changed
731951749ba2e014566553d17c512e947472951a060Wink Saville        static final int CMD_TETHER_CONNECTION_CHANGED   = 12;
7322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
73364c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mDefaultState;
7342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
73564c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mInitialState;
73664c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mStartingState;
73764c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mTetheredState;
7382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
73964c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mUnavailableState;
7402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private boolean mAvailable;
7422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private boolean mTethered;
7435a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        int mLastError;
7442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String mIfaceName;
7466a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        String mMyUpstreamIfaceName;  // may change over time
7476a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
74865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean mUsb;
7492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
750dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        TetherInterfaceSM(String name, Looper looper, boolean usb) {
751dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            super(name, looper);
7522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaceName = name;
75365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            mUsb = usb;
7545a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
7552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mInitialState = new InitialState();
7572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mInitialState);
7582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStartingState = new StartingState();
7592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStartingState);
7602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mTetheredState = new TetheredState();
7612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mTetheredState);
7622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mUnavailableState = new UnavailableState();
7632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mUnavailableState);
7642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            setInitialState(mInitialState);
7662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public String toString() {
7692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            String res = new String();
7702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            res += mIfaceName + " - ";
77164c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville            IState current = getCurrentState();
7722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mInitialState) res += "InitialState";
7732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mStartingState) res += "StartingState";
7742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mTetheredState) res += "TetheredState";
7752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mUnavailableState) res += "UnavailableState";
7762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (mAvailable) res += " - Available";
7772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (mTethered) res += " - Tethered";
7785a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            res += " - lastError =" + mLastError;
7792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return res;
7802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7825a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        public synchronized int getLastError() {
7835a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return mLastError;
7845a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
7855a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
7865a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        private synchronized void setLastError(int error) {
7875a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            mLastError = error;
7885a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
7895a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            if (isErrored()) {
7905a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                if (mUsb) {
7915a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    // note everything's been unwound by this point so nothing to do on
7925a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    // further error..
7935a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    Tethering.this.configureUsbIface(false);
7945a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                }
7955a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            }
7965a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
7975a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
7982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // synchronized between this getter and the following setter
7992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public synchronized boolean isAvailable() {
8002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return mAvailable;
8012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
8032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private synchronized void setAvailable(boolean available) {
8042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mAvailable = available;
8052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
8072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // synchronized between this getter and the following setter
8082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public synchronized boolean isTethered() {
8092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return mTethered;
8102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
8122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private synchronized void setTethered(boolean tethered) {
8132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mTethered = tethered;
8142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
8162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // synchronized between this getter and the following setter
8172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public synchronized boolean isErrored() {
8185a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return (mLastError != ConnectivityManager.TETHER_ERROR_NO_ERROR);
8192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
82164c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class InitialState extends State {
8222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
8232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
8242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(true);
8252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(false);
8262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
8272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
8292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
8302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
831924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "InitialState.processMessage what=" + message.what);
8322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
8332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
8342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_REQUESTED:
8355a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
8366a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_REQUESTED,
8376a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
8382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mStartingState);
8392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
8412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mUnavailableState);
8422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
8442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
8452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
8472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
8482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
85164c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class StartingState extends State {
8522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
8532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
8542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
85565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                if (mUsb) {
856d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    if (!Tethering.this.configureUsbIface(true)) {
8576a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
8586a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
8595a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastError(ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
8605a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
861951749ba2e014566553d17c512e947472951a060Wink Saville                        transitionTo(mInitialState);
86265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        return;
86365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    }
86465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
8652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
8666a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
867951749ba2e014566553d17c512e947472951a060Wink Saville                // Skipping StartingState
868951749ba2e014566553d17c512e947472951a060Wink Saville                transitionTo(mTetheredState);
8692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
8712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
872924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "StartingState.processMessage what=" + message.what);
8732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
8742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
8752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    // maybe a parent class?
8762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_UNREQUESTED:
8776a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
8786a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
87965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        if (mUsb) {
880d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                            if (!Tethering.this.configureUsbIface(false)) {
8815a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                setLastErrorAndTransitionToInitialState(
8825a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
88365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                                break;
88465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
88565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
8862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
8872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_CELL_DUN_ERROR:
8892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_ENABLE_ERROR:
8902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_DISABLE_ERROR:
8912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_START_TETHERING_ERROR:
8922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_STOP_TETHERING_ERROR:
8932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_SET_DNS_FORWARDERS_ERROR:
8945a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastErrorAndTransitionToInitialState(
8955a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                ConnectivityManager.TETHER_ERROR_MASTER_ERROR);
8962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
8986a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
8996a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
9002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mUnavailableState);
9012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
9022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
9032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
9042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
9052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
9062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
9072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
9082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
90964c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class TetheredState extends State {
9102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
9112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
9122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
913c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.tetherInterface(mIfaceName);
9142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {
9155a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    setLastError(ConnectivityManager.TETHER_ERROR_TETHER_IFACE_ERROR);
9165a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
917951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mInitialState);
9182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    return;
9192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
920924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "Tethered " + mIfaceName);
9212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
9222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(true);
9232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
9242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
9252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
9262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
927924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "TetheredState.processMessage what=" + message.what);
9282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
9292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean error = false;
9302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
9312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_UNREQUESTED:
9322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
9336a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (mMyUpstreamIfaceName != null) {
9345a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            try {
935c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                                mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName);
936b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mMyUpstreamIfaceName = null;
9376a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
9386a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
939c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                                    mNMService.untetherInterface(mIfaceName);
9406a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
9415a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
9426a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastErrorAndTransitionToInitialState(
9436a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                        ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR);
9446a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                break;
9456a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
9462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
9472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        try {
948c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                            mNMService.untetherInterface(mIfaceName);
9492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } catch (Exception e) {
9505a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
9515a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR);
9522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
9532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
9546a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
9556a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
9562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (message.what == CMD_TETHER_UNREQUESTED) {
95765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            if (mUsb) {
958d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                                if (!Tethering.this.configureUsbIface(false)) {
9595a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    setLastError(
9605a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                            ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
96165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                                }
96265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
9635a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            transitionTo(mInitialState);
9642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } else if (message.what == CMD_INTERFACE_DOWN) {
9652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            transitionTo(mUnavailableState);
9662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
967924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                        if (DEBUG) Log.d(TAG, "Untethered " + mIfaceName);
9682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
9696a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_TETHER_CONNECTION_CHANGED:
9706a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        String newUpstreamIfaceName = (String)(message.obj);
9711039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        if ((mMyUpstreamIfaceName == null && newUpstreamIfaceName == null) ||
9721039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                                (mMyUpstreamIfaceName != null &&
9731039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                                mMyUpstreamIfaceName.equals(newUpstreamIfaceName))) {
9741039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                            if (DEBUG) Log.d(TAG, "Connection changed noop - dropping");
9751039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                            break;
9761039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        }
9776a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (mMyUpstreamIfaceName != null) {
9786a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            try {
979c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                                mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName);
980b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mMyUpstreamIfaceName = null;
9816a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
9826a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
983c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                                    mNMService.untetherInterface(mIfaceName);
9846a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
9856a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
9866a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastErrorAndTransitionToInitialState(
9876a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                        ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR);
9886a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                break;
9896a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
9906a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        }
9916a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (newUpstreamIfaceName != null) {
9926a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            try {
993c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                                mNMService.enableNat(mIfaceName, newUpstreamIfaceName);
9946a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
9956a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
996c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                                    mNMService.untetherInterface(mIfaceName);
9976a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
9986a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
9996a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastError(ConnectivityManager.TETHER_ERROR_ENABLE_NAT_ERROR);
10006a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                transitionTo(mInitialState);
10016a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                return true;
10026a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
10036a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        }
10046a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mMyUpstreamIfaceName = newUpstreamIfaceName;
10056a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        break;
10062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_CELL_DUN_ERROR:
10072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_ENABLE_ERROR:
10082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_DISABLE_ERROR:
10092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_START_TETHERING_ERROR:
10102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_STOP_TETHERING_ERROR:
10112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_SET_DNS_FORWARDERS_ERROR:
10122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        error = true;
10132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        // fall through
10142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_DEAD:
10156a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (mMyUpstreamIfaceName != null) {
10165a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            try {
1017c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                                mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName);
1018b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mMyUpstreamIfaceName = null;
10196a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
10206a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
1021c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                                    mNMService.untetherInterface(mIfaceName);
10226a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
10235a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
10246a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastErrorAndTransitionToInitialState(
10256a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                        ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR);
10266a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                break;
10276a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
10282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
10292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        try {
1030c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                            mNMService.untetherInterface(mIfaceName);
10312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } catch (Exception e) {
10325a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
10335a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR);
10342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
10352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
10362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (error) {
10375a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
10385a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_MASTER_ERROR);
10392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
10402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
1041924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                        if (DEBUG) Log.d(TAG, "Tether lost upstream connection " + mIfaceName);
10422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        sendTetherStateChangedBroadcast();
104365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        if (mUsb) {
1044d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                            if (!Tethering.this.configureUsbIface(false)) {
10455a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                setLastError(ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
104665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
104765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
10482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
10492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
10512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
10522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
10542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
10552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
10562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
10572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
105864c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class UnavailableState extends State {
10592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
10602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
10612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
10625a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
10632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(false);
10642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
10652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
10662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
10672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
10682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
10692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
10702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_UP:
10712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
10722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
10742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
10752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
10772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
10782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
10792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
10802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10815a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        void setLastErrorAndTransitionToInitialState(int error) {
10825a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            setLastError(error);
10835a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            transitionTo(mInitialState);
10842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
108565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
10862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
10872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
108864c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville    class TetherMasterSM extends StateMachine {
10892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // an interface SM has requested Tethering
10902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        static final int CMD_TETHER_MODE_REQUESTED   = 1;
10912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // an interface SM has unrequested Tethering
10922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        static final int CMD_TETHER_MODE_UNREQUESTED = 2;
10936a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // upstream connection change - do the right thing
10946a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_UPSTREAM_CHANGED        = 3;
10952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // we received notice that the cellular DUN connection is up
10966a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_CELL_CONNECTION_RENEW   = 4;
10976a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // we don't have a valid upstream conn, check again after a delay
1098951749ba2e014566553d17c512e947472951a060Wink Saville        static final int CMD_RETRY_UPSTREAM          = 5;
10992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // This indicates what a timeout event relates to.  A state that
11012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // sends itself a delayed timeout event and handles incoming timeout events
11022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // should inc this when it is entered and whenever it sends a new timeout event.
11032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // We do not flush the old ones.
11042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private int mSequenceNumber;
11052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
110664c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mInitialState;
110764c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mTetherModeAliveState;
11082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
110964c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mSetIpForwardingEnabledErrorState;
111064c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mSetIpForwardingDisabledErrorState;
111164c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mStartTetheringErrorState;
111264c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mStopTetheringErrorState;
111364c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mSetDnsForwardersErrorState;
11142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private ArrayList mNotifyList;
11162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11171039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt        private int mCurrentConnectionSequence;
1118ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt        private int mMobileApnReserved = ConnectivityManager.TYPE_NONE;
11192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1120b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt        private String mUpstreamIfaceName = null;
1121b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt
11226a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        private static final int UPSTREAM_SETTLE_TIME_MS     = 10000;
11236a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        private static final int CELL_CONNECTION_RENEW_MS    = 40000;
11242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1125dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        TetherMasterSM(String name, Looper looper) {
1126dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            super(name, looper);
11272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            //Add states
11292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mInitialState = new InitialState();
11302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mInitialState);
11312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mTetherModeAliveState = new TetherModeAliveState();
11322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mTetherModeAliveState);
11332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetIpForwardingEnabledErrorState = new SetIpForwardingEnabledErrorState();
11352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetIpForwardingEnabledErrorState);
11362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetIpForwardingDisabledErrorState = new SetIpForwardingDisabledErrorState();
11372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetIpForwardingDisabledErrorState);
11382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStartTetheringErrorState = new StartTetheringErrorState();
11392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStartTetheringErrorState);
11402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStopTetheringErrorState = new StopTetheringErrorState();
11412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStopTetheringErrorState);
11422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetDnsForwardersErrorState = new SetDnsForwardersErrorState();
11432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetDnsForwardersErrorState);
11442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mNotifyList = new ArrayList();
11462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            setInitialState(mInitialState);
11472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
11482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
114964c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class TetherMasterUtilState extends State {
11506a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected final static boolean TRY_TO_SETUP_MOBILE_CONNECTION = true;
11516a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected final static boolean WAIT_FOR_NETWORK_TO_SETTLE     = false;
11526a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
1153d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            @Override
1154d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            public boolean processMessage(Message m) {
1155d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return false;
1156d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
1157ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            protected String enableString(int apnType) {
1158ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                switch (apnType) {
1159ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                case ConnectivityManager.TYPE_MOBILE_DUN:
1160ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    return Phone.FEATURE_ENABLE_DUN_ALWAYS;
1161ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                case ConnectivityManager.TYPE_MOBILE:
1162ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                case ConnectivityManager.TYPE_MOBILE_HIPRI:
1163ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    return Phone.FEATURE_ENABLE_HIPRI;
1164ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                }
1165ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                return null;
1166ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            }
1167ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            protected boolean turnOnUpstreamMobileConnection(int apnType) {
11681039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                boolean retValue = true;
1169ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (apnType == ConnectivityManager.TYPE_NONE) return false;
1170ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (apnType != mMobileApnReserved) turnOffUpstreamMobileConnection();
1171d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
1172c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b);
11731039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                int result = Phone.APN_REQUEST_FAILED;
1174ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                String enableString = enableString(apnType);
1175ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (enableString == null) return false;
1176d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1177c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    result = cm.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
1178ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                            enableString, new Binder());
1179d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1180d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
11811039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                switch (result) {
11826a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                case Phone.APN_ALREADY_ACTIVE:
11836a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                case Phone.APN_REQUEST_STARTED:
1184ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    mMobileApnReserved = apnType;
11851039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    Message m = obtainMessage(CMD_CELL_CONNECTION_RENEW);
11861039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    m.arg1 = ++mCurrentConnectionSequence;
11871039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    sendMessageDelayed(m, CELL_CONNECTION_RENEW_MS);
11886a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    break;
11896a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                case Phone.APN_REQUEST_FAILED:
11906a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                default:
11911039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    retValue = false;
11926a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    break;
11936a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
11946a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
1195d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return retValue;
1196d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
1197ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            protected boolean turnOffUpstreamMobileConnection() {
1198ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (mMobileApnReserved != ConnectivityManager.TYPE_NONE) {
11996a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
1200c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b);
12016a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    try {
1202c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                        cm.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
1203ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                                enableString(mMobileApnReserved));
12046a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    } catch (Exception e) {
12056a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        return false;
12066a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    }
1207ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    mMobileApnReserved = ConnectivityManager.TYPE_NONE;
1208d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1209d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1210d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
12116a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected boolean turnOnMasterTetherSettings() {
1212d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1213c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.setIpForwardingEnabled(true);
1214d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1215951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mSetIpForwardingEnabledErrorState);
1216d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1217d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1218d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1219c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.startTethering(mDhcpRange);
1220d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1221547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                    try {
1222c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                        mNMService.stopTethering();
1223c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                        mNMService.startTethering(mDhcpRange);
1224547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                    } catch (Exception ee) {
1225547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                        transitionTo(mStartTetheringErrorState);
1226547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                        return false;
1227547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                    }
1228d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1229d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1230c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.setDnsForwarders(mDnsServers);
1231d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1232951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mSetDnsForwardersErrorState);
1233d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1234d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1235d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1236d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
12376a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected boolean turnOffMasterTetherSettings() {
1238d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1239c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.stopTethering();
1240d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1241d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    transitionTo(mStopTetheringErrorState);
1242d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1243d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1244d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1245c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.setIpForwardingEnabled(false);
1246d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1247d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    transitionTo(mSetIpForwardingDisabledErrorState);
1248d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1249d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1250d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                transitionTo(mInitialState);
1251d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1252d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
1253ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
1254ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt            protected void chooseUpstreamType(boolean tryCell) {
125505d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
125605d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b);
1257ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                int upType = ConnectivityManager.TYPE_NONE;
1258ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                String iface = null;
125905d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt
1260ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                for (Integer netType : mUpstreamIfaceTypes) {
1261ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    NetworkInfo info = null;
1262ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    try {
1263ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                        info = cm.getNetworkInfo(netType.intValue());
1264ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    } catch (RemoteException e) { }
1265ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    if ((info != null) && info.isConnected()) {
1266ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                        upType = netType.intValue();
1267ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                        break;
1268c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    }
1269c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                }
12701039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt
1271924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) {
1272ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    Log.d(TAG, "chooseUpstreamType(" + tryCell + "), preferredApn ="
1273ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                            + mPreferredUpstreamMobileApn + ", got type=" + upType);
1274924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                }
1275ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
1276ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                // if we're on DUN, put our own grab on it
1277ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (upType == ConnectivityManager.TYPE_MOBILE_DUN ||
1278ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                        upType == ConnectivityManager.TYPE_MOBILE_HIPRI) {
1279ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    turnOnUpstreamMobileConnection(upType);
12806a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
1281ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
1282ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                if (upType == ConnectivityManager.TYPE_NONE) {
1283ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    boolean tryAgainLater = true;
1284ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    if ((tryCell == TRY_TO_SETUP_MOBILE_CONNECTION) &&
1285ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                            (turnOnUpstreamMobileConnection(mPreferredUpstreamMobileApn) == true)) {
1286ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                        // we think mobile should be coming up - don't set a retry
1287ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                        tryAgainLater = false;
12881039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    }
1289ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    if (tryAgainLater) {
12901039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        sendMessageDelayed(CMD_RETRY_UPSTREAM, UPSTREAM_SETTLE_TIME_MS);
129177e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                    }
1292ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                } else {
1293ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    LinkProperties linkProperties = null;
1294ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    try {
1295ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                        linkProperties = cm.getLinkProperties(upType);
1296ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    } catch (RemoteException e) { }
1297ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                    if (linkProperties != null) iface = linkProperties.getInterfaceName();
12986a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
1299b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                notifyTetheredOfNewUpstreamIface(iface);
13006a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            }
1301ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt
1302b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt            protected void notifyTetheredOfNewUpstreamIface(String ifaceName) {
1303924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "notifying tethered with iface =" + ifaceName);
1304b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                mUpstreamIfaceName = ifaceName;
13056a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                for (Object o : mNotifyList) {
13066a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    TetherInterfaceSM sm = (TetherInterfaceSM)o;
13076a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED,
13086a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            ifaceName);
13092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
13102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
13122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
13136a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        class InitialState extends TetherMasterUtilState {
13142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
13162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
1319924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "MasterInitialState.processMessage what=" + message.what);
13202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
13212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
13222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
1323ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                        checkDunRequired();
13242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
1325924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                        if (DEBUG) Log.d(TAG, "Tether Mode requested by " + who.toString());
13262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        mNotifyList.add(who);
13276a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        transitionTo(mTetherModeAliveState);
13282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_UNREQUESTED:
13302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        who = (TetherInterfaceSM)message.obj;
1331924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                        if (DEBUG) Log.d(TAG, "Tether Mode unrequested by " + who.toString());
13322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        int index = mNotifyList.indexOf(who);
13332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (index != -1) {
13346a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            mNotifyList.remove(who);
13352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
13362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
13382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
13392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
13412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
13422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
13442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1345d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        class TetherModeAliveState extends TetherMasterUtilState {
134619b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt            boolean mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE;
13472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
134919b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt                mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE; // better try something first pass
135019b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt                                                        // or crazy tests cases will fail
13516a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                chooseUpstreamType(mTryCell);
13526a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                mTryCell = !mTryCell;
13536a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                turnOnMasterTetherSettings(); // may transition us out
13546a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            }
13556a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            @Override
13566a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            public void exit() {
1357ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                turnOffUpstreamMobileConnection();
1358b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                notifyTetheredOfNewUpstreamIface(null);
13592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
1362924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "TetherModeAliveState.processMessage what=" + message.what);
13632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
13642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
13652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
13662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
13672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        mNotifyList.add(who);
1368b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                        who.sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED,
1369b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mUpstreamIfaceName);
13702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_UNREQUESTED:
13722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        who = (TetherInterfaceSM)message.obj;
13732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        int index = mNotifyList.indexOf(who);
13742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (index != -1) {
13752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            mNotifyList.remove(index);
13762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            if (mNotifyList.isEmpty()) {
1377d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                                turnOffMasterTetherSettings(); // transitions appropriately
13782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            }
13792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
13802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13816a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_UPSTREAM_CHANGED:
138205d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                        // need to try DUN immediately if Wifi goes down
138305d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                        mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE;
13846a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        chooseUpstreamType(mTryCell);
13856a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTryCell = !mTryCell;
13862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13876a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_CELL_CONNECTION_RENEW:
13886a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        // make sure we're still using a requested connection - may have found
13896a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        // wifi or something since then.
13901039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        if (mCurrentConnectionSequence == message.arg1) {
1391924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                            if (DEBUG) {
1392924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                                Log.d(TAG, "renewing mobile connection - requeuing for another " +
1393924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                                        CELL_CONNECTION_RENEW_MS + "ms");
1394924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                            }
1395ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt                            turnOnUpstreamMobileConnection(mMobileApnReserved);
13966a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        }
139765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        break;
139801758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                    case CMD_RETRY_UPSTREAM:
139901758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                        chooseUpstreamType(mTryCell);
140001758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                        mTryCell = !mTryCell;
140101758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                        break;
140201758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                    default:
140301758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                        retValue = false;
140401758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo                        break;
14052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
14062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
14072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
141064c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class ErrorState extends State {
14112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            int mErrorNotification;
14122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
14142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
14152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
14162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
14172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
14186a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        who.sendMessage(mErrorNotification);
14192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
14202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
14212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                       retValue = false;
14222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
14232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
14242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            void notify(int msgType) {
14262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                mErrorNotification = msgType;
14272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                for (Object o : mNotifyList) {
14282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    TetherInterfaceSM sm = (TetherInterfaceSM)o;
14296a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(msgType);
14302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
14312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetIpForwardingEnabledErrorState extends ErrorState {
14352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setIpForwardingEnabled");
14382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_IP_FORWARDING_ENABLE_ERROR);
14392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetIpForwardingDisabledErrorState extends ErrorState {
14432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setIpForwardingDisabled");
14462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_IP_FORWARDING_DISABLE_ERROR);
14472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class StartTetheringErrorState extends ErrorState {
14512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in startTethering");
14542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_START_TETHERING_ERROR);
14552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
1456c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.setIpForwardingEnabled(false);
14572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
14582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class StopTetheringErrorState extends ErrorState {
14622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in stopTethering");
14652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_STOP_TETHERING_ERROR);
14662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
1467c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.setIpForwardingEnabled(false);
14682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
14692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetDnsForwardersErrorState extends ErrorState {
14732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setDnsForwarders");
14762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_SET_DNS_FORWARDERS_ERROR);
14772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
1478c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.stopTethering();
14792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
14802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
1481c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh                    mNMService.setIpForwardingEnabled(false);
14822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
14832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
1484d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
1485d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
1486d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
14872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
14882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (mContext.checkCallingOrSelfPermission(
14892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
14902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            pw.println("Permission Denial: can't dump ConnectivityService.Tether " +
14912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    "from from pid=" + Binder.getCallingPid() + ", uid=" +
14922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    Binder.getCallingUid());
14932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    return;
14942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        pw.println();
14972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        pw.println("Tether state:");
14982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
14992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object o : mIfaces.values()) {
15002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                pw.println(" "+o.toString());
15012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
1502d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
15032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        pw.println();
15042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return;
1505d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
1506d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt}
1507