Skip to content

Commit

Permalink
request exact alarm permissions to be able to set the correct time fo…
Browse files Browse the repository at this point in the history
…r sleep timer
  • Loading branch information
mebarbosa committed Dec 12, 2024
1 parent 14d010a commit 88dccff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
3 changes: 3 additions & 0 deletions modules/services/repositories/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

<uses-permission android:name="android.permission.POST_NOTIFICATIONS" />

<uses-permission android:name="android.permission.SCHEDULE_EXACT_ALARM"
tools:ignore="ProtectedPermissions" />

<application android:usesCleartextTraffic="true">
<provider
android:name="au.com.shiftyjelly.pocketcasts.repositories.playback.auto.AlbumArtContentProvider"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.os.Build
import android.provider.Settings
import android.text.format.DateUtils
import androidx.annotation.RequiresApi
import au.com.shiftyjelly.pocketcasts.analytics.AnalyticsEvent.PLAYER_SLEEP_TIMER_RESTARTED
import au.com.shiftyjelly.pocketcasts.analytics.AnalyticsTracker
import au.com.shiftyjelly.pocketcasts.utils.log.LogBuffer
Expand Down Expand Up @@ -38,9 +41,9 @@ class SleepTimer @Inject constructor(
private var lastEpisodeUuidAutomaticEnded: String? = null

fun sleepAfter(duration: Duration, onSuccess: () -> Unit) {
val sleepAt = System.currentTimeMillis().milliseconds + duration
val sleepAt = System.currentTimeMillis() + duration.inWholeMilliseconds

if (createAlarm(sleepAt.inWholeMilliseconds)) {
if (createAlarm(sleepAt)) {
lastSleepAfterTime = duration
cancelAutomaticSleepOnEpisodeEndRestart()
cancelAutomaticSleepOnChapterEndRestart()
Expand Down Expand Up @@ -126,13 +129,28 @@ class SleepTimer @Inject constructor(

private fun shouldRestartSleepEndOfChapter(diffTime: Duration, isSleepEndOfChapterRunning: Boolean) = diffTime < MIN_TIME_TO_RESTART_SLEEP_TIMER_IN_MINUTES && !isSleepEndOfChapterRunning && lastSleepAfterEndOfChapterTime != null

@RequiresApi(Build.VERSION_CODES.S)
private fun requestExactAlarmPermissionIfNeeded(context: Context) {
val intent = Intent(Settings.ACTION_REQUEST_SCHEDULE_EXACT_ALARM)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK
context.startActivity(intent)
}

private fun createAlarm(timeMs: Long): Boolean {
val sleepIntent = getSleepIntent()
val alarmManager = getAlarmManager()

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && !alarmManager.canScheduleExactAlarms()) {
LogBuffer.e(TAG, "Cannot schedule exact alarms. Permission not granted.")
requestExactAlarmPermissionIfNeeded(context)
return false
}

alarmManager.cancel(sleepIntent)

return try {
LogBuffer.i(TAG, "Starting...")
alarmManager.setAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeMs, sleepIntent)
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, timeMs, sleepIntent)
sleepTimeMs = timeMs
lastTimeSleepTimeHasFinished = timeMs.milliseconds
true
Expand Down

0 comments on commit 88dccff

Please sign in to comment.