Skip to content

Commit

Permalink
Fixed: Fix termux-open failing to open files with "#" and remove hard…
Browse files Browse the repository at this point in the history
…coded "content" and "file" strings

termux-open "/data/data/com.termux/files/home/te#st.sh"
  • Loading branch information
agnostic-apollo committed Jan 22, 2022
1 parent d96883c commit 3e518a6
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 6 deletions.
18 changes: 12 additions & 6 deletions app/src/main/java/com/termux/app/TermuxOpenReceiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.ActivityNotFoundException;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
Expand All @@ -15,6 +16,7 @@

import com.termux.app.utils.PluginUtils;
import com.termux.shared.data.IntentUtils;
import com.termux.shared.data.UriUtils;
import com.termux.shared.logger.Logger;
import com.termux.shared.termux.TermuxConstants;

Expand All @@ -37,8 +39,8 @@ public void onReceive(Context context, Intent intent) {
}

Logger.logVerbose(LOG_TAG, "Intent Received:\n" + IntentUtils.getIntentString(intent));
Logger.logVerbose(LOG_TAG, "uri: \"" + data + "\", path: \"" + data.getPath() + "\", fragment: \"" + data.getFragment() + "\"");

final String filePath = data.getPath();
final String contentTypeExtra = intent.getStringExtra("content-type");
final boolean useChooser = intent.getBooleanExtra("chooser", false);
final String intentAction = intent.getAction() == null ? Intent.ACTION_VIEW : intent.getAction();
Expand All @@ -52,8 +54,8 @@ public void onReceive(Context context, Intent intent) {
break;
}

final boolean isExternalUrl = data.getScheme() != null && !data.getScheme().equals("file");
if (isExternalUrl) {
String scheme = data.getScheme();
if (scheme != null && !ContentResolver.SCHEME_FILE.equals(scheme)) {
Intent urlIntent = new Intent(intentAction, data);
if (intentAction.equals(Intent.ACTION_SEND)) {
urlIntent.putExtra(Intent.EXTRA_TEXT, data.toString());
Expand All @@ -70,6 +72,9 @@ public void onReceive(Context context, Intent intent) {
return;
}

// Get full path including fragment (anything after last "#")
String filePath = UriUtils.getUriFilePath(data);

final File fileToShare = new File(filePath);
if (!(fileToShare.isFile() && fileToShare.canRead())) {
Logger.logError(LOG_TAG, "termux-open: Not a readable file: '" + fileToShare.getAbsolutePath() + "'");
Expand All @@ -93,7 +98,8 @@ public void onReceive(Context context, Intent intent) {
contentTypeToUse = contentTypeExtra;
}

Uri uriToShare = Uri.parse("content://" + TermuxConstants.TERMUX_FILE_SHARE_URI_AUTHORITY + fileToShare.getAbsolutePath());
// Do not create Uri with Uri.parse() and use Uri.Builder().path(), check UriUtils.getUriFilePath().
Uri uriToShare = UriUtils.getContentUri(TermuxConstants.TERMUX_FILE_SHARE_URI_AUTHORITY, fileToShare.getAbsolutePath());

if (Intent.ACTION_SEND.equals(intentAction)) {
sendIntent.putExtra(Intent.EXTRA_STREAM, uriToShare);
Expand Down Expand Up @@ -184,8 +190,8 @@ public ParcelFileDescriptor openFile(@NonNull Uri uri, @NonNull String mode) thr
File file = new File(uri.getPath());
try {
String path = file.getCanonicalPath();
String callingPackage = getCallingPackage();
Logger.logDebug(LOG_TAG, "Open file request received from " + callingPackage + " for \"" + path + "\" with mode \"" + mode + "\"");
String callingPackageName = getCallingPackage();
Logger.logDebug(LOG_TAG, "Open file request received from " + callingPackageName + " for \"" + path + "\" with mode \"" + mode + "\"");
String storagePath = Environment.getExternalStorageDirectory().getCanonicalPath();
// See https://support.google.com/faqs/answer/7496913:
if (!(path.startsWith(TermuxConstants.TERMUX_FILES_DIR_PATH) || path.startsWith(storagePath))) {
Expand Down
75 changes: 75 additions & 0 deletions termux-shared/src/main/java/com/termux/shared/data/UriUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package com.termux.shared.data;

import android.content.ContentResolver;
import android.net.Uri;

import androidx.annotation.NonNull;

public class UriUtils {

/**
* Get the full file path from a {@link Uri}.
*
* If the {@link Uri} was created from file path with {@link Uri#parse(String)}, like "am"
* command "-d" option does, and the path contained a "#", then anything after it would become
* the fragment and {@link Uri#getPath()} will only return the path before it, which would be
* invalid. The fragment must be manually appended to the path to get the full path.
*
* If the {@link Uri} was created with {@link Uri.Builder} and path was set
* with {@link Uri.Builder#path(String)}, then "#" will automatically be encoded to "%23"
* and separate fragment will not exist.
*
* @param uri The {@link Uri} to get basename from.
* @return Returns the file path if found, otherwise {@code null}.
*/
public static String getUriFilePath(Uri uri) {
if (uri == null) return null;
String path = uri.getPath();
if (DataUtils.isNullOrEmpty(path)) return null;
String fragment = uri.getFragment();
return path + (DataUtils.isNullOrEmpty(fragment) ? "" : "#" + fragment);
}

/**
* Get {@link ContentResolver#SCHEME_FILE} {@link Uri} for path.
*
* @param path The path for the {@link Uri}.
* @return Returns the {@link Uri}.
*/
public static Uri getFileUri(@NonNull String path) {
return new Uri.Builder().scheme(ContentResolver.SCHEME_FILE).path(path).build();
}

/**
* Get {@link ContentResolver#SCHEME_FILE} {@link Uri} for path.
*
* @param authority The authority for the {@link Uri}.
* @param path The path for the {@link Uri}.
* @return Returns the {@link Uri}.
*/
public static Uri getFileUri(@NonNull String authority, @NonNull String path) {
return new Uri.Builder().scheme(ContentResolver.SCHEME_FILE).authority(authority).path(path).build();
}

/**
* Get {@link ContentResolver#SCHEME_CONTENT} {@link Uri} for path.
*
* @param path The path for the {@link Uri}.
* @return Returns the {@link Uri}.
*/
public static Uri getContentUri(@NonNull String path) {
return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).path(path).build();
}

/**
* Get {@link ContentResolver#SCHEME_CONTENT} {@link Uri} for path.
*
* @param authority The authority for the {@link Uri}.
* @param path The path for the {@link Uri}.
* @return Returns the {@link Uri}.
*/
public static Uri getContentUri(@NonNull String authority, @NonNull String path) {
return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority).path(path).build();
}

}

1 comment on commit 3e518a6

@mountaineerbr
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed termux-open does not open files with ":" in them in my system, either. Hope this fixes this issue as well. Cheers!

Please sign in to comment.