|
| 1 | +"use strict"; |
| 2 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 3 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 4 | +}; |
| 5 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 6 | +exports.withAndroidGoogleCast = void 0; |
| 7 | +const config_plugins_1 = require("@expo/config-plugins"); |
| 8 | +const generateCode_1 = require("@expo/config-plugins/build/utils/generateCode"); |
| 9 | +const fs_1 = __importDefault(require("fs")); |
| 10 | +const { addMetaDataItemToMainApplication, getMainApplicationOrThrow } = config_plugins_1.AndroidConfig.Manifest; |
| 11 | +const META_PROVIDER_CLASS = "com.google.android.gms.cast.framework.OPTIONS_PROVIDER_CLASS_NAME"; |
| 12 | +const META_RECEIVER_APP_ID = "com.reactnative.googlecast.RECEIVER_APPLICATION_ID"; |
| 13 | +const withAndroidManifestCast = (config, { receiverAppId } = {}) => { |
| 14 | + return config_plugins_1.withAndroidManifest(config, (config) => { |
| 15 | + const mainApplication = getMainApplicationOrThrow(config.modResults); |
| 16 | + addMetaDataItemToMainApplication(mainApplication, META_PROVIDER_CLASS, |
| 17 | + // This is the native Java class |
| 18 | + "com.reactnative.googlecast.GoogleCastOptionsProvider"); |
| 19 | + if (receiverAppId) { |
| 20 | + addMetaDataItemToMainApplication(mainApplication, META_RECEIVER_APP_ID, receiverAppId); |
| 21 | + } |
| 22 | + return config; |
| 23 | + }); |
| 24 | +}; |
| 25 | +const withAppBuildGradleImport = (config, { version }) => { |
| 26 | + return config_plugins_1.withAppBuildGradle(config, (config) => { |
| 27 | + if (config.modResults.language !== "groovy") |
| 28 | + throw new Error("react-native-google-cast config plugin does not support Kotlin app/build.gradle yet."); |
| 29 | + config.modResults.contents = addGoogleCastImport(config.modResults.contents, { |
| 30 | + version, |
| 31 | + }).contents; |
| 32 | + return config; |
| 33 | + }); |
| 34 | +}; |
| 35 | +const withMainActivityLazyLoading = (config) => { |
| 36 | + return config_plugins_1.withDangerousMod(config, [ |
| 37 | + "android", |
| 38 | + async (config) => { |
| 39 | + const file = await config_plugins_1.AndroidConfig.Paths.getMainActivityAsync(config.modRequest.projectRoot); |
| 40 | + if (file.language === "java") { |
| 41 | + const src = addGoogleCastLazyLoadingImport(file.contents).contents; |
| 42 | + await fs_1.default.promises.writeFile(file.path, src, "utf-8"); |
| 43 | + } |
| 44 | + else { |
| 45 | + throw new Error("react-native-google-cast config plugin does not support kotlin MainActivity yet."); |
| 46 | + } |
| 47 | + return config; |
| 48 | + }, |
| 49 | + ]); |
| 50 | +}; |
| 51 | +const withAndroidGoogleCast = (config, props) => { |
| 52 | + var _a; |
| 53 | + config = withAndroidManifestCast(config, { |
| 54 | + receiverAppId: props.receiverAppId, |
| 55 | + }); |
| 56 | + config = withMainActivityLazyLoading(config); |
| 57 | + config = withAppBuildGradleImport(config, { |
| 58 | + // gradle dep version |
| 59 | + version: (_a = props.androidPlayServicesCastFrameworkVersion) !== null && _a !== void 0 ? _a : "+", |
| 60 | + }); |
| 61 | + return config; |
| 62 | +}; |
| 63 | +exports.withAndroidGoogleCast = withAndroidGoogleCast; |
| 64 | +function addGoogleCastLazyLoadingImport(src) { |
| 65 | + const newSrc = []; |
| 66 | + newSrc.push(" CastContext.getSharedInstance(this);"); |
| 67 | + return generateCode_1.mergeContents({ |
| 68 | + tag: "react-native-google-cast-onCreate", |
| 69 | + src, |
| 70 | + newSrc: newSrc.join("\n"), |
| 71 | + anchor: /super\.onCreate\(\w+\);/, |
| 72 | + offset: 1, |
| 73 | + comment: "//", |
| 74 | + }); |
| 75 | +} |
| 76 | +// TODO: Add this ability to autolinking |
| 77 | +// dependencies { implementation "com.google.android.gms:play-services-cast-framework:+" } |
| 78 | +function addGoogleCastImport(src, { version } = {}) { |
| 79 | + const newSrc = []; |
| 80 | + newSrc.push(` implementation "com.google.android.gms:play-services-cast-framework:${version || "+"}"`); |
| 81 | + return generateCode_1.mergeContents({ |
| 82 | + tag: "react-native-google-cast-dependencies", |
| 83 | + src, |
| 84 | + newSrc: newSrc.join("\n"), |
| 85 | + anchor: /dependencies(?:\s+)?\{/, |
| 86 | + offset: 1, |
| 87 | + comment: "//", |
| 88 | + }); |
| 89 | +} |
0 commit comments