Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5ec629f0 authored by Badhri Jagan Sridharan's avatar Badhri Jagan Sridharan
Browse files

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
parent 298c49e4
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -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;
}


+30 −1
Original line number Diff line number Diff line
@@ -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");
            }