Merge "Fix USB tethering"

This commit is contained in:
Mike Lockwood
2011-07-23 19:36:53 -07:00
committed by Android (Google) Code Review

View File

@@ -31,6 +31,7 @@ import android.content.DialogInterface;
import android.content.Intent; import android.content.Intent;
import android.content.IntentFilter; import android.content.IntentFilter;
import android.content.res.AssetManager; import android.content.res.AssetManager;
import android.hardware.usb.UsbManager;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
@@ -95,6 +96,9 @@ public class TetherSettings extends SettingsPreferenceFragment
private WifiManager mWifiManager; private WifiManager mWifiManager;
private WifiConfiguration mWifiConfig = null; private WifiConfiguration mWifiConfig = null;
private boolean mUsbConnected;
private boolean mMassStorageActive;
private boolean mBluetoothEnableForTether; private boolean mBluetoothEnableForTether;
@Override @Override
@@ -253,8 +257,14 @@ public class TetherSettings extends SettingsPreferenceFragment
updateState(available.toArray(new String[available.size()]), updateState(available.toArray(new String[available.size()]),
active.toArray(new String[active.size()]), active.toArray(new String[active.size()]),
errored.toArray(new String[errored.size()])); errored.toArray(new String[errored.size()]));
} else if (action.equals(Intent.ACTION_MEDIA_SHARED) || } else if (action.equals(Intent.ACTION_MEDIA_SHARED)) {
action.equals(Intent.ACTION_MEDIA_UNSHARED)) { 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(); updateState();
} else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) { } else if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
if (mBluetoothEnableForTether) { if (mBluetoothEnableForTether) {
@@ -285,10 +295,15 @@ public class TetherSettings extends SettingsPreferenceFragment
final Activity activity = getActivity(); final Activity activity = getActivity();
mMassStorageActive = Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
mTetherChangeReceiver = new TetherChangeReceiver(); mTetherChangeReceiver = new TetherChangeReceiver();
IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED); IntentFilter filter = new IntentFilter(ConnectivityManager.ACTION_TETHER_STATE_CHANGED);
Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter); Intent intent = activity.registerReceiver(mTetherChangeReceiver, filter);
filter = new IntentFilter();
filter.addAction(UsbManager.ACTION_USB_STATE);
activity.registerReceiver(mTetherChangeReceiver, filter);
filter = new IntentFilter(); filter = new IntentFilter();
filter.addAction(Intent.ACTION_MEDIA_SHARED); filter.addAction(Intent.ACTION_MEDIA_SHARED);
filter.addAction(Intent.ACTION_MEDIA_UNSHARED); filter.addAction(Intent.ACTION_MEDIA_UNSHARED);
@@ -334,14 +349,11 @@ public class TetherSettings extends SettingsPreferenceFragment
String[] errored) { String[] errored) {
ConnectivityManager cm = ConnectivityManager cm =
(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
boolean usbAvailable = false; boolean usbAvailable = mUsbConnected && !mMassStorageActive;
int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR; int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
boolean massStorageActive =
Environment.MEDIA_SHARED.equals(Environment.getExternalStorageState());
for (String s : available) { for (String s : available) {
for (String regex : mUsbRegexs) { for (String regex : mUsbRegexs) {
if (s.matches(regex)) { if (s.matches(regex)) {
usbAvailable = true;
if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) { if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
usbError = cm.getLastTetherError(s); usbError = cm.getLastTetherError(s);
} }
@@ -377,7 +389,7 @@ public class TetherSettings extends SettingsPreferenceFragment
mUsbTether.setSummary(R.string.usb_tethering_errored_subtext); mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
mUsbTether.setEnabled(false); mUsbTether.setEnabled(false);
mUsbTether.setChecked(false); mUsbTether.setChecked(false);
} else if (massStorageActive) { } else if (mMassStorageActive) {
mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext); mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
mUsbTether.setEnabled(false); mUsbTether.setEnabled(false);
mUsbTether.setChecked(false); mUsbTether.setChecked(false);
@@ -434,40 +446,18 @@ public class TetherSettings extends SettingsPreferenceFragment
@Override @Override
public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) { public boolean onPreferenceTreeClick(PreferenceScreen screen, Preference preference) {
if (preference == mUsbTether) {
boolean newState = mUsbTether.isChecked();
ConnectivityManager cm = ConnectivityManager cm =
(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if (newState) { if (preference == mUsbTether) {
String[] available = cm.getTetherableIfaces(); boolean newState = mUsbTether.isChecked();
String usbIface = findIface(available, mUsbRegexs); if (cm.setUsbTethering(newState) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
if (usbIface == null) {
updateState();
return true;
}
if (cm.tether(usbIface) != ConnectivityManager.TETHER_ERROR_NO_ERROR) {
mUsbTether.setChecked(false); mUsbTether.setChecked(false);
mUsbTether.setSummary(R.string.usb_tethering_errored_subtext); mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
return true; return true;
} }
mUsbTether.setSummary(""); 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("");
}
} else if (preference == mBluetoothTether) { } else if (preference == mBluetoothTether) {
boolean bluetoothTetherState = mBluetoothTether.isChecked(); boolean bluetoothTetherState = mBluetoothTether.isChecked();
@@ -486,8 +476,6 @@ public class TetherSettings extends SettingsPreferenceFragment
} else { } else {
boolean errored = false; boolean errored = false;
ConnectivityManager cm =
(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
String [] tethered = cm.getTetheredIfaces(); String [] tethered = cm.getTetheredIfaces();
String bluetoothIface = findIface(tethered, mBluetoothRegexs); String bluetoothIface = findIface(tethered, mBluetoothRegexs);
if (bluetoothIface != null && if (bluetoothIface != null &&