Usbsetting 中tethering 设置流程
一 资源位置及入口文件
USB tethering, usb_tethering_button_text
Xml/tether_prefs.xml------usb_tether_settings,布局文件
packages/apps/Settings/AndroidManifest.xml
<!-- Keep compatibility with old shortcuts.-->
<activity-alias android:name=".TetherSettings"
android:label="@string/tether_settings_title_all"
android:clearTaskOnLaunch="true"
android:exported="true"
android:targetActivity="Settings$TetherSettingsActivity">
<meta-dataandroid:name="com.android.settings.FRAGMENT_CLASS"
android:value="com.android.settings.TetherSettings"/>
<meta-dataandroid:name="com.android.settings.TOP_LEVEL_HEADER_ID"
android:resource="@id/wireless_settings"/>
<meta-dataandroid:name="com.android.settings.PARENT_FRAGMENT_TITLE"
android:resource="@string/wireless_networks_settings_title"/>
<meta-dataandroid:name="com.android.settings.PARENT_FRAGMENT_CLASS"
android:value="com.android.settings.Settings$WirelessSettingsActivity"/>
</activity-alias>
TetherSettings.java
二 触发流程
2.1 TetherSettings.java
packages/apps/Settings/src/com/android/settings/TetherSettings.java
onPreferenceTreeClick
SXlog.d(TAG, "onPreferenceTreeClick -setusbTethering(" + newState + ") mUsbTethering: " + mUsbTethering);
setUsbTethering(true);
下面是设置时抓取的log日志
D/TetherSettings( 543):onPreferenceTreeClick - setusbTethering(true)mUsbTethering: true
D/Tethering( 271):setUsbTethering(true)
D/UsbDeviceManager( 271):setCurrentFunction(rndis) default: false
W/UsbDeviceManager( 271):handleMessage: 2
W/UsbDeviceManager( 271):setEnabledFunctions: functions = rndis
W/UsbDeviceManager( 271):setEnabledFunctions, mDefaultFunctions: mtp,adb
W/UsbDeviceManager( 271):setEnabledFunctions, mCurrentFunctions: mtp,adb
D/UsbDeviceManager( 271):setEnabledFunctions, mSettingFunction: mtp,adb
W/UsbDeviceManager( 271):containsFunction, functions: rndis
W/UsbDeviceManager( 271):containsFunction, function: adb
W/UsbDeviceManager( 271):containsFunction index: -1
W/UsbDeviceManager( 271):addFunction, functions: rndis
W/UsbDeviceManager( 271):addFunction, function: adb
W/UsbDeviceManager( 271):containsFunction, functions: rndis
W/UsbDeviceManager( 271):containsFunction, function: acm
W/UsbDeviceManager( 271):containsFunction index: -1
W/UsbDeviceManager( 271):containsFunction, functions: rndis,adb
W/UsbDeviceManager( 271):containsFunction, function: acm
W/UsbDeviceManager( 271):containsFunction index: -1
D/UsbDeviceManager( 271):setUsbConfig(none)
W/UsbDeviceManager( 271):setUsbConfig, config: none
public boolean onPreferenceTreeClick(PreferenceScreenscreen, Preference preference) {
ConnectivityManager cm =
(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if (preference == mUsbTether) {
if (!mUsbTethering) {
boolean newState = mUsbTether.isChecked();
mUsbTethering = true;
mUsbTetherCheckEnable = false;
mUsbTether.setEnabled(false);
SXlog.d(TAG, "onPreferenceTreeClick -setusbTethering(" + newState + ") mUsbTethering: " + mUsbTethering);
if (newState) {
startProvisioningIfNecessary(USB_TETHERING);
} else {
setUsbTethering(newState);
}
} else {
return true;
}
} else if (preference == mBluetoothTether) {
private void setUsbTethering(boolean enabled){
ConnectivityManager cm =
(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if (cm.setUsbTethering(enabled) !=ConnectivityManager.TETHER_ERROR_NO_ERROR) {
mUsbTether.setChecked(false);
mUsbTether.setSummary(R.string.usb_tethering_errored_subtext);
return;
}
mUsbTether.setSummary("");
}
2.2 ConnectivityManager.java
frameworks/base/core/java/android/net/ConnectivityManager.java
private final IConnectivityManager mService;
public int setUsbTethering(boolean enable){
try {
return mService.setUsbTethering(enable);
} catch (RemoteException e) {
return TETHER_ERROR_SERVICE_UNAVAIL;
}
}
manager 和service有一个对应关系,固定的规则。manager是为了sdk诞生的,方便app开发者调用。其实可以直接调用service,如mountservice是没有mountmanager的。
service是在系统起来是就被android系统启动的,而manager是后期有需要时实例化起来的。
Service的目录在:/frameworks/base/services/java/com/android/server/
manager的目录在:frameworks/base/core/java/android
2.3 ConnectivityService.java
frameworks/base/services/java/com/android/server
privateTethering mTethering;
public int setUsbTethering(boolean enable){
enforceTetherAccessPermission();
if (isTetheringSupported()) {
return mTethering.setUsbTethering(enable);
} else {
return ConnectivityManager.TETHER_ERROR_UNSUPPORTED;
}
}
2.4 Tethering.java
frameworks/base/services/java/com/android/server/connectivity/Tethering.java
public int setUsbTethering(boolean enable){
UsbManager usbManager =(UsbManager)mContext.getSystemService(Context.USB_SERVICE);
………………
else {
mUsbTetherRequested = true;
usbManager.setCurrentFunction(UsbManager.USB_FUNCTION_RNDIS,false);
}
通过service,来获取manager,进行操作。这个tethering.java的构造函数
public Tethering(Context context, INetworkManagementServicenmService,
INetworkStatsService statsService, IConnectivityManagerconnService, Looper looper) {
Context是获取全局信息的接口,
2.5 UsbManager.java
/frameworks/base/core/java/android/hardware/usb/UsbManager.java
public void setCurrentFunction(String function, booleanmakeDefault) {
try {
mService.setCurrentFunction(function,makeDefault);
} catch (RemoteException e) {
Log.e(TAG, "RemoteException in setCurrentFunction", e);
}
}
2.6frameworks/base/services/java/com/android/server/usb$
mengfd1@tablet-C:~/work/A2107/frameworks/base/services/java/com/android/server$cd usb
mengfd1@tablet-C:~/work/A2107/frameworks/base/services/java/com/android/server/usb$ls
UsbDeviceManager.java UsbHostManager.java UsbService.java UsbSettingsManager.java
/frameworks/base/services/java/com/android/server/usb/UsbService.java
public void setCurrentFunction(String function, booleanmakeDefault) {
mContext.enforceCallingOrSelfPermission(android.Manifest.permission.MANAGE_USB,null);
if (mDeviceManager != null) {
mDeviceManager.setCurrentFunction(function,makeDefault);
} else {
throw new IllegalStateException("USB device mode notsupported");
}
}
/frameworks/base/services/java/com/android/server/usb/UsbDeviceManager.java
public void setCurrentFunction(Stringfunction, boolean makeDefault) {
if (DEBUG) Slog.d(TAG, "setCurrentFunction(" + function + ")default: " + makeDefault);
mHandler.sendMessage(MSG_SET_CURRENT_FUNCTION, function,makeDefault);
}
public void handleMessage(Message msg){
case MSG_SET_CURRENT_FUNCTION:
String function = (String)msg.obj;
boolean makeDefault = (msg.arg1 == 1);
if (function != null &&function.equals(UsbManager.USB_FUNCTION_CHARGING_ONLY)){
mSettingUsbCharging = true;
SXlog.d(TAG, "handleMessage - MSG_SET_CURRENT_FUNCTION -USB_FUNCTION_CHARGING_ONLY - makeDefault: " +makeDefault);
} else {
mSettingUsbCharging = false;
}
setEnabledFunctions(function, makeDefault);
SXlog.d(TAG, "handleMessage - MSG_SET_CURRENT_FUNCTION - function:" + function);
break;
private void setEnabledFunctions(Stringfunctions, boolean makeDefault) {
private boolean setUsbConfig(Stringconfig) {
SystemProperties.set("sys.usb.config", config);