Skip to content

Commit

Permalink
feat(metadata): process importance
Browse files Browse the repository at this point in the history
  • Loading branch information
YYChen01988 committed Feb 6, 2024
1 parent c41050e commit 698d24d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,30 @@ 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().get("processImportance")
assertEquals("foreground", result)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,32 @@ internal class AppDataCollector(
)
}

private fun getProcessImportance(): String? {
try {
return when (activityManager?.runningAppProcesses?.last()?.importance) {
ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND -> "foreground"
ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND_SERVICE -> "foreground service"
ActivityManager.RunningAppProcessInfo.IMPORTANCE_TOP_SLEEPING -> "top sleeping"
ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE -> "visible"
ActivityManager.RunningAppProcessInfo.IMPORTANCE_PERCEPTIBLE -> "perceptible"
ActivityManager.RunningAppProcessInfo.IMPORTANCE_CANT_SAVE_STATE -> "can't save state"
ActivityManager.RunningAppProcessInfo.IMPORTANCE_SERVICE -> "service"
ActivityManager.RunningAppProcessInfo.IMPORTANCE_CACHED -> "cached"
ActivityManager.RunningAppProcessInfo.IMPORTANCE_GONE -> "gone"
else -> "unknown importance (${activityManager?.runningAppProcesses?.last()?.importance})"
}
} catch (e: Exception) {
return null
}
}

fun getAppDataMetadata(): MutableMap<String, Any?> {
val map = HashMap<String, Any?>()
map["name"] = appName
map["activeScreen"] = sessionTracker.contextActivity
map["lowMemory"] = memoryTrimState.isLowMemory
map["memoryTrimLevel"] = memoryTrimState.trimLevelDescription
map["processImportance"] = getProcessImportance()

populateRuntimeMemoryMetadata(map)

Expand Down

0 comments on commit 698d24d

Please sign in to comment.