diff options
| author | 2024-07-24 20:23:27 +0000 | |
|---|---|---|
| committer | 2024-07-25 19:17:22 +0000 | |
| commit | c7182bb1ce297de4f553ef6da6ff7183c8678ac3 (patch) | |
| tree | 2a0e750b82fa3e698a04af446df462d9b07b16d0 | |
| parent | a3351585d8c6046a977527a94824c7d24fccfeef (diff) | |
Allow csdWarning Dialog notification to trigger either an activity or
broadcast depending on the action intents provided
Flag: com.android.systemui.sounddose_customization
Test: MANUAL took video
Bug: 355015067
Change-Id: I7f630618b8c113da874a8fd00f8e7f086551c545
5 files changed, 100 insertions, 50 deletions
diff --git a/packages/SystemUI/src/com/android/systemui/volume/CsdWarningAction.kt b/packages/SystemUI/src/com/android/systemui/volume/CsdWarningAction.kt new file mode 100644 index 000000000000..a77acb5a3e08 --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/volume/CsdWarningAction.kt @@ -0,0 +1,48 @@ +/* + * 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) + } +} diff --git a/packages/SystemUI/src/com/android/systemui/volume/CsdWarningDialog.java b/packages/SystemUI/src/com/android/systemui/volume/CsdWarningDialog.java index bb230e6b0305..a63660ba2804 100644 --- a/packages/SystemUI/src/com/android/systemui/volume/CsdWarningDialog.java +++ b/packages/SystemUI/src/com/android/systemui/volume/CsdWarningDialog.java @@ -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,39 +350,45 @@ 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); - // 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); - } + 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 (action.getLabel().equals(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); } } } diff --git a/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogImpl.java b/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogImpl.java index 0770d8926389..e56f6b32c085 100644 --- a/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogImpl.java +++ b/packages/SystemUI/src/com/android/systemui/volume/VolumeDialogImpl.java @@ -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); } 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 49aedccde258..bebf1cfa86e7 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/volume/CsdWarningDialogTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/volume/CsdWarningDialogTest.java @@ -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()); 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 b5cbf598bd05..caa177908db0 100644 --- a/packages/SystemUI/tests/src/com/android/systemui/volume/VolumeDialogImplTest.java +++ b/packages/SystemUI/tests/src/com/android/systemui/volume/VolumeDialogImplTest.java @@ -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; } }; |