Search in sources :

Example 6 with DocumentFile

use of android.support.provider.DocumentFile in project AnExplorer by 1hakr.

the class UsbStorageProvider method copy.

private String copy(String sourceDocumentId, String targetParentDocumentId) throws IOException {
    final String afterDocId;
    final UsbFile before = getFile(sourceDocumentId);
    final UsbFile after = getFile(targetParentDocumentId);
    boolean isSourceUSB = sourceDocumentId.startsWith(ROOT_ID_USB);
    boolean isTargetUSB = targetParentDocumentId.startsWith(ROOT_ID_USB);
    if (!(isSourceUSB && isTargetUSB)) {
        DocumentFile sourceDirectory = getDocumentFile(sourceDocumentId);
        DocumentFile targetDirectory = getDocumentFile(targetParentDocumentId);
        if (!FileUtils.moveDocument(getContext(), sourceDirectory, targetDirectory)) {
            throw new IllegalStateException("Failed to copy ");
        }
        afterDocId = targetParentDocumentId;
    } else {
        UsbPartition usbPartition = mRoots.get(getRootIdForDocId(sourceDocumentId));
        final FileSystem fileSystem = usbPartition.fileSystem;
        final UsbFile newFile = after.createFile(before.getName());
        InputStream inputStream = UsbFileStreamFactory.createBufferedInputStream(before, fileSystem);
        OutputStream outputStream = UsbFileStreamFactory.createBufferedOutputStream(newFile, fileSystem);
        if (!FileUtils.copy(inputStream, outputStream)) {
            throw new IllegalStateException("Failed to copy " + before);
        }
        afterDocId = getDocIdForFile(after);
    }
    return afterDocId;
}
Also used : DocumentFile(android.support.provider.DocumentFile) UsbFile(com.github.mjdev.libaums.fs.UsbFile) UsbFileInputStream(com.github.mjdev.libaums.fs.UsbFileInputStream) InputStream(java.io.InputStream) FileSystem(com.github.mjdev.libaums.fs.FileSystem) OutputStream(java.io.OutputStream) UsbFileOutputStream(com.github.mjdev.libaums.fs.UsbFileOutputStream)

Example 7 with DocumentFile

use of android.support.provider.DocumentFile in project AnExplorer by 1hakr.

the class ExternalStorageProvider method createDocument.

@Override
public String createDocument(String docId, String mimeType, String displayName) throws FileNotFoundException {
    displayName = FileUtils.buildValidFatFilename(displayName);
    final File parent = getFileForDocId(docId);
    if (!parent.isDirectory()) {
        throw new IllegalArgumentException("Parent document isn't a directory");
    }
    final File file = FileUtils.buildUniqueFile(parent, mimeType, displayName);
    DocumentFile documentFile = getDocumentFile(docId, parent);
    if (Document.MIME_TYPE_DIR.equals(mimeType)) {
        DocumentFile newFile = documentFile.createDirectory(displayName);
        if (!newFile.exists()) {
            throw new IllegalStateException("Failed to mkdir " + file);
        }
    } else {
        DocumentFile newFile = documentFile.createFile(mimeType, displayName);
        if (!newFile.exists()) {
            throw new IllegalStateException("Failed to touch " + file);
        }
    }
    final String afterDocId = getDocIdForFile(file);
    notifyDocumentsChanged(afterDocId);
    return afterDocId;
}
Also used : DocumentFile(android.support.provider.DocumentFile) DocumentInfo.getCursorString(dev.dworks.apps.anexplorer.model.DocumentInfo.getCursorString) FileUtils.getTypeForFile(dev.dworks.apps.anexplorer.misc.FileUtils.getTypeForFile) DocumentFile(android.support.provider.DocumentFile) File(java.io.File)

Example 8 with DocumentFile

use of android.support.provider.DocumentFile in project AnExplorer by 1hakr.

the class ExternalStorageProvider method move.

private String move(String sourceDocumentId, String targetParentDocumentId) throws FileNotFoundException {
    final String afterDocId;
    final File source = getFile(sourceDocumentId);
    final File target = getFile(targetParentDocumentId);
    boolean isSourceOther = isFromOtherProvider(sourceDocumentId);
    boolean isTargetOther = isFromOtherProvider(targetParentDocumentId);
    if ((isSourceOther || isTargetOther)) {
        DocumentFile sourceDirectory = getDocumentFile(sourceDocumentId, source);
        DocumentFile targetDirectory = getDocumentFile(targetParentDocumentId, target);
        if (!FileUtils.moveDocument(getContext(), sourceDirectory, targetDirectory)) {
            throw new IllegalStateException("Failed to move " + source);
        } else {
            if (!sourceDirectory.delete()) {
                throw new IllegalStateException("Failed to move " + source);
            }
        }
        afterDocId = targetParentDocumentId;
    } else {
        final File after = new File(target, source.getName());
        if (after.exists()) {
            throw new IllegalStateException("Already exists " + after);
        }
        if (!source.renameTo(after)) {
            throw new IllegalStateException("Failed to move to " + after);
        } else {
            notifyDocumentsChanged(targetParentDocumentId);
            FileUtils.updateMediaStore(getContext(), source.getPath());
        }
        afterDocId = getDocIdForFile(target);
    }
    return afterDocId;
}
Also used : DocumentFile(android.support.provider.DocumentFile) DocumentInfo.getCursorString(dev.dworks.apps.anexplorer.model.DocumentInfo.getCursorString) FileUtils.getTypeForFile(dev.dworks.apps.anexplorer.misc.FileUtils.getTypeForFile) DocumentFile(android.support.provider.DocumentFile) File(java.io.File)

