Skip to content

Commit 9616fd6

Browse files
committed
feat: implement window insets handling for Android 13+
1 parent d0ca06d commit 9616fd6

File tree

2 files changed

+26
-6
lines changed

2 files changed

+26
-6
lines changed

android/app/src/main/java/com/foxdebug/acode/MainActivity.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.foxdebug.acode
22

33
import android.content.Context
4+
import android.os.Build
45
import android.os.Bundle
6+
import androidx.core.view.ViewCompat
7+
import androidx.core.view.WindowInsetsCompat
58
import androidx.lifecycle.lifecycleScope
69
import com.foxdebug.acode.plugins.NativeLayer
710
import com.getcapacitor.BridgeActivity
@@ -30,6 +33,23 @@ class MainActivity : BridgeActivity() {
3033

3134
super.onCreate(savedInstanceState)
3235
activityRef = WeakReference(this)
36+
37+
// only apply insets for android 13+
38+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
39+
ViewCompat.setOnApplyWindowInsetsListener(window.decorView) { v, insets ->
40+
val systemBarsInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars())
41+
val imeInsets = insets.getInsets(WindowInsetsCompat.Type.ime())
42+
val isImeVisible = insets.isVisible(WindowInsetsCompat.Type.ime())
43+
44+
v.setPadding(
45+
systemBarsInsets.left,
46+
systemBarsInsets.top,
47+
systemBarsInsets.right,
48+
if (isImeVisible) imeInsets.bottom else systemBarsInsets.bottom
49+
)
50+
insets
51+
}
52+
}
3353
}
3454

3555
override fun onDestroy() {

android/app/src/main/java/com/foxdebug/acode/plugins/NativeLayer.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ class NativeLayer : Plugin() {
2727
* Upon clicking "OK", the dialog will resolve the call.
2828
*/
2929
@PluginMethod
30-
fun showDialog(call: PluginCall) = with(call){
31-
autoRejectOnError(onFailure = {}){
32-
existsNot("title"){
30+
fun showDialog(call: PluginCall) = with(call) {
31+
autoRejectOnError(onFailure = {}) {
32+
existsNot("title") {
3333
return@with
3434
}
35-
existsNot("message"){
35+
existsNot("message") {
3636
return@with
3737
}
3838

@@ -48,7 +48,7 @@ class NativeLayer : Plugin() {
4848
AlertDialog.Builder(context).apply {
4949
setTitle(title)
5050
setMessage(message)
51-
setPositiveButton("OK",null)
51+
setPositiveButton("OK", null)
5252
show()
5353
}
5454
call.resolve()
@@ -84,7 +84,7 @@ class NativeLayer : Plugin() {
8484
*/
8585
@PluginMethod
8686
fun launchIntent(call: PluginCall) {
87-
call.autoRejectOnError(onFailure = {}){
87+
call.autoRejectOnError(onFailure = {}) {
8888
val constructorNumber = call.getInt("constructor_number") ?: run {
8989
call.reject("Missing 'constructor_number'")
9090
return

0 commit comments

Comments
 (0)