WIP. Refactoring NotificationWorker

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
This commit is contained in:
Marcel Hibbe 2023-02-23 16:27:05 +01:00
parent d191a93ce9
commit ebcab60df5
No known key found for this signature in database
GPG Key ID: C793F8B59F43CE7B
4 changed files with 123 additions and 106 deletions

View File

@ -382,7 +382,7 @@ 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

View File

@ -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

View File

@ -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,12 +321,39 @@ 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
enrichPushMessageByNcNotificationData(ncNotification)
val newIntent = enrichIntentByNcNotificationData(intent, ncNotification)
showNotification(newIntent)
}
override fun onError(e: Throwable) {
// unused atm
}
override fun onComplete() {
// unused atm
}
})
}
private fun enrichIntentByNcNotificationData(
intent: Intent,
ncNotification: com.nextcloud.talk.models.json.notifications.Notification?
): Intent {
val newIntent = Intent(intent)
if ("recording" == ncNotification?.objectType) { if ("recording" == ncNotification?.objectType) {
val bundle = Bundle() val bundle = Bundle()
bundle.putParcelable(KEY_NOTIFICATION_RECORDING_NOTIFICATION, ncNotification) bundle.putParcelable(KEY_NOTIFICATION_RECORDING_NOTIFICATION, ncNotification)
intent.putExtras(bundle) newIntent.putExtras(bundle)
} }
return newIntent
}
private fun enrichPushMessageByNcNotificationData(
ncNotification: com.nextcloud.talk.models.json.notifications.Notification?
) {
if (ncNotification!!.messageRichParameters != null && if (ncNotification!!.messageRichParameters != null &&
ncNotification.messageRichParameters!!.size > 0 ncNotification.messageRichParameters!!.size > 0
) { ) {
@ -380,9 +382,13 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
} else { } else {
pushMessage.subject = ncNotification.subject!! pushMessage.subject = ncNotification.subject!!
} }
if (callHashMap.containsKey("call-type")) { if (callHashMap.containsKey("call-type")) {
conversationType = callHashMap["call-type"] conversationType = callHashMap["call-type"]
} }
if ("recording" == ncNotification.objectType) {
conversationType = "recording"
}
} }
val notificationUser = NotificationUser() val notificationUser = NotificationUser()
if (userHashMap != null && userHashMap.isNotEmpty()) { if (userHashMap != null && userHashMap.isNotEmpty()) {
@ -398,17 +404,6 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
} }
} }
pushMessage.objectId = ncNotification.objectId pushMessage.objectId = ncNotification.objectId
showNotification(intent)
}
override fun onError(e: Throwable) {
// unused atm
}
override fun onComplete() {
// unused atm
}
})
} }
@Suppress("MagicNumber") @Suppress("MagicNumber")
@ -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)
} }

View File

@ -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;
} }