Tethering.java revision 6c2260b3af3a9243aa7f04cf03106f31817cb4c1
1d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt/* 2d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * Copyright (C) 2010 The Android Open Source Project 3d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * 4d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * Licensed under the Apache License, Version 2.0 (the "License"); 5d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * you may not use this file except in compliance with the License. 6d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * You may obtain a copy of the License at 7d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * 8d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * http://www.apache.org/licenses/LICENSE-2.0 9d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * 10d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * Unless required by applicable law or agreed to in writing, software 11d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * distributed under the License is distributed on an "AS IS" BASIS, 12d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * See the License for the specific language governing permissions and 14d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * limitations under the License. 15d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt */ 16d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 17d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltpackage com.android.server.connectivity; 18d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 19d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.app.Notification; 20d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.app.NotificationManager; 21d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.app.PendingIntent; 226fdd0c6274c81b337ad35b70480f881daf7354c3Danica Changimport android.bluetooth.BluetoothPan; 23d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.content.BroadcastReceiver; 24d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.content.Context; 25d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.content.Intent; 26d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.content.IntentFilter; 272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.content.pm.PackageManager; 28d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.content.res.Resources; 29c4308f01c965571dc2354107c3574df113e397eeMike Lockwoodimport android.hardware.usb.UsbManager; 30d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.net.ConnectivityManager; 3165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwaltimport android.net.InterfaceConfiguration; 322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.net.IConnectivityManager; 33d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.net.INetworkManagementEventObserver; 34ed1264093234b1f1354cd5f669eb82a50bb56869Robert Greenwaltimport android.net.LinkAddress; 3505d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwaltimport android.net.LinkProperties; 362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.net.NetworkInfo; 37e590373ea71251cfffc8f22f011e2e6335dce716Robert Greenwaltimport android.net.NetworkUtils; 382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.os.Binder; 396eef5c687677b5965f769c62f50edda25abb431eMike Lockwoodimport android.os.Handler; 407eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwaltimport android.os.HandlerThread; 41d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.os.IBinder; 42d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.os.INetworkManagementService; 43dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwaltimport android.os.Looper; 442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport android.os.Message; 45d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.os.RemoteException; 46d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.os.ServiceManager; 47d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.provider.Settings; 48d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport android.util.Log; 49d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport com.android.internal.telephony.Phone; 5164c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Savilleimport com.android.internal.util.IState; 5264c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Savilleimport com.android.internal.util.State; 5364c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Savilleimport com.android.internal.util.StateMachine; 542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.io.FileDescriptor; 562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.io.PrintWriter; 5704808c294027f8bc318643a94c85a999257d7f52Robert Greenwaltimport java.net.InetAddress; 58d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltimport java.util.ArrayList; 59ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwaltimport java.util.Collection; 602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.util.HashMap; 61ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwaltimport java.util.Iterator; 626fdd0c6274c81b337ad35b70480f881daf7354c3Danica Changimport java.util.LinkedList; 632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwaltimport java.util.Set; 64d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt/** 65d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt * @hide 662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt * 672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt * Timeout 682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt * 692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt * TODO - look for parent classes and code sharing 70d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt */ 7165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt 72d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwaltpublic class Tethering extends INetworkManagementEventObserver.Stub { 73d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 74d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt private Context mContext; 75924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt private final static String TAG = "Tethering"; 761039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt private final static boolean DEBUG = true; 77d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // TODO - remove both of these - should be part of interface inspection/selection stuff 792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private String[] mTetherableUsbRegexs; 802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private String[] mTetherableWifiRegexs; 816fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang private String[] mTetherableBluetoothRegexs; 82ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt private Collection<Integer> mUpstreamIfaceTypes; 83ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt 84ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt private static final Integer MOBILE_TYPE = new Integer(ConnectivityManager.TYPE_MOBILE); 85ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt private static final Integer HIPRI_TYPE = new Integer(ConnectivityManager.TYPE_MOBILE_HIPRI); 86ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt private static final Integer DUN_TYPE = new Integer(ConnectivityManager.TYPE_MOBILE_DUN); 87ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt 88ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt // if we have to connect to mobile, what APN type should we use? Calculated by examining the 89ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt // upstream type list and the DUN_REQUIRED secure-setting 90ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt private int mPreferredUpstreamMobileApn = ConnectivityManager.TYPE_NONE; 912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 92c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh private INetworkManagementService mNMService; 937eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt private Looper mLooper; 947eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt private HandlerThread mThread; 95dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt 96030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt private HashMap<String, TetherInterfaceSM> mIfaces; // all tethered/tetherable ifaces 97d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 98d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt private BroadcastReceiver mStateReceiver; 99d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 100bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt private static final String USB_NEAR_IFACE_ADDR = "192.168.42.129"; 101b979f79158f9c470fa09ff3b96d72db274262201Robert Greenwalt private static final int USB_PREFIX_LENGTH = 24; 102bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt 103707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh // USB is 192.168.42.1 and 255.255.255.0 104707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh // Wifi is 192.168.43.1 and 255.255.255.0 105707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh // BT is limited to max default of 5 connections. 192.168.44.1 to 192.168.48.1 106707952ecd4b6cae25aabcf51f94d702a65847e9eJaikumar Ganesh // with 255.255.255.0 107d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt 1082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private String[] mDhcpRange; 109c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh private static final String[] DHCP_DEFAULT_RANGE = { 110c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh "192.168.42.2", "192.168.42.254", "192.168.43.2", "192.168.43.254", 111c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh "192.168.44.2", "192.168.44.254", "192.168.45.2", "192.168.45.254", 112c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh "192.168.46.2", "192.168.46.254", "192.168.47.2", "192.168.47.254", 113c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh "192.168.48.2", "192.168.48.254", 114c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh }; 1152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 1162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private String[] mDnsServers; 117d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt private static final String DNS_DEFAULT_SERVER1 = "8.8.8.8"; 118df255c2283b2bcf2d5e70d095a4d2333c1cb80a3Robert Greenwalt private static final String DNS_DEFAULT_SERVER2 = "8.8.4.4"; 1192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 12064c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private StateMachine mTetherMasterSM; 121a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 122a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt private Notification mTetheredNotification; 1232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 1246c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood private boolean mRndisEnabled; // track the RNDIS function enabled state 1256c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood private boolean mUsbTetherRequested; // true if USB tethering should be started 1266c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood // when RNDIS is enabled 127030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt 128c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh public Tethering(Context context, INetworkManagementService nmService, Looper looper) { 129d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt mContext = context; 130c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService = nmService; 131dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt mLooper = looper; 132d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 1332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mIfaces = new HashMap<String, TetherInterfaceSM>(); 134d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 1357eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt // make our own thread so we don't anr the system 1367eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt mThread = new HandlerThread("Tethering"); 1377eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt mThread.start(); 1387eae25021d3aa71c7616ea766f9a4d5af3b1772bRobert Greenwalt mLooper = mThread.getLooper(); 139dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt mTetherMasterSM = new TetherMasterSM("TetherMaster", mLooper); 1402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mTetherMasterSM.start(); 1412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 142bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt mStateReceiver = new StateReceiver(); 143d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt IntentFilter filter = new IntentFilter(); 144770126a678ccc9328a89407ffc82f4d998b25427Mike Lockwood filter.addAction(UsbManager.ACTION_USB_STATE); 1452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); 146bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt mContext.registerReceiver(mStateReceiver, filter); 147bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt 148bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt filter = new IntentFilter(); 149030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt filter.addAction(Intent.ACTION_MEDIA_SHARED); 150030f5e18dfa71673708deb75e8aa1f196bebbd99Robert Greenwalt filter.addAction(Intent.ACTION_MEDIA_UNSHARED); 151bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt filter.addDataScheme("file"); 152bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt mContext.registerReceiver(mStateReceiver, filter); 153bb51d9feea260e1527628b878319c0cf76adadebRobert Greenwalt 1542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mDhcpRange = context.getResources().getStringArray( 1552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt com.android.internal.R.array.config_tether_dhcp_range); 156bfb7bfa53847832db2a3eb05e5eff7cb974c3c7aRobert Greenwalt if ((mDhcpRange.length == 0) || (mDhcpRange.length % 2 ==1)) { 157c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mDhcpRange = DHCP_DEFAULT_RANGE; 1582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 1592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 1602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mTetherableUsbRegexs = context.getResources().getStringArray( 1612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt com.android.internal.R.array.config_tether_usb_regexs); 1622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mTetherableWifiRegexs = context.getResources().getStringArray( 1632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt com.android.internal.R.array.config_tether_wifi_regexs); 1646fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang mTetherableBluetoothRegexs = context.getResources().getStringArray( 1656fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang com.android.internal.R.array.config_tether_bluetooth_regexs); 166ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt int ifaceTypes[] = context.getResources().getIntArray( 167ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt com.android.internal.R.array.config_tether_upstream_types); 168ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt mUpstreamIfaceTypes = new ArrayList(); 169ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt for (int i : ifaceTypes) { 170ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt mUpstreamIfaceTypes.add(new Integer(i)); 171ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 172ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt 173ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt // check if the upstream type list needs to be modified due to secure-settings 174ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt checkDunRequired(); 1752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 1762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // TODO - remove and rely on real notifications of the current iface 1772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mDnsServers = new String[2]; 178d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt mDnsServers[0] = DNS_DEFAULT_SERVER1; 179d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt mDnsServers[1] = DNS_DEFAULT_SERVER2; 180d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 181d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 1826143f5f7392fb0d3e5702a46a2415bd0ecb6efb4Mike J. Chen public void interfaceStatusChanged(String iface, boolean up) { 1836143f5f7392fb0d3e5702a46a2415bd0ecb6efb4Mike J. Chen if (DEBUG) Log.d(TAG, "interfaceStatusChanged " + iface + ", " + up); 1842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt boolean found = false; 18565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt boolean usb = false; 186a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt if (isWifi(iface)) { 187a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt found = true; 188a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } else if (isUsb(iface)) { 189a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt found = true; 190a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt usb = true; 1916fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang } else if (isBluetooth(iface)) { 1926fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang found = true; 1932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 1942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (found == false) return; 1952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 1962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt synchronized (mIfaces) { 1972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM sm = mIfaces.get(iface); 1986143f5f7392fb0d3e5702a46a2415bd0ecb6efb4Mike J. Chen if (up) { 1992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (sm == null) { 200dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt sm = new TetherInterfaceSM(iface, mLooper, usb); 2012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mIfaces.put(iface, sm); 2022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt sm.start(); 2032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 2042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } else { 2052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (sm != null) { 2066a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt sm.sendMessage(TetherInterfaceSM.CMD_INTERFACE_DOWN); 2072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mIfaces.remove(iface); 2082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 2092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 2102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 211d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 212d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 213f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen public void interfaceLinkStateChanged(String iface, boolean up) { 214f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen } 215f59c7d0f2ac8d489b6d8118543a57ea4a603eacfMike J. Chen 216a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt private boolean isUsb(String iface) { 217a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt for (String regex : mTetherableUsbRegexs) { 218a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt if (iface.matches(regex)) return true; 219a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 220a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt return false; 221a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 222a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 223a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt public boolean isWifi(String iface) { 224a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt for (String regex : mTetherableWifiRegexs) { 225a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt if (iface.matches(regex)) return true; 226a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 227a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt return false; 228a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 229a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 2306fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang public boolean isBluetooth(String iface) { 2316fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang for (String regex : mTetherableBluetoothRegexs) { 2326fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang if (iface.matches(regex)) return true; 2336fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang } 2346fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang return false; 2356fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang } 2366c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood 2372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void interfaceAdded(String iface) { 2382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt boolean found = false; 23965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt boolean usb = false; 240a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt if (isWifi(iface)) { 241a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt found = true; 2422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 243a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt if (isUsb(iface)) { 244a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt found = true; 245a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt usb = true; 246d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 2476fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang if (isBluetooth(iface)) { 2486fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang found = true; 2496fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang } 2502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (found == false) { 251924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt if (DEBUG) Log.d(TAG, iface + " is not a tetherable iface, ignoring"); 252d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt return; 253d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 25465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt 2552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt synchronized (mIfaces) { 2562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM sm = mIfaces.get(iface); 2572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (sm != null) { 258924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt if (DEBUG) Log.d(TAG, "active iface (" + iface + ") reported as added, ignoring"); 2592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return; 2602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 261dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt sm = new TetherInterfaceSM(iface, mLooper, usb); 2622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mIfaces.put(iface, sm); 2632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt sm.start(); 2642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 265924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt if (DEBUG) Log.d(TAG, "interfaceAdded :" + iface); 266d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 267d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 2682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void interfaceRemoved(String iface) { 2692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt synchronized (mIfaces) { 2702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM sm = mIfaces.get(iface); 2712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (sm == null) { 272924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt if (DEBUG) { 273924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt Log.e(TAG, "attempting to remove unknown iface (" + iface + "), ignoring"); 274924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt } 2752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return; 2762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 2776a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt sm.sendMessage(TetherInterfaceSM.CMD_INTERFACE_DOWN); 2782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mIfaces.remove(iface); 279d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 280d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 281d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 28212b933d0d9252decaae9fee2456bb1e1cd94c085JP Abgrall public void limitReached(String limitName, String iface) {} 28312b933d0d9252decaae9fee2456bb1e1cd94c085JP Abgrall 2845a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt public int tether(String iface) { 285d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt Log.d(TAG, "Tethering " + iface); 2862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM sm = null; 2872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt synchronized (mIfaces) { 2882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt sm = mIfaces.get(iface); 289d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 2902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (sm == null) { 2912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Log.e(TAG, "Tried to Tether an unknown iface :" + iface + ", ignoring"); 2925a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE; 293d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 2945a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt if (!sm.isAvailable() && !sm.isErrored()) { 2952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Log.e(TAG, "Tried to Tether an unavailable iface :" + iface + ", ignoring"); 2965a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE; 297d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 2986a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt sm.sendMessage(TetherInterfaceSM.CMD_TETHER_REQUESTED); 2995a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt return ConnectivityManager.TETHER_ERROR_NO_ERROR; 300d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 301d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 3025a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt public int untether(String iface) { 303d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt Log.d(TAG, "Untethering " + iface); 3042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM sm = null; 3052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt synchronized (mIfaces) { 3062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt sm = mIfaces.get(iface); 3072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 3082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (sm == null) { 3092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Log.e(TAG, "Tried to Untether an unknown iface :" + iface + ", ignoring"); 3105a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE; 311d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 3122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (sm.isErrored()) { 3132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Log.e(TAG, "Tried to Untethered an errored iface :" + iface + ", ignoring"); 3145a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt return ConnectivityManager.TETHER_ERROR_UNAVAIL_IFACE; 315d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 3166a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt sm.sendMessage(TetherInterfaceSM.CMD_TETHER_UNREQUESTED); 3175a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt return ConnectivityManager.TETHER_ERROR_NO_ERROR; 3185a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 3195a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt 3205a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt public int getLastTetherError(String iface) { 3215a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt TetherInterfaceSM sm = null; 3225a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt synchronized (mIfaces) { 3235a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt sm = mIfaces.get(iface); 3245a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 3255a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt if (sm == null) { 3265a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt Log.e(TAG, "Tried to getLastTetherError on an unknown iface :" + iface + ", ignoring"); 3275a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt return ConnectivityManager.TETHER_ERROR_UNKNOWN_IFACE; 3285a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 3295a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt return sm.getLastError(); 3302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 331d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 3322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private void sendTetherStateChangedBroadcast() { 3332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE); 334c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b); 335d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt try { 336c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh if (!cm.isTetheringSupported()) return; 3372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } catch (RemoteException e) { 3382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return; 339d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 340d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 3412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt ArrayList<String> availableList = new ArrayList<String>(); 3422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt ArrayList<String> activeList = new ArrayList<String>(); 3432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt ArrayList<String> erroredList = new ArrayList<String>(); 3442a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 345a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt boolean wifiTethered = false; 346a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt boolean usbTethered = false; 3476fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang boolean bluetoothTethered = false; 348a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 3492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt synchronized (mIfaces) { 3502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Set ifaces = mIfaces.keySet(); 3512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt for (Object iface : ifaces) { 3522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM sm = mIfaces.get(iface); 3532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (sm != null) { 3546fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang if (sm.isErrored()) { 3552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt erroredList.add((String)iface); 3562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } else if (sm.isAvailable()) { 3572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt availableList.add((String)iface); 3582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } else if (sm.isTethered()) { 359a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt if (isUsb((String)iface)) { 360a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt usbTethered = true; 361a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } else if (isWifi((String)iface)) { 362a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt wifiTethered = true; 3636fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang } else if (isBluetooth((String)iface)) { 3646fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang bluetoothTethered = true; 365a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 3662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt activeList.add((String)iface); 3672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 3682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 369d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 370d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 371d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt Intent broadcast = new Intent(ConnectivityManager.ACTION_TETHER_STATE_CHANGED); 372d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt broadcast.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING); 3732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_AVAILABLE_TETHER, 3742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt availableList); 3752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ACTIVE_TETHER, activeList); 3762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt broadcast.putStringArrayListExtra(ConnectivityManager.EXTRA_ERRORED_TETHER, 3772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt erroredList); 3782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mContext.sendStickyBroadcast(broadcast); 379924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt if (DEBUG) { 380924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt Log.d(TAG, "sendTetherStateChangedBroadcast " + availableList.size() + ", " + 381924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt activeList.size() + ", " + erroredList.size()); 382924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt } 383a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 384a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt if (usbTethered) { 3856fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang if (wifiTethered || bluetoothTethered) { 386a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general); 387a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } else { 388a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_usb); 389a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 390a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } else if (wifiTethered) { 3916fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang if (bluetoothTethered) { 3926fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_general); 3936fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang } else { 3946fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_wifi); 3956fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang } 3966fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang } else if (bluetoothTethered) { 3976fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang showTetheredNotification(com.android.internal.R.drawable.stat_sys_tether_bluetooth); 398a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } else { 399a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt clearTetheredNotification(); 400a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 401a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 402a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 403a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt private void showTetheredNotification(int icon) { 404a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt NotificationManager notificationManager = 405a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE); 406a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt if (notificationManager == null) { 407a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt return; 408a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 409a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 410db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt if (mTetheredNotification != null) { 411db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt if (mTetheredNotification.icon == icon) { 412db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt return; 413db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt } 414db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt notificationManager.cancel(mTetheredNotification.icon); 415db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt } 416db3fe9edd4cb638d3dd20b23456f6cdb0a414ed1Robert Greenwalt 417a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt Intent intent = new Intent(); 418a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt intent.setClassName("com.android.settings", "com.android.settings.TetherSettings"); 419a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 420a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 421a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt PendingIntent pi = PendingIntent.getActivity(mContext, 0, intent, 0); 422a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 423a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt Resources r = Resources.getSystem(); 424a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt CharSequence title = r.getText(com.android.internal.R.string.tethered_notification_title); 425a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt CharSequence message = r.getText(com.android.internal.R.string. 426a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt tethered_notification_message); 427a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 4286fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang if (mTetheredNotification == null) { 429a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt mTetheredNotification = new Notification(); 430a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt mTetheredNotification.when = 0; 431a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 432a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt mTetheredNotification.icon = icon; 433a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt mTetheredNotification.defaults &= ~Notification.DEFAULT_SOUND; 434a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt mTetheredNotification.flags = Notification.FLAG_ONGOING_EVENT; 435a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt mTetheredNotification.tickerText = title; 436a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt mTetheredNotification.setLatestEventInfo(mContext, title, message, pi); 437a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 438a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt notificationManager.notify(mTetheredNotification.icon, mTetheredNotification); 439a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 440a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt 441a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt private void clearTetheredNotification() { 442a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt NotificationManager notificationManager = 443a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt (NotificationManager)mContext.getSystemService(Context.NOTIFICATION_SERVICE); 444a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt if (notificationManager != null && mTetheredNotification != null) { 445a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt notificationManager.cancel(mTetheredNotification.icon); 446a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt mTetheredNotification = null; 447a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt } 4485a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 4495a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt 4502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private class StateReceiver extends BroadcastReceiver { 451d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt public void onReceive(Context content, Intent intent) { 4522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt String action = intent.getAction(); 453770126a678ccc9328a89407ffc82f4d998b25427Mike Lockwood if (action.equals(UsbManager.ACTION_USB_STATE)) { 4546c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood synchronized (Tethering.this) { 4556c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood boolean usbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false); 4566c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood mRndisEnabled = intent.getBooleanExtra(UsbManager.USB_FUNCTION_RNDIS, false); 4576c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood // start tethering if we have a request pending 4586c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood if (usbConnected && mRndisEnabled && mUsbTetherRequested) { 4596c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood tetherUsb(true); 4606c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } 4616c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood mUsbTetherRequested = false; 4626c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } 4632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } else if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { 464924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt if (DEBUG) Log.d(TAG, "Tethering got CONNECTIVITY_ACTION"); 4656a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED); 466d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 467d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 468d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 469d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 4706c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood private void tetherUsb(boolean enable) { 4716c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood if (DEBUG) Log.d(TAG, "tetherUsb " + enable); 4723c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood 47365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt String[] ifaces = new String[0]; 47465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt try { 475c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh ifaces = mNMService.listInterfaces(); 47665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } catch (Exception e) { 4773c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood Log.e(TAG, "Error listing Interfaces", e); 47865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt return; 47965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 48065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt for (String iface : ifaces) { 481a599fe7c0d8b4d4bdf4accb099cb17bebcb9088bRobert Greenwalt if (isUsb(iface)) { 4826c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood int result = (enable ? tether(iface) : untether(iface)); 4836c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood if (result == ConnectivityManager.TETHER_ERROR_NO_ERROR) { 4846c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood return; 48565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 48665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 48765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 4886c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood Log.e(TAG, "unable start or stop USB tethering"); 48965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 49065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt 49165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt // configured when we start tethering and unconfig'd on error or conclusion 492d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt private boolean configureUsbIface(boolean enabled) { 493924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt if (DEBUG) Log.d(TAG, "configureUsbIface(" + enabled + ")"); 49465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt 4956c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood // toggle the USB interfaces 4966c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood String[] ifaces = new String[0]; 4973c2a2f6789ee257e3838ffb0f3f117e08825ad5fMike Lockwood try { 4986c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood ifaces = mNMService.listInterfaces(); 4996c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } catch (Exception e) { 5006c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood Log.e(TAG, "Error listing Interfaces", e); 5016c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood return false; 5026c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } 5036c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood for (String iface : ifaces) { 5046c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood if (isUsb(iface)) { 5056c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood InterfaceConfiguration ifcg = null; 5066c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood try { 5076c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood ifcg = mNMService.getInterfaceConfig(iface); 5086c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood if (ifcg != null) { 5096c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood InetAddress addr = NetworkUtils.numericToInetAddress(USB_NEAR_IFACE_ADDR); 5106c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood ifcg.addr = new LinkAddress(addr, USB_PREFIX_LENGTH); 5116c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood if (enabled) { 5126c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood ifcg.interfaceFlags = ifcg.interfaceFlags.replace("down", "up"); 5136c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } else { 5146c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood ifcg.interfaceFlags = ifcg.interfaceFlags.replace("up", "down"); 51565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 5166c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood ifcg.interfaceFlags = ifcg.interfaceFlags.replace("running", ""); 5176c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood ifcg.interfaceFlags = ifcg.interfaceFlags.replace(" "," "); 5186c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood mNMService.setInterfaceConfig(iface, ifcg); 51965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 5206c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } catch (Exception e) { 5216c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood Log.e(TAG, "Error configuring interface " + iface, e); 5226c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood return false; 52365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 52465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 5256c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } 52665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt 52765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt return true; 52865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 52965ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt 5302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public String[] getTetherableUsbRegexs() { 5312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return mTetherableUsbRegexs; 5322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 5332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 5342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public String[] getTetherableWifiRegexs() { 5352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return mTetherableWifiRegexs; 5362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 5372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 5386fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang public String[] getTetherableBluetoothRegexs() { 5396fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang return mTetherableBluetoothRegexs; 5406fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang } 5416fdd0c6274c81b337ad35b70480f881daf7354c3Danica Chang 5426c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood public int setUsbTethering(boolean enable) { 5436c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood UsbManager usbManager = (UsbManager)mContext.getSystemService(Context.USB_SERVICE); 5446c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood 5456c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood synchronized (this) { 5466c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood if (enable) { 5476c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood if (mRndisEnabled) { 5486c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood tetherUsb(true); 5496c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } else { 5506c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood mUsbTetherRequested = true; 5516c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood usbManager.setCurrentFunction(UsbManager.USB_FUNCTION_RNDIS, false); 5526c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } 5536c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } else { 5546c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood tetherUsb(false); 5556c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood if (mRndisEnabled) { 5566c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood usbManager.setCurrentFunction(null, false); 5576c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } 5586c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood mUsbTetherRequested = false; 5596c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } 5606c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } 5616c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood return ConnectivityManager.TETHER_ERROR_NO_ERROR; 5626c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood } 5636c2260b3af3a9243aa7f04cf03106f31817cb4c1Mike Lockwood 564ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt public int[] getUpstreamIfaceTypes() { 565ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt int values[] = new int[mUpstreamIfaceTypes.size()]; 566ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt Iterator<Integer> iterator = mUpstreamIfaceTypes.iterator(); 567ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt for (int i=0; i < mUpstreamIfaceTypes.size(); i++) { 568ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt values[i] = iterator.next(); 569ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 570ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt return values; 571c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt } 572c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt 573ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt public void checkDunRequired() { 574ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt int requiredApn = ((Settings.Secure.getInt(mContext.getContentResolver(), 575ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt Settings.Secure.TETHER_DUN_REQUIRED, 0) == 1) ? 576ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt ConnectivityManager.TYPE_MOBILE_DUN : 577ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt ConnectivityManager.TYPE_MOBILE_HIPRI); 578ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (mPreferredUpstreamMobileApn != requiredApn) { 579ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (requiredApn == ConnectivityManager.TYPE_MOBILE_DUN) { 580ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt while (mUpstreamIfaceTypes.contains(MOBILE_TYPE)) { 581ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt mUpstreamIfaceTypes.remove(MOBILE_TYPE); 582ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 583ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt while (mUpstreamIfaceTypes.contains(HIPRI_TYPE)) { 584ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt mUpstreamIfaceTypes.remove(HIPRI_TYPE); 585ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 586ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (mUpstreamIfaceTypes.contains(DUN_TYPE) == false) { 587ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt mUpstreamIfaceTypes.add(DUN_TYPE); 588ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 589ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } else { 590ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt while (mUpstreamIfaceTypes.contains(DUN_TYPE)) { 591ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt mUpstreamIfaceTypes.remove(DUN_TYPE); 592ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 593ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (mUpstreamIfaceTypes.contains(MOBILE_TYPE) == false) { 594ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt mUpstreamIfaceTypes.add(MOBILE_TYPE); 595ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 596ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (mUpstreamIfaceTypes.contains(HIPRI_TYPE) == false) { 597ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt mUpstreamIfaceTypes.add(HIPRI_TYPE); 598ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 599ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 600ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt mPreferredUpstreamMobileApn = requiredApn; 601ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 602c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt } 603c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt 6042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public String[] getTetheredIfaces() { 6052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt ArrayList<String> list = new ArrayList<String>(); 6062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt synchronized (mIfaces) { 6072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Set keys = mIfaces.keySet(); 6082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt for (Object key : keys) { 6092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM sm = mIfaces.get(key); 6102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (sm.isTethered()) { 6112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt list.add((String)key); 6122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 6132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 6142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 6152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt String[] retVal = new String[list.size()]; 6162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt for (int i=0; i < list.size(); i++) { 6172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt retVal[i] = list.get(i); 6182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 6192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return retVal; 6202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 6212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 6222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public String[] getTetherableIfaces() { 6232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt ArrayList<String> list = new ArrayList<String>(); 6242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt synchronized (mIfaces) { 6252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Set keys = mIfaces.keySet(); 6262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt for (Object key : keys) { 6272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM sm = mIfaces.get(key); 6282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (sm.isAvailable()) { 6292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt list.add((String)key); 6302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 6312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 6322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 6332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt String[] retVal = new String[list.size()]; 6342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt for (int i=0; i < list.size(); i++) { 6352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt retVal[i] = list.get(i); 6362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 6372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return retVal; 6382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 6392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 6405a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt public String[] getErroredIfaces() { 6415a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt ArrayList<String> list = new ArrayList<String>(); 6425a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt synchronized (mIfaces) { 6435a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt Set keys = mIfaces.keySet(); 6445a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt for (Object key : keys) { 6455a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt TetherInterfaceSM sm = mIfaces.get(key); 6465a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt if (sm.isErrored()) { 6475a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt list.add((String)key); 6485a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 6495a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 6505a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 6515a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt String[] retVal = new String[list.size()]; 6525a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt for (int i= 0; i< list.size(); i++) { 6535a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt retVal[i] = list.get(i); 6545a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 6555a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt return retVal; 6565a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 6575a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt 6581ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo //TODO: Temporary handling upstream change triggered without 6591ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo // CONNECTIVITY_ACTION. Only to accomodate interface 6601ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo // switch during HO. 6611ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo // @see bug/4455071 6621ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo public void handleTetherIfaceChange() { 6631ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo mTetherMasterSM.sendMessage(TetherMasterSM.CMD_UPSTREAM_CHANGED); 6641ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo } 6651ccb10e25d5af8ef1d997b0ed2291dbbcaacf44eKazuhiro Ondo 66664c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville class TetherInterfaceSM extends StateMachine { 6672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // notification from the master SM that it's not in tether mode 6686a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_TETHER_MODE_DEAD = 1; 6692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // request from the user that it wants to tether 6706a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_TETHER_REQUESTED = 2; 6712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // request from the user that it wants to untether 6726a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_TETHER_UNREQUESTED = 3; 6732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // notification that this interface is down 6746a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_INTERFACE_DOWN = 4; 6752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // notification that this interface is up 6766a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_INTERFACE_UP = 5; 6772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // notification from the master SM that it had an error turning on cellular dun 6786a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_CELL_DUN_ERROR = 6; 6792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // notification from the master SM that it had trouble enabling IP Forwarding 6806a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_IP_FORWARDING_ENABLE_ERROR = 7; 6812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // notification from the master SM that it had trouble disabling IP Forwarding 6826a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_IP_FORWARDING_DISABLE_ERROR = 8; 6832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // notification from the master SM that it had trouble staring tethering 6846a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_START_TETHERING_ERROR = 9; 6852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // notification from the master SM that it had trouble stopping tethering 6866a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_STOP_TETHERING_ERROR = 10; 6872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // notification from the master SM that it had trouble setting the DNS forwarders 6886a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_SET_DNS_FORWARDERS_ERROR = 11; 6896a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt // the upstream connection has changed 690951749ba2e014566553d17c512e947472951a060Wink Saville static final int CMD_TETHER_CONNECTION_CHANGED = 12; 6912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 69264c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mDefaultState; 6932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 69464c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mInitialState; 69564c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mStartingState; 69664c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mTetheredState; 6972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 69864c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mUnavailableState; 6992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 7002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private boolean mAvailable; 7012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private boolean mTethered; 7025a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt int mLastError; 7032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 7042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt String mIfaceName; 7056a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt String mMyUpstreamIfaceName; // may change over time 7066a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt 70765ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt boolean mUsb; 7082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 709dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt TetherInterfaceSM(String name, Looper looper, boolean usb) { 710dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt super(name, looper); 7112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mIfaceName = name; 71265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt mUsb = usb; 7135a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR); 7142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 7152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mInitialState = new InitialState(); 7162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt addState(mInitialState); 7172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mStartingState = new StartingState(); 7182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt addState(mStartingState); 7192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mTetheredState = new TetheredState(); 7202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt addState(mTetheredState); 7212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mUnavailableState = new UnavailableState(); 7222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt addState(mUnavailableState); 7232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 7242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt setInitialState(mInitialState); 7252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 7262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 7272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public String toString() { 7282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt String res = new String(); 7292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt res += mIfaceName + " - "; 73064c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville IState current = getCurrentState(); 7312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (current == mInitialState) res += "InitialState"; 7322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (current == mStartingState) res += "StartingState"; 7332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (current == mTetheredState) res += "TetheredState"; 7342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (current == mUnavailableState) res += "UnavailableState"; 7352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (mAvailable) res += " - Available"; 7362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (mTethered) res += " - Tethered"; 7375a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt res += " - lastError =" + mLastError; 7382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return res; 7392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 7402a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 7415a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt public synchronized int getLastError() { 7425a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt return mLastError; 7435a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 7445a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt 7455a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt private synchronized void setLastError(int error) { 7465a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt mLastError = error; 7475a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt 7485a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt if (isErrored()) { 7495a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt if (mUsb) { 7505a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt // note everything's been unwound by this point so nothing to do on 7515a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt // further error.. 7525a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt Tethering.this.configureUsbIface(false); 7535a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 7545a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 7555a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt } 7565a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt 7572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // synchronized between this getter and the following setter 7582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public synchronized boolean isAvailable() { 7592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return mAvailable; 7602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 7612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 7622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private synchronized void setAvailable(boolean available) { 7632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mAvailable = available; 7642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 7652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 7662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // synchronized between this getter and the following setter 7672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public synchronized boolean isTethered() { 7682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return mTethered; 7692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 7702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 7712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private synchronized void setTethered(boolean tethered) { 7722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mTethered = tethered; 7732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 7742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 7752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // synchronized between this getter and the following setter 7762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public synchronized boolean isErrored() { 7775a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt return (mLastError != ConnectivityManager.TETHER_ERROR_NO_ERROR); 7782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 7792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 78064c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville class InitialState extends State { 7812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 7822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void enter() { 7832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt setAvailable(true); 7842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt setTethered(false); 7852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt sendTetherStateChangedBroadcast(); 7862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 7872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 7882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 7892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public boolean processMessage(Message message) { 790924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt if (DEBUG) Log.d(TAG, "InitialState.processMessage what=" + message.what); 7912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt boolean retValue = true; 7922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt switch (message.what) { 7932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_TETHER_REQUESTED: 7945a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR); 7956a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_REQUESTED, 7966a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt TetherInterfaceSM.this); 7972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt transitionTo(mStartingState); 7982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 7992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_INTERFACE_DOWN: 8002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt transitionTo(mUnavailableState); 8012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 8022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt default: 8032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt retValue = false; 8042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 8052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 8062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return retValue; 8072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 8082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 8092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 81064c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville class StartingState extends State { 8112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 8122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void enter() { 8132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt setAvailable(false); 81465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt if (mUsb) { 815d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt if (!Tethering.this.configureUsbIface(true)) { 8166a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED, 8176a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt TetherInterfaceSM.this); 8185a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastError(ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR); 8195a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt 820951749ba2e014566553d17c512e947472951a060Wink Saville transitionTo(mInitialState); 82165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt return; 82265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 82365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 8242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt sendTetherStateChangedBroadcast(); 8256a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt 826951749ba2e014566553d17c512e947472951a060Wink Saville // Skipping StartingState 827951749ba2e014566553d17c512e947472951a060Wink Saville transitionTo(mTetheredState); 8282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 8292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 8302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public boolean processMessage(Message message) { 831924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt if (DEBUG) Log.d(TAG, "StartingState.processMessage what=" + message.what); 8322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt boolean retValue = true; 8332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt switch (message.what) { 8342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // maybe a parent class? 8352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_TETHER_UNREQUESTED: 8366a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED, 8376a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt TetherInterfaceSM.this); 83865ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt if (mUsb) { 839d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt if (!Tethering.this.configureUsbIface(false)) { 8405a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastErrorAndTransitionToInitialState( 8415a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR); 84265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt break; 84365ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 84465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 8452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt transitionTo(mInitialState); 8462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 8472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_CELL_DUN_ERROR: 8482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_IP_FORWARDING_ENABLE_ERROR: 8492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_IP_FORWARDING_DISABLE_ERROR: 8502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_START_TETHERING_ERROR: 8512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_STOP_TETHERING_ERROR: 8522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_SET_DNS_FORWARDERS_ERROR: 8535a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastErrorAndTransitionToInitialState( 8545a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt ConnectivityManager.TETHER_ERROR_MASTER_ERROR); 8552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 8562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_INTERFACE_DOWN: 8576a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED, 8586a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt TetherInterfaceSM.this); 8592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt transitionTo(mUnavailableState); 8602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 8612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt default: 8622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt retValue = false; 8632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 8642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return retValue; 8652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 8662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 8672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 86864c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville class TetheredState extends State { 8692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 8702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void enter() { 8712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt try { 872c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.tetherInterface(mIfaceName); 8732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } catch (Exception e) { 8745a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastError(ConnectivityManager.TETHER_ERROR_TETHER_IFACE_ERROR); 8755a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt 876951749ba2e014566553d17c512e947472951a060Wink Saville transitionTo(mInitialState); 8772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return; 8782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 879924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt if (DEBUG) Log.d(TAG, "Tethered " + mIfaceName); 8802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt setAvailable(false); 8812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt setTethered(true); 8822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt sendTetherStateChangedBroadcast(); 8832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 8842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 8852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public boolean processMessage(Message message) { 886924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt if (DEBUG) Log.d(TAG, "TetheredState.processMessage what=" + message.what); 8872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt boolean retValue = true; 8882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt boolean error = false; 8892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt switch (message.what) { 8902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_TETHER_UNREQUESTED: 8912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_INTERFACE_DOWN: 8926a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt if (mMyUpstreamIfaceName != null) { 8935a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt try { 894c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName); 895b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt mMyUpstreamIfaceName = null; 8966a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } catch (Exception e) { 8976a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt try { 898c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.untetherInterface(mIfaceName); 8996a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } catch (Exception ee) {} 9005a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt 9016a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt setLastErrorAndTransitionToInitialState( 9026a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR); 9036a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt break; 9046a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 9052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 9062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt try { 907c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.untetherInterface(mIfaceName); 9082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } catch (Exception e) { 9095a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastErrorAndTransitionToInitialState( 9105a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR); 9112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 9122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 9136a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt mTetherMasterSM.sendMessage(TetherMasterSM.CMD_TETHER_MODE_UNREQUESTED, 9146a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt TetherInterfaceSM.this); 9152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (message.what == CMD_TETHER_UNREQUESTED) { 91665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt if (mUsb) { 917d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt if (!Tethering.this.configureUsbIface(false)) { 9185a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastError( 9195a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR); 92065ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 92165ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 9225a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt transitionTo(mInitialState); 9232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } else if (message.what == CMD_INTERFACE_DOWN) { 9242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt transitionTo(mUnavailableState); 9252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 926924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt if (DEBUG) Log.d(TAG, "Untethered " + mIfaceName); 9272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 9286a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt case CMD_TETHER_CONNECTION_CHANGED: 9296a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt String newUpstreamIfaceName = (String)(message.obj); 9301039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt if ((mMyUpstreamIfaceName == null && newUpstreamIfaceName == null) || 9311039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt (mMyUpstreamIfaceName != null && 9321039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt mMyUpstreamIfaceName.equals(newUpstreamIfaceName))) { 9331039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt if (DEBUG) Log.d(TAG, "Connection changed noop - dropping"); 9341039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt break; 9351039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt } 9366a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt if (mMyUpstreamIfaceName != null) { 9376a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt try { 938c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName); 939b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt mMyUpstreamIfaceName = null; 9406a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } catch (Exception e) { 9416a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt try { 942c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.untetherInterface(mIfaceName); 9436a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } catch (Exception ee) {} 9446a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt 9456a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt setLastErrorAndTransitionToInitialState( 9466a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR); 9476a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt break; 9486a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 9496a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 9506a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt if (newUpstreamIfaceName != null) { 9516a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt try { 952c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.enableNat(mIfaceName, newUpstreamIfaceName); 9536a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } catch (Exception e) { 9546a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt try { 955c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.untetherInterface(mIfaceName); 9566a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } catch (Exception ee) {} 9576a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt 9586a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt setLastError(ConnectivityManager.TETHER_ERROR_ENABLE_NAT_ERROR); 9596a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt transitionTo(mInitialState); 9606a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt return true; 9616a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 9626a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 9636a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt mMyUpstreamIfaceName = newUpstreamIfaceName; 9646a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt break; 9652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_CELL_DUN_ERROR: 9662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_IP_FORWARDING_ENABLE_ERROR: 9672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_IP_FORWARDING_DISABLE_ERROR: 9682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_START_TETHERING_ERROR: 9692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_STOP_TETHERING_ERROR: 9702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_SET_DNS_FORWARDERS_ERROR: 9712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt error = true; 9722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // fall through 9732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_TETHER_MODE_DEAD: 9746a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt if (mMyUpstreamIfaceName != null) { 9755a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt try { 976c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.disableNat(mIfaceName, mMyUpstreamIfaceName); 977b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt mMyUpstreamIfaceName = null; 9786a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } catch (Exception e) { 9796a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt try { 980c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.untetherInterface(mIfaceName); 9816a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } catch (Exception ee) {} 9825a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt 9836a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt setLastErrorAndTransitionToInitialState( 9846a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt ConnectivityManager.TETHER_ERROR_DISABLE_NAT_ERROR); 9856a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt break; 9866a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 9872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 9882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt try { 989c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.untetherInterface(mIfaceName); 9902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } catch (Exception e) { 9915a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastErrorAndTransitionToInitialState( 9925a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt ConnectivityManager.TETHER_ERROR_UNTETHER_IFACE_ERROR); 9932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 9942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 9952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (error) { 9965a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastErrorAndTransitionToInitialState( 9975a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt ConnectivityManager.TETHER_ERROR_MASTER_ERROR); 9982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 9992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 1000924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt if (DEBUG) Log.d(TAG, "Tether lost upstream connection " + mIfaceName); 10012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt sendTetherStateChangedBroadcast(); 100265ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt if (mUsb) { 1003d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt if (!Tethering.this.configureUsbIface(false)) { 10045a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastError(ConnectivityManager.TETHER_ERROR_IFACE_CFG_ERROR); 100565ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 100665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt } 10072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt transitionTo(mInitialState); 10082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 10092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt default: 10102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt retValue = false; 10112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 10122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 10132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return retValue; 10142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 10152a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 10162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 101764c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville class UnavailableState extends State { 10182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 10192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void enter() { 10202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt setAvailable(false); 10215a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastError(ConnectivityManager.TETHER_ERROR_NO_ERROR); 10222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt setTethered(false); 10232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt sendTetherStateChangedBroadcast(); 10242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 10252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 10262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public boolean processMessage(Message message) { 10272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt boolean retValue = true; 10282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt switch (message.what) { 10292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_INTERFACE_UP: 10302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt transitionTo(mInitialState); 10312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 10322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt default: 10332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt retValue = false; 10342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 10352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 10362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return retValue; 10372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 10382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 10392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 10405a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt void setLastErrorAndTransitionToInitialState(int error) { 10415a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt setLastError(error); 10425a73506cdd466f2b96686ced3ff0f7ca224d1143Robert Greenwalt transitionTo(mInitialState); 10432a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 104465ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt 10452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 10462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 104764c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville class TetherMasterSM extends StateMachine { 10482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // an interface SM has requested Tethering 10492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt static final int CMD_TETHER_MODE_REQUESTED = 1; 10502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // an interface SM has unrequested Tethering 10512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt static final int CMD_TETHER_MODE_UNREQUESTED = 2; 10526a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt // upstream connection change - do the right thing 10536a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_UPSTREAM_CHANGED = 3; 10542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // we received notice that the cellular DUN connection is up 10556a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt static final int CMD_CELL_CONNECTION_RENEW = 4; 10566a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt // we don't have a valid upstream conn, check again after a delay 1057951749ba2e014566553d17c512e947472951a060Wink Saville static final int CMD_RETRY_UPSTREAM = 5; 10582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 10592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // This indicates what a timeout event relates to. A state that 10602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // sends itself a delayed timeout event and handles incoming timeout events 10612a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // should inc this when it is entered and whenever it sends a new timeout event. 10622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt // We do not flush the old ones. 10632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private int mSequenceNumber; 10642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 106564c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mInitialState; 106664c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mTetherModeAliveState; 10672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 106864c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mSetIpForwardingEnabledErrorState; 106964c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mSetIpForwardingDisabledErrorState; 107064c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mStartTetheringErrorState; 107164c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mStopTetheringErrorState; 107264c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville private State mSetDnsForwardersErrorState; 10732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 10742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt private ArrayList mNotifyList; 10752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 10761039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt private int mCurrentConnectionSequence; 1077ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt private int mMobileApnReserved = ConnectivityManager.TYPE_NONE; 10782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 1079b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt private String mUpstreamIfaceName = null; 1080b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt 10816a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt private static final int UPSTREAM_SETTLE_TIME_MS = 10000; 10826a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt private static final int CELL_CONNECTION_RENEW_MS = 40000; 10832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 1084dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt TetherMasterSM(String name, Looper looper) { 1085dfadaeac088cabce854d8f476405cd412f82593aRobert Greenwalt super(name, looper); 10862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 10872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt //Add states 10882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mInitialState = new InitialState(); 10892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt addState(mInitialState); 10902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mTetherModeAliveState = new TetherModeAliveState(); 10912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt addState(mTetherModeAliveState); 10922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 10932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mSetIpForwardingEnabledErrorState = new SetIpForwardingEnabledErrorState(); 10942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt addState(mSetIpForwardingEnabledErrorState); 10952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mSetIpForwardingDisabledErrorState = new SetIpForwardingDisabledErrorState(); 10962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt addState(mSetIpForwardingDisabledErrorState); 10972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mStartTetheringErrorState = new StartTetheringErrorState(); 10982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt addState(mStartTetheringErrorState); 10992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mStopTetheringErrorState = new StopTetheringErrorState(); 11002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt addState(mStopTetheringErrorState); 11012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mSetDnsForwardersErrorState = new SetDnsForwardersErrorState(); 11022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt addState(mSetDnsForwardersErrorState); 11032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 11042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mNotifyList = new ArrayList(); 11052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt setInitialState(mInitialState); 11062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 11072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 110864c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville class TetherMasterUtilState extends State { 11096a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt protected final static boolean TRY_TO_SETUP_MOBILE_CONNECTION = true; 11106a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt protected final static boolean WAIT_FOR_NETWORK_TO_SETTLE = false; 11116a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt 1112d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt @Override 1113d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt public boolean processMessage(Message m) { 1114d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt return false; 1115d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 1116ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt protected String enableString(int apnType) { 1117ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt switch (apnType) { 1118ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt case ConnectivityManager.TYPE_MOBILE_DUN: 1119ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt return Phone.FEATURE_ENABLE_DUN_ALWAYS; 1120ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt case ConnectivityManager.TYPE_MOBILE: 1121ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt case ConnectivityManager.TYPE_MOBILE_HIPRI: 1122ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt return Phone.FEATURE_ENABLE_HIPRI; 1123ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 1124ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt return null; 1125ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } 1126ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt protected boolean turnOnUpstreamMobileConnection(int apnType) { 11271039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt boolean retValue = true; 1128ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (apnType == ConnectivityManager.TYPE_NONE) return false; 1129ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (apnType != mMobileApnReserved) turnOffUpstreamMobileConnection(); 1130d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE); 1131c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b); 11321039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt int result = Phone.APN_REQUEST_FAILED; 1133ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt String enableString = enableString(apnType); 1134ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (enableString == null) return false; 1135d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt try { 1136c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh result = cm.startUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, 1137ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt enableString, new Binder()); 1138d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } catch (Exception e) { 1139d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 11401039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt switch (result) { 11416a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt case Phone.APN_ALREADY_ACTIVE: 11426a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt case Phone.APN_REQUEST_STARTED: 1143ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt mMobileApnReserved = apnType; 11441039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt Message m = obtainMessage(CMD_CELL_CONNECTION_RENEW); 11451039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt m.arg1 = ++mCurrentConnectionSequence; 11461039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt sendMessageDelayed(m, CELL_CONNECTION_RENEW_MS); 11476a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt break; 11486a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt case Phone.APN_REQUEST_FAILED: 11496a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt default: 11501039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt retValue = false; 11516a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt break; 11526a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 11536a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt 1154d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt return retValue; 1155d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 1156ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt protected boolean turnOffUpstreamMobileConnection() { 1157ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (mMobileApnReserved != ConnectivityManager.TYPE_NONE) { 11586a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE); 1159c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b); 11606a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt try { 1161c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh cm.stopUsingNetworkFeature(ConnectivityManager.TYPE_MOBILE, 1162ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt enableString(mMobileApnReserved)); 11636a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } catch (Exception e) { 11646a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt return false; 11656a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 1166ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt mMobileApnReserved = ConnectivityManager.TYPE_NONE; 1167d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 1168d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt return true; 1169d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 11706a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt protected boolean turnOnMasterTetherSettings() { 1171d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt try { 1172c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.setIpForwardingEnabled(true); 1173d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } catch (Exception e) { 1174951749ba2e014566553d17c512e947472951a060Wink Saville transitionTo(mSetIpForwardingEnabledErrorState); 1175d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt return false; 1176d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 1177d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt try { 1178c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.startTethering(mDhcpRange); 1179d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } catch (Exception e) { 1180547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt try { 1181c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.stopTethering(); 1182c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.startTethering(mDhcpRange); 1183547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt } catch (Exception ee) { 1184547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt transitionTo(mStartTetheringErrorState); 1185547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt return false; 1186547e4e757b539e19694ff6e35b3d4a856f965e5bRobert Greenwalt } 1187d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 1188d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt try { 1189c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.setDnsForwarders(mDnsServers); 1190d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } catch (Exception e) { 1191951749ba2e014566553d17c512e947472951a060Wink Saville transitionTo(mSetDnsForwardersErrorState); 1192d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt return false; 1193d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 1194d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt return true; 1195d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 11966a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt protected boolean turnOffMasterTetherSettings() { 1197d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt try { 1198c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.stopTethering(); 1199d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } catch (Exception e) { 1200d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt transitionTo(mStopTetheringErrorState); 1201d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt return false; 1202d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 1203d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt try { 1204c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.setIpForwardingEnabled(false); 1205d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } catch (Exception e) { 1206d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt transitionTo(mSetIpForwardingDisabledErrorState); 1207d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt return false; 1208d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 1209d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt transitionTo(mInitialState); 1210d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt return true; 1211d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt } 1212ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt 1213ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt protected void chooseUpstreamType(boolean tryCell) { 121405d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt IBinder b = ServiceManager.getService(Context.CONNECTIVITY_SERVICE); 121505d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt IConnectivityManager cm = IConnectivityManager.Stub.asInterface(b); 1216ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt int upType = ConnectivityManager.TYPE_NONE; 1217ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt String iface = null; 121805d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt 1219ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt for (Integer netType : mUpstreamIfaceTypes) { 1220ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt NetworkInfo info = null; 1221ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt try { 1222ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt info = cm.getNetworkInfo(netType.intValue()); 1223ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } catch (RemoteException e) { } 1224ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if ((info != null) && info.isConnected()) { 1225ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt upType = netType.intValue(); 1226ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt break; 1227c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt } 1228c9d5fb7c95f158d9a31d534895373afcfad77806Robert Greenwalt } 12291039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt 1230924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt if (DEBUG) { 1231ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt Log.d(TAG, "chooseUpstreamType(" + tryCell + "), preferredApn =" 1232ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt + mPreferredUpstreamMobileApn + ", got type=" + upType); 1233924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt } 1234ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt 1235ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt // if we're on DUN, put our own grab on it 1236ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (upType == ConnectivityManager.TYPE_MOBILE_DUN || 1237ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt upType == ConnectivityManager.TYPE_MOBILE_HIPRI) { 1238ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt turnOnUpstreamMobileConnection(upType); 12396a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 1240ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt 1241ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (upType == ConnectivityManager.TYPE_NONE) { 1242ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt boolean tryAgainLater = true; 1243ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if ((tryCell == TRY_TO_SETUP_MOBILE_CONNECTION) && 1244ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt (turnOnUpstreamMobileConnection(mPreferredUpstreamMobileApn) == true)) { 1245ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt // we think mobile should be coming up - don't set a retry 1246ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt tryAgainLater = false; 12471039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt } 1248ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (tryAgainLater) { 12491039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt sendMessageDelayed(CMD_RETRY_UPSTREAM, UPSTREAM_SETTLE_TIME_MS); 125077e638dee8b8b0f9c0fcb5403f8c6fb97b52826fRobert Greenwalt } 1251ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } else { 1252ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt LinkProperties linkProperties = null; 1253ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt try { 1254ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt linkProperties = cm.getLinkProperties(upType); 1255ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt } catch (RemoteException e) { } 1256ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt if (linkProperties != null) iface = linkProperties.getInterfaceName(); 12576a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 1258b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt notifyTetheredOfNewUpstreamIface(iface); 12596a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 1260ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt 1261b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt protected void notifyTetheredOfNewUpstreamIface(String ifaceName) { 1262924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt if (DEBUG) Log.d(TAG, "notifying tethered with iface =" + ifaceName); 1263b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt mUpstreamIfaceName = ifaceName; 12646a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt for (Object o : mNotifyList) { 12656a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt TetherInterfaceSM sm = (TetherInterfaceSM)o; 12666a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt sm.sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED, 12676a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt ifaceName); 12682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 12692a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 12702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 12712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 12726a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt class InitialState extends TetherMasterUtilState { 12732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 12742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void enter() { 12752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 12762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 12772a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public boolean processMessage(Message message) { 1278924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt if (DEBUG) Log.d(TAG, "MasterInitialState.processMessage what=" + message.what); 12792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt boolean retValue = true; 12802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt switch (message.what) { 12812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_TETHER_MODE_REQUESTED: 1282ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt checkDunRequired(); 12832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM who = (TetherInterfaceSM)message.obj; 1284924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt if (DEBUG) Log.d(TAG, "Tether Mode requested by " + who.toString()); 12852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mNotifyList.add(who); 12866a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt transitionTo(mTetherModeAliveState); 12872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 12882a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_TETHER_MODE_UNREQUESTED: 12892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt who = (TetherInterfaceSM)message.obj; 1290924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt if (DEBUG) Log.d(TAG, "Tether Mode unrequested by " + who.toString()); 12912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt int index = mNotifyList.indexOf(who); 12922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (index != -1) { 12936a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt mNotifyList.remove(who); 12942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 12952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 12962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt default: 12972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt retValue = false; 12982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 12992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return retValue; 13012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 1304d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt class TetherModeAliveState extends TetherMasterUtilState { 130519b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt boolean mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE; 13062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 13072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void enter() { 130819b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE; // better try something first pass 130919b9ab46a9faee6031aaf6c78748401943fea117Robert Greenwalt // or crazy tests cases will fail 13106a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt chooseUpstreamType(mTryCell); 13116a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt mTryCell = !mTryCell; 13126a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt turnOnMasterTetherSettings(); // may transition us out 13136a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 13146a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt @Override 13156a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt public void exit() { 1316ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt turnOffUpstreamMobileConnection(); 1317b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt notifyTetheredOfNewUpstreamIface(null); 13182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 13202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public boolean processMessage(Message message) { 1321924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt if (DEBUG) Log.d(TAG, "TetherModeAliveState.processMessage what=" + message.what); 13222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt boolean retValue = true; 13232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt switch (message.what) { 13242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_TETHER_MODE_REQUESTED: 13252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM who = (TetherInterfaceSM)message.obj; 13262a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mNotifyList.add(who); 1327b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt who.sendMessage(TetherInterfaceSM.CMD_TETHER_CONNECTION_CHANGED, 1328b37f0c6513241f79a6ea0e3c162dc540d028b60cRobert Greenwalt mUpstreamIfaceName); 13292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 13302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_TETHER_MODE_UNREQUESTED: 13312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt who = (TetherInterfaceSM)message.obj; 13322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt int index = mNotifyList.indexOf(who); 13332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (index != -1) { 13342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mNotifyList.remove(index); 13352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (mNotifyList.isEmpty()) { 1336d70a3d4e2d356aab4cac313f1f9d8272a5e3e8d0Robert Greenwalt turnOffMasterTetherSettings(); // transitions appropriately 13372a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 13406a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt case CMD_UPSTREAM_CHANGED: 134105d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt // need to try DUN immediately if Wifi goes down 134205d8936f275762bab0d66c6337e352388c8eeee0Robert Greenwalt mTryCell = !WAIT_FOR_NETWORK_TO_SETTLE; 13436a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt chooseUpstreamType(mTryCell); 13446a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt mTryCell = !mTryCell; 13452a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 13466a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt case CMD_CELL_CONNECTION_RENEW: 13476a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt // make sure we're still using a requested connection - may have found 13486a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt // wifi or something since then. 13491039872f597995cab2f88b9c841f8c2b8e71c451Robert Greenwalt if (mCurrentConnectionSequence == message.arg1) { 1350924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt if (DEBUG) { 1351924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt Log.d(TAG, "renewing mobile connection - requeuing for another " + 1352924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt CELL_CONNECTION_RENEW_MS + "ms"); 1353924cc94caaab5e9ad0714e0877e871290316e2b1Robert Greenwalt } 1354ccf83af1017b7b1eaf12f6ec3d2cb2137a7cdda9Robert Greenwalt turnOnUpstreamMobileConnection(mMobileApnReserved); 13556a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt } 135665ae29bd852ff468ad003af241d5177fe016c74aRobert Greenwalt break; 135701758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo case CMD_RETRY_UPSTREAM: 135801758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo chooseUpstreamType(mTryCell); 135901758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo mTryCell = !mTryCell; 136001758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo break; 136101758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo default: 136201758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo retValue = false; 136301758e81b3ad89934581885bb2fc7006510ec639Kazuhiro Ondo break; 13642a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13652a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return retValue; 13662a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13672a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13682a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 136964c42cae4482fe0157e977b8ddd0f2c2436b3f31Wink Saville class ErrorState extends State { 13702a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt int mErrorNotification; 13712a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 13722a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public boolean processMessage(Message message) { 13732a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt boolean retValue = true; 13742a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt switch (message.what) { 13752a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt case CMD_TETHER_MODE_REQUESTED: 13762a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM who = (TetherInterfaceSM)message.obj; 13776a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt who.sendMessage(mErrorNotification); 13782a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt break; 13792a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt default: 13802a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt retValue = false; 13812a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13822a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return retValue; 13832a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13842a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt void notify(int msgType) { 13852a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt mErrorNotification = msgType; 13862a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt for (Object o : mNotifyList) { 13872a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt TetherInterfaceSM sm = (TetherInterfaceSM)o; 13886a1967c9088b9f05665462efa9d98210143500bdRobert Greenwalt sm.sendMessage(msgType); 13892a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13902a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13912a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 13922a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13932a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt class SetIpForwardingEnabledErrorState extends ErrorState { 13942a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 13952a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void enter() { 13962a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Log.e(TAG, "Error in setIpForwardingEnabled"); 13972a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt notify(TetherInterfaceSM.CMD_IP_FORWARDING_ENABLE_ERROR); 13982a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 13992a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14002a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 14012a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt class SetIpForwardingDisabledErrorState extends ErrorState { 14022a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 14032a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void enter() { 14042a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Log.e(TAG, "Error in setIpForwardingDisabled"); 14052a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt notify(TetherInterfaceSM.CMD_IP_FORWARDING_DISABLE_ERROR); 14062a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14072a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14082a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 14092a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt class StartTetheringErrorState extends ErrorState { 14102a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 14112a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void enter() { 14122a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Log.e(TAG, "Error in startTethering"); 14132a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt notify(TetherInterfaceSM.CMD_START_TETHERING_ERROR); 14142a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt try { 1415c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.setIpForwardingEnabled(false); 14162a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } catch (Exception e) {} 14172a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14182a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14192a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 14202a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt class StopTetheringErrorState extends ErrorState { 14212a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 14222a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void enter() { 14232a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Log.e(TAG, "Error in stopTethering"); 14242a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt notify(TetherInterfaceSM.CMD_STOP_TETHERING_ERROR); 14252a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt try { 1426c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.setIpForwardingEnabled(false); 14272a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } catch (Exception e) {} 14282a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14292a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14302a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 14312a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt class SetDnsForwardersErrorState extends ErrorState { 14322a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt @Override 14332a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void enter() { 14342a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Log.e(TAG, "Error in setDnsForwarders"); 14352a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt notify(TetherInterfaceSM.CMD_SET_DNS_FORWARDERS_ERROR); 14362a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt try { 1437c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.stopTethering(); 14382a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } catch (Exception e) {} 14392a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt try { 1440c9338305bf4d5cfb9ec2a1d64144d8cef32a00a5Chia-chi Yeh mNMService.setIpForwardingEnabled(false); 14412a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } catch (Exception e) {} 14422a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 1443d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 1444d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 1445d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt 14462a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt public void dump(FileDescriptor fd, PrintWriter pw, String[] args) { 14472a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt if (mContext.checkCallingOrSelfPermission( 14482a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt android.Manifest.permission.DUMP) != PackageManager.PERMISSION_GRANTED) { 14492a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt pw.println("Permission Denial: can't dump ConnectivityService.Tether " + 14502a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt "from from pid=" + Binder.getCallingPid() + ", uid=" + 14512a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt Binder.getCallingUid()); 14522a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return; 14532a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 14542a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt 14552a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt pw.println(); 14562a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt pw.println("Tether state:"); 14572a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt synchronized (mIfaces) { 14582a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt for (Object o : mIfaces.values()) { 14592a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt pw.println(" "+o.toString()); 14602a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt } 1461d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 14622a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt pw.println(); 14632a091d7aa0c174986387e5d56bf97a87fe075bdbRobert Greenwalt return; 1464d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt } 1465d0e18ffb82b59d38aeaf0e552f48e734202719abRobert Greenwalt} 1466