diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/browser/cloudflare/CaptchaNotifier.kt b/app/src/main/kotlin/org/koitharu/kotatsu/browser/cloudflare/CaptchaNotifier.kt index 8a4f40516..d57cc09a4 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/browser/cloudflare/CaptchaNotifier.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/browser/cloudflare/CaptchaNotifier.kt @@ -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" diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Coil.kt b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Coil.kt index d870f8d3b..782811b67 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Coil.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/core/util/ext/Coil.kt @@ -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) } diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/suggestions/ui/SuggestionsWorker.kt b/app/src/main/kotlin/org/koitharu/kotatsu/suggestions/ui/SuggestionsWorker.kt index dc6fbfa41..dc37f78ca 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/suggestions/ui/SuggestionsWorker.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/suggestions/ui/SuggestionsWorker.kt @@ -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()) diff --git a/app/src/main/kotlin/org/koitharu/kotatsu/tracker/work/TrackWorker.kt b/app/src/main/kotlin/org/koitharu/kotatsu/tracker/work/TrackWorker.kt index 820f05c6d..7cd1f04ec 100644 --- a/app/src/main/kotlin/org/koitharu/kotatsu/tracker/work/TrackWorker.kt +++ b/app/src/main/kotlin/org/koitharu/kotatsu/tracker/work/TrackWorker.kt @@ -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 ->