这个 修改主要在 Android 系统中修改了 USB 权限请求和声音提示的相关代码,使得在插入 USB 设备时,系统不再弹出权限请求对话框,并且不再播放声音提示。
修改的文件:
frameworks/base/packages/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java
frameworks/base/services/core/java/com/android/server/notification/NotificationManagerService.java
frameworks/base/services/usb/java/com/android/server/usb/UsbHostManager.java
frameworks/base/services/usb/java/com/android/server/usb/UsbSettingsManager.java
文件修改说明:
UsbPermissionActivity.java
: 在UsbPermissionActivity
类中,禁用了setupAlert
方法,并将mPermissionGranted
设置为true
,使得在插入 USB 设备时,系统不再弹出权限请求对话框。NotificationManagerService.java
: 在NotificationManagerService
类中,禁用了播放声音提示的相关代码,使得在插入 USB 设备时,系统不再播放声音提示。UsbHostManager.java
: 在UsbHostManager
类中,添加了mNewConfiguration
和mNewInterface
的初始化代码。UsbSettingsManager.java
: 在UsbSettingsManager
类中,修改了hasPermission
和checkPermission
方法,使得这些方法总是返回true
,即系统总是认为已经获得了 USB 设备的权限。
--- .../systemui/usb/UsbPermissionActivity.java | 4 +++- .../notification/NotificationManagerService.java | 6 ++++-- .../com/android/server/usb/UsbHostManager.java | 3 +++ .../com/android/server/usb/UsbSettingsManager.java | 23 +++++++++++++--------- 4 files changed, 24 insertions(+), 12 deletions(-) mode change 100644 => 100755 frameworks/base/services/core/java/com/android/server/notification/NotificationManagerService.java diff --git a/frameworks/base/packages/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java b/frameworks/base/packages/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java index 1e69fc5..e0bb559 100755 --- a/frameworks/base/packages/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java +++ b/frameworks/base/packages/SystemUI/src/com/android/systemui/usb/UsbPermissionActivity.java @@ -110,7 +110,9 @@ public class UsbPermissionActivity extends AlertActivity com.android.internal.R.id.clearDefaultHint); mClearDefaultHint.setVisibility(View.GONE); - setupAlert(); + //setupAlert(); + mPermissionGranted = true; + finish(); } diff --git a/frameworks/base/services/core/java/com/android/server/notification/NotificationManagerService.java b/frameworks/base/services/core/java/com/android/server/notification/NotificationManagerService.java old mode 100644 new mode 100755 index 9b24172..e01699d --- a/frameworks/base/services/core/java/com/android/server/notification/NotificationManagerService.java +++ b/frameworks/base/services/core/java/com/android/server/notification/NotificationManagerService.java @@ -2230,12 +2230,14 @@ public class NotificationManagerService extends SystemService { AudioAttributes.toLegacyStreamType(audioAttributes)) != 0) && !mAudioManager.isAudioFocusExclusive()) { final long identity = Binder.clearCallingIdentity(); - try { + //modify disabled usb plug in sound playing 220322 + /*try { final IRingtonePlayer player = mAudioManager.getRingtonePlayer(); if (player != null) { if (DBG) Slog.v(TAG, "Playing sound " + soundUri + " with attributes " + audioAttributes); + player.playAsync(soundUri, record.sbn.getUser(), looping, audioAttributes); buzzBeepBlinked = true; @@ -2243,7 +2245,7 @@ public class NotificationManagerService extends SystemService { } catch (RemoteException e) { } finally { Binder.restoreCallingIdentity(identity); - } + }*/ } } diff --git a/frameworks/base/services/usb/java/com/android/server/usb/UsbHostManager.java b/frameworks/base/services/usb/java/com/android/server/usb/UsbHostManager.java index e769bda..088fece 100644 --- a/frameworks/base/services/usb/java/com/android/server/usb/UsbHostManager.java +++ b/frameworks/base/services/usb/java/com/android/server/usb/UsbHostManager.java @@ -230,6 +230,9 @@ public class UsbHostManager { mNewConfigurations = null; mNewInterfaces = null; mNewEndpoints = null; + //这里是新加的代码 + mNewConfiguration = null; + mNewInterface = null; } } diff --git a/frameworks/base/services/usb/java/com/android/server/usb/UsbSettingsManager.java b/frameworks/base/services/usb/java/com/android/server/usb/UsbSettingsManager.java index 37b5c51..2ccbbac 100644 --- a/frameworks/base/services/usb/java/com/android/server/usb/UsbSettingsManager.java +++ b/frameworks/base/services/usb/java/com/android/server/usb/UsbSettingsManager.java @@ -981,7 +981,7 @@ class UsbSettingsManager { } public boolean hasPermission(UsbDevice device) { - synchronized (mLock) { + /* synchronized (mLock) { int uid = Binder.getCallingUid(); if (uid == Process.SYSTEM_UID || mDisablePermissionDialogs) { return true; @@ -992,10 +992,12 @@ class UsbSettingsManager { } return uidList.get(uid); } +*/ + return true; } public boolean hasPermission(UsbAccessory accessory) { - synchronized (mLock) { + /*synchronized (mLock) { int uid = Binder.getCallingUid(); if (uid == Process.SYSTEM_UID || mDisablePermissionDialogs) { return true; @@ -1005,19 +1007,20 @@ class UsbSettingsManager { return false; } return uidList.get(uid); - } + }*/ + return true; } public void checkPermission(UsbDevice device) { - if (!hasPermission(device)) { + /* if (!hasPermission(device)) { throw new SecurityException("User has not given permission to device " + device); - } + }*/ } public void checkPermission(UsbAccessory accessory) { - if (!hasPermission(accessory)) { + /* if (!hasPermission(accessory)) { throw new SecurityException("User has not given permission to accessory " + accessory); - } + }*/ } private void requestPermissionDialog(Intent intent, String packageName, PendingIntent pi) { @@ -1054,7 +1057,8 @@ class UsbSettingsManager { Intent intent = new Intent(); // respond immediately if permission has already been granted - if (hasPermission(device)) { + //if (hasPermission(device)) { + if (true) { intent.putExtra(UsbManager.EXTRA_DEVICE, device); intent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, true); try { @@ -1074,7 +1078,8 @@ class UsbSettingsManager { Intent intent = new Intent(); // respond immediately if permission has already been granted - if (hasPermission(accessory)) { + //if (hasPermission(accessory)) { + if (true) { intent.putExtra(UsbManager.EXTRA_ACCESSORY, accessory); intent.putExtra(UsbManager.EXTRA_PERMISSION_GRANTED, true); try { -- 1.9.1
- 调试阶段:问题调试
插着HIDPOS开机 -----------开机上电usb只有以下3个 Bus 001 Device 005: ID 1a40:0201 Bus 001 Device 001: ID 1d6b:0002 Bus 001 Device 006: ID 26f1:8803 -----------重新拔插一下USB变成了4个 Bus 001 Device 005: ID 1a40:0201 Bus 001 Device 001: ID 1d6b:0002 Bus 001 Device 008: ID 26f1:8803 Bus 001 Device 007: ID 1c4f:0034 ----不能识别设备 UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=3,mSubclass=1,mProtocol=2,mEndpoints=[ ----可以识别设备 UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=3,mSubclass=0,mProtocol=0,mEndpoints=[ ----拔插鼠标 727-911/system_process D/UsbHostManager: Added device UsbDevice[mName=/dev/bus/usb/001/004,mVendorId=7247,mProductId=52,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=SIGMACHIP,mProductName=Usb Mouse,mSerialNumber=null,mConfigurations=[ UsbConfiguration[mId=1,mName=null,mAttributes=160,mMaxPower=50,mInterfaces=[ UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=3,mSubclass=1,mProtocol=2,mEndpoints=[ UsbEndpoint[mAddress=129,mAttributes=3,mMaxPacketSize=4,mInterval=10]]]] ----拔插HID POS 727-911/system_process D/UsbHostManager: Added device UsbDevice[mName=/dev/bus/usb/001/005,mVendorId=9969,mProductId=34819,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=TMS,mProductName=HIDPOS,mSerialNumber=1234567890abcd,mConfigurations=[ UsbConfiguration[mId=1,mName=null,mAttributes=224,mMaxPower=50,mInterfaces=[ UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=3,mSubclass=0,mProtocol=0,mEndpoints=[ UsbEndpoint[mAddress=130,mAttributes=3,mMaxPacketSize=64,mInterval=4] UsbEndpoint[mAddress=1,mAttributes=3,mMaxPacketSize=64,mInterval=1]]]] 67:01000010 USB Host State: /dev/bus/usb/001/014: UsbDevice[mName=/dev/bus/usb/001/014,mVendorId=7247,mProductId=52,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=SIGMACHIP,mProductName=Usb Mouse,mSerialNumber=null,mConfigurations=[ UsbConfiguration[mId=1,mName=null,mAttributes=160,mMaxPower=50,mInterfaces=[ UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=3,mSubclass=1,mProtocol=2,mEndpoints=[ UsbEndpoint[mAddress=129,mAttributes=3,mMaxPacketSize=4,mInterval=10]]]] /dev/bus/usb/001/013: UsbDevice[mName=/dev/bus/usb/001/013,mVendorId=6127,mProductId=24600,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=Lite-On Technology Corp.,mProductName=Lenovo USB Keyboard,mSerialNumber=null,mConfigurations=[ UsbConfiguration[mId=1,mName=null,mAttributes=160,mMaxPower=50,mInterfaces=[]] USB Host State: /dev/bus/usb/001/015: UsbDevice[mName=/dev/bus/usb/001/015,mVendorId=6127,mProductId=24600,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=Lite-On Technology Corp.,mProductName=Lenovo USB Keyboard,mSerialNumber=null,mConfigurations=[ UsbConfiguration[mId=1,mName=null,mAttributes=160,mMaxPower=50,mInterfaces=[ UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=3,mSubclass=1,mProtocol=1,mEndpoints=[ UsbEndpoint[mAddress=129,mAttributes=3,mMaxPacketSize=8,mInterval=10]] UsbInterface[mId=1,mAlternateSetting=0,mName=null,mClass=3,mSubclass=0,mProtocol=0,mEndpoints=[ UsbEndpoint[mAddress=130,mAttributes=3,mMaxPacketSize=3,mInterval=10]]]] /dev/bus/usb/001/014: UsbDevice[mName=/dev/bus/usb/001/014,mVendorId=7247,mProductId=52,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=SIGMACHIP,mProductName=Usb Mouse,mSerialNumber=null,mConfigurations=[ UsbConfiguration[mId=1,mName=null,mAttributes=160,mMaxPower=50,mInterfaces=[]] USB Host State: /dev/bus/usb/001/016: UsbDevice[mName=/dev/bus/usb/001/016,mVendorId=7247,mProductId=52,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=SIGMACHIP,mProductName=Usb Mouse,mSerialNumber=null,mConfigurations=[ UsbConfiguration[mId=1,mName=null,mAttributes=160,mMaxPower=50,mInterfaces=[ UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=3,mSubclass=1,mProtocol=2,mEndpoints=[ UsbEndpoint[mAddress=129,mAttributes=3,mMaxPacketSize=4,mInterval=10]]]] /dev/bus/usb/001/015: UsbDevice[mName=/dev/bus/usb/001/015,mVendorId=6127,mProductId=24600,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=Lite-On Technology Corp.,mProductName=Lenovo USB Keyboard,mSerialNumber=null,mConfigurations=[ UsbConfiguration[mId=1,mName=null,mAttributes=160,mMaxPower=50,mInterfaces=[]] USB Host State: /dev/bus/usb/001/016: UsbDevice[mName=/dev/bus/usb/001/016,mVendorId=7247,mProductId=52,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=SIGMACHIP,mProductName=Usb Mouse,mSerialNumber=null,mConfigurations=[ UsbConfiguration[mId=1,mName=null,mAttributes=160,mMaxPower=50,mInterfaces=[]] /dev/bus/usb/001/017: UsbDevice[mName=/dev/bus/usb/001/017,mVendorId=6127,mProductId=24600,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=Lite-On Technology Corp.,mProductName=Lenovo USB Keyboard,mSerialNumber=null,mConfigurations=[ UsbConfiguration[mId=1,mName=null,mAttributes=160,mMaxPower=50,mInterfaces=[ UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=3,mSubclass=1,mProtocol=1,mEndpoints=[ UsbEndpoint[mAddress=129,mAttributes=3,mMaxPacketSize=8,mInterval=10]] UsbInterface[mId=1,mAlternateSetting=0,mName=null,mClass=3,mSubclass=0,mProtocol=0,mEndpoints=[ UsbEndpoint[mAddress=130,mAttributes=3,mMaxPacketSize=3,mInterval=10]]]]
- 调试阶段:问题验证
复现方式: 通过先后拔插USB调试打印中,发现dumpsys usb , hid pos 没有UsbInterface会导致APP端的usb 通讯流程失败。 问题原因: Android 系统 usb host manger 会将第一个USB设备的UsbInterface对象替换,所以当有两台USB设备链接时,始终都 会有一台USB设备无法与之通信。 解决办法: 将mNewConfiguration对象,mNewInterface对象都置位空,这样在第二个USB设备初始化时,mNewConfiguration就 保持为空的状态,不会进行二次赋值。 gpio修改的弊端: 拔插usb iddig, hid pos 概率性能出现UsbInterface,却决于usb加载顺序。 修改过后的打印: USB Host State: /dev/bus/usb/001/009: UsbDevice[mName=/dev/bus/usb/001/009,mVendorId=7247,mProductId=52,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=SIGMACHIP,mProductName=Usb Mouse,mSerialNumber=null,mConfigurations=[ UsbConfiguration[mId=1,mName=null,mAttributes=160,mMaxPower=50,mInterfaces=[ >>>>>UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=3,mSubclass=1,mProtocol=2,mEndpoints=[ UsbEndpoint[mAddress=129,mAttributes=3,mMaxPacketSize=4,mInterval=10]]]] /dev/bus/usb/001/008: UsbDevice[mName=/dev/bus/usb/001/008,mVendorId=9969,mProductId=34819,mClass=0,mSubclass=0,mProtocol=0,mManufacturerName=TMS,mProductName=HIDPOS,mSerialNumber=1234567890abcd,mConfigurations=[ UsbConfiguration[mId=1,mName=null,mAttributes=224,mMaxPower=50,mInterfaces=[ //正确打印会出现 >>>>>UsbInterface[mId=0,mAlternateSetting=0,mName=null,mClass=3,mSubclass=0,mProtocol=0,mEndpoints=[ UsbEndpoint[mAddress=130,mAttributes=3,mMaxPacketSize=64,mInterval=4] UsbEndpoint[mAddress=1,mAttributes=3,mMaxPacketSize=64,mInterval=1]]]]
- 调试阶段:APP端USB调试函数
public void usbPrint() { // 取连接到设备上的USB设备集合 usbManager = (UsbManager) activity.getSystemService(Context.USB_SERVICE); HashMap<String, UsbDevice> map = usbManager.getDeviceList(); // 没有连接设备 if (map.isEmpty()) { // ToastUtil.showToast(activity, activity.getString(R.string.connect_usb)); return; } // 遍历集合取指定的USB设备 UsbDevice usbDevice = null; for (UsbDevice device : map.values()) { int VendorID = device.getVendorId(); int ProductID = device.getProductId(); if (VendorID == 9969 && ProductID == 34819) { usbDevice = device; break; } } // 没有找到设备 if (usbDevice == null) return; Log.d(TAG, "print usb device 22:" + usbDevice); // 程序是否有操作设备的权限 if (!usbManager.hasPermission(usbDevice)) { usbManager.requestPermission(usbDevice, mPermissionIntent); return; } Log.d(TAG, "print usb device 33:" + usbDevice.getInterfaceCount()); // ShellUtils.execCommand("echo \"-w=67:0 1 0 0 0 1 0\" >/sys/devices/virtual/misc/mtgpio/pin", true); if (usbDevice.getInterfaceCount() == 0) { //ShellUtils.execCommand("echo \"-w=67:0 1 1 0 1 1 0\" >/sys/devices/virtual/misc/mtgpio/pin",false); } // 设备接口, 注意设备不同接口不同, 这里取第一个 UsbInterface usbInterface = usbDevice.getInterface(0); Log.d(TAG, "print usb device 44:" + usbInterface); // 分配端点, 注意设备不同端点不同, 这里取第一个 outEndpoint = usbInterface.getEndpoint(0); Log.d(TAG, "print usb device 55:" + outEndpoint); // 打开设备建立连接 connection = usbManager.openDevice(usbDevice); connection.claimInterface(usbInterface, true); }