-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathMainActivity.kt
More file actions
60 lines (49 loc) · 2 KB
/
MainActivity.kt
File metadata and controls
60 lines (49 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package com.foxdebug.acode
import android.content.Context
import android.os.Build
import android.os.Bundle
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.foxdebug.acode.plugins.NativeLayer
import com.getcapacitor.BridgeActivity
import kotlinx.coroutines.CoroutineScope
import java.lang.ref.WeakReference
class MainActivity : BridgeActivity() {
companion object {
private var activityRef: WeakReference<MainActivity?>? = WeakReference(null)
fun getActivityContext(): Context? {
return activityRef?.get()
}
val lifeCycleScope: CoroutineScope
get() {
return activityRef?.get()?.lifecycleScope ?: throw IllegalStateException("Activity is not available")
}
}
override fun onCreate(savedInstanceState: Bundle?) {
//register plugins before calling super
registerPlugin(NativeLayer::class.java)
super.onCreate(savedInstanceState)
activityRef = WeakReference(this)
// only apply insets for android 13+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
ViewCompat.setOnApplyWindowInsetsListener(window.decorView) { v, insets ->
val systemBarsInsets = insets.getInsets(WindowInsetsCompat.Type.systemBars())
val imeInsets = insets.getInsets(WindowInsetsCompat.Type.ime())
val isImeVisible = insets.isVisible(WindowInsetsCompat.Type.ime())
v.setPadding(
systemBarsInsets.left,
systemBarsInsets.top,
systemBarsInsets.right,
if (isImeVisible) imeInsets.bottom else systemBarsInsets.bottom
)
insets
}
}
}
override fun onDestroy() {
super.onDestroy()
activityRef = WeakReference(null)
activityRef = null
}
}