From 66d7513001c4fd16eaead0f3a0322928c1d711ce Mon Sep 17 00:00:00 2001 From: jiabin Date: Wed, 21 Feb 2024 20:29:47 +0000 Subject: Use the descriptor passed in when adding USB device instead of querying again. When USB device is added, the raw descriptor has already been reported. It is important to check if that raw descriptor is null or not to avoid crash when creating a parser. What is more, given the descriptor has been known when added the device, UsbHostManager can just cache that descriptor instead of querying the raw descriptor again. Bug: 326268581 Test: repo steps in bug Change-Id: Iecefbd2eccbd981e77c4afe9cf8b00d830de27d3 --- services/usb/java/com/android/server/usb/UsbHostManager.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/services/usb/java/com/android/server/usb/UsbHostManager.java b/services/usb/java/com/android/server/usb/UsbHostManager.java index 8b44579eda33..36adeec74e36 100644 --- a/services/usb/java/com/android/server/usb/UsbHostManager.java +++ b/services/usb/java/com/android/server/usb/UsbHostManager.java @@ -380,6 +380,11 @@ public class UsbHostManager { return false; } + if (descriptors == null) { + Slog.e(TAG, "Failed to add device as the descriptor is null"); + return false; + } + UsbDescriptorParser parser = new UsbDescriptorParser(deviceAddress, descriptors); if (deviceClass == UsbConstants.USB_CLASS_PER_INTERFACE && !checkUsbInterfacesDenyListed(parser)) { @@ -462,8 +467,7 @@ public class UsbHostManager { } // Tracking - addConnectionRecord(deviceAddress, ConnectionRecord.CONNECT, - parser.getRawDescriptors()); + addConnectionRecord(deviceAddress, ConnectionRecord.CONNECT, descriptors); // Stats collection FrameworkStatsLog.write(FrameworkStatsLog.USB_DEVICE_ATTACHED, -- cgit v1.2.3-59-g8ed1b