Download gallery to the data folder not cache folder

This commit is contained in:
tom5079
2019-06-05 23:03:37 +09:00
parent 1faf6cb208
commit bcd515b2db
8 changed files with 96 additions and 26 deletions

View File

@@ -3,6 +3,7 @@
package="xyz.quaver.pupil"> package="xyz.quaver.pupil">
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application <application
android:allowBackup="true" android:allowBackup="true"
@@ -14,16 +15,6 @@
android:theme="@style/AppTheme" android:theme="@style/AppTheme"
android:name=".Pupil"> android:name=".Pupil">
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="xyz.quaver.pupil.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/filepaths"/>
</provider>
<activity android:name=".ReaderActivity" <activity android:name=".ReaderActivity"
android:parentActivityName=".MainActivity" android:parentActivityName=".MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"/> android:configChanges="keyboardHidden|orientation|screenSize"/>

View File

@@ -15,6 +15,7 @@ import android.widget.TextView
import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.cardview.widget.CardView import androidx.cardview.widget.CardView
import androidx.core.content.ContextCompat
import androidx.core.content.res.ResourcesCompat import androidx.core.content.res.ResourcesCompat
import androidx.core.view.GravityCompat import androidx.core.view.GravityCompat
import com.arlib.floatingsearchview.FloatingSearchView import com.arlib.floatingsearchview.FloatingSearchView
@@ -359,10 +360,9 @@ class MainActivity : AppCompatActivity() {
isEnabled = !(adapter as GalleryBlockAdapter).completeFlag.get(galleryBlock.id, false) isEnabled = !(adapter as GalleryBlockAdapter).completeFlag.get(galleryBlock.id, false)
setOnClickListener { setOnClickListener {
val downloader = GalleryDownloader.get(galleryBlock.id) val downloader = GalleryDownloader.get(galleryBlock.id)
if (downloader == null) { if (downloader == null)
GalleryDownloader(context, galleryBlock, true).start() GalleryDownloader(context, galleryBlock, true).start()
downloads.add(galleryBlock.id) else {
} else {
downloader.cancel() downloader.cancel()
downloader.clearNotification() downloader.clearNotification()
} }
@@ -377,12 +377,25 @@ class MainActivity : AppCompatActivity() {
this?.cancelAndJoin() this?.cancelAndJoin()
this?.clearNotification() this?.clearNotification()
} }
val cache = File(cacheDir, "imageCache/${galleryBlock.id}/images/") val cache = File(cacheDir, "imageCache/${galleryBlock.id}")
val data = File(ContextCompat.getDataDir(this@MainActivity), "images/${galleryBlock.id}")
cache.deleteRecursively() cache.deleteRecursively()
data.deleteRecursively()
downloads.remove(galleryBlock.id)
if (mode == Mode.DOWNLOAD) {
runOnUiThread {
cancelFetch()
clearGalleries()
fetchGalleries(query)
loadBlocks()
}
}
dialog.dismiss()
(adapter as GalleryBlockAdapter).completeFlag.put(galleryBlock.id, false) (adapter as GalleryBlockAdapter).completeFlag.put(galleryBlock.id, false)
} }
dialog.dismiss()
} }
dialog.show() dialog.show()

View File

