diff options
| -rw-r--r-- | services/usb/java/com/android/server/usb/UsbPortManager.java | 85 |
1 files changed, 53 insertions, 32 deletions
diff --git a/services/usb/java/com/android/server/usb/UsbPortManager.java b/services/usb/java/com/android/server/usb/UsbPortManager.java index b972b5f10a39..aec85d37bf09 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); @@ -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()) |