/
IntentProxy.java
443 lines (386 loc) Β· 11.4 KB
/
IntentProxy.java
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
/**
* Appcelerator Titanium Mobile
* Copyright (c) 2009-2013 by Appcelerator, Inc. All Rights Reserved.
* Licensed under the terms of the Apache Public License
* Please see the LICENSE included with this distribution for details.
*/
package org.appcelerator.titanium.proxy;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.appcelerator.kroll.KrollDict;
import org.appcelerator.kroll.KrollProxy;
import org.appcelerator.kroll.annotations.Kroll;
import org.appcelerator.kroll.common.Log;
import org.appcelerator.titanium.TiApplication;
import org.appcelerator.titanium.TiBlob;
import org.appcelerator.titanium.TiC;
import org.appcelerator.titanium.util.TiConvert;
import android.graphics.Bitmap;
import android.content.ComponentName;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
@Kroll.proxy(propertyAccessors = {
TiC.PROPERTY_CLASS_NAME,
TiC.PROPERTY_PACKAGE_NAME,
TiC.PROPERTY_URL
})
/**
* This is a proxy representation of the Android Intent type.
* Refer to <a href="http://developer.android.com/reference/android/content/Intent.html">Android Intent</a>
* for more details.
*/
public class IntentProxy extends KrollProxy
{
private static final String TAG = "TiIntent";
public static final int TYPE_ACTIVITY = 0;
public static final int TYPE_SERVICE = 1;
public static final int TYPE_BROADCAST = 2;
protected Intent intent;
protected int type = TYPE_ACTIVITY;
public IntentProxy()
{
}
public IntentProxy(Intent intent)
{
this.intent = intent;
ComponentName componentName = intent.getComponent();
if (componentName != null) {
String packageName = componentName.getPackageName();
if (packageName != null)
setProperty(TiC.PROPERTY_PACKAGE_NAME, packageName);
String className = componentName.getClassName();
if (className != null)
setProperty(TiC.PROPERTY_CLASS_NAME, className);
}
// Other properties have dedicated getters.
}
protected static char[] escapeChars = new char[] {
'\\', '/', ' ', '.', '$', '&', '@'
};
protected static String getURLClassName(String url, int type)
{
switch (type) {
case TYPE_ACTIVITY: return getURLClassName(url, "Activity");
case TYPE_SERVICE: return getURLClassName(url, "Service");
case TYPE_BROADCAST: return getURLClassName(url, "Broadcast");
}
return null;
}
protected static String getURLClassName(String url, String appendage)
{
List<String> parts = Arrays.asList(url.split("/"));
if (parts.size() == 0) return null;
int start = 0;
if (parts.get(0).equals("app:") && parts.size() >= 3) {
start = 2;
}
String className = TextUtils.join("_", parts.subList(start, parts.size()));
if (className.endsWith(".js")) {
className = className.substring(0, className.length()-3);
}
if (className.length() > 1) {
className = className.substring(0, 1).toUpperCase() + className.substring(1);
} else {
className = className.toUpperCase();
}
for (char escapeChar : escapeChars) {
className = className.replace(escapeChar, '_');
}
return className+appendage;
}
public void handleCreationDict(KrollDict dict)
{
super.handleCreationDict(dict);
intent = new Intent();
// See which set of options we have to work with.
String action = dict.getString(TiC.PROPERTY_ACTION);
String url = dict.getString(TiC.PROPERTY_URL);
String data = dict.getString(TiC.PROPERTY_DATA);
String className = dict.getString(TiC.PROPERTY_CLASS_NAME);
String packageName = dict.getString(TiC.PROPERTY_PACKAGE_NAME);
String type = dict.getString(TiC.PROPERTY_TYPE);
int flags = 0;
if (dict.containsKey(TiC.PROPERTY_FLAGS)) {
flags = TiConvert.toInt(dict, TiC.PROPERTY_FLAGS);
Log.d(TAG, "Setting flags: " + Integer.toString(flags), Log.DEBUG_MODE);
intent.setFlags(flags);
} else {
setProperty(TiC.PROPERTY_FLAGS, intent.getFlags());
}
if (action != null) {
Log.d(TAG, "Setting action: " + action, Log.DEBUG_MODE);
intent.setAction(action);
}
if (packageName != null) {
Log.d(TAG, "Setting package: " + packageName, Log.DEBUG_MODE);
intent.setPackage(packageName);
}
if (url != null) {
Log.d(TAG, "Creating intent for JS Activity/Service @ " + url, Log.DEBUG_MODE);
packageName = TiApplication.getInstance().getPackageName();
className = packageName + "." + getURLClassName(url, this.type);
}
if (className != null) {
if (packageName != null) {
Log.d(TAG, "Both className and packageName set, using intent.setClassName(packageName, className",
Log.DEBUG_MODE);
intent.setClassName(packageName, className);
} else {
try {
Class<?> c = getClass().getClassLoader().loadClass(className);
intent.setClass(TiApplication.getInstance().getApplicationContext(), c);
} catch (ClassNotFoundException e) {
Log.e(TAG, "Unable to locate class for name: " + className);
throw new IllegalStateException("Missing class for name: " + className, e);
}
}
}
if (type == null) {
if (action != null && action.equals(Intent.ACTION_SEND)) {
type = "text/plain";
}
}
// setType and setData are inexplicably intertwined
// calling setType by itself clears the type and vice-versa
// if you have both you _must_ call setDataAndType
if (type != null) {
Log.d(TAG, "Setting type: " + type, Log.DEBUG_MODE);
if (data != null) {
intent.setDataAndType(Uri.parse(data), type);
} else {
intent.setType(type);
}
} else if (data != null) {
intent.setData(Uri.parse(data));
}
}
@Kroll.method
public void putExtra(String key, Object value)
{
if (value == null) {
return;
}
if (value instanceof String) {
intent.putExtra(key, (String) value);
} else if (value instanceof Boolean) {
intent.putExtra(key, (Boolean) value);
} else if (value instanceof Double) {
intent.putExtra(key, (Double) value);
} else if (value instanceof Integer) {
intent.putExtra(key, (Integer) value);
} else if (value instanceof Long) {
intent.putExtra(key, (Long) value);
} else if (value instanceof IntentProxy) {
intent.putExtra(key, (Intent) ((IntentProxy) value).getIntent());
} else if (value instanceof TiBlob) {
intent.putExtra(key, ((TiBlob) value).getImage());
} else if (value instanceof Object[]) {
try {
Object[] objVal = (Object[]) value;
String[] stringArray = Arrays.copyOf(objVal, objVal.length, String[].class);
intent.putExtra(key, stringArray);
} catch (Exception ex) {
Log.e(TAG, "Error unimplemented put conversion ", ex.getMessage());
}
} else {
Log.w(TAG, "Warning unimplemented put conversion for " + value.getClass().getCanonicalName() + " trying String");
intent.putExtra(key, TiConvert.toString(value));
}
}
@Kroll.method
public void addFlags(int flags)
{
intent.addFlags(flags);
}
@Kroll.setProperty @Kroll.method
public void setFlags(int flags)
{
intent.setFlags(flags);
}
@Kroll.getProperty @Kroll.method
public int getFlags()
{
return intent.getFlags();
}
@Kroll.method
public void putExtraUri(String key, Object value)
{
if (value == null) {
return;
}
if (value instanceof String) {
intent.putExtra(key, Uri.parse((String) value));
} else if (value instanceof Object[]) {
try {
Object[] objVal = (Object[]) value;
String[] stringArray = Arrays.copyOf(objVal, objVal.length, String[].class);
ArrayList<Uri> imageUris = new ArrayList<Uri>();
for(String s: stringArray) {
imageUris.add(Uri.parse(s));
}
intent.putParcelableArrayListExtra(key, imageUris);
} catch (Exception ex) {
Log.e(TAG, "Error unimplemented put conversion ", ex.getMessage());
}
}
}
@Kroll.method
public void addCategory(String category)
{
if (category != null) {
Log.d(TAG, "Adding category: " + category, Log.DEBUG_MODE);
intent.addCategory(category);
}
}
@Kroll.method
public String getStringExtra(String name)
{
if (!intent.hasExtra(name)) {
return null;
}
String result = intent.getStringExtra(name);
if (result == null) {
// One more try as parcelable extra, such as when it's a Uri.
// We can't really support getParcelableExtra(name) by itself,
// since the type of object coming out of it is unknown and
// might not make its way successfully over to Javascript.
// By getting it as a string, we at least allow people to grab
// Uris (Intent.STREAM) stored as parcelable extras, which is a
// very common use case.
Object parcelable = intent.getParcelableExtra(name);
if (parcelable != null) {
result = parcelable.toString();
}
}
return result;
}
@Kroll.method
public boolean getBooleanExtra(String name, boolean defaultValue)
{
return intent.getBooleanExtra(name, defaultValue);
}
@Kroll.method
public int getIntExtra(String name, int defaultValue)
{
return intent.getIntExtra(name, defaultValue);
}
@Kroll.method
public long getLongExtra(String name, long defaultValue)
{
return intent.getLongExtra(name, defaultValue);
}
@Kroll.method
public double getDoubleExtra(String name, double defaultValue)
{
return intent.getDoubleExtra(name, defaultValue);
}
@Kroll.method
public TiBlob getBlobExtra(String name)
{
InputStream is = null;
ByteArrayOutputStream bos = null;
try {
Object returnData = intent.getExtras().getParcelable(name);
if (returnData instanceof Uri) {
Uri uri = (Uri) returnData;
is = TiApplication.getInstance().getContentResolver().openInputStream(uri);
bos = new ByteArrayOutputStream();
int len;
int size = 4096;
byte[] buf = new byte[size];
while ((len = is.read(buf, 0, size)) != -1) {
bos.write(buf, 0, len);
}
buf = bos.toByteArray();
return TiBlob.blobFromData(buf);
} else if (returnData instanceof Bitmap) {
Bitmap returnBitmapData = (Bitmap) returnData;
return TiBlob.blobFromImage(returnBitmapData);
}
} catch (Exception e) {
Log.e(TAG, "Error getting blob extra: " + e.getMessage(), e);
return null;
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
Log.e(TAG, e.getMessage(), Log.DEBUG_MODE);
}
}
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
Log.e(TAG, e.getMessage(), Log.DEBUG_MODE);
}
}
}
return null;
}
@Kroll.method @Kroll.getProperty
public String getData()
{
return intent.getDataString();
}
/**
* @return the associated intent.
*/
public Intent getIntent()
{
return intent;
}
@Kroll.method @Kroll.getProperty
public String getType()
{
return intent.getType();
}
@Kroll.method @Kroll.setProperty
public void setType(String type)
{
intent.setType(type);
}
@Kroll.method @Kroll.getProperty
public String getAction()
{
return intent.getAction();
}
@Kroll.method @Kroll.setProperty
public void setAction(String action)
{
intent.setAction(action);
}
/**
* @return intent type for internal purposes (TYPE_ACTIVITY, etc.)
*/
public int getInternalType()
{
return type;
}
/**
* Sets the intent type.
* @param type the intent type for internal purposes (TYPE_ACTIVITY etc.)
*/
public void setInternalType(int type)
{
this.type = type;
}
@Kroll.method
public boolean hasExtra(String name)
{
if (intent != null) {
return intent.hasExtra(name);
}
return false;
}
@Override
public String getApiName()
{
return "Ti.Android.Intent";
}
}