From 1b64f746967ef2fff7b90c5d47134131f40faee5 Mon Sep 17 00:00:00 2001 From: Lalit2-at-420259454397 Date: Wed, 31 Jan 2024 00:02:01 +0530 Subject: [PATCH] Fixes #3991 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- .../asynctasks/LoadFilesListTask.java | 83 ++++++++++++++----- 1 file changed, 63 insertions(+), 20 deletions(-) diff --git a/app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/LoadFilesListTask.java b/app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/LoadFilesListTask.java index 6f031fc2b9..42ce33f08f 100644 --- a/app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/LoadFilesListTask.java +++ b/app/src/main/java/com/amaze/filemanager/asynchronous/asynctasks/LoadFilesListTask.java @@ -536,23 +536,41 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) { private @Nullable List listRecentFiles() { final Context context = this.context.get(); + final MainFragment mainFragment = this.mainFragmentReference.get(); if (context == null) { cancel(true); return null; } + MainFragmentViewModel viewModel = mainFragment.getMainFragmentViewModel(); + List recentFiles = new ArrayList<>(40); + + Cursor cursor = getCursor(context); + if (cursor == null) return recentFiles; + if (cursor.getCount() > 0 && cursor.moveToFirst()) { + do { + String path = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA)); + File f = new File(path); + if (f.isDirectory()) { + List files = getFilesFromDirectory(f); + for (File file : files) { + compareFileAndAddToList(viewModel, recentFiles, file); + } + } + } while (cursor.moveToNext()); + } + cursor.close(); + return recentFiles; + } - List recentFiles = new ArrayList<>(20); + @Nullable + private Cursor getCursor(Context context) { final String[] projection = { MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.DATE_MODIFIED }; - Calendar c = Calendar.getInstance(); - c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) - 2); - Date d = c.getTime(); Cursor cursor; if (SDK_INT >= Q) { Bundle queryArgs = new Bundle(); - queryArgs.putInt(ContentResolver.QUERY_ARG_LIMIT, 20); queryArgs.putStringArray( ContentResolver.QUERY_ARG_SORT_COLUMNS, new String[] {MediaStore.Files.FileColumns.DATE_MODIFIED}); @@ -574,23 +592,48 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) { null, MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC LIMIT 20"); } - if (cursor == null) return recentFiles; - if (cursor.getCount() > 0 && cursor.moveToFirst()) { - do { - String path = cursor.getString(cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA)); - File f = new File(path); - if (d.compareTo(new Date(f.lastModified())) != 1 && !f.isDirectory()) { - HybridFileParcelable strings = - RootHelper.generateBaseFile(new File(path), showHiddenFiles); - if (strings != null) { - LayoutElementParcelable parcelable = createListParcelables(strings); - if (parcelable != null) recentFiles.add(parcelable); - } + return cursor; + } + + private void compareFileAndAddToList( + MainFragmentViewModel viewModel, List recentFiles, File file) { + Calendar c = Calendar.getInstance(); + c.set(Calendar.DAY_OF_YEAR, c.get(Calendar.DAY_OF_YEAR) - 2); + Date d = c.getTime(); + + if (d.compareTo(new Date(file.lastModified())) != 1 && !file.isDirectory()) { + HybridFileParcelable strings = RootHelper.generateBaseFile(file, showHiddenFiles); + if (strings != null) { + LayoutElementParcelable parcelable = createListParcelables(strings); + if (parcelable != null) { + recentFiles.add(parcelable); + viewModel.incrementFileCount(); } - } while (cursor.moveToNext()); + } } - cursor.close(); - return recentFiles; + } + + /** + * Recursively fetches the files from directory tree and adds all the files in a list + * + * @param f: File + * @return List of files in directory tree. + */ + private List getFilesFromDirectory(File f) { + List allFilesInDir = new ArrayList<>(); + try { + File[] files = f.listFiles(); + for (File file : files) { + if (file.isDirectory()) { + getFilesFromDirectory(file); + } else { + allFilesInDir.add(file); + } + } + } catch (Exception exception) { + LOG.error(exception.getLocalizedMessage()); + } + return allFilesInDir; } private @Nullable List listTrashBinFiles() {