I want to get information about a device such as RAM (mem size), GPU (model, mem size), CPU (model, count of cores) programmatically in android app(kotlin). I found some similar posts (for example that), but there is no solution there. Help me please
**FOR RAM**
private val activityManager: ActivityManager
fun getTotalBytes(): Long {
val memoryInfo = ActivityManager.MemoryInfo()
activityManager.getMemoryInfo(memoryInfo)
return memoryInfo.totalMem
}
fun getAvailableBytes(): Long {
val memoryInfo = ActivityManager.MemoryInfo()
activityManager.getMemoryInfo(memoryInfo)
return memoryInfo.availMem
}
fun getAvailablePercentage(): Int {
val total = getTotalBytes().toDouble()
val available = getAvailableBytes().toDouble()
return (available / total * 100).toInt()
}
fun getThreshold(): Long {
val memoryInfo = ActivityManager.MemoryInfo()
activityManager.getMemoryInfo(memoryInfo)
return memoryInfo.threshold
**FOR CPU**
private const val CPU_INFO_DIR = "/sys/devices/system/cpu/"
fun getNumberOfCores(): Int {
return if (Build.VERSION.SDK_INT >= 17) {
Runtime.getRuntime().availableProcessors()
} else {
getNumCoresLegacy()
}
}
/**
* Checking frequencies directories and return current value if exists (otherwise we can
* assume that core is stopped - value -1)
*/
fun getCurrentFreq(coreNumber: Int): Long {
val currentFreqPath = "${CPU_INFO_DIR}cpu$coreNumber/cpufreq/scaling_cur_freq"
return try {
RandomAccessFile(currentFreqPath, "r").use { it.readLine().toLong() / 1000 }
} catch (e: Exception) {
Timber.e("getCurrentFreq() - cannot read file")
-1
}
}
/**
* Read max/min frequencies for specific [coreNumber]. Return [Pair] with min and max frequency
* or [Pair] with -1.
*/
fun getMinMaxFreq(coreNumber: Int): Pair<Long, Long> {
val minPath = "${CPU_INFO_DIR}cpu$coreNumber/cpufreq/cpuinfo_min_freq"
val maxPath = "${CPU_INFO_DIR}cpu$coreNumber/cpufreq/cpuinfo_max_freq"
return try {
val minMhz = RandomAccessFile(minPath, "r").use { it.readLine().toLong() / 1000 }
val maxMhz = RandomAccessFile(maxPath, "r").use { it.readLine().toLong() / 1000 }
Pair(minMhz, maxMhz)
} catch (e: Exception) {
Timber.e("getMinMaxFreq() - cannot read file")
Pair(-1, -1)
}
}
private fun getNumCoresLegacy(): Int {
class CpuFilter : FileFilter {
override fun accept(pathname: File): Boolean {
// Check if filename is "cpu", followed by a single digit number
if (Pattern.matches("cpu[0-9]+", pathname.name)) {
return true
}
return false
}
}
return try {
File(CPU_INFO_DIR).listFiles(CpuFilter())!!.size
} catch (e: Exception) {
1
}
}
**FOR GPU**
fun getGlEsVersion(): String {
return activityManager.deviceConfigurationInfo.glEsVersion
}