diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index 79ae271d..b11ada70 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -43,6 +43,21 @@
android:parentActivityName=".app.TermuxActivity"
android:label="@string/application_name" />
+
+
+
+
+
+
+
+
+
+
= 0) attachmentFileName = c.getString(fileNameColumnId);
+ }
+ }
+
+ if (attachmentFileName == null) attachmentFileName = subjectFromIntent;
+
+ InputStream in = getContentResolver().openInputStream(uri);
+ promptNameAndSave(in, attachmentFileName);
+ } catch (Exception e) {
+ showErrorDialogAndQuit("Unable to handle shared content:\n\n" + e.getMessage());
+ Log.e("termux", "handleContentUri(uri=" + uri + ") failed", e);
+ }
+ }
+
+ void promptNameAndSave(final InputStream in, final String attachmentFileName) {
+ DialogUtils.textInput(this, R.string.file_received_title, attachmentFileName
+ , android.R.string.ok, new DialogUtils.TextSetListener() {
+ @Override
+ public void onTextSet(final String text) {
+ if (saveStreamWithName(in, text) == null) return;
+ finish();
+ }
+ }, R.string.file_received_open_folder_button, new DialogUtils.TextSetListener() {
+ @Override
+ public void onTextSet(String text) {
+ if (saveStreamWithName(in, text) == null) return;
+
+ Intent executeIntent = new Intent(TermuxService.ACTION_EXECUTE);
+ executeIntent.putExtra(TermuxService.EXTRA_CURRENT_WORKING_DIRECTORY, TERMUX_RECEIVEDIR);
+ executeIntent.setClass(TermuxFileReceiverActivity.this, TermuxService.class);
+ startService(executeIntent);
+ finish();
+ }
+ }, R.string.file_received_edit_button, new DialogUtils.TextSetListener() {
+ @Override
+ public void onTextSet(String text) {
+ File outFile = saveStreamWithName(in, text);
+ if (outFile == null) return;
+
+ final File editorProgramFile = new File(EDITOR_PROGRAM);
+ if (!editorProgramFile.isFile()) {
+ showErrorDialogAndQuit("The following file does not exist:\n$HOME/bin/termux-file-editor\n\n"
+ + "Create this file as a script or a symlink - it will be called with the received file as only argument.");
+ return;
+ }
+
+ // Do this for the user if necessary:
+ editorProgramFile.setExecutable(true);
+
+ final Uri scriptUri = new Uri.Builder().scheme("file").path(EDITOR_PROGRAM).build();
+
+ Intent executeIntent = new Intent(TermuxService.ACTION_EXECUTE, scriptUri);
+ executeIntent.setClass(TermuxFileReceiverActivity.this, TermuxService.class);
+ executeIntent.putExtra(TermuxService.EXTRA_ARGUMENTS, new String[]{outFile.getAbsolutePath()});
+ startService(executeIntent);
+ finish();
+ }
+ }, new DialogInterface.OnDismissListener() {
+ @Override
+ public void onDismiss(DialogInterface dialog) {
+ if (mFinishOnDismissNameDialog) finish();
+ }
+ });
+ }
+
+ public File saveStreamWithName(InputStream in, String attachmentFileName) {
+ File receiveDir = new File(TERMUX_RECEIVEDIR);
+ if (!receiveDir.isDirectory() && !receiveDir.mkdirs()) {
+ showErrorDialogAndQuit("Cannot create directory: " + receiveDir.getAbsolutePath());
+ return null;
+ }
+ try {
+ final File outFile = new File(receiveDir, attachmentFileName);
+ try (FileOutputStream f = new FileOutputStream(outFile)) {
+ byte[] buffer = new byte[4096];
+ int readBytes;
+ while ((readBytes = in.read(buffer)) > 0) {
+ f.write(buffer, 0, readBytes);
+ }
+ }
+ return outFile;
+ } catch (IOException e) {
+ showErrorDialogAndQuit("Error saving file:\n\n" + e);
+ Log.e("termux", "Error saving file", e);
+ return null;
+ }
+ }
+
+ void handleUrlAndFinish(final String url) {
+ final File urlOpenerProgramFile = new File(URL_OPENER_PROGRAM);
+ if (!urlOpenerProgramFile.isFile()) {
+ showErrorDialogAndQuit("The following file does not exist:\n$HOME/bin/termux-url-opener\n\n"
+ + "Create this file as a script or a symlink - it will be called with the shared URL as only argument.");
+ return;
+ }
+
+ // Do this for the user if necessary:
+ urlOpenerProgramFile.setExecutable(true);
+
+ final Uri urlOpenerProgramUri = new Uri.Builder().scheme("file").path(URL_OPENER_PROGRAM).build();
+
+ Intent executeIntent = new Intent(TermuxService.ACTION_EXECUTE, urlOpenerProgramUri);
+ executeIntent.setClass(TermuxFileReceiverActivity.this, TermuxService.class);
+ executeIntent.putExtra(TermuxService.EXTRA_ARGUMENTS, new String[]{url});
+ startService(executeIntent);
+ finish();
+ }
+
+}
diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml
index b17f2de3..52121b32 100644
--- a/app/src/main/res/values/strings.xml
+++ b/app/src/main/res/values/strings.xml
@@ -55,4 +55,8 @@
Empty folder.
+ Save file in ~/downloads/
+ Edit
+ Open folder
+