Search in sources :

Example 36 with DocumentFile

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;
    }
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) File(java.io.File) DocumentFile(android.support.v4.provider.DocumentFile)

Example 37 with DocumentFile

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];
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) ArrayList(java.util.ArrayList) File(java.io.File) DocumentFile(android.support.v4.provider.DocumentFile) Uri(android.net.Uri)

Example 38 with DocumentFile

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;
    }
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) Uri(android.net.Uri) File(java.io.File) DocumentFile(android.support.v4.provider.DocumentFile)

Example 39 with DocumentFile

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;
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile)

Example 40 with DocumentFile

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;
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile)

Aggregations

DocumentFile (android.support.v4.provider.DocumentFile)65 File (java.io.File)36 IOException (java.io.IOException)22 Uri (android.net.Uri)21 ContentResolver (android.content.ContentResolver)12 SmbFile (jcifs.smb.SmbFile)11 MalformedURLException (java.net.MalformedURLException)10 SmbException (jcifs.smb.SmbException)10 FileOutputStream (java.io.FileOutputStream)9 ShellNotRunningException (com.amaze.filemanager.exceptions.ShellNotRunningException)8 OutputStream (java.io.OutputStream)8 InputStream (java.io.InputStream)7 SuppressLint (android.annotation.SuppressLint)6 CloudStorage (com.cloudrail.si.interfaces.CloudStorage)6 FileInputStream (java.io.FileInputStream)6 SFtpClientTemplate (com.amaze.filemanager.filesystem.ssh.SFtpClientTemplate)4 BufferedOutputStream (java.io.BufferedOutputStream)4 FileNotFoundException (java.io.FileNotFoundException)4 FileChannel (java.nio.channels.FileChannel)4 SFTPClient (net.schmizz.sshj.sftp.SFTPClient)4