Skip to content

Commit e855ea5

Browse files
committed
Fixes #3991
RCA - There was a check in `LoadFilesListTask.listRecentFiles()` that only list the file if it is not directory, which is not a case if file is not created in `storage/emulated0/` directory. The recently changed file would always come wrapped in some folder if created in a directory other than `storage/emulated0/`. Which was causing this issue. Resolution - In the solution provided it is assumed that we have to only show the files that were modified, hence, only modified/created files are listed and not the folders (as the title suggests ‘Recent Files’). Here, if the file is wrapped in a directory, the directory is recursively explored to check the files it contains. As we loop around recursively we go on adding the files that we find in a final list that is to be returned. In this code, every file that comes from cursor will always be wrapped in some folder and it will be explored recursively and listed. Co-authored-by: Lalit2-at-420259454397 <i0PyMDN2fLAzHZkUWuyruefG2clLhyinw6Gna7H9WWM=> Signed-off-by: VishnuSanal <t.v.s10123@gmail.com>
1 parent 62b9545 commit e855ea5

1 file changed

Lines changed: 77 additions & 25 deletions

File tree

app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/LoadFilesListTask.java

Lines changed: 77 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -563,16 +563,38 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) {
563563
}
564564

565565
private @Nullable List<LayoutElementParcelable> listRecentFiles() {
566-
final Context context = this.context.get();
566+
final Context c = context.get();
567567

568-
if (context == null) {
568+
if (c == null) {
569569
cancel(true);
570570
return null;
571571
}
572572

573-
List<LayoutElementParcelable> recentFiles = new ArrayList<>(20);
573+
final MainFragment mainFragment = mainFragmentReference.get();
574+
MainFragmentViewModel viewModel = mainFragment.getMainFragmentViewModel();
575+
576+
List<LayoutElementParcelable> recentFiles = new ArrayList<>(40);
577+
578+
Cursor cursor = getRecentFilesCursor(c);
579+
if (cursor == null) return recentFiles;
580+
if (cursor.getCount() > 0 && cursor.moveToFirst()) {
581+
do {
582+
String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA));
583+
File f = new File(filePath);
584+
if (f.isDirectory()) {
585+
List<File> files = getFilesFromDirectory(mainFragment, filePath);
586+
for (File file : files) compareFileAndAddToList(viewModel, recentFiles, file);
587+
} else compareFileAndAddToList(viewModel, recentFiles, f);
588+
} while (cursor.moveToNext());
589+
}
590+
cursor.close();
591+
return recentFiles;
592+
}
593+
594+
@Nullable
595+
private Cursor getRecentFilesCursor(Context c) {
574596
final String[] projection = {
575-
MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.DATE_MODIFIED
597+
MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DATE_MODIFIED
576598
};
577599
final String selection = MediaStore.Files.FileColumns.DATA + " NOT LIKE ?";
578600
final String[] selectionArgs = new String[] {AppConfig.getTrashBinBasePath() + "%"};
@@ -583,51 +605,81 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) {
583605
try {
584606
if (SDK_INT >= Q) {
585607
Bundle queryArgs = new Bundle();
586-
queryArgs.putInt(ContentResolver.QUERY_ARG_LIMIT, 20);
608+
587609
queryArgs.putStringArray(
588610
ContentResolver.QUERY_ARG_SORT_COLUMNS,
589-
new String[] {MediaStore.Files.FileColumns.DATE_MODIFIED});
611+
new String[] {MediaStore.MediaColumns.DATE_MODIFIED});
590612
queryArgs.putInt(
591613
ContentResolver.QUERY_ARG_SORT_DIRECTION,
592-
ContentResolver.QUERY_SORT_DIRECTION_DESCENDING);
614+
ContentResolver.QUERY_SORT_DIRECTION_DESCENDING);queryArgs.putInt(
615+
ContentResolver.QUERY_ARG_LIMIT,
616+
100);
593617
queryArgs.putString(ContentResolver.QUERY_ARG_SQL_SELECTION, selection);
594618
queryArgs.putStringArray(ContentResolver.QUERY_ARG_SQL_SELECTION_ARGS, selectionArgs);
595619
cursor =
596-
context
620+
c
597621
.getContentResolver()
598622
.query(MediaStore.Files.getContentUri("external"), projection, queryArgs, null);
599623
} else {
600624
cursor =
601-
context
625+
c
602626
.getContentResolver()
603627
.query(
604628
MediaStore.Files.getContentUri("external"),
605629
projection,
606630
selection,
607631
selectionArgs,
608-
MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC LIMIT 20");
632+
MediaStore.MediaColumns.DATE_MODIFIED + " DESC LIMIT 100");
609633
}
610634
} catch (SecurityException e) {
611635
// Storage permission denied; treat as no recent files instead of crashing.
612636
return recentFiles;
613637
}
614-
if (cursor == null) return recentFiles;
615-
if (cursor.getCount() > 0 && cursor.moveToFirst()) {
616-
do {
617-
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA));
618-
File f = new File(path);
619-
if (d.compareTo(new Date(f.lastModified())) != 1 && !f.isDirectory()) {
620-
HybridFileParcelable strings =
621-
RootHelper.generateBaseFile(new File(path), showHiddenFiles);
622-
if (strings != null) {
623-
LayoutElementParcelable parcelable = createListParcelables(strings);
624-
if (parcelable != null) recentFiles.add(parcelable);
625-
}
638+
return cursor;
639+
}
640+
641+
private void compareFileAndAddToList(
642+
MainFragmentViewModel viewModel, List<LayoutElementParcelable> recentFiles, File file) {
643+
Calendar c = Calendar.getInstance();
644+
c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) - 2);
645+
Date d = c.getTime();
646+
647+
if (d.compareTo(new Date(file.lastModified())) != 1 && !file.isDirectory()) {
648+
HybridFileParcelable strings = RootHelper.generateBaseFile(file, showHiddenFiles);
649+
if (strings != null) {
650+
LayoutElementParcelable parcelable = createListParcelables(strings);
651+
if (parcelable != null) {
652+
recentFiles.add(parcelable);
653+
viewModel.incrementFileCount();
626654
}
627-
} while (cursor.moveToNext());
655+
}
628656
}
629-
cursor.close();
630-
return recentFiles;
657+
}
658+
659+
/**
660+
* fetches the files from directory tree and adds all the files in a list
661+
*
662+
* @param mainFragment: the main fragment reference
663+
* @param filePath: the file filePath
664+
* @return List of files in directory tree.
665+
*/
666+
private List<File> getFilesFromDirectory(MainFragment mainFragment, String filePath) {
667+
668+
List<File> files = new ArrayList<>();
669+
670+
ListFilesCommand.INSTANCE.listFiles(
671+
filePath,
672+
mainFragment.requireMainActivity().isRootExplorer(),
673+
showHiddenFiles,
674+
mode -> {
675+
return null;
676+
},
677+
hybridFileParcelable -> {
678+
files.add(hybridFileParcelable.getFile());
679+
return null;
680+
});
681+
682+
return files;
631683
}
632684

633685
private @Nullable List<LayoutElementParcelable> listTrashBinFiles() {

0 commit comments

Comments
 (0)