android usb 从模式切换,android5.1-在系统设置里添加设置选项 以及 USB-OTG 模式切换...

裴楚青
2023-12-01

以实现在设置中添加USB选项,切换HOST模式与OTG模式为例:

1,settings 应用一般在 目录:\packages\apps\Settings\src\com\android\settings\SettingsActivity.java       设置java文件

主要看如下方法:

private void updateTilesList(Listtarget) {

final boolean showDev = mDevelopmentPreferences.getBoolean(

DevelopmentSettings.PREF_SHOW,

android.os.Build.TYPE.equals("eng"));

final UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);

final int size = target.size();

for (int i = 0; i < size; i++) {

DashboardCategory category = target.get(i);

// Ids are integers, so downcasting is ok

int id = (int) category.id;

int n = category.getTilesCount() - 1;

while (n >= 0) {

DashboardTile tile = category.getTile(n);

boolean removeTile = false;

id = (int) tile.id;

if (id == R.id.operator_settings || id == R.id.manufacturer_settings) {

if (!Utils.updateTileToSpecificActivityFromMetaDataOrRemove(this, tile)) {

removeTile = true;

}

} else if (id == R.id.wifi_settings) {

// Remove WiFi Settings if WiFi service is not available.

if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {

removeTile = true;

}

} else if (id == R.id.bluetooth_settings) {

// Remove Bluetooth Settings if Bluetooth service is not available.

if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_BLUETOOTH)||(SystemProperties.get("ro.rk.bt_enable", "true").equals("false"))) {

removeTile = true;

}

} else if (id == R.id.data_usage_settings) {

// Remove data usage when kernel module not enabled

final INetworkManagementService netManager = INetworkManagementService.Stub

.asInterface(ServiceManager.getService(Context.NETWORKMANAGEMENT_SERVICE));

try {

if (!netManager.isBandwidthControlEnabled()) {

removeTile = true;

}

} catch (RemoteException e) {

// ignored

}

} else if (id == R.id.battery_settings) {

// Remove battery settings when battery is not available. (e.g. TV)

if (!mBatteryPresent) {

removeTile = true;

}

} else if (id == R.id.home_settings) {

if (!updateHomeSettingTiles(tile)) {

removeTile = true;

}

} else if (id == R.id.user_settings) {

boolean hasMultipleUsers =

((UserManager) getSystemService(Context.USER_SERVICE))

.getUserCount() > 1;

if (!UserHandle.MU_ENABLED

|| (!UserManager.supportsMultipleUsers()

&& !hasMultipleUsers)

|| Utils.isMonkeyRunning()) {

removeTile = true;

}

} else if (id == R.id.nfc_payment_settings) {

if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_NFC)) {

removeTile = true;

} else {

// Only show if NFC is on and we have the HCE feature

NfcAdapter adapter = NfcAdapter.getDefaultAdapter(this);

if (adapter == null || !adapter.isEnabled() ||

!getPackageManager().hasSystemFeature(

PackageManager.FEATURE_NFC_HOST_CARD_EMULATION)) {

removeTile = true;

}

}

} else if (id == R.id.print_settings) {

boolean hasPrintingSupport = getPackageManager().hasSystemFeature(

PackageManager.FEATURE_PRINTING);

if (!hasPrintingSupport) {

removeTile = true;

}

} else if (id == R.id.development_settings) {

if (!showDev || um.hasUserRestriction(

UserManager.DISALLOW_DEBUGGING_FEATURES)) {

removeTile = true;

}

} else if(id ==R.id.hdmi_settings){

boolean isTablet = "box".equals(SystemProperties.get("ro.target.product", "tablet"));

if (isTablet || SystemProperties.get("ro.rk.hdmi_enable", "true").equals("false")){

removeTile = true;

}

} else if(id ==R.id.screenshot_settings){

if (SystemProperties.get("ro.rk.screenshot_enable", "true").equals("false")){

removeTile = true;

}

} else if(id ==R.id.usb_settings){

//

//if (!"box".equals(SystemProperties.get("ro.target.product", "tablet"))){

if (SystemProperties.get("ro.rk.usb_settings", "true").equals("false")){

removeTile = true;

}

}

if (UserHandle.MU_ENABLED && UserHandle.myUserId() != 0

&& !ArrayUtils.contains(SETTINGS_FOR_RESTRICTED, id)) {

removeTile = true;

}

if (removeTile && n < category.getTilesCount()) {

category.removeTile(n);

}

n--;

}

}

}

其中,这个判断就是判断是否在设置中生成USB选项;

(id ==R.id.usb_settings){

//

//if (!"box".equals(SystemProperties.get("ro.target.product", "tablet"))){

if (SystemProperties.get("ro.rk.usb_settings", "true").equals("false")){

removeTile = true;

}

}

要使USB选项生成,需要在 \device\rockchip\common\device.mk  文件中  PRODUCT_PROPERTY_OVERRIDES  下添加上  ro.rk.usb_settings=true   \

还有几个相关的文件:

\packages\apps\Settings\src\com\android\settings\Settings.java

\packages\apps\Settings\AndroidManifest.xml                                          声明文件

\packages\apps\Settings\res\xml\dashboard_categories.xml                    布局文件

\packages\apps\Settings\src\com\android\settings\UsbSetting.java         usb类文件

\packages\apps\Settings\res\xml\usb_switch.xml                                      布局文件

\packages\apps\Settings\res\xml\usb_settings.xml                                    布局文件

接下来分析USB-OTG

usb 分两种模式   HOST MODE    和   DEVICE MODE

HOST mode :主板上作为主设备端,与从设备 进行通讯;例如,U盘插入,主板读取U盘数据

DEVICE mode :主板作为从设备,与PC  进行连接,通过 OTG 线; 此时电脑上可以识别出从设备连接;

可以通过查看结果:

cat /sys/bus/platform/drivers/usb20_otg/force_usb_mode

可以通过修改  /sys/bus/platform/drivers/usb20_otg/force_usb_mode

中的值去打开otg 连接pc的功能。 1->0 or 1->2均可以 打开

反之,则 打开 U盘读取 功能,主板作为 主设备;

echo 1 > /sys/bus/platform/drivers/usb20_otg/force_usb_mode

以上做的 USB 选项,就是选择 切换模式

在 rk3288.dtsi 中可以选择 默认的模式

usb0: usb@ff580000 {

compatible = "rockchip,rk3288_usb20_otg";

reg = <0xff580000 0x40000>;

interrupts = ;

clocks = , ;

clock-names = "clk_usbphy0", "hclk_usb0";

resets = , ,

;

reset-names = "otg_ahb", "otg_phy", "otg_controller";

/*0 - Normal, 1 - Force Host, 2 - Force Device*/

//yr change code

//rockchip,usb-mode = <0>;

rockchip,usb-mode = <1>;           // 修改默认USB-OTG为HOST模式,识别U盘

};

参考连接:

1、  http://www.cnblogs.com/grein/p/5892723.html

2、 http://blog.csdn.net/sunjing_/article/details/51085895

3、 http://www.07net01.com/program/2016/03/1391773.html

 类似资料: