termux-app
102 строки · 3.7 Кб
1package com.termux.shared.net.uri;
2
3import android.net.Uri;
4
5import androidx.annotation.NonNull;
6import androidx.annotation.Nullable;
7
8import com.termux.shared.data.DataUtils;
9import com.termux.shared.file.FileUtils;
10
11public class UriUtils {
12
13/**
14* Get the full file path from a {@link Uri} including the fragment.
15*
16* If the {@link Uri} was created from file path with {@link Uri#parse(String)}, like "am"
17* command "-d" option does, and the path contained a "#", then anything after it would become
18* the fragment and {@link Uri#getPath()} will only return the path before it, which would be
19* invalid. The fragment must be manually appended to the path to get the full path.
20*
21* If the {@link Uri} was created with {@link Uri.Builder} and path was set
22* with {@link Uri.Builder#path(String)}, then "#" will automatically be encoded to "%23"
23* and separate fragment will not exist.
24*
25* @param uri The {@link Uri} to get file path from.
26* @return Returns the file path if found, otherwise {@code null}.
27*/
28@Nullable
29public static String getUriFilePathWithFragment(Uri uri) {
30if (uri == null) return null;
31String path = uri.getPath();
32if (DataUtils.isNullOrEmpty(path)) return null;
33String fragment = uri.getFragment();
34return path + (DataUtils.isNullOrEmpty(fragment) ? "" : "#" + fragment);
35}
36
37/**
38* Get the file basename from a {@link Uri}. The file basename is anything after last forward
39* slash "/" in the path, or the path itself if its not found.
40*
41* @param uri The {@link Uri} to get basename from.
42* @param withFragment If the {@link Uri} fragment should be included in basename.
43* @return Returns the file basename if found, otherwise {@code null}.
44*/
45@Nullable
46public static String getUriFileBasename(Uri uri, boolean withFragment) {
47if (uri == null) return null;
48
49String path;
50if (withFragment) {
51path = getUriFilePathWithFragment(uri);
52} else {
53path = uri.getPath();
54if (DataUtils.isNullOrEmpty(path)) return null;
55}
56
57return FileUtils.getFileBasename(path);
58}
59
60/**
61* Get {@link UriScheme#SCHEME_FILE} {@link Uri} for path.
62*
63* @param path The path for the {@link Uri}.
64* @return Returns the {@link Uri}.
65*/
66public static Uri getFileUri(@NonNull String path) {
67return new Uri.Builder().scheme(UriScheme.SCHEME_FILE).path(path).build();
68}
69
70/**
71* Get {@link UriScheme#SCHEME_FILE} {@link Uri} for path.
72*
73* @param authority The authority for the {@link Uri}.
74* @param path The path for the {@link Uri}.
75* @return Returns the {@link Uri}.
76*/
77public static Uri getFileUri(@NonNull String authority, @NonNull String path) {
78return new Uri.Builder().scheme(UriScheme.SCHEME_FILE).authority(authority).path(path).build();
79}
80
81/**
82* Get {@link UriScheme#SCHEME_CONTENT} {@link Uri} for path.
83*
84* @param path The path for the {@link Uri}.
85* @return Returns the {@link Uri}.
86*/
87public static Uri getContentUri(@NonNull String path) {
88return new Uri.Builder().scheme(UriScheme.SCHEME_CONTENT).path(path).build();
89}
90
91/**
92* Get {@link UriScheme#SCHEME_CONTENT} {@link Uri} for path.
93*
94* @param authority The authority for the {@link Uri}.
95* @param path The path for the {@link Uri}.
96* @return Returns the {@link Uri}.
97*/
98public static Uri getContentUri(@NonNull String authority, @NonNull String path) {
99return new Uri.Builder().scheme(UriScheme.SCHEME_CONTENT).authority(authority).path(path).build();
100}
101
102}
103