diff options
| author | 2018-11-16 17:17:03 -0800 | |
|---|---|---|
| committer | 2018-12-04 12:14:12 -0800 | |
| commit | faf62071639165d2ef9aaabcd1c82cbb494b67e2 (patch) | |
| tree | b814a9dbda546779c6bb5f13baaff29797fb87ae | |
| parent | 223b3c7999eef531c3dc22998124f62ef15719cf (diff) | |
Log connection duration.
last_connect_duration_millis will be set to 0 for a connect event.
last_connect_duration_millis will be set to the amount of time the port
was left connected for a disconnect event.
Bug: 118783261
Test: Manually tested by running: adb shell cmd stats print-logs,
adb logcat | grep statsd | grep \(70\)
Change-Id: Ifd1df55fb6317e789a2b04236ce0a6e4665e3664
| -rw-r--r-- | cmds/statsd/src/atoms.proto | 3 | ||||
| -rw-r--r-- | core/proto/android/service/usb.proto | 2 | ||||
| -rw-r--r-- | services/usb/java/com/android/server/usb/UsbPortManager.java | 35 |
3 files changed, 35 insertions, 5 deletions
diff --git a/cmds/statsd/src/atoms.proto b/cmds/statsd/src/atoms.proto index 790a6d167b6e..d0596c4bc170 100644 --- a/cmds/statsd/src/atoms.proto +++ b/cmds/statsd/src/atoms.proto @@ -1264,6 +1264,9 @@ message UsbConnectorStateChanged { } optional State state = 1; optional string id = 2; + // Last active session in ms. + // 0 when the port is in connected state. + optional int64 last_connect_duration_millis = 3; } /** diff --git a/core/proto/android/service/usb.proto b/core/proto/android/service/usb.proto index ed040f43253a..f7dcee282a57 100644 --- a/core/proto/android/service/usb.proto +++ b/core/proto/android/service/usb.proto @@ -206,6 +206,8 @@ message UsbPortInfoProto { optional bool can_change_mode = 3; optional bool can_change_power_role = 4; optional bool can_change_data_role = 5; + optional int64 connected_at_millis = 6; + optional int64 last_connect_duration_millis = 7; } message UsbPortProto { diff --git a/services/usb/java/com/android/server/usb/UsbPortManager.java b/services/usb/java/com/android/server/usb/UsbPortManager.java index 89d4e632f892..96618f569928 100644 --- a/services/usb/java/com/android/server/usb/UsbPortManager.java +++ b/services/usb/java/com/android/server/usb/UsbPortManager.java @@ -41,6 +41,7 @@ import android.os.Message; import android.os.Parcel; import android.os.Parcelable; import android.os.RemoteException; +import android.os.SystemClock; import android.os.UserHandle; import android.service.usb.UsbPortInfoProto; import android.service.usb.UsbPortManagerProto; @@ -706,6 +707,8 @@ 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)); + + // Log to statsd if (!mConnected.containsKey(portInfo.mUsbPort.getId()) || (mConnected.get(portInfo.mUsbPort.getId()) != portInfo.mUsbPortStatus.isConnected())) { @@ -714,7 +717,7 @@ public class UsbPortManager { portInfo.mUsbPortStatus.isConnected() ? StatsLog.USB_CONNECTOR_STATE_CHANGED__STATE__STATE_CONNECTED : StatsLog.USB_CONNECTOR_STATE_CHANGED__STATE__STATE_DISCONNECTED, - portInfo.mUsbPort.getId()); + portInfo.mUsbPort.getId(), portInfo.mLastConnectDurationMillis); } } @@ -762,7 +765,12 @@ public class UsbPortManager { public boolean mCanChangeMode; public boolean mCanChangePowerRole; public boolean mCanChangeDataRole; - public int mDisposition; // default initialized to 0 which means added + // default initialized to 0 which means added + public int mDisposition; + // Tracks elapsedRealtime() of when the port was connected + public long mConnectedAtMillis; + // 0 when port is connected. Else reports the last connected duration + public long mLastConnectDurationMillis; public PortInfo(String portId, int supportedModes) { mUsbPort = new UsbPort(portId, supportedModes); @@ -772,6 +780,8 @@ public class UsbPortManager { int currentPowerRole, boolean canChangePowerRole, int currentDataRole, boolean canChangeDataRole, int supportedRoleCombinations) { + boolean dispositionChanged = false; + mCanChangeMode = canChangeMode; mCanChangePowerRole = canChangePowerRole; mCanChangeDataRole = canChangeDataRole; @@ -783,9 +793,18 @@ public class UsbPortManager { != supportedRoleCombinations) { mUsbPortStatus = new UsbPortStatus(currentMode, currentPowerRole, currentDataRole, supportedRoleCombinations); - return true; + dispositionChanged = true; } - return false; + + if (mUsbPortStatus.isConnected() && mConnectedAtMillis == 0) { + mConnectedAtMillis = SystemClock.elapsedRealtime(); + mLastConnectDurationMillis = 0; + } else if (!mUsbPortStatus.isConnected() && mConnectedAtMillis != 0) { + mLastConnectDurationMillis = SystemClock.elapsedRealtime() - mConnectedAtMillis; + mConnectedAtMillis = 0; + } + + return dispositionChanged; } void dump(@NonNull DualDumpOutputStream dump, @NonNull String idName, long id) { @@ -798,6 +817,10 @@ public class UsbPortManager { mCanChangePowerRole); dump.write("can_change_data_role", UsbPortInfoProto.CAN_CHANGE_DATA_ROLE, mCanChangeDataRole); + dump.write("connected_at_millis", + UsbPortInfoProto.CONNECTED_AT_MILLIS, mConnectedAtMillis); + dump.write("last_connect_duration_millis", + UsbPortInfoProto.LAST_CONNECT_DURATION_MILLIS, mLastConnectDurationMillis); dump.end(token); } @@ -807,7 +830,9 @@ public class UsbPortManager { return "port=" + mUsbPort + ", status=" + mUsbPortStatus + ", canChangeMode=" + mCanChangeMode + ", canChangePowerRole=" + mCanChangePowerRole - + ", canChangeDataRole=" + mCanChangeDataRole; + + ", canChangeDataRole=" + mCanChangeDataRole + + ", connectedAtMillis=" + mConnectedAtMillis + + ", lastConnectDurationMillis=" + mLastConnectDurationMillis; } } |