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

Commit 392b747d authored by Badhri Jagan Sridharan's avatar Badhri Jagan Sridharan
Browse files

UsbContaminant dialog

Bug: 119642987
Bug: 117330206
Bug: 77606903

Test: Ran the following sequence:
1. Add simulated port: dumpsys usb add-port "matrix" ufp
2. Set contaminant present to true:
   dumpsys usb set-contaminant-status "matrix" true
   Contaminant present notifcation shows up and dialog pops up upon
   clicking the notification.
3. Set contaminant present to false:
   dumpsys usb set-contaminant-status "matrix" false
   Contaminant present notification is dismissed and the safe to use
   notification shows up.

Change-Id: I33d22caafbedb2a21a0f61f663ebddf5c9e3f84b
parent 34d358c3
Loading
Loading
Loading
Loading
+9 −0
Original line number Diff line number Diff line
@@ -386,6 +386,15 @@
            android:excludeFromRecents="true">
        </activity>

        <!-- started from UsbPortManager -->
        <activity android:name=".usb.UsbContaminantActivity"
            android:exported="true"
            android:permission="android.permission.MANAGE_USB"
            android:theme="@style/Theme.SystemUI.Dialog.Alert"
            android:finishOnCloseSystemDialogs="true"
            android:excludeFromRecents="true">
        </activity>

        <!-- started from AdbDebuggingManager -->
        <activity android:name=".usb.UsbDebuggingActivity"
            android:permission="android.permission.MANAGE_DEBUGGING"
+6 −0
Original line number Diff line number Diff line
@@ -159,6 +159,12 @@
    <!-- Message of notification shown when trying to enable USB debugging but a secondary user is the current foreground user. -->
    <string name="usb_debugging_secondary_user_message">The user currently signed in to this device can\'t turn on USB debugging. To use this feature, switch to the primary user.</string>

    <!-- Title of USB contaminant presence dialog [CHAR LIMIT=NONE] -->
    <string name="usb_contaminant_title">USB port disabled</string>

    <!-- 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>

    <!-- 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>
+73 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.usb;

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.view.Window;
import android.view.WindowManager;

import com.android.internal.app.AlertActivity;
import com.android.internal.app.AlertController;
import com.android.systemui.R;

/**
 * Activity that alerts the user when contaminant is detected on USB port.
 */
public class UsbContaminantActivity extends AlertActivity
                                  implements DialogInterface.OnClickListener {
    private static final String TAG = "UsbContaminantActivity";

    private UsbDisconnectedReceiver mDisconnectedReceiver;
    private UsbPort mUsbPort;

    @Override
    public void onCreate(Bundle icicle) {
        Window window = getWindow();
        window.addSystemFlags(WindowManager.LayoutParams
                .SYSTEM_FLAG_HIDE_NON_SYSTEM_OVERLAY_WINDOWS);
        window.setType(WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG);

        super.onCreate(icicle);

        Intent intent = getIntent();
        ParcelableUsbPort port = intent.getParcelableExtra(UsbManager.EXTRA_PORT);
        mUsbPort = port.getUsbPort(getSystemService(UsbManager.class));

        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;

        setupAlert();
    }

    @Override
    public void onWindowAttributesChanged(WindowManager.LayoutParams params) {
        super.onWindowAttributesChanged(params);
    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        finish();
    }
}
+13 −1
Original line number Diff line number Diff line
@@ -33,6 +33,7 @@ import android.Manifest;
import android.annotation.NonNull;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
@@ -187,6 +188,8 @@ public class UsbPortManager {
        PortInfo currentPortInfo = null;
        Resources r = mContext.getResources();

        // 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) {
@@ -210,12 +213,22 @@ public class UsbPortManager {
            CharSequence message = r.getText(
                    com.android.internal.R.string.usb_contaminant_detected_message);

            Intent intent = new Intent();
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setClassName("com.android.systemui",
                    "com.android.systemui.usb.UsbContaminantActivity");
            intent.putExtra(UsbManager.EXTRA_PORT, ParcelableUsbPort.of(currentPortInfo.mUsbPort));

            PendingIntent pi = PendingIntent.getActivityAsUser(mContext, 0,
                                intent, 0, null, UserHandle.CURRENT);

            Notification.Builder builder = new Notification.Builder(mContext, channel)
                    .setOngoing(true)
                    .setTicker(title)
                    .setColor(mContext.getColor(
                           com.android.internal.R.color
                           .system_notification_accent_color))
                    .setContentIntent(pi)
                    .setContentTitle(title)
                    .setContentText(message)
                    .setVisibility(Notification.VISIBILITY_PUBLIC)
@@ -254,7 +267,6 @@ public class UsbPortManager {
        }
    }


    public UsbPort[] getPorts() {
        synchronized (mLock) {
            final int count = mPorts.size();