From e4a60acd92e64ad662687c1f54f0aed631991c62 Mon Sep 17 00:00:00 2001 From: Rustam Ismayilov Date: Tue, 20 Aug 2024 12:21:22 +0200 Subject: [PATCH] Find headset automatically Find headset algorithmically instead of using hardcoded value Print more details in main activity --- .../java/com/rlabs/dakc/DakcTileService.kt | 10 ++++-- .../main/java/com/rlabs/dakc/MainActivity.kt | 36 ++++++++++--------- 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/rlabs/dakc/DakcTileService.kt b/app/src/main/java/com/rlabs/dakc/DakcTileService.kt index bbb3746..7a2a24d 100644 --- a/app/src/main/java/com/rlabs/dakc/DakcTileService.kt +++ b/app/src/main/java/com/rlabs/dakc/DakcTileService.kt @@ -12,8 +12,14 @@ class DakcTileService : TileService() { // Called when the user taps on your tile in an active or inactive state. override fun onClick() { super.onClick() - Log.d("QS", "Tile tapped") - runAsRoot(arrayOf("tinymix -D 2 3 120")) + Log.d("Dakc", "Tile tapped") + var n = 0 + var mixerName = "" + while (!mixerName.contains("USB-C to 3.5mm") && n < 10) { + mixerName = runAsRoot(arrayOf("tinymix -D $n | head")).first.toString().split("\n")[0] + n++ + } + runAsRoot(arrayOf("tinymix -D ${n - 1} 3 120")) } } diff --git a/app/src/main/java/com/rlabs/dakc/MainActivity.kt b/app/src/main/java/com/rlabs/dakc/MainActivity.kt index 556989b..9055937 100644 --- a/app/src/main/java/com/rlabs/dakc/MainActivity.kt +++ b/app/src/main/java/com/rlabs/dakc/MainActivity.kt @@ -11,43 +11,45 @@ class MainActivity : ComponentActivity() { lateinit var dev1: TextView lateinit var dev2: TextView lateinit var dev3: TextView - lateinit var button1: Button + lateinit var refreshButton: Button override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContentView(R.layout.activity_main) - dev1 = findViewById(R.id.dev1) - dev1.text = TinymixOut(device = "1") + dev1 = findViewById(R.id.dev1) dev2 = findViewById(R.id.dev2) - dev2.text = TinymixOut(device = "2") - dev3 = findViewById(R.id.dev3) - dev3.text = TinymixOut(device = "2", 7) - - button1 = findViewById(R.id.button1) - button1.setOnClickListener { - dev1.text = TinymixOut(device = "1") - dev2.text = TinymixOut(device = "2") - dev3.text = TinymixOut(device = "2", 7) + fun readFields() { + dev1.text = tinymixOut(device = "0") + dev2.text = tinymixOut(device = "1") + dev3.text = tinymixOut(device = "2") } + readFields() - } + refreshButton = findViewById(R.id.button1) + refreshButton.setOnClickListener { readFields() } + } } -fun TinymixOut(device: String, lineNum: Int = 0): String { - val outPair = runAsRoot(arrayOf("tinymix -D $device")) +fun tinymixOut(device: String, lineNum: Int = 0): String { + val outPair = runAsRoot(arrayOf("tinymix -D $device | head -20")) val out = outPair.first.toString() val errout = outPair.second.toString() - val firstLine = if (errout.isNotEmpty()) errout.split("\n")[0] else out.split("\n")[lineNum] + val outArr = out.split("\n") + + val firstLine = if (errout.isNotEmpty()) errout.split("\n")[0] else outArr[lineNum] + val iVolLine = outArr.indexOfFirst { s -> s.contains("Headset Playback Volume") } + + val volInfo = if (errout.isEmpty() && iVolLine != -1) outArr[iVolLine] else "" Log.d("tinymix", out) - return "tinymix -D $device out: \n$firstLine" + return "tinymix -D $device out: \n$firstLine \n$volInfo" } \ No newline at end of file