76 lines
2.8 KiB
Kotlin
76 lines
2.8 KiB
Kotlin
package ru.officialdakari.fmd.receivers
|
|
|
|
import android.content.BroadcastReceiver
|
|
import android.content.Context
|
|
import android.content.Intent
|
|
import android.content.SharedPreferences
|
|
import android.net.Uri
|
|
import android.telephony.SmsManager
|
|
import android.telephony.SmsMessage
|
|
import android.util.Log
|
|
import ru.officialdakari.fmd.CommandHandler
|
|
import ru.officialdakari.fmd.LockActivity
|
|
import ru.officialdakari.fmd.RingActivity
|
|
|
|
|
|
class SmsReceiver: BroadcastReceiver() {
|
|
private val preferences: SharedPreferences? = null
|
|
|
|
var lockIntent: Intent? = null
|
|
var ringIntent: Intent? = null
|
|
|
|
fun initIntents(ctx: Context) {
|
|
ringIntent = Intent(ctx, RingActivity::class.java).apply {
|
|
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
|
|
addFlags(Intent.FLAG_FROM_BACKGROUND)
|
|
}
|
|
lockIntent = Intent(ctx, LockActivity::class.java).apply {
|
|
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
|
|
addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
|
|
addFlags(Intent.FLAG_FROM_BACKGROUND)
|
|
}
|
|
}
|
|
|
|
override fun onReceive(context: Context, intent: Intent) {
|
|
if (intent.getAction() == "android.provider.Telephony.SMS_RECEIVED") {
|
|
if (lockIntent == null || ringIntent == null) {
|
|
initIntents(context)
|
|
}
|
|
val bundle = intent.getExtras()
|
|
var msgs: Array<SmsMessage?>? = null
|
|
var msg_from: String?
|
|
if (bundle != null) {
|
|
try {
|
|
val pdus = bundle.get("pdus") as Array<Any?>?
|
|
msgs = kotlin.arrayOfNulls<SmsMessage>(pdus!!.size)
|
|
for (i in msgs.indices) {
|
|
msgs[i] = SmsMessage.createFromPdu(pdus[i] as ByteArray?)
|
|
msg_from = msgs[i]?.getOriginatingAddress()
|
|
val msgBody: String? = msgs[i]?.getMessageBody()
|
|
if (msg_from != null && msgBody != null) {
|
|
if (msgBody.startsWith("fma ")) onFmaCommand(context, msg_from, msgBody);
|
|
}
|
|
}
|
|
} catch (e: Exception) {
|
|
Log.d("Exception caught FMD",e.stackTraceToString());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private fun onFmaCommand(context: Context, msgFrom: String, msgBody: String) {
|
|
Log.w("FMD", "SMS from $msgFrom: $msgBody")
|
|
val args = msgBody.split(" ")
|
|
if (!args[0].contentEquals("fma")) return;
|
|
val handler = CommandHandler(context, lockIntent!!, ringIntent!!)
|
|
val smsManager = SmsManager.getDefault()
|
|
handler.handleCommand(msgBody, { repl ->
|
|
if (repl.length != 0) {
|
|
smsManager.sendTextMessage(msgFrom, null, repl, null, null)
|
|
}
|
|
})
|
|
}
|
|
} |