From c27f836495de93c80feec502a917e824471ffd95 Mon Sep 17 00:00:00 2001 From: SmartbearYing Date: Tue, 6 Feb 2024 01:40:15 +0000 Subject: [PATCH] feat(metadata): process importance --- bugsnag-android-core/detekt-baseline.xml | 1 + .../bugsnag/android/AppDataCollectorTest.kt | 31 ++++++++++ .../com/bugsnag/android/AppDataCollector.kt | 62 +++++++++++++++++++ 3 files changed, 94 insertions(+) diff --git a/bugsnag-android-core/detekt-baseline.xml b/bugsnag-android-core/detekt-baseline.xml index bccf015f28..1435111433 100644 --- a/bugsnag-android-core/detekt-baseline.xml +++ b/bugsnag-android-core/detekt-baseline.xml @@ -2,6 +2,7 @@ + CyclomaticComplexMethod:AppDataCollector.kt$AppDataCollector$@SuppressLint("SwitchIntDef") @Suppress("DEPRECATION") private fun getProcessImportance(): String? CyclomaticComplexMethod:ConfigInternal.kt$ConfigInternal$fun getConfigDifferences(): Map<String, Any> ImplicitDefaultLocale:DeliveryHeaders.kt$String.format("%02x", byte) 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] */ var buildUuid: String?, /** * The application type set in [Configuration#version] */ var type: String?, /** * The version code of the application set in [Configuration.versionCode] */ var versionCode: Number? ) diff --git a/bugsnag-android-core/src/androidTest/java/com/bugsnag/android/AppDataCollectorTest.kt b/bugsnag-android-core/src/androidTest/java/com/bugsnag/android/AppDataCollectorTest.kt index 5847d1fe4e..0d1870a378 100644 --- a/bugsnag-android-core/src/androidTest/java/com/bugsnag/android/AppDataCollectorTest.kt +++ b/bugsnag-android-core/src/androidTest/java/com/bugsnag/android/AppDataCollectorTest.kt @@ -126,4 +126,35 @@ class AppDataCollectorTest { val result = collector.getInstallerPackageName() assertEquals("Test Installer name", result) } + + @Test + fun testGetProcessImportance() = withBuildSdkInt(Build.VERSION_CODES.Q) { + val packageManager = mock(PackageManager::class.java) + `when`(packageManager.getApplicationLabel(any())).thenReturn("Test App name") + `when`(am.runningAppProcesses).thenReturn( + listOf( + ActivityManager.RunningAppProcessInfo().apply { + importance = ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND + } + ) + ) + + val collector = AppDataCollector( + context, + packageManager, + client.immutableConfig, + client.sessionTracker, + am, + client.launchCrashTracker, + client.memoryTrimState + ) + + val result = collector.getAppDataMetadata()["processImportance"] + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { + assertEquals(null, result) + } else { + assertEquals("foreground", result) + } + } } diff --git a/bugsnag-android-core/src/main/java/com/bugsnag/android/AppDataCollector.kt b/bugsnag-android-core/src/main/java/com/bugsnag/android/AppDataCollector.kt index ee23a07a72..9c48825fcd 100644 --- a/bugsnag-android-core/src/main/java/com/bugsnag/android/AppDataCollector.kt +++ b/bugsnag-android-core/src/main/java/com/bugsnag/android/AppDataCollector.kt @@ -2,14 +2,31 @@ package com.bugsnag.android import android.annotation.SuppressLint import android.app.ActivityManager +import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_CACHED +import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE +import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_EMPTY +import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND +import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE +import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_GONE +import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE +import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE_PRE_26 +import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE +import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_TOP_SLEEPING +import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_TOP_SLEEPING_PRE_28 +import android.app.ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE +import android.app.ActivityManager.RunningAppProcessInfo.REASON_PROVIDER_IN_USE +import android.app.ActivityManager.RunningAppProcessInfo.REASON_SERVICE_IN_USE import android.app.Application import android.content.Context import android.content.pm.PackageManager import android.os.Build.VERSION import android.os.Build.VERSION_CODES +import android.os.Process import android.os.SystemClock import com.bugsnag.android.internal.ImmutableConfig +const val IMPORTANCE_CANT_SAVE_STATE_PRE_26 = 170 + /** * Collects various data on the application state */ @@ -49,12 +66,55 @@ internal class AppDataCollector( ) } + @SuppressLint("SwitchIntDef") + @Suppress("DEPRECATION") + private fun getProcessImportance(): String? { + try { + val appInfo = ActivityManager.RunningAppProcessInfo() + if (appInfo.pid == 0) { + return null + } + + if (VERSION.SDK_INT >= VERSION_CODES.JELLY_BEAN) { + ActivityManager.getMyMemoryState(appInfo) + } else { + activityManager?.runningAppProcesses?.find { + it.pid == Process.myPid() + }?.let { + appInfo.importance = it.importance + } + } + + return when (appInfo.importance) { + IMPORTANCE_FOREGROUND -> "foreground" + IMPORTANCE_FOREGROUND_SERVICE -> "foreground service" + IMPORTANCE_TOP_SLEEPING -> "top sleeping" + IMPORTANCE_TOP_SLEEPING_PRE_28 -> "top sleeping" + IMPORTANCE_VISIBLE -> "visible" + IMPORTANCE_PERCEPTIBLE -> "perceptible" + IMPORTANCE_PERCEPTIBLE_PRE_26 -> "perceptible" + IMPORTANCE_CANT_SAVE_STATE -> "can't save state" + IMPORTANCE_CANT_SAVE_STATE_PRE_26 -> "can't save state" + IMPORTANCE_SERVICE -> "service" + IMPORTANCE_CACHED -> "cached/background" + IMPORTANCE_GONE -> "gone" + IMPORTANCE_EMPTY -> "empty" + REASON_PROVIDER_IN_USE -> "provider in use" + REASON_SERVICE_IN_USE -> "service in use" + else -> "unknown importance (${activityManager?.runningAppProcesses?.last()?.importance})" + } + } catch (e: Exception) { + return null + } + } + fun getAppDataMetadata(): MutableMap { val map = HashMap() map["name"] = appName map["activeScreen"] = sessionTracker.contextActivity map["lowMemory"] = memoryTrimState.isLowMemory map["memoryTrimLevel"] = memoryTrimState.trimLevelDescription + map["processImportance"] = getProcessImportance() populateRuntimeMemoryMetadata(map) @@ -128,6 +188,7 @@ internal class AppDataCollector( packageManager != null && copy != null -> { packageManager.getApplicationLabel(copy).toString() } + else -> null } } @@ -156,6 +217,7 @@ internal class AppDataCollector( VERSION.SDK_INT >= VERSION_CODES.P -> { Application.getProcessName() } + else -> { // see https://stackoverflow.com/questions/19631894 val clz = Class.forName("android.app.ActivityThread")