From 504acdaae47e3f285cdb6dee26fa0f5a39a06500 Mon Sep 17 00:00:00 2001 From: stefanosiano Date: Mon, 12 Aug 2024 11:11:30 +0200 Subject: [PATCH 1/5] added logs for test added post on main thread to post check on main thread --- .../core/performance/AppStartMetrics.java | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/performance/AppStartMetrics.java b/sentry-android-core/src/main/java/io/sentry/android/core/performance/AppStartMetrics.java index a220f5eb4a..270a55fdc4 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/performance/AppStartMetrics.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/performance/AppStartMetrics.java @@ -7,6 +7,8 @@ import android.os.Handler; import android.os.Looper; import android.os.SystemClock; +import android.util.Log; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.VisibleForTesting; @@ -241,24 +243,31 @@ public void registerApplicationForegroundCheck(final @NotNull Application applic isCallbackRegistered = true; appLaunchedInForeground = appLaunchedInForeground || ContextUtils.isForegroundImportance(); application.registerActivityLifecycleCallbacks(instance); + checkCreateTimeOnMain(application); + new Handler(Looper.getMainLooper()).post(() -> checkCreateTimeOnMain(application)); + } + + private void checkCreateTimeOnMain(final @NotNull Application application) { new Handler(Looper.getMainLooper()) - .post( - () -> { - // if no activity has ever been created, app was launched in background - if (onCreateTime == null) { - appLaunchedInForeground = false; - } - application.unregisterActivityLifecycleCallbacks(instance); - // we stop the app start profiler, as it's useless and likely to timeout - if (appStartProfiler != null && appStartProfiler.isRunning()) { - appStartProfiler.close(); - appStartProfiler = null; - } - }); + .post( + () -> { + Log.e("AppStartTest", "Application callback check"); + // if no activity has ever been created, app was launched in background + if (onCreateTime == null) { + appLaunchedInForeground = false; + } + application.unregisterActivityLifecycleCallbacks(instance); + // we stop the app start profiler, as it's useless and likely to timeout + if (appStartProfiler != null && appStartProfiler.isRunning()) { + appStartProfiler.close(); + appStartProfiler = null; + } + }); } @Override public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) { + Log.e("AppStartTest", "Activity created"); // An activity already called onCreate() if (!appLaunchedInForeground || onCreateTime != null) { return; From e08674aa191d09a390c0fe80dfa3f6605fa5a200 Mon Sep 17 00:00:00 2001 From: stefanosiano Date: Mon, 12 Aug 2024 12:15:46 +0200 Subject: [PATCH 2/5] removed first check --- .../io/sentry/android/core/performance/AppStartMetrics.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/performance/AppStartMetrics.java b/sentry-android-core/src/main/java/io/sentry/android/core/performance/AppStartMetrics.java index 270a55fdc4..9847c1fbcd 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/performance/AppStartMetrics.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/performance/AppStartMetrics.java @@ -243,7 +243,7 @@ public void registerApplicationForegroundCheck(final @NotNull Application applic isCallbackRegistered = true; appLaunchedInForeground = appLaunchedInForeground || ContextUtils.isForegroundImportance(); application.registerActivityLifecycleCallbacks(instance); - checkCreateTimeOnMain(application); +// checkCreateTimeOnMain(application); new Handler(Looper.getMainLooper()).post(() -> checkCreateTimeOnMain(application)); } From 89e37828b1ec1cfd721cb938d4327eae9735ebd8 Mon Sep 17 00:00:00 2001 From: stefanosiano Date: Mon, 12 Aug 2024 12:19:29 +0200 Subject: [PATCH 3/5] reverted logic (for test) --- .../io/sentry/android/core/performance/AppStartMetrics.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/performance/AppStartMetrics.java b/sentry-android-core/src/main/java/io/sentry/android/core/performance/AppStartMetrics.java index 9847c1fbcd..3ad1282dc1 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/performance/AppStartMetrics.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/performance/AppStartMetrics.java @@ -243,8 +243,8 @@ public void registerApplicationForegroundCheck(final @NotNull Application applic isCallbackRegistered = true; appLaunchedInForeground = appLaunchedInForeground || ContextUtils.isForegroundImportance(); application.registerActivityLifecycleCallbacks(instance); -// checkCreateTimeOnMain(application); - new Handler(Looper.getMainLooper()).post(() -> checkCreateTimeOnMain(application)); + checkCreateTimeOnMain(application); +// new Handler(Looper.getMainLooper()).post(() -> checkCreateTimeOnMain(application)); } private void checkCreateTimeOnMain(final @NotNull Application application) { From 8a327cbe942004f27e4f15c9890808b6e74ba8db Mon Sep 17 00:00:00 2001 From: stefanosiano Date: Mon, 12 Aug 2024 12:26:54 +0200 Subject: [PATCH 4/5] cleaned logic --- .../core/performance/AppStartMetrics.java | 37 +++++++++---------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/sentry-android-core/src/main/java/io/sentry/android/core/performance/AppStartMetrics.java b/sentry-android-core/src/main/java/io/sentry/android/core/performance/AppStartMetrics.java index 3ad1282dc1..90c49ed07a 100644 --- a/sentry-android-core/src/main/java/io/sentry/android/core/performance/AppStartMetrics.java +++ b/sentry-android-core/src/main/java/io/sentry/android/core/performance/AppStartMetrics.java @@ -7,8 +7,6 @@ import android.os.Handler; import android.os.Looper; import android.os.SystemClock; -import android.util.Log; - import androidx.annotation.NonNull; import androidx.annotation.Nullable; import androidx.annotation.VisibleForTesting; @@ -243,31 +241,32 @@ public void registerApplicationForegroundCheck(final @NotNull Application applic isCallbackRegistered = true; appLaunchedInForeground = appLaunchedInForeground || ContextUtils.isForegroundImportance(); application.registerActivityLifecycleCallbacks(instance); - checkCreateTimeOnMain(application); -// new Handler(Looper.getMainLooper()).post(() -> checkCreateTimeOnMain(application)); + // We post on the main thread a task to post a check on the main thread. On Pixel devices + // (possibly others) the first task posted on the main thread is called before the + // Activity.onCreate callback. This is a workaround for that, so that the Activity.onCreate + // callback is called before the application one. + new Handler(Looper.getMainLooper()).post(() -> checkCreateTimeOnMain(application)); } private void checkCreateTimeOnMain(final @NotNull Application application) { new Handler(Looper.getMainLooper()) - .post( - () -> { - Log.e("AppStartTest", "Application callback check"); - // if no activity has ever been created, app was launched in background - if (onCreateTime == null) { - appLaunchedInForeground = false; - } - application.unregisterActivityLifecycleCallbacks(instance); - // we stop the app start profiler, as it's useless and likely to timeout - if (appStartProfiler != null && appStartProfiler.isRunning()) { - appStartProfiler.close(); - appStartProfiler = null; - } - }); + .post( + () -> { + // if no activity has ever been created, app was launched in background + if (onCreateTime == null) { + appLaunchedInForeground = false; + } + application.unregisterActivityLifecycleCallbacks(instance); + // we stop the app start profiler, as it's useless and likely to timeout + if (appStartProfiler != null && appStartProfiler.isRunning()) { + appStartProfiler.close(); + appStartProfiler = null; + } + }); } @Override public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) { - Log.e("AppStartTest", "Activity created"); // An activity already called onCreate() if (!appLaunchedInForeground || onCreateTime != null) { return; From 1e08b610fb55e75d57d2b82134d9ac22ed8d6e1d Mon Sep 17 00:00:00 2001 From: stefanosiano Date: Mon, 12 Aug 2024 12:32:33 +0200 Subject: [PATCH 5/5] updated changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09ecbba6f4..7b500b004f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ ### Fixes +- Fix app start spans missing from Pixel devices ([#3634](https://github.com/getsentry/sentry-java/pull/3634)) - Avoid ArrayIndexOutOfBoundsException on Android cpu data collection ([#3598](https://github.com/getsentry/sentry-java/pull/3598)) - Fix lazy select queries instrumentation ([#3604](https://github.com/getsentry/sentry-java/pull/3604)) - Session Replay: buffer mode improvements ([#3622](https://github.com/getsentry/sentry-java/pull/3622))