2323import com .facebook .react .bridge .WritableMap ;
2424import com .facebook .react .bridge .WritableNativeArray ;
2525import com .facebook .react .bridge .WritableNativeMap ;
26- import com .facebook .react .module .annotations .ReactModule ;
2726
2827import java .io .BufferedInputStream ;
2928import java .io .File ;
4241import io .sentry .DateUtils ;
4342import io .sentry .HubAdapter ;
4443import io .sentry .ILogger ;
44+ import io .sentry .ISerializer ;
4545import io .sentry .Integration ;
4646import io .sentry .Sentry ;
4747import io .sentry .SentryDate ;
5454import io .sentry .android .core .BuildInfoProvider ;
5555import io .sentry .android .core .CurrentActivityHolder ;
5656import io .sentry .android .core .NdkIntegration ;
57- import io .sentry .android .core .ScreenshotEventProcessor ;
5857import io .sentry .android .core .SentryAndroid ;
58+ import io .sentry .android .core .ViewHierarchyEventProcessor ;
5959import io .sentry .protocol .SdkVersion ;
6060import io .sentry .protocol .SentryException ;
6161import io .sentry .protocol .SentryPackage ;
6262import io .sentry .protocol .User ;
63+ import io .sentry .protocol .ViewHierarchy ;
64+ import io .sentry .util .JsonSerializationUtils ;
6365
6466public class RNSentryModuleImpl {
6567
@@ -74,7 +76,6 @@ public class RNSentryModuleImpl {
7476 private final PackageInfo packageInfo ;
7577 private FrameMetricsAggregator frameMetricsAggregator = null ;
7678 private boolean androidXAvailable ;
77- private ScreenshotEventProcessor screenshotEventProcessor ;
7879
7980 private static boolean didFetchAppStart ;
8081
@@ -153,6 +154,9 @@ public void initNativeSdk(final ReadableMap rnOptions, Promise promise) {
153154 if (rnOptions .hasKey ("attachScreenshot" )) {
154155 options .setAttachScreenshot (rnOptions .getBoolean ("attachScreenshot" ));
155156 }
157+ if (rnOptions .hasKey ("attachViewHierarchy" )) {
158+ options .setAttachViewHierarchy (rnOptions .getBoolean ("attachViewHierarchy" ));
159+ }
156160 if (rnOptions .hasKey ("sendDefaultPii" )) {
157161 options .setSendDefaultPii (rnOptions .getBoolean ("sendDefaultPii" ));
158162 }
@@ -389,6 +393,35 @@ private static byte[] takeScreenshotOnUiThread(Activity activity) {
389393 return bytesWrapper [0 ];
390394 }
391395
396+ public void fetchViewHierarchy (Promise promise ) {
397+ final @ Nullable Activity activity = getCurrentActivity ();
398+ final @ Nullable ViewHierarchy viewHierarchy = ViewHierarchyEventProcessor .snapshotViewHierarchy (activity , logger );
399+ if (viewHierarchy == null ) {
400+ logger .log (SentryLevel .ERROR , "Could not get ViewHierarchy." );
401+ promise .resolve (null );
402+ return ;
403+ }
404+
405+ ISerializer serializer = HubAdapter .getInstance ().getOptions ().getSerializer ();
406+ final @ Nullable byte [] bytes = JsonSerializationUtils .bytesFrom (serializer , logger , viewHierarchy );
407+ if (bytes == null ) {
408+ logger .log (SentryLevel .ERROR , "Could not serialize ViewHierarchy." );
409+ promise .resolve (null );
410+ return ;
411+ }
412+ if (bytes .length < 1 ) {
413+ logger .log (SentryLevel .ERROR , "Got empty bytes array after serializing ViewHierarchy." );
414+ promise .resolve (null );
415+ return ;
416+ }
417+
418+ final WritableNativeArray data = new WritableNativeArray ();
419+ for (final byte b : bytes ) {
420+ data .pushInt (b );
421+ }
422+ promise .resolve (data );
423+ }
424+
392425 private static PackageInfo getPackageInfo (Context ctx ) {
393426 try {
394427 return ctx .getPackageManager ().getPackageInfo (ctx .getPackageName (), 0 );
0 commit comments