Show blocking reason if available

This commit is contained in:
moezbhatti
2019-10-10 19:15:38 -04:00
parent 56d13dfe3e
commit d46193abba
16 changed files with 182 additions and 80 deletions
@@ -50,15 +50,15 @@ class CallControlBlockingClient @Inject constructor(
override fun getAction(address: String): Single<BlockingClient.Action> = Single.fromCallable {
val uri = Uri.withAppendedPath(CallControl.LOOKUP_TEXT_URI, address)
val blocked = tryOrNull {
val blockReason = tryOrNull {
context.contentResolver.query(uri, projection, null, null, null) // Query URI
?.use { cursor -> cursor.map(::LookupResult) } // Map to Result object
?.any { result -> result.blockReason != null } // Check if any are blocked
} == true // If none are blocked or we errored at some point, return false
?.find { result -> result.blockReason != null } // Check if any are blocked
}?.blockReason // If none are blocked or we errored at some point, return false
when (blocked) {
true -> BlockingClient.Action.BLOCK
false -> BlockingClient.Action.UNBLOCK
when (blockReason) {
null -> BlockingClient.Action.Unblock
else -> BlockingClient.Action.Block(blockReason)
}
}
@@ -33,8 +33,8 @@ class QksmsBlockingClient @Inject constructor(
override fun getAction(address: String): Single<BlockingClient.Action> = Single.fromCallable {
when (blockingRepo.isBlocked(address)) {
true -> BlockingClient.Action.BLOCK
false -> BlockingClient.Action.UNBLOCK
true -> BlockingClient.Action.Block()
false -> BlockingClient.Action.Unblock
}
}
@@ -59,8 +59,8 @@ class ShouldIAnswerBlockingClient @Inject constructor(
return Binder(context, address).isBlocked()
.map { blocked ->
when (blocked) {
true -> BlockingClient.Action.BLOCK
false -> BlockingClient.Action.DO_NOTHING
true -> BlockingClient.Action.Block()
false -> BlockingClient.Action.DoNothing
}
}
}
@@ -25,7 +25,7 @@ import io.realm.RealmMigration
class QkRealmMigration : RealmMigration {
companion object {
const val SCHEMA_VERSION: Long = 6
const val SCHEMA_VERSION: Long = 7
}
override fun migrate(realm: DynamicRealm, oldVersion: Long, newVersion: Long) {
@@ -80,9 +80,17 @@ class QkRealmMigration : RealmMigration {
version++
}
if (version == 6L) {
realm.schema.get("Conversation")
?.addField("blockingClient", Integer::class.java)
?.addField("blockReason", String::class.java)
version++
}
if (version < newVersion) {
throw IllegalStateException("Migration missing from v$oldVersion to v$newVersion")
}
}
}
}
@@ -297,14 +297,19 @@ class ConversationRepositoryImpl @Inject constructor(
}
}
override fun markBlocked(vararg threadIds: Long) {
override fun markBlocked(threadIds: List<Long>, blockingClient: Int, blockReason: String?) {
Realm.getDefaultInstance().use { realm ->
val conversations = realm.where(Conversation::class.java)
.anyOf("id", threadIds)
.anyOf("id", threadIds.toLongArray())
.equalTo("blocked", false)
.findAll()
realm.executeTransaction {
conversations.forEach { it.blocked = true }
conversations.forEach { conversation ->
conversation.blocked = true
conversation.blockingClient = blockingClient
conversation.blockReason = blockReason
}
}
}
}
@@ -316,7 +321,11 @@ class ConversationRepositoryImpl @Inject constructor(
.findAll()
realm.executeTransaction {
conversations.forEach { it.blocked = false }
conversations.forEach { conversation ->
conversation.blocked = false
conversation.blockingClient = null
conversation.blockReason = null
}
}
}
}
@@ -81,7 +81,7 @@ class SyncRepositoryImpl @Inject constructor(
val realm = Realm.getDefaultInstance()
realm.beginTransaction()
var persistedData = realm.where(Conversation::class.java)
val persistedData = realm.copyFromRealm(realm.where(Conversation::class.java)
.beginGroup()
.equalTo("archived", true)
.or()
@@ -90,9 +90,14 @@ class SyncRepositoryImpl @Inject constructor(
.equalTo("pinned", true)
.or()
.isNotEmpty("name")
.or()
.isNotNull("blockingClient")
.or()
.isNotEmpty("blockReason")
.endGroup()
.findAll()
.map { PersistedData(it.id, it.archived, it.blocked, it.pinned, it.name) }
.findAll())
.associateBy { conversation -> conversation.id }
.toMutableMap()
realm.delete(Contact::class.java)
realm.delete(Conversation::class.java)
@@ -125,26 +130,26 @@ class SyncRepositoryImpl @Inject constructor(
// Migrate blocked conversations from 2.7.3
val oldBlockedSenders = rxPrefs.getStringSet("pref_key_blocked_senders")
persistedData += oldBlockedSenders.get()
oldBlockedSenders.get()
.map { threadIdString -> threadIdString.toLong() }
.filter { threadId -> persistedData.none { it.id == threadId } }
.map { threadId -> PersistedData(threadId, false, true, false, "") }
.filter { threadId -> !persistedData.contains(threadId) }
.forEach { threadId -> persistedData[threadId] = Conversation(id = threadId, blocked = true) }
// Sync conversations
conversationCursor?.use {
val conversations = conversationCursor
.map { cursor ->
progress++
syncProgress.onNext(SyncRepository.SyncProgress.Running(max, progress, false))
cursorToConversation.map(cursor)
val conversations = conversationCursor.map { cursor ->
progress++
syncProgress.onNext(SyncRepository.SyncProgress.Running(max, progress, false))
cursorToConversation.map(cursor).apply {
persistedData[id]?.let { persistedConversation ->
archived = persistedConversation.archived
blocked = persistedConversation.blocked
pinned = persistedConversation.pinned
name = persistedConversation.name
blockingClient = persistedConversation.blockingClient
blockReason = persistedConversation.blockReason
}
persistedData.forEach { data ->
val conversation = conversations.firstOrNull { conversation -> conversation.id == data.id }
conversation?.archived = data.archived
conversation?.blocked = data.blocked
conversation?.pinned = data.pinned
conversation?.name = data.name
}
}
realm.where(Message::class.java)
@@ -152,8 +157,7 @@ class SyncRepositoryImpl @Inject constructor(
.distinct("threadId")
.findAll()
.forEach { message ->
val conversation = conversations
.firstOrNull { conversation -> conversation.id == message.threadId }
val conversation = conversations.find { conversation -> conversation.id == message.threadId }
conversation?.date = message.date
conversation?.snippet = message.getSummary()
conversation?.me = message.isMe()
@@ -165,16 +169,15 @@ class SyncRepositoryImpl @Inject constructor(
// Sync recipients
recipientCursor?.use {
val contacts = realm.copyToRealm(getContacts())
val recipients = recipientCursor
.map { cursor ->
progress++
syncProgress.onNext(SyncRepository.SyncProgress.Running(max, progress, false))
cursorToRecipient.map(cursor).apply {
contact = contacts.firstOrNull { contact ->
contact.numbers.any { phoneNumberUtils.compare(address, it.address) }
}
}
val recipients = recipientCursor.map { cursor ->
progress++
syncProgress.onNext(SyncRepository.SyncProgress.Running(max, progress, false))
cursorToRecipient.map(cursor).apply {
contact = contacts.firstOrNull { contact ->
contact.numbers.any { phoneNumberUtils.compare(address, it.address) }
}
}
}
realm.insertOrUpdate(recipients)
}