@simplysm/capacitor-plugin-intent 12.16.41

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.
package/README.md ADDED
@@ -0,0 +1,188 @@
1
+ # @simplysm/capacitor-plugin-intent
2
+
3
+ Capacitor Intent Plugin -- Android intent send/receive for industrial devices (barcode scanners, PDA, etc.). Provides subscription-based intent receiving, intent sending with extras, launch intent retrieval, and startActivityForResult support.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @simplysm/capacitor-plugin-intent
9
+ ```
10
+
11
+ ## API Overview
12
+
13
+ | API | Type | Description |
14
+ |-----|------|-------------|
15
+ | `Intent` | Abstract class | Static methods for subscribing to, sending, and managing Android intents |
16
+ | `IIntentResult` | Interface | Intent result containing action and extras |
17
+ | `IStartActivityForResultOptions` | Interface | Options for starting an activity for result |
18
+ | `IActivityResult` | Interface | Activity result containing resultCode, data, and extras |
19
+ | `IIntentPlugin` | Interface | Low-level Capacitor plugin interface for intent operations |
20
+
21
+ ## API Reference
22
+
23
+ ### `IIntentResult`
24
+
25
+ Result object received from an intent or launch intent.
26
+
27
+ ```typescript
28
+ export interface IIntentResult {
29
+ action?: string;
30
+ extras?: Record<string, unknown>;
31
+ }
32
+ ```
33
+
34
+ | Field | Type | Description |
35
+ |-------|------|-------------|
36
+ | `action` | `string \| undefined` | The intent action string |
37
+ | `extras` | `Record<string, unknown> \| undefined` | Extra data attached to the intent |
38
+
39
+ ### `IStartActivityForResultOptions`
40
+
41
+ Options for starting an activity and receiving its result.
42
+
43
+ ```typescript
44
+ export interface IStartActivityForResultOptions {
45
+ action: string;
46
+ uri?: string;
47
+ extras?: Record<string, unknown>;
48
+ package?: string;
49
+ component?: string;
50
+ type?: string;
51
+ }
52
+ ```
53
+
54
+ | Field | Type | Description |
55
+ |-------|------|-------------|
56
+ | `action` | `string` | Intent action (required) |
57
+ | `uri` | `string \| undefined` | Data URI |
58
+ | `extras` | `Record<string, unknown> \| undefined` | Extra data to include in the intent |
59
+ | `package` | `string \| undefined` | Target package name |
60
+ | `component` | `string \| undefined` | Target component class name (requires `package`) |
61
+ | `type` | `string \| undefined` | MIME type |
62
+
63
+ ### `IActivityResult`
64
+
65
+ Result returned from a started activity.
66
+
67
+ ```typescript
68
+ export interface IActivityResult {
69
+ resultCode: number;
70
+ data?: string;
71
+ extras?: Record<string, unknown>;
72
+ }
73
+ ```
74
+
75
+ | Field | Type | Description |
76
+ |-------|------|-------------|
77
+ | `resultCode` | `number` | Activity result code (`RESULT_OK = -1`, `RESULT_CANCELED = 0`) |
78
+ | `data` | `string \| undefined` | Result data URI |
79
+ | `extras` | `Record<string, unknown> \| undefined` | Result extras |
80
+
81
+ ### `IIntentPlugin`
82
+
83
+ Low-level Capacitor plugin interface. Use `Intent` instead for a simplified API.
84
+
85
+ ```typescript
86
+ export interface IIntentPlugin {
87
+ subscribe(
88
+ options: { filters: string[] },
89
+ callback: (result: IIntentResult) => void,
90
+ ): Promise<{ id: string }>;
91
+ unsubscribe(options: { id: string }): Promise<void>;
92
+ unsubscribeAll(): Promise<void>;
93
+ send(options: { action: string; extras?: Record<string, unknown> }): Promise<void>;
94
+ getLaunchIntent(): Promise<IIntentResult>;
95
+ startActivityForResult(options: IStartActivityForResultOptions): Promise<IActivityResult>;
96
+ }
97
+ ```
98
+
99
+ | Method | Parameters | Returns | Description |
100
+ |--------|------------|---------|-------------|
101
+ | `subscribe` | `options: { filters: string[] }, callback` | `Promise<{ id: string }>` | Register an intent receiver with action filters |
102
+ | `unsubscribe` | `options: { id: string }` | `Promise<void>` | Remove a specific intent receiver by ID |
103
+ | `unsubscribeAll` | -- | `Promise<void>` | Remove all intent receivers |
104
+ | `send` | `options: { action, extras? }` | `Promise<void>` | Send an intent with action and optional extras |
105
+ | `getLaunchIntent` | -- | `Promise<IIntentResult>` | Get the intent that launched the app |
106
+ | `startActivityForResult` | `options: IStartActivityForResultOptions` | `Promise<IActivityResult>` | Start an activity and receive its result |
107
+
108
+ ### `Intent`
109
+
110
+ Abstract class with static methods for Android intent send/receive. Designed for industrial device integration (barcode scanners, PDA, etc.).
111
+
112
+ ```typescript
113
+ export abstract class Intent {
114
+ static async subscribe(
115
+ filters: string[],
116
+ callback: (result: IIntentResult) => void,
117
+ ): Promise<() => Promise<void>>;
118
+
119
+ static async unsubscribeAll(): Promise<void>;
120
+
121
+ static async send(options: {
122
+ action: string;
123
+ extras?: Record<string, unknown>;
124
+ }): Promise<void>;
125
+
126
+ static async getLaunchIntent(): Promise<IIntentResult>;
127
+
128
+ static async startActivityForResult(
129
+ options: IStartActivityForResultOptions,
130
+ ): Promise<IActivityResult>;
131
+ }
132
+ ```
133
+
134
+ | Method | Parameters | Returns | Description |
135
+ |--------|------------|---------|-------------|
136
+ | `subscribe` | `filters: string[], callback: (result: IIntentResult) => void` | `Promise<() => Promise<void>>` | Register an intent receiver; returns an unsubscribe function |
137
+ | `unsubscribeAll` | -- | `Promise<void>` | Remove all registered intent receivers |
138
+ | `send` | `options: { action: string; extras?: Record<string, unknown> }` | `Promise<void>` | Send an intent with action and optional extras |
139
+ | `getLaunchIntent` | -- | `Promise<IIntentResult>` | Retrieve the intent that launched the application |
140
+ | `startActivityForResult` | `options: IStartActivityForResultOptions` | `Promise<IActivityResult>` | Start an activity and receive its result |
141
+
142
+ ## Usage Examples
143
+
144
+ ### Subscribe to intents from a barcode scanner
145
+
146
+ ```typescript
147
+ import { Intent } from "@simplysm/capacitor-plugin-intent";
148
+
149
+ const unsubscribe = await Intent.subscribe(
150
+ ["com.symbol.datawedge.api.RESULT_ACTION"],
151
+ (result) => {
152
+ console.log("Action:", result.action);
153
+ console.log("Extras:", result.extras);
154
+ },
155
+ );
156
+
157
+ // Later, unsubscribe
158
+ await unsubscribe();
159
+ ```
160
+
161
+ ### Send an intent to trigger a scan
162
+
163
+ ```typescript
164
+ import { Intent } from "@simplysm/capacitor-plugin-intent";
165
+
166
+ await Intent.send({
167
+ action: "com.symbol.datawedge.api.ACTION",
168
+ extras: {
169
+ "com.symbol.datawedge.api.SOFT_SCAN_TRIGGER": "TOGGLE_SCANNING",
170
+ },
171
+ });
172
+ ```
173
+
174
+ ### Start an activity for result
175
+
176
+ ```typescript
177
+ import { Intent } from "@simplysm/capacitor-plugin-intent";
178
+
179
+ const result = await Intent.startActivityForResult({
180
+ action: "android.intent.action.GET_CONTENT",
181
+ type: "image/*",
182
+ });
183
+
184
+ if (result.resultCode === -1) {
185
+ // RESULT_OK
186
+ console.log("Selected:", result.data);
187
+ }
188
+ ```
@@ -0,0 +1,14 @@
1
+ apply plugin: 'com.android.library'
2
+
3
+ android {
4
+ namespace "kr.co.simplysm.capacitor.intent"
5
+ compileSdk project.hasProperty('compileSdkVersion') ? rootProject.ext.compileSdkVersion : 35
6
+ defaultConfig {
7
+ minSdk project.hasProperty('minSdkVersion') ? rootProject.ext.minSdkVersion : 23
8
+ }
9
+ }
10
+
11
+ dependencies {
12
+ implementation project(':capacitor-android')
13
+ implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion"
14
+ }
@@ -0,0 +1,3 @@
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
3
+ </manifest>
@@ -0,0 +1,376 @@
1
+ package kr.co.simplysm.capacitor.intent;
2
+
3
+ import android.content.BroadcastReceiver;
4
+ import android.content.ComponentName;
5
+ import android.content.Context;
6
+ import android.content.Intent;
7
+ import android.content.IntentFilter;
8
+ import android.net.Uri;
9
+ import android.os.Build;
10
+ import android.os.Bundle;
11
+ import android.os.Parcelable;
12
+ import android.util.Log;
13
+
14
+ import androidx.activity.result.ActivityResult;
15
+
16
+ import com.getcapacitor.JSArray;
17
+ import com.getcapacitor.JSObject;
18
+ import com.getcapacitor.Plugin;
19
+ import com.getcapacitor.PluginCall;
20
+ import com.getcapacitor.PluginMethod;
21
+ import com.getcapacitor.annotation.ActivityCallback;
22
+ import com.getcapacitor.annotation.CapacitorPlugin;
23
+
24
+ import org.json.JSONArray;
25
+ import org.json.JSONException;
26
+ import org.json.JSONObject;
27
+
28
+ import java.util.HashMap;
29
+ import java.util.Iterator;
30
+ import java.util.Map;
31
+ import java.util.Set;
32
+ import java.util.UUID;
33
+
34
+ @CapacitorPlugin(name = "Intent")
35
+ public class IntentPlugin extends Plugin {
36
+
37
+ private static final String TAG = "IntentPlugin";
38
+ private final Map<String, BroadcastReceiver> receivers = new HashMap<>();
39
+
40
+ @Override
41
+ protected void handleOnNewIntent(Intent intent) {
42
+ super.handleOnNewIntent(intent);
43
+ notifyListeners("onNewIntent", intentToJson(intent));
44
+ }
45
+
46
+ @PluginMethod(returnType = PluginMethod.RETURN_CALLBACK)
47
+ public void subscribe(PluginCall call) {
48
+ try {
49
+ JSArray filters = call.getArray("filters");
50
+
51
+ if (filters == null || filters.length() == 0) {
52
+ call.reject("filters is required");
53
+ return;
54
+ }
55
+
56
+ call.setKeepAlive(true);
57
+
58
+ String receiverId = UUID.randomUUID().toString();
59
+
60
+ IntentFilter intentFilter = new IntentFilter();
61
+ for (int i = 0; i < filters.length(); i++) {
62
+ intentFilter.addAction(filters.getString(i));
63
+ }
64
+
65
+ BroadcastReceiver receiver = new BroadcastReceiver() {
66
+ @Override
67
+ public void onReceive(Context context, Intent intent) {
68
+ JSObject result = intentToJson(intent);
69
+ result.put("id", receiverId);
70
+ call.resolve(result);
71
+ }
72
+ };
73
+
74
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
75
+ getContext().registerReceiver(receiver, intentFilter, Context.RECEIVER_EXPORTED);
76
+ } else {
77
+ getContext().registerReceiver(receiver, intentFilter);
78
+ }
79
+
80
+ receivers.put(receiverId, receiver);
81
+
82
+ JSObject ret = new JSObject();
83
+ ret.put("id", receiverId);
84
+ call.resolve(ret);
85
+ } catch (Exception e) {
86
+ Log.e(TAG, "subscribe failed", e);
87
+ call.reject("subscribe failed: " + e.getMessage());
88
+ }
89
+ }
90
+
91
+ @PluginMethod
92
+ public void unsubscribe(PluginCall call) {
93
+ try {
94
+ String id = call.getString("id");
95
+ if (id == null) {
96
+ call.reject("id is required");
97
+ return;
98
+ }
99
+
100
+ BroadcastReceiver receiver = receivers.remove(id);
101
+ if (receiver != null) {
102
+ getContext().unregisterReceiver(receiver);
103
+ }
104
+
105
+ call.resolve();
106
+ } catch (Exception e) {
107
+ Log.e(TAG, "unsubscribe failed", e);
108
+ call.reject("unsubscribe failed: " + e.getMessage());
109
+ }
110
+ }
111
+
112
+ @PluginMethod
113
+ public void unsubscribeAll(PluginCall call) {
114
+ try {
115
+ for (BroadcastReceiver receiver : receivers.values()) {
116
+ try {
117
+ getContext().unregisterReceiver(receiver);
118
+ } catch (Exception ignored) {
119
+ }
120
+ }
121
+ receivers.clear();
122
+ call.resolve();
123
+ } catch (Exception e) {
124
+ Log.e(TAG, "unsubscribeAll failed", e);
125
+ call.reject("unsubscribeAll failed: " + e.getMessage());
126
+ }
127
+ }
128
+
129
+ @PluginMethod
130
+ public void send(PluginCall call) {
131
+ try {
132
+ String action = call.getString("action");
133
+ if (action == null) {
134
+ call.reject("action is required");
135
+ return;
136
+ }
137
+
138
+ Intent intent = new Intent(action);
139
+
140
+ JSObject extras = call.getObject("extras");
141
+ if (extras != null) {
142
+ populateExtras(intent, extras);
143
+ }
144
+
145
+ getContext().sendBroadcast(intent);
146
+ call.resolve();
147
+ } catch (Exception e) {
148
+ Log.e(TAG, "send failed", e);
149
+ call.reject("send failed: " + e.getMessage());
150
+ }
151
+ }
152
+
153
+ @PluginMethod
154
+ public void getLaunchIntent(PluginCall call) {
155
+ try {
156
+ Intent intent = getActivity().getIntent();
157
+ call.resolve(intentToJson(intent));
158
+ } catch (Exception e) {
159
+ Log.e(TAG, "getLaunchIntent failed", e);
160
+ call.reject("getLaunchIntent failed: " + e.getMessage());
161
+ }
162
+ }
163
+
164
+ @PluginMethod
165
+ public void startActivityForResult(PluginCall call) {
166
+ try {
167
+ String action = call.getString("action");
168
+ if (action == null) {
169
+ call.reject("action is required");
170
+ return;
171
+ }
172
+
173
+ Intent intent = new Intent(action);
174
+
175
+ String uri = call.getString("uri");
176
+ String type = call.getString("type");
177
+ if (uri != null && type != null) {
178
+ intent.setDataAndType(Uri.parse(uri), type);
179
+ } else if (uri != null) {
180
+ intent.setData(Uri.parse(uri));
181
+ } else if (type != null) {
182
+ intent.setType(type);
183
+ }
184
+
185
+ String pkg = call.getString("package");
186
+ String component = call.getString("component");
187
+ if (component != null && pkg == null) {
188
+ call.reject("component requires package");
189
+ return;
190
+ }
191
+ if (pkg != null && component != null) {
192
+ intent.setComponent(new ComponentName(pkg, component));
193
+ } else if (pkg != null) {
194
+ intent.setPackage(pkg);
195
+ }
196
+
197
+ JSObject extras = call.getObject("extras");
198
+ if (extras != null) {
199
+ populateExtras(intent, extras);
200
+ }
201
+
202
+ startActivityForResult(call, intent, "onActivityResult");
203
+ } catch (Exception e) {
204
+ Log.e(TAG, "startActivityForResult failed", e);
205
+ call.reject("startActivityForResult failed: " + e.getMessage());
206
+ }
207
+ }
208
+
209
+ @ActivityCallback
210
+ private void onActivityResult(PluginCall call, ActivityResult result) {
211
+ if (call == null) {
212
+ return;
213
+ }
214
+
215
+ JSObject ret = new JSObject();
216
+ ret.put("resultCode", result.getResultCode());
217
+
218
+ Intent data = result.getData();
219
+ if (data != null) {
220
+ String dataString = data.getDataString();
221
+ if (dataString != null) {
222
+ ret.put("data", dataString);
223
+ }
224
+
225
+ Bundle extras = data.getExtras();
226
+ if (extras != null && !extras.isEmpty()) {
227
+ ret.put("extras", bundleToJson(extras));
228
+ }
229
+ }
230
+
231
+ call.resolve(ret);
232
+ }
233
+
234
+ private void populateExtras(Intent intent, JSObject extras) throws JSONException {
235
+ Iterator<String> keys = extras.keys();
236
+ while (keys.hasNext()) {
237
+ String key = keys.next();
238
+ Object value = extras.get(key);
239
+
240
+ if (value instanceof String) {
241
+ intent.putExtra(key, (String) value);
242
+ } else if (value instanceof Integer) {
243
+ intent.putExtra(key, (Integer) value);
244
+ } else if (value instanceof Long) {
245
+ intent.putExtra(key, (Long) value);
246
+ } else if (value instanceof Double) {
247
+ intent.putExtra(key, (Double) value);
248
+ } else if (value instanceof Boolean) {
249
+ intent.putExtra(key, (Boolean) value);
250
+ } else if (value instanceof JSONArray) {
251
+ JSONArray arr = (JSONArray) value;
252
+ String[] strArr = new String[arr.length()];
253
+ for (int i = 0; i < arr.length(); i++) {
254
+ strArr[i] = arr.getString(i);
255
+ }
256
+ intent.putExtra(key, strArr);
257
+ } else if (value instanceof JSONObject) {
258
+ Bundle bundle = jsonToBundle((JSONObject) value);
259
+ intent.putExtra(key, bundle);
260
+ }
261
+ }
262
+ }
263
+
264
+ private Bundle jsonToBundle(JSONObject json) throws JSONException {
265
+ Bundle bundle = new Bundle();
266
+ Iterator<String> keys = json.keys();
267
+ while (keys.hasNext()) {
268
+ String key = keys.next();
269
+ Object value = json.get(key);
270
+
271
+ if (value instanceof String) {
272
+ bundle.putString(key, (String) value);
273
+ } else if (value instanceof Integer) {
274
+ bundle.putInt(key, (Integer) value);
275
+ } else if (value instanceof Long) {
276
+ bundle.putLong(key, (Long) value);
277
+ } else if (value instanceof Double) {
278
+ bundle.putDouble(key, (Double) value);
279
+ } else if (value instanceof Boolean) {
280
+ bundle.putBoolean(key, (Boolean) value);
281
+ }
282
+ }
283
+ return bundle;
284
+ }
285
+
286
+ private JSObject intentToJson(Intent intent) {
287
+ JSObject json = new JSObject();
288
+
289
+ if (intent == null) {
290
+ return json;
291
+ }
292
+
293
+ try {
294
+ json.put("action", intent.getAction());
295
+
296
+ Bundle extras = intent.getExtras();
297
+ if (extras != null) {
298
+ json.put("extras", bundleToJson(extras));
299
+ }
300
+ } catch (Exception e) {
301
+ Log.e(TAG, "intentToJson failed", e);
302
+ }
303
+
304
+ return json;
305
+ }
306
+
307
+ private JSObject bundleToJson(Bundle bundle) {
308
+ JSObject json = new JSObject();
309
+
310
+ if (bundle == null) {
311
+ return json;
312
+ }
313
+
314
+ for (String key : bundle.keySet()) {
315
+ try {
316
+ Object value = bundle.get(key);
317
+
318
+ if (value == null) {
319
+ json.put(key, JSONObject.NULL);
320
+ } else if (value instanceof String) {
321
+ json.put(key, value);
322
+ } else if (value instanceof Integer) {
323
+ json.put(key, value);
324
+ } else if (value instanceof Long) {
325
+ json.put(key, value);
326
+ } else if (value instanceof Double) {
327
+ json.put(key, value);
328
+ } else if (value instanceof Float) {
329
+ json.put(key, ((Float) value).doubleValue());
330
+ } else if (value instanceof Boolean) {
331
+ json.put(key, value);
332
+ } else if (value instanceof Bundle) {
333
+ json.put(key, bundleToJson((Bundle) value));
334
+ } else if (value instanceof String[]) {
335
+ JSArray arr = new JSArray();
336
+ for (String s : (String[]) value) {
337
+ arr.put(s);
338
+ }
339
+ json.put(key, arr);
340
+ } else if (value instanceof int[]) {
341
+ JSArray arr = new JSArray();
342
+ for (int i : (int[]) value) {
343
+ arr.put(i);
344
+ }
345
+ json.put(key, arr);
346
+ } else if (value instanceof Parcelable) {
347
+ json.put(key, value.toString());
348
+ } else if (value instanceof Parcelable[]) {
349
+ JSArray arr = new JSArray();
350
+ for (Parcelable p : (Parcelable[]) value) {
351
+ arr.put(p.toString());
352
+ }
353
+ json.put(key, arr);
354
+ } else {
355
+ json.put(key, value.toString());
356
+ }
357
+ } catch (Exception e) {
358
+ Log.w(TAG, "bundleToJson key failed: " + key, e);
359
+ }
360
+ }
361
+
362
+ return json;
363
+ }
364
+
365
+ @Override
366
+ protected void handleOnDestroy() {
367
+ for (BroadcastReceiver receiver : receivers.values()) {
368
+ try {
369
+ getContext().unregisterReceiver(receiver);
370
+ } catch (Exception ignored) {
371
+ }
372
+ }
373
+ receivers.clear();
374
+ super.handleOnDestroy();
375
+ }
376
+ }
@@ -0,0 +1,63 @@
1
+ export interface IIntentResult {
2
+ /** Intent action */
3
+ action?: string;
4
+ /** Extra data */
5
+ extras?: Record<string, unknown>;
6
+ }
7
+ export interface IStartActivityForResultOptions {
8
+ /** Intent action */
9
+ action: string;
10
+ /** Data URI */
11
+ uri?: string;
12
+ /** Extra data */
13
+ extras?: Record<string, unknown>;
14
+ /** Target package name */
15
+ package?: string;
16
+ /** Target component class name */
17
+ component?: string;
18
+ /** MIME type */
19
+ type?: string;
20
+ }
21
+ export interface IActivityResult {
22
+ /** Activity result code (RESULT_OK = -1, RESULT_CANCELED = 0) */
23
+ resultCode: number;
24
+ /** Result data URI */
25
+ data?: string;
26
+ /** Result extras */
27
+ extras?: Record<string, unknown>;
28
+ }
29
+ export interface IIntentPlugin {
30
+ /**
31
+ * Intent 수신기 등록
32
+ */
33
+ subscribe(options: {
34
+ filters: string[];
35
+ }, callback: (result: IIntentResult) => void): Promise<{
36
+ id: string;
37
+ }>;
38
+ /**
39
+ * 특정 Intent 수신기 해제
40
+ */
41
+ unsubscribe(options: {
42
+ id: string;
43
+ }): Promise<void>;
44
+ /**
45
+ * 모든 Intent 수신기 해제
46
+ */
47
+ unsubscribeAll(): Promise<void>;
48
+ /**
49
+ * Intent 전송
50
+ */
51
+ send(options: {
52
+ action: string;
53
+ extras?: Record<string, unknown>;
54
+ }): Promise<void>;
55
+ /**
56
+ * 앱 시작 Intent 가져오기
57
+ */
58
+ getLaunchIntent(): Promise<IIntentResult>;
59
+ /**
60
+ * Activity 시작 후 결과 수신
61
+ */
62
+ startActivityForResult(options: IStartActivityForResultOptions): Promise<IActivityResult>;
63
+ }
@@ -0,0 +1 @@
1
+ export {};