-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed: Fix termux-open failing to open files with "#" and remove hard…
…coded "content" and "file" strings termux-open "/data/data/com.termux/files/home/te#st.sh"
- Loading branch information
1 parent
d96883c
commit 3e518a6
Showing
2 changed files
with
87 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
termux-shared/src/main/java/com/termux/shared/data/UriUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |
3e518a6
There was a problem hiding this comment.
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!