Skip to content

Commit

Permalink
fix: crash when trying to block numbers (closes #415)
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Oct 29, 2024
1 parent a5b090f commit 85664b6
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ import com.bnyro.contacts.presentation.screens.calllog.model.CallModel
import com.bnyro.contacts.presentation.screens.contacts.model.ContactsModel
import com.bnyro.contacts.presentation.screens.settings.model.ThemeModel
import com.bnyro.contacts.util.IntentHelper
import com.bnyro.contacts.util.PermissionHelper


@OptIn(ExperimentalMaterial3Api::class, ExperimentalFoundationApi::class)
Expand Down Expand Up @@ -305,7 +306,7 @@ fun CallLogOptionsSheet(
}
LaunchedEffect(Unit) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (!BlockedNumberContract.canCurrentUserBlockNumbers(context)) return@LaunchedEffect
if (!PermissionHelper.canBlockNumbers(context)) return@LaunchedEffect
isBlocked = BlockedNumberContract.isBlocked(context, log.phoneNumber)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ import com.bnyro.contacts.presentation.screens.sms.model.SmsModel
import com.bnyro.contacts.util.CalendarUtils
import com.bnyro.contacts.util.ExportHelper
import com.bnyro.contacts.util.IntentHelper
import com.bnyro.contacts.util.PermissionHelper
import com.bnyro.contacts.util.extension.toast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
Expand Down Expand Up @@ -247,7 +248,7 @@ fun SMSThreadOptionsSheet(
}
LaunchedEffect(Unit) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (!BlockedNumberContract.canCurrentUserBlockNumbers(context)) return@LaunchedEffect
if (!PermissionHelper.canBlockNumbers(context)) return@LaunchedEffect
isBlocked = BlockedNumberContract.isBlocked(context, thread.address)
}
}
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/com/bnyro/contacts/util/PermissionHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,13 @@ package com.bnyro.contacts.util
import android.app.Activity
import android.content.Context
import android.content.pm.PackageManager
import android.os.Build
import android.provider.BlockedNumberContract
import android.provider.Telephony
import android.telecom.TelecomManager
import androidx.annotation.RequiresApi
import androidx.core.app.ActivityCompat
import androidx.core.content.getSystemService

object PermissionHelper {
fun checkPermissions(activity: Activity, permissions: Array<String>): Boolean {
Expand All @@ -25,4 +31,12 @@ object PermissionHelper {
) == PackageManager.PERMISSION_GRANTED
}
}

@RequiresApi(Build.VERSION_CODES.N)
fun canBlockNumbers(context: Context): Boolean {
if (!BlockedNumberContract.canCurrentUserBlockNumbers(context)) return false

return Telephony.Sms.getDefaultSmsPackage(context) == context.packageName ||
context.getSystemService<TelecomManager>()?.defaultDialerPackage == context.packageName
}
}

0 comments on commit 85664b6

Please sign in to comment.