termux-app
166 строк · 6.4 Кб
1package com.termux.shared.data;
2
3import android.content.Intent;
4import android.os.Bundle;
5import android.os.Parcelable;
6
7import androidx.annotation.NonNull;
8
9import java.util.Arrays;
10
11public class IntentUtils {
12
13private 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*/
26public static String getStringExtraIfSet(@NonNull Intent intent, String key, String def, boolean throwExceptionIfNotSet) throws Exception {
27String value = getStringExtraIfSet(intent, key, def);
28if (value == null && throwExceptionIfNotSet)
29throw new Exception("The \"" + key + "\" key string value is null or empty");
30return 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*/
41public static String getStringExtraIfSet(@NonNull Intent intent, String key, String def) {
42String value = intent.getStringExtra(key);
43if (value == null || value.isEmpty()) {
44if (def != null && !def.isEmpty())
45return def;
46else
47return null;
48}
49return 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*/
61public static Integer getIntegerExtraIfSet(@NonNull Intent intent, String key, Integer def) {
62try {
63String value = intent.getStringExtra(key);
64if (value == null || value.isEmpty()) {
65return def;
66}
67
68return Integer.parseInt(value);
69}
70catch (Exception e) {
71return 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*/
87public static String[] getStringArrayExtraIfSet(@NonNull Intent intent, String key, String[] def, boolean throwExceptionIfNotSet) throws Exception {
88String[] value = getStringArrayExtraIfSet(intent, key, def);
89if (value == null && throwExceptionIfNotSet)
90throw new Exception("The \"" + key + "\" key string array is null or empty");
91return 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*/
102public static String[] getStringArrayExtraIfSet(Intent intent, String key, String[] def) {
103String[] value = intent.getStringArrayExtra(key);
104if (value == null || value.length == 0) {
105if (def != null && def.length != 0)
106return def;
107else
108return null;
109}
110return value;
111}
112
113public static String getIntentString(Intent intent) {
114if (intent == null) return null;
115
116return intent.toString() + "\n" + getBundleString(intent.getExtras());
117}
118
119public static String getBundleString(Bundle bundle) {
120if (bundle == null || bundle.size() == 0) return "Bundle[]";
121
122StringBuilder bundleString = new StringBuilder("Bundle[\n");
123boolean first = true;
124for (String key : bundle.keySet()) {
125if (!first)
126bundleString.append("\n");
127
128bundleString.append(key).append(": `");
129
130Object value = bundle.get(key);
131if (value instanceof int[]) {
132bundleString.append(Arrays.toString((int[]) value));
133} else if (value instanceof byte[]) {
134bundleString.append(Arrays.toString((byte[]) value));
135} else if (value instanceof boolean[]) {
136bundleString.append(Arrays.toString((boolean[]) value));
137} else if (value instanceof short[]) {
138bundleString.append(Arrays.toString((short[]) value));
139} else if (value instanceof long[]) {
140bundleString.append(Arrays.toString((long[]) value));
141} else if (value instanceof float[]) {
142bundleString.append(Arrays.toString((float[]) value));
143} else if (value instanceof double[]) {
144bundleString.append(Arrays.toString((double[]) value));
145} else if (value instanceof String[]) {
146bundleString.append(Arrays.toString((String[]) value));
147} else if (value instanceof CharSequence[]) {
148bundleString.append(Arrays.toString((CharSequence[]) value));
149} else if (value instanceof Parcelable[]) {
150bundleString.append(Arrays.toString((Parcelable[]) value));
151} else if (value instanceof Bundle) {
152bundleString.append(getBundleString((Bundle) value));
153} else {
154bundleString.append(value);
155}
156
157bundleString.append("`");
158
159first = false;
160}
161
162bundleString.append("\n]");
163return bundleString.toString();
164}
165
166}
167