1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const xml2js_1 = require("xml2js");
|
4 | const Strings_1 = require("./Strings");
|
5 | const facebookSchemeActivity = (scheme) => `
|
6 | <activity
|
7 | android:name="com.facebook.CustomTabActivity"
|
8 | android:exported="true">
|
9 | <intent-filter>
|
10 | <action android:name="android.intent.action.VIEW" />
|
11 | <category android:name="android.intent.category.DEFAULT" />
|
12 | <category android:name="android.intent.category.BROWSABLE" />
|
13 | <data android:scheme="${scheme}" />
|
14 | </intent-filter>
|
15 | </activity>
|
16 | `;
|
17 | function getFacebookScheme(config) {
|
18 | var _a;
|
19 | return _a = config.facebookScheme, (_a !== null && _a !== void 0 ? _a : null);
|
20 | }
|
21 | exports.getFacebookScheme = getFacebookScheme;
|
22 | function getFacebookAppId(config) {
|
23 | var _a;
|
24 | return _a = config.facebookAppId, (_a !== null && _a !== void 0 ? _a : null);
|
25 | }
|
26 | exports.getFacebookAppId = getFacebookAppId;
|
27 | function getFacebookDisplayName(config) {
|
28 | var _a;
|
29 | return _a = config.facebookDisplayName, (_a !== null && _a !== void 0 ? _a : null);
|
30 | }
|
31 | exports.getFacebookDisplayName = getFacebookDisplayName;
|
32 | function getFacebookAutoInitEnabled(config) {
|
33 | return config.hasOwnProperty('facebookAutoInitEnabled') ? config.facebookAutoInitEnabled : null;
|
34 | }
|
35 | exports.getFacebookAutoInitEnabled = getFacebookAutoInitEnabled;
|
36 | function getFacebookAutoLogAppEvents(config) {
|
37 | return config.hasOwnProperty('facebookAutoLogAppEventsEnabled')
|
38 | ? config.facebookAutoLogAppEventsEnabled
|
39 | : null;
|
40 | }
|
41 | exports.getFacebookAutoLogAppEvents = getFacebookAutoLogAppEvents;
|
42 | function getFacebookAdvertiserIDCollection(config) {
|
43 | return config.hasOwnProperty('facebookAdvertiserIDCollectionEnabled')
|
44 | ? config.facebookAdvertiserIDCollectionEnabled
|
45 | : null;
|
46 | }
|
47 | exports.getFacebookAdvertiserIDCollection = getFacebookAdvertiserIDCollection;
|
48 | async function setFacebookAppIdString(config, projectDirectory) {
|
49 | const appId = getFacebookAppId(config);
|
50 | if (!appId) {
|
51 | return false;
|
52 | }
|
53 | const stringsPath = await Strings_1.getProjectStringsXMLPathAsync(projectDirectory);
|
54 | if (!stringsPath) {
|
55 | throw new Error(`There was a problem setting your Facebook App ID in ${stringsPath}.`);
|
56 | }
|
57 | let stringsJSON = await Strings_1.readStringsXMLAsync(stringsPath);
|
58 | let stringItemToAdd = [{ _: appId, $: { name: 'facebook_app_id' } }];
|
59 | stringsJSON = Strings_1.setStringItem(stringItemToAdd, stringsJSON);
|
60 | try {
|
61 | await Strings_1.writeStringsXMLAsync(stringsPath, stringsJSON);
|
62 | }
|
63 | catch (e) {
|
64 | throw new Error(`Error setting facebookAppId. Cannot write strings.xml to ${stringsPath}.`);
|
65 | }
|
66 | return true;
|
67 | }
|
68 | exports.setFacebookAppIdString = setFacebookAppIdString;
|
69 | async function setFacebookConfig(config, manifestDocument) {
|
70 | const scheme = getFacebookScheme(config);
|
71 | const appId = getFacebookAppId(config);
|
72 | const displayName = getFacebookDisplayName(config);
|
73 | const autoInitEnabled = getFacebookAutoInitEnabled(config);
|
74 | const autoLogAppEvents = getFacebookAutoLogAppEvents(config);
|
75 | const advertiserIdCollection = getFacebookAdvertiserIDCollection(config);
|
76 | let mainApplication = manifestDocument.manifest.application.filter((e) => e['$']['android:name'] === '.MainApplication')[0];
|
77 | if (scheme) {
|
78 | const facebookSchemeActivityXML = facebookSchemeActivity(scheme);
|
79 | const parser = new xml2js_1.Parser();
|
80 | const facebookSchemeActivityJSON = await parser.parseStringPromise(facebookSchemeActivityXML);
|
81 |
|
82 | if (mainApplication.hasOwnProperty('activity')) {
|
83 | mainApplication['activity'] = mainApplication['activity'].concat(facebookSchemeActivityJSON['activity']);
|
84 | }
|
85 | else {
|
86 | mainApplication['activity'] = facebookSchemeActivityJSON['activity'];
|
87 | }
|
88 | }
|
89 | if (appId) {
|
90 | mainApplication = addMetaDataItemToMainApplication(mainApplication, 'com.facebook.sdk.ApplicationId', '@string/facebook_app_id'
|
91 | );
|
92 | }
|
93 | if (displayName) {
|
94 | mainApplication = addMetaDataItemToMainApplication(mainApplication, 'com.facebook.sdk.ApplicationName', displayName);
|
95 | }
|
96 | if (autoInitEnabled !== null) {
|
97 | mainApplication = addMetaDataItemToMainApplication(mainApplication, 'com.facebook.sdk.AutoInitEnabled', autoInitEnabled ? 'true' : 'false');
|
98 | }
|
99 | if (autoLogAppEvents !== null) {
|
100 | mainApplication = addMetaDataItemToMainApplication(mainApplication, 'com.facebook.sdk.AutoLogAppEventsEnabled', autoLogAppEvents ? 'true' : 'false');
|
101 | }
|
102 | if (advertiserIdCollection !== null) {
|
103 | mainApplication = addMetaDataItemToMainApplication(mainApplication, 'com.facebook.sdk.AdvertiserIDCollectionEnabled', advertiserIdCollection ? 'true' : 'false');
|
104 | }
|
105 | return manifestDocument;
|
106 | }
|
107 | exports.setFacebookConfig = setFacebookConfig;
|
108 | function addMetaDataItemToMainApplication(mainApplication, itemName, itemValue) {
|
109 | let existingMetaDataItem;
|
110 | const newItem = {
|
111 | $: {
|
112 | 'android:name': itemName,
|
113 | 'android:value': itemValue,
|
114 | },
|
115 | };
|
116 | if (mainApplication.hasOwnProperty('meta-data')) {
|
117 | existingMetaDataItem = mainApplication['meta-data'].filter((e) => e['$']['android:name'] === itemName);
|
118 | if (existingMetaDataItem.length) {
|
119 | existingMetaDataItem[0]['$']['android:value'] = itemValue;
|
120 | }
|
121 | else {
|
122 | mainApplication['meta-data'].push(newItem);
|
123 | }
|
124 | }
|
125 | else {
|
126 | mainApplication['meta-data'] = [newItem];
|
127 | }
|
128 | return mainApplication;
|
129 | }
|
130 |
|
\ | No newline at end of file |