Skip to content

Commit

Permalink
Merge branch 'main' into improve_javadoc
Browse files Browse the repository at this point in the history
  • Loading branch information
wzieba committed Feb 1, 2024
2 parents 317a8fa + a23b6c2 commit ebe95fe
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 57 deletions.
58 changes: 58 additions & 0 deletions example/src/main/java/com/example/InternalDebugOnlyData.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
}
75 changes: 18 additions & 57 deletions example/src/main/java/com/example/MainActivity.java
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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);

Expand All @@ -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");
}
Expand All @@ -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());
Expand Down Expand Up @@ -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);
}
}
}

0 comments on commit ebe95fe

Please sign in to comment.