mirror of
https://github.com/fankes/termux-app.git
synced 2025-09-06 02:35:19 +08:00
Added: Add UriScheme and move UriUtils to com.termux.shared.net package
This commit is contained in:
@@ -2,7 +2,6 @@ package com.termux.app;
|
|||||||
|
|
||||||
import android.content.ActivityNotFoundException;
|
import android.content.ActivityNotFoundException;
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.ContentResolver;
|
|
||||||
import android.content.ContentValues;
|
import android.content.ContentValues;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
@@ -16,8 +15,9 @@ import android.webkit.MimeTypeMap;
|
|||||||
|
|
||||||
import com.termux.app.utils.PluginUtils;
|
import com.termux.app.utils.PluginUtils;
|
||||||
import com.termux.shared.data.IntentUtils;
|
import com.termux.shared.data.IntentUtils;
|
||||||
import com.termux.shared.data.UriUtils;
|
import com.termux.shared.net.UriUtils;
|
||||||
import com.termux.shared.logger.Logger;
|
import com.termux.shared.logger.Logger;
|
||||||
|
import com.termux.shared.models.net.UriScheme;
|
||||||
import com.termux.shared.termux.TermuxConstants;
|
import com.termux.shared.termux.TermuxConstants;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
@@ -55,7 +55,7 @@ public class TermuxOpenReceiver extends BroadcastReceiver {
|
|||||||
}
|
}
|
||||||
|
|
||||||
String scheme = data.getScheme();
|
String scheme = data.getScheme();
|
||||||
if (scheme != null && !ContentResolver.SCHEME_FILE.equals(scheme)) {
|
if (scheme != null && !UriScheme.SCHEME_FILE.equals(scheme)) {
|
||||||
Intent urlIntent = new Intent(intentAction, data);
|
Intent urlIntent = new Intent(intentAction, data);
|
||||||
if (intentAction.equals(Intent.ACTION_SEND)) {
|
if (intentAction.equals(Intent.ACTION_SEND)) {
|
||||||
urlIntent.putExtra(Intent.EXTRA_TEXT, data.toString());
|
urlIntent.putExtra(Intent.EXTRA_TEXT, data.toString());
|
||||||
|
@@ -26,7 +26,7 @@ import com.termux.app.settings.properties.TermuxAppSharedProperties;
|
|||||||
import com.termux.app.terminal.TermuxTerminalSessionClient;
|
import com.termux.app.terminal.TermuxTerminalSessionClient;
|
||||||
import com.termux.app.utils.PluginUtils;
|
import com.termux.app.utils.PluginUtils;
|
||||||
import com.termux.shared.data.IntentUtils;
|
import com.termux.shared.data.IntentUtils;
|
||||||
import com.termux.shared.data.UriUtils;
|
import com.termux.shared.net.UriUtils;
|
||||||
import com.termux.shared.models.errors.Errno;
|
import com.termux.shared.models.errors.Errno;
|
||||||
import com.termux.shared.shell.ShellUtils;
|
import com.termux.shared.shell.ShellUtils;
|
||||||
import com.termux.shared.termux.shell.TermuxShellEnvironmentClient;
|
import com.termux.shared.termux.shell.TermuxShellEnvironmentClient;
|
||||||
|
@@ -1,7 +1,6 @@
|
|||||||
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;
|
||||||
@@ -13,8 +12,9 @@ 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.net.UriUtils;
|
||||||
import com.termux.shared.interact.MessageDialogUtils;
|
import com.termux.shared.interact.MessageDialogUtils;
|
||||||
|
import com.termux.shared.models.net.UriScheme;
|
||||||
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;
|
||||||
import com.termux.shared.termux.TermuxConstants.TERMUX_APP.TERMUX_SERVICE;
|
import com.termux.shared.termux.TermuxConstants.TERMUX_APP.TERMUX_SERVICE;
|
||||||
@@ -93,9 +93,9 @@ public class TermuxFileReceiverActivity extends Activity {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
|
if (UriScheme.SCHEME_CONTENT.equals(scheme)) {
|
||||||
handleContentUri(dataUri, sharedTitle);
|
handleContentUri(dataUri, sharedTitle);
|
||||||
} else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
|
} else if (UriScheme.SCHEME_FILE.equals(scheme)) {
|
||||||
Logger.logVerbose(LOG_TAG, "uri: \"" + dataUri + "\", path: \"" + dataUri.getPath() + "\", fragment: \"" + dataUri.getFragment() + "\"");
|
Logger.logVerbose(LOG_TAG, "uri: \"" + dataUri + "\", path: \"" + dataUri.getPath() + "\", fragment: \"" + dataUri.getFragment() + "\"");
|
||||||
|
|
||||||
// Get full path including fragment (anything after last "#")
|
// Get full path including fragment (anything after last "#")
|
||||||
|
@@ -0,0 +1,28 @@
|
|||||||
|
package com.termux.shared.models.net;
|
||||||
|
|
||||||
|
import android.net.Uri;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The {@link Uri} schemes.
|
||||||
|
*
|
||||||
|
* https://www.iana.org/assignments/uri-schemes/uri-schemes.xhtml
|
||||||
|
* https://en.wikipedia.org/wiki/List_of_URI_schemes
|
||||||
|
*/
|
||||||
|
public class UriScheme {
|
||||||
|
|
||||||
|
/** Android app resource. */
|
||||||
|
public static final String SCHEME_ANDROID_RESOURCE = "android.resource";
|
||||||
|
|
||||||
|
/** Android content provider. https://www.iana.org/assignments/uri-schemes/prov/content. */
|
||||||
|
public static final String SCHEME_CONTENT = "content";
|
||||||
|
|
||||||
|
/** Filesystem or android app asset. https://www.rfc-editor.org/rfc/rfc8089.html. */
|
||||||
|
public static final String SCHEME_FILE = "file";
|
||||||
|
|
||||||
|
/* Hypertext Transfer Protocol. */
|
||||||
|
public static final String SCHEME_HTTP = "http";
|
||||||
|
|
||||||
|
/* Hypertext Transfer Protocol Secure. */
|
||||||
|
public static final String SCHEME_HTTPS = "https";
|
||||||
|
|
||||||
|
}
|
@@ -1,25 +1,26 @@
|
|||||||
package com.termux.shared.data;
|
package com.termux.shared.net;
|
||||||
|
|
||||||
import android.content.ContentResolver;
|
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
|
import com.termux.shared.data.DataUtils;
|
||||||
import com.termux.shared.file.FileUtils;
|
import com.termux.shared.file.FileUtils;
|
||||||
|
import com.termux.shared.models.net.UriScheme;
|
||||||
|
|
||||||
public class UriUtils {
|
public class UriUtils {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the full file path from a {@link Uri}.
|
* 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"
|
* 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
|
* 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
|
* 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.
|
* 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
|
* 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"
|
* with {@link Uri.Builder#path(String)}, then "#" will automatically be encoded to "%23"
|
||||||
* and separate fragment will not exist.
|
* and separate fragment will not exist.
|
||||||
*
|
*
|
||||||
* @param uri The {@link Uri} to get basename from.
|
* @param uri The {@link Uri} to get basename from.
|
||||||
* @return Returns the file path if found, otherwise {@code null}.
|
* @return Returns the file path if found, otherwise {@code null}.
|
||||||
@@ -55,45 +56,45 @@ public class UriUtils {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get {@link ContentResolver#SCHEME_FILE} {@link Uri} for path.
|
* Get {@link UriScheme#SCHEME_FILE} {@link Uri} for path.
|
||||||
*
|
*
|
||||||
* @param path The path for the {@link Uri}.
|
* @param path The path for the {@link Uri}.
|
||||||
* @return Returns the {@link Uri}.
|
* @return Returns the {@link Uri}.
|
||||||
*/
|
*/
|
||||||
public static Uri getFileUri(@NonNull String path) {
|
public static Uri getFileUri(@NonNull String path) {
|
||||||
return new Uri.Builder().scheme(ContentResolver.SCHEME_FILE).path(path).build();
|
return new Uri.Builder().scheme(UriScheme.SCHEME_FILE).path(path).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get {@link ContentResolver#SCHEME_FILE} {@link Uri} for path.
|
* Get {@link UriScheme#SCHEME_FILE} {@link Uri} for path.
|
||||||
*
|
*
|
||||||
* @param authority The authority for the {@link Uri}.
|
* @param authority The authority for the {@link Uri}.
|
||||||
* @param path The path for the {@link Uri}.
|
* @param path The path for the {@link Uri}.
|
||||||
* @return Returns the {@link Uri}.
|
* @return Returns the {@link Uri}.
|
||||||
*/
|
*/
|
||||||
public static Uri getFileUri(@NonNull String authority, @NonNull String path) {
|
public static Uri getFileUri(@NonNull String authority, @NonNull String path) {
|
||||||
return new Uri.Builder().scheme(ContentResolver.SCHEME_FILE).authority(authority).path(path).build();
|
return new Uri.Builder().scheme(UriScheme.SCHEME_FILE).authority(authority).path(path).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get {@link ContentResolver#SCHEME_CONTENT} {@link Uri} for path.
|
* Get {@link UriScheme#SCHEME_CONTENT} {@link Uri} for path.
|
||||||
*
|
*
|
||||||
* @param path The path for the {@link Uri}.
|
* @param path The path for the {@link Uri}.
|
||||||
* @return Returns the {@link Uri}.
|
* @return Returns the {@link Uri}.
|
||||||
*/
|
*/
|
||||||
public static Uri getContentUri(@NonNull String path) {
|
public static Uri getContentUri(@NonNull String path) {
|
||||||
return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).path(path).build();
|
return new Uri.Builder().scheme(UriScheme.SCHEME_CONTENT).path(path).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get {@link ContentResolver#SCHEME_CONTENT} {@link Uri} for path.
|
* Get {@link UriScheme#SCHEME_CONTENT} {@link Uri} for path.
|
||||||
*
|
*
|
||||||
* @param authority The authority for the {@link Uri}.
|
* @param authority The authority for the {@link Uri}.
|
||||||
* @param path The path for the {@link Uri}.
|
* @param path The path for the {@link Uri}.
|
||||||
* @return Returns the {@link Uri}.
|
* @return Returns the {@link Uri}.
|
||||||
*/
|
*/
|
||||||
public static Uri getContentUri(@NonNull String authority, @NonNull String path) {
|
public static Uri getContentUri(@NonNull String authority, @NonNull String path) {
|
||||||
return new Uri.Builder().scheme(ContentResolver.SCHEME_CONTENT).authority(authority).path(path).build();
|
return new Uri.Builder().scheme(UriScheme.SCHEME_CONTENT).authority(authority).path(path).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Reference in New Issue
Block a user