forked from upstream-mirrors/quik
Build out blocking client for pre api 24 devices
This commit is contained in:
@@ -23,25 +23,22 @@ import android.content.Intent
|
||||
import android.os.Build
|
||||
import android.provider.BlockedNumberContract
|
||||
import android.telecom.TelecomManager
|
||||
import android.telephony.PhoneNumberUtils
|
||||
import androidx.core.content.contentValuesOf
|
||||
import com.moez.QKSMS.extensions.anyOf
|
||||
import com.moez.QKSMS.model.BlockedNumber
|
||||
import com.moez.QKSMS.repository.BlockingRepository
|
||||
import io.reactivex.Completable
|
||||
import io.reactivex.Single
|
||||
import io.realm.Realm
|
||||
import javax.inject.Inject
|
||||
|
||||
class QkBlockingClient @Inject constructor(private val context: Context) : BlockingClient {
|
||||
class QkBlockingClient @Inject constructor(
|
||||
private val context: Context,
|
||||
private val blockingRepo: BlockingRepository
|
||||
) : BlockingClient {
|
||||
|
||||
override fun isBlocked(address: String): Single<Boolean> = Single.fromCallable {
|
||||
when {
|
||||
Build.VERSION.SDK_INT >= 24 -> BlockedNumberContract.isBlocked(context, address)
|
||||
|
||||
else -> Realm.getDefaultInstance()
|
||||
.where(BlockedNumber::class.java)
|
||||
.findAll()
|
||||
.any { number -> PhoneNumberUtils.compare(number.address, address) }
|
||||
else -> blockingRepo.isBlocked(address)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,14 +51,7 @@ class QkBlockingClient @Inject constructor(private val context: Context) : Block
|
||||
context.contentResolver.insert(BlockedNumberContract.BlockedNumbers.CONTENT_URI, cv)
|
||||
}
|
||||
|
||||
else -> Realm.getDefaultInstance().use { realm ->
|
||||
val maxId = realm.where(BlockedNumber::class.java)
|
||||
.max("id")?.toLong() ?: -1
|
||||
|
||||
realm.executeTransaction {
|
||||
realm.insert(addresses.mapIndexed { index, address -> BlockedNumber(maxId + 1 + index, address) })
|
||||
}
|
||||
}
|
||||
else -> blockingRepo.blockNumber(*addresses.toTypedArray())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,21 +63,7 @@ class QkBlockingClient @Inject constructor(private val context: Context) : Block
|
||||
BlockedNumberContract.unblock(context, address)
|
||||
}
|
||||
|
||||
else -> Realm.getDefaultInstance().use { realm ->
|
||||
val ids = realm
|
||||
.where(BlockedNumber::class.java)
|
||||
.findAll()
|
||||
.filter { number ->
|
||||
addresses.any { address -> PhoneNumberUtils.compare(number.address, address) }
|
||||
}
|
||||
.map { number -> number.id }
|
||||
.toLongArray()
|
||||
|
||||
realm.executeTransaction {
|
||||
realm.where(BlockedNumber::class.java)
|
||||
.anyOf("id", ids)
|
||||
}
|
||||
}
|
||||
else -> blockingRepo.unblockNumbers(*addresses.toTypedArray())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,7 +76,7 @@ class QkBlockingClient @Inject constructor(private val context: Context) : Block
|
||||
context.startActivity(intent)
|
||||
}
|
||||
|
||||
else -> {} // TODO
|
||||
else -> {} // TODO: Do this here once we implement AndroidX navigation
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
||||
*
|
||||
* This file is part of QKSMS.
|
||||
*
|
||||
* QKSMS is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* QKSMS is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package com.moez.QKSMS.repository
|
||||
|
||||
import android.telephony.PhoneNumberUtils
|
||||
import com.moez.QKSMS.extensions.anyOf
|
||||
import com.moez.QKSMS.model.BlockedNumber
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmResults
|
||||
import javax.inject.Inject
|
||||
|
||||
class BlockingRepositoryImpl @Inject constructor() : BlockingRepository {
|
||||
|
||||
override fun blockNumber(vararg addresses: String) {
|
||||
val newAddresses = addresses.filter { address -> !isBlocked(address) }
|
||||
|
||||
Realm.getDefaultInstance().use { realm ->
|
||||
val maxId = realm.where(BlockedNumber::class.java)
|
||||
.max("id")?.toLong() ?: -1
|
||||
|
||||
realm.executeTransaction {
|
||||
realm.insert(newAddresses.mapIndexed { index, address -> BlockedNumber(maxId + 1 + index, address) })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun getBlockedNumbers(): RealmResults<BlockedNumber> {
|
||||
val realm = Realm.getDefaultInstance()
|
||||
return realm.where(BlockedNumber::class.java)
|
||||
.findAllAsync()
|
||||
}
|
||||
|
||||
override fun isBlocked(address: String): Boolean {
|
||||
return Realm.getDefaultInstance().use { realm ->
|
||||
realm.where(BlockedNumber::class.java)
|
||||
.findAll()
|
||||
.any { number -> PhoneNumberUtils.compare(number.address, address) }
|
||||
}
|
||||
}
|
||||
|
||||
override fun unblockNumber(id: Long) {
|
||||
Realm.getDefaultInstance().use { realm ->
|
||||
realm.executeTransaction {
|
||||
realm.where(BlockedNumber::class.java)
|
||||
.equalTo("id", id)
|
||||
.findAll()
|
||||
.deleteAllFromRealm()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun unblockNumbers(vararg addresses: String) {
|
||||
Realm.getDefaultInstance().use { realm ->
|
||||
val ids = realm.where(BlockedNumber::class.java)
|
||||
.findAll()
|
||||
.filter { number ->
|
||||
addresses.any { address -> PhoneNumberUtils.compare(number.address, address) }
|
||||
}
|
||||
.map { number -> number.id }
|
||||
.toLongArray()
|
||||
|
||||
realm.executeTransaction {
|
||||
realm.where(BlockedNumber::class.java)
|
||||
.anyOf("id", ids)
|
||||
.findAll()
|
||||
.deleteAllFromRealm()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user