Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sleep timer: Add logs #2246

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -609,24 +609,28 @@ class PlayerViewModel @Inject constructor(
}

fun sleepTimerAfter(mins: Int) {
LogBuffer.i(SleepTimer.TAG, "Sleep after $mins minutes configured")
sleepTimer.sleepAfter(duration = mins.toDuration(DurationUnit.MINUTES)) {
playbackManager.updateSleepTimerStatus(sleepTimeRunning = true)
}
}

fun sleepTimerAfterEpisode(episodes: Int = 1) {
LogBuffer.i(SleepTimer.TAG, "Sleep after $episodes episodes configured")
settings.setlastSleepEndOfEpisodes(episodes)
playbackManager.updateSleepTimerStatus(sleepTimeRunning = true, sleepAfterEpisodes = episodes)
sleepTimer.cancelTimer()
}

fun sleepTimerAfterChapter(chapters: Int = 1) {
LogBuffer.i(SleepTimer.TAG, "Sleep after $chapters chapters configured")
settings.setlastSleepEndOfChapters(chapters)
playbackManager.updateSleepTimerStatus(sleepTimeRunning = true, sleepAfterChapters = chapters)
sleepTimer.cancelTimer()
}

fun cancelSleepTimer() {
LogBuffer.i(SleepTimer.TAG, "Cancelled sleep timer")
playbackManager.updateSleepTimerStatus(sleepTimeRunning = false)
sleepTimer.cancelTimer()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2129,10 +2129,14 @@ open class PlaybackManager @Inject constructor(
updateSleepTimerStatus(sleepTimeRunning = true)
},
onRestartSleepOnEpisodeEnd = {
updateSleepTimerStatus(sleepTimeRunning = true, sleepAfterEpisodes = settings.getlastSleepEndOfEpisodes())
val episodes = settings.getlastSleepEndOfEpisodes()
LogBuffer.i(SleepTimer.TAG, "Sleep timer was restarted with end of $episodes episodes set")
updateSleepTimerStatus(sleepTimeRunning = true, sleepAfterEpisodes = episodes)
},
onRestartSleepOnChapterEnd = {
updateSleepTimerStatus(sleepTimeRunning = true, sleepAfterChapters = settings.getlastSleepEndOfChapter())
val chapter = settings.getlastSleepEndOfChapter()
LogBuffer.i(SleepTimer.TAG, "Sleep timer was restarted with end of $chapter chapter set")
updateSleepTimerStatus(sleepTimeRunning = true, sleepAfterChapters = chapter)
},
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class SleepTimer @Inject constructor(
private const val NUMBER_OF_CHAPTERS_KEY = "number_of_chapters"
private const val END_OF_EPISODE_VALUE = "end_of_episode"
private const val END_OF_CHAPTER_VALUE = "end_of_chapter"
const val TAG: String = "SleepTimer"
}

private var sleepTimeMs: Long? = null
Expand Down Expand Up @@ -60,6 +61,7 @@ class SleepTimer @Inject constructor(
timeInMillis = currentTimeMs
add(Calendar.MINUTE, minutes)
}
LogBuffer.i(TAG, "Added extra time: $minutes")
createAlarm(time.timeInMillis)
}

Expand Down Expand Up @@ -91,20 +93,23 @@ class SleepTimer @Inject constructor(
} else if (shouldRestartSleepAfterTime(diffTime, timerState.isSleepTimerRunning)) {
lastSleepAfterTime?.let {
analyticsTracker.track(PLAYER_SLEEP_TIMER_RESTARTED, mapOf(TIME_KEY to it.inWholeSeconds))
LogBuffer.i(TAG, "Was restarted with ${it.inWholeMinutes} minutes set")
sleepAfter(it, onRestartSleepAfterTime)
}
}
}
}

fun setEndOfEpisodeUuid(uuid: String) {
LogBuffer.i(TAG, "Episode $uuid was marked as end of episode")
lastEpisodeUuidAutomaticEnded = uuid
lastTimeSleepTimeHasFinished = System.currentTimeMillis().milliseconds
cancelAutomaticSleepAfterTimeRestart()
cancelAutomaticSleepOnChapterEndRestart()
}

fun setEndOfChapter() {
LogBuffer.i(TAG, "End of chapter was reached")
val time = System.currentTimeMillis().milliseconds
lastSleepAfterEndOfChapterTime = time
lastTimeSleepTimeHasFinished = time
Expand Down Expand Up @@ -137,6 +142,7 @@ class SleepTimer @Inject constructor(
false
} else {
return try {
LogBuffer.i(TAG, "Starting...")
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeMs, sleepIntent)
sleepTimeMs = timeMs
lastTimeSleepTimeHasFinished = timeMs.milliseconds
Expand All @@ -149,6 +155,7 @@ class SleepTimer @Inject constructor(
}

fun cancelTimer() {
LogBuffer.i(TAG, "Cleaning automatic sleep timer feature...")
getAlarmManager().cancel(getSleepIntent())
cancelSleepTime()
cancelAutomaticSleepAfterTimeRestart()
Expand All @@ -164,6 +171,7 @@ class SleepTimer @Inject constructor(

val timeLeft = sleepTimeMs - System.currentTimeMillis()
if (timeLeft < 0) {
LogBuffer.i(TAG, "Cancelled because time is up")
cancelSleepTime()
return null
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import au.com.shiftyjelly.pocketcasts.analytics.AnalyticsTrackerWrapper
import au.com.shiftyjelly.pocketcasts.localization.R
import au.com.shiftyjelly.pocketcasts.preferences.Settings
import au.com.shiftyjelly.pocketcasts.utils.extensions.isAppForeground
import au.com.shiftyjelly.pocketcasts.utils.log.LogBuffer
import dagger.hilt.android.qualifiers.ApplicationContext
import javax.inject.Inject
import javax.inject.Singleton
Expand Down Expand Up @@ -75,7 +76,10 @@ class SleepTimerRestartWhenShakingDevice @Inject constructor(
playbackManager.playSleepTimeTone()
}
}
time?.let { trackSleepTimeRestart(it) }
time?.let {
LogBuffer.i(SleepTimer.TAG, "Restarted with ${time.inWholeMinutes} minutes set after shaking device")
trackSleepTimeRestart(it)
}
}

private fun trackSleepTimeRestart(time: Duration) {
Expand Down
Loading