use of android.support.v4.provider.DocumentFile in project frostwire by frostwire.
the class LollipopFileSystem method getFile.
private static DocumentFile getFile(Context context, File file, boolean create) {
try {
String path = file.getAbsolutePath();
DocumentFile cached = CACHE.get(path);
if (cached != null && cached.isFile()) {
return cached;
}
File parent = file.getParentFile();
if (parent == null) {
return DocumentFile.fromFile(file);
}
DocumentFile f = getDirectory(context, parent, false);
if (f == null && create) {
f = getDirectory(context, parent, create);
}
if (f != null) {
String name = file.getName();
DocumentFile child = f.findFile(name);
if (child != null) {
if (child.isFile()) {
f = child;
} else {
f = null;
}
} else {
if (create) {
f = f.createFile("application/octet-stream", name);
} else {
f = null;
}
}
}
if (f != null) {
CACHE.put(path, f);
}
return f;
} catch (Throwable e) {
LOG.error("Error getting file: " + file, e);
return null;
}
}
use of android.support.v4.provider.DocumentFile in project frostwire by frostwire.
the class LollipopFileSystem method listFiles.
@Override
public File[] listFiles(File file, FileFilter filter) {
try {
File[] files = file.listFiles(filter);
if (files != null) {
return files;
}
} catch (Throwable e) {
// ignore, try with SAF
}
LOG.warn("Using SAF for listFiles, could be a costly operation");
DocumentFile f = getDirectory(app, file, false);
if (f == null) {
// does not exists
return null;
}
DocumentFile[] files = f.listFiles();
if (filter != null && files != null) {
List<File> result = new ArrayList<>(files.length);
for (DocumentFile file1 : files) {
Uri uri = file1.getUri();
String path = getDocumentPath(uri);
if (path == null) {
continue;
}
File child = new File(path);
if (filter.accept(child)) {
result.add(child);
}
}
return result.toArray(new File[0]);
}
return new File[0];
}
use of android.support.v4.provider.DocumentFile in project frostwire by frostwire.
the class LollipopFileSystem method getDocument.
private static DocumentFile getDocument(Context context, File file) {
try {
String path = file.getAbsolutePath();
DocumentFile cached = CACHE.get(path);
if (cached != null) {
return cached;
}
String baseFolder = getExtSdCardFolder(context, file);
if (baseFolder == null) {
return file.exists() ? DocumentFile.fromFile(file) : null;
}
baseFolder = combineRoot(baseFolder);
String fullPath = file.getAbsolutePath();
String relativePath = baseFolder.length() < fullPath.length() ? fullPath.substring(baseFolder.length() + 1) : "";
String[] segments = relativePath.split("/");
Uri rootUri = getDocumentUri(context, new File(baseFolder));
DocumentFile f = DocumentFile.fromTreeUri(context, rootUri);
for (String segment : segments) {
DocumentFile child = f.findFile(segment);
if (child != null) {
f = child;
} else {
return null;
}
}
if (f != null) {
CACHE.put(path, f);
}
return f;
} catch (Throwable e) {
LOG.error("Error getting document: " + file, e);
return null;
}
}
use of android.support.v4.provider.DocumentFile in project frostwire by frostwire.
the class LollipopFileSystem method length.
@Override
public long length(File file) {
long r = file.length();
if (r > 0) {
return r;
}
DocumentFile f = getDocument(app, file);
return f != null ? f.length() : 0;
}
use of android.support.v4.provider.DocumentFile in project frostwire by frostwire.
the class LollipopFileSystem method lastModified.
@Override
public long lastModified(File file) {
long r = file.lastModified();
if (r > 0) {
return r;
}
DocumentFile f = getDocument(app, file);
return f != null ? f.lastModified() : 0;
}
Aggregations