From 486c105026abf5c50edc4e17ad5c8f5119cc6239 Mon Sep 17 00:00:00 2001 From: Rehan Date: Thu, 28 Sep 2023 16:25:38 +0500 Subject: [PATCH] added lifecycle events to java --- .../ActivityLifecycleEventsListener.java | 70 +++++++++++++++++++ .../sample/java_layout/SampleApplication.java | 3 + .../java_layout/ui/core/BaseActivity.java | 7 ++ 3 files changed, 80 insertions(+) create mode 100644 samples/java_layout/src/main/java/io/customer/android/sample/java_layout/ActivityLifecycleEventsListener.java diff --git a/samples/java_layout/src/main/java/io/customer/android/sample/java_layout/ActivityLifecycleEventsListener.java b/samples/java_layout/src/main/java/io/customer/android/sample/java_layout/ActivityLifecycleEventsListener.java new file mode 100644 index 000000000..e5ab8dfbc --- /dev/null +++ b/samples/java_layout/src/main/java/io/customer/android/sample/java_layout/ActivityLifecycleEventsListener.java @@ -0,0 +1,70 @@ +package io.customer.android.sample.java_layout; + +import android.app.Activity; +import android.app.Application; +import android.os.Bundle; + +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import java.util.HashMap; +import java.util.Map; + +import io.customer.sdk.CustomerIO; +import io.customer.sdk.CustomerIOShared; +import io.customer.sdk.util.Logger; + +public class ActivityLifecycleEventsListener implements Application.ActivityLifecycleCallbacks { + + private final String lifecycleEventKey = "ActivityLifecycleEvent"; + private Logger logger = CustomerIOShared.instance().getDiStaticGraph().getLogger(); + + public void logEvent(@NonNull String eventName, @NonNull Activity activity) { + logger.debug(String.format("%s: %s %s", lifecycleEventKey, eventName, activity.getClass().getSimpleName())); + + try { + CustomerIO sdkInstance = CustomerIO.instance(); + Map extras = new HashMap<>(); + extras.put("EventName", eventName); + extras.put("ActivityName", activity.getClass().getSimpleName()); + sdkInstance.track(lifecycleEventKey, extras); + } catch (Exception e) { + logger.debug("SDK not yet initialized"); + } + } + + @Override + public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle) { + logEvent("onActivityCreated", activity); + } + + @Override + public void onActivityStarted(@NonNull Activity activity) { + logEvent("onActivityStarted", activity); + } + + @Override + public void onActivityResumed(@NonNull Activity activity) { + logEvent("onActivityResumed", activity); + } + + @Override + public void onActivityPaused(@NonNull Activity activity) { + logEvent("onActivityPaused", activity); + } + + @Override + public void onActivityStopped(@NonNull Activity activity) { + logEvent("onActivityStopped", activity); + } + + @Override + public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle) { + logEvent("onActivitySaveInstanceState", activity); + } + + @Override + public void onActivityDestroyed(@NonNull Activity activity) { + logEvent("onActivityDestroyed", activity); + } +} diff --git a/samples/java_layout/src/main/java/io/customer/android/sample/java_layout/SampleApplication.java b/samples/java_layout/src/main/java/io/customer/android/sample/java_layout/SampleApplication.java index 1acc379e8..25ffba26d 100644 --- a/samples/java_layout/src/main/java/io/customer/android/sample/java_layout/SampleApplication.java +++ b/samples/java_layout/src/main/java/io/customer/android/sample/java_layout/SampleApplication.java @@ -6,12 +6,15 @@ public class SampleApplication extends Application { private final ApplicationGraph appGraph = new ApplicationGraph(this); + public ActivityLifecycleEventsListener lifecycleEventsListener; @Override public void onCreate() { super.onCreate(); // Initialize Customer.io SDK on app start appGraph.getCustomerIORepository().initializeSdk(SampleApplication.this); + lifecycleEventsListener = new ActivityLifecycleEventsListener(); + registerActivityLifecycleCallbacks(lifecycleEventsListener); } public ApplicationGraph getApplicationGraph() { diff --git a/samples/java_layout/src/main/java/io/customer/android/sample/java_layout/ui/core/BaseActivity.java b/samples/java_layout/src/main/java/io/customer/android/sample/java_layout/ui/core/BaseActivity.java index 2b28c89f1..28693ffb7 100644 --- a/samples/java_layout/src/main/java/io/customer/android/sample/java_layout/ui/core/BaseActivity.java +++ b/samples/java_layout/src/main/java/io/customer/android/sample/java_layout/ui/core/BaseActivity.java @@ -1,5 +1,6 @@ package io.customer.android.sample.java_layout.ui.core; +import android.content.Intent; import android.os.Bundle; import androidx.annotation.EmptySuper; @@ -19,6 +20,12 @@ public abstract class BaseActivity extends AppCompatActi protected abstract VB inflateViewBinding(); + @Override + protected void onNewIntent(Intent intent) { + super.onNewIntent(intent); + ((SampleApplication) getApplication()).lifecycleEventsListener.logEvent("onNewIntent", this); + } + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState);