From 69a09577179cc96cd90f724ad330ddde13f9eed6 Mon Sep 17 00:00:00 2001 From: Mike Lockwood Date: Tue, 19 Jul 2011 13:30:03 -0700 Subject: [PATCH] Fix USB tethering The previous approach no longer works with the new USB drivers, since the usb0 interface is no longer enabled by default. This introduced a chicken & egg problem - usb0 will not be enabled until the user tries to start tethering, but Settings will not enable the checkbox unless usb0 is enabled. To fix this we add an explicit call to start USB tethering in the connectivity manager. This will enable RNDIS if necessary and then bring up tethering once usb0 is enabled. Change-Id: If3972c89e05377af00b7a71cc2588e44bd4cfaed Signed-off-by: Mike Lockwood --- src/com/android/settings/TetherSettings.java | 66 ++++++++------------ 1 file changed, 27 insertions(+), 39 deletions(-) diff --git a/src/com/android/settings/TetherSettings.java b/src/com/android/settings/TetherSettings.java index 1513d43cf91..99917250661 100644 --- a/src/com/android/settings/TetherSettings.java +++ b/src/com/android/settings/TetherSettings.java @@ -31,6 +31,7 @@ import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.content.res.AssetManager; +import android.hardware.usb.UsbManager; import android.net.ConnectivityManager; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiManager; @@ -95,6 +96,9 @@ public class TetherSettings extends SettingsPreferenceFragment private WifiManager mWifiManager; private WifiConfiguration mWifiConfig = null; + private boolean mUsbConnected; + private boolean mMassStorageActive; + private boolean mBluetoothEnableForTether; @Override @@ -253,8 +257,14 @@ public class TetherSettings extends SettingsPreferenceFragment updateState(available.toArray(new String[available.size()]), active.toArray(new String[active.size()]), errored.toArray(new String[errored.size()])); - } else if (action.equals(Intent.ACTION_MEDIA_SHARED) || - action.equals(Intent.ACTION_MEDIA_UNSHARED)) { + } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) { + mMassStorageActive = true; + updateState(); + } else if (action.equals(Intent.ACTION_MEDIA_UNSHARED)) { + mMassStorageActive = false; + updateState(); + } else if (action.equals(UsbManager.ACTION_USB_STATE)) { + mUsbConnected = intent.getBooleanExtra(UsbManager.USB_CONNECTED, false); updateState(); } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { if (mBluetoothEnableForTether) { @@ -285,10 +295,15 @@ public class TetherSettings extends SettingsPreferenceFragment final Activity activity = getActivity(); + mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState()); mTetherChangeReceiver = new TetherChangeReceiver(); IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED); Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter); + filter = new IntentFilter(); + filter.addAction(UsbManager.ACTION_USB_STATE); + activity.registerReceiver(mTetherChangeReceiver, filter); + filter = new IntentFilter(); filter.addAction(Intent.ACTION_MEDIA_SHARED); filter.addAction(Intent.ACTION_MEDIA_UNSHARED); @@ -334,14 +349,11 @@ public class TetherSettings extends SettingsPreferenceFragment String[] errored) { ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); - boolean usbAvailable = false; + boolean usbAvailable = mUsbConnected && !mMassStorageActive; int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR; - boolean massStorageActive = - Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState()); for (String s : available) { for (String regex : mUsbRegexs) { if (s.matches(regex)) { - usbAvailable = true; if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) { usbError = cm.getLastTetherError(s); } @@ -377,7 +389,7 @@ public class TetherSettings extends SettingsPreferenceFragment mUsbTether.setSummary(R.string.usb_tethering_errored_subtext); mUsbTether.setEnabled(false); mUsbTether.setChecked(false); - } else if (massStorageActive) { + } else if (mMassStorageActive) { mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext); mUsbTether.setEnabled(false); mUsbTether.setChecked(false); @@ -434,40 +446,18 @@ public class TetherSettings extends SettingsPreferenceFragment @Override public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) { + ConnectivityManager cm = + (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); + if (preference == mUsbTether) { boolean newState = mUsbTether.isChecked(); - ConnectivityManager cm = - (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); - - if (newState) { - String[] available = cm.getTetherableIfaces(); - - String usbIface = findIface(available, mUsbRegexs); - if (usbIface == null) { - updateState(); - return true; - } - if (cm.tether(usbIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) { - mUsbTether.setChecked(false); - mUsbTether.setSummary(R.string.usb_tethering_errored_subtext); - return true; - } - mUsbTether.setSummary(""); - } else { - String [] tethered = cm.getTetheredIfaces(); - - String usbIface = findIface(tethered, mUsbRegexs); - if (usbIface == null) { - updateState(); - return true; - } - if (cm.untether(usbIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) { - mUsbTether.setSummary(R.string.usb_tethering_errored_subtext); - return true; - } - mUsbTether.setSummary(""); + if (cm.setUsbTethering(newState) != ConnectivityManager.TETHER_ERROR_NO_ERROR) { + mUsbTether.setChecked(false); + mUsbTether.setSummary(R.string.usb_tethering_errored_subtext); + return true; } + mUsbTether.setSummary(""); } else if (preference == mBluetoothTether) { boolean bluetoothTetherState = mBluetoothTether.isChecked(); @@ -486,8 +476,6 @@ public class TetherSettings extends SettingsPreferenceFragment } else { boolean errored = false; - ConnectivityManager cm = - (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); String [] tethered = cm.getTetheredIfaces(); String bluetoothIface = findIface(tethered, mBluetoothRegexs); if (bluetoothIface != null &&