Skip to content

Commit

Permalink
Many fixes, setting current folder outside the async func
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Dec 12, 2024
1 parent 9362504 commit 2257a83
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 53 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class GsFileBrowserListAdapter extends RecyclerView.Adapter<GsFileBrowser
public static final File VIRTUAL_STORAGE_RECENTS = new File(VIRTUAL_STORAGE_ROOT, "Recent");
public static final File VIRTUAL_STORAGE_FAVOURITE = new File(VIRTUAL_STORAGE_ROOT, "Favourites");
public static final File VIRTUAL_STORAGE_POPULAR = new File(VIRTUAL_STORAGE_ROOT, "Popular");
public static final File VIRTUAL_STORAGE_APP_DATA_PRIVATE = new File(VIRTUAL_STORAGE_ROOT, "appdata-private");
public static final File VIRTUAL_STORAGE_APP_DATA_PRIVATE = new File(VIRTUAL_STORAGE_ROOT, "AppData (data partition)");
private static final File GO_BACK_SIGNIFIER = new File("__GO_BACK__");
private static final StrikethroughSpan STRIKE_THROUGH_SPAN = new StrikethroughSpan();
public static final String EXTRA_CURRENT_FOLDER = "EXTRA_CURRENT_FOLDER";
Expand Down Expand Up @@ -160,15 +160,13 @@ public void updateVirtualFolders() {
}

for (final File file : ContextCompat.getExternalFilesDirs(_context, null)) {
final File remap = new File(VIRTUAL_STORAGE_ROOT, "appdata-public (" + file.getName() + ")");
if (file == null || (file != null && file.getParentFile() == null)) {
continue;
}
final File remap = new File(VIRTUAL_STORAGE_ROOT, "AppData (" + file.getParentFile().toString().replace("/", "-").substring(1) + ")");
_virtualMapping.put(remap, file);
}

for (final Pair<File, String> p : cu.getAppDataPublicDirs(_context, false, true, false)) {
final File remap = new File(VIRTUAL_STORAGE_ROOT, "sdcard (" + p.second + ")");
_virtualMapping.put(remap, p.first);
}

