From 065845f1be20debf5cd8b18257e1815b293b8a4e Mon Sep 17 00:00:00 2001 From: tom5079 Date: Sun, 21 Jun 2020 14:45:57 +0900 Subject: [PATCH] Cache disable setting added --- .../pupil/adapters/GalleryBlockAdapter.kt | 3 +- .../quaver/pupil/adapters/ReaderAdapter.kt | 14 +++-- .../java/xyz/quaver/pupil/ui/MainActivity.kt | 18 +++--- .../xyz/quaver/pupil/ui/ReaderActivity.kt | 17 ++++-- .../xyz/quaver/pupil/util/download/Cache.kt | 60 ++++++++++++------- .../pupil/util/download/DownloadWorker.kt | 15 ++++- .../main/java/xyz/quaver/pupil/util/update.kt | 3 +- app/src/main/res/values-ja/strings.xml | 2 + app/src/main/res/values-ko/strings.xml | 2 + app/src/main/res/values/strings.xml | 2 + app/src/main/res/xml/root_preferences.xml | 4 ++ 11 files changed, 93 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/xyz/quaver/pupil/adapters/GalleryBlockAdapter.kt b/app/src/main/java/xyz/quaver/pupil/adapters/GalleryBlockAdapter.kt index 1cdf9301..c795763c 100644 --- a/app/src/main/java/xyz/quaver/pupil/adapters/GalleryBlockAdapter.kt +++ b/app/src/main/java/xyz/quaver/pupil/adapters/GalleryBlockAdapter.kt @@ -28,6 +28,7 @@ import android.view.ViewGroup import android.widget.LinearLayout import androidx.cardview.widget.CardView import androidx.core.content.ContextCompat +import androidx.preference.PreferenceManager import androidx.recyclerview.widget.RecyclerView import androidx.swiperefreshlayout.widget.CircularProgressDrawable import androidx.vectordrawable.graphics.drawable.Animatable2Compat @@ -77,7 +78,7 @@ class GalleryBlockAdapter(private val glide: RequestManager, private val galleri val reader = Cache(context).getReaderOrNull(galleryID) CoroutineScope(Dispatchers.Main).launch { - if (reader == null) { + if (reader == null || PreferenceManager.getDefaultSharedPreferences(context).getBoolean("cache_disable", false)) { view.galleryblock_progressbar.visibility = View.GONE view.galleryblock_progress_complete.visibility = View.GONE return@launch 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 f6b90722..5869043e 100644 --- a/app/src/main/java/xyz/quaver/pupil/adapters/ReaderAdapter.kt +++ b/app/src/main/java/xyz/quaver/pupil/adapters/ReaderAdapter.kt @@ -32,7 +32,6 @@ import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import xyz.quaver.hitomi.Reader import xyz.quaver.pupil.R -import xyz.quaver.pupil.util.download.Cache import xyz.quaver.pupil.util.download.DownloadWorker import java.util.* import kotlin.concurrent.schedule @@ -50,6 +49,8 @@ class ReaderAdapter(private val glide: RequestManager, class ViewHolder(val view: View) : RecyclerView.ViewHolder(view) + var downloadWorker: DownloadWorker? = null + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { return LayoutInflater.from(parent.context).inflate( R.layout.item_reader, parent, false @@ -61,6 +62,9 @@ class ReaderAdapter(private val glide: RequestManager, override fun onBindViewHolder(holder: ViewHolder, position: Int) { holder.view as ConstraintLayout + if (downloadWorker == null) + downloadWorker = DownloadWorker.getInstance(holder.view.context) + if (isFullScreen) { holder.view.layoutParams.height = RecyclerView.LayoutParams.MATCH_PARENT holder.view.container.layoutParams.height = ConstraintLayout.LayoutParams.MATCH_PARENT @@ -82,15 +86,15 @@ class ReaderAdapter(private val glide: RequestManager, holder.view.reader_index.text = (position+1).toString() - val images = Cache(holder.view.context).getImage(galleryID, position) - val progress = DownloadWorker.getInstance(holder.view.context).progress[galleryID]?.get(position) + val image = downloadWorker!!.results[galleryID]?.get(position) + val progress = downloadWorker!!.progress[galleryID]?.get(position) - if (progress?.isInfinite() == true && images != null) { + if (progress?.isInfinite() == true && image != null) { holder.view.reader_item_progressbar.visibility = View.INVISIBLE holder.view.image.post { glide - .load(images) + .load(image) .diskCacheStrategy(DiskCacheStrategy.NONE) .skipMemoryCache(true) .fitCenter() 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 20e6e9b3..172f4896 100644 --- a/app/src/main/java/xyz/quaver/pupil/ui/MainActivity.kt +++ b/app/src/main/java/xyz/quaver/pupil/ui/MainActivity.kt @@ -31,10 +31,7 @@ import android.view.MotionEvent import android.view.View import android.view.WindowManager import android.view.inputmethod.EditorInfo -import android.widget.EditText -import android.widget.ImageView -import android.widget.LinearLayout -import android.widget.TextView +import android.widget.* import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.cardview.widget.CardView @@ -439,13 +436,16 @@ class MainActivity : AppCompatActivity() { onDownloadClickedHandler = { position -> val galleryID = galleries[position].id val worker = DownloadWorker.getInstance(context) - - if (Cache(context).isDownloading(galleryID)) //download in progress - worker.cancel(galleryID) + if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean("cache_disable", false)) + Toast.makeText(context, R.string.settings_download_when_cache_disable_warning, Toast.LENGTH_SHORT).show() else { - Cache(context).setDownloading(galleryID, true) + if (Cache(context).isDownloading(galleryID)) //download in progress + worker.cancel(galleryID) + else { + Cache(context).setDownloading(galleryID, true) - worker.queue.add(galleryID) + worker.queue.add(galleryID) + } } closeAllItems() diff --git a/app/src/main/java/xyz/quaver/pupil/ui/ReaderActivity.kt b/app/src/main/java/xyz/quaver/pupil/ui/ReaderActivity.kt index 4bd346f4..14306eea 100644 --- a/app/src/main/java/xyz/quaver/pupil/ui/ReaderActivity.kt +++ b/app/src/main/java/xyz/quaver/pupil/ui/ReaderActivity.kt @@ -23,6 +23,7 @@ import android.graphics.drawable.Animatable import android.graphics.drawable.Drawable import android.os.Bundle import android.view.* +import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat @@ -321,13 +322,17 @@ class ReaderActivity : AppCompatActivity() { animateDownloadFAB(Cache(context).isDownloading(galleryID)) //If download in progress, animate button setOnClickListener { - if (Cache(context).isDownloading(galleryID)) { - Cache(context).setDownloading(galleryID, false) + if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean("cache_disable", false)) + Toast.makeText(context, R.string.settings_download_when_cache_disable_warning, Toast.LENGTH_SHORT).show() + else { + if (Cache(context).isDownloading(galleryID)) { + Cache(context).setDownloading(galleryID, false) - animateDownloadFAB(false) - } else { - Cache(context).setDownloading(galleryID, true) - animateDownloadFAB(true) + animateDownloadFAB(false) + } else { + Cache(context).setDownloading(galleryID, true) + animateDownloadFAB(true) + } } } } 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 c1e53abd..3b78315b 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 @@ -34,10 +34,8 @@ import xyz.quaver.pupil.util.getCachedGallery import xyz.quaver.pupil.util.getDownloadDirectory import xyz.quaver.pupil.util.isParentOf import xyz.quaver.pupil.util.json -import java.io.BufferedInputStream import java.io.File import java.io.FileOutputStream -import java.io.InputStream import java.net.URL import java.util.* import java.util.concurrent.locks.Lock @@ -47,6 +45,7 @@ class Cache(context: Context) : ContextWrapper(context) { companion object { private val moving = mutableListOf() + private val readers = SparseArray() } private val locks = SparseArray() @@ -68,7 +67,7 @@ class Cache(context: Context) : ContextWrapper(context) { // Search in this order // Download -> Cache fun getCachedGallery(galleryID: Int) = getCachedGallery(this, galleryID).also { - if (!it.exists()) + if (!it.exists() && !preference.getBoolean("cache_disable", false)) it.mkdirs() } @@ -88,6 +87,9 @@ class Cache(context: Context) : ContextWrapper(context) { } fun setCachedMetadata(galleryID: Int, metadata: Metadata) { + if (preference.getBoolean("cache_disable", false)) + return + val file = File(getCachedGallery(galleryID), ".metadata").also { if (!it.exists()) it.createNewFile() @@ -99,6 +101,7 @@ class Cache(context: Context) : ContextWrapper(context) { suspend fun getThumbnail(galleryID: Int): String? { val metadata = Cache(this).getCachedMetadata(galleryID) + @Suppress("BlockingMethodInNonBlockingContext") val thumbnail = if (metadata?.thumbnail == null) withContext(Dispatchers.IO) { val thumbnails = getGalleryBlock(galleryID)?.thumbnails @@ -159,7 +162,7 @@ class Cache(context: Context) : ContextWrapper(context) { } fun getReaderOrNull(galleryID: Int): Reader? { - return getCachedMetadata(galleryID)?.reader + return readers[galleryID] ?: getCachedMetadata(galleryID)?.reader } suspend fun getReader(galleryID: Int): Reader? { @@ -180,28 +183,33 @@ class Cache(context: Context) : ContextWrapper(context) { it } - val reader = if (metadata?.reader == null) { - var retval: Reader? = null + val reader = + if (readers[galleryID] != null) + return readers[galleryID] + else if (metadata?.reader == null) { + var retval: Reader? = null - for (source in sources) { - retval = try { - withContext(Dispatchers.IO) { - withTimeoutOrNull(1000) { - source.value.invoke() + for (source in sources) { + retval = try { + withContext(Dispatchers.IO) { + withTimeoutOrNull(1000) { + source.value.invoke() + } } + } catch (e: Exception) { + FirebaseCrashlytics.getInstance().recordException(e) + null } - } catch (e: Exception) { - FirebaseCrashlytics.getInstance().recordException(e) - null + + if (retval != null) + break } - if (retval != null) - break - } + retval + } else + metadata.reader - retval - } else - metadata.reader + readers.put(galleryID, reader) setCachedMetadata( galleryID, @@ -242,18 +250,24 @@ class Cache(context: Context) : ContextWrapper(context) { } - fun putImage(galleryID: Int, index: Int, ext: String, data: InputStream) { + fun putImage(galleryID: Int, index: Int, ext: String, data: ByteArray) { + if (preference.getBoolean("cache_disable", false)) + return + val cache = File(getCachedGallery(galleryID), "%05d.$ext".format(index)).also { if (!it.exists()) it.createNewFile() } - BufferedInputStream(data).use { - it.copyTo(FileOutputStream(cache)) + FileOutputStream(cache).use { + it.write(data) } } fun moveToDownload(galleryID: Int) { + if (preference.getBoolean("cache_disable", false)) + return + if (moving.contains(galleryID)) return 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 29c23471..63226ab6 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 @@ -143,6 +143,7 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont * null -> Download in progress / Loading */ val exception = SparseArray?>() + val results = SparseArray?>() val notification = SparseArray() private val loop = loop() @@ -189,6 +190,7 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont progress.clear() exception.clear() + results.clear() notification.clear() notificationManager.cancelAll() } @@ -205,6 +207,7 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont progress.remove(galleryID) exception.remove(galleryID) + results.remove(galleryID) notification.remove(galleryID) notificationManager.cancel(galleryID) @@ -253,6 +256,7 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont if (reader == null) { progress.put(galleryID, null) exception.put(galleryID, null) + results.put(galleryID, null) Cache(this@DownloadWorker).setDownloading(galleryID, false) return@launch @@ -267,6 +271,9 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont 0F }.toMutableList()) exception.put(galleryID, reader.galleryInfo.files.map { null }.toMutableList()) + results.put(galleryID, reader.galleryInfo.files.indices.map { index -> + cache?.firstOrNull { it?.nameWithoutExtension?.toIntOrNull() == index }?.readBytes() + }.toMutableList()) if (notification[galleryID] == null) initNotification(galleryID) @@ -316,13 +323,19 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont try { response.body().use { - Cache(this@DownloadWorker).putImage(galleryID, i, ext, it!!.byteStream()) + it!! + + results[galleryID]?.set(i, it.source().readByteArray()) } progress[galleryID]?.set(i, Float.POSITIVE_INFINITY) notify(galleryID) CoroutineScope(Dispatchers.IO).launch { + results[galleryID]?.get(i)?.also { + Cache(this@DownloadWorker).putImage(galleryID, i, ext, it) + } + if (isCompleted(galleryID)) { with(Cache(this@DownloadWorker)) { if (isDownloading(galleryID)) { diff --git a/app/src/main/java/xyz/quaver/pupil/util/update.kt b/app/src/main/java/xyz/quaver/pupil/util/update.kt index f56a0540..406837cd 100644 --- a/app/src/main/java/xyz/quaver/pupil/util/update.kt +++ b/app/src/main/java/xyz/quaver/pupil/util/update.kt @@ -50,7 +50,6 @@ import java.io.File import java.io.IOException import java.net.URL import java.util.* -import java.util.concurrent.Executors import java.util.concurrent.TimeUnit fun getReleases(url: String) : JsonArray { @@ -320,7 +319,7 @@ fun importOldGalleries(context: Context, folder: File) = CoroutineScope(Dispatch @Suppress("NAME_SHADOWING") val index = it.nameWithoutExtension.toIntOrNull() ?: return@forEach - Cache(context).putImage(galleryID, index, it.extension, it.inputStream()) + Cache(context).putImage(galleryID, index, it.extension, it.readBytes()) } } diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index 9cea3240..3c2e9311 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -145,4 +145,6 @@ Pupil指紋ロック™ こうかはばつぐんだ! 登場人物を全て18歳以上にする + キャッシュを使用しない + キャッシュを使用しないため、ダウンロードできません \ No newline at end of file diff --git a/app/src/main/res/values-ko/strings.xml b/app/src/main/res/values-ko/strings.xml index ddce14a2..e8aa216c 100644 --- a/app/src/main/res/values-ko/strings.xml +++ b/app/src/main/res/values-ko/strings.xml @@ -145,4 +145,6 @@ Pupil 지문 인식™ 힘세고 강한 지문 인식 히익 페도 + 캐시 비활성화 + 캐시를 활성화 해야 다운로드를 진행할 수 있습니다 \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 39c6f654..3fbdaa51 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -153,6 +153,8 @@ %s available Custom Location This folder is not writable. Please select another folder. + Disable Cache + Download is disabled when the cache is disabled Low quality images Load low quality images to improve load speed and data usage diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml index cd28fb7b..89a0203c 100644 --- a/app/src/main/res/xml/root_preferences.xml +++ b/app/src/main/res/xml/root_preferences.xml @@ -47,6 +47,10 @@ app:key="dl_location" app:title="@string/settings_dl_location"/> + +