Skip to content

Commit 86cae63

Browse files
fix: webview resize
1 parent 47fe9bc commit 86cae63

File tree

3 files changed

+96
-7
lines changed

3 files changed

+96
-7
lines changed

hooks/post-process.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ copyDirRecursively(localResPath, resPath);
2828
enableLegacyJni();
2929
enableStaticContext();
3030
patchTargetSdkVersion();
31+
enableKeyboardWorkaround();
3132

3233

3334
function getTmpDir() {
@@ -185,6 +186,57 @@ function enableStaticContext() {
185186
}
186187
}
187188

189+
function enableKeyboardWorkaround() {
190+
try{
191+
const prefix = execSync('npm prefix').toString().trim();
192+
const mainActivityPath = path.join(
193+
prefix,
194+
'platforms/android/app/src/main/java/com/foxdebug/acode/MainActivity.java'
195+
);
196+
197+
if (!fs.existsSync(mainActivityPath)) {
198+
return;
199+
}
200+
201+
let content = fs.readFileSync(mainActivityPath, 'utf-8');
202+
203+
// Skip if already patched
204+
if (content.includes('SoftInputAssist')) {
205+
return;
206+
}
207+
208+
// Add import
209+
if (!content.includes('import com.foxdebug.system.SoftInputAssist;')) {
210+
content = content.replace(
211+
/import java.lang.ref.WeakReference;/,
212+
match =>
213+
match + '\nimport com.foxdebug.system.SoftInputAssist;'
214+
);
215+
}
216+
217+
// Declare field
218+
if (!content.includes('private SoftInputAssist softInputAssist;')) {
219+
content = content.replace(
220+
/public class MainActivity extends CordovaActivity\s*\{/,
221+
match =>
222+
match +
223+
`\n\n private SoftInputAssist softInputAssist;\n`
224+
);
225+
}
226+
227+
// Initialize in onCreate
228+
content = content.replace(
229+
/loadUrl\(launchUrl\);/,
230+
`loadUrl(launchUrl);\n\n softInputAssist = new SoftInputAssist(this);`
231+
);
232+
233+
fs.writeFileSync(mainActivityPath, content, 'utf-8');
234+
console.log('[Cordova Hook] ✅ Enabled keyboard workaround');
235+
} catch (err) {
236+
console.error('[Cordova Hook] ❌ Failed to enable keyboard workaround:', err.message);
237+
}
238+
}
239+
188240

189241
/**
190242
* Copy directory recursively
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.foxdebug.system;
2+
3+
import android.app.Activity;
4+
import android.graphics.Rect;
5+
import android.view.View;
6+
7+
public class SoftInputAssist {
8+
private final View rootView;
9+
private final View contentView;
10+
private int baseline = -1;
11+
12+
public SoftInputAssist(Activity activity) {
13+
rootView = activity.getWindow().getDecorView();
14+
contentView = activity.findViewById(android.R.id.content);
15+
16+
rootView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
17+
Rect r = new Rect();
18+
rootView.getWindowVisibleDisplayFrame(r);
19+
20+
int heightDiff = rootView.getHeight() - (r.bottom - r.top);
21+
22+
// Save baseline (system bars only)
23+
if (baseline == -1) {
24+
baseline = heightDiff;
25+
}
26+
27+
int keyboardHeight = heightDiff - baseline;
28+
29+
if (keyboardHeight > 0) {
30+
contentView.setPadding(0, 0, 0, keyboardHeight);
31+
} else {
32+
contentView.setPadding(0, 0, 0, 0);
33+
}
34+
});
35+
}
36+
}

src/plugins/system/plugin.xml

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@
3737
<resource-file src="res/android/icon.ttf" target="assets/font/icon.ttf" />
3838
<source-file src="android/com/foxdebug/system/Ui.java" target-dir="src/com/foxdebug/system"/>
3939
<source-file src="android/com/foxdebug/system/System.java" target-dir="src/com/foxdebug/system"/>
40-
41-
<framework src="androidx.core:core:1.6.0" />
42-
<framework src="androidx.core:core-google-shortcuts:1.0.0" />
43-
<framework src="androidx.documentfile:documentfile:1.0.1" />
44-
<source-file src="android/com/foxdebug/system/RewardPassManager.java" target-dir="src/com/foxdebug/system"/>
45-
</platform>
46-
</plugin>
40+
<source-file src="android/com/foxdebug/system/SoftInputAssist.java" target-dir="src/com/foxdebug/system"/>
41+
42+
<framework src="androidx.core:core:1.6.0" />
43+
<framework src="androidx.core:core-google-shortcuts:1.0.0" />
44+
<framework src="androidx.documentfile:documentfile:1.0.1" />
45+
<source-file src="android/com/foxdebug/system/RewardPassManager.java" target-dir="src/com/foxdebug/system"/>
46+
</platform>
47+
</plugin>

0 commit comments

Comments
 (0)