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

Commit 223b3c79 authored by Badhri Jagan Sridharan's avatar Badhri Jagan Sridharan
Browse files

Log UsbConnectorStateChanged events

The following would the logged:
1. Status of the connection - connected/disconnected.
2. Port id as a device could have multiple ports.

Bug: 118783261
Test: Manually tested by running: adb shell cmd stats print-logs,
adb logcat | grep statsd | grep \(70\)
Change-Id: Ideea36c7745293f5b6ec9cc35fd5f709f5175d5a
parent 298c49e4
Loading
Loading
Loading
Loading
+4 −3
Original line number Diff line number Diff line
@@ -1255,14 +1255,15 @@ message BluetoothConnectionStateChanged {
 * Logs when something is plugged into or removed from the USB-C connector.
 *
 * Logged from:
 *  Vendor USB HAL.
 *  UsbService
 */
message UsbConnectorStateChanged {
    enum State {
      DISCONNECTED = 0;
      CONNECTED = 1;
        STATE_DISCONNECTED = 0;
        STATE_CONNECTED = 1;
    }
    optional State state = 1;
    optional string id = 2;
}

/**
+16 −0
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ import android.service.usb.UsbPortManagerProto;
import android.util.ArrayMap;
import android.util.Log;
import android.util.Slog;
import android.util.StatsLog;

import com.android.internal.annotations.GuardedBy;
import com.android.internal.util.IndentingPrintWriter;
@@ -54,6 +55,7 @@ import com.android.internal.util.dump.DualDumpOutputStream;
import com.android.server.FgThread;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.NoSuchElementException;

/**
@@ -117,6 +119,10 @@ public class UsbPortManager {
    private final ArrayMap<String, RawPortInfo> mSimulatedPorts =
            new ArrayMap<>();

    // Maintains the current connected status of the port.
    // Uploads logs only when the connection status is changes.
    private final HashMap<String, Boolean> mConnected = new HashMap<>();

    public UsbPortManager(Context context) {
        mContext = context;
        try {
@@ -700,6 +706,16 @@ public class UsbPortManager {
        // Guard against possible reentrance by posting the broadcast from the handler
        // instead of from within the critical section.
        mHandler.post(() -> mContext.sendBroadcastAsUser(intent, UserHandle.ALL));
        if (!mConnected.containsKey(portInfo.mUsbPort.getId())
                || (mConnected.get(portInfo.mUsbPort.getId())
                != portInfo.mUsbPortStatus.isConnected())) {
            mConnected.put(portInfo.mUsbPort.getId(), portInfo.mUsbPortStatus.isConnected());
            StatsLog.write(StatsLog.USB_CONNECTOR_STATE_CHANGED,
                    portInfo.mUsbPortStatus.isConnected()
                    ? StatsLog.USB_CONNECTOR_STATE_CHANGED__STATE__STATE_CONNECTED :
                    StatsLog.USB_CONNECTOR_STATE_CHANGED__STATE__STATE_DISCONNECTED,
                    portInfo.mUsbPort.getId());
        }
    }

    private static void logAndPrint(int priority, IndentingPrintWriter pw, String msg) {