fixed alarm and sleep intents - cancel correctly, don't restart service
parent
f8209f2127
commit
c77ca30e46
|
|
@ -32,7 +32,6 @@ import androidx.media.AudioManagerCompat
|
|||
import androidx.preference.PreferenceManager
|
||||
import com.google.android.exoplayer2.*
|
||||
import com.google.android.exoplayer2.metadata.icy.*
|
||||
import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
|
||||
import fr.forum_thalie.tsumugi.alarm.RadioAlarm
|
||||
import fr.forum_thalie.tsumugi.alarm.RadioSleeper
|
||||
import fr.forum_thalie.tsumugi.planning.Planning
|
||||
|
|
@ -282,17 +281,18 @@ class RadioService : MediaBrowserServiceCompat() {
|
|||
PlayerStore.instance.isServiceStarted.value = false
|
||||
PlayerStore.instance.isInitialized = false
|
||||
|
||||
|
||||
RadioSleeper.instance.cancelSleep(this, isClosing = true)
|
||||
|
||||
PreferenceManager.getDefaultSharedPreferences(this).edit {
|
||||
this.putBoolean("isSleeping", false)
|
||||
this.commit()
|
||||
}
|
||||
RadioSleeper.instance.cancelSleep(this)
|
||||
|
||||
apiTicker.cancel() // stops the timer.
|
||||
Log.d(tag, radioTag + "destroyed")
|
||||
// if the service is destroyed, the application had become useless.
|
||||
// exitProcess(0)
|
||||
Process.killProcess(Process.myPid())
|
||||
exitProcess(0)
|
||||
}
|
||||
|
||||
// ########################################
|
||||
|
|
@ -351,13 +351,11 @@ class RadioService : MediaBrowserServiceCompat() {
|
|||
setBufferDurationsMs(minBufferMillis, maxBufferMillis, bufferForPlayback, bufferForPlaybackAfterRebuffer)
|
||||
}.createDefaultLoadControl()
|
||||
|
||||
/*
|
||||
|
||||
val playerBuilder = SimpleExoPlayer.Builder(this)
|
||||
playerBuilder.setLoadControl(loadControl)
|
||||
player = playerBuilder.build()
|
||||
*/
|
||||
|
||||
player = ExoPlayerFactory.newSimpleInstance(this, DefaultTrackSelector(), loadControl)
|
||||
|
||||
player.addMetadataOutput {
|
||||
for (i in 0 until it.length()) {
|
||||
|
|
|
|||
|
|
@ -19,29 +19,39 @@ class RadioAlarm {
|
|||
RadioAlarm()
|
||||
}
|
||||
}
|
||||
lateinit var alarmIntent: PendingIntent
|
||||
|
||||
private lateinit var alarmIntent: PendingIntent
|
||||
private lateinit var showIntent: PendingIntent
|
||||
|
||||
private fun defineIntents(c: Context)
|
||||
{
|
||||
alarmIntent = Intent(c, BootBroadcastReceiver::class.java).let { intent ->
|
||||
intent.putExtra("action", "$tag.${Actions.PLAY_OR_FALLBACK.name}")
|
||||
PendingIntent.getBroadcast(c, 0, intent, 0)
|
||||
}
|
||||
showIntent = Intent(c, ParametersActivity::class.java).let { intent ->
|
||||
intent.putExtra("action", "alarm")
|
||||
PendingIntent.getActivity(c, 0, intent, 0)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fun cancelAlarm(c: Context)
|
||||
{
|
||||
if (::alarmIntent.isInitialized)
|
||||
{
|
||||
val alarmManager = c.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
alarmManager.cancel(alarmIntent)
|
||||
}
|
||||
defineIntents(c)
|
||||
val alarmManager = c.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
alarmManager.cancel(alarmIntent)
|
||||
}
|
||||
|
||||
fun setNextAlarm(c: Context, isForce: Boolean = false, forceTime: Int? = null, forceDays: Set<String>? = null)
|
||||
{
|
||||
defineIntents(c)
|
||||
// don't do anything if the preference is set to FALSE, of course.
|
||||
if (!PreferenceManager.getDefaultSharedPreferences(c).getBoolean("isWakingUp", false) && !isForce)
|
||||
return
|
||||
|
||||
val alarmManager = c.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
alarmIntent = Intent(c, BootBroadcastReceiver::class.java).let { intent ->
|
||||
intent.putExtra("action", "$tag.${Actions.PLAY_OR_FALLBACK.name}")
|
||||
PendingIntent.getBroadcast(c, 0, intent, 0)
|
||||
}
|
||||
|
||||
val showIntent = Intent(c, ParametersActivity::class.java).let { intent ->
|
||||
intent.putExtra("action", ActionOpenParam.ALARM.name)
|
||||
PendingIntent.getActivity(c, 0, intent, 0)
|
||||
|
|
@ -95,19 +105,12 @@ class RadioAlarm {
|
|||
|
||||
fun snooze(c: Context)
|
||||
{
|
||||
defineIntents(c)
|
||||
val snoozeString = preferenceStore.getString("snoozeDuration", "10") ?: "10"
|
||||
val snoozeMinutes = if (snoozeString == c.getString(R.string.disable)) 0 else Integer.parseInt(snoozeString)
|
||||
if (snoozeMinutes > 0)
|
||||
{
|
||||
val alarmManager = c.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
alarmIntent = Intent(c, BootBroadcastReceiver::class.java).let { intent ->
|
||||
intent.putExtra("action", "$tag.${Actions.PLAY_OR_FALLBACK.name}")
|
||||
PendingIntent.getBroadcast(c, 0, intent, 0)
|
||||
}
|
||||
val showIntent = Intent(c, ParametersActivity::class.java).let { intent ->
|
||||
intent.putExtra("action", "alarm")
|
||||
PendingIntent.getActivity(c, 0, intent, 0)
|
||||
}
|
||||
|
||||
AlarmManagerCompat.setAlarmClock(alarmManager, Calendar.getInstance().timeInMillis + (snoozeMinutes * 60 * 1000), showIntent, alarmIntent)
|
||||
|
||||
|
|
|
|||
|
|
@ -30,8 +30,21 @@ class RadioSleeper {
|
|||
private lateinit var sleepIntent: PendingIntent
|
||||
private lateinit var fadeOutIntent: PendingIntent
|
||||
|
||||
private fun defineIntents(c: Context)
|
||||
{
|
||||
sleepIntent = Intent(c, RadioService::class.java).let { intent ->
|
||||
intent.putExtra("action", Actions.KILL.name)
|
||||
PendingIntent.getService(c, 99, intent, 0)
|
||||
}
|
||||
fadeOutIntent = Intent(c, RadioService::class.java).let { intent ->
|
||||
intent.putExtra("action", Actions.FADE_OUT.name)
|
||||
PendingIntent.getService(c, 98, intent, 0)
|
||||
}
|
||||
}
|
||||
|
||||
fun setSleep(c: Context, isForce: Boolean = false, forceDuration: Long? = null)
|
||||
{
|
||||
defineIntents(c)
|
||||
// don't do anything if the preference is set to FALSE, of course.
|
||||
if (!PreferenceManager.getDefaultSharedPreferences(c).getBoolean("isSleeping", false) && !isForce)
|
||||
return
|
||||
|
|
@ -39,19 +52,13 @@ class RadioSleeper {
|
|||
val minutes: Long = forceDuration ?: Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(c).getString("sleepDuration", "1") ?: "1").toLong()
|
||||
|
||||
val alarmManager = c.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
sleepIntent = Intent(c, RadioService::class.java).let { intent ->
|
||||
intent.putExtra("action", Actions.KILL.name)
|
||||
PendingIntent.getService(c, 99, intent, 0)
|
||||
}
|
||||
|
||||
|
||||
val currentMillis = System.currentTimeMillis()
|
||||
if (minutes > 0)
|
||||
{
|
||||
AlarmManagerCompat.setExactAndAllowWhileIdle(alarmManager, AlarmManager.RTC_WAKEUP, currentMillis + (minutes * 60 * 1000), sleepIntent)
|
||||
fadeOutIntent = Intent(c, RadioService::class.java).let { intent ->
|
||||
intent.putExtra("action", Actions.FADE_OUT.name)
|
||||
PendingIntent.getService(c, 98, intent, 0)
|
||||
}
|
||||
|
||||
AlarmManagerCompat.setExactAndAllowWhileIdle(alarmManager, AlarmManager.RTC_WAKEUP, currentMillis + (minutes * 60 * 1000) - (1 * 60 * 1000), fadeOutIntent)
|
||||
sleepAtMillis.value = System.currentTimeMillis() + (minutes * 60 * 1000) - 1 // this -1 allows to round the division for display at the right integer
|
||||
Log.d(tag, "set sleep to $minutes minutes")
|
||||
|
|
@ -59,19 +66,20 @@ class RadioSleeper {
|
|||
}
|
||||
|
||||
|
||||
fun cancelSleep(c: Context)
|
||||
fun cancelSleep(c: Context, isClosing: Boolean = false)
|
||||
{
|
||||
if (::sleepIntent.isInitialized)
|
||||
{
|
||||
val alarmManager = c.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
alarmManager.cancel(sleepIntent)
|
||||
alarmManager.cancel(fadeOutIntent)
|
||||
defineIntents(c)
|
||||
val alarmManager = c.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
||||
alarmManager.cancel(sleepIntent)
|
||||
alarmManager.cancel(fadeOutIntent)
|
||||
|
||||
if (!isClosing)
|
||||
{
|
||||
val cancelFadeOutIntent = Intent(c, RadioService::class.java).putExtra("action", Actions.CANCEL_FADE_OUT.name)
|
||||
c.startService(cancelFadeOutIntent)
|
||||
|
||||
Log.d(tag, "cancelled sleep")
|
||||
}
|
||||
|
||||
Log.d(tag, "cancelled sleep")
|
||||
sleepAtMillis.value = null
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue