Search in sources :

Example 1 with DocumentFile

use of android.support.v4.provider.DocumentFile in project frostwire by frostwire.

the class LollipopFileSystem method mkdirs.

@Override
public boolean mkdirs(File file) {
    if (file.mkdirs()) {
        return true;
    }
    DocumentFile f = getDirectory(app, file, false);
    if (f != null) {
        // already exists
        return false;
    }
    f = getDirectory(app, file, true);
    return f != null;
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile)

Example 2 with DocumentFile

use of android.support.v4.provider.DocumentFile in project frostwire by frostwire.

the class LollipopFileSystem method copy.

@Override
public boolean copy(File src, File dest) {
    try {
        FileUtils.copyFile(src, dest);
        return true;
    } catch (Throwable e) {
    // ignore
    }
    DocumentFile srcF = getFile(app, src, false);
    DocumentFile destF = getFile(app, dest, true);
    if (srcF == null) {
        LOG.error("Unable to obtain document for file: " + src);
        return false;
    }
    if (destF == null) {
        LOG.error("Unable to obtain or create document for file: " + dest);
        return false;
    }
    return copy(app, srcF, destF);
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile)

Example 3 with DocumentFile

use of android.support.v4.provider.DocumentFile in project frostwire by frostwire.

the class LollipopFileSystem method openFD.

public int openFD(File file, String mode) {
    if (!("r".equals(mode) || "w".equals(mode) || "rw".equals(mode))) {
        LOG.error("Only r, w or rw modes supported");
        return -1;
    }
    DocumentFile f = getFile(app, file, true);
    if (f == null) {
        LOG.error("Unable to obtain or create document for file: " + file);
        return -1;
    }
    try {
        ContentResolver cr = app.getContentResolver();
        ParcelFileDescriptor fd = cr.openFileDescriptor(f.getUri(), mode);
        if (fd == null) {
            return -1;
        }
        return fd.detachFd();
    } catch (Throwable e) {
        LOG.error("Unable to get native fd", e);
        return -1;
    }
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) ParcelFileDescriptor(android.os.ParcelFileDescriptor) ContentResolver(android.content.ContentResolver)

Example 4 with DocumentFile

use of android.support.v4.provider.DocumentFile in project frostwire by frostwire.

the class LollipopFileSystem method getDirectory.

private static DocumentFile getDirectory(Context context, File dir, boolean create) {
    try {
        String path = dir.getAbsolutePath();
        DocumentFile cached = CACHE.get(path);
        if (cached != null && cached.isDirectory()) {
            return cached;
        }
        String baseFolder = getExtSdCardFolder(context, dir);
        if (baseFolder == null) {
            if (create) {
                return dir.mkdirs() ? DocumentFile.fromFile(dir) : null;
            } else {
                return dir.isDirectory() ? DocumentFile.fromFile(dir) : null;
            }
        }
        baseFolder = combineRoot(baseFolder);
        String fullPath = dir.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);
        // special FrostWire case
        if (create) {
            if (baseFolder.endsWith("/FrostWire") && !f.exists()) {
                baseFolder = baseFolder.substring(0, baseFolder.length() - 10);
                rootUri = getDocumentUri(context, new File(baseFolder));
                f = DocumentFile.fromTreeUri(context, rootUri);
                f = f.findFile("FrostWire");
                if (f == null) {
                    f = f.createDirectory("FrostWire");
                    if (f == null) {
                        return null;
                    }
                }
            }
        }
        for (String segment : segments) {
            DocumentFile child = f.findFile(segment);
            if (child != null) {
                f = child;
            } else {
                if (create) {
                    f = f.createDirectory(segment);
                    if (f == null) {
                        return null;
                    }
                } else {
                    return null;
                }
            }
        }
        f = f.isDirectory() ? f : null;
        if (f != null) {
            CACHE.put(path, f);
        }
        return f;
    } catch (Throwable e) {
        LOG.error("Error getting directory: " + dir, 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 5 with DocumentFile

use of android.support.v4.provider.DocumentFile in project frostwire by frostwire.

the class StoragePicker method handle.

public static String handle(Context context, int requestCode, int resultCode, Intent data) {
    String result = null;
    try {
        if (resultCode == Activity.RESULT_OK && requestCode == SELECT_FOLDER_REQUEST_CODE) {
            Uri treeUri = data.getData();
            ContentResolver cr = context.getContentResolver();
            final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            cr.takePersistableUriPermission(treeUri, takeFlags);
            if (treeUri == null) {
                UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_null);
                result = null;
            } else {
                DocumentFile file = DocumentFile.fromTreeUri(context, treeUri);
                if (!file.isDirectory()) {
                    UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_not_directory);
                    result = null;
                } else if (!file.canWrite()) {
                    UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_cant_write);
                    result = null;
                } else {
                    LollipopFileSystem fs = (LollipopFileSystem) Platforms.fileSystem();
                    result = fs.getTreePath(treeUri);
                    if (result != null && !result.endsWith("/FrostWire")) {
                        DocumentFile f = file.findFile("FrostWire");
                        if (f == null) {
                            file.createDirectory("FrostWire");
                        }
                    }
                }
            }
        }
    } catch (Throwable e) {
        UIUtils.showShortMessage(context, R.string.storage_picker_treeuri_error);
        LOG.error("Error handling folder selection", e);
        result = null;
    }
    if (result != null) {
        ConfigurationManager.instance().setStoragePath(result);
        BTEngine.ctx.dataDir = Platforms.data();
        BTEngine.ctx.torrentsDir = Platforms.torrents();
    }
    return result;
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) Uri(android.net.Uri) ContentResolver(android.content.ContentResolver)

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