forked from NdoleStudio/httpsms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReceivedReceiver.kt
More file actions
53 lines (44 loc) · 1.58 KB
/
ReceivedReceiver.kt
File metadata and controls
53 lines (44 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package com.httpsms
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.provider.Telephony
import timber.log.Timber
import java.time.ZoneOffset
import java.time.ZonedDateTime
class ReceivedReceiver: BroadcastReceiver()
{
override fun onReceive(context: Context,intent: Intent) {
if (intent.action != Telephony.Sms.Intents.SMS_RECEIVED_ACTION) {
Timber.e("received invalid intent with action [${intent.action}]")
return
}
var smsSender = ""
var smsBody = ""
for (smsMessage in Telephony.Sms.Intents.getMessagesFromIntent(intent)) {
smsSender = smsMessage.displayOriginatingAddress
smsBody += smsMessage.messageBody
}
handleMessageReceived(
context,
smsSender,
Settings.getOwnerOrDefault(context),
smsBody
)
}
private fun handleMessageReceived(context: Context, from: String, to : String, content: String) {
val timestamp = ZonedDateTime.now(ZoneOffset.UTC)
if (!Settings.isLoggedIn(context)) {
Timber.w("user is not logged in")
return
}
if (!Settings.getActiveStatus(context)) {
Timber.w("user is not active")
return
}
Thread {
Timber.i("forwarding received message from [${from}]")
HttpSmsApiService(Settings.getApiKeyOrDefault(context)).receive(from, to, content, timestamp)
}.start()
}
}