From 695e10c6f6ca7f1a97dcd368dbc09b0b36ae2838 Mon Sep 17 00:00:00 2001 From: Rustam Ismayilov Date: Mon, 30 Sep 2024 00:13:08 +0200 Subject: [PATCH] Enable auto DAkC Add broadcast receiver - automatically run tinymix if usb-headphone is attached Add back tile service overrides - fixes tile being un-functional after restart Some refactors --- app/src/main/AndroidManifest.xml | 11 +++ app/src/main/java/com/rlabs/dakc/Commands.kt | 76 +++++++++++++++++++ .../java/com/rlabs/dakc/DakcTileService.kt | 60 ++++----------- .../main/java/com/rlabs/dakc/MainActivity.kt | 27 ++----- .../rlabs/dakc/UsbHeadsetIntentReceiver.kt | 43 +++++++++++ app/src/main/res/layout/activity_main.xml | 12 +++ 6 files changed, 165 insertions(+), 64 deletions(-) create mode 100644 app/src/main/java/com/rlabs/dakc/Commands.kt create mode 100644 app/src/main/java/com/rlabs/dakc/UsbHeadsetIntentReceiver.kt diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index be2117e..5b9e828 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -2,6 +2,7 @@ + + @@ -35,6 +39,13 @@ + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/rlabs/dakc/Commands.kt b/app/src/main/java/com/rlabs/dakc/Commands.kt new file mode 100644 index 0000000..130f0f4 --- /dev/null +++ b/app/src/main/java/com/rlabs/dakc/Commands.kt @@ -0,0 +1,76 @@ +package com.rlabs.dakc + +import android.util.Log +import java.io.BufferedReader +import java.io.InputStreamReader + + +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 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 \n$volInfo" + +} + +fun setVolume(volume: Int = 120) { + 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 $volume")) +} + +fun runAsRoot(commands: Array): Pair { + val output = StringBuilder() + val errorOutput = StringBuilder() + + try { + for (command in commands) { + val processBuilder = ProcessBuilder("su", "-c", command) + val process = processBuilder.start() + + // Read the output + val reader = BufferedReader(InputStreamReader(process.inputStream)) + val errorReader = BufferedReader(InputStreamReader(process.errorStream)) + + var line: String? + while (reader.readLine().also { line = it } != null) { + output.append(line).append("\n") + } + while (errorReader.readLine().also { line = it } != null) { + errorOutput.append(line).append("\n") + } + + process.waitFor() + + // Log the output + if (output.isNotEmpty()) { + Log.d("Command Output", output.toString()) + } + if (errorOutput.isNotEmpty()) { + Log.e("Command Error", errorOutput.toString()) + } + + + } + } catch (e: Exception) { + e.printStackTrace() + } + + return Pair(output, errorOutput) +} \ No newline at end of file diff --git a/app/src/main/java/com/rlabs/dakc/DakcTileService.kt b/app/src/main/java/com/rlabs/dakc/DakcTileService.kt index 7a2a24d..9f95dc6 100644 --- a/app/src/main/java/com/rlabs/dakc/DakcTileService.kt +++ b/app/src/main/java/com/rlabs/dakc/DakcTileService.kt @@ -2,8 +2,6 @@ package com.rlabs.dakc import android.service.quicksettings.TileService import android.util.Log -import java.io.BufferedReader -import java.io.InputStreamReader class DakcTileService : TileService() { @@ -13,53 +11,27 @@ class DakcTileService : TileService() { override fun onClick() { super.onClick() 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")) + setVolume() } -} - -fun runAsRoot(commands: Array): Pair { - val output = StringBuilder() - val errorOutput = StringBuilder() - - try { - for (command in commands) { - val processBuilder = ProcessBuilder("su", "-c", command) - val process = processBuilder.start() - - // Read the output - val reader = BufferedReader(InputStreamReader(process.inputStream)) - val errorReader = BufferedReader(InputStreamReader(process.errorStream)) - - var line: String? - while (reader.readLine().also { line = it } != null) { - output.append(line).append("\n") - } - while (errorReader.readLine().also { line = it } != null) { - errorOutput.append(line).append("\n") - } - - process.waitFor() + // Called when the user adds your tile. + override fun onTileAdded() { + super.onTileAdded() + } - // Log the output - if (output.isNotEmpty()) { - Log.d("Command Output", output.toString()) - } - if (errorOutput.isNotEmpty()) { - Log.e("Command Error", errorOutput.toString()) - } + // Called when your app can update your tile. + override fun onStartListening() { + super.onStartListening() + } + // Called when your app can no longer update your tile. + override fun onStopListening() { + super.onStopListening() + } - } - } catch (e: Exception) { - e.printStackTrace() + // Called when the user removes your tile. + override fun onTileRemoved() { + super.onTileRemoved() } - return Pair(output, errorOutput) } \ No newline at end of file diff --git a/app/src/main/java/com/rlabs/dakc/MainActivity.kt b/app/src/main/java/com/rlabs/dakc/MainActivity.kt index 9055937..eba955e 100644 --- a/app/src/main/java/com/rlabs/dakc/MainActivity.kt +++ b/app/src/main/java/com/rlabs/dakc/MainActivity.kt @@ -12,6 +12,8 @@ class MainActivity : ComponentActivity() { lateinit var dev2: TextView lateinit var dev3: TextView lateinit var refreshButton: Button + lateinit var dakcButton: Button + override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() @@ -31,25 +33,10 @@ class MainActivity : ComponentActivity() { refreshButton = findViewById(R.id.button1) refreshButton.setOnClickListener { readFields() } + dakcButton = findViewById(R.id.dakcIt) + dakcButton.setOnClickListener { + Log.d("Dakc", "DAKC button in MainActivity tapped") + setVolume() + } } - -} - -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 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 \n$volInfo" - } \ No newline at end of file diff --git a/app/src/main/java/com/rlabs/dakc/UsbHeadsetIntentReceiver.kt b/app/src/main/java/com/rlabs/dakc/UsbHeadsetIntentReceiver.kt new file mode 100644 index 0000000..54608fa --- /dev/null +++ b/app/src/main/java/com/rlabs/dakc/UsbHeadsetIntentReceiver.kt @@ -0,0 +1,43 @@ +package com.rlabs.dakc + +import android.content.BroadcastReceiver +import android.content.Context +import android.content.Intent +import android.hardware.usb.UsbDevice +import android.hardware.usb.UsbManager +import android.util.Log + + +private const val TAG = "UsbHeadsetIntentReceiver" + +class UsbHeadsetIntentReceiver : BroadcastReceiver() { + + override fun onReceive(context: Context?, intent: Intent?) { + when (intent?.action) { + UsbManager.ACTION_USB_DEVICE_ATTACHED -> { + val usbDevice: UsbDevice? = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE) + var productName = "" + + // Check if the device is non-null + usbDevice?.let { + Log.d(TAG, "USB Device Detected") + Log.d(TAG, "Vendor ID: ${it.vendorId}") + Log.d(TAG, "Product ID: ${it.productId}") + Log.d(TAG, "Device Class: ${it.deviceClass}") + Log.d(TAG, "Device Subclass: ${it.deviceSubclass}") + Log.d(TAG, "Device Name: ${it.deviceName}") + Log.d(TAG, "Device Protocol: ${it.deviceProtocol}") + Log.d(TAG, "Interface Count: ${it.interfaceCount}") + Log.d(TAG, "Product name: ${it.productName}") + Log.d(TAG, "Manufacturer name: ${it.manufacturerName}") + Log.d(TAG, "Config Count: ${it.configurationCount}") + productName = it.productName.toString() + } + if (productName.contains("Headphone")) { + Log.i(TAG, "Usb accessory $productName detected") + setVolume() + } + } + } + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 9dd91c7..d834edb 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -50,4 +50,16 @@ app:layout_constraintHorizontal_bias="0.526" app:layout_constraintBottom_toBottomOf="parent" /> +