add contact blocking helpers
This commit is contained in:
parent
d4676ceadf
commit
40fa4f69ad
2 changed files with 38 additions and 1 deletions
|
@ -1,12 +1,15 @@
|
|||
package com.simplemobiletools.commons.extensions
|
||||
|
||||
import android.annotation.TargetApi
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.database.Cursor
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import android.os.Handler
|
||||
import android.os.Looper
|
||||
import android.provider.ContactsContract
|
||||
import android.telephony.PhoneNumberUtils
|
||||
import com.simplemobiletools.commons.R
|
||||
import com.simplemobiletools.commons.activities.BaseSimpleActivity
|
||||
import com.simplemobiletools.commons.databases.ContactsDatabase
|
||||
|
@ -356,3 +359,30 @@ fun BaseSimpleActivity.tryInitiateCall(contact: Contact, onStartCallIntent: (pho
|
|||
initiateCall(contact, onStartCallIntent)
|
||||
}
|
||||
}
|
||||
|
||||
fun Context.isContactBlocked(contact: Contact, callback: (Boolean) -> Unit) {
|
||||
val phoneNumbers = contact.phoneNumbers.map { PhoneNumberUtils.stripSeparators(it.value) }
|
||||
getBlockedNumbersWithContact { blockedNumbersWithContact ->
|
||||
val blockedNumbers = blockedNumbersWithContact.map { it.number }
|
||||
val allNumbersBlocked = phoneNumbers.all { it in blockedNumbers }
|
||||
callback(allNumbersBlocked)
|
||||
}
|
||||
}
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.N)
|
||||
fun Context.blockContact(contact: Contact) {
|
||||
ensureBackgroundThread {
|
||||
contact.phoneNumbers.forEach {
|
||||
addBlockedNumber(PhoneNumberUtils.stripSeparators(it.value))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@TargetApi(Build.VERSION_CODES.N)
|
||||
fun Context.unblockContact(contact: Contact) {
|
||||
ensureBackgroundThread {
|
||||
contact.phoneNumbers.forEach {
|
||||
deleteBlockedNumber(PhoneNumberUtils.stripSeparators(it.value))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -118,7 +118,7 @@
|
|||
<item quantity="other">%d rows</item>
|
||||
</plurals>
|
||||
|
||||
<!-- Blocked numbers -->
|
||||
<!-- Blocked numbers & contacts -->
|
||||
<string name="manage_blocked_numbers">Manage blocked numbers</string>
|
||||
<string name="not_blocking_anyone">You are not blocking anyone.</string>
|
||||
<string name="add_a_blocked_number">Add a blocked number</string>
|
||||
|
@ -136,6 +136,13 @@
|
|||
<string name="block_hidden_messages">Block messages from hidden numbers</string>
|
||||
<string name="add_blocked_number_helper_text">Enter a number or a pattern (e.g. *12345*, +1*8888) to block all calls and messages from numbers matching the pattern.</string>
|
||||
<string name="must_make_default_caller_id_app">Can\'t block unknown numbers without caller ID permission.</string>
|
||||
<!-- TODO: add missing translations-->
|
||||
<string name="block_contact">Block contact</string>
|
||||
<string name="block_contact_success">Contact blocked</string>
|
||||
<string name="block_contact_fail">Contact could not be blocked</string>
|
||||
<string name="unblock_contact">Unblock contact</string>
|
||||
<string name="unblock_contact_success">Contact unblocked</string>
|
||||
<string name="unblock_contact_fail">Contact could not be unblocked</string>
|
||||
|
||||
<!-- Favorites -->
|
||||
<string name="favorites">Favorites</string>
|
||||
|
|
Loading…
Reference in a new issue