Search in sources :

Example 6 with UsbFile

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

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

the class UsbStorageProvider method openDocument.

@Override
public ParcelFileDescriptor openDocument(String documentId, String mode, CancellationSignal signal) throws FileNotFoundException {
    try {
        UsbFile file = getFileForDocId(documentId);
        /*            final int accessMode = ParcelFileDescriptorUtil.parseMode(mode);
            if ((accessMode | ParcelFileDescriptor.MODE_READ_ONLY) == ParcelFileDescriptor.MODE_READ_ONLY) {
                return ParcelFileDescriptorUtil.pipeFrom(new UsbFileInputStream(file));
            } else if ((accessMode | ParcelFileDescriptor.MODE_WRITE_ONLY) == ParcelFileDescriptor.MODE_WRITE_ONLY) {
                return ParcelFileDescriptorUtil.pipeTo(new UsbFileOutputStream(file));
            }*/
        final boolean isWrite = (mode.indexOf('w') != -1);
        if (isWrite) {
            return ParcelFileDescriptorUtil.pipeTo(new UsbFileOutputStream(file));
        } else {
            return ParcelFileDescriptorUtil.pipeFrom(new UsbFileInputStream(file));
        }
    } catch (IOException e) {
        throw new FileNotFoundException(e.getMessage());
    }
}
Also used : UsbFile(com.github.mjdev.libaums.fs.UsbFile) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) UsbFileInputStream(com.github.mjdev.libaums.fs.UsbFileInputStream) UsbFileOutputStream(com.github.mjdev.libaums.fs.UsbFileOutputStream)

Example 8 with UsbFile

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

the class UsbStorageProvider method createDocument.

@Override
public String createDocument(String parentDocumentId, String mimeType, String displayName) throws FileNotFoundException {
    try {
        UsbFile parent = getFileForDocId(parentDocumentId);
        UsbFile child;
        if (Document.MIME_TYPE_DIR.equals(mimeType)) {
            child = parent.createDirectory(displayName);
        } else {
            child = parent.createFile(getFileName(mimeType, displayName));
        }
        final String afterDocId = getDocIdForFile(child);
        notifyDocumentsChanged(afterDocId);
        return afterDocId;
    } catch (IOException e) {
        throw new FileNotFoundException(e.getMessage());
    }
}
Also used : UsbFile(com.github.mjdev.libaums.fs.UsbFile) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Example 9 with UsbFile

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

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

the class UsbStorageProvider method deleteDocument.

@Override
public void deleteDocument(String documentId) throws FileNotFoundException {
    try {
        UsbFile file = getFileForDocId(documentId);
        file.delete();
        mFileCache.remove(documentId);
        notifyDocumentsChanged(documentId);
    } catch (IOException e) {
        throw new FileNotFoundException(e.getMessage());
    }
}
Also used : UsbFile(com.github.mjdev.libaums.fs.UsbFile) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException)

Aggregations

UsbFile (com.github.mjdev.libaums.fs.UsbFile)13 FileNotFoundException (java.io.FileNotFoundException)6 IOException (java.io.IOException)6 MatrixCursor (dev.dworks.apps.anexplorer.cursor.MatrixCursor)3 UsbDevice (android.hardware.usb.UsbDevice)2 DocumentFile (android.support.provider.DocumentFile)2 FileSystem (com.github.mjdev.libaums.fs.FileSystem)2 UsbFileInputStream (com.github.mjdev.libaums.fs.UsbFileInputStream)2 UsbFileOutputStream (com.github.mjdev.libaums.fs.UsbFileOutputStream)2 PendingIntent (android.app.PendingIntent)1 Intent (android.content.Intent)1 UsbManager (android.hardware.usb.UsbManager)1 Handler (android.os.Handler)1 ArrayMap (android.support.v4.util.ArrayMap)1 Spannable (android.text.Spannable)1 SpannableString (android.text.SpannableString)1 ForegroundColorSpan (android.text.style.ForegroundColorSpan)1 AbstractUsbFile (com.github.mjdev.libaums.fs.AbstractUsbFile)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1