From ead68b520171cd83944212a05cf290801c2255c9 Mon Sep 17 00:00:00 2001 From: tom5079 Date: Wed, 2 Sep 2020 13:28:05 +0900 Subject: [PATCH] Moved storage managing settings to another fragment --- app/proguard-rules.pro | 1 + .../ui/fragment/ManageStorageFragment.kt | 167 ++++++++++++++++++ .../pupil/ui/fragment/SettingsFragment.kt | 103 ----------- app/src/main/res/values-ja/strings.xml | 1 + app/src/main/res/values-ko/strings.xml | 1 + app/src/main/res/values/strings.xml | 4 + .../res/xml/manage_storage_preferences.xml | 34 ++++ app/src/main/res/xml/root_preferences.xml | 12 +- 8 files changed, 210 insertions(+), 113 deletions(-) create mode 100644 app/src/main/java/xyz/quaver/pupil/ui/fragment/ManageStorageFragment.kt create mode 100644 app/src/main/res/xml/manage_storage_preferences.xml diff --git a/app/proguard-rules.pro b/app/proguard-rules.pro index d5e3055f..a4e83a1d 100644 --- a/app/proguard-rules.pro +++ b/app/proguard-rules.pro @@ -47,4 +47,5 @@ kotlinx.serialization.KSerializer serializer(...); } -keep class xyz.quaver.pupil.ui.fragment.ManageFavoritesFragment +-keep class xyz.quaver.pupil.ui.fragment.ManageStorageFragment -keep class xyz.quaver.pupil.util.Preferences \ No newline at end of file diff --git a/app/src/main/java/xyz/quaver/pupil/ui/fragment/ManageStorageFragment.kt b/app/src/main/java/xyz/quaver/pupil/ui/fragment/ManageStorageFragment.kt new file mode 100644 index 00000000..b3f929ed --- /dev/null +++ b/app/src/main/java/xyz/quaver/pupil/ui/fragment/ManageStorageFragment.kt @@ -0,0 +1,167 @@ +/* + * 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.ui.fragment + +import android.os.Bundle +import androidx.appcompat.app.AlertDialog +import androidx.preference.Preference +import androidx.preference.PreferenceFragmentCompat +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import xyz.quaver.pupil.R +import xyz.quaver.pupil.histories +import xyz.quaver.pupil.util.downloader.DownloadManager +import xyz.quaver.pupil.util.getDownloadDirectory +import java.io.BufferedReader +import java.io.File +import java.io.InputStreamReader + +class ManageStorageFragment : PreferenceFragmentCompat(), Preference.OnPreferenceClickListener { + + 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 + + when (key) { + "delete_cache" -> { + val dir = File(requireContext().cacheDir, "imageCache") + + AlertDialog.Builder(requireContext()).apply { + setTitle(R.string.warning) + setMessage(R.string.settings_clear_cache_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 { + launch(Dispatchers.Main) { + this@with.summary = it + } + } + } + } + setNegativeButton(android.R.string.no) { _, _ -> } + }.show() + } + "delete_downloads" -> { + val dir = DownloadManager.getInstance(context).downloadFolder + + AlertDialog.Builder(requireContext()).apply { + 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 { + launch(Dispatchers.Main) { + this@with.summary = it + } + } + } + } + setNegativeButton(android.R.string.no) { _, _ -> } + }.show() + } + "clear_history" -> { + AlertDialog.Builder(requireContext()).apply { + setTitle(R.string.warning) + setMessage(R.string.settings_clear_history_alert_message) + setPositiveButton(android.R.string.yes) { _, _ -> + histories.clear() + summary = getString(R.string.settings_clear_history_summary, histories.size) + } + setNegativeButton(android.R.string.no) { _, _ -> } + }.show() + } + else -> return false + } + } + + return true + } + + private fun initPreferences() { + with(findPreference("delete_cache")) { + this ?: return@with + + val dir = File(requireContext().cacheDir, "imageCache") + + summary = getString(R.string.settings_storage_usage_loading) + CoroutineScope(Dispatchers.IO).launch { + getDirSize(dir).let { + launch(Dispatchers.Main) { + this@with.summary = it + } + } + } + + onPreferenceClickListener = this@ManageStorageFragment + } + + with(findPreference("delete_downloads")) { + this ?: return@with + + val dir = DownloadManager.getInstance(context).downloadFolder + + summary = getString(R.string.settings_storage_usage_loading) + CoroutineScope(Dispatchers.IO).launch { + getDirSize(dir).let { + launch(Dispatchers.Main) { + this@with.summary = it + } + } + } + + onPreferenceClickListener = this@ManageStorageFragment + } + + with(findPreference("clear_history")) { + this ?: return@with + + summary = getString(R.string.settings_clear_history_summary, histories.size) + + onPreferenceClickListener = this@ManageStorageFragment + } + } + +} \ No newline at end of file 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 a0b14f26..a801ebce 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 @@ -68,16 +68,6 @@ class SettingsFragment : } } - private fun getDirSize(dir: File) : String { - return context?.getString(R.string.settings_storage_usage, - Runtime.getRuntime().exec("du -hs " + dir.absolutePath).let { - BufferedReader(InputStreamReader(it.inputStream)).use { reader -> - reader.readLine()?.split('\t')?.firstOrNull() ?: "0" - } - } - ) ?: "" - } - override fun onPreferenceClick(preference: Preference?): Boolean { with (preference) { this ?: return false @@ -86,63 +76,6 @@ class SettingsFragment : "app_version" -> { checkUpdate(activity as SettingsActivity, true) } - "delete_cache" -> { - val dir = File(requireContext().cacheDir, "imageCache") - - AlertDialog.Builder(requireContext()).apply { - setTitle(R.string.warning) - setMessage(R.string.settings_clear_cache_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 { - launch(Dispatchers.Main) { - this@with.summary = it - } - } - } - } - setNegativeButton(android.R.string.no) { _, _ -> } - }.show() - } - "delete_downloads" -> { - val dir = getDownloadDirectory(requireContext()) - - AlertDialog.Builder(requireContext()).apply { - 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 { - launch(Dispatchers.Main) { - this@with.summary = it - } - } - } - } - setNegativeButton(android.R.string.no) { _, _ -> } - }.show() - } - "clear_history" -> { - AlertDialog.Builder(requireContext()).apply { - setTitle(R.string.warning) - setMessage(R.string.settings_clear_history_alert_message) - setPositiveButton(android.R.string.yes) { _, _ -> - histories.clear() - summary = getString(R.string.settings_clear_history_summary, histories.size) - } - setNegativeButton(android.R.string.no) { _, _ -> } - }.show() - } "download_folder" -> { DownloadLocationDialog(requireActivity()).show() } @@ -166,9 +99,6 @@ class SettingsFragment : ProxyDialog(requireContext()) .show() } - "nomedia" -> { - File(getDownloadDirectory(context), ".nomedia").createNewFile() - } "user_id" -> { (context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager).setPrimaryClip( ClipData.newPlainText("user_id", Preferences.get("user_id")) @@ -269,39 +199,6 @@ class SettingsFragment : onPreferenceClickListener = this@SettingsFragment } - "delete_cache" -> { - val dir = File(requireContext().cacheDir, "imageCache") - - summary = getString(R.string.settings_storage_usage_loading) - CoroutineScope(Dispatchers.IO).launch { - getDirSize(dir).let { - launch(Dispatchers.Main) { - this@with.summary = it - } - } - } - - onPreferenceClickListener = this@SettingsFragment - } - "delete_downloads" -> { - val dir = getDownloadDirectory(requireContext()) - - summary = getString(R.string.settings_storage_usage_loading) - CoroutineScope(Dispatchers.IO).launch { - getDirSize(dir).let { - launch(Dispatchers.Main) { - this@with.summary = it - } - } - } - - onPreferenceClickListener = this@SettingsFragment - } - "clear_history" -> { - summary = getString(R.string.settings_clear_history_summary, histories.size) - - onPreferenceClickListener = this@SettingsFragment - } "download_folder_name" -> { (this as EditTextPreference).dialogMessage = getString(R.string.settings_download_folder_name_message, formatMap.keys.toString()) onPreferenceChangeListener = this@SettingsFragment diff --git a/app/src/main/res/values-ja/strings.xml b/app/src/main/res/values-ja/strings.xml index f8bf1d22..a87f5870 100644 --- a/app/src/main/res/values-ja/strings.xml +++ b/app/src/main/res/values-ja/strings.xml @@ -146,4 +146,5 @@ フォルダ名パターン フォルダ名に使用できない文字が含まれています %sに含まれている文字列を対応する変数に置換します + ストレージ管理 \ 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 8b9f7a33..67f88eff 100644 --- a/app/src/main/res/values-ko/strings.xml +++ b/app/src/main/res/values-ko/strings.xml @@ -146,4 +146,5 @@ 폴더명 패턴 폴더 패턴에 사용할 수 없는 문자가 포함되어 있습니다 지원되는 변수는 %s 입니다 + 저장소 관리 \ 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 d45a0b99..12182e71 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -126,6 +126,10 @@ Storage + + + + Manage Storage Currently using %s Calculating storage usage… Clear cache diff --git a/app/src/main/res/xml/manage_storage_preferences.xml b/app/src/main/res/xml/manage_storage_preferences.xml new file mode 100644 index 00000000..f6413aaa --- /dev/null +++ b/app/src/main/res/xml/manage_storage_preferences.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/xml/root_preferences.xml b/app/src/main/res/xml/root_preferences.xml index 690b7639..5c3c629e 100644 --- a/app/src/main/res/xml/root_preferences.xml +++ b/app/src/main/res/xml/root_preferences.xml @@ -32,16 +32,8 @@ app:title="@string/settings_storage"> - - - - + app:fragment="xyz.quaver.pupil.ui.fragment.ManageStorageFragment" + app:title="@string/settings_manage_storage" />