Merge pull request #2048 from nextcloud/feature/230/reply-from-notification-error

Reply from notification - initial error handling
This commit is contained in:
Marcel Hibbe 2022-05-17 13:33:30 +02:00 committed by GitHub
commit 968e8d2bab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 4 deletions

View File

@ -26,12 +26,16 @@ import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Build import android.os.Build
import android.text.SpannableStringBuilder
import android.text.style.ForegroundColorSpan
import android.util.Log
import androidx.annotation.RequiresApi import androidx.annotation.RequiresApi
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat import androidx.core.app.NotificationManagerCompat
import androidx.core.app.Person import androidx.core.app.Person
import androidx.core.app.RemoteInput import androidx.core.app.RemoteInput
import autodagger.AutoInjector import autodagger.AutoInjector
import com.nextcloud.talk.R
import com.nextcloud.talk.api.NcApi import com.nextcloud.talk.api.NcApi
import com.nextcloud.talk.application.NextcloudTalkApplication import com.nextcloud.talk.application.NextcloudTalkApplication
import com.nextcloud.talk.models.database.UserEntity import com.nextcloud.talk.models.database.UserEntity
@ -103,9 +107,10 @@ class DirectReplyReceiver : BroadcastReceiver() {
confirmReplySent() confirmReplySent()
} }
@RequiresApi(Build.VERSION_CODES.N)
override fun onError(e: Throwable) { override fun onError(e: Throwable) {
// TODO - inform the user that sending of the reply failed Log.e(TAG, "Failed to send reply", e)
// unused atm informReplyFailed()
} }
override fun onComplete() { override fun onComplete() {
@ -114,6 +119,19 @@ class DirectReplyReceiver : BroadcastReceiver() {
}) })
} }
@RequiresApi(Build.VERSION_CODES.N)
private fun confirmReplySent() {
appendMessageToNotification(replyMessage!!)
}
@RequiresApi(Build.VERSION_CODES.N)
private fun informReplyFailed() {
val errorColor = ForegroundColorSpan(context.resources.getColor(R.color.medium_emphasis_text, context.theme))
val errorMessageHeader = context.resources.getString(R.string.nc_message_failed_to_send)
val errorMessage = SpannableStringBuilder().append("$errorMessageHeader\n$replyMessage", errorColor, 0)
appendMessageToNotification(errorMessage)
}
@RequiresApi(Build.VERSION_CODES.N) @RequiresApi(Build.VERSION_CODES.N)
private fun findActiveNotification(notificationId: Int): Notification? { private fun findActiveNotification(notificationId: Int): Notification? {
val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
@ -121,7 +139,7 @@ class DirectReplyReceiver : BroadcastReceiver() {
} }
@RequiresApi(Build.VERSION_CODES.N) @RequiresApi(Build.VERSION_CODES.N)
private fun confirmReplySent() { private fun appendMessageToNotification(reply: CharSequence) {
// Implementation inspired by the SO question and article below: // Implementation inspired by the SO question and article below:
// https://stackoverflow.com/questions/51549456/android-o-notification-for-direct-reply-message // 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 // https://medium.com/@sidorovroman3/android-how-to-use-messagingstyle-for-notifications-without-caching-messages-c414ef2b816c
@ -145,7 +163,7 @@ class DirectReplyReceiver : BroadcastReceiver() {
.setName(currentUser.displayName) .setName(currentUser.displayName)
.setIcon(NotificationUtils.loadAvatarSync(avatarUrl)) .setIcon(NotificationUtils.loadAvatarSync(avatarUrl))
.build() .build()
val message = NotificationCompat.MessagingStyle.Message(replyMessage, System.currentTimeMillis(), me) val message = NotificationCompat.MessagingStyle.Message(reply, System.currentTimeMillis(), me)
previousStyle?.addMessage(message) previousStyle?.addMessage(message)
// Set the updated style // Set the updated style
@ -154,4 +172,8 @@ class DirectReplyReceiver : BroadcastReceiver() {
// Update the active notification. // Update the active notification.
NotificationManagerCompat.from(context).notify(systemNotificationId!!, previousBuilder.build()) NotificationManagerCompat.from(context).notify(systemNotificationId!!, previousBuilder.build())
} }
companion object {
const val TAG = "DirectReplyReceiver"
}
} }

View File

@ -325,6 +325,7 @@
<string name="nc_formatted_message_you">You: %1$s</string> <string name="nc_formatted_message_you">You: %1$s</string>
<string name="nc_message_read">Message read</string> <string name="nc_message_read">Message read</string>
<string name="nc_message_sent">Message sent</string> <string name="nc_message_sent">Message sent</string>
<string name="nc_message_failed_to_send">Failed to send message:</string>
<string name="nc_remote_audio_off">Remote audio off</string> <string name="nc_remote_audio_off">Remote audio off</string>
<string name="nc_add_attachment">Add attachment</string> <string name="nc_add_attachment">Add attachment</string>