diff --git a/hooks/post-process.js b/hooks/post-process.js index 85aef4009..21c411934 100644 --- a/hooks/post-process.js +++ b/hooks/post-process.js @@ -28,6 +28,7 @@ copyDirRecursively(localResPath, resPath); enableLegacyJni(); enableStaticContext(); patchTargetSdkVersion(); +enableKeyboardWorkaround(); function getTmpDir() { @@ -185,6 +186,57 @@ function enableStaticContext() { } } +function enableKeyboardWorkaround() { + try{ + const prefix = execSync('npm prefix').toString().trim(); + const mainActivityPath = path.join( + prefix, + 'platforms/android/app/src/main/java/com/foxdebug/acode/MainActivity.java' + ); + + if (!fs.existsSync(mainActivityPath)) { + return; + } + + let content = fs.readFileSync(mainActivityPath, 'utf-8'); + + // Skip if already patched + if (content.includes('SoftInputAssist')) { + return; + } + + // Add import + if (!content.includes('import com.foxdebug.system.SoftInputAssist;')) { + content = content.replace( + /import java.lang.ref.WeakReference;|import org\.apache\.cordova\.\*;/, + match => + match + '\nimport com.foxdebug.system.SoftInputAssist;' + ); + } + + // Declare field + if (!content.includes('private SoftInputAssist softInputAssist;')) { + content = content.replace( + /public class MainActivity extends CordovaActivity\s*\{/, + match => + match + + `\n\n private SoftInputAssist softInputAssist;\n` + ); + } + + // Initialize in onCreate + content = content.replace( + /loadUrl\(launchUrl\);/, + `loadUrl(launchUrl);\n\n softInputAssist = new SoftInputAssist(this);` + ); + + fs.writeFileSync(mainActivityPath, content, 'utf-8'); + console.log('[Cordova Hook] ✅ Enabled keyboard workaround'); + } catch (err) { + console.error('[Cordova Hook] ❌ Failed to enable keyboard workaround:', err.message); + } +} + /** * Copy directory recursively diff --git a/src/plugins/system/android/com/foxdebug/system/SoftInputAssist.java b/src/plugins/system/android/com/foxdebug/system/SoftInputAssist.java new file mode 100644 index 000000000..90b8d9ca4 --- /dev/null +++ b/src/plugins/system/android/com/foxdebug/system/SoftInputAssist.java @@ -0,0 +1,66 @@ +package com.foxdebug.system; + +import android.app.Activity; +import android.view.View; +import java.util.List; + +import androidx.core.graphics.Insets; +import androidx.core.view.ViewCompat; +import androidx.core.view.WindowInsetsCompat; +import androidx.core.view.WindowInsetsAnimationCompat; + +public class SoftInputAssist { + + private boolean animationRunning = false; + + public SoftInputAssist(Activity activity) { + View contentView = activity.findViewById(android.R.id.content); + + ViewCompat.setOnApplyWindowInsetsListener(contentView, (v, insets) -> { + + if (!animationRunning) { + Insets ime = insets.getInsets(WindowInsetsCompat.Type.ime()); + Insets nav = insets.getInsets(WindowInsetsCompat.Type.navigationBars()); + + int keyboardHeight = Math.max(0, ime.bottom - nav.bottom); + + v.setPadding(0, 0, 0, keyboardHeight); + } + + return insets; + }); + + ViewCompat.setWindowInsetsAnimationCallback( + contentView, + new WindowInsetsAnimationCompat.Callback( + WindowInsetsAnimationCompat.Callback.DISPATCH_MODE_CONTINUE_ON_SUBTREE + ) { + + @Override + public void onPrepare(WindowInsetsAnimationCompat animation) { + animationRunning = true; + } + + @Override + public void onEnd(WindowInsetsAnimationCompat animation) { + animationRunning = false; + } + + @Override + public WindowInsetsCompat onProgress( + WindowInsetsCompat insets, + List runningAnimations) { + + Insets ime = insets.getInsets(WindowInsetsCompat.Type.ime()); + Insets nav = insets.getInsets(WindowInsetsCompat.Type.navigationBars()); + + int keyboardHeight = Math.max(0, ime.bottom - nav.bottom); + + contentView.setPadding(contentView.getPaddingLeft(), contentView.getPaddingTop(), contentView.getPaddingRight(), keyboardHeight); + + return insets; + } + } + ); + } +} \ No newline at end of file diff --git a/src/plugins/system/plugin.xml b/src/plugins/system/plugin.xml index cca8cce88..caa34109f 100644 --- a/src/plugins/system/plugin.xml +++ b/src/plugins/system/plugin.xml @@ -37,10 +37,11 @@ - - - - - - - + + + + + + + +