Skip to content

Commit

Permalink
Fix for directory, fix for name clobbering in attachment
Browse files Browse the repository at this point in the history
  • Loading branch information
harshad1 committed Nov 10, 2023
1 parent 2ea9d01 commit 9797350
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<File> insertFileLink = (file) -> {
// If path is not under notebook, copy it to the res folder
if (!GsFileUtils.isChild(_appSettings.getNotebookDirectory(), file)) {
Expand All @@ -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,
Expand All @@ -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
Expand Down

0 comments on commit 9797350

Please sign in to comment.