Example 9 with DocumentFile

use of android.support.provider.DocumentFile in project AnExplorer by 1hakr.

the class ExternalStorageProvider method copy.

private String copy(String sourceDocumentId, String targetParentDocumentId) throws FileNotFoundException {
    final String afterDocId;
    final File source = getFile(sourceDocumentId);
    final File target = getFile(targetParentDocumentId);
    boolean isSourceOther = isFromOtherProvider(sourceDocumentId);
    boolean isTargetOther = isFromOtherProvider(targetParentDocumentId);
    if ((isSourceOther || isTargetOther)) {
        DocumentFile sourceDirectory = getDocumentFile(sourceDocumentId, source);
        DocumentFile targetDirectory = getDocumentFile(targetParentDocumentId, target);
        if (!FileUtils.moveDocument(getContext(), sourceDirectory, targetDirectory)) {
            throw new IllegalStateException("Failed to copy " + source);
        }
        afterDocId = targetParentDocumentId;
    } else {
        if (!FileUtils.moveDocument(source, target, null)) {
            throw new IllegalStateException("Failed to copy " + source);
        }
        afterDocId = getDocIdForFile(target);
    }
    return afterDocId;
}
Also used : DocumentFile(android.support.provider.DocumentFile) DocumentInfo.getCursorString(dev.dworks.apps.anexplorer.model.DocumentInfo.getCursorString) FileUtils.getTypeForFile(dev.dworks.apps.anexplorer.misc.FileUtils.getTypeForFile) DocumentFile(android.support.provider.DocumentFile) File(java.io.File)

Example 10 with DocumentFile

use of android.support.provider.DocumentFile in project AnExplorer by 1hakr.

the class NoteActivity method getOutStream.

private OutputStream getOutStream(Uri uri) {
    String scheme = uri.getScheme();
    if (scheme.startsWith(ContentResolver.SCHEME_CONTENT)) {
        try {
            DocumentFile documentFile = DocumentsApplication.getSAFManager(getApplicationContext()).getDocumentFile(uri);
            Uri finalUri = null == documentFile ? uri : documentFile.getUri();
            return getContentResolver().openOutputStream(finalUri);
        } catch (Exception e) {
            CrashReportingManager.logException(e);
        }
    } else if (scheme.startsWith(ContentResolver.SCHEME_FILE)) {
        File f = new File(uri.getPath());
        if (f.exists()) {
            try {
                return new FileOutputStream(f);
            } catch (Exception e) {
                CrashReportingManager.logException(e);
            }
        }
    }
    return null;
}
Also used : DocumentFile(android.support.provider.DocumentFile) FileOutputStream(java.io.FileOutputStream) UsbFileOutputStream(com.github.mjdev.libaums.fs.UsbFileOutputStream) Uri(android.net.Uri) DocumentFile(android.support.provider.DocumentFile) UsbFile(com.github.mjdev.libaums.fs.UsbFile) File(java.io.File) IOException(java.io.IOException)

Aggregations

DocumentFile (android.support.provider.DocumentFile)11 File (java.io.File)6 FileUtils.getTypeForFile (dev.dworks.apps.anexplorer.misc.FileUtils.getTypeForFile)5 DocumentInfo.getCursorString (dev.dworks.apps.anexplorer.model.DocumentInfo.getCursorString)5 UsbFile (com.github.mjdev.libaums.fs.UsbFile)3 Uri (android.net.Uri)2 UsbFileOutputStream (com.github.mjdev.libaums.fs.UsbFileOutputStream)2 IOException (java.io.IOException)2 SuppressLint (android.annotation.SuppressLint)1 Point (android.graphics.Point)1 BasicDocumentFile (android.support.provider.BasicDocumentFile)1 UsbDocumentFile (android.support.provider.UsbDocumentFile)1 FileSystem (com.github.mjdev.libaums.fs.FileSystem)1 UsbFileInputStream (com.github.mjdev.libaums.fs.UsbFileInputStream)1 RowBuilder (dev.dworks.apps.anexplorer.cursor.MatrixCursor.RowBuilder)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1