|
@@ -30,8 +30,21 @@ class RadioSleeper {
|
30
|
30
|
private lateinit var sleepIntent: PendingIntent
|
31
|
31
|
private lateinit var fadeOutIntent: PendingIntent
|
32
|
32
|
|
|
33
|
+ private fun defineIntents(c: Context)
|
|
34
|
+ {
|
|
35
|
+ sleepIntent = Intent(c, RadioService::class.java).let { intent ->
|
|
36
|
+ intent.putExtra("action", Actions.KILL.name)
|
|
37
|
+ PendingIntent.getService(c, 99, intent, 0)
|
|
38
|
+ }
|
|
39
|
+ fadeOutIntent = Intent(c, RadioService::class.java).let { intent ->
|
|
40
|
+ intent.putExtra("action", Actions.FADE_OUT.name)
|
|
41
|
+ PendingIntent.getService(c, 98, intent, 0)
|
|
42
|
+ }
|
|
43
|
+ }
|
|
44
|
+
|
33
|
45
|
fun setSleep(c: Context, isForce: Boolean = false, forceDuration: Long? = null)
|
34
|
46
|
{
|
|
47
|
+ defineIntents(c)
|
35
|
48
|
// don't do anything if the preference is set to FALSE, of course.
|
36
|
49
|
if (!PreferenceManager.getDefaultSharedPreferences(c).getBoolean("isSleeping", false) && !isForce)
|
37
|
50
|
return
|
|
@@ -39,19 +52,13 @@ class RadioSleeper {
|
39
|
52
|
val minutes: Long = forceDuration ?: Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(c).getString("sleepDuration", "1") ?: "1").toLong()
|
40
|
53
|
|
41
|
54
|
val alarmManager = c.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
42
|
|
- sleepIntent = Intent(c, RadioService::class.java).let { intent ->
|
43
|
|
- intent.putExtra("action", Actions.KILL.name)
|
44
|
|
- PendingIntent.getService(c, 99, intent, 0)
|
45
|
|
- }
|
|
55
|
+
|
46
|
56
|
|
47
|
57
|
val currentMillis = System.currentTimeMillis()
|
48
|
58
|
if (minutes > 0)
|
49
|
59
|
{
|
50
|
60
|
AlarmManagerCompat.setExactAndAllowWhileIdle(alarmManager, AlarmManager.RTC_WAKEUP, currentMillis + (minutes * 60 * 1000), sleepIntent)
|
51
|
|
- fadeOutIntent = Intent(c, RadioService::class.java).let { intent ->
|
52
|
|
- intent.putExtra("action", Actions.FADE_OUT.name)
|
53
|
|
- PendingIntent.getService(c, 98, intent, 0)
|
54
|
|
- }
|
|
61
|
+
|
55
|
62
|
AlarmManagerCompat.setExactAndAllowWhileIdle(alarmManager, AlarmManager.RTC_WAKEUP, currentMillis + (minutes * 60 * 1000) - (1 * 60 * 1000), fadeOutIntent)
|
56
|
63
|
sleepAtMillis.value = System.currentTimeMillis() + (minutes * 60 * 1000) - 1 // this -1 allows to round the division for display at the right integer
|
57
|
64
|
Log.d(tag, "set sleep to $minutes minutes")
|
|
@@ -59,19 +66,20 @@ class RadioSleeper {
|
59
|
66
|
}
|
60
|
67
|
|
61
|
68
|
|
62
|
|
- fun cancelSleep(c: Context)
|
|
69
|
+ fun cancelSleep(c: Context, isClosing: Boolean = false)
|
63
|
70
|
{
|
64
|
|
- if (::sleepIntent.isInitialized)
|
65
|
|
- {
|
66
|
|
- val alarmManager = c.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
67
|
|
- alarmManager.cancel(sleepIntent)
|
68
|
|
- alarmManager.cancel(fadeOutIntent)
|
|
71
|
+ defineIntents(c)
|
|
72
|
+ val alarmManager = c.getSystemService(Context.ALARM_SERVICE) as AlarmManager
|
|
73
|
+ alarmManager.cancel(sleepIntent)
|
|
74
|
+ alarmManager.cancel(fadeOutIntent)
|
69
|
75
|
|
|
76
|
+ if (!isClosing)
|
|
77
|
+ {
|
70
|
78
|
val cancelFadeOutIntent = Intent(c, RadioService::class.java).putExtra("action", Actions.CANCEL_FADE_OUT.name)
|
71
|
79
|
c.startService(cancelFadeOutIntent)
|
72
|
|
-
|
73
|
|
- Log.d(tag, "cancelled sleep")
|
74
|
80
|
}
|
|
81
|
+
|
|
82
|
+ Log.d(tag, "cancelled sleep")
|
75
|
83
|
sleepAtMillis.value = null
|
76
|
84
|
}
|
77
|
85
|
}
|