Search in sources :

Example 1 with DocumentFile

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

the class SAFManager method getDocumentFile.

public DocumentFile getDocumentFile(String docId, File file) throws FileNotFoundException {
    DocumentFile documentFile = null;
    if (null != file && file.canWrite()) {
        documentFile = DocumentFile.fromFile(file);
        return documentFile;
    }
    if (docId.startsWith(ROOT_ID_SECONDARY) && Utils.hasLollipop()) {
        String newDocId = docId.substring(ROOT_ID_SECONDARY.length());
        Uri uri = getRootUri(newDocId);
        if (null == uri) {
            if (null != file) {
                documentFile = DocumentFile.fromFile(file);
            }
            return documentFile;
        }
        Uri fileUri = buildDocumentUriMaybeUsingTree(uri, newDocId);
        documentFile = BasicDocumentFile.fromUri(mContext, fileUri);
    } else if (docId.startsWith(ROOT_ID_USB)) {
        documentFile = UsbDocumentFile.fromUri(mContext, docId);
    } else {
        if (null != file) {
            documentFile = DocumentFile.fromFile(file);
        } else {
            documentFile = BasicDocumentFile.fromUri(mContext, DocumentsContract.buildDocumentUri(ExternalStorageProvider.AUTHORITY, docId));
        }
    }
    return documentFile;
}
Also used : BasicDocumentFile(android.support.provider.BasicDocumentFile) DocumentFile(android.support.provider.DocumentFile) UsbDocumentFile(android.support.provider.UsbDocumentFile) Uri(android.net.Uri)

Example 2 with DocumentFile

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

the class ExternalStorageProvider method deleteDocument.

@Override
public void deleteDocument(String docId) throws FileNotFoundException {
    final File file = getFileForDocId(docId);
    DocumentFile documentFile = getDocumentFile(docId, file);
    if (!documentFile.delete()) {
        throw new IllegalStateException("Failed to delete " + file);
    }
    FileUtils.removeMediaStore(getContext(), file);
    notifyDocumentsChanged(docId);
}
Also used : DocumentFile(android.support.provider.DocumentFile) FileUtils.getTypeForFile(dev.dworks.apps.anexplorer.misc.FileUtils.getTypeForFile) DocumentFile(android.support.provider.DocumentFile) File(java.io.File)

Example 3 with DocumentFile

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

the class ExternalStorageProvider method includeFile.

private void includeFile(MatrixCursor result, String docId, File file) throws FileNotFoundException {
    if (docId == null) {
        docId = getDocIdForFile(file);
    } else {
        file = getFileForDocId(docId);
    }
    DocumentFile documentFile = getDocumentFile(docId, file);
    int flags = 0;
    if (documentFile.canWrite()) {
        if (file.isDirectory()) {
            flags |= Document.FLAG_DIR_SUPPORTS_CREATE;
        } else {
            flags |= Document.FLAG_SUPPORTS_WRITE;
        }
        flags |= Document.FLAG_SUPPORTS_DELETE;
        flags |= Document.FLAG_SUPPORTS_RENAME;
        flags |= Document.FLAG_SUPPORTS_MOVE;
        flags |= Document.FLAG_SUPPORTS_COPY;
        flags |= Document.FLAG_SUPPORTS_ARCHIVE;
        flags |= Document.FLAG_SUPPORTS_BOOKMARK;
        flags |= Document.FLAG_SUPPORTS_EDIT;
        if (isTelevision()) {
            flags |= Document.FLAG_DIR_PREFERS_GRID;
        }
    }
    final String mimeType = getTypeForFile(file);
    if (DocumentArchiveHelper.isSupportedArchiveType(mimeType)) {
        flags |= Document.FLAG_ARCHIVE;
    }
    final String displayName = file.getName();
    if (!showFilesHidden && !TextUtils.isEmpty(displayName)) {
        if (displayName.charAt(0) == '.') {
            return;
        }
    }
    if (MimePredicate.mimeMatches(MimePredicate.VISUAL_MIMES, mimeType)) {
        flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
    }
    final RowBuilder row = result.newRow();
    row.add(Document.COLUMN_DOCUMENT_ID, docId);
    row.add(Document.COLUMN_DISPLAY_NAME, displayName);
    row.add(Document.COLUMN_SIZE, file.length());
    row.add(Document.COLUMN_MIME_TYPE, mimeType);
    row.add(Document.COLUMN_PATH, file.getAbsolutePath());
    row.add(Document.COLUMN_FLAGS, flags);
    if (file.isDirectory() && null != file.list()) {
        row.add(Document.COLUMN_SUMMARY, FileUtils.formatFileCount(file.list().length));
    }
    // Only publish dates reasonably after epoch
    long lastModified = file.lastModified();
    if (lastModified > 31536000000L) {
        row.add(Document.COLUMN_LAST_MODIFIED, lastModified);
    }
}
Also used : DocumentFile(android.support.provider.DocumentFile) DocumentInfo.getCursorString(dev.dworks.apps.anexplorer.model.DocumentInfo.getCursorString) RowBuilder(dev.dworks.apps.anexplorer.cursor.MatrixCursor.RowBuilder) SuppressLint(android.annotation.SuppressLint) Point(android.graphics.Point)

Example 4 with DocumentFile

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

the class ExternalStorageProvider method renameDocument.

@Override
public String renameDocument(String docId, String displayName) throws FileNotFoundException {
    // Since this provider treats renames as generating a completely new
    // docId, we're okay with letting the MIME type change.
    displayName = FileUtils.buildValidFatFilename(displayName);
    final File before = getFileForDocId(docId);
    DocumentFile documentFile = getDocumentFile(docId, before);
    final File after = new File(before.getParentFile(), displayName);
    if (after.exists()) {
        throw new IllegalStateException("Already exists " + after);
    }
    if (!documentFile.renameTo(displayName)) {
        throw new IllegalStateException("Failed to rename to " + after);
    }
    final String afterDocId = getDocIdForFile(after);
    if (!TextUtils.equals(docId, afterDocId)) {
        notifyDocumentsChanged(afterDocId);
        return afterDocId;
    } else {
        return null;
    }
}
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 5 with DocumentFile

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

the class UsbStorageProvider method move.

private String move(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 move ");
        } else {
            if (!sourceDirectory.delete()) {
                throw new IllegalStateException("Failed to move ");
            }
        }
        afterDocId = targetParentDocumentId;
    } else {
        before.moveTo(after);
        afterDocId = getDocIdForFile(after);
    }
    return afterDocId;
}
Also used : DocumentFile(android.support.provider.DocumentFile) UsbFile(com.github.mjdev.libaums.fs.UsbFile)

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