mirror of
https://github.com/advplyr/audiobookshelf-app.git
synced 2026-07-29 07:58:43 +02:00
Add: Sleep Timers #24
This commit is contained in:
@@ -122,4 +122,9 @@ class MainActivity : BridgeActivity() {
|
||||
// Mandatory for Activity, but not for Fragment & ComponentActivity
|
||||
storageHelper.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
||||
}
|
||||
|
||||
override fun onUserInteraction() {
|
||||
super.onUserInteraction()
|
||||
Log.d(tag, "USER INTERACTION")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,8 @@ import androidx.core.content.ContextCompat
|
||||
import com.getcapacitor.*
|
||||
import com.getcapacitor.annotation.CapacitorPlugin
|
||||
import org.json.JSONObject
|
||||
import java.util.*
|
||||
import kotlin.concurrent.schedule
|
||||
|
||||
@CapacitorPlugin(name = "MyNativeAudio")
|
||||
class MyNativeAudio : Plugin() {
|
||||
@@ -35,6 +37,9 @@ class MyNativeAudio : Plugin() {
|
||||
jsobj.put("playWhenReady", playWhenReady)
|
||||
notifyListeners("onPrepareMedia", jsobj)
|
||||
}
|
||||
override fun onSleepTimerEnded() {
|
||||
emit("onSleepTimerEnded", true)
|
||||
}
|
||||
})
|
||||
}
|
||||
mainActivity.pluginCallback = foregroundServiceReady
|
||||
@@ -201,4 +206,26 @@ class MyNativeAudio : Plugin() {
|
||||
Log.d(tag, "Setting Audiobooks ${audiobookObjs.size}")
|
||||
playerNotificationService.setAudiobooks(audiobookObjs)
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
fun setSleepTimer(call: PluginCall) {
|
||||
var sleepTimeout:Long = call.getString("timeout", "360000")!!.toLong()
|
||||
|
||||
playerNotificationService.setSleepTimer(sleepTimeout)
|
||||
call.resolve()
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
fun getSleepTimerTime(call: PluginCall) {
|
||||
var time = playerNotificationService.getSleepTimerTime()
|
||||
val ret = JSObject()
|
||||
ret.put("value", time)
|
||||
call.resolve(ret)
|
||||
}
|
||||
|
||||
@PluginMethod
|
||||
fun cancelSleepTimer(call: PluginCall) {
|
||||
playerNotificationService.cancelSleepTimer()
|
||||
call.resolve()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +51,7 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
|
||||
fun onPlayingUpdate(isPlaying: Boolean)
|
||||
fun onMetadata(metadata: JSObject)
|
||||
fun onPrepare(audiobookId:String, playWhenReady:Boolean)
|
||||
fun onSleepTimerEnded()
|
||||
}
|
||||
|
||||
private val tag = "PlayerService"
|
||||
@@ -84,6 +85,8 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
|
||||
private var lastPauseTime: Long = 0 //ms
|
||||
private var onSeekBack: Boolean = false
|
||||
|
||||
private var sleepTimerTask:TimerTask? = null
|
||||
|
||||
fun setCustomObjectListener(mylistener: MyCustomObjectListener) {
|
||||
listener = mylistener
|
||||
}
|
||||
@@ -124,7 +127,7 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
|
||||
}
|
||||
|
||||
@RequiresApi(Build.VERSION_CODES.O)
|
||||
private fun createNotificationChannel(channelId: String, channelName: String): String{
|
||||
private fun createNotificationChannel(channelId: String, channelName: String): String {
|
||||
val chan = NotificationChannel(channelId,
|
||||
channelName, NotificationManager.IMPORTANCE_HIGH)
|
||||
chan.lightColor = Color.DKGRAY
|
||||
@@ -718,5 +721,33 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun setSleepTimer(timeout:Long) {
|
||||
Log.d(tag, "Setting Sleep Timer for $timeout")
|
||||
|
||||
sleepTimerTask?.cancel()
|
||||
sleepTimerTask = Timer("SleepTimer",false).schedule(timeout) {
|
||||
Log.d(tag, "Sleep Timer Done")
|
||||
Handler(Looper.getMainLooper()).post() {
|
||||
if (mPlayer.isPlaying) {
|
||||
Log.d(tag, "Sleep Timer Pausing Player")
|
||||
mPlayer.pause()
|
||||
}
|
||||
if (listener != null) listener.onSleepTimerEnded()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun getSleepTimerTime():Long? {
|
||||
var time = sleepTimerTask?.scheduledExecutionTime()
|
||||
Log.d(tag, "Sleep Timer execution time $time")
|
||||
return time
|
||||
}
|
||||
|
||||
fun cancelSleepTimer() {
|
||||
Log.d(tag, "Canceling Sleep Timer")
|
||||
sleepTimerTask?.cancel()
|
||||
sleepTimerTask = null
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user