diff options
| author | 2019-03-25 17:17:18 +0000 | |
|---|---|---|
| committer | 2019-03-25 17:17:18 +0000 | |
| commit | c6479fd8f410dfb489a1f5cb45ff06cfcc3227a5 (patch) | |
| tree | 8ef751317499f66f9496fd7f185ade6396da2193 | |
| parent | fc394d1a350f5a452283603df0e31043dfcf9447 (diff) | |
| parent | 3df511319c2f8fe8b3184e46f9157e9996be1cac (diff) | |
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"
3 files changed, 94 insertions, 40 deletions
diff --git a/packages/SystemUI/res/values/strings.xml b/packages/SystemUI/res/values/strings.xml index 62309fdd2fcd..aca99e3cfae1 100644 --- a/packages/SystemUI/res/values/strings.xml +++ b/packages/SystemUI/res/values/strings.xml @@ -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> diff --git a/packages/SystemUI/src/com/android/systemui/usb/UsbContaminantActivity.java b/packages/SystemUI/src/com/android/systemui/usb/UsbContaminantActivity.java index fa4b3fe4be18..ecf608beb91c 100644 --- a/packages/SystemUI/src/com/android/systemui/usb/UsbContaminantActivity.java +++ b/packages/SystemUI/src/com/android/systemui/usb/UsbContaminantActivity.java @@ -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(); } } diff --git a/services/usb/java/com/android/server/usb/UsbPortManager.java b/services/usb/java/com/android/server/usb/UsbPortManager.java index ae05750eed9f..96e12ce837ca 100644 --- a/services/usb/java/com/android/server/usb/UsbPortManager.java +++ b/services/usb/java/com/android/server/usb/UsbPortManager.java @@ -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,32 +248,41 @@ 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 = 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; - CharSequence message = r.getText( - com.android.internal.R.string.usb_contaminant_not_detected_message); - - Notification.Builder builder = new Notification.Builder(mContext, channel) - .setSmallIcon(com.android.internal.R.drawable.ic_usb_48dp) - .setTicker(title) - .setColor(mContext.getColor( - com.android.internal.R.color - .system_notification_accent_color)) - .setContentTitle(title) - .setContentText(message) - .setVisibility(Notification.VISIBILITY_PUBLIC) - .setStyle(new Notification.BigTextStyle() - .bigText(message)); - Notification notification = builder.build(); - mNotificationManager.notifyAsUser(null, mIsPortContaminatedNotificationId, notification, - UserHandle.ALL); + mIsPortContaminatedNotificationId = 0; + + // 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; + CharSequence message = r.getText( + com.android.internal.R.string.usb_contaminant_not_detected_message); + + Notification.Builder builder = new Notification.Builder(mContext, channel) + .setSmallIcon(com.android.internal.R.drawable.ic_usb_48dp) + .setTicker(title) + .setColor(mContext.getColor( + com.android.internal.R.color + .system_notification_accent_color)) + .setContentTitle(title) + .setContentText(message) + .setVisibility(Notification.VISIBILITY_PUBLIC) + .setStyle(new Notification.BigTextStyle() + .bigText(message)); + Notification notification = builder.build(); + 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)); + } - // Log to statsd + private void enableContaminantDetectionIfNeeded(PortInfo portInfo, IndentingPrintWriter pw) { + if (!mConnected.containsKey(portInfo.mUsbPort.getId())) { + return; + } + + 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())) { |