add an activity for managing blocked numbers

This commit is contained in:
tibbi 2018-11-30 15:13:03 +01:00
parent d00a7da67a
commit 4131aae576
8 changed files with 180 additions and 38 deletions

View File

@ -203,6 +203,11 @@
android:label="@string/frequently_asked_questions"
android:parentActivityName="com.simplemobiletools.commons.activities.AboutActivity"/>
<activity
android:name=".activities.ManageBlockedNumbersActivity"
android:label="@string/blocked_numbers"
android:parentActivityName=".activities.SettingsActivity"/>
<activity
android:name=".activities.DialpadActivity"
android:label="@string/dialpad"

View File

@ -0,0 +1,61 @@
package com.simplemobiletools.contacts.pro.activities
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import com.simplemobiletools.commons.extensions.beVisibleIf
import com.simplemobiletools.commons.extensions.getAdjustedPrimaryColor
import com.simplemobiletools.commons.extensions.underlineText
import com.simplemobiletools.commons.extensions.updateTextColors
import com.simplemobiletools.commons.interfaces.RefreshRecyclerViewListener
import com.simplemobiletools.contacts.pro.R
import com.simplemobiletools.contacts.pro.extensions.getBlockedNumbers
import kotlinx.android.synthetic.main.activity_manage_blocked_numbers.*
class ManageBlockedNumbersActivity : SimpleActivity(), RefreshRecyclerViewListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_manage_blocked_numbers)
updateBlockedNumbers()
updateTextColors(manage_blocked_numbers_wrapper)
manage_blocked_numbers_placeholder_2.apply {
underlineText()
setTextColor(getAdjustedPrimaryColor())
setOnClickListener {
addBlockedNumber()
}
}
}
private fun updateBlockedNumbers() {
Thread {
val blockedNumbers = getBlockedNumbers()
runOnUiThread {
manage_blocked_numbers_placeholder.beVisibleIf(blockedNumbers.isEmpty())
manage_blocked_numbers_placeholder_2.beVisibleIf(blockedNumbers.isEmpty())
}
}.start()
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu_add_blocked_number, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.add_blocked_number -> addBlockedNumber()
else -> return super.onOptionsItemSelected(item)
}
return true
}
override fun refreshItems() {
updateBlockedNumbers()
}
private fun addBlockedNumber() {
}
}

View File

@ -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))
}
}

View File

@ -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<String> {
return sourceNames
}
@TargetApi(Build.VERSION_CODES.N)
fun Context.getBlockedNumbers(): ArrayList<BlockedNumber> {
val blockedNumbers = ArrayList<BlockedNumber>()
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)

View File

@ -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<BlockedNumber> {
val blockedNumbers = ArrayList<BlockedNumber>()
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
}
}

View File

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/manage_blocked_numbers_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.simplemobiletools.commons.views.MyRecyclerView
android:id="@+id/manage_blocked_numbers_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:scrollbars="vertical"
app:layoutManager="com.simplemobiletools.commons.views.MyLinearLayoutManager"/>
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/manage_blocked_numbers_placeholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingLeft="@dimen/big_margin"
android:paddingTop="@dimen/activity_margin"
android:paddingRight="@dimen/big_margin"
android:text="@string/not_blocking_anyone"
android:textSize="@dimen/bigger_text_size"
android:visibility="gone"/>
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/manage_blocked_numbers_placeholder_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/manage_blocked_numbers_placeholder"
android:layout_centerHorizontal="true"
android:background="?attr/selectableItemBackground"
android:gravity="center"
android:padding="@dimen/activity_margin"
android:text="@string/add_a_blocked_number"
android:textSize="@dimen/bigger_text_size"
android:visibility="gone"/>
</RelativeLayout>

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/manage_blocked_number_holder"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
android:foreground="@drawable/selector"
android:padding="@dimen/activity_margin">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/manage_blocked_number_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginTop="@dimen/medium_margin"/>
</RelativeLayout>

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/add_blocked_number"
android:icon="@drawable/ic_plus"
android:title="@string/add_a_blocked_number"
app:showAsAction="ifRoom"/>
</menu>