Skip to content

Commit cd1efaa

Browse files
fix: webview resize (#1947)
* fix: webview resize * Update hooks/post-process.js Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * Update src/plugins/system/android/com/foxdebug/system/SoftInputAssist.java Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> * feat: animations * feat: added support for keyboards with no animations * Update src/plugins/system/android/com/foxdebug/system/SoftInputAssist.java Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com> --------- Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
1 parent 47fe9bc commit cd1efaa

File tree

3 files changed

+126
-7
lines changed

3 files changed

+126
-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;|import org\.apache\.cordova\.\*;/,
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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.foxdebug.system;
2+
3+
import android.app.Activity;
4+
import android.view.View;
5+
import java.util.List;
6+
7+
import androidx.core.graphics.Insets;
8+
import androidx.core.view.ViewCompat;
9+
import androidx.core.view.WindowInsetsCompat;
10+
import androidx.core.view.WindowInsetsAnimationCompat;
11+
12+
public class SoftInputAssist {
13+
14+
private boolean animationRunning = false;
15+
16+
public SoftInputAssist(Activity activity) {
17+
View contentView = activity.findViewById(android.R.id.content);
18+
19+
ViewCompat.setOnApplyWindowInsetsListener(contentView, (v, insets) -> {
20+
21+
if (!animationRunning) {
22+
Insets ime = insets.getInsets(WindowInsetsCompat.Type.ime());
23+
Insets nav = insets.getInsets(WindowInsetsCompat.Type.navigationBars());
24+
25+
int keyboardHeight = Math.max(0, ime.bottom - nav.bottom);
26+
27+
v.setPadding(0, 0, 0, keyboardHeight);
28+
}
29+
30+
return insets;
31+
});
32+
33+
ViewCompat.setWindowInsetsAnimationCallback(
34+
contentView,
35+
new WindowInsetsAnimationCompat.Callback(
36+
WindowInsetsAnimationCompat.Callback.DISPATCH_MODE_CONTINUE_ON_SUBTREE
37+
) {
38+
39+
@Override
40+
public void onPrepare(WindowInsetsAnimationCompat animation) {
41+
animationRunning = true;
42+
}
43+
44+
@Override
45+
public void onEnd(WindowInsetsAnimationCompat animation) {
46+
animationRunning = false;
47+
}
48+
49+
@Override
50+
public WindowInsetsCompat onProgress(
51+
WindowInsetsCompat insets,
52+
List<WindowInsetsAnimationCompat> runningAnimations) {
53+
54+
Insets ime = insets.getInsets(WindowInsetsCompat.Type.ime());
55+
Insets nav = insets.getInsets(WindowInsetsCompat.Type.navigationBars());
56+
57+
int keyboardHeight = Math.max(0, ime.bottom - nav.bottom);
58+
59+
contentView.setPadding(contentView.getPaddingLeft(), contentView.getPaddingTop(), contentView.getPaddingRight(), keyboardHeight);
60+
61+
return insets;
62+
}
63+
}
64+
);
65+
}
66+
}

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)