From 3d77c555173ab08eef75c0f501b0f7b724c9eb03 Mon Sep 17 00:00:00 2001 From: yksolanki9 Date: Mon, 26 Sep 2022 14:34:48 +0530 Subject: [PATCH] Add support for multiple file uploads --- .../sharewithplugin/ShareWithPlugin.java | 38 ++++++++++++++----- src/definitions.ts | 2 +- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/android/src/main/java/com/capacitor/plugins/sharewithplugin/ShareWithPlugin.java b/android/src/main/java/com/capacitor/plugins/sharewithplugin/ShareWithPlugin.java index 3c58f6a..9462299 100644 --- a/android/src/main/java/com/capacitor/plugins/sharewithplugin/ShareWithPlugin.java +++ b/android/src/main/java/com/capacitor/plugins/sharewithplugin/ShareWithPlugin.java @@ -8,6 +8,7 @@ import com.getcapacitor.Plugin; import com.getcapacitor.annotation.CapacitorPlugin; +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -17,23 +18,42 @@ public class ShareWithPlugin extends Plugin { @Override protected void handleOnNewIntent(Intent intent) { super.handleOnNewIntent(intent); - String action = intent.getAction(); - Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM); - if (!Intent.ACTION_SEND.equals(action) || imageUri == null) { + if (!Intent.ACTION_SEND.equals(action) && !Intent.ACTION_SEND_MULTIPLE.equals(action)) { return; } - Map imageData = getImageDataFromUri(imageUri); + if(Intent.ACTION_SEND.equals(action)) { + Uri imageUri = intent.getParcelableExtra(Intent.EXTRA_STREAM); + if(imageUri == null) { + return; + } - JSObject ret = new JSObject(); + Map imageData = getImageDataFromUri(imageUri); + JSObject ret = new JSObject(); + for (Map.Entry entry : imageData.entrySet()) { + ret.put(entry.getKey(), entry.getValue()); + } + notifyListeners("FILE_SINGLE", ret, true); + } else { + ArrayList imageUriArray = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM); - for (Map.Entry entry : imageData.entrySet()) { - ret.put(entry.getKey(), entry.getValue()); - } + if(imageUriArray == null) { + return; + } - notifyListeners("FILE_SINGLE", ret, true); + JSObject retArray = new JSObject(); + for(int i = 0; i < imageUriArray.size(); i++) { + Map imageData = getImageDataFromUri(imageUriArray.get(i)); + JSObject ret = new JSObject(); + for (Map.Entry entry : imageData.entrySet()) { + ret.put(entry.getKey(), entry.getValue()); + } + retArray.put(Integer.toString(i), ret); + } + notifyListeners("FILE_MULTIPLE", retArray, true); + } } public Map getImageDataFromUri(Uri uri) { diff --git a/src/definitions.ts b/src/definitions.ts index e4ce3ab..fb44faf 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -10,5 +10,5 @@ export interface ShareWithPlugin { * @param eventName {string} - Name of the event to listen to * @param {listenerFunc} listenerFunc - The callback that is executed when the event occurs */ - addListener(eventName: 'FILE_SINGLE', listenerFunc: (event: any) => void): Promise & PluginListenerHandle; + addListener(eventName: 'FILE_SINGLE' | 'FILE_MULTIPLE', listenerFunc: (event: any) => void): Promise & PluginListenerHandle; }