const { AndroidConfig, withAndroidManifest, withMainActivity } = require("expo/config-plugins");
// FULL_USER respects rotation lock. Foldables change smallestScreenWidthDp without recreating the activity.
const ORIENTATION_METHODS = `
// Applied in onCreate and re-applied on fold/unfold; added by
// withAndroidTabletOrientation.
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
applyTabletOrientation()
}
private fun applyTabletOrientation() {
requestedOrientation = if (resources.configuration.smallestScreenWidthDp >= 600) {
ActivityInfo.SCREEN_ORIENTATION_FULL_USER
} else {
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
}
`;
const ORIENTATION_ON_CREATE_CALL = `
applyTabletOrientation()`;
function insertAfter(contents, anchor, insertion, description) {
const index = contents.indexOf(anchor);
if (index === -1) {
throw new Error(
`withAndroidTabletOrientation: could not find ${description} in MainActivity — the Expo template changed; update the plugin anchors.`,
);
}
const end = index + anchor.length;
return contents.slice(0, end) + insertion + contents.slice(end);
}
module.exports = function withAndroidTabletOrientation(config) {
config = withAndroidManifest(config, (nextConfig) => {
const mainActivity = AndroidConfig.Manifest.getMainActivityOrThrow(nextConfig.modResults);
delete mainActivity.$["android:screenOrientation"];
return nextConfig;
});
return withMainActivity(config, (nextConfig) => {
let contents = nextConfig.modResults.contents;
if (nextConfig.modResults.language !== "kt") {
throw new Error("withAndroidTabletOrientation: MainActivity must be Kotlin.");
}
if (!contents.includes("SCREEN_ORIENTATION_FULL_USER")) {
contents = insertAfter(
contents,
"import android.os.Bundle",
"\nimport android.content.pm.ActivityInfo\nimport android.content.res.Configuration",
"the android.os.Bundle import",
);
contents = insertAfter(
contents,
"class MainActivity : ReactActivity() {",
ORIENTATION_METHODS,
"the MainActivity class declaration",
);
}
// Relocate the call in existing native projects too, before splash setup and React content.
contents = contents.replace(
/override fun onCreate\(savedInstanceState: Bundle\?\) \{[\s\S]*?\n \}/,
(onCreate) => onCreate.replace(/\n[ \t]*applyTabletOrientation\(\)/g, ""),
);
contents = insertAfter(
contents,
"override fun onCreate(savedInstanceState: Bundle?) {",
ORIENTATION_ON_CREATE_CALL,
"the onCreate declaration",
);
nextConfig.modResults.contents = contents;
return nextConfig;
});
};