Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Lalit2-at-420259454397 authored and Lalit2-at-420259454397 committed Jan 30, 2024
1 parent 12d4990 commit 1b64f74
Showing 1 changed file with 63 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -536,23 +536,41 @@ else if (cursor.getCount() > 0 && cursor.moveToFirst()) {

private @Nullable List<LayoutElementParcelable> 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<LayoutElementParcelable> 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<File> files = getFilesFromDirectory(f);
for (File file : files) {
compareFileAndAddToList(viewModel, recentFiles, file);
}
}
} while (cursor.moveToNext());
}
cursor.close();
return recentFiles;
}

List<LayoutElementParcelable> 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});
Expand All @@ -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<LayoutElementParcelable> 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<File> getFilesFromDirectory(File f) {
List<File> 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<LayoutElementParcelable> listTrashBinFiles() {
Expand Down

0 comments on commit 1b64f74

Please sign in to comment.