From f522beb57d33cc622df94ee3c7ed2991941ad12f Mon Sep 17 00:00:00 2001 From: Julius Linus Date: Thu, 19 Oct 2023 10:52:09 -0500 Subject: [PATCH] Bubbles - Only enabled on Conversations deemed important - Enabled ChatActivity to be resizable and embedded in Android Manifest Signed-off-by: Julius Linus --- app/src/main/AndroidManifest.xml | 8 +- .../ConversationInfoActivity.kt | 5 +- .../nextcloud/talk/jobs/NotificationWorker.kt | 103 +++++++++++++++--- 3 files changed, 94 insertions(+), 22 deletions(-) diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 89f0d25ef..0834e9e86 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -115,7 +115,8 @@ + android:windowSoftInputMode="adjustResize" + > @@ -217,7 +218,10 @@ + android:theme="@style/AppTheme" + android:allowEmbedded="true" + android:resizeableActivity="true" + /> handleNonCallPushMessage() TYPE_CALL -> handleCallPushMessage() @@ -319,6 +334,7 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor override fun onNext(notificationOverall: NotificationOverall) { val ncNotification = notificationOverall.ocs!!.notification + Log.d(TAG, "Notification is: $ncNotification") if (ncNotification != null) { enrichPushMessageByNcNotificationData(ncNotification) showNotification(intent, ncNotification) @@ -436,10 +452,10 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor val autoCancelOnClick = TYPE_RECORDING != pushMessage.type - val notificationBuilder = NotificationCompat.Builder(context!!, "1") + val notificationBuilder = NotificationCompat.Builder(context!!, "4") .setPriority(NotificationCompat.PRIORITY_HIGH) .setCategory(category) - .setLargeIcon(getLargeIcon()) + // .setLargeIcon(getLargeIcon()) .setSmallIcon(R.drawable.ic_logo) .setContentTitle(contentTitle) .setContentText(contentText) @@ -495,6 +511,12 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor prepareChatNotification(notificationBuilder, activeStatusBarNotification, systemNotificationId) addReplyAction(notificationBuilder, systemNotificationId) addMarkAsReadAction(notificationBuilder, systemNotificationId) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && + TYPE_REMINDER != + pushMessage.type + ) { + setBubble(notificationBuilder) + } } if (TYPE_RECORDING == pushMessage.type && ncNotification != null) { @@ -556,23 +578,13 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor val person = Person.Builder() .setKey(signatureVerification.user!!.id.toString() + "@" + notificationUser.id) .setName(EmojiCompat.get().process(notificationUser.name!!)) - .setBot("bot" == userType) + .setImportant(true) + .setBot(true) notificationBuilder.setOnlyAlertOnce(true) - - if ("user" == userType || "guest" == userType) { - val baseUrl = signatureVerification.user!!.baseUrl - val avatarUrl = if ("user" == userType) { - ApiUtils.getUrlForAvatar( - baseUrl, - notificationUser.id, - false - ) - } else { - ApiUtils.getUrlForGuestAvatar(baseUrl, notificationUser.name, false) - } - person.setIcon(loadAvatarSync(avatarUrl, context!!)) - } - notificationBuilder.setStyle(getStyle(person.build(), style)) + person.setIcon(getAvatarIcon()) + val personBuilt = person.build() + notificationBuilder.setStyle(getStyle(personBuilt, style)) + notificationBuilder.addPerson(personBuilt) } private fun buildIntentForAction(cls: Class<*>, systemNotificationId: Int, messageId: Int): PendingIntent { @@ -619,6 +631,60 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor } } + @RequiresApi(Build.VERSION_CODES.R) + private fun setBubble(notificationBuilder: NotificationCompat.Builder) { + val intent = getIntentToOpenConversation()!! + + val shortcutId = pushMessage.notificationUser!!.id!! + val shortcuts = ShortcutManagerCompat.getDynamicShortcuts(context!!) + val index = shortcuts.firstOrNull { + it.id == shortcutId + } + + if (index == null) { + val shortCutInfo = ShortcutInfoCompat.Builder(context!!, shortcutId) + .setCategories(setOf(Notification.CATEGORY_MESSAGE)) + .setIntent(Intent(Intent.ACTION_DEFAULT)) + .setLongLived(true) + .setShortLabel(pushMessage.notificationUser!!.name!!) + .build() + ShortcutManagerCompat.pushDynamicShortcut(context!!, shortCutInfo) + } + + val bubbleData = + BubbleMetadata.Builder(intent, getAvatarIcon()) + .setDesiredHeight(DESIRED_SIZE) + .setIntent(intent) + + bubbleData.setIcon(getAvatarIcon()) + + notificationBuilder + .setBubbleMetadata(bubbleData.build()) + .setShortcutId(shortcutId) + .setLocusId(LocusIdCompat(shortcutId)) + } + + private fun getAvatarIcon(): IconCompat { + val notificationUser = pushMessage.notificationUser + val userType = notificationUser!!.type + var result: IconCompat? = null + if ("user" == userType || "guest" == userType) { + val baseUrl = signatureVerification.user!!.baseUrl + val avatarUrl = if ("user" == userType) { + ApiUtils.getUrlForAvatar( + baseUrl, + notificationUser.type, + false + ) + } else { + ApiUtils.getUrlForGuestAvatar(baseUrl, notificationUser.name, false) + } + result = loadAvatarSync(avatarUrl, context!!) + } + + return result ?: IconCompat.createWithResource(context!!, R.drawable.account_circle_96dp) + } + private fun addReplyAction(notificationBuilder: NotificationCompat.Builder, systemNotificationId: Int) { val replyLabel = context!!.resources.getString(R.string.nc_reply) val remoteInput = RemoteInput.Builder(NotificationUtils.KEY_DIRECT_REPLY) @@ -994,5 +1060,6 @@ class NotificationWorker(context: Context, workerParams: WorkerParameters) : Wor private const val TIMER_COUNT = 12 private const val TIMER_DELAY: Long = 5 private const val GET_ROOM_RETRY_COUNT: Long = 3 + private const val DESIRED_SIZE = 600 } }