From cee5564f395f91e44da1286e7f3cc4d748e869e7 Mon Sep 17 00:00:00 2001 From: Dariusz Olszewski Date: Sat, 14 May 2022 23:42:20 +0200 Subject: [PATCH] Reply from notification - initial error handling Signed-off-by: Dariusz Olszewski --- .../talk/receivers/DirectReplyReceiver.kt | 33 ++++++++++++++++--- app/src/main/res/values/strings.xml | 1 + 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/receivers/DirectReplyReceiver.kt b/app/src/main/java/com/nextcloud/talk/receivers/DirectReplyReceiver.kt index 95e81a1e8..3b9e5153f 100644 --- a/app/src/main/java/com/nextcloud/talk/receivers/DirectReplyReceiver.kt +++ b/app/src/main/java/com/nextcloud/talk/receivers/DirectReplyReceiver.kt @@ -26,12 +26,15 @@ import android.content.BroadcastReceiver import android.content.Context import android.content.Intent import android.os.Build +import android.text.Html +import android.util.Log import androidx.annotation.RequiresApi import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat import androidx.core.app.Person import androidx.core.app.RemoteInput import autodagger.AutoInjector +import com.nextcloud.talk.R import com.nextcloud.talk.api.NcApi import com.nextcloud.talk.application.NextcloudTalkApplication import com.nextcloud.talk.models.database.UserEntity @@ -103,9 +106,10 @@ class DirectReplyReceiver : BroadcastReceiver() { confirmReplySent() } + @RequiresApi(Build.VERSION_CODES.N) override fun onError(e: Throwable) { - // TODO - inform the user that sending of the reply failed - // unused atm + Log.e(TAG, "Failed to send reply", e) + informReplyFailed() } override fun onComplete() { @@ -114,6 +118,23 @@ class DirectReplyReceiver : BroadcastReceiver() { }) } + @RequiresApi(Build.VERSION_CODES.N) + private fun confirmReplySent() { + appendMessageToNotification(replyMessage!!) + } + + @RequiresApi(Build.VERSION_CODES.N) + private fun informReplyFailed() { + val errorMessage = + Html.fromHtml( + "" + + context.resources.getString(R.string.nc_message_failed_to_send) + + "", + Html.FROM_HTML_MODE_COMPACT + ) + appendMessageToNotification(errorMessage) + } + @RequiresApi(Build.VERSION_CODES.N) private fun findActiveNotification(notificationId: Int): Notification? { val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager @@ -121,7 +142,7 @@ class DirectReplyReceiver : BroadcastReceiver() { } @RequiresApi(Build.VERSION_CODES.N) - private fun confirmReplySent() { + private fun appendMessageToNotification(reply: CharSequence) { // Implementation inspired by the SO question and article below: // https://stackoverflow.com/questions/51549456/android-o-notification-for-direct-reply-message // https://medium.com/@sidorovroman3/android-how-to-use-messagingstyle-for-notifications-without-caching-messages-c414ef2b816c @@ -145,7 +166,7 @@ class DirectReplyReceiver : BroadcastReceiver() { .setName(currentUser.displayName) .setIcon(NotificationUtils.loadAvatarSync(avatarUrl)) .build() - val message = NotificationCompat.MessagingStyle.Message(replyMessage, System.currentTimeMillis(), me) + val message = NotificationCompat.MessagingStyle.Message(reply, System.currentTimeMillis(), me) previousStyle?.addMessage(message) // Set the updated style @@ -154,4 +175,8 @@ class DirectReplyReceiver : BroadcastReceiver() { // Update the active notification. NotificationManagerCompat.from(context).notify(systemNotificationId!!, previousBuilder.build()) } + + companion object { + const val TAG = "DirectReplyReceiver" + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a6d4d94ba..a5cb32f38 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -325,6 +325,7 @@ You: %1$s Message read Message sent + Failed to send message Remote audio off Add attachment