Skip to content

Commit

Permalink
Fix missing mutability in PendingIntent creation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mopsalarm committed Dec 27, 2021
1 parent f162b77 commit 6d8266b
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 34 deletions.
81 changes: 51 additions & 30 deletions app/src/main/java/com/pr0gramm/app/services/NotificationService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import com.pr0gramm.app.ui.*
import com.pr0gramm.app.util.*
import com.pr0gramm.app.util.di.injector
import com.squareup.picasso.Picasso
import kotlinx.coroutines.flow.collect
import java.io.IOException
import java.util.Collections.synchronizedSet
import java.util.concurrent.atomic.AtomicInteger
Expand All @@ -40,8 +39,10 @@ import java.util.concurrent.atomic.AtomicInteger
/**
*/

class NotificationService(private val context: Application,
private val inboxService: InboxService) {
class NotificationService(
private val context: Application,
private val inboxService: InboxService
) {

private val logger = Logger("NotificationService")

Expand Down Expand Up @@ -93,8 +94,10 @@ class NotificationService(private val context: Application,
nm.createNotificationChannel(ch)
}

private fun notify(type: NotificationType, id: Int? = null,
configure: NotificationCompat.Builder.() -> Unit) {
private fun notify(
type: NotificationType, id: Int? = null,
configure: NotificationCompat.Builder.() -> Unit
) {

val n = NotificationCompat.Builder(context, type.channel).apply(configure).build()
nm.notify(type.channel, id ?: type.id, n)
Expand Down Expand Up @@ -168,9 +171,9 @@ class NotificationService(private val context: Application,
}

val messageGroups = messages
.filter { it.isUnread }
.groupBy { Pair(it.type, it.groupId) }
.values
.filter { it.isUnread }
.groupBy { Pair(it.type, it.groupId) }
.values

// convert to notifications
val notificationConfigs = messageGroups.map { messages ->
Expand Down Expand Up @@ -236,16 +239,21 @@ class NotificationService(private val context: Application,
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK

return TaskStackBuilder.create(context)
.addParentStack(UpdateActivity::class.java)
.addNextIntent(intent)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)!!
.addParentStack(UpdateActivity::class.java)
.addNextIntent(intent)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)!!
}

private fun viewFileIntent(uri: Uri): PendingIntent {
val intent = Intent(Intent.ACTION_VIEW)
intent.setDataAndType(uri, MimeTypeHelper.guessFromFileExtension(uri.toString()))
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
return PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
return PendingIntent.getActivity(
context,
0,
intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
}

fun cancelForUnreadConversation(conversationName: String) {
Expand Down Expand Up @@ -324,7 +332,7 @@ private class MessageNotificationConfig(context: Context, messages: List<Message

// select the most recent message
private val message = messages.maxByOrNull { it.creationTime }
?: throw IllegalArgumentException("Must contain at least one message.")
?: throw IllegalArgumentException("Must contain at least one message.")

// the type of all messages, if they all have the same type, null otherwise.
val messageType: MessageType = message.type
Expand Down Expand Up @@ -461,34 +469,43 @@ class NotificationHelperService(val context: Context) {
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK

return TaskStackBuilder.create(context)
.addNextIntentWithParentStack(intent)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)!!
.addNextIntentWithParentStack(intent)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)!!
}

fun markAsReadIntent(message: Message): PendingIntent {
val intent = InboxNotificationCanceledReceiver.makeIntent(context, message)
return PendingIntent.getBroadcast(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT)
return PendingIntent.getBroadcast(
context, 0, intent,
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)
}

/**
* This builds the little "reply" action under a notification.
*/
fun buildReplyAction(notificationId: Int, message: Message): NotificationCompat.Action {
// build the intent to fire on reply
val pendingIntent = PendingIntent.getBroadcast(context, 0,
MessageReplyReceiver.makeIntent(context, notificationId, message),
PendingIntent.FLAG_UPDATE_CURRENT)
val pendingIntent = PendingIntent.getBroadcast(
context, 0,
MessageReplyReceiver.makeIntent(context, notificationId, message),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
)

// the input field
val remoteInput = RemoteInput.Builder("msg")
.setLabel(context.getString(R.string.notify_reply_to_x, message.name))
.build()
.setLabel(context.getString(R.string.notify_reply_to_x, message.name))
.build()

// add everything as an action
return NotificationCompat.Action.Builder(R.drawable.ic_reply, context.getString(R.string.notify_reply), pendingIntent)
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY)
.addRemoteInput(remoteInput)
.build()
return NotificationCompat.Action.Builder(
R.drawable.ic_reply,
context.getString(R.string.notify_reply),
pendingIntent
)
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_REPLY)
.addRemoteInput(remoteInput)
.build()
}

/**
Expand All @@ -498,9 +515,13 @@ class NotificationHelperService(val context: Context) {
val pendingIntent = inboxActivityIntent(inboxType, conversationName)

// add everything as an action
return NotificationCompat.Action.Builder(R.drawable.ic_action_email, context.getString(R.string.notify_to_inbox), pendingIntent)
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_MARK_AS_READ)
.build()
return NotificationCompat.Action.Builder(
R.drawable.ic_action_email,
context.getString(R.string.notify_to_inbox),
pendingIntent
)
.setSemanticAction(NotificationCompat.Action.SEMANTIC_ACTION_MARK_AS_READ)
.build()
}

fun openItemIntent(message: Message): PendingIntent {
Expand All @@ -515,8 +536,8 @@ class NotificationHelperService(val context: Context) {
intent.putExtra(MainActivity.EXTRA_MARK_AS_READ_MESSAGE_TYPE, MessageType.STALK.name)

return TaskStackBuilder.create(context)
.addNextIntentWithParentStack(intent)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT)!!
.addNextIntentWithParentStack(intent)
.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)!!
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,18 @@ class PreloadService : IntentService("PreloadService"), LazyInjectorAware {
jobId = System.currentTimeMillis()
canceled = false

val cancelIntent = PendingIntent.getService(this, 0,
Intent(this, PreloadService::class.java).putExtra(EXTRA_CANCEL, jobId),
PendingIntent.FLAG_UPDATE_CURRENT)
val cancelIntent = PendingIntent.getService(
this, 0,
Intent(this, PreloadService::class.java).putExtra(EXTRA_CANCEL, jobId),
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)

// update notification
show {
val icon = R.drawable.ic_white_action_clear
addAction(icon, getString(R.string.cancel), cancelIntent)

setProgress(100 * items.size, 0, false)
setContentIntent(cancelIntent)
}

// create a wake lock
Expand Down

0 comments on commit 6d8266b

Please sign in to comment.