2020-10-31 11:58:33 +01:00
|
|
|
package org.schabi.newpipe.settings.custom;
|
2020-09-08 19:02:05 +02:00
|
|
|
|
2022-04-14 18:40:55 +02:00
|
|
|
import static org.schabi.newpipe.player.notification.NotificationConstants.ACTION_RECREATE_NOTIFICATION;
|
|
|
|
|
2020-10-31 11:58:33 +01:00
|
|
|
import android.content.Context;
|
2020-09-08 19:02:05 +02:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.content.SharedPreferences;
|
|
|
|
import android.graphics.drawable.Drawable;
|
2020-10-31 11:58:33 +01:00
|
|
|
import android.util.AttributeSet;
|
2020-09-08 19:02:05 +02:00
|
|
|
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;
|
2021-01-15 17:11:04 +01:00
|
|
|
|
2020-09-08 19:02:05 +02:00
|
|
|
import androidx.annotation.NonNull;
|
2021-03-28 22:31:03 +02:00
|
|
|
import androidx.annotation.Nullable;
|
2020-09-08 19:02:05 +02:00
|
|
|
import androidx.appcompat.app.AlertDialog;
|
|
|
|
import androidx.appcompat.content.res.AppCompatResources;
|
2020-10-22 01:49:51 +02:00
|
|
|
import androidx.core.graphics.drawable.DrawableCompat;
|
2020-10-31 11:58:33 +01:00
|
|
|
import androidx.preference.Preference;
|
|
|
|
import androidx.preference.PreferenceViewHolder;
|
2021-01-15 17:11:04 +01:00
|
|
|
|
2022-07-09 17:58:03 +02:00
|
|
|
import org.schabi.newpipe.App;
|
2020-09-08 19:02:05 +02:00
|
|
|
import org.schabi.newpipe.R;
|
2022-05-28 00:39:02 +02:00
|
|
|
import org.schabi.newpipe.databinding.ListRadioIconItemBinding;
|
|
|
|
import org.schabi.newpipe.databinding.SingleChoiceDialogViewBinding;
|
2022-04-14 18:40:55 +02:00
|
|
|
import org.schabi.newpipe.player.notification.NotificationConstants;
|
2020-09-08 19:02:05 +02:00
|
|
|
import org.schabi.newpipe.util.DeviceUtils;
|
|
|
|
import org.schabi.newpipe.util.ThemeHelper;
|
|
|
|
import org.schabi.newpipe.views.FocusOverlayView;
|
|
|
|
|
2022-07-29 21:21:15 +02:00
|
|
|
import java.util.ArrayList;
|
2021-01-15 17:11:04 +01:00
|
|
|
import java.util.List;
|
2022-07-29 21:21:15 +02:00
|
|
|
import java.util.stream.IntStream;
|
2021-01-15 17:11:04 +01:00
|
|
|
|
2020-10-31 11:58:33 +01:00
|
|
|
public class NotificationActionsPreference extends Preference {
|
|
|
|
|
|
|
|
public NotificationActionsPreference(final Context context, final AttributeSet attrs) {
|
|
|
|
super(context, attrs);
|
|
|
|
setLayoutResource(R.layout.settings_notification);
|
|
|
|
}
|
2020-09-08 19:02:05 +02:00
|
|
|
|
|
|
|
|
2021-03-28 22:31:03 +02:00
|
|
|
@Nullable private NotificationSlot[] notificationSlots = null;
|
|
|
|
@Nullable private List<Integer> compactSlots = null;
|
2020-09-08 19:02:05 +02:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Lifecycle
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
@Override
|
2022-03-18 18:15:44 +01:00
|
|
|
public void onBindViewHolder(@NonNull final PreferenceViewHolder holder) {
|
2020-10-31 11:58:33 +01:00
|
|
|
super.onBindViewHolder(holder);
|
2020-09-08 19:02:05 +02:00
|
|
|
|
2020-10-31 11:58:33 +01:00
|
|
|
holder.itemView.setClickable(false);
|
|
|
|
setupActions(holder.itemView);
|
2020-09-08 19:02:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2020-10-31 11:58:33 +01:00
|
|
|
public void onDetached() {
|
|
|
|
super.onDetached();
|
2020-09-08 19:02:05 +02:00
|
|
|
saveChanges();
|
2022-07-09 17:58:03 +02:00
|
|
|
// set package to this app's package to prevent the intent from being seen outside
|
|
|
|
getContext().sendBroadcast(new Intent(ACTION_RECREATE_NOTIFICATION)
|
|
|
|
.setPackage(App.PACKAGE_NAME));
|
2020-09-08 19:02:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Setup
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
private void setupActions(@NonNull final View view) {
|
2022-07-29 21:21:15 +02:00
|
|
|
compactSlots = new ArrayList<>(NotificationConstants
|
|
|
|
.getCompactSlotsFromPreferences(getContext(), getSharedPreferences(), 5));
|
|
|
|
notificationSlots = IntStream.range(0, 5).mapToObj(i -> new NotificationSlot(i, view))
|
|
|
|
.toArray(NotificationSlot[]::new);
|
2020-09-08 19:02:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Saving
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
private void saveChanges() {
|
2021-03-28 22:31:03 +02:00
|
|
|
if (compactSlots != null && notificationSlots != null) {
|
|
|
|
final SharedPreferences.Editor editor = getSharedPreferences().edit();
|
2020-09-08 19:02:05 +02:00
|
|
|
|
2021-03-28 22:31:03 +02:00
|
|
|
for (int i = 0; i < 3; i++) {
|
|
|
|
editor.putInt(getContext().getString(
|
|
|
|
NotificationConstants.SLOT_COMPACT_PREF_KEYS[i]),
|
|
|
|
(i < compactSlots.size() ? compactSlots.get(i) : -1));
|
|
|
|
}
|
2020-09-08 19:02:05 +02:00
|
|
|
|
2021-03-28 22:31:03 +02:00
|
|
|
for (int i = 0; i < 5; i++) {
|
|
|
|
editor.putInt(getContext().getString(NotificationConstants.SLOT_PREF_KEYS[i]),
|
|
|
|
notificationSlots[i].selectedAction);
|
|
|
|
}
|
2020-09-08 19:02:05 +02:00
|
|
|
|
2021-03-28 22:31:03 +02:00
|
|
|
editor.apply();
|
|
|
|
}
|
2020-09-08 19:02:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
final View view = parentView.findViewById(SLOT_ITEMS[i]);
|
|
|
|
setupSelectedAction(view);
|
|
|
|
setupTitle(view);
|
|
|
|
setupCheckbox(view);
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
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 {
|
2020-10-31 11:58:33 +01:00
|
|
|
Toast.makeText(getContext(),
|
2020-09-08 19:02:05 +02:00
|
|
|
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);
|
2020-10-31 11:58:33 +01:00
|
|
|
selectedAction = getSharedPreferences().getInt(
|
|
|
|
getContext().getString(NotificationConstants.SLOT_PREF_KEYS[i]),
|
2020-09-08 19:02:05 +02:00
|
|
|
NotificationConstants.SLOT_DEFAULTS[i]);
|
|
|
|
updateInfo();
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateInfo() {
|
|
|
|
if (NotificationConstants.ACTION_ICONS[selectedAction] == 0) {
|
|
|
|
icon.setImageDrawable(null);
|
|
|
|
} else {
|
2020-10-31 11:58:33 +01:00
|
|
|
icon.setImageDrawable(AppCompatResources.getDrawable(getContext(),
|
2020-09-08 19:02:05 +02:00
|
|
|
NotificationConstants.ACTION_ICONS[selectedAction]));
|
|
|
|
}
|
|
|
|
|
2020-10-31 11:58:33 +01:00
|
|
|
summary.setText(NotificationConstants.getActionName(getContext(), selectedAction));
|
2020-09-08 19:02:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void openActionChooserDialog() {
|
2020-10-31 11:58:33 +01:00
|
|
|
final LayoutInflater inflater = LayoutInflater.from(getContext());
|
2022-05-28 00:39:02 +02:00
|
|
|
final SingleChoiceDialogViewBinding binding =
|
|
|
|
SingleChoiceDialogViewBinding.inflate(inflater);
|
2020-09-08 19:02:05 +02:00
|
|
|
|
2020-10-31 11:58:33 +01:00
|
|
|
final AlertDialog alertDialog = new AlertDialog.Builder(getContext())
|
2020-09-08 19:02:05 +02:00
|
|
|
.setTitle(SLOT_TITLES[i])
|
2022-05-28 00:39:02 +02:00
|
|
|
.setView(binding.getRoot())
|
2020-09-08 19:02:05 +02:00
|
|
|
.setCancelable(true)
|
|
|
|
.create();
|
|
|
|
|
|
|
|
final View.OnClickListener radioButtonsClickListener = v -> {
|
2020-09-22 16:46:09 +02:00
|
|
|
selectedAction = NotificationConstants.SLOT_ALLOWED_ACTIONS[i][v.getId()];
|
2020-09-08 19:02:05 +02:00
|
|
|
updateInfo();
|
|
|
|
alertDialog.dismiss();
|
|
|
|
};
|
|
|
|
|
|
|
|
for (int id = 0; id < NotificationConstants.SLOT_ALLOWED_ACTIONS[i].length; ++id) {
|
|
|
|
final int action = NotificationConstants.SLOT_ALLOWED_ACTIONS[i][id];
|
2022-05-28 00:39:02 +02:00
|
|
|
final RadioButton radioButton = ListRadioIconItemBinding.inflate(inflater)
|
|
|
|
.getRoot();
|
2020-09-08 19:02:05 +02:00
|
|
|
|
|
|
|
// if present set action icon with correct color
|
|
|
|
if (NotificationConstants.ACTION_ICONS[action] != 0) {
|
2020-10-31 11:58:33 +01:00
|
|
|
Drawable drawable = AppCompatResources.getDrawable(getContext(),
|
2020-09-08 19:02:05 +02:00
|
|
|
NotificationConstants.ACTION_ICONS[action]);
|
|
|
|
if (drawable != null) {
|
2020-10-31 11:58:33 +01:00
|
|
|
final int color = ThemeHelper.resolveColorFromAttr(getContext(),
|
2020-09-08 19:02:05 +02:00
|
|
|
android.R.attr.textColorPrimary);
|
2020-10-22 01:49:51 +02:00
|
|
|
drawable = DrawableCompat.wrap(drawable).mutate();
|
2022-07-06 23:48:38 +02:00
|
|
|
drawable.setTint(color);
|
2022-05-10 04:15:01 +02:00
|
|
|
radioButton.setCompoundDrawablesRelativeWithIntrinsicBounds(null,
|
|
|
|
null, drawable, null);
|
2020-09-08 19:02:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-31 11:58:33 +01:00
|
|
|
radioButton.setText(NotificationConstants.getActionName(getContext(), action));
|
2020-09-08 19:02:05 +02:00
|
|
|
radioButton.setChecked(action == selectedAction);
|
|
|
|
radioButton.setId(id);
|
|
|
|
radioButton.setLayoutParams(new RadioGroup.LayoutParams(
|
|
|
|
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
|
|
|
radioButton.setOnClickListener(radioButtonsClickListener);
|
2022-05-28 00:39:02 +02:00
|
|
|
binding.list.addView(radioButton);
|
2020-09-08 19:02:05 +02:00
|
|
|
}
|
|
|
|
alertDialog.show();
|
|
|
|
|
2020-10-31 11:58:33 +01:00
|
|
|
if (DeviceUtils.isTv(getContext())) {
|
2020-09-08 19:02:05 +02:00
|
|
|
FocusOverlayView.setupFocusObserver(alertDialog);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|