Skip to content

Commit de50a39

Browse files
committed
fix(startup): more startup processing to complete in the background
1 parent 88c6179 commit de50a39

19 files changed

+110
-82
lines changed

bugsnag-android-core/detekt-baseline.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<ID>CyclomaticComplexMethod:ConfigInternal.kt$ConfigInternal$fun getConfigDifferences(): Map&lt;String, Any></ID>
77
<ID>ImplicitDefaultLocale:Deliverable.kt$Deliverable$String.format("%02x", byte)</ID>
88
<ID>LongParameterList:App.kt$App$( /** * The architecture of the running application binary */ var binaryArch: String?, /** * The package name of the application */ var id: String?, /** * The release stage set in [Configuration.releaseStage] */ var releaseStage: String?, /** * The version of the application set in [Configuration.version] */ var version: String?, /** The revision ID from the manifest (React Native apps only) */ var codeBundleId: String?, /** * The unique identifier for the build of the application set in [Configuration.buildUuid] */ buildUuid: Provider&lt;String?>?, /** * The application type set in [Configuration#version] */ var type: String?, /** * The version code of the application set in [Configuration.versionCode] */ var versionCode: Number? )</ID>
9-
<ID>LongParameterList:AppDataCollector.kt$AppDataCollector$( appContext: Context, private val packageManager: PackageManager?, private val config: ImmutableConfig, private val sessionTracker: SessionTracker, private val activityManager: ActivityManager?, private val launchCrashTracker: LaunchCrashTracker, private val memoryTrimState: MemoryTrimState )</ID>
9+
<ID>LongParameterList:AppDataCollector.kt$AppDataCollector$( appContext: Context, private val packageManager: PackageManager?, private val config: ImmutableConfig, private val sessionTracker: Provider&lt;SessionTracker>, private val activityManager: ActivityManager?, private val launchCrashTracker: LaunchCrashTracker, private val memoryTrimState: MemoryTrimState )</ID>
1010
<ID>LongParameterList:AppWithState.kt$AppWithState$( binaryArch: String?, id: String?, releaseStage: String?, version: String?, codeBundleId: String?, buildUuid: Provider&lt;String?>?, type: String?, versionCode: Number?, /** * The number of milliseconds the application was running before the event occurred */ var duration: Number?, /** * The number of milliseconds the application was running in the foreground before the * event occurred */ var durationInForeground: Number?, /** * Whether the application was in the foreground when the event occurred */ var inForeground: Boolean?, /** * Whether the application was launching when the event occurred */ var isLaunching: Boolean? )</ID>
1111
<ID>LongParameterList:AppWithState.kt$AppWithState$( binaryArch: String?, id: String?, releaseStage: String?, version: String?, codeBundleId: String?, buildUuid: String?, type: String?, versionCode: Number?, /** * The number of milliseconds the application was running before the event occurred */ duration: Number?, /** * The number of milliseconds the application was running in the foreground before the * event occurred */ durationInForeground: Number?, /** * Whether the application was in the foreground when the event occurred */ inForeground: Boolean?, /** * Whether the application was launching when the event occurred */ isLaunching: Boolean? )</ID>
1212
<ID>LongParameterList:AppWithState.kt$AppWithState$( config: ImmutableConfig, binaryArch: String?, id: String?, releaseStage: String?, version: String?, codeBundleId: String?, duration: Number?, durationInForeground: Number?, inForeground: Boolean?, isLaunching: Boolean? )</ID>

bugsnag-android-core/src/androidTest/java/com/bugsnag/android/AppDataCollectorForegroundTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.bugsnag.android
22

