@solidnative-dev/fabric-host 0.0.0-stage → 0.1.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +87 -2
- package/dist/fatal-error-handler.d.ts +27 -0
- package/dist/fatal-error-handler.d.ts.map +1 -0
- package/dist/fatal-error-handler.js +106 -0
- package/dist/fatal-error-handler.js.map +1 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/native-binding.d.ts +183 -0
- package/dist/native-binding.d.ts.map +1 -0
- package/dist/native-binding.js +554 -0
- package/dist/native-binding.js.map +1 -0
- package/dist/native-fabric-host.d.ts +32 -0
- package/dist/native-fabric-host.d.ts.map +1 -0
- package/dist/native-fabric-host.js +275 -0
- package/dist/native-fabric-host.js.map +1 -0
- package/dist/native-transaction.d.ts +17 -0
- package/dist/native-transaction.d.ts.map +1 -0
- package/dist/native-transaction.js +124 -0
- package/dist/native-transaction.js.map +1 -0
- package/dist/react-native-0.88.d.ts +77 -0
- package/dist/react-native-0.88.d.ts.map +1 -0
- package/dist/react-native-0.88.js +79 -0
- package/dist/react-native-0.88.js.map +1 -0
- package/dist/surface-readiness.d.ts +18 -0
- package/dist/surface-readiness.d.ts.map +1 -0
- package/dist/surface-readiness.js +43 -0
- package/dist/surface-readiness.js.map +1 -0
- package/native/README.md +502 -0
- package/native/SolidNativeFabric.podspec +84 -0
- package/native/android/SolidNativeBindingsInstaller.cpp +4218 -0
- package/native/android/SolidNativeBindingsInstaller.h +8 -0
- package/native/android/SolidNativeRuntime.cmake +61 -0
- package/native/android/java/dev/solidnative/runtime/SolidNativeBindingsInstaller.kt +239 -0
- package/native/android/java/dev/solidnative/runtime/SolidNativeDebugModule.kt +191 -0
- package/native/android/java/dev/solidnative/runtime/SolidNativePackage.kt +43 -0
- package/native/android/java/dev/solidnative/runtime/SolidNativePlatformModule.kt +81 -0
- package/native/android/java/dev/solidnative/runtime/SolidNativeStartupFailureView.kt +93 -0
- package/native/android/java/dev/solidnative/runtime/SolidNativeSurface.kt +423 -0
- package/native/android/solid-native.gradle +90 -0
- package/native/apple/SolidNativeDebugRequest.mm +421 -0
- package/native/fabric/SolidNativeDebugRequest.h +12 -0
- package/native/fabric/SolidNativeDirtyNodes.h +26 -0
- package/native/fabric/SolidNativeFabricApi.cpp +224 -0
- package/native/fabric/SolidNativeFabricApi.h +107 -0
- package/native/fabric/SolidNativeFabricApplication.h +32 -0
- package/native/fabric/SolidNativeFabricApplication.mm +373 -0
- package/native/fabric/SolidNativeFabricJSIBinding.h +43 -0
- package/native/fabric/SolidNativeFabricJSIBinding.mm +4491 -0
- package/native/fabric/SolidNativeFabricSmokeApplication.h +26 -0
- package/native/fabric/SolidNativeFabricSmokeApplication.mm +331 -0
- package/native/fabric/SolidNativeFabricSurface.h +63 -0
- package/native/fabric/SolidNativeFabricSurface.mm +549 -0
- package/native/fabric/SolidNativeFabricTransactionCoordinator.cpp +1275 -0
- package/native/fabric/SolidNativeFabricTransactionCoordinator.h +90 -0
- package/native/fabric/SolidNativeFabricTransactionCoordinatorApple.mm +97 -0
- package/native/fabric/SolidNativeRootViewController.h +20 -0
- package/native/fabric/SolidNativeRootViewController.mm +83 -0
- package/native/fabric/SolidNativeStagedNodeMap.h +104 -0
- package/native/fabric/SolidNativeStartupFailureView.h +17 -0
- package/native/fabric/SolidNativeStartupFailureView.mm +77 -0
- package/native/react-native-boundary.json +106 -0
- package/native/worklets/SolidNativeUIWorklet.cpp +1080 -0
- package/native/worklets/SolidNativeUIWorklet.h +245 -0
- package/package.json +41 -3
- package/scripts/verify-react-native-boundary.mjs +282 -0
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
package dev.solidnative.runtime
|
|
2
|
+
|
|
3
|
+
import android.app.Activity
|
|
4
|
+
import android.content.pm.ApplicationInfo
|
|
5
|
+
import android.graphics.Color
|
|
6
|
+
import android.os.Looper
|
|
7
|
+
import android.view.Gravity
|
|
8
|
+
import android.widget.TextView
|
|
9
|
+
|
|
10
|
+
/** Renders a dependency-free terminal startup fallback after Fabric cannot own the screen. */
|
|
11
|
+
object SolidNativeStartupFailureView {
|
|
12
|
+
@JvmStatic
|
|
13
|
+
@JvmOverloads
|
|
14
|
+
fun show(activity: Activity, error: Throwable? = null) {
|
|
15
|
+
render(
|
|
16
|
+
activity,
|
|
17
|
+
error?.javaClass?.simpleName?.ifEmpty { "Error" },
|
|
18
|
+
error?.message,
|
|
19
|
+
)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
internal fun show(activity: Activity, name: String, message: String) {
|
|
23
|
+
render(activity, name, message)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
private fun render(activity: Activity, name: String?, message: String?) {
|
|
27
|
+
check(Looper.myLooper() === Looper.getMainLooper()) {
|
|
28
|
+
"SolidNativeStartupFailureView.show must run on the Android main thread."
|
|
29
|
+
}
|
|
30
|
+
val debuggable =
|
|
31
|
+
activity.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE != 0
|
|
32
|
+
val message =
|
|
33
|
+
if (debuggable && name != null && message != null) {
|
|
34
|
+
"$GENERIC_MESSAGE\n\n${boundedDiagnostic(name, message)}"
|
|
35
|
+
} else {
|
|
36
|
+
GENERIC_MESSAGE
|
|
37
|
+
}
|
|
38
|
+
val density = activity.resources.displayMetrics.density
|
|
39
|
+
val padding = (32 * density).toInt()
|
|
40
|
+
val view =
|
|
41
|
+
TextView(activity).apply {
|
|
42
|
+
text = message
|
|
43
|
+
contentDescription = message
|
|
44
|
+
gravity = Gravity.CENTER
|
|
45
|
+
setPadding(padding, padding, padding, padding)
|
|
46
|
+
setTextColor(Color.rgb(17, 24, 39))
|
|
47
|
+
setBackgroundColor(Color.rgb(247, 248, 250))
|
|
48
|
+
textSize = 18f
|
|
49
|
+
isFocusable = true
|
|
50
|
+
}
|
|
51
|
+
activity.setContentView(view)
|
|
52
|
+
view.requestFocus()
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
private fun boundedDiagnostic(rawName: String, rawMessage: String): String {
|
|
56
|
+
val name = sanitize(rawName.ifEmpty { "Error" }, MAX_NAME_LENGTH)
|
|
57
|
+
val maximumMessageLength =
|
|
58
|
+
MAX_DIAGNOSTIC_LENGTH - name.length - DIAGNOSTIC_SEPARATOR.length
|
|
59
|
+
val message =
|
|
60
|
+
sanitize(
|
|
61
|
+
rawMessage.ifEmpty { "No diagnostic message was provided." },
|
|
62
|
+
maximumMessageLength,
|
|
63
|
+
)
|
|
64
|
+
return "$name$DIAGNOSTIC_SEPARATOR$message"
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
private fun sanitize(value: String, maximumLength: Int): String {
|
|
68
|
+
val truncated = value.length > maximumLength
|
|
69
|
+
val contentLength = if (truncated) maximumLength - 1 else maximumLength
|
|
70
|
+
val normalized =
|
|
71
|
+
buildString(minOf(value.length, maximumLength)) {
|
|
72
|
+
for (character in value) {
|
|
73
|
+
if (length >= contentLength) break
|
|
74
|
+
append(
|
|
75
|
+
if (character == '\n' || character == '\t' || !character.isISOControl()) {
|
|
76
|
+
character
|
|
77
|
+
} else {
|
|
78
|
+
' '
|
|
79
|
+
},
|
|
80
|
+
)
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
if (!truncated) return normalized
|
|
84
|
+
val safePrefix =
|
|
85
|
+
if (normalized.lastOrNull()?.isHighSurrogate() == true) normalized.dropLast(1) else normalized
|
|
86
|
+
return "$safePrefix…"
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
private const val GENERIC_MESSAGE = "Solid Native could not start."
|
|
90
|
+
private const val DIAGNOSTIC_SEPARATOR = ": "
|
|
91
|
+
private const val MAX_NAME_LENGTH = 128
|
|
92
|
+
private const val MAX_DIAGNOSTIC_LENGTH = 2_048
|
|
93
|
+
}
|
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
package dev.solidnative.runtime
|
|
2
|
+
|
|
3
|
+
import android.app.Activity
|
|
4
|
+
import android.content.pm.ApplicationInfo
|
|
5
|
+
import android.os.Looper
|
|
6
|
+
import android.util.Log
|
|
7
|
+
import android.view.MotionEvent
|
|
8
|
+
import android.view.View
|
|
9
|
+
import android.view.VelocityTracker
|
|
10
|
+
import androidx.core.os.ConfigurationCompat
|
|
11
|
+
import androidx.core.text.TextUtilsCompat
|
|
12
|
+
import com.facebook.react.ReactHost
|
|
13
|
+
import com.facebook.react.interfaces.TaskInterface
|
|
14
|
+
import com.facebook.react.interfaces.fabric.ReactSurface
|
|
15
|
+
import com.facebook.react.modules.i18nmanager.I18nUtil
|
|
16
|
+
import java.util.Locale
|
|
17
|
+
|
|
18
|
+
fun interface SolidNativeSurfaceStatusHandler {
|
|
19
|
+
fun onStatus(status: String, error: Throwable?)
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Owns the package-supported, full-screen Android Fabric surface and its native UI worklet hooks.
|
|
24
|
+
* Start and stop this object on the Android main thread.
|
|
25
|
+
*/
|
|
26
|
+
class SolidNativeSurface private constructor(
|
|
27
|
+
private val activity: Activity,
|
|
28
|
+
private val bindingsInstaller: SolidNativeBindingsInstaller,
|
|
29
|
+
private val surface: ReactSurface,
|
|
30
|
+
private val statusHandler: SolidNativeSurfaceStatusHandler?,
|
|
31
|
+
) {
|
|
32
|
+
private val rootView = checkNotNull(surface.view) { "React Native did not create a surface view." }
|
|
33
|
+
private val uiWorkletPans = mutableMapOf<Long, UIWorkletPanAttachment>()
|
|
34
|
+
private var stopping = false
|
|
35
|
+
private var stopped = false
|
|
36
|
+
private var terminalFailureRequested = false
|
|
37
|
+
|
|
38
|
+
val view: View
|
|
39
|
+
get() = rootView
|
|
40
|
+
|
|
41
|
+
val surfaceId: Int
|
|
42
|
+
get() = surface.surfaceID
|
|
43
|
+
|
|
44
|
+
fun stop() {
|
|
45
|
+
requireMainThread("stop")
|
|
46
|
+
beginStop()
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
private fun start() {
|
|
50
|
+
bindingsInstaller.setSurfaceStopHandler(::stopFromHost)
|
|
51
|
+
bindingsInstaller.setFatalErrorHandler(::showFatalFromHost)
|
|
52
|
+
bindingsInstaller.setUIWorkletOutputHandler(::applyUIWorkletFrame)
|
|
53
|
+
bindingsInstaller.setUIWorkletPanGestureHandlers(
|
|
54
|
+
::attachUIWorkletPanGesture,
|
|
55
|
+
::detachUIWorkletPanGesture,
|
|
56
|
+
)
|
|
57
|
+
activity.setContentView(rootView)
|
|
58
|
+
val startTask =
|
|
59
|
+
try {
|
|
60
|
+
surface.start()
|
|
61
|
+
} catch (error: Throwable) {
|
|
62
|
+
reportStatus("surface-start-failed", error)
|
|
63
|
+
beginStop()
|
|
64
|
+
return
|
|
65
|
+
}
|
|
66
|
+
waitForStart(startTask, 0)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
private fun waitForStart(task: TaskInterface<Void>, attempt: Int) {
|
|
70
|
+
if (stopping) return
|
|
71
|
+
if (task.isCompleted()) {
|
|
72
|
+
val error = task.getError()
|
|
73
|
+
if (task.isCancelled() || task.isFaulted() || error != null) {
|
|
74
|
+
val failure =
|
|
75
|
+
error
|
|
76
|
+
?: IllegalStateException(
|
|
77
|
+
if (task.isCancelled()) {
|
|
78
|
+
"React Native cancelled the Solid Native Fabric surface start."
|
|
79
|
+
} else {
|
|
80
|
+
"React Native failed the Solid Native Fabric surface start."
|
|
81
|
+
},
|
|
82
|
+
)
|
|
83
|
+
reportStatus("surface-start-failed", failure)
|
|
84
|
+
beginStop()
|
|
85
|
+
return
|
|
86
|
+
}
|
|
87
|
+
reportStatus("surface-started", null)
|
|
88
|
+
publishSurfaceWhenReady(0)
|
|
89
|
+
return
|
|
90
|
+
}
|
|
91
|
+
if (attempt >= MAX_TASK_ATTEMPTS) {
|
|
92
|
+
val error =
|
|
93
|
+
IllegalStateException(
|
|
94
|
+
"React Native did not start the Solid Native Fabric surface within ten seconds.",
|
|
95
|
+
)
|
|
96
|
+
reportStatus("surface-start-failed", error)
|
|
97
|
+
beginStop()
|
|
98
|
+
return
|
|
99
|
+
}
|
|
100
|
+
rootView.postDelayed(
|
|
101
|
+
{ waitForStart(task, attempt + 1) },
|
|
102
|
+
TASK_RETRY_MILLISECONDS,
|
|
103
|
+
)
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
private fun publishSurfaceWhenReady(attempt: Int) {
|
|
107
|
+
if (stopping) return
|
|
108
|
+
val currentSurfaceId = surface.surfaceID
|
|
109
|
+
if (currentSurfaceId > 0) {
|
|
110
|
+
try {
|
|
111
|
+
bindingsInstaller.publishSurface(currentSurfaceId)
|
|
112
|
+
reportStatus("surface-ready", null)
|
|
113
|
+
} catch (error: Throwable) {
|
|
114
|
+
reportStatus("surface-publish-failed", error)
|
|
115
|
+
beginStop()
|
|
116
|
+
}
|
|
117
|
+
return
|
|
118
|
+
}
|
|
119
|
+
if (attempt >= MAX_TASK_ATTEMPTS) {
|
|
120
|
+
val error =
|
|
121
|
+
IllegalStateException(
|
|
122
|
+
"Fabric did not allocate a Solid Native surface within ten seconds.",
|
|
123
|
+
)
|
|
124
|
+
reportStatus("surface-publish-failed", error)
|
|
125
|
+
beginStop()
|
|
126
|
+
return
|
|
127
|
+
}
|
|
128
|
+
rootView.postDelayed(
|
|
129
|
+
{ publishSurfaceWhenReady(attempt + 1) },
|
|
130
|
+
TASK_RETRY_MILLISECONDS,
|
|
131
|
+
)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
private fun stopFromHost(requestedSurfaceId: Int) {
|
|
135
|
+
if (requestedSurfaceId != surface.surfaceID) return
|
|
136
|
+
beginStop()
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
private fun showFatalFromHost(
|
|
140
|
+
requestedSurfaceId: Int,
|
|
141
|
+
name: String,
|
|
142
|
+
message: String,
|
|
143
|
+
) {
|
|
144
|
+
if (
|
|
145
|
+
requestedSurfaceId != surface.surfaceID ||
|
|
146
|
+
stopping ||
|
|
147
|
+
stopped ||
|
|
148
|
+
terminalFailureRequested
|
|
149
|
+
) {
|
|
150
|
+
return
|
|
151
|
+
}
|
|
152
|
+
terminalFailureRequested = true
|
|
153
|
+
val failure = IllegalStateException("$name: $message")
|
|
154
|
+
reportStatus("runtime-failed", failure)
|
|
155
|
+
beginStop()
|
|
156
|
+
SolidNativeStartupFailureView.show(activity, name, message)
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
private fun beginStop() {
|
|
160
|
+
if (stopping || stopped) return
|
|
161
|
+
stopping = true
|
|
162
|
+
bindingsInstaller.setSurfaceStopHandler(null)
|
|
163
|
+
bindingsInstaller.setFatalErrorHandler(null)
|
|
164
|
+
bindingsInstaller.setUIWorkletOutputHandler(null)
|
|
165
|
+
bindingsInstaller.setUIWorkletPanGestureHandlers(null, null)
|
|
166
|
+
clearUIWorkletPanGestures()
|
|
167
|
+
|
|
168
|
+
val currentSurfaceId = surface.surfaceID
|
|
169
|
+
var initialFailure: Throwable? = null
|
|
170
|
+
if (currentSurfaceId > 0) {
|
|
171
|
+
try {
|
|
172
|
+
bindingsInstaller.deactivateSurface(currentSurfaceId)
|
|
173
|
+
} catch (error: Throwable) {
|
|
174
|
+
initialFailure = error
|
|
175
|
+
Log.e(TAG, "Failed to deactivate Solid Native surface work.", error)
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
val stopTask =
|
|
180
|
+
try {
|
|
181
|
+
surface.stop()
|
|
182
|
+
} catch (error: Throwable) {
|
|
183
|
+
finishStop(mergeFailure(initialFailure, error))
|
|
184
|
+
return
|
|
185
|
+
}
|
|
186
|
+
waitForStop(stopTask, 0, initialFailure)
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
private fun waitForStop(
|
|
190
|
+
task: TaskInterface<Void>,
|
|
191
|
+
attempt: Int,
|
|
192
|
+
initialFailure: Throwable?,
|
|
193
|
+
) {
|
|
194
|
+
if (task.isCompleted()) {
|
|
195
|
+
val taskFailure =
|
|
196
|
+
task.getError()
|
|
197
|
+
?: if (task.isCancelled()) {
|
|
198
|
+
IllegalStateException("React Native cancelled the Fabric surface stop.")
|
|
199
|
+
} else if (task.isFaulted()) {
|
|
200
|
+
IllegalStateException("React Native failed the Fabric surface stop.")
|
|
201
|
+
} else {
|
|
202
|
+
null
|
|
203
|
+
}
|
|
204
|
+
finishStop(mergeFailure(initialFailure, taskFailure))
|
|
205
|
+
return
|
|
206
|
+
}
|
|
207
|
+
if (attempt >= MAX_TASK_ATTEMPTS) {
|
|
208
|
+
finishStop(
|
|
209
|
+
mergeFailure(
|
|
210
|
+
initialFailure,
|
|
211
|
+
IllegalStateException(
|
|
212
|
+
"React Native did not stop the Solid Native Fabric surface within ten seconds.",
|
|
213
|
+
),
|
|
214
|
+
),
|
|
215
|
+
)
|
|
216
|
+
return
|
|
217
|
+
}
|
|
218
|
+
rootView.postDelayed(
|
|
219
|
+
{ waitForStop(task, attempt + 1, initialFailure) },
|
|
220
|
+
TASK_RETRY_MILLISECONDS,
|
|
221
|
+
)
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
private fun finishStop(initialFailure: Throwable?) {
|
|
225
|
+
var failure = initialFailure
|
|
226
|
+
try {
|
|
227
|
+
surface.clear()
|
|
228
|
+
} catch (error: Throwable) {
|
|
229
|
+
failure = mergeFailure(failure, error)
|
|
230
|
+
}
|
|
231
|
+
try {
|
|
232
|
+
surface.detach()
|
|
233
|
+
} catch (error: Throwable) {
|
|
234
|
+
failure = mergeFailure(failure, error)
|
|
235
|
+
}
|
|
236
|
+
val currentSurfaceId = surface.surfaceID
|
|
237
|
+
if (currentSurfaceId > 0) {
|
|
238
|
+
try {
|
|
239
|
+
bindingsInstaller.clearSurface(currentSurfaceId)
|
|
240
|
+
} catch (error: Throwable) {
|
|
241
|
+
failure = mergeFailure(failure, error)
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
stopped = true
|
|
245
|
+
reportStatus(if (failure == null) "surface-stopped" else "surface-stop-failed", failure)
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
private fun applyUIWorkletFrame(frame: UIWorkletFrame): Boolean {
|
|
249
|
+
if (stopping || stopped) return false
|
|
250
|
+
val target = activity.window.decorView.findViewById<View>(frame.targetTag) ?: return false
|
|
251
|
+
if (!target.isAttachedToWindow) return false
|
|
252
|
+
val values =
|
|
253
|
+
doubleArrayOf(
|
|
254
|
+
frame.opacity,
|
|
255
|
+
frame.translateX,
|
|
256
|
+
frame.translateY,
|
|
257
|
+
frame.scaleX,
|
|
258
|
+
frame.scaleY,
|
|
259
|
+
frame.rotation,
|
|
260
|
+
)
|
|
261
|
+
if (values.any { !it.isNaN() && !it.isFinite() }) return false
|
|
262
|
+
if (!frame.opacity.isNaN() && frame.opacity !in 0.0..1.0) return false
|
|
263
|
+
|
|
264
|
+
val density = activity.resources.displayMetrics.density
|
|
265
|
+
if (!frame.opacity.isNaN()) target.alpha = frame.opacity.toFloat()
|
|
266
|
+
if (!frame.translateX.isNaN()) target.translationX = frame.translateX.toFloat() * density
|
|
267
|
+
if (!frame.translateY.isNaN()) target.translationY = frame.translateY.toFloat() * density
|
|
268
|
+
if (!frame.scaleX.isNaN()) target.scaleX = frame.scaleX.toFloat()
|
|
269
|
+
if (!frame.scaleY.isNaN()) target.scaleY = frame.scaleY.toFloat()
|
|
270
|
+
if (!frame.rotation.isNaN()) target.rotation = frame.rotation.toFloat()
|
|
271
|
+
return true
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
private fun attachUIWorkletPanGesture(token: Long, targetTag: Int): Boolean {
|
|
275
|
+
if (stopping || stopped || uiWorkletPans.containsKey(token)) return false
|
|
276
|
+
val target = activity.window.decorView.findViewById<View>(targetTag) ?: return false
|
|
277
|
+
if (!target.isAttachedToWindow) return false
|
|
278
|
+
val density = activity.resources.displayMetrics.density.toDouble()
|
|
279
|
+
var startRawX = 0.0
|
|
280
|
+
var startRawY = 0.0
|
|
281
|
+
var velocityTracker: VelocityTracker? = null
|
|
282
|
+
val listener =
|
|
283
|
+
View.OnTouchListener { _, event ->
|
|
284
|
+
val action = event.actionMasked
|
|
285
|
+
if (action == MotionEvent.ACTION_DOWN) {
|
|
286
|
+
velocityTracker?.recycle()
|
|
287
|
+
velocityTracker = VelocityTracker.obtain()
|
|
288
|
+
}
|
|
289
|
+
velocityTracker?.addMovement(event)
|
|
290
|
+
val phase =
|
|
291
|
+
when (action) {
|
|
292
|
+
MotionEvent.ACTION_DOWN -> {
|
|
293
|
+
startRawX = event.rawX.toDouble()
|
|
294
|
+
startRawY = event.rawY.toDouble()
|
|
295
|
+
0
|
|
296
|
+
}
|
|
297
|
+
MotionEvent.ACTION_MOVE -> 1
|
|
298
|
+
MotionEvent.ACTION_UP -> 2
|
|
299
|
+
MotionEvent.ACTION_CANCEL -> 3
|
|
300
|
+
else -> return@OnTouchListener true
|
|
301
|
+
}
|
|
302
|
+
if (action == MotionEvent.ACTION_UP) {
|
|
303
|
+
velocityTracker?.computeCurrentVelocity(1_000)
|
|
304
|
+
}
|
|
305
|
+
val velocityX =
|
|
306
|
+
if (action == MotionEvent.ACTION_UP) {
|
|
307
|
+
(velocityTracker?.xVelocity?.toDouble() ?: 0.0) / density
|
|
308
|
+
} else {
|
|
309
|
+
0.0
|
|
310
|
+
}
|
|
311
|
+
val velocityY =
|
|
312
|
+
if (action == MotionEvent.ACTION_UP) {
|
|
313
|
+
(velocityTracker?.yVelocity?.toDouble() ?: 0.0) / density
|
|
314
|
+
} else {
|
|
315
|
+
0.0
|
|
316
|
+
}
|
|
317
|
+
SolidNativeBindingsInstaller.dispatchUIWorkletPanGesture(
|
|
318
|
+
token = token,
|
|
319
|
+
phase = phase,
|
|
320
|
+
translationX = (event.rawX.toDouble() - startRawX) / density,
|
|
321
|
+
translationY = (event.rawY.toDouble() - startRawY) / density,
|
|
322
|
+
timestamp = event.eventTime.toDouble(),
|
|
323
|
+
velocityX = velocityX,
|
|
324
|
+
velocityY = velocityY,
|
|
325
|
+
)
|
|
326
|
+
if (action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
|
|
327
|
+
velocityTracker?.recycle()
|
|
328
|
+
velocityTracker = null
|
|
329
|
+
}
|
|
330
|
+
true
|
|
331
|
+
}
|
|
332
|
+
target.setOnTouchListener(listener)
|
|
333
|
+
uiWorkletPans[token] =
|
|
334
|
+
UIWorkletPanAttachment(target) {
|
|
335
|
+
velocityTracker?.recycle()
|
|
336
|
+
velocityTracker = null
|
|
337
|
+
}
|
|
338
|
+
return true
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
private fun detachUIWorkletPanGesture(token: Long) {
|
|
342
|
+
val attachment = uiWorkletPans.remove(token) ?: return
|
|
343
|
+
attachment.target.setOnTouchListener(null)
|
|
344
|
+
attachment.release()
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
private fun clearUIWorkletPanGestures() {
|
|
348
|
+
for (attachment in uiWorkletPans.values) {
|
|
349
|
+
attachment.target.setOnTouchListener(null)
|
|
350
|
+
attachment.release()
|
|
351
|
+
}
|
|
352
|
+
uiWorkletPans.clear()
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
private fun reportStatus(status: String, error: Throwable?) {
|
|
356
|
+
try {
|
|
357
|
+
statusHandler?.onStatus(status, error)
|
|
358
|
+
} catch (handlerError: Throwable) {
|
|
359
|
+
Log.e(TAG, "The Solid Native surface status handler failed.", handlerError)
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
companion object {
|
|
364
|
+
@JvmStatic
|
|
365
|
+
@JvmOverloads
|
|
366
|
+
fun start(
|
|
367
|
+
activity: Activity,
|
|
368
|
+
reactHost: ReactHost,
|
|
369
|
+
bindingsInstaller: SolidNativeBindingsInstaller,
|
|
370
|
+
statusHandler: SolidNativeSurfaceStatusHandler? = null,
|
|
371
|
+
): SolidNativeSurface {
|
|
372
|
+
requireMainThread("start")
|
|
373
|
+
synchronizeLayoutDirection(activity)
|
|
374
|
+
val surface = reactHost.createSurface(activity, "", null)
|
|
375
|
+
return SolidNativeSurface(activity, bindingsInstaller, surface, statusHandler).also {
|
|
376
|
+
it.start()
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
|
|
380
|
+
private fun requireMainThread(operation: String) {
|
|
381
|
+
check(Looper.myLooper() === Looper.getMainLooper()) {
|
|
382
|
+
"SolidNativeSurface.$operation must run on the Android main thread."
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* React Native 0.87 resolves natural RTL against an arbitrary available locale instead of the
|
|
388
|
+
* Activity's effective application locale. Synchronize its package-scoped Fabric preferences
|
|
389
|
+
* before surface creation so scoped Android locale changes drive both the native view tree and
|
|
390
|
+
* Yoga without evaluating or exposing the JavaScript I18nManager mutation facade. Updating both
|
|
391
|
+
* flags is necessary because forceRTL(false) otherwise falls through to the defective natural
|
|
392
|
+
* locale lookup.
|
|
393
|
+
*/
|
|
394
|
+
private fun synchronizeLayoutDirection(activity: Activity) {
|
|
395
|
+
val locale =
|
|
396
|
+
ConfigurationCompat.getLocales(activity.resources.configuration)[0]
|
|
397
|
+
?: Locale.getDefault()
|
|
398
|
+
val supportsRTL =
|
|
399
|
+
activity.applicationInfo.flags and ApplicationInfo.FLAG_SUPPORTS_RTL != 0
|
|
400
|
+
val isRTL =
|
|
401
|
+
supportsRTL &&
|
|
402
|
+
TextUtilsCompat.getLayoutDirectionFromLocale(locale) == View.LAYOUT_DIRECTION_RTL
|
|
403
|
+
I18nUtil.instance.allowRTL(activity, isRTL)
|
|
404
|
+
I18nUtil.instance.forceRTL(activity, isRTL)
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
private fun mergeFailure(current: Throwable?, next: Throwable?): Throwable? {
|
|
408
|
+
if (next == null) return current
|
|
409
|
+
if (current == null) return next
|
|
410
|
+
if (current !== next) current.addSuppressed(next)
|
|
411
|
+
return current
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
private const val TAG = "SolidNativeSurface"
|
|
415
|
+
private const val MAX_TASK_ATTEMPTS = 1_000
|
|
416
|
+
private const val TASK_RETRY_MILLISECONDS = 10L
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
private data class UIWorkletPanAttachment(
|
|
421
|
+
val target: View,
|
|
422
|
+
val release: () -> Unit,
|
|
423
|
+
)
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// Package-owned Android application integration for the reviewed React Native boundary.
|
|
2
|
+
// Apply this file only after the Android application and one supported Kotlin plugin.
|
|
3
|
+
|
|
4
|
+
if (!plugins.hasPlugin("com.android.application")) {
|
|
5
|
+
throw new GradleException(
|
|
6
|
+
"@solidnative-dev/fabric-host/android-gradle requires com.android.application"
|
|
7
|
+
)
|
|
8
|
+
}
|
|
9
|
+
def usesBuiltInKotlin = plugins.hasPlugin("com.android.built-in-kotlin")
|
|
10
|
+
def usesExternalKotlin = plugins.hasPlugin("org.jetbrains.kotlin.android")
|
|
11
|
+
if (!usesBuiltInKotlin && !usesExternalKotlin) {
|
|
12
|
+
throw new GradleException(
|
|
13
|
+
"@solidnative-dev/fabric-host/android-gradle requires com.android.built-in-kotlin or org.jetbrains.kotlin.android"
|
|
14
|
+
)
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
def fabricHostDirectory = file(
|
|
18
|
+
providers.exec {
|
|
19
|
+
workingDir(rootProject.projectDir)
|
|
20
|
+
commandLine(
|
|
21
|
+
"node",
|
|
22
|
+
"--print",
|
|
23
|
+
"require('path').resolve(require.resolve('@solidnative-dev/fabric-host'), '../..')"
|
|
24
|
+
)
|
|
25
|
+
}.standardOutput.asText.get().trim()
|
|
26
|
+
)
|
|
27
|
+
def androidDirectory = new File(fabricHostDirectory, "native/android")
|
|
28
|
+
def cmakeIntegration = new File(androidDirectory, "SolidNativeRuntime.cmake")
|
|
29
|
+
def kotlinDirectory = new File(androidDirectory, "java")
|
|
30
|
+
def boundaryFile = new File(fabricHostDirectory, "native/react-native-boundary.json")
|
|
31
|
+
|
|
32
|
+
if (!cmakeIntegration.isFile() || !kotlinDirectory.isDirectory() || !boundaryFile.isFile()) {
|
|
33
|
+
throw new GradleException(
|
|
34
|
+
"The installed @solidnative-dev/fabric-host package has an incomplete Android native seam at " +
|
|
35
|
+
androidDirectory
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
def boundary = new groovy.json.JsonSlurper().parse(boundaryFile)
|
|
40
|
+
def reactNativeRelease = boundary.reactNativeVersion
|
|
41
|
+
def reactNativeRuntimeMatch = reactNativeRelease instanceof String
|
|
42
|
+
? (reactNativeRelease =~ /^(\d+)\.(\d+)\.(\d+)(?:[-+][0-9A-Za-z.-]+)?$/)
|
|
43
|
+
: null
|
|
44
|
+
if (reactNativeRuntimeMatch == null || !reactNativeRuntimeMatch.matches()) {
|
|
45
|
+
throw new GradleException(
|
|
46
|
+
"@solidnative-dev/fabric-host requires an exact React Native release in " +
|
|
47
|
+
boundaryFile
|
|
48
|
+
)
|
|
49
|
+
}
|
|
50
|
+
def reactNativeRuntimeParts = [
|
|
51
|
+
reactNativeRuntimeMatch.group(1),
|
|
52
|
+
reactNativeRuntimeMatch.group(2),
|
|
53
|
+
reactNativeRuntimeMatch.group(3),
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
def integrationProperty = "solidNativeFabricHostDirectory"
|
|
57
|
+
if (project.extensions.extraProperties.has(integrationProperty)) {
|
|
58
|
+
def configuredDirectory = file(
|
|
59
|
+
project.extensions.extraProperties.get(integrationProperty)
|
|
60
|
+
)
|
|
61
|
+
if (configuredDirectory != fabricHostDirectory) {
|
|
62
|
+
throw new GradleException(
|
|
63
|
+
"@solidnative-dev/fabric-host Android integration was already applied from " +
|
|
64
|
+
configuredDirectory
|
|
65
|
+
)
|
|
66
|
+
}
|
|
67
|
+
} else {
|
|
68
|
+
project.extensions.extraProperties.set(
|
|
69
|
+
integrationProperty,
|
|
70
|
+
fabricHostDirectory
|
|
71
|
+
)
|
|
72
|
+
android.sourceSets.named("main") { sourceSet ->
|
|
73
|
+
if (usesBuiltInKotlin) {
|
|
74
|
+
sourceSet.kotlin.directories.add(kotlinDirectory.absolutePath)
|
|
75
|
+
} else {
|
|
76
|
+
// The external Kotlin Android plugin compiles Kotlin files from
|
|
77
|
+
// Android Java source roots as well as its conventional Kotlin
|
|
78
|
+
// roots. This keeps the integration compatible with the exact
|
|
79
|
+
// upstream React Native application template selected by the
|
|
80
|
+
// reviewed boundary manifest.
|
|
81
|
+
sourceSet.java.srcDir(kotlinDirectory)
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
android.defaultConfig.externalNativeBuild.cmake.arguments(
|
|
85
|
+
"-DSOLID_NATIVE_FABRIC_HOST_DIR=${fabricHostDirectory.absolutePath}",
|
|
86
|
+
"-DSOLID_NATIVE_REACT_NATIVE_VERSION_MAJOR=${reactNativeRuntimeParts[0]}",
|
|
87
|
+
"-DSOLID_NATIVE_REACT_NATIVE_VERSION_MINOR=${reactNativeRuntimeParts[1]}",
|
|
88
|
+
"-DSOLID_NATIVE_REACT_NATIVE_VERSION_PATCH=${reactNativeRuntimeParts[2]}"
|
|
89
|
+
)
|
|
90
|
+
}
|