Refactoring intent receivers for gmap links

This commit is contained in:
Eric van der Vlist 2023-04-17 15:09:08 +02:00
parent f9517b07b4
commit d3c0cfd393
3 changed files with 31 additions and 51 deletions

View File

@ -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({

View File

@ -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;
};

View File

@ -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) => {