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
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package com.httpsms

import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import androidx.work.*
import com.google.firebase.messaging.FirebaseMessagingService
import com.google.firebase.messaging.RemoteMessage
import com.httpsms.SentReceiver.FailedMessageWorker
import timber.log.Timber

import com.google.android.mms.pdu_alt.CharacterSets
import com.google.android.mms.pdu_alt.EncodedStringValue
import com.google.android.mms.pdu_alt.PduBody
import com.google.android.mms.pdu_alt.PduComposer
import com.google.android.mms.pdu_alt.PduPart
import com.google.android.mms.pdu_alt.SendReq
import okhttp3.MediaType
import java.io.File

class MyFirebaseMessagingService : FirebaseMessagingService() {
    // [START receive_message]
    override fun onMessageReceived(remoteMessage: RemoteMessage) {
        initTimber()
        Timber.d(MyFirebaseMessagingService::onMessageReceived.name)

        if (remoteMessage.data.containsKey(Constants.KEY_HEARTBEAT_ID)) {
            Timber.w("received heartbeat message with ID [${remoteMessage.data[Constants.KEY_HEARTBEAT_ID]}] and priority [${remoteMessage.priority}] and original priority [${remoteMessage.originalPriority}]")
            sendHeartbeat()
            return
        }

        val messageID = remoteMessage.data[Constants.KEY_MESSAGE_ID]
        if (messageID == null)  {
            Timber.e("cannot get message id from notification data with key [${Constants.KEY_MESSAGE_ID}]")
            return
        }

        scheduleJob(messageID)
    }
    // [END receive_message]

    // [START on_new_token]
    /**

     * Called if the FCM registration token is updated. This may occur if the security of

     * the previous token had been compromised. Note that this is called when the

     * FCM registration token is initially generated so this is where you would retrieve the token.

     */
    override fun onNewToken(token: String) {
        initTimber()
        Timber.d("Refreshed token: $token")

        // If you want to send messages to this application instance or
        // manage this apps subscriptions on the server side, send the
        // FCM registration token to your app server.
        sendRegistrationToServer(token)
    }
    // [END on_new_token]

    private fun sendHeartbeat() {
        Timber.d("sending heartbeat from FCM notification")
        if (!Settings.isLoggedIn(applicationContext)) {
            Timber.w("user is not logged in, not sending heartbeat")
            return
        }
        Thread {
            try {
                val phoneNumbers = mutableListOf<String>()
                phoneNumbers.add(Settings.getSIM1PhoneNumber(applicationContext))
                if (Settings.getActiveStatus(applicationContext, Constants.SIM2)) {
                    phoneNumbers.add(Settings.getSIM2PhoneNumber(applicationContext))
                }

                HttpSmsApiService.create(applicationContext).storeHeartbeat(phoneNumbers.toTypedArray(), Settings.isCharging(applicationContext))
                Settings.setHeartbeatTimestampAsync(applicationContext, System.currentTimeMillis())
            } catch (exception: Exception) {
                Timber.e(exception)
            }
            Timber.d("finished sending pulse")
        }.start()
    }

    private fun scheduleJob(messageID: String) {
        // [START dispatch_job]
        val constraints = Constraints.Builder()
            .setRequiredNetworkType(NetworkType.CONNECTED)
            .build()

        val inputData: Data = workDataOf(Constants.KEY_MESSAGE_ID to messageID)
        val work = OneTimeWorkRequest
            .Builder(SendSmsWorker::class.java)
            .setConstraints(constraints)
            .setInputData(inputData)
            .addTag(messageID)
            .build()

        WorkManager
            .getInstance(this)
            .enqueue(work)

        Timber.d("work enqueued with ID [${work.id}] for messageID [${messageID}]")
        // [END dispatch_job]
    }
    private fun sendRegistrationToServer(token: String) {
        Timber.d("sendRegistrationTokenToServer($token)")
        Settings.setFcmTokenAsync(this, token)

        if (Settings.isLoggedIn(this)) {
            Timber.d("updating SIM1 phone with new fcm token")
            val response = HttpSmsApiService.create(this).updateFcmToken(Settings.getSIM1PhoneNumber(this), Constants.SIM1, token)
            if (response.first != null) {
                Settings.setUserID(this, response.first!!.userID)
            }
        }

        if(Settings.isDualSIM(this)) {
            Timber.d("updating SIM2 phone with new fcm token")
            HttpSmsApiService.create(this).updateFcmToken(Settings.getSIM2PhoneNumber(this), Constants.SIM2, token)
        }
    }

