Skip to content

Instantly share code, notes, and snippets.

@jarrodnorwell
Created December 3, 2024 18:23
Show Gist options
  • Save jarrodnorwell/31262f112c6cc16d6ad4dc86fbb3f1bd to your computer and use it in GitHub Desktop.
Save jarrodnorwell/31262f112c6cc16d6ad4dc86fbb3f1bd to your computer and use it in GitHub Desktop.
Android Device Stats
package com.antique.temp
import android.net.Uri
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.LargeTopAppBar
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.core.net.toUri
import com.antique.temp.ui.theme.TempTheme
import java.io.File
import androidx.documentfile.provider.DocumentFile
import androidx.lifecycle.lifecycleScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
fun getTemperature(path: String): Float {
return try {
val tempFile = File(path)
val temp = tempFile.readText().trim().toFloat()
// Convert from millidegrees Celsius to Celsius
temp / 1000
} catch (e: Exception) {
e.printStackTrace()
-1f
}
}
fun listFoldersInSysThermal(): List<Uri> {
var _folders = listOf<Uri>()
// Path to the thermal directory
val thermalDir = File("/sys/class/thermal")
// Check if the directory exists and is indeed a directory
if (thermalDir.exists() && thermalDir.isDirectory) {
// List all the subdirectories (thermal zones)
val folders = thermalDir.listFiles { file -> file.isDirectory }
if (folders != null) {
for (folder in folders) {
println("Folder: ${folder.name}")
if (folder.name.startsWith("thermal_zone")) {
_folders += folder.toUri()
}
}
} else {
println("No folders found.")
}
} else {
println("The path does not exist or is not a directory.")
}
return _folders
}
@Composable
fun Card(name: String, temp: Float) {
Row(
modifier = Modifier
.padding(bottom = 20.dp)
) {
Text(name,
style = MaterialTheme.typography.bodyLarge)
Spacer(
modifier = Modifier
.weight(1.0f)
)
Text("${temp}°C",
fontWeight = FontWeight.Bold,
color = when {
temp > 50.0f -> Color.Red
temp > 30.0f && temp < 50.0f -> Color(255, 155, 0)
else -> Color.Green
},
style = MaterialTheme.typography.bodyLarge)
}
}
class MainActivity : ComponentActivity() {
@OptIn(ExperimentalMaterial3Api::class)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val temps = listFoldersInSysThermal()
var _temps = mutableListOf<Pair<String, Float>>()
temps.let {
it.forEach { file ->
file.path?.let {
val temp = File("$it/temp")
val type = File("$it/type")
if (temp.exists() and type.exists()) {
val temp = getTemperature(temp.path)
if (temp != -1f && temp > 0) {
println("${type.readText().trim()}: $temp°C")
_temps += Pair(type.readText().trim(), temp)
}
}
}
}
}
_temps.sortByDescending { it.first }
_temps.reverse()
enableEdgeToEdge()
setContent {
TempTheme {
Scaffold(
modifier = Modifier.fillMaxSize(),
topBar = {
LargeTopAppBar(
title = {
Text("Temperatures",
style = MaterialTheme.typography.displaySmall)
}
)
}
) {
Column(
modifier = Modifier
.padding(it)
.padding(horizontal = 20.dp)
.verticalScroll(rememberScrollState())
) {
_temps.forEach {
Card(it.first, it.second)
}
}
}
}
}
}
}
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
modifier = modifier
)
}
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
TempTheme {
Greeting("Android")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment