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

Commit

Permalink
Fix android schemes being added incorrectly (#2507)
Browse files Browse the repository at this point in the history
  • Loading branch information
EvanBacon committed Aug 26, 2020
1 parent 626b80d commit fc27865
Showing 1 changed file with 19 additions and 24 deletions.
43 changes: 19 additions & 24 deletions packages/config/src/android/Scheme.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Parser } from 'xml2js';

import { ExpoConfig } from '../Config.types';
import { addWarningAndroid } from '../WarningAggregator';
import { Document } from './Manifest';

export type IntentFilterProps = {
Expand All @@ -11,9 +10,8 @@ export type IntentFilterProps = {

export function getScheme(config: { scheme?: string | string[] }): string[] {
if (Array.isArray(config.scheme)) {
function validate(value: any): value is string {
return typeof value === 'string';
}
const validate = (value: any): value is string => typeof value === 'string';

return config.scheme.filter<string>(validate);
} else if (typeof config.scheme === 'string') {
return [config.scheme];
Expand All @@ -34,26 +32,23 @@ export async function setScheme(
return manifestDocument;
}

const mainActivity = manifestDocument.manifest.application[0].activity.filter(
(e: any) => e['$']['android:name'] === '.MainActivity'
);

const intentFiltersXML = `
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
${scheme.map(scheme => `<data android:scheme="${scheme}"/>`).join('\n')}
</intent-filter>`;
const parser = new Parser();
const intentFiltersJSON = await parser.parseStringPromise(intentFiltersXML);

if ('intent-filter' in mainActivity[0]) {
mainActivity[0]['intent-filter'] = mainActivity[0]['intent-filter'].concat(
intentFiltersJSON['intent-filter']
if (!ensureManifestHasValidIntentFilter(manifestDocument)) {
addWarningAndroid(
'scheme',
`Cannot add schemes because the provided manifest does not have a valid Activity with \`android:launchMode="singleTask"\`.\nThis guide can help you get setup properly https://expo.fyi/setup-android-uri-scheme`
);
} else {
mainActivity[0]['intent-filter'] = intentFiltersJSON['intent-filter'];
return manifestDocument;
}

// Get the current schemes and remove them.
const currentSchemes = getSchemesFromManifest(manifestDocument);
for (const uri of currentSchemes) {
manifestDocument = removeScheme(uri, manifestDocument);
}

// Now add all the new schemes.
for (const uri of scheme) {
manifestDocument = appendScheme(uri, manifestDocument);
}

return manifestDocument;
Expand Down

0 comments on commit fc27865

Please sign in to comment.