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;
}
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());
}
}
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());
}
}
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;
}
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());
}
}
Aggregations