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