com-easystep2-datawedge-plugin-intent-capacitor 4.4.0 → 4.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,14 @@
1
1
  <?xml version="1.0" encoding="utf-8"?>
2
2
  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3
3
  package="com.easystep2.datawedge.plugin.intent">
4
-
5
- <!-- No special permissions needed for basic intent operations -->
6
-
7
- </manifest>
4
+
5
+ <application>
6
+ <activity>
7
+ <intent-filter>
8
+ <action android:name="${applicationId}.ACTION" />
9
+ <category android:name="android.intent.category.DEFAULT" />
10
+ </intent-filter>
11
+ </activity>
12
+ </application>
13
+
14
+ </manifest>
@@ -2,23 +2,28 @@ package com.easystep2.datawedge.plugin.intent;
2
2
 
3
3
  import android.app.Activity;
4
4
  import android.content.BroadcastReceiver;
5
+ import android.content.ComponentName;
5
6
  import android.content.Context;
6
7
  import android.content.Intent;
7
8
  import android.content.IntentFilter;
8
9
  import android.content.pm.PackageManager;
9
- import android.net.Uri;
10
+ import android.os.Build;
10
11
  import android.os.Bundle;
12
+ import android.text.Html;
11
13
  import android.util.Log;
12
14
 
13
15
  import com.getcapacitor.JSObject;
14
16
  import com.getcapacitor.Plugin;
15
17
  import com.getcapacitor.PluginCall;
16
18
  import com.getcapacitor.PluginMethod;
19
+ import com.getcapacitor.annotation.ActivityCallback;
17
20
  import com.getcapacitor.annotation.CapacitorPlugin;
18
21
 
22
+ import org.json.JSONArray;
19
23
  import org.json.JSONException;
20
24
  import org.json.JSONObject;
21
25
 
26
+ import java.lang.reflect.Array;
22
27
  import java.util.ArrayList;
23
28
  import java.util.HashMap;
24
29
  import java.util.Iterator;
@@ -26,71 +31,342 @@ import java.util.Map;
26
31
 
27
32
  @CapacitorPlugin(name = "IntentShim")
