Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
added AppStartMetrics.registerApplicationForegroundCheck in the Sentr…
…yPerformanceProvider, other than AppStartMetrics.onApplicationCreate
  • Loading branch information
stefanosiano committed Jul 10, 2024
commit 9b09e7300592a650092927e70c58f2152f3e0b2e
1 change: 1 addition & 0 deletions sentry-android-core/api/sentry-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ public class io/sentry/android/core/performance/AppStartMetrics : io/sentry/andr
public static fun onApplicationPostCreate (Landroid/app/Application;)V
public static fun onContentProviderCreate (Landroid/content/ContentProvider;)V
public static fun onContentProviderPostCreate (Landroid/content/ContentProvider;)V
public fun registerApplicationForegroundCheck (Landroid/app/Application;)V
public fun setAppLaunchedInForeground (Z)V
public fun setAppStartProfiler (Lio/sentry/ITransactionProfiler;)V
public fun setAppStartSamplingDecision (Lio/sentry/TracesSamplingDecision;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ private void onAppLaunched(

final @NotNull TimeSpan appStartTimespan = appStartMetrics.getAppStartTimeSpan();
appStartTimespan.setStartedAt(Process.getStartUptimeMillis());
appStartMetrics.registerApplicationForegroundCheck(app);

final AtomicBoolean firstDrawDone = new AtomicBoolean(false);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,20 +223,29 @@ public static void onApplicationCreate(final @NotNull Application application) {
final @NotNull AppStartMetrics instance = getInstance();
if (instance.applicationOnCreate.hasNotStarted()) {
instance.applicationOnCreate.setStartedAt(now);
application.registerActivityLifecycleCallbacks(instance);
instance.appLaunchedInForeground =
instance.appLaunchedInForeground || ContextUtils.isForegroundImportance();
new Handler(Looper.getMainLooper())
.post(
() -> {
// if no activity has ever been created, app was launched in background
if (instance.onCreateTime == null) {
instance.appLaunchedInForeground = false;
}
});
instance.registerApplicationForegroundCheck(application);
}
}

/**
* Register a callback to check if an activity was started after the application was created
*
* @param application The application object to register the callback to
*/
public void registerApplicationForegroundCheck(final @NotNull Application application) {
application.registerActivityLifecycleCallbacks(instance);
new Handler(Looper.getMainLooper())
.post(
() -> {
// if no activity has ever been created, app was launched in background
if (onCreateTime == null) {
appLaunchedInForeground = false;
}
});
}

@Override
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
// An activity already called onCreate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.mockito.kotlin.any
import org.mockito.kotlin.eq
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.times
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import org.robolectric.annotation.Config
Expand Down Expand Up @@ -164,7 +165,8 @@ class SentryPerformanceProviderTest {
fun `provider properly registers and unregisters ActivityLifecycleCallbacks`() {
val provider = fixture.getSut()

verify(fixture.mockContext).registerActivityLifecycleCallbacks(any())
// It register once for the provider itself and once for the appStartMetrics
verify(fixture.mockContext, times(2)).registerActivityLifecycleCallbacks(any())
provider.onAppStartDone()
verify(fixture.mockContext).unregisterActivityLifecycleCallbacks(any())
}
Expand Down