@@ -68,16 +68,30 @@ class GalleryBlockAdapter(private val galleries: List<Pair<GalleryBlock, Deferre
} }
//Check cache //Check cache
val readerCache = File(context.cacheDir, "imageCache/${gallery.id}/reader.json") val readerCache = {
val imageCache = File(context.cacheDir, "imageCache/${gallery.id}/images") File(ContextCompat.getDataDir(context), "images/${gallery.id}/reader.json").let {
when {
it.exists() -> it
else -> File(context.cacheDir, "imageCache/${gallery.id}/reader.json")
}
}
}
val imageCache = {
File(ContextCompat.getDataDir(context), "images/${gallery.id}/images").let {
when {
it.exists() -> it
else -> File(context.cacheDir, "imageCache/${gallery.id}/images")
}
}
}
if (readerCache.exists()) { if (readerCache.invoke().exists()) {
val reader = Json(JsonConfiguration.Stable) val reader = Json(JsonConfiguration.Stable)
.parse(ReaderItem.serializer().list, readerCache.readText()) .parse(ReaderItem.serializer().list, readerCache.invoke().readText())
with(galleryblock_progressbar) { with(galleryblock_progressbar) {
max = reader.size max = reader.size
progress = imageCache.list()?.size ?: 0 progress = imageCache.invoke().list()?.size ?: 0
visibility = View.VISIBLE visibility = View.VISIBLE
} }
@@ -89,9 +103,9 @@ class GalleryBlockAdapter(private val galleries: List<Pair<GalleryBlock, Deferre
val refresh = Timer(false).schedule(0, 1000) { val refresh = Timer(false).schedule(0, 1000) {
post { post {
with(view.galleryblock_progressbar) { with(view.galleryblock_progressbar) {
progress = imageCache.list()?.size ?: 0 progress = imageCache.invoke().list()?.size ?: 0
if (!readerCache.exists()) { if (!readerCache.invoke().exists()) {
visibility = View.GONE visibility = View.GONE
max = 0 max = 0
progress = 0 progress = 0
@@ -100,7 +114,7 @@ class GalleryBlockAdapter(private val galleries: List<Pair<GalleryBlock, Deferre
} else { } else {
if (visibility == View.GONE) { if (visibility == View.GONE) {
val reader = Json(JsonConfiguration.Stable) val reader = Json(JsonConfiguration.Stable)
.parse(ReaderItem.serializer().list, readerCache.readText()) .parse(ReaderItem.serializer().list, readerCache.invoke().readText())
max = reader.size max = reader.size
visibility = View.VISIBLE visibility = View.VISIBLE
} }

View File

@@ -8,6 +8,7 @@ import android.util.SparseArray
import androidx.core.app.NotificationCompat import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat import androidx.core.app.NotificationManagerCompat
import androidx.core.app.TaskStackBuilder import androidx.core.app.TaskStackBuilder
import androidx.core.content.ContextCompat
import androidx.preference.PreferenceManager import androidx.preference.PreferenceManager
import kotlinx.coroutines.* import kotlinx.coroutines.*
import kotlinx.io.IOException import kotlinx.io.IOException
@@ -15,6 +16,7 @@ import kotlinx.serialization.json.Json
import kotlinx.serialization.json.JsonConfiguration import kotlinx.serialization.json.JsonConfiguration
import kotlinx.serialization.list import kotlinx.serialization.list
import xyz.quaver.hitomi.* import xyz.quaver.hitomi.*
import xyz.quaver.pupil.Pupil
import xyz.quaver.pupil.R import xyz.quaver.pupil.R
import xyz.quaver.pupil.ReaderActivity import xyz.quaver.pupil.ReaderActivity
import java.io.File import java.io.File
@@ -31,16 +33,30 @@ class GalleryDownloader(
_notify: Boolean = false _notify: Boolean = false
) : ContextWrapper(base) { ) : ContextWrapper(base) {
val downloads = (applicationContext as Pupil).downloads
var download: Boolean = false var download: Boolean = false
set(value) { set(value) {
if (value) { if (value) {
field = true field = true
notificationManager.notify(galleryBlock.id, notificationBuilder.build()) notificationManager.notify(galleryBlock.id, notificationBuilder.build())
val data = File(ContextCompat.getDataDir(this), "images/${galleryBlock.id}")
val cache = File(cacheDir, "imageCache/${galleryBlock.id}")
if (cache.exists() && !data.exists()) {
cache.copyRecursively(data, true)
cache.deleteRecursively()
}
if (!reader.isActive && downloadJob?.isActive != true) if (!reader.isActive && downloadJob?.isActive != true)
field = false field = false
downloads.add(galleryBlock.id)
} else { } else {
field = false field = false
downloads.remove(galleryBlock.id)
} }
onNotifyChangedHandler?.invoke(value) onNotifyChangedHandler?.invoke(value)
@@ -74,7 +90,12 @@ class GalleryDownloader(
val useHiyobi = preference.getBoolean("use_hiyobi", false) val useHiyobi = preference.getBoolean("use_hiyobi", false)
//Check cache //Check cache
val cache = File(cacheDir, "imageCache/${galleryBlock.id}/reader.json") val cache = File(ContextCompat.getDataDir(this@GalleryDownloader), "images/${galleryBlock.id}/reader.json").let {
when {
it.exists() -> it
else -> File(cacheDir, "imageCache/${galleryBlock.id}/reader.json")
}
}
if (cache.exists()) { if (cache.exists()) {
val cached = json.parse(serializer, cache.readText()) val cached = json.parse(serializer, cache.readText())
@@ -148,7 +169,12 @@ class GalleryDownloader(
val name = "$index".padStart(4, '0') val name = "$index".padStart(4, '0')
val ext = url.split('.').last() val ext = url.split('.').last()
val cache = File(cacheDir, "/imageCache/${galleryBlock.id}/images/$name.$ext") val cache = File(ContextCompat.getDataDir(this@GalleryDownloader), "images/${galleryBlock.id}/images/$name.$ext").let {
when {
it.exists() -> it
else -> File(cacheDir, "/imageCache/${galleryBlock.id}/images/$name.$ext")
}
}
if (!cache.exists()) if (!cache.exists())
try { try {
@@ -163,6 +189,8 @@ class GalleryDownloader(
} catch (e: Exception) { } catch (e: Exception) {
cache.delete() cache.delete()
downloads.remove(galleryBlock.id)
onErrorHandler?.invoke(e) onErrorHandler?.invoke(e)
notificationBuilder notificationBuilder
@@ -189,8 +217,19 @@ class GalleryDownloader(
.setContentText(getString(R.string.reader_notification_complete)) .setContentText(getString(R.string.reader_notification_complete))
.setProgress(0, 0, false) .setProgress(0, 0, false)
if (download) if (download) {
File(cacheDir, "imageCache/${galleryBlock.id}").let {
if (it.exists()) {
it.copyRecursively(
File(ContextCompat.getDataDir(this@GalleryDownloader), "images/${galleryBlock.id}"),
true
)
it.deleteRecursively()
}
}
notificationManager.notify(galleryBlock.id, notificationBuilder.build()) notificationManager.notify(galleryBlock.id, notificationBuilder.build())
}
download = false download = false
} }
@@ -207,6 +246,8 @@ class GalleryDownloader(
suspend fun cancelAndJoin() { suspend fun cancelAndJoin() {
downloadJob?.cancelAndJoin() downloadJob?.cancelAndJoin()
remove(galleryBlock.id)
} }
fun invokeOnReaderLoaded() { fun invokeOnReaderLoaded() {

View File

@@ -20,4 +20,12 @@
android:text="@string/main_dialog_delete" android:text="@string/main_dialog_delete"
app:layout_constraintTop_toBottomOf="@id/main_dialog_download"/> app:layout_constraintTop_toBottomOf="@id/main_dialog_download"/>
<Button
android:id="@+id/main_export"
style="?borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/main_dialog_export"
app:layout_constraintTop_toBottomOf="@id/main_dialog_delete"/>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>

View File

@@ -59,4 +59,5 @@
<string name="main_move">%1$dページへ移動</string> <string name="main_move">%1$dページへ移動</string>
<string name="https_block_alert_title">(Korean only)</string> <string name="https_block_alert_title">(Korean only)</string>
<string name="https_block_alert">(Korean only)</string> <string name="https_block_alert">(Korean only)</string>
<string name="main_dialog_export">ギャラリーエクスポート</string>
</resources> </resources>

View File

@@ -59,4 +59,5 @@
<string name="main_move">%1$d 페이지로 이동</string> <string name="main_move">%1$d 페이지로 이동</string>
<string name="https_block_alert_title">접속 불가 현상 안내</string> <string name="https_block_alert_title">접속 불가 현상 안내</string>
<string name="https_block_alert">최근 https 차단으로 접속이 안 되는 경우가 발생하고 있습니다\n이 경우 플레이스토어에서 SNIper앱을 이용하시면 정상이용이 가능합니다.</string> <string name="https_block_alert">최근 https 차단으로 접속이 안 되는 경우가 발생하고 있습니다\n이 경우 플레이스토어에서 SNIper앱을 이용하시면 정상이용이 가능합니다.</string>
<string name="main_dialog_export">갤러리 저장</string>
</resources> </resources>

View File

@@ -46,6 +46,7 @@
<string name="main_move">Move to page %1$d</string> <string name="main_move">Move to page %1$d</string>
<string name="main_dialog_delete">Delete this gallery</string> <string name="main_dialog_delete">Delete this gallery</string>
<string name="main_dialog_export">Export this gallery</string>
<string name="help_dialog_title">WIP</string> <string name="help_dialog_title">WIP</string>
<string name="help_dialog_message">While in progress!</string> <string name="help_dialog_message">While in progress!</string>