Skip to content

Commit

Permalink
refactor(exitinfo): replace the ExitInfoPlugin constructor arguments …
Browse files Browse the repository at this point in the history
…with a more Java friendly ExitInfoPluginConfiguration
  • Loading branch information
lemnik committed Oct 3, 2023
1 parent 215f252 commit 1df7ea3
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 32 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
public final class com/bugsnag/android/BugsnagExitInfoPlugin : com/bugsnag/android/Plugin {
public fun <init> ()V
public fun <init> (Z)V
public fun <init> (ZZ)V
public fun <init> (ZZZ)V
public synthetic fun <init> (ZZZILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun <init> (Lcom/bugsnag/android/ExitInfoPluginConfiguration;)V
public synthetic fun <init> (Lcom/bugsnag/android/ExitInfoPluginConfiguration;ILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun load (Lcom/bugsnag/android/Client;)V
public fun unload ()V
}

public final class com/bugsnag/android/ExitInfoPluginConfiguration {
public fun <init> ()V
public fun <init> (ZZZ)V
public synthetic fun <init> (ZZZILkotlin/jvm/internal/DefaultConstructorMarker;)V
public fun equals (Ljava/lang/Object;)Z
public final fun getDisableProcessStateSummaryOverride ()Z
public final fun getIncludeLogcat ()Z
public final fun getListOpenFds ()Z
public fun hashCode ()I
public final fun setDisableProcessStateSummaryOverride (Z)V
public final fun setIncludeLogcat (Z)V
public final fun setListOpenFds (Z)V
}

8 changes: 7 additions & 1 deletion bugsnag-plugin-android-exitinfo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,20 @@ dependencies {
implementation 'com.google.protobuf:protobuf-javalite:3.24.2'
}

android.libraryVariants.configureEach { variant ->
variant.processJavaResourcesProvider.configure {
exclude('**/*.proto')
}
}

protobuf {
protoc {
artifact = 'com.google.protobuf:protoc:3.24.2'
}
generateProtoTasks {
all().configureEach { task ->
task.builtins {
java{
java {
option "lite"
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,17 @@ import androidx.annotation.RequiresApi

@RequiresApi(Build.VERSION_CODES.R)
class BugsnagExitInfoPlugin @JvmOverloads constructor(

/**
* Whether to add the list of open FDs to correlated reports
*/
private val listOpenFds: Boolean = true,

/**
* Whether to report stored logcat messages metadata
*/
private val includeLogcat: Boolean = false,

/**
* Turn of event correlation based on the processStateSummary field, this can
* set to `true` if the field is required by the app
*/
private val disableProcessStateSummaryOverride: Boolean = false

configuration: ExitInfoPluginConfiguration = ExitInfoPluginConfiguration()
) : Plugin {

private var exitInfoCallback: ExitInfoCallback? = null
private val configuration = configuration.copy()

override fun load(client: Client) {
if (!disableProcessStateSummaryOverride) {
if (!configuration.disableProcessStateSummaryOverride) {
client.addOnSession(
OnSessionCallback { session: Session ->
val am =
client.appContext.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
am.setProcessStateSummary(session.id.toByteArray())
val am = client.appContext.safeGetActivityManager()
am?.setProcessStateSummary(session.id.toByteArray())
return@OnSessionCallback true
}
)
Expand All @@ -44,15 +27,28 @@ class BugsnagExitInfoPlugin @JvmOverloads constructor(
val oldPid = exitInfoPluginStore.load()
exitInfoPluginStore.persist(android.os.Process.myPid())

exitInfoCallback = ExitInfoCallback(
val exitInfoCallback = ExitInfoCallback(
client.appContext,
oldPid,
TombstoneEventEnhancer(client.logger, listOpenFds, includeLogcat),
TraceEventEnhancer(client.logger, client.immutableConfig.projectPackages)
TombstoneEventEnhancer(
client.logger,
configuration.listOpenFds,
configuration.includeLogcat
),
TraceEventEnhancer(
client.logger,
client.immutableConfig.projectPackages
)
)

client.addOnSend(exitInfoCallback)
}

override fun unload() {
override fun unload() = Unit

private fun Context.safeGetActivityManager(): ActivityManager? = try {
getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
} catch (e: Exception) {
null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.bugsnag.android

class ExitInfoPluginConfiguration(
/**
* Whether to add the list of open file descriptors to correlated reports
*/
var listOpenFds: Boolean = true,

/**
* Whether to report stored logcat messages metadata
*/
var includeLogcat: Boolean = false,

/**
* Turn off event correlation based on the
* [processStateSummary](ActivityManager.setProcessStateSummary) field. This can set to `true`
* to stop `BugsnagExitInfoPlugin` overwriting the field if it is being used by the app.
*/
var disableProcessStateSummaryOverride: Boolean = false
) {
constructor() : this(true, false, false)

internal fun copy() =
ExitInfoPluginConfiguration(listOpenFds, includeLogcat, disableProcessStateSummaryOverride)

override fun equals(other: Any?): Boolean {
return other is ExitInfoPluginConfiguration &&
listOpenFds == other.listOpenFds &&
includeLogcat == other.includeLogcat &&
disableProcessStateSummaryOverride == other.disableProcessStateSummaryOverride
}

override fun hashCode(): Int {
var result = listOpenFds.hashCode()
result = 31 * result + includeLogcat.hashCode()
result = 31 * result + disableProcessStateSummaryOverride.hashCode()
return result
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ internal class ExitInfoPluginStore(config: ImmutableConfig) {
try {
val text = pid.toString()
file.writeText(text)
logger.d("Persisted: $text")
} catch (exc: Throwable) {
logger.w("Unexpectedly failed to persist PID.", exc)
}
Expand Down

0 comments on commit 1df7ea3

Please sign in to comment.