Browse Source

started updating intent calls. Crash when selecting day in alarm

yattoz 2 years ago
parent
commit
185f0867e2

+ 2 - 1
app/src/main/java/fr/forum_thalie/tsumugi/BaseNotification.kt View File

50
         notificationManager.notify(notificationId, notification)
50
         notificationManager.notify(notificationId, notification)
51
     }
51
     }
52
 
52
 
53
+    @RequiresApi(Build.VERSION_CODES.M)
53
     open fun create(c: Context) {
54
     open fun create(c: Context) {
54
         notificationManager = c.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
55
         notificationManager = c.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
55
 
56
 
58
         // thanks to the launchMode specified in the Manifest : android:launchMode="singleTop"
59
         // thanks to the launchMode specified in the Manifest : android:launchMode="singleTop"
59
         val pendingIntent = PendingIntent.getActivity(
60
         val pendingIntent = PendingIntent.getActivity(
60
             c, 0,
61
             c, 0,
61
-            notificationIntent, 0
62
+            notificationIntent, PendingIntent.FLAG_IMMUTABLE
62
         )
63
         )
63
         var channelID = ""
64
         var channelID = ""
64
 
65
 

+ 8 - 5
app/src/main/java/fr/forum_thalie/tsumugi/NowPlayingNotification.kt View File

3
 import android.app.PendingIntent
3
 import android.app.PendingIntent
4
 import android.content.Context
4
 import android.content.Context
5
 import android.content.Intent
5
 import android.content.Intent
6
+import android.os.Build
6
 import android.support.v4.media.session.MediaSessionCompat
7
 import android.support.v4.media.session.MediaSessionCompat
7
 import android.support.v4.media.session.PlaybackStateCompat
8
 import android.support.v4.media.session.PlaybackStateCompat
9
+import androidx.annotation.RequiresApi
8
 import androidx.core.app.NotificationCompat
10
 import androidx.core.app.NotificationCompat
9
 import fr.forum_thalie.tsumugi.playerstore.PlayerStore
11
 import fr.forum_thalie.tsumugi.playerstore.PlayerStore
10
 
12
 
34
         // got it right
36
         // got it right
35
         val delIntent = Intent(c, RadioService::class.java)
37
         val delIntent = Intent(c, RadioService::class.java)
36
         delIntent.putExtra("action", Actions.KILL.name)
38
         delIntent.putExtra("action", Actions.KILL.name)
37
-        val deleteIntent = PendingIntent.getService(c, 0, delIntent, PendingIntent.FLAG_NO_CREATE)
39
+        val deleteIntent = PendingIntent.getService(c, 0, delIntent, PendingIntent.FLAG_NO_CREATE + PendingIntent.FLAG_IMMUTABLE)
38
         builder.setDeleteIntent(deleteIntent)
40
         builder.setDeleteIntent(deleteIntent)
39
 
41
 
40
         mediaStyle = androidx.media.app.NotificationCompat.DecoratedMediaCustomViewStyle().also {
42
         mediaStyle = androidx.media.app.NotificationCompat.DecoratedMediaCustomViewStyle().also {
46
         update(c)
48
         update(c)
47
     }
49
     }
48
 
50
 
51
+    @RequiresApi(Build.VERSION_CODES.M)
49
     fun update(c: Context, isUpdatingNotificationButton: Boolean = false, isRinging: Boolean = false) {
52
     fun update(c: Context, isUpdatingNotificationButton: Boolean = false, isRinging: Boolean = false) {
50
 
53
 
51
         if (isUpdatingNotificationButton)
54
         if (isUpdatingNotificationButton)
72
 
75
 
73
             playPauseAction = if (PlayerStore.instance.playbackState.value == PlaybackStateCompat.STATE_PLAYING) {
76
             playPauseAction = if (PlayerStore.instance.playbackState.value == PlaybackStateCompat.STATE_PLAYING) {
74
                 intent.putExtra("action", Actions.PAUSE.name)
77
                 intent.putExtra("action", Actions.PAUSE.name)
75
-                val pendingButtonIntent = PendingIntent.getService(c, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT)
78
+                val pendingButtonIntent = PendingIntent.getService(c, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT + PendingIntent.FLAG_IMMUTABLE)
76
                 NotificationCompat.Action.Builder(R.drawable.ic_pause, "Pause", pendingButtonIntent).build()
79
                 NotificationCompat.Action.Builder(R.drawable.ic_pause, "Pause", pendingButtonIntent).build()
77
             } else {
80
             } else {
78
                 intent.putExtra("action", Actions.PLAY.name)
81
                 intent.putExtra("action", Actions.PLAY.name)
79
-                val pendingButtonIntent = PendingIntent.getService(c, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT)
82
+                val pendingButtonIntent = PendingIntent.getService(c, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT + PendingIntent.FLAG_IMMUTABLE)
80
                 NotificationCompat.Action.Builder(R.drawable.ic_play,"Play", pendingButtonIntent).build()
83
                 NotificationCompat.Action.Builder(R.drawable.ic_play,"Play", pendingButtonIntent).build()
81
             }
84
             }
82
             builder.addAction(playPauseAction)
85
             builder.addAction(playPauseAction)
83
             val intent2 = Intent(c, RadioService::class.java)
86
             val intent2 = Intent(c, RadioService::class.java)
84
             intent2.putExtra("action", Actions.KILL.name)
87
             intent2.putExtra("action", Actions.KILL.name)
85
-            val pendingButtonIntent = PendingIntent.getService(c, 2, intent2, PendingIntent.FLAG_UPDATE_CURRENT)
88
+            val pendingButtonIntent = PendingIntent.getService(c, 2, intent2, PendingIntent.FLAG_UPDATE_CURRENT + PendingIntent.FLAG_IMMUTABLE)
86
             val stopAction = NotificationCompat.Action.Builder(R.drawable.ic_close,"Stop", pendingButtonIntent).build()
89
             val stopAction = NotificationCompat.Action.Builder(R.drawable.ic_close,"Stop", pendingButtonIntent).build()
87
             builder.addAction(stopAction)
90
             builder.addAction(stopAction)
88
 
91
 
92
 
95
 
93
                 val snoozeIntent = Intent(c, RadioService::class.java)
96
                 val snoozeIntent = Intent(c, RadioService::class.java)
94
                 snoozeIntent.putExtra("action", Actions.SNOOZE.name)
97
                 snoozeIntent.putExtra("action", Actions.SNOOZE.name)
95
-                val pendingSnoozeIntent = PendingIntent.getService(c, 5, snoozeIntent, PendingIntent.FLAG_UPDATE_CURRENT)
98
+                val pendingSnoozeIntent = PendingIntent.getService(c, 5, snoozeIntent, PendingIntent.FLAG_UPDATE_CURRENT + PendingIntent.FLAG_IMMUTABLE)
96
                 val snoozeAction = NotificationCompat.Action.Builder(R.drawable.ic_alarm, "Snooze ($snoozeMinutes min.)", pendingSnoozeIntent ).build()
99
                 val snoozeAction = NotificationCompat.Action.Builder(R.drawable.ic_alarm, "Snooze ($snoozeMinutes min.)", pendingSnoozeIntent ).build()
97
                 if (snoozeMinutes > 0)
100
                 if (snoozeMinutes > 0)
98
                     builder.addAction(snoozeAction)
101
                     builder.addAction(snoozeAction)

+ 2 - 1
app/src/main/java/fr/forum_thalie/tsumugi/RadioService.kt View File

193
 
193
 
194
         // Define managers
194
         // Define managers
195
         telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
195
         telephonyManager = getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
196
-        telephonyManager?.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE)
196
+        // telephonyManager?.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE)
197
+
197
         audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
198
         audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
198
 
199
 
199
         //define the audioFocusRequest
200
         //define the audioFocusRequest

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

4
 import android.app.PendingIntent
4
 import android.app.PendingIntent
5
 import android.content.Context
5
 import android.content.Context
6
 import android.content.Intent
6
 import android.content.Intent
7
+import android.os.Build
7
 import android.util.Log
8
 import android.util.Log
9
+import androidx.annotation.RequiresApi
8
 import androidx.core.app.AlarmManagerCompat
10
 import androidx.core.app.AlarmManagerCompat
9
 import fr.forum_thalie.tsumugi.BootBroadcastReceiver
11
 import fr.forum_thalie.tsumugi.BootBroadcastReceiver
10
 import androidx.preference.PreferenceManager
12
 import androidx.preference.PreferenceManager
27
     {
29
     {
28
         alarmIntent = Intent(c, BootBroadcastReceiver::class.java).let { intent ->
30
         alarmIntent = Intent(c, BootBroadcastReceiver::class.java).let { intent ->
29
             intent.putExtra("action", "$tag.${Actions.PLAY_OR_FALLBACK.name}")
31
             intent.putExtra("action", "$tag.${Actions.PLAY_OR_FALLBACK.name}")
30
-            PendingIntent.getBroadcast(c, 0, intent, 0)
32
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
33
+                PendingIntent.getBroadcast(
34
+                    c,
35
+                    0,
36
+                    intent,
37
+                    PendingIntent.FLAG_IMMUTABLE
38
+                )
39
+            } else {
40
+                PendingIntent.getBroadcast(
41
+                    c,
42
+                    0,
43
+                    intent,
44
+                    0
45
+                )
46
+            }
31
         }
47
         }
32
         showIntent = Intent(c, ParametersActivity::class.java).let { intent ->
48
         showIntent = Intent(c, ParametersActivity::class.java).let { intent ->
33
             intent.putExtra("action", "alarm")
49
             intent.putExtra("action", "alarm")
34
-            PendingIntent.getActivity(c, 0, intent, 0)
50
+            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
51
+                PendingIntent.getActivity(c, 0, intent, PendingIntent.FLAG_IMMUTABLE)
52
+            } else {
53
+                PendingIntent.getActivity(c, 0, intent, 0)
54
+            }
35
         }
55
         }
36
     }
56
     }
37
 
57
 
43
         alarmManager.cancel(alarmIntent)
63
         alarmManager.cancel(alarmIntent)
44
     }
