From 9797350014f3d920f8357de020156a273029e14b Mon Sep 17 00:00:00 2001 From: Harshad Vedartham Date: Fri, 10 Nov 2023 15:15:42 -0800 Subject: [PATCH] Fix for directory, fix for name clobbering in attachment --- .../markor/activity/DocumentActivity.java | 20 ++++++++------ .../frontend/AttachLinkOrFileDialog.java | 27 +++++++++++++------ 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/net/gsantner/markor/activity/DocumentActivity.java b/app/src/main/java/net/gsantner/markor/activity/DocumentActivity.java index 5da877d7e..7a37554a7 100644 --- a/app/src/main/java/net/gsantner/markor/activity/DocumentActivity.java +++ b/app/src/main/java/net/gsantner/markor/activity/DocumentActivity.java @@ -81,16 +81,20 @@ public static void launch(Activity activity, File path, Boolean doPreview, Inten } public static void handleFileClick(Activity activity, File file, Integer lineNumber) { - if (activity != null && file != null) { - final boolean readableDirectory = file.isDirectory() && file.canRead(); - final boolean creatableFile = FormatRegistry.isFileSupported(file) && GsFileUtils.canCreate(file); - if (readableDirectory || creatableFile) { + if (activity == null || file == null) { + return; + } + + if (file.isDirectory()) { + if (file.canRead()) { launch(activity, file, null, null, lineNumber); - } else if (GsFileUtils.getFilenameExtension(file).equals(".apk")) { - GsContextUtils.instance.requestApkInstallation(activity, file); - } else { - askUserIfWantsToOpenFileInThisApp(activity, file); } + } else if (FormatRegistry.isFileSupported(file) && GsFileUtils.canCreate(file)) { + launch(activity, file, null, null, lineNumber); + } else if (GsFileUtils.getFilenameExtension(file).equals(".apk")) { + GsContextUtils.instance.requestApkInstallation(activity, file); + } else { + askUserIfWantsToOpenFileInThisApp(activity, file); } } diff --git a/app/src/main/java/net/gsantner/markor/frontend/AttachLinkOrFileDialog.java b/app/src/main/java/net/gsantner/markor/frontend/AttachLinkOrFileDialog.java index 9e1a50ef6..4a0ffdb76 100644 --- a/app/src/main/java/net/gsantner/markor/frontend/AttachLinkOrFileDialog.java +++ b/app/src/main/java/net/gsantner/markor/frontend/AttachLinkOrFileDialog.java @@ -259,6 +259,15 @@ private static void insertItem( } }; + // Pull dialog elements + final EditText nameEdit, pathEdit; + if (dialog != null) { + nameEdit = dialog.findViewById(R.id.ui__select_path_dialog__name); + pathEdit = dialog.findViewById(R.id.ui__select_path_dialog__url); + } else { + nameEdit = pathEdit = null; + } + final GsCallback.a1 insertFileLink = (file) -> { // If path is not under notebook, copy it to the res folder if (!GsFileUtils.isChild(_appSettings.getNotebookDirectory(), file)) { @@ -267,13 +276,20 @@ private static void insertItem( GsFileUtils.copyFile(file, local); file = local; } - final String title = GsFileUtils.getFilenameWithoutExtension(file); + + final String title; + if (nameEdit != null) { + title = nameEdit.getText().toString(); + } else { + title = GsFileUtils.getFilenameWithoutExtension(file); + } + final String path = GsFileUtils.relativePath(currentFile, file); insertLink.callback(title, path); }; final MarkorContextUtils shu = new MarkorContextUtils(activity); - final BroadcastReceiver br = shu.receiveResultFromLocalBroadcast( + final BroadcastReceiver receiver = shu.receiveResultFromLocalBroadcast( activity, (intent, _br) -> insertFileLink.callback(new File(intent.getStringExtra(MarkorContextUtils.EXTRA_FILEPATH))), true, @@ -282,13 +298,8 @@ private static void insertItem( "" + MarkorContextUtils.REQUEST_RECORD_AUDIO ); - final EditText nameEdit, pathEdit; if (dialog != null) { - nameEdit = dialog.findViewById(R.id.ui__select_path_dialog__name); - pathEdit = dialog.findViewById(R.id.ui__select_path_dialog__url); - dialog.setOnDismissListener(d -> LocalBroadcastManager.getInstance(activity).unregisterReceiver(br)); - } else { - nameEdit = pathEdit = null; + dialog.setOnDismissListener(d -> LocalBroadcastManager.getInstance(activity).unregisterReceiver(receiver)); } // Do each thing as necessary