Skip to content

Commit 5a8c4f1

Browse files
Fixed|Changed: Fix TermuxFileReceiverActivity incorrect handling of intent extras
- If the `EXTRA_TEXT` value of the intent passed was empty instead of `null`, it was incorrectly assumed that text was passed, even though a valid `EXTRA_STREAM` may have been passed. Now `EXTRA_STREAM` will be checked first. - Added empty extra and empty/`null` filename checks before trying to create a file with an empty filename and failing. - Enable logging of intent passed at verbose log level. - Changed to a better error dialog. Closes #2247
1 parent b68a398 commit 5a8c4f1

File tree

1 file changed

+54
-21
lines changed

1 file changed

+54
-21
lines changed

app/src/main/java/com/termux/filepicker/TermuxFileReceiverActivity.java

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
package com.termux.filepicker;
22

33
import android.app.Activity;
4-
import android.app.AlertDialog;
54
import android.content.Intent;
65
import android.database.Cursor;
76
import android.net.Uri;
87
import android.provider.OpenableColumns;
98
import android.util.Patterns;
109

1110
import com.termux.R;
11+
import com.termux.shared.data.DataUtils;
12+
import com.termux.shared.data.IntentUtils;
13+
import com.termux.shared.interact.MessageDialogUtils;
1214
import com.termux.shared.interact.TextInputDialogUtils;
1315
import com.termux.shared.termux.TermuxConstants;
1416
import com.termux.shared.termux.TermuxConstants.TERMUX_APP.TERMUX_SERVICE;
@@ -39,6 +41,8 @@ public class TermuxFileReceiverActivity extends Activity {
3941
*/
4042
boolean mFinishOnDismissNameDialog = true;
4143

44+
private static final String API_TAG = TermuxConstants.TERMUX_APP_NAME + "FileReceiver";
45+
4246
private static final String LOG_TAG = "TermuxFileReceiverActivity";
4347

4448
static boolean isSharedTextAnUrl(String sharedText) {
@@ -55,44 +59,66 @@ protected void onResume() {
5559
final String type = intent.getType();
5660
final String scheme = intent.getScheme();
5761

62+
Logger.logVerbose(LOG_TAG, "Intent Received:\n" + IntentUtils.getIntentString(intent));
63+
64+
final String sharedTitle = IntentUtils.getStringExtraIfSet(intent, Intent.EXTRA_TITLE, null);
65+
5866
if (Intent.ACTION_SEND.equals(action) && type != null) {
5967
final String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
6068
final Uri sharedUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
6169

62-
if (sharedText != null) {
70+
if (sharedUri != null) {
71+
handleContentUri(sharedUri, sharedTitle);
72+
} else if (sharedText != null) {
6373
if (isSharedTextAnUrl(sharedText)) {
6474
handleUrlAndFinish(sharedText);
6575
} else {
66-
String subject = intent.getStringExtra(Intent.EXTRA_SUBJECT);
67-
if (subject == null) subject = intent.getStringExtra(Intent.EXTRA_TITLE);
76+
String subject = IntentUtils.getStringExtraIfSet(intent, Intent.EXTRA_SUBJECT, null);
77+
if (subject == null) subject = sharedTitle;
6878
if (subject != null) subject += ".txt";
6979
promptNameAndSave(new ByteArrayInputStream(sharedText.getBytes(StandardCharsets.UTF_8)), subject);
7080
}
71-
} else if (sharedUri != null) {
72-
handleContentUri(sharedUri, intent.getStringExtra(Intent.EXTRA_TITLE));
7381
} else {
7482
showErrorDialogAndQuit("Send action without content - nothing to save.");
7583
}
76-
} else if ("content".equals(scheme)) {
77-
handleContentUri(intent.getData(), intent.getStringExtra(Intent.EXTRA_TITLE));
78-
} else if ("file".equals(scheme)) {
79-
// When e.g. clicking on a downloaded apk:
80-
String path = intent.getData().getPath();
81-
File file = new File(path);
82-
try {
83-
FileInputStream in = new FileInputStream(file);
84-
promptNameAndSave(in, file.getName());
85-
} catch (FileNotFoundException e) {
86-
showErrorDialogAndQuit("Cannot open file: " + e.getMessage() + ".");
87-
}
8884
} else {
89-
showErrorDialogAndQuit("Unable to receive any file or URL.");
85+
Uri dataUri = intent.getData();
86+
87+
if (dataUri == null) {
88+
showErrorDialogAndQuit("Data uri not passed.");
89+
return;
90+
}
91+
92+
if ("content".equals(scheme)) {
93+
handleContentUri(dataUri, sharedTitle);
94+
} else if ("file".equals(scheme)) {
95+
// When e.g. clicking on a downloaded apk:
96+
String path = dataUri.getPath();
97+
if (DataUtils.isNullOrEmpty(path)) {
98+
showErrorDialogAndQuit("File path from data uri is null, empty or invalid.");
99+
return;
100+
}
101+
102+
File file = new File(path);
103+
try {
104+
FileInputStream in = new FileInputStream(file);
105+
promptNameAndSave(in, file.getName());
106+
} catch (FileNotFoundException e) {
107+
showErrorDialogAndQuit("Cannot open file: " + e.getMessage() + ".");
108+
}
109+
} else {
110+
showErrorDialogAndQuit("Unable to receive any file or URL.");
111+
}
90112
}
91113
}
92114

93115
void showErrorDialogAndQuit(String message) {
94116
mFinishOnDismissNameDialog = false;
95-
new AlertDialog.Builder(this).setMessage(message).setOnDismissListener(dialog -> finish()).setPositiveButton(android.R.string.ok, (dialog, which) -> finish()).show();
117+
MessageDialogUtils.showMessage(this,
118+
API_TAG, message,
119+
null, (dialog, which) -> finish(),
120+
null, null,
121+
dialog -> finish());
96122
}
97123

98124
void handleContentUri(final Uri uri, String subjectFromIntent) {
@@ -157,10 +183,17 @@ void promptNameAndSave(final InputStream in, final String attachmentFileName) {
157183

158184
public File saveStreamWithName(InputStream in, String attachmentFileName) {
159185
File receiveDir = new File(TERMUX_RECEIVEDIR);
186+
187+
if (DataUtils.isNullOrEmpty(attachmentFileName)) {
188+
showErrorDialogAndQuit("File name cannot be null or empty");
189+
return null;
190+
}
191+
160192
if (!receiveDir.isDirectory() && !receiveDir.mkdirs()) {
161193
showErrorDialogAndQuit("Cannot create directory: " + receiveDir.getAbsolutePath());
162194
return null;
163195
}
196+
164197
try {
165198
final File outFile = new File(receiveDir, attachmentFileName);
166199
try (FileOutputStream f = new FileOutputStream(outFile)) {
@@ -182,7 +215,7 @@ void handleUrlAndFinish(final String url) {
182215
final File urlOpenerProgramFile = new File(URL_OPENER_PROGRAM);
183216
if (!urlOpenerProgramFile.isFile()) {
184217
showErrorDialogAndQuit("The following file does not exist:\n$HOME/bin/termux-url-opener\n\n"
185-
+ "Create this file as a script or a symlink - it will be called with the shared URL as only argument.");
218+
+ "Create this file as a script or a symlink - it will be called with the shared URL as the first argument.");
186219
return;
187220
}
188221

0 commit comments

Comments
 (0)