@capgo/capacitor-intent-launcher 0.0.1

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.
@@ -0,0 +1,284 @@
1
+ package app.capgo.capacitor.intentlauncher;
2
+
3
+ import android.content.ComponentName;
4
+ import android.content.Intent;
5
+ import android.content.pm.PackageManager;
6
+ import android.graphics.Bitmap;
7
+ import android.graphics.Canvas;
8
+ import android.graphics.drawable.BitmapDrawable;
9
+ import android.graphics.drawable.Drawable;
10
+ import android.net.Uri;
11
+ import android.os.Bundle;
12
+ import android.util.Base64;
13
+ import androidx.activity.result.ActivityResult;
14
+ import com.getcapacitor.JSObject;
15
+ import com.getcapacitor.Plugin;
16
+ import com.getcapacitor.PluginCall;
17
+ import com.getcapacitor.PluginMethod;
18
+ import com.getcapacitor.annotation.ActivityCallback;
19
+ import com.getcapacitor.annotation.CapacitorPlugin;
20
+ import java.io.ByteArrayOutputStream;
21
+ import java.util.Iterator;
22
+ import org.json.JSONArray;
23
+ import org.json.JSONException;
24
+ import org.json.JSONObject;
25
+
26
+ @CapacitorPlugin(name = "IntentLauncher")
27
+ public class IntentLauncherPlugin extends Plugin {
28
+
29
+ private final String pluginVersion = "0.0.1";
30
+
31
+ @PluginMethod
32
+ public void startActivityAsync(PluginCall call) {
33
+ String action = call.getString("action");
34
+ if (action == null || action.isEmpty()) {
35
+ call.reject("Action is required");
36
+ return;
37
+ }
38
+
39
+ try {
40
+ Intent intent = new Intent(action);
41
+
42
+ // Set category
43
+ String category = call.getString("category");
44
+ if (category != null && !category.isEmpty()) {
45
+ intent.addCategory(category);
46
+ }
47
+
48
+ // Set data URI
49
+ String data = call.getString("data");
50
+ String type = call.getString("type");
51
+
52
+ if (data != null && !data.isEmpty() && type != null && !type.isEmpty()) {
53
+ intent.setDataAndType(Uri.parse(data), type);
54
+ } else if (data != null && !data.isEmpty()) {
55
+ intent.setData(Uri.parse(data));
56
+ } else if (type != null && !type.isEmpty()) {
57
+ intent.setType(type);
58
+ }
59
+
60
+ // Set component (package + class)
61
+ String packageName = call.getString("packageName");
62
+ String className = call.getString("className");
63
+ if (packageName != null && !packageName.isEmpty() && className != null && !className.isEmpty()) {
64
+ intent.setComponent(new ComponentName(packageName, className));
65
+ } else if (packageName != null && !packageName.isEmpty()) {
66
+ intent.setPackage(packageName);
67
+ }
68
+
69
+ // Set flags
70
+ Integer flags = call.getInt("flags");
71
+ if (flags != null) {
72
+ intent.setFlags(flags);
73
+ }
74
+
75
+ // Set extras
76
+ JSObject extra = call.getObject("extra");
77
+ if (extra != null) {
78
+ Bundle bundle = jsonToBundle(extra);
79
+ intent.putExtras(bundle);
80
+ }
81
+
82
+ // Start activity for result
83
+ startActivityForResult(call, intent, "handleActivityResult");
84
+ } catch (Exception e) {
85
+ call.reject("Failed to start activity: " + e.getMessage(), e);
86
+ }
87
+ }
88
+
89
+ @ActivityCallback
90
+ private void handleActivityResult(PluginCall call, ActivityResult result) {
91
+ if (call == null) {
92
+ return;
93
+ }
94
+
95
+ JSObject ret = new JSObject();
96
+ ret.put("resultCode", result.getResultCode());
97
+
98
+ Intent data = result.getData();
99
+ if (data != null) {
100
+ if (data.getData() != null) {
101
+ ret.put("data", data.getData().toString());
102
+ }
103
+
104
+ // Convert extras to JSON
105
+ Bundle extras = data.getExtras();
106
+ if (extras != null) {
107
+ JSObject extraObject = bundleToJson(extras);
108
+ ret.put("extra", extraObject);
109
+ }
110
+ }
111
+
112
+ call.resolve(ret);
113
+ }
114
+
115
+ @PluginMethod
116
+ public void openApplication(PluginCall call) {
117
+ String packageName = call.getString("packageName");
118
+ if (packageName == null || packageName.isEmpty()) {
119
+ call.reject("Package name is required");
120
+ return;
121
+ }
122
+
123
+ try {
124
+ PackageManager pm = getContext().getPackageManager();
125
+ Intent intent = pm.getLaunchIntentForPackage(packageName);
126
+
127
+ if (intent == null) {
128
+ call.reject("Application not found: " + packageName);
129
+ return;
130
+ }
131
+
132
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
133
+ getContext().startActivity(intent);
134
+ call.resolve();
135
+ } catch (Exception e) {
136
+ call.reject("Failed to open application: " + e.getMessage(), e);
137
+ }
138
+ }
139
+
140
+ @PluginMethod
141
+ public void getApplicationIconAsync(PluginCall call) {
142
+ String packageName = call.getString("packageName");
143
+ if (packageName == null || packageName.isEmpty()) {
144
+ call.reject("Package name is required");
145
+ return;
146
+ }
147
+
148
+ try {
149
+ PackageManager pm = getContext().getPackageManager();
150
+ Drawable drawable = pm.getApplicationIcon(packageName);
151
+
152
+ Bitmap bitmap = drawableToBitmap(drawable);
153
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
154
+ bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
155
+ byte[] bytes = baos.toByteArray();
156
+ String base64 = Base64.encodeToString(bytes, Base64.NO_WRAP);
157
+
158
+ JSObject ret = new JSObject();
159
+ ret.put("icon", "data:image/png;base64," + base64);
160
+ call.resolve(ret);
161
+ } catch (PackageManager.NameNotFoundException e) {
162
+ JSObject ret = new JSObject();
163
+ ret.put("icon", "");
164
+ call.resolve(ret);
165
+ } catch (Exception e) {
166
+ call.reject("Failed to get application icon: " + e.getMessage(), e);
167
+ }
168
+ }
169
+
170
+ @PluginMethod
171
+ public void getPluginVersion(final PluginCall call) {
172
+ try {
173
+ final JSObject ret = new JSObject();
174
+ ret.put("version", this.pluginVersion);
175
+ call.resolve(ret);
176
+ } catch (final Exception e) {
177
+ call.reject("Could not get plugin version", e);
178
+ }
179
+ }
180
+
181
+ private Bitmap drawableToBitmap(Drawable drawable) {
182
+ if (drawable instanceof BitmapDrawable) {
183
+ return ((BitmapDrawable) drawable).getBitmap();
184
+ }
185
+
186
+ int width = drawable.getIntrinsicWidth();
187
+ int height = drawable.getIntrinsicHeight();
188
+
189
+ if (width <= 0) width = 1;
190
+ if (height <= 0) height = 1;
191
+
192
+ Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
193
+ Canvas canvas = new Canvas(bitmap);
194
+ drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
195
+ drawable.draw(canvas);
196
+ return bitmap;
197
+ }
198
+
199
+ private Bundle jsonToBundle(JSObject json) {
200
+ Bundle bundle = new Bundle();
201
+ Iterator<String> keys = json.keys();
202
+
203
+ while (keys.hasNext()) {
204
+ String key = keys.next();
205
+ try {
206
+ Object value = json.get(key);
207
+
208
+ if (value instanceof String) {
209
+ bundle.putString(key, (String) value);
210
+ } else if (value instanceof Integer) {
211
+ bundle.putInt(key, (Integer) value);
212
+ } else if (value instanceof Long) {
213
+ bundle.putLong(key, (Long) value);
214
+ } else if (value instanceof Double) {
215
+ bundle.putDouble(key, (Double) value);
216
+ } else if (value instanceof Boolean) {
217
+ bundle.putBoolean(key, (Boolean) value);
218
+ } else if (value instanceof JSONArray) {
219
+ JSONArray array = (JSONArray) value;
220
+ if (array.length() > 0) {
221
+ Object first = array.get(0);
222
+ if (first instanceof String) {
223
+ String[] stringArray = new String[array.length()];
224
+ for (int i = 0; i < array.length(); i++) {
225
+ stringArray[i] = array.getString(i);
226
+ }
227
+ bundle.putStringArray(key, stringArray);
228
+ } else if (first instanceof Integer) {
229
+ int[] intArray = new int[array.length()];
230
+ for (int i = 0; i < array.length(); i++) {
231
+ intArray[i] = array.getInt(i);
232
+ }
233
+ bundle.putIntArray(key, intArray);
234
+ }
235
+ }
236
+ } else if (value instanceof JSONObject) {
237
+ bundle.putBundle(key, jsonToBundle(JSObject.fromJSONObject((JSONObject) value)));
238
+ }
239
+ } catch (JSONException e) {
240
+ // Skip this key
241
+ }
242
+ }
243
+
244
+ return bundle;
245
+ }
246
+
247
+ private JSObject bundleToJson(Bundle bundle) {
248
+ JSObject json = new JSObject();
249
+
250
+ for (String key : bundle.keySet()) {
251
+ Object value = bundle.get(key);
252
+
253
+ if (value instanceof String) {
254
+ json.put(key, (String) value);
255
+ } else if (value instanceof Integer) {
256
+ json.put(key, (Integer) value);
257
+ } else if (value instanceof Long) {
258
+ json.put(key, (Long) value);
259
+ } else if (value instanceof Double) {
260
+ json.put(key, (Double) value);
261
+ } else if (value instanceof Boolean) {
262
+ json.put(key, (Boolean) value);
263
+ } else if (value instanceof Bundle) {
264
+ json.put(key, bundleToJson((Bundle) value));
265
+ } else if (value instanceof String[]) {
266
+ JSONArray array = new JSONArray();
267
+ for (String s : (String[]) value) {
268
+ array.put(s);
269
+ }
270
+ json.put(key, array);
271
+ } else if (value instanceof int[]) {
272
+ JSONArray array = new JSONArray();
273
+ for (int i : (int[]) value) {
274
+ array.put(i);
275
+ }
276
+ json.put(key, array);
277
+ } else if (value != null) {
278
+ json.put(key, value.toString());
279
+ }
280
+ }
281
+
282
+ return json;
283
+ }
284
+ }