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/>. */
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);
}
}

View File

@ -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,8 +275,9 @@ 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();
if (event != null)
evaluateGBDeviceEvent(event);
}