diff --git a/data/build.gradle b/data/build.gradle index 915ed179..031be0bb 100644 --- a/data/build.gradle +++ b/data/build.gradle @@ -81,6 +81,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 files('libs/callcontrol-datashare-release.aar') implementation project(":android-smsmms") implementation project(':common') implementation project(':domain') diff --git a/data/libs/callcontrol-datashare-release.aar b/data/libs/callcontrol-datashare-release.aar new file mode 100644 index 00000000..24c2fd79 Binary files /dev/null and b/data/libs/callcontrol-datashare-release.aar differ diff --git a/data/src/main/java/com/moez/QKSMS/blocking/CallControlBlockingClient.kt b/data/src/main/java/com/moez/QKSMS/blocking/CallControlBlockingClient.kt new file mode 100644 index 00000000..758d4bf4 --- /dev/null +++ b/data/src/main/java/com/moez/QKSMS/blocking/CallControlBlockingClient.kt @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2017 Moez Bhatti + * + * 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 . + */ +package com.moez.QKSMS.blocking + +import android.content.Context +import android.database.Cursor +import android.net.Uri +import androidx.core.database.getStringOrNull +import com.callcontrol.datashare.CallControl +import com.moez.QKSMS.extensions.map +import com.moez.QKSMS.util.tryOrNull +import io.reactivex.Single +import javax.inject.Inject + +class CallControlBlockingClient @Inject constructor( + private val context: Context +) : BlockingClient { + + class LookupResult(cursor: Cursor) { + val displayName: String? = cursor.getStringOrNull(cursor.getColumnIndexOrThrow(CallControl.Lookup.DISPLAY_NAME)) + val blockReason: String? = cursor.getStringOrNull(cursor.getColumnIndexOrThrow(CallControl.Lookup.BLOCK_REASON)) + } + + override fun shouldBlock(address: String): Single { + val uri = Uri.withAppendedPath(CallControl.LOOKUP_TEXT_URI, address) + return Single.fromCallable { + tryOrNull { + context.contentResolver.query(uri, null, 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 + } + } + +} diff --git a/data/src/main/java/com/moez/QKSMS/blocking/QkBlockingClient.kt b/data/src/main/java/com/moez/QKSMS/blocking/QkBlockingClient.kt new file mode 100644 index 00000000..15f53f3a --- /dev/null +++ b/data/src/main/java/com/moez/QKSMS/blocking/QkBlockingClient.kt @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2017 Moez Bhatti + * + * 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 . + */ +package com.moez.QKSMS.blocking + +import io.reactivex.Single +import javax.inject.Inject + +class QkBlockingClient @Inject constructor() : BlockingClient { + + override fun shouldBlock(address: String): Single { + return Single.just(false) // TODO: This will be implemented once we're able to reject these messages entirely + } + +} diff --git a/data/src/main/java/com/moez/QKSMS/blocking/ShouldIAnswerBlockingClient.kt b/data/src/main/java/com/moez/QKSMS/blocking/ShouldIAnswerBlockingClient.kt index a427a2b5..9f62715d 100644 --- a/data/src/main/java/com/moez/QKSMS/blocking/ShouldIAnswerBlockingClient.kt +++ b/data/src/main/java/com/moez/QKSMS/blocking/ShouldIAnswerBlockingClient.kt @@ -32,7 +32,6 @@ import com.moez.QKSMS.util.Preferences import com.moez.QKSMS.util.tryOrNull import io.reactivex.Single import io.reactivex.subjects.SingleSubject -import timber.log.Timber import javax.inject.Inject class ShouldIAnswerBlockingClient @Inject constructor( @@ -104,9 +103,7 @@ class ShouldIAnswerBlockingClient @Inject constructor( what = GET_NUMBER_RATING data = bundleOf("number" to address) replyTo = Messenger(IncomingHandler { response -> - val shouldBlock = response.rating == RATING_NEGATIVE || response.wantBlock - subject.onSuccess(shouldBlock) - Timber.v("Should block: $shouldBlock") + subject.onSuccess(response.rating == RATING_NEGATIVE || response.wantBlock) // We're done, so unbind the service if (isBound && serviceMessenger != null) { diff --git a/domain/src/main/java/com/moez/QKSMS/interactor/ReceiveMms.kt b/domain/src/main/java/com/moez/QKSMS/interactor/ReceiveMms.kt index a635f7c5..c13f0e00 100644 --- a/domain/src/main/java/com/moez/QKSMS/interactor/ReceiveMms.kt +++ b/domain/src/main/java/com/moez/QKSMS/interactor/ReceiveMms.kt @@ -27,6 +27,7 @@ import com.moez.QKSMS.repository.ConversationRepository import com.moez.QKSMS.repository.MessageRepository import com.moez.QKSMS.repository.SyncRepository import io.reactivex.Flowable +import timber.log.Timber import javax.inject.Inject class ReceiveMms @Inject constructor( @@ -55,6 +56,7 @@ class ReceiveMms @Inject constructor( // turns out that it should be blocked, then delete it // TODO Don't store blocked messages in the first place !blockingClient.shouldBlock(message.address).blockingGet().also { blocked -> + Timber.v("Should block: $blocked") if (blocked) messageRepo.deleteMessages(message.id) } } diff --git a/domain/src/main/java/com/moez/QKSMS/interactor/ReceiveSms.kt b/domain/src/main/java/com/moez/QKSMS/interactor/ReceiveSms.kt index b706f8a8..675ff9e1 100644 --- a/domain/src/main/java/com/moez/QKSMS/interactor/ReceiveSms.kt +++ b/domain/src/main/java/com/moez/QKSMS/interactor/ReceiveSms.kt @@ -26,6 +26,7 @@ import com.moez.QKSMS.manager.ShortcutManager import com.moez.QKSMS.repository.ConversationRepository import com.moez.QKSMS.repository.MessageRepository import io.reactivex.Flowable +import timber.log.Timber import javax.inject.Inject class ReceiveSms @Inject constructor( @@ -45,7 +46,9 @@ class ReceiveSms @Inject constructor( .filter { // Don't continue if the sender is blocked val address = it.messages[0].displayOriginatingAddress - !blockingClient.shouldBlock(address).blockingGet() + val shouldBlock = blockingClient.shouldBlock(address).blockingGet() + Timber.v("Should block: $shouldBlock") + !shouldBlock } .map { val messages = it.messages diff --git a/presentation/src/main/AndroidManifest.xml b/presentation/src/main/AndroidManifest.xml index bdc4132e..47b1260b 100644 --- a/presentation/src/main/AndroidManifest.xml +++ b/presentation/src/main/AndroidManifest.xml @@ -194,6 +194,9 @@ + diff --git a/presentation/src/main/java/com/moez/QKSMS/injection/AppModule.kt b/presentation/src/main/java/com/moez/QKSMS/injection/AppModule.kt index 089d3b0f..1067969f 100644 --- a/presentation/src/main/java/com/moez/QKSMS/injection/AppModule.kt +++ b/presentation/src/main/java/com/moez/QKSMS/injection/AppModule.kt @@ -26,6 +26,8 @@ import android.preference.PreferenceManager import androidx.lifecycle.ViewModelProvider import com.f2prateek.rx.preferences2.RxSharedPreferences import com.moez.QKSMS.blocking.BlockingClient +import com.moez.QKSMS.blocking.CallControlBlockingClient +import com.moez.QKSMS.blocking.QkBlockingClient import com.moez.QKSMS.blocking.ShouldIAnswerBlockingClient import com.moez.QKSMS.common.ViewModelFactory import com.moez.QKSMS.common.util.NotificationManagerImpl @@ -76,6 +78,7 @@ import com.moez.QKSMS.repository.ScheduledMessageRepository import com.moez.QKSMS.repository.ScheduledMessageRepositoryImpl import com.moez.QKSMS.repository.SyncRepository import com.moez.QKSMS.repository.SyncRepositoryImpl +import com.moez.QKSMS.util.Preferences import com.squareup.moshi.Moshi import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory import dagger.Module @@ -129,7 +132,16 @@ class AppModule(private var application: Application) { fun provideAnalyticsManager(manager: AnalyticsManagerImpl): AnalyticsManager = manager @Provides - fun externalBlockingManager(manager: ShouldIAnswerBlockingClient): BlockingClient = manager + fun externalBlockingManager( + callControl: CallControlBlockingClient, + sia: ShouldIAnswerBlockingClient, + qksms: QkBlockingClient, + prefs: Preferences + ): BlockingClient = when { + prefs.callControl.get() -> callControl + prefs.sia.get() -> sia + else -> qksms + } @Provides fun changelogManager(manager: ChangelogManagerImpl): ChangelogManager = manager @@ -152,7 +164,6 @@ class AppModule(private var application: Application) { @Provides fun provideWidgetManager(manager: WidgetManagerImpl): WidgetManager = manager - // Mapper @Provides @@ -170,7 +181,6 @@ class AppModule(private var application: Application) { @Provides fun provideCursorToRecipient(mapper: CursorToRecipientImpl): CursorToRecipient = mapper - // Repository @Provides