Skip to content

Commit

Permalink
filebrowser::rename file::fix/workaround for Android's filesystem cas…
Browse files Browse the repository at this point in the history
…e-insensitive filenaming, by @gsantner, closes #1481
  • Loading branch information
gsantner committed Nov 4, 2021
1 parent 285d036 commit 7541863
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
15 changes: 14 additions & 1 deletion app/src/main/java/net/gsantner/opoc/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
Expand Down Expand Up @@ -515,4 +514,18 @@ public static String sha512sum(final byte[] bytes) {
return null;
}
}

// Return true if the target file exists, false if there is an issue with the file or it's parent directories
public static boolean fileExists(final File checkFile) {
File[] files;
if (checkFile != null && checkFile.getParentFile() != null && (files = checkFile.getParentFile().listFiles()) != null) {
final String checkFilename = checkFile.getName();
for (final File f : files) {
if (f.getName().equals(checkFilename)) {
return true;
}
}
}
return false;
}
}
12 changes: 6 additions & 6 deletions app/thirdparty/java/other/writeily/ui/WrRenameDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
boolean renamed = false;
boolean filenameChanged = !file.getName().equals(newFileName);
if (filenameChanged) {
_filenameClash = checkFilenameClash(file, newFileName);
_filenameClash = optShowFilenameClashDialog(file, newFileName);
}
if (shareUtil.isUnderStorageAccessFolder(file)) {
DocumentFile dof = shareUtil.getDocumentFile(file, file.isDirectory());
Expand Down Expand Up @@ -148,13 +148,13 @@ public void afterTextChanged(Editable s) {
});
}

private boolean checkFilenameClash(File originalFile, String newName) {
File newFile = new File(originalFile.getParent(), newName);
if (newFile.exists()) {
// Return true in case of filename clash
private boolean optShowFilenameClashDialog(File originalFile, String newName) {
final boolean clash = FileUtils.fileExists(new File(originalFile.getParent(), newName));
if (clash) {
((TextView) _dialog.findViewById(R.id.dialog_message)).setText(R.string.file_folder_already_exists_please_use_a_different_name);
_dialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
return true;
}
return false;
return clash;
}
}

0 comments on commit 7541863

Please sign in to comment.