    private fun initTimber() {
        if (Timber.treeCount > 1) {
            Timber.d("timber is already initialized with count [${Timber.treeCount}]")
            return
        }

        if(Settings.isDebugLogEnabled(this)) {
            Timber.plant(Timber.DebugTree())
            Timber.plant(LogzTree(this.applicationContext))
        }
    }

    internal class SendSmsWorker(appContext: Context, workerParams: WorkerParameters) : Worker(appContext, workerParams) {
        override fun doWork(): Result {
            if (!Settings.isLoggedIn(applicationContext)) {
                Timber.w("user is not logged in, stopping processing")
                return Result.failure()
            }

            val messageID = this.inputData.getString(Constants.KEY_MESSAGE_ID)
            if (messageID == null) {
                Timber.e("cannot get outstanding message for work [${this.id}]")
                return Result.failure()
            }

            val message = getMessage(applicationContext, messageID) ?: return Result.failure()

            if (message.contact.isBlank()) {
                Timber.w("message contact is blank, stopping processing")
                handleFailed(applicationContext, messageID, "The contact phone number is empty.")
                return Result.failure()
            }

            if (!Settings.getActiveStatus(applicationContext, message.sim)) {
                Timber.w("[${message.sim}] SIM is not active, stopping processing")
                handleFailed(applicationContext, messageID, "Outgoing messages have been disabled on the mobile app")
                return Result.failure()
            }

            if (message.encrypted && Settings.getEncryptionKey(applicationContext).isNullOrEmpty()) {
                Timber.w("[${message.sim}] message is encrypted but the encryption key is empty")
                handleFailed(applicationContext, messageID, "Outgoing message is encrypted but mobile app has no encryption key")
                return Result.failure()
            }
            if (message.encrypted) {
                try {
                    Encrypter.decrypt(Settings.getEncryptionKey(applicationContext)!!, message.content)
                } catch (exception: Exception) {
                    Timber.e(exception)
                    handleFailed(applicationContext, messageID, "Cannot decrypt the outgoing message. Check your encryption key on the Android app.")
                    return Result.failure()
                }
            }

            Receiver.register(applicationContext)

            if (message.attachments != null && message.attachments.isNotEmpty()) {
                return handleMmsMessage(message)
            }

            val parts = getMessageParts(applicationContext, message)
            if (parts.size == 1) {
                return handleSingleMessage(message, parts.first())
            }
            return handleMultipartMessage(message, parts)
        }

        fun extractFileName(url: String, prefix: String, mimeType: String? = null): String {
            val fileName = url.substringAfterLast("/")
                .substringBefore("?")
                .takeIf { it.isNotBlank() && it.contains(".") }
                ?: run {
                    val extension = mimeType?.let { mime ->
                        val ext = mime.substringAfterLast("/")
                        if (ext.isNotBlank()) ".$ext" else ".bin"
                    } ?: ""
                    "attachment$extension"
                }

            return "${prefix}_$fileName"
        }

