@devries-cameron20 / t3code-1 / commits / 7406ca2981
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

fix(mobile): keep Android tablet cold launches in landscape (#28825)

Valerii Kuznietsov committed Nov 6, 2026 at 00:49 UTC 7406ca2981d01f2019bed4e8ab21c6705dc4a261
2 files changed +127 -16
apps/mobile/plugins/withAndroidTabletOrientation.cjs
+25 -16
@@ -1,4 +1,4 @@
1 -const { withMainActivity } = require("expo/config-plugins");
1 +const { AndroidConfig, withAndroidManifest, withMainActivity } = require("expo/config-plugins");
2
3 // FULL_USER respects rotation lock. Foldables change smallestScreenWidthDp without recreating the activity.
4
@@ -34,32 +34,41 @@ function insertAfter(contents, anchor, insertion, description) {
34 }
35
36 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 + });
42 return withMainActivity(config, (nextConfig) => {
43 let contents = nextConfig.modResults.contents;
44 if (nextConfig.modResults.language !== "kt") {
45 throw new Error("withAndroidTabletOrientation: MainActivity must be Kotlin.");
46 }
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 + );
60 }
61
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, ""),
66 );
67 contents = insertAfter(
68 contents,
60 - "super.onCreate(null)",
69 + "override fun onCreate(savedInstanceState: Bundle?) {",
70 ORIENTATION_ON_CREATE_CALL,
62 - "the super.onCreate call",
71 + "the onCreate declaration",
72 );
73
74 nextConfig.modResults.contents = contents;
apps/mobile/plugins/withAndroidTabletOrientation.test.ts new
+102
@@ -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 +});