Customize only 2 notification actions on Android 13+

This commit is contained in:
Stypox 2023-12-29 12:13:08 +01:00
parent 5a4dae2070
commit 30f0db1d28
No known key found for this signature in database
GPG key ID: 4BDF1B40A49FDD23
3 changed files with 31 additions and 10 deletions

View file

@ -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();
}

View file

@ -7,7 +7,7 @@
android:paddingTop="16dp">
<org.schabi.newpipe.views.NewPipeTextView
android:id="@+id/textView"
android:id="@+id/summary"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
@ -29,7 +29,7 @@
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
app:layout_constraintTop_toBottomOf="@+id/summary" />
<include
android:id="@+id/notificationAction1"

View file

@ -57,7 +57,8 @@
<string name="notification_action_2_title">Third action button</string>
<string name="notification_action_3_title">Fourth action button</string>
<string name="notification_action_4_title">Fifth action button</string>
<string name="notification_actions_summary">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</string>
<string name="notification_actions_summary">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.</string>
<string name="notification_actions_summary_android13">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.</string>
<string name="notification_actions_at_most_three">You can select at most three actions to show in the compact notification!</string>
<string name="notification_action_repeat">Repeat</string>
<string name="notification_action_shuffle">Shuffle</string>