Skip to content

Commit

Permalink
Enable auto DAkC
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rustammendel committed Sep 29, 2024
1 parent 90f95bf commit 695e10c
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 64 deletions.
11 changes: 11 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">

<uses-feature android:name="android.hardware.usb.accessory" />
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
Expand All @@ -19,6 +20,9 @@
android:icon="@drawable/audiodack"
android:label="DAkC it"
android:permission="android.permission.BIND_QUICK_SETTINGS_TILE">
<meta-data
android:name="android.service.quicksettings.ACTIVE_TILE"
android:value="true" />
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE" />
</intent-filter>
Expand All @@ -35,6 +39,13 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".UsbHeadsetIntentReceiver"
android:exported="true">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
</receiver>
</application>

</manifest>
76 changes: 76 additions & 0 deletions app/src/main/java/com/rlabs/dakc/Commands.kt
Original file line number Diff line number Diff line change
@@ -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<String>): Pair<StringBuilder, StringBuilder> {
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)
}
60 changes: 16 additions & 44 deletions app/src/main/java/com/rlabs/dakc/DakcTileService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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<String>): Pair<StringBuilder, StringBuilder> {
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)
}
27 changes: 7 additions & 20 deletions app/src/main/java/com/rlabs/dakc/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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"

}
43 changes: 43 additions & 0 deletions app/src/main/java/com/rlabs/dakc/UsbHeadsetIntentReceiver.kt
Original file line number Diff line number Diff line change
@@ -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()
}
}
}
}
}
12 changes: 12 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,16 @@
app:layout_constraintHorizontal_bias="0.526"
app:layout_constraintBottom_toBottomOf="parent" />

<Button
android:text="DAkC it"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/dakcIt"
android:layout_marginTop="24dp"
app:layout_constraintTop_toBottomOf="@+id/button1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.526"/>


</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 695e10c

Please sign in to comment.