From 0991105877df2926efbe85a5b9a111af0143c91a Mon Sep 17 00:00:00 2001 From: Andrey Pavlenko Date: Sat, 16 May 2020 13:24:59 +0300 Subject: [PATCH] Playback timer --- .../media/service/MediaSessionCallback.java | 30 +++++++++++ .../aap/fermata/ui/view/ControlPanelView.java | 51 +++++++++++++++++++ fermata/src/main/res/drawable/timer.xml | 18 +++++++ fermata/src/main/res/values-ru/strings.xml | 4 ++ fermata/src/main/res/values/ids.xml | 1 + fermata/src/main/res/values/strings.xml | 5 +- 6 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 fermata/src/main/res/drawable/timer.xml diff --git a/fermata/src/main/java/me/aap/fermata/media/service/MediaSessionCallback.java b/fermata/src/main/java/me/aap/fermata/media/service/MediaSessionCallback.java index c020a557..bf7ae11e 100644 --- a/fermata/src/main/java/me/aap/fermata/media/service/MediaSessionCallback.java +++ b/fermata/src/main/java/me/aap/fermata/media/service/MediaSessionCallback.java @@ -1265,4 +1265,34 @@ public int hashCode() { return view.hashCode(); } } + + private PlaybackTimer playbackTimer; + + public int getPlaybackTimer() { + return (playbackTimer == null) ? 0 + : Math.max((int) (playbackTimer.time - System.currentTimeMillis()) / 1000, 0); + } + + public void setPlaybackTimer(int time) { + if (time == 0) { + playbackTimer = null; + } else { + int delay = time * 1000; + PlaybackTimer timer = this.playbackTimer = new PlaybackTimer(delay + System.currentTimeMillis()); + handler.postDelayed(timer, delay); + } + } + + private final class PlaybackTimer implements Runnable { + final long time; + + public PlaybackTimer(long time) { + this.time = time; + } + + @Override + public void run() { + if (playbackTimer == this) onStop(); + } + } } \ No newline at end of file diff --git a/fermata/src/main/java/me/aap/fermata/ui/view/ControlPanelView.java b/fermata/src/main/java/me/aap/fermata/ui/view/ControlPanelView.java index 31bcfa4c..84382e5d 100644 --- a/fermata/src/main/java/me/aap/fermata/ui/view/ControlPanelView.java +++ b/fermata/src/main/java/me/aap/fermata/ui/view/ControlPanelView.java @@ -35,6 +35,7 @@ import me.aap.utils.app.App; import me.aap.utils.function.BooleanSupplier; import me.aap.utils.function.DoubleSupplier; +import me.aap.utils.function.IntSupplier; import me.aap.utils.pref.BasicPreferenceStore; import me.aap.utils.pref.PreferenceSet; import me.aap.utils.pref.PreferenceStore; @@ -320,6 +321,7 @@ protected void buildPlayableMenu(MainActivityDelegate a, OverlayMenu.Builder b, b.addItem(R.id.audio_effects, R.drawable.equalizer, R.string.audio_effects); b.addItem(R.id.speed, R.drawable.speed, R.string.speed).setSubmenu(s -> new SpeedMenuHandler().build(s, getItem())); + b.addItem(R.id.timer, R.drawable.timer, R.string.timer).setSubmenu(s -> new TimerMenuHandler(a).build(s)); } @Override @@ -550,6 +552,55 @@ public void applyFloatPref(Pref pref, float value) { } } + private static final class TimerMenuHandler implements OverlayMenu.CloseHandler { + private final Pref H = Pref.i("H", 0); + private final Pref M = Pref.i("M", 0); + private PreferenceStore store = new BasicPreferenceStore(); + private final MediaSessionCallback cb; + + TimerMenuHandler(MainActivityDelegate activity) { + cb = activity.getMediaSessionCallback(); + } + + void build(OverlayMenu.Builder b) { + PreferenceSet set = new PreferenceSet(); + int time = cb.getPlaybackTimer(); + + if (time > 0) { + int h = time / 3600; + int m = (time - h * 3600) / 60; + store.applyIntPref(H, h); + store.applyIntPref(M, m); + } + + set.addIntPref(o -> { + o.title = R.string.hours; + o.store = store; + o.pref = H; + o.seekMin = 0; + o.seekMax = 12; + }); + set.addIntPref(o -> { + o.title = R.string.minutes; + o.store = store; + o.pref = M; + o.seekMin = 0; + o.seekMax = 60; + o.seekScale = 5; + }); + + set.addToMenu(b, true); + b.setCloseHandlerHandler(this); + } + + @Override + public void menuClosed(OverlayMenu menu) { + int h = store.getIntPref(H); + int m = store.getIntPref(M); + cb.setPlaybackTimer(h * 3600 + m * 60); + } + } + private int getStartDelay() { return (prefs == null) ? 0 : prefs.getVideoControlStartDelayPref() * 1000; } diff --git a/fermata/src/main/res/drawable/timer.xml b/fermata/src/main/res/drawable/timer.xml new file mode 100644 index 00000000..de1a56dd --- /dev/null +++ b/fermata/src/main/res/drawable/timer.xml @@ -0,0 +1,18 @@ + + + + + + diff --git a/fermata/src/main/res/values-ru/strings.xml b/fermata/src/main/res/values-ru/strings.xml index 13bb26ee..29e89f16 100644 --- a/fermata/src/main/res/values-ru/strings.xml +++ b/fermata/src/main/res/values-ru/strings.xml @@ -187,6 +187,10 @@ Если вам нравится приложение, вы можете купить мне кофе. Пожалуйста, воспользуйтесь телефоном, чтобы купить кофе. + Таймер + Часы + Минуты + О приложении Fermata Media Player

Fermata Media Player - это бесплатный, с открытым исходным кодом, аудио и видео плеер diff --git a/fermata/src/main/res/values/ids.xml b/fermata/src/main/res/values/ids.xml index 7c605046..ef4c7952 100644 --- a/fermata/src/main/res/values/ids.xml +++ b/fermata/src/main/res/values/ids.xml @@ -94,6 +94,7 @@ + diff --git a/fermata/src/main/res/values/strings.xml b/fermata/src/main/res/values/strings.xml index df02b8f7..ae000b2c 100644 --- a/fermata/src/main/res/values/strings.xml +++ b/fermata/src/main/res/values/strings.xml @@ -79,7 +79,6 @@ 4x3 16x9 - Audio Audio delay Preferred audio stream languages @@ -204,6 +203,10 @@ If you like the application, please consider buying me a coffee. Please use your phone to buy a coffee. + Timer + Hours + Minutes + About Fermata Media Player

Fermata Media Player is a free, open source audio and video player with a simple and