Search in sources :

Example 26 with DocumentFile

use of android.support.v4.provider.DocumentFile in project Shuttle by timusus.

the class SafManager method getWriteableDocumentFiles.

/**
 * Checks the passed in paths to see whether the file at the given path is available in our
 * document tree. If it is, and we have write permission, the document file is added to the
 * passed in list of document files.
 */
public List<DocumentFile> getWriteableDocumentFiles(List<File> files) {
    List<DocumentFile> documentFiles = new ArrayList<>();
    String treeUri = getDocumentTree();
    if (treeUri == null) {
        // We don't have any document tree at all - so we're not going to have permission for any files.
        return documentFiles;
    }
    // we're satisfied we don't have permission.
    for (File file : files) {
        DocumentFile documentFile = getWriteableDocumentFile(file);
        if (documentFile != null && documentFile.canWrite()) {
            documentFiles.add(documentFile);
        }
    }
    return documentFiles;
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) ArrayList(java.util.ArrayList) File(java.io.File) DocumentFile(android.support.v4.provider.DocumentFile)

Example 27 with DocumentFile

use of android.support.v4.provider.DocumentFile in project Shuttle by timusus.

the class DeleteDialog method deleteSongs.

@SuppressLint("CheckResult")
Single<Integer> deleteSongs() {
    return Single.fromCallable(() -> {
        int deletedSongs = 0;
        if (!documentFilesForDeletion.isEmpty()) {
            deletedSongs += Stream.of(documentFilesForDeletion).filter(DocumentFile::delete).count();
            tidyUp(songsForSafDeletion);
            documentFilesForDeletion.clear();
            songsForSafDeletion.clear();
        }
        if (!songsForNormalDeletion.isEmpty()) {
            deletedSongs += Stream.of(songsForNormalDeletion).filter(SongExtKt::delete).count();
            tidyUp(songsForNormalDeletion);
            songsForNormalDeletion.clear();
        }
        return deletedSongs;
    });
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) SongExtKt(com.simplecity.amp_library.utils.extensions.SongExtKt) SuppressLint(android.annotation.SuppressLint) SuppressLint(android.annotation.SuppressLint)

Example 28 with DocumentFile

use of android.support.v4.provider.DocumentFile in project cw-omnibus by commonsguy.

the class DocumentStorageService method load.

private void load(Uri document) {
    try {
        boolean weHavePermission = false;
        boolean isContent = ContentResolver.SCHEME_CONTENT.equals(document.getScheme());
        if (isContent) {
            int perms = Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION;
            getContentResolver().takePersistableUriPermission(document, perms);
            for (UriPermission perm : getContentResolver().getPersistedUriPermissions()) {
                if (perm.getUri().equals(document)) {
                    weHavePermission = true;
                }
            }
        } else {
            weHavePermission = true;
        }
        if (weHavePermission) {
            try {
                InputStream is = getContentResolver().openInputStream(document);
                try {
                    String text = slurp(is);
                    DocumentFile docFile;
                    if (isContent) {
                        docFile = DocumentFile.fromSingleUri(this, document);
                    } else {
                        docFile = DocumentFile.fromFile(new File(document.getPath()));
                    }
                    EventBus.getDefault().post(new DocumentLoadedEvent(document, text, docFile.getName(), docFile.canWrite()));
                } finally {
                    is.close();
                }
            } catch (Exception e) {
                Log.e(getClass().getSimpleName(), "Exception loading " + document.toString(), e);
                EventBus.getDefault().post(new DocumentLoadErrorEvent(document, e));
            }
        } else {
            Log.e(getClass().getSimpleName(), "We failed to get permissions for " + document.toString());
            EventBus.getDefault().post(new DocumentPermissionFailureEvent(document));
        }
    } catch (SecurityException e) {
        Log.e(getClass().getSimpleName(), "Exception getting permissions for " + document.toString(), e);
        EventBus.getDefault().post(new DocumentPermissionFailureEvent(document));
    }
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) InputStream(java.io.InputStream) UriPermission(android.content.UriPermission) File(java.io.File) DocumentFile(android.support.v4.provider.DocumentFile) IOException(java.io.IOException)

Example 29 with DocumentFile

use of android.support.v4.provider.DocumentFile in project cw-omnibus by commonsguy.

the class DurablizerService method onHandleIntent.

@Override
protected void onHandleIntent(Intent intent) {
    Uri document = intent.getData();
    boolean weHaveDurablePermission = obtainDurablePermission(document);
    if (!weHaveDurablePermission) {
        document = makeLocalCopy(document);
    }
    if (weHaveDurablePermission || document != null) {
        Log.d(getClass().getSimpleName(), document.toString());
        DocumentFile docFile = buildDocFileForUri(document);
        Log.d(getClass().getSimpleName(), "Display name: " + docFile.getName());
        Log.d(getClass().getSimpleName(), "Size: " + Long.toString(docFile.length()));
        EventBus.getDefault().post(new ContentReadyEvent(docFile));
    }
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) Uri(android.net.Uri)

Example 30 with DocumentFile

use of android.support.v4.provider.DocumentFile in project cw-omnibus by commonsguy.

the class DurablizerService method makeLocalCopy.

private Uri makeLocalCopy(Uri document) {
    DocumentFile docFile = buildDocFileForUri(document);
    Uri result = null;
    if (docFile.getName() != null) {
        File f = new File(getFilesDir(), docFile.getName());
        try {
            FileOutputStream fos = new FileOutputStream(f);
            BufferedOutputStream out = new BufferedOutputStream(fos);
            InputStream in = getContentResolver().openInputStream(document);
            try {
                byte[] buffer = new byte[8192];
                int len = 0;
                while ((len = in.read(buffer)) >= 0) {
                    out.write(buffer, 0, len);
                }
                out.flush();
                result = Uri.fromFile(f);
            } finally {
                fos.getFD().sync();
                out.close();
                in.close();
            }
        } catch (Exception e) {
            Log.e(getClass().getSimpleName(), "Exception copying content to file", e);
        }
    }
    return (result);
}
Also used : DocumentFile(android.support.v4.provider.DocumentFile) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) Uri(android.net.Uri) File(java.io.File) DocumentFile(android.support.v4.provider.DocumentFile) BufferedOutputStream(java.io.BufferedOutputStream)

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