From fbd4cb810ab7a346bcb0c80a565d581d8256d954 Mon Sep 17 00:00:00 2001 From: ~noodlez1232 Date: Fri, 3 Jan 2025 12:19:17 -0800 Subject: [PATCH] AsteroidOS: Add volume control This pull request adds volume control to the AsteroidOS implementation. It's not quite what I wanted from it, but it's probably good enough. Hopefully it's good enough for other people too --- .../asteroidos/AsteroidOSMediaCommand.java | 25 +++++++++++++++++-- .../asteroidos/AsteroidOSDeviceSupport.java | 6 +++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/asteroidos/AsteroidOSMediaCommand.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/asteroidos/AsteroidOSMediaCommand.java index 316efd2de..a3cbacb41 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/asteroidos/AsteroidOSMediaCommand.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/devices/asteroidos/AsteroidOSMediaCommand.java @@ -16,6 +16,9 @@ along with this program. If not, see . */ package nodomain.freeyourgadget.gadgetbridge.devices.asteroidos; +import android.content.Context; +import android.media.AudioManager; + import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl; /** @@ -29,12 +32,18 @@ public class AsteroidOSMediaCommand { public static final byte COMMAND_VOLUME = 0x4; public byte command; - public AsteroidOSMediaCommand(byte value) { - command = value; + public byte[] raw_values; + public Context context; + + public AsteroidOSMediaCommand(byte[] values, Context device_context) { + command = values[0]; + raw_values = values; + context = device_context; } /** * Convert the MediaCommand to a music control event + * * @return the matching music control event */ public GBDeviceEventMusicControl toMusicControlEvent() { @@ -53,9 +62,21 @@ public class AsteroidOSMediaCommand { event.event = GBDeviceEventMusicControl.Event.PAUSE; break; case COMMAND_VOLUME: + setVolume(raw_values[1]); + event = null; + break; default: event.event = GBDeviceEventMusicControl.Event.UNKNOWN; } return event; } + + private void setVolume(byte volume) { + final AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + + final int volumeMax = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); + final int finalVol = (int) Math.round((volume * volumeMax) / 100f); + if (audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) != finalVol) + audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, (int) Math.round((volume * volumeMax) / 100f), AudioManager.FLAG_SHOW_UI); + } } diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/asteroidos/AsteroidOSDeviceSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/asteroidos/AsteroidOSDeviceSupport.java index bae9b9eb8..65b41c8b1 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/asteroidos/AsteroidOSDeviceSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/asteroidos/AsteroidOSDeviceSupport.java @@ -18,6 +18,7 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.asteroidos; import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGattCharacteristic; +import android.content.Context; import androidx.annotation.NonNull; @@ -274,9 +275,10 @@ public class AsteroidOSDeviceSupport extends AbstractBTLEDeviceSupport { */ public void handleMediaCommand (BluetoothGattCharacteristic characteristic) { LOG.info("handle media command"); - AsteroidOSMediaCommand command = new AsteroidOSMediaCommand(characteristic.getValue()[0]); + AsteroidOSMediaCommand command = new AsteroidOSMediaCommand(characteristic.getValue(), getContext()); GBDeviceEventMusicControl event = command.toMusicControlEvent(); - evaluateGBDeviceEvent(event); + if (event != null) + evaluateGBDeviceEvent(event); } @Override