|
| 1 | +import { |
| 2 | + AndroidConfig, |
| 3 | + ConfigPlugin, |
| 4 | + createRunOncePlugin, |
| 5 | + withAndroidManifest, |
| 6 | +} from '@expo/config-plugins'; |
| 7 | +const { getMainApplicationOrThrow, prefixAndroidKeys } = AndroidConfig.Manifest; |
| 8 | + |
| 9 | +const INTERSTITIAL_AD_ACTIVITY = 'com.facebook.ads.InterstitialAdActivity'; |
| 10 | + |
| 11 | +export const withFacebookManifest: ConfigPlugin = (config) => { |
| 12 | + return withAndroidManifest(config, (config) => { |
| 13 | + config.modResults = setFacebookConfig(config.modResults); |
| 14 | + return config; |
| 15 | + }); |
| 16 | +}; |
| 17 | + |
| 18 | +export function setFacebookConfig( |
| 19 | + androidManifest: AndroidConfig.Manifest.AndroidManifest |
| 20 | +) { |
| 21 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 22 | + let mainApplication = getMainApplicationOrThrow(androidManifest); |
| 23 | + mainApplication = ensureFacebookActivity({ mainApplication }); |
| 24 | + |
| 25 | + return androidManifest; |
| 26 | +} |
| 27 | + |
| 28 | +function ensureFacebookActivity({ |
| 29 | + mainApplication, |
| 30 | +}: { |
| 31 | + mainApplication: AndroidConfig.Manifest.ManifestApplication; |
| 32 | +}) { |
| 33 | + if (Array.isArray(mainApplication.activity)) { |
| 34 | + // Remove all Facebook InterstitialAdActivity first |
| 35 | + mainApplication.activity = mainApplication.activity.filter((activity) => { |
| 36 | + return activity.$?.['android:name'] !== INTERSTITIAL_AD_ACTIVITY; |
| 37 | + }); |
| 38 | + } else { |
| 39 | + mainApplication.activity = []; |
| 40 | + } |
| 41 | + |
| 42 | + mainApplication.activity.push(getFacebookAdActivity()); |
| 43 | + return mainApplication; |
| 44 | +} |
| 45 | + |
| 46 | +function buildXMLItem({ |
| 47 | + head, |
| 48 | + children, |
| 49 | +}: { |
| 50 | + head: Record<string, string>; |
| 51 | + children?: Record<string, string | any[]>; |
| 52 | +}) { |
| 53 | + return { ...(children ?? {}), $: head }; |
| 54 | +} |
| 55 | + |
| 56 | +function getFacebookAdActivity() { |
| 57 | + /** |
| 58 | +<activity |
| 59 | + android:name="com.facebook.ads.InterstitialAdActivity" |
| 60 | + android:configChanges="keyboardHidden|orientation" |
| 61 | +/> |
| 62 | + */ |
| 63 | + return buildXMLItem({ |
| 64 | + head: prefixAndroidKeys({ |
| 65 | + name: INTERSTITIAL_AD_ACTIVITY, |
| 66 | + configChanges: 'keyboardHidden|orientation', |
| 67 | + }), |
| 68 | + }) as AndroidConfig.Manifest.ManifestActivity; |
| 69 | +} |
| 70 | + |
| 71 | +/** |
| 72 | + * Apply react-native-fbads configuration for Expo SDK 44 projects. |
| 73 | + */ |
| 74 | +const withReactNativeFbads: ConfigPlugin = (config) => { |
| 75 | + return withFacebookManifest(config); |
| 76 | +}; |
| 77 | + |
| 78 | +const pkg = require('react-native-fbads/package.json'); |
| 79 | + |
| 80 | +export default createRunOncePlugin(withReactNativeFbads, pkg.name, pkg.version); |
0 commit comments