33
import android.content.Context
44
import android.os.SystemClock
5+
import com.bugsnag.android.internal.dag.ValueProvider
56
import org.junit.Assert.assertEquals
67
import org.junit.Assert.assertTrue
78
import org.junit.Before
@@ -41,7 +42,7 @@ class AppDataCollectorForegroundTest {
4142
appContext,
4243
null,
4344
config,
44-
sessionTracker,
45+
ValueProvider(sessionTracker),
4546
null,
4647
launchCrashTracker,
4748
memoryTrimState

bugsnag-android-core/src/androidTest/java/com/bugsnag/android/AppDataCollectorTest.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.content.pm.PackageManager
66
import android.os.Build
77
import android.os.Process
88
import androidx.test.core.app.ApplicationProvider
9+
import com.bugsnag.android.internal.dag.ValueProvider
910
import org.junit.Assert.assertEquals
1011
import org.junit.Assert.assertNull
1112
import org.junit.Assert.assertTrue
@@ -49,7 +50,7 @@ class AppDataCollectorTest {
4950
context,
5051
context.packageManager,
5152
client.immutableConfig,
52-
client.sessionTracker,
53+
ValueProvider(client.sessionTracker),
5354
am,
5455
client.launchCrashTracker,
5556
client.memoryTrimState
@@ -70,7 +71,7 @@ class AppDataCollectorTest {
7071
context,
7172
context.packageManager,
7273
client.immutableConfig,
73-
client.sessionTracker,
74+
ValueProvider(client.sessionTracker),
7475
am,
7576
client.launchCrashTracker,
7677
client.memoryTrimState
@@ -93,7 +94,7 @@ class AppDataCollectorTest {
9394
context,
9495
context.packageManager,
9596
client.immutableConfig,
96-
client.sessionTracker,
97+
ValueProvider(client.sessionTracker),
9798
am,
9899
client.launchCrashTracker,
99100
client.memoryTrimState
@@ -115,7 +116,7 @@ class AppDataCollectorTest {
115116
context,
116117
packageManager,
117118
client.immutableConfig,
118-
client.sessionTracker,
119+
ValueProvider(client.sessionTracker),
119120
am,
120121
client.launchCrashTracker,
121122
client.memoryTrimState
@@ -144,7 +145,7 @@ class AppDataCollectorTest {
144145
context,
145146
packageManager,
146147
client.immutableConfig,
147-
client.sessionTracker,
148+
ValueProvider(client.sessionTracker),
148149
am,
149150
client.launchCrashTracker,
150151
client.memoryTrimState
@@ -171,7 +172,7 @@ class AppDataCollectorTest {
171172
context,
172173
packageManager,
173174
client.immutableConfig,
174-
client.sessionTracker,
175+
ValueProvider(client.sessionTracker),
175176
am,
176177
client.launchCrashTracker,
177178
client.memoryTrimState
@@ -198,7 +199,7 @@ class AppDataCollectorTest {
198199
context,
199200
packageManager,
200201
client.immutableConfig,
201-
client.sessionTracker,
202+
ValueProvider(client.sessionTracker),
202203
am,
203204
client.launchCrashTracker,
204205
client.memoryTrimState

bugsnag-android-core/src/androidTest/java/com/bugsnag/android/FileStoreTest.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package com.bugsnag.android
22

33
import android.app.Application
44
import androidx.test.core.app.ApplicationProvider
5+
import com.bugsnag.android.internal.dag.ValueProvider
56
import org.junit.Assert.assertEquals
67
import org.junit.Test
78
import org.junit.runner.RunWith
@@ -48,6 +49,6 @@ internal class CustomFileStore(
4849
folder: File,
4950
maxStoreCount: Int,
5051
delegate: Delegate?
51-
) : FileStore(folder, maxStoreCount, NoopLogger, delegate) {
52+
) : FileStore(folder, maxStoreCount, NoopLogger, ValueProvider(delegate)) {
5253
override fun getFilename(obj: Any?) = "foo.json"
5354
}

bugsnag-android-core/src/main/java/com/bugsnag/android/AppDataCollector.kt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import android.os.Build.VERSION_CODES
2424
import android.os.Process
2525
import android.os.SystemClock
2626
import com.bugsnag.android.internal.ImmutableConfig
27+
import com.bugsnag.android.internal.dag.Provider
2728

2829
/**
2930
* Collects various data on the application state
@@ -32,7 +33,7 @@ internal class AppDataCollector(
3233
appContext: Context,
3334
private val packageManager: PackageManager?,
3435
private val config: ImmutableConfig,
35-
private val sessionTracker: SessionTracker,
36+
private val sessionTracker: Provider<SessionTracker>,
3637
private val activityManager: ActivityManager?,
3738
private val launchCrashTracker: LaunchCrashTracker,
3839
private val memoryTrimState: MemoryTrimState
@@ -54,7 +55,7 @@ internal class AppDataCollector(
5455
App(config, binaryArch, packageName, releaseStage, versionName, codeBundleId)
5556

5657
fun generateAppWithState(): AppWithState {
57-
val inForeground = sessionTracker.isInForeground
58+
val inForeground = sessionTracker.get().isInForeground
5859
val durationInForeground = calculateDurationInForeground(inForeground)
5960

6061
return AppWithState(
@@ -118,7 +119,7 @@ internal class AppDataCollector(
118119
fun getAppDataMetadata(): MutableMap<String, Any?> {
119120
val map = HashMap<String, Any?>()
120121
map["name"] = appName
121-
map["activeScreen"] = sessionTracker.contextActivity
122+
map["activeScreen"] = sessionTracker.get().contextActivity
122123
map["lowMemory"] = memoryTrimState.isLowMemory
123124
map["memoryTrimLevel"] = memoryTrimState.trimLevelDescription
124125
map["processImportance"] = getProcessImportance()
@@ -168,15 +169,15 @@ internal class AppDataCollector(
168169
*
169170
* @return the duration in ms
170171
*/
171-
internal fun calculateDurationInForeground(inForeground: Boolean? = sessionTracker.isInForeground): Long? {
172+
internal fun calculateDurationInForeground(inForeground: Boolean? = sessionTracker.get().isInForeground): Long? {
172173
if (inForeground == null) {
173174
return null
174175
}
175176

176177
val nowMs = SystemClock.elapsedRealtime()
177178
var durationMs: Long = 0
178179

179-
val sessionStartTimeMs: Long = sessionTracker.lastEnteredForegroundMs
180+
val sessionStartTimeMs: Long = sessionTracker.get().lastEnteredForegroundMs
180181

181182
if (inForeground && sessionStartTimeMs != 0L) {
182183
durationMs = nowMs - sessionStartTimeMs

bugsnag-android-core/src/main/java/com/bugsnag/android/DataCollectionModule.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ internal class DataCollectionModule(
3434
ctx,
3535
ctx.packageManager,
3636
cfg,
37-
trackerModule.sessionTracker.get(),
37+
trackerModule.sessionTracker,
3838
systemServiceModule.activityManager,
3939
trackerModule.launchCrashTracker,
4040
memoryTrimState

bugsnag-android-core/src/main/java/com/bugsnag/android/EventStorageModule.kt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ internal class EventStorageModule(
2929
cfg.logger,
3030
cfg,
3131
systemServiceModule.storageManager,
32-
dataCollectionModule.appDataCollector.get(),
32+
dataCollectionModule.appDataCollector,
3333
dataCollectionModule.deviceDataCollector,
34-
trackerModule.sessionTracker.get(),
34+
trackerModule.sessionTracker,
3535
notifier,
3636
bgTaskService
3737
) else null
@@ -43,7 +43,7 @@ internal class EventStorageModule(
4343
cfg.logger,
4444
notifier,
4545
bgTaskService,
46-
delegate.getOrNull(),
46+
delegate,
4747
callbackState
4848
)
4949
}

bugsnag-android-core/src/main/java/com/bugsnag/android/EventStore.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.bugsnag.android.internal.BackgroundTaskService
99
import com.bugsnag.android.internal.ForegroundDetector
1010
import com.bugsnag.android.internal.ImmutableConfig
1111
import com.bugsnag.android.internal.TaskType
12+
import com.bugsnag.android.internal.dag.Provider
1213
import java.io.File
1314
import java.util.Calendar
1415
import java.util.Date
@@ -27,7 +28,7 @@ internal class EventStore(
2728
logger: Logger,
2829
notifier: Notifier,
2930
bgTaskService: BackgroundTaskService,
30-
delegate: Delegate?,
31+
delegate: Provider<out Delegate?>?,
3132
callbackState: CallbackState
3233
) : FileStore(
3334
File(config.persistenceDirectory.value, "bugsnag/errors"),

bugsnag-android-core/src/main/java/com/bugsnag/android/FileStore.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.bugsnag.android
22

33
import com.bugsnag.android.JsonStream.Streamable
4+
import com.bugsnag.android.internal.dag.Provider
45
import java.io.BufferedWriter
56
import java.io.File
67
import java.io.FileNotFoundException
@@ -15,7 +16,7 @@ internal abstract class FileStore(
1516
val storageDir: File,
1617
private val maxStoreCount: Int,
1718
protected open val logger: Logger,
18-
protected val delegate: Delegate?
19+
protected val delegate: Provider<out Delegate?>?
1920
) {
2021
internal fun interface Delegate {
2122
/**
@@ -66,7 +67,7 @@ internal abstract class FileStore(
6667
out.write(content)
6768
} catch (exc: Exception) {
6869
val eventFile = File(filePath)
69-
delegate?.onErrorIOFailure(exc, eventFile, "NDK Crash report copy")
70+
delegate?.getOrNull()?.onErrorIOFailure(exc, eventFile, "NDK Crash report copy")
7071
IOUtils.deleteFile(eventFile, logger)
7172
} finally {
7273
try {
@@ -100,7 +101,7 @@ internal abstract class FileStore(
100101
logger.w("Ignoring FileNotFoundException - unable to create file", exc)
101102
} catch (exc: Exception) {
102103
val eventFile = File(filename)
103-
delegate?.onErrorIOFailure(exc, eventFile, "Crash report serialization")
104+
delegate?.getOrNull()?.onErrorIOFailure(exc, eventFile, "Crash report serialization")
104105
IOUtils.deleteFile(eventFile, logger)
105106
} finally {
106107
IOUtils.closeQuietly(stream)

bugsnag-android-core/src/main/java/com/bugsnag/android/InternalReportDelegate.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,20 +32,20 @@ class InternalReportDelegate implements EventStore.Delegate {
3232
@Nullable
3333
final StorageManager storageManager;
3434

35-
final AppDataCollector appDataCollector;
35+
final Provider<AppDataCollector> appDataCollector;
3636
final Provider<DeviceDataCollector> deviceDataCollector;
3737
final Context appContext;
38-
final SessionTracker sessionTracker;
38+
final Provider<SessionTracker> sessionTracker;
3939
final Notifier notifier;
4040
final BackgroundTaskService backgroundTaskService;
4141

4242
InternalReportDelegate(Context context,
4343
Logger logger,
4444
ImmutableConfig immutableConfig,
4545
@Nullable StorageManager storageManager,
46-
AppDataCollector appDataCollector,
46+
Provider<AppDataCollector> appDataCollector,
4747
Provider<DeviceDataCollector> deviceDataCollector,
48-
SessionTracker sessionTracker,
48+
Provider<SessionTracker> sessionTracker,
4949
Notifier notifier,
5050
BackgroundTaskService backgroundTaskService) {
5151
this.logger = logger;
@@ -101,7 +101,7 @@ void recordStorageCacheBehavior(Event event) {
101101
* This is intended for internal use only, and reports will not be visible to end-users.
102102
*/
103103
void reportInternalBugsnagError(@NonNull Event event) {
104-
event.setApp(appDataCollector.generateAppWithState());
104+
event.setApp(appDataCollector.get().generateAppWithState());
105105
event.setDevice(deviceDataCollector.get().generateDeviceWithState(new Date().getTime()));
106106

107107
event.addMetadata(INTERNAL_DIAGNOSTICS_TAB, "notifierName", notifier.getName());

0 commit comments

Comments
 (0)