Build out blocking client for pre api 24 devices

This commit is contained in:
moezbhatti
2019-06-26 02:01:46 -04:00
parent c04a368cc8
commit 83090452b5
19 changed files with 624 additions and 38 deletions
@@ -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()
}
}
}
}