Check with Call Control to see if we should block message

This commit is contained in:
moezbhatti
2019-05-29 23:50:37 -04:00
parent 4db125037a
commit 3396fdbd99
9 changed files with 105 additions and 8 deletions
+1
View File
@@ -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')
Binary file not shown.
@@ -0,0 +1,51 @@
/*
* 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.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<Boolean> {
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
}
}
}
@@ -0,0 +1,30 @@
/*
* 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.blocking
import io.reactivex.Single
import javax.inject.Inject
class QkBlockingClient @Inject constructor() : BlockingClient {
override fun shouldBlock(address: String): Single<Boolean> {
return Single.just(false) // TODO: This will be implemented once we're able to reject these messages entirely
}
}
@@ -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) {
@@ -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)
}
}
@@ -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
@@ -194,6 +194,9 @@
</intent-filter>
</service>
<meta-data
android:name="com.callcontrol.datashare.messaging"
android:value="true" />
<meta-data
android:name="preloaded_fonts"
android:resource="@array/preloaded_fonts" />
@@ -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