Search in sources :

Example 1 with FileSystem

use of com.github.mjdev.libaums.fs.FileSystem in project AnExplorer by 1hakr.

the class UsbStorageProvider method queryRoots.

@Override
public Cursor queryRoots(String[] projection) throws FileNotFoundException {
    // Create a cursor with either the requested fields, or the default projection if "projection" is null.
    final MatrixCursor result = new MatrixCursor(resolveRootProjection(projection));
    for (ArrayMap.Entry<String, UsbPartition> root : mRoots.entrySet()) {
        UsbPartition usbPartition = root.getValue();
        UsbDevice usbDevice = usbPartition.device;
        FileSystem fileSystem = usbPartition.fileSystem;
        UsbFile rootDirectory = null;
        String volumeLabel = null;
        Long availableBytes = 0L;
        Long capactityBytes = 0L;
        String documentId = root.getKey() + ROOT_SEPERATOR;
        if (null != fileSystem) {
            rootDirectory = fileSystem.getRootDirectory();
            volumeLabel = fileSystem.getVolumeLabel();
            availableBytes = fileSystem.getFreeSpace();
            capactityBytes = fileSystem.getCapacity();
            documentId = getDocIdForFile(rootDirectory);
        }
        String title = UsbUtils.getName(usbDevice);
        if (TextUtils.isEmpty(title)) {
            title = getContext().getString(R.string.root_usb);
        }
        int flags = Root.FLAG_SUPPORTS_CREATE | Root.FLAG_SUPPORTS_EDIT | Root.FLAG_LOCAL_ONLY | Root.FLAG_ADVANCED | Root.FLAG_SUPPORTS_IS_CHILD;
        final MatrixCursor.RowBuilder row = result.newRow();
        // These columns are required
        row.add(Root.COLUMN_ROOT_ID, root.getKey());
        row.add(Root.COLUMN_DOCUMENT_ID, documentId);
        row.add(Root.COLUMN_TITLE, title);
        row.add(Root.COLUMN_FLAGS, flags);
        // These columns are optional
        row.add(Root.COLUMN_SUMMARY, volumeLabel);
        row.add(Root.COLUMN_AVAILABLE_BYTES, availableBytes);
        row.add(Root.COLUMN_CAPACITY_BYTES, capactityBytes);
        row.add(Root.COLUMN_PATH, UsbUtils.getPath(usbDevice));
    // Root.COLUMN_MIME_TYPE is another optional column and useful if you have multiple roots with different
    // types of mime types (roots that don't match the requested mime type are automatically hidden)
    }
    return result;
}
Also used : UsbFile(com.github.mjdev.libaums.fs.UsbFile) FileSystem(com.github.mjdev.libaums.fs.FileSystem) UsbDevice(android.hardware.usb.UsbDevice) ArrayMap(android.support.v4.util.ArrayMap) MatrixCursor(dev.dworks.apps.anexplorer.cursor.MatrixCursor)

Example 2 with FileSystem

use of com.github.mjdev.libaums.fs.FileSystem 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 3 with FileSystem

use of com.github.mjdev.libaums.fs.FileSystem in project AnExplorer by 1hakr.

the class UsbStorageProvider method getDocIdForFile.

private String getDocIdForFile(UsbFile file) throws FileNotFoundException {
    if (file.isRoot()) {
        for (Map.Entry<String, UsbPartition> root : mRoots.entrySet()) {
            FileSystem fileSystem = root.getValue().fileSystem;
            if (null != fileSystem) {
                if (file.equals(fileSystem.getRootDirectory())) {
                    String documentId = root.getKey() + ROOT_SEPERATOR;
                    mFileCache.put(documentId, file);
                    return documentId;
                }
            } else {
                String documentId = root.getKey() + ROOT_SEPERATOR;
                mFileCache.put(documentId, file);
                return documentId;
            }
        }
        throw new FileNotFoundException("Missing root entry");
    }
    String documentId = getDocIdForFile(file.getParent()) + DIRECTORY_SEPERATOR + file.getName();
    mFileCache.put(documentId, file);
    return documentId;
}
Also used : FileSystem(com.github.mjdev.libaums.fs.FileSystem) FileNotFoundException(java.io.FileNotFoundException) ArrayMap(android.support.v4.util.ArrayMap) Map(java.util.Map) MimeTypeMap(android.webkit.MimeTypeMap)

Aggregations

FileSystem (com.github.mjdev.libaums.fs.FileSystem)3 ArrayMap (android.support.v4.util.ArrayMap)2 UsbFile (com.github.mjdev.libaums.fs.UsbFile)2 UsbDevice (android.hardware.usb.UsbDevice)1 DocumentFile (android.support.provider.DocumentFile)1 MimeTypeMap (android.webkit.MimeTypeMap)1 UsbFileInputStream (com.github.mjdev.libaums.fs.UsbFileInputStream)1 UsbFileOutputStream (com.github.mjdev.libaums.fs.UsbFileOutputStream)1 MatrixCursor (dev.dworks.apps.anexplorer.cursor.MatrixCursor)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 Map (java.util.Map)1