Skip to content

Commit

Permalink
refactor: perform root detection on a background thread
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalwrench committed Mar 18, 2021
1 parent ecf986e commit a4d23fb
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
[#1194](https://github.com/bugsnag/bugsnag-android/pull/1194)
[#1195](https://github.com/bugsnag/bugsnag-android/pull/1195)
[#1198](https://github.com/bugsnag/bugsnag-android/pull/1198)
[#1200](https://github.com/bugsnag/bugsnag-android/pull/1200)

* Add public API for crash-on-launch detection
[#1157](https://github.com/bugsnag/bugsnag-android/pull/1157)
Expand Down
2 changes: 1 addition & 1 deletion bugsnag-android-core/detekt-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<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>
<ID>LongParameterList:Device.kt$Device$( buildInfo: DeviceBuildInfo, /** * The Application Binary Interface used */ var cpuAbi: Array&lt;String&gt;?, /** * Whether the device has been jailbroken */ var jailbroken: Boolean?, /** * A UUID generated by Bugsnag and used for the individual application on a device */ var id: String?, /** * The IETF language tag of the locale used */ var locale: String?, /** * The total number of bytes of memory on the device */ var totalMemory: Long?, /** * A collection of names and their versions of the primary languages, frameworks or * runtimes that the application is running on */ var runtimeVersions: MutableMap&lt;String, Any&gt;? )</ID>
<ID>LongParameterList:DeviceBuildInfo.kt$DeviceBuildInfo$( val manufacturer: String?, val model: String?, val osVersion: String?, val apiLevel: Int?, val osBuild: String?, val fingerprint: String?, val tags: String?, val brand: String?, val cpuAbis: Array&lt;String&gt;? )</ID>
<ID>LongParameterList:DeviceDataCollector.kt$DeviceDataCollector$( private val connectivity: Connectivity, private val appContext: Context, private val resources: Resources?, private val deviceId: String?, private val buildInfo: DeviceBuildInfo, private val dataDirectory: File, rootDetector: RootDetector, private val logger: Logger )</ID>
<ID>LongParameterList:DeviceDataCollector.kt$DeviceDataCollector$( private val connectivity: Connectivity, private val appContext: Context, private val resources: Resources?, private val deviceId: String?, private val buildInfo: DeviceBuildInfo, private val dataDirectory: File, rootDetector: RootDetector, bgTaskService: BackgroundTaskService, private val logger: Logger )</ID>
<ID>LongParameterList:DeviceWithState.kt$DeviceWithState$( buildInfo: DeviceBuildInfo, jailbroken: Boolean?, id: String?, locale: String?, totalMemory: Long?, runtimeVersions: MutableMap&lt;String, Any&gt;, /** * The number of free bytes of storage available on the device */ var freeDisk: Long?, /** * The number of free bytes of memory available on the device */ var freeMemory: Long?, /** * The orientation of the device when the event occurred: either portrait or landscape */ var orientation: String?, /** * The timestamp on the device when the event occurred */ var time: Date? )</ID>
<ID>LongParameterList:EventFilenameInfo.kt$EventFilenameInfo.Companion$( obj: Any, uuid: String = UUID.randomUUID().toString(), apiKey: String?, timestamp: Long = System.currentTimeMillis(), config: ImmutableConfig, isLaunching: Boolean? = null )</ID>
<ID>LongParameterList:StateEvent.kt$StateEvent.Install$( val apiKey: String, val autoDetectNdkCrashes: Boolean, val appVersion: String?, val buildUuid: String?, val releaseStage: String?, val lastRunInfoPath: String, val consecutiveLaunchCrashes: Int )</ID>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public Unit invoke(Boolean hasConnection, String networkState) {
Resources resources = appContext.getResources();
deviceDataCollector = new DeviceDataCollector(connectivity, appContext,
resources, deviceId, info, Environment.getDataDirectory(),
new RootDetector(), logger);
new RootDetector(), bgTaskService, logger);

if (appContext instanceof Application) {
Application application = (Application) appContext;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import java.io.File
import java.util.Date
import java.util.HashMap
import java.util.Locale
import java.util.concurrent.Callable
import java.util.concurrent.Future
import java.util.concurrent.RejectedExecutionException
import kotlin.math.max
import kotlin.math.min

Expand All @@ -24,30 +27,43 @@ internal class DeviceDataCollector(
private val buildInfo: DeviceBuildInfo,
private val dataDirectory: File,
rootDetector: RootDetector,
bgTaskService: BackgroundTaskService,
private val logger: Logger
) {

private val displayMetrics = resources?.displayMetrics
private val emulator = isEmulator()
private val rooted = rootDetector.isRooted()
private val screenDensity = getScreenDensity()
private val dpi = getScreenDensityDpi()
private val screenResolution = getScreenResolution()
private val locale = Locale.getDefault().toString()
private val cpuAbi = getCpuAbi()
private val runtimeVersions: MutableMap<String, Any>
private val rootedFuture: Future<Boolean>?

init {
val map = mutableMapOf<String, Any>()
buildInfo.apiLevel?.let { map["androidApiLevel"] = it }
buildInfo.osBuild?.let { map["osBuild"] = it }
runtimeVersions = map

rootedFuture = try {
bgTaskService.submitTask(
TaskType.IO,
Callable {
rootDetector.isRooted()
}
)
} catch (exc: RejectedExecutionException) {
logger.w("Failed to perform root detection checks", exc)
null
}
}

fun generateDevice() = Device(
buildInfo,
cpuAbi,
rooted,
checkIsRooted(),
deviceId,
locale,
calculateTotalMemory(),
Expand All @@ -56,7 +72,7 @@ internal class DeviceDataCollector(

fun generateDeviceWithState(now: Long) = DeviceWithState(
buildInfo,
rooted,
checkIsRooted(),
deviceId,
locale,
calculateTotalMemory(),
Expand All @@ -81,6 +97,17 @@ internal class DeviceDataCollector(
return map
}

private fun checkIsRooted(): Boolean {
if (rootedFuture != null) {
return try {
rootedFuture.get()
} catch (exc: Exception) {
false
}
}
return false
}

/**
* Guesses whether the current device is an emulator or not, erring on the side of caution
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ internal class DeviceDataCollectorSerializationTest {
buildInfo,
File(""),
rootDetector,
BackgroundTaskService(),
NoopLogger
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ internal class DeviceMetadataSerializationTest {
buildInfo,
File(""),
rootDetector,
BackgroundTaskService(),
NoopLogger
)

Expand Down

0 comments on commit a4d23fb

Please sign in to comment.