Sounddose custom actions for notifications
Flag: com.android.systemui.sounddose_customization
Test: Manual took video
Change-Id: I21bbfb1bb5bfe5dfedce9d88bf652d8b1fb1ea91
Bug: 345227709
diff --git a/packages/SystemUI/aconfig/systemui.aconfig b/packages/SystemUI/aconfig/systemui.aconfig
index 28e88d2..6e6e4b2 100644
--- a/packages/SystemUI/aconfig/systemui.aconfig
+++ b/packages/SystemUI/aconfig/systemui.aconfig
@@ -1163,3 +1163,11 @@
purpose: PURPOSE_BUGFIX
}
}
+
+flag {
+ name: "sounddose_customization"
+ namespace: "systemui"
+ description: "Enables custom actions for sounddose notifications"
+ bug: "345227709"
+}
+
diff --git a/packages/SystemUI/plugin/src/com/android/systemui/plugins/VolumeDialog.java b/packages/SystemUI/plugin/src/com/android/systemui/plugins/VolumeDialog.java
index 9e5db73..cd86402 100644
--- a/packages/SystemUI/plugin/src/com/android/systemui/plugins/VolumeDialog.java
+++ b/packages/SystemUI/plugin/src/com/android/systemui/plugins/VolumeDialog.java
@@ -26,6 +26,7 @@
@DependsOn(target = Callback.class)
public interface VolumeDialog extends Plugin {
String ACTION = "com.android.systemui.action.PLUGIN_VOLUME";
+ String ACTION_VOLUME_UNDO = "com.android.systemui.volume.ACTION_VOLUME_UNDO";
int VERSION = 1;
void init(int windowType, Callback callback);
diff --git a/packages/SystemUI/res/values/strings.xml b/packages/SystemUI/res/values/strings.xml
index a6dd14a..9f1f1a0 100644
--- a/packages/SystemUI/res/values/strings.xml
+++ b/packages/SystemUI/res/values/strings.xml
@@ -3656,4 +3656,6 @@
<string name="home_controls_dream_label">Home Controls</string>
<!-- Description for home control panel [CHAR LIMIT=67] -->
<string name="home_controls_dream_description">Quickly access your home controls as a screensaver</string>
+ <!-- Label for volume undo action [CHAR LIMIT=NONE] -->
+ <string name="volume_undo_action">Undo</string>
</resources>
diff --git a/packages/SystemUI/src/com/android/systemui/volume/CsdWarningDialog.java b/packages/SystemUI/src/com/android/systemui/volume/CsdWarningDialog.java
index bd698ab..bb230e6 100644
--- a/packages/SystemUI/src/com/android/systemui/volume/CsdWarningDialog.java
+++ b/packages/SystemUI/src/com/android/systemui/volume/CsdWarningDialog.java
@@ -30,21 +30,31 @@
import android.media.AudioManager;
import android.provider.Settings;
import android.util.Log;
+import android.util.Pair;
import android.view.KeyEvent;
import android.view.WindowManager;
+import androidx.annotation.VisibleForTesting;
+
import com.android.internal.annotations.GuardedBy;
import com.android.internal.messages.nano.SystemMessageProto;
+import com.android.systemui.Flags;
+import com.android.systemui.broadcast.BroadcastDispatcher;
import com.android.systemui.dagger.qualifiers.Background;
+import com.android.systemui.plugins.VolumeDialog;
import com.android.systemui.res.R;
import com.android.systemui.statusbar.phone.SystemUIDialog;
import com.android.systemui.util.NotificationChannels;
import com.android.systemui.util.concurrency.DelayableExecutor;
+import com.google.common.collect.ImmutableList;
+
import dagger.assisted.Assisted;
import dagger.assisted.AssistedFactory;
import dagger.assisted.AssistedInject;
+import java.util.Optional;
+
/**
* A class that implements the three Computed Sound Dose-related warnings defined in
* {@link AudioManager}:
@@ -75,6 +85,9 @@
private static final int KEY_CONFIRM_ALLOWED_AFTER_MS = 1000; // milliseconds
// time after which action is taken when the user hasn't ack'd or dismissed the dialog
public static final int NO_ACTION_TIMEOUT_MS = 5000;
+ // Notification dismiss intent
+ private static final String DISMISS_CSD_NOTIFICATION =
+ "com.android.systemui.volume.DISMISS_CSD_NOTIFICATION";
private final Context mContext;
private final AudioManager mAudioManager;
@@ -95,21 +108,32 @@
private long mShowTime;
+ @VisibleForTesting public int mCachedMediaStreamVolume;
+ private Optional<ImmutableList<Pair<String, Intent>>> mActionIntents;
+ private final BroadcastDispatcher mBroadcastDispatcher;
+
/**
* To inject dependencies and allow for easier testing
*/
@AssistedFactory
public interface Factory {
- /**
- * Create a dialog object
- */
- CsdWarningDialog create(int csdWarning, Runnable onCleanup);
+ /** Create a dialog object */
+ CsdWarningDialog create(
+ int csdWarning,
+ Runnable onCleanup,
+ Optional<ImmutableList<Pair<String, Intent>>> actionIntents);
}
@AssistedInject
- public CsdWarningDialog(@Assisted @AudioManager.CsdWarning int csdWarning, Context context,
- AudioManager audioManager, NotificationManager notificationManager,
- @Background DelayableExecutor delayableExecutor, @Assisted Runnable onCleanup) {
+ public CsdWarningDialog(
+ @Assisted @AudioManager.CsdWarning int csdWarning,
+ Context context,
+ AudioManager audioManager,
+ NotificationManager notificationManager,
+ @Background DelayableExecutor delayableExecutor,
+ @Assisted Runnable onCleanup,
+ @Assisted Optional<ImmutableList<Pair<String, Intent>>> actionIntents,
+ BroadcastDispatcher broadcastDispatcher) {
super(context);
mCsdWarning = csdWarning;
mContext = context;
@@ -118,7 +142,8 @@
mOnCleanup = onCleanup;
mDelayableExecutor = delayableExecutor;
-
+ mActionIntents = actionIntents;
+ mBroadcastDispatcher = broadcastDispatcher;
getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ERROR);
setShowForAllUsers(true);
setMessage(mContext.getString(getStringForWarning(csdWarning)));
@@ -133,14 +158,17 @@
Context.RECEIVER_EXPORTED_UNAUDITED);
if (csdWarning == AudioManager.CSD_WARNING_DOSE_REACHED_1X) {
- mNoUserActionRunnable = () -> {
- if (mCsdWarning == AudioManager.CSD_WARNING_DOSE_REACHED_1X) {
- // unlike on the 5x dose repeat, level is only reduced to RS1 when the warning
- // is not acknowledged quickly enough
- mAudioManager.lowerVolumeToRs1();
- sendNotification(/*for5XCsd=*/false);
- }
- };
+ mNoUserActionRunnable =
+ () -> {
+ if (mCsdWarning == AudioManager.CSD_WARNING_DOSE_REACHED_1X) {
+ // unlike on the 5x dose repeat, level is only reduced to RS1 when the
+ // warning is not acknowledged quickly enough
+ mCachedMediaStreamVolume =
+ mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
+ mAudioManager.lowerVolumeToRs1();
+ sendNotification(/* for5XCsd= */ false);
+ }
+ };
} else {
mNoUserActionRunnable = null;
}
@@ -242,6 +270,38 @@
}
};
+ @VisibleForTesting
+ public final BroadcastReceiver mReceiverUndo =
+ new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ if (Flags.sounddoseCustomization()
+ && VolumeDialog.ACTION_VOLUME_UNDO.equals(intent.getAction())) {
+ if (D.BUG) Log.d(TAG, "Received ACTION_VOLUME_UNDO");
+ mAudioManager.setStreamVolume(
+ AudioManager.STREAM_MUSIC,
+ mCachedMediaStreamVolume,
+ AudioManager.FLAG_SHOW_UI);
+ mNotificationManager.cancel(
+ SystemMessageProto.SystemMessage.NOTE_CSD_LOWER_AUDIO);
+ destroy();
+ }
+ }
+ };
+
+ @VisibleForTesting
+ public final BroadcastReceiver mReceiverDismissNotification =
+ new BroadcastReceiver() {
+ @Override
+ public void onReceive(Context context, Intent intent) {
+ if (Flags.sounddoseCustomization()
+ && DISMISS_CSD_NOTIFICATION.equals(intent.getAction())) {
+ if (D.BUG) Log.d(TAG, "Received DISMISS_CSD_NOTIFICATION");
+ destroy();
+ }
+ }
+ };
+
private @StringRes int getStringForWarning(@AudioManager.CsdWarning int csdWarning) {
switch (csdWarning) {
case AudioManager.CSD_WARNING_DOSE_REACHED_1X:
@@ -259,7 +319,7 @@
Log.w(TAG, "Notification dose repeat 5x is not shown for " + mCsdWarning);
return;
}
-
+ mCachedMediaStreamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mAudioManager.lowerVolumeToRs1();
sendNotification(/*for5XCsd=*/true);
}
@@ -288,7 +348,62 @@
.setAutoCancel(true)
.setCategory(Notification.CATEGORY_SYSTEM);
+ 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);
+ // 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)) {
+ final IntentFilter filterUndo = new IntentFilter(
+ VolumeDialog.ACTION_VOLUME_UNDO);
+ mBroadcastDispatcher.registerReceiver(mReceiverUndo,
+ filterUndo,
+ /* executor = default */ null,
+ /* user = default */ null,
+ Context.RECEIVER_NOT_EXPORTED,
+ /* permission = default */ null);
+
+ // Register receiver to learn if notification has been dismissed.
+ // This is required to unregister receivers to prevent leak.
+ Intent dismissIntent = new Intent(DISMISS_CSD_NOTIFICATION)
+ .setPackage(mContext.getPackageName());
+ PendingIntent pendingDismissIntent = PendingIntent.getBroadcast(mContext,
+ 0, dismissIntent, FLAG_IMMUTABLE);
+ mBroadcastDispatcher.registerReceiver(mReceiverDismissNotification,
+ new IntentFilter(DISMISS_CSD_NOTIFICATION),
+ /* executor = default */ null,
+ /* user = default */ null,
+ Context.RECEIVER_NOT_EXPORTED,
+ /* permission = default */ null);
+ builder.setDeleteIntent(pendingDismissIntent);
+ }
+ }
+ }
+ }
+
mNotificationManager.notify(SystemMessageProto.SystemMessage.NOTE_CSD_LOWER_AUDIO,
builder.build());
}
+
+ /**
+ * Unregister the Undo action receiver when the notification is dismissed.
+ * Unregister cannot happen when CsdWarning dialog is dismissed
+ * as the notification lifecycle would be longer.
+ */
+ @VisibleForTesting
+ public void destroy() {
+ if (Flags.sounddoseCustomization()
+ && mActionIntents.isPresent()
+ && !mActionIntents.get().isEmpty()) {
+ mBroadcastDispatcher.unregisterReceiver(mReceiverUndo);
+ mBroadcastDispatcher.unregisterReceiver(mReceiverDismissNotification);
+ }
+ }
}
diff --git a/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogImpl.java b/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogImpl.java
index 795d427..6b02e1a 100644
--- a/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogImpl.java
+++ b/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogImpl.java
@@ -50,6 +50,7 @@
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;
@@ -77,6 +78,7 @@
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;
@@ -140,11 +142,14 @@
import com.android.systemui.volume.panel.shared.flag.VolumePanelFlag;
import com.android.systemui.volume.ui.navigation.VolumeNavigator;
+import com.google.common.collect.ImmutableList;
+
import dagger.Lazy;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
+import java.util.Optional;
import java.util.function.Consumer;
/**
@@ -315,6 +320,9 @@
private final com.android.systemui.util.time.SystemClock mSystemClock;
private final VolumePanelFlag mVolumePanelFlag;
private final VolumeDialogInteractor mInteractor;
+ // Optional actions for soundDose
+ private Optional<ImmutableList<Pair<String, Intent>>> mCsdWarningNotificationActions =
+ Optional.of(ImmutableList.of());
public VolumeDialogImpl(
Context context,
@@ -2198,6 +2206,11 @@
rescheduleTimeoutH();
}
+ public void setCsdWarningNotificationActionIntents(
+ ImmutableList<Pair<String, Intent>> actionIntent) {
+ mCsdWarningNotificationActions = Optional.of(actionIntent);
+ }
+
@VisibleForTesting void showCsdWarningH(int csdWarning, int durationMs) {
synchronized (mSafetyWarningLock) {
@@ -2212,7 +2225,8 @@
recheckH(null);
};
- mCsdDialog = mCsdWarningDialogFactory.create(csdWarning, cleanUp);
+ mCsdDialog = mCsdWarningDialogFactory.create(
+ csdWarning, cleanUp, mCsdWarningNotificationActions);
mCsdDialog.show();
}
recheckH(null);
diff --git a/packages/SystemUI/tests/src/com/android/systemui/volume/CsdWarningDialogTest.java b/packages/SystemUI/tests/src/com/android/systemui/volume/CsdWarningDialogTest.java
index c81623e..49aedcc 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/volume/CsdWarningDialogTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/volume/CsdWarningDialogTest.java
@@ -19,28 +19,46 @@
import static android.media.AudioManager.CSD_WARNING_DOSE_REACHED_1X;
import static android.media.AudioManager.CSD_WARNING_DOSE_REPEATED_5X;
+import static com.google.common.truth.Truth.assertThat;
+
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
import android.app.Notification;
import android.app.NotificationManager;
+import android.content.Intent;
+import android.content.pm.PackageManager;
+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;
import com.android.internal.messages.nano.SystemMessageProto;
+import com.android.systemui.Flags;
import com.android.systemui.SysuiTestCase;
+import com.android.systemui.broadcast.BroadcastDispatcher;
+import com.android.systemui.plugins.VolumeDialog;
import com.android.systemui.util.concurrency.FakeExecutor;
import com.android.systemui.util.time.FakeSystemClock;
+import com.google.common.collect.ImmutableList;
+
+import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import java.util.List;
+import java.util.Optional;
+
@SmallTest
@RunWith(AndroidJUnit4.class)
@TestableLooper.RunWithLooper
@@ -48,41 +66,109 @@
private NotificationManager mNotificationManager;
private AudioManager mAudioManager;
+ private BroadcastDispatcher mFakeBroadcastDispatcher;
+ private CsdWarningDialog mDialog;
+ private static final String DISMISS_CSD_NOTIFICATION =
+ "com.android.systemui.volume.DISMISS_CSD_NOTIFICATION";
@Before
public void setup() {
mNotificationManager = mock(NotificationManager.class);
- getContext().addMockSystemService(NotificationManager.class, mNotificationManager);
+ mContext.addMockSystemService(NotificationManager.class, mNotificationManager);
mAudioManager = mock(AudioManager.class);
- getContext().addMockSystemService(AudioManager.class, mAudioManager);
+ mContext.addMockSystemService(AudioManager.class, mAudioManager);
+ mFakeBroadcastDispatcher = getFakeBroadcastDispatcher();
}
@Test
public void create1XCsdDialogAndWait_sendsNotification() {
FakeExecutor executor = new FakeExecutor(new FakeSystemClock());
// instantiate directly instead of via factory; we don't want executor to be @Background
- CsdWarningDialog dialog = new CsdWarningDialog(CSD_WARNING_DOSE_REACHED_1X, mContext,
- mAudioManager, mNotificationManager, executor, null);
+ mDialog = new CsdWarningDialog(CSD_WARNING_DOSE_REACHED_1X, mContext,
+ mAudioManager, mNotificationManager, executor, null,
+ Optional.of(ImmutableList.of(new Pair("", new Intent()))),
+ mFakeBroadcastDispatcher);
- dialog.show();
+ mDialog.show();
executor.advanceClockToLast();
executor.runAllReady();
- dialog.dismiss();
+ mDialog.dismiss();
verify(mNotificationManager).notify(
eq(SystemMessageProto.SystemMessage.NOTE_CSD_LOWER_AUDIO), any(Notification.class));
}
@Test
- public void create5XCsdDiSalogAndWait_willSendNotification() {
+ public void create5XCsdDialogAndWait_willSendNotification() {
FakeExecutor executor = new FakeExecutor(new FakeSystemClock());
- CsdWarningDialog dialog = new CsdWarningDialog(CSD_WARNING_DOSE_REPEATED_5X, mContext,
- mAudioManager, mNotificationManager, executor, null);
+ mDialog = new CsdWarningDialog(CSD_WARNING_DOSE_REPEATED_5X, mContext,
+ mAudioManager, mNotificationManager, executor, null,
+ Optional.of(ImmutableList.of(new Pair("", new Intent()))),
+ mFakeBroadcastDispatcher);
- dialog.show();
+ mDialog.show();
verify(mNotificationManager).notify(
eq(SystemMessageProto.SystemMessage.NOTE_CSD_LOWER_AUDIO), any(Notification.class));
}
+
+ @Test
+ @EnableFlags(Flags.FLAG_SOUNDDOSE_CUSTOMIZATION)
+ public void create1XCsdDialogWithActionsAndUndoIntent_willRegisterReceiverAndUndoVolume() {
+ FakeExecutor executor = new FakeExecutor(new FakeSystemClock());
+ Intent undoIntent = new Intent(VolumeDialog.ACTION_VOLUME_UNDO)
+ .setPackage(mContext.getPackageName());
+ mDialog = new CsdWarningDialog(CSD_WARNING_DOSE_REPEATED_5X, mContext,
+ mAudioManager, mNotificationManager, executor, null,
+ Optional.of(ImmutableList.of(new Pair("Undo", undoIntent))),
+ mFakeBroadcastDispatcher);
+
+ when(mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC)).thenReturn(25);
+ mDialog.show();
+ executor.advanceClockToLast();
+ executor.runAllReady();
+ mDialog.dismiss();
+ mDialog.mReceiverUndo.onReceive(mContext, undoIntent);
+
+ verify(mNotificationManager).notify(
+ eq(SystemMessageProto.SystemMessage.NOTE_CSD_LOWER_AUDIO),
+ any(Notification.class));
+ verify(mAudioManager).setStreamVolume(
+ eq(AudioManager.STREAM_MUSIC),
+ eq(25),
+ eq(AudioManager.FLAG_SHOW_UI));
+ }
+
+ @Test
+ @EnableFlags(Flags.FLAG_SOUNDDOSE_CUSTOMIZATION)
+ public void deleteNotificationIntent_willUnregisterAllReceivers() {
+ FakeExecutor executor = new FakeExecutor(new FakeSystemClock());
+ Intent undoIntent = new Intent(VolumeDialog.ACTION_VOLUME_UNDO)
+ .setPackage(mContext.getPackageName());
+ mDialog = new CsdWarningDialog(CSD_WARNING_DOSE_REPEATED_5X, mContext,
+ mAudioManager, mNotificationManager, executor, null,
+ Optional.of(ImmutableList.of(new Pair("Undo", undoIntent))),
+ mFakeBroadcastDispatcher);
+ Intent dismissIntent = new Intent(DISMISS_CSD_NOTIFICATION)
+ .setPackage(mContext.getPackageName());
+
+ mDialog.mReceiverDismissNotification.onReceive(mContext, dismissIntent);
+ mDialog.show();
+ executor.advanceClockToLast();
+ executor.runAllReady();
+ mDialog.dismiss();
+
+ List<ResolveInfo> resolveInfoListDismiss = mContext.getPackageManager()
+ .queryBroadcastReceivers(dismissIntent, PackageManager.GET_RESOLVED_FILTER);
+ assertThat(resolveInfoListDismiss).hasSize(0);
+ List<ResolveInfo> resolveInfoListUndo = mContext.getPackageManager()
+ .queryBroadcastReceivers(undoIntent, PackageManager.GET_RESOLVED_FILTER);
+ assertThat(resolveInfoListUndo).hasSize(0);
+ }
+
+ @After
+ public void tearDown() {
+ mDialog.destroy();
+ }
}
diff --git a/packages/SystemUI/tests/src/com/android/systemui/volume/VolumeDialogImplTest.java b/packages/SystemUI/tests/src/com/android/systemui/volume/VolumeDialogImplTest.java
index 6efb7d8..cdfcca6 100644
--- a/packages/SystemUI/tests/src/com/android/systemui/volume/VolumeDialogImplTest.java
+++ b/packages/SystemUI/tests/src/com/android/systemui/volume/VolumeDialogImplTest.java
@@ -43,6 +43,7 @@
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;
@@ -55,6 +56,7 @@
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;
@@ -92,6 +94,8 @@
import com.android.systemui.volume.panel.shared.flag.VolumePanelFlag;
import com.android.systemui.volume.ui.navigation.VolumeNavigator;
+import com.google.common.collect.ImmutableList;
+
import dagger.Lazy;
import junit.framework.Assert;
@@ -107,6 +111,7 @@
import org.mockito.MockitoAnnotations;
import java.util.Arrays;
+import java.util.Optional;
import java.util.function.Predicate;
@SmallTest
@@ -157,11 +162,12 @@
private final CsdWarningDialog.Factory mCsdWarningDialogFactory =
new CsdWarningDialog.Factory() {
- @Override
- public CsdWarningDialog create(int warningType, Runnable onCleanup) {
- return mCsdWarningDialog;
- }
- };
+ @Override
+ public CsdWarningDialog create(int warningType, Runnable onCleanup,
+ Optional<ImmutableList<Pair<String, Intent>>> actionIntents) {
+ return mCsdWarningDialog;
+ }
+ };
@Mock
private VibratorHelper mVibratorHelper;