allow deleting items from Recents

This commit is contained in:
tibbi
2020-05-11 13:16:33 +02:00
parent 8dcd610811
commit b4683d946c
9 changed files with 87 additions and 15 deletions

View File

@ -4,8 +4,9 @@ import android.annotation.SuppressLint
import android.content.Context
import android.provider.CallLog.Calls
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.PERMISSION_READ_CALL_LOG
import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_CALL_LOG
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.overloads.times
import com.simplemobiletools.dialer.models.RecentCall
class RecentsHelper(private val context: Context) {
@ -13,7 +14,7 @@ class RecentsHelper(private val context: Context) {
fun getRecentCalls(callback: (ArrayList<RecentCall>) -> Unit) {
ensureBackgroundThread {
val recentCalls = ArrayList<RecentCall>()
if (!context.hasPermission(PERMISSION_READ_CALL_LOG)) {
if (!context.hasPermission(PERMISSION_WRITE_CALL_LOG)) {
callback(recentCalls)
return@ensureBackgroundThread
}
@ -52,4 +53,19 @@ class RecentsHelper(private val context: Context) {
callback(recentCalls)
}
}
@SuppressLint("MissingPermission")
fun removeRecentCalls(ids: ArrayList<Int>, callback: () -> Unit) {
ensureBackgroundThread {
val uri = Calls.CONTENT_URI
ids.chunked(30).forEach { chunk ->
val selection = "${Calls._ID} IN (${getQuestionMarks(chunk.size)})"
val selectionArgs = chunk.map { it.toString() }.toTypedArray()
context.contentResolver.delete(uri, selection, selectionArgs)
}
callback()
}
}
private fun getQuestionMarks(size: Int) = ("?," * size).trimEnd(',')
}