28
33
  public class IntentShimPlugin extends Plugin {
29
- private static final String TAG = "IntentShim";
30
- private BroadcastReceiver broadcastReceiver = null;
31
- private boolean debugEnabled = false;
32
34
 
33
- @PluginMethod
34
- public void echo(PluginCall call) {
35
- String value = call.getString("value");
36
- JSObject ret = new JSObject();
37
- ret.put("value", value);
38
- call.resolve(ret);
39
- }
35
+ private static final String LOG_TAG = "IntentShimPlugin";
36
+ private final Map<BroadcastReceiver, PluginCall> receiverCalls = new HashMap<>();
37
+ private PluginCall onNewIntentCall = null;
38
+ private Intent deferredIntent = null;
40
39
 
41
- @PluginMethod
42
- public void registerBroadcastReceiver(PluginCall call) {
43
- // Implementation for registering a broadcast receiver
40
+ @Override
41
+ public void handleOnNewIntent(Intent intent) {
42
+ super.handleOnNewIntent(intent);
43
+ if (onNewIntentCall != null) {
44
+ fireOnNewIntent(intent);
45
+ } else {
46
+ this.deferredIntent = intent;
47
+ }
44
48
  }
45
49
 
46
- @PluginMethod
47
- public void unregisterBroadcastReceiver(PluginCall call) {
48
- // Implementation for unregistering a broadcast receiver
50
+ @PluginMethod(returnType = PluginMethod.RETURN_CALLBACK)
51
+ public void onIntent(PluginCall call) {
52
+ call.setKeepAlive(true);
53
+ this.onNewIntentCall = call;
54
+ if (this.deferredIntent != null) {
55
+ fireOnNewIntent(this.deferredIntent);
56
+ this.deferredIntent = null;
57
+ }
49
58
  }
50
59
 
51
60
  @PluginMethod
52
61
  public void sendBroadcast(PluginCall call) {
53
- // Implementation for sending a broadcast
62
+ try {
63
+ Intent intent = populateIntent(call.getData());
64
+ getContext().sendBroadcast(intent);
65
+ call.resolve();
66
+ } catch (JSONException e) {
67
+ call.reject("Error creating intent: " + e.getMessage());
68
+ }
54
69
  }
55
70
 
56
71
  @PluginMethod
57
72
  public void startActivity(PluginCall call) {
58
- // Implementation for starting an activity
73
+ try {
74
+ Intent intent = populateIntent(call.getData());
75
+ getContext().startActivity(intent);
76
+ call.resolve();
77
+ } catch (JSONException e) {
78
+ call.reject("Error creating intent: " + e.getMessage());
79
+ }
59
80
  }
60
81
 
61
82
  @PluginMethod
62
- public void getIntent(PluginCall call) {
63
- // Implementation for getting the current intent
83
+ public void startActivityForResult(PluginCall call) {
84
+ try {
85
+ Intent intent = populateIntent(call.getData());
86
+ int requestCode = call.getInt("requestCode", 1);
87
+ startActivityForResult(call, intent, "activityResult");
88
+ } catch (JSONException e) {
89
+ call.reject("Error creating intent: " + e.getMessage());
90
+ }
91
+ }
92
+
93
+ @ActivityCallback
94
+ private void activityResult(PluginCall call, ActivityResult result) {
95
+ if (call == null) {
96
+ return;
97
+ }
98
+ Intent intent = result.getData();
99
+ JSObject intentJson = getIntentJson(intent);
100
+ intentJson.put("requestCode", result.getRequestCode());
101
+ intentJson.put("resultCode", result.getResultCode());
102
+ call.resolve(intentJson);
103
+ }
104
+
105
+ @PluginMethod(returnType = PluginMethod.RETURN_CALLBACK)
106
+ public void registerBroadcastReceiver(PluginCall call) {
107
+ call.setKeepAlive(true);
108
+
109
+ JSObject filters = call.getData();
110
+ JSONArray filterActions = filters.getArray("filterActions");
111
+
112
+ if (filterActions == null || filterActions.length() == 0) {
113
+ call.reject("filterActions argument is required.");
114
+ return;
115
+ }
116
+
117
+ IntentFilter filter = new IntentFilter();
118
+ try {
119
+ for (int i = 0; i < filterActions.length(); i++) {
120
+ filter.addAction(filterActions.getString(i));
121
+ }
122
+
123
+ JSONArray filterCategories = filters.getArray("filterCategories");
124
+ if (filterCategories != null) {
125
+ for (int i = 0; i < filterCategories.length(); i++) {
126
+ filter.addCategory(filterCategories.getString(i));
127
+ }
128
+ }
129
+
130
+ JSONArray filterDataSchemes = filters.getArray("filterDataSchemes");
131
+ if (filterDataSchemes != null) {
132
+ for (int i = 0; i < filterDataSchemes.length(); i++) {
133
+ filter.addDataScheme(filterDataSchemes.getString(i));
134
+ }
135
+ }
136
+ } catch (JSONException e) {
137
+ call.reject("Error parsing filters: " + e.getMessage());
138
+ return;
139
+ }
140
+
141
+ BroadcastReceiver receiver = new BroadcastReceiver() {
142
+ @Override
143
+ public void onReceive(Context context, Intent intent) {
144
+ PluginCall savedCall = receiverCalls.get(this);
145
+ if (savedCall != null) {
146
+ savedCall.resolve(getIntentJson(intent));
147
+ }
148
+ }
149
+ };
150
+
151
+ getContext().registerReceiver(receiver, filter);
152
+ receiverCalls.put(receiver, call);
64
153
  }
65
154
 
66
155
  @PluginMethod
67
- public void startActivityForResult(PluginCall call) {
68
- // Implementation for starting an activity for result
156
+ public void unregisterBroadcastReceiver(PluginCall call) {
157
+ for (BroadcastReceiver receiver : receiverCalls.keySet()) {
158
+ try {
159
+ getContext().unregisterReceiver(receiver);
160
+ } catch (Exception e) {
161
+ Log.e(LOG_TAG, "Error unregistering broadcast receiver: " + e.getMessage());
162
+ }
163
+ }
164
+ receiverCalls.clear();
165
+ call.resolve();
69
166
  }
70
167
 
71
168
  @PluginMethod
72
- public void sendResult(PluginCall call) {
73
- // Implementation for sending a result back to the calling activity
169
+ public void getIntent(PluginCall call) {
170
+ Intent intent;
171
+ if (this.deferredIntent != null) {
172
+ intent = this.deferredIntent;
173
+ this.deferredIntent = null;
174
+ } else {
175
+ intent = getActivity().getIntent();
176
+ }
177
+ call.resolve(getIntentJson(intent));
74
178
  }
75
179
 
76
180
  @PluginMethod
77
- public void onIntent(PluginCall call) {
78
- // Implementation for registering an intent listener
181
+ public void sendResult(PluginCall call) {
182
+ Intent result = new Intent();
183
+ JSObject data = call.getData();
184
+ if (data != null && data.has("extras")) {
185
+ try {
186
+ result.putExtras(toBundle(data.getJSObject("extras")));
187
+ } catch (JSONException e) {
188
+ Log.e(LOG_TAG, "Error converting extras to bundle: " + e.getMessage());
189
+ }
190
+ }
191
+ getActivity().setResult(Activity.RESULT_OK, result);
192
+ getActivity().finish();
193
+ call.resolve();
79
194
  }
80
195
 
81
196
  @PluginMethod
82
197
  public void packageExists(PluginCall call) {
83
- // Implementation for checking if a package exists
198
+ String packageName = call.getString("package");
199
+ if (packageName == null) {
200
+ call.reject("package argument is required");
201
+ return;
202
+ }
203
+ try {
204
+ getContext().getPackageManager().getPackageInfo(packageName, 0);
205
+ call.resolve(new JSObject().put("exists", true));
206
+ } catch (PackageManager.NameNotFoundException e) {
207
+ call.resolve(new JSObject().put("exists", false));
208
+ }
84
209
  }
85
210
 
86
- @PluginMethod
87
- public void setDebugMode(PluginCall call) {
88
- // Implementation for enabling or disabling debug mode
211
+ private Intent populateIntent(JSObject obj) throws JSONException {
212
+ Intent intent = new Intent();
213
+
214
+ if (obj.has("action")) {
215
+ intent.setAction(obj.getString("action"));
216
+ }
217
+ if (obj.has("type")) {
218
+ intent.setType(obj.getString("type"));
219
+ }
220
+ if (obj.has("url")) {
221
+ intent.setData(Uri.parse(obj.getString("url")));
222
+ }
223
+ if (obj.has("package")) {
224
+ intent.setPackage(obj.getString("package"));
225
+ }
226
+
227
+ if (obj.has("component")) {
228
+ JSObject component = obj.getJSObject("component");
229
+ String pkg = component.getString("package");
230
+ String cls = component.getString("class");
231
+ if (pkg != null && cls != null) {
232
+ intent.setComponent(new ComponentName(pkg, cls));
233
+ }
234
+ }
235
+
236
+ if (obj.has("flags")) {
237
+ JSONArray flags = obj.getJSONArray("flags");
238
+ for (int i = 0; i < flags.length(); i++) {
239
+ intent.addFlags(flags.getInt(i));
240
+ }
241
+ }
242
+
243
+ if (obj.has("extras")) {
244
+ intent.putExtras(toBundle(obj.getJSObject("extras")));
245
+ }
246
+
247
+ return intent;
248
+ }
249
+
250
+ private void fireOnNewIntent(Intent intent) {
251
+ if (this.onNewIntentCall != null) {
252
+ JSObject intentJson = getIntentJson(intent);
253
+ this.onNewIntentCall.resolve(intentJson);
254
+ }
255
+ }
256
+
257
+ private JSObject getIntentJson(Intent intent) {
258
+ JSObject json = new JSObject();
259
+ if (intent == null) {
260
+ return json;
261
+ }
262
+ try {
263
+ json.put("action", intent.getAction());
264
+ json.put("data", intent.getDataString());
265
+ json.put("type", intent.getType());
266
+ json.put("package", intent.getPackage());
267
+ json.put("extras", toJsonObject(intent.getExtras()));
268
+ if (intent.getComponent() != null) {
269
+ json.put("component", intent.getComponent().flattenToString());
270
+ }
271
+ } catch (Exception e) {
272
+ Log.e(LOG_TAG, "Error converting intent to JSON: " + e.getMessage());
273
+ }
274
+ return json;
275
+ }
276
+
277
+ private static JSObject toJsonObject(Bundle bundle) {
278
+ JSObject json = new JSObject();
279
+ if (bundle == null) {
280
+ return json;
281
+ }
282
+ for (String key : bundle.keySet()) {
283
+ try {
284
+ json.put(key, toJsonValue(bundle.get(key)));
285
+ } catch (JSONException e) {
286
+ Log.e(LOG_TAG, "Cannot convert bundle to JSON: " + e.getMessage(), e);
287
+ }
288
+ }
289
+ return json;
290
+ }
291
+
292
+ private static Object toJsonValue(final Object value) throws JSONException {
293
+ if (value == null) {
294
+ return JSONObject.NULL;
295
+ } else if (value instanceof Bundle) {
296
+ return toJsonObject((Bundle) value);
297
+ } else if (value.getClass().isArray()) {
298
+ JSONArray result = new JSONArray();
299
+ int length = Array.getLength(value);
300
+ for (int i = 0; i < length; ++i) {
301
+ result.put(toJsonValue(Array.get(value, i)));
302
+ }
303
+ return result;
304
+ } else if (value instanceof ArrayList<?>) {
305
+ JSONArray result = new JSONArray();
306
+ for (Object item : (ArrayList<?>) value) {
307
+ result.put(toJsonValue(item));
308
+ }
309
+ return result;
310
+ } else if (value instanceof String ||
311
+ value instanceof Boolean ||
312
+ value instanceof Integer ||
313
+ value instanceof Long ||
314
+ value instanceof Double) {
315
+ return value;
316
+ } else {
317
+ return String.valueOf(value);
318
+ }
319
+ }
320
+
321
+ private Bundle toBundle(JSObject obj) throws JSONException {
322
+ Bundle bundle = new Bundle();
323
+ if (obj == null) {
324
+ return bundle;
325
+ }
326
+ Iterator<String> keys = obj.keys();
327
+ while (keys.hasNext()) {
328
+ String key = keys.next();
329
+ Object value = obj.get(key);
330
+
331
+ if (value instanceof String) {
332
+ bundle.putString(key, (String) value);
333
+ } else if (value instanceof Boolean) {
334
+ bundle.putBoolean(key, (Boolean) value);
335
+ } else if (value instanceof Integer) {
336
+ bundle.putInt(key, (Integer) value);
337
+ } else if (value instanceof Long) {
338
+ bundle.putLong(key, (Long) value);
339
+ } else if (value instanceof Double) {
340
+ bundle.putDouble(key, (Double) value);
341
+ } else if (value instanceof JSObject) {
342
+ bundle.putBundle(key, toBundle((JSObject) value));
343
+ } else if (value instanceof JSONArray) {
344
+ // This part is complex; simplified for common cases
345
+ // For DataWedge, string arrays or parcelable arrays are common
346
+ ArrayList list = toArrayList((JSONArray) value);
347
+ // Heuristic to determine array type
348
+ if (!list.isEmpty()) {
349
+ if (list.get(0) instanceof String) {
350
+ bundle.putStringArrayList(key, list);
351
+ } else if (list.get(0) instanceof Bundle) {
352
+ bundle.putParcelableArrayList(key, list);
353
+ }
354
+ }
355
+ }
356
+ }
357
+ return bundle;
89
358
  }
90
359
 
91
- private void log(String message) {
92
- if (debugEnabled) {
93
- Log.d(TAG, message);
360
+ private ArrayList toArrayList(JSONArray array) throws JSONException {
361
+ ArrayList list = new ArrayList();
362
+ for (int i = 0; i < array.length(); i++) {
363
+ Object value = array.get(i);
364
+ if (value instanceof JSONObject) {
365
+ list.add(toBundle(new JSObject(value.toString())));
366
+ } else {
367
+ list.add(value);
368
+ }
94
369
  }
370
+ return list;
95
371
  }
96
- }
372
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "com-easystep2-datawedge-plugin-intent-capacitor",
3
- "version": "4.4.0",
3
+ "version": "4.4.3",
4
4
  "description": "Capacitor plugin for Android Intents",
5
5
  "main": "plugin.js",
6
6
  "module": "esm/index.js",