Tethering.java revision 64c42cae4482fe0157e977b8ddd0f2c2436b3f31
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;
602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.util.HashMap;
616fdd0c6274c81b337ad35b70480f881daf7354c3Danica Changimport java.util.LinkedList;
622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.util.Set;
63d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt/**
64d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * @hide
652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt *
662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt * Timeout
672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt *
682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt * TODO - look for parent classes and code sharing
69d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt */
7065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
71d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltpublic class Tethering extends INetworkManagementEventObserver.Stub {
72d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
73d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    private Context mContext;
74924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt    private final static String TAG = "Tethering";
751039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt    private final static boolean DEBUG = true;
76d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
77c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    private boolean mBooted = false;
78c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    //used to remember if we got connected before boot finished
79c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    private boolean mDeferedUsbConnection = false;
80d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    // TODO - remove both of these - should be part of interface inspection/selection stuff
822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mTetherableUsbRegexs;
832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mTetherableWifiRegexs;
846fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    private String[] mTetherableBluetoothRegexs;
85c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    private String[] mUpstreamIfaceRegexs;
862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
877eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt    private Looper mLooper;
887eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt    private HandlerThread mThread;
89dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt
90030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private HashMap<String, TetherInterfaceSM> mIfaces; // all tethered/tetherable ifaces
91d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
92d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    private BroadcastReceiver mStateReceiver;
93d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
94bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String USB_NEAR_IFACE_ADDR      = "192.168.42.129";
95b979f79158f9c470fa09ff3b96d72db274262201Robert Greenwalt    private static final int USB_PREFIX_LENGTH        = 24;
96bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt
97707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    // USB is  192.168.42.1 and 255.255.255.0
98707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    // Wifi is 192.168.43.1 and 255.255.255.0
99707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    // BT is limited to max default of 5 connections. 192.168.44.1 to 192.168.48.1
100707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    // with 255.255.255.0
101d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt
1022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mDhcpRange;
103bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String DHCP_DEFAULT_RANGE1_START = "192.168.42.2";
104bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String DHCP_DEFAULT_RANGE1_STOP  = "192.168.42.254";
105bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String DHCP_DEFAULT_RANGE2_START = "192.168.43.2";
106bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String DHCP_DEFAULT_RANGE2_STOP  = "192.168.43.254";
107707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    private static final String DHCP_DEFAULT_RANGE3_START = "192.168.44.2";
1086fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    private static final String DHCP_DEFAULT_RANGE3_STOP  = "192.168.44.254";
109707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    private static final String DHCP_DEFAULT_RANGE4_START = "192.168.45.2";
110707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    private static final String DHCP_DEFAULT_RANGE4_STOP  = "192.168.45.254";
111707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    private static final String DHCP_DEFAULT_RANGE5_START = "192.168.46.2";
112707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    private static final String DHCP_DEFAULT_RANGE5_STOP  = "192.168.46.254";
113707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    private static final String DHCP_DEFAULT_RANGE6_START = "192.168.47.2";
114707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    private static final String DHCP_DEFAULT_RANGE6_STOP  = "192.168.47.254";
115707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    private static final String DHCP_DEFAULT_RANGE7_START = "192.168.48.2";
116707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    private static final String DHCP_DEFAULT_RANGE7_STOP  = "192.168.48.254";
117707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh
1182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mDnsServers;
120d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private static final String DNS_DEFAULT_SERVER1 = "8.8.8.8";
121df255c2283b2bcf2d5e70d095a4d2333c1cb80a3Robert Greenwalt    private static final String DNS_DEFAULT_SERVER2 = "8.8.4.4";
1222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
12377b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt    // resampled each time we turn on tethering - used as cache for settings/config-val
124030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private boolean mDunRequired;  // configuration info - must use DUN apn on 3g
125030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
12664c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville    private StateMachine mTetherMasterSM;
127a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
128a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private Notification mTetheredNotification;
1292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
130030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    // whether we can tether is the && of these two - they come in as separate
131030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    // broadcasts so track them so we can decide what to do when either changes
132030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private boolean mUsbMassStorageOff;  // track the status of USB Mass Storage
133030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private boolean mUsbConnected;       // track the status of USB connection
134030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
135dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt    public Tethering(Context context, Looper looper) {
136d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        mContext = context;
137dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        mLooper = looper;
138d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
139d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        // register for notifications from NetworkManagement Service
140d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
141d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
142d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        try {
143d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            service.registerObserver(this);
144d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        } catch (RemoteException e) {
145d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            Log.e(TAG, "Error registering observer :" + e);
146d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
147d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
1482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mIfaces = new HashMap<String, TetherInterfaceSM>();
149d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
1507eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        // make our own thread so we don't anr the system
1517eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        mThread = new HandlerThread("Tethering");
1527eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        mThread.start();
1537eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        mLooper = mThread.getLooper();
154dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        mTetherMasterSM = new TetherMasterSM("TetherMaster", mLooper);
1552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mTetherMasterSM.start();
1562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
157bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        mStateReceiver = new StateReceiver();
158d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        IntentFilter filter = new IntentFilter();
159770126a678ccc9328a89407ffc82f4d998b25427Mike Lockwood        filter.addAction(UsbManager.ACTION_USB_STATE);
1602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
161c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt        filter.addAction(Intent.ACTION_BOOT_COMPLETED);
162bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        mContext.registerReceiver(mStateReceiver, filter);
163bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt
164bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        filter = new IntentFilter();
165030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        filter.addAction(Intent.ACTION_MEDIA_SHARED);
166030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
167bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        filter.addDataScheme("file");
168bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        mContext.registerReceiver(mStateReceiver, filter);
169bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt
170030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        mUsbMassStorageOff = !Environment.MEDIA_SHARED.equals(
171030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                Environment.getExternalStorageState());
1722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mDhcpRange = context.getResources().getStringArray(
1742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                com.android.internal.R.array.config_tether_dhcp_range);
175bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt        if ((mDhcpRange.length == 0) || (mDhcpRange.length % 2 ==1)) {
176707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh            mDhcpRange = new String[14];
177bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt            mDhcpRange[0] = DHCP_DEFAULT_RANGE1_START;
178bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt            mDhcpRange[1] = DHCP_DEFAULT_RANGE1_STOP;
179bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt            mDhcpRange[2] = DHCP_DEFAULT_RANGE2_START;
180bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt            mDhcpRange[3] = DHCP_DEFAULT_RANGE2_STOP;
1816fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            mDhcpRange[4] = DHCP_DEFAULT_RANGE3_START;
1826fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            mDhcpRange[5] = DHCP_DEFAULT_RANGE3_STOP;
183707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh            mDhcpRange[6] = DHCP_DEFAULT_RANGE4_START;
184707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh            mDhcpRange[7] = DHCP_DEFAULT_RANGE4_STOP;
185707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh            mDhcpRange[8] = DHCP_DEFAULT_RANGE5_START;
186707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh            mDhcpRange[9] = DHCP_DEFAULT_RANGE5_STOP;
187707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh            mDhcpRange[10] = DHCP_DEFAULT_RANGE6_START;
188707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh            mDhcpRange[11] = DHCP_DEFAULT_RANGE6_STOP;
189707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh            mDhcpRange[12] = DHCP_DEFAULT_RANGE7_START;
190707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh            mDhcpRange[13] = DHCP_DEFAULT_RANGE7_STOP;
1912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
19277b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt        mDunRequired = false; // resample when we turn on
1932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mTetherableUsbRegexs = context.getResources().getStringArray(
1952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                com.android.internal.R.array.config_tether_usb_regexs);
1962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mTetherableWifiRegexs = context.getResources().getStringArray(
1972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                com.android.internal.R.array.config_tether_wifi_regexs);
1986fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        mTetherableBluetoothRegexs = context.getResources().getStringArray(
1996fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                com.android.internal.R.array.config_tether_bluetooth_regexs);
200c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt        mUpstreamIfaceRegexs = context.getResources().getStringArray(
201c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                com.android.internal.R.array.config_tether_upstream_regexs);
2022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
2032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // TODO - remove and rely on real notifications of the current iface
2042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mDnsServers = new String[2];
205d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        mDnsServers[0] = DNS_DEFAULT_SERVER1;
206d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        mDnsServers[1] = DNS_DEFAULT_SERVER2;
207d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
208d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
2092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void interfaceLinkStatusChanged(String iface, boolean link) {
210924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) Log.d(TAG, "interfaceLinkStatusChanged " + iface + ", " + link);
2112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        boolean found = false;
21265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean usb = false;
213a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (isWifi(iface)) {
214a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
215a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        } else if (isUsb(iface)) {
216a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
217a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            usb = true;
2186fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        } else if (isBluetooth(iface)) {
2196fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            found = true;
2202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
2212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (found == false) return;
2222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
2232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
2242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
2252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (link) {
2262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm == null) {
227dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt                    sm = new TetherInterfaceSM(iface, mLooper, usb);
2282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    mIfaces.put(iface, sm);
2292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    sm.start();
2302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
2312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            } else {
2322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm != null) {
2336a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(TetherInterfaceSM.CMD_INTERFACE_DOWN);
2342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    mIfaces.remove(iface);
2352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
2362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
2372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
238d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
239d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
240a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private boolean isUsb(String iface) {
241a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        for (String regex : mTetherableUsbRegexs) {
242a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (iface.matches(regex)) return true;
243a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
244a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        return false;
245a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
246a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
247a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    public boolean isWifi(String iface) {
248a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        for (String regex : mTetherableWifiRegexs) {
249a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (iface.matches(regex)) return true;
250a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
251a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        return false;
252a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
253a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
2546fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    public boolean isBluetooth(String iface) {
2556fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        for (String regex : mTetherableBluetoothRegexs) {
2566fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            if (iface.matches(regex)) return true;
2576fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        }
2586fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        return false;
2596fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    }
2602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void interfaceAdded(String iface) {
26165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
26265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
2632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        boolean found = false;
26465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean usb = false;
265a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (isWifi(iface)) {
266a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
2672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
268a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (isUsb(iface)) {
269a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
270a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            usb = true;
271d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
2726fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        if (isBluetooth(iface)) {
2736fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            found = true;
2746fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        }
2752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (found == false) {
276924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt            if (DEBUG) Log.d(TAG, iface + " is not a tetherable iface, ignoring");
277d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            return;
278d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
27965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
2802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
2812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
2822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (sm != null) {
283924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "active iface (" + iface + ") reported as added, ignoring");
2842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return;
2852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
286dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            sm = new TetherInterfaceSM(iface, mLooper, usb);
2872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaces.put(iface, sm);
2882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm.start();
2892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
290924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) Log.d(TAG, "interfaceAdded :" + iface);
291d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
292d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
2932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void interfaceRemoved(String iface) {
2942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
2952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
2962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (sm == null) {
297924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) {
298924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                    Log.e(TAG, "attempting to remove unknown iface (" + iface + "), ignoring");
299924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                }
3002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return;
3012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
3026a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            sm.sendMessage(TetherInterfaceSM.CMD_INTERFACE_DOWN);
3032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaces.remove(iface);
304d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
305d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
306d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3075a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int tether(String iface) {
308d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Log.d(TAG, "Tethering " + iface);
3092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        TetherInterfaceSM sm = null;
3102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
3112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm = mIfaces.get(iface);
312d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm == null) {
3142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Tether an unknown iface :" + iface + ", ignoring");
3155a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
316d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3175a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        if (!sm.isAvailable() && !sm.isErrored()) {
3182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Tether an unavailable iface :" + iface + ", ignoring");
3195a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
320d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3216a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        sm.sendMessage(TetherInterfaceSM.CMD_TETHER_REQUESTED);
3225a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return ConnectivityManager.TETHER_ERROR_NO_ERROR;
323d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
324d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3255a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int untether(String iface) {
326d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Log.d(TAG, "Untethering " + iface);
3272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        TetherInterfaceSM sm = null;
3282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
3292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm = mIfaces.get(iface);
3302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
3312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm == null) {
3322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Untether an unknown iface :" + iface + ", ignoring");
3335a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
334d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm.isErrored()) {
3362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Untethered an errored iface :" + iface + ", ignoring");
3375a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
338d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3396a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        sm.sendMessage(TetherInterfaceSM.CMD_TETHER_UNREQUESTED);
3405a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return ConnectivityManager.TETHER_ERROR_NO_ERROR;
3415a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
3425a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
3435a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int getLastTetherError(String iface) {
3445a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        TetherInterfaceSM sm = null;
3455a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        synchronized (mIfaces) {
3465a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            sm = mIfaces.get(iface);
3475a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
3485a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        if (sm == null) {
3495a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            Log.e(TAG, "Tried to getLastTetherError on an unknown iface :" + iface + ", ignoring");
3505a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
3515a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
3525a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return sm.getLastError();
3532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
354d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private void sendTetherStateChangedBroadcast() {
3562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
3572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
358d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        try {
3592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (!service.isTetheringSupported()) return;
3602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        } catch (RemoteException e) {
3612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return;
362d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
363d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> availableList = new ArrayList<String>();
3652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> activeList = new ArrayList<String>();
3662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> erroredList = new ArrayList<String>();
3672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
368a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        boolean wifiTethered = false;
369a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        boolean usbTethered = false;
3706fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        boolean bluetoothTethered = false;
371a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
3722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
3732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set ifaces = mIfaces.keySet();
3742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object iface : ifaces) {
3752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(iface);
3762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm != null) {
3776fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                    if (sm.isErrored()) {
3782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        erroredList.add((String)iface);
3792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    } else if (sm.isAvailable()) {
3802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        availableList.add((String)iface);
3812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    } else if (sm.isTethered()) {
382a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        if (isUsb((String)iface)) {
383a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            usbTethered = true;
384a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        } else if (isWifi((String)iface)) {
385a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            wifiTethered = true;
3866fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                      } else if (isBluetooth((String)iface)) {
3876fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                            bluetoothTethered = true;
388a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        }
3892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        activeList.add((String)iface);
3902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    }
3912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
392d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            }
393d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
394d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Intent broadcast = new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
395d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        broadcast.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
3962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_AVAILABLE_TETHER,
3972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                availableList);
3982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ACTIVE_TETHER, activeList);
3992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ERRORED_TETHER,
4002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                erroredList);
4012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mContext.sendStickyBroadcast(broadcast);
402924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) {
403924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt            Log.d(TAG, "sendTetherStateChangedBroadcast " + availableList.size() + ", " +
404924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                    activeList.size() + ", " + erroredList.size());
405924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        }
406a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
407a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (usbTethered) {
4086fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            if (wifiTethered || bluetoothTethered) {
409a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general);
410a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            } else {
411a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_usb);
412a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            }
413a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        } else if (wifiTethered) {
4146fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            if (bluetoothTethered) {
4156fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general);
4166fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            } else {
4176fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_wifi);
4186fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            }
4196fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        } else if (bluetoothTethered) {
4206fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_bluetooth);
421a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        } else {
422a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            clearTetheredNotification();
423a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
424a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
425a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
426a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private void showTetheredNotification(int icon) {
427a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        NotificationManager notificationManager =
428a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
429a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (notificationManager == null) {
430a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            return;
431a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
432a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
433db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt        if (mTetheredNotification != null) {
434db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            if (mTetheredNotification.icon == icon) {
435db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt                return;
436db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            }
437db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            notificationManager.cancel(mTetheredNotification.icon);
438db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt        }
439db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt
440a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        Intent intent = new Intent();
441a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        intent.setClassName("com.android.settings", "com.android.settings.TetherSettings");
442a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
443a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
444a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);
445a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
446a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        Resources r = Resources.getSystem();
447a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        CharSequence title = r.getText(com.android.internal.R.string.tethered_notification_title);
448a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        CharSequence message = r.getText(com.android.internal.R.string.
449a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                tethered_notification_message);
450a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
4516fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        if (mTetheredNotification == null) {
452a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification = new Notification();
453a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification.when = 0;
454a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
455a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.icon = icon;
456a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.defaults &= ~Notification.DEFAULT_SOUND;
457a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.flags = Notification.FLAG_ONGOING_EVENT;
458a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.tickerText = title;
459a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.setLatestEventInfo(mContext, title, message, pi);
460a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
461a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        notificationManager.notify(mTetheredNotification.icon, mTetheredNotification);
462a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
463a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
464a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private void clearTetheredNotification() {
465a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        NotificationManager notificationManager =
466a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
467a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (notificationManager != null && mTetheredNotification != null) {
468a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            notificationManager.cancel(mTetheredNotification.icon);
469a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification = null;
470a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
4715a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
4725a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
473030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private void updateUsbStatus() {
474030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        boolean enable = mUsbConnected && mUsbMassStorageOff;
475030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
476030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        if (mBooted) {
477030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            enableUsbIfaces(enable);
478030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        }
479030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    }
480030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
4812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private class StateReceiver extends BroadcastReceiver {
482d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        public void onReceive(Context content, Intent intent) {
4832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            String action = intent.getAction();
484770126a678ccc9328a89407ffc82f4d998b25427Mike Lockwood            if (action.equals(UsbManager.ACTION_USB_STATE)) {
485b92df0f66926252cf60cdda7812bb1d50ce9378eMike Lockwood                mUsbConnected = intent.getExtras().getBoolean(UsbManager.USB_CONNECTED);
486b92df0f66926252cf60cdda7812bb1d50ce9378eMike Lockwood                updateUsbStatus();
487030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
488030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                mUsbMassStorageOff = false;
489030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                updateUsbStatus();
490030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            }
491030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
492030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                mUsbMassStorageOff = true;
493030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                updateUsbStatus();
4942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
495924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "Tethering got CONNECTIVITY_ACTION");
4966a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED);
497c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt            } else if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
498c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                mBooted = true;
499030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                updateUsbStatus();
500d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            }
501d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
502d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
503d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
50465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    // used on cable insert/remove
505d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private void enableUsbIfaces(boolean enable) {
50665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
50765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
50865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        String[] ifaces = new String[0];
50965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        try {
51065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            ifaces = service.listInterfaces();
51165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        } catch (Exception e) {
51265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            Log.e(TAG, "Error listing Interfaces :" + e);
51365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            return;
51465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
51565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        for (String iface : ifaces) {
516a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (isUsb(iface)) {
517a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                if (enable) {
518a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    interfaceAdded(iface);
519a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                } else {
520a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    interfaceRemoved(iface);
52165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
52265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
52365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
52465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    }
52565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
5266fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    // toggled when we enter/leave the fully tethered state
527d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private boolean enableUsbRndis(boolean enabled) {
528924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) Log.d(TAG, "enableUsbRndis(" + enabled + ")");
52965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
53065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
53165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
53265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        try {
53365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            if (enabled) {
53465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                synchronized (this) {
53565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    if (!service.isUsbRNDISStarted()) {
53665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        service.startUsbRNDIS();
53765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    }
53865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
53965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            } else {
54065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                if (service.isUsbRNDISStarted()) {
54165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    service.stopUsbRNDIS();
54265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
54365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
54465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        } catch (Exception e) {
54565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            Log.e(TAG, "Error toggling usb RNDIS :" + e);
54665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            return false;
54765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
54865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        return true;
54965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    }
55065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
55165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    // configured when we start tethering and unconfig'd on error or conclusion
552d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private boolean configureUsbIface(boolean enabled) {
553924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) Log.d(TAG, "configureUsbIface(" + enabled + ")");
55465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
55565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
55665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
55765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
55865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        // bring toggle the interfaces
55965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        String[] ifaces = new String[0];
56065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        try {
56165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            ifaces = service.listInterfaces();
56265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        } catch (Exception e) {
56365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            Log.e(TAG, "Error listing Interfaces :" + e);
56465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            return false;
56565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
56665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        for (String iface : ifaces) {
567a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (isUsb(iface)) {
568a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                InterfaceConfiguration ifcg = null;
569a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                try {
570a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    ifcg = service.getInterfaceConfig(iface);
571a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    if (ifcg != null) {
572e590373ea71251cfffc8f22f011e2e6335dce716Robert Greenwalt                        InetAddress addr = NetworkUtils.numericToInetAddress(USB_NEAR_IFACE_ADDR);
573b979f79158f9c470fa09ff3b96d72db274262201Robert Greenwalt                        ifcg.addr = new LinkAddress(addr, USB_PREFIX_LENGTH);
574a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        if (enabled) {
575a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            ifcg.interfaceFlags = ifcg.interfaceFlags.replace("down", "up");
576a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        } else {
577a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            ifcg.interfaceFlags = ifcg.interfaceFlags.replace("up", "down");
57865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
579a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        ifcg.interfaceFlags = ifcg.interfaceFlags.replace("running", "");
580a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        ifcg.interfaceFlags = ifcg.interfaceFlags.replace("  "," ");
581a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        service.setInterfaceConfig(iface, ifcg);
58265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    }
583a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                } catch (Exception e) {
584a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    Log.e(TAG, "Error configuring interface " + iface + ", :" + e);
585a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    return false;
58665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
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
605c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    public String[] getUpstreamIfaceRegexs() {
606c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt        return mUpstreamIfaceRegexs;
607c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    }
608c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt
609c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    public boolean isDunRequired() {
61077b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt        boolean defaultVal = mContext.getResources().getBoolean(
61177b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt                com.android.internal.R.bool.config_tether_dun_required);
61277b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt        boolean result = (Settings.Secure.getInt(mContext.getContentResolver(),
61377b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt                Settings.Secure.TETHER_DUN_REQUIRED, (defaultVal ? 1 : 0)) == 1);
61477b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt        return result;
615c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    }
616c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt
6172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetheredIfaces() {
6182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> list = new ArrayList<String>();
6192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
6202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set keys = mIfaces.keySet();
6212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object key : keys) {
6222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
6232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm.isTethered()) {
6242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    list.add((String)key);
6252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
6262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
6272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String[] retVal = new String[list.size()];
6292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        for (int i=0; i < list.size(); i++) {
6302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            retVal[i] = list.get(i);
6312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return retVal;
6332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
6342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetherableIfaces() {
6362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> list = new ArrayList<String>();
6372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
6382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set keys = mIfaces.keySet();
6392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object key : keys) {
6402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
6412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm.isAvailable()) {
6422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    list.add((String)key);
6432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
6442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
6452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String[] retVal = new String[list.size()];
6472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        for (int i=0; i < list.size(); i++) {
6482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            retVal[i] = list.get(i);
6492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return retVal;
6512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
6522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6535a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public String[] getErroredIfaces() {
6545a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        ArrayList<String> list = new ArrayList<String>();
6555a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        synchronized (mIfaces) {
6565a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            Set keys = mIfaces.keySet();
6575a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            for (Object key : keys) {
6585a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
6595a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                if (sm.isErrored()) {
6605a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    list.add((String)key);
6615a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                }
6625a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            }
6635a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
6645a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        String[] retVal = new String[list.size()];
6655a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        for (int i= 0; i< list.size(); i++) {
6665a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            retVal[i] = list.get(i);
6675a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
6685a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return retVal;
6695a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
6705a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
6712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
67264c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville    class TetherInterfaceSM extends StateMachine {
6732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it's not in tether mode
6746a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_MODE_DEAD            =  1;
6752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // request from the user that it wants to tether
6766a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_REQUESTED            =  2;
6772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // request from the user that it wants to untether
6786a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_UNREQUESTED          =  3;
6792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification that this interface is down
6806a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_INTERFACE_DOWN              =  4;
6812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification that this interface is up
6826a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_INTERFACE_UP                =  5;
6832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had an error turning on cellular dun
6846a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_CELL_DUN_ERROR              =  6;
6852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble enabling IP Forwarding
6866a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_IP_FORWARDING_ENABLE_ERROR  =  7;
6872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble disabling IP Forwarding
6886a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_IP_FORWARDING_DISABLE_ERROR =  8;
6892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble staring tethering
6906a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_START_TETHERING_ERROR       =  9;
6912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble stopping tethering
6926a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_STOP_TETHERING_ERROR        = 10;
6932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble setting the DNS forwarders
6946a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_SET_DNS_FORWARDERS_ERROR    = 11;
6956a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // the upstream connection has changed
696951749ba2e014566553d17c512e947472951a060Wink Saville        static final int CMD_TETHER_CONNECTION_CHANGED   = 12;
6972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
69864c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mDefaultState;
6992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
70064c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mInitialState;
70164c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mStartingState;
70264c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mTetheredState;
7032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
70464c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mUnavailableState;
7052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private boolean mAvailable;
7072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private boolean mTethered;
7085a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        int mLastError;
7092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String mIfaceName;
7116a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        String mMyUpstreamIfaceName;  // may change over time
7126a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
71365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean mUsb;
7142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
715dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        TetherInterfaceSM(String name, Looper looper, boolean usb) {
716dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            super(name, looper);
7172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaceName = name;
71865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            mUsb = usb;
7195a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
7202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mInitialState = new InitialState();
7222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mInitialState);
7232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStartingState = new StartingState();
7242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStartingState);
7252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mTetheredState = new TetheredState();
7262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mTetheredState);
7272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mUnavailableState = new UnavailableState();
7282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mUnavailableState);
7292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            setInitialState(mInitialState);
7312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public String toString() {
7342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            String res = new String();
7352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            res += mIfaceName + " - ";
73664c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville            IState current = getCurrentState();
7372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mInitialState) res += "InitialState";
7382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mStartingState) res += "StartingState";
7392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mTetheredState) res += "TetheredState";
7402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mUnavailableState) res += "UnavailableState";
7412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (mAvailable) res += " - Available";
7422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (mTethered) res += " - Tethered";
7435a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            res += " - lastError =" + mLastError;
7442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return res;
7452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7475a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        public synchronized int getLastError() {
7485a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return mLastError;
7495a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
7505a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
7515a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        private synchronized void setLastError(int error) {
7525a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            mLastError = error;
7535a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
7545a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            if (isErrored()) {
7555a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                if (mUsb) {
7565a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    // note everything's been unwound by this point so nothing to do on
7575a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    // further error..
7585a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    Tethering.this.configureUsbIface(false);
7595a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                }
7605a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            }
7615a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
7625a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
7632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // synchronized between this getter and the following setter
7642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public synchronized boolean isAvailable() {
7652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return mAvailable;
7662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private synchronized void setAvailable(boolean available) {
7692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mAvailable = available;
7702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // synchronized between this getter and the following setter
7732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public synchronized boolean isTethered() {
7742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return mTethered;
7752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private synchronized void setTethered(boolean tethered) {
7782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mTethered = tethered;
7792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // synchronized between this getter and the following setter
7822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public synchronized boolean isErrored() {
7835a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return (mLastError != ConnectivityManager.TETHER_ERROR_NO_ERROR);
7842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
78664c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class InitialState extends State {
7872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
7882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
7892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(true);
7902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(false);
7912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
7922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
7932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
7952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
796924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "InitialState.processMessage what=" + message.what);
7972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
7982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
7992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_REQUESTED:
8005a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
8016a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_REQUESTED,
8026a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
8032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mStartingState);
8042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
8062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mUnavailableState);
8072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
8092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
8102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
8122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
8132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
81664c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class StartingState extends State {
8172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
8182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
8192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
82065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                if (mUsb) {
821d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    if (!Tethering.this.configureUsbIface(true)) {
8226a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
8236a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
8245a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastError(ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
8255a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
826951749ba2e014566553d17c512e947472951a060Wink Saville                        transitionTo(mInitialState);
82765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        return;
82865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    }
82965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
8302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
8316a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
832951749ba2e014566553d17c512e947472951a060Wink Saville                // Skipping StartingState
833951749ba2e014566553d17c512e947472951a060Wink Saville                transitionTo(mTetheredState);
8342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
8362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
837924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "StartingState.processMessage what=" + message.what);
8382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
8392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
8402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    // maybe a parent class?
8412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_UNREQUESTED:
8426a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
8436a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
84465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        if (mUsb) {
845d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                            if (!Tethering.this.configureUsbIface(false)) {
8465a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                setLastErrorAndTransitionToInitialState(
8475a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
84865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                                break;
84965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
85065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
8512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
8522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_CELL_DUN_ERROR:
8542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_ENABLE_ERROR:
8552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_DISABLE_ERROR:
8562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_START_TETHERING_ERROR:
8572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_STOP_TETHERING_ERROR:
8582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_SET_DNS_FORWARDERS_ERROR:
8595a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastErrorAndTransitionToInitialState(
8605a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                ConnectivityManager.TETHER_ERROR_MASTER_ERROR);
8612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
8636a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
8646a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
8652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mUnavailableState);
8662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
8682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
8692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
8702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
8712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
87464c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class TetheredState extends State {
8752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
8762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
8772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
8782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                INetworkManagementService service =
8792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        INetworkManagementService.Stub.asInterface(b);
8802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
8812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    service.tetherInterface(mIfaceName);
8822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {
8835a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    setLastError(ConnectivityManager.TETHER_ERROR_TETHER_IFACE_ERROR);
8845a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
885951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mInitialState);
8862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    return;
8872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
888d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                if (mUsb) Tethering.this.enableUsbRndis(true);
889924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "Tethered " + mIfaceName);
8902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
8912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(true);
8922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
8932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
89565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            public void exit() {
896d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                if (mUsb) Tethering.this.enableUsbRndis(false);
89765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
89865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            @Override
8992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
900924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "TetheredState.processMessage what=" + message.what);
9012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
9022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean error = false;
9032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
9042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_UNREQUESTED:
9052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
9062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
9072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        INetworkManagementService service =
9082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                                INetworkManagementService.Stub.asInterface(b);
9096a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (mMyUpstreamIfaceName != null) {
9105a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            try {
9116a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                service.disableNat(mIfaceName, mMyUpstreamIfaceName);
912b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mMyUpstreamIfaceName = null;
9136a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
9146a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
9156a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                    service.untetherInterface(mIfaceName);
9166a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
9175a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
9186a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastErrorAndTransitionToInitialState(
9196a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                        ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR);
9206a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                break;
9216a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
9222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
9232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        try {
9242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            service.untetherInterface(mIfaceName);
9252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } catch (Exception e) {
9265a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
9275a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR);
9282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
9292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
9306a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
9316a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
9322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (message.what == CMD_TETHER_UNREQUESTED) {
93365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            if (mUsb) {
934d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                                if (!Tethering.this.configureUsbIface(false)) {
9355a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    setLastError(
9365a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                            ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
93765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                                }
93865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
9395a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            transitionTo(mInitialState);
9402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } else if (message.what == CMD_INTERFACE_DOWN) {
9412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            transitionTo(mUnavailableState);
9422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
943924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                        if (DEBUG) Log.d(TAG, "Untethered " + mIfaceName);
9442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
9456a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_TETHER_CONNECTION_CHANGED:
9466a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        String newUpstreamIfaceName = (String)(message.obj);
9476a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
9486a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        service = INetworkManagementService.Stub.asInterface(b);
9491039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        if ((mMyUpstreamIfaceName == null && newUpstreamIfaceName == null) ||
9501039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                                (mMyUpstreamIfaceName != null &&
9511039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                                mMyUpstreamIfaceName.equals(newUpstreamIfaceName))) {
9521039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                            if (DEBUG) Log.d(TAG, "Connection changed noop - dropping");
9531039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                            break;
9541039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        }
9556a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (mMyUpstreamIfaceName != null) {
9566a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            try {
9576a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                service.disableNat(mIfaceName, mMyUpstreamIfaceName);
958b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mMyUpstreamIfaceName = null;
9596a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
9606a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
9616a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                    service.untetherInterface(mIfaceName);
9626a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
9636a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
9646a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastErrorAndTransitionToInitialState(
9656a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                        ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR);
9666a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                break;
9676a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
9686a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        }
9696a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (newUpstreamIfaceName != null) {
9706a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            try {
9716a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                service.enableNat(mIfaceName, newUpstreamIfaceName);
9726a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
9736a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
9746a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                    service.untetherInterface(mIfaceName);
9756a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
9766a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
9776a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastError(ConnectivityManager.TETHER_ERROR_ENABLE_NAT_ERROR);
9786a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                transitionTo(mInitialState);
9796a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                return true;
9806a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
9816a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        }
9826a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mMyUpstreamIfaceName = newUpstreamIfaceName;
9836a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        break;
9842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_CELL_DUN_ERROR:
9852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_ENABLE_ERROR:
9862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_DISABLE_ERROR:
9872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_START_TETHERING_ERROR:
9882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_STOP_TETHERING_ERROR:
9892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_SET_DNS_FORWARDERS_ERROR:
9902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        error = true;
9912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        // fall through
9922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_DEAD:
9932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
9942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        service = INetworkManagementService.Stub.asInterface(b);
9956a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (mMyUpstreamIfaceName != null) {
9965a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            try {
9976a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                service.disableNat(mIfaceName, mMyUpstreamIfaceName);
998b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mMyUpstreamIfaceName = null;
9996a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
10006a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
10016a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                    service.untetherInterface(mIfaceName);
10026a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
10035a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
10046a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastErrorAndTransitionToInitialState(
10056a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                        ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR);
10066a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                break;
10076a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
10082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
10092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        try {
10102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            service.untetherInterface(mIfaceName);
10112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } catch (Exception e) {
10125a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
10135a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR);
10142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
10152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
10162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (error) {
10175a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
10185a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_MASTER_ERROR);
10192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
10202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
1021924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                        if (DEBUG) Log.d(TAG, "Tether lost upstream connection " + mIfaceName);
10222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        sendTetherStateChangedBroadcast();
102365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        if (mUsb) {
1024d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                            if (!Tethering.this.configureUsbIface(false)) {
10255a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                setLastError(ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
102665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
102765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
10282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
10292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
10312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
10322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
10342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
10352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
10362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
10372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
103864c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class UnavailableState extends State {
10392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
10402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
10412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
10425a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
10432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(false);
10442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
10452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
10462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
10472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
10482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
10492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
10502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_UP:
10512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
10522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
10542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
10552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
10572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
10582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
10592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
10602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10615a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        void setLastErrorAndTransitionToInitialState(int error) {
10625a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            setLastError(error);
10635a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            transitionTo(mInitialState);
10642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
106565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
10662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
10672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
106864c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville    class TetherMasterSM extends StateMachine {
10692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // an interface SM has requested Tethering
10702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        static final int CMD_TETHER_MODE_REQUESTED   = 1;
10712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // an interface SM has unrequested Tethering
10722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        static final int CMD_TETHER_MODE_UNREQUESTED = 2;
10736a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // upstream connection change - do the right thing
10746a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_UPSTREAM_CHANGED        = 3;
10752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // we received notice that the cellular DUN connection is up
10766a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_CELL_CONNECTION_RENEW   = 4;
10776a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // we don't have a valid upstream conn, check again after a delay
1078951749ba2e014566553d17c512e947472951a060Wink Saville        static final int CMD_RETRY_UPSTREAM          = 5;
10792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // This indicates what a timeout event relates to.  A state that
10812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // sends itself a delayed timeout event and handles incoming timeout events
10822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // should inc this when it is entered and whenever it sends a new timeout event.
10832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // We do not flush the old ones.
10842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private int mSequenceNumber;
10852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
108664c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mInitialState;
108764c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mTetherModeAliveState;
10882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
108964c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mSetIpForwardingEnabledErrorState;
109064c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mSetIpForwardingDisabledErrorState;
109164c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mStartTetheringErrorState;
109264c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mStopTetheringErrorState;
109364c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        private State mSetDnsForwardersErrorState;
10942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private ArrayList mNotifyList;
10962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10971039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt        private int mCurrentConnectionSequence;
10981039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt        private boolean mMobileReserved = false;
10992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1100b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt        private String mUpstreamIfaceName = null;
1101b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt
11026a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        private static final int UPSTREAM_SETTLE_TIME_MS     = 10000;
11036a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        private static final int CELL_CONNECTION_RENEW_MS    = 40000;
11042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1105dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        TetherMasterSM(String name, Looper looper) {
1106dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            super(name, looper);
11072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            //Add states
11092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mInitialState = new InitialState();
11102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mInitialState);
11112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mTetherModeAliveState = new TetherModeAliveState();
11122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mTetherModeAliveState);
11132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetIpForwardingEnabledErrorState = new SetIpForwardingEnabledErrorState();
11152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetIpForwardingEnabledErrorState);
11162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetIpForwardingDisabledErrorState = new SetIpForwardingDisabledErrorState();
11172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetIpForwardingDisabledErrorState);
11182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStartTetheringErrorState = new StartTetheringErrorState();
11192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStartTetheringErrorState);
11202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStopTetheringErrorState = new StopTetheringErrorState();
11212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStopTetheringErrorState);
11222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetDnsForwardersErrorState = new SetDnsForwardersErrorState();
11232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetDnsForwardersErrorState);
11242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mNotifyList = new ArrayList();
11262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            setInitialState(mInitialState);
11272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
11282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
112964c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class TetherMasterUtilState extends State {
11306a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected final static boolean TRY_TO_SETUP_MOBILE_CONNECTION = true;
11316a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected final static boolean WAIT_FOR_NETWORK_TO_SETTLE     = false;
11326a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
1133d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            @Override
1134d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            public boolean processMessage(Message m) {
1135d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return false;
1136d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
11371039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt            protected boolean turnOnMobileConnection() {
11381039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                boolean retValue = true;
11391039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                if (mMobileReserved) return retValue;
1140d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
1141d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
11421039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                int result = Phone.APN_REQUEST_FAILED;
1143d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
11441039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    result = service.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
1145e32e812b11520b244374371ee49eaed20c84a6e3Robert Greenwalt                            (mDunRequired ? Phone.FEATURE_ENABLE_DUN_ALWAYS :
1146e32e812b11520b244374371ee49eaed20c84a6e3Robert Greenwalt                            Phone.FEATURE_ENABLE_HIPRI), new Binder());
1147d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1148d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
11491039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                switch (result) {
11506a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                case Phone.APN_ALREADY_ACTIVE:
11516a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                case Phone.APN_REQUEST_STARTED:
11521039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    mMobileReserved = true;
11531039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    Message m = obtainMessage(CMD_CELL_CONNECTION_RENEW);
11541039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    m.arg1 = ++mCurrentConnectionSequence;
11551039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    sendMessageDelayed(m, CELL_CONNECTION_RENEW_MS);
11566a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    break;
11576a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                case Phone.APN_REQUEST_FAILED:
11586a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                default:
11591039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    retValue = false;
11606a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    break;
11616a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
11626a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
1163d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return retValue;
1164d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
11656a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected boolean turnOffMobileConnection() {
11661039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                if (mMobileReserved) {
11676a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
11686a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    IConnectivityManager service =
11696a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            IConnectivityManager.Stub.asInterface(b);
11706a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    try {
11716a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        service.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
117219b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt                                (mDunRequired? Phone.FEATURE_ENABLE_DUN_ALWAYS :
117377e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                             Phone.FEATURE_ENABLE_HIPRI));
11746a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    } catch (Exception e) {
11756a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        return false;
11766a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    }
11771039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    mMobileReserved = false;
1178d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1179d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1180d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
11816a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected boolean turnOnMasterTetherSettings() {
1182d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1183d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                INetworkManagementService service =
1184d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                        INetworkManagementService.Stub.asInterface(b);
1185d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1186d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    service.setIpForwardingEnabled(true);
1187d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1188951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mSetIpForwardingEnabledErrorState);
1189d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1190d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1191d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1192bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt                    service.startTethering(mDhcpRange);
1193d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1194547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                    try {
1195547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                        service.stopTethering();
1196547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                        service.startTethering(mDhcpRange);
1197547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                    } catch (Exception ee) {
1198547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                        transitionTo(mStartTetheringErrorState);
1199547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                        return false;
1200547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                    }
1201d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1202d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1203d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    service.setDnsForwarders(mDnsServers);
1204d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1205951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mSetDnsForwardersErrorState);
1206d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1207d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1208d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1209d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
12106a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected boolean turnOffMasterTetherSettings() {
1211d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1212d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                INetworkManagementService service =
1213d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                        INetworkManagementService.Stub.asInterface(b);
1214d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1215d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    service.stopTethering();
1216d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1217d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    transitionTo(mStopTetheringErrorState);
1218d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1219d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1220d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1221d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    service.setIpForwardingEnabled(false);
1222d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1223d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    transitionTo(mSetIpForwardingDisabledErrorState);
1224d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1225d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1226d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                transitionTo(mInitialState);
1227d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1228d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
12296a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected String findActiveUpstreamIface() {
1230c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                // check for what iface we can use - if none found switch to error.
123105d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
123205d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b);
123305d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt
123405d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                try {
123505d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                    LinkProperties defaultProp = cm.getActiveLinkProperties();
123605d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                    if (defaultProp != null) {
123705d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                        String iface = defaultProp.getInterfaceName();
123805d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                        for(String regex : mUpstreamIfaceRegexs) {
123905d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                            if (iface.matches(regex)) return iface;
124005d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                        }
124105d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                    }
124205d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                } catch (RemoteException e) { }
124305d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt
124405d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1245c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
1246c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt
1247c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                String[] ifaces = new String[0];
1248c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                try {
1249c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    ifaces = service.listInterfaces();
1250c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                } catch (Exception e) {
1251c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    Log.e(TAG, "Error listing Interfaces :" + e);
1252c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    return null;
1253c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                }
12546a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
1255c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                for (String iface : ifaces) {
1256c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    for (String regex : mUpstreamIfaceRegexs) {
1257c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                        if (iface.matches(regex)) {
125829552095e19e3faf2006ac1051da41d9bd212d6fIrfan Sheriff                            // verify it is active
1259c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            InterfaceConfiguration ifcg = null;
1260c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            try {
1261c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                                ifcg = service.getInterfaceConfig(iface);
126229552095e19e3faf2006ac1051da41d9bd212d6fIrfan Sheriff                                if (ifcg.isActive()) {
126329552095e19e3faf2006ac1051da41d9bd212d6fIrfan Sheriff                                    return iface;
126429552095e19e3faf2006ac1051da41d9bd212d6fIrfan Sheriff                                }
1265c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            } catch (Exception e) {
1266c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                                Log.e(TAG, "Error getting iface config :" + e);
1267c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                                // ignore - try next
1268c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                                continue;
1269c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            }
1270c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                        }
1271c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    }
1272c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                }
1273c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                return null;
1274c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt            }
12751039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt
12766a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected void chooseUpstreamType(boolean tryCell) {
12776a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                // decide if the current upstream is good or not and if not
12786a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                // do something about it (start up DUN if required or HiPri if not)
12796a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                String iface = findActiveUpstreamIface();
12806a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
12816a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b);
12821039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                mMobileReserved = false;
1283924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) {
1284924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                    Log.d(TAG, "chooseUpstreamType(" + tryCell + "),  dunRequired ="
1285924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                            + mDunRequired + ", iface=" + iface);
1286924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                }
128777e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                if (iface != null) {
12886a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    try {
128977e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                        if (mDunRequired) {
129077e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                            // check if Dun is on - we can use that
129177e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                            NetworkInfo info = cm.getNetworkInfo(
129277e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                    ConnectivityManager.TYPE_MOBILE_DUN);
129377e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                            if (info.isConnected()) {
1294924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                                if (DEBUG) Log.d(TAG, "setting dun ifacename =" + iface);
129577e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                // even if we're already connected - it may be somebody else's
129677e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                // refcount, so add our own
129777e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                turnOnMobileConnection();
129877e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                            } else {
129977e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                // verify the iface is not the default mobile - can't use that!
130077e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                info = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
130177e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                if (info.isConnected()) {
130277e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                    iface = null; // can't accept this one
130377e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                }
130477e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                            }
130577e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                        } else {
1306924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                            if (DEBUG) Log.d(TAG, "checking if hipri brought us this connection");
13076a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            NetworkInfo info = cm.getNetworkInfo(
13086a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                    ConnectivityManager.TYPE_MOBILE_HIPRI);
13096a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            if (info.isConnected()) {
1310924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                                if (DEBUG) Log.d(TAG, "yes - hipri in use");
131177e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                // even if we're already connected - it may be sombody else's
131277e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                // refcount, so add our own
13136a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                turnOnMobileConnection();
13142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            }
13152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
131677e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                    } catch (RemoteException e) {
131777e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                        Log.e(TAG, "RemoteException calling ConnectivityManager " + e);
131877e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                        iface = null;
13196a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    }
13206a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
132177e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                // may have been set to null in the if above
132277e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                if (iface == null ) {
13231039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    boolean success = false;
132477e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                    if (tryCell == TRY_TO_SETUP_MOBILE_CONNECTION) {
13251039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        success = turnOnMobileConnection();
13261039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    }
13271039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    if (!success) {
13281039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        // wait for things to settle and retry
13291039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        sendMessageDelayed(CMD_RETRY_UPSTREAM, UPSTREAM_SETTLE_TIME_MS);
133077e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                    }
13316a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
1332b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                notifyTetheredOfNewUpstreamIface(iface);
13336a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            }
1334b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt            protected void notifyTetheredOfNewUpstreamIface(String ifaceName) {
1335924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "notifying tethered with iface =" + ifaceName);
1336b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                mUpstreamIfaceName = ifaceName;
13376a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                for (Object o : mNotifyList) {
13386a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    TetherInterfaceSM sm = (TetherInterfaceSM)o;
13396a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED,
13406a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            ifaceName);
13412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
13422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
13442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
13456a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        class InitialState extends TetherMasterUtilState {
13462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
13481039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                mMobileReserved = false;
13492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
1352924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "MasterInitialState.processMessage what=" + message.what);
13532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
13542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
13552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
135677b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt                        mDunRequired = isDunRequired();
13572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
1358924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                        if (DEBUG) Log.d(TAG, "Tether Mode requested by " + who.toString());
13592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        mNotifyList.add(who);
13606a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        transitionTo(mTetherModeAliveState);
13612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_UNREQUESTED:
13632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        who = (TetherInterfaceSM)message.obj;
1364924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                        if (DEBUG) Log.d(TAG, "Tether Mode unrequested by " + who.toString());
13652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        int index = mNotifyList.indexOf(who);
13662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (index != -1) {
13676a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            mNotifyList.remove(who);
13682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
13692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
13712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
13722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
13742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
13752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
13772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1378d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        class TetherModeAliveState extends TetherMasterUtilState {
137919b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt            boolean mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE;
13802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
138219b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt                mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE; // better try something first pass
138319b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt                                                        // or crazy tests cases will fail
13846a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                chooseUpstreamType(mTryCell);
13856a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                mTryCell = !mTryCell;
13866a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                turnOnMasterTetherSettings(); // may transition us out
13876a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            }
13886a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            @Override
13896a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            public void exit() {
13906a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                turnOffMobileConnection();
1391b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                notifyTetheredOfNewUpstreamIface(null);
13922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
1395924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "TetherModeAliveState.processMessage what=" + message.what);
13962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
13972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
13982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
13992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
14002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        mNotifyList.add(who);
1401b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                        who.sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED,
1402b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mUpstreamIfaceName);
14032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
14042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_UNREQUESTED:
14052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        who = (TetherInterfaceSM)message.obj;
14062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        int index = mNotifyList.indexOf(who);
14072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (index != -1) {
14082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            mNotifyList.remove(index);
14092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            if (mNotifyList.isEmpty()) {
1410d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                                turnOffMasterTetherSettings(); // transitions appropriately
14112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            }
14122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
14132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
14146a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_UPSTREAM_CHANGED:
141505d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                        // need to try DUN immediately if Wifi goes down
141605d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                        mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE;
14176a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        chooseUpstreamType(mTryCell);
14186a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTryCell = !mTryCell;
14192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
14206a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_CELL_CONNECTION_RENEW:
14216a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        // make sure we're still using a requested connection - may have found
14226a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        // wifi or something since then.
14231039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        if (mCurrentConnectionSequence == message.arg1) {
1424924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                            if (DEBUG) {
1425924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                                Log.d(TAG, "renewing mobile connection - requeuing for another " +
1426924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                                        CELL_CONNECTION_RENEW_MS + "ms");
1427924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                            }
14281039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                            mMobileReserved = false; // need to renew it
14296a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            turnOnMobileConnection();
14306a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        }
143165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        break;
14326a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                   case CMD_RETRY_UPSTREAM:
14336a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                       chooseUpstreamType(mTryCell);
14346a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                       mTryCell = !mTryCell;
14356a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                       break;
14366a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                   default:
14372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                       retValue = false;
14382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                       break;
14392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
14402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
14412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
144464c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville        class ErrorState extends State {
14452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            int mErrorNotification;
14462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
14482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
14492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
14502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
14512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
14526a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        who.sendMessage(mErrorNotification);
14532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
14542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
14552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                       retValue = false;
14562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
14572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
14582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            void notify(int msgType) {
14602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                mErrorNotification = msgType;
14612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                for (Object o : mNotifyList) {
14622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    TetherInterfaceSM sm = (TetherInterfaceSM)o;
14636a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(msgType);
14642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
14652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetIpForwardingEnabledErrorState extends ErrorState {
14692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setIpForwardingEnabled");
14722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_IP_FORWARDING_ENABLE_ERROR);
14732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetIpForwardingDisabledErrorState extends ErrorState {
14772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setIpForwardingDisabled");
14802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_IP_FORWARDING_DISABLE_ERROR);
14812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class StartTetheringErrorState extends ErrorState {
14852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in startTethering");
14882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_START_TETHERING_ERROR);
14892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
14902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                INetworkManagementService service =
14912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        INetworkManagementService.Stub.asInterface(b);
14922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
14932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    service.setIpForwardingEnabled(false);
14942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
14952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class StopTetheringErrorState extends ErrorState {
14992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
15002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
15012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in stopTethering");
15022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_STOP_TETHERING_ERROR);
15032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
15042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                INetworkManagementService service =
15052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                         INetworkManagementService.Stub.asInterface(b);
15062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
15072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    service.setIpForwardingEnabled(false);
15082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
15092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
15102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
15112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
15122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetDnsForwardersErrorState extends ErrorState {
15132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
15142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
15152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setDnsForwarders");
15162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_SET_DNS_FORWARDERS_ERROR);
15172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
15182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                INetworkManagementService service =
15192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        INetworkManagementService.Stub.asInterface(b);
15202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
15212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    service.stopTethering();
15222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
15232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
15242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    service.setIpForwardingEnabled(false);
15252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
15262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
1527d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
1528d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
1529d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
15302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
15312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (mContext.checkCallingOrSelfPermission(
15322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
15332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            pw.println("Permission Denial: can't dump ConnectivityService.Tether " +
15342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    "from from pid=" + Binder.getCallingPid() + ", uid=" +
15352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    Binder.getCallingUid());
15362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    return;
15372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
15382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
15392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        pw.println();
15402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        pw.println("Tether state:");
15412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
15422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object o : mIfaces.values()) {
15432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                pw.println(" "+o.toString());
15442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
1545d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
15462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        pw.println();
15472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return;
1548d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
1549d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt}
1550