Show captcha notfication for images

This commit is contained in:
Koitharu
2023-07-30 17:44:24 +03:00
parent 50554c6936
commit cd4317dec5
4 changed files with 19 additions and 17 deletions

View File

@@ -7,27 +7,21 @@ import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import androidx.core.app.PendingIntentCompat
import androidx.core.net.toUri
import dagger.hilt.android.qualifiers.ApplicationContext
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import coil.request.ErrorResult
import coil.request.ImageRequest
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.core.exceptions.CloudFlareProtectedException
import org.koitharu.kotatsu.parsers.model.ContentType
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class CaptchaNotifier @Inject constructor(
@ApplicationContext private val context: Context,
) {
private val mutex = Mutex()
class CaptchaNotifier(
private val context: Context,
) : ImageRequest.Listener {
@SuppressLint("MissingPermission")
suspend fun notify(exception: CloudFlareProtectedException) = mutex.withLock {
fun notify(exception: CloudFlareProtectedException) {
val manager = NotificationManagerCompat.from(context)
if (!manager.areNotificationsEnabled()) {
return@withLock
return
}
val channel = NotificationChannelCompat.Builder(CHANNEL_ID, NotificationManagerCompat.IMPORTANCE_DEFAULT)
.setName(context.getString(R.string.captcha_required))
@@ -63,6 +57,14 @@ class CaptchaNotifier @Inject constructor(
manager.notify(TAG, exception.source.hashCode(), notification)
}
override fun onError(request: ImageRequest, result: ErrorResult) {
super.onError(request, result)
val e = result.throwable
if (e is CloudFlareProtectedException) {
notify(e)
}
}
private companion object {
private const val CHANNEL_ID = "captcha"

View File

@@ -12,6 +12,7 @@ import coil.request.SuccessResult
import coil.util.CoilUtils
import com.google.android.material.progressindicator.BaseProgressIndicator
import org.koitharu.kotatsu.R
import org.koitharu.kotatsu.browser.cloudflare.CaptchaNotifier
import org.koitharu.kotatsu.core.ui.image.RegionBitmapDecoder
import org.koitharu.kotatsu.core.util.progress.ImageRequestIndicatorListener
import org.koitharu.kotatsu.parsers.model.MangaSource
@@ -28,6 +29,7 @@ fun ImageView.newImageRequest(lifecycleOwner: LifecycleOwner, data: Any?): Image
.data(data)
.lifecycle(lifecycleOwner)
.crossfade(context)
.listener(CaptchaNotifier(context.applicationContext))
.target(this)
}

View File

@@ -82,7 +82,6 @@ class SuggestionsWorker @AssistedInject constructor(
private val appSettings: AppSettings,
private val mangaRepositoryFactory: MangaRepository.Factory,
private val sourcesRepository: MangaSourcesRepository,
private val captchaNotifier: CaptchaNotifier,
) : CoroutineWorker(appContext, params) {
private val notificationManager by lazy { NotificationManagerCompat.from(appContext) }
@@ -211,7 +210,7 @@ class SuggestionsWorker @AssistedInject constructor(
list.take(MAX_SOURCE_RESULTS)
}.onFailure { e ->
if (e is CloudFlareProtectedException) {
captchaNotifier.notify(e)
CaptchaNotifier(applicationContext).notify(e)
}
e.printStackTraceDebug()
}.getOrDefault(emptyList())

View File

@@ -69,7 +69,6 @@ class TrackWorker @AssistedInject constructor(
private val settings: AppSettings,
private val tracker: Tracker,
@TrackerLogger private val logger: FileLogger,
private val captchaNotifier: CaptchaNotifier,
) : CoroutineWorker(context, workerParams) {
private val notificationManager by lazy { NotificationManagerCompat.from(applicationContext) }
@@ -130,7 +129,7 @@ class TrackWorker @AssistedInject constructor(
tracker.fetchUpdates(track, commit = true)
}.onFailure { e ->
if (e is CloudFlareProtectedException) {
captchaNotifier.notify(e)
CaptchaNotifier(applicationContext).notify(e)
}
logger.log("checkUpdatesAsync", e)
}.onSuccess { updates ->