        private fun handleMmsMessage(message: Message): Result {
            Timber.d("Processing MMS for message ID [${message.id}]")
            val apiService = HttpSmsApiService.create(applicationContext)

            val downloadedFiles = mutableListOf<Pair<File, MediaType>>()

            try {
                for ((index, attachment) in message.attachments!!.withIndex()) {
                    val file = apiService.downloadAttachment(applicationContext, attachment, message.id, index)
                    if (file.first == null || file.second == null) {
                        handleFailed(applicationContext, message.id, "Failed to download attachment or file size exceeded 1.5MB.")
                        return Result.failure()
                    }
                    downloadedFiles.add(Pair(file.first!!, file.second!!))
                }

                val sendReq = SendReq()

                val encodedContact = EncodedStringValue(message.contact)
                sendReq.to = arrayOf(encodedContact)

                val pduBody = PduBody()

                if (message.content.isNotEmpty()) {
                    val textPart = PduPart()
                    textPart.setCharset(CharacterSets.UTF_8)
                    textPart.contentType = "text/plain".toByteArray()
                    textPart.name = "text".toByteArray()
                    textPart.contentId = "text".toByteArray()
                    textPart.contentLocation = "text".toByteArray()

                    var messageBody = message.content
                    val encryptionKey = Settings.getEncryptionKey(applicationContext)
                    if (message.encrypted && !encryptionKey.isNullOrEmpty()) {
                        messageBody = Encrypter.decrypt(encryptionKey, messageBody)
                    }
                    textPart.data = messageBody.toByteArray(Charsets.UTF_8)

                    pduBody.addPart(textPart)
                }

                for ((index, file) in downloadedFiles.withIndex()) {
                    val fileBytes = file.first.readBytes()

                    val mediaPart = PduPart()
                    mediaPart.contentType = file.second.toString().toByteArray()


                    val fileName = extractFileName(message.attachments[index], index.toString(), file.second.toString())
                    mediaPart.name = fileName.toByteArray()
                    mediaPart.contentId = fileName.toByteArray()
                    mediaPart.contentLocation = fileName.toByteArray()
                    mediaPart.data = fileBytes

                    Timber.d("Adding MMS attachment with name [$fileName] and size [${fileBytes.size}] and type [${file.second}]")

                    pduBody.addPart(mediaPart)
                }

                sendReq.body = pduBody

                val pduComposer = PduComposer(applicationContext, sendReq)
                val pduBytes = pduComposer.make()

                if (pduBytes == null) {
                    Timber.e("PduComposer failed to generate PDU byte array")
                    handleFailed(applicationContext, message.id, "Failed to compose MMS PDU.")
                    return Result.failure()
                }

                val mmsDir = java.io.File(applicationContext.cacheDir, "mms_attachments")
                if (!mmsDir.exists()) {
                    mmsDir.mkdirs()
                }

                val pduFile = java.io.File(mmsDir, "pdu_${message.id}.dat")
                java.io.FileOutputStream(pduFile).use { it.write(pduBytes) }

                val pduUri = androidx.core.content.FileProvider.getUriForFile(
                    applicationContext,
                    "${BuildConfig.APPLICATION_ID}.fileprovider",
                    pduFile
                )

                val sentIntent = createPendingIntent(message.id, SmsManagerService.sentAction())
                SmsManagerService().sendMultimediaMessage(applicationContext, pduUri, message.sim, sentIntent)

                Timber.d("Successfully dispatched MMS for message ID [${message.id}]")
                return Result.success()

            } catch (e: Exception) {
                Timber.e(e, "Failed to send MMS for message ID [${message.id}]")
                handleFailed(applicationContext, message.id, e.message ?: "Internal error while building or sending MMS.")
                return Result.failure()
            } finally {
                // Clean up any downloaded temporary files
                downloadedFiles.forEach { file ->
                    if (file.first.exists()) {
                        file.first.delete()
                    }
                }

                // Also clean up the MMS PDU file to avoid cache buildup in cases where
                // sendMultimediaMessage fails before the sent broadcast is delivered.
                try {
                    // The PDU file is stored under the "mms_attachments" cache subdirectory;
                    // delete it from the same location to ensure cleanup is effective.
                    val pduDir = File(applicationContext.cacheDir, "mms_attachments")
                    val pduFile = File(pduDir, "pdu_${message.id}.dat")
                    if (pduFile.exists()) {
                        val deleted = pduFile.delete()
                        if (!deleted) {
                            Timber.w("Failed to delete MMS PDU file for message ID [${message.id}] at [${pduFile.absolutePath}]")
                        }
                    }
                } catch (cleanupException: Exception) {
                    // Best-effort cleanup; log but do not change the original result.
                    Timber.w(cleanupException, "Error while cleaning up MMS PDU file for message ID [${message.id}]")
                }
            }
        }

