Repository navigation
Expand file tree
/
Copy pathwithAndroidWindowDisplayMetrics.cjs
More file actions
87 lines (81 loc) · 3.01 KB
/
Copy pathwithAndroidWindowDisplayMetrics.cjs
File metadata and controls
87 lines (81 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const { withMainActivity } = require("expo/config-plugins");
// React Native measures text with application-display density, which differs
// from the activity on external displays. Expo constructs its root after
// onCreate, resetting those metrics; ReactHost resets them on configuration
// changes too. Rebind after both, before text is measured.
const DISPLAY_METRICS_METHODS = `
// Expo constructs the root view after onCreate; its constructor resets metrics.
override fun setContentView(view: View?) {
super.setContentView(view)
DisplayMetricsHolder.initDisplayMetrics(this)
}
`;
function insertAfter(contents, anchor, insertion, description) {
const index = contents.indexOf(anchor);
if (index === -1) {
throw new Error(
`withAndroidWindowDisplayMetrics: 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);
}
function addImport(contents, importLine) {
if (contents.includes(importLine)) {
return contents;
}
return insertAfter(
contents,
"import android.os.Bundle",
`\n${importLine}`,
"the android.os.Bundle import",
);
}
module.exports = function withAndroidWindowDisplayMetrics(config) {
return withMainActivity(config, (nextConfig) => {
let contents = nextConfig.modResults.contents;
if (nextConfig.modResults.language !== "kt") {
throw new Error("withAndroidWindowDisplayMetrics: MainActivity must be Kotlin.");
}
if (contents.includes("DisplayMetricsHolder.initDisplayMetrics(this)")) {
return nextConfig;
}
contents = addImport(contents, "import android.view.View");
contents = addImport(contents, "import com.facebook.react.uimanager.DisplayMetricsHolder");
contents = insertAfter(
contents,
"class MainActivity : ReactActivity() {",
DISPLAY_METRICS_METHODS,
"the MainActivity class declaration",
);
// Extend the tablet-orientation override, after ReactHost resets metrics.
const overrideStart = contents.indexOf(
"override fun onConfigurationChanged(newConfig: Configuration) {",
);
if (overrideStart !== -1) {
contents =
contents.slice(0, overrideStart) +
insertAfter(
contents.slice(overrideStart),
"super.onConfigurationChanged(newConfig)",
"\n DisplayMetricsHolder.initDisplayMetrics(this)",
"the super call in the onConfigurationChanged override",
);
} else {
contents = addImport(contents, "import android.content.res.Configuration");
contents = insertAfter(
contents,
"class MainActivity : ReactActivity() {",
`
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
DisplayMetricsHolder.initDisplayMetrics(this)
}
`,
"the MainActivity class declaration",
);
}
nextConfig.modResults.contents = contents;
return nextConfig;
});
};