Skip to content

Commit

Permalink
[receiver] try to determine SIM slot number
Browse files Browse the repository at this point in the history
  • Loading branch information
capcom6 committed Oct 22, 2024
1 parent 505708a commit 12758d9
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package me.capcom.smsgateway.helpers

import android.Manifest
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.telephony.SubscriptionManager
import androidx.core.app.ActivityCompat

object SubscriptionsHelper {
@Suppress("DEPRECATION")
fun getSubscriptionsManager(context: Context): SubscriptionManager? = when {
Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP_MR1 -> null
Build.VERSION.SDK_INT < 31 -> SubscriptionManager.from(context)
else -> context.getSystemService(SubscriptionManager::class.java)
}

fun getSimSlotIndex(context: Context, subscriptionId: Int): Int? {
if (ActivityCompat.checkSelfPermission(
context,
Manifest.permission.READ_PHONE_STATE
) != PackageManager.PERMISSION_GRANTED
) {
return null
}

val subscriptionManager = getSubscriptionsManager(context) ?: return null
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP_MR1) {
subscriptionManager.activeSubscriptionInfoList.find {
it.subscriptionId == subscriptionId
}?.simSlotIndex
} else {
null
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.capcom.smsgateway.modules.messages

import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.app.PendingIntent
import android.content.Context
Expand All @@ -9,7 +10,6 @@ import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.telephony.SmsManager
import android.telephony.SubscriptionManager
import android.telephony.TelephonyManager
import android.util.Log
import androidx.core.app.ActivityCompat
Expand All @@ -20,6 +20,7 @@ import me.capcom.smsgateway.data.entities.MessageRecipient
import me.capcom.smsgateway.data.entities.MessageWithRecipients
import me.capcom.smsgateway.domain.ProcessingState
import me.capcom.smsgateway.helpers.PhoneHelper
import me.capcom.smsgateway.helpers.SubscriptionsHelper
import me.capcom.smsgateway.modules.encryption.EncryptionService
import me.capcom.smsgateway.modules.events.EventBus
import me.capcom.smsgateway.modules.health.domain.CheckResult
Expand Down Expand Up @@ -324,6 +325,7 @@ class MessagesService(
}
}

@SuppressLint("NewApi")
private fun getSmsManager(simNumber: Int?): SmsManager {
return if (simNumber != null) {
if (ActivityCompat.checkSelfPermission(
Expand All @@ -334,16 +336,8 @@ class MessagesService(
throw UnsupportedOperationException("SIM selection requires READ_PHONE_STATE permission")
}

@Suppress("DEPRECATION")
val subscriptionManager: SubscriptionManager = when {
Build.VERSION.SDK_INT < 22 -> throw UnsupportedOperationException("SIM selection available from API 22")
Build.VERSION.SDK_INT < 31 -> SubscriptionManager.from(context)
else -> context.getSystemService(SubscriptionManager::class.java)
}

if (subscriptionManager.activeSubscriptionInfoCount <= simNumber) {
throw UnsupportedOperationException("SIM ${simNumber + 1} not found")
}
val subscriptionManager = SubscriptionsHelper.getSubscriptionsManager(context)
?: throw UnsupportedOperationException("SIM selection available from API 22")

subscriptionManager.activeSubscriptionInfoList.find {
it.simSlotIndex == simNumber
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.provider.Telephony.Sms.Intents
import me.capcom.smsgateway.helpers.SubscriptionsHelper
import me.capcom.smsgateway.modules.receiver.events.MessageReceivedEvent
import me.capcom.smsgateway.modules.webhooks.WebHooksService
import me.capcom.smsgateway.modules.webhooks.domain.WebHookEvent
Expand All @@ -25,11 +26,29 @@ class MessagesReceiver : BroadcastReceiver(), KoinComponent {
val event = MessageReceivedEvent(
message = text,
phoneNumber = firstMessage.displayOriginatingAddress,
simNumber = extractSimNumber(context, intent),
receivedAt = Date(firstMessage.timestampMillis),
)

webHooksService.emit(WebHookEvent.SmsReceived, event)
}

private fun extractSimNumber(context: Context, intent: Intent): Int? {
if (intent.extras?.containsKey("android.telephony.extra.SLOT_INDEX") == true) {
return intent.extras?.getInt("android.telephony.extra.SLOT_INDEX")?.let { it + 1 }
}

val subscriptionId = when {
intent.extras?.containsKey("android.telephony.extra.SUBSCRIPTION_INDEX") == true -> intent.extras?.getInt(
"android.telephony.extra.SUBSCRIPTION_INDEX"
)

intent.extras?.containsKey("subscription") == true -> intent.extras?.getInt("subscription")
else -> null
} ?: return null

return SubscriptionsHelper.getSimSlotIndex(context, subscriptionId)?.let { it + 1 }
}

private val webHooksService: WebHooksService by inject()
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class MessageReceivedEvent(
val message: String,
val phoneNumber: String,
val receivedAt: Date,
val simNumber: Int?,
) : AppEvent(
name = NAME
) {
Expand Down

0 comments on commit 12758d9

Please sign in to comment.