Skip to content

Commit

Permalink
fix(expo): update scheme injection (#512)
Browse files Browse the repository at this point in the history
* removed appending of scheme to all intents as it's not required by the android sdk
* inject "<activity android:name="com.facebook.FacebookActivity" ... />" activity as per android SDK. Updated test case.
* executed "yarn build:plugin"
* style(lint): result of `yarn validate:prettier:fix`

---------

Co-authored-by: Andrej Pavlovic <andrej.pavlovic@massedge.com>
  • Loading branch information
mikehardy and andrejpavlovic authored Apr 10, 2024
1 parent 2038e5d commit 3b08371
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 21 deletions.
26 changes: 19 additions & 7 deletions plugin/build/withFacebookAndroid.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.setFacebookConfig = exports.withAndroidPermissions = exports.withFacebookManifest = exports.withFacebookAppIdString = void 0;
const config_1 = require("./config");
const config_plugins_1 = require("@expo/config-plugins");
const Scheme_1 = require("@expo/config-plugins/build/android/Scheme");
const { buildResourceItem } = config_plugins_1.AndroidConfig.Resources;
const { removeStringItem, setStringItem } = config_plugins_1.AndroidConfig.Strings;
const { addMetaDataItemToMainApplication, getMainApplicationOrThrow, prefixAndroidKeys, removeMetaDataItemFromMainApplication, } = config_plugins_1.AndroidConfig.Manifest;
const FACEBOOK_ACTIVITY = 'com.facebook.FacebookActivity';
const CUSTOM_TAB_ACTIVITY = 'com.facebook.CustomTabActivity';
const STRING_FACEBOOK_APP_ID = 'facebook_app_id';
const STRING_FB_LOGIN_PROTOCOL_SCHEME = 'fb_login_protocol_scheme';
Expand Down Expand Up @@ -48,7 +48,21 @@ function buildAndroidItem(datum) {
const head = prefixAndroidKeys(item);
return buildXMLItem({ head });
}
function getFacebookSchemeActivity() {
function getFacebookActivity() {
/**
<activity android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name" />
*/
return buildXMLItem({
head: prefixAndroidKeys({
name: FACEBOOK_ACTIVITY,
configChanges: 'keyboard|keyboardHidden|screenLayout|screenSize|orientation',
label: '@string/app_name'
}),
});
}
function getCustomTabActivity() {
/**
<activity
android:name="com.facebook.CustomTabActivity"
Expand Down Expand Up @@ -86,15 +100,16 @@ function ensureFacebookActivity({ mainApplication, scheme, }) {
if (Array.isArray(mainApplication.activity)) {
// Remove all Facebook CustomTabActivities first
mainApplication.activity = mainApplication.activity.filter((activity) => {
return activity.$?.['android:name'] !== CUSTOM_TAB_ACTIVITY;
return ![FACEBOOK_ACTIVITY, CUSTOM_TAB_ACTIVITY].includes(activity.$?.['android:name']);
});
}
else {
mainApplication.activity = [];
}
// If a new scheme is defined, append it to the activity.
if (scheme) {
mainApplication.activity.push(getFacebookSchemeActivity());
mainApplication.activity.push(getFacebookActivity());
mainApplication.activity.push(getCustomTabActivity());
}
return mainApplication;
}
Expand Down Expand Up @@ -139,9 +154,6 @@ function setFacebookConfig(props, androidManifest) {
const advertiserIdCollection = (0, config_1.getFacebookAdvertiserIDCollection)(props);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let mainApplication = getMainApplicationOrThrow(androidManifest);
if (scheme && !(0, Scheme_1.hasScheme)(scheme, androidManifest)) {
androidManifest = (0, Scheme_1.appendScheme)(scheme, androidManifest);
}
mainApplication = ensureFacebookActivity({ scheme, mainApplication });
if (appID) {
mainApplication = addMetaDataItemToMainApplication(mainApplication, META_APP_ID, `@string/${STRING_FACEBOOK_APP_ID}`);
Expand Down
7 changes: 6 additions & 1 deletion plugin/src/__tests__/withFacebookAndroid-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,15 @@ describe('Android facebook config', () => {
}

const facebookActivity = mainApplication.activity?.filter(
(e) => e.$['android:name'] === 'com.facebook.CustomTabActivity',
(e) => e.$['android:name'] === 'com.facebook.FacebookActivity',
);
expect(facebookActivity).toHaveLength(1);

const customTabActivity = mainApplication.activity?.filter(
(e) => e.$['android:name'] === 'com.facebook.CustomTabActivity',
);
expect(customTabActivity).toHaveLength(1);

const applicationId = mainApplication['meta-data']?.filter(
(e) => e.$['android:name'] === 'com.facebook.sdk.ApplicationId',
);
Expand Down
38 changes: 25 additions & 13 deletions plugin/src/withFacebookAndroid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,6 @@ import {
withAndroidManifest,
withStringsXml,
} from '@expo/config-plugins';
import {
appendScheme,
hasScheme,
} from '@expo/config-plugins/build/android/Scheme';

const {buildResourceItem} = AndroidConfig.Resources;
const {removeStringItem, setStringItem} = AndroidConfig.Strings;
Expand All @@ -28,6 +24,7 @@ const {
removeMetaDataItemFromMainApplication,
} = AndroidConfig.Manifest;

const FACEBOOK_ACTIVITY = 'com.facebook.FacebookActivity';
const CUSTOM_TAB_ACTIVITY = 'com.facebook.CustomTabActivity';
const STRING_FACEBOOK_APP_ID = 'facebook_app_id';
const STRING_FB_LOGIN_PROTOCOL_SCHEME = 'fb_login_protocol_scheme';
Expand Down Expand Up @@ -79,18 +76,34 @@ function buildXMLItem({
children,
}: {
head: Record<string, string>;
children?: Record<string, string | any[]>;
children?: Record<string, string | unknown[]>;
}) {
return {...(children ?? {}), $: head};
}

function buildAndroidItem(datum: string | Record<string, any>) {
function buildAndroidItem(datum: string | Record<string, unknown>) {
const item = typeof datum === 'string' ? {name: datum} : datum;
const head = prefixAndroidKeys(item);
return buildXMLItem({head});
}

function getFacebookSchemeActivity() {
function getFacebookActivity() {
/**
<activity android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="@string/app_name" />
*/
return buildXMLItem({
head: prefixAndroidKeys({
name: FACEBOOK_ACTIVITY,
configChanges:
'keyboard|keyboardHidden|screenLayout|screenSize|orientation',
label: '@string/app_name',
}),
}) as AndroidConfig.Manifest.ManifestActivity;
}

function getCustomTabActivity() {
/**
<activity
android:name="com.facebook.CustomTabActivity"
Expand Down Expand Up @@ -135,15 +148,18 @@ function ensureFacebookActivity({
if (Array.isArray(mainApplication.activity)) {
// Remove all Facebook CustomTabActivities first
mainApplication.activity = mainApplication.activity.filter((activity) => {
return activity.$?.['android:name'] !== CUSTOM_TAB_ACTIVITY;
return ![FACEBOOK_ACTIVITY, CUSTOM_TAB_ACTIVITY].includes(
activity.$?.['android:name'],
);
});
} else {
mainApplication.activity = [];
}

// If a new scheme is defined, append it to the activity.
if (scheme) {
mainApplication.activity.push(getFacebookSchemeActivity());
mainApplication.activity.push(getFacebookActivity());
mainApplication.activity.push(getCustomTabActivity());
}
return mainApplication;
}
Expand Down Expand Up @@ -219,10 +235,6 @@ export function setFacebookConfig(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
let mainApplication = getMainApplicationOrThrow(androidManifest);

if (scheme && !hasScheme(scheme, androidManifest)) {
androidManifest = appendScheme(scheme, androidManifest);
}

mainApplication = ensureFacebookActivity({scheme, mainApplication});

if (appID) {
Expand Down

0 comments on commit 3b08371

Please sign in to comment.