summaryrefslogtreecommitdiff
path: root/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java
diff options
context:
space:
mode:
Diffstat (limited to 'app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java')
-rw-r--r--app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java163
1 files changed, 99 insertions, 64 deletions
diff --git a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java
index 48b578be7..5656a1afe 100644
--- a/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java
+++ b/app/src/main/java/de/danoeh/antennapod/activity/MediaplayerActivity.java
@@ -1,6 +1,7 @@
package de.danoeh.antennapod.activity;
import android.annotation.TargetApi;
+import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.TypedArray;
@@ -9,6 +10,7 @@ import android.graphics.PixelFormat;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
+import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Menu;
@@ -42,6 +44,7 @@ import de.danoeh.antennapod.core.util.Converter;
import de.danoeh.antennapod.core.util.Flavors;
import de.danoeh.antennapod.core.util.ShareUtils;
import de.danoeh.antennapod.core.util.StorageUtils;
+import de.danoeh.antennapod.core.util.Supplier;
import de.danoeh.antennapod.core.util.playback.MediaPlayerError;
import de.danoeh.antennapod.core.util.playback.Playable;
import de.danoeh.antennapod.core.util.playback.PlaybackController;
@@ -49,6 +52,8 @@ import de.danoeh.antennapod.dialog.SleepTimerDialog;
import de.danoeh.antennapod.dialog.VariableSpeedDialog;
import rx.Observable;
import rx.android.schedulers.AndroidSchedulers;
+import rx.functions.Action1;
+import rx.functions.Func1;
import rx.schedulers.Schedulers;
@@ -178,6 +183,13 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
};
}
+ static protected TextView getTxtvFFFromActivity(MediaplayerActivity activity) {
+ return activity.txtvFF;
+ }
+ static protected TextView getTxtvRevFromActivity(MediaplayerActivity activity) {
+ return activity.txtvRev;
+ }
+
protected void onSetSpeedAbilityChanged() {
Log.d(TAG, "onSetSpeedAbilityChanged()");
updatePlaybackSpeedButton();
@@ -650,6 +662,85 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
// Only meaningful on AudioplayerActivity, where it is overridden.
}
+ /**
+ * Abstract directions to skip forward or back (rewind) and encapsulates behavior to get or set preference (including update of UI on the skip buttons).
+ */
+ static public enum SkipDirection {
+ SKIP_FORWARD(UserPreferences::getFastForwardSecs, MediaplayerActivity::getTxtvFFFromActivity, UserPreferences::setFastForwardSecs, R.string.pref_fast_forward),
+ SKIP_REWIND(UserPreferences::getRewindSecs, MediaplayerActivity::getTxtvRevFromActivity, UserPreferences::setRewindSecs, R.string.pref_rewind);
+
+ private final Supplier<Integer> getPrefSecsFn;
+ private final Func1<MediaplayerActivity, TextView> getTextViewFn;
+ private final Action1<Integer> setPrefSecsFn;
+ private final int titleResourceID;
+
+ /**
+ * Constructor for skip direction enum. Stores references to utility functions and resource
+ * id's that vary dependending on the direction.
+ *
+ * @param getPrefSecsFn Handle to function that retrieves current seconds of the skip delta
+ * @param getTextViewFn Handle to function that gets the TextView which displays the current skip delta value
+ * @param setPrefSecsFn Handle to function that sets the preference (setting) for the skip delta value (and optionally updates the button label with the current values)
+ * @param titleResourceID ID of the resource string with the title for a view
+ */
+ SkipDirection(Supplier<Integer> getPrefSecsFn, Func1<MediaplayerActivity, TextView> getTextViewFn, Action1<Integer> setPrefSecsFn, int titleResourceID) {
+ this.getPrefSecsFn = getPrefSecsFn;
+ this.getTextViewFn = getTextViewFn;
+ this.setPrefSecsFn = setPrefSecsFn;
+ this.titleResourceID = titleResourceID;
+ }
+
+
+ public int getPrefSkipSeconds() {
+ return(getPrefSecsFn.get());
+ }
+
+ /**
+ * Updates preferences for a forward or backward skip depending on the direction of the instance, optionally updating the UI.
+ *
+ * @param seconds Number of seconds to set the preference associated with the direction of the instance.
+ * @param activity MediaplyerActivity that contains textview to update the display of the skip delta setting (or null if nothing to update)
+ */
+ public void setPrefSkipSeconds(int seconds, @Nullable Activity activity) {
+ setPrefSecsFn.call(seconds);
+
+ /// Optionlally, update display of skip value on ff/rewind buttons in mediaplayer
+ if (activity != null && activity instanceof MediaplayerActivity) {
+ TextView tv = getTextViewFn.call((MediaplayerActivity)activity);
+ if (tv != null) tv.setText(String.valueOf(seconds));
+ }
+ }
+ public int getTitleResourceID() {
+ return titleResourceID;
+ }
+ }
+
+ static public void showSkipPreference(Activity activity, SkipDirection direction) {
+ int checked = 0;
+ int skipSecs = direction.getPrefSkipSeconds();
+ final int[] values = activity.getResources().getIntArray(R.array.seek_delta_values);
+ final String[] choices = new String[values.length];
+ for (int i = 0; i < values.length; i++) {
+ if (skipSecs == values[i]) {
+ checked = i;
+ }
+ choices[i] = String.valueOf(values[i]) + " " + activity.getString(R.string.time_seconds);
+ }
+
+ AlertDialog.Builder builder = new AlertDialog.Builder(activity);
+ builder.setTitle(direction.getTitleResourceID());
+ builder.setSingleChoiceItems(choices, checked, null);
+ builder.setNegativeButton(R.string.cancel_label, null);
+ builder.setPositiveButton(R.string.confirm_label, (dialog, which) -> {
+ int choice = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
+ if (choice < 0 || choice >= values.length) {
+ System.err.printf("Choice in showSkipPreference is out of bounds %d", choice);
+ } else {
+ direction.setPrefSkipSeconds(values[choice], activity);
+ }
+ });
+ builder.create().show();
+ }
protected void setupGUI() {
setContentView(getContentViewResourceId());
@@ -692,7 +783,7 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
butFF = (ImageButton) findViewById(R.id.butFF);
txtvFF = (TextView) findViewById(R.id.txtvFF);
if (txtvFF != null) {
- txtvFF.setText(String.valueOf(UserPreferences.getFastFowardSecs()));
+ txtvFF.setText(String.valueOf(UserPreferences.getFastForwardSecs()));
}
butSkip = (ImageButton) findViewById(R.id.butSkip);
@@ -704,37 +795,9 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
if (butRev != null) {
butRev.setOnClickListener(v -> onRewind());
- butRev.setOnLongClickListener(new View.OnLongClickListener() {
-
- int choice;
-
- @Override
- public boolean onLongClick(View v) {
- int checked = 0;
- int rewindSecs = UserPreferences.getRewindSecs();
- final int[] values = getResources().getIntArray(R.array.seek_delta_values);
- final String[] choices = new String[values.length];
- for (int i = 0; i < values.length; i++) {
- if (rewindSecs == values[i]) {
- checked = i;
- }
- choices[i] = String.valueOf(values[i]) + " " + getString(R.string.time_seconds);
- }
- choice = values[checked];
- AlertDialog.Builder builder = new AlertDialog.Builder(MediaplayerActivity.this);
- builder.setTitle(R.string.pref_rewind);
- builder.setSingleChoiceItems(choices, checked,
- (dialog, which) -> choice = values[which]);
- builder.setNegativeButton(R.string.cancel_label, null);
- builder.setPositiveButton(R.string.confirm_label, (dialog, which) -> {
- UserPreferences.setPrefRewindSecs(choice);
- if(txtvRev != null){
- txtvRev.setText(String.valueOf(choice));
- }
- });
- builder.create().show();
- return true;
- }
+ butRev.setOnLongClickListener(v -> {
+ showSkipPreference(MediaplayerActivity.this, SkipDirection.SKIP_REWIND);
+ return true;
});
}
@@ -742,37 +805,9 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
if (butFF != null) {
butFF.setOnClickListener(v -> onFastForward());
- butFF.setOnLongClickListener(new View.OnLongClickListener() {
-
- int choice;
-
- @Override
- public boolean onLongClick(View v) {
- int checked = 0;
- int rewindSecs = UserPreferences.getFastFowardSecs();
- final int[] values = getResources().getIntArray(R.array.seek_delta_values);
- final String[] choices = new String[values.length];
- for (int i = 0; i < values.length; i++) {
- if (rewindSecs == values[i]) {
- checked = i;
- }
- choices[i] = String.valueOf(values[i]) + " " + getString(R.string.time_seconds);
- }
- choice = values[checked];
- AlertDialog.Builder builder = new AlertDialog.Builder(MediaplayerActivity.this);
- builder.setTitle(R.string.pref_fast_forward);
- builder.setSingleChoiceItems(choices, checked,
- (dialog, which) -> choice = values[which]);
- builder.setNegativeButton(R.string.cancel_label, null);
- builder.setPositiveButton(R.string.confirm_label, (dialog, which) -> {
- UserPreferences.setPrefFastForwardSecs(choice);
- if(txtvFF != null) {
- txtvFF.setText(String.valueOf(choice));
- }
- });
- builder.create().show();
- return true;
- }
+ butFF.setOnLongClickListener(v -> {
+ showSkipPreference(MediaplayerActivity.this, SkipDirection.SKIP_FORWARD);
+ return false;
});
}
@@ -801,7 +836,7 @@ public abstract class MediaplayerActivity extends CastEnabledActivity implements
return;
}
int curr = controller.getPosition();
- controller.seekTo(curr + UserPreferences.getFastFowardSecs() * 1000);
+ controller.seekTo(curr + UserPreferences.getFastForwardSecs() * 1000);
}
protected abstract int getContentViewResourceId();