Skip to content

Commit ab6e139

Browse files
authored
Merge d564f99 into 373370e
2 parents 373370e + d564f99 commit ab6e139

File tree

11 files changed

+225
-330
lines changed

11 files changed

+225
-330
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
### Fixes
6+
7+
- Fix Ensure app start type is set, even when ActivityLifecycleIntegration is not running ([#4216](https://github.com/getsentry/sentry-java/pull/4216))
8+
39
## 7.22.0
410

511
### Fixes

sentry-android-core/api/sentry-android-core.api

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -447,14 +447,15 @@ public class io/sentry/android/core/performance/AppStartMetrics : io/sentry/andr
447447
public static fun getInstance ()Lio/sentry/android/core/performance/AppStartMetrics;
448448
public fun getSdkInitTimeSpan ()Lio/sentry/android/core/performance/TimeSpan;
449449
public fun isAppLaunchedInForeground ()Z
450-
public fun isColdStartValid ()Z
451450
public fun onActivityCreated (Landroid/app/Activity;Landroid/os/Bundle;)V
451+
public fun onActivityDestroyed (Landroid/app/Activity;)V
452+
public fun onActivityStarted (Landroid/app/Activity;)V
452453
public fun onAppStartSpansSent ()V
453454
public static fun onApplicationCreate (Landroid/app/Application;)V
454455
public static fun onApplicationPostCreate (Landroid/app/Application;)V
455456
public static fun onContentProviderCreate (Landroid/content/ContentProvider;)V
456457
public static fun onContentProviderPostCreate (Landroid/content/ContentProvider;)V
457-
public fun registerApplicationForegroundCheck (Landroid/app/Application;)V
458+
public fun registerLifecycleCallbacks (Landroid/app/Application;)V
458459
public fun restartAppStart (J)V
459460
public fun setAppLaunchedInForeground (Z)V
460461
public fun setAppStartProfiler (Lio/sentry/ITransactionProfiler;)V

sentry-android-core/src/main/java/io/sentry/android/core/ActivityLifecycleIntegration.java

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,6 @@ public synchronized void onActivityCreated(
397397
if (!isAllActivityCallbacksAvailable) {
398398
onActivityPreCreated(activity, savedInstanceState);
399399
}
400-
setColdStart(savedInstanceState);
401400
if (hub != null && options != null && options.isEnableScreenTracking()) {
402401
final @Nullable String activityClassName = ClassUtil.getClassName(activity);
403402
hub.configureScope(scope -> scope.setScreen(activityClassName));
@@ -553,17 +552,6 @@ public synchronized void onActivityDestroyed(final @NotNull Activity activity) {
553552
// activity stack still.
554553
// if the activity is opened again and not in memory, transactions will be created normally.
555554
activitiesWithOngoingTransactions.remove(activity);
556-
557-
if (activitiesWithOngoingTransactions.isEmpty()) {
558-
clear();
559-
}
560-
}
561-
562-
private void clear() {
563-
firstActivityCreated = false;
564-
lastPausedTime = new SentryNanotimeDate(new Date(0), 0);
565-
lastPausedUptimeMillis = 0;
566-
activityLifecycleMap.clear();
567555
}
568556

569557
private void finishSpan(final @Nullable ISpan span) {
@@ -705,27 +693,6 @@ WeakHashMap<Activity, ISpan> getTtfdSpanMap() {
705693
return ttfdSpanMap;
706694
}
707695

708-
private void setColdStart(final @Nullable Bundle savedInstanceState) {
709-
if (!firstActivityCreated) {
710-
final @NotNull TimeSpan appStartSpan = AppStartMetrics.getInstance().getAppStartTimeSpan();
711-
// If the app start span already started and stopped, it means the app restarted without
712-
// killing the process, so we are in a warm start
713-
// If the app has an invalid cold start, it means it was started in the background, like
714-
// via BroadcastReceiver, so we consider it a warm start
715-
if ((appStartSpan.hasStarted() && appStartSpan.hasStopped())
716-
|| (!AppStartMetrics.getInstance().isColdStartValid())) {
717-
AppStartMetrics.getInstance().restartAppStart(lastPausedUptimeMillis);
718-
AppStartMetrics.getInstance().setAppStartType(AppStartMetrics.AppStartType.WARM);
719-
} else {
720-
AppStartMetrics.getInstance()
721-
.setAppStartType(
722-
savedInstanceState == null
723-
? AppStartMetrics.AppStartType.COLD
724-
: AppStartMetrics.AppStartType.WARM);
725-
}
726-
}
727-
}
728-
729696
private @NotNull String getTtidDesc(final @NotNull String activityName) {
730697
return activityName + " initial display";
731698
}

sentry-android-core/src/main/java/io/sentry/android/core/SentryAndroid.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public static synchronized void init(
152152
}
153153
}
154154
if (context.getApplicationContext() instanceof Application) {
155-
appStartMetrics.registerApplicationForegroundCheck(
155+
appStartMetrics.registerLifecycleCallbacks(
156156
(Application) context.getApplicationContext());
157157
}
158158
final @NotNull TimeSpan sdkInitTimeSpan = appStartMetrics.getSdkInitTimeSpan();

sentry-android-core/src/main/java/io/sentry/android/core/SentryPerformanceProvider.java

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,13 @@
33
import static io.sentry.Sentry.APP_START_PROFILING_CONFIG_FILE_NAME;
44

55
import android.annotation.SuppressLint;
6-
import android.app.Activity;
76
import android.app.Application;
87
import android.content.Context;
98
import android.content.pm.ProviderInfo;
109
import android.net.Uri;
1110
import android.os.Build;
12-
import android.os.Handler;
13-
import android.os.Looper;
1411
import android.os.Process;
1512
import android.os.SystemClock;
16-
import androidx.annotation.NonNull;
1713
import io.sentry.ILogger;
1814
import io.sentry.ITransactionProfiler;
1915
import io.sentry.JsonSerializer;
@@ -22,9 +18,7 @@
2218
import io.sentry.SentryLevel;
2319
import io.sentry.SentryOptions;
2420
import io.sentry.TracesSamplingDecision;
25-
import io.sentry.android.core.internal.util.FirstDrawDoneListener;
2621
import io.sentry.android.core.internal.util.SentryFrameMetricsCollector;
27-
import io.sentry.android.core.performance.ActivityLifecycleCallbacksAdapter;
2822
import io.sentry.android.core.performance.AppStartMetrics;
2923
import io.sentry.android.core.performance.TimeSpan;
3024
import java.io.BufferedReader;
@@ -33,7 +27,6 @@
3327
import java.io.FileNotFoundException;
3428
import java.io.InputStreamReader;
3529
import java.io.Reader;
36-
import java.util.concurrent.atomic.AtomicBoolean;
3730
import org.jetbrains.annotations.ApiStatus;
3831
import org.jetbrains.annotations.NotNull;
3932
import org.jetbrains.annotations.Nullable;
@@ -185,8 +178,9 @@ private void onAppLaunched(
185178

186179
// performance v2: Uses Process.getStartUptimeMillis()
187180
// requires API level 24+
188-
if (buildInfoProvider.getSdkInfoVersion() < android.os.Build.VERSION_CODES.N) {
189-
return;
181+
if (buildInfoProvider.getSdkInfoVersion() >= android.os.Build.VERSION_CODES.N) {
182+
final @NotNull TimeSpan appStartTimespan = appStartMetrics.getAppStartTimeSpan();
183+
appStartTimespan.setStartedAt(Process.getStartUptimeMillis());
190184
}
191185

192186
if (context instanceof Application) {
@@ -196,40 +190,6 @@ private void onAppLaunched(
196190
return;
197191
}
198192

199-
final @NotNull TimeSpan appStartTimespan = appStartMetrics.getAppStartTimeSpan();
200-
appStartTimespan.setStartedAt(Process.getStartUptimeMillis());
201-
appStartMetrics.registerApplicationForegroundCheck(app);
202-
203-
final AtomicBoolean firstDrawDone = new AtomicBoolean(false);
204-
205-
activityCallback =
206-
new ActivityLifecycleCallbacksAdapter() {
207-
@Override
208-
public void onActivityStarted(@NonNull Activity activity) {
209-
if (firstDrawDone.get()) {
210-
return;
211-
}
212-
if (activity.getWindow() != null) {
213-
FirstDrawDoneListener.registerForNextDraw(
214-
activity, () -> onAppStartDone(), buildInfoProvider);
215-
} else {
216-
new Handler(Looper.getMainLooper()).post(() -> onAppStartDone());
217-
}
218-
}
219-
};
220-
221-
app.registerActivityLifecycleCallbacks(activityCallback);
222-
}
223-
224-
synchronized void onAppStartDone() {
225-
final @NotNull AppStartMetrics appStartMetrics = AppStartMetrics.getInstance();
226-
appStartMetrics.getSdkInitTimeSpan().stop();
227-
appStartMetrics.getAppStartTimeSpan().stop();
228-
229-
if (app != null) {
230-
if (activityCallback != null) {
231-
app.unregisterActivityLifecycleCallbacks(activityCallback);
232-
}
233-
}
193+
appStartMetrics.registerLifecycleCallbacks(app);
234194
}
235195
}

0 commit comments

Comments
 (0)