Android7.0 usb网络共享开关
涉及的文件:
packages\apps\Settings\src\com\android\settings\TetherSettings.java
packages\apps\Settings\src\com\android\settings\TetherService.java
frameworks\base\services\core\java\com\android\server\connectivity\Tethering.java
TetherSettings.java中重点函数
private void updateState() {
String[] available = mCm.getTetherableIfaces();
String[] tethered = mCm.getTetheredIfaces();
String[] errored = mCm.getTetheringErroredIfaces();
updateState(available, tethered, errored);
}
private void updateState(String[] available, String[] tethered,
String[] errored) {
updateUsbState(available, tethered, errored);
updateBluetoothState(available, tethered, errored);
}
private void updateUsbState(String[] available, String[] tethered,
String[] errored) {
boolean usbAvailable = mUsbConnected && !mMassStorageActive;
int usbError = ConnectivityManager.TETHER_ERROR_NO_ERROR;
for (String s : available) {
for (String regex : mUsbRegexs) {
if (s.matches(regex)) {
if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
usbError = mCm.getLastTetherError(s);
}
}
}
}
boolean usbTethered = false;
for (String s : tethered) {
for (String regex : mUsbRegexs) {
if (s.matches(regex)) usbTethered = true;
}
}
boolean usbErrored = false;
for (String s: errored) {
for (String regex : mUsbRegexs) {
if (s.matches(regex)) usbErrored = true;
}
}
if (usbTethered) {
// 已共享网络
mUsbTether.setSummary(R.string.usb_tethering_active_subtext);
mUsbTether.setEnabled(!mDataSaverEnabled);
mUsbTether.setChecked(true);
} else if (usbAvailable) {
if (usbError == ConnectivityManager.TETHER_ERROR_NO_ERROR) {
// usb已连接,usb共享选项打开选项从灰色变为可操作
mUsbTether.setSummary(R.string.usb_tethering_available_subtext);
} else {
mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
}
mUsbTether.setEnabled(!mDataSaverEnabled);
mUsbTether.setChecked(false);
} else if (usbErrored) {
mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
mUsbTether.setEnabled(false);
mUsbTether.setChecked(false);
} else if (mMassStorageActive) {
// usb共享和adb、访问Android存储公用一个usb,当选择浏览Android设备文件时,usb共享不可以使用
// 提示此时状态为文件浏览模式,usb共享不可操作
mUsbTether.setSummary(R.string.usb_tethering_storage_active_subtext);
mUsbTether.setEnabled(false);
mUsbTether.setChecked(false);
} else {
// usb断开连接
mUsbTether.setSummary(R.string.usb_tethering_unavailable_subtext);
mUsbTether.setEnabled(false);
mUsbTether.setChecked(false);
}
}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
private void startTethering(int choice) {
if (choice == TETHERING_BLUETOOTH) {
// Turn on Bluetooth first.
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (adapter.getState() == BluetoothAdapter.STATE_OFF) {
mBluetoothEnableForTether = true;
adapter.enable();
mBluetoothTether.setSummary(R.string.bluetooth_turning_on);
mBluetoothTether.setEnabled(false);
return;
}
}
mCm.startTethering(choice, true, mStartTetheringCallback, mHandler);
}
@Override
public boolean onPreferenceTreeClick(Preference preference) {
if (preference == mUsbTether) {
if (mUsbTether.isChecked()) {
// 打开usb网络共享
startTethering(TETHERING_USB);
} else {
// 关闭USB网络共享
mCm.stopTethering(TETHERING_USB);
}
} else if (preference == mBluetoothTether) {
if (mBluetoothTether.isChecked()) {
startTethering(TETHERING_BLUETOOTH);
} else {
mCm.stopTethering(TETHERING_BLUETOOTH);
// No ACTION_TETHER_STATE_CHANGED is fired or bluetooth unless a device is
// connected. Need to update state manually.
updateState();
}
} else if (preference == mCreateNetwork) {
showDialog(DIALOG_AP_SETTINGS);
}
return super.onPreferenceTreeClick(preference);
}
1234567891011121314151617181920212223242526272829303132333435363738394041
framework中的重点函数
public int setUsbTethering(boolean enable) {
if (VDBG) Log.d(TAG, "setUsbTethering(" + enable + ")");
UsbManager usbManager = (UsbManager)mContext.getSystemService(Context.USB_SERVICE);
synchronized (mPublicSync) {
if (enable) {
if (mRndisEnabled) {
final long ident = Binder.clearCallingIdentity();
try {
// 如果对/sys/class/android_usb/android0已经设置为rndis
// 对usb interface设置ip
tetherMatchingInterfaces(true, ConnectivityManager.TETHERING_USB);
} finally {
Binder.restoreCallingIdentity(ident);
}
} else {
mUsbTetherRequested = true;
// 对/sys/class/android_usb/android0设置为rndis模式
usbManager.setCurrentFunction(UsbManager.USB_FUNCTION_RNDIS);
}
} else {
final long ident = Binder.clearCallingIdentity();
try {
tetherMatchingInterfaces(false, ConnectivityManager.TETHERING_USB);
} finally {
Binder.restoreCallingIdentity(ident);
}
if (mRndisEnabled) {
usbManager.setCurrentFunction(null);
}
mUsbTetherRequested = false;
}
}
return ConnectivityManager.TETHER_ERROR_NO_ERROR;
}
1234567891011121314151617181920212223242526272829303132333435
...
on property:sys.usb.config=ptp
write /sys/class/android_usb/android0/enable 0
write /sys/class/android_usb/android0/idVendor 1f3a
write /sys/class/android_usb/android0/idProduct 1008
write /sys/class/android_usb/android0/functions ${sys.usb.config}
write /sys/class/android_usb/android0/enable 1
setprop sys.usb.state ${sys.usb.config}
on property:sys.usb.config=ptp,adb
write /sys/class/android_usb/android0/enable 0
write /sys/class/android_usb/android0/idVendor 1f3a
write /sys/class/android_usb/android0/idProduct 1009
write /sys/class/android_usb/android0/functions ${sys.usb.config}
write /sys/class/android_usb/android0/enable 1
start adbd
setprop sys.usb.state ${sys.usb.config}
on property:sys.usb.config=rndis
write /sys/class/android_usb/android0/enable 0
write /sys/class/android_usb/android0/idVendor 1f3a
write /sys/class/android_usb/android0/idProduct 100a
write /sys/class/android_usb/android0/functions ${sys.usb.config}
write /sys/class/android_usb/android0/bDeviceClass 224
write /sys/class/android_usb/android0/enable 1
setprop sys.usb.state ${sys.usb.config}
on property:sys.usb.config=rndis,adb
write /sys/class/android_usb/android0/enable 0
write /sys/class/android_usb/android0/idVendor 1f3a
write /sys/class/android_usb/android0/idProduct 100b
write /sys/class/android_usb/android0/functions ${sys.usb.config}
write /sys/class/android_usb/android0/bDeviceClass 224
write /sys/class/android_usb/android0/enable 1
start adbd
setprop sys.usb.state ${sys.usb.config}
...