Skip to content

Commit

Permalink
Update:Sleep timer shake to reset anytime while timer is running #260
Browse files Browse the repository at this point in the history
  • Loading branch information
advplyr committed Jan 28, 2023
1 parent 860c7aa commit f06afbd
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import org.json.JSONException

class FolderScanner(var ctx: Context) {
private val tag = "FolderScanner"
var jacksonMapper = jacksonObjectMapper().enable(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature())
private var jacksonMapper = jacksonObjectMapper().enable(JsonReadFeature.ALLOW_UNESCAPED_CONTROL_CHARS.mappedFeature())

data class DownloadItemScanResult(val localLibraryItem:LocalLibraryItem, var localMediaProgress:LocalMediaProgress?)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
package com.audiobookshelf.app.player
package com.audiobookshelf.app.managers

import android.content.Context
import android.os.*
import android.util.Log
import com.audiobookshelf.app.device.DeviceManager
import com.audiobookshelf.app.player.PlayerNotificationService
import com.audiobookshelf.app.player.SLEEP_TIMER_WAKE_UP_EXPIRATION
import java.util.*
import kotlin.concurrent.schedule
import kotlin.math.roundToInt

const val SLEEP_EXTENSION_TIME = 900000L // 15m

class SleepTimerManager constructor(val playerNotificationService:PlayerNotificationService) {
class ShakeSensitivity {
companion object {
val veryHigh = 1f
val high = 1.3f
val medium = 1.6f
val low = 2f
val veryLow = 2.7f
}
}

class SleepTimerManager constructor(val playerNotificationService: PlayerNotificationService) {
private val tag = "SleepTimerManager"

private var sleepTimerTask:TimerTask? = null
Expand Down Expand Up @@ -121,7 +133,7 @@ class SleepTimerManager constructor(val playerNotificationService:PlayerNotifica
return true
}

fun clearSleepTimer() {
private fun clearSleepTimer() {
sleepTimerTask?.cancel()
sleepTimerTask = null
sleepTimerEndTime = 0
Expand Down Expand Up @@ -163,20 +175,6 @@ class SleepTimerManager constructor(val playerNotificationService:PlayerNotifica
}
}

private fun extendSleepTime() {
if (!sleepTimerRunning) return
setVolume(1F)
if (sleepTimerEndTime == 0L) {
sleepTimerLength += sleepTimerExtensionTime
if (sleepTimerLength + getCurrentTime() > getDuration()) sleepTimerLength = getDuration() - getCurrentTime()
} else {
sleepTimerEndTime += sleepTimerExtensionTime
if (sleepTimerEndTime > getDuration()) sleepTimerEndTime = getDuration()
}

playerNotificationService.clientEventEmitter?.onSleepTimerSet(getSleepTimerTimeRemainingSeconds())
}

fun checkShouldExtendSleepTimer() {
if (!sleepTimerRunning) {
if (sleepTimerFinishedAt <= 0L) return
Expand All @@ -195,11 +193,11 @@ class SleepTimerManager constructor(val playerNotificationService:PlayerNotifica
play()
return
}
// Only extend if within 30 seconds of finishing
val sleepTimeRemaining = getSleepTimerTimeRemainingSeconds()
if (sleepTimeRemaining <= 30) {

// Does not apply to chapter sleep timers
if (sleepTimerEndTime == 0L) {
vibrate()
extendSleepTime()
setSleepTimer(sleepTimerExtensionTime, false)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import com.audiobookshelf.app.data.*
import com.audiobookshelf.app.data.DeviceInfo
import com.audiobookshelf.app.device.DeviceManager
import com.audiobookshelf.app.managers.DbManager
import com.audiobookshelf.app.managers.SleepTimerManager
import com.audiobookshelf.app.media.MediaManager
import com.audiobookshelf.app.server.ApiHandler
import com.fasterxml.jackson.annotation.JsonIgnore
Expand Down Expand Up @@ -394,7 +395,7 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
SkipForwardCustomActionProvider(),
))
}
mediaSessionConnector.setCustomActionProviders(*customActionProviders.toTypedArray());
mediaSessionConnector.setCustomActionProviders(*customActionProviders.toTypedArray())

playbackSession.mediaPlayer = getMediaPlayer()

Expand Down Expand Up @@ -640,10 +641,6 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
return currentPlaybackSession?.clone()
}

fun getCurrentPlaybackSessionId() :String? {
return currentPlaybackSession?.id
}

fun getCurrentBookChapter():BookChapter? {
return currentPlaybackSession?.getChapterForTime(this.getCurrentTime())
}
Expand Down Expand Up @@ -828,7 +825,7 @@ class PlayerNotificationService : MediaBrowserServiceCompat() {
clientEventEmitter?.onPlaybackClosed()
PlayerListener.lastPauseTime = 0
isClosed = true
stopForeground(true)
stopForeground(Service.STOP_FOREGROUND_REMOVE)
stopSelf()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class ShakeDetector : SensorEventListener {
* from the Google Play Store and run it to see how
* many G's it takes to register a shake
*/
private const val SHAKE_THRESHOLD_GRAVITY = 1.5f // orig 2.7f
var SHAKE_THRESHOLD_GRAVITY = 1.5f // orig 2.7f
private const val SHAKE_SLOP_TIME_MS = 500
private const val SHAKE_COUNT_RESET_TIME_MS = 3000
}
Expand Down
2 changes: 1 addition & 1 deletion pages/settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export default {
settingInfo: {
disableShakeToResetSleepTimer: {
name: 'Disable shake to reset sleep timer',
message: 'The sleep timer will start fading out when 30s is remaining. Shaking your device will reset the timer if it is within 30s OR has finished less than 2 mintues ago. Enable this setting to disable that feature.'
message: 'Shaking your device while the timer is running OR within 2 minutes of the timer expiring will reset the sleep timer. Enable this setting to disable shake to reset.'
}
},
lockCurrentOrientation: false,
Expand Down

0 comments on commit f06afbd

Please sign in to comment.