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

Commit 26c490c5 authored by Badhri Jagan Sridharan's avatar Badhri Jagan Sridharan
Browse files

Support contaminant detection disable workflow

Do not show "Safe to use USB port" when contaminant detection is
disabled by the user.

Bug: 128534822
Test: Tested with the mock circuit.
Change-Id: Iebd12f04a6d8bfd7be5d673cf5a6742cf3d6f281
parent 397a98c8
Loading
Loading
Loading
Loading
+53 −32
Original line number Diff line number Diff line
@@ -150,8 +150,8 @@ public class UsbPortManager {
    private NotificationManager mNotificationManager;

    /**
     * If there currently is a notification about contaminated USB port shown the id of the
     * notification, or 0 if there is none.
     * If there currently is a notification related to contaminated USB port management
     * shown the id of the notification, or 0 if there is none.
     */
    private int mIsPortContaminatedNotificationId;

@@ -191,18 +191,24 @@ public class UsbPortManager {
    private void updateContaminantNotification() {
        PortInfo currentPortInfo = null;
        Resources r = mContext.getResources();
        int contaminantStatus = UsbPortStatus.CONTAMINANT_DETECTION_NOT_DETECTED;

        // Not handling multiple ports here. Showing the notification
        // for the first port that returns CONTAMINANT_PRESENCE_DETECTED.
        for (PortInfo portInfo : mPorts.values()) {
            if (portInfo.mUsbPortStatus.getContaminantDetectionStatus()
                    == UsbPortStatus.CONTAMINANT_DETECTION_DETECTED) {
            contaminantStatus = portInfo.mUsbPortStatus.getContaminantDetectionStatus();
            if (contaminantStatus == UsbPortStatus.CONTAMINANT_DETECTION_DETECTED
                    || contaminantStatus == UsbPortStatus.CONTAMINANT_DETECTION_DISABLED) {
                currentPortInfo = portInfo;
                break;
            }
        }

        if (currentPortInfo != null && mIsPortContaminatedNotificationId
        // Current contminant status is detected while "safe to use usb port"
        // notification is displayed. Remove safe to use usb port notification
        // and push contaminant detected notification.
        if (contaminantStatus == UsbPortStatus.CONTAMINANT_DETECTION_DETECTED
                    && mIsPortContaminatedNotificationId
                    != SystemMessage.NOTE_USB_CONTAMINANT_DETECTED) {
            if (mIsPortContaminatedNotificationId
                    == SystemMessage.NOTE_USB_CONTAMINANT_NOT_DETECTED) {
@@ -242,12 +248,20 @@ public class UsbPortManager {
            Notification notification = builder.build();
            mNotificationManager.notifyAsUser(null, mIsPortContaminatedNotificationId, notification,
                    UserHandle.ALL);
        } else if (currentPortInfo == null && mIsPortContaminatedNotificationId
        // No contaminant is detected but contaminant detection notification is displayed.
        // Remove contaminant detection notification and push safe to use USB port notification.
        } else if (contaminantStatus != UsbPortStatus.CONTAMINANT_DETECTION_DETECTED
                && mIsPortContaminatedNotificationId
                == SystemMessage.NOTE_USB_CONTAMINANT_DETECTED) {
            mNotificationManager.cancelAsUser(null, mIsPortContaminatedNotificationId,
                    UserHandle.ALL);
            mIsPortContaminatedNotificationId = 0;

            mIsPortContaminatedNotificationId = SystemMessage.NOTE_USB_CONTAMINANT_NOT_DETECTED;
            // Dont show safe to use notification when contaminant detection is disabled.
            // Show only when the status is changing from detected to not detected.
            if (contaminantStatus == UsbPortStatus.CONTAMINANT_DETECTION_NOT_DETECTED) {
                mIsPortContaminatedNotificationId =
                        SystemMessage.NOTE_USB_CONTAMINANT_NOT_DETECTED;
                int titleRes = com.android.internal.R.string.usb_contaminant_not_detected_title;
                CharSequence title = r.getText(titleRes);
                String channel = SystemNotificationChannels.ALERTS;
@@ -266,8 +280,9 @@ public class UsbPortManager {
                        .setStyle(new Notification.BigTextStyle()
                        .bigText(message));
                Notification notification = builder.build();
            mNotificationManager.notifyAsUser(null, mIsPortContaminatedNotificationId, notification,
                    UserHandle.ALL);
                mNotificationManager.notifyAsUser(null, mIsPortContaminatedNotificationId,
                        notification, UserHandle.ALL);
            }
        }
    }

@@ -319,8 +334,8 @@ public class UsbPortManager {
        }

        try {
            // Oneway call into the hal
            android.hardware.usb.V1_2.IUsb proxy = (android.hardware.usb.V1_2.IUsb) mProxy;
            // Oneway call into the hal. Use the castFrom method from HIDL.
            android.hardware.usb.V1_2.IUsb proxy = android.hardware.usb.V1_2.IUsb.castFrom(mProxy);
            proxy.enableContaminantPresenceDetection(portId, enable);
        } catch (RemoteException e) {
            logAndPrintException(pw, "Failed to set contaminant detection", e);
@@ -950,7 +965,7 @@ public class UsbPortManager {

    private void handlePortLocked(PortInfo portInfo, IndentingPrintWriter pw) {
        sendPortChangedBroadcastLocked(portInfo);
        logToStatsd(portInfo);
        logToStatsd(portInfo, pw);
        updateContaminantNotification();
    }

@@ -1001,7 +1016,7 @@ public class UsbPortManager {
                Manifest.permission.MANAGE_USB));
    }

    private void logToStatsd(PortInfo portInfo) {
    private void logToStatsd(PortInfo portInfo, IndentingPrintWriter pw) {
        // Port is removed
        if (portInfo.mUsbPortStatus == null) {
            if (mConnected.containsKey(portInfo.mUsbPort.getId())) {
@@ -1037,6 +1052,12 @@ public class UsbPortManager {
                    ? StatsLog.USB_CONNECTOR_STATE_CHANGED__STATE__STATE_CONNECTED :
                    StatsLog.USB_CONNECTOR_STATE_CHANGED__STATE__STATE_DISCONNECTED,
                    portInfo.mUsbPort.getId(), portInfo.mLastConnectDurationMillis);
            // Contaminant detection might have been temporarily disabled by the user
            // through SystemUI.
            // Re-enable contaminant detection when the accessory is unplugged.
            if (!portInfo.mUsbPortStatus.isConnected()) {
                enableContaminantDetection(portInfo.mUsbPort.getId(), true, pw);
            }
        }

        if (!mContaminantStatus.containsKey(portInfo.mUsbPort.getId())