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

Commit 19f4d1a9 authored by Mukta Kulkarni's avatar Mukta Kulkarni Committed by Android (Google) Code Review
Browse files

Merge "Allow csdWarning Dialog notification to trigger either an activity or...

Merge "Allow csdWarning Dialog notification to trigger either an activity or broadcast depending on the action intents provided" into main
parents b4424cf1 c7182bb1
Loading
Loading
Loading
Loading
+48 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.volume

import android.app.PendingIntent
import android.app.PendingIntent.FLAG_IMMUTABLE
import android.app.PendingIntent.FLAG_UPDATE_CURRENT
import android.content.Context
import android.content.Intent

/**
 * label: Notification action label text. intent: The Intent used to start Activity or Broadcast.
 * isActivity: Defines if the pending intent should start an activity. Default is to broadcast
 */
data class CsdWarningAction(
    val label: String? = null,
    val intent: Intent? = null,
    val isActivity: Boolean = false,
) {
    fun toPendingIntent(context: Context): PendingIntent? {
        if (label == null || intent == null) {
            return null
        }
        if (isActivity) {
            return PendingIntent.getActivity(
                context,
                0,
                intent,
                PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
            )
        }
        return PendingIntent.getBroadcast(context, 0, intent, FLAG_IMMUTABLE)
    }
}
+42 −37
Original line number Diff line number Diff line
@@ -30,7 +30,6 @@ import android.content.IntentFilter;
import android.media.AudioManager;
import android.provider.Settings;
import android.util.Log;
import android.util.Pair;
import android.view.KeyEvent;
import android.view.WindowManager;

