Skip to content
This repository has been archived by the owner on Mar 5, 2024. It is now read-only.

fix: pending android callbacks #34

Merged
merged 7 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .eslintignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
plugins
metro.config.js
babel.config.js
babel.config.js
android
ios
3 changes: 3 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
module.exports = {
root: true,
extends: ["universe/native"],
rules: {
"import/order": "off",
},
};
91 changes: 60 additions & 31 deletions hooks/useShareIntent.js
Original file line number Diff line number Diff line change
@@ -1,65 +1,94 @@
import Constants from "expo-constants";
import { useEffect, useRef, useState } from "react";
import { AppState } from "react-native";
import ReceiveSharingIntent from "react-native-receive-sharing-intent";

export default function useShareIntent() {
const appState = useRef(AppState.currentState);
const [shareIntent, setShareIntent] = useState(null);

useEffect(() => {
const subscription = AppState.addEventListener("change", (nextAppState) => {
if (
appState.current === "active" &&
["inactive", "background"].includes(nextAppState)
) {
console.log("useShareIntent[to-background] reset intent");
setShareIntent(null);
}
import Constants from "expo-constants";

appState.current = nextAppState;
});
return () => {
subscription.remove();
};
}, []);
import ReceiveSharingIntent from "react-native-receive-sharing-intent";

useEffect(() => {
console.log("useShareIntent[mount]", Constants.expoConfig.scheme);
ReceiveSharingIntent?.getReceivedFiles(
export const getShareIntentAsync = async () => {
return new Promise((resolve, reject) => {
ReceiveSharingIntent.getReceivedFiles(
(data) => {
if (!data || data.length === 0) {
console.log("useShareIntent[data] no share intent detected");
return;
}
const intent = data[0];
if (intent.weblink || intent.text) {
const link = intent.weblink || intent.text || "";
console.log("useShareIntent[text/url]", link);
setShareIntent(JSON.stringify(link));
console.debug("useShareIntent[text/url]", link);
resolve({ text: JSON.stringify(link) });
} else if (intent.filePath) {
console.log("useShareIntent[file]", {
console.debug("useShareIntent[file]", {
uri: intent.contentUri || intent.filePath,
mimeType: intent.mimeType,
fileName: intent.fileName,
});
setShareIntent({
resolve({
uri: intent.contentUri || intent.filePath,
mimeType: intent.mimeType,
fileName: intent.fileName,
});
} else {
console.log("useShareIntent[mount] share type not handled", data);
console.warn("useShareIntent[get] share type not handled", data);
reject(new Error("TYPE_NOT_HANDLED"));
}
},
(err) => {
console.log("useShareIntent[mount] error", err);
console.error("useShareIntent[get] internal native module error", err);
reject(err);
},
Constants.expoConfig.scheme,
);
});
};

export const clearShareIntent = () => {
ReceiveSharingIntent?.clearReceivedFiles();
};

export default function useShareIntent() {
const appState = useRef(AppState.currentState);
const [shareIntent, setShareIntent] = useState(null);
const [error, setError] = useState();

const refreshShareIntent = () =>
getShareIntentAsync()
.then(setShareIntent)
.catch((err) => setError("shareIntent error : " + err?.message));

useEffect(() => {
const subscription = AppState.addEventListener("change", (nextAppState) => {
if (nextAppState === "active") {
console.debug("useShareIntent[active] refresh intent");
refreshShareIntent();
} else if (
appState.current === "active" &&
["inactive", "background"].includes(nextAppState)
) {
console.debug("useShareIntent[to-background] reset intent");
setShareIntent(null);
clearShareIntent();
}

appState.current = nextAppState;
});
return () => {
ReceiveSharingIntent?.clearReceivedFiles();
subscription.remove();
};
}, []);

useEffect(() => {
console.debug("useShareIntent[mount]", Constants.expoConfig.scheme);
refreshShareIntent();
return clearShareIntent;
}, []);

console.debug("useShareIntent[render]", shareIntent);

return {
shareIntent,
resetShareIntent: () => setShareIntent(null),
error,
};
}
211 changes: 199 additions & 12 deletions patches/react-native-receive-sharing-intent+2.0.0.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,166 @@
diff --git a/node_modules/react-native-receive-sharing-intent/android/src/main/java/com/reactnativereceivesharingintent/ReceiveSharingIntentHelper.java b/node_modules/react-native-receive-sharing-intent/android/src/main/java/com/reactnativereceivesharingintent/ReceiveSharingIntentHelper.java
index 69126eb..6ecf9b4 100644
--- a/node_modules/react-native-receive-sharing-intent/android/src/main/java/com/reactnativereceivesharingintent/ReceiveSharingIntentHelper.java
+++ b/node_modules/react-native-receive-sharing-intent/android/src/main/java/com/reactnativereceivesharingintent/ReceiveSharingIntentHelper.java
@@ -26,83 +26,92 @@ public class ReceiveSharingIntentHelper {
this.context = context;
}

- @RequiresApi(api = Build.VERSION_CODES.KITKAT)
- public void sendFileNames(Context context, Intent intent, Promise promise){
- try {
- String action = intent.getAction();
- String type = intent.getType();
- if(type == null) { return; }
- if(!type.startsWith("text") && (Objects.equals(action, Intent.ACTION_SEND) || Objects.equals(action, Intent.ACTION_SEND_MULTIPLE))){
+@RequiresApi(api = Build.VERSION_CODES.KITKAT)
+public void sendFileNames(Context context, Intent intent, Promise promise){
+ try {
+ String action = intent.getAction();
+ String type = intent.getType();
+ if(type == null) {
+ promise.resolve(null);
+ return;
+ }
+ if(!type.startsWith("text") && (Objects.equals(action, Intent.ACTION_SEND) || Objects.equals(action, Intent.ACTION_SEND_MULTIPLE))){
+ WritableMap files = getMediaUris(intent,context);
+ if(files == null) {
+ promise.reject("error", "Files is null");
+ return;
+ }
+ promise.resolve(files);
+ }else if(type.startsWith("text") && Objects.equals(action, Intent.ACTION_SEND)){
+ String text = null;
+ String subject = null;
+ try{
+ text = intent.getStringExtra(Intent.EXTRA_TEXT);
+ subject = intent.getStringExtra(Intent.EXTRA_SUBJECT);
+ }catch (Exception ignored){ }
+ if(text == null){
WritableMap files = getMediaUris(intent,context);
- if(files == null) return;
- promise.resolve(files);
- }else if(type.startsWith("text") && Objects.equals(action, Intent.ACTION_SEND)){
- String text = null;
- String subject = null;
- try{
- text = intent.getStringExtra(Intent.EXTRA_TEXT);
- subject = intent.getStringExtra(Intent.EXTRA_SUBJECT);
- }catch (Exception ignored){ }
- if(text == null){
- WritableMap files = getMediaUris(intent,context);
- if(files == null) return;
- promise.resolve(files);
- }else{
- WritableMap files = new WritableNativeMap();
- WritableMap file = new WritableNativeMap();
- file.putString("contentUri",null);
- file.putString("filePath", null);
- file.putString("fileName", null);
- file.putString("extension", null);
- if(text.startsWith("http")){
- file.putString("weblink", text);
- file.putString("text",null);
- }else{
- file.putString("weblink", null);
- file.putString("text",text);
- }
- file.putString("subject", subject);
- files.putMap("0",file);
- promise.resolve(files);
+ if(files == null) {
+ promise.reject("error", "Files is null");
+ return;
}
-
- }else if(Objects.equals(action, Intent.ACTION_VIEW)){
- String link = intent.getDataString();
+ promise.resolve(files);
+ }else{
WritableMap files = new WritableNativeMap();
WritableMap file = new WritableNativeMap();
file.putString("contentUri",null);
file.putString("filePath", null);
- file.putString("mimeType",null);
- file.putString("text",null);
- file.putString("weblink", link);
file.putString("fileName", null);
file.putString("extension", null);
+ if(text.startsWith("http")){
+ file.putString("weblink", text);
+ file.putString("text",null);
+ }else{
+ file.putString("weblink", null);
+ file.putString("text",text);
+ }
+ file.putString("subject", subject);
files.putMap("0",file);
promise.resolve(files);
}
- else if (Objects.equals(action, "android.intent.action.PROCESS_TEXT")) {
- String text = null;
- try {
- text = intent.getStringExtra(intent.EXTRA_PROCESS_TEXT);
- } catch (Exception e) {
- }
- WritableMap files = new WritableNativeMap();
- WritableMap file = new WritableNativeMap();
- file.putString("contentUri", null);
- file.putString("filePath", null);
- file.putString("fileName", null);
- file.putString("extension", null);
- file.putString("weblink", null);
- file.putString("text", text);
- files.putMap("0", file);
- promise.resolve(files);
- }else{
- promise.reject("error","Invalid file type.");
+
+ }else if(Objects.equals(action, Intent.ACTION_VIEW)){
+ String link = intent.getDataString();
+ WritableMap files = new WritableNativeMap();
+ WritableMap file = new WritableNativeMap();
+ file.putString("contentUri",null);
+ file.putString("filePath", null);
+ file.putString("mimeType",null);
+ file.putString("text",null);
+ file.putString("weblink", link);
+ file.putString("fileName", null);
+ file.putString("extension", null);
+ files.putMap("0",file);
+ promise.resolve(files);
+ }
+ else if (Objects.equals(action, "android.intent.action.PROCESS_TEXT")) {
+ String text = null;
+ try {
+ text = intent.getStringExtra(intent.EXTRA_PROCESS_TEXT);
+ } catch (Exception e) {
}
- }catch (Exception e){
- promise.reject("error",e.toString());
+ WritableMap files = new WritableNativeMap();
+ WritableMap file = new WritableNativeMap();
+ file.putString("contentUri", null);
+ file.putString("filePath", null);
+ file.putString("fileName", null);
+ file.putString("extension", null);
+ file.putString("weblink", null);
+ file.putString("text", text);
+ files.putMap("0", file);
+ promise.resolve(files);
+ }else{
+ promise.reject("error","Invalid file type.");
}
- };
+ }catch (Exception e){
+ promise.reject("error",e.toString());
+ }
+};


@RequiresApi(api = Build.VERSION_CODES.KITKAT)
diff --git a/node_modules/react-native-receive-sharing-intent/android/src/main/java/com/reactnativereceivesharingintent/ReceiveSharingIntentModule.java b/node_modules/react-native-receive-sharing-intent/android/src/main/java/com/reactnativereceivesharingintent/ReceiveSharingIntentModule.java
index f752144..725918a 100644
index f752144..f0bd892 100644
--- a/node_modules/react-native-receive-sharing-intent/android/src/main/java/com/reactnativereceivesharingintent/ReceiveSharingIntentModule.java
+++ b/node_modules/react-native-receive-sharing-intent/android/src/main/java/com/reactnativereceivesharingintent/ReceiveSharingIntentModule.java
@@ -18,6 +18,7 @@ public class ReceiveSharingIntentModule extends ReactContextBaseJavaModule {
Expand All @@ -10,35 +171,61 @@ index f752144..725918a 100644

public ReceiveSharingIntentModule(ReactApplicationContext reactContext) {
super(reactContext);
@@ -30,6 +31,7 @@ public class ReceiveSharingIntentModule extends ReactContextBaseJavaModule {
@@ -30,18 +31,24 @@ public class ReceiveSharingIntentModule extends ReactContextBaseJavaModule {
protected void onNewIntent(Intent intent) {
Activity mActivity = getCurrentActivity();
if(mActivity == null) { return; }
+ oldIntent = mActivity.getIntent();
mActivity.setIntent(intent);
}

@@ -40,7 +42,9 @@ public class ReceiveSharingIntentModule extends ReactContextBaseJavaModule {
if(mActivity == null) { return; }
Intent intent = mActivity.getIntent();
receiveSharingIntentHelper.sendFileNames(reactContext, intent, promise);
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
- @ReactMethod
- public void getFileNames(Promise promise){
- Activity mActivity = getCurrentActivity();
- if(mActivity == null) { return; }
- Intent intent = mActivity.getIntent();
- receiveSharingIntentHelper.sendFileNames(reactContext, intent, promise);
- mActivity.setIntent(null);
+ if (oldIntent != null) {
+ mActivity.setIntent(oldIntent);
+ }
+@ReactMethod
+public void getFileNames(Promise promise) {
+ Activity mActivity = getCurrentActivity();
+ if (mActivity == null) {
+ promise.reject("ACTIVITY_NULL", "Activity is null");
+ return;
+ }
+ Intent intent = mActivity.getIntent();
+ receiveSharingIntentHelper.sendFileNames(reactContext, intent, promise);
+ if (oldIntent != null) {
+ mActivity.setIntent(oldIntent);
}
+}

@ReactMethod
public void clearFileNames(){
diff --git a/node_modules/react-native-receive-sharing-intent/src/ReceiveSharingIntent.ts b/node_modules/react-native-receive-sharing-intent/src/ReceiveSharingIntent.ts
index 735c191..91dab4b 100644
index 735c191..8415a3a 100644
--- a/node_modules/react-native-receive-sharing-intent/src/ReceiveSharingIntent.ts
+++ b/node_modules/react-native-receive-sharing-intent/src/ReceiveSharingIntent.ts
@@ -33,7 +33,7 @@ class ReceiveSharingIntentModule implements IReceiveSharingIntent {
@@ -33,18 +33,20 @@ class ReceiveSharingIntentModule implements IReceiveSharingIntent {
}

clearReceivedFiles(){
- this.isClear = true;
+ ReceiveSharingIntent.clearFileNames();
}



protected getFileNames(handler: Function, errorHandler: Function, url: string){
if(this.isIos){
ReceiveSharingIntent.getFileNames(url).then((data: any)=>{
+ if (!data) { handler([]); return; }
let files = this.utils.sortData(data);
handler(files);
}).catch((e:any)=>errorHandler(e));
}else{
ReceiveSharingIntent.getFileNames().then((fileObject: any) => {
+ if (!fileObject) { handler([]); return; }
let files = Object.keys(fileObject).map((k) => fileObject[k])
handler(files);
}).catch((e:any)=>errorHandler(e));
Loading
Loading