Reputation: 335
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
Upvotes: 1
Views: 2589
Reputation: 124
**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
}
Upvotes: 5
Reputation: 164
You can use this -
Log.i("TAG", "SERIAL: " + Build.SERIAL);
Log.i("TAG","MODEL: " + Build.MODEL);
Log.i("TAG","ID: " + Build.ID);
Log.i("TAG","Manufacture: " + Build.MANUFACTURER);
Log.i("TAG","brand: " + Build.BRAND);
Log.i("TAG","type: " + Build.TYPE);
Log.i("TAG","user: " + Build.USER);
Log.i("TAG","BASE: " + Build.VERSION_CODES.BASE);
Log.i("TAG","INCREMENTAL " + Build.VERSION.INCREMENTAL);
Log.i("TAG","SDK " + Build.VERSION.SDK);
Log.i("TAG","BOARD: " + Build.BOARD);
Log.i("TAG","BRAND " + Build.BRAND);
Log.i("TAG","HOST " + Build.HOST);
Log.i("TAG","FINGERPRINT: "+Build.FINGERPRINT);
Log.i("TAG","Version Code: " + Build.VERSION.RELEASE);
Upvotes: 1
Reputation: 3015
I've been looking around android files and what they contain and found a way to get cpu name and available ram memory size, but did not found any way to get to the gpu name
fun fetchCpuName(): String {
return File("/proc/cpuinfo").readLines().last() // Cpu name
}
fun fetchRamInfo(): String {
return File("/proc/meminfo").readLines().first() // MemTotal
}
This probably won't help much, but still might help someone in the future
Upvotes: 2
Reputation: 440
Some of this information is available via the android.os.Build
class.
Upvotes: 0