From 003d6fb760f692812ca21f612324610cb24bf5e0 Mon Sep 17 00:00:00 2001 From: Wojtek Zieba Date: Thu, 1 Feb 2024 11:58:53 +0100 Subject: [PATCH] fix: extract internal calls to `InternalDebugOnlyData` --- .../com/example/InternalDebugOnlyData.java | 58 ++++++++++++++ .../main/java/com/example/MainActivity.java | 75 +++++-------------- 2 files changed, 76 insertions(+), 57 deletions(-) create mode 100644 example/src/main/java/com/example/InternalDebugOnlyData.java diff --git a/example/src/main/java/com/example/InternalDebugOnlyData.java b/example/src/main/java/com/example/InternalDebugOnlyData.java new file mode 100644 index 0000000..59a0f2f --- /dev/null +++ b/example/src/main/java/com/example/InternalDebugOnlyData.java @@ -0,0 +1,58 @@ +package com.example; + +import com.parsely.parselyandroid.ParselyTrackerInternal; + +import org.jetbrains.annotations.Nullable; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; + +/** + * The InternalDebugOnlyData class is designed to provide access to internal data for debugging + * purposes through reflection. The methods within this class are intended for internal use only + * and should *not* be utilized within the SDK user's code. + * @noinspection KotlinInternalInJava + */ +class InternalDebugOnlyData { + private final ParselyTrackerInternal parselyTracker; + + InternalDebugOnlyData(ParselyTrackerInternal parselyTracker) { + this.parselyTracker = parselyTracker; + } + + boolean engagementIsActive() { + return (boolean) invokePrivateMethod("engagementIsActive"); + } + + @Nullable + Double getEngagementInterval() { + return (Double) invokePrivateMethod("getEngagementInterval"); + } + + @Nullable + Double getVideoEngagementInterval() { + return (Double) invokePrivateMethod("getVideoEngagementInterval"); + } + + long getFlushInterval() { + return (long) invokePrivateMethod("getFlushInterval"); + } + + boolean videoIsActive() { + return (boolean) invokePrivateMethod("videoIsActive"); + } + + boolean flushTimerIsActive() { + return (boolean) invokePrivateMethod("flushTimerIsActive"); + } + + private Object invokePrivateMethod(String methodName) { + try { + Method method = ParselyTrackerInternal.class.getDeclaredMethod(methodName); + method.setAccessible(true); + return method.invoke(parselyTracker); + } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { + throw new RuntimeException(e); + } + } +} diff --git a/example/src/main/java/com/example/MainActivity.java b/example/src/main/java/com/example/MainActivity.java index 8b45e35..89ecbca 100644 --- a/example/src/main/java/com/example/MainActivity.java +++ b/example/src/main/java/com/example/MainActivity.java @@ -1,32 +1,29 @@ package com.example; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.HashMap; -import java.util.Map; -import java.util.Timer; -import java.util.TimerTask; - +import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; -import android.app.Activity; -import android.view.Menu; import android.view.View; import android.widget.TextView; -import com.parsely.parselyandroid.*; +import com.parsely.parselyandroid.ParselyTracker; +import com.parsely.parselyandroid.ParselyTrackerInternal; +import com.parsely.parselyandroid.ParselyVideoMetadata; -import org.jetbrains.annotations.Nullable; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.HashMap; +import java.util.Map; +import java.util.Timer; +import java.util.TimerTask; /** * @noinspection KotlinInternalInJava */ public class MainActivity extends Activity { - private ParselyTracker parselyTracker; + private InternalDebugOnlyData internalDebugOnlyData; @Override protected void onCreate(Bundle savedInstanceState) { @@ -35,7 +32,7 @@ protected void onCreate(Bundle savedInstanceState) { // initialize the Parsely tracker with your site id and the current Context ParselyTracker.init("example.com", 30, this, true); - parselyTracker = ParselyTracker.sharedInstance(); + internalDebugOnlyData = new InternalDebugOnlyData((ParselyTrackerInternal) ParselyTracker.sharedInstance()); final TextView intervalView = (TextView) findViewById(R.id.interval); @@ -49,8 +46,8 @@ protected void onCreate(Bundle savedInstanceState) { public void handleMessage(Message msg) { TextView[] v = (TextView[]) msg.obj; TextView iView = v[0]; - if (flushTimerIsActive()) { - iView.setText(String.format("Flush Interval: %d", getFlushInterval())); + if (internalDebugOnlyData.flushTimerIsActive()) { + iView.setText(String.format("Flush Interval: %d", internalDebugOnlyData.getFlushInterval())); } else { iView.setText("Flush timer inactive"); } @@ -72,23 +69,23 @@ public void run() { private void updateEngagementStrings() { StringBuilder eMsg = new StringBuilder("Engagement is "); - if (engagementIsActive()) { + if (internalDebugOnlyData.engagementIsActive()) { eMsg.append("active."); } else { eMsg.append("inactive."); } - eMsg.append(String.format(" (interval: %.01fms)", getEngagementInterval())); + eMsg.append(String.format(" (interval: %.01fms)", internalDebugOnlyData.getEngagementInterval())); TextView eView = findViewById(R.id.et_interval); eView.setText(eMsg.toString()); StringBuilder vMsg = new StringBuilder("Video is "); - if (videoIsActive()) { + if (internalDebugOnlyData.videoIsActive()) { vMsg.append("active."); } else { vMsg.append("inactive."); } - vMsg.append(String.format(" (interval: %.01fms)", getVideoEngagementInterval())); + vMsg.append(String.format(" (interval: %.01fms)", internalDebugOnlyData.getVideoEngagementInterval())); TextView vView = findViewById(R.id.video_interval); vView.setText(vMsg.toString()); @@ -138,40 +135,4 @@ public void trackPause(View view) { public void trackReset(View view) { ParselyTracker.sharedInstance().resetVideo(); } - - private boolean engagementIsActive() { - return (boolean) invokePrivateMethod("engagementIsActive"); - } - - @Nullable - private Double getEngagementInterval() { - return (Double) invokePrivateMethod("getEngagementInterval"); - } - - @Nullable - private Double getVideoEngagementInterval() { - return (Double) invokePrivateMethod("getVideoEngagementInterval"); - } - - private long getFlushInterval() { - return (long) invokePrivateMethod("getFlushInterval"); - } - - private boolean videoIsActive() { - return (boolean) invokePrivateMethod("videoIsActive"); - } - - private boolean flushTimerIsActive() { - return (boolean) invokePrivateMethod("flushTimerIsActive"); - } - - private Object invokePrivateMethod(String methodName) { - try { - Method method = ParselyTrackerInternal.class.getDeclaredMethod(methodName); - method.setAccessible(true); - return method.invoke(parselyTracker); - } catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { - throw new RuntimeException(e); - } - } }