From 5634e94f3eaf9719a4e452b38102e47e8aa836f7 Mon Sep 17 00:00:00 2001 From: Pupil Date: Mon, 10 Feb 2020 16:28:13 +0900 Subject: [PATCH 1/3] DocumentFileX --- app/build.gradle | 7 +- .../quaver/pupil/util/saf/DocumentFileX.kt | 52 ++++++++ .../quaver/pupil/util/saf/RawDocumentFileX.kt | 84 +++++++++++++ .../pupil/util/saf/TreeDocumentFileX.kt | 118 ++++++++++++++++++ 4 files changed, 256 insertions(+), 5 deletions(-) create mode 100644 app/src/main/java/xyz/quaver/pupil/util/saf/DocumentFileX.kt create mode 100644 app/src/main/java/xyz/quaver/pupil/util/saf/RawDocumentFileX.kt create mode 100644 app/src/main/java/xyz/quaver/pupil/util/saf/TreeDocumentFileX.kt diff --git a/app/build.gradle b/app/build.gradle index 5df53c31..be2fd917 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -60,9 +60,6 @@ dependencies { implementation 'androidx.constraintlayout:constraintlayout:1.1.3' implementation 'androidx.preference:preference:1.1.0' implementation 'androidx.gridlayout:gridlayout:1.0.0' - implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0' - implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.2.0' - implementation 'androidx.legacy:legacy-support-v4:1.0.0' implementation "androidx.biometric:biometric:1.0.1" implementation 'com.android.support:multidex:1.0.3' implementation "com.daimajia.swipelayout:library:1.2.0@aar" @@ -72,7 +69,7 @@ dependencies { implementation 'com.crashlytics.sdk.android:crashlytics:2.10.1' implementation 'com.github.arimorty:floatingsearchview:2.1.1' implementation 'com.github.clans:fab:1.6.4' - implementation 'com.github.bumptech.glide:glide:4.10.0' + implementation 'com.github.bumptech.glide:glide:4.11.0' implementation('com.github.bumptech.glide:recyclerview-integration:4.11.0') { transitive = false } @@ -81,7 +78,7 @@ dependencies { implementation 'com.github.chrisbanes:PhotoView:2.3.0' implementation 'com.andrognito.patternlockview:patternlockview:1.0.0' implementation "ru.noties.markwon:core:${markwonVersion}" - kapt 'com.github.bumptech.glide:compiler:4.10.0' + kapt 'com.github.bumptech.glide:compiler:4.11.0' testImplementation 'junit:junit:4.13' androidTestImplementation 'androidx.test.ext:junit:1.1.1' androidTestImplementation 'androidx.test:rules:1.2.0' diff --git a/app/src/main/java/xyz/quaver/pupil/util/saf/DocumentFileX.kt b/app/src/main/java/xyz/quaver/pupil/util/saf/DocumentFileX.kt new file mode 100644 index 00000000..776c4ea3 --- /dev/null +++ b/app/src/main/java/xyz/quaver/pupil/util/saf/DocumentFileX.kt @@ -0,0 +1,52 @@ +/* + * Pupil, Hitomi.la viewer for Android + * Copyright (C) 2020 tom5079 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package xyz.quaver.pupil.util.saf + +import android.content.Context +import android.net.Uri +import java.io.File + +abstract class DocumentFileX { + + abstract fun createFile(displayName: String): DocumentFileX? + abstract fun createDirectory(displayName: String): DocumentFileX? + abstract fun getUri(): Uri + abstract fun getName(): String + abstract fun getParentFile(): DocumentFileX? + abstract fun isDirectory(): Boolean + abstract fun isFile(): Boolean + abstract fun length(): Long + abstract fun canRead(): Boolean + abstract fun canWrite(): Boolean + abstract fun delete(): Boolean + abstract fun exists(): Boolean + abstract fun listFiles(): List? + abstract fun findFile(displayName: String): DocumentFileX? + + companion object { + fun fromFile(file: File): DocumentFileX { + return RawDocumentFileX(file) + } + + fun fromTreeUri(context: Context, uri: Uri): DocumentFileX { + return TreeDocumentFileX(context, uri) + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/xyz/quaver/pupil/util/saf/RawDocumentFileX.kt b/app/src/main/java/xyz/quaver/pupil/util/saf/RawDocumentFileX.kt new file mode 100644 index 00000000..fa8d754e --- /dev/null +++ b/app/src/main/java/xyz/quaver/pupil/util/saf/RawDocumentFileX.kt @@ -0,0 +1,84 @@ +/* + * Pupil, Hitomi.la viewer for Android + * Copyright (C) 2020 tom5079 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package xyz.quaver.pupil.util.saf + +import android.net.Uri +import java.io.File + +class RawDocumentFileX(private var file: File) : DocumentFileX() { + + override fun createFile(displayName: String) = + File(file, displayName).let { + try { + it.createNewFile() + RawDocumentFileX(it) + } catch (e: Exception) { + null + } + } + + override fun createDirectory(displayName: String) = + File(file, displayName).let { + if (it.isDirectory || it.mkdir()) + RawDocumentFileX(it) + else + null + } + + override fun getUri() = Uri.fromFile(file)!! + + override fun getName() = file.name + + override fun getParentFile() = + file.parentFile.let { + if (it == null) + null + else + RawDocumentFileX(it) + } + + override fun isDirectory() = file.isDirectory + + override fun isFile() = file.isFile + + override fun length() = file.length() + + override fun canRead() = file.canRead() + + override fun canWrite() = file.canWrite() + + override fun delete() = + if (file.isDirectory) + file.deleteRecursively() + else + file.delete() + + override fun exists() = file.exists() + + override fun listFiles() = file.listFiles()?.map { RawDocumentFileX(it) } + + override fun findFile(displayName: String) = + File(file, displayName).let { + if (it.exists()) + RawDocumentFileX(it) + else + null + } + +} \ No newline at end of file diff --git a/app/src/main/java/xyz/quaver/pupil/util/saf/TreeDocumentFileX.kt b/app/src/main/java/xyz/quaver/pupil/util/saf/TreeDocumentFileX.kt new file mode 100644 index 00000000..08978165 --- /dev/null +++ b/app/src/main/java/xyz/quaver/pupil/util/saf/TreeDocumentFileX.kt @@ -0,0 +1,118 @@ +/* + * Pupil, Hitomi.la viewer for Android + * Copyright (C) 2020 tom5079 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package xyz.quaver.pupil.util.saf + +import android.annotation.TargetApi +import android.content.Context +import android.net.Uri +import android.provider.DocumentsContract + +@TargetApi(21) +class TreeDocumentFileX( + private val context: Context, + private val uri: Uri, + private var name: String? = null, + private var documentID: String? = null, + private var length: Long? = null +) : DocumentFileX() { + + init { + val projection = mutableListOf() + + if (name == null) + projection.add(DocumentsContract.Document.COLUMN_DISPLAY_NAME) + if (documentID == null) + projection.add(DocumentsContract.Document.COLUMN_DOCUMENT_ID) + if (length == null) + projection.add(DocumentsContract.Document.COLUMN_SIZE) + + if (projection.isNotEmpty()) { + val contentResolver = context.contentResolver + + contentResolver.query(uri, projection.toTypedArray(), null, null, null).use { cursor -> + while (cursor?.moveToNext() == true) { + cursor.columnNames.forEach { column -> + val index = cursor.getColumnIndex(column) + + when (column) { + DocumentsContract.Document.COLUMN_DISPLAY_NAME -> this.name = cursor.getString(index) + DocumentsContract.Document.COLUMN_DOCUMENT_ID -> this.documentID = cursor.getString(index) + DocumentsContract.Document.COLUMN_SIZE -> this.length = cursor.getLong(index) + } + } + } + } + } + } + + override fun createFile(displayName: String): DocumentFileX? { + val uri = kotlin.runCatching { + DocumentsContract.createDocument(context.contentResolver, uri, "null", displayName) + }.getOrNull() ?: return null + + return TreeDocumentFileX(context, uri, displayName, DocumentsContract.getDocumentId(uri), 0) + } + + override fun createDirectory(displayName: String): DocumentFileX? { + val uri = kotlin.runCatching { + DocumentsContract.createDocument(context.contentResolver, uri, "null", displayName) + }.getOrNull() ?: return null + } + + override fun getUri() = uri + + override fun getName() = name ?: "null" + + override fun getParentFile(): DocumentFileX? + + override fun isDirectory() = name?.contains('.') == false + override fun isFile() = name?.contains('.') == true + + override fun length() = length ?: -1 + + override fun canRead(): Boolean + override fun canWrite(): Boolean + + override fun delete() = + kotlin.runCatching { + DocumentsContract.deleteDocument(context.contentResolver, uri) + }.getOrElse { false } + + override fun exists() = documentID == null + + override fun listFiles(): List? { + val contentResolver = context.contentResolver + val children = DocumentsContract.buildChildDocumentsUriUsingTree(uri, DocumentsContract.getDocumentId(uri)) + + contentResolver.query( + children, + arrayOf( + DocumentsContract.Document.COLUMN_MIME_TYPE + ), + null, + null, + null + ).use { + + } + } + + override fun findFile(displayName: String): DocumentFileX? + +} \ No newline at end of file From a714a8230bc707782905f23f38922443c51dfdde Mon Sep 17 00:00:00 2001 From: Pupil Date: Tue, 11 Feb 2020 21:23:34 +0900 Subject: [PATCH 2/3] ugh --- app/build.gradle | 1 + app/src/main/AndroidManifest.xml | 4 +- app/src/main/java/xyz/quaver/pupil/Pupil.kt | 13 +---- .../xyz/quaver/pupil/ui/SettingsActivity.kt | 4 +- .../pupil/ui/dialog/DownloadLocationDialog.kt | 5 +- .../java/xyz/quaver/pupil/util/ConstValues.kt | 3 +- .../xyz/quaver/pupil/util/download/Cache.kt | 12 ++--- .../pupil/util/download/DownloadWorker.kt | 9 ++-- .../main/java/xyz/quaver/pupil/util/file.kt | 51 +++++++++---------- .../quaver/pupil/util/saf/DocumentFileX.kt | 1 - .../quaver/pupil/util/saf/RawDocumentFileX.kt | 2 +- .../pupil/util/saf/TreeDocumentFileX.kt | 18 +++++-- 12 files changed, 57 insertions(+), 66 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index be2fd917..56855fab 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -61,6 +61,7 @@ dependencies { implementation 'androidx.preference:preference:1.1.0' implementation 'androidx.gridlayout:gridlayout:1.0.0' implementation "androidx.biometric:biometric:1.0.1" + implementation "androidx.documentfile:documentfile:1.0.1" implementation 'com.android.support:multidex:1.0.3' implementation "com.daimajia.swipelayout:library:1.2.0@aar" implementation 'com.google.android.material:material:1.2.0-alpha04' diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 09fe339b..e363d1e3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -6,9 +6,7 @@ - + { if (resultCode == Activity.RESULT_OK) { data?.data?.also { uri -> - val takeFlags: Int = intent.flags and (Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION) - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) - contentResolver.takePersistableUriPermission(uri, takeFlags) + contentResolver.takePersistableUriPermission(uri, Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION) if (DocumentFile.fromTreeUri(this, uri)?.canWrite() == false) Snackbar.make(settings, R.string.settings_dl_location_not_writable, Snackbar.LENGTH_LONG).show() diff --git a/app/src/main/java/xyz/quaver/pupil/ui/dialog/DownloadLocationDialog.kt b/app/src/main/java/xyz/quaver/pupil/ui/dialog/DownloadLocationDialog.kt index d6c2b8b1..9542b981 100644 --- a/app/src/main/java/xyz/quaver/pupil/ui/dialog/DownloadLocationDialog.kt +++ b/app/src/main/java/xyz/quaver/pupil/ui/dialog/DownloadLocationDialog.kt @@ -37,6 +37,7 @@ import xyz.quaver.pupil.R import xyz.quaver.pupil.util.REQUEST_DOWNLOAD_FOLDER import xyz.quaver.pupil.util.REQUEST_DOWNLOAD_FOLDER_OLD import xyz.quaver.pupil.util.byteToString +import xyz.quaver.pupil.util.getDownloadDirectory @SuppressLint("InflateParams") class DownloadLocationDialog(val activity: Activity) : AlertDialog(activity) { @@ -106,9 +107,9 @@ class DownloadLocationDialog(val activity: Activity) : AlertDialog(activity) { buttons.add(button to null) }) - val pref = Uri.parse(preference.getString("dl_location", null)) + val pref = getDownloadDirectory(context) val index = externalFilesDirs.indexOfFirst { - Uri.fromFile(it).toString() == pref.toString() + Uri.fromFile(it).toString() == pref.uri.toString() } if (index < 0) diff --git a/app/src/main/java/xyz/quaver/pupil/util/ConstValues.kt b/app/src/main/java/xyz/quaver/pupil/util/ConstValues.kt index 807c1379..57cbf9c9 100644 --- a/app/src/main/java/xyz/quaver/pupil/util/ConstValues.kt +++ b/app/src/main/java/xyz/quaver/pupil/util/ConstValues.kt @@ -21,4 +21,5 @@ package xyz.quaver.pupil.util const val REQUEST_LOCK = 38238 const val REQUEST_RESTORE = 16546 const val REQUEST_DOWNLOAD_FOLDER = 3874 -const val REQUEST_DOWNLOAD_FOLDER_OLD = 3425 \ No newline at end of file +const val REQUEST_DOWNLOAD_FOLDER_OLD = 3425 +const val REQUEST_DOWNLOAD_FOLDER_AND_PERMISSION = 34257 \ No newline at end of file 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 0224c36d..ff103c63 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 @@ -207,10 +207,7 @@ class Cache(context: Context) : ContextWrapper(context) { throw IllegalArgumentException("File name is not a number") cache.let { - if (it.findFile(name) != null) - it - else - it.createFile("null", name) + it.findFile(name) ?: it.createFile("null", name) }?.writeBytes(this, data) } @@ -221,11 +218,14 @@ class Cache(context: Context) : ContextWrapper(context) { val download = getDownloadDirectory(this) if (!download.isParentOf(cache)) { + val target = getDownloadDirectory(this).let { + it.findFile(galleryID.toString()) ?: it.createDirectory(galleryID.toString()) + } + cache.copyRecursively(this, download) cache.deleteRecursively() } - } else - getDownloadDirectory(this).createDirectory(galleryID.toString()) + } } fun isDownloading(galleryID: Int) = getCachedMetadata(galleryID)?.isDownloading == true 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 647571b3..fa0cd5f0 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 @@ -152,12 +153,6 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont val request = chain.request() var response = chain.proceed(request) - var retry = preferences.getInt("retry", 3) - while (!response.isSuccessful && retry > 0) { - response = chain.proceed(request) - retry-- - } - response.newBuilder() .body(ProgressResponseBody(request.tag(), response.body(), progressListener)) .build() @@ -288,6 +283,7 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont for (i in reader.galleryInfo.indices) { val callback = object : Callback { override fun onFailure(call: Call, e: IOException) { + Log.i("PUPILD", "FAILED $i") if (Fabric.isInitialized()) Crashlytics.logException(e) @@ -307,6 +303,7 @@ class DownloadWorker private constructor(context: Context) : ContextWrapper(cont } override fun onResponse(call: Call, response: Response) { + Log.i("PUPILD", "SUCCESS $i") response.body().use { val res = it.bytes() val ext = diff --git a/app/src/main/java/xyz/quaver/pupil/util/file.kt b/app/src/main/java/xyz/quaver/pupil/util/file.kt index a27629c0..8cf67847 100644 --- a/app/src/main/java/xyz/quaver/pupil/util/file.kt +++ b/app/src/main/java/xyz/quaver/pupil/util/file.kt @@ -33,17 +33,22 @@ fun getCachedGallery(context: Context, galleryID: Int) = DocumentFile.fromFile(File(context.cacheDir, "imageCache/$galleryID")) fun getDownloadDirectory(context: Context) : DocumentFile { - val uri = PreferenceManager.getDefaultSharedPreferences(context).getString("dl_location", null).let { + return runCatching { + val uri = PreferenceManager.getDefaultSharedPreferences(context).getString("dl_location", null).let { if (it != null) Uri.parse(it) else Uri.fromFile(context.getExternalFilesDir(null)) - } + } - return if (uri.toString().startsWith("file")) - DocumentFile.fromFile(File(uri.path!!)) - else - DocumentFile.fromTreeUri(context, uri) ?: DocumentFile.fromFile(context.getExternalFilesDir(null)!!) + if (uri.toString().startsWith("file")) + DocumentFile.fromFile(File(uri.path!!)) + else + DocumentFile.fromTreeUri(context, uri) ?: DocumentFile.fromFile(context.getExternalFilesDir(null)!!) + }.getOrElse { + PreferenceManager.getDefaultSharedPreferences(context).edit().remove("dl_location").apply() + DocumentFile.fromFile(context.getExternalFilesDir(null)!!) + } } fun convertUpdateUri(context: Context, uri: Uri) : Uri = @@ -104,32 +109,26 @@ fun DocumentFile.copyRecursively( if (!exists()) throw Exception("The source file doesn't exist.") - if (this.isFile) { - target.let { - if (it.findFile(name!!) != null) - it - else - createFile("null", name!!)!! - }.writeBytes( - context, - readBytes(context) - ) - } else if (this.isDirectory) { - target.createDirectory(name!!).also { newTarget -> - listFiles().forEach { child -> - child.copyRecursively(context, newTarget!!) + this.listFiles().forEach { child -> + try { + if (child.isFile) { + target.findFile(name!!) ?: target.createFile("null", name!!)!! + .writeBytes( + context, + readBytes(context) + ) + } else if (child.isDirectory) { + target.findFile(name!!) ?: target.createDirectory(name!!).also { newTarget -> + child.copyRecursively(context, newTarget!!) + } } + } catch (e: Exception) { + e.printStackTrace() } } } fun DocumentFile.deleteRecursively() { - - if (this.isDirectory) - listFiles().forEach { - it.deleteRecursively() - } - this.delete() } diff --git a/app/src/main/java/xyz/quaver/pupil/util/saf/DocumentFileX.kt b/app/src/main/java/xyz/quaver/pupil/util/saf/DocumentFileX.kt index 776c4ea3..385515fa 100644 --- a/app/src/main/java/xyz/quaver/pupil/util/saf/DocumentFileX.kt +++ b/app/src/main/java/xyz/quaver/pupil/util/saf/DocumentFileX.kt @@ -28,7 +28,6 @@ abstract class DocumentFileX { abstract fun createDirectory(displayName: String): DocumentFileX? abstract fun getUri(): Uri abstract fun getName(): String - abstract fun getParentFile(): DocumentFileX? abstract fun isDirectory(): Boolean abstract fun isFile(): Boolean abstract fun length(): Long diff --git a/app/src/main/java/xyz/quaver/pupil/util/saf/RawDocumentFileX.kt b/app/src/main/java/xyz/quaver/pupil/util/saf/RawDocumentFileX.kt index fa8d754e..030487de 100644 --- a/app/src/main/java/xyz/quaver/pupil/util/saf/RawDocumentFileX.kt +++ b/app/src/main/java/xyz/quaver/pupil/util/saf/RawDocumentFileX.kt @@ -45,7 +45,7 @@ class RawDocumentFileX(private var file: File) : DocumentFileX() { override fun getName() = file.name - override fun getParentFile() = + fun getParentFile() = file.parentFile.let { if (it == null) null diff --git a/app/src/main/java/xyz/quaver/pupil/util/saf/TreeDocumentFileX.kt b/app/src/main/java/xyz/quaver/pupil/util/saf/TreeDocumentFileX.kt index 08978165..b8f1e209 100644 --- a/app/src/main/java/xyz/quaver/pupil/util/saf/TreeDocumentFileX.kt +++ b/app/src/main/java/xyz/quaver/pupil/util/saf/TreeDocumentFileX.kt @@ -73,21 +73,25 @@ class TreeDocumentFileX( val uri = kotlin.runCatching { DocumentsContract.createDocument(context.contentResolver, uri, "null", displayName) }.getOrNull() ?: return null + + return TreeDocumentFileX(context, uri, displayName, DocumentsContract.getDocumentId(uri), 0) } override fun getUri() = uri override fun getName() = name ?: "null" - override fun getParentFile(): DocumentFileX? - override fun isDirectory() = name?.contains('.') == false override fun isFile() = name?.contains('.') == true override fun length() = length ?: -1 - override fun canRead(): Boolean - override fun canWrite(): Boolean + override fun canRead(): Boolean { + TODO("Not impelmented") + } + override fun canWrite(): Boolean { + TODO("Not impelmented") + } override fun delete() = kotlin.runCatching { @@ -111,8 +115,12 @@ class TreeDocumentFileX( ).use { } + + TODO("Not impelmented") } - override fun findFile(displayName: String): DocumentFileX? + override fun findFile(displayName: String): DocumentFileX? { + TODO("Not impelmented") + } } \ No newline at end of file From 9d1998fe52c4d3f82e4327ced09f093656336166 Mon Sep 17 00:00:00 2001 From: Pupil Date: Wed, 12 Feb 2020 09:03:47 +0900 Subject: [PATCH 3/3] deleted deleteRecursively --- .../main/java/xyz/quaver/pupil/ui/MainActivity.kt | 7 +------ .../xyz/quaver/pupil/ui/fragment/SettingsFragment.kt | 4 ++-- .../java/xyz/quaver/pupil/util/download/Cache.kt | 12 +++++++++--- app/src/main/java/xyz/quaver/pupil/util/file.kt | 4 ---- 4 files changed, 12 insertions(+), 15 deletions(-) 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 54df43d2..ff76da76 100644 --- a/app/src/main/java/xyz/quaver/pupil/ui/MainActivity.kt +++ b/app/src/main/java/xyz/quaver/pupil/ui/MainActivity.kt @@ -429,12 +429,7 @@ class MainActivity : AppCompatActivity() { CoroutineScope(Dispatchers.Default).launch { DownloadWorker.getInstance(context).cancel(galleryID) - var cache = Cache(context).getCachedGallery(galleryID) - - while (cache != null) { - cache.deleteRecursively() - cache = Cache(context).getCachedGallery(galleryID) - } + Cache(context).getCachedGallery(galleryID)?.delete() histories.remove(galleryID) diff --git a/app/src/main/java/xyz/quaver/pupil/ui/fragment/SettingsFragment.kt b/app/src/main/java/xyz/quaver/pupil/ui/fragment/SettingsFragment.kt index 98e29b6f..6ac085ca 100644 --- a/app/src/main/java/xyz/quaver/pupil/ui/fragment/SettingsFragment.kt +++ b/app/src/main/java/xyz/quaver/pupil/ui/fragment/SettingsFragment.kt @@ -93,7 +93,7 @@ class SettingsFragment : setMessage(R.string.settings_clear_cache_alert_message) setPositiveButton(android.R.string.yes) { _, _ -> if (dir.exists()) - dir.deleteRecursively() + dir.delete() summary = getDirSize(dir) } @@ -108,7 +108,7 @@ class SettingsFragment : setMessage(R.string.settings_clear_downloads_alert_message) setPositiveButton(android.R.string.yes) { _, _ -> if (dir.exists()) - dir.deleteRecursively() + dir.delete() summary = getDirSize(dir) } 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 ff103c63..a3ccde2e 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.Log import androidx.documentfile.provider.DocumentFile import androidx.preference.PreferenceManager import kotlinx.coroutines.CoroutineScope @@ -189,8 +190,13 @@ class Cache(context: Context) : ContextWrapper(context) { fun getImages(galleryID: Int): List? { val gallery = getCachedGallery(galleryID) ?: return null val reader = getReaderOrNull(galleryID) ?: return null + + val time = System.currentTimeMillis() + val images = gallery.listFiles() + Log.i("PUPILD", "${System.currentTimeMillis() - time} ms") + return reader.galleryInfo.indices.map { index -> images.firstOrNull { file -> file.name?.startsWith("%05d".format(index)) == true } } @@ -220,10 +226,10 @@ class Cache(context: Context) : ContextWrapper(context) { if (!download.isParentOf(cache)) { val target = getDownloadDirectory(this).let { it.findFile(galleryID.toString()) ?: it.createDirectory(galleryID.toString()) - } + } ?: return - cache.copyRecursively(this, download) - cache.deleteRecursively() + cache.copyRecursively(this, target) + cache.delete() } } } diff --git a/app/src/main/java/xyz/quaver/pupil/util/file.kt b/app/src/main/java/xyz/quaver/pupil/util/file.kt index 8cf67847..400d6711 100644 --- a/app/src/main/java/xyz/quaver/pupil/util/file.kt +++ b/app/src/main/java/xyz/quaver/pupil/util/file.kt @@ -128,10 +128,6 @@ fun DocumentFile.copyRecursively( } } -fun DocumentFile.deleteRecursively() { - this.delete() -} - fun DocumentFile.walk(state: LinkedList = LinkedList()) : Queue { if (state.isEmpty()) state.push(this)