From d3c0cfd393741872594d17c8d6fd00439e428213 Mon Sep 17 00:00:00 2001 From: evlist Date: Mon, 17 Apr 2023 15:09:08 +0200 Subject: [PATCH] Refactoring intent receivers for gmap links --- src/components/map/Map.tsx | 51 ----------------------- src/lib/intent-receivers/gmap-receiver.ts | 29 +++++++++++++ src/lib/intent-receivers/index.ts | 2 + 3 files changed, 31 insertions(+), 51 deletions(-) create mode 100644 src/lib/intent-receivers/gmap-receiver.ts diff --git a/src/components/map/Map.tsx b/src/components/map/Map.tsx index 3cee1d9..c6e1557 100644 --- a/src/components/map/Map.tsx +++ b/src/components/map/Map.tsx @@ -95,57 +95,6 @@ const Map: Component = () => { if (receiveIntent(intent, navigate)) { return; } - if ( - intent.action === 'android.intent.action.SEND' && - !!intent.extras['android.intent.extra.TEXT'] - ) { - /* - - Intent from gmaps (example): - - { - "clipItems": [ - { - "text": "https://www.google.fr/maps/place/Th%C3%A9%C3%A2tre+Campoamor,+C.+Pelayo,+33003+Oviedo,+Asturias/@43.3622041,-5.8485043,17z/data=!4m6!3m5!1s0xd368cfacd5f4783:0x8629a4fbbc3232b5!8m2!3d43.3628346!4d-5.8479215!16s%2Fg%2F121d1flv" - } - ], - "type": "text/plain", - "extras": { - "android.intent.extra.SUBJECT": "", - "android.intent.extra.TEXT": "https://www.google.fr/maps/place/Th%C3%A9%C3%A2tre+Campoamor,+C.+Pelayo,+33003+Oviedo,+Asturias/@43.3622041,-5.8485043,17z/data=!4m6!3m5!1s0xd368cfacd5f4783:0x8629a4fbbc3232b5!8m2!3d43.3628346!4d-5.8479215!16s%2Fg%2F121d1flv" - }, - "action": "android.intent.action.SEND", - "flags": 272629761, - "component": "ComponentInfo{com.dyomedea.dyomedea/com.dyomedea.dyomedea.MainActivity}" -} - - or - - { - "clipItems": [ - { - "text": "https://www.google.fr/maps/place/C.+San+Francisco,+1,+33003+Oviedo,+Asturias/@43.3629409,-5.8458296,16z/data=!4m6!3m5!1s0xd368ce5636451f5:0x23dd7ba12d269d1b!8m2!3d43.3617786!4d-5.8464287!16s%2Fg%2F11bw44f29g" - } - ], - "type": "text/plain", - "extras": { - "android.intent.extra.SUBJECT": "", - "android.intent.extra.TEXT": "https://www.google.fr/maps/place/C.+San+Francisco,+1,+33003+Oviedo,+Asturias/@43.3629409,-5.8458296,16z/data=!4m6!3m5!1s0xd368ce5636451f5:0x23dd7ba12d269d1b!8m2!3d43.3617786!4d-5.8464287!16s%2Fg%2F11bw44f29g" - }, - "action": "android.intent.action.SEND", - "flags": 272629761, - "component": "ComponentInfo{com.dyomedea.dyomedea/com.dyomedea.dyomedea.MainActivity}" -} - - */ - - const matches = intent.extras['android.intent.extra.TEXT'].match( - /^https:\/\/www\.google\..+\/maps\/place\/(.+\/)?@(-?[0-9.]+),(-?[0-9.]+),[0-9]+z\/.*$/ - ); - if (matches) { - findLocation(navigate, [matches[3], matches[2]]); - } - } }); } else { console.log({ diff --git a/src/lib/intent-receivers/gmap-receiver.ts b/src/lib/intent-receivers/gmap-receiver.ts new file mode 100644 index 0000000..fb8ce9d --- /dev/null +++ b/src/lib/intent-receivers/gmap-receiver.ts @@ -0,0 +1,29 @@ +import { findLocation } from '../../components/finder'; + +const ACCEPTED_ACTIONS = ['android.intent.action.SEND']; + +export const gmapReceiver = ( + intent: any, + url: URL | undefined, + navigate: any +) => { + if (!ACCEPTED_ACTIONS.includes(intent.action)) { + return false; + } + + if (!intent.extras || !intent.extras['android.intent.extra.TEXT']) { + return false; + } + + const matches = intent.extras['android.intent.extra.TEXT'].match( + /^https:\/\/www\.google\..+\/maps\/place\/(.+\/)?@(-?[0-9.]+),(-?[0-9.]+),[0-9]+z\/.*$/ + ); + + if (!matches) { + return false; + } + + findLocation(navigate, [matches[3], matches[2]]); + + return true; +}; diff --git a/src/lib/intent-receivers/index.ts b/src/lib/intent-receivers/index.ts index 81d4db5..b204365 100644 --- a/src/lib/intent-receivers/index.ts +++ b/src/lib/intent-receivers/index.ts @@ -1,4 +1,5 @@ import { geoLocationReceiver } from './geo-location'; +import { gmapReceiver } from './gmap-receiver'; import { gpxReceiver } from './gpx-receiver'; import { invitationReceiver } from './invitation-receiver'; import { webLinksReceiver } from './web-links'; @@ -8,6 +9,7 @@ const receivers = { gpxReceiver, geoLocationReceiver, webLinksReceiver, + gmapReceiver, }; export const receiveIntent = (intent: any, navigate: any) => {