From 11e048abb1fc1eee6588b93da87657d197381c27 Mon Sep 17 00:00:00 2001 From: Stypox Date: Tue, 22 Sep 2020 16:46:09 +0200 Subject: [PATCH] Remove hardcoded and duplicate strings, use exoplayer ones --- .../newpipe/player/NotificationConstants.java | 58 +++++++--- .../newpipe/player/NotificationUtil.java | 103 +++++++++--------- .../NotificationSettingsFragment.java | 7 +- .../res/layout/related_streams_header.xml | 2 +- app/src/main/res/values/strings.xml | 11 +- 5 files changed, 95 insertions(+), 86 deletions(-) diff --git a/app/src/main/java/org/schabi/newpipe/player/NotificationConstants.java b/app/src/main/java/org/schabi/newpipe/player/NotificationConstants.java index 599e18e65..cf58c8f76 100644 --- a/app/src/main/java/org/schabi/newpipe/player/NotificationConstants.java +++ b/app/src/main/java/org/schabi/newpipe/player/NotificationConstants.java @@ -6,9 +6,9 @@ import android.content.SharedPreferences; import androidx.annotation.DrawableRes; import androidx.annotation.IntDef; import androidx.annotation.NonNull; -import androidx.annotation.StringRes; import org.schabi.newpipe.R; +import org.schabi.newpipe.util.Localization; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -41,22 +41,6 @@ public final class NotificationConstants { PLAY_PAUSE, PLAY_PAUSE_BUFFERING, REPEAT, SHUFFLE, CLOSE}) public @interface Action { } - @StringRes - public static final int[] ACTION_SUMMARIES = { - R.string.notification_action_nothing, - R.string.notification_action_previous, - R.string.notification_action_next, - R.string.notification_action_rewind, - R.string.notification_action_forward, - R.string.notification_action_smart_rewind_previous, - R.string.notification_action_smart_forward_next, - R.string.notification_action_play_pause, - R.string.notification_action_play_pause_buffering, - R.string.notification_action_repeat, - R.string.notification_action_shuffle, - R.string.close, - }; - @DrawableRes public static final int[] ACTION_ICONS = { 0, @@ -110,6 +94,46 @@ public final class NotificationConstants { R.string.notification_slot_compact_2_key, }; + + public static String getActionName(@NonNull final Context context, @Action final int action) { + switch (action) { + case PREVIOUS: + return context.getString(R.string.exo_controls_previous_description); + case NEXT: + return context.getString(R.string.exo_controls_next_description); + case REWIND: + return context.getString(R.string.exo_controls_rewind_description); + case FORWARD: + return context.getString(R.string.exo_controls_fastforward_description); + case SMART_REWIND_PREVIOUS: + return Localization.concatenateStrings( + context.getString(R.string.exo_controls_rewind_description), + context.getString(R.string.exo_controls_previous_description)); + case SMART_FORWARD_NEXT: + return Localization.concatenateStrings( + context.getString(R.string.exo_controls_fastforward_description), + context.getString(R.string.exo_controls_next_description)); + case PLAY_PAUSE: + return Localization.concatenateStrings( + context.getString(R.string.exo_controls_play_description), + context.getString(R.string.exo_controls_pause_description)); + case PLAY_PAUSE_BUFFERING: + return Localization.concatenateStrings( + context.getString(R.string.exo_controls_play_description), + context.getString(R.string.exo_controls_pause_description), + context.getString(R.string.notification_action_buffering)); + case REPEAT: + return context.getString(R.string.notification_action_repeat); + case SHUFFLE: + return context.getString(R.string.notification_action_shuffle); + case CLOSE: + return context.getString(R.string.close); + case NOTHING: default: + return context.getString(R.string.notification_action_nothing); + } + } + + /** * @param context the context to use * @param sharedPreferences the shared preferences to query values from diff --git a/app/src/main/java/org/schabi/newpipe/player/NotificationUtil.java b/app/src/main/java/org/schabi/newpipe/player/NotificationUtil.java index cde280793..577c82557 100644 --- a/app/src/main/java/org/schabi/newpipe/player/NotificationUtil.java +++ b/app/src/main/java/org/schabi/newpipe/player/NotificationUtil.java @@ -12,6 +12,7 @@ import android.util.Log; import androidx.annotation.DrawableRes; import androidx.annotation.Nullable; +import androidx.annotation.StringRes; import androidx.core.app.NotificationCompat; import androidx.core.app.NotificationManagerCompat; import androidx.core.content.ContextCompat; @@ -214,75 +215,89 @@ public final class NotificationUtil { final int baseActionIcon = NotificationConstants.ACTION_ICONS[selectedAction]; switch (selectedAction) { case NotificationConstants.PREVIOUS: - return getAction(player, baseActionIcon, "Previous", ACTION_PLAY_PREVIOUS); + return getAction(player, baseActionIcon, + R.string.exo_controls_previous_description, ACTION_PLAY_PREVIOUS); case NotificationConstants.NEXT: - return getAction(player, baseActionIcon, "Next", ACTION_PLAY_NEXT); + return getAction(player, baseActionIcon, + R.string.exo_controls_next_description, ACTION_PLAY_NEXT); case NotificationConstants.REWIND: - return getAction(player, baseActionIcon, "Rewind", ACTION_FAST_REWIND); + return getAction(player, baseActionIcon, + R.string.exo_controls_rewind_description, ACTION_FAST_REWIND); case NotificationConstants.FORWARD: - return getAction(player, baseActionIcon, "Forward", ACTION_FAST_FORWARD); + return getAction(player, baseActionIcon, + R.string.exo_controls_fastforward_description, ACTION_FAST_FORWARD); case NotificationConstants.SMART_REWIND_PREVIOUS: if (player.playQueue != null && player.playQueue.size() > 1) { return getAction(player, R.drawable.exo_notification_previous, - "Previous", ACTION_PLAY_PREVIOUS); + R.string.exo_controls_previous_description, ACTION_PLAY_PREVIOUS); } else { return getAction(player, R.drawable.exo_controls_rewind, - "Rewind", ACTION_FAST_REWIND); + R.string.exo_controls_rewind_description, ACTION_FAST_REWIND); } case NotificationConstants.SMART_FORWARD_NEXT: if (player.playQueue != null && player.playQueue.size() > 1) { return getAction(player, R.drawable.exo_notification_next, - "Next", ACTION_PLAY_NEXT); + R.string.exo_controls_next_description, ACTION_PLAY_NEXT); } else { return getAction(player, R.drawable.exo_controls_fastforward, - "Forward", ACTION_FAST_FORWARD); + R.string.exo_controls_fastforward_description, ACTION_FAST_FORWARD); } case NotificationConstants.PLAY_PAUSE: - final boolean pauseOrPlay = player.isPlaying() + if (player.isPlaying() || player.getCurrentState() == BasePlayer.STATE_PREFLIGHT || player.getCurrentState() == BasePlayer.STATE_BLOCKED - || player.getCurrentState() == BasePlayer.STATE_BUFFERING; - return getAction(player, - pauseOrPlay ? R.drawable.exo_notification_pause - : R.drawable.exo_notification_play, - pauseOrPlay ? "Pause" : "Play", - ACTION_PLAY_PAUSE); + || player.getCurrentState() == BasePlayer.STATE_BUFFERING) { + return getAction(player, R.drawable.exo_notification_pause, + R.string.exo_controls_pause_description, ACTION_PLAY_PAUSE); + } else { + return getAction(player, R.drawable.exo_notification_play, + R.string.exo_controls_play_description, ACTION_PLAY_PAUSE); + } case NotificationConstants.PLAY_PAUSE_BUFFERING: if (player.getCurrentState() == BasePlayer.STATE_PREFLIGHT || player.getCurrentState() == BasePlayer.STATE_BLOCKED || player.getCurrentState() == BasePlayer.STATE_BUFFERING) { return getAction(player, R.drawable.ic_hourglass_top_white_24dp_png, - "Buffering", ACTION_BUFFERING); + R.string.notification_action_buffering, ACTION_BUFFERING); + } else if (player.isPlaying()) { + return getAction(player, R.drawable.exo_notification_pause, + R.string.exo_controls_pause_description, ACTION_PLAY_PAUSE); } else { - return getAction(player, - player.isPlaying() ? R.drawable.exo_notification_pause - : R.drawable.exo_notification_play, - player.isPlaying() ? "Pause" : "Play", - ACTION_PLAY_PAUSE); + return getAction(player, R.drawable.exo_notification_play, + R.string.exo_controls_play_description, ACTION_PLAY_PAUSE); } case NotificationConstants.REPEAT: - return getAction(player, getRepeatModeDrawable(player.getRepeatMode()), - getRepeatModeTitle(player.getRepeatMode()), ACTION_REPEAT); + if (player.getRepeatMode() == REPEAT_MODE_ALL) { + return getAction(player, 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(player, R.drawable.exo_media_action_repeat_one, + R.string.exo_controls_repeat_one_description, ACTION_REPEAT); + } else /* player.getRepeatMode() == REPEAT_MODE_OFF */ { + return getAction(player, R.drawable.exo_media_action_repeat_off, + R.string.exo_controls_repeat_off_description, ACTION_REPEAT); + } case NotificationConstants.SHUFFLE: - final boolean shuffled = player.playQueue != null && player.playQueue.isShuffled(); - return getAction(player, - shuffled ? R.drawable.exo_controls_shuffle_on - : R.drawable.exo_controls_shuffle_off, - shuffled ? "ShuffleOn" : "ShuffleOff", - ACTION_SHUFFLE); + if (player.playQueue != null && player.playQueue.isShuffled()) { + return getAction(player, R.drawable.exo_controls_shuffle_on, + R.string.exo_controls_shuffle_on_description, ACTION_SHUFFLE); + } else { + return getAction(player, R.drawable.exo_controls_shuffle_off, + R.string.exo_controls_shuffle_off_description, ACTION_SHUFFLE); + } case NotificationConstants.CLOSE: return getAction(player, R.drawable.ic_close_white_24dp_png, - "Close", ACTION_CLOSE); + R.string.close, ACTION_CLOSE); case NotificationConstants.NOTHING: default: @@ -293,31 +308,11 @@ public final class NotificationUtil { private NotificationCompat.Action getAction(final VideoPlayerImpl player, @DrawableRes final int drawable, - final String title, + @StringRes final int title, final String intentAction) { - return new NotificationCompat.Action(drawable, title, PendingIntent.getBroadcast( - player.context, NOTIFICATION_ID, new Intent(intentAction), FLAG_UPDATE_CURRENT)); - } - - @DrawableRes - private int getRepeatModeDrawable(final int repeatMode) { - if (repeatMode == REPEAT_MODE_ALL) { - return R.drawable.exo_controls_repeat_all; - } else if (repeatMode == REPEAT_MODE_ONE) { - return R.drawable.exo_controls_repeat_one; - } else /* repeatMode == REPEAT_MODE_OFF */ { - return R.drawable.exo_controls_repeat_off; - } - } - - private String getRepeatModeTitle(final int repeatMode) { - if (repeatMode == REPEAT_MODE_ALL) { - return "RepeatAll"; - } else if (repeatMode == REPEAT_MODE_ONE) { - return "RepeatOne"; - } else /* repeatMode == REPEAT_MODE_OFF */ { - return "RepeatOff"; - } + return new NotificationCompat.Action(drawable, player.context.getString(title), + PendingIntent.getBroadcast(player.context, NOTIFICATION_ID, + new Intent(intentAction), FLAG_UPDATE_CURRENT)); } private Intent getIntentForNotification(final VideoPlayerImpl player) { diff --git a/app/src/main/java/org/schabi/newpipe/settings/NotificationSettingsFragment.java b/app/src/main/java/org/schabi/newpipe/settings/NotificationSettingsFragment.java index f4bbc96a7..ce1e9e5a5 100644 --- a/app/src/main/java/org/schabi/newpipe/settings/NotificationSettingsFragment.java +++ b/app/src/main/java/org/schabi/newpipe/settings/NotificationSettingsFragment.java @@ -209,7 +209,7 @@ public class NotificationSettingsFragment extends Fragment { NotificationConstants.ACTION_ICONS[selectedAction])); } - summary.setText(NotificationConstants.ACTION_SUMMARIES[selectedAction]); + summary.setText(NotificationConstants.getActionName(requireContext(), selectedAction)); } void openActionChooserDialog() { @@ -225,8 +225,7 @@ public class NotificationSettingsFragment extends Fragment { .create(); final View.OnClickListener radioButtonsClickListener = v -> { - final int id = ((RadioButton) v).getId(); - selectedAction = NotificationConstants.SLOT_ALLOWED_ACTIONS[i][id]; + selectedAction = NotificationConstants.SLOT_ALLOWED_ACTIONS[i][v.getId()]; updateInfo(); alertDialog.dismiss(); }; @@ -253,7 +252,7 @@ public class NotificationSettingsFragment extends Fragment { } } - radioButton.setText(NotificationConstants.ACTION_SUMMARIES[action]); + radioButton.setText(NotificationConstants.getActionName(requireContext(), action)); radioButton.setChecked(action == selectedAction); radioButton.setId(id); radioButton.setLayoutParams(new RadioGroup.LayoutParams( diff --git a/app/src/main/res/layout/related_streams_header.xml b/app/src/main/res/layout/related_streams_header.xml index b98244b7e..77be2247b 100644 --- a/app/src/main/res/layout/related_streams_header.xml +++ b/app/src/main/res/layout/related_streams_header.xml @@ -12,7 +12,7 @@ android:layout_height="wrap_content" android:layout_marginLeft="12dp" android:layout_alignBaseline="@+id/autoplay_switch" - android:text="@string/next_video_title" + android:text="@string/exo_controls_next_description" android:textAppearance="?android:attr/textAppearanceMedium" android:textSize="12sp" tools:ignore="RtlHardcoded" /> diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 1f4be4c83..c9119783b 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -69,16 +69,9 @@ Edit each notification action below by tapping on it.\nSelect up to three of them to be shown in the compact notification by using the checkboxes on the right. You can select at most three actions to show in the compact notification! - Previous - Next - Rewind - Forward - Rewind / Previous - Forward / Next - Play / Pause / Buffering - Play / Pause Repeat Shuffle + Buffering Nothing Audio @@ -126,7 +119,6 @@ Resume playing Continue playing after interruptions (e.g. phonecalls) Download - Next Autoplay Show \'Next\' and \'Similar\' videos Show \"Hold to append\" tip @@ -159,7 +151,6 @@ Queued on background player Queued on popup player https://www.c3s.cc/ - Play Content Age restricted content Show age restricted video. Future changes are possible from the settings.