refresh Storage fragment data on resume

This commit is contained in:
tibbi
2021-10-13 20:03:40 +02:00
parent 149a8e628d
commit 5b1d6ea32b
6 changed files with 47 additions and 43 deletions

View File

@ -98,7 +98,7 @@ class MainActivity : SimpleActivity() {
} }
getAllFragments().forEach { getAllFragments().forEach {
it?.setupColors(config.textColor, config.primaryColor) it?.onResume(config.textColor, config.primaryColor)
} }
if (storedFontSize != config.fontSize) { if (storedFontSize != config.fontSize) {

View File

@ -20,7 +20,7 @@ class ViewPagerAdapter(val activity: SimpleActivity) : PagerAdapter() {
(view as MyViewPagerFragment).apply { (view as MyViewPagerFragment).apply {
setupFragment(activity) setupFragment(activity)
setupColors(activity.config.textColor, activity.config.primaryColor) onResume(activity.config.textColor, activity.config.primaryColor)
} }
return view return view

View File

@ -47,7 +47,7 @@ class ItemsFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerF
} }
} }
override fun setupColors(textColor: Int, primaryColor: Int) { override fun onResume(textColor: Int, primaryColor: Int) {
context!!.updateTextColors(this) context!!.updateTextColors(this)
items_fastscroller.updatePrimaryColor() items_fastscroller.updatePrimaryColor()
storedItems = ArrayList() storedItems = ArrayList()

View File

@ -36,7 +36,7 @@ abstract class MyViewPagerFragment(context: Context, attributeSet: AttributeSet)
abstract fun setupFragment(activity: SimpleActivity) abstract fun setupFragment(activity: SimpleActivity)
abstract fun setupColors(textColor: Int, primaryColor: Int) abstract fun onResume(textColor: Int, primaryColor: Int)
abstract fun refreshFragment() abstract fun refreshFragment()
} }

View File

