also looking at this
feat(android): the app-state trio in Zig — ratchet 59 → 56
#184
6 files
+422
-3
| @@ -1,6 +1,8 @@ | ||
| 1 | 1 | package com.craft.runtime |
| 2 | 2 | |
| 3 | 3 | import android.app.Activity |
| 4 | import androidx.lifecycle.ProcessLifecycleOwner | |
| 5 | import androidx.lifecycle.LifecycleEventObserver | |
| 4 | 6 | import android.net.NetworkRequest |
| 5 | 7 | import android.net.NetworkCapabilities |
| 6 | 8 | import android.net.Network |
| @@ -147,6 +149,10 @@ object CraftNative { | ||
| 147 | 149 | private external fun nativeNetworkChanged(activity: Activity) |
| 148 | 150 | private external fun nativeStartNetworkMonitoring(activity: Activity): Boolean |
| 149 | 151 | 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? | |
| 150 | 156 | |
| 151 | 157 | /** |
| 152 | 158 | * `getDeviceInfo`, or null to use the Kotlin implementation. |
| @@ -848,4 +854,67 @@ object CraftNative { | ||
| 848 | 854 | false |
| 849 | 855 | } |
| 850 | 856 | } |
| 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 | } | |
| 851 | 920 | } |