Log UsbDeviceAttached events for Audio devices

UsbDeviceAttached is logged when the device has an audio interface.
Info such as VID, PID, whether the device has hid or mass storage
interface and the duration for which the usb device was connected
would be logged as well.

Bug: 118783261
Test: Manually tested by running: adb shell cmd stats print-logs,
    adb logcat | grep statsd | grep \(77\)
Change-Id: I57bbfd22ef377553daa113968fe53c4fe0f8e4d2
This commit is contained in:
Badhri Jagan Sridharan
2018-11-16 15:39:22 -08:00
parent 298c49e4f0
commit 5ec629f020
2 changed files with 36 additions and 1 deletions

View File

@@ -1207,6 +1207,12 @@ message UsbDeviceAttached {
optional bool has_audio = 3;
optional bool has_hid = 4;
optional bool has_storage = 5;
enum State {
STATE_DISCONNECTED = 0;
STATE_CONNECTED = 1;
}
optional State state = 6;
optional int64 last_connect_duration_ms = 7;
}

View File

@@ -32,7 +32,9 @@ import android.service.usb.UsbConnectionRecordProto;
import android.service.usb.UsbHostManagerProto;
import android.service.usb.UsbIsHeadsetProto;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Slog;
import android.util.StatsLog;
import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.IndentingPrintWriter;
@@ -86,6 +88,7 @@ public class UsbHostManager {
private int mNumConnects; // TOTAL # of connect/disconnect
private final LinkedList<ConnectionRecord> mConnections = new LinkedList<ConnectionRecord>();
private ConnectionRecord mLastConnect;
private final ArrayMap<String, ConnectionRecord> mConnected = new ArrayMap<>();
/*
* ConnectionRecord
@@ -300,6 +303,11 @@ public class UsbHostManager {
if (mode != ConnectionRecord.DISCONNECT) {
mLastConnect = rec;
}
if (mode == ConnectionRecord.CONNECT) {
mConnected.put(deviceAddress, rec);
} else if (mode == ConnectionRecord.DISCONNECT) {
mConnected.remove(deviceAddress);
}
}
private void logUsbDevice(UsbDescriptorParser descriptorParser) {
@@ -408,6 +416,14 @@ public class UsbHostManager {
// Tracking
addConnectionRecord(deviceAddress, ConnectionRecord.CONNECT,
parser.getRawDescriptors());
// Stats collection
if (parser.hasAudioInterface()) {
StatsLog.write(StatsLog.USB_DEVICE_ATTACHED, newDevice.getVendorId(),
newDevice.getProductId(), parser.hasAudioInterface(),
parser.hasHIDInterface(), parser.hasStorageInterface(),
StatsLog.USB_DEVICE_ATTACHED__STATE__STATE_CONNECTED, 0);
}
}
}
@@ -432,9 +448,22 @@ public class UsbHostManager {
mUsbAlsaManager.usbDeviceRemoved(deviceAddress);
mSettingsManager.usbDeviceRemoved(device);
getCurrentUserSettings().usbDeviceRemoved(device);
ConnectionRecord current = mConnected.get(deviceAddress);
// Tracking
addConnectionRecord(deviceAddress, ConnectionRecord.DISCONNECT, null);
if (current != null) {
UsbDescriptorParser parser = new UsbDescriptorParser(deviceAddress,
current.mDescriptors);
if (parser.hasAudioInterface()) {
// Stats collection
StatsLog.write(StatsLog.USB_DEVICE_ATTACHED, device.getVendorId(),
device.getProductId(), parser.hasAudioInterface(),
parser.hasHIDInterface(), parser.hasStorageInterface(),
StatsLog.USB_DEVICE_ATTACHED__STATE__STATE_DISCONNECTED,
System.currentTimeMillis() - current.mTimestamp);
}
}
} else {
Slog.d(TAG, "Removed device at " + deviceAddress + " was already gone");
}