-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement Android SR for ReactTextView and ReactEditText
- Loading branch information
1 parent
ffc1015
commit b27e59a
Showing
14 changed files
with
979 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...ion-replay/android/src/main/kotlin/com/datadog/reactnative/sessionreplay/DrawableUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.datadog.reactnative.sessionreplay | ||
|
||
import android.graphics.drawable.Drawable | ||
import android.graphics.drawable.InsetDrawable | ||
import android.graphics.drawable.LayerDrawable | ||
import com.facebook.react.views.view.ReactViewBackgroundDrawable | ||
|
||
internal fun getReactBackgroundFromDrawable(drawable: Drawable?): ReactViewBackgroundDrawable? { | ||
if (drawable is ReactViewBackgroundDrawable) { | ||
return drawable | ||
} | ||
|
||
if (drawable is InsetDrawable) { | ||
return getReactBackgroundFromDrawable(drawable.drawable) | ||
} | ||
|
||
if (drawable is LayerDrawable) { | ||
for (layerNumber in 0 until drawable.numberOfLayers) { | ||
val layer = drawable.getDrawable(layerNumber) | ||
if (layer is ReactViewBackgroundDrawable) { | ||
return layer | ||
} | ||
} | ||
} | ||
|
||
return null | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...android/src/main/kotlin/com/datadog/reactnative/sessionreplay/ReactTextShadowNodeUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package com.datadog.reactnative.sessionreplay | ||
|
||
import android.util.Log | ||
import com.facebook.react.bridge.ReactContext | ||
import com.facebook.react.uimanager.ReactShadowNode | ||
import com.facebook.react.uimanager.UIImplementation | ||
import com.facebook.react.uimanager.UIManagerModule | ||
import com.facebook.react.views.text.ReactTextShadowNode | ||
import com.facebook.react.views.text.TextAttributes | ||
import okhttp3.internal.notify | ||
import okhttp3.internal.wait | ||
|
||
internal class ReactTextShadowNodeUtils( | ||
private val reactContext: ReactContext | ||
) { | ||
internal fun getFontSize(shadowNode: ReactTextShadowNode): Int { | ||
shadowNode.javaClass.superclass?.getDeclaredField("mTextAttributes").let { | ||
it?.isAccessible = true | ||
val textAttributes = it?.get(shadowNode) as TextAttributes | ||
return textAttributes.effectiveFontSize | ||
} | ||
} | ||
|
||
internal fun getFontFamily(shadowNode: ReactTextShadowNode): String? { | ||
shadowNode.javaClass.superclass.getDeclaredField("mFontFamily").let { | ||
it.isAccessible = true | ||
return it.get(shadowNode)?.toString() | ||
} | ||
} | ||
|
||
internal fun getGravity(viewId: Int): Int? { | ||
val shadowNode = getShadowNode(viewId) as? ReactTextShadowNode ?: return null | ||
shadowNode.javaClass.superclass.getDeclaredField("mTextAlign").let { | ||
it.isAccessible = true | ||
return it.getInt(shadowNode) | ||
} | ||
} | ||
|
||
internal fun getColor(shadowNode: ReactTextShadowNode): Int { | ||
shadowNode.javaClass.superclass.getDeclaredField("mColor").let { | ||
it.isAccessible = true | ||
return it.getInt(shadowNode) | ||
} | ||
} | ||
|
||
internal fun getShadowNode(viewId: Int): ReactShadowNode<out ReactShadowNode<*>>? { | ||
val uiManagerModule = try { | ||
reactContext.getNativeModule(UIManagerModule::class.java) as UIManagerModule | ||
} catch (e: IllegalStateException) { | ||
Log.e( | ||
ReactTextShadowNodeUtils::class.java.canonicalName, | ||
"Unable to resolve uiManagerModule", | ||
e | ||
) | ||
return null | ||
} | ||
var target: ReactShadowNode<out ReactShadowNode<*>>? = null | ||
val shadowNodeRunnable = Runnable { | ||
val node = uiManagerModule.resolveShadowNode(viewId) | ||
if (node != null) { | ||
target = node | ||
} | ||
synchronized(this) { | ||
this.notify() | ||
} | ||
} | ||
synchronized(this) { | ||
reactContext.runOnNativeModulesQueueThread(shadowNodeRunnable) | ||
this.wait() | ||
return target | ||
} | ||
} | ||
|
||
private fun UIManagerModule.resolveShadowNode(tag: Int): ReactShadowNode<out ReactShadowNode<*>>? { | ||
javaClass.getDeclaredField("mUIImplementation").let { | ||
it.isAccessible = true | ||
val value = it.get(this) as UIImplementation | ||
return value.resolveShadowNode(tag) | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...replay/android/src/main/kotlin/com/datadog/reactnative/sessionreplay/extensions/IntExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
/* | ||
* Unless explicitly stated otherwise all files in this repository are licensed under the Apache License Version 2.0. | ||
* This product includes software developed at Datadog (https://www.datadoghq.com/). | ||
* Copyright 2016-Present Datadog, Inc. | ||
*/ | ||
|
||
package com.datadog.reactnative.sessionreplay.extensions | ||
|
||
internal fun Int.densityNormalized(density: Float): Int { | ||
return if (density == 0f) { | ||
this | ||
} else { | ||
(this / density).toInt() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
...kotlin/com/datadog/reactnative/sessionreplay/extensions/ReactViewBackgroundDrawableExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.datadog.reactnative.sessionreplay.extensions | ||
|
||
import com.datadog.android.sessionreplay.model.MobileSegment | ||
import com.datadog.reactnative.sessionreplay.formatAsRgba | ||
import com.facebook.react.uimanager.Spacing | ||
import com.facebook.react.views.view.ReactViewBackgroundDrawable | ||
|
||
internal fun ReactViewBackgroundDrawable.resolveShapeAndBorder(opacity: Float, pixelDensity: Float): Pair<MobileSegment.ShapeStyle?, MobileSegment.ShapeBorder?> { | ||
val borderProps = resolveBorder(this, pixelDensity) | ||
val colorHexString = formatAsRgba(this.getBackgroundColor()) | ||
val cornerRadius = this.fullBorderRadius.toLong().convertToDensityNormalized(pixelDensity) | ||
|
||
return MobileSegment.ShapeStyle( | ||
colorHexString, | ||
opacity, | ||
cornerRadius | ||
) to borderProps | ||
} | ||
|
||
private fun ReactViewBackgroundDrawable.getBackgroundColor(): Int { | ||
javaClass.getDeclaredField("mColor").let { | ||
it.isAccessible = true | ||
return it.getInt(this) | ||
} | ||
} | ||
|
||
private fun resolveBorder(backgroundDrawable: ReactViewBackgroundDrawable?, pixelDensity: Float): MobileSegment.ShapeBorder? { | ||
if (backgroundDrawable == null) { | ||
return null | ||
} | ||
|
||
val borderWidth = backgroundDrawable.fullBorderWidth.toLong().convertToDensityNormalized(pixelDensity) | ||
val borderColor = formatAsRgba(backgroundDrawable.getBorderColor(Spacing.ALL)) | ||
|
||
return MobileSegment.ShapeBorder( | ||
color = borderColor, | ||
width = borderWidth | ||
) | ||
} |
Oops, something went wrong.