()
| 211 | } |
| 212 | |
| 213 | function enableKeyboardWorkaround() { |
| 214 | try{ |
| 215 | const prefix = execSync('npm prefix').toString().trim(); |
| 216 | const packageName = getPackageName(); |
| 217 | const mainActivityPath = path.join( |
| 218 | prefix, |
| 219 | 'platforms/android/app/src/main/java', |
| 220 | packageName.replace(/\./g, '/'), |
| 221 | 'MainActivity.java' |
| 222 | ); |
| 223 | |
| 224 | if (!fs.existsSync(mainActivityPath)) { |
| 225 | console.warn('[Cordova Hook] ⚠️ MainActivity.java not found at', mainActivityPath); |
| 226 | return; |
| 227 | } |
| 228 | |
| 229 | let content = fs.readFileSync(mainActivityPath, 'utf-8'); |
| 230 | |
| 231 | // Skip if already patched |
| 232 | if (content.includes('SoftInputAssist')) { |
| 233 | console.log('[Cordova Hook] ✅ Keyboard workaround already enabled, skipping'); |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | // Add import |
| 238 | if (!content.includes('import com.foxdebug.system.SoftInputAssist;')) { |
| 239 | content = content.replace( |
| 240 | /import java.lang.ref.WeakReference;|import org\.apache\.cordova\.\*;/, |
| 241 | match => |
| 242 | match + '\nimport com.foxdebug.system.SoftInputAssist;' |
| 243 | ); |
| 244 | } |
| 245 | |
| 246 | // Declare field |
| 247 | if (!content.includes('private SoftInputAssist softInputAssist;')) { |
| 248 | content = content.replace( |
| 249 | /public class MainActivity extends CordovaActivity\s*\{/, |
| 250 | match => |
| 251 | match + |
| 252 | `\n\n private SoftInputAssist softInputAssist;\n` |
| 253 | ); |
| 254 | } |
| 255 | |
| 256 | // Initialize in onCreate |
| 257 | content = content.replace( |
| 258 | /loadUrl\(launchUrl\);/, |
| 259 | `loadUrl(launchUrl);\n\n softInputAssist = new SoftInputAssist(this);` |
| 260 | ); |
| 261 | |
| 262 | fs.writeFileSync(mainActivityPath, content, 'utf-8'); |
| 263 | console.log('[Cordova Hook] ✅ Enabled keyboard workaround'); |
| 264 | } catch (err) { |
| 265 | console.error('[Cordova Hook] ❌ Failed to enable keyboard workaround:', err.message); |
| 266 | } |
| 267 | } |
| 268 | |
| 269 | |
| 270 | /** |
no test coverage detected