Begining to refactor intent receivers to support GPX imports from more sources

This commit is contained in:
Eric van der Vlist 2023-04-17 10:41:30 +02:00
parent 8242680137
commit f82181347c
4 changed files with 90 additions and 9 deletions

View File

@ -46,8 +46,32 @@
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/gpx+xml" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/xml" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/xml" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/xml" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/xml" />
</intent-filter>
@ -70,8 +94,10 @@
</intent-filter>
<intent-filter>
<action android:name="com.darryncampbell.cordova.plugin.intent.ACTION"/>
<category android:name="android.intent.category.DEFAULT"/>
<action
android:name="com.darryncampbell.cordova.plugin.intent.ACTION"
/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter android:autoVerify="true">

View File

@ -53,6 +53,7 @@ import Invitation, {
import { returnOrUpdate } from 'ol/extent';
import Updater from '../updater';
import SystemDrawer from '../system-drawer';
import { receiveIntent } from '../../lib/intent-receivers';
const [getState, setState] = createSignal({
lon: 0,
@ -101,6 +102,9 @@ const Map: Component = () => {
if (searchInvitationInIntent(intent)) {
return;
}
if (receiveIntent(intent)) {
return;
}
if (intent.action === 'android.intent.action.VIEW') {
const url = new URL(intent.data);
console.log({ caller: 'Intent receiver', intent, url });
@ -166,13 +170,6 @@ const Map: Component = () => {
if (matches) {
findLocation(navigate, [matches[3], matches[2]]);
}
} else if (
intent.action === 'android.intent.action.SEND' &&
!!intent.extras['android.intent.extra.STREAM']
) {
const uri = intent.extras['android.intent.extra.STREAM'];
// intent.type is 'image/*' for gallery share
importUrls([uri]);
}
});
} else {

View File

@ -0,0 +1,34 @@
import { importUrls } from '../../components/import';
const ACCEPTED_ACTIONS = [
'android.intent.action.VIEW',
'android.intent.action.SEND',
];
const ACCEPTED_PROTOCOLS = ['content:'];
const ACCEPTED_TYPES = [
'application/xml',
'application/gpx+xml',
'text/xml',
'text/plain',
'image/*',
'image/jpeg',
];
export const gpxReceiver = (intent: any, url: URL | undefined) => {
if (!ACCEPTED_ACTIONS.includes(intent.action)) {
return false;
}
if (!url) {
return false;
}
if (!ACCEPTED_PROTOCOLS.includes(url.protocol)) {
return false;
}
importUrls([url.toString()]);
return true;
};

View File

@ -0,0 +1,24 @@
import { gpxReceiver } from './gpx-receiver';
const receivers = {
gpxReceiver,
};
export const receiveIntent = (intent: any) => {
let url: URL | undefined;
try {
url = new URL(
intent.extras && intent.extras['android.intent.extra.STREAM']
? intent.extras['android.intent.extra.STREAM']
: intent.data
);
} catch (error) {
console.error({ caller: 'receiveIntent / URL', error });
}
const found = Object.values(receivers).some((receiver) =>
receiver(intent, url)
);
return found;
};