Migrate blocked conversations to qk blocking manager

This commit is contained in:
moezbhatti
2019-09-08 20:51:12 -04:00
parent 3aba92825b
commit 4c9130e11a
8 changed files with 77 additions and 22 deletions
+1
View File
@@ -82,6 +82,7 @@ dependencies {
implementation "com.jakewharton.timber:timber:$timber_version"
implementation "com.squareup.moshi:moshi:$moshi_version"
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:$coroutines_version"
implementation project(":android-smsmms")
implementation project(':common')
implementation project(':domain')
@@ -31,7 +31,7 @@ class QksmsBlockingClient @Inject constructor(
override fun getClientCapability() = BlockingClient.Capability.BLOCK_WITHOUT_PERMISSION
override fun getAction(address: String): Single<BlockingClient.Action> = Single.fromCallable {
override fun getAction(address: String): Single<BlockingClient.Action> = Single.fromCallable {
when (blockingRepo.isBlocked(address)) {
true -> BlockingClient.Action.BLOCK
false -> BlockingClient.Action.UNBLOCK
@@ -29,7 +29,6 @@ import android.os.Message
import android.os.Messenger
import androidx.core.os.bundleOf
import com.moez.QKSMS.common.util.extensions.isInstalled
import com.moez.QKSMS.util.Preferences
import com.moez.QKSMS.util.tryOrNull
import io.reactivex.Completable
import io.reactivex.Single
@@ -37,7 +36,6 @@ import io.reactivex.subjects.SingleSubject
import javax.inject.Inject
class ShouldIAnswerBlockingClient @Inject constructor(
prefs: Preferences,
private val context: Context
) : BlockingClient {
@@ -50,14 +48,6 @@ class ShouldIAnswerBlockingClient @Inject constructor(
const val GET_NUMBER_RATING = 1
}
init {
// Migrate from old SIA preference to blocking manager preference
if (prefs.sia.get()) {
prefs.blockingManager.set(Preferences.BLOCKING_MANAGER_SIA)
prefs.sia.delete()
}
}
override fun isAvailable(): Boolean = listOf("org.mistergroup.shouldianswer",
"org.mistergroup.shouldianswerpersonal",
"org.mistergroup.muzutozvednout")
@@ -40,12 +40,12 @@ class ChangelogManagerImpl @Inject constructor(
private val prefs: Preferences
) : ChangelogManager {
private val oldVersion: Int get() = prefs.version.get()
private val oldVersion: Int get() = prefs.changelogVersion.get()
private val versionCode: Int get() = context.packageManager.getPackageInfo(context.packageName, 0).versionCode
override fun didUpdate(): Boolean {
if (oldVersion == 0) {
prefs.version.set(versionCode)
prefs.changelogVersion.set(versionCode)
}
return when {
@@ -92,7 +92,7 @@ class ChangelogManagerImpl @Inject constructor(
}
override fun markChangelogSeen() {
prefs.version.set(versionCode)
prefs.changelogVersion.set(versionCode)
}
private data class ChangelogResponse(
@@ -0,0 +1,66 @@
/*
* Copyright (C) 2019 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.migration
import android.content.Context
import com.moez.QKSMS.blocking.QksmsBlockingClient
import com.moez.QKSMS.repository.ConversationRepository
import com.moez.QKSMS.util.Preferences
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import javax.inject.Inject
class QkMigration @Inject constructor(
context: Context,
private val conversationRepo: ConversationRepository,
private val prefs: Preferences,
private val qksmsBlockingClient: QksmsBlockingClient
) {
init {
val oldVersion = prefs.version.get()
val newVersion = context.packageManager.getPackageInfo(context.packageName, 0).versionCode
if (oldVersion < 2199) {
upgradeTo370()
}
prefs.changelogVersion.set(newVersion)
}
private fun upgradeTo370() = GlobalScope.launch {
// Migrate changelog version
prefs.changelogVersion.set(prefs.version.get())
// Migrate from old SIA preference to blocking manager preference
if (prefs.sia.get()) {
prefs.blockingManager.set(Preferences.BLOCKING_MANAGER_SIA)
prefs.sia.delete()
}
// Migrate blocked conversations into QK blocking client
val addresses = conversationRepo.getBlockedConversations()
.flatMap { conversation -> conversation.recipients }
.map { recipient -> recipient.address }
.distinct()
qksmsBlockingClient.block(addresses).blockingAwait()
}
}
@@ -22,7 +22,6 @@ import android.content.ContentUris
import android.content.Context
import android.provider.Telephony
import android.telephony.PhoneNumberUtils
import com.moez.QKSMS.blocking.BlockingClient
import com.moez.QKSMS.compat.TelephonyCompat
import com.moez.QKSMS.extensions.anyOf
import com.moez.QKSMS.extensions.map
@@ -42,7 +41,6 @@ import java.util.concurrent.TimeUnit
import javax.inject.Inject
class ConversationRepositoryImpl @Inject constructor(
private val blockingClient: BlockingClient,
private val context: Context,
private val conversationFilter: ConversationFilter,
private val cursorToConversation: CursorToConversation,
@@ -354,10 +352,6 @@ class ConversationRepositoryImpl @Inject constructor(
}
}
conversation.blocked = recipients.any { recipient ->
blockingClient.getAction(recipient.address).blockingGet() == BlockingClient.Action.BLOCK
}
conversation.recipients.clear()
conversation.recipients.addAll(recipients)
realm.executeTransaction { it.insertOrUpdate(conversation) }
@@ -69,6 +69,8 @@ class Preferences @Inject constructor(private val rxPrefs: RxSharedPreferences)
// Internal
val night = rxPrefs.getBoolean("night", false)
val canUseSubId = rxPrefs.getBoolean("canUseSubId", true)
val version = rxPrefs.getInteger("version", 0)
val changelogVersion = rxPrefs.getInteger("changelogVersion", 0)
@Deprecated("This should only be accessed when migrating to @blockingManager")
val sia = rxPrefs.getBoolean("sia", false)
@@ -100,7 +102,6 @@ class Preferences @Inject constructor(private val rxPrefs: RxSharedPreferences)
val mobileOnly = rxPrefs.getBoolean("mobileOnly", false)
val mmsSize = rxPrefs.getInteger("mmsSize", 300)
val logging = rxPrefs.getBoolean("logging", false)
val version = rxPrefs.getInteger("version", 0)
init {
// Migrate from old night mode preference to new one, now that we support android Q night mode
@@ -31,6 +31,7 @@ import com.moez.QKSMS.common.util.FileLoggingTree
import com.moez.QKSMS.injection.AppComponentManager
import com.moez.QKSMS.injection.appComponent
import com.moez.QKSMS.manager.AnalyticsManager
import com.moez.QKSMS.migration.QkMigration
import com.moez.QKSMS.migration.QkRealmMigration
import com.moez.QKSMS.util.NightModeManager
import com.uber.rxdogtag.RxDogTag
@@ -48,10 +49,12 @@ import javax.inject.Inject
class QKApplication : Application(), HasActivityInjector, HasBroadcastReceiverInjector, HasServiceInjector {
/**
* Inject this so that it is forced to initialize
* Inject these so that they are forced to initialize
*/
@Suppress("unused")
@Inject lateinit var analyticsManager: AnalyticsManager
@Suppress("unused")
@Inject lateinit var qkMigration: QkMigration
@Inject lateinit var dispatchingActivityInjector: DispatchingAndroidInjector<Activity>
@Inject lateinit var dispatchingBroadcastReceiverInjector: DispatchingAndroidInjector<BroadcastReceiver>