-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
1 parent
90f95bf
commit 695e10c
Showing
6 changed files
with
165 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
app/src/main/java/com/rlabs/dakc/UsbHeadsetIntentReceiver.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters