[Android] Add waiting feature to sleep timer
tda1009
git at videolan.org
Wed Nov 16 08:56:28 UTC 2022
vlc-android | branch: master | tda1009 <ducanhtr at gmail.com> | Wed Nov 16 08:56:27 2022 +0000| [93f180fb7ac7066ae0aef92ab0a04081875c0dfc] | committer: Nicolas Pomepuy
Add waiting feature to sleep timer
Fixes #2585
> https://code.videolan.org/videolan/vlc-android/commit/93f180fb7ac7066ae0aef92ab0a04081875c0dfc
---
.../resources/src/main/res/values/strings.xml | 1 +
.../src/main/java/org/videolan/tools/Settings.kt | 2 ++
.../vlc-android/res/layout/dialog_time_picker.xml | 11 ++++++++++-
.../src/org/videolan/vlc/PlaybackService.kt | 8 +++++++-
.../org/videolan/vlc/gui/dialogs/SleepTimerDialog.kt | 20 ++++++++++++++++++++
5 files changed, 40 insertions(+), 2 deletions(-)
diff --git a/application/resources/src/main/res/values/strings.xml b/application/resources/src/main/res/values/strings.xml
index f8a44f4429..60fcc361c4 100644
--- a/application/resources/src/main/res/values/strings.xml
+++ b/application/resources/src/main/res/values/strings.xml
@@ -175,6 +175,7 @@
<string name="sleep_in">Sleep in</string>
<string name="jump_to_time">Jump to Time</string>
<string name="subtitle_select">Select subtitle file</string>
+ <string name="wait_before_sleep">Wait for current media item to finish first</string>
<string name="server_add_title">Add a new server to favorites</string>
<string name="server_domain_hint">Server address (domain name or IP)</string>
diff --git a/application/tools/src/main/java/org/videolan/tools/Settings.kt b/application/tools/src/main/java/org/videolan/tools/Settings.kt
index c5cc5c4a51..e72459aa3b 100644
--- a/application/tools/src/main/java/org/videolan/tools/Settings.kt
+++ b/application/tools/src/main/java/org/videolan/tools/Settings.kt
@@ -183,6 +183,8 @@ const val WIDGETS_BACKGROUND_LAST_COLORS = "widgets_background_last_colors"
const val WIDGETS_FOREGROUND_LAST_COLORS = "widgets_foreground_last_colors"
const val CUSTOM_POPUP_HEIGHT = "custom_popup_height"
+const val SLEEP_TIMER_WAIT = "sleep_timer_wait"
+
const val NOTIFICATION_PERMISSION_ASKED = "notification_permission_asked"
//files
diff --git a/application/vlc-android/res/layout/dialog_time_picker.xml b/application/vlc-android/res/layout/dialog_time_picker.xml
index 01f1a4886c..3aa493a74a 100644
--- a/application/vlc-android/res/layout/dialog_time_picker.xml
+++ b/application/vlc-android/res/layout/dialog_time_picker.xml
@@ -258,6 +258,15 @@
</LinearLayout>
+ <CheckBox
+ android:id="@+id/tim_pic_wait_checkbox"
+ android:layout_width="wrap_content"
+ android:layout_height="wrap_content"
+ android:layout_marginStart="16dp"
+ android:layout_marginTop="16dp"
+ android:text="@string/wait_before_sleep"
+ app:layout_constraintStart_toStartOf="parent"
+ app:layout_constraintTop_toBottomOf="@id/linearLayout5" />
<Button
android:id="@+id/tim_pic_delete_current"
@@ -286,7 +295,7 @@
android:text="@string/ok"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
- app:layout_constraintTop_toBottomOf="@+id/linearLayout5" />
+ app:layout_constraintTop_toBottomOf="@+id/tim_pic_wait_checkbox" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
\ No newline at end of file
diff --git a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
index 4e9f3edbba..4093853982 100644
--- a/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
+++ b/application/vlc-android/src/org/videolan/vlc/PlaybackService.kt
@@ -113,6 +113,8 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner, CoroutineSc
private val audioFocusHelper by lazy { VLCAudioFocusHelper(this) }
private lateinit var browserCallback: MediaBrowserCallback
var sleepTimerJob: Job? = null
+ var waitForMediaEnd = false
+ private var mediaEndReached = false
// Playback management
internal lateinit var mediaSession: MediaSessionCompat
@@ -230,6 +232,7 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner, CoroutineSc
updateMetadata()
}
MediaPlayer.Event.MediaChanged -> if (BuildConfig.DEBUG) Log.d(TAG, "onEvent: MediaChanged")
+ MediaPlayer.Event.EndReached -> mediaEndReached = true
}
cbActor.trySend(CbMediaPlayerEvent(event))
}
@@ -1728,10 +1731,13 @@ class PlaybackService : MediaBrowserServiceCompat(), LifecycleOwner, CoroutineSc
sleepTimerJob = launch {
while (isActive) {
playerSleepTime.value?.let {
- if (System.currentTimeMillis() > it.timeInMillis) {
+ val timerExpired = System.currentTimeMillis() > it.timeInMillis
+ val shouldStop = if (waitForMediaEnd) timerExpired && mediaEndReached else timerExpired
+ if (shouldStop) {
withContext(Dispatchers.Main) { if (isPlaying) stop() else setSleepTimer(null) }
}
}
+ if (mediaEndReached) mediaEndReached = false
delay(1000)
}
}
diff --git a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SleepTimerDialog.kt b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SleepTimerDialog.kt
index cd8388dbd6..af6b1a8a0a 100644
--- a/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SleepTimerDialog.kt
+++ b/application/vlc-android/src/org/videolan/vlc/gui/dialogs/SleepTimerDialog.kt
@@ -22,10 +22,16 @@
package org.videolan.vlc.gui.dialogs
+import android.content.Context
+import android.content.SharedPreferences
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
+import android.widget.CheckBox
+import org.videolan.tools.SLEEP_TIMER_WAIT
+import org.videolan.tools.Settings
+import org.videolan.tools.putSingle
import org.videolan.vlc.PlaybackService
import org.videolan.vlc.R
import org.videolan.vlc.viewmodels.PlaylistModel
@@ -33,16 +39,28 @@ import java.util.*
class SleepTimerDialog : PickTimeFragment() {
+ private lateinit var settings: SharedPreferences
+ private lateinit var waitCheckBox: CheckBox
private val playlistModel by lazy { PlaylistModel.get(this) }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = super.onCreateView(inflater, container, savedInstanceState)
maxTimeSize = 4
+ settings = Settings.getInstance(requireActivity())
return view
}
+ override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
+ super.onViewCreated(view, savedInstanceState)
+ waitCheckBox = view.findViewById(R.id.tim_pic_wait_checkbox)
+ waitCheckBox.isChecked = settings.getBoolean(SLEEP_TIMER_WAIT, false)
+ }
+
override fun executeAction() {
+ playlistModel.service?.waitForMediaEnd = waitCheckBox.isChecked
+ settings.putSingle(SLEEP_TIMER_WAIT, waitCheckBox.isChecked)
+
val hours = if (hours != "") java.lang.Long.parseLong(hours) * HOURS_IN_MICROS else 0L
val minutes = if (minutes != "") java.lang.Long.parseLong(minutes) * MINUTES_IN_MICROS else 0L
val interval = (hours + minutes) / MILLIS_IN_MICROS //Interval in ms
@@ -59,7 +77,9 @@ class SleepTimerDialog : PickTimeFragment() {
override fun onClick(v: View) {
if (v.id == R.id.tim_pic_delete_current) {
+ playlistModel.service?.waitForMediaEnd = false
playlistModel.service?.setSleepTimer(null)
+ settings.putSingle(SLEEP_TIMER_WAIT, false)
dismiss()
} else super.onClick(v)
}
More information about the Android
mailing list