Added Custom download folder

This commit is contained in:
Pupil
2020-02-08 19:01:45 +09:00
parent 938156aa71
commit ecaecc1b91
22 changed files with 499 additions and 573 deletions

View File

@@ -19,10 +19,12 @@
package xyz.quaver.pupil.ui.fragment
import android.content.Intent
import android.content.SharedPreferences
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatDelegate
import androidx.core.content.ContextCompat
import androidx.documentfile.provider.DocumentFile
import androidx.preference.Preference
import androidx.preference.PreferenceCategory
import androidx.preference.PreferenceFragmentCompat
@@ -42,7 +44,14 @@ import java.io.File
class SettingsFragment :
PreferenceFragmentCompat(),
Preference.OnPreferenceClickListener,
Preference.OnPreferenceChangeListener {
Preference.OnPreferenceChangeListener,
SharedPreferences.OnSharedPreferenceChangeListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
PreferenceManager.getDefaultSharedPreferences(context).registerOnSharedPreferenceChangeListener(this)
}
override fun onResume() {
super.onResume()
@@ -62,7 +71,7 @@ class SettingsFragment :
}
}
private fun getDirSize(dir: File) : String {
private fun getDirSize(dir: DocumentFile) : String {
val size = dir.walk().map { it.length() }.sum()
return getString(R.string.settings_clear_summary, byteToString(size))
@@ -77,7 +86,7 @@ class SettingsFragment :
checkUpdate(activity as SettingsActivity, true)
}
"delete_cache" -> {
val dir = File(context.cacheDir, "imageCache")
val dir = DocumentFile.fromFile(File(context.cacheDir, "imageCache"))
AlertDialog.Builder(context).apply {
setTitle(R.string.warning)
@@ -92,7 +101,7 @@ class SettingsFragment :
}.show()
}
"delete_downloads" -> {
val dir = getDownloadDirectory(context)
val dir = getDownloadDirectory(context)!!
AlertDialog.Builder(context).apply {
setTitle(R.string.warning)
@@ -101,10 +110,6 @@ class SettingsFragment :
if (dir.exists())
dir.deleteRecursively()
val downloads = (activity!!.application as Pupil).downloads
downloads.clear()
summary = getDirSize(dir)
}
setNegativeButton(android.R.string.no) { _, _ -> }
@@ -124,14 +129,7 @@ class SettingsFragment :
}.show()
}
"dl_location" -> {
DownloadLocationDialog(context).apply {
onDownloadLocationChangedListener = { value ->
PreferenceManager.getDefaultSharedPreferences(context).edit()
.putInt(key, value)
.apply()
summary = getDownloadDirectory(context).absolutePath
}
}.show()
DownloadLocationDialog(activity!!).show()
}
"default_query" -> {
DefaultQueryDialog(context).apply {
@@ -143,7 +141,7 @@ class SettingsFragment :
}
"app_lock" -> {
val intent = Intent(context, LockActivity::class.java)
activity?.startActivityForResult(intent, (activity as SettingsActivity).REQUEST_LOCK)
activity?.startActivityForResult(intent, REQUEST_LOCK)
}
"mirrors" -> {
MirrorDialog(context)
@@ -151,8 +149,8 @@ class SettingsFragment :
}
"backup" -> {
File(ContextCompat.getDataDir(context), "favorites.json").copyTo(
File(getDownloadDirectory(context), "favorites.json"),
true
context,
getDownloadDirectory(context)?.createFile("null", "favorites.json")!!
)
Snackbar.make(this@SettingsFragment.listView, R.string.settings_backup_snackbar, Snackbar.LENGTH_LONG)
@@ -164,7 +162,7 @@ class SettingsFragment :
type = "*/*"
}
activity?.startActivityForResult(intent, (activity as SettingsActivity).REQUEST_RESTORE)
activity?.startActivityForResult(intent, REQUEST_RESTORE)
}
else -> return false
}
@@ -191,6 +189,15 @@ class SettingsFragment :
return true
}
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences?, key: String?) {
when (key) {
"dl_location" -> {
findPreference<Preference>(key)?.summary =
FileUtils.getPath(context, getDownloadDirectory(context!!)?.uri)
}
}
}
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)
@@ -217,13 +224,13 @@ class SettingsFragment :
onPreferenceClickListener = this@SettingsFragment
}
"delete_cache" -> {
val dir = File(context.cacheDir, "imageCache")
val dir = DocumentFile.fromFile(File(context.cacheDir, "imageCache"))
summary = getDirSize(dir)
onPreferenceClickListener = this@SettingsFragment
}
"delete_downloads" -> {
val dir = getDownloadDirectory(context)
val dir = getDownloadDirectory(context)!!
summary = getDirSize(dir)
onPreferenceClickListener = this@SettingsFragment
@@ -235,7 +242,7 @@ class SettingsFragment :
onPreferenceClickListener = this@SettingsFragment
}
"dl_location" -> {
summary = getDownloadDirectory(context).absolutePath
summary = FileUtils.getPath(context, getDownloadDirectory(context)?.uri)
onPreferenceClickListener = this@SettingsFragment
}