use new path guesser also in normal filebrowser

This commit is contained in:
Adrian Ulrich 2021-03-28 17:21:36 +02:00
parent 1cb700c3c2
commit cf0aaf1a57
3 changed files with 32 additions and 27 deletions

View File

@ -1,6 +1,6 @@
/*
* Copyright (C) 2010, 2011 Christopher Eby <kreed@kreed.org>
* Copyright (C) 2015-2016 Adrian Ulrich <adrian@blinkenlights.ch>
* Copyright (C) 2015-2021 Adrian Ulrich <adrian@blinkenlights.ch>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
@ -191,11 +191,14 @@ public class FileSystemAdapter
mFileObserver = new Observer(file.getPath());
}
ArrayList<File> files;
File[] readdir = file.listFiles(mFileFilter);
if (readdir == null)
readdir = new File[]{};
if (readdir != null) {
files = new ArrayList<File>(Arrays.asList(readdir));
} else {
files = FileUtils.getFallbackDirectories((Context)mActivity, file);
}
ArrayList<File> files = new ArrayList<File>(Arrays.asList(readdir));
Collections.sort(files, mFileComparator);
if (!mFsRoot.equals(file))
files.add(0, new File(file, FileUtils.NAME_PARENT_FOLDER));

View File

@ -24,8 +24,11 @@ import java.net.URI;
import java.net.URLConnection;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.StringTokenizer;
import java.nio.file.Path;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
@ -247,6 +250,27 @@ public class FileUtils {
return index > 0 ? filename.substring(index) : "";
}
/**
* Returns a list of directores contained in 'dir' which are very likely to exist, based
* on what Android told us about the existence of external media dirs.
* This is required as users otherwise may end up in folders they can not navigate out of.
*/
public static ArrayList<File> getFallbackDirectories(Context context, File dir) {
HashSet<File> result = new HashSet<>();
Path prefix = dir.toPath();
for (File f : context.getExternalMediaDirs()) {
Path p = f.toPath();
if (p.getNameCount() <= prefix.getNameCount())
continue;
if (!p.startsWith(prefix))
continue;
Path sp = p.subpath(prefix.getNameCount(), prefix.getNameCount()+1);
result.add(new File(dir, sp.toString()));
}
return new ArrayList<File>(result);
}
/**
* Returns the ID which the media library uses for a file.
*

View File

@ -33,10 +33,8 @@ import java.io.File;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.nio.file.Path;
public class FolderPickerAdapter
extends ArrayAdapter<FolderPickerAdapter.Item>
@ -192,7 +190,7 @@ public class FolderPickerAdapter
if (l != null) {
dirs = Arrays.asList(l);
} else {
dirs = getFallbackDirectories(path);
dirs = FileUtils.getFallbackDirectories(mContext, path);
}
Collections.sort(dirs);
@ -209,24 +207,4 @@ public class FolderPickerAdapter
}
}
/**
* Returns a list of directores contained in 'dir' which are very likely to exist, based
* on what Android told us about the existence of external media dirs.
* This is required as users otherwise may end up in folders they can not navigate out of.
*/
private List<File> getFallbackDirectories(File dir) {
HashSet<File> result = new HashSet<>();
Path prefix = dir.toPath();
for (File f : mContext.getExternalMediaDirs()) {
Path p = f.toPath();
if (p.getNameCount() <= prefix.getNameCount())
continue;
if (!p.startsWith(prefix))
continue;
Path sp = p.subpath(prefix.getNameCount(), prefix.getNameCount()+1);
result.add(new File(dir, sp.toString()));
}
return new ArrayList<File>(result);
}
}