also looking at this
feat(android): the first listener shim, and network monitoring on it — 61 → 59
#183
5 files
+246
-13
| @@ -1,6 +1,11 @@ | ||
| 1 | 1 | package com.craft.runtime |
| 2 | 2 | |
| 3 | 3 | import android.app.Activity |
| 4 | import android.net.NetworkRequest | |
| 5 | import android.net.NetworkCapabilities | |
| 6 | import android.net.Network | |
| 7 | import android.net.ConnectivityManager | |
| 8 | import android.content.Context | |
| 4 | 9 | import androidx.core.content.ContextCompat |
| 5 | 10 | import android.content.Intent |
| 6 | 11 | import android.os.Handler |
| @@ -139,6 +144,9 @@ object CraftNative { | ||
| 139 | 144 | private external fun nativeStopLocationRecording(activity: Activity): String? |
| 140 | 145 | private external fun nativePauseLocationRecording(activity: Activity): String? |
| 141 | 146 | private external fun nativeResumeLocationRecording(activity: Activity): String? |
| 147 | private external fun nativeNetworkChanged(activity: Activity) | |
| 148 | private external fun nativeStartNetworkMonitoring(activity: Activity): Boolean | |
| 149 | private external fun nativeStopNetworkMonitoring(activity: Activity): Boolean | |
| 142 | 150 | |
| 143 | 151 | /** |
| 144 | 152 | * `getDeviceInfo`, or null to use the Kotlin implementation. |
| @@ -747,4 +755,97 @@ object CraftNative { | ||
| 747 | 755 | .setAction(LocationRecordingService.ACTION_STOP) |
| 748 | 756 | ) |
| 749 | 757 | } |
| 758 | ||
| 759 | // ==================== Network monitoring ==================== | |
| 760 | // | |
| 761 | // The first listener shim. `ConnectivityManager.NetworkCallback` is an | |
| 762 | // abstract Java class, and JNI cannot subclass one — `RegisterNatives` | |
| 763 | // binds methods onto a class that already exists in the APK, so a native | |
| 764 | // can be *called* from Java but cannot be a Java object of a type the | |
| 765 | // framework demands. | |
| 766 | // | |
| 767 | // The split is the one `runOnMain` uses: the object lives here, the work | |
| 768 | // lives in Zig. This class holds the callback and the registration; every | |
| 769 | // override forwards to `nativeNetworkChanged`, which reads the status and | |
| 770 | // sends the event. | |
| 771 | // | |
| 772 | // Unlike `runOnMain` there is no token, because there is only ever one | |
| 773 | // watch — which is also true of the CraftBridge field this replaces, and | |
| 774 | // is the reason both of them leak a second one (see the note on start). | |
| 775 | ||
| 776 | private var networkWatch: ConnectivityManager.NetworkCallback? = null | |
| 777 | ||
| 778 | /** | |
| 779 | * Register a network callback, exactly as `CraftBridge` would. | |
| 780 | * | |
| 781 | * Including the part that is wrong: a second call registers a second | |
| 782 | * callback and overwrites the reference to the first, which then runs | |
| 783 | * until the process dies and can never be unregistered. That is the | |
| 784 | * shim's behaviour and is reproduced rather than guarded, because guarding | |
| 785 | * would mean a page calling start twice gets one event per change here and | |
| 786 | * two on the Kotlin path. | |
| 787 | */ | |
| 788 | @JvmStatic | |
| 789 | fun startNetworkWatch(activity: Activity) { | |
| 790 | val manager = | |
| 791 | activity.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
| 792 | ||
| 793 | val watch = object : ConnectivityManager.NetworkCallback() { | |
| 794 | override fun onAvailable(network: Network) = changed(activity) | |
| 795 | override fun onLost(network: Network) = changed(activity) | |
| 796 | override fun onCapabilitiesChanged( | |
| 797 | network: Network, | |
| 798 | networkCapabilities: NetworkCapabilities | |
| 799 | ) = changed(activity) | |
| 800 | } | |
| 801 | ||
| 802 | val request = NetworkRequest.Builder() | |
| 803 | .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) | |
| 804 | .build() | |
| 805 | ||
| 806 | manager.registerNetworkCallback(request, watch) | |
| 807 | networkWatch = watch | |
| 808 | } | |
| 809 | ||
| 810 | @JvmStatic | |
| 811 | fun stopNetworkWatch(activity: Activity) { | |
| 812 | val manager = | |
| 813 | activity.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
| 814 | networkWatch?.let { manager.unregisterNetworkCallback(it) } | |
| 815 | networkWatch = null | |
| 816 | } | |
| 817 | ||
| 818 | /** | |
| 819 | * Every override lands here. | |
| 820 | * | |
| 821 | * This runs on a Binder thread, not the main one — which is fine, because | |
| 822 | * the reply channel attaches the thread it is called on and hops to the | |
| 823 | * main looper itself. | |
| 824 | */ | |
| 825 | private fun changed(activity: Activity) { | |
| 826 | try { | |
| 827 | nativeNetworkChanged(activity) | |
| 828 | } catch (e: UnsatisfiedLinkError) { | |
| 829 | // The library went away. There is no promise waiting on this and | |
| 830 | // nothing to retry. | |
| 831 | } | |
| 832 | } | |
| 833 | ||
| 834 | fun startNetworkMonitoring(activity: Activity): Boolean { | |
| 835 | if (!isAvailable) return false | |
| 836 | return try { | |
| 837 | nativeStartNetworkMonitoring(activity) | |
| 838 | } catch (e: UnsatisfiedLinkError) { | |
| 839 | false | |
| 840 | } | |
| 841 | } | |
| 842 | ||
| 843 | fun stopNetworkMonitoring(activity: Activity): Boolean { | |
| 844 | if (!isAvailable) return false | |
| 845 | return try { | |
| 846 | nativeStopNetworkMonitoring(activity) | |
| 847 | } catch (e: UnsatisfiedLinkError) { | |
| 848 | false | |
| 849 | } | |
| 850 | } | |
| 750 | 851 | } |