@@ -109,7 +108,7 @@ public class CsdWarningDialog extends SystemUIDialog
    private long mShowTime;

    @VisibleForTesting public int mCachedMediaStreamVolume;
    private Optional<ImmutableList<Pair<String, Intent>>> mActionIntents;
    private Optional<ImmutableList<CsdWarningAction>> mActionIntents;
    private final BroadcastDispatcher mBroadcastDispatcher;

    /**
@@ -121,7 +120,7 @@ public class CsdWarningDialog extends SystemUIDialog
        CsdWarningDialog create(
                int csdWarning,
                Runnable onCleanup,
                Optional<ImmutableList<Pair<String, Intent>>> actionIntents);
                Optional<ImmutableList<CsdWarningAction>> actionIntents);
    }

    @AssistedInject
@@ -132,7 +131,7 @@ public class CsdWarningDialog extends SystemUIDialog
            NotificationManager notificationManager,
            @Background DelayableExecutor delayableExecutor,
            @Assisted Runnable onCleanup,
            @Assisted Optional<ImmutableList<Pair<String, Intent>>> actionIntents,
            @Assisted Optional<ImmutableList<CsdWarningAction>> actionIntents,
            BroadcastDispatcher broadcastDispatcher) {
        super(context);
        mCsdWarning = csdWarning;
@@ -351,16 +350,22 @@ public class CsdWarningDialog extends SystemUIDialog
        if (Flags.sounddoseCustomization()
                && mActionIntents.isPresent()
                && !mActionIntents.get().isEmpty()) {
            ImmutableList<Pair<String, Intent>> actionIntentsList = mActionIntents.get();
            for (Pair<String, Intent> intentPair : actionIntentsList) {
                if (intentPair != null && intentPair.first != null && intentPair.second != null) {
                    PendingIntent pendingActionIntent =
                            PendingIntent.getBroadcast(mContext, 0, intentPair.second,
                                    FLAG_IMMUTABLE);
                    builder.addAction(0, intentPair.first, pendingActionIntent);
            ImmutableList<CsdWarningAction> actionIntentsList = mActionIntents.get();
            for (CsdWarningAction action : actionIntentsList) {
                if (action.getLabel() == null || action.getIntent() == null) {
                    Log.w(TAG, "Null action intent received. Skipping addition to notification");
                    continue;
                }
                PendingIntent pendingActionIntent = action.toPendingIntent(mContext);
                if (pendingActionIntent == null) {
                    Log.w(TAG, "Null pending intent received. Skipping addition to notification");
                    continue;
                }
                builder.addAction(0, action.getLabel(), pendingActionIntent);

                // Register receiver to undo volume only when
                // notification conaining the undo action would be sent.
                    if (intentPair.first == mContext.getString(R.string.volume_undo_action)) {
                if (action.getLabel().equals(mContext.getString(R.string.volume_undo_action))) {
                    final IntentFilter filterUndo = new IntentFilter(
                            VolumeDialog.ACTION_VOLUME_UNDO);
                    mBroadcastDispatcher.registerReceiver(mReceiverUndo,
@@ -374,7 +379,8 @@ public class CsdWarningDialog extends SystemUIDialog
                    // This is required to unregister receivers to prevent leak.
                    Intent dismissIntent = new Intent(DISMISS_CSD_NOTIFICATION)
                            .setPackage(mContext.getPackageName());
                        PendingIntent pendingDismissIntent = PendingIntent.getBroadcast(mContext,
                    PendingIntent pendingDismissIntent = PendingIntent.getBroadcast(
                            mContext,
                            0, dismissIntent, FLAG_IMMUTABLE);
                    mBroadcastDispatcher.registerReceiver(mReceiverDismissNotification,
                            new IntentFilter(DISMISS_CSD_NOTIFICATION),
@@ -386,7 +392,6 @@ public class CsdWarningDialog extends SystemUIDialog
                }
            }
        }
        }

        mNotificationManager.notify(SystemMessageProto.SystemMessage.NOTE_CSD_LOWER_AUDIO,
                builder.build());
+3 −5
Original line number Diff line number Diff line
@@ -51,7 +51,6 @@ import android.app.KeyguardManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.ColorStateList;
import android.content.res.Configuration;
@@ -79,7 +78,6 @@ import android.provider.Settings;
import android.provider.Settings.Global;
import android.text.InputFilter;
import android.util.Log;
import android.util.Pair;
import android.util.Slog;
import android.util.SparseBooleanArray;
import android.view.ContextThemeWrapper;
@@ -322,8 +320,8 @@ public class VolumeDialogImpl implements VolumeDialog, Dumpable,
    private final VolumePanelFlag mVolumePanelFlag;
    private final VolumeDialogInteractor mInteractor;
    // Optional actions for soundDose
    private Optional<ImmutableList<Pair<String, Intent>>> mCsdWarningNotificationActions =
            Optional.of(ImmutableList.of());
    private Optional<ImmutableList<CsdWarningAction>>
            mCsdWarningNotificationActions = Optional.of(ImmutableList.of());

    public VolumeDialogImpl(
            Context context,
@@ -2231,7 +2229,7 @@ public class VolumeDialogImpl implements VolumeDialog, Dumpable,
    }

    public void setCsdWarningNotificationActionIntents(
            ImmutableList<Pair<String, Intent>> actionIntent) {
            ImmutableList<CsdWarningAction> actionIntent) {
        mCsdWarningNotificationActions = Optional.of(actionIntent);
    }

+6 −5
Original line number Diff line number Diff line
@@ -36,7 +36,6 @@ import android.content.pm.ResolveInfo;
import android.media.AudioManager;
import android.platform.test.annotations.EnableFlags;
import android.testing.TestableLooper;
import android.util.Pair;

import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;
@@ -70,6 +69,8 @@ public class CsdWarningDialogTest extends SysuiTestCase {
    private CsdWarningDialog mDialog;
    private static final String DISMISS_CSD_NOTIFICATION =
            "com.android.systemui.volume.DISMISS_CSD_NOTIFICATION";
    private final Optional<ImmutableList<CsdWarningAction>> mEmptyActions =
            Optional.of(ImmutableList.of());

    @Before
    public void setup() {
@@ -87,7 +88,7 @@ public class CsdWarningDialogTest extends SysuiTestCase {
        // instantiate directly instead of via factory; we don't want executor to be @Background
        mDialog = new CsdWarningDialog(CSD_WARNING_DOSE_REACHED_1X, mContext,
                mAudioManager, mNotificationManager, executor, null,
                Optional.of(ImmutableList.of(new Pair("", new Intent()))),
                mEmptyActions,
                mFakeBroadcastDispatcher);

        mDialog.show();
@@ -104,7 +105,7 @@ public class CsdWarningDialogTest extends SysuiTestCase {
        FakeExecutor executor =  new FakeExecutor(new FakeSystemClock());
        mDialog = new CsdWarningDialog(CSD_WARNING_DOSE_REPEATED_5X, mContext,
                mAudioManager, mNotificationManager, executor, null,
                Optional.of(ImmutableList.of(new Pair("", new Intent()))),
                mEmptyActions,
                mFakeBroadcastDispatcher);

        mDialog.show();
@@ -121,7 +122,7 @@ public class CsdWarningDialogTest extends SysuiTestCase {
                .setPackage(mContext.getPackageName());
        mDialog = new CsdWarningDialog(CSD_WARNING_DOSE_REPEATED_5X, mContext,
                mAudioManager, mNotificationManager, executor, null,
                Optional.of(ImmutableList.of(new Pair("Undo", undoIntent))),
                Optional.of(ImmutableList.of(new CsdWarningAction("Undo", undoIntent, false))),
                mFakeBroadcastDispatcher);

        when(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)).thenReturn(25);
@@ -148,7 +149,7 @@ public class CsdWarningDialogTest extends SysuiTestCase {
                .setPackage(mContext.getPackageName());
        mDialog = new CsdWarningDialog(CSD_WARNING_DOSE_REPEATED_5X, mContext,
                mAudioManager, mNotificationManager, executor, null,
                Optional.of(ImmutableList.of(new Pair("Undo", undoIntent))),
                Optional.of(ImmutableList.of(new CsdWarningAction("Undo", undoIntent, false))),
                mFakeBroadcastDispatcher);
        Intent dismissIntent = new Intent(DISMISS_CSD_NOTIFICATION)
                .setPackage(mContext.getPackageName());
+1 −3
Original line number Diff line number Diff line
@@ -44,7 +44,6 @@ import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import android.app.KeyguardManager;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Canvas;
@@ -57,7 +56,6 @@ import android.platform.test.annotations.EnableFlags;
import android.provider.Settings;
import android.testing.TestableLooper;
import android.util.Log;
import android.util.Pair;
import android.view.Gravity;
import android.view.InputDevice;
import android.view.MotionEvent;
@@ -166,7 +164,7 @@ public class VolumeDialogImplTest extends SysuiTestCase {
            new CsdWarningDialog.Factory() {
                @Override
                public CsdWarningDialog create(int warningType, Runnable onCleanup,
                        Optional<ImmutableList<Pair<String, Intent>>> actionIntents) {
                        Optional<ImmutableList<CsdWarningAction>> actionIntents) {
                    return mCsdWarningDialog;
                }
            };