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,7 +382,7 @@ public interface NcApi {
|
||||
@Url String url);
|
||||
|
||||
@GET
|
||||
Observable<NotificationOverall> getNotification(@Header("Authorization") String authorization,
|
||||
Observable<NotificationOverall> getNcNotification(@Header("Authorization") String authorization,
|
||||
@Url String url);
|
||||
|
||||
@FormUrlEncoded
|
||||
|
@ -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()) {
|
||||
Log.d(TAG, "pushMessage.type: " + pushMessage.type)
|
||||
when (pushMessage.type) {
|
||||
TYPE_CHAT -> handleChatNotification()
|
||||
TYPE_ROOM -> handleRoomNotification()
|
||||
TYPE_CALL -> handleCallNotification()
|
||||
TYPE_RECORDING -> handleRecordingNotification()
|
||||
TYPE_CHAT, TYPE_ROOM, TYPE_RECORDING -> handleNonCallPushMessage()
|
||||
TYPE_CALL -> handleCallPushMessage()
|
||||
else -> Log.e(TAG, "unknown pushMessage.type")
|
||||
}
|
||||
} else {
|
||||
@ -178,25 +176,16 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
|
||||
return Result.success()
|
||||
}
|
||||
|
||||
private fun handleChatNotification() {
|
||||
val chatIntent = Intent(context, MainActivity::class.java)
|
||||
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)
|
||||
private fun handleNonCallPushMessage() {
|
||||
val mainActivityIntent = createMainActivityIntent()
|
||||
if (pushMessage.notificationId != Long.MIN_VALUE) {
|
||||
showNotificationWithObjectData(chatIntent)
|
||||
getNcDataAndShowNotification(mainActivityIntent)
|
||||
} else {
|
||||
showNotification(chatIntent)
|
||||
showNotification(mainActivityIntent)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* handle messages with type 'room', e.g. "xxx invited you to a group conversation"
|
||||
*/
|
||||
private fun handleRoomNotification() {
|
||||
private fun createMainActivityIntent(): Intent {
|
||||
val intent = Intent(context, MainActivity::class.java)
|
||||
intent.flags = Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
val bundle = Bundle()
|
||||
@ -204,12 +193,10 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
|
||||
bundle.putParcelable(KEY_USER_ENTITY, signatureVerification.user)
|
||||
bundle.putBoolean(KEY_FROM_NOTIFICATION_START_CALL, false)
|
||||
intent.putExtras(bundle)
|
||||
if (bundle.containsKey(KEY_ROOM_TOKEN)) {
|
||||
showNotificationWithObjectData(intent)
|
||||
}
|
||||
return intent
|
||||
}
|
||||
|
||||
private fun handleCallNotification() {
|
||||
private fun handleCallPushMessage() {
|
||||
val fullScreenIntent = Intent(context, CallNotificationActivity::class.java)
|
||||
val bundle = Bundle()
|
||||
bundle.putString(KEY_ROOM_TOKEN, pushMessage.id)
|
||||
@ -261,18 +248,6 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
|
||||
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() {
|
||||
credentials = ApiUtils.getCredentials(
|
||||
signatureVerification.user!!.username,
|
||||
@ -327,13 +302,13 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
|
||||
|
||||
private fun isSpreedNotification() = SPREED_APP == pushMessage.app
|
||||
|
||||
private fun showNotificationWithObjectData(intent: Intent) {
|
||||
private fun getNcDataAndShowNotification(intent: Intent) {
|
||||
val user = signatureVerification.user
|
||||
|
||||
// see https://github.com/nextcloud/notifications/blob/master/docs/ocs-endpoint-v2.md
|
||||
ncApi.getNotification(
|
||||
ncApi.getNcNotification(
|
||||
credentials,
|
||||
ApiUtils.getUrlForNotificationWithId(
|
||||
ApiUtils.getUrlForNcNotificationWithId(
|
||||
user!!.baseUrl,
|
||||
(pushMessage.notificationId!!).toString()
|
||||
)
|
||||
@ -346,12 +321,39 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
|
||||
override fun onNext(notificationOverall: NotificationOverall) {
|
||||
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) {
|
||||
val bundle = Bundle()
|
||||
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 &&
|
||||
ncNotification.messageRichParameters!!.size > 0
|
||||
) {
|
||||
@ -380,9 +382,13 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
|
||||
} 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()) {
|
||||
@ -398,17 +404,6 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
|
||||
}
|
||||
}
|
||||
pushMessage.objectId = ncNotification.objectId
|
||||
showNotification(intent)
|
||||
}
|
||||
|
||||
override fun onError(e: Throwable) {
|
||||
// unused atm
|
||||
}
|
||||
|
||||
override fun onComplete() {
|
||||
// unused atm
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
@Suppress("MagicNumber")
|
||||
@ -425,6 +420,10 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
|
||||
}
|
||||
|
||||
when (conversationType) {
|
||||
"recording" -> {
|
||||
pushMessage.subject = "new Recording available"
|
||||
largeIcon = ContextCompat.getDrawable(context!!, R.drawable.ic_baseline_videocam_24)?.toBitmap()!!
|
||||
}
|
||||
"one2one" -> {
|
||||
pushMessage.subject = ""
|
||||
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 uri = Uri.parse(signatureVerification.user!!.baseUrl)
|
||||
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")
|
||||
.setLargeIcon(largeIcon)
|
||||
.setSmallIcon(smallIcon)
|
||||
@ -461,18 +471,9 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor
|
||||
.setShowWhen(true)
|
||||
.setContentIntent(pendingIntent)
|
||||
.setAutoCancel(true)
|
||||
if (!TextUtils.isEmpty(pushMessage.subject)) {
|
||||
notificationBuilder.setContentTitle(
|
||||
EmojiCompat.get().process(pushMessage.subject)
|
||||
)
|
||||
}
|
||||
if (!TextUtils.isEmpty(pushMessage.text)) {
|
||||
notificationBuilder.setContentText(
|
||||
EmojiCompat.get().process(pushMessage.text!!)
|
||||
)
|
||||
}
|
||||
|
||||
notificationBuilder.color = context!!.resources.getColor(R.color.colorPrimary)
|
||||
.setContentTitle(contentTitle)
|
||||
.setContentText(contentText)
|
||||
.setColor(context!!.resources.getColor(R.color.colorPrimary))
|
||||
|
||||
val notificationInfoBundle = Bundle()
|
||||
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 &&
|
||||
TYPE_RECORDING == pushMessage.type &&
|
||||
pushMessage.notificationUser != null
|
||||
pushMessage.notificationUser != null // null
|
||||
) {
|
||||
prepareChatNotification(notificationBuilder, activeStatusBarNotification, systemNotificationId)
|
||||
}
|
||||
|
@ -392,7 +392,7 @@ public class ApiUtils {
|
||||
}
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user