Skip to content

Commit

Permalink
Add notification color configuration. Closes #1183
Browse files Browse the repository at this point in the history
  • Loading branch information
F43nd1r committed Apr 11, 2023
1 parent b5dddf4 commit 6bccf9c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,9 @@

package org.acra.config

import androidx.annotation.ColorInt
import androidx.annotation.DrawableRes
import androidx.annotation.StringRes
import com.faendir.kotlin.autodsl.AutoDsl
import org.acra.ACRAConstants
import org.acra.annotation.AcraDsl
import org.acra.ktx.plus

Expand Down Expand Up @@ -170,6 +169,15 @@ class NotificationConfiguration(
* @since 5.0.0
*/
val sendOnClick: Boolean = false,

/**
* set notification color
*
* @see androidx.core.app.NotificationCompat.Builder.setColor
* @since 5.9.8
*/
@ColorInt
val color: Int? = null,
) : Configuration {
override fun enabled(): Boolean = enabled
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,34 +63,33 @@ class NotificationInteraction : HasConfigPlugin(NotificationConfiguration::class
}
//configure base notification
val notification = NotificationCompat.Builder(context, CHANNEL)
.setWhen(System.currentTimeMillis())
.setContentTitle(notificationConfig.title)
.setContentText(notificationConfig.text)
.setSmallIcon(notificationConfig.resIcon)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setWhen(System.currentTimeMillis())
.setContentTitle(notificationConfig.title)
.setContentText(notificationConfig.text)
.setSmallIcon(notificationConfig.resIcon)
.setPriority(NotificationCompat.PRIORITY_HIGH)
//add ticker if set
if (notificationConfig.tickerText?.isNotEmpty() == true) {
notification.setTicker(notificationConfig.tickerText)
}
notificationConfig.tickerText?.takeIf { it.isNotBlank() }?.let { notification.setTicker(it) }
//add color if set
notificationConfig.color?.let { notification.color = it }
val sendIntent = getSendIntent(context, config, reportFile)
val discardIntent = getDiscardIntent(context)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N && notificationConfig.sendWithCommentButtonText?.isNotEmpty() == true) {
val remoteInput = RemoteInput.Builder(KEY_COMMENT)
if (notificationConfig.commentPrompt?.isNotEmpty() == true) {
remoteInput.setLabel(notificationConfig.commentPrompt)
}
notificationConfig.commentPrompt?.takeIf { it.isNotBlank() }?.let { remoteInput.setLabel(it) }
notification.addAction(
NotificationCompat.Action.Builder(notificationConfig.resSendWithCommentButtonIcon, notificationConfig.sendWithCommentButtonText, sendIntent)
.addRemoteInput(remoteInput.build()).build())
NotificationCompat.Action.Builder(notificationConfig.resSendWithCommentButtonIcon, notificationConfig.sendWithCommentButtonText, sendIntent)
.addRemoteInput(remoteInput.build()).build()
)
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
val bigView = getBigView(context, notificationConfig)
notification.addAction(notificationConfig.resSendButtonIcon, notificationConfig.sendButtonText ?: context.getString(android.R.string.ok), sendIntent)
.addAction(notificationConfig.resDiscardButtonIcon, notificationConfig.discardButtonText ?: context.getString(android.R.string.cancel), discardIntent)
.setCustomContentView(getSmallView(context, notificationConfig, sendIntent, discardIntent))
.setCustomBigContentView(bigView)
.setCustomHeadsUpContentView(bigView)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
.addAction(notificationConfig.resDiscardButtonIcon, notificationConfig.discardButtonText ?: context.getString(android.R.string.cancel), discardIntent)
.setCustomContentView(getSmallView(context, notificationConfig, sendIntent, discardIntent))
.setCustomBigContentView(bigView)
.setCustomHeadsUpContentView(bigView)
.setStyle(NotificationCompat.DecoratedCustomViewStyle())
}
//On old devices we have no notification buttons, so we have to set the intent to the only possible interaction: click
if (notificationConfig.sendOnClick || Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
Expand All @@ -101,7 +100,7 @@ class NotificationInteraction : HasConfigPlugin(NotificationConfiguration::class
return false
}

private val pendingIntentFlags = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
private val pendingIntentFlags = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
} else {
PendingIntent.FLAG_UPDATE_CURRENT
Expand Down

0 comments on commit 6bccf9c

Please sign in to comment.