Skip to content

Commit

Permalink
Migrate blocked conversations to qk blocking manager
Browse files Browse the repository at this point in the history
  • Loading branch information
moezbhatti committed Sep 9, 2019
1 parent 3aba928 commit 4c9130e
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 22 deletions.
1 change: 1 addition & 0 deletions data/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,13 @@ 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
import io.reactivex.subjects.SingleSubject
import javax.inject.Inject

class ShouldIAnswerBlockingClient @Inject constructor(
prefs: Preferences,
private val context: Context
) : BlockingClient {

Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -92,7 +92,7 @@ class ChangelogManagerImpl @Inject constructor(
}

override fun markChangelogSeen() {
prefs.version.set(versionCode)
prefs.changelogVersion.set(versionCode)
}

private data class ChangelogResponse(
Expand Down
66 changes: 66 additions & 0 deletions data/src/main/java/com/moez/QKSMS/migration/QkMigration.kt
Original file line number Diff line number Diff line change
@@ -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()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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) }
Expand Down
3 changes: 2 additions & 1 deletion domain/src/main/java/com/moez/QKSMS/util/Preferences.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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>
Expand Down

0 comments on commit 4c9130e

Please sign in to comment.