|
| 1 | +package io.sentry.react.replay; |
| 2 | + |
| 3 | +import android.os.Bundle; |
| 4 | +import android.util.DisplayMetrics; |
| 5 | +import android.view.View; |
| 6 | +import android.view.ViewTreeObserver; |
| 7 | +import androidx.annotation.NonNull; |
| 8 | +import androidx.fragment.app.Fragment; |
| 9 | +import androidx.fragment.app.FragmentManager; |
| 10 | +import androidx.fragment.app.FragmentManager.FragmentLifecycleCallbacks; |
| 11 | +import io.sentry.ILogger; |
| 12 | +import io.sentry.ReplayController; |
| 13 | +import io.sentry.ScopesAdapter; |
| 14 | +import io.sentry.SentryLevel; |
| 15 | +import io.sentry.android.replay.ReplayIntegration; |
| 16 | +import java.lang.ref.WeakReference; |
| 17 | +import org.jetbrains.annotations.NotNull; |
| 18 | +import org.jetbrains.annotations.Nullable; |
| 19 | + |
| 20 | +public class RNSentryReplayFragmentLifecycleTracer extends FragmentLifecycleCallbacks { |
| 21 | + private @NotNull final ILogger logger; |
| 22 | + |
| 23 | + private @Nullable ReplayIntegration replayIntegration; |
| 24 | + |
| 25 | + private int lastWidth = -1; |
| 26 | + private int lastHeight = -1; |
| 27 | + |
| 28 | + private @Nullable WeakReference<View> currentViewRef; |
| 29 | + private @Nullable ViewTreeObserver.OnGlobalLayoutListener currentListener; |
| 30 | + |
| 31 | + public RNSentryReplayFragmentLifecycleTracer(@NotNull ILogger logger) { |
| 32 | + this.logger = logger; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public void onFragmentViewCreated( |
| 37 | + @NotNull FragmentManager fm, |
| 38 | + @NotNull Fragment f, |
| 39 | + @NotNull View v, |
| 40 | + @Nullable Bundle savedInstanceState) { |
| 41 | + // Add layout listener to detect configuration changes after detaching any previous one |
| 42 | + detachLayoutChangeListener(); |
| 43 | + attachLayoutChangeListener(v); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void onFragmentViewDestroyed(@NonNull FragmentManager fm, @NonNull Fragment f) { |
| 48 | + detachLayoutChangeListener(); |
| 49 | + } |
| 50 | + |
| 51 | + private void attachLayoutChangeListener(final View view) { |
| 52 | + final WeakReference<View> weakView = new WeakReference<>(view); |
| 53 | + |
| 54 | + final ViewTreeObserver.OnGlobalLayoutListener listener = |
| 55 | + new ViewTreeObserver.OnGlobalLayoutListener() { |
| 56 | + @Override |
| 57 | + public void onGlobalLayout() { |
| 58 | + final View v = weakView.get(); |
| 59 | + if (v != null) { |
| 60 | + checkAndNotifyWindowSizeChange(v); |
| 61 | + } |
| 62 | + } |
| 63 | + }; |
| 64 | + |
| 65 | + currentViewRef = new WeakReference<>(view); |
| 66 | + currentListener = listener; |
| 67 | + |
| 68 | + view.getViewTreeObserver().addOnGlobalLayoutListener(listener); |
| 69 | + } |
| 70 | + |
| 71 | + private void detachLayoutChangeListener() { |
| 72 | + final View view = currentViewRef != null ? currentViewRef.get() : null; |
| 73 | + if (view != null && currentListener != null) { |
| 74 | + try { |
| 75 | + ViewTreeObserver observer = view.getViewTreeObserver(); |
| 76 | + if (observer != null) { |
| 77 | + observer.removeOnGlobalLayoutListener(currentListener); |
| 78 | + } |
| 79 | + } catch (Exception e) { |
| 80 | + logger.log(SentryLevel.DEBUG, "Failed to remove layout change listener", e); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + currentViewRef = null; |
| 85 | + currentListener = null; |
| 86 | + } |
| 87 | + |
| 88 | + private void checkAndNotifyWindowSizeChange(View view) { |
| 89 | + try { |
| 90 | + DisplayMetrics metrics = view.getContext().getResources().getDisplayMetrics(); |
| 91 | + int currentWidth = metrics.widthPixels; |
| 92 | + int currentHeight = metrics.heightPixels; |
| 93 | + |
| 94 | + if (lastWidth == currentWidth && lastHeight == currentHeight) { |
| 95 | + return; |
| 96 | + } |
| 97 | + lastWidth = currentWidth; |
| 98 | + lastHeight = currentHeight; |
| 99 | + |
| 100 | + notifyReplayIntegrationOfSizeChange(currentWidth, currentHeight); |
| 101 | + } catch (Exception e) { |
| 102 | + logger.log(SentryLevel.DEBUG, "Failed to check window size", e); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private void notifyReplayIntegrationOfSizeChange(int width, int height) { |
| 107 | + if (replayIntegration == null) { |
| 108 | + replayIntegration = getReplayIntegration(); |
| 109 | + } |
| 110 | + |
| 111 | + if (replayIntegration == null) { |
| 112 | + return; |
| 113 | + } |
| 114 | + |
| 115 | + try { |
| 116 | + replayIntegration.onWindowSizeChanged(width, height); |
| 117 | + } catch (Exception e) { |
| 118 | + logger.log(SentryLevel.DEBUG, "Failed to notify replay integration of size change", e); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + private @Nullable ReplayIntegration getReplayIntegration() { |
| 123 | + try { |
| 124 | + final ReplayController replayController = |
| 125 | + ScopesAdapter.getInstance().getOptions().getReplayController(); |
| 126 | + |
| 127 | + if (replayController instanceof ReplayIntegration) { |
| 128 | + return (ReplayIntegration) replayController; |
| 129 | + } else { |
| 130 | + logger.log(SentryLevel.DEBUG, "Error getting replay integration"); |
| 131 | + } |
| 132 | + } catch (Exception e) { |
| 133 | + logger.log(SentryLevel.DEBUG, "Error getting replay integration", e); |
| 134 | + } |
| 135 | + return null; |
| 136 | + } |
| 137 | +} |
0 commit comments