Commit 1c73d226 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Separate code in funs

parent 75ba17fa
...@@ -9,6 +9,7 @@ import android.content.Context ...@@ -9,6 +9,7 @@ import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.support.annotation.RequiresApi
import android.support.v4.app.NotificationCompat import android.support.v4.app.NotificationCompat
import android.support.v4.app.NotificationManagerCompat import android.support.v4.app.NotificationManagerCompat
import android.text.Html import android.text.Html
...@@ -21,16 +22,22 @@ object PushManager { ...@@ -21,16 +22,22 @@ object PushManager {
val messageStack = SparseArray<ArrayList<String>>() val messageStack = SparseArray<ArrayList<String>>()
fun handle(context: Context, data: Bundle) { fun handle(context: Context, data: Bundle) {
val now = System.currentTimeMillis()
val appContext = context.applicationContext val appContext = context.applicationContext
val message = data["message"] as String
val image = data["image"] as String
val ejson = data["ejson"] as String
val notificationId = data["notId"] as String val notificationId = data["notId"] as String
val style = data["style"] as String val style = data["style"] as String
val summaryText = data["summaryText"] as String val summaryText = data["summaryText"] as String
val count = data["count"] as String val count = data["count"] as String
val pushMessage = PushMessage(data["title"] as String, val pushMessage = PushMessage(data["title"] as String,
data["message"] as String, message,
data["image"] as String, image,
data["ejson"] as String) ejson,
count,
notificationId,
summaryText,
style)
val res = appContext.resources val res = appContext.resources
...@@ -38,72 +45,85 @@ object PushManager { ...@@ -38,72 +45,85 @@ object PushManager {
stackMessage(notificationId.toInt(), pushMessage.message) stackMessage(notificationId.toInt(), pushMessage.message)
val notificationManager: NotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
val notification: Notification
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val notificationManager: NotificationManager = notification = createNotificationForOreoAndAbove(appContext, pushMessage, smallIcon)
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.notify(notificationId.toInt(), notification)
with(pushMessage) {
val channel = NotificationChannel(notificationId, sender.username, NotificationManager.IMPORTANCE_HIGH)
val notification = Notification.Builder(appContext, pushMessage.rid)
.setAutoCancel(true)
.setShowWhen(true)
.setWhen(now)
.setContentTitle(title.fromHtml())
.setContentText(message.fromHtml())
.setNumber(count.toInt())
.setSmallIcon(smallIcon)
.build()
channel.enableLights(true)
channel.enableVibration(true)
notificationManager.createNotificationChannel(channel)
notificationManager.notify(notificationId.toInt(), notification)
}
} else { } else {
with(pushMessage) { notification = createCompatNotification(appContext, pushMessage, smallIcon)
val notificationBuilder = NotificationCompat.Builder(appContext) NotificationManagerCompat.from(appContext).notify(notificationId.toInt(), notification)
.setAutoCancel(true) }
.setShowWhen(true) }
.setDefaults(Notification.DEFAULT_ALL)
.setWhen(now) fun clearStack(notificationId: Int) {
.setContentTitle(title.fromHtml()) messageStack.delete(notificationId)
.setContentText(message.fromHtml()) }
.setNumber(count.toInt())
.setSmallIcon(smallIcon) private fun createCompatNotification(context: Context, pushMessage: PushMessage, smallIcon: Int): Notification {
.setDeleteIntent(getDismissIntent(appContext, notificationId.toInt())) with(pushMessage) {
val notificationBuilder = NotificationCompat.Builder(context)
if ("inbox" == style) { .setAutoCancel(true)
val messages = messageStack.get(notificationId.toInt()) .setShowWhen(true)
val messageCount = messages.size .setDefaults(Notification.DEFAULT_ALL)
if (messageCount > 1) { .setWhen(createdAt)
val summary = summaryText.replace("%n%", messageCount.toString()) .setContentTitle(title.fromHtml())
.fromHtml() .setContentText(message.fromHtml())
val inbox = NotificationCompat.InboxStyle() .setNumber(count.toInt())
.setBigContentTitle(title.fromHtml()) .setSmallIcon(smallIcon)
.setSummaryText(summary) .setDeleteIntent(getDismissIntent(context, notificationId.toInt()))
messages.forEach { msg ->
inbox.addLine(msg.fromHtml()) if ("inbox" == style) {
} val messages = chat.rocket.android.push.PushManager.messageStack.get(notificationId.toInt())
notificationBuilder.setStyle(inbox) val messageCount = messages.size
} else { if (messageCount > 1) {
val bigText = NotificationCompat.BigTextStyle() val summary = summaryText.replace("%n%", messageCount.toString())
.bigText(message.fromHtml()) .fromHtml()
.setBigContentTitle(title.fromHtml()) val inbox = android.support.v4.app.NotificationCompat.InboxStyle()
notificationBuilder.setStyle(bigText) .setBigContentTitle(title.fromHtml())
.setSummaryText(summary)
messages.forEach { msg ->
inbox.addLine(msg.fromHtml())
} }
notificationBuilder.setStyle(inbox)
} else { } else {
notificationBuilder.setContentText(message.fromHtml()) val bigText = android.support.v4.app.NotificationCompat.BigTextStyle()
.bigText(message.fromHtml())
.setBigContentTitle(title.fromHtml())
notificationBuilder.setStyle(bigText)
} }
} else {
val notification = notificationBuilder.build() notificationBuilder.setContentText(message.fromHtml())
NotificationManagerCompat.from(appContext).notify(notificationId.toInt(), notification)
} }
return notificationBuilder.build()
} }
} }
fun clearStack(notificationId: Int) { @RequiresApi(Build.VERSION_CODES.O)
messageStack.delete(notificationId) private fun createNotificationForOreoAndAbove(context: Context, pushMessage: PushMessage, smallIcon: Int): Notification {
val notificationManager: NotificationManager =
context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
with(pushMessage) {
val channel = NotificationChannel(notificationId, sender.username, NotificationManager.IMPORTANCE_HIGH)
val notification = Notification.Builder(context, pushMessage.rid)
.setAutoCancel(true)
.setShowWhen(true)
.setWhen(createdAt)
.setContentTitle(title.fromHtml())
.setContentText(message.fromHtml())
.setNumber(count.toInt())
.setSmallIcon(smallIcon)
.build()
channel.enableLights(true)
channel.enableVibration(true)
notificationManager.createNotificationChannel(channel)
return notification
}
} }
private fun stackMessage(id: Int, message: String) { private fun stackMessage(id: Int, message: String) {
...@@ -124,12 +144,20 @@ object PushManager { ...@@ -124,12 +144,20 @@ object PushManager {
return PendingIntent.getBroadcast(context, notificationId, deleteIntent, 0) return PendingIntent.getBroadcast(context, notificationId, deleteIntent, 0)
} }
data class PushMessage(val title: String, val message: String, val image: String?, val ejson: String) { data class PushMessage(val title: String,
val message: String,
val image: String?,
val ejson: String,
val count: String,
val notificationId: String,
val summaryText: String,
val style: String) {
val host: String val host: String
val rid: String val rid: String
val type: String val type: String
val name: String? val name: String?
val sender: Sender val sender: Sender
val createdAt: Long
init { init {
val json = JSONObject(ejson) val json = JSONObject(ejson)
...@@ -138,6 +166,7 @@ object PushManager { ...@@ -138,6 +166,7 @@ object PushManager {
type = json.getString("type") type = json.getString("type")
name = json.optString("name") name = json.optString("name")
sender = Sender(json.getString("sender")) sender = Sender(json.getString("sender"))
createdAt = System.currentTimeMillis()
} }
data class Sender(val sender: String) { data class Sender(val sender: String) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment