33import static io .sentry .Sentry .APP_START_PROFILING_CONFIG_FILE_NAME ;
44
55import android .annotation .SuppressLint ;
6- import android .app .Activity ;
76import android .app .Application ;
87import android .content .Context ;
98import android .content .pm .ProviderInfo ;
109import android .net .Uri ;
1110import android .os .Build ;
12- import android .os .Bundle ;
13- import android .os .Handler ;
14- import android .os .Looper ;
1511import android .os .Process ;
1612import android .os .SystemClock ;
17- import android .util .Log ;
18- import androidx .annotation .NonNull ;
1913import io .sentry .ILogger ;
2014import io .sentry .ITransactionProfiler ;
2115import io .sentry .JsonSerializer ;
2418import io .sentry .SentryLevel ;
2519import io .sentry .SentryOptions ;
2620import io .sentry .TracesSamplingDecision ;
27- import io .sentry .android .core .internal .util .FirstDrawDoneListener ;
2821import io .sentry .android .core .internal .util .SentryFrameMetricsCollector ;
29- import io .sentry .android .core .performance .ActivityLifecycleCallbacksAdapter ;
3022import io .sentry .android .core .performance .AppStartMetrics ;
3123import io .sentry .android .core .performance .TimeSpan ;
3224import java .io .BufferedReader ;
3527import java .io .FileNotFoundException ;
3628import java .io .InputStreamReader ;
3729import java .io .Reader ;
38- import java .util .concurrent .atomic .AtomicBoolean ;
39- import java .util .concurrent .atomic .AtomicInteger ;
4030import org .jetbrains .annotations .ApiStatus ;
4131import org .jetbrains .annotations .NotNull ;
4232import org .jetbrains .annotations .Nullable ;
@@ -54,8 +44,6 @@ public final class SentryPerformanceProvider extends EmptySecureContentProvider
5444
5545 private final @ NotNull ILogger logger ;
5646 private final @ NotNull BuildInfoProvider buildInfoProvider ;
57- private final AtomicInteger activeActivitiesCounter = new AtomicInteger ();
58- private final AtomicBoolean firstDrawDone = new AtomicBoolean (false );
5947
6048 @ TestOnly
6149 SentryPerformanceProvider (
@@ -190,8 +178,9 @@ private void onAppLaunched(
190178
191179 // performance v2: Uses Process.getStartUptimeMillis()
192180 // requires API level 24+
193- if (buildInfoProvider .getSdkInfoVersion () < android .os .Build .VERSION_CODES .N ) {
194- return ;
181+ if (buildInfoProvider .getSdkInfoVersion () >= android .os .Build .VERSION_CODES .N ) {
182+ final @ NotNull TimeSpan appStartTimespan = appStartMetrics .getAppStartTimeSpan ();
183+ appStartTimespan .setStartedAt (Process .getStartUptimeMillis ());
195184 }
196185
197186 if (context instanceof Application ) {
@@ -201,61 +190,6 @@ private void onAppLaunched(
201190 return ;
202191 }
203192
204- final @ NotNull TimeSpan appStartTimespan = appStartMetrics .getAppStartTimeSpan ();
205- appStartTimespan .setStartedAt (Process .getStartUptimeMillis ());
206- appStartMetrics .registerApplicationForegroundCheck (app );
207-
208- activityCallback =
209- new ActivityLifecycleCallbacksAdapter () {
210-
211- @ Override
212- public void onActivityCreated (
213- @ NotNull Activity activity , @ Nullable Bundle savedInstanceState ) {
214- Log .d ("TAG" , "onActivityCreated" );
215- activeActivitiesCounter .incrementAndGet ();
216-
217- // In case the SDK gets initialized async or the
218- // ActivityLifecycleIntegration is not enabled (e.g on RN due to Context not being
219- // instanceof Application)
220- // the app start type never gets set
221- if (!firstDrawDone .get ()) {
222- final long now = SystemClock .uptimeMillis ();
223- AppStartMetrics .getInstance ().updateAppStartType (savedInstanceState != null , now );
224- }
225- }
226-
227- @ Override
228- public void onActivityStarted (@ NotNull Activity activity ) {
229- if (firstDrawDone .get ()) {
230- return ;
231- }
232- if (activity .getWindow () != null ) {
233- FirstDrawDoneListener .registerForNextDraw (
234- activity , () -> onAppStartDone (), buildInfoProvider );
235- } else {
236- new Handler (Looper .getMainLooper ()).post (() -> onAppStartDone ());
237- }
238- }
239-
240- @ Override
241- public void onActivityDestroyed (@ NonNull Activity activity ) {
242- final int remainingActivities = activeActivitiesCounter .decrementAndGet ();
243- // if the app is moving into background, reset firstDrawDone
244- // as the next Activity is considered like a new app start
245- if (remainingActivities == 0 && !activity .isChangingConfigurations ()) {
246- firstDrawDone .set (false );
247- }
248- }
249- };
250-
251- app .registerActivityLifecycleCallbacks (activityCallback );
252- }
253-
254- synchronized void onAppStartDone () {
255- if (!firstDrawDone .getAndSet (true )) {
256- final @ NotNull AppStartMetrics appStartMetrics = AppStartMetrics .getInstance ();
257- appStartMetrics .getSdkInitTimeSpan ().stop ();
258- appStartMetrics .getAppStartTimeSpan ().stop ();
259- }
193+ appStartMetrics .registerLifecycleCallbacks (app );
260194 }
261195}
0 commit comments