Skip to content

Commit

Permalink
Merge pull request #109 from Parsely/remove_unneeded_methods_from_api
Browse files Browse the repository at this point in the history
Remove unneeded methods from api
  • Loading branch information
wzieba authored Feb 1, 2024
2 parents a65311e + 003d6fb commit 6cc8a79
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 64 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);
}
}
}
45 changes: 27 additions & 18 deletions example/src/main/java/com/example/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
package com.example;

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 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 InternalDebugOnlyData internalDebugOnlyData;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// initialize the Parsely tracker with your site id and the current Context
ParselyTracker.init("example.com", 30, this, true);
internalDebugOnlyData = new InternalDebugOnlyData((ParselyTrackerInternal) ParselyTracker.sharedInstance());

final TextView intervalView = (TextView) findViewById(R.id.interval);

Expand All @@ -39,8 +46,8 @@ protected void onCreate(Bundle savedInstanceState) {
public void handleMessage(Message msg) {
TextView[] v = (TextView[]) msg.obj;
TextView iView = v[0];
if (ParselyTracker.sharedInstance().flushTimerIsActive()) {
iView.setText(String.format("Flush Interval: %d", ParselyTracker.sharedInstance().getFlushInterval()));
if (internalDebugOnlyData.flushTimerIsActive()) {
iView.setText(String.format("Flush Interval: %d", internalDebugOnlyData.getFlushInterval()));
} else {
iView.setText("Flush timer inactive");
}
Expand All @@ -62,23 +69,23 @@ public void run() {

private void updateEngagementStrings() {
StringBuilder eMsg = new StringBuilder("Engagement is ");
if (ParselyTracker.sharedInstance().engagementIsActive()) {
if (internalDebugOnlyData.engagementIsActive()) {
eMsg.append("active.");
} else {
eMsg.append("inactive.");
}
eMsg.append(String.format(" (interval: %.01fms)", ParselyTracker.sharedInstance().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 (ParselyTracker.sharedInstance().videoIsActive()) {
if (internalDebugOnlyData.videoIsActive()) {
vMsg.append("active.");
} else {
vMsg.append("inactive.");
}
vMsg.append(String.format(" (interval: %.01fms)", ParselyTracker.sharedInstance().getVideoEngagementInterval()));
vMsg.append(String.format(" (interval: %.01fms)", internalDebugOnlyData.getVideoEngagementInterval()));

TextView vView = findViewById(R.id.video_interval);
vView.setText(vMsg.toString());
Expand Down Expand Up @@ -117,13 +124,15 @@ public void trackPlay(View view) {
90
);
// NOTE: For videos embedded in an article, "url" should be the URL for that article.
ParselyTracker.sharedInstance().trackPlay("http://example.com/app-videos", null, metadata, null);
ParselyTracker.sharedInstance().trackPlay("http://example.com/app-videos", "", metadata, null);

}

public void trackPause(View view) {
ParselyTracker.sharedInstance().trackPause();
}

public void trackReset(View view) {ParselyTracker.sharedInstance().resetVideo(); }
public void trackReset(View view) {
ParselyTracker.sharedInstance().resetVideo();
}
}
6 changes: 0 additions & 6 deletions parsely/api/parsely.api
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,6 @@ public final class com/parsely/parselyandroid/ParselyNotInitializedException : j

public abstract interface class com/parsely/parselyandroid/ParselyTracker {
public static final field Companion Lcom/parsely/parselyandroid/ParselyTracker$Companion;
public abstract fun engagementIsActive ()Z
public abstract fun flushTimerIsActive ()Z
public abstract fun getEngagementInterval ()Ljava/lang/Double;
public abstract fun getFlushInterval ()J
public abstract fun getVideoEngagementInterval ()Ljava/lang/Double;
public static fun init (Ljava/lang/String;ILandroid/content/Context;)V
public static fun init (Ljava/lang/String;ILandroid/content/Context;Z)V
public static fun init (Ljava/lang/String;Landroid/content/Context;)V
Expand All @@ -29,7 +24,6 @@ public abstract interface class com/parsely/parselyandroid/ParselyTracker {
public abstract fun trackPageview (Ljava/lang/String;Ljava/lang/String;Lcom/parsely/parselyandroid/ParselyMetadata;Ljava/util/Map;)V
public abstract fun trackPause ()V
public abstract fun trackPlay (Ljava/lang/String;Ljava/lang/String;Lcom/parsely/parselyandroid/ParselyVideoMetadata;Ljava/util/Map;)V
public abstract fun videoIsActive ()Z
}

public final class com/parsely/parselyandroid/ParselyTracker$Companion {
Expand Down
32 changes: 0 additions & 32 deletions parsely/src/main/java/com/parsely/parselyandroid/ParselyTracker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,36 +26,6 @@ import org.jetbrains.annotations.TestOnly
*/
public interface ParselyTracker {

/**
* Get the heartbeat interval
*
* @return The base engagement tracking interval.
*/
public val engagementInterval: Double?

public val videoEngagementInterval: Double?

/**
* Returns the interval at which the event queue is flushed to Parse.ly.
*
* @return The interval at which the event queue is flushed to Parse.ly.
*/
public val flushInterval: Long

/**
* Returns whether the engagement tracker is running.
*
* @return Whether the engagement tracker is running.
*/
public fun engagementIsActive(): Boolean

/**
* Returns whether video tracking is active.
*
* @return Whether video tracking is active.
*/
public fun videoIsActive(): Boolean

/**
* Register a pageview event using a URL and optional metadata.
*
Expand Down Expand Up @@ -162,8 +132,6 @@ public interface ParselyTracker {
*/
public fun resetVideo()

public fun flushTimerIsActive(): Boolean

public companion object {
private const val DEFAULT_FLUSH_INTERVAL_SECS = 60
private var instance: ParselyTrackerInternal? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,22 @@ internal class ParselyTrackerInternal internal constructor(
)
}

override val engagementInterval: Double?
private val engagementInterval: Double?
get() = engagementManager?.intervalMillis

override val videoEngagementInterval: Double?
private val videoEngagementInterval: Double?
get() = videoEngagementManager?.intervalMillis

override fun engagementIsActive(): Boolean {
@Suppress("unused") // used via reflection in sample app
private fun engagementIsActive(): Boolean {
return engagementManager?.isRunning ?: false
}

override fun videoIsActive(): Boolean {
private fun videoIsActive(): Boolean {
return videoEngagementManager?.isRunning ?: false
}

override val flushInterval: Long
private val flushInterval: Long
get() = flushManager.intervalMillis / 1000

override fun trackPageview(
Expand Down Expand Up @@ -228,7 +229,7 @@ internal class ParselyTrackerInternal internal constructor(
*
* @return Whether the event queue flush timer is running.
*/
override fun flushTimerIsActive(): Boolean {
private fun flushTimerIsActive(): Boolean {
return flushManager.isRunning
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ParselyTrackerTest {

@Test(expected = ParselyNotInitializedException::class)
fun `given no prior initialization, when executing a method, throw the exception`() {
ParselyTracker.sharedInstance().engagementIsActive()
ParselyTracker.sharedInstance().startEngagement("url")
}

@Test(expected = ParselyAlreadyInitializedException::class)
Expand All @@ -30,7 +30,7 @@ class ParselyTrackerTest {
fun `given tracker initialized, when calling a method, do not throw any exception`() {
ParselyTracker.init(siteId = "", context = RuntimeEnvironment.getApplication())

ParselyTracker.sharedInstance().engagementIsActive()
ParselyTracker.sharedInstance().startEngagement("url")
}

@After
Expand Down

0 comments on commit 6cc8a79

Please sign in to comment.