Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache results from PackageManager #1288

Merged
merged 3 commits into from
Jun 24, 2021
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
* Include `app.binaryArch` in all events
[#1287](https://github.com/bugsnag/bugsnag-android/pull/1287)

* Cache results from PackageManager
[#1288](https://github.com/bugsnag/bugsnag-android/pull/1288)

## 5.9.4 (2021-05-26)

* Unity: add methods for setting autoNotify and autoDetectAnrs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ static ImmutableConfig convert(Configuration config) {
} catch (IOException ignored) {
// swallow
}
return ImmutableConfigKt.convertToImmutableConfig(config, null);
return ImmutableConfigKt.convertToImmutableConfig(config, null, null, null);
}

static Device generateDevice() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ internal class LastRunInfoStoreTest {
val config = convertToImmutableConfig(
generateConfiguration().apply {
persistenceDirectory = ApplicationProvider.getApplicationContext<Context>().cacheDir
}
},
packageInfo = null,
appInfo = null
)
file = File(config.persistenceDirectory, "last-run-info")
file.delete()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ internal class AppDataCollector(
var codeBundleId: String? = null

private val packageName: String = appContext.packageName
private var packageInfo = packageManager?.getPackageInfo(packageName, 0)
private var appInfo: ApplicationInfo? = packageManager?.getApplicationInfo(packageName, 0)
private var packageInfo = config.packageInfo
fractalwrench marked this conversation as resolved.
Show resolved Hide resolved
private var appInfo: ApplicationInfo? = config.appInfo
fractalwrench marked this conversation as resolved.
Show resolved Hide resolved
private val bgWorkRestricted = isBackgroundWorkRestricted()

private var binaryArch: String? = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.bugsnag.android.internal

import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import androidx.annotation.VisibleForTesting
import com.bugsnag.android.BreadcrumbType
Expand Down Expand Up @@ -47,7 +48,11 @@ data class ImmutableConfig(
val maxPersistedEvents: Int,
val maxPersistedSessions: Int,
val persistenceDirectory: File,
val sendLaunchCrashesSynchronously: Boolean
val sendLaunchCrashesSynchronously: Boolean,

// results cached here to avoid unnecessary lookups in Client.
val packageInfo: PackageInfo?,
val appInfo: ApplicationInfo?
) {

@JvmName("getErrorApiDeliveryParams")
Expand Down Expand Up @@ -118,9 +123,12 @@ data class ImmutableConfig(
}
}

@JvmOverloads
internal fun convertToImmutableConfig(
config: Configuration,
buildUuid: String? = null
buildUuid: String? = null,
packageInfo: PackageInfo? = null,
appInfo: ApplicationInfo? = null
): ImmutableConfig {
val errorTypes = when {
config.autoDetectErrors -> config.enabledErrorTypes.copy()
Expand Down Expand Up @@ -151,7 +159,9 @@ internal fun convertToImmutableConfig(
maxPersistedSessions = config.maxPersistedSessions,
enabledBreadcrumbTypes = config.enabledBreadcrumbTypes?.toSet(),
persistenceDirectory = config.persistenceDirectory!!,
sendLaunchCrashesSynchronously = config.sendLaunchCrashesSynchronously
sendLaunchCrashesSynchronously = config.sendLaunchCrashesSynchronously,
packageInfo = packageInfo,
appInfo = appInfo
)
}

Expand Down Expand Up @@ -208,7 +218,7 @@ internal fun sanitiseConfiguration(
if (configuration.persistenceDirectory == null) {
configuration.persistenceDirectory = appContext.cacheDir
}
return convertToImmutableConfig(configuration, buildUuid)
return convertToImmutableConfig(configuration, buildUuid, packageInfo, appInfo)
}

internal const val RELEASE_STAGE_DEVELOPMENT = "development"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import android.app.ActivityManager
import android.content.Context
import android.content.pm.ApplicationInfo
import android.content.pm.PackageManager
import com.bugsnag.android.BugsnagTestUtils.convert
import com.bugsnag.android.internal.convertToImmutableConfig
import org.junit.Test
import org.junit.runner.RunWith
import org.junit.runners.Parameterized
Expand Down Expand Up @@ -40,14 +40,14 @@ internal class AppMetadataSerializationTest {

// populate metadata fields
`when`(sessionTracker.contextActivity).thenReturn("MyActivity")
`when`(pm.getApplicationInfo(any(), anyInt())).thenReturn(ApplicationInfo())
`when`(pm.getApplicationLabel(any())).thenReturn("MyApp")
`when`(pm.getApplicationInfo(any(), anyInt())).thenReturn(ApplicationInfo())

// construct AppDataCollector object
val appData = AppDataCollector(
context,
pm,
convert(config),
convertToImmutableConfig(config, null, null, ApplicationInfo()),
sessionTracker,
am,
launchCrashTracker,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ static ImmutableConfig generateConfig() throws IOException {
32,
32,
Files.createTempDirectory("foo").toFile(),
true
true,
null,
null
);
}

Expand Down