From 8204a15276314ef46da6ce563feb7a62281d6708 Mon Sep 17 00:00:00 2001 From: Pupil Date: Sat, 22 Feb 2020 20:30:42 +0900 Subject: [PATCH] Proxy applied to thumbnails --- .../xyz/quaver/pupil/util/download/Cache.kt | 5 +- .../pupil/util/download/DownloadWorker.kt | 62 ++++++++++++------- 2 files changed, 44 insertions(+), 23 deletions(-) diff --git a/app/src/main/java/xyz/quaver/pupil/util/download/Cache.kt b/app/src/main/java/xyz/quaver/pupil/util/download/Cache.kt index a4808a9a..d851b75b 100644 --- a/app/src/main/java/xyz/quaver/pupil/util/download/Cache.kt +++ b/app/src/main/java/xyz/quaver/pupil/util/download/Cache.kt @@ -30,6 +30,7 @@ import kotlinx.coroutines.withContext import xyz.quaver.Code import xyz.quaver.hitomi.GalleryBlock import xyz.quaver.hitomi.Reader +import xyz.quaver.proxy import xyz.quaver.pupil.util.getCachedGallery import xyz.quaver.pupil.util.getDownloadDirectory import xyz.quaver.pupil.util.json @@ -78,7 +79,9 @@ class Cache(context: Context) : ContextWrapper(context) { withContext(Dispatchers.IO) { val thumbnails = getGalleryBlock(galleryID)?.thumbnails try { - Base64.encodeToString(URL(thumbnails?.firstOrNull()).readBytes(), Base64.DEFAULT) + Base64.encodeToString(URL(thumbnails?.firstOrNull()).openConnection(proxy).getInputStream().use { + it.readBytes() + }, Base64.DEFAULT) } catch (e: Exception) { null } diff --git a/app/src/main/java/xyz/quaver/pupil/util/download/DownloadWorker.kt b/app/src/main/java/xyz/quaver/pupil/util/download/DownloadWorker.kt index bb3a2716..b729a6c7 100644 --- a/app/src/main/java/xyz/quaver/pupil/util/download/DownloadWorker.kt +++ b/app/src/main/java/xyz/quaver/pupil/util/download/DownloadWorker.kt @@ -23,6 +23,7 @@ import android.content.Context import android.content.ContextWrapper import android.content.Intent import android.content.SharedPreferences +import android.util.Log import android.util.SparseArray import androidx.core.app.NotificationCompat import androidx.core.app.NotificationManagerCompat @@ -159,7 +160,7 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont fun buildClient() = OkHttpClient.Builder() .addInterceptor(interceptor) - .dispatcher(Dispatcher(Executors.newFixedThreadPool(4))) + .dispatcher(Dispatcher(Executors.newSingleThreadExecutor())) .proxy(proxy) .build() @@ -279,6 +280,7 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont for (i in reader.galleryInfo.files.indices) { val callback = object : Callback { override fun onFailure(call: Call, e: IOException) { + Log.i("PUPILD", "FAIL ${call.request().tag()} (${e.message})") if (Fabric.isInitialized() && e.message != "Canceled") Crashlytics.logException(e) @@ -287,43 +289,59 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont notify(galleryID) - if (isCompleted(galleryID)) { - with(Cache(this@DownloadWorker)) { - if (isDownloading(galleryID)) { - moveToDownload(galleryID) - setDownloading(galleryID, false) + CoroutineScope(Dispatchers.IO).launch { + if (isCompleted(galleryID) && clients.indexOfKey(galleryID) >= 0) { + clients.remove(galleryID) + with(Cache(this@DownloadWorker)) { + if (isDownloading(galleryID)) { + moveToDownload(galleryID) + setDownloading(galleryID, false) + } } } - clients.remove(galleryID) } } override fun onResponse(call: Call, response: Response) { - response.body().use { - val res = it.bytes() - val ext = - call.request().url().encodedPath().split('.').last() + Log.i("PUPILD", "OK ${call.request().tag()}") - Cache(this@DownloadWorker).putImage(galleryID, "%05d.%s".format(i, ext), res) - progress[galleryID]?.set(i, Float.POSITIVE_INFINITY) - } + try { + response.body().use { + val res = it.bytes() + val ext = + call.request().url().encodedPath().split('.').last() - notify(galleryID) + Cache(this@DownloadWorker).putImage(galleryID, "%05d.%s".format(i, ext), res) + progress[galleryID]?.set(i, Float.POSITIVE_INFINITY) + } - if (isCompleted(galleryID)) { - with(Cache(this@DownloadWorker)) { - if (isDownloading(galleryID)) { - moveToDownload(galleryID) - setDownloading(galleryID, false) + notify(galleryID) + + CoroutineScope(Dispatchers.IO).launch { + if (isCompleted(galleryID) && clients.indexOfKey(galleryID) >= 0) { + clients.remove(galleryID) + with(Cache(this@DownloadWorker)) { + if (isDownloading(galleryID)) { + moveToDownload(galleryID) + setDownloading(galleryID, false) + } + } } } - clients.remove(galleryID) + + Log.i("PUPILD", "SUCCESS ${call.request().tag()}") + } catch (e: Exception) { + Log.i("PUPILD", "FAIL ON OK ${call.request().tag()} (${e.message})") } } } - if (progress[galleryID]?.get(i)?.isFinite() == true) + if (progress[galleryID]?.get(i)?.isFinite() == true) { queueDownload(galleryID, reader, i, callback) + Log.i("PUPILD", "$galleryID QUEUED $i") + } else { + Log.i("PUPILD", "$galleryID SKIPPED $i (${progress[galleryID]?.get(i)})") + } } }