Skip to content

Commit 7307e21

Browse files
authored
Merge pull request #77 from Parsely/flush_events_on_app_on_stop
2 parents 2071aad + bed343b commit 7307e21

File tree

8 files changed

+43
-33
lines changed

8 files changed

+43
-33
lines changed

.github/workflows/publish.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ jobs:
1212
- uses: actions/checkout@v3
1313
with:
1414
fetch-depth: 0
15-
- name: set up JDK 11
15+
- name: set up JDK
1616
uses: actions/setup-java@v3
1717
with:
18-
java-version: '11'
18+
java-version: '17'
1919
distribution: 'temurin'
2020
cache: gradle
2121
- name: Build library

.github/workflows/readme.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ jobs:
1010
- uses: actions/checkout@v3
1111
with:
1212
fetch-depth: 0
13-
- name: set up JDK 11
13+
- name: set up JDK
1414
uses: actions/setup-java@v3
1515
with:
16-
java-version: '11'
16+
java-version: '17'
1717
distribution: 'temurin'
1818
cache: gradle
1919
- name: Build library

example/src/main/java/com/example/MainActivity.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,6 @@ public void run() {
7878

7979
}
8080

81-
@Override
82-
protected void onDestroy() {
83-
ParselyTracker.sharedInstance().flushEventQueue();
84-
super.onDestroy();
85-
}
86-
8781
private void updateEngagementStrings() {
8882
StringBuilder eMsg = new StringBuilder("Engagement is ");
8983
if (ParselyTracker.sharedInstance().engagementIsActive() == true) {
@@ -150,6 +144,4 @@ public void trackPause(View view) {
150144
}
151145

152146
public void trackReset(View view) {ParselyTracker.sharedInstance().resetVideo(); }
153-
154-
public void flushEventQueue(View view) {ParselyTracker.sharedInstance().flushEventQueue(); }
155147
}

example/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,11 @@
6868
android:onClick="trackReset"
6969
android:text="@string/button_reset_video" />
7070

71-
<Button
72-
android:id="@+id/flush_events_button"
73-
android:layout_width="wrap_content"
74-
android:layout_height="wrap_content"
75-
android:layout_below="@+id/reset_video_button"
76-
android:layout_centerHorizontal="true"
77-
android:onClick="flushEventQueue"
78-
android:text="@string/button_flush_event_queue" />
79-
8071
<TextView
8172
android:id="@+id/queue_size"
8273
android:layout_width="wrap_content"
8374
android:layout_height="wrap_content"
84-
android:layout_below="@+id/flush_events_button"
75+
android:layout_below="@+id/reset_video_button"
8576
android:layout_centerHorizontal="true"
8677
android:text="Queued events: 0" />
8778

example/src/main/res/values/strings.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@
88
<string name="button_track_video">Track Video</string>
99
<string name="button_pause_video">Pause Video</string>
1010
<string name="button_reset_video">Reset Video</string>
11-
<string name="button_flush_event_queue">Flush Event Queue</string>
1211

1312
</resources>

gradle.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,6 @@ org.gradle.jvmargs=-XX:MaxMetaspaceSize=2g
2020

2121
android.disableAutomaticComponentCreation=true
2222
android.useAndroidX=true
23+
24+
android.experimental.lint.version=8.1.0
25+

parsely/build.gradle

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,24 @@ android {
3030
}
3131

3232
dependencies {
33+
def kotlinVersion = "1.8.10"
34+
3335
implementation 'androidx.appcompat:appcompat:1.4.2'
3436
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.3'
3537
implementation 'com.google.android.gms:play-services-ads-identifier:18.0.1'
38+
implementation 'androidx.lifecycle:lifecycle-process:2.6.2'
39+
constraints {
40+
add("implementation", "org.jetbrains.kotlin:kotlin-stdlib-jdk7") {
41+
version {
42+
require(kotlinVersion)
43+
}
44+
}
45+
add("implementation", "org.jetbrains.kotlin:kotlin-stdlib-jdk8") {
46+
version {
47+
require(kotlinVersion)
48+
}
49+
}
50+
}
3651
}
3752

3853
apply from: "${rootProject.projectDir}/publication.gradle"

parsely/src/main/java/com/parsely/parselyandroid/ParselyTracker.java

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@
2424
import android.provider.Settings.Secure;
2525
import androidx.annotation.NonNull;
2626
import androidx.annotation.Nullable;
27+
import androidx.lifecycle.Lifecycle;
28+
import androidx.lifecycle.LifecycleEventObserver;
29+
import androidx.lifecycle.ProcessLifecycleOwner;
2730

2831
import com.fasterxml.jackson.databind.ObjectMapper;
2932
import com.google.android.gms.ads.identifier.AdvertisingIdClient;
@@ -102,6 +105,14 @@ protected ParselyTracker(String siteId, int flushInterval, Context c) {
102105
if (getStoredQueue().size() > 0) {
103106
startFlushTimer();
104107
}
108+
109+
ProcessLifecycleOwner.get().getLifecycle().addObserver(
110+
(LifecycleEventObserver) (lifecycleOwner, event) -> {
111+
if (event == Lifecycle.Event.ON_STOP) {
112+
flushEvents();
113+
}
114+
}
115+
);
105116
}
106117

107118
/**
@@ -484,17 +495,12 @@ private void enqueueEvent(Map<String, Object> event) {
484495
}
485496

486497
/**
487-
* Flush events to Parsely.
488-
* <p>
489-
* Empties the event queue and sends the appropriate requests to Parsely.
490-
* Called automatically after a number of seconds determined by {@link #getFlushInterval()}.
491-
* <p>
492-
* To make sure all of the queued events are flushed to Parse.ly's servers,
493-
* call this method in your main activity's `onDestroy()` method.
498+
* Deprecated since 3.1.1. The SDK now automatically flushes the queue on app lifecycle events.
499+
* Any usage of this method is safe to remove and will have no effect. Keeping for backwards compatibility.
494500
*/
501+
@Deprecated
495502
public void flushEventQueue() {
496-
// needed for call from MainActivity
497-
new FlushQueue().execute();
503+
// no-op
498504
}
499505

500506
/**
@@ -840,7 +846,7 @@ public void start() {
840846

841847
runningTask = new TimerTask() {
842848
public void run() {
843-
flushEventQueue();
849+
flushEvents();
844850
}
845851
};
846852
parentTimer.scheduleAtFixedRate(runningTask, intervalMillis, intervalMillis);
@@ -865,6 +871,10 @@ public long getIntervalMillis() {
865871
}
866872
}
867873

874+
private void flushEvents() {
875+
new FlushQueue().execute();
876+
}
877+
868878
/**
869879
* Engagement manager for article and video engagement.
870880
* <p>

0 commit comments

Comments
 (0)