44
55package app.tauri.plugin
66
7- import android.app.PendingIntent
87import android.content.res.Configuration
98import android.content.Context
109import android.content.Intent
@@ -26,7 +25,7 @@ import com.fasterxml.jackson.databind.ObjectMapper
2625import com.fasterxml.jackson.databind.module.SimpleModule
2726import java.lang.reflect.InvocationTargetException
2827
29- class PluginManager ( val activity : AppCompatActivity ) {
28+ object PluginManager {
3029 fun interface RequestPermissionsCallback {
3130 fun onResult (permissions : Map <String , Boolean >)
3231 }
@@ -35,16 +34,33 @@ class PluginManager(val activity: AppCompatActivity) {
3534 fun onResult (result : ActivityResult )
3635 }
3736
37+ lateinit var activity: AppCompatActivity
3838 private val plugins: HashMap <String , PluginHandle > = HashMap ()
39- private val startActivityForResultLauncher: ActivityResultLauncher <Intent >
40- private val startIntentSenderForResultLauncher: ActivityResultLauncher <IntentSenderRequest >
41- private val requestPermissionsLauncher: ActivityResultLauncher <Array <String >>
39+ private lateinit var startActivityForResultLauncher: ActivityResultLauncher <Intent >
40+ private lateinit var startIntentSenderForResultLauncher: ActivityResultLauncher <IntentSenderRequest >
41+ private lateinit var requestPermissionsLauncher: ActivityResultLauncher <Array <String >>
4242 private var requestPermissionsCallback: RequestPermissionsCallback ? = null
4343 private var startActivityForResultCallback: ActivityResultCallback ? = null
4444 private var startIntentSenderForResultCallback: ActivityResultCallback ? = null
45- private var jsonMapper: ObjectMapper
45+ private var jsonMapper: ObjectMapper = ObjectMapper ()
46+ .disable(DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES )
47+ .enable(DeserializationFeature .FAIL_ON_NULL_FOR_PRIMITIVES )
48+ .setVisibility(PropertyAccessor .FIELD , JsonAutoDetect .Visibility .ANY )
4649
4750 init {
51+ val channelDeserializer = ChannelDeserializer ({ channelId, payload ->
52+ sendChannelData(channelId, payload)
53+ }, jsonMapper)
54+ jsonMapper
55+ .registerModule(SimpleModule ().addDeserializer(Channel ::class .java, channelDeserializer))
56+ }
57+
58+ fun onActivityCreate (activity : AppCompatActivity ) {
59+ // TODO: on destroy, we should change to a different activity
60+ if (::activity.isInitialized) {
61+ return
62+ }
63+ this .activity = activity
4864 startActivityForResultLauncher =
4965 activity.registerForActivityResult(ActivityResultContracts .StartActivityForResult ()
5066 ) { result ->
@@ -68,17 +84,6 @@ class PluginManager(val activity: AppCompatActivity) {
6884 requestPermissionsCallback!! .onResult(result)
6985 }
7086 }
71-
72- jsonMapper = ObjectMapper ()
73- .disable(DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES )
74- .enable(DeserializationFeature .FAIL_ON_NULL_FOR_PRIMITIVES )
75- .setVisibility(PropertyAccessor .FIELD , JsonAutoDetect .Visibility .ANY )
76-
77- val channelDeserializer = ChannelDeserializer ({ channelId, payload ->
78- sendChannelData(channelId, payload)
79- }, jsonMapper)
80- jsonMapper
81- .registerModule(SimpleModule ().addDeserializer(Channel ::class .java, channelDeserializer))
8287 }
8388
8489 fun onNewIntent (intent : Intent ) {
@@ -99,9 +104,9 @@ class PluginManager(val activity: AppCompatActivity) {
99104 }
100105 }
101106
102- fun onRestart () {
107+ fun onRestart (activity : AppCompatActivity ) {
103108 for (plugin in plugins.values) {
104- plugin.instance.onRestart( )
109+ plugin.instance.triggerOnRestart(activity )
105110 }
106111 }
107112
@@ -111,9 +116,9 @@ class PluginManager(val activity: AppCompatActivity) {
111116 }
112117 }
113118
114- fun onDestroy () {
119+ fun onDestroy (activity : AppCompatActivity ) {
115120 for (plugin in plugins.values) {
116- plugin.instance.onDestroy( )
121+ plugin.instance.triggerOnDestroy(activity )
117122 }
118123 }
119124
@@ -201,14 +206,12 @@ class PluginManager(val activity: AppCompatActivity) {
201206 }
202207 }
203208
204- companion object {
205- fun <T > loadConfig (context : Context , plugin : String , cls : Class <T >): T {
206- val tauriConfigJson = FsUtils .readAsset(context.assets, " tauri.conf.json" )
207- val mapper = ObjectMapper ()
208- .configure(DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false )
209- val config = mapper.readValue(tauriConfigJson, Config ::class .java)
210- return mapper.readValue(config.plugins[plugin].toString(), cls)
211- }
209+ fun <T > loadConfig (context : Context , plugin : String , cls : Class <T >): T {
210+ val tauriConfigJson = FsUtils .readAsset(context.assets, " tauri.conf.json" )
211+ val mapper = ObjectMapper ()
212+ .configure(DeserializationFeature .FAIL_ON_UNKNOWN_PROPERTIES , false )
213+ val config = mapper.readValue(tauriConfigJson, Config ::class .java)
214+ return mapper.readValue(config.plugins[plugin].toString(), cls)
212215 }
213216
214217 private external fun handlePluginResponse (id : Int , success : String? , error : String? )
0 commit comments