termux-app

Форк
0
166 строк · 6.4 Кб
1
package com.termux.shared.data;
2

3
import android.content.Intent;
4
import android.os.Bundle;
5
import android.os.Parcelable;
6

7
import androidx.annotation.NonNull;
8

9
import java.util.Arrays;
10

11
public class IntentUtils {
12

13
    private static final String LOG_TAG = "IntentUtils";
14

15

16
    /**
17
     * Get a {@link String} extra from an {@link Intent} if its not {@code null} or empty.
18
     *
19
     * @param intent The {@link Intent} to get the extra from.
20
     * @param key The {@link String} key name.
21
     * @param def The default value if extra is not set.
22
     * @param throwExceptionIfNotSet If set to {@code true}, then an exception will be thrown if extra
23
     *                               is not set.
24
     * @return Returns the {@link String} extra if set, otherwise {@code null}.
25
     */
26
    public static String getStringExtraIfSet(@NonNull Intent intent, String key, String def, boolean throwExceptionIfNotSet) throws Exception {
27
        String value = getStringExtraIfSet(intent, key, def);
28
        if (value == null && throwExceptionIfNotSet)
29
            throw new Exception("The \"" + key + "\" key string value is null or empty");
30
        return value;
31
    }
32

33
    /**
34
     * Get a {@link String} extra from an {@link Intent} if its not {@code null} or empty.
35
     *
36
     * @param intent The {@link Intent} to get the extra from.
37
     * @param key The {@link String} key name.
38
     * @param def The default value if extra is not set.
39
     * @return Returns the {@link String} extra if set, otherwise {@code null}.
40
     */
41
    public static String getStringExtraIfSet(@NonNull Intent intent, String key, String def) {
42
        String value = intent.getStringExtra(key);
43
        if (value == null || value.isEmpty()) {
44
            if (def != null && !def.isEmpty())
45
                return def;
46
            else
47
                return null;
48
        }
49
        return value;
50
    }
51

52
    /**
53
     * Get an {@link Integer} from an {@link Intent} stored as a {@link String} extra if its not
54
     * {@code null} or empty.
55
     *
56
     * @param intent The {@link Intent} to get the extra from.
57
     * @param key The {@link String} key name.
58
     * @param def The default value if extra is not set.
59
     * @return Returns the {@link Integer} extra if set, otherwise {@code null}.
60
     */
61
    public static Integer getIntegerExtraIfSet(@NonNull Intent intent, String key, Integer def) {
62
        try {
63
            String value = intent.getStringExtra(key);
64
            if (value == null || value.isEmpty()) {
65
                return def;
66
            }
67

68
            return Integer.parseInt(value);
69
        }
70
        catch (Exception e) {
71
            return def;
72
        }
73
    }
74

75

76

77
    /**
78
     * Get a {@link String[]} extra from an {@link Intent} if its not {@code null} or empty.
79
     *
80
     * @param intent The {@link Intent} to get the extra from.
81
     * @param key The {@link String} key name.
82
     * @param def The default value if extra is not set.
83
     * @param throwExceptionIfNotSet If set to {@code true}, then an exception will be thrown if extra
84
     *                               is not set.
85
     * @return Returns the {@link String[]} extra if set, otherwise {@code null}.
86
     */
87
    public static String[] getStringArrayExtraIfSet(@NonNull Intent intent, String key, String[] def, boolean throwExceptionIfNotSet) throws Exception {
88
        String[] value = getStringArrayExtraIfSet(intent, key, def);
89
        if (value == null && throwExceptionIfNotSet)
90
            throw new Exception("The \"" + key + "\" key string array is null or empty");
91
        return value;
92
    }
93

94
    /**
95
     * Get a {@link String[]} extra from an {@link Intent} if its not {@code null} or empty.
96
     *
97
     * @param intent The {@link Intent} to get the extra from.
98
     * @param key The {@link String} key name.
99
     * @param def The default value if extra is not set.
100
     * @return Returns the {@link String[]} extra if set, otherwise {@code null}.
101
     */
102
    public static String[] getStringArrayExtraIfSet(Intent intent, String key, String[] def) {
103
        String[] value = intent.getStringArrayExtra(key);
104
        if (value == null || value.length == 0) {
105
            if (def != null && def.length != 0)
106
                return def;
107
            else
108
                return null;
109
        }
110
        return value;
111
    }
112

113
    public static String getIntentString(Intent intent) {
114
        if (intent == null) return null;
115

116
        return intent.toString() + "\n" + getBundleString(intent.getExtras());
117
    }
118

119
    public static String getBundleString(Bundle bundle) {
120
        if (bundle == null || bundle.size() == 0) return "Bundle[]";
121

122
        StringBuilder bundleString = new StringBuilder("Bundle[\n");
123
        boolean first = true;
124
        for (String key : bundle.keySet()) {
125
            if (!first)
126
                bundleString.append("\n");
127

128
            bundleString.append(key).append(": `");
129

130
            Object value = bundle.get(key);
131
            if (value instanceof int[]) {
132
                bundleString.append(Arrays.toString((int[]) value));
133
            } else if (value instanceof byte[]) {
134
                bundleString.append(Arrays.toString((byte[]) value));
135
            } else if (value instanceof boolean[]) {
136
                bundleString.append(Arrays.toString((boolean[]) value));
137
            } else if (value instanceof short[]) {
138
                bundleString.append(Arrays.toString((short[]) value));
139
            } else if (value instanceof long[]) {
140
                bundleString.append(Arrays.toString((long[]) value));
141
            } else if (value instanceof float[]) {
142
                bundleString.append(Arrays.toString((float[]) value));
143
            } else if (value instanceof double[]) {
144
                bundleString.append(Arrays.toString((double[]) value));
145
            } else if (value instanceof String[]) {
146
                bundleString.append(Arrays.toString((String[]) value));
147
            } else if (value instanceof CharSequence[]) {
148
                bundleString.append(Arrays.toString((CharSequence[]) value));
149
            } else if (value instanceof Parcelable[]) {
150
                bundleString.append(Arrays.toString((Parcelable[]) value));
151
            } else if (value instanceof Bundle) {
152
                bundleString.append(getBundleString((Bundle) value));
153
            } else {
154
                bundleString.append(value);
155
            }
156

157
            bundleString.append("`");
158

159
            first = false;
160
        }
161

162
        bundleString.append("\n]");
163
        return bundleString.toString();
164
    }
165

166
}
167

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

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

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

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