        private fun handleMultipartMessage(message:Message, parts: ArrayList<String>): Result {
            Timber.d("sending multipart SMS for message with ID [${message.id}]")
            return try {
                val sentIntents = ArrayList<PendingIntent>()
                val deliveredIntents = ArrayList<PendingIntent>()

                for (i in 0 until parts.size) {
                    var id = "${message.id}.$i"

                    // Listen for 'delivered' and 'sent' intents only on the last part in the
                    // multipart SMS message
                    if (i == parts.size -1) {
                        id = message.id
                    }

                    sentIntents.add(createPendingIntent(id, SmsManagerService.sentAction()))
                    deliveredIntents.add(createPendingIntent(id, SmsManagerService.deliveredAction()))
                }
                SmsManagerService().sendMultipartMessage(this.applicationContext,message.contact, parts, message.sim, sentIntents, deliveredIntents)
                Timber.d("sent SMS for message with ID [${message.id}] in [${parts.size}] parts")
                Result.success()
            } catch (e: Exception) {
                Timber.e(e)
                Timber.d("could not send SMS for message with ID [${message.id}] in [${parts.size}] parts")
                handleFailed(this.applicationContext, message.id, e.message ?: e.javaClass.simpleName)
                Result.failure()
            }
        }

        private fun handleSingleMessage(message:Message, content: String): Result {
            sendMessage(
                message,
                content,
                createPendingIntent(message.id, SmsManagerService.sentAction()),
                createPendingIntent(message.id, SmsManagerService.deliveredAction())
            )
            return Result.success()
        }

        private fun handleFailed(context: Context, messageID: String, reason: String) {
            Timber.d("sending [FAILED] event for message with ID [${messageID}]")

            val constraints = Constraints.Builder()
                .setRequiredNetworkType(NetworkType.CONNECTED)
                .build()

            val inputData: Data = workDataOf(
                Constants.KEY_MESSAGE_ID to messageID,
                Constants.KEY_MESSAGE_REASON to reason,
                Constants.KEY_MESSAGE_TIMESTAMP to Settings.currentTimestamp()
            )

            val work = OneTimeWorkRequest
                .Builder(FailedMessageWorker::class.java)
                .setConstraints(constraints)
                .setInputData(inputData)
                .build()

            WorkManager
                .getInstance(context)
                .enqueue(work)

            Timber.d("work enqueued with ID [${work.id}] for [FAILED] message with ID [${messageID}]")
        }

        private fun getMessage(context: Context, messageID: String): Message? {
            Timber.d("fetching message with ID [${messageID}]")
            val message =  HttpSmsApiService.create(context).getOutstandingMessage(messageID)

            if (message != null) {
                Timber.d("fetched message with ID [${message.id}]")
                return message
            }

            Timber.e("cannot get message from API with ID [${messageID}]")
            return null
        }

        private fun sendMessage(message: Message, content: String, sentIntent: PendingIntent, deliveredIntent: PendingIntent) {
            Timber.d("sending SMS for message with ID [${message.id}]")
            try {
                SmsManagerService().sendTextMessage(this.applicationContext,message.contact, content, message.sim, sentIntent, deliveredIntent)
            } catch (e: Exception) {
                Timber.e(e)
                Timber.d("could not send SMS for message with ID [${message.id}]")
                handleFailed(this.applicationContext, message.id, e.message ?: e.javaClass.simpleName)
                return
            }
            Timber.d("sent SMS for message with ID [${message.id}]")
        }

        private fun getMessageParts(context: Context, message: Message): ArrayList<String> {
            Timber.d("getting parts for message with ID [${message.id}]")

            var messageBody  = message.content
            val encryptionKey = Settings.getEncryptionKey(context)
            if (message.encrypted && !encryptionKey.isNullOrEmpty()) {
                messageBody = Encrypter.decrypt(encryptionKey, messageBody)
            }

            return try {
                val parts = SmsManagerService().messageParts(context, messageBody)
                Timber.d("message with ID [${message.id}] has [${parts.size}] parts")
                parts
            } catch (e: Exception) {
                Timber.e(e)
                Timber.d("could not get parts message with ID [${message.id}] returning [1] part with entire content")
                val list = ArrayList<String>()
                list.add(messageBody)
                list
            }
        }

        private fun createPendingIntent(id: String, action: String): PendingIntent {
            val intent = Intent(action)
            intent.putExtra(Constants.KEY_MESSAGE_ID, id)

            return PendingIntent.getBroadcast(
                this.applicationContext,
                id.hashCode(),
                intent,
                PendingIntent.FLAG_IMMUTABLE
            )
        }
    }
}