Settings开发者模式添加OTG切换功能

📅 2026/6/30 1:33:26
Settings开发者模式添加OTG切换功能
在SettingsLib中创建AbstractEnableOtgPreferenceController来 作为接口管理在Settings APP中创建OtgPreferenceController起到呈上起下的作用XML配置字符配置位置找到的去Framework去查找frameworks/base/packages/SettingsLib/res/values/strings.xmlstring nameenable_adbUSB debugging/string !-- Setting checkbox summary for Whether to enable USB debugging support on the phone -- string nameenable_adb_summaryDebug mode when USB is connected/string string nameenable_otgOTG Mode/string !-- Setting checkbox summary for Whether to enable otg debugging support on the phone -- string nameenable_otg_summaryOTG mode when OTG interface is connected/stringUI所在位置在Settings当中 ,packages/apps/Settings/res/xml/development_settings.xmlcom.android.settingslib.RestrictedSwitchPreference android:keyenable_adb android:titlestring/enable_adb android:summarystring/enable_adb_summary / SwitchPreference android:keyenable_otg android:titlestring/enable_otg android:summarystring/enable_otg_summary /Fragment加载packages/apps/Settings/src/com/android/settings/development/DevelopmentSettingsDashboardFragment.java在初始化过程中 框架会 自动遍历所有Controllercontrollers.add(new OtgPreferenceController(context));在Settings中extend AbstractEnableAdbPreferenceControllerOtgPreferenceController extend AbstractEnableOtgPreferenceController类 来调用SettingsLib方法package com.android.settings.development; import android.content.Context; import com.android.settings.core.PreferenceControllerMixin; import com.android.settingslib.development.AbstractEnableOtgPreferenceController; /** * Preference controller for the OTG mode switch in Developer Options. * * pExtends {link AbstractEnableOtgPreferenceController} from SettingsLib which handles * all core logic: reading/writing the sysfs node at * {code /sys/devices/platform/2602e000.syscon/2602e000.syscon:usb2-phy0/otg_mode}, * UI state updates, and developer-options-switch reset behavior. * * pNo confirmation dialog is shown — toggling takes effect immediately. */ public class OtgPreferenceController extends AbstractEnableOtgPreferenceController implements PreferenceControllerMixin { public OtgPreferenceController(Context context) { super(context); } }xml 如何 和 Java 通过 Key 实现 绑定public abstract class AbstractEnableOtgPreferenceController extends DeveloperOptionsPreferenceController { private static final String TAG EnableOtgPrefCtrl; private static final String KEY_ENABLE_OTG enable_otg; /** Sysfs node path for OTG mode control on RK3576 platform. */ private static final String OTG_MODE_SYSFS_PATH /sys/devices/platform/2602e000.syscon/2602e000.syscon:usb2-phy0/otg_mode; private static final String OTG_PORT persist.sys.otg.mode; /** Value written to sysfs to enable OTG mode. */ private static final String OTG_MODE_VALUE_ON otg; /** Value written to sysfs to disable OTG (i.e., switch to HOST mode). */ private static final String OTG_MODE_VALUE_OFF host; public AbstractEnableOtgPreferenceController(Context context) { super(context); } // 告诉框架此类事哪个XML元素的Controller返回值与xml中定义的key完全一致。 Override public String getPreferenceKey() { return KEY_ENABLE_OTG; } Override public void displayPreference(PreferenceScreen screen) { super.displayPreference(screen); if (isAvailable()) { mPreference (TwoStatePreference) screen.findPreference(KEY_ENABLE_OTG); } } // true是否可见 Override public boolean isAvailable() { return true; } Override public void updateState(Preference preference) { boolean otgEnabled isOtgEnabled(); ((TwoStatePreference) preference).setChecked(otgEnabled); } Override public boolean handlePreferenceTreeClick(Preference preference) { if (!KEY_ENABLE_OTG.equals(preference.getKey())) { return false; } boolean isChecked ((TwoStatePreference) preference).isChecked(); writeOtgSetting(isChecked); return true; } /** * Write OTG mode to the sysfs node. * * param enable true for OTG mode, false for HOST mode.