Fixed: Fix TermuxFileReceiverActivity failing to open files with "#" and remove hardcoded "content" and "file" strings and fix indentation

am start -a android.intent.action.VIEW -n com.termux/.filepicker.TermuxFileReceiverActivity -d "file:///data/data/com.termux/files/home/te#st.sh"
This commit is contained in:
agnostic-apollo
2021-10-23 04:33:23 +05:00
parent b559d5a0bd
commit 74b23cb209

View File

@@ -1,15 +1,19 @@
package com.termux.filepicker; package com.termux.filepicker;
import android.app.Activity; import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent; import android.content.Intent;
import android.database.Cursor; import android.database.Cursor;
import android.net.Uri; import android.net.Uri;
import android.provider.OpenableColumns; import android.provider.OpenableColumns;
import android.util.Patterns; import android.util.Patterns;
import androidx.annotation.NonNull;
import com.termux.R; import com.termux.R;
import com.termux.shared.data.DataUtils; import com.termux.shared.data.DataUtils;
import com.termux.shared.data.IntentUtils; import com.termux.shared.data.IntentUtils;
import com.termux.shared.data.UriUtils;
import com.termux.shared.interact.MessageDialogUtils; import com.termux.shared.interact.MessageDialogUtils;
import com.termux.shared.termux.interact.TextInputDialogUtils; import com.termux.shared.termux.interact.TextInputDialogUtils;
import com.termux.shared.termux.TermuxConstants; import com.termux.shared.termux.TermuxConstants;
@@ -89,11 +93,13 @@ public class TermuxFileReceiverActivity extends Activity {
return; return;
} }
if ("content".equals(scheme)) { if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
handleContentUri(dataUri, sharedTitle); handleContentUri(dataUri, sharedTitle);
} else if ("file".equals(scheme)) { } else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
// When e.g. clicking on a downloaded apk: Logger.logVerbose(LOG_TAG, "uri: \"" + dataUri + "\", path: \"" + dataUri.getPath() + "\", fragment: \"" + dataUri.getFragment() + "\"");
String path = dataUri.getPath();
// Get full path including fragment (anything after last "#")
String path = UriUtils.getUriFilePath(dataUri);
if (DataUtils.isNullOrEmpty(path)) { if (DataUtils.isNullOrEmpty(path)) {
showErrorDialogAndQuit("File path from data uri is null, empty or invalid."); showErrorDialogAndQuit("File path from data uri is null, empty or invalid.");
return; return;
@@ -121,8 +127,10 @@ public class TermuxFileReceiverActivity extends Activity {
dialog -> finish()); dialog -> finish());
} }
void handleContentUri(final Uri uri, String subjectFromIntent) { void handleContentUri(@NonNull final Uri uri, String subjectFromIntent) {
try { try {
Logger.logVerbose(LOG_TAG, "uri: \"" + uri + "\", path: \"" + uri.getPath() + "\", fragment: \"" + uri.getFragment() + "\"");
String attachmentFileName = null; String attachmentFileName = null;
String[] projection = new String[]{OpenableColumns.DISPLAY_NAME}; String[] projection = new String[]{OpenableColumns.DISPLAY_NAME};
@@ -144,29 +152,30 @@ public class TermuxFileReceiverActivity extends Activity {
} }
void promptNameAndSave(final InputStream in, final String attachmentFileName) { void promptNameAndSave(final InputStream in, final String attachmentFileName) {
TextInputDialogUtils.textInput(this, R.string.title_file_received, attachmentFileName, R.string.action_file_received_edit, text -> { TextInputDialogUtils.textInput(this, R.string.title_file_received, attachmentFileName,
File outFile = saveStreamWithName(in, text); R.string.action_file_received_edit, text -> {
if (outFile == null) return; File outFile = saveStreamWithName(in, text);
if (outFile == null) return;
final File editorProgramFile = new File(EDITOR_PROGRAM); final File editorProgramFile = new File(EDITOR_PROGRAM);
if (!editorProgramFile.isFile()) { if (!editorProgramFile.isFile()) {
showErrorDialogAndQuit("The following file does not exist:\n$HOME/bin/termux-file-editor\n\n" 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."); + "Create this file as a script or a symlink - it will be called with the received file as only argument.");
return; return;
} }
// Do this for the user if necessary: // Do this for the user if necessary:
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
editorProgramFile.setExecutable(true); editorProgramFile.setExecutable(true);
final Uri scriptUri = new Uri.Builder().scheme("file").path(EDITOR_PROGRAM).build(); final Uri scriptUri = UriUtils.getFileUri(EDITOR_PROGRAM);
Intent executeIntent = new Intent(TERMUX_SERVICE.ACTION_SERVICE_EXECUTE, scriptUri); Intent executeIntent = new Intent(TERMUX_SERVICE.ACTION_SERVICE_EXECUTE, scriptUri);
executeIntent.setClass(TermuxFileReceiverActivity.this, TermuxService.class); executeIntent.setClass(TermuxFileReceiverActivity.this, TermuxService.class);
executeIntent.putExtra(TERMUX_SERVICE.EXTRA_ARGUMENTS, new String[]{outFile.getAbsolutePath()}); executeIntent.putExtra(TERMUX_SERVICE.EXTRA_ARGUMENTS, new String[]{outFile.getAbsolutePath()});
startService(executeIntent); startService(executeIntent);
finish(); finish();
}, },
R.string.action_file_received_open_directory, text -> { R.string.action_file_received_open_directory, text -> {
if (saveStreamWithName(in, text) == null) return; if (saveStreamWithName(in, text) == null) return;
@@ -223,7 +232,7 @@ public class TermuxFileReceiverActivity extends Activity {
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
urlOpenerProgramFile.setExecutable(true); urlOpenerProgramFile.setExecutable(true);
final Uri urlOpenerProgramUri = new Uri.Builder().scheme("file").path(URL_OPENER_PROGRAM).build(); final Uri urlOpenerProgramUri = UriUtils.getFileUri(URL_OPENER_PROGRAM);
Intent executeIntent = new Intent(TERMUX_SERVICE.ACTION_SERVICE_EXECUTE, urlOpenerProgramUri); Intent executeIntent = new Intent(TERMUX_SERVICE.ACTION_SERVICE_EXECUTE, urlOpenerProgramUri);
executeIntent.setClass(TermuxFileReceiverActivity.this, TermuxService.class); executeIntent.setClass(TermuxFileReceiverActivity.this, TermuxService.class);