sort files on the Storage fragment from latest to oldest

This commit is contained in:
tibbi 2023-01-07 19:24:34 +01:00
parent b12702848c
commit 63fa771118
2 changed files with 32 additions and 17 deletions

View File

@ -147,9 +147,9 @@ class MainActivity : SimpleActivity() {
val currentFragment = getCurrentFragment() val currentFragment = getCurrentFragment()
if (main_menu.isSearchOpen) { if (main_menu.isSearchOpen) {
main_menu.closeSearch() main_menu.closeSearch()
} else if (currentFragment is RecentsFragment) { } else if (currentFragment is RecentsFragment || currentFragment is StorageFragment) {
super.onBackPressed() super.onBackPressed()
} else if (currentFragment != null && currentFragment.breadcrumbs.getItemCount() <= 1) { } else if (currentFragment!!.breadcrumbs.getItemCount() <= 1) {
if (!wasBackJustPressed && config.pressBackTwice) { if (!wasBackJustPressed && config.pressBackTwice) {
wasBackJustPressed = true wasBackJustPressed = true
toast(R.string.press_back_again) toast(R.string.press_back_again)
@ -160,7 +160,7 @@ class MainActivity : SimpleActivity() {
finish() finish()
} }
} else { } else {
currentFragment?.breadcrumbs?.removeBreadcrumb() ?: return currentFragment.breadcrumbs?.removeBreadcrumb()
openPath(currentFragment.breadcrumbs.getLastItem().path) openPath(currentFragment.breadcrumbs.getLastItem().path)
} }
} }

View File

@ -2,6 +2,7 @@ package com.simplemobiletools.filemanager.pro.fragments
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.app.usage.StorageStatsManager import android.app.usage.StorageStatsManager
import android.content.ContentResolver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import android.os.Handler import android.os.Handler
@ -10,6 +11,7 @@ import android.provider.MediaStore
import android.provider.Settings import android.provider.Settings
import android.util.AttributeSet import android.util.AttributeSet
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.LOWER_ALPHA import com.simplemobiletools.commons.helpers.LOWER_ALPHA
import com.simplemobiletools.commons.helpers.SHORT_ANIMATION_DURATION import com.simplemobiletools.commons.helpers.SHORT_ANIMATION_DURATION
@ -279,16 +281,27 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
) )
try { try {
context?.queryCursor(uri, projection) { cursor -> if (isOreoPlus()) {
val queryArgs = bundleOf(
ContentResolver.QUERY_ARG_SORT_COLUMNS to arrayOf(MediaStore.Files.FileColumns.DATE_MODIFIED),
ContentResolver.QUERY_ARG_SORT_DIRECTION to ContentResolver.QUERY_SORT_DIRECTION_DESCENDING
)
context?.contentResolver?.query(uri, projection, queryArgs, null)
} else {
val sortOrder = "${MediaStore.Files.FileColumns.DATE_MODIFIED} DESC"
context?.contentResolver?.query(uri, projection, null, null, sortOrder)
}?.use { cursor ->
if (cursor.moveToFirst()) {
do {
try { try {
val name = cursor.getStringValue(MediaStore.Files.FileColumns.DISPLAY_NAME) val name = cursor.getStringValue(MediaStore.Files.FileColumns.DISPLAY_NAME)
if (!showHidden && name.startsWith(".")) { if (!showHidden && name.startsWith(".")) {
return@queryCursor continue
} }
val size = cursor.getLongValue(MediaStore.Files.FileColumns.SIZE) val size = cursor.getLongValue(MediaStore.Files.FileColumns.SIZE)
if (size == 0L) { if (size == 0L) {
return@queryCursor continue
} }
val path = cursor.getStringValue(MediaStore.Files.FileColumns.DATA) val path = cursor.getStringValue(MediaStore.Files.FileColumns.DATA)
@ -296,6 +309,8 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
fileDirItems.add(FileDirItem(path, name, false, 0, size, lastModified)) fileDirItems.add(FileDirItem(path, name, false, 0, size, lastModified))
} catch (e: Exception) { } catch (e: Exception) {
} }
} while (cursor.moveToNext())
}
} }
} catch (e: Exception) { } catch (e: Exception) {
context?.showErrorToast(e) context?.showErrorToast(e)