From 8d640cc3a5294a2de7f0f3b93b27c18909bf5539 Mon Sep 17 00:00:00 2001 From: Kai Koenig Date: Tue, 11 Jun 2019 21:57:00 +0200 Subject: [PATCH 1/6] Small syntax improvement --- .../com/raygun/raygun4android/logging/TimberRaygunLogger.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/provider/src/main/java/com/raygun/raygun4android/logging/TimberRaygunLogger.java b/provider/src/main/java/com/raygun/raygun4android/logging/TimberRaygunLogger.java index 2f4f504..fe46a19 100644 --- a/provider/src/main/java/com/raygun/raygun4android/logging/TimberRaygunLogger.java +++ b/provider/src/main/java/com/raygun/raygun4android/logging/TimberRaygunLogger.java @@ -1,7 +1,6 @@ package com.raygun.raygun4android.logging; interface TimberRaygunLogger { - static void init(){ - }; + static void init(){} } From 490a6a12beec05b49c0536f7a256ed1a205428c7 Mon Sep 17 00:00:00 2001 From: Kai Koenig Date: Tue, 11 Jun 2019 21:58:21 +0200 Subject: [PATCH 2/6] Improving situation with context tracking in RUM and moved to WeakReferences to avoid leaks as much as possible. --- .../java/com/raygun/raygun4android/RUM.java | 53 ++++++++++--------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/provider/src/main/java/com/raygun/raygun4android/RUM.java b/provider/src/main/java/com/raygun/raygun4android/RUM.java index 2e72a65..cd7cbdf 100644 --- a/provider/src/main/java/com/raygun/raygun4android/RUM.java +++ b/provider/src/main/java/com/raygun/raygun4android/RUM.java @@ -9,6 +9,7 @@ import android.os.Build; import android.os.Bundle; +import java.lang.ref.WeakReference; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.ZoneId; @@ -30,9 +31,9 @@ public class RUM implements ActivityLifecycleCallbacks { private static RUM rum; - private static Activity mainActivity; - private static Activity currentActivity; - private static Activity loadingActivity; + private static WeakReference mainActivity; + private static WeakReference currentActivity; + private static WeakReference loadingActivity; private static long startTime; private static long lastSeenTime; private static String sessionId; @@ -45,8 +46,8 @@ private static void attach(Activity mainActivity) { Application application = mainActivity.getApplication(); if (application != null) { - RUM.mainActivity = mainActivity; - RUM.currentActivity = mainActivity; + RUM.mainActivity = new WeakReference<>(mainActivity); + RUM.currentActivity = new WeakReference<>(mainActivity); RUM.startTime = System.nanoTime(); RUM.rum = new RUM(); @@ -69,18 +70,18 @@ static void attach(Activity mainActivity, boolean networkLogging) { } protected static void detach() { - if (RUM.rum != null && RUM.mainActivity != null && RUM.mainActivity.getApplication() != null) { - RUM.mainActivity.getApplication().unregisterActivityLifecycleCallbacks(RUM.rum); - RUM.mainActivity = null; - RUM.currentActivity = null; + if (RUM.rum != null && RUM.mainActivity.get() != null && RUM.mainActivity.get().getApplication() != null) { + RUM.mainActivity.get().getApplication().unregisterActivityLifecycleCallbacks(RUM.rum); + RUM.mainActivity.clear(); + RUM.currentActivity.clear(); RUM.rum = null; } } static void sendRemainingActivity() { if (RUM.rum != null) { - if (RUM.loadingActivity != null) { - String activityName = getActivityName(RUM.loadingActivity); + if (RUM.loadingActivity.get() != null) { + String activityName = getActivityName(RUM.loadingActivity.get()); long diff = System.nanoTime() - RUM.startTime; long duration = TimeUnit.NANOSECONDS.toMillis(diff); @@ -107,15 +108,15 @@ private static void enqueueWorkForRUMService(String apiKey, String jsonPayload) @Override public void onActivityCreated(Activity activity, Bundle bundle) { RaygunLogger.v("RUM - onActivityCreated"); - if (RUM.currentActivity == null) { + if (RUM.currentActivity.get() == null) { if (doesNeedSessionRotation()) { rotateSession(currentSessionUser,currentSessionUser); } } - if (activity != RUM.currentActivity) { - RUM.currentActivity = activity; - RUM.loadingActivity = activity; + if (activity != RUM.currentActivity.get()) { + RUM.currentActivity = new WeakReference<>(activity); + RUM.loadingActivity = new WeakReference<>(activity); RUM.startTime = System.nanoTime(); } @@ -125,15 +126,15 @@ public void onActivityCreated(Activity activity, Bundle bundle) { @Override public void onActivityStarted(Activity activity) { RaygunLogger.v("RUM - onActivityStarted"); - if (RUM.currentActivity == null) { + if (RUM.currentActivity.get() == null) { if (doesNeedSessionRotation()) { rotateSession(currentSessionUser,currentSessionUser); } } - if (activity != RUM.currentActivity) { - RUM.currentActivity = activity; - RUM.loadingActivity = activity; + if (activity != RUM.currentActivity.get()) { + RUM.currentActivity = new WeakReference<>(activity); + RUM.loadingActivity = new WeakReference<>(activity); RUM.startTime = System.nanoTime(); } @@ -143,7 +144,7 @@ public void onActivityStarted(Activity activity) { @Override public void onActivityResumed(Activity activity) { RaygunLogger.v("RUM - onActivityResumed"); - if (RUM.currentActivity == null) { + if (RUM.currentActivity.get() == null) { if (doesNeedSessionRotation()) { rotateSession(currentSessionUser,currentSessionUser); } @@ -152,13 +153,13 @@ public void onActivityResumed(Activity activity) { String activityName = getActivityName(activity); long duration = 0; - if (activity == RUM.currentActivity) { + if (activity == RUM.currentActivity.get()) { long diff = System.nanoTime() - RUM.startTime; duration = TimeUnit.NANOSECONDS.toMillis(diff); } - RUM.currentActivity = activity; - RUM.loadingActivity = null; + RUM.currentActivity = new WeakReference<>(activity); + RUM.loadingActivity.clear(); sendRUMTimingEvent(RaygunRUMEventType.ACTIVITY_LOADED, activityName, duration); @@ -174,9 +175,9 @@ public void onActivityPaused(Activity activity) { @Override public void onActivityStopped(Activity activity) { RaygunLogger.v("RUM - onActivityStopped"); - if (activity == RUM.currentActivity) { - RUM.currentActivity = null; - RUM.loadingActivity = null; + if (activity == RUM.currentActivity.get()) { + RUM.currentActivity.clear(); + RUM.loadingActivity.clear(); RUM.lastSeenTime = System.currentTimeMillis(); } } From 8290251399b231f68d7204e4a0834e1dde86047c Mon Sep 17 00:00:00 2001 From: Kai Koenig Date: Wed, 12 Jun 2019 12:19:39 +0200 Subject: [PATCH 3/6] Fixing some issues around WR. --- .../java/com/raygun/raygun4android/RUM.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/provider/src/main/java/com/raygun/raygun4android/RUM.java b/provider/src/main/java/com/raygun/raygun4android/RUM.java index cd7cbdf..a7ada7a 100644 --- a/provider/src/main/java/com/raygun/raygun4android/RUM.java +++ b/provider/src/main/java/com/raygun/raygun4android/RUM.java @@ -72,15 +72,15 @@ static void attach(Activity mainActivity, boolean networkLogging) { protected static void detach() { if (RUM.rum != null && RUM.mainActivity.get() != null && RUM.mainActivity.get().getApplication() != null) { RUM.mainActivity.get().getApplication().unregisterActivityLifecycleCallbacks(RUM.rum); - RUM.mainActivity.clear(); - RUM.currentActivity.clear(); + RUM.mainActivity = null; + RUM.currentActivity = null; RUM.rum = null; } } static void sendRemainingActivity() { if (RUM.rum != null) { - if (RUM.loadingActivity.get() != null) { + if (RUM.loadingActivity != null && RUM.loadingActivity.get() != null) { String activityName = getActivityName(RUM.loadingActivity.get()); long diff = System.nanoTime() - RUM.startTime; @@ -108,13 +108,13 @@ private static void enqueueWorkForRUMService(String apiKey, String jsonPayload) @Override public void onActivityCreated(Activity activity, Bundle bundle) { RaygunLogger.v("RUM - onActivityCreated"); - if (RUM.currentActivity.get() == null) { + if (RUM.currentActivity == null || RUM.currentActivity.get() == null) { if (doesNeedSessionRotation()) { rotateSession(currentSessionUser,currentSessionUser); } } - if (activity != RUM.currentActivity.get()) { + if (RUM.currentActivity == null || RUM.currentActivity != null && RUM.currentActivity.get() != activity) { RUM.currentActivity = new WeakReference<>(activity); RUM.loadingActivity = new WeakReference<>(activity); RUM.startTime = System.nanoTime(); @@ -126,13 +126,13 @@ public void onActivityCreated(Activity activity, Bundle bundle) { @Override public void onActivityStarted(Activity activity) { RaygunLogger.v("RUM - onActivityStarted"); - if (RUM.currentActivity.get() == null) { + if (RUM.currentActivity == null || RUM.currentActivity.get() == null) { if (doesNeedSessionRotation()) { rotateSession(currentSessionUser,currentSessionUser); } } - if (activity != RUM.currentActivity.get()) { + if (RUM.currentActivity == null || RUM.currentActivity != null && RUM.currentActivity.get() != activity) { RUM.currentActivity = new WeakReference<>(activity); RUM.loadingActivity = new WeakReference<>(activity); RUM.startTime = System.nanoTime(); @@ -144,7 +144,7 @@ public void onActivityStarted(Activity activity) { @Override public void onActivityResumed(Activity activity) { RaygunLogger.v("RUM - onActivityResumed"); - if (RUM.currentActivity.get() == null) { + if (RUM.currentActivity == null || RUM.currentActivity.get() == null) { if (doesNeedSessionRotation()) { rotateSession(currentSessionUser,currentSessionUser); } @@ -153,13 +153,13 @@ public void onActivityResumed(Activity activity) { String activityName = getActivityName(activity); long duration = 0; - if (activity == RUM.currentActivity.get()) { + if (RUM.currentActivity != null && activity == RUM.currentActivity.get()) { long diff = System.nanoTime() - RUM.startTime; duration = TimeUnit.NANOSECONDS.toMillis(diff); } RUM.currentActivity = new WeakReference<>(activity); - RUM.loadingActivity.clear(); + RUM.loadingActivity = null; sendRUMTimingEvent(RaygunRUMEventType.ACTIVITY_LOADED, activityName, duration); @@ -175,9 +175,9 @@ public void onActivityPaused(Activity activity) { @Override public void onActivityStopped(Activity activity) { RaygunLogger.v("RUM - onActivityStopped"); - if (activity == RUM.currentActivity.get()) { - RUM.currentActivity.clear(); - RUM.loadingActivity.clear(); + if (RUM.currentActivity != null && activity == RUM.currentActivity.get()) { + RUM.currentActivity = null; + RUM.loadingActivity = null; RUM.lastSeenTime = System.currentTimeMillis(); } } From 6791a1f92423f359ee4a89f6c3b2cc48f4ed2d14 Mon Sep 17 00:00:00 2001 From: Kai Koenig Date: Mon, 17 Jun 2019 22:44:18 +0200 Subject: [PATCH 4/6] Implemented Second Activity in sample app --- app/src/main/AndroidManifest.xml | 5 + .../raygun4android/sample/MainActivity.kt | 22 +++-- .../raygun4android/sample/SecondActivity.kt | 83 ++++++++++++++++ app/src/main/res/layout/activity_main.xml | 9 ++ app/src/main/res/layout/activity_second.xml | 94 +++++++++++++++++++ 5 files changed, 205 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/com/raygun/raygun4android/sample/SecondActivity.kt create mode 100644 app/src/main/res/layout/activity_second.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d93a21c..091a47e 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -19,6 +19,11 @@ + + + (R.id.button_send) + val buttonCrash = findViewById