From 8204a15276314ef46da6ce563feb7a62281d6708 Mon Sep 17 00:00:00 2001 From: Pupil Date: Sat, 22 Feb 2020 20:30:42 +0900 Subject: [PATCH 1/6] 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)})") + } } } From 994aa99797acff8a1a9ee1920459bfcd579ee24d Mon Sep 17 00:00:00 2001 From: Pupil Date: Sun, 23 Feb 2020 10:28:29 +0900 Subject: [PATCH 2/6] Fixed image loading bug --- app/build.gradle | 4 ++-- app/release/output.json | 2 +- .../java/xyz/quaver/pupil/adapters/ReaderAdapter.kt | 5 ++--- .../main/java/xyz/quaver/pupil/util/download/Cache.kt | 11 +++++------ 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 1e0376f8..1849eb92 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -19,8 +19,8 @@ android { applicationId "xyz.quaver.pupil" minSdkVersion 16 targetSdkVersion 29 - versionCode 42 - versionName "4.6" + versionCode 43 + versionName "4.7-beta1" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" multiDexEnabled true vectorDrawables.useSupportLibrary = true diff --git a/app/release/output.json b/app/release/output.json index e58db7af..a2fe94d2 100644 --- a/app/release/output.json +++ b/app/release/output.json @@ -1 +1 @@ -[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":42,"versionName":"4.6","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}] \ No newline at end of file +[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":43,"versionName":"4.7-beta1","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}] \ No newline at end of file diff --git a/app/src/main/java/xyz/quaver/pupil/adapters/ReaderAdapter.kt b/app/src/main/java/xyz/quaver/pupil/adapters/ReaderAdapter.kt index d271f22e..2b75fe4d 100644 --- a/app/src/main/java/xyz/quaver/pupil/adapters/ReaderAdapter.kt +++ b/app/src/main/java/xyz/quaver/pupil/adapters/ReaderAdapter.kt @@ -119,8 +119,9 @@ class ReaderAdapter(private val context: Context, holder.view.reader_index.text = (position+1).toString() val images = Cache(context).getImage(galleryID, position) + val progress = DownloadWorker.getInstance(context).progress[galleryID]?.get(position) - if (images != null) { + if (progress?.isInfinite() == true && images != null) { glide .load(images) .diskCacheStrategy(DiskCacheStrategy.NONE) @@ -132,8 +133,6 @@ class ReaderAdapter(private val context: Context, } .into(holder.view.image) } else { - val progress = DownloadWorker.getInstance(context).progress[galleryID]?.get(position) - if (progress?.isNaN() == true) { if (Fabric.isInitialized()) Crashlytics.logException(DownloadWorker.getInstance(context).exception[galleryID]?.get(position)) 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..195917c6 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 @@ -27,6 +27,7 @@ import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.async import kotlinx.coroutines.withContext +import kotlinx.io.InputStream import xyz.quaver.Code import xyz.quaver.hitomi.GalleryBlock import xyz.quaver.hitomi.Reader @@ -34,6 +35,7 @@ import xyz.quaver.pupil.util.getCachedGallery import xyz.quaver.pupil.util.getDownloadDirectory import xyz.quaver.pupil.util.json import java.io.File +import java.io.FileOutputStream import java.net.URL class Cache(context: Context) : ContextWrapper(context) { @@ -212,16 +214,13 @@ class Cache(context: Context) : ContextWrapper(context) { return null } - fun putImage(galleryID: Int, name: String, data: ByteArray) { - val cache = File(getCachedGallery(galleryID), name).also { + fun putImage(galleryID: Int, index: Int, ext: String, data: InputStream) { + val cache = File(getCachedGallery(galleryID), "%05d.$ext".format(index)).also { if (!it.exists()) it.createNewFile() } - if (!Regex("""^[0-9]+.+$""").matches(name)) - throw IllegalArgumentException("File name is not a number") - - cache.writeBytes(data) + data.copyTo(FileOutputStream(cache)) } fun moveToDownload(galleryID: Int) { From 9d0898b26c8fd689af14848426b06877d21961f6 Mon Sep 17 00:00:00 2001 From: Pupil Date: Sun, 23 Feb 2020 10:30:57 +0900 Subject: [PATCH 3/6] Fixed image loading bug --- .../pupil/util/download/DownloadWorker.kt | 69 ++++++++++++------- 1 file changed, 44 insertions(+), 25 deletions(-) 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..f0ac1864 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 @@ -46,6 +47,7 @@ import xyz.quaver.pupil.ui.ReaderActivity import java.io.IOException import java.util.concurrent.Executors import java.util.concurrent.LinkedBlockingQueue +import java.util.concurrent.TimeUnit @UseExperimental(ExperimentalCoroutinesApi::class) class DownloadWorker private constructor(context: Context) : ContextWrapper(context) { @@ -153,12 +155,14 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont val response = chain.proceed(request) response.newBuilder() - .body(ProgressResponseBody(request.tag(), response.body(), progressListener)) - .build() + .body(ProgressResponseBody(request.tag(), response.body(), progressListener)) + .build() } fun buildClient() = OkHttpClient.Builder() .addInterceptor(interceptor) + .connectTimeout(0, TimeUnit.SECONDS) + .readTimeout(0, TimeUnit.SECONDS) .dispatcher(Dispatcher(Executors.newFixedThreadPool(4))) .proxy(proxy) .build() @@ -279,6 +283,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 +292,57 @@ 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 { + val ext = call.request().url().encodedPath().split('.').last() - notify(galleryID) + response.body().use { + Cache(this@DownloadWorker).putImage(galleryID, i, ext, it.byteStream()) + } + 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)})") + } } } @@ -354,7 +373,7 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont } val pendingIntent = TaskStackBuilder.create(this).run { addNextIntentWithParentStack(intent) - getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT) + getPendingIntent(galleryID, PendingIntent.FLAG_UPDATE_CURRENT) } notification.put(galleryID, NotificationCompat.Builder(this, "download").apply { @@ -369,7 +388,7 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont private fun loop() = CoroutineScope(Dispatchers.Default).launch { while (true) { - if (queue.isEmpty() || clients.size() > preferences.getInt("max_download", 4)) + if (queue.isEmpty() || clients.size() >= preferences.getInt("max_download", 4)) continue val galleryID = queue.poll() ?: continue From 03444f070f4c41a227859e458d44642823e9634f Mon Sep 17 00:00:00 2001 From: Pupil Date: Sun, 23 Feb 2020 10:40:09 +0900 Subject: [PATCH 4/6] App built From 9bddf95013a6947f0ad5060107f8844b4aa99dfa Mon Sep 17 00:00:00 2001 From: Pupil Date: Sun, 23 Feb 2020 21:18:19 +0900 Subject: [PATCH 5/6] Image loading fixed --- app/build.gradle | 2 +- app/release/output.json | 2 +- .../quaver/pupil/adapters/ReaderAdapter.kt | 2 ++ .../java/xyz/quaver/pupil/ui/MainActivity.kt | 4 +++- .../xyz/quaver/pupil/util/download/Cache.kt | 24 +++++++++++++++++-- .../pupil/util/download/DownloadWorker.kt | 14 ++++++----- app/src/main/res/layout/item_reader.xml | 3 +++ 7 files changed, 40 insertions(+), 11 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 1849eb92..4cb4355a 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,7 +20,7 @@ android { minSdkVersion 16 targetSdkVersion 29 versionCode 43 - versionName "4.7-beta1" + versionName "4.7-beta2" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" multiDexEnabled true vectorDrawables.useSupportLibrary = true diff --git a/app/release/output.json b/app/release/output.json index a2fe94d2..a14f3503 100644 --- a/app/release/output.json +++ b/app/release/output.json @@ -1 +1 @@ -[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":43,"versionName":"4.7-beta1","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}] \ No newline at end of file +[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":43,"versionName":"4.7-beta2","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}] \ No newline at end of file diff --git a/app/src/main/java/xyz/quaver/pupil/adapters/ReaderAdapter.kt b/app/src/main/java/xyz/quaver/pupil/adapters/ReaderAdapter.kt index 2b75fe4d..ef560f17 100644 --- a/app/src/main/java/xyz/quaver/pupil/adapters/ReaderAdapter.kt +++ b/app/src/main/java/xyz/quaver/pupil/adapters/ReaderAdapter.kt @@ -122,6 +122,8 @@ class ReaderAdapter(private val context: Context, val progress = DownloadWorker.getInstance(context).progress[galleryID]?.get(position) if (progress?.isInfinite() == true && images != null) { + holder.view.reader_item_progressbar.visibility = View.GONE + glide .load(images) .diskCacheStrategy(DiskCacheStrategy.NONE) diff --git a/app/src/main/java/xyz/quaver/pupil/ui/MainActivity.kt b/app/src/main/java/xyz/quaver/pupil/ui/MainActivity.kt index 97f50840..6202ab8e 100644 --- a/app/src/main/java/xyz/quaver/pupil/ui/MainActivity.kt +++ b/app/src/main/java/xyz/quaver/pupil/ui/MainActivity.kt @@ -961,7 +961,9 @@ class MainActivity : AppCompatActivity() { } Mode.DOWNLOAD -> { val downloads = getDownloadDirectory(this@MainActivity).listFiles()?.filter { file -> - file.isDirectory && (file.name.toIntOrNull() != null) && File(file, ".metadata").exists() + file.isDirectory && file.name.toIntOrNull() != null + }?.sortedByDescending { + it.lastModified() }?.map { it.name.toInt() } ?: emptyList() 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 c8d1b8b6..ddca6c04 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 @@ -21,6 +21,7 @@ package xyz.quaver.pupil.util.download import android.content.Context import android.content.ContextWrapper import android.util.Base64 +import android.util.SparseArray import androidx.preference.PreferenceManager import com.crashlytics.android.Crashlytics import kotlinx.coroutines.CoroutineScope @@ -38,9 +39,25 @@ import xyz.quaver.pupil.util.json import java.io.File import java.io.FileOutputStream import java.net.URL +import java.util.concurrent.locks.Lock +import java.util.concurrent.locks.ReentrantLock class Cache(context: Context) : ContextWrapper(context) { + private val locks = SparseArray() + private fun lock(galleryID: Int) { + synchronized(locks) { + if (locks.indexOfKey(galleryID) < 0) + locks.put(galleryID, ReentrantLock()) + } + + locks[galleryID].lock() + } + + private fun unlock(galleryID: Int) { + locks[galleryID]?.unlock() + } + private val preference = PreferenceManager.getDefaultSharedPreferences(this) // Search in this order @@ -217,13 +234,16 @@ class Cache(context: Context) : ContextWrapper(context) { return null } + fun putImage(galleryID: Int, index: Int, ext: String, data: InputStream) { val cache = File(getCachedGallery(galleryID), "%05d.$ext".format(index)).also { if (!it.exists()) it.createNewFile() } - data.copyTo(FileOutputStream(cache)) + data.use { + it.copyTo(FileOutputStream(cache)) + } } fun moveToDownload(galleryID: Int) { @@ -233,7 +253,7 @@ class Cache(context: Context) : ContextWrapper(context) { } val download = File(getDownloadDirectory(this), galleryID.toString()) - cache.copyRecursively(download, true) + cache.copyRecursively(download, true) { _, _ -> OnErrorAction.SKIP } cache.deleteRecursively() } 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 f0ac1864..9af62271 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 @@ -37,13 +37,14 @@ import okio.* import xyz.quaver.Code import xyz.quaver.hitomi.Reader import xyz.quaver.hitomi.getReferer -import xyz.quaver.hitomi.urlFromUrlFromHash +import xyz.quaver.hitomi.imageUrlFromImage import xyz.quaver.hiyobi.cookie import xyz.quaver.hiyobi.createImgList import xyz.quaver.hiyobi.user_agent import xyz.quaver.proxy import xyz.quaver.pupil.R import xyz.quaver.pupil.ui.ReaderActivity +import java.io.File import java.io.IOException import java.util.concurrent.Executors import java.util.concurrent.LinkedBlockingQueue @@ -215,10 +216,10 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont when (reader.code) { Code.HITOMI -> { url( - urlFromUrlFromHash( + imageUrlFromImage( galleryID, reader.galleryInfo.files[index], - if (lowQuality) "webp" else null + lowQuality ) ) addHeader("Referer", getReferer(galleryID)) @@ -256,7 +257,7 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont val cache = Cache(this@DownloadWorker).getImages(galleryID) progress.put(galleryID, reader.galleryInfo.files.indices.map { index -> - if (cache?.getOrNull(index) != null) + if (cache?.firstOrNull { it?.nameWithoutExtension?.toIntOrNull() == index } != null) Float.POSITIVE_INFINITY else 0F @@ -308,9 +309,9 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont override fun onResponse(call: Call, response: Response) { Log.i("PUPILD", "OK ${call.request().tag()}") - try { - val ext = call.request().url().encodedPath().split('.').last() + val ext = call.request().url().encodedPath().split('.').last() + try { response.body().use { Cache(this@DownloadWorker).putImage(galleryID, i, ext, it.byteStream()) } @@ -332,6 +333,7 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont Log.i("PUPILD", "SUCCESS ${call.request().tag()}") } catch (e: Exception) { + File(Cache(this@DownloadWorker).getCachedGallery(galleryID), "%05d.$ext".format(i)).delete() Log.i("PUPILD", "FAIL ON OK ${call.request().tag()} (${e.message})") } } diff --git a/app/src/main/res/layout/item_reader.xml b/app/src/main/res/layout/item_reader.xml index 60a1f261..dc1a1359 100644 --- a/app/src/main/res/layout/item_reader.xml +++ b/app/src/main/res/layout/item_reader.xml @@ -27,8 +27,11 @@ android:id="@+id/container" android:layout_width="match_parent" android:layout_height="0dp" + android:maxHeight="5000dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintLeft_toLeftOf="parent" + app:layout_constraintRight_toRightOf="parent" android:background="@drawable/reader_item_boundary"> Date: Mon, 24 Feb 2020 12:49:19 +0900 Subject: [PATCH 6/6] Bug fix --- app/build.gradle | 2 +- app/release/output.json | 2 +- .../quaver/pupil/adapters/ReaderAdapter.kt | 20 ++++++++- .../pupil/util/download/DownloadWorker.kt | 42 ++++++++++++++++--- app/src/main/res/layout/item_reader.xml | 1 - 5 files changed, 58 insertions(+), 9 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 4cb4355a..a9284b02 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -20,7 +20,7 @@ android { minSdkVersion 16 targetSdkVersion 29 versionCode 43 - versionName "4.7-beta2" + versionName "4.7" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" multiDexEnabled true vectorDrawables.useSupportLibrary = true diff --git a/app/release/output.json b/app/release/output.json index a14f3503..cc20512c 100644 --- a/app/release/output.json +++ b/app/release/output.json @@ -1 +1 @@ -[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":43,"versionName":"4.7-beta2","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}] \ No newline at end of file +[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":43,"versionName":"4.7","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}] \ No newline at end of file diff --git a/app/src/main/java/xyz/quaver/pupil/adapters/ReaderAdapter.kt b/app/src/main/java/xyz/quaver/pupil/adapters/ReaderAdapter.kt index ef560f17..c0ec6ce1 100644 --- a/app/src/main/java/xyz/quaver/pupil/adapters/ReaderAdapter.kt +++ b/app/src/main/java/xyz/quaver/pupil/adapters/ReaderAdapter.kt @@ -19,6 +19,7 @@ package xyz.quaver.pupil.adapters import android.content.Context +import android.graphics.BitmapFactory import android.view.LayoutInflater import android.view.View import android.view.ViewGroup @@ -122,12 +123,23 @@ class ReaderAdapter(private val context: Context, val progress = DownloadWorker.getInstance(context).progress[galleryID]?.get(position) if (progress?.isInfinite() == true && images != null) { - holder.view.reader_item_progressbar.visibility = View.GONE + holder.view.reader_item_progressbar.visibility = View.INVISIBLE + holder.view.container.apply { + val options = BitmapFactory.Options().apply { + inJustDecodeBounds = true + } + + BitmapFactory.decodeFile(images.canonicalPath, options) + + maxWidth = options.outWidth + maxHeight = options.outHeight + } glide .load(images) .diskCacheStrategy(DiskCacheStrategy.NONE) .skipMemoryCache(true) + .dontTransform() .error(R.drawable.image_broken_variant) .apply { if (BuildConfig.CENSOR) @@ -135,6 +147,12 @@ class ReaderAdapter(private val context: Context, } .into(holder.view.image) } else { + holder.view.reader_item_progressbar.visibility = View.VISIBLE + holder.view.container.apply { + maxWidth = Integer.MAX_VALUE + maxHeight = Integer.MAX_VALUE + } + if (progress?.isNaN() == true) { if (Fabric.isInitialized()) Crashlytics.logException(DownloadWorker.getInstance(context).exception[galleryID]?.get(position)) 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 9af62271..2d54ef0c 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 @@ -333,7 +333,26 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont Log.i("PUPILD", "SUCCESS ${call.request().tag()}") } catch (e: Exception) { + + progress[galleryID]?.set(i, Float.NaN) + exception[galleryID]?.set(i, e) + + 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) + } + } + } + } + File(Cache(this@DownloadWorker).getCachedGallery(galleryID), "%05d.$ext".format(i)).delete() + Log.i("PUPILD", "FAIL ON OK ${call.request().tag()} (${e.message})") } } @@ -352,13 +371,17 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont val max = progress[galleryID]?.size ?: 0 val progress = progress[galleryID]?.count { !it.isFinite() } ?: 0 - if (isCompleted(galleryID)) + Log.i("PUPILD", "NOTIFY $galleryID ${isCompleted(galleryID)} $progress/$max") + + if (isCompleted(galleryID)) { notification[galleryID] ?.setContentText(getString(R.string.reader_notification_complete)) ?.setSmallIcon(android.R.drawable.stat_sys_download_done) ?.setProgress(0, 0, false) ?.setOngoing(false) - else + + notificationManager.cancel(galleryID) + } else notification[galleryID] ?.setProgress(max, progress, false) ?.setContentText("$progress/$max") @@ -390,18 +413,27 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont private fun loop() = CoroutineScope(Dispatchers.Default).launch { while (true) { - if (queue.isEmpty() || clients.size() >= preferences.getInt("max_download", 4)) + if (queue.isEmpty()) continue - val galleryID = queue.poll() ?: continue + val galleryID = queue.peek() ?: continue if (clients.indexOfKey(galleryID) >= 0) // Gallery already downloading! continue - initNotification(galleryID) + if (notification[galleryID] == null) + initNotification(galleryID) + if (Cache(this@DownloadWorker).isDownloading(galleryID)) notificationManager.notify(galleryID, notification[galleryID].build()) + + if (clients.size() >= preferences.getInt("max_download", 4)) + continue + + Log.i("PUPILD", "QUEUED $galleryID #${clients.size()+1}") + worker.put(galleryID, download(galleryID)) + queue.poll() } } diff --git a/app/src/main/res/layout/item_reader.xml b/app/src/main/res/layout/item_reader.xml index dc1a1359..01a50804 100644 --- a/app/src/main/res/layout/item_reader.xml +++ b/app/src/main/res/layout/item_reader.xml @@ -27,7 +27,6 @@ android:id="@+id/container" android:layout_width="match_parent" android:layout_height="0dp" - android:maxHeight="5000dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent"