ソースを参照

fixed alarm and sleep intents - cancel correctly, don't restart service

yattoz 5 年 前
コミット
c77ca30e46

+ 5 - 7
app/src/main/java/fr/forum_thalie/tsumugi/RadioService.kt ファイルの表示

@@ -32,7 +32,6 @@ import androidx.media.AudioManagerCompat
32 32
 import androidx.preference.PreferenceManager
33 33
 import com.google.android.exoplayer2.*
34 34
 import com.google.android.exoplayer2.metadata.icy.*
35
-import com.google.android.exoplayer2.trackselection.DefaultTrackSelector
36 35
 import fr.forum_thalie.tsumugi.alarm.RadioAlarm
37 36
 import fr.forum_thalie.tsumugi.alarm.RadioSleeper
38 37
 import fr.forum_thalie.tsumugi.planning.Planning
@@ -282,17 +281,18 @@ class RadioService : MediaBrowserServiceCompat() {
282 281
         PlayerStore.instance.isServiceStarted.value = false
283 282
         PlayerStore.instance.isInitialized = false
284 283
 
284
+
285
+        RadioSleeper.instance.cancelSleep(this, isClosing = true)
286
+
285 287
         PreferenceManager.getDefaultSharedPreferences(this).edit {
286 288
             this.putBoolean("isSleeping", false)
287 289
             this.commit()
288 290
         }
289
-        RadioSleeper.instance.cancelSleep(this)
290 291
 
291 292
         apiTicker.cancel() // stops the timer.
292 293
         Log.d(tag, radioTag + "destroyed")
293 294
         // if the service is destroyed, the application had become useless.
294
-        // exitProcess(0)
295
-        Process.killProcess(Process.myPid())
295
+        exitProcess(0)
296 296
     }
297 297
 
298 298
     // ########################################
@@ -351,13 +351,11 @@ class RadioService : MediaBrowserServiceCompat() {
351 351
             setBufferDurationsMs(minBufferMillis, maxBufferMillis, bufferForPlayback, bufferForPlaybackAfterRebuffer)
352 352
         }.createDefaultLoadControl()
353 353
 
354
-        /*
354
+
355 355
         val playerBuilder = SimpleExoPlayer.Builder(this)
356 356
         playerBuilder.setLoadControl(loadControl)
357 357
         player = playerBuilder.build()
358
-        */
359 358
 
360
-        player = ExoPlayerFactory.newSimpleInstance(this, DefaultTrackSelector(), loadControl)
361 359
 
362 360
         player.addMetadataOutput {
363 361
             for (i in 0 until it.length()) {

+ 21 - 18
app/src/main/java/fr/forum_thalie/tsumugi/alarm/RadioAlarm.kt ファイルの表示

@@ -19,29 +19,39 @@ class RadioAlarm {
19 19
             RadioAlarm()
20 20
         }
21 21
     }
22
-    lateinit var alarmIntent: PendingIntent
23 22
 
23
+    private lateinit var alarmIntent: PendingIntent
24
+    private lateinit var showIntent: PendingIntent
24 25
 
25
-    fun cancelAlarm(c: Context)
26
+    private fun defineIntents(c: Context)
26 27
     {
27
-        if (::alarmIntent.isInitialized)
28
-        {
29
-            val alarmManager = c.getSystemService(Context.ALARM_SERVICE) as AlarmManager
30
-            alarmManager.cancel(alarmIntent)
28
+        alarmIntent = Intent(c, BootBroadcastReceiver::class.java).let { intent ->
29
+            intent.putExtra("action", "$tag.${Actions.PLAY_OR_FALLBACK.name}")
30
+            PendingIntent.getBroadcast(c, 0, intent, 0)
31
+        }
32
+        showIntent = Intent(c, ParametersActivity::class.java).let { intent ->
33
+            intent.putExtra("action", "alarm")
34
+            PendingIntent.getActivity(c, 0, intent, 0)
31 35
         }
32 36
     }
33 37
 
38
+
39
+    fun cancelAlarm(c: Context)
40
+    {
41
+        defineIntents(c)
42
+        val alarmManager = c.getSystemService(Context.ALARM_SERVICE) as AlarmManager
43
+        alarmManager.cancel(alarmIntent)
44
+    }
45
+
34 46
     fun setNextAlarm(c: Context, isForce: Boolean = false, forceTime: Int? = null, forceDays: Set<String>? = null)
35 47
     {
48
+        defineIntents(c)
36 49
         // don't do anything if the preference is set to FALSE, of course.
37 50
         if (!PreferenceManager.getDefaultSharedPreferences(c).getBoolean("isWakingUp", false) && !isForce)
38 51
             return
39 52
 
40 53
         val alarmManager = c.getSystemService(Context.ALARM_SERVICE) as AlarmManager
41
-        alarmIntent = Intent(c, BootBroadcastReceiver::class.java).let { intent ->
42
-            intent.putExtra("action", "$tag.${Actions.PLAY_OR_FALLBACK.name}")
43
-            PendingIntent.getBroadcast(c, 0, intent, 0)
44
-        }
54
+
45 55
         val showIntent = Intent(c, ParametersActivity::class.java).let { intent ->
46 56
             intent.putExtra("action", ActionOpenParam.ALARM.name)
47 57
             PendingIntent.getActivity(c, 0, intent, 0)
@@ -95,19 +105,12 @@ class RadioAlarm {
95 105
 
96 106
     fun snooze(c: Context)
97 107
     {
108
+        defineIntents(c)
98 109
         val snoozeString = preferenceStore.getString("snoozeDuration", "10") ?: "10"
99 110
         val snoozeMinutes = if (snoozeString == c.getString(R.string.disable)) 0  else Integer.parseInt(snoozeString)
100 111
         if (snoozeMinutes > 0)
101 112
         {
102 113
             val alarmManager = c.getSystemService(Context.ALARM_SERVICE) as AlarmManager
103
-            alarmIntent = Intent(c, BootBroadcastReceiver::class.java).let { intent ->
104
-                intent.putExtra("action", "$tag.${Actions.PLAY_OR_FALLBACK.name}")
105
-                PendingIntent.getBroadcast(c, 0, intent, 0)
106
-            }
107
-            val showIntent = Intent(c, ParametersActivity::class.java).let { intent ->
108
-                intent.putExtra("action", "alarm")
109
-                PendingIntent.getActivity(c, 0, intent, 0)
110
-            }
111 114
 
112 115
             AlarmManagerCompat.setAlarmClock(alarmManager, Calendar.getInstance().timeInMillis + (snoozeMinutes * 60 * 1000), showIntent, alarmIntent)
113 116
 

+ 24 - 16
app/src/main/java/fr/forum_thalie/tsumugi/alarm/RadioSleeper.kt ファイルの表示

@@ -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
 }