Skip to content

Commit 63fa771

Browse files
committed
sort files on the Storage fragment from latest to oldest
1 parent b127028 commit 63fa771

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

app/src/main/kotlin/com/simplemobiletools/filemanager/pro/activities/MainActivity.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ class MainActivity : SimpleActivity() {
147147
val currentFragment = getCurrentFragment()
148148
if (main_menu.isSearchOpen) {
149149
main_menu.closeSearch()
150-
} else if (currentFragment is RecentsFragment) {
150+
} else if (currentFragment is RecentsFragment || currentFragment is StorageFragment) {
151151
super.onBackPressed()
152-
} else if (currentFragment != null && currentFragment.breadcrumbs.getItemCount() <= 1) {
152+
} else if (currentFragment!!.breadcrumbs.getItemCount() <= 1) {
153153
if (!wasBackJustPressed && config.pressBackTwice) {
154154
wasBackJustPressed = true
155155
toast(R.string.press_back_again)
@@ -160,7 +160,7 @@ class MainActivity : SimpleActivity() {
160160
finish()
161161
}
162162
} else {
163-
currentFragment?.breadcrumbs?.removeBreadcrumb() ?: return
163+
currentFragment.breadcrumbs?.removeBreadcrumb()
164164
openPath(currentFragment.breadcrumbs.getLastItem().path)
165165
}
166166
}

app/src/main/kotlin/com/simplemobiletools/filemanager/pro/fragments/StorageFragment.kt

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.simplemobiletools.filemanager.pro.fragments
22

33
import android.annotation.SuppressLint
44
import android.app.usage.StorageStatsManager
5+
import android.content.ContentResolver
56
import android.content.Context
67
import android.content.Intent
78
import android.os.Handler
@@ -10,6 +11,7 @@ import android.provider.MediaStore
1011
import android.provider.Settings
1112
import android.util.AttributeSet
1213
import androidx.appcompat.app.AppCompatActivity
14+
import androidx.core.os.bundleOf
1315
import com.simplemobiletools.commons.extensions.*
1416
import com.simplemobiletools.commons.helpers.LOWER_ALPHA
1517
import com.simplemobiletools.commons.helpers.SHORT_ANIMATION_DURATION
@@ -279,22 +281,35 @@ class StorageFragment(context: Context, attributeSet: AttributeSet) : MyViewPage
279281
)
280282

281283
try {
282-
context?.queryCursor(uri, projection) { cursor ->
283-
try {
284-
val name = cursor.getStringValue(MediaStore.Files.FileColumns.DISPLAY_NAME)
285-
if (!showHidden && name.startsWith(".")) {
286-
return@queryCursor
287-
}
284+
if (isOreoPlus()) {
285+
val queryArgs = bundleOf(
286+
ContentResolver.QUERY_ARG_SORT_COLUMNS to arrayOf(MediaStore.Files.FileColumns.DATE_MODIFIED),
287+
ContentResolver.QUERY_ARG_SORT_DIRECTION to ContentResolver.QUERY_SORT_DIRECTION_DESCENDING
288+
)
289+
context?.contentResolver?.query(uri, projection, queryArgs, null)
290+
} else {
291+
val sortOrder = "${MediaStore.Files.FileColumns.DATE_MODIFIED} DESC"
292+
context?.contentResolver?.query(uri, projection, null, null, sortOrder)
293+
}?.use { cursor ->
294+
if (cursor.moveToFirst()) {
295+
do {
296+
try {
297+
val name = cursor.getStringValue(MediaStore.Files.FileColumns.DISPLAY_NAME)
298+
if (!showHidden && name.startsWith(".")) {
299+
continue
300+
}
288301

289-
val size = cursor.getLongValue(MediaStore.Files.FileColumns.SIZE)
290-
if (size == 0L) {
291-
return@queryCursor
292-
}
302+
val size = cursor.getLongValue(MediaStore.Files.FileColumns.SIZE)
303+
if (size == 0L) {
304+
continue
305+
}
293306

294-
val path = cursor.getStringValue(MediaStore.Files.FileColumns.DATA)
295-
val lastModified = cursor.getLongValue(MediaStore.Files.FileColumns.DATE_MODIFIED) * 1000
296-
fileDirItems.add(FileDirItem(path, name, false, 0, size, lastModified))
297-
} catch (e: Exception) {
307+
val path = cursor.getStringValue(MediaStore.Files.FileColumns.DATA)
308+
val lastModified = cursor.getLongValue(MediaStore.Files.FileColumns.DATE_MODIFIED) * 1000
309+
fileDirItems.add(FileDirItem(path, name, false, 0, size, lastModified))
310+
} catch (e: Exception) {
311+
}
312+
} while (cursor.moveToNext())
298313
}
299314
}
300315
} catch (e: Exception) {

0 commit comments

Comments
 (0)