final File download = getDownloadFolder();
if (download != null) {
_virtualMapping.put(new File(VIRTUAL_STORAGE_ROOT, "Download"), download);
Expand Down Expand Up @@ -598,7 +596,7 @@ public File createDirectoryHere(final CharSequence name) {

final String trimmed = name.toString().trim();

if (trimmed.length() == 0) {
if (trimmed.isEmpty()) {
return null;
}

Expand Down Expand Up @@ -709,26 +707,27 @@ private void loadFolder(final File folder, final File show) {
final File toShow = show == null ? _fileToShowAfterNextLoad : show;
_fileToShowAfterNextLoad = null;

_currentFolder = folder;

try {
executorService.execute(() -> _loadFolder(toLoad, toShow));
executorService.execute(() -> _loadFolder(folderChanged, toShow));
} catch (RejectedExecutionException ignored) { // during exit
}
}

// This function is not called on the main thread, so post to the UI thread
private synchronized void _loadFolder(final @NonNull File folder, final @Nullable File toShow) {
// This function is not called on the main thread
private synchronized void _loadFolder(final boolean folderChanged, final @Nullable File toShow) {

if (_recyclerView == null) {
if (_recyclerView == null || _currentFolder == null) {
return;
}

final boolean folderChanged = !folder.equals(_currentFolder);
final List<File> newData = new ArrayList<>();

// Make sure /storage/emulated/0 is browsable, even though filesystem says it's not accessible
if (folder.equals(new File("/"))) {
if (_currentFolder.equals(new File("/"))) {
newData.add(VIRTUAL_STORAGE_ROOT);
} else if (folder.equals(VIRTUAL_STORAGE_ROOT)) {
} else if (_currentFolder.equals(VIRTUAL_STORAGE_ROOT)) {
newData.addAll(_virtualMapping.keySet());

// SD Card and other external storage directories that are also not listable
Expand All @@ -739,34 +738,36 @@ private synchronized void _loadFolder(final @NonNull File folder, final @Nullabl
}
newData.add(f);
}
} else if (folder.equals(VIRTUAL_STORAGE_EMULATED)) {
newData.add(new File(folder, "" + _userId));
} else if (folder.equals(VIRTUAL_STORAGE_RECENTS)) {
} else if (_currentFolder.equals(VIRTUAL_STORAGE_EMULATED)) {
newData.add(new File(_currentFolder, "" + _userId));
} else if (_currentFolder.equals(VIRTUAL_STORAGE_RECENTS)) {
newData.addAll(_dopt.recentFiles);
} else if (folder.equals(VIRTUAL_STORAGE_POPULAR)) {
} else if (_currentFolder.equals(VIRTUAL_STORAGE_POPULAR)) {
newData.addAll(_dopt.popularFiles);
} else if (folder.equals(VIRTUAL_STORAGE_FAVOURITE)) {
} else if (_currentFolder.equals(VIRTUAL_STORAGE_FAVOURITE)) {
newData.addAll(_dopt.favouriteFiles);
} else if (folder.isDirectory()) {
GsCollectionUtils.addAll(newData, folder.listFiles(GsFileBrowserListAdapter.this));
}

if (_currentFolder.isDirectory() && _currentFolder.canRead()) {
GsCollectionUtils.addAll(newData, _currentFolder.listFiles(GsFileBrowserListAdapter.this));
}

GsCollectionUtils.deduplicate(newData);

// Don't sort recent or virtual root items - use the default order
if (!Arrays.asList(VIRTUAL_STORAGE_RECENTS, VIRTUAL_STORAGE_ROOT).contains(folder)) {
if (!Arrays.asList(VIRTUAL_STORAGE_RECENTS, VIRTUAL_STORAGE_ROOT).contains(_currentFolder)) {
GsFileUtils.sortFiles(newData, _dopt.sortByType, _dopt.sortFolderFirst, _dopt.sortReverse);
}

// Testing if modtimes have changed (modtimes generally only increase)
final long modSum = GsCollectionUtils.accumulate(newData, (f, s) -> s + f.lastModified(), 0L);
final boolean modSumChanged = modSum != _prevModSum;

if (canGoUp(folder)) {
if (isVirtualFolder(folder) || _virtualMapping.containsValue(folder) || GsFileUtils.isChild(VIRTUAL_STORAGE_ROOT, folder)) {
if (canGoUp(_currentFolder)) {
if (isVirtualFolder(_currentFolder) || _virtualMapping.containsValue(_currentFolder) || GsFileUtils.isChild(VIRTUAL_STORAGE_ROOT, _currentFolder)) {
newData.add(0, VIRTUAL_STORAGE_ROOT);
} else {
newData.add(0, folder.getParentFile());
newData.add(0, _currentFolder.getParentFile());
}
}

Expand All @@ -781,7 +782,6 @@ private synchronized void _loadFolder(final @NonNull File folder, final @Nullabl
_adapterDataFiltered.clear();
_adapterDataFiltered.addAll(filteredData);
_currentSelection.retainAll(_adapterData);
_currentFolder = folder;
_prevModSum = modSum;

if (folderChanged) {
Expand Down
26 changes: 0 additions & 26 deletions app/src/main/java/net/gsantner/opoc/util/GsContextUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2935,30 +2935,4 @@ public void onAnimationEnd(Animator animation) {

return true;
}

public static boolean openFolderInFileManager(final Context context, final File folder) {
if (!folder.canRead() || !folder.isDirectory()) {
return false;
}

final Intent intent = new Intent(Intent.ACTION_VIEW);

Uri uri = Uri.fromFile(folder);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
uri = Uri.parse(folder.toURI().toString()); // Use the correct URI for Android Nougat and above
intent.setDataAndType(uri, "*/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
intent.setDataAndType(uri, "resource/folder");
}

intent.addCategory(Intent.CATEGORY_DEFAULT);

if (intent.resolveActivity(context.getPackageManager()) != null) {
context.startActivity(intent);
return true;
}

return false;
}
}

0 comments on commit 2257a83

Please sign in to comment.