1f1bb9346SEvan Bacon"use strict";
2f1bb9346SEvan Baconvar __importDefault = (this && this.__importDefault) || function (mod) {
3f1bb9346SEvan Bacon    return (mod && mod.__esModule) ? mod : { "default": mod };
4f1bb9346SEvan Bacon};
5f1bb9346SEvan BaconObject.defineProperty(exports, "__esModule", { value: true });
68f4283e8Sandyexports.removeExpoSchemaFromVerifiedIntentFilters = exports.setGeneratedAndroidScheme = exports.withGeneratedAndroidScheme = void 0;
7f1bb9346SEvan Baconconst config_plugins_1 = require("@expo/config-plugins");
8f1bb9346SEvan Baconconst getDefaultScheme_1 = __importDefault(require("./getDefaultScheme"));
9f1bb9346SEvan Baconconst withGeneratedAndroidScheme = (config) => {
10f1bb9346SEvan Bacon    return (0, config_plugins_1.withAndroidManifest)(config, (config) => {
11f1bb9346SEvan Bacon        config.modResults = setGeneratedAndroidScheme(config, config.modResults);
128f4283e8Sandy        config.modResults = removeExpoSchemaFromVerifiedIntentFilters(config, config.modResults);
13f1bb9346SEvan Bacon        return config;
14f1bb9346SEvan Bacon    });
15f1bb9346SEvan Bacon};
16f1bb9346SEvan Baconexports.withGeneratedAndroidScheme = withGeneratedAndroidScheme;
17f1bb9346SEvan Baconfunction setGeneratedAndroidScheme(config, androidManifest) {
18f1bb9346SEvan Bacon    // Generate a cross-platform scheme used to launch the dev client.
19f1bb9346SEvan Bacon    const scheme = (0, getDefaultScheme_1.default)(config);
20f1bb9346SEvan Bacon    if (!config_plugins_1.AndroidConfig.Scheme.hasScheme(scheme, androidManifest)) {
21f1bb9346SEvan Bacon        androidManifest = config_plugins_1.AndroidConfig.Scheme.appendScheme(scheme, androidManifest);
22f1bb9346SEvan Bacon    }
23f1bb9346SEvan Bacon    return androidManifest;
24f1bb9346SEvan Bacon}
25f1bb9346SEvan Baconexports.setGeneratedAndroidScheme = setGeneratedAndroidScheme;
268f4283e8Sandy/**
278f4283e8Sandy * Remove the custom Expo dev client scheme from intent filters, which are set to `autoVerify=true`.
288f4283e8Sandy * The custom scheme `<data android:scheme="exp+<slug>"/>` seems to block verification for these intent filters.
298f4283e8Sandy * This plugin makes sure there is no scheme in the autoVerify intent filters, that starts with `exp+`.
308f4283e8Sandy
319d94b613Slukmccall * Iterate over all `autoVerify=true` intent filters, and pull out schemes matching with `exp+<slug>`.
328f4283e8Sandy *
338f4283e8Sandy * @param {AndroidManifest} androidManifest
348f4283e8Sandy */
358f4283e8Sandyfunction removeExpoSchemaFromVerifiedIntentFilters(config, androidManifest) {
368f4283e8Sandy    // Generate a cross-platform scheme used to launch the dev client.
378f4283e8Sandy    const defaultScheme = (0, getDefaultScheme_1.default)(config);
388f4283e8Sandy    // see: https://github.com/expo/expo-cli/blob/f1624c75b52cc1c4f99354ec4021494e0eff74aa/packages/config-plugins/src/android/Scheme.ts#L164-L179
398f4283e8Sandy    for (const application of androidManifest.manifest.application || []) {
408f4283e8Sandy        for (const activity of application.activity || []) {
418f4283e8Sandy            if (activityHasSingleTaskLaunchMode(activity)) {
428f4283e8Sandy                for (const intentFilter of activity['intent-filter'] || []) {
43*1daf3609Slukmccall                    if (intentFilterHasAutoVerification(intentFilter) && intentFilter?.data) {
448f4283e8Sandy                        intentFilter.data = intentFilterRemoveSchemeFromData(intentFilter, (scheme) => scheme === defaultScheme);
458f4283e8Sandy                    }
468f4283e8Sandy                }
478f4283e8Sandy                break;
488f4283e8Sandy            }
498f4283e8Sandy        }
508f4283e8Sandy    }
518f4283e8Sandy    return androidManifest;
528f4283e8Sandy}
538f4283e8Sandyexports.removeExpoSchemaFromVerifiedIntentFilters = removeExpoSchemaFromVerifiedIntentFilters;
548f4283e8Sandy/**
558f4283e8Sandy * Determine if the activity should contain the intent filters to clean.
568f4283e8Sandy *
578f4283e8Sandy * @see https://github.com/expo/expo-cli/blob/f1624c75b52cc1c4f99354ec4021494e0eff74aa/packages/config-plugins/src/android/Scheme.ts#L166
588f4283e8Sandy */
598f4283e8Sandyfunction activityHasSingleTaskLaunchMode(activity) {
60*1daf3609Slukmccall    return activity?.$?.['android:launchMode'] === 'singleTask';
618f4283e8Sandy}
628f4283e8Sandy/**
638f4283e8Sandy * Determine if the intent filter has `autoVerify=true`.
648f4283e8Sandy */
658f4283e8Sandyfunction intentFilterHasAutoVerification(intentFilter) {
66*1daf3609Slukmccall    return intentFilter?.$?.['android:autoVerify'] === 'true';
678f4283e8Sandy}
688f4283e8Sandy/**
698f4283e8Sandy * Remove schemes from the intent filter that matches the function.
708f4283e8Sandy */
718f4283e8Sandyfunction intentFilterRemoveSchemeFromData(intentFilter, schemeMatcher) {
72*1daf3609Slukmccall    return intentFilter?.data?.filter((entry) => !schemeMatcher(entry?.$['android:scheme'] || ''));
738f4283e8Sandy}
74