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
This commit is contained in:
~noodlez1232 2025-01-03 12:19:17 -08:00
parent 9edbf160c7
commit fbd4cb810a
2 changed files with 27 additions and 4 deletions

View File

@ -16,6 +16,9 @@
along with this program. If not, see <https://www.gnu.org/licenses/>. */ along with this program. If not, see <https://www.gnu.org/licenses/>. */
package nodomain.freeyourgadget.gadgetbridge.devices.asteroidos; package nodomain.freeyourgadget.gadgetbridge.devices.asteroidos;
import android.content.Context;
import android.media.AudioManager;
import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventMusicControl;
/** /**
@ -29,12 +32,18 @@ public class AsteroidOSMediaCommand {
public static final byte COMMAND_VOLUME = 0x4; public static final byte COMMAND_VOLUME = 0x4;
public byte command; public byte command;
public AsteroidOSMediaCommand(byte value) { public byte[] raw_values;
command = value; 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 * Convert the MediaCommand to a music control event
*
* @return the matching music control event * @return the matching music control event
*/ */
public GBDeviceEventMusicControl toMusicControlEvent() { public GBDeviceEventMusicControl toMusicControlEvent() {
@ -53,9 +62,21 @@ public class AsteroidOSMediaCommand {
event.event = GBDeviceEventMusicControl.Event.PAUSE; event.event = GBDeviceEventMusicControl.Event.PAUSE;
break; break;
case COMMAND_VOLUME: case COMMAND_VOLUME:
setVolume(raw_values[1]);
event = null;
break;
default: default:
event.event = GBDeviceEventMusicControl.Event.UNKNOWN; event.event = GBDeviceEventMusicControl.Event.UNKNOWN;
} }
return event; 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);
}
} }

View File

@ -18,6 +18,7 @@ package nodomain.freeyourgadget.gadgetbridge.service.devices.asteroidos;
import android.bluetooth.BluetoothGatt; import android.bluetooth.BluetoothGatt;
import android.bluetooth.BluetoothGattCharacteristic; import android.bluetooth.BluetoothGattCharacteristic;
import android.content.Context;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -274,9 +275,10 @@ public class AsteroidOSDeviceSupport extends AbstractBTLEDeviceSupport {
*/ */
public void handleMediaCommand (BluetoothGattCharacteristic characteristic) { public void handleMediaCommand (BluetoothGattCharacteristic characteristic) {
LOG.info("handle media command"); LOG.info("handle media command");
AsteroidOSMediaCommand command = new AsteroidOSMediaCommand(characteristic.getValue()[0]); AsteroidOSMediaCommand command = new AsteroidOSMediaCommand(characteristic.getValue(), getContext());
GBDeviceEventMusicControl event = command.toMusicControlEvent(); GBDeviceEventMusicControl event = command.toMusicControlEvent();
evaluateGBDeviceEvent(event); if (event != null)
evaluateGBDeviceEvent(event);
} }
@Override @Override