Skip to content

Commit

Permalink
Merge pull request #238 from TinfoilSubmarine/sim_only_if_applicable
Browse files Browse the repository at this point in the history
only show SIM if applicable
  • Loading branch information
x13a authored Apr 19, 2024
2 parents 7bba720 + fd20fc7 commit 75ba11d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
12 changes: 10 additions & 2 deletions app/src/main/java/me/lucky/silence/Utils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,13 @@ class Utils {
.getSystemService(RoleManager::class.java)
?.isRoleHeld(RoleManager.ROLE_CALL_SCREENING) ?: false

fun getModemCount(ctx: Context): Int {
fun getModemCount(ctx: Context, modem: Modem): Int {
val telephonyManager = ctx.getSystemService(TelephonyManager::class.java)
return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
telephonyManager?.supportedModemCount
when (modem) {
Modem.ACTIVE -> telephonyManager?.activeModemCount
Modem.SUPPORTED -> telephonyManager?.supportedModemCount
}
} else {
@Suppress("deprecation")
telephonyManager?.phoneCount
Expand All @@ -78,4 +81,9 @@ class Utils {
false -> key.and(value.inv())
}
}
}

enum class Modem {
ACTIVE,
SUPPORTED,
}
4 changes: 3 additions & 1 deletion app/src/main/java/me/lucky/silence/ctl/BroadcastReceiver.kt
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.os.Build
import me.lucky.silence.Modem

import me.lucky.silence.Preferences
import me.lucky.silence.Sim
Expand Down Expand Up @@ -46,7 +47,8 @@ class BroadcastReceiver : BroadcastReceiver() {
}

private fun setSimState(ctx: Context, flag: Sim, state: Boolean) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && Utils.getModemCount(ctx) >= 2)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S &&
Utils.getModemCount(ctx, Modem.SUPPORTED) >= 2)
Preferences(ctx).apply {
sim = when (state) {
true -> sim.or(flag.value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import androidx.core.text.isDigitsOnly
import com.google.i18n.phonenumbers.NumberParseException
import com.google.i18n.phonenumbers.PhoneNumberUtil
import com.google.i18n.phonenumbers.Phonenumber
import me.lucky.silence.Modem
import me.lucky.silence.NotificationManager
import me.lucky.silence.Preferences
import me.lucky.silence.ResponseOption
Expand All @@ -36,7 +37,7 @@ class CallScreeningService : CallScreeningService() {
prefs = Preferences(this)
callScreeningHelper = CallScreeningHelper(this)
phoneNumberUtil = PhoneNumberUtil.getInstance()
isMultiSim = Utils.getModemCount(this) >= 2
isMultiSim = Utils.getModemCount(this, Modem.SUPPORTED) >= 2
telephonyManager = getSystemService(TelephonyManager::class.java)
subscriptionManager = getSystemService(SubscriptionManager::class.java)
}
Expand Down Expand Up @@ -92,21 +93,19 @@ class CallScreeningService : CallScreeningService() {
val tel = callDetails.handle?.schemeSpecificPart
val isNotify = responseOptions.and(ResponseOption.SkipNotification.value) == 0
&& tel != null
respondToCall(
callDetails,
CallResponse.Builder()
.setDisallowCall(disallowCall)
.setRejectCall(responseOptions.and(ResponseOption.RejectCall.value) != 0
&& disallowCall)
.setSilenceCall(responseOptions.and(ResponseOption.SilenceCall.value) != 0)
.setSkipCallLog(responseOptions.and(ResponseOption.SkipCallLog.value) != 0
&& disallowCall)
.setSkipNotification(!isNotify && disallowCall)
.build(),
)
val response = CallResponse.Builder()
.setDisallowCall(disallowCall)
.setRejectCall(responseOptions.and(ResponseOption.RejectCall.value) != 0
&& disallowCall)
.setSilenceCall(responseOptions.and(ResponseOption.SilenceCall.value) != 0)
.setSkipCallLog(responseOptions.and(ResponseOption.SkipCallLog.value) != 0
&& disallowCall)
.setSkipNotification(!isNotify && disallowCall)
.build()
if (isNotify && disallowCall) {
var sim: Sim? = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && isMultiSim) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S &&
Utils.getModemCount(this, Modem.ACTIVE) >= 2) {
sim = when {
checkSimSlot(0) -> Sim.SIM_1
checkSimSlot(1) -> Sim.SIM_2
Expand All @@ -115,6 +114,7 @@ class CallScreeningService : CallScreeningService() {
}
notificationManager.notifyBlockedCall(tel ?: return, sim)
}
respondToCall(callDetails, response)
}

private fun isStirVerified(callDetails: Call.Details) =
Expand Down
17 changes: 12 additions & 5 deletions app/src/main/java/me/lucky/silence/ui/MainScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.compose.rememberNavController
import me.lucky.silence.Contact
import me.lucky.silence.Message
import me.lucky.silence.Modem
import me.lucky.silence.Preferences
import me.lucky.silence.R
import me.lucky.silence.Utils
Expand Down Expand Up @@ -177,11 +178,17 @@ fun MainScreen(
getPreference = { prefs.isBlockEnabled },
setPreference = { prefs.isBlockEnabled = it },
),
Module(
name = R.string.sim,
description = R.string.sim_description,
navigation = onNavigateToSim,
),
*(if (Utils.getModemCount(ctx, Modem.SUPPORTED) >= 2) {
arrayOf(
Module(
name = R.string.sim,
description = R.string.sim_description,
navigation = onNavigateToSim,
),
)
} else {
emptyArray()
}),
Module(
name = R.string.extra,
description = R.string.extra_description,
Expand Down

0 comments on commit 75ba11d

Please sign in to comment.