Skip to content

Commit 7406ca2

Browse files
authored
fix(mobile): keep Android tablet cold launches in landscape (#28825)
1 parent fab95f3 commit 7406ca2

2 files changed

Lines changed: 127 additions & 16 deletions

File tree

‎apps/mobile/plugins/withAndroidTabletOrientation.cjs‎

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { withMainActivity } = require("expo/config-plugins");
1+
const { AndroidConfig, withAndroidManifest, withMainActivity } = require("expo/config-plugins");
22

33
// FULL_USER respects rotation lock. Foldables change smallestScreenWidthDp without recreating the activity.
44

@@ -34,32 +34,41 @@ function insertAfter(contents, anchor, insertion, description) {
3434
}
3535

3636
module.exports = function withAndroidTabletOrientation(config) {
37+
config = withAndroidManifest(config, (nextConfig) => {
38+
const mainActivity = AndroidConfig.Manifest.getMainActivityOrThrow(nextConfig.modResults);
39+
delete mainActivity.$["android:screenOrientation"];
40+
return nextConfig;
41+
});
3742
return withMainActivity(config, (nextConfig) => {
3843
let contents = nextConfig.modResults.contents;
3944
if (nextConfig.modResults.language !== "kt") {
4045
throw new Error("withAndroidTabletOrientation: MainActivity must be Kotlin.");
4146
}
42-
if (contents.includes("SCREEN_ORIENTATION_FULL_USER")) {
43-
return nextConfig;
47+
if (!contents.includes("SCREEN_ORIENTATION_FULL_USER")) {
48+
contents = insertAfter(
49+
contents,
50+
"import android.os.Bundle",
51+
"\nimport android.content.pm.ActivityInfo\nimport android.content.res.Configuration",
52+
"the android.os.Bundle import",
53+
);
54+
contents = insertAfter(
55+
contents,
56+
"class MainActivity : ReactActivity() {",
57+
ORIENTATION_METHODS,
58+
"the MainActivity class declaration",
59+
);
4460
}
4561

46-
contents = insertAfter(
47-
contents,
48-
"import android.os.Bundle",
49-
"\nimport android.content.pm.ActivityInfo\nimport android.content.res.Configuration",
50-
"the android.os.Bundle import",
51-
);
52-
contents = insertAfter(
53-
contents,
54-
"class MainActivity : ReactActivity() {",
55-
ORIENTATION_METHODS,
56-
"the MainActivity class declaration",
62+
// Relocate the call in existing native projects too, before splash setup and React content.
63+
contents = contents.replace(
64+
/override fun onCreate\(savedInstanceState: Bundle\?\) \{[\s\S]*?\n \}/,
65+
(onCreate) => onCreate.replace(/\n[ \t]*applyTabletOrientation\(\)/g, ""),
5766
);
5867
contents = insertAfter(
5968
contents,
60-
"super.onCreate(null)",
69+
"override fun onCreate(savedInstanceState: Bundle?) {",
6170
ORIENTATION_ON_CREATE_CALL,
62-
"the super.onCreate call",
71+
"the onCreate declaration",
6372
);
6473

6574
nextConfig.modResults.contents = contents;
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import * as NodeFS from "node:fs";
2+
import * as NodeOS from "node:os";
3+
import * as NodePath from "node:path";
4+
import { AndroidConfig, compileModsAsync, XML } from "expo/config-plugins";
5+
import { afterEach, describe, expect, it } from "vite-plus/test";
6+
7+
import withAndroidTabletOrientation from "./withAndroidTabletOrientation.cjs";
8+
9+
const MANIFEST = `<manifest xmlns:android="http://schemas.android.com/apk/res/android">
10+
<application android:name=".MainApplication">
11+
<activity android:name=".MainActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenSize|screenLayout|uiMode|smallestScreenSize" android:exported="true">
12+
<intent-filter>
13+
<action android:name="android.intent.action.MAIN"/>
14+
<category android:name="android.intent.category.LAUNCHER"/>
15+
</intent-filter>
16+
</activity>
17+
</application>
18+
</manifest>`;
19+
20+
// Trimmed from the MainActivity.kt that `expo prebuild` generates for this app.
21+
const MAIN_ACTIVITY = `package com.t3tools.t3code
22+
23+
import android.os.Build
24+
import android.os.Bundle
25+
26+
import com.facebook.react.ReactActivity
27+
28+
class MainActivity : ReactActivity() {
29+
override fun onCreate(savedInstanceState: Bundle?) {
30+
// setTheme(R.style.AppTheme);
31+
super.onCreate(null)
32+
}
33+
34+
override fun getMainComponentName(): String = "main"
35+
}
36+
`;
37+
38+
const projectRoots: string[] = [];
39+
40+
afterEach(() => {
41+
for (const root of projectRoots.splice(0)) NodeFS.rmSync(root, { recursive: true, force: true });
42+
});
43+
44+
async function prebuildAndroid(input = { manifest: MANIFEST, mainActivity: MAIN_ACTIVITY }) {
45+
const projectRoot = NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "tablet-orientation-"));
46+
projectRoots.push(projectRoot);
47+
const sourceRoot = NodePath.join(projectRoot, "android/app/src/main");
48+
const activityPath = NodePath.join(sourceRoot, "java/com/t3tools/t3code/MainActivity.kt");
49+
const manifestPath = NodePath.join(sourceRoot, "AndroidManifest.xml");
50+
NodeFS.mkdirSync(NodePath.dirname(activityPath), { recursive: true });
51+
NodeFS.writeFileSync(manifestPath, input.manifest);
52+
NodeFS.writeFileSync(activityPath, input.mainActivity);
53+
54+
const config = withAndroidTabletOrientation({
55+
name: "T3 Code",
56+
slug: "t3code",
57+
orientation: "portrait",
58+
});
59+
// Prebuild registers Expo's orientation mod after app plugins. Mods run in
60+
// reverse order, so our plugin must remove the portrait lock Expo writes.
61+
await compileModsAsync(AndroidConfig.Orientation.withOrientation(config), {
62+
projectRoot,
63+
platforms: ["android"],
64+
});
65+
return {
66+
manifest: NodeFS.readFileSync(manifestPath, "utf8"),
67+
mainActivity: NodeFS.readFileSync(activityPath, "utf8"),
68+
};
69+
}
70+
71+
describe("withAndroidTabletOrientation", () => {
72+
it("generates an unlocked launcher manifest while preserving rotation and fold handling", async () => {
73+
const { manifest } = await prebuildAndroid();
74+
const mainActivity = AndroidConfig.Manifest.getMainActivityOrThrow(
75+
(await XML.parseXMLAsync(manifest)) as AndroidConfig.Manifest.AndroidManifest,
76+
);
77+
78+
expect(mainActivity.$["android:screenOrientation"]).toBeUndefined();
79+
expect(mainActivity.$["android:configChanges"]?.split("|")).toEqual(
80+
expect.arrayContaining(["orientation", "screenSize", "smallestScreenSize"]),
81+
);
82+
});
83+
84+
it("generates the phone/tablet policy before activity creation and stays stable on repeated prebuild", async () => {
85+
const once = await prebuildAndroid();
86+
87+
expect(once.mainActivity).toMatch(
88+
/override fun onCreate\(savedInstanceState: Bundle\?\)\s*\{[^}]*applyTabletOrientation\(\)[^}]*super\.onCreate\(null\)/,
89+
);
90+
expect(once.mainActivity).toMatch(
91+
/private fun applyTabletOrientation\(\)\s*\{\s*requestedOrientation = if \(resources\.configuration\.smallestScreenWidthDp >= 600\)\s*\{\s*ActivityInfo\.SCREEN_ORIENTATION_FULL_USER\s*\} else \{\s*ActivityInfo\.SCREEN_ORIENTATION_PORTRAIT\s*\}/,
92+
);
93+
expect(await prebuildAndroid(once)).toEqual(once);
94+
const existingActivity = once.mainActivity
95+
.replace(
96+
"override fun onCreate(savedInstanceState: Bundle?) {\n applyTabletOrientation()",
97+
"override fun onCreate(savedInstanceState: Bundle?) {",
98+
)
99+
.replace("super.onCreate(null)", "super.onCreate(null)\n applyTabletOrientation()");
100+
expect(await prebuildAndroid({ ...once, mainActivity: existingActivity })).toEqual(once);
101+
});
102+
});

0 commit comments

Comments
 (0)