@ -64,7 +64,7 @@ class RecentsFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
} }
} }
override fun setupColors(textColor: Int, primaryColor: Int) { override fun onResume(textColor: Int, primaryColor: Int) {
recents_placeholder.setTextColor(textColor) recents_placeholder.setTextColor(textColor)
getRecyclerAdapter()?.apply { getRecyclerAdapter()?.apply {

View File

@ -25,38 +25,7 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
override fun setupFragment(activity: SimpleActivity) { override fun setupFragment(activity: SimpleActivity) {
total_space.text = String.format(context.getString(R.string.total_storage), "") total_space.text = String.format(context.getString(R.string.total_storage), "")
getSizes()
ensureBackgroundThread {
getMainStorageStats(activity)
val filesSize = getSizesByMimeType()
val imagesSize = filesSize[IMAGES]!!
val videosSize = filesSize[VIDEOS]!!
val audioSize = filesSize[AUDIO]!!
val documentsSize = filesSize[DOCUMENTS]!!
val archivesSize = filesSize[ARCHIVES]!!
val othersSize = filesSize[OTHERS]!!
activity.runOnUiThread {
images_size.text = imagesSize.formatSize()
images_progressbar.progress = (imagesSize / SIZE_DIVIDER).toInt()
videos_size.text = videosSize.formatSize()
videos_progressbar.progress = (videosSize / SIZE_DIVIDER).toInt()
audio_size.text = audioSize.formatSize()
audio_progressbar.progress = (audioSize / SIZE_DIVIDER).toInt()
documents_size.text = documentsSize.formatSize()
documents_progressbar.progress = (documentsSize / SIZE_DIVIDER).toInt()
archives_size.text = archivesSize.formatSize()
archives_progressbar.progress = (archivesSize / SIZE_DIVIDER).toInt()
others_size.text = othersSize.formatSize()
others_progressbar.progress = (othersSize / SIZE_DIVIDER).toInt()
}
}
free_space_holder.setOnClickListener { free_space_holder.setOnClickListener {
try { try {
@ -77,7 +46,8 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
override fun refreshFragment() {} override fun refreshFragment() {}
override fun setupColors(textColor: Int, primaryColor: Int) { override fun onResume(textColor: Int, primaryColor: Int) {
getSizes()
context.updateTextColors(storage_fragment) context.updateTextColors(storage_fragment)
main_storage_usage_progressbar.setIndicatorColor(primaryColor) main_storage_usage_progressbar.setIndicatorColor(primaryColor)
@ -115,6 +85,40 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
} }
} }
private fun getSizes() {
ensureBackgroundThread {
getMainStorageStats(context)
val filesSize = getSizesByMimeType()
val imagesSize = filesSize[IMAGES]!!
val videosSize = filesSize[VIDEOS]!!
val audioSize = filesSize[AUDIO]!!
val documentsSize = filesSize[DOCUMENTS]!!
val archivesSize = filesSize[ARCHIVES]!!
val othersSize = filesSize[OTHERS]!!
post {
images_size.text = imagesSize.formatSize()
images_progressbar.progress = (imagesSize / SIZE_DIVIDER).toInt()
videos_size.text = videosSize.formatSize()
videos_progressbar.progress = (videosSize / SIZE_DIVIDER).toInt()
audio_size.text = audioSize.formatSize()
audio_progressbar.progress = (audioSize / SIZE_DIVIDER).toInt()
documents_size.text = documentsSize.formatSize()
documents_progressbar.progress = (documentsSize / SIZE_DIVIDER).toInt()
archives_size.text = archivesSize.formatSize()
archives_progressbar.progress = (archivesSize / SIZE_DIVIDER).toInt()
others_size.text = othersSize.formatSize()
others_progressbar.progress = (othersSize / SIZE_DIVIDER).toInt()
}
}
}
private fun getSizesByMimeType(): HashMap<String, Long> { private fun getSizesByMimeType(): HashMap<String, Long> {
val uri = MediaStore.Files.getContentUri("external") val uri = MediaStore.Files.getContentUri("external")
val projection = arrayOf( val projection = arrayOf(
@ -177,20 +181,20 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
} }
@SuppressLint("NewApi") @SuppressLint("NewApi")
private fun getMainStorageStats(activity: SimpleActivity) { private fun getMainStorageStats(context: Context) {
val externalDirs = activity.getExternalFilesDirs(null) val externalDirs = context.getExternalFilesDirs(null)
val storageManager = activity.getSystemService(AppCompatActivity.STORAGE_SERVICE) as StorageManager val storageManager = context.getSystemService(AppCompatActivity.STORAGE_SERVICE) as StorageManager
externalDirs.forEach { file -> externalDirs.forEach { file ->
val storageVolume = storageManager.getStorageVolume(file) ?: return val storageVolume = storageManager.getStorageVolume(file) ?: return
if (storageVolume.isPrimary) { if (storageVolume.isPrimary) {
// internal storage // internal storage
val storageStatsManager = activity.getSystemService(AppCompatActivity.STORAGE_STATS_SERVICE) as StorageStatsManager val storageStatsManager = context.getSystemService(AppCompatActivity.STORAGE_STATS_SERVICE) as StorageStatsManager
val uuid = StorageManager.UUID_DEFAULT val uuid = StorageManager.UUID_DEFAULT
val totalSpace = storageStatsManager.getTotalBytes(uuid) val totalSpace = storageStatsManager.getTotalBytes(uuid)
val freeSpace = storageStatsManager.getFreeBytes(uuid) val freeSpace = storageStatsManager.getFreeBytes(uuid)
activity.runOnUiThread { post {
arrayOf( arrayOf(
main_storage_usage_progressbar, images_progressbar, videos_progressbar, audio_progressbar, documents_progressbar, main_storage_usage_progressbar, images_progressbar, videos_progressbar, audio_progressbar, documents_progressbar,
archives_progressbar, others_progressbar archives_progressbar, others_progressbar