-
Notifications
You must be signed in to change notification settings - Fork 120
/
startApp.java
410 lines (344 loc) · 11.2 KB
/
startApp.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
/**
com.lampa.startapp, ver. 6.1.6
https://github.com/lampaa/com.lampa.startapp
Phonegap plugin for check or launch other application in android device (iOS support).
bug tracker: https://github.com/lampaa/com.lampa.startapp/issues
*/
package com.lampa.startapp;
import org.apache.cordova.CallbackContext;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONException;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.ComponentName;
import android.content.IntentFilter;
import android.content.pm.PackageManager;
import android.content.pm.PackageInfo;
import java.util.HashMap;
import java.util.Iterator;
import android.net.Uri;
import android.os.Bundle;
public class startApp extends Assets {
private HashMap<Integer, BroadcastReceiver> broadcastReceiverHashMap = new HashMap<Integer, BroadcastReceiver>();
private CallbackContext callbackContext;
/**
* Executes the request and returns PluginResult.
*
* @param action The action to execute.
* @param args JSONArray of arguments for the plugin.
* @param callbackContext The callback context used when calling back into JavaScript.
* @return Always return true.
*/
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
if(action.equals("start")) {
this.start(args, callbackContext);
}
else if(action.equals("check")) {
this.check(args, callbackContext);
}
else if(action.equals("receiver")) {
this.receiver(args, callbackContext);
}
else if(action.equals("unReceiver")) {
this.receiver(args, callbackContext);
}
else if(action.equals("getExtras")) {
this.getExtras(callbackContext);
}
else if(action.equals("getExtra")) {
this.getExtra(args, callbackContext);
}
return true;
}
/**
*
* @param args
* @param callback
*/
private void receiver(JSONArray args, final CallbackContext callback) {
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
JSONObject result = new JSONObject();
try {
result.put("_ACTION_VALUE_FORMAT_", intent.getAction());
Bundle bundle = intent.getExtras();
if (bundle != null) {
for (String key : bundle.keySet()) {
result.put(key, bundle.get(key));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, result);
pluginResult.setKeepCallback(true);
callback.sendPluginResult(pluginResult);
}
};
try {
JSONArray values = args.getJSONArray(0);
IntentFilter filter = new IntentFilter();
for(int i=0; i < values.length(); i++) {
filter.addAction(values.getString(i));
}
//cordova.getActivity().getApplicationContext()
cordova.getActivity().getApplicationContext().registerReceiver(receiver, filter);
broadcastReceiverHashMap.put(receiver.hashCode(), receiver);
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, receiver.hashCode());
pluginResult.setKeepCallback(true);
callback.sendPluginResult(pluginResult);
}
catch (Exception ex) {
ex.printStackTrace();
callback.error("Error register receiver: " + ex.getMessage());
}
}
/**
* startApp
*/
public void start(JSONArray args, CallbackContext callback) {
Intent LaunchIntent;
JSONObject params;
JSONArray flags;
JSONArray component;
JSONObject extra;
String key;
int i;
try {
if (args.get(0) instanceof JSONObject) {
params = args.getJSONObject(0);
/**
* disable parsing intent values
*/
if(params.has("noParse")) {
NO_PARSE_INTENT_VALS = true;
}
/**
* set application
* http://developer.android.com/reference/android/content/Intent.html(java.lang.String)
*/
if(params.has("application")) {
PackageManager manager = cordova.getActivity().getApplicationContext().getPackageManager();
LaunchIntent = manager.getLaunchIntentForPackage(params.getString("application"));
if (LaunchIntent == null) {
callback.error("Application \""+ params.getString("application") +"\" not found!");
return;
}
}
/**
* set intent
* http://developer.android.com/reference/android/content/Intent.html (java.lang.String)
*/
else {
LaunchIntent = new Intent();
if(params.has("intent")) {
ComponentName ci = new ComponentName(cordova.getActivity().getPackageName(), params.getString("intent"));
LaunchIntent.setComponent(ci);
}
}
/**
* set package
* http://developer.android.com/reference/android/content/Intent.html#setPackage(java.lang.String)
*/
if(params.has("package")) {
LaunchIntent.setPackage(params.getString("package"));
}
/**
* set action
* http://developer.android.com/intl/ru/reference/android/content/Intent.html#setAction%28java.lang.String%29
*/
if(params.has("action")) {
LaunchIntent.setAction(getIntentValueString(params.getString("action")));
}
/**
* set category
* http://developer.android.com/intl/ru/reference/android/content/Intent.html#addCategory%28java.lang.String%29
*/
if(params.has("category")) {
LaunchIntent.addCategory(getIntentValueString(params.getString("category")));
}
/**
* set type
* http://developer.android.com/intl/ru/reference/android/content/Intent.html#setType%28java.lang.String%29
*/
if(params.has("type")) {
LaunchIntent.setType(params.getString("type"));
}
/**
* set data (uri)
* http://developer.android.com/intl/ru/reference/android/content/Intent.html#setData%28android.net.Uri%29
*/
if(params.has("uri")) {
LaunchIntent.setData(Uri.parse(params.getString("uri")));
}
/**
* set flags
* http://developer.android.com/intl/ru/reference/android/content/Intent.html#addFlags%28int%29
*/
if(params.has("flags")) {
flags = params.getJSONArray("flags");
for(i=0; i < flags.length(); i++) {
LaunchIntent.addFlags(getIntentValue(flags.getString(i)));
}
}
/**
* set component
* http://developer.android.com/intl/ru/reference/android/content/Intent.html#setComponent%28android.content.ComponentName%29
*/
if(params.has("component")) {
component = params.getJSONArray("component");
if(component.length() == 2) {
LaunchIntent.setComponent(new ComponentName(component.getString(0), component.getString(1)));
}
}
/**
* set extra fields
*/
if(!args.isNull(1)) {
extra = args.getJSONObject(1);
Iterator<String> iter = extra.keys();
while (iter.hasNext()) {
key = iter.next();
Object value = extra.get(key);
if(value instanceof Integer) {
LaunchIntent.putExtra(parseExtraName(key), extra.getInt(key));
}
if(params.has("matchDoubleInString") && matchDoubleInString(extra.getString(key))) {
LaunchIntent.putExtra(parseExtraName(key), extra.getDouble(key));
} else if(value instanceof String) {
LaunchIntent.putExtra(parseExtraName(key), extra.getString(key));
}
if(value instanceof Boolean) {
LaunchIntent.putExtra(parseExtraName(key), extra.getBoolean(key));
}
}
}
/**
* launch intent
*/
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK);
pluginResult.setKeepCallback(true);
if(params.has("intentstart") && "startActivityForResult".equals(params.getString("intentstart"))) {
cordova.setActivityResultCallback (this);
callbackContext = callback;
cordova.getActivity().startActivityForResult(LaunchIntent, 1);
}
else if(params.has("intentstart") && "sendBroadcast".equals(params.getString("intentstart"))) {
cordova.getActivity().sendBroadcast(LaunchIntent);
}
else {
cordova.getActivity().startActivity(LaunchIntent);
}
callback.sendPluginResult(pluginResult);
}
else {
callback.error("Incorrect params, array is not array object!");
}
}
catch (Exception e) {
e.printStackTrace();
callback.error(e.getClass() + ": " + e.getMessage());
}
}
/**
* checkApp
*/
private void check(JSONArray args, CallbackContext callback) {
JSONObject params;
try {
if (args.get(0) instanceof JSONObject) {
params = args.getJSONObject(0);
if(params.has("package")) {
PackageManager pm = cordova.getActivity().getApplicationContext().getPackageManager();
// get package info
final PackageInfo PackInfo = pm.getPackageInfo(params.getString("package"), PackageManager.GET_ACTIVITIES);
// create json object
JSONObject info = new JSONObject() {{
put("versionName", PackInfo.versionName);
put("packageName", PackInfo.packageName);
put("versionCode", PackInfo.versionCode);
put("applicationInfo", PackInfo.applicationInfo);
}};
callback.success(info);
}
else {
callback.error("Value \"package\" in null!");
}
}
else {
callback.error("Incorrect params, array is not array object!");
}
}
catch (Exception e) {
callback.error(e.getClass() + ": " + e.getMessage());
e.printStackTrace();
}
}
/**
* getExtras
*/
private void getExtras(CallbackContext callback) {
try {
Bundle extras = cordova.getActivity().getIntent().getExtras();
JSONObject info = new JSONObject();
if (extras != null) {
for (String key : extras.keySet()) {
info.put(key, extras.get(key).toString());
}
}
callback.success(info);
}
catch(JSONException e) {
callback.error(e.getMessage());
e.printStackTrace();
}
}
/**
* getExtra
*/
private void getExtra(JSONArray args, CallbackContext callback) {
try {
String extraName = parseExtraName(args.getString(0));
Intent extraIntent = cordova.getActivity().getIntent();
if(extraIntent.hasExtra(extraName)) {
String extraValue = extraIntent.getStringExtra(extraName);
if (extraValue == null) {
extraValue = (extraIntent.getParcelableExtra(extraName)).toString();
}
callback.success(extraValue);
}
else {
callback.error("extra field not found");
}
}
catch(JSONException e) {
callback.error(e.getMessage());
e.printStackTrace();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(callbackContext != null) {
JSONObject result = new JSONObject();
try {
result.put("_ACTION_requestCode_", requestCode);
result.put("_ACTION_resultCode_", resultCode);
Bundle bundle = intent == null ? null : intent.getExtras();
if (bundle != null) {
for (String key : bundle.keySet()) {
result.put(key, bundle.get(key));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, result);
pluginResult.setKeepCallback(true);
callbackContext.sendPluginResult(pluginResult);
}
}
}