mirror of
https://github.com/nextcloud/talk-android
synced 2025-06-19 19:49:33 +01:00
WIP. Refactoring NotificationWorker
Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
parent
d191a93ce9
commit
ebcab60df5
@ -382,8 +382,8 @@ public interface NcApi {
|
|||||||
@Url String url);
|
@Url String url);
|
||||||
|
|
||||||
@GET
|
@GET
|
||||||
Observable<NotificationOverall> getNotification(@Header("Authorization") String authorization,
|
Observable<NotificationOverall> getNcNotification(@Header("Authorization") String authorization,
|
||||||
@Url String url);
|
@Url String url);
|
||||||
|
|
||||||
@FormUrlEncoded
|
@FormUrlEncoded
|
||||||
@POST
|
@POST
|
||||||
|
@ -0,0 +1,16 @@
|
|||||||
|
package com.nextcloud.talk.data
|
||||||
|
|
||||||
|
import android.os.Parcelable
|
||||||
|
import kotlinx.parcelize.Parcelize
|
||||||
|
|
||||||
|
@Parcelize
|
||||||
|
data class NotificationConfirmDialog(
|
||||||
|
var title: String? = "",
|
||||||
|
var question: String? = "",
|
||||||
|
var primaryActionDescription: String? = "",
|
||||||
|
var primaryActionUrl: String? = "",
|
||||||
|
var primaryActionMethod: String? = "",
|
||||||
|
var secondaryActionDescription: String? = "",
|
||||||
|
var secondaryActionUrl: String? = "",
|
||||||
|
var secondaryActionMethod: String? = ""
|
||||||
|
) : Parcelable
|
@ -165,10 +165,8 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
|
|||||||
} else if (isSpreedNotification()) {
|
} else if (isSpreedNotification()) {
|
||||||
Log.d(TAG, "pushMessage.type: " + pushMessage.type)
|
Log.d(TAG, "pushMessage.type: " + pushMessage.type)
|
||||||
when (pushMessage.type) {
|
when (pushMessage.type) {
|
||||||
TYPE_CHAT -> handleChatNotification()
|
TYPE_CHAT, TYPE_ROOM, TYPE_RECORDING -> handleNonCallPushMessage()
|
||||||
TYPE_ROOM -> handleRoomNotification()
|
TYPE_CALL -> handleCallPushMessage()
|
||||||
TYPE_CALL -> handleCallNotification()
|
|
||||||
TYPE_RECORDING -> handleRecordingNotification()
|
|
||||||
else -> Log.e(TAG, "unknown pushMessage.type")
|
else -> Log.e(TAG, "unknown pushMessage.type")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@ -178,25 +176,16 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
|
|||||||
return Result.success()
|
return Result.success()
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleChatNotification() {
|
private fun handleNonCallPushMessage() {
|
||||||
val chatIntent = Intent(context, MainActivity::class.java)
|
val mainActivityIntent = createMainActivityIntent()
|
||||||
chatIntent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
|
|
||||||
val chatBundle = Bundle()
|
|
||||||
chatBundle.putString(KEY_ROOM_TOKEN, pushMessage.id)
|
|
||||||
chatBundle.putParcelable(KEY_USER_ENTITY, signatureVerification.user)
|
|
||||||
chatBundle.putBoolean(KEY_FROM_NOTIFICATION_START_CALL, false)
|
|
||||||
chatIntent.putExtras(chatBundle)
|
|
||||||
if (pushMessage.notificationId != Long.MIN_VALUE) {
|
if (pushMessage.notificationId != Long.MIN_VALUE) {
|
||||||
showNotificationWithObjectData(chatIntent)
|
getNcDataAndShowNotification(mainActivityIntent)
|
||||||
} else {
|
} else {
|
||||||
showNotification(chatIntent)
|
showNotification(mainActivityIntent)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private fun createMainActivityIntent(): Intent {
|
||||||
* handle messages with type 'room', e.g. "xxx invited you to a group conversation"
|
|
||||||
*/
|
|
||||||
private fun handleRoomNotification() {
|
|
||||||
val intent = Intent(context, MainActivity::class.java)
|
val intent = Intent(context, MainActivity::class.java)
|
||||||
intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
|
intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
|
||||||
val bundle = Bundle()
|
val bundle = Bundle()
|
||||||
@ -204,12 +193,10 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
|
|||||||
bundle.putParcelable(KEY_USER_ENTITY, signatureVerification.user)
|
bundle.putParcelable(KEY_USER_ENTITY, signatureVerification.user)
|
||||||
bundle.putBoolean(KEY_FROM_NOTIFICATION_START_CALL, false)
|
bundle.putBoolean(KEY_FROM_NOTIFICATION_START_CALL, false)
|
||||||
intent.putExtras(bundle)
|
intent.putExtras(bundle)
|
||||||
if (bundle.containsKey(KEY_ROOM_TOKEN)) {
|
return intent
|
||||||
showNotificationWithObjectData(intent)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleCallNotification() {
|
private fun handleCallPushMessage() {
|
||||||
val fullScreenIntent = Intent(context, CallNotificationActivity::class.java)
|
val fullScreenIntent = Intent(context, CallNotificationActivity::class.java)
|
||||||
val bundle = Bundle()
|
val bundle = Bundle()
|
||||||
bundle.putString(KEY_ROOM_TOKEN, pushMessage.id)
|
bundle.putString(KEY_ROOM_TOKEN, pushMessage.id)
|
||||||
@ -261,18 +248,6 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
|
|||||||
checkIfCallIsActive(signatureVerification)
|
checkIfCallIsActive(signatureVerification)
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleRecordingNotification() {
|
|
||||||
val chatWithRecordingIntent = Intent(context, MainActivity::class.java)
|
|
||||||
chatWithRecordingIntent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
|
|
||||||
val chatBundle = Bundle()
|
|
||||||
chatBundle.putString(KEY_ROOM_TOKEN, pushMessage.id)
|
|
||||||
chatBundle.putParcelable(KEY_USER_ENTITY, signatureVerification.user)
|
|
||||||
chatWithRecordingIntent.putExtras(chatBundle)
|
|
||||||
if (pushMessage.notificationId != Long.MIN_VALUE) {
|
|
||||||
showNotificationWithObjectData(chatWithRecordingIntent)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun initNcApiAndCredentials() {
|
private fun initNcApiAndCredentials() {
|
||||||
credentials = ApiUtils.getCredentials(
|
credentials = ApiUtils.getCredentials(
|
||||||
signatureVerification.user!!.username,
|
signatureVerification.user!!.username,
|
||||||
@ -327,13 +302,13 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
|
|||||||
|
|
||||||
private fun isSpreedNotification() = SPREED_APP == pushMessage.app
|
private fun isSpreedNotification() = SPREED_APP == pushMessage.app
|
||||||
|
|
||||||
private fun showNotificationWithObjectData(intent: Intent) {
|
private fun getNcDataAndShowNotification(intent: Intent) {
|
||||||
val user = signatureVerification.user
|
val user = signatureVerification.user
|
||||||
|
|
||||||
// see https://github.com/nextcloud/notifications/blob/master/docs/ocs-endpoint-v2.md
|
// see https://github.com/nextcloud/notifications/blob/master/docs/ocs-endpoint-v2.md
|
||||||
ncApi.getNotification(
|
ncApi.getNcNotification(
|
||||||
credentials,
|
credentials,
|
||||||
ApiUtils.getUrlForNotificationWithId(
|
ApiUtils.getUrlForNcNotificationWithId(
|
||||||
user!!.baseUrl,
|
user!!.baseUrl,
|
||||||
(pushMessage.notificationId!!).toString()
|
(pushMessage.notificationId!!).toString()
|
||||||
)
|
)
|
||||||
@ -346,59 +321,9 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
|
|||||||
override fun onNext(notificationOverall: NotificationOverall) {
|
override fun onNext(notificationOverall: NotificationOverall) {
|
||||||
val ncNotification = notificationOverall.ocs!!.notification
|
val ncNotification = notificationOverall.ocs!!.notification
|
||||||
|
|
||||||
if ("recording" == ncNotification?.objectType) {
|
enrichPushMessageByNcNotificationData(ncNotification)
|
||||||
val bundle = Bundle()
|
val newIntent = enrichIntentByNcNotificationData(intent, ncNotification)
|
||||||
bundle.putParcelable(KEY_NOTIFICATION_RECORDING_NOTIFICATION, ncNotification)
|
showNotification(newIntent)
|
||||||
intent.putExtras(bundle)
|
|
||||||
}
|
|
||||||
|
|
||||||
if (ncNotification!!.messageRichParameters != null &&
|
|
||||||
ncNotification.messageRichParameters!!.size > 0
|
|
||||||
) {
|
|
||||||
pushMessage.text = getParsedMessage(
|
|
||||||
ncNotification.messageRich,
|
|
||||||
ncNotification.messageRichParameters
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
pushMessage.text = ncNotification.message
|
|
||||||
}
|
|
||||||
|
|
||||||
val subjectRichParameters = ncNotification.subjectRichParameters
|
|
||||||
|
|
||||||
pushMessage.timestamp = ncNotification.datetime!!.millis
|
|
||||||
|
|
||||||
if (subjectRichParameters != null && subjectRichParameters.size > 0) {
|
|
||||||
val callHashMap = subjectRichParameters["call"]
|
|
||||||
val userHashMap = subjectRichParameters["user"]
|
|
||||||
val guestHashMap = subjectRichParameters["guest"]
|
|
||||||
if (callHashMap != null && callHashMap.size > 0 && callHashMap.containsKey("name")) {
|
|
||||||
if (subjectRichParameters.containsKey("reaction")) {
|
|
||||||
pushMessage.subject = ""
|
|
||||||
pushMessage.text = ncNotification.subject
|
|
||||||
} else if (ncNotification.objectType == "chat") {
|
|
||||||
pushMessage.subject = callHashMap["name"]!!
|
|
||||||
} else {
|
|
||||||
pushMessage.subject = ncNotification.subject!!
|
|
||||||
}
|
|
||||||
if (callHashMap.containsKey("call-type")) {
|
|
||||||
conversationType = callHashMap["call-type"]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
val notificationUser = NotificationUser()
|
|
||||||
if (userHashMap != null && userHashMap.isNotEmpty()) {
|
|
||||||
notificationUser.id = userHashMap["id"]
|
|
||||||
notificationUser.type = userHashMap["type"]
|
|
||||||
notificationUser.name = userHashMap["name"]
|
|
||||||
pushMessage.notificationUser = notificationUser
|
|
||||||
} else if (guestHashMap != null && guestHashMap.isNotEmpty()) {
|
|
||||||
notificationUser.id = guestHashMap["id"]
|
|
||||||
notificationUser.type = guestHashMap["type"]
|
|
||||||
notificationUser.name = guestHashMap["name"]
|
|
||||||
pushMessage.notificationUser = notificationUser
|
|
||||||
}
|
|
||||||
}
|
|
||||||
pushMessage.objectId = ncNotification.objectId
|
|
||||||
showNotification(intent)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun onError(e: Throwable) {
|
override fun onError(e: Throwable) {
|
||||||
@ -411,6 +336,76 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun enrichIntentByNcNotificationData(
|
||||||
|
intent: Intent,
|
||||||
|
ncNotification: com.nextcloud.talk.models.json.notifications.Notification?
|
||||||
|
): Intent {
|
||||||
|
val newIntent = Intent(intent)
|
||||||
|
|
||||||
|
if ("recording" == ncNotification?.objectType) {
|
||||||
|
val bundle = Bundle()
|
||||||
|
bundle.putParcelable(KEY_NOTIFICATION_RECORDING_NOTIFICATION, ncNotification)
|
||||||
|
newIntent.putExtras(bundle)
|
||||||
|
}
|
||||||
|
|
||||||
|
return newIntent
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun enrichPushMessageByNcNotificationData(
|
||||||
|
ncNotification: com.nextcloud.talk.models.json.notifications.Notification?
|
||||||
|
) {
|
||||||
|
if (ncNotification!!.messageRichParameters != null &&
|
||||||
|
ncNotification.messageRichParameters!!.size > 0
|
||||||
|
) {
|
||||||
|
pushMessage.text = getParsedMessage(
|
||||||
|
ncNotification.messageRich,
|
||||||
|
ncNotification.messageRichParameters
|
||||||
|
)
|
||||||
|
} else {
|
||||||
|
pushMessage.text = ncNotification.message
|
||||||
|
}
|
||||||
|
|
||||||
|
val subjectRichParameters = ncNotification.subjectRichParameters
|
||||||
|
|
||||||
|
pushMessage.timestamp = ncNotification.datetime!!.millis
|
||||||
|
|
||||||
|
if (subjectRichParameters != null && subjectRichParameters.size > 0) {
|
||||||
|
val callHashMap = subjectRichParameters["call"]
|
||||||
|
val userHashMap = subjectRichParameters["user"]
|
||||||
|
val guestHashMap = subjectRichParameters["guest"]
|
||||||
|
if (callHashMap != null && callHashMap.size > 0 && callHashMap.containsKey("name")) {
|
||||||
|
if (subjectRichParameters.containsKey("reaction")) {
|
||||||
|
pushMessage.subject = ""
|
||||||
|
pushMessage.text = ncNotification.subject
|
||||||
|
} else if (ncNotification.objectType == "chat") {
|
||||||
|
pushMessage.subject = callHashMap["name"]!!
|
||||||
|
} else {
|
||||||
|
pushMessage.subject = ncNotification.subject!!
|
||||||
|
}
|
||||||
|
|
||||||
|
if (callHashMap.containsKey("call-type")) {
|
||||||
|
conversationType = callHashMap["call-type"]
|
||||||
|
}
|
||||||
|
if ("recording" == ncNotification.objectType) {
|
||||||
|
conversationType = "recording"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
val notificationUser = NotificationUser()
|
||||||
|
if (userHashMap != null && userHashMap.isNotEmpty()) {
|
||||||
|
notificationUser.id = userHashMap["id"]
|
||||||
|
notificationUser.type = userHashMap["type"]
|
||||||
|
notificationUser.name = userHashMap["name"]
|
||||||
|
pushMessage.notificationUser = notificationUser
|
||||||
|
} else if (guestHashMap != null && guestHashMap.isNotEmpty()) {
|
||||||
|
notificationUser.id = guestHashMap["id"]
|
||||||
|
notificationUser.type = guestHashMap["type"]
|
||||||
|
notificationUser.name = guestHashMap["name"]
|
||||||
|
pushMessage.notificationUser = notificationUser
|
||||||
|
}
|
||||||
|
}
|
||||||
|
pushMessage.objectId = ncNotification.objectId
|
||||||
|
}
|
||||||
|
|
||||||
@Suppress("MagicNumber")
|
@Suppress("MagicNumber")
|
||||||
private fun showNotification(intent: Intent) {
|
private fun showNotification(intent: Intent) {
|
||||||
val largeIcon: Bitmap
|
val largeIcon: Bitmap
|
||||||
@ -425,6 +420,10 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
|
|||||||
}
|
}
|
||||||
|
|
||||||
when (conversationType) {
|
when (conversationType) {
|
||||||
|
"recording" -> {
|
||||||
|
pushMessage.subject = "new Recording available"
|
||||||
|
largeIcon = ContextCompat.getDrawable(context!!, R.drawable.ic_baseline_videocam_24)?.toBitmap()!!
|
||||||
|
}
|
||||||
"one2one" -> {
|
"one2one" -> {
|
||||||
pushMessage.subject = ""
|
pushMessage.subject = ""
|
||||||
largeIcon = ContextCompat.getDrawable(context!!, R.drawable.ic_people_group_black_24px)?.toBitmap()!!
|
largeIcon = ContextCompat.getDrawable(context!!, R.drawable.ic_people_group_black_24px)?.toBitmap()!!
|
||||||
@ -451,6 +450,17 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
|
|||||||
val pendingIntent = PendingIntent.getActivity(context, requestCode, intent, intentFlag)
|
val pendingIntent = PendingIntent.getActivity(context, requestCode, intent, intentFlag)
|
||||||
val uri = Uri.parse(signatureVerification.user!!.baseUrl)
|
val uri = Uri.parse(signatureVerification.user!!.baseUrl)
|
||||||
val baseUrl = uri.host
|
val baseUrl = uri.host
|
||||||
|
|
||||||
|
var contentTitle: CharSequence? = ""
|
||||||
|
if (!TextUtils.isEmpty(pushMessage.subject)) {
|
||||||
|
contentTitle = EmojiCompat.get().process(pushMessage.subject)
|
||||||
|
}
|
||||||
|
|
||||||
|
var contentText: CharSequence? = ""
|
||||||
|
if (!TextUtils.isEmpty(pushMessage.text)) {
|
||||||
|
contentText = EmojiCompat.get().process(pushMessage.text!!)
|
||||||
|
}
|
||||||
|
|
||||||
val notificationBuilder = NotificationCompat.Builder(context!!, "1")
|
val notificationBuilder = NotificationCompat.Builder(context!!, "1")
|
||||||
.setLargeIcon(largeIcon)
|
.setLargeIcon(largeIcon)
|
||||||
.setSmallIcon(smallIcon)
|
.setSmallIcon(smallIcon)
|
||||||
@ -461,18 +471,9 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
|
|||||||
.setShowWhen(true)
|
.setShowWhen(true)
|
||||||
.setContentIntent(pendingIntent)
|
.setContentIntent(pendingIntent)
|
||||||
.setAutoCancel(true)
|
.setAutoCancel(true)
|
||||||
if (!TextUtils.isEmpty(pushMessage.subject)) {
|
.setContentTitle(contentTitle)
|
||||||
notificationBuilder.setContentTitle(
|
.setContentText(contentText)
|
||||||
EmojiCompat.get().process(pushMessage.subject)
|
.setColor(context!!.resources.getColor(R.color.colorPrimary))
|
||||||
)
|
|
||||||
}
|
|
||||||
if (!TextUtils.isEmpty(pushMessage.text)) {
|
|
||||||
notificationBuilder.setContentText(
|
|
||||||
EmojiCompat.get().process(pushMessage.text!!)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
notificationBuilder.color = context!!.resources.getColor(R.color.colorPrimary)
|
|
||||||
|
|
||||||
val notificationInfoBundle = Bundle()
|
val notificationInfoBundle = Bundle()
|
||||||
notificationInfoBundle.putLong(KEY_INTERNAL_USER_ID, signatureVerification.user!!.id!!)
|
notificationInfoBundle.putLong(KEY_INTERNAL_USER_ID, signatureVerification.user!!.id!!)
|
||||||
@ -519,7 +520,7 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
|
|||||||
|
|
||||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N &&
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N &&
|
||||||
TYPE_RECORDING == pushMessage.type &&
|
TYPE_RECORDING == pushMessage.type &&
|
||||||
pushMessage.notificationUser != null
|
pushMessage.notificationUser != null // null
|
||||||
) {
|
) {
|
||||||
prepareChatNotification(notificationBuilder, activeStatusBarNotification, systemNotificationId)
|
prepareChatNotification(notificationBuilder, activeStatusBarNotification, systemNotificationId)
|
||||||
}
|
}
|
||||||
|
@ -392,7 +392,7 @@ public class ApiUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// see https://github.com/nextcloud/notifications/blob/master/docs/ocs-endpoint-v2.md
|
// see https://github.com/nextcloud/notifications/blob/master/docs/ocs-endpoint-v2.md
|
||||||
public static String getUrlForNotificationWithId(String baseUrl, String notificationId) {
|
public static String getUrlForNcNotificationWithId(String baseUrl, String notificationId) {
|
||||||
return baseUrl + ocsApiVersion + "/apps/notifications/api/v2/notifications/" + notificationId;
|
return baseUrl + ocsApiVersion + "/apps/notifications/api/v2/notifications/" + notificationId;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user