11package com.httpsms
22
3+ import android.app.Application
34import android.app.PendingIntent
45import android.content.Context
56import android.content.Intent
67import androidx.work.*
78import com.google.firebase.messaging.FirebaseMessagingService
89import com.google.firebase.messaging.RemoteMessage
910import timber.log.Timber
10- import java.time.ZoneOffset
11- import java.time.ZonedDateTime
1211
1312class MyFirebaseMessagingService : FirebaseMessagingService () {
1413 // [START receive_message]
@@ -133,14 +132,29 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
133132 val message = getMessage(applicationContext, messageID) ? : return Result .failure()
134133 if (! Settings .getActiveStatus(applicationContext, message.sim)) {
135134 Timber .w(" [${message.sim} ] SIM is not active, stopping processing" )
136- handleFailed(applicationContext, messageID)
135+ handleFailed(applicationContext, messageID, " Outgoing messages have been disabled on the mobile app " )
137136 return Result .failure()
138137 }
139138
139+ if (message.encrypted && Settings .getEncryptionKey(applicationContext).isNullOrEmpty()) {
140+ Timber .w(" [${message.sim} ] message is encrypted but the encryption key is empty" )
141+ handleFailed(applicationContext, messageID, " Outgoing message is encrypted but mobile app has no encryption key" )
142+ return Result .failure()
143+ }
144+ if (message.encrypted) {
145+ try {
146+ Encrypter .decrypt(Settings .getEncryptionKey(applicationContext)!! , message.content)
147+ } catch (exception: Exception ) {
148+ Timber .e(exception)
149+ handleFailed(applicationContext, messageID, " Cannot decrypt the outgoing message. Check your encryption key on the Android app." )
150+ return Result .failure()
151+ }
152+ }
153+
140154 Receiver .register(applicationContext)
141155 val parts = getMessageParts(applicationContext, message)
142156 if (parts.size == 1 ) {
143- return handleSingleMessage(message)
157+ return handleSingleMessage(message, parts.first() )
144158 }
145159 return handleMultipartMessage(message, parts)
146160 }
@@ -174,16 +188,17 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
174188 }
175189
176190
177- private fun handleSingleMessage (message : Message ): Result {
191+ private fun handleSingleMessage (message : Message , content : String ): Result {
178192 sendMessage(
179193 message,
194+ content,
180195 createPendingIntent(message.id, SmsManagerService .sentAction()),
181196 createPendingIntent(message.id, SmsManagerService .deliveredAction())
182197 )
183198 return Result .success()
184199 }
185200
186- private fun handleFailed (context : Context , messageID : String ) {
201+ private fun handleFailed (context : Context , messageID : String , reason : String ) {
187202 Timber .d(" sending [FAILED] event for message with ID [${messageID} ]" )
188203
189204 val constraints = Constraints .Builder ()
@@ -192,7 +207,7 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
192207
193208 val inputData: Data = workDataOf(
194209 Constants .KEY_MESSAGE_ID to messageID,
195- Constants .KEY_MESSAGE_REASON to " MOBILE_APP_INACTIVE " ,
210+ Constants .KEY_MESSAGE_REASON to reason ,
196211 Constants .KEY_MESSAGE_TIMESTAMP to Settings .currentTimestamp()
197212 )
198213
@@ -222,10 +237,10 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
222237 return null
223238 }
224239
225- private fun sendMessage (message : Message , sentIntent : PendingIntent , deliveredIntent : PendingIntent ) {
240+ private fun sendMessage (message : Message , content : String , sentIntent : PendingIntent , deliveredIntent : PendingIntent ) {
226241 Timber .d(" sending SMS for message with ID [${message.id} ]" )
227242 try {
228- SmsManagerService ().sendTextMessage(this .applicationContext,message.contact, message. content, message.sim, sentIntent, deliveredIntent)
243+ SmsManagerService ().sendTextMessage(this .applicationContext,message.contact, content, message.sim, sentIntent, deliveredIntent)
229244 } catch (e: Exception ) {
230245 Timber .e(e)
231246 Timber .d(" could not send SMS for message with ID [${message.id} ]" )
@@ -236,15 +251,22 @@ class MyFirebaseMessagingService : FirebaseMessagingService() {
236251
237252 private fun getMessageParts (context : Context , message : Message ): ArrayList <String > {
238253 Timber .d(" getting parts for message with ID [${message.id} ]" )
254+
255+ var messageBody = message.content
256+ val encryptionKey = Settings .getEncryptionKey(context)
257+ if (message.encrypted && ! encryptionKey.isNullOrEmpty()) {
258+ messageBody = Encrypter .decrypt(encryptionKey, messageBody)
259+ }
260+
239261 return try {
240- val parts = SmsManagerService ().messageParts(context, message.content )
262+ val parts = SmsManagerService ().messageParts(context, messageBody )
241263 Timber .d(" message with ID [${message.id} ] has [${parts.size} ] parts" )
242264 parts
243265 } catch (e: Exception ) {
244266 Timber .e(e)
245267 Timber .d(" could not get parts message with ID [${message.id} ] returning [1] part with entire content" )
246268 val list = ArrayList <String >()
247- list.add(message.content )
269+ list.add(messageBody )
248270 list
249271 }
250272 }
0 commit comments