This commit is contained in:
tom5079
2020-09-02 20:15:26 +09:00
parent ead68b5201
commit 37be8ccf7f
12 changed files with 76 additions and 51 deletions

View File

@@ -23,6 +23,8 @@ import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.ContextCompat
@@ -95,6 +97,12 @@ class Pupil : Application() {
try {
Preferences.get<String>("download_folder").also {
if (Build.VERSION.SDK_INT > 19)
contentResolver.takePersistableUriPermission(
Uri.parse(it),
Intent.FLAG_GRANT_READ_URI_PERMISSION or Intent.FLAG_GRANT_WRITE_URI_PERMISSION
)
if (!FileX(this, it).canWrite())
throw Exception()
}

View File

@@ -44,7 +44,7 @@ class UpdateBroadcastReciever : BroadcastReceiver() {
val downloadID: Long = Preferences["update_download_id"]
val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
if (intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1) != downloadID)
if (intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -2) != downloadID)
return
// Get target uri

View File

@@ -24,9 +24,13 @@ import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import xyz.quaver.io.FileX
import xyz.quaver.io.util.deleteRecursively
import xyz.quaver.pupil.R
import xyz.quaver.pupil.histories
import xyz.quaver.pupil.util.byteToString
import xyz.quaver.pupil.util.downloader.DownloadManager
import xyz.quaver.pupil.util.getDownloadDirectory
import java.io.BufferedReader
@@ -35,22 +39,14 @@ import java.io.InputStreamReader
class ManageStorageFragment : PreferenceFragmentCompat(), Preference.OnPreferenceClickListener {
private var job: Job? = null
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.manage_storage_preferences, rootKey)
initPreferences()
}
private fun getDirSize(dir: File) : String {
return context?.getString(R.string.settings_storage_usage,
Runtime.getRuntime().exec("du -hs " + dir.canonicalPath).let {
BufferedReader(InputStreamReader(it.inputStream)).use { reader ->
reader.readLine()?.split('\t')?.firstOrNull() ?: "0"
}
}
) ?: ""
}
override fun onPreferenceClick(preference: Preference?): Boolean {
with(preference) {
this ?: return false
@@ -66,12 +62,15 @@ class ManageStorageFragment : PreferenceFragmentCompat(), Preference.OnPreferenc
if (dir.exists())
dir.deleteRecursively()
summary = getString(R.string.settings_storage_usage_loading)
summary = getString(R.string.settings_storage_usage, byteToString(0))
CoroutineScope(Dispatchers.IO).launch {
getDirSize(dir).let {
var size = 0L
dir.walk().forEach {
size += it.length()
launch(Dispatchers.Main) {
this@with.summary = it
summary = getString(R.string.settings_storage_usage, byteToString(size))
}
}
}
@@ -86,15 +85,27 @@ class ManageStorageFragment : PreferenceFragmentCompat(), Preference.OnPreferenc
setTitle(R.string.warning)
setMessage(R.string.settings_clear_downloads_alert_message)
setPositiveButton(android.R.string.yes) { _, _ ->
if (dir.exists())
dir.deleteRecursively()
summary = getString(R.string.settings_storage_usage_loading)
CoroutineScope(Dispatchers.IO).launch {
getDirSize(dir).let {
job?.cancel()
launch(Dispatchers.Main) {
summary = getString(R.string.settings_storage_usage_loading)
}
if (dir.exists())
dir.listFiles()?.forEach { (it as FileX).deleteRecursively() }
job = launch {
var size = 0L
launch(Dispatchers.Main) {
this@with.summary = it
summary = getString(R.string.settings_storage_usage, byteToString(size))
}
dir.walk().forEach {
size += it.length()
launch(Dispatchers.Main) {
summary = getString(R.string.settings_storage_usage, byteToString(size))
}
}
}
}
@@ -126,11 +137,15 @@ class ManageStorageFragment : PreferenceFragmentCompat(), Preference.OnPreferenc
val dir = File(requireContext().cacheDir, "imageCache")
summary = getString(R.string.settings_storage_usage_loading)
summary = getString(R.string.settings_storage_usage, byteToString(0))
CoroutineScope(Dispatchers.IO).launch {
getDirSize(dir).let {
var size = 0L
dir.walk().forEach {
size += it.length()
launch(Dispatchers.Main) {
this@with.summary = it
summary = getString(R.string.settings_storage_usage, byteToString(size))
}
}
}
@@ -143,12 +158,17 @@ class ManageStorageFragment : PreferenceFragmentCompat(), Preference.OnPreferenc
val dir = DownloadManager.getInstance(context).downloadFolder
summary = getString(R.string.settings_storage_usage_loading)
CoroutineScope(Dispatchers.IO).launch {
getDirSize(dir).let {
summary = getString(R.string.settings_storage_usage, byteToString(0))
job?.cancel()
job = CoroutineScope(Dispatchers.IO).launch {
var size = 0L
dir.walk().forEach {
launch(Dispatchers.Main) {
this@with.summary = it
summary = getString(R.string.settings_storage_usage, byteToString(size))
}
size += it.length()
}
}
@@ -164,4 +184,9 @@ class ManageStorageFragment : PreferenceFragmentCompat(), Preference.OnPreferenc
}
}
override fun onDestroy() {
job?.cancel()
super.onDestroy()
}
}

View File

@@ -28,7 +28,7 @@ object Preferences: SharedPreferences by preferences {
val defMap = mapOf(
String::class to "",
Int::class to -1,
Long::class to -1,
Long::class to -1L,
Boolean::class to false,
Set::class to emptySet<Any>()
)

View File

@@ -20,6 +20,7 @@ package xyz.quaver.pupil.util.downloader
import android.content.Context
import android.content.ContextWrapper
import android.util.Log
import kotlinx.coroutines.runBlocking
import kotlinx.serialization.decodeFromString
import kotlinx.serialization.encodeToString

View File

@@ -53,15 +53,15 @@ fun String.wordCapitalize() : String {
return result.joinToString(" ")
}
fun byteToString(byte: Long, precision : Int = 1) : String {
private val suffix = listOf(
"B",
"kB",
"MB",
"GB",
"TB" //really?
)
val suffix = listOf(
"B",
"kB",
"MB",
"GB",
"TB" //really?
)
fun byteToString(byte: Long, precision : Int = 1) : String {
var size = byte.toDouble(); var suffixIndex = 0
while (size >= 1024) {
@@ -70,7 +70,6 @@ fun byteToString(byte: Long, precision : Int = 1) : String {
}
return "%.${precision}f ${suffix[suffixIndex]}".format(size)
}
/**

View File

@@ -138,13 +138,11 @@ fun checkUpdate(context: Context, force: Boolean = false) {
setMessage(Markwon.create(context).toMarkdown(msg))
setPositiveButton(android.R.string.yes) { _, _ ->
val preference = PreferenceManager.getDefaultSharedPreferences(context)
val downloadManager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
//Cancel any download queued before
val id = preference.getLong("update_download_id", -1)
val id: Long = Preferences["update_download_id"]
if (id != -1L)
downloadManager.remove(id)
@@ -158,7 +156,7 @@ fun checkUpdate(context: Context, force: Boolean = false) {
.setDestinationUri(Uri.fromFile(target))
downloadManager.enqueue(request).also {
preference.edit().putLong("update_download_id", it).apply()
Preferences["update_download_id"] = it
}
}
setNegativeButton(if (force) android.R.string.no else R.string.ignore_update) { _, _ ->