From 1b64f746967ef2fff7b90c5d47134131f40faee5 Mon Sep 17 00:00:00 2001 From: Lalit2-at-420259454397 Date: Wed, 31 Jan 2024 00:02:01 +0530 Subject: [PATCH 1/4] 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() { From 6304fdcac438ac05e78b1349fa93f2709a9b3994 Mon Sep 17 00:00:00 2001 From: Vishnu Sanal T Date: Sun, 13 Oct 2024 21:22:07 +0530 Subject: [PATCH 2/4] address review comments --- .../asynctasks/LoadFilesListTask.java | 39 +++++++++---------- 1 file changed, 19 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 8e99b2ee58..29245e1e1f 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,28 +536,26 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) { } private @Nullable List listRecentFiles() { - final Context context = this.context.get(); - final MainFragment mainFragment = this.mainFragmentReference.get(); + final Context c = context.get(); - if (context == null) { + if (c == null) { cancel(true); return null; } - MainFragmentViewModel viewModel = mainFragment.getMainFragmentViewModel(); + List recentFiles = new ArrayList<>(40); - Cursor cursor = getCursor(context); + Cursor cursor = getRecentFilesCursor(c); 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); + String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)); + File f = new File(filePath); if (f.isDirectory()) { List files = getFilesFromDirectory(f); - for (File file : files) { - compareFileAndAddToList(viewModel, recentFiles, file); - } - } + for (File file : files) + compareFileAndAddToList(recentFiles, file); + } else compareFileAndAddToList(recentFiles, f); } while (cursor.moveToNext()); } cursor.close(); @@ -565,39 +563,41 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) { } @Nullable - private Cursor getCursor(Context context) { + private Cursor getRecentFilesCursor(Context c) { final String[] projection = { - MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.DATE_MODIFIED + MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DATE_MODIFIED }; Cursor cursor; if (SDK_INT >= Q) { Bundle queryArgs = new Bundle(); queryArgs.putStringArray( ContentResolver.QUERY_ARG_SORT_COLUMNS, - new String[] {MediaStore.Files.FileColumns.DATE_MODIFIED}); + new String[] {MediaStore.MediaColumns.DATE_MODIFIED}); queryArgs.putInt( ContentResolver.QUERY_ARG_SORT_DIRECTION, ContentResolver.QUERY_SORT_DIRECTION_DESCENDING); + queryArgs.putInt( + ContentResolver.QUERY_ARG_LIMIT, + 100); cursor = - context + c .getContentResolver() .query(MediaStore.Files.getContentUri("external"), projection, queryArgs, null); } else { cursor = - context + c .getContentResolver() .query( MediaStore.Files.getContentUri("external"), projection, null, null, - MediaStore.Files.FileColumns.DATE_MODIFIED + " DESC LIMIT 20"); + MediaStore.MediaColumns.DATE_MODIFIED + " DESC LIMIT 100"); } return cursor; } - private void compareFileAndAddToList( - MainFragmentViewModel viewModel, List recentFiles, File file) { + private void compareFileAndAddToList(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(); @@ -608,7 +608,6 @@ private void compareFileAndAddToList( LayoutElementParcelable parcelable = createListParcelables(strings); if (parcelable != null) { recentFiles.add(parcelable); - viewModel.incrementFileCount(); } } } From 76fa5dc1aeba6fcb39264c6ff0a16fe4c87b6e6b Mon Sep 17 00:00:00 2001 From: Vishnu Sanal T Date: Sun, 13 Oct 2024 21:51:43 +0530 Subject: [PATCH 3/4] use `ListFilesCommand#listFiles` --- .../asynctasks/LoadFilesListTask.java | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 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 29245e1e1f..6fb882f7f9 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 @@ -543,6 +543,9 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) { return null; } + final MainFragment mainFragment = mainFragmentReference.get(); + MainFragmentViewModel viewModel = mainFragment.getMainFragmentViewModel(); + List recentFiles = new ArrayList<>(40); Cursor cursor = getRecentFilesCursor(c); @@ -552,10 +555,10 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) { String filePath = cursor.getString(cursor.getColumnIndex(MediaStore.MediaColumns.DATA)); File f = new File(filePath); if (f.isDirectory()) { - List files = getFilesFromDirectory(f); + List files = getFilesFromDirectory(mainFragment, filePath); for (File file : files) - compareFileAndAddToList(recentFiles, file); - } else compareFileAndAddToList(recentFiles, f); + compareFileAndAddToList(viewModel, recentFiles, file); + } else compareFileAndAddToList(viewModel, recentFiles, f); } while (cursor.moveToNext()); } cursor.close(); @@ -597,7 +600,7 @@ private Cursor getRecentFilesCursor(Context c) { return cursor; } - private void compareFileAndAddToList(List recentFiles, File file) { + 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(); @@ -608,32 +611,34 @@ private void compareFileAndAddToList(List recentFiles, LayoutElementParcelable parcelable = createListParcelables(strings); if (parcelable != null) { recentFiles.add(parcelable); + viewModel.incrementFileCount(); } } } } /** - * Recursively fetches the files from directory tree and adds all the files in a list + * fetches the files from directory tree and adds all the files in a list * - * @param f: File + * @param mainFragment: the main fragment reference + * @param filePath: the file filePath * @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 List getFilesFromDirectory(MainFragment mainFragment, String filePath) { + + List files = new ArrayList<>(); + + ListFilesCommand.INSTANCE.listFiles( + filePath, + mainFragment.requireMainActivity().isRootExplorer(), + showHiddenFiles, + mode -> { return null; }, + hybridFileParcelable -> { + files.add(hybridFileParcelable.getFile()); + return null; + }); + + return files; } private @Nullable List listTrashBinFiles() { From 0be590d1ca00e75bc42cd4a4c68b5835a8f8e0fe Mon Sep 17 00:00:00 2001 From: Vishnu Sanal T Date: Sun, 13 Oct 2024 21:54:18 +0530 Subject: [PATCH 4/4] chore: spotless --- .../asynctasks/LoadFilesListTask.java | 36 +++++++++---------- 1 file changed, 17 insertions(+), 19 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 6fb882f7f9..0ec17cebfc 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 @@ -556,8 +556,7 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) { File f = new File(filePath); if (f.isDirectory()) { List files = getFilesFromDirectory(mainFragment, filePath); - for (File file : files) - compareFileAndAddToList(viewModel, recentFiles, file); + for (File file : files) compareFileAndAddToList(viewModel, recentFiles, file); } else compareFileAndAddToList(viewModel, recentFiles, f); } while (cursor.moveToNext()); } @@ -568,7 +567,7 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) { @Nullable private Cursor getRecentFilesCursor(Context c) { final String[] projection = { - MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DATE_MODIFIED + MediaStore.MediaColumns.DATA, MediaStore.MediaColumns.DATE_MODIFIED }; Cursor cursor; if (SDK_INT >= Q) { @@ -579,17 +578,13 @@ private Cursor getRecentFilesCursor(Context c) { queryArgs.putInt( ContentResolver.QUERY_ARG_SORT_DIRECTION, ContentResolver.QUERY_SORT_DIRECTION_DESCENDING); - queryArgs.putInt( - ContentResolver.QUERY_ARG_LIMIT, - 100); + queryArgs.putInt(ContentResolver.QUERY_ARG_LIMIT, 100); cursor = - c - .getContentResolver() + c.getContentResolver() .query(MediaStore.Files.getContentUri("external"), projection, queryArgs, null); } else { cursor = - c - .getContentResolver() + c.getContentResolver() .query( MediaStore.Files.getContentUri("external"), projection, @@ -600,7 +595,8 @@ private Cursor getRecentFilesCursor(Context c) { return cursor; } - private void compareFileAndAddToList(MainFragmentViewModel viewModel, List recentFiles, File file) { + 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(); @@ -629,14 +625,16 @@ private List getFilesFromDirectory(MainFragment mainFragment, String fileP List files = new ArrayList<>(); ListFilesCommand.INSTANCE.listFiles( - filePath, - mainFragment.requireMainActivity().isRootExplorer(), - showHiddenFiles, - mode -> { return null; }, - hybridFileParcelable -> { - files.add(hybridFileParcelable.getFile()); - return null; - }); + filePath, + mainFragment.requireMainActivity().isRootExplorer(), + showHiddenFiles, + mode -> { + return null; + }, + hybridFileParcelable -> { + files.add(hybridFileParcelable.getFile()); + return null; + }); return files; }