diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index fc11ce08..c965a109 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -203,6 +203,11 @@ android:label="@string/frequently_asked_questions" android:parentActivityName="com.simplemobiletools.commons.activities.AboutActivity"/> + + addBlockedNumber() + else -> return super.onOptionsItemSelected(item) + } + return true + } + + override fun refreshItems() { + updateBlockedNumbers() + } + + private fun addBlockedNumber() { + + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/SettingsActivity.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/SettingsActivity.kt index 145b79b6..db351714 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/SettingsActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/activities/SettingsActivity.kt @@ -1,6 +1,7 @@ package com.simplemobiletools.contacts.pro.activities import android.annotation.TargetApi +import android.content.Intent import android.os.Build import android.os.Bundle import com.simplemobiletools.commons.dialogs.RadioGroupDialog @@ -12,7 +13,6 @@ import com.simplemobiletools.contacts.pro.R import com.simplemobiletools.contacts.pro.dialogs.ManageVisibleFieldsDialog import com.simplemobiletools.contacts.pro.dialogs.ManageVisibleTabsDialog import com.simplemobiletools.contacts.pro.extensions.config -import com.simplemobiletools.contacts.pro.extensions.telecomManager import com.simplemobiletools.contacts.pro.helpers.ON_CLICK_CALL_CONTACT import com.simplemobiletools.contacts.pro.helpers.ON_CLICK_EDIT_CONTACT import com.simplemobiletools.contacts.pro.helpers.ON_CLICK_VIEW_CONTACT @@ -64,11 +64,12 @@ class SettingsActivity : SimpleActivity() { } } + // support for device-wise blocking came on Android 7, rely only on that @TargetApi(Build.VERSION_CODES.N) private fun setupManageBlockedNumbers() { settings_manage_blocked_numbers_holder.beVisibleIf(isNougatPlus()) settings_manage_blocked_numbers_holder.setOnClickListener { - startActivity(telecomManager.createManageBlockedNumbersIntent()) + startActivity(Intent(this, ManageBlockedNumbersActivity::class.java)) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Context.kt index 6f590ccc..5aec8054 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/extensions/Context.kt @@ -1,19 +1,21 @@ package com.simplemobiletools.contacts.pro.extensions +import android.annotation.TargetApi import android.content.ContentValues import android.content.Context import android.content.Intent import android.database.Cursor import android.net.Uri +import android.os.Build +import android.provider.BlockedNumberContract import android.provider.BlockedNumberContract.BlockedNumbers import android.provider.ContactsContract import android.telecom.TelecomManager import androidx.core.content.FileProvider -import com.simplemobiletools.commons.extensions.getIntValue -import com.simplemobiletools.commons.extensions.hasPermission -import com.simplemobiletools.commons.extensions.toast +import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.PERMISSION_READ_CONTACTS import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CONTACTS +import com.simplemobiletools.commons.helpers.isNougatPlus import com.simplemobiletools.contacts.pro.BuildConfig import com.simplemobiletools.contacts.pro.R import com.simplemobiletools.contacts.pro.activities.EditContactActivity @@ -22,6 +24,7 @@ import com.simplemobiletools.contacts.pro.databases.ContactsDatabase import com.simplemobiletools.contacts.pro.helpers.* import com.simplemobiletools.contacts.pro.interfaces.ContactsDao import com.simplemobiletools.contacts.pro.interfaces.GroupsDao +import com.simplemobiletools.contacts.pro.models.BlockedNumber import com.simplemobiletools.contacts.pro.models.Contact import com.simplemobiletools.contacts.pro.models.ContactSource import com.simplemobiletools.contacts.pro.models.Organization @@ -287,6 +290,39 @@ fun Context.getVisibleContactSources(): ArrayList { return sourceNames } +@TargetApi(Build.VERSION_CODES.N) +fun Context.getBlockedNumbers(): ArrayList { + val blockedNumbers = ArrayList() + if (!isNougatPlus()) { + return blockedNumbers + } + + val uri = BlockedNumberContract.BlockedNumbers.CONTENT_URI + val projection = arrayOf( + BlockedNumberContract.BlockedNumbers.COLUMN_ID, + BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER, + BlockedNumberContract.BlockedNumbers.COLUMN_E164_NUMBER + ) + + var cursor: Cursor? = null + try { + cursor = contentResolver.query(uri, projection, null, null, null) + if (cursor?.moveToFirst() == true) { + do { + val id = cursor.getLongValue(BlockedNumberContract.BlockedNumbers.COLUMN_ID) + val number = cursor.getStringValue(BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER) ?: "" + val normalizedNumber = cursor.getStringValue(BlockedNumberContract.BlockedNumbers.COLUMN_E164_NUMBER) ?: "" + val blockedNumber = BlockedNumber(id, number, normalizedNumber) + blockedNumbers.add(blockedNumber) + } while (cursor.moveToNext()) + } + } finally { + cursor?.close() + } + + return blockedNumbers +} + fun Context.addBlockedNumber(number: String) { ContentValues().apply { put(BlockedNumbers.COLUMN_ORIGINAL_NUMBER, number) diff --git a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt index 10abc5bc..8c701fbc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt +++ b/app/src/main/kotlin/com/simplemobiletools/contacts/pro/helpers/ContactsHelper.kt @@ -3,15 +3,12 @@ package com.simplemobiletools.contacts.pro.helpers import android.accounts.Account import android.accounts.AccountManager import android.annotation.SuppressLint -import android.annotation.TargetApi import android.content.* import android.database.Cursor import android.graphics.Bitmap import android.net.Uri -import android.os.Build import android.os.Handler import android.os.Looper -import android.provider.BlockedNumberContract import android.provider.CallLog import android.provider.ContactsContract import android.provider.ContactsContract.CommonDataKinds @@ -1550,7 +1547,7 @@ class ContactsHelper(val context: Context) { return@Thread } - val blockedNumbers = getBlockedNumbers() + val blockedNumbers = context.getBlockedNumbers() val uri = CallLog.Calls.CONTENT_URI val projection = arrayOf( CallLog.Calls._ID, @@ -1629,33 +1626,4 @@ class ContactsHelper(val context: Context) { } }.start() } - - @TargetApi(Build.VERSION_CODES.N) - private fun getBlockedNumbers(): ArrayList { - val blockedNumbers = ArrayList() - if (!isNougatPlus()) { - return blockedNumbers - } - - val uri = BlockedNumberContract.BlockedNumbers.CONTENT_URI - val projection = arrayOf(BlockedNumberContract.BlockedNumbers.COLUMN_ID, BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER, BlockedNumberContract.BlockedNumbers.COLUMN_E164_NUMBER) - - var cursor: Cursor? = null - try { - cursor = context.contentResolver.query(uri, projection, null, null, null) - if (cursor?.moveToFirst() == true) { - do { - val id = cursor.getLongValue(BlockedNumberContract.BlockedNumbers.COLUMN_ID) - val number = cursor.getStringValue(BlockedNumberContract.BlockedNumbers.COLUMN_ORIGINAL_NUMBER) ?: "" - val normalizedNumber = cursor.getStringValue(BlockedNumberContract.BlockedNumbers.COLUMN_E164_NUMBER) ?: "" - val blockedNumber = BlockedNumber(id, number, normalizedNumber) - blockedNumbers.add(blockedNumber) - } while (cursor.moveToNext()) - } - } finally { - cursor?.close() - } - - return blockedNumbers - } } diff --git a/app/src/main/res/layout/activity_manage_blocked_numbers.xml b/app/src/main/res/layout/activity_manage_blocked_numbers.xml new file mode 100644 index 00000000..64a4f9b3 --- /dev/null +++ b/app/src/main/res/layout/activity_manage_blocked_numbers.xml @@ -0,0 +1,42 @@ + + + + + + + + + + diff --git a/app/src/main/res/layout/item_blocked_number.xml b/app/src/main/res/layout/item_blocked_number.xml new file mode 100644 index 00000000..e78f8724 --- /dev/null +++ b/app/src/main/res/layout/item_blocked_number.xml @@ -0,0 +1,20 @@ + + + + + + diff --git a/app/src/main/res/menu/menu_add_blocked_number.xml b/app/src/main/res/menu/menu_add_blocked_number.xml new file mode 100644 index 00000000..fb290efd --- /dev/null +++ b/app/src/main/res/menu/menu_add_blocked_number.xml @@ -0,0 +1,9 @@ + + + +