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

Commit c6479fd8 authored by Badhri Jagan Sridharan's avatar Badhri Jagan Sridharan Committed by Android (Google) Code Review
Browse files

Merge changes from topic "128534822"

* changes:
  UsbPortManager: Re-enable contaminant detection when port is unplugged
  Support contaminant detection disable workflow
  Refactor actions that are needed to be taken during port status changes.
  Add option to "Enable USB"
parents fc394d1a 3df51131
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -165,6 +165,12 @@
    <!-- Message of USB contaminant presence dialog [CHAR LIMIT=NONE] -->
    <string name="usb_contaminant_message">To protect your device from liquid or debris, the USB port is disabled and won\u2019t detect any accessories.\n\nYou\u2019ll be notified when it\u2019s safe to use the USB port again.</string>

    <!-- Toast for enabling ports from USB contaminant dialog [CHAR LIMIT=NONE] -->
    <string name="usb_port_enabled">USB port enabled to detect chargers and accessories</string>

    <!-- Button text to disable contaminant detection [CHAR LIMIT=NONE] -->
    <string name="usb_disable_contaminant_detection">Enable USB</string>

    <!-- Checkbox label for application compatibility mode ON (zooming app to look like it's running
         on a phone).  [CHAR LIMIT=25] -->
    <string name="compat_mode_on">Zoom to fill screen</string>
+16 −3
Original line number Diff line number Diff line
@@ -16,14 +16,17 @@

package com.android.systemui.usb;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.hardware.usb.ParcelableUsbPort;
import android.hardware.usb.UsbManager;
import android.hardware.usb.UsbPort;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;

import com.android.internal.app.AlertActivity;
import com.android.internal.app.AlertController;
@@ -36,7 +39,6 @@ public class UsbContaminantActivity extends AlertActivity
                                  implements DialogInterface.OnClickListener {
    private static final String TAG = "UsbContaminantActivity";

    private UsbDisconnectedReceiver mDisconnectedReceiver;
    private UsbPort mUsbPort;

    @Override
@@ -55,8 +57,10 @@ public class UsbContaminantActivity extends AlertActivity
        final AlertController.AlertParams ap = mAlertParams;
        ap.mTitle = getString(R.string.usb_contaminant_title);
        ap.mMessage = getString(R.string.usb_contaminant_message);
        ap.mPositiveButtonText = getString(android.R.string.ok);
        ap.mPositiveButtonListener = this;
        ap.mNegativeButtonText = getString(android.R.string.ok);
        ap.mNeutralButtonText = getString(R.string.usb_disable_contaminant_detection);
        ap.mNegativeButtonListener = this;
        ap.mNeutralButtonListener = this;

        setupAlert();
    }
@@ -68,6 +72,15 @@ public class UsbContaminantActivity extends AlertActivity

    @Override
    public void onClick(DialogInterface dialog, int which) {
        if (which == AlertDialog.BUTTON_NEUTRAL) {
            try {
                mUsbPort.enableContaminantDetection(false);
                Toast.makeText(this, R.string.usb_port_enabled,
                    Toast.LENGTH_SHORT).show();
            } catch (Exception e) {
                Log.e(TAG, "Unable to notify Usb service", e);
            }
        }
        finish();
    }
}
+72 −37
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);
@@ -948,22 +963,26 @@ public class UsbPortManager {
        }
    }

    private void handlePortAddedLocked(PortInfo portInfo, IndentingPrintWriter pw) {
        logAndPrint(Log.INFO, pw, "USB port added: " + portInfo);
    private void handlePortLocked(PortInfo portInfo, IndentingPrintWriter pw) {
        sendPortChangedBroadcastLocked(portInfo);
        enableContaminantDetectionIfNeeded(portInfo, pw);
        logToStatsd(portInfo, pw);
        updateContaminantNotification();
    }

    private void handlePortAddedLocked(PortInfo portInfo, IndentingPrintWriter pw) {
        logAndPrint(Log.INFO, pw, "USB port added: " + portInfo);
        handlePortLocked(portInfo, pw);
    }

    private void handlePortChangedLocked(PortInfo portInfo, IndentingPrintWriter pw) {
        logAndPrint(Log.INFO, pw, "USB port changed: " + portInfo);
        sendPortChangedBroadcastLocked(portInfo);
        updateContaminantNotification();
        handlePortLocked(portInfo, pw);
    }

    private void handlePortRemovedLocked(PortInfo portInfo, IndentingPrintWriter pw) {
        logAndPrint(Log.INFO, pw, "USB port removed: " + portInfo);
        sendPortChangedBroadcastLocked(portInfo);
        updateContaminantNotification();
        handlePortLocked(portInfo, pw);
    }

    // Constants have to be converted between USB HAL V1.2 ContaminantDetectionStatus
@@ -996,9 +1015,25 @@ public class UsbPortManager {
        // instead of from within the critical section.
        mHandler.post(() -> mContext.sendBroadcastAsUser(intent, UserHandle.ALL,
                Manifest.permission.MANAGE_USB));
    }

    private void enableContaminantDetectionIfNeeded(PortInfo portInfo, IndentingPrintWriter pw) {
        if (!mConnected.containsKey(portInfo.mUsbPort.getId())) {
            return;
        }

        // Log to statsd
        if (mConnected.get(portInfo.mUsbPort.getId())
                && !portInfo.mUsbPortStatus.isConnected()
                && portInfo.mUsbPortStatus.getContaminantDetectionStatus()
                == UsbPortStatus.CONTAMINANT_DETECTION_DISABLED) {
            // Contaminant detection might have been temporarily disabled by the user
            // through SystemUI.
            // Re-enable contaminant detection when the accessory is unplugged.
            enableContaminantDetection(portInfo.mUsbPort.getId(), true, pw);
        }
    }

    private void logToStatsd(PortInfo portInfo, IndentingPrintWriter pw) {
        // Port is removed
        if (portInfo.mUsbPortStatus == null) {
            if (mConnected.containsKey(portInfo.mUsbPort.getId())) {