Browse Source

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

yattoz 5 years ago
parent
commit
c77ca30e46

+ 5 - 7
app/src/main/java/fr/forum_thalie/tsumugi/RadioService.kt View File

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

+ 21 - 18
app/src/main/java/fr/forum_thalie/tsumugi/alarm/RadioAlarm.kt View File

19
             RadioAlarm()
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
     fun setNextAlarm(c: Context, isForce: Boolean = false, forceTime: Int? = null, forceDays: Set<String>? = null)
46
     fun setNextAlarm(c: Context, isForce: Boolean = false, forceTime: Int? = null, forceDays: Set<String>? = null)
35
     {
47
     {
48
+        defineIntents(c)
36
         // don't do anything if the preference is set to FALSE, of course.
49
         // don't do anything if the preference is set to FALSE, of course.
37
         if (!PreferenceManager.getDefaultSharedPreferences(c).getBoolean("isWakingUp", false) && !isForce)
50
         if (!PreferenceManager.getDefaultSharedPreferences(c).getBoolean("isWakingUp", false) && !isForce)
38
             return
51
             return
39
 
52
 
40
         val alarmManager = c.getSystemService(Context.ALARM_SERVICE) as AlarmManager
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
         val showIntent = Intent(c, ParametersActivity::class.java).let { intent ->
55
         val showIntent = Intent(c, ParametersActivity::class.java).let { intent ->
46
             intent.putExtra("action", ActionOpenParam.ALARM.name)
56
             intent.putExtra("action", ActionOpenParam.ALARM.name)
47
             PendingIntent.getActivity(c, 0, intent, 0)
57
             PendingIntent.getActivity(c, 0, intent, 0)
95
 
105
 
96
     fun snooze(c: Context)
106
     fun snooze(c: Context)
97
     {
107
     {
108
+        defineIntents(c)
98
         val snoozeString = preferenceStore.getString("snoozeDuration", "10") ?: "10"
109
         val snoozeString = preferenceStore.getString("snoozeDuration", "10") ?: "10"
99
         val snoozeMinutes = if (snoozeString == c.getString(R.string.disable)) 0  else Integer.parseInt(snoozeString)
110
         val snoozeMinutes = if (snoozeString == c.getString(R.string.disable)) 0  else Integer.parseInt(snoozeString)
100
         if (snoozeMinutes > 0)
111
         if (snoozeMinutes > 0)
101
         {
112
         {
102
             val alarmManager = c.getSystemService(Context.ALARM_SERVICE) as AlarmManager
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
             AlarmManagerCompat.setAlarmClock(alarmManager, Calendar.getInstance().timeInMillis + (snoozeMinutes * 60 * 1000), showIntent, alarmIntent)
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 View File

30
     private lateinit var sleepIntent: PendingIntent
30
     private lateinit var sleepIntent: PendingIntent
31
     private lateinit var fadeOutIntent: PendingIntent
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
     fun setSleep(c: Context, isForce: Boolean = false, forceDuration: Long? = null)
45
     fun setSleep(c: Context, isForce: Boolean = false, forceDuration: Long? = null)
34
     {
46
     {
47
+        defineIntents(c)
35
         // don't do anything if the preference is set to FALSE, of course.
48
         // don't do anything if the preference is set to FALSE, of course.
36
         if (!PreferenceManager.getDefaultSharedPreferences(c).getBoolean("isSleeping", false) && !isForce)
49
         if (!PreferenceManager.getDefaultSharedPreferences(c).getBoolean("isSleeping", false) && !isForce)
37
             return
50
             return
39
         val minutes: Long = forceDuration ?: Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(c).getString("sleepDuration", "1") ?: "1").toLong()
52
         val minutes: Long = forceDuration ?: Integer.parseInt(PreferenceManager.getDefaultSharedPreferences(c).getString("sleepDuration", "1") ?: "1").toLong()
40
 
53
 
41
         val alarmManager = c.getSystemService(Context.ALARM_SERVICE) as AlarmManager
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
         val currentMillis = System.currentTimeMillis()
57
         val currentMillis = System.currentTimeMillis()
48
         if (minutes > 0)
58
         if (minutes > 0)
49
         {
59
         {
50
             AlarmManagerCompat.setExactAndAllowWhileIdle(alarmManager, AlarmManager.RTC_WAKEUP, currentMillis + (minutes * 60 * 1000),  sleepIntent)
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
             AlarmManagerCompat.setExactAndAllowWhileIdle(alarmManager, AlarmManager.RTC_WAKEUP, currentMillis + (minutes * 60 * 1000) - (1 * 60 * 1000), fadeOutIntent)
62
             AlarmManagerCompat.setExactAndAllowWhileIdle(alarmManager, AlarmManager.RTC_WAKEUP, currentMillis + (minutes * 60 * 1000) - (1 * 60 * 1000), fadeOutIntent)
56
             sleepAtMillis.value = System.currentTimeMillis() + (minutes * 60 * 1000) - 1 // this -1 allows to round the division for display at the right integer
63
             sleepAtMillis.value = System.currentTimeMillis() + (minutes * 60 * 1000) - 1 // this -1 allows to round the division for display at the right integer
57
             Log.d(tag, "set sleep to $minutes minutes")
64
             Log.d(tag, "set sleep to $minutes minutes")
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
             val cancelFadeOutIntent = Intent(c, RadioService::class.java).putExtra("action", Actions.CANCEL_FADE_OUT.name)
78
             val cancelFadeOutIntent = Intent(c, RadioService::class.java).putExtra("action", Actions.CANCEL_FADE_OUT.name)
71
             c.startService(cancelFadeOutIntent)
79
             c.startService(cancelFadeOutIntent)
72
-
73
-            Log.d(tag, "cancelled sleep")
74
         }
80
         }
81
+
82
+        Log.d(tag, "cancelled sleep")
75
         sleepAtMillis.value = null
83
         sleepAtMillis.value = null
76
     }
84
     }
77
 }
85
 }