-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparseUriInternal.java
More file actions
220 lines (197 loc) · 8.97 KB
/
Copy pathparseUriInternal.java
File metadata and controls
220 lines (197 loc) · 8.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//https://cs.android.com/android/platform/superproject/+/master:frameworks/base/core/java/android/content/Intent.java
private static Intent parseUriInternal(String uri, @UriFlags int flags)
throws URISyntaxException {
int i = 0;
try {
final boolean androidApp = uri.startsWith("android-app:");
// Validate intent scheme if requested.
if ((flags&(URI_INTENT_SCHEME|URI_ANDROID_APP_SCHEME)) != 0) {
if (!uri.startsWith("intent:") && !androidApp) {
Intent intent = new Intent(ACTION_VIEW);
try {
intent.setData(Uri.parse(uri));
} catch (IllegalArgumentException e) {
throw new URISyntaxException(uri, e.getMessage());
}
return intent;
}
}
i = uri.lastIndexOf("#");
// simple case
if (i == -1) {
if (!androidApp) {
return new Intent(ACTION_VIEW, Uri.parse(uri));
}
// old format Intent URI
} else if (!uri.startsWith("#Intent;", i)) {
if (!androidApp) {
return getIntentOld(uri, flags);
} else {
i = -1;
}
}
// new format
Intent intent = new Intent(ACTION_VIEW);
Intent baseIntent = intent;
boolean explicitAction = false;
boolean inSelector = false;
// fetch data part, if present
String scheme = null;
String data;
if (i >= 0) {
data = uri.substring(0, i);
i += 8; // length of "#Intent;"
} else {
data = uri;
}
// loop over contents of Intent, all name=value;
while (i >= 0 && !uri.startsWith("end", i)) {
int eq = uri.indexOf('=', i);
if (eq < 0) eq = i-1;
int semi = uri.indexOf(';', i);
String value = eq < semi ? Uri.decode(uri.substring(eq + 1, semi)) : "";
// action
if (uri.startsWith("action=", i)) {
intent.setAction(value);
if (!inSelector) {
explicitAction = true;
}
}
// categories
else if (uri.startsWith("category=", i)) {
intent.addCategory(value);
}
// type
else if (uri.startsWith("type=", i)) {
intent.mType = value;
}
// identifier
else if (uri.startsWith("identifier=", i)) {
intent.mIdentifier = value;
}
// launch flags
else if (uri.startsWith("launchFlags=", i)) {
intent.mFlags = Integer.decode(value).intValue();
if ((flags& URI_ALLOW_UNSAFE) == 0) {
intent.mFlags &= ~IMMUTABLE_FLAGS;
}
}
// package
else if (uri.startsWith("package=", i)) {
intent.mPackage = value;
}
// component
else if (uri.startsWith("component=", i)) {
intent.mComponent = ComponentName.unflattenFromString(value);
}
// scheme
else if (uri.startsWith("scheme=", i)) {
if (inSelector) {
intent.mData = Uri.parse(value + ":");
} else {
scheme = value;
}
}
// source bounds
else if (uri.startsWith("sourceBounds=", i)) {
intent.mSourceBounds = Rect.unflattenFromString(value);
}
// selector
else if (semi == (i+3) && uri.startsWith("SEL", i)) {
intent = new Intent();
inSelector = true;
}
// extra
else {
String key = Uri.decode(uri.substring(i + 2, eq));
// create Bundle if it doesn't already exist
if (intent.mExtras == null) intent.mExtras = new Bundle();
Bundle b = intent.mExtras;
// add EXTRA
if (uri.startsWith("S.", i)) b.putString(key, value);
else if (uri.startsWith("B.", i)) b.putBoolean(key, Boolean.parseBoolean(value));
else if (uri.startsWith("b.", i)) b.putByte(key, Byte.parseByte(value));
else if (uri.startsWith("c.", i)) b.putChar(key, value.charAt(0));
else if (uri.startsWith("d.", i)) b.putDouble(key, Double.parseDouble(value));
else if (uri.startsWith("f.", i)) b.putFloat(key, Float.parseFloat(value));
else if (uri.startsWith("i.", i)) b.putInt(key, Integer.parseInt(value));
else if (uri.startsWith("l.", i)) b.putLong(key, Long.parseLong(value));
else if (uri.startsWith("s.", i)) b.putShort(key, Short.parseShort(value));
else throw new URISyntaxException(uri, "unknown EXTRA type", i);
}
// move to the next item
i = semi + 1;
}
if (inSelector) {
// The Intent had a selector; fix it up.
if (baseIntent.mPackage == null) {
baseIntent.setSelector(intent);
}
intent = baseIntent;
}
if (data != null) {
if (data.startsWith("intent:")) {
data = data.substring(7);
if (scheme != null) {
data = scheme + ':' + data;
}
} else if (data.startsWith("android-app:")) {
if (data.charAt(12) == '/' && data.charAt(13) == '/') {
// Correctly formed android-app, first part is package name.
int end = data.indexOf('/', 14);
if (end < 0) {
// All we have is a package name.
intent.mPackage = data.substring(14);
if (!explicitAction) {
intent.setAction(ACTION_MAIN);
}
data = "";
} else {
// Target the Intent at the given package name always.
String authority = null;
intent.mPackage = data.substring(14, end);
int newEnd;
if ((end+1) < data.length()) {
if ((newEnd=data.indexOf('/', end+1)) >= 0) {
// Found a scheme, remember it.
scheme = data.substring(end+1, newEnd);
end = newEnd;
if (end < data.length() && (newEnd=data.indexOf('/', end+1)) >= 0) {
// Found a authority, remember it.
authority = data.substring(end+1, newEnd);
end = newEnd;
}
} else {
// All we have is a scheme.
scheme = data.substring(end+1);
}
}
if (scheme == null) {
// If there was no scheme, then this just targets the package.
if (!explicitAction) {
intent.setAction(ACTION_MAIN);
}
data = "";
} else if (authority == null) {
data = scheme + ":";
} else {
data = scheme + "://" + authority + data.substring(end);
}
}
} else {
data = "";
}
}
if (data.length() > 0) {
try {
intent.mData = Uri.parse(data);
} catch (IllegalArgumentException e) {
throw new URISyntaxException(uri, e.getMessage());
}
}
}
return intent;
} catch (IndexOutOfBoundsException e) {
throw new URISyntaxException(uri, "illegal Intent URI format", i);
}
}