mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-01-10 09:01:55 +01:00
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:
parent
9edbf160c7
commit
fbd4cb810a
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user