From 5a4dae207001da7e3bd3f65dcb7dfb89b6c82ae8 Mon Sep 17 00:00:00 2001 From: Stypox Date: Fri, 29 Dec 2023 11:36:12 +0100 Subject: [PATCH 1/9] Fix settings_notification.xml indentation --- .../main/res/layout/settings_notification.xml | 111 +++++++++--------- 1 file changed, 55 insertions(+), 56 deletions(-) diff --git a/app/src/main/res/layout/settings_notification.xml b/app/src/main/res/layout/settings_notification.xml index d4af3fe71..e5d73a4ae 100644 --- a/app/src/main/res/layout/settings_notification.xml +++ b/app/src/main/res/layout/settings_notification.xml @@ -1,5 +1,4 @@ - + android:id="@+id/textView" + android:layout_width="0dp" + android:layout_height="wrap_content" + android:layout_marginStart="16dp" + android:layout_marginEnd="16dp" + android:clickable="false" + android:focusable="false" + android:gravity="center" + android:text="@string/notification_actions_summary" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintHorizontal_bias="0.0" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" /> - + - + - + - + - + - + From 30f0db1d28987805612325839432898d1ba77625 Mon Sep 17 00:00:00 2001 From: Stypox Date: Fri, 29 Dec 2023 12:13:08 +0100 Subject: [PATCH 2/9] Customize only 2 notification actions on Android 13+ --- .../custom/NotificationActionsPreference.java | 34 +++++++++++++++---- .../main/res/layout/settings_notification.xml | 4 +-- app/src/main/res/values/strings.xml | 3 +- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationActionsPreference.java b/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationActionsPreference.java index 3e92f297e..29a7c49a0 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationActionsPreference.java +++ b/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationActionsPreference.java @@ -6,6 +6,7 @@ import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.ColorStateList; +import android.os.Build; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; @@ -35,6 +36,7 @@ import org.schabi.newpipe.util.ThemeHelper; import org.schabi.newpipe.views.FocusOverlayView; import java.util.List; +import java.util.Objects; import java.util.stream.IntStream; public class NotificationActionsPreference extends Preference { @@ -56,6 +58,11 @@ public class NotificationActionsPreference extends Preference { public void onBindViewHolder(@NonNull final PreferenceViewHolder holder) { super.onBindViewHolder(holder); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + ((TextView) holder.itemView.findViewById(R.id.summary)) + .setText(R.string.notification_actions_summary_android13); + } + holder.itemView.setClickable(false); setupActions(holder.itemView); } @@ -137,11 +144,19 @@ public class NotificationActionsPreference extends Preference { NotificationSlot(final int actionIndex, final View parentView) { this.i = actionIndex; - + selectedAction = Objects.requireNonNull(getSharedPreferences()).getInt( + getContext().getString(NotificationConstants.SLOT_PREF_KEYS[i]), + NotificationConstants.SLOT_DEFAULTS[i]); final View view = parentView.findViewById(SLOT_ITEMS[i]); - setupSelectedAction(view); - setupTitle(view); - setupCheckbox(view); + + // only show the last two notification slots on Android 13+ + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU || i >= 3) { + setupSelectedAction(view); + setupTitle(view); + setupCheckbox(view); + } else { + view.setVisibility(View.GONE); + } } void setupTitle(final View view) { @@ -153,6 +168,14 @@ public class NotificationActionsPreference extends Preference { void setupCheckbox(final View view) { final CheckBox compactSlotCheckBox = view.findViewById(R.id.notificationActionCheckBox); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + // there are no compact slots to customize on Android 33+ + compactSlotCheckBox.setVisibility(View.GONE); + view.findViewById(R.id.notificationActionCheckBoxClickableArea) + .setVisibility(View.GONE); + return; + } + compactSlotCheckBox.setChecked(compactSlots.contains(i)); view.findViewById(R.id.notificationActionCheckBoxClickableArea).setOnClickListener( v -> { @@ -174,9 +197,6 @@ public class NotificationActionsPreference extends Preference { void setupSelectedAction(final View view) { icon = view.findViewById(R.id.notificationActionIcon); summary = view.findViewById(R.id.notificationActionSummary); - selectedAction = getSharedPreferences().getInt( - getContext().getString(NotificationConstants.SLOT_PREF_KEYS[i]), - NotificationConstants.SLOT_DEFAULTS[i]); updateInfo(); } diff --git a/app/src/main/res/layout/settings_notification.xml b/app/src/main/res/layout/settings_notification.xml index e5d73a4ae..b8195f18c 100644 --- a/app/src/main/res/layout/settings_notification.xml +++ b/app/src/main/res/layout/settings_notification.xml @@ -7,7 +7,7 @@ android:paddingTop="16dp"> + app:layout_constraintTop_toBottomOf="@+id/summary" /> Third action button Fourth action button Fifth action button - Edit each notification action below by tapping on it. Select up to three of them to be shown in the compact notification by using the checkboxes on the right + Edit each notification action below by tapping on it. Select up to three of them to be shown in the compact notification by using the checkboxes on the right. + Edit each notification action below by tapping on it. The first three actions (play/pause, previous and next) are set by the system and cannot be customized. You can select at most three actions to show in the compact notification! Repeat Shuffle From aab6580195c2448b12a72b439550bd2ad2a3ed42 Mon Sep 17 00:00:00 2001 From: Stypox Date: Fri, 29 Dec 2023 12:31:59 +0100 Subject: [PATCH 3/9] Extract NotificationSlot from NotificationActionsPreference --- .../custom/NotificationActionsPreference.java | 187 ++---------------- .../settings/custom/NotificationSlot.java | 172 ++++++++++++++++ 2 files changed, 193 insertions(+), 166 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/settings/custom/NotificationSlot.java diff --git a/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationActionsPreference.java b/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationActionsPreference.java index 29a7c49a0..43e9d6f0c 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationActionsPreference.java +++ b/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationActionsPreference.java @@ -5,38 +5,22 @@ import static org.schabi.newpipe.player.notification.NotificationConstants.ACTIO import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; -import android.content.res.ColorStateList; import android.os.Build; import android.util.AttributeSet; -import android.view.LayoutInflater; import android.view.View; -import android.view.ViewGroup; import android.widget.CheckBox; -import android.widget.ImageView; -import android.widget.RadioButton; -import android.widget.RadioGroup; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.appcompat.app.AlertDialog; -import androidx.appcompat.content.res.AppCompatResources; -import androidx.core.widget.TextViewCompat; import androidx.preference.Preference; import androidx.preference.PreferenceViewHolder; import org.schabi.newpipe.App; import org.schabi.newpipe.R; -import org.schabi.newpipe.databinding.ListRadioIconItemBinding; -import org.schabi.newpipe.databinding.SingleChoiceDialogViewBinding; import org.schabi.newpipe.player.notification.NotificationConstants; -import org.schabi.newpipe.util.DeviceUtils; -import org.schabi.newpipe.util.ThemeHelper; -import org.schabi.newpipe.views.FocusOverlayView; import java.util.List; -import java.util.Objects; import java.util.stream.IntStream; public class NotificationActionsPreference extends Preference { @@ -47,8 +31,9 @@ public class NotificationActionsPreference extends Preference { } - @Nullable private NotificationSlot[] notificationSlots = null; - @Nullable private List compactSlots = null; + private NotificationSlot[] notificationSlots; + private List compactSlots; + //////////////////////////////////////////////////////////////////////////// // Lifecycle @@ -85,10 +70,26 @@ public class NotificationActionsPreference extends Preference { compactSlots = NotificationConstants.getCompactSlotsFromPreferences(getContext(), getSharedPreferences(), 5); notificationSlots = IntStream.range(0, 5) - .mapToObj(i -> new NotificationSlot(i, view)) + .mapToObj(i -> new NotificationSlot(getContext(), getSharedPreferences(), i, view, + compactSlots.contains(i), this::onToggleCompactSlot)) .toArray(NotificationSlot[]::new); } + private void onToggleCompactSlot(final int i, final CheckBox checkBox) { + if (checkBox.isChecked()) { + compactSlots.remove((Integer) i); + } else if (compactSlots.size() < 3) { + compactSlots.add(i); + } else { + Toast.makeText(getContext(), + R.string.notification_actions_at_most_three, + Toast.LENGTH_SHORT).show(); + return; + } + + checkBox.toggle(); + } + //////////////////////////////////////////////////////////////////////////// // Saving @@ -106,156 +107,10 @@ public class NotificationActionsPreference extends Preference { for (int i = 0; i < 5; i++) { editor.putInt(getContext().getString(NotificationConstants.SLOT_PREF_KEYS[i]), - notificationSlots[i].selectedAction); + notificationSlots[i].getSelectedAction()); } editor.apply(); } } - - - //////////////////////////////////////////////////////////////////////////// - // Notification action - //////////////////////////////////////////////////////////////////////////// - - private static final int[] SLOT_ITEMS = { - R.id.notificationAction0, - R.id.notificationAction1, - R.id.notificationAction2, - R.id.notificationAction3, - R.id.notificationAction4, - }; - - private static final int[] SLOT_TITLES = { - R.string.notification_action_0_title, - R.string.notification_action_1_title, - R.string.notification_action_2_title, - R.string.notification_action_3_title, - R.string.notification_action_4_title, - }; - - private class NotificationSlot { - - final int i; - @NotificationConstants.Action int selectedAction; - - ImageView icon; - TextView summary; - - NotificationSlot(final int actionIndex, final View parentView) { - this.i = actionIndex; - selectedAction = Objects.requireNonNull(getSharedPreferences()).getInt( - getContext().getString(NotificationConstants.SLOT_PREF_KEYS[i]), - NotificationConstants.SLOT_DEFAULTS[i]); - final View view = parentView.findViewById(SLOT_ITEMS[i]); - - // only show the last two notification slots on Android 13+ - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU || i >= 3) { - setupSelectedAction(view); - setupTitle(view); - setupCheckbox(view); - } else { - view.setVisibility(View.GONE); - } - } - - void setupTitle(final View view) { - ((TextView) view.findViewById(R.id.notificationActionTitle)) - .setText(SLOT_TITLES[i]); - view.findViewById(R.id.notificationActionClickableArea).setOnClickListener( - v -> openActionChooserDialog()); - } - - void setupCheckbox(final View view) { - final CheckBox compactSlotCheckBox = view.findViewById(R.id.notificationActionCheckBox); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - // there are no compact slots to customize on Android 33+ - compactSlotCheckBox.setVisibility(View.GONE); - view.findViewById(R.id.notificationActionCheckBoxClickableArea) - .setVisibility(View.GONE); - return; - } - - compactSlotCheckBox.setChecked(compactSlots.contains(i)); - view.findViewById(R.id.notificationActionCheckBoxClickableArea).setOnClickListener( - v -> { - if (compactSlotCheckBox.isChecked()) { - compactSlots.remove((Integer) i); - } else if (compactSlots.size() < 3) { - compactSlots.add(i); - } else { - Toast.makeText(getContext(), - R.string.notification_actions_at_most_three, - Toast.LENGTH_SHORT).show(); - return; - } - - compactSlotCheckBox.toggle(); - }); - } - - void setupSelectedAction(final View view) { - icon = view.findViewById(R.id.notificationActionIcon); - summary = view.findViewById(R.id.notificationActionSummary); - updateInfo(); - } - - void updateInfo() { - if (NotificationConstants.ACTION_ICONS[selectedAction] == 0) { - icon.setImageDrawable(null); - } else { - icon.setImageDrawable(AppCompatResources.getDrawable(getContext(), - NotificationConstants.ACTION_ICONS[selectedAction])); - } - - summary.setText(NotificationConstants.getActionName(getContext(), selectedAction)); - } - - void openActionChooserDialog() { - final LayoutInflater inflater = LayoutInflater.from(getContext()); - final SingleChoiceDialogViewBinding binding = - SingleChoiceDialogViewBinding.inflate(inflater); - - final AlertDialog alertDialog = new AlertDialog.Builder(getContext()) - .setTitle(SLOT_TITLES[i]) - .setView(binding.getRoot()) - .setCancelable(true) - .create(); - - final View.OnClickListener radioButtonsClickListener = v -> { - selectedAction = NotificationConstants.SLOT_ALLOWED_ACTIONS[i][v.getId()]; - updateInfo(); - alertDialog.dismiss(); - }; - - for (int id = 0; id < NotificationConstants.SLOT_ALLOWED_ACTIONS[i].length; ++id) { - final int action = NotificationConstants.SLOT_ALLOWED_ACTIONS[i][id]; - final RadioButton radioButton = ListRadioIconItemBinding.inflate(inflater) - .getRoot(); - - // if present set action icon with correct color - final int iconId = NotificationConstants.ACTION_ICONS[action]; - if (iconId != 0) { - radioButton.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, iconId, 0); - - final var color = ColorStateList.valueOf(ThemeHelper - .resolveColorFromAttr(getContext(), android.R.attr.textColorPrimary)); - TextViewCompat.setCompoundDrawableTintList(radioButton, color); - } - - radioButton.setText(NotificationConstants.getActionName(getContext(), action)); - radioButton.setChecked(action == selectedAction); - radioButton.setId(id); - radioButton.setLayoutParams(new RadioGroup.LayoutParams( - ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); - radioButton.setOnClickListener(radioButtonsClickListener); - binding.list.addView(radioButton); - } - alertDialog.show(); - - if (DeviceUtils.isTv(getContext())) { - FocusOverlayView.setupFocusObserver(alertDialog); - } - } - } } diff --git a/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationSlot.java b/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationSlot.java new file mode 100644 index 000000000..412215d0f --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationSlot.java @@ -0,0 +1,172 @@ +package org.schabi.newpipe.settings.custom; + +import android.content.Context; +import android.content.SharedPreferences; +import android.content.res.ColorStateList; +import android.os.Build; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.CheckBox; +import android.widget.ImageView; +import android.widget.RadioButton; +import android.widget.RadioGroup; +import android.widget.TextView; + +import androidx.appcompat.app.AlertDialog; +import androidx.appcompat.content.res.AppCompatResources; +import androidx.core.widget.TextViewCompat; + +import org.schabi.newpipe.R; +import org.schabi.newpipe.databinding.ListRadioIconItemBinding; +import org.schabi.newpipe.databinding.SingleChoiceDialogViewBinding; +import org.schabi.newpipe.player.notification.NotificationConstants; +import org.schabi.newpipe.util.DeviceUtils; +import org.schabi.newpipe.util.ThemeHelper; +import org.schabi.newpipe.views.FocusOverlayView; + +import java.util.Objects; +import java.util.function.BiConsumer; + +class NotificationSlot { + + private static final int[] SLOT_ITEMS = { + R.id.notificationAction0, + R.id.notificationAction1, + R.id.notificationAction2, + R.id.notificationAction3, + R.id.notificationAction4, + }; + + private static final int[] SLOT_TITLES = { + R.string.notification_action_0_title, + R.string.notification_action_1_title, + R.string.notification_action_2_title, + R.string.notification_action_3_title, + R.string.notification_action_4_title, + }; + + private final int i; + private @NotificationConstants.Action int selectedAction; + private final Context context; + private final BiConsumer onToggleCompactSlot; + + private ImageView icon; + private TextView summary; + + NotificationSlot(final Context context, + final SharedPreferences prefs, + final int actionIndex, + final View parentView, + final boolean isCompactSlotChecked, + final BiConsumer onToggleCompactSlot) { + this.context = context; + this.i = actionIndex; + this.onToggleCompactSlot = onToggleCompactSlot; + + selectedAction = Objects.requireNonNull(prefs).getInt( + context.getString(NotificationConstants.SLOT_PREF_KEYS[i]), + NotificationConstants.SLOT_DEFAULTS[i]); + final View view = parentView.findViewById(SLOT_ITEMS[i]); + + // only show the last two notification slots on Android 13+ + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU || i >= 3) { + setupSelectedAction(view); + setupTitle(view); + setupCheckbox(view, isCompactSlotChecked); + } else { + view.setVisibility(View.GONE); + } + } + + void setupTitle(final View view) { + ((TextView) view.findViewById(R.id.notificationActionTitle)) + .setText(SLOT_TITLES[i]); + view.findViewById(R.id.notificationActionClickableArea).setOnClickListener( + v -> openActionChooserDialog()); + } + + void setupCheckbox(final View view, final boolean isCompactSlotChecked) { + final CheckBox compactSlotCheckBox = view.findViewById(R.id.notificationActionCheckBox); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + // there are no compact slots to customize on Android 33+ + compactSlotCheckBox.setVisibility(View.GONE); + view.findViewById(R.id.notificationActionCheckBoxClickableArea) + .setVisibility(View.GONE); + return; + } + + compactSlotCheckBox.setChecked(isCompactSlotChecked); + view.findViewById(R.id.notificationActionCheckBoxClickableArea).setOnClickListener( + v -> onToggleCompactSlot.accept(i, compactSlotCheckBox)); + } + + void setupSelectedAction(final View view) { + icon = view.findViewById(R.id.notificationActionIcon); + summary = view.findViewById(R.id.notificationActionSummary); + updateInfo(); + } + + void updateInfo() { + if (NotificationConstants.ACTION_ICONS[selectedAction] == 0) { + icon.setImageDrawable(null); + } else { + icon.setImageDrawable(AppCompatResources.getDrawable(context, + NotificationConstants.ACTION_ICONS[selectedAction])); + } + + summary.setText(NotificationConstants.getActionName(context, selectedAction)); + } + + void openActionChooserDialog() { + final LayoutInflater inflater = LayoutInflater.from(context); + final SingleChoiceDialogViewBinding binding = + SingleChoiceDialogViewBinding.inflate(inflater); + + final AlertDialog alertDialog = new AlertDialog.Builder(context) + .setTitle(SLOT_TITLES[i]) + .setView(binding.getRoot()) + .setCancelable(true) + .create(); + + final View.OnClickListener radioButtonsClickListener = v -> { + selectedAction = NotificationConstants.SLOT_ALLOWED_ACTIONS[i][v.getId()]; + updateInfo(); + alertDialog.dismiss(); + }; + + for (int id = 0; id < NotificationConstants.SLOT_ALLOWED_ACTIONS[i].length; ++id) { + final int action = NotificationConstants.SLOT_ALLOWED_ACTIONS[i][id]; + final RadioButton radioButton = ListRadioIconItemBinding.inflate(inflater) + .getRoot(); + + // if present set action icon with correct color + final int iconId = NotificationConstants.ACTION_ICONS[action]; + if (iconId != 0) { + radioButton.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, iconId, 0); + + final var color = ColorStateList.valueOf(ThemeHelper + .resolveColorFromAttr(context, android.R.attr.textColorPrimary)); + TextViewCompat.setCompoundDrawableTintList(radioButton, color); + } + + radioButton.setText(NotificationConstants.getActionName(context, action)); + radioButton.setChecked(action == selectedAction); + radioButton.setId(id); + radioButton.setLayoutParams(new RadioGroup.LayoutParams( + ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); + radioButton.setOnClickListener(radioButtonsClickListener); + binding.list.addView(radioButton); + } + alertDialog.show(); + + if (DeviceUtils.isTv(context)) { + FocusOverlayView.setupFocusObserver(alertDialog); + } + } + + @NotificationConstants.Action + public int getSelectedAction() { + return selectedAction; + } +} From 9fb8125655060a343eb0a68d9a3fe69b01a28b9d Mon Sep 17 00:00:00 2001 From: Stypox Date: Fri, 29 Dec 2023 14:04:18 +0100 Subject: [PATCH 4/9] Allow each notification slot to contain any possible action --- .../notification/NotificationConstants.java | 37 +++++++--------- .../player/notification/NotificationUtil.java | 43 ++++++++++++------- .../custom/NotificationActionsPreference.java | 5 ++- .../settings/custom/NotificationSlot.java | 6 +-- 4 files changed, 48 insertions(+), 43 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationConstants.java b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationConstants.java index 89bf0b22a..b9607f7ea 100644 --- a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationConstants.java +++ b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationConstants.java @@ -13,7 +13,7 @@ import org.schabi.newpipe.util.Localization; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; -import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.SortedSet; import java.util.TreeSet; @@ -65,10 +65,16 @@ public final class NotificationConstants { public static final int CLOSE = 11; @Retention(RetentionPolicy.SOURCE) - @IntDef({NOTHING, PREVIOUS, NEXT, REWIND, FORWARD, SMART_REWIND_PREVIOUS, SMART_FORWARD_NEXT, - PLAY_PAUSE, PLAY_PAUSE_BUFFERING, REPEAT, SHUFFLE, CLOSE}) + @IntDef({NOTHING, PREVIOUS, NEXT, REWIND, FORWARD, + SMART_REWIND_PREVIOUS, SMART_FORWARD_NEXT, PLAY_PAUSE, PLAY_PAUSE_BUFFERING, REPEAT, + SHUFFLE, CLOSE}) public @interface Action { } + @Action + public static final int[] ALL_ACTIONS = {NOTHING, PREVIOUS, NEXT, REWIND, FORWARD, + SMART_REWIND_PREVIOUS, SMART_FORWARD_NEXT, PLAY_PAUSE, PLAY_PAUSE_BUFFERING, REPEAT, + SHUFFLE, CLOSE}; + @DrawableRes public static final int[] ACTION_ICONS = { 0, @@ -95,16 +101,6 @@ public final class NotificationConstants { CLOSE, }; - @Action - public static final int[][] SLOT_ALLOWED_ACTIONS = { - new int[] {PREVIOUS, REWIND, SMART_REWIND_PREVIOUS}, - new int[] {REWIND, PLAY_PAUSE, PLAY_PAUSE_BUFFERING}, - new int[] {NEXT, FORWARD, SMART_FORWARD_NEXT, PLAY_PAUSE, PLAY_PAUSE_BUFFERING}, - new int[] {NOTHING, PREVIOUS, NEXT, REWIND, FORWARD, SMART_REWIND_PREVIOUS, - SMART_FORWARD_NEXT, REPEAT, SHUFFLE, CLOSE}, - new int[] {NOTHING, NEXT, FORWARD, SMART_FORWARD_NEXT, REPEAT, SHUFFLE, CLOSE}, - }; - public static final int[] SLOT_PREF_KEYS = { R.string.notification_slot_0_key, R.string.notification_slot_1_key, @@ -165,14 +161,11 @@ public final class NotificationConstants { /** * @param context the context to use * @param sharedPreferences the shared preferences to query values from - * @param slotCount remove indices >= than this value (set to {@code 5} to do nothing, or make - * it lower if there are slots with empty actions) * @return a sorted list of the indices of the slots to use as compact slots */ - public static List getCompactSlotsFromPreferences( + public static Collection getCompactSlotsFromPreferences( @NonNull final Context context, - final SharedPreferences sharedPreferences, - final int slotCount) { + final SharedPreferences sharedPreferences) { final SortedSet compactSlots = new TreeSet<>(); for (int i = 0; i < 3; i++) { final int compactSlot = sharedPreferences.getInt( @@ -180,14 +173,14 @@ public final class NotificationConstants { if (compactSlot == Integer.MAX_VALUE) { // settings not yet populated, return default values - return new ArrayList<>(SLOT_COMPACT_DEFAULTS); + return SLOT_COMPACT_DEFAULTS; } - // a negative value (-1) is set when the user does not want a particular compact slot - if (compactSlot >= 0 && compactSlot < slotCount) { + if (compactSlot >= 0) { + // compact slot is < 0 if there are less than 3 checked checkboxes compactSlots.add(compactSlot); } } - return new ArrayList<>(compactSlots); + return compactSlots; } } diff --git a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java index 3fa7c2623..551e2b863 100644 --- a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java +++ b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java @@ -23,6 +23,8 @@ import org.schabi.newpipe.player.Player; import org.schabi.newpipe.player.mediasession.MediaSessionPlayerUi; import org.schabi.newpipe.util.NavigationHelper; +import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Objects; import java.util.Optional; @@ -101,21 +103,7 @@ public final class NotificationUtil { new NotificationCompat.Builder(player.getContext(), player.getContext().getString(R.string.notification_channel_id)); - initializeNotificationSlots(); - - // count the number of real slots, to make sure compact slots indices are not out of bound - int nonNothingSlotCount = 5; - if (notificationSlots[3] == NotificationConstants.NOTHING) { - --nonNothingSlotCount; - } - if (notificationSlots[4] == NotificationConstants.NOTHING) { - --nonNothingSlotCount; - } - - // build the compact slot indices array (need code to convert from Integer... because Java) - final List compactSlotList = NotificationConstants.getCompactSlotsFromPreferences( - player.getContext(), player.getPrefs(), nonNothingSlotCount); - final int[] compactSlots = compactSlotList.stream().mapToInt(Integer::intValue).toArray(); + final int[] compactSlots = initializeNotificationSlots(); final MediaStyle mediaStyle = new MediaStyle().setShowActionsInCompactView(compactSlots); player.UIs() @@ -209,12 +197,35 @@ public final class NotificationUtil { // ACTIONS ///////////////////////////////////////////////////// - private void initializeNotificationSlots() { + /** + * The compact slots array from settings contains indices from 0 to 4, each referring to one of + * the five actions configurable by the user. However, if the user sets an action to "Nothing", + * then all of the actions coming after will have a "settings index" different than the index + * of the corresponding action when sent to the system. + * + * @return the indices of compact slots referred to the list of non-nothing actions that will be + * sent to the system + */ + private int[] initializeNotificationSlots() { + final Collection settingsCompactSlots = NotificationConstants + .getCompactSlotsFromPreferences(player.getContext(), player.getPrefs()); + final List adjustedCompactSlots = new ArrayList<>(); + + int nonNothingIndex = 0; for (int i = 0; i < 5; ++i) { notificationSlots[i] = player.getPrefs().getInt( player.getContext().getString(NotificationConstants.SLOT_PREF_KEYS[i]), NotificationConstants.SLOT_DEFAULTS[i]); + + if (notificationSlots[i] != NotificationConstants.NOTHING) { + if (settingsCompactSlots.contains(i)) { + adjustedCompactSlots.add(nonNothingIndex); + } + nonNothingIndex += 1; + } } + + return adjustedCompactSlots.stream().mapToInt(Integer::intValue).toArray(); } @SuppressLint("RestrictedApi") diff --git a/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationActionsPreference.java b/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationActionsPreference.java index 43e9d6f0c..7dfddef20 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationActionsPreference.java +++ b/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationActionsPreference.java @@ -20,6 +20,7 @@ import org.schabi.newpipe.App; import org.schabi.newpipe.R; import org.schabi.newpipe.player.notification.NotificationConstants; +import java.util.ArrayList; import java.util.List; import java.util.stream.IntStream; @@ -67,8 +68,8 @@ public class NotificationActionsPreference extends Preference { //////////////////////////////////////////////////////////////////////////// private void setupActions(@NonNull final View view) { - compactSlots = NotificationConstants.getCompactSlotsFromPreferences(getContext(), - getSharedPreferences(), 5); + compactSlots = new ArrayList<>(NotificationConstants.getCompactSlotsFromPreferences( + getContext(), getSharedPreferences())); notificationSlots = IntStream.range(0, 5) .mapToObj(i -> new NotificationSlot(getContext(), getSharedPreferences(), i, view, compactSlots.contains(i), this::onToggleCompactSlot)) diff --git a/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationSlot.java b/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationSlot.java index 412215d0f..074532876 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationSlot.java +++ b/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationSlot.java @@ -130,13 +130,13 @@ class NotificationSlot { .create(); final View.OnClickListener radioButtonsClickListener = v -> { - selectedAction = NotificationConstants.SLOT_ALLOWED_ACTIONS[i][v.getId()]; + selectedAction = NotificationConstants.ALL_ACTIONS[v.getId()]; updateInfo(); alertDialog.dismiss(); }; - for (int id = 0; id < NotificationConstants.SLOT_ALLOWED_ACTIONS[i].length; ++id) { - final int action = NotificationConstants.SLOT_ALLOWED_ACTIONS[i][id]; + for (int id = 0; id < NotificationConstants.ALL_ACTIONS.length; ++id) { + final int action = NotificationConstants.ALL_ACTIONS[id]; final RadioButton radioButton = ListRadioIconItemBinding.inflate(inflater) .getRoot(); From 2c4c28309900276bfa5e36a3cebbf0a16730a1a9 Mon Sep 17 00:00:00 2001 From: Stypox Date: Thu, 16 Nov 2023 09:47:27 +0100 Subject: [PATCH 5/9] Extract NotificationActionData from NotificationUtil --- .../notification/NotificationActionData.java | 168 ++++++++++++++++++ .../player/notification/NotificationUtil.java | 136 ++------------ 2 files changed, 184 insertions(+), 120 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/player/notification/NotificationActionData.java diff --git a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationActionData.java b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationActionData.java new file mode 100644 index 000000000..fd5e03bf1 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationActionData.java @@ -0,0 +1,168 @@ +package org.schabi.newpipe.player.notification; + +import static com.google.android.exoplayer2.Player.REPEAT_MODE_ALL; +import static com.google.android.exoplayer2.Player.REPEAT_MODE_ONE; +import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_CLOSE; +import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_FAST_FORWARD; +import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_FAST_REWIND; +import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_PLAY_NEXT; +import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_PLAY_PAUSE; +import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_PLAY_PREVIOUS; +import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_REPEAT; +import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_SHUFFLE; + +import android.content.Context; + +import androidx.annotation.DrawableRes; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import org.schabi.newpipe.R; +import org.schabi.newpipe.player.Player; + +public final class NotificationActionData { + @Nullable + private final String action; + @NonNull + private final String name; + @DrawableRes + private final int icon; + + public NotificationActionData(@Nullable final String action, @NonNull final String name, + @DrawableRes final int icon) { + this.action = action; + this.name = name; + this.icon = icon; + } + + @Nullable + public String action() { + return action; + } + + @NonNull + public String name() { + return name; + } + + @DrawableRes + public int icon() { + return icon; + } + + + @Nullable + public static NotificationActionData fromNotificationActionEnum( + @NonNull final Player player, + @NotificationConstants.Action final int selectedAction + ) { + + final int baseActionIcon = NotificationConstants.ACTION_ICONS[selectedAction]; + final Context ctx = player.getContext(); + + switch (selectedAction) { + case NotificationConstants.PREVIOUS: + return new NotificationActionData(ACTION_PLAY_PREVIOUS, + ctx.getString(R.string.exo_controls_previous_description), baseActionIcon); + + case NotificationConstants.NEXT: + return new NotificationActionData(ACTION_PLAY_NEXT, + ctx.getString(R.string.exo_controls_next_description), baseActionIcon); + + case NotificationConstants.REWIND: + return new NotificationActionData(ACTION_FAST_REWIND, + ctx.getString(R.string.exo_controls_rewind_description), baseActionIcon); + + case NotificationConstants.FORWARD: + return new NotificationActionData(ACTION_FAST_FORWARD, + ctx.getString(R.string.exo_controls_fastforward_description), + baseActionIcon); + + case NotificationConstants.SMART_REWIND_PREVIOUS: + if (player.getPlayQueue() != null && player.getPlayQueue().size() > 1) { + return new NotificationActionData(ACTION_PLAY_PREVIOUS, + ctx.getString(R.string.exo_controls_previous_description), + R.drawable.exo_notification_previous); + } else { + return new NotificationActionData(ACTION_FAST_REWIND, + ctx.getString(R.string.exo_controls_rewind_description), + R.drawable.exo_controls_rewind); + } + + case NotificationConstants.SMART_FORWARD_NEXT: + if (player.getPlayQueue() != null && player.getPlayQueue().size() > 1) { + return new NotificationActionData(ACTION_PLAY_NEXT, + ctx.getString(R.string.exo_controls_next_description), + R.drawable.exo_notification_next); + } else { + return new NotificationActionData(ACTION_FAST_FORWARD, + ctx.getString(R.string.exo_controls_fastforward_description), + R.drawable.exo_controls_fastforward); + } + + case NotificationConstants.PLAY_PAUSE_BUFFERING: + if (player.getCurrentState() == Player.STATE_PREFLIGHT + || player.getCurrentState() == Player.STATE_BLOCKED + || player.getCurrentState() == Player.STATE_BUFFERING) { + // null intent action -> show hourglass icon that does nothing when clicked + return new NotificationActionData(null, + ctx.getString(R.string.notification_action_buffering), + R.drawable.ic_hourglass_top); + } + + // fallthrough + case NotificationConstants.PLAY_PAUSE: + if (player.getCurrentState() == Player.STATE_COMPLETED) { + return new NotificationActionData(ACTION_PLAY_PAUSE, + ctx.getString(R.string.exo_controls_pause_description), + R.drawable.ic_replay); + } else if (player.isPlaying() + || player.getCurrentState() == Player.STATE_PREFLIGHT + || player.getCurrentState() == Player.STATE_BLOCKED + || player.getCurrentState() == Player.STATE_BUFFERING) { + return new NotificationActionData(ACTION_PLAY_PAUSE, + ctx.getString(R.string.exo_controls_pause_description), + R.drawable.exo_notification_pause); + } else { + return new NotificationActionData(ACTION_PLAY_PAUSE, + ctx.getString(R.string.exo_controls_play_description), + R.drawable.exo_notification_play); + } + + case NotificationConstants.REPEAT: + if (player.getRepeatMode() == REPEAT_MODE_ALL) { + return new NotificationActionData(ACTION_REPEAT, + ctx.getString(R.string.exo_controls_repeat_all_description), + R.drawable.exo_media_action_repeat_all); + } else if (player.getRepeatMode() == REPEAT_MODE_ONE) { + return new NotificationActionData(ACTION_REPEAT, + ctx.getString(R.string.exo_controls_repeat_one_description), + R.drawable.exo_media_action_repeat_one); + } else /* player.getRepeatMode() == REPEAT_MODE_OFF */ { + return new NotificationActionData(ACTION_REPEAT, + ctx.getString(R.string.exo_controls_repeat_off_description), + R.drawable.exo_media_action_repeat_off); + } + + case NotificationConstants.SHUFFLE: + if (player.getPlayQueue() != null && player.getPlayQueue().isShuffled()) { + return new NotificationActionData(ACTION_SHUFFLE, + ctx.getString(R.string.exo_controls_shuffle_on_description), + R.drawable.exo_controls_shuffle_on); + } else { + return new NotificationActionData(ACTION_SHUFFLE, + ctx.getString(R.string.exo_controls_shuffle_off_description), + R.drawable.exo_controls_shuffle_off); + } + + case NotificationConstants.CLOSE: + return new NotificationActionData(ACTION_CLOSE, ctx.getString(R.string.close), + R.drawable.ic_close); + + case NotificationConstants.NOTHING: + default: + // do nothing + return null; + } + } +} diff --git a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java index 551e2b863..72b979f9d 100644 --- a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java +++ b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java @@ -1,16 +1,19 @@ package org.schabi.newpipe.player.notification; +import static android.app.PendingIntent.FLAG_UPDATE_CURRENT; +import static androidx.media.app.NotificationCompat.MediaStyle; +import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_CLOSE; + import android.annotation.SuppressLint; +import android.app.PendingIntent; import android.content.Intent; import android.content.pm.ServiceInfo; import android.graphics.Bitmap; import android.os.Build; import android.util.Log; -import androidx.annotation.DrawableRes; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import androidx.annotation.StringRes; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; import androidx.core.app.PendingIntentCompat; @@ -29,19 +32,6 @@ import java.util.List; import java.util.Objects; import java.util.Optional; -import static android.app.PendingIntent.FLAG_UPDATE_CURRENT; -import static androidx.media.app.NotificationCompat.MediaStyle; -import static com.google.android.exoplayer2.Player.REPEAT_MODE_ALL; -import static com.google.android.exoplayer2.Player.REPEAT_MODE_ONE; -import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_CLOSE; -import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_FAST_FORWARD; -import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_FAST_REWIND; -import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_PLAY_NEXT; -import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_PLAY_PAUSE; -import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_PLAY_PREVIOUS; -import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_REPEAT; -import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_SHUFFLE; - /** * This is a utility class for player notifications. */ @@ -238,115 +228,21 @@ public final class NotificationUtil { private void addAction(final NotificationCompat.Builder builder, @NotificationConstants.Action final int slot) { - final NotificationCompat.Action action = getAction(slot); - if (action != null) { - builder.addAction(action); + @Nullable final NotificationActionData data = + NotificationActionData.fromNotificationActionEnum(player, slot); + if (data == null) { + return; } - } - @Nullable - private NotificationCompat.Action getAction( - @NotificationConstants.Action final int selectedAction) { - final int baseActionIcon = NotificationConstants.ACTION_ICONS[selectedAction]; - switch (selectedAction) { - case NotificationConstants.PREVIOUS: - return getAction(baseActionIcon, - R.string.exo_controls_previous_description, ACTION_PLAY_PREVIOUS); - - case NotificationConstants.NEXT: - return getAction(baseActionIcon, - R.string.exo_controls_next_description, ACTION_PLAY_NEXT); - - case NotificationConstants.REWIND: - return getAction(baseActionIcon, - R.string.exo_controls_rewind_description, ACTION_FAST_REWIND); - - case NotificationConstants.FORWARD: - return getAction(baseActionIcon, - R.string.exo_controls_fastforward_description, ACTION_FAST_FORWARD); - - case NotificationConstants.SMART_REWIND_PREVIOUS: - if (player.getPlayQueue() != null && player.getPlayQueue().size() > 1) { - return getAction(R.drawable.exo_notification_previous, - R.string.exo_controls_previous_description, ACTION_PLAY_PREVIOUS); - } else { - return getAction(R.drawable.exo_controls_rewind, - R.string.exo_controls_rewind_description, ACTION_FAST_REWIND); - } - - case NotificationConstants.SMART_FORWARD_NEXT: - if (player.getPlayQueue() != null && player.getPlayQueue().size() > 1) { - return getAction(R.drawable.exo_notification_next, - R.string.exo_controls_next_description, ACTION_PLAY_NEXT); - } else { - return getAction(R.drawable.exo_controls_fastforward, - R.string.exo_controls_fastforward_description, ACTION_FAST_FORWARD); - } - - case NotificationConstants.PLAY_PAUSE_BUFFERING: - if (player.getCurrentState() == Player.STATE_PREFLIGHT - || player.getCurrentState() == Player.STATE_BLOCKED - || player.getCurrentState() == Player.STATE_BUFFERING) { - // null intent -> show hourglass icon that does nothing when clicked - return new NotificationCompat.Action(R.drawable.ic_hourglass_top, - player.getContext().getString(R.string.notification_action_buffering), - null); - } - - // fallthrough - case NotificationConstants.PLAY_PAUSE: - if (player.getCurrentState() == Player.STATE_COMPLETED) { - return getAction(R.drawable.ic_replay, - R.string.exo_controls_pause_description, ACTION_PLAY_PAUSE); - } else if (player.isPlaying() - || player.getCurrentState() == Player.STATE_PREFLIGHT - || player.getCurrentState() == Player.STATE_BLOCKED - || player.getCurrentState() == Player.STATE_BUFFERING) { - return getAction(R.drawable.exo_notification_pause, - R.string.exo_controls_pause_description, ACTION_PLAY_PAUSE); - } else { - return getAction(R.drawable.exo_notification_play, - R.string.exo_controls_play_description, ACTION_PLAY_PAUSE); - } - - case NotificationConstants.REPEAT: - if (player.getRepeatMode() == REPEAT_MODE_ALL) { - return getAction(R.drawable.exo_media_action_repeat_all, - R.string.exo_controls_repeat_all_description, ACTION_REPEAT); - } else if (player.getRepeatMode() == REPEAT_MODE_ONE) { - return getAction(R.drawable.exo_media_action_repeat_one, - R.string.exo_controls_repeat_one_description, ACTION_REPEAT); - } else /* player.getRepeatMode() == REPEAT_MODE_OFF */ { - return getAction(R.drawable.exo_media_action_repeat_off, - R.string.exo_controls_repeat_off_description, ACTION_REPEAT); - } - - case NotificationConstants.SHUFFLE: - if (player.getPlayQueue() != null && player.getPlayQueue().isShuffled()) { - return getAction(R.drawable.exo_controls_shuffle_on, - R.string.exo_controls_shuffle_on_description, ACTION_SHUFFLE); - } else { - return getAction(R.drawable.exo_controls_shuffle_off, - R.string.exo_controls_shuffle_off_description, ACTION_SHUFFLE); - } - - case NotificationConstants.CLOSE: - return getAction(R.drawable.ic_close, - R.string.close, ACTION_CLOSE); - - case NotificationConstants.NOTHING: - default: - // do nothing - return null; + final PendingIntent intent; + if (data.action() == null) { + intent = null; + } else { + intent = PendingIntentCompat.getBroadcast(player.getContext(), NOTIFICATION_ID, + new Intent(data.action()), FLAG_UPDATE_CURRENT, false); } - } - private NotificationCompat.Action getAction(@DrawableRes final int drawable, - @StringRes final int title, - final String intentAction) { - return new NotificationCompat.Action(drawable, player.getContext().getString(title), - PendingIntentCompat.getBroadcast(player.getContext(), NOTIFICATION_ID, - new Intent(intentAction), FLAG_UPDATE_CURRENT, false)); + builder.addAction(new NotificationCompat.Action(data.icon(), data.name(), intent)); } private Intent getIntentForNotification() { From 5edafca05ae485940094fb940a4fbac2886d4bd9 Mon Sep 17 00:00:00 2001 From: Stypox Date: Thu, 16 Nov 2023 11:04:01 +0100 Subject: [PATCH 6/9] Implement notification actions via MediaSessionConnector on Android 13+ --- .../mediasession/MediaSessionPlayerUi.java | 111 ++++++++++++++++++ .../SessionConnectorActionProvider.java | 51 ++++++++ .../player/notification/NotificationUtil.java | 18 ++- 3 files changed, 176 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/player/mediasession/SessionConnectorActionProvider.java diff --git a/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java b/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java index 6f76a91d1..53d6c297a 100644 --- a/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java +++ b/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java @@ -1,10 +1,12 @@ package org.schabi.newpipe.player.mediasession; import static org.schabi.newpipe.MainActivity.DEBUG; +import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_RECREATE_NOTIFICATION; import android.content.Intent; import android.content.SharedPreferences; import android.graphics.Bitmap; +import android.os.Build; import android.support.v4.media.MediaMetadataCompat; import android.support.v4.media.session.MediaSessionCompat; import android.util.Log; @@ -14,14 +16,20 @@ import androidx.annotation.Nullable; import androidx.media.session.MediaButtonReceiver; import com.google.android.exoplayer2.ForwardingPlayer; +import com.google.android.exoplayer2.Player.RepeatMode; import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector; import org.schabi.newpipe.R; +import org.schabi.newpipe.extractor.stream.StreamInfo; import org.schabi.newpipe.player.Player; +import org.schabi.newpipe.player.notification.NotificationActionData; +import org.schabi.newpipe.player.notification.NotificationConstants; import org.schabi.newpipe.player.ui.PlayerUi; import org.schabi.newpipe.player.ui.VideoPlayerUi; import org.schabi.newpipe.util.StreamTypeUtil; +import java.util.ArrayList; +import java.util.List; import java.util.Optional; public class MediaSessionPlayerUi extends PlayerUi @@ -163,4 +171,107 @@ public class MediaSessionPlayerUi extends PlayerUi return builder.build(); } + + + private void updateMediaSessionActions() { + // On Android 13+ (or Android T or API 33+) the actions in the player notification can't be + // controlled directly anymore, but are instead derived from custom media session actions. + // However the system allows customizing only two of these actions, since the other three + // are fixed to play-pause-buffering, previous, next. + + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { + // Although setting media session actions on older android versions doesn't seem to + // cause any trouble, it also doesn't seem to do anything, so we don't do anything to + // save battery. Check out NotificationUtil.updateActions() to see what happens on + // older android versions. + return; + } + + final List actions = new ArrayList<>(2); + for (int i = 3; i < 5; ++i) { + // only use the fourth and fifth actions (the settings page also shows only the last 2) + final int action = player.getPrefs().getInt( + player.getContext().getString(NotificationConstants.SLOT_PREF_KEYS[i]), + NotificationConstants.SLOT_DEFAULTS[i]); + + @Nullable final NotificationActionData data = + NotificationActionData.fromNotificationActionEnum(player, action); + + if (data != null) { + actions.add(new SessionConnectorActionProvider(data, context)); + } + } + + sessionConnector.setCustomActionProviders( + actions.toArray(new MediaSessionConnector.CustomActionProvider[0])); + } + + @Override + public void onBlocked() { + super.onBlocked(); + updateMediaSessionActions(); + } + + @Override + public void onPlaying() { + super.onPlaying(); + updateMediaSessionActions(); + } + + @Override + public void onBuffering() { + super.onBuffering(); + updateMediaSessionActions(); + } + + @Override + public void onPaused() { + super.onPaused(); + updateMediaSessionActions(); + } + + @Override + public void onPausedSeek() { + super.onPausedSeek(); + updateMediaSessionActions(); + } + + @Override + public void onCompleted() { + super.onCompleted(); + updateMediaSessionActions(); + } + + @Override + public void onRepeatModeChanged(@RepeatMode final int repeatMode) { + super.onRepeatModeChanged(repeatMode); + updateMediaSessionActions(); + } + + @Override + public void onShuffleModeEnabledChanged(final boolean shuffleModeEnabled) { + super.onShuffleModeEnabledChanged(shuffleModeEnabled); + updateMediaSessionActions(); + } + + @Override + public void onBroadcastReceived(final Intent intent) { + super.onBroadcastReceived(intent); + if (ACTION_RECREATE_NOTIFICATION.equals(intent.getAction())) { + // the notification actions changed + updateMediaSessionActions(); + } + } + + @Override + public void onMetadataChanged(@NonNull final StreamInfo info) { + super.onMetadataChanged(info); + updateMediaSessionActions(); + } + + @Override + public void onPlayQueueEdited() { + super.onPlayQueueEdited(); + updateMediaSessionActions(); + } } diff --git a/app/src/main/java/org/schabi/newpipe/player/mediasession/SessionConnectorActionProvider.java b/app/src/main/java/org/schabi/newpipe/player/mediasession/SessionConnectorActionProvider.java new file mode 100644 index 000000000..7b109c149 --- /dev/null +++ b/app/src/main/java/org/schabi/newpipe/player/mediasession/SessionConnectorActionProvider.java @@ -0,0 +1,51 @@ +package org.schabi.newpipe.player.mediasession; + +import android.content.Context; +import android.content.Intent; +import android.os.Bundle; +import android.support.v4.media.session.PlaybackStateCompat; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import com.google.android.exoplayer2.Player; +import com.google.android.exoplayer2.ext.mediasession.MediaSessionConnector; + +import org.schabi.newpipe.player.notification.NotificationActionData; + +import java.lang.ref.WeakReference; + +public class SessionConnectorActionProvider implements MediaSessionConnector.CustomActionProvider { + + private final NotificationActionData data; + @NonNull + private final WeakReference context; + + public SessionConnectorActionProvider(final NotificationActionData notificationActionData, + @NonNull final Context context) { + this.data = notificationActionData; + this.context = new WeakReference<>(context); + } + + @Override + public void onCustomAction(@NonNull final Player player, + @NonNull final String action, + @Nullable final Bundle extras) { + final Context actualContext = context.get(); + if (actualContext != null) { + actualContext.sendBroadcast(new Intent(action)); + } + } + + @Nullable + @Override + public PlaybackStateCompat.CustomAction getCustomAction(@NonNull final Player player) { + if (data.action() == null) { + return null; + } else { + return new PlaybackStateCompat.CustomAction.Builder( + data.action(), data.name(), data.icon() + ).build(); + } + } +} diff --git a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java index 72b979f9d..b3cfed1ce 100644 --- a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java +++ b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java @@ -92,15 +92,21 @@ public final class NotificationUtil { final NotificationCompat.Builder builder = new NotificationCompat.Builder(player.getContext(), player.getContext().getString(R.string.notification_channel_id)); + final MediaStyle mediaStyle = new MediaStyle(); - final int[] compactSlots = initializeNotificationSlots(); - - final MediaStyle mediaStyle = new MediaStyle().setShowActionsInCompactView(compactSlots); + // setup media style (compact notification slots and media session) + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { + // notification actions are ignored on Android 13+, and are replaced by code in + // MediaSessionPlayerUi + final int[] compactSlots = initializeNotificationSlots(); + mediaStyle.setShowActionsInCompactView(compactSlots); + } player.UIs() .get(MediaSessionPlayerUi.class) .flatMap(MediaSessionPlayerUi::getSessionToken) .ifPresent(mediaStyle::setMediaSession); + // setup notification builder builder.setStyle(mediaStyle) .setPriority(NotificationCompat.PRIORITY_HIGH) .setVisibility(NotificationCompat.VISIBILITY_PUBLIC) @@ -135,7 +141,11 @@ public final class NotificationUtil { notificationBuilder.setContentText(player.getUploaderName()); notificationBuilder.setTicker(player.getVideoTitle()); - updateActions(notificationBuilder); + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.TIRAMISU) { + // notification actions are ignored on Android 13+, and are replaced by code in + // MediaSessionPlayerUi + updateActions(notificationBuilder); + } } From 17e88f17498234f50ea58ba22bc8c6ab72d6d673 Mon Sep 17 00:00:00 2001 From: Stypox Date: Fri, 29 Dec 2023 15:08:44 +0100 Subject: [PATCH 7/9] Do not update notification actions if nothing changed This should avoid costly updates of the media session. --- .../mediasession/MediaSessionPlayerUi.java | 45 ++++++++++++------- .../notification/NotificationActionData.java | 17 ++++++- 2 files changed, 45 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java b/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java index 53d6c297a..737ebc5dd 100644 --- a/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java +++ b/app/src/main/java/org/schabi/newpipe/player/mediasession/MediaSessionPlayerUi.java @@ -28,9 +28,11 @@ import org.schabi.newpipe.player.ui.PlayerUi; import org.schabi.newpipe.player.ui.VideoPlayerUi; import org.schabi.newpipe.util.StreamTypeUtil; -import java.util.ArrayList; import java.util.List; +import java.util.Objects; import java.util.Optional; +import java.util.stream.Collectors; +import java.util.stream.IntStream; public class MediaSessionPlayerUi extends PlayerUi implements SharedPreferences.OnSharedPreferenceChangeListener { @@ -42,6 +44,10 @@ public class MediaSessionPlayerUi extends PlayerUi private final String ignoreHardwareMediaButtonsKey; private boolean shouldIgnoreHardwareMediaButtons = false; + // used to check whether any notification action changed, before sending costly updates + private List prevNotificationActions = List.of(); + + public MediaSessionPlayerUi(@NonNull final Player player) { super(player); ignoreHardwareMediaButtonsKey = @@ -71,6 +77,10 @@ public class MediaSessionPlayerUi extends PlayerUi sessionConnector.setMetadataDeduplicationEnabled(true); sessionConnector.setMediaMetadataProvider(exoPlayer -> buildMediaMetadata()); + + // force updating media session actions by resetting the previous ones + prevNotificationActions = List.of(); + updateMediaSessionActions(); } @Override @@ -88,6 +98,7 @@ public class MediaSessionPlayerUi extends PlayerUi mediaSession.release(); mediaSession = null; } + prevNotificationActions = List.of(); } @Override @@ -187,23 +198,25 @@ public class MediaSessionPlayerUi extends PlayerUi return; } - final List actions = new ArrayList<>(2); - for (int i = 3; i < 5; ++i) { - // only use the fourth and fifth actions (the settings page also shows only the last 2) - final int action = player.getPrefs().getInt( - player.getContext().getString(NotificationConstants.SLOT_PREF_KEYS[i]), - NotificationConstants.SLOT_DEFAULTS[i]); + // only use the fourth and fifth actions (the settings page also shows only the last 2 on + // Android 13+) + final List newNotificationActions = IntStream.of(3, 4) + .map(i -> player.getPrefs().getInt( + player.getContext().getString(NotificationConstants.SLOT_PREF_KEYS[i]), + NotificationConstants.SLOT_DEFAULTS[i])) + .mapToObj(action -> NotificationActionData + .fromNotificationActionEnum(player, action)) + .filter(Objects::nonNull) + .collect(Collectors.toList()); - @Nullable final NotificationActionData data = - NotificationActionData.fromNotificationActionEnum(player, action); - - if (data != null) { - actions.add(new SessionConnectorActionProvider(data, context)); - } + // avoid costly notification actions update, if nothing changed from last time + if (!newNotificationActions.equals(prevNotificationActions)) { + prevNotificationActions = newNotificationActions; + sessionConnector.setCustomActionProviders( + newNotificationActions.stream() + .map(data -> new SessionConnectorActionProvider(data, context)) + .toArray(SessionConnectorActionProvider[]::new)); } - - sessionConnector.setCustomActionProviders( - actions.toArray(new MediaSessionConnector.CustomActionProvider[0])); } @Override diff --git a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationActionData.java b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationActionData.java index fd5e03bf1..98ee3d7b8 100644 --- a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationActionData.java +++ b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationActionData.java @@ -20,6 +20,8 @@ import androidx.annotation.Nullable; import org.schabi.newpipe.R; import org.schabi.newpipe.player.Player; +import java.util.Objects; + public final class NotificationActionData { @Nullable private final String action; @@ -50,7 +52,6 @@ public final class NotificationActionData { return icon; } - @Nullable public static NotificationActionData fromNotificationActionEnum( @NonNull final Player player, @@ -165,4 +166,18 @@ public final class NotificationActionData { return null; } } + + + @Override + public boolean equals(@Nullable final Object obj) { + return (obj instanceof NotificationActionData other) + && Objects.equals(this.action, other.action) + && this.name.equals(other.name) + && this.icon == other.icon; + } + + @Override + public int hashCode() { + return Objects.hash(action, name, icon); + } } From 4b1824e8c16aed9725ba970ca00b8cae37171cf6 Mon Sep 17 00:00:00 2001 From: Stypox Date: Fri, 29 Dec 2023 15:13:18 +0100 Subject: [PATCH 8/9] Allow play/pausing from notification when buffering This change is in line with a recent change in how the play/pause button behaves in the player ui: if the buffering indicator is shown, it's still possible to toggle play/pause, to allow e.g. pausing videos before they even start. This change was needed because on Android 13+ notification actions can't be null, and thus the buffering hourglass action wasn't shown. --- .../SessionConnectorActionProvider.java | 10 +++------- .../notification/NotificationActionData.java | 16 ++++++++++------ .../player/notification/NotificationUtil.java | 10 ++-------- 3 files changed, 15 insertions(+), 21 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/mediasession/SessionConnectorActionProvider.java b/app/src/main/java/org/schabi/newpipe/player/mediasession/SessionConnectorActionProvider.java index 7b109c149..a5c9fccc9 100644 --- a/app/src/main/java/org/schabi/newpipe/player/mediasession/SessionConnectorActionProvider.java +++ b/app/src/main/java/org/schabi/newpipe/player/mediasession/SessionConnectorActionProvider.java @@ -40,12 +40,8 @@ public class SessionConnectorActionProvider implements MediaSessionConnector.Cus @Nullable @Override public PlaybackStateCompat.CustomAction getCustomAction(@NonNull final Player player) { - if (data.action() == null) { - return null; - } else { - return new PlaybackStateCompat.CustomAction.Builder( - data.action(), data.name(), data.icon() - ).build(); - } + return new PlaybackStateCompat.CustomAction.Builder( + data.action(), data.name(), data.icon() + ).build(); } } diff --git a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationActionData.java b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationActionData.java index 98ee3d7b8..b3abcd0b5 100644 --- a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationActionData.java +++ b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationActionData.java @@ -11,6 +11,7 @@ import static org.schabi.newpipe.player.notification.NotificationConstants.ACTIO import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_REPEAT; import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_SHUFFLE; +import android.annotation.SuppressLint; import android.content.Context; import androidx.annotation.DrawableRes; @@ -23,21 +24,23 @@ import org.schabi.newpipe.player.Player; import java.util.Objects; public final class NotificationActionData { - @Nullable + + @NonNull private final String action; @NonNull private final String name; @DrawableRes private final int icon; - public NotificationActionData(@Nullable final String action, @NonNull final String name, + + public NotificationActionData(@NonNull final String action, @NonNull final String name, @DrawableRes final int icon) { this.action = action; this.name = name; this.icon = icon; } - @Nullable + @NonNull public String action() { return action; } @@ -52,6 +55,8 @@ public final class NotificationActionData { return icon; } + + @SuppressLint("PrivateResource") // we currently use Exoplayer's internal strings and icons @Nullable public static NotificationActionData fromNotificationActionEnum( @NonNull final Player player, @@ -105,8 +110,7 @@ public final class NotificationActionData { if (player.getCurrentState() == Player.STATE_PREFLIGHT || player.getCurrentState() == Player.STATE_BLOCKED || player.getCurrentState() == Player.STATE_BUFFERING) { - // null intent action -> show hourglass icon that does nothing when clicked - return new NotificationActionData(null, + return new NotificationActionData(ACTION_PLAY_PAUSE, ctx.getString(R.string.notification_action_buffering), R.drawable.ic_hourglass_top); } @@ -171,7 +175,7 @@ public final class NotificationActionData { @Override public boolean equals(@Nullable final Object obj) { return (obj instanceof NotificationActionData other) - && Objects.equals(this.action, other.action) + && this.action.equals(other.action) && this.name.equals(other.name) && this.icon == other.icon; } diff --git a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java index b3cfed1ce..30420b0c7 100644 --- a/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java +++ b/app/src/main/java/org/schabi/newpipe/player/notification/NotificationUtil.java @@ -244,14 +244,8 @@ public final class NotificationUtil { return; } - final PendingIntent intent; - if (data.action() == null) { - intent = null; - } else { - intent = PendingIntentCompat.getBroadcast(player.getContext(), NOTIFICATION_ID, - new Intent(data.action()), FLAG_UPDATE_CURRENT, false); - } - + final PendingIntent intent = PendingIntentCompat.getBroadcast(player.getContext(), + NOTIFICATION_ID, new Intent(data.action()), FLAG_UPDATE_CURRENT, false); builder.addAction(new NotificationCompat.Action(data.icon(), data.name(), intent)); } From f98548698af0e4d5a58c99a187b35c3e92d10119 Mon Sep 17 00:00:00 2001 From: Stypox Date: Sat, 30 Dec 2023 21:55:32 +0100 Subject: [PATCH 9/9] Android 33 -> Android 13 Co-authored-by: Tobi --- .../org/schabi/newpipe/settings/custom/NotificationSlot.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationSlot.java b/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationSlot.java index 074532876..981ba3e75 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationSlot.java +++ b/app/src/main/java/org/schabi/newpipe/settings/custom/NotificationSlot.java @@ -89,7 +89,7 @@ class NotificationSlot { void setupCheckbox(final View view, final boolean isCompactSlotChecked) { final CheckBox compactSlotCheckBox = view.findViewById(R.id.notificationActionCheckBox); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - // there are no compact slots to customize on Android 33+ + // there are no compact slots to customize on Android 13+ compactSlotCheckBox.setVisibility(View.GONE); view.findViewById(R.id.notificationActionCheckBoxClickableArea) .setVisibility(View.GONE);