64
     }
45
 
65
 
66
+    @RequiresApi(Build.VERSION_CODES.M)
46
     fun setNextAlarm(c: Context, isForce: Boolean = false, forceTime: Int? = null, forceDays: Set<String>? = null)
67
     fun setNextAlarm(c: Context, isForce: Boolean = false, forceTime: Int? = null, forceDays: Set<String>? = null)
47
     {
68
     {
48
         defineIntents(c)
69
         defineIntents(c)
54
 
75
 
55
         val showIntent = Intent(c, ParametersActivity::class.java).let { intent ->
76
         val showIntent = Intent(c, ParametersActivity::class.java).let { intent ->
56
             intent.putExtra("action", ActionOpenParam.ALARM.name)
77
             intent.putExtra("action", ActionOpenParam.ALARM.name)
57
-            PendingIntent.getActivity(c, 0, intent, 0)
78
+            PendingIntent.getActivity(c, 0, intent, PendingIntent.FLAG_IMMUTABLE)
58
         }
79
         }
59
         val time = findNextAlarmTime(c, forceTime, forceDays)
80
         val time = findNextAlarmTime(c, forceTime, forceDays)
60
         if (time > 0)
81
         if (time > 0)

+ 6 - 2
app/src/main/java/fr/forum_thalie/tsumugi/alarm/RadioSleeper.kt View File

4
 import android.app.PendingIntent
4
 import android.app.PendingIntent
5
 import android.content.Context
5
 import android.content.Context
6
 import android.content.Intent
6
 import android.content.Intent
7
+import android.os.Build
7
 import android.util.Log
8
 import android.util.Log
9
+import androidx.annotation.RequiresApi
8
 import androidx.core.app.AlarmManagerCompat
10
 import androidx.core.app.AlarmManagerCompat
9
 import androidx.lifecycle.MutableLiveData
11
 import androidx.lifecycle.MutableLiveData
10
 import androidx.preference.PreferenceManager
12
 import androidx.preference.PreferenceManager
30
     private lateinit var sleepIntent: PendingIntent
32
     private lateinit var sleepIntent: PendingIntent
31
     private lateinit var fadeOutIntent: PendingIntent
33
     private lateinit var fadeOutIntent: PendingIntent
32
 
34
 
35
+    @RequiresApi(Build.VERSION_CODES.M)
33
     private fun defineIntents(c: Context)
36
     private fun defineIntents(c: Context)
34
     {
37
     {
35
         sleepIntent = Intent(c, RadioService::class.java).let { intent ->
38
         sleepIntent = Intent(c, RadioService::class.java).let { intent ->
36
             intent.putExtra("action", Actions.KILL.name)
39
             intent.putExtra("action", Actions.KILL.name)
37
-            PendingIntent.getService(c, 99, intent, 0)
40
+            PendingIntent.getService(c, 99, intent, PendingIntent.FLAG_IMMUTABLE)
38
         }
41
         }
39
         fadeOutIntent = Intent(c, RadioService::class.java).let { intent ->
42
         fadeOutIntent = Intent(c, RadioService::class.java).let { intent ->
40
             intent.putExtra("action", Actions.FADE_OUT.name)
43
             intent.putExtra("action", Actions.FADE_OUT.name)
41
-            PendingIntent.getService(c, 98, intent, 0)
44
+            PendingIntent.getService(c, 98, intent, PendingIntent.FLAG_IMMUTABLE)
42
         }
45
         }
43
     }
46
     }
44
 
47
 
48
+    @RequiresApi(Build.VERSION_CODES.M)
45
     fun setSleep(c: Context, isForce: Boolean = false, forceDuration: Long? = null)
49
     fun setSleep(c: Context, isForce: Boolean = false, forceDuration: Long? = null)
46
     {
50
     {
47
         defineIntents(c)
51
         defineIntents(c)