import { Android , AndroidIntentFiltersData , ExpoConfig } from '@expo/config-types' ; import { createAndroidManifestPlugin } from '../plugins/android-plugins' ; import { AndroidManifest , getMainActivityOrThrow , ManifestIntentFilter } from './Manifest' ; type AndroidIntentFilters = NonNullable < Android [ 'intentFilters' ] > ; const GENERATED_TAG = 'data-generated' ; export const withAndroidIntentFilters = createAndroidManifestPlugin ( setAndroidIntentFilters , 'withAndroidIntentFilters' ) ; export function getIntentFilters ( config ) { return config . android ?. intentFilters ?? [ ] ; } export function setAndroidIntentFilters ( config , androidManifest ) { const mainActivity = getMainActivityOrThrow ( androidManifest ) ; if ( mainActivity [ 'intent-filter' ] ?. length ) { mainActivity [ 'intent-filter' ] = mainActivity [ 'intent-filter' ] . filter ( value => value . $ ?. [ GENERATED_TAG ] !== 'true' ) ; } const intentFilters = getIntentFilters ( config ) ; if ( ! intentFilters . length ) { return androidManifest ; } mainActivity [ 'intent-filter' ] = mainActivity [ 'intent-filter' ] ?. concat ( renderIntentFilters ( intentFilters ) ) ; return androidManifest ; } export default function renderIntentFilters ( intentFilters ) { return intentFilters . map ( intentFilter => { return { $ : { 'android:autoVerify' : intentFilter . autoVerify ? 'true' : undefined , [ GENERATED_TAG ] : 'true' } , action : [ { $ : { 'android:name' : `android.intent.action.${ intentFilter . action }` } } , ] , data : renderIntentFilterData ( intentFilter . data ) , category : renderIntentFilterCategory ( intentFilter . category ) } ; } ) ; } /** Like `<data android:scheme="exp"/>` */ function renderIntentFilterData ( data ? ) { return ( Array . isArray ( data ) ? data : [ data ] ) . filter ( Boolean ) . map ( datum => ( { $ : Object . entries ( datum ?? { } ) . reduce ( ( prev , [ key , value ] ) => ( { ... prev , [ `android:${ key }` ] : value } ) , { } ) } ) ) ; } /** Like `<category android:name="android.intent.category.DEFAULT"/>` */ function renderIntentFilterCategory ( category ? ) { return ( Array . isArray ( category ) ? category : [ category ] ) . filter ( Boolean ) . map ( cat => ( { $ : { 'android:name' : `android.intent.category.${ cat }` } } ) ) ; }