Tethering.java revision e590373ea71251cfffc8f22f011e2e6335dce716
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;
29770126a678ccc9328a89407ffc82f4d998b25427Mike Lockwoodimport android.hardware.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;
522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport com.android.internal.util.HierarchicalState;
532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport com.android.internal.util.HierarchicalStateMachine;
542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.io.FileDescriptor;
562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.io.PrintWriter;
5704808c294027f8bc318643a94c85a999257d7f52Robert Greenwaltimport java.net.InetAddress;
58d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport java.util.ArrayList;
592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.util.HashMap;
606fdd0c6274c81b337ad35b70480f881daf7354c3Danica Changimport java.util.LinkedList;
612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.util.Set;
62d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt/**
63d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * @hide
642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt *
652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt * Timeout
662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt *
672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt * TODO - look for parent classes and code sharing
68d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt */
6965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
70d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltpublic class Tethering extends INetworkManagementEventObserver.Stub {
71d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
72d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    private Context mContext;
73924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt    private final static String TAG = "Tethering";
741039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt    private final static boolean DEBUG = true;
75d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
76c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    private boolean mBooted = false;
77c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    //used to remember if we got connected before boot finished
78c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    private boolean mDeferedUsbConnection = false;
79d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    // TODO - remove both of these - should be part of interface inspection/selection stuff
812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mTetherableUsbRegexs;
822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mTetherableWifiRegexs;
836fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    private String[] mTetherableBluetoothRegexs;
84c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    private String[] mUpstreamIfaceRegexs;
852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
867eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt    private Looper mLooper;
877eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt    private HandlerThread mThread;
88dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt
89030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private HashMap<String, TetherInterfaceSM> mIfaces; // all tethered/tetherable ifaces
90d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
91d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    private BroadcastReceiver mStateReceiver;
92d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
93bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String USB_NEAR_IFACE_ADDR      = "192.168.42.129";
94b979f79158f9c470fa09ff3b96d72db274262201Robert Greenwalt    private static final int USB_PREFIX_LENGTH        = 24;
95bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt
96707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    // USB is  192.168.42.1 and 255.255.255.0
97707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    // Wifi is 192.168.43.1 and 255.255.255.0
98707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    // BT is limited to max default of 5 connections. 192.168.44.1 to 192.168.48.1
99707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    // with 255.255.255.0
100d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt
1012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mDhcpRange;
102bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String DHCP_DEFAULT_RANGE1_START = "192.168.42.2";
103bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String DHCP_DEFAULT_RANGE1_STOP  = "192.168.42.254";
104bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String DHCP_DEFAULT_RANGE2_START = "192.168.43.2";
105bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt    private static final String DHCP_DEFAULT_RANGE2_STOP  = "192.168.43.254";
106707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    private static final String DHCP_DEFAULT_RANGE3_START = "192.168.44.2";
1076fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    private static final String DHCP_DEFAULT_RANGE3_STOP  = "192.168.44.254";
108707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    private static final String DHCP_DEFAULT_RANGE4_START = "192.168.45.2";
109707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    private static final String DHCP_DEFAULT_RANGE4_STOP  = "192.168.45.254";
110707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    private static final String DHCP_DEFAULT_RANGE5_START = "192.168.46.2";
111707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    private static final String DHCP_DEFAULT_RANGE5_STOP  = "192.168.46.254";
112707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    private static final String DHCP_DEFAULT_RANGE6_START = "192.168.47.2";
113707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    private static final String DHCP_DEFAULT_RANGE6_STOP  = "192.168.47.254";
114707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    private static final String DHCP_DEFAULT_RANGE7_START = "192.168.48.2";
115707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh    private static final String DHCP_DEFAULT_RANGE7_STOP  = "192.168.48.254";
116707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh
1172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private String[] mDnsServers;
119d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private static final String DNS_DEFAULT_SERVER1 = "8.8.8.8";
120df255c2283b2bcf2d5e70d095a4d2333c1cb80a3Robert Greenwalt    private static final String DNS_DEFAULT_SERVER2 = "8.8.4.4";
1212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
12277b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt    // resampled each time we turn on tethering - used as cache for settings/config-val
123030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private boolean mDunRequired;  // configuration info - must use DUN apn on 3g
124030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
125a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private HierarchicalStateMachine mTetherMasterSM;
126a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
127a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private Notification mTetheredNotification;
1282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
129030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    // whether we can tether is the && of these two - they come in as separate
130030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    // broadcasts so track them so we can decide what to do when either changes
131030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private boolean mUsbMassStorageOff;  // track the status of USB Mass Storage
132030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private boolean mUsbConnected;       // track the status of USB connection
133030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
134dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt    public Tethering(Context context, Looper looper) {
135d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        mContext = context;
136dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        mLooper = looper;
137d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
138d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        // register for notifications from NetworkManagement Service
139d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
140d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
141d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        try {
142d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            service.registerObserver(this);
143d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        } catch (RemoteException e) {
144d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            Log.e(TAG, "Error registering observer :" + e);
145d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
146d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
1472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mIfaces = new HashMap<String, TetherInterfaceSM>();
148d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
1497eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        // make our own thread so we don't anr the system
1507eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        mThread = new HandlerThread("Tethering");
1517eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        mThread.start();
1527eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt        mLooper = mThread.getLooper();
153dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        mTetherMasterSM = new TetherMasterSM("TetherMaster", mLooper);
1542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mTetherMasterSM.start();
1552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
156bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        mStateReceiver = new StateReceiver();
157d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        IntentFilter filter = new IntentFilter();
158770126a678ccc9328a89407ffc82f4d998b25427Mike Lockwood        filter.addAction(UsbManager.ACTION_USB_STATE);
1592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
160c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt        filter.addAction(Intent.ACTION_BOOT_COMPLETED);
161bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        mContext.registerReceiver(mStateReceiver, filter);
162bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt
163bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        filter = new IntentFilter();
164030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        filter.addAction(Intent.ACTION_MEDIA_SHARED);
165030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
166bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        filter.addDataScheme("file");
167bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt        mContext.registerReceiver(mStateReceiver, filter);
168bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt
169030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        mUsbMassStorageOff = !Environment.MEDIA_SHARED.equals(
170030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                Environment.getExternalStorageState());
1712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mDhcpRange = context.getResources().getStringArray(
1732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                com.android.internal.R.array.config_tether_dhcp_range);
174bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt        if ((mDhcpRange.length == 0) || (mDhcpRange.length % 2 ==1)) {
175707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh            mDhcpRange = new String[14];
176bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt            mDhcpRange[0] = DHCP_DEFAULT_RANGE1_START;
177bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt            mDhcpRange[1] = DHCP_DEFAULT_RANGE1_STOP;
178bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt            mDhcpRange[2] = DHCP_DEFAULT_RANGE2_START;
179bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt            mDhcpRange[3] = DHCP_DEFAULT_RANGE2_STOP;
1806fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            mDhcpRange[4] = DHCP_DEFAULT_RANGE3_START;
1816fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            mDhcpRange[5] = DHCP_DEFAULT_RANGE3_STOP;
182707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh            mDhcpRange[6] = DHCP_DEFAULT_RANGE4_START;
183707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh            mDhcpRange[7] = DHCP_DEFAULT_RANGE4_STOP;
184707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh            mDhcpRange[8] = DHCP_DEFAULT_RANGE5_START;
185707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh            mDhcpRange[9] = DHCP_DEFAULT_RANGE5_STOP;
186707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh            mDhcpRange[10] = DHCP_DEFAULT_RANGE6_START;
187707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh            mDhcpRange[11] = DHCP_DEFAULT_RANGE6_STOP;
188707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh            mDhcpRange[12] = DHCP_DEFAULT_RANGE7_START;
189707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh            mDhcpRange[13] = DHCP_DEFAULT_RANGE7_STOP;
1902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
19177b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt        mDunRequired = false; // resample when we turn on
1922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mTetherableUsbRegexs = context.getResources().getStringArray(
1942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                com.android.internal.R.array.config_tether_usb_regexs);
1952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mTetherableWifiRegexs = context.getResources().getStringArray(
1962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                com.android.internal.R.array.config_tether_wifi_regexs);
1976fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        mTetherableBluetoothRegexs = context.getResources().getStringArray(
1986fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                com.android.internal.R.array.config_tether_bluetooth_regexs);
199c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt        mUpstreamIfaceRegexs = context.getResources().getStringArray(
200c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                com.android.internal.R.array.config_tether_upstream_regexs);
2012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
2022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // TODO - remove and rely on real notifications of the current iface
2032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mDnsServers = new String[2];
204d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        mDnsServers[0] = DNS_DEFAULT_SERVER1;
205d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        mDnsServers[1] = DNS_DEFAULT_SERVER2;
206d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
207d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
2082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void interfaceLinkStatusChanged(String iface, boolean link) {
209924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) Log.d(TAG, "interfaceLinkStatusChanged " + iface + ", " + link);
2102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        boolean found = false;
21165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean usb = false;
212a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (isWifi(iface)) {
213a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
214a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        } else if (isUsb(iface)) {
215a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
216a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            usb = true;
2176fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        } else if (isBluetooth(iface)) {
2186fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            found = true;
2192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
2202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (found == false) return;
2212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
2222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
2232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
2242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (link) {
2252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm == null) {
226dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt                    sm = new TetherInterfaceSM(iface, mLooper, usb);
2272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    mIfaces.put(iface, sm);
2282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    sm.start();
2292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
2302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            } else {
2312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm != null) {
2326a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(TetherInterfaceSM.CMD_INTERFACE_DOWN);
2332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    mIfaces.remove(iface);
2342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
2352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
2362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
237d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
238d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
239a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private boolean isUsb(String iface) {
240a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        for (String regex : mTetherableUsbRegexs) {
241a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (iface.matches(regex)) return true;
242a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
243a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        return false;
244a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
245a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
246a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    public boolean isWifi(String iface) {
247a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        for (String regex : mTetherableWifiRegexs) {
248a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (iface.matches(regex)) return true;
249a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
250a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        return false;
251a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
252a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
2536fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    public boolean isBluetooth(String iface) {
2546fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        for (String regex : mTetherableBluetoothRegexs) {
2556fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            if (iface.matches(regex)) return true;
2566fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        }
2576fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        return false;
2586fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    }
2592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void interfaceAdded(String iface) {
26065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
26165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
2622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        boolean found = false;
26365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean usb = false;
264a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (isWifi(iface)) {
265a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
2662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
267a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (isUsb(iface)) {
268a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            found = true;
269a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            usb = true;
270d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
2716fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        if (isBluetooth(iface)) {
2726fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            found = true;
2736fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        }
2742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (found == false) {
275924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt            if (DEBUG) Log.d(TAG, iface + " is not a tetherable iface, ignoring");
276d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            return;
277d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
27865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
2792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
2802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
2812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (sm != null) {
282924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "active iface (" + iface + ") reported as added, ignoring");
2832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return;
2842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
285dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            sm = new TetherInterfaceSM(iface, mLooper, usb);
2862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaces.put(iface, sm);
2872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm.start();
2882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
289924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) Log.d(TAG, "interfaceAdded :" + iface);
290d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
291d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
2922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void interfaceRemoved(String iface) {
2932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
2942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            TetherInterfaceSM sm = mIfaces.get(iface);
2952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (sm == null) {
296924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) {
297924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                    Log.e(TAG, "attempting to remove unknown iface (" + iface + "), ignoring");
298924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                }
2992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return;
3002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
3016a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            sm.sendMessage(TetherInterfaceSM.CMD_INTERFACE_DOWN);
3022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaces.remove(iface);
303d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
304d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
305d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3065a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int tether(String iface) {
307d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Log.d(TAG, "Tethering " + iface);
3082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        TetherInterfaceSM sm = null;
3092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
3102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm = mIfaces.get(iface);
311d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm == null) {
3132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Tether an unknown iface :" + iface + ", ignoring");
3145a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
315d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3165a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        if (!sm.isAvailable() && !sm.isErrored()) {
3172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Tether an unavailable iface :" + iface + ", ignoring");
3185a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
319d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3206a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        sm.sendMessage(TetherInterfaceSM.CMD_TETHER_REQUESTED);
3215a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return ConnectivityManager.TETHER_ERROR_NO_ERROR;
322d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
323d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3245a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int untether(String iface) {
325d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Log.d(TAG, "Untethering " + iface);
3262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        TetherInterfaceSM sm = null;
3272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
3282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            sm = mIfaces.get(iface);
3292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
3302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm == null) {
3312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Untether an unknown iface :" + iface + ", ignoring");
3325a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
333d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (sm.isErrored()) {
3352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Log.e(TAG, "Tried to Untethered an errored iface :" + iface + ", ignoring");
3365a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE;
337d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
3386a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        sm.sendMessage(TetherInterfaceSM.CMD_TETHER_UNREQUESTED);
3395a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return ConnectivityManager.TETHER_ERROR_NO_ERROR;
3405a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
3415a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
3425a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public int getLastTetherError(String iface) {
3435a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        TetherInterfaceSM sm = null;
3445a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        synchronized (mIfaces) {
3455a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            sm = mIfaces.get(iface);
3465a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
3475a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        if (sm == null) {
3485a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            Log.e(TAG, "Tried to getLastTetherError on an unknown iface :" + iface + ", ignoring");
3495a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE;
3505a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
3515a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return sm.getLastError();
3522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
353d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private void sendTetherStateChangedBroadcast() {
3552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
3562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
357d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        try {
3582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (!service.isTetheringSupported()) return;
3592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        } catch (RemoteException e) {
3602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return;
361d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
362d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
3632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> availableList = new ArrayList<String>();
3642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> activeList = new ArrayList<String>();
3652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> erroredList = new ArrayList<String>();
3662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
367a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        boolean wifiTethered = false;
368a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        boolean usbTethered = false;
3696fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        boolean bluetoothTethered = false;
370a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
3712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
3722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set ifaces = mIfaces.keySet();
3732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object iface : ifaces) {
3742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(iface);
3752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm != null) {
3766fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                    if (sm.isErrored()) {
3772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        erroredList.add((String)iface);
3782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    } else if (sm.isAvailable()) {
3792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        availableList.add((String)iface);
3802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    } else if (sm.isTethered()) {
381a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        if (isUsb((String)iface)) {
382a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            usbTethered = true;
383a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        } else if (isWifi((String)iface)) {
384a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            wifiTethered = true;
3856fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                      } else if (isBluetooth((String)iface)) {
3866fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                            bluetoothTethered = true;
387a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        }
3882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        activeList.add((String)iface);
3892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    }
3902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
391d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            }
392d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
393d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        Intent broadcast = new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
394d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        broadcast.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
3952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_AVAILABLE_TETHER,
3962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                availableList);
3972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ACTIVE_TETHER, activeList);
3982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ERRORED_TETHER,
3992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                erroredList);
4002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        mContext.sendStickyBroadcast(broadcast);
401924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) {
402924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt            Log.d(TAG, "sendTetherStateChangedBroadcast " + availableList.size() + ", " +
403924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                    activeList.size() + ", " + erroredList.size());
404924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        }
405a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
406a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (usbTethered) {
4076fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            if (wifiTethered || bluetoothTethered) {
408a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general);
409a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            } else {
410a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_usb);
411a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            }
412a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        } else if (wifiTethered) {
4136fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            if (bluetoothTethered) {
4146fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general);
4156fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            } else {
4166fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang                showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_wifi);
4176fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            }
4186fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        } else if (bluetoothTethered) {
4196fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang            showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_bluetooth);
420a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        } else {
421a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            clearTetheredNotification();
422a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
423a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
424a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
425a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private void showTetheredNotification(int icon) {
426a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        NotificationManager notificationManager =
427a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
428a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (notificationManager == null) {
429a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            return;
430a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
431a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
432db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt        if (mTetheredNotification != null) {
433db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            if (mTetheredNotification.icon == icon) {
434db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt                return;
435db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            }
436db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt            notificationManager.cancel(mTetheredNotification.icon);
437db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt        }
438db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt
439a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        Intent intent = new Intent();
440a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        intent.setClassName("com.android.settings", "com.android.settings.TetherSettings");
441a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
442a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
443a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0);
444a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
445a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        Resources r = Resources.getSystem();
446a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        CharSequence title = r.getText(com.android.internal.R.string.tethered_notification_title);
447a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        CharSequence message = r.getText(com.android.internal.R.string.
448a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                tethered_notification_message);
449a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
4506fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        if (mTetheredNotification == null) {
451a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification = new Notification();
452a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification.when = 0;
453a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
454a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.icon = icon;
455a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.defaults &= ~Notification.DEFAULT_SOUND;
456a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.flags = Notification.FLAG_ONGOING_EVENT;
457a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.tickerText = title;
458a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        mTetheredNotification.setLatestEventInfo(mContext, title, message, pi);
459a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
460a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        notificationManager.notify(mTetheredNotification.icon, mTetheredNotification);
461a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    }
462a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt
463a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt    private void clearTetheredNotification() {
464a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        NotificationManager notificationManager =
465a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE);
466a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        if (notificationManager != null && mTetheredNotification != null) {
467a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            notificationManager.cancel(mTetheredNotification.icon);
468a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            mTetheredNotification = null;
469a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt        }
4705a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
4715a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
472030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    private void updateUsbStatus() {
473030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        boolean enable = mUsbConnected && mUsbMassStorageOff;
474030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
475030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        if (mBooted) {
476030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            enableUsbIfaces(enable);
477030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt        }
478030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt    }
479030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt
4802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    private class StateReceiver extends BroadcastReceiver {
481d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        public void onReceive(Context content, Intent intent) {
4822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            String action = intent.getAction();
483770126a678ccc9328a89407ffc82f4d998b25427Mike Lockwood            if (action.equals(UsbManager.ACTION_USB_STATE)) {
484b92df0f66926252cf60cdda7812bb1d50ce9378eMike Lockwood                mUsbConnected = intent.getExtras().getBoolean(UsbManager.USB_CONNECTED);
485b92df0f66926252cf60cdda7812bb1d50ce9378eMike Lockwood                updateUsbStatus();
486030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
487030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                mUsbMassStorageOff = false;
488030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                updateUsbStatus();
489030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            }
490030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt            else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) {
491030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                mUsbMassStorageOff = true;
492030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                updateUsbStatus();
4932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
494924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "Tethering got CONNECTIVITY_ACTION");
4956a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED);
496c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt            } else if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {
497c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                mBooted = true;
498030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt                updateUsbStatus();
499d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt            }
500d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
501d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
502d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
50365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    // used on cable insert/remove
504d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private void enableUsbIfaces(boolean enable) {
50565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
50665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
50765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        String[] ifaces = new String[0];
50865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        try {
50965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            ifaces = service.listInterfaces();
51065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        } catch (Exception e) {
51165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            Log.e(TAG, "Error listing Interfaces :" + e);
51265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            return;
51365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
51465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        for (String iface : ifaces) {
515a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (isUsb(iface)) {
516a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                if (enable) {
517a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    interfaceAdded(iface);
518a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                } else {
519a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    interfaceRemoved(iface);
52065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
52165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
52265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
52365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    }
52465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
5256fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    // toggled when we enter/leave the fully tethered state
526d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private boolean enableUsbRndis(boolean enabled) {
527924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) Log.d(TAG, "enableUsbRndis(" + enabled + ")");
52865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
52965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
53065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
53165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        try {
53265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            if (enabled) {
53365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                synchronized (this) {
53465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    if (!service.isUsbRNDISStarted()) {
53565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        service.startUsbRNDIS();
53665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    }
53765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
53865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            } else {
53965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                if (service.isUsbRNDISStarted()) {
54065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    service.stopUsbRNDIS();
54165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
54265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
54365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        } catch (Exception e) {
54465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            Log.e(TAG, "Error toggling usb RNDIS :" + e);
54565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            return false;
54665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
54765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        return true;
54865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    }
54965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
55065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    // configured when we start tethering and unconfig'd on error or conclusion
551d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt    private boolean configureUsbIface(boolean enabled) {
552924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt        if (DEBUG) Log.d(TAG, "configureUsbIface(" + enabled + ")");
55365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
55465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
55565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
55665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
55765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        // bring toggle the interfaces
55865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        String[] ifaces = new String[0];
55965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        try {
56065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            ifaces = service.listInterfaces();
56165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        } catch (Exception e) {
56265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            Log.e(TAG, "Error listing Interfaces :" + e);
56365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            return false;
56465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
56565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        for (String iface : ifaces) {
566a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt            if (isUsb(iface)) {
567a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                InterfaceConfiguration ifcg = null;
568a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                try {
569a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    ifcg = service.getInterfaceConfig(iface);
570a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    if (ifcg != null) {
571e590373ea71251cfffc8f22f011e2e6335dce716Robert Greenwalt                        InetAddress addr = NetworkUtils.numericToInetAddress(USB_NEAR_IFACE_ADDR);
572b979f79158f9c470fa09ff3b96d72db274262201Robert Greenwalt                        ifcg.addr = new LinkAddress(addr, USB_PREFIX_LENGTH);
573a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        if (enabled) {
574a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            ifcg.interfaceFlags = ifcg.interfaceFlags.replace("down", "up");
575a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        } else {
576a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                            ifcg.interfaceFlags = ifcg.interfaceFlags.replace("up", "down");
57765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
578a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        ifcg.interfaceFlags = ifcg.interfaceFlags.replace("running", "");
579a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        ifcg.interfaceFlags = ifcg.interfaceFlags.replace("  "," ");
580a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                        service.setInterfaceConfig(iface, ifcg);
58165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    }
582a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                } catch (Exception e) {
583a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    Log.e(TAG, "Error configuring interface " + iface + ", :" + e);
584a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt                    return false;
58565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
58665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
58765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        }
58865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
58965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        return true;
59065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt    }
59165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
5922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetherableUsbRegexs() {
5932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return mTetherableUsbRegexs;
5942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
5952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
5962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetherableWifiRegexs() {
5972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return mTetherableWifiRegexs;
5982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
5992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6006fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    public String[] getTetherableBluetoothRegexs() {
6016fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang        return mTetherableBluetoothRegexs;
6026fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang    }
6036fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang
604c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    public String[] getUpstreamIfaceRegexs() {
605c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt        return mUpstreamIfaceRegexs;
606c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    }
607c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt
608c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    public boolean isDunRequired() {
60977b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt        boolean defaultVal = mContext.getResources().getBoolean(
61077b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt                com.android.internal.R.bool.config_tether_dun_required);
61177b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt        boolean result = (Settings.Secure.getInt(mContext.getContentResolver(),
61277b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt                Settings.Secure.TETHER_DUN_REQUIRED, (defaultVal ? 1 : 0)) == 1);
61377b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt        return result;
614c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt    }
615c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt
6162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetheredIfaces() {
6172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> list = new ArrayList<String>();
6182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
6192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set keys = mIfaces.keySet();
6202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object key : keys) {
6212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
6222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm.isTethered()) {
6232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    list.add((String)key);
6242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
6252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
6262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String[] retVal = new String[list.size()];
6282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        for (int i=0; i < list.size(); i++) {
6292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            retVal[i] = list.get(i);
6302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return retVal;
6322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
6332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public String[] getTetherableIfaces() {
6352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        ArrayList<String> list = new ArrayList<String>();
6362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
6372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            Set keys = mIfaces.keySet();
6382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object key : keys) {
6392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
6402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                if (sm.isAvailable()) {
6412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    list.add((String)key);
6422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
6432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
6442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String[] retVal = new String[list.size()];
6462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        for (int i=0; i < list.size(); i++) {
6472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            retVal[i] = list.get(i);
6482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
6492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return retVal;
6502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
6512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6525a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    public String[] getErroredIfaces() {
6535a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        ArrayList<String> list = new ArrayList<String>();
6545a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        synchronized (mIfaces) {
6555a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            Set keys = mIfaces.keySet();
6565a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            for (Object key : keys) {
6575a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                TetherInterfaceSM sm = mIfaces.get(key);
6585a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                if (sm.isErrored()) {
6595a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    list.add((String)key);
6605a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                }
6615a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            }
6625a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
6635a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        String[] retVal = new String[list.size()];
6645a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        for (int i= 0; i< list.size(); i++) {
6655a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            retVal[i] = list.get(i);
6665a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
6675a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        return retVal;
6685a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt    }
6695a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
6702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    class TetherInterfaceSM extends HierarchicalStateMachine {
6722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it's not in tether mode
6736a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_MODE_DEAD            =  1;
6742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // request from the user that it wants to tether
6756a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_REQUESTED            =  2;
6762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // request from the user that it wants to untether
6776a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_TETHER_UNREQUESTED          =  3;
6782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification that this interface is down
6796a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_INTERFACE_DOWN              =  4;
6802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification that this interface is up
6816a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_INTERFACE_UP                =  5;
6822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had an error turning on cellular dun
6836a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_CELL_DUN_ERROR              =  6;
6842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble enabling IP Forwarding
6856a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_IP_FORWARDING_ENABLE_ERROR  =  7;
6862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble disabling IP Forwarding
6876a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_IP_FORWARDING_DISABLE_ERROR =  8;
6882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble staring tethering
6896a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_START_TETHERING_ERROR       =  9;
6902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble stopping tethering
6916a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_STOP_TETHERING_ERROR        = 10;
6922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // notification from the master SM that it had trouble setting the DNS forwarders
6936a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_SET_DNS_FORWARDERS_ERROR    = 11;
6946a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // the upstream connection has changed
695951749ba2e014566553d17c512e947472951a060Wink Saville        static final int CMD_TETHER_CONNECTION_CHANGED   = 12;
6962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mDefaultState;
6982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
6992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mInitialState;
7002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mStartingState;
7012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mTetheredState;
7022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mUnavailableState;
7042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private boolean mAvailable;
7062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private boolean mTethered;
7075a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        int mLastError;
7082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        String mIfaceName;
7106a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        String mMyUpstreamIfaceName;  // may change over time
7116a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
71265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt        boolean mUsb;
7132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
714dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        TetherInterfaceSM(String name, Looper looper, boolean usb) {
715dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            super(name, looper);
7162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mIfaceName = name;
71765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            mUsb = usb;
7185a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
7192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mInitialState = new InitialState();
7212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mInitialState);
7222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStartingState = new StartingState();
7232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStartingState);
7242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mTetheredState = new TetheredState();
7252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mTetheredState);
7262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mUnavailableState = new UnavailableState();
7272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mUnavailableState);
7282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            setInitialState(mInitialState);
7302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public String toString() {
7332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            String res = new String();
7342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            res += mIfaceName + " - ";
7352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            HierarchicalState current = getCurrentState();
7362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mInitialState) res += "InitialState";
7372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mStartingState) res += "StartingState";
7382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mTetheredState) res += "TetheredState";
7392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (current == mUnavailableState) res += "UnavailableState";
7402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (mAvailable) res += " - Available";
7412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            if (mTethered) res += " - Tethered";
7425a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            res += " - lastError =" + mLastError;
7432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return res;
7442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7465a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        public synchronized int getLastError() {
7475a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return mLastError;
7485a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
7495a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
7505a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        private synchronized void setLastError(int error) {
7515a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            mLastError = error;
7525a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
7535a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            if (isErrored()) {
7545a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                if (mUsb) {
7555a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    // note everything's been unwound by this point so nothing to do on
7565a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    // further error..
7575a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    Tethering.this.configureUsbIface(false);
7585a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                }
7595a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            }
7605a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        }
7615a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
7622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // synchronized between this getter and the following setter
7632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public synchronized boolean isAvailable() {
7642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return mAvailable;
7652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private synchronized void setAvailable(boolean available) {
7682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mAvailable = available;
7692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // synchronized between this getter and the following setter
7722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public synchronized boolean isTethered() {
7732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            return mTethered;
7742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private synchronized void setTethered(boolean tethered) {
7772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mTethered = tethered;
7782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // synchronized between this getter and the following setter
7812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        public synchronized boolean isErrored() {
7825a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            return (mLastError != ConnectivityManager.TETHER_ERROR_NO_ERROR);
7832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
7842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class InitialState extends HierarchicalState {
7862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
7872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
7882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(true);
7892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(false);
7902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
7912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
7922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
7932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
7942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
795924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "InitialState.processMessage what=" + message.what);
7962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
7972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
7982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_REQUESTED:
7995a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
8006a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_REQUESTED,
8016a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
8022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mStartingState);
8032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
8052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mUnavailableState);
8062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
8082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
8092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
8112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
8122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
8152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class StartingState extends HierarchicalState {
8162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
8172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
8182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
81965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                if (mUsb) {
820d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    if (!Tethering.this.configureUsbIface(true)) {
8216a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
8226a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
8235a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastError(ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
8245a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
825951749ba2e014566553d17c512e947472951a060Wink Saville                        transitionTo(mInitialState);
82665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        return;
82765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                    }
82865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                }
8292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
8306a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
831951749ba2e014566553d17c512e947472951a060Wink Saville                // Skipping StartingState
832951749ba2e014566553d17c512e947472951a060Wink Saville                transitionTo(mTetheredState);
8332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
8352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
836924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "StartingState.processMessage what=" + message.what);
8372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
8382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
8392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    // maybe a parent class?
8402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_UNREQUESTED:
8416a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
8426a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
84365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        if (mUsb) {
844d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                            if (!Tethering.this.configureUsbIface(false)) {
8455a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                setLastErrorAndTransitionToInitialState(
8465a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
84765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                                break;
84865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
84965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
8502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
8512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_CELL_DUN_ERROR:
8532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_ENABLE_ERROR:
8542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_DISABLE_ERROR:
8552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_START_TETHERING_ERROR:
8562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_STOP_TETHERING_ERROR:
8572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_SET_DNS_FORWARDERS_ERROR:
8585a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                        setLastErrorAndTransitionToInitialState(
8595a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                ConnectivityManager.TETHER_ERROR_MASTER_ERROR);
8602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
8626a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
8636a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
8642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mUnavailableState);
8652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
8662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
8672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
8682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
8692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
8702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
8722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
8732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class TetheredState extends HierarchicalState {
8742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
8752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
8762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
8772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                INetworkManagementService service =
8782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        INetworkManagementService.Stub.asInterface(b);
8792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
8802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    service.tetherInterface(mIfaceName);
8812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {
8825a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                    setLastError(ConnectivityManager.TETHER_ERROR_TETHER_IFACE_ERROR);
8835a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
884951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mInitialState);
8852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    return;
8862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
887d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                if (mUsb) Tethering.this.enableUsbRndis(true);
888924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "Tethered " + mIfaceName);
8892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
8902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(true);
8912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
8922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
8932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
89465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            public void exit() {
895d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                if (mUsb) Tethering.this.enableUsbRndis(false);
89665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            }
89765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt            @Override
8982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
899924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "TetheredState.processMessage what=" + message.what);
9002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
9012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean error = false;
9022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
9032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_UNREQUESTED:
9042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_DOWN:
9052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
9062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        INetworkManagementService service =
9072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                                INetworkManagementService.Stub.asInterface(b);
9086a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (mMyUpstreamIfaceName != null) {
9095a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            try {
9106a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                service.disableNat(mIfaceName, mMyUpstreamIfaceName);
911b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mMyUpstreamIfaceName = null;
9126a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
9136a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
9146a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                    service.untetherInterface(mIfaceName);
9156a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
9165a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
9176a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastErrorAndTransitionToInitialState(
9186a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                        ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR);
9196a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                break;
9206a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
9212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
9222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        try {
9232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            service.untetherInterface(mIfaceName);
9242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } catch (Exception e) {
9255a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
9265a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR);
9272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
9282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
9296a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED,
9306a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                TetherInterfaceSM.this);
9312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (message.what == CMD_TETHER_UNREQUESTED) {
93265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            if (mUsb) {
933d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                                if (!Tethering.this.configureUsbIface(false)) {
9345a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    setLastError(
9355a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                            ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
93665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                                }
93765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
9385a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            transitionTo(mInitialState);
9392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } else if (message.what == CMD_INTERFACE_DOWN) {
9402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            transitionTo(mUnavailableState);
9412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
942924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                        if (DEBUG) Log.d(TAG, "Untethered " + mIfaceName);
9432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
9446a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_TETHER_CONNECTION_CHANGED:
9456a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        String newUpstreamIfaceName = (String)(message.obj);
9466a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
9476a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        service = INetworkManagementService.Stub.asInterface(b);
9481039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        if ((mMyUpstreamIfaceName == null && newUpstreamIfaceName == null) ||
9491039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                                (mMyUpstreamIfaceName != null &&
9501039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                                mMyUpstreamIfaceName.equals(newUpstreamIfaceName))) {
9511039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                            if (DEBUG) Log.d(TAG, "Connection changed noop - dropping");
9521039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                            break;
9531039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        }
9546a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (mMyUpstreamIfaceName != null) {
9556a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            try {
9566a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                service.disableNat(mIfaceName, mMyUpstreamIfaceName);
957b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mMyUpstreamIfaceName = null;
9586a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
9596a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
9606a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                    service.untetherInterface(mIfaceName);
9616a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
9626a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
9636a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastErrorAndTransitionToInitialState(
9646a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                        ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR);
9656a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                break;
9666a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
9676a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        }
9686a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (newUpstreamIfaceName != null) {
9696a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            try {
9706a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                service.enableNat(mIfaceName, newUpstreamIfaceName);
9716a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
9726a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
9736a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                    service.untetherInterface(mIfaceName);
9746a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
9756a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
9766a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastError(ConnectivityManager.TETHER_ERROR_ENABLE_NAT_ERROR);
9776a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                transitionTo(mInitialState);
9786a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                return true;
9796a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
9806a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        }
9816a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mMyUpstreamIfaceName = newUpstreamIfaceName;
9826a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        break;
9832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_CELL_DUN_ERROR:
9842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_ENABLE_ERROR:
9852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_IP_FORWARDING_DISABLE_ERROR:
9862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_START_TETHERING_ERROR:
9872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_STOP_TETHERING_ERROR:
9882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_SET_DNS_FORWARDERS_ERROR:
9892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        error = true;
9902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        // fall through
9912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_DEAD:
9922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
9932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        service = INetworkManagementService.Stub.asInterface(b);
9946a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        if (mMyUpstreamIfaceName != null) {
9955a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            try {
9966a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                service.disableNat(mIfaceName, mMyUpstreamIfaceName);
997b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mMyUpstreamIfaceName = null;
9986a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            } catch (Exception e) {
9996a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                try {
10006a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                    service.untetherInterface(mIfaceName);
10016a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                } catch (Exception ee) {}
10025a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt
10036a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                setLastErrorAndTransitionToInitialState(
10046a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                        ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR);
10056a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                break;
10066a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            }
10072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
10082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        try {
10092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            service.untetherInterface(mIfaceName);
10102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        } catch (Exception e) {
10115a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
10125a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR);
10132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
10142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
10152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (error) {
10165a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                            setLastErrorAndTransitionToInitialState(
10175a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                    ConnectivityManager.TETHER_ERROR_MASTER_ERROR);
10182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            break;
10192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
1020924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                        if (DEBUG) Log.d(TAG, "Tether lost upstream connection " + mIfaceName);
10212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        sendTetherStateChangedBroadcast();
102265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        if (mUsb) {
1023d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                            if (!Tethering.this.configureUsbIface(false)) {
10245a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                                setLastError(ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR);
102565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                            }
102665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        }
10272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
10282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
10302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
10312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
10332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
10342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
10352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
10362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class UnavailableState extends HierarchicalState {
10382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
10392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
10402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setAvailable(false);
10415a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt                setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR);
10422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                setTethered(false);
10432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                sendTetherStateChangedBroadcast();
10442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
10452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
10462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
10472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
10482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
10492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_INTERFACE_UP:
10502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        transitionTo(mInitialState);
10512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
10532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
10542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
10552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
10562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
10572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
10582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
10592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10605a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt        void setLastErrorAndTransitionToInitialState(int error) {
10615a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            setLastError(error);
10625a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt            transitionTo(mInitialState);
10632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
106465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt
10652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    }
10662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    class TetherMasterSM extends HierarchicalStateMachine {
10682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // an interface SM has requested Tethering
10692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        static final int CMD_TETHER_MODE_REQUESTED   = 1;
10702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // an interface SM has unrequested Tethering
10712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        static final int CMD_TETHER_MODE_UNREQUESTED = 2;
10726a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // upstream connection change - do the right thing
10736a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_UPSTREAM_CHANGED        = 3;
10742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // we received notice that the cellular DUN connection is up
10756a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        static final int CMD_CELL_CONNECTION_RENEW   = 4;
10766a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        // we don't have a valid upstream conn, check again after a delay
1077951749ba2e014566553d17c512e947472951a060Wink Saville        static final int CMD_RETRY_UPSTREAM          = 5;
10782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // This indicates what a timeout event relates to.  A state that
10802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // sends itself a delayed timeout event and handles incoming timeout events
10812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // should inc this when it is entered and whenever it sends a new timeout event.
10822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        // We do not flush the old ones.
10832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private int mSequenceNumber;
10842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mInitialState;
10862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mTetherModeAliveState;
10872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mSetIpForwardingEnabledErrorState;
10892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mSetIpForwardingDisabledErrorState;
10902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mStartTetheringErrorState;
10912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mStopTetheringErrorState;
10922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private HierarchicalState mSetDnsForwardersErrorState;
10932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        private ArrayList mNotifyList;
10952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
10961039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt        private int mCurrentConnectionSequence;
10971039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt        private boolean mMobileReserved = false;
10982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1099b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt        private String mUpstreamIfaceName = null;
1100b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt
11016a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        private static final int UPSTREAM_SETTLE_TIME_MS     = 10000;
11026a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        private static final int CELL_CONNECTION_RENEW_MS    = 40000;
11032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1104dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt        TetherMasterSM(String name, Looper looper) {
1105dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt            super(name, looper);
11062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            //Add states
11082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mInitialState = new InitialState();
11092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mInitialState);
11102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mTetherModeAliveState = new TetherModeAliveState();
11112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mTetherModeAliveState);
11122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetIpForwardingEnabledErrorState = new SetIpForwardingEnabledErrorState();
11142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetIpForwardingEnabledErrorState);
11152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetIpForwardingDisabledErrorState = new SetIpForwardingDisabledErrorState();
11162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetIpForwardingDisabledErrorState);
11172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStartTetheringErrorState = new StartTetheringErrorState();
11182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStartTetheringErrorState);
11192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mStopTetheringErrorState = new StopTetheringErrorState();
11202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mStopTetheringErrorState);
11212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mSetDnsForwardersErrorState = new SetDnsForwardersErrorState();
11222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            addState(mSetDnsForwardersErrorState);
11232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
11242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            mNotifyList = new ArrayList();
11252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            setInitialState(mInitialState);
11262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
11272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1128d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        class TetherMasterUtilState extends HierarchicalState {
11296a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected final static boolean TRY_TO_SETUP_MOBILE_CONNECTION = true;
11306a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected final static boolean WAIT_FOR_NETWORK_TO_SETTLE     = false;
11316a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
1132d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            @Override
1133d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            public boolean processMessage(Message m) {
1134d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return false;
1135d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
11361039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt            protected boolean turnOnMobileConnection() {
11371039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                boolean retValue = true;
11381039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                if (mMobileReserved) return retValue;
1139d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
1140d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                IConnectivityManager service = IConnectivityManager.Stub.asInterface(b);
11411039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                int result = Phone.APN_REQUEST_FAILED;
1142d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
11431039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    result = service.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
1144e32e812b11520b244374371ee49eaed20c84a6e3Robert Greenwalt                            (mDunRequired ? Phone.FEATURE_ENABLE_DUN_ALWAYS :
1145e32e812b11520b244374371ee49eaed20c84a6e3Robert Greenwalt                            Phone.FEATURE_ENABLE_HIPRI), new Binder());
1146d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1147d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
11481039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                switch (result) {
11496a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                case Phone.APN_ALREADY_ACTIVE:
11506a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                case Phone.APN_REQUEST_STARTED:
11511039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    mMobileReserved = true;
11521039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    Message m = obtainMessage(CMD_CELL_CONNECTION_RENEW);
11531039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    m.arg1 = ++mCurrentConnectionSequence;
11541039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    sendMessageDelayed(m, CELL_CONNECTION_RENEW_MS);
11556a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    break;
11566a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                case Phone.APN_REQUEST_FAILED:
11576a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                default:
11581039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    retValue = false;
11596a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    break;
11606a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
11616a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
1162d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return retValue;
1163d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
11646a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected boolean turnOffMobileConnection() {
11651039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                if (mMobileReserved) {
11666a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
11676a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    IConnectivityManager service =
11686a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            IConnectivityManager.Stub.asInterface(b);
11696a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    try {
11706a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        service.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE,
117119b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt                                (mDunRequired? Phone.FEATURE_ENABLE_DUN_ALWAYS :
117277e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                             Phone.FEATURE_ENABLE_HIPRI));
11736a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    } catch (Exception e) {
11746a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        return false;
11756a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    }
11761039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    mMobileReserved = false;
1177d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1178d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1179d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
11806a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected boolean turnOnMasterTetherSettings() {
1181d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1182d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                INetworkManagementService service =
1183d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                        INetworkManagementService.Stub.asInterface(b);
1184d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1185d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    service.setIpForwardingEnabled(true);
1186d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1187951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mSetIpForwardingEnabledErrorState);
1188d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1189d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1190d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1191bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt                    service.startTethering(mDhcpRange);
1192d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1193547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                    try {
1194547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                        service.stopTethering();
1195547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                        service.startTethering(mDhcpRange);
1196547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                    } catch (Exception ee) {
1197547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                        transitionTo(mStartTetheringErrorState);
1198547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                        return false;
1199547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt                    }
1200d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1201d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1202d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    service.setDnsForwarders(mDnsServers);
1203d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1204951749ba2e014566553d17c512e947472951a060Wink Saville                    transitionTo(mSetDnsForwardersErrorState);
1205d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1206d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1207d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1208d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
12096a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected boolean turnOffMasterTetherSettings() {
1210d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1211d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                INetworkManagementService service =
1212d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                        INetworkManagementService.Stub.asInterface(b);
1213d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1214d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    service.stopTethering();
1215d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1216d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    transitionTo(mStopTetheringErrorState);
1217d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1218d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1219d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                try {
1220d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    service.setIpForwardingEnabled(false);
1221d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                } catch (Exception e) {
1222d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    transitionTo(mSetIpForwardingDisabledErrorState);
1223d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                    return false;
1224d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                }
1225d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                transitionTo(mInitialState);
1226d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                return true;
1227d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt            }
12286a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected String findActiveUpstreamIface() {
1229c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                // check for what iface we can use - if none found switch to error.
123005d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
123105d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b);
123205d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt
123305d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                try {
123405d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                    LinkProperties defaultProp = cm.getActiveLinkProperties();
123505d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                    if (defaultProp != null) {
123605d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                        String iface = defaultProp.getInterfaceName();
123705d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                        for(String regex : mUpstreamIfaceRegexs) {
123805d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                            if (iface.matches(regex)) return iface;
123905d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                        }
124005d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                    }
124105d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                } catch (RemoteException e) { }
124205d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt
124305d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
1244c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                INetworkManagementService service = INetworkManagementService.Stub.asInterface(b);
1245c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt
1246c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                String[] ifaces = new String[0];
1247c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                try {
1248c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    ifaces = service.listInterfaces();
1249c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                } catch (Exception e) {
1250c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    Log.e(TAG, "Error listing Interfaces :" + e);
1251c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    return null;
1252c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                }
12536a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt
1254c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                for (String iface : ifaces) {
1255c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    for (String regex : mUpstreamIfaceRegexs) {
1256c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                        if (iface.matches(regex)) {
125729552095e19e3faf2006ac1051da41d9bd212d6fIrfan Sheriff                            // verify it is active
1258c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            InterfaceConfiguration ifcg = null;
1259c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            try {
1260c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                                ifcg = service.getInterfaceConfig(iface);
126129552095e19e3faf2006ac1051da41d9bd212d6fIrfan Sheriff                                if (ifcg.isActive()) {
126229552095e19e3faf2006ac1051da41d9bd212d6fIrfan Sheriff                                    return iface;
126329552095e19e3faf2006ac1051da41d9bd212d6fIrfan Sheriff                                }
1264c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            } catch (Exception e) {
1265c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                                Log.e(TAG, "Error getting iface config :" + e);
1266c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                                // ignore - try next
1267c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                                continue;
1268c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                            }
1269c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                        }
1270c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                    }
1271c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                }
1272c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt                return null;
1273c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt            }
12741039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt
12756a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            protected void chooseUpstreamType(boolean tryCell) {
12766a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                // decide if the current upstream is good or not and if not
12776a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                // do something about it (start up DUN if required or HiPri if not)
12786a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                String iface = findActiveUpstreamIface();
12796a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE);
12806a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b);
12811039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                mMobileReserved = false;
1282924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) {
1283924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                    Log.d(TAG, "chooseUpstreamType(" + tryCell + "),  dunRequired ="
1284924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                            + mDunRequired + ", iface=" + iface);
1285924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                }
128677e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                if (iface != null) {
12876a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    try {
128877e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                        if (mDunRequired) {
128977e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                            // check if Dun is on - we can use that
129077e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                            NetworkInfo info = cm.getNetworkInfo(
129177e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                    ConnectivityManager.TYPE_MOBILE_DUN);
129277e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                            if (info.isConnected()) {
1293924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                                if (DEBUG) Log.d(TAG, "setting dun ifacename =" + iface);
129477e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                // even if we're already connected - it may be somebody else's
129577e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                // refcount, so add our own
129677e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                turnOnMobileConnection();
129777e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                            } else {
129877e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                // verify the iface is not the default mobile - can't use that!
129977e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                info = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
130077e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                if (info.isConnected()) {
130177e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                    iface = null; // can't accept this one
130277e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                }
130377e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                            }
130477e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                        } else {
1305924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                            if (DEBUG) Log.d(TAG, "checking if hipri brought us this connection");
13066a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            NetworkInfo info = cm.getNetworkInfo(
13076a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                    ConnectivityManager.TYPE_MOBILE_HIPRI);
13086a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            if (info.isConnected()) {
1309924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                                if (DEBUG) Log.d(TAG, "yes - hipri in use");
131077e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                // even if we're already connected - it may be sombody else's
131177e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                                // refcount, so add our own
13126a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                                turnOnMobileConnection();
13132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            }
13142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
131577e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                    } catch (RemoteException e) {
131677e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                        Log.e(TAG, "RemoteException calling ConnectivityManager " + e);
131777e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                        iface = null;
13186a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    }
13196a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
132077e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                // may have been set to null in the if above
132177e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                if (iface == null ) {
13221039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    boolean success = false;
132377e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                    if (tryCell == TRY_TO_SETUP_MOBILE_CONNECTION) {
13241039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        success = turnOnMobileConnection();
13251039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    }
13261039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                    if (!success) {
13271039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        // wait for things to settle and retry
13281039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        sendMessageDelayed(CMD_RETRY_UPSTREAM, UPSTREAM_SETTLE_TIME_MS);
132977e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt                    }
13306a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                }
1331b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                notifyTetheredOfNewUpstreamIface(iface);
13326a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            }
1333b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt            protected void notifyTetheredOfNewUpstreamIface(String ifaceName) {
1334924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "notifying tethered with iface =" + ifaceName);
1335b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                mUpstreamIfaceName = ifaceName;
13366a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                for (Object o : mNotifyList) {
13376a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    TetherInterfaceSM sm = (TetherInterfaceSM)o;
13386a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED,
13396a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            ifaceName);
13402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
13412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
13432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
13446a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt        class InitialState extends TetherMasterUtilState {
13452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
13471039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                mMobileReserved = false;
13482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
1351924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "MasterInitialState.processMessage what=" + message.what);
13522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
13532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
13542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
135577b32ddbbeeb13a07b16f23af705567c75a8e3dfRobert Greenwalt                        mDunRequired = isDunRequired();
13562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
1357924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                        if (DEBUG) Log.d(TAG, "Tether Mode requested by " + who.toString());
13582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        mNotifyList.add(who);
13596a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        transitionTo(mTetherModeAliveState);
13602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_UNREQUESTED:
13622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        who = (TetherInterfaceSM)message.obj;
1363924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                        if (DEBUG) Log.d(TAG, "Tether Mode unrequested by " + who.toString());
13642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        int index = mNotifyList.indexOf(who);
13652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (index != -1) {
13666a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            mNotifyList.remove(who);
13672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
13682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
13702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        retValue = false;
13712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
13722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
13732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
13742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
13762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
1377d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt        class TetherModeAliveState extends TetherMasterUtilState {
137819b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt            boolean mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE;
13792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
138119b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt                mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE; // better try something first pass
138219b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt                                                        // or crazy tests cases will fail
13836a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                chooseUpstreamType(mTryCell);
13846a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                mTryCell = !mTryCell;
13856a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                turnOnMasterTetherSettings(); // may transition us out
13866a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            }
13876a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            @Override
13886a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt            public void exit() {
13896a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                turnOffMobileConnection();
1390b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                notifyTetheredOfNewUpstreamIface(null);
13912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
13922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
13932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
1394924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                if (DEBUG) Log.d(TAG, "TetherModeAliveState.processMessage what=" + message.what);
13952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
13962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
13972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
13982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
13992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        mNotifyList.add(who);
1400b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                        who.sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED,
1401b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt                                mUpstreamIfaceName);
14022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
14032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_UNREQUESTED:
14042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        who = (TetherInterfaceSM)message.obj;
14052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        int index = mNotifyList.indexOf(who);
14062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        if (index != -1) {
14072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            mNotifyList.remove(index);
14082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            if (mNotifyList.isEmpty()) {
1409d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt                                turnOffMasterTetherSettings(); // transitions appropriately
14102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                            }
14112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        }
14122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
14136a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_UPSTREAM_CHANGED:
141405d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                        // need to try DUN immediately if Wifi goes down
141505d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt                        mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE;
14166a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        chooseUpstreamType(mTryCell);
14176a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        mTryCell = !mTryCell;
14182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
14196a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    case CMD_CELL_CONNECTION_RENEW:
14206a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        // make sure we're still using a requested connection - may have found
14216a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        // wifi or something since then.
14221039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                        if (mCurrentConnectionSequence == message.arg1) {
1423924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                            if (DEBUG) {
1424924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                                Log.d(TAG, "renewing mobile connection - requeuing for another " +
1425924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                                        CELL_CONNECTION_RENEW_MS + "ms");
1426924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt                            }
14271039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt                            mMobileReserved = false; // need to renew it
14286a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                            turnOnMobileConnection();
14296a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        }
143065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt                        break;
14316a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                   case CMD_RETRY_UPSTREAM:
14326a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                       chooseUpstreamType(mTryCell);
14336a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                       mTryCell = !mTryCell;
14346a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                       break;
14356a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                   default:
14362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                       retValue = false;
14372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                       break;
14382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
14392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
14402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class ErrorState extends HierarchicalState {
14442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            int mErrorNotification;
14452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public boolean processMessage(Message message) {
14472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                boolean retValue = true;
14482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                switch (message.what) {
14492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    case CMD_TETHER_MODE_REQUESTED:
14502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        TetherInterfaceSM who = (TetherInterfaceSM)message.obj;
14516a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                        who.sendMessage(mErrorNotification);
14522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        break;
14532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    default:
14542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                       retValue = false;
14552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
14562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                return retValue;
14572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            void notify(int msgType) {
14592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                mErrorNotification = msgType;
14602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                for (Object o : mNotifyList) {
14612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    TetherInterfaceSM sm = (TetherInterfaceSM)o;
14626a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt                    sm.sendMessage(msgType);
14632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                }
14642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetIpForwardingEnabledErrorState extends ErrorState {
14682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setIpForwardingEnabled");
14712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_IP_FORWARDING_ENABLE_ERROR);
14722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetIpForwardingDisabledErrorState extends ErrorState {
14762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setIpForwardingDisabled");
14792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_IP_FORWARDING_DISABLE_ERROR);
14802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class StartTetheringErrorState extends ErrorState {
14842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
14862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in startTethering");
14872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_START_TETHERING_ERROR);
14882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
14892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                INetworkManagementService service =
14902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        INetworkManagementService.Stub.asInterface(b);
14912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
14922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    service.setIpForwardingEnabled(false);
14932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
14942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
14952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
14962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
14972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class StopTetheringErrorState extends ErrorState {
14982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
14992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
15002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in stopTethering");
15012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_STOP_TETHERING_ERROR);
15022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
15032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                INetworkManagementService service =
15042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                         INetworkManagementService.Stub.asInterface(b);
15052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
15062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    service.setIpForwardingEnabled(false);
15072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
15082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
15092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
15102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
15112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        class SetDnsForwardersErrorState extends ErrorState {
15122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            @Override
15132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            public void enter() {
15142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                Log.e(TAG, "Error in setDnsForwarders");
15152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                notify(TetherInterfaceSM.CMD_SET_DNS_FORWARDERS_ERROR);
15162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                IBinder b = ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE);
15172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                INetworkManagementService service =
15182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                        INetworkManagementService.Stub.asInterface(b);
15192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
15202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    service.stopTethering();
15212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
15222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                try {
15232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    service.setIpForwardingEnabled(false);
15242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                } catch (Exception e) {}
15252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
1526d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
1527d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
1528d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt
15292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt    public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
15302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        if (mContext.checkCallingOrSelfPermission(
15312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) {
15322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            pw.println("Permission Denial: can't dump ConnectivityService.Tether " +
15332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    "from from pid=" + Binder.getCallingPid() + ", uid=" +
15342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    Binder.getCallingUid());
15352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                    return;
15362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        }
15372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt
15382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        pw.println();
15392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        pw.println("Tether state:");
15402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        synchronized (mIfaces) {
15412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            for (Object o : mIfaces.values()) {
15422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt                pw.println(" "+o.toString());
15432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt            }
1544d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt        }
15452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        pw.println();
15462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt        return;
1547d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt    }
1548d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt}
1549