ReviewOS

also looking at this

craft-native/craft

feat(android): the app-state trio in Zig — ratchet 59 → 56

#184
Merged glennmichael123 wants to merge feat/android-app-state into main
6 files +422 -3
packages/android/templates/CraftNative.kt.templatemodified+69-0
Changes to packages/android/templates/CraftNative.kt.template
@@ -1,6 +1,8 @@
11package com.craft.runtime
22
33import android.app.Activity
4import androidx.lifecycle.ProcessLifecycleOwner
5import androidx.lifecycle.LifecycleEventObserver
46import android.net.NetworkRequest
57import android.net.NetworkCapabilities
68import android.net.Network
@@ -147,6 +149,10 @@ object CraftNative {
147149 private external fun nativeNetworkChanged(activity: Activity)
148150 private external fun nativeStartNetworkMonitoring(activity: Activity): Boolean
149151 private external fun nativeStopNetworkMonitoring(activity: Activity): Boolean
152 private external fun nativeAppStateEvent(eventName: String)
153 private external fun nativeStartAppStateMonitoring(activity: Activity): Boolean
154 private external fun nativeStopAppStateMonitoring(activity: Activity): Boolean
155 private external fun nativeGetAppState(): String?
150156
151157 /**
152158 * `getDeviceInfo`, or null to use the Kotlin implementation.
@@ -848,4 +854,67 @@ object CraftNative {
848854 false
849855 }
850856 }
857
858 // ==================== App state ====================
859 //
860 // The second listener shim. `LifecycleEventObserver` is a Java interface,
861 // so the object is here; the mapping, the state and the two deliveries are
862 // in Zig.
863 //
864 // One observer instance, added and removed — `LifecycleRegistry` keys its
865 // observers by identity, so adding twice is a no-op. That is why this one
866 // has no leak to reproduce, unlike the network watch.
867 //
868 // The native takes no Activity: everything it does is state and the reply
869 // channel, and neither needs one.
870
871 private val appStateObserver = LifecycleEventObserver { _, event ->
872 try {
873 nativeAppStateEvent(event.name)
874 } catch (e: UnsatisfiedLinkError) {
875 // The library went away. Nothing is waiting on this.
876 }
877 }
878
879 @JvmStatic
880 fun startAppStateWatch(activity: Activity) {
881 activity.runOnUiThread {
882 ProcessLifecycleOwner.get().lifecycle.addObserver(appStateObserver)
883 }
884 }
885
886 @JvmStatic
887 fun stopAppStateWatch(activity: Activity) {
888 activity.runOnUiThread {
889 ProcessLifecycleOwner.get().lifecycle.removeObserver(appStateObserver)
890 }
891 }
892
893 fun startAppStateMonitoring(activity: Activity): Boolean {
894 if (!isAvailable) return false
895 return try {
896 nativeStartAppStateMonitoring(activity)
897 } catch (e: UnsatisfiedLinkError) {
898 false
899 }
900 }
901
902 fun stopAppStateMonitoring(activity: Activity): Boolean {
903 if (!isAvailable) return false
904 return try {
905 nativeStopAppStateMonitoring(activity)
906 } catch (e: UnsatisfiedLinkError) {
907 false
908 }
909 }
910
911 /** Null is "Zig declined"; the state itself is never null. */
912 fun getAppState(): String? {
913 if (!isAvailable) return null
914 return try {
915 nativeGetAppState()
916 } catch (e: UnsatisfiedLinkError) {
917 null
918 }
919 }
851920}