termux-app

Форк
0
102 строки · 3.7 Кб
1
package com.termux.shared.net.uri;
2

3
import android.net.Uri;
4

5
import androidx.annotation.NonNull;
6
import androidx.annotation.Nullable;
7

8
import com.termux.shared.data.DataUtils;
9
import com.termux.shared.file.FileUtils;
10

11
public 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
29
    public static String getUriFilePathWithFragment(Uri uri) {
30
        if (uri == null) return null;
31
        String path = uri.getPath();
32
        if (DataUtils.isNullOrEmpty(path)) return null;
33
        String fragment = uri.getFragment();
34
        return 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
46
    public static String getUriFileBasename(Uri uri, boolean withFragment) {
47
        if (uri == null) return null;
48

49
        String path;
50
        if (withFragment) {
51
            path = getUriFilePathWithFragment(uri);
52
        } else {
53
            path = uri.getPath();
54
            if (DataUtils.isNullOrEmpty(path)) return null;
55
        }
56

57
        return 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
     */
66
    public static Uri getFileUri(@NonNull String path) {
67
        return 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
     */
77
    public static Uri getFileUri(@NonNull String authority, @NonNull String path) {
78
        return 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
     */
87
    public static Uri getContentUri(@NonNull String path) {
88
        return 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
     */
98
    public static Uri getContentUri(@NonNull String authority, @NonNull String path) {
99
        return new Uri.Builder().scheme(UriScheme.SCHEME_CONTENT).authority(authority).path(path).build();
100
    }
101

102
}
103

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.