DirectReplyReceiver.kt 3.49 KB
Newer Older
1 2 3 4 5 6
package chat.rocket.android.push

import android.app.NotificationManager
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
7
import androidx.core.app.RemoteInput
8 9
import android.widget.Toast
import chat.rocket.android.R
10
import chat.rocket.android.server.infrastructure.ConnectionManagerFactory
11 12 13
import chat.rocket.common.RocketChatException
import chat.rocket.core.internal.rest.sendMessage
import dagger.android.AndroidInjection
Filipe de Lima Brito's avatar
Filipe de Lima Brito committed
14 15
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
16 17 18 19 20 21 22 23 24 25 26 27 28
import timber.log.Timber
import java.util.*
import javax.inject.Inject

/**
 * BroadcastReceiver for direct reply on notifications.
 */
class DirectReplyReceiver : BroadcastReceiver() {

    @Inject
    lateinit var factory: ConnectionManagerFactory
    @Inject
    lateinit var groupedPushes: GroupedPush
29 30
    @Inject
    lateinit var pushManager: PushManager
31 32
    @Inject
    lateinit var manager: NotificationManager
33 34 35 36 37 38

    override fun onReceive(context: Context, intent: Intent) {
        AndroidInjection.inject(this, context)
        if (ACTION_REPLY == intent.action) {
            val message = intent.getParcelableExtra<PushMessage>(EXTRA_PUSH_MESSAGE)
            message?.let {
Filipe de Lima Brito's avatar
Filipe de Lima Brito committed
39
                MainScope().launch {
40 41
                    val notificationId = it.notificationId.toInt()
                    val hostname = it.info.host
42 43
                    try {
                        sendMessage(it, extractReplyMessage(intent))
44 45
                        clearNotificationsByHostAndNotificationId(hostname, notificationId)
                        manager.cancel(notificationId)
46 47 48 49 50 51
                        val feedback = context.getString(R.string.notif_success_sending, it.title)
                        Toast.makeText(context, feedback, Toast.LENGTH_SHORT).show()
                    } catch (ex: RocketChatException) {
                        Timber.e(ex)
                        val feedback = context.getString(R.string.notif_error_sending)
                        Toast.makeText(context, feedback, Toast.LENGTH_SHORT).show()
52 53
                        clearNotificationsByHostAndNotificationId(hostname, notificationId)
                        pushManager.showNotification(it)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
                    }
                }
            }
        }
    }

    private suspend fun sendMessage(message: PushMessage, replyText: CharSequence?) {
        replyText?.let { reply ->
            val currentServer = message.info.hostname
            val roomId = message.info.roomId
            val connectionManager = factory.create(currentServer)
            val client = connectionManager.client
            val id = UUID.randomUUID().toString()
            client.sendMessage(id, roomId, reply.toString())
            // Do we need to disconnect here?
        }
    }

    private fun extractReplyMessage(intent: Intent): CharSequence? {
        val bundle = RemoteInput.getResultsFromIntent(intent)
        if (bundle != null) {
            return bundle.getCharSequence(REMOTE_INPUT_REPLY)
        }
        return null
    }

    /**
     * Clear notifications by the host they belong to and its unique id.
     */
    private fun clearNotificationsByHostAndNotificationId(host: String, notificationId: Int) {
        if (groupedPushes.hostToPushMessageList.isNotEmpty()) {
            val notifications = groupedPushes.hostToPushMessageList[host]
            notifications?.let {
                notifications.removeAll {
                    it.notificationId.toInt() == notificationId
                }
            }
        }
    }
}