Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,11 @@ public boolean onCreate() {

@Override
public String createDocument(String parentDocumentId, String mimeType, String displayName) throws FileNotFoundException {
File newFile = new File(parentDocumentId, displayName);
File parent = getFileForDocId(parentDocumentId);
File newFile = new File(parent, displayName);
int noConflictId = 2;
while (newFile.exists()) {
newFile = new File(parentDocumentId, displayName + " (" + noConflictId++ + ")");
newFile = new File(parent, displayName + " (" + noConflictId++ + ")");
}
try {
boolean succeeded;
Expand All @@ -146,6 +147,23 @@ public void deleteDocument(String documentId) throws FileNotFoundException {
}
}

// Implementation of renameDocument is required for atomic write operations in external editors
@Override
public String renameDocument(String documentId, String displayName) throws FileNotFoundException {
final File oldFile = getFileForDocId(documentId);
final File newFile = new File(oldFile.getParentFile(), displayName);

if (newFile.exists()) {
throw new FileNotFoundException("Failed to rename: destination file '" + displayName + "' already exists.");
}

if (!oldFile.renameTo(newFile)) {
throw new FileNotFoundException("Failed to rename document with id " + documentId);
}

return getDocIdForFile(newFile);
}

@Override
public String getDocumentType(String documentId) throws FileNotFoundException {
File file = getFileForDocId(documentId);
Expand Down Expand Up @@ -249,7 +267,14 @@ private void includeFile(MatrixCursor result, String docId, File file)
} else if (file.canWrite()) {
flags |= Document.FLAG_SUPPORTS_WRITE;
}
if (file.getParentFile().canWrite()) flags |= Document.FLAG_SUPPORTS_DELETE;

// Advertise RENAME and DELETE flags if the parent is writable.
// Added check for null parent to avoid NPE on root folders.
File parent = file.getParentFile();
if (parent != null && parent.canWrite()) {
flags |= Document.FLAG_SUPPORTS_DELETE;
flags |= Document.FLAG_SUPPORTS_RENAME;
}

final String displayName = file.getName();
final String mimeType = getMimeType(file);
Expand All @@ -265,4 +290,4 @@ private void includeFile(MatrixCursor result, String docId, File file)
row.add(Document.COLUMN_ICON, R.mipmap.ic_launcher);
}

}
}