Spaces:
Sleeping
Sleeping
File size: 5,960 Bytes
a6a4a7d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | import { useEffect } from 'react'
export function useGlobalAndroidIntent() {
useEffect(() => {
const forceRedirectToColorRM = (uri: string) => {
console.log("[Global] File intent detected. Redirecting to ColorRM immediately:", uri);
window.location.href = '/color_rm.html?importPdf=' + encodeURIComponent(uri);
}
const handleFile = async (uri: string) => {
console.log('[Global] Received shared file URI:', uri)
// FAST PATH: Redirect immediately if it's a content/file URI, assuming it's for ColorRM
if (uri.startsWith('content://') || uri.startsWith('file://')) {
forceRedirectToColorRM(uri);
return;
}
// For other file types, buffer them
console.log('[Global] Non-PDF file buffered:', uri);
window.pendingFileUri = uri;
}
const handleFiles = (uris: string[]) => {
console.log('[Global] Received multiple shared files:', uris);
// Store URIs in sessionStorage so ColorRM can retrieve them after redirect
// This prevents the data from being lost if the native buffer is cleared by the first read
sessionStorage.setItem('pending_shared_uris', JSON.stringify(uris));
// For multiple files, we always redirect to ColorRM with a special param or just the first one
// ColorRM will then check sessionStorage for the full list
if (uris.length > 0) {
forceRedirectToColorRM(uris[0]);
}
}
const handleUrl = (url: string) => {
console.log('[Global] Received shared URL:', url)
// Buffer URL for later
window.pendingUrl = url;
}
// Define global handlers override
window.handleSharedFile = (uri: string) => {
handleFile(uri)
}
window.handleSharedFiles = (uris: string[]) => {
handleFiles(uris)
}
window.handleSharedUrl = (url: string) => {
handleUrl(url)
}
// 1. Process JS-buffered items
if (window.pendingFileUri) {
handleFile(window.pendingFileUri)
}
if (window.pendingUrl) {
handleUrl(window.pendingUrl)
}
const checkNativeBuffer = () => {
if (window.AndroidNative) {
console.log("[Global] AndroidNative interface found. Checking buffer...");
try {
const pendingFile = window.AndroidNative.getPendingFileUri?.();
if (pendingFile) {
console.log('[Global] Found native pending file:', pendingFile);
handleFile(pendingFile);
}
const pendingFiles = window.AndroidNative.getPendingFileUris?.();
if (pendingFiles) {
console.log('[Global] Found native pending files:', pendingFiles);
try {
const uris = JSON.parse(pendingFiles);
if (Array.isArray(uris) && uris.length > 0) {
handleFiles(uris);
}
} catch (e) {
console.error("[Global] Error parsing pending files JSON:", e);
}
}
const pendingText = window.AndroidNative.getPendingSharedText?.();
if (pendingText) {
console.log('[Global] Found native pending text:', pendingText);
handleUrl(pendingText);
}
} catch (e) {
console.error("[Global] Error checking native pending intents:", e);
}
} else {
console.warn("[Global] AndroidNative interface NOT found. Native intent buffer unavailable.");
// alert("Debug: Native Bridge NOT detected. Please rebuild the Android app.");
}
}
// 2. Poll Native-buffered items immediately
checkNativeBuffer();
// 3. Listen for App Resume (for background intents)
if (window.Capacitor && window.Capacitor.Plugins && window.Capacitor.Plugins.App) {
const App = window.Capacitor.Plugins.App;
App.addListener('appStateChange', (state: any) => {
if (state.isActive) {
console.log("[Global] App resumed, checking native buffer...");
checkNativeBuffer();
}
});
App.addListener('appUrlOpen', (data: any) => {
console.log('[Global] App opened with URL:', data);
if (data.url) {
if (data.url.startsWith('file://') || data.url.startsWith('content://')) {
forceRedirectToColorRM(data.url);
} else {
handleUrl(data.url);
}
}
});
const checkAppLaunchUrl = async () => {
try {
const result = await App.getLaunchUrl();
if (result && result.url) {
console.log('[Global] App launched with URL: ' + result.url);
if (result.url.startsWith('file://') || result.url.startsWith('content://')) {
forceRedirectToColorRM(result.url);
} else {
handleUrl(result.url);
}
}
} catch (e) {
console.error("[Global] Error checking launch URL:", e);
}
};
checkAppLaunchUrl();
}
}, [])
} |