diff --git a/app/src/main/java/com/termux/filepicker/TermuxDocumentsProvider.java b/app/src/main/java/com/termux/filepicker/TermuxDocumentsProvider.java index 48b16391fd..6a84904033 100644 --- a/app/src/main/java/com/termux/filepicker/TermuxDocumentsProvider.java +++ b/app/src/main/java/com/termux/filepicker/TermuxDocumentsProvider.java @@ -71,7 +71,7 @@ public Cursor queryRoots(String[] projection) throws FileNotFoundException { row.add(Root.COLUMN_ROOT_ID, getDocIdForFile(BASE_DIR)); row.add(Root.COLUMN_DOCUMENT_ID, getDocIdForFile(BASE_DIR)); row.add(Root.COLUMN_SUMMARY, null); - row.add(Root.COLUMN_FLAGS, Root.FLAG_SUPPORTS_CREATE | Root.FLAG_SUPPORTS_SEARCH); + row.add(Root.COLUMN_FLAGS, Root.FLAG_SUPPORTS_CREATE | Root.FLAG_SUPPORTS_SEARCH | Root.FLAG_SUPPORTS_IS_CHILD); row.add(Root.COLUMN_TITLE, applicationName); row.add(Root.COLUMN_MIME_TYPES, ALL_MIME_TYPES); row.add(Root.COLUMN_AVAILABLE_BYTES, BASE_DIR.getFreeSpace()); @@ -117,6 +117,29 @@ public boolean onCreate() { return true; } + @Override + public String createDocument(String parentDocumentId, String mimeType, String displayName) throws FileNotFoundException { + File newFile = new File(parentDocumentId, displayName); + int noConflictId = 2; + while (newFile.exists()) { + newFile = new File(parentDocumentId, displayName + " (" + noConflictId++ + ")"); + } + try { + boolean succeeded; + if (Document.MIME_TYPE_DIR.equals(mimeType)) { + succeeded = newFile.mkdir(); + } else { + succeeded = newFile.createNewFile(); + } + if (!succeeded) { + throw new FileNotFoundException("Failed to create document with id " + newFile.getPath()); + } + } catch (IOException e) { + throw new FileNotFoundException("Failed to create document with id " + newFile.getPath()); + } + return newFile.getPath(); + } + @Override public void deleteDocument(String documentId) throws FileNotFoundException { File file = getFileForDocId(documentId); @@ -169,6 +192,11 @@ public Cursor querySearchDocuments(String rootId, String query, String[] project return result; } + @Override + public boolean isChildDocument(String parentDocumentId, String documentId) { + return documentId.startsWith(parentDocumentId); + } + /** * Get the document id given a file. This document id must be consistent across time as other * applications may save the ID and use it to reference documents later. @@ -220,10 +248,11 @@ private void includeFile(MatrixCursor result, String docId, File file) int flags = 0; if (file.isDirectory()) { - if (file.isDirectory() && file.canWrite()) flags |= Document.FLAG_DIR_SUPPORTS_CREATE; + if (file.canWrite()) flags |= Document.FLAG_DIR_SUPPORTS_CREATE; } else if (file.canWrite()) { - flags |= Document.FLAG_SUPPORTS_WRITE | Document.FLAG_SUPPORTS_DELETE; + flags |= Document.FLAG_SUPPORTS_WRITE; } + if (file.getParentFile().canWrite()) flags |= Document.FLAG_SUPPORTS_DELETE; final String displayName = file.getName(); final String mimeType = getMimeType(file);