1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-12-27 02:55:50 +01:00

Huami: Send media volume to device

This commit is contained in:
José Rebelo 2022-05-05 14:14:15 +01:00
parent 37ba8c291e
commit 682b215985
10 changed files with 83 additions and 1 deletions

View File

@ -60,6 +60,13 @@ public interface EventHandler {
void onSetMusicInfo(MusicSpec musicSpec); void onSetMusicInfo(MusicSpec musicSpec);
/**
* Sets the current phone media volume.
*
* @param volume the volume percentage (0 to 100).
*/
void onSetPhoneVolume(final float volume);
void onEnableRealtimeSteps(boolean enable); void onEnableRealtimeSteps(boolean enable);
void onInstallApp(Uri uri); void onInstallApp(Uri uri);

View File

@ -224,6 +224,13 @@ public class GBDeviceService implements DeviceService {
invokeService(intent); invokeService(intent);
} }
@Override
public void onSetPhoneVolume(final float volume) {
Intent intent = createIntent().setAction(ACTION_SET_PHONE_VOLUME)
.putExtra(EXTRA_PHONE_VOLUME, volume);
invokeService(intent);
}
@Override @Override
public void onSetReminders(ArrayList<? extends Reminder> reminders) { public void onSetReminders(ArrayList<? extends Reminder> reminders) {
Intent intent = createIntent().setAction(ACTION_SET_REMINDERS) Intent intent = createIntent().setAction(ACTION_SET_REMINDERS)

View File

@ -39,6 +39,7 @@ public interface DeviceService extends EventHandler {
String ACTION_SETTIME = PREFIX + ".action.settime"; String ACTION_SETTIME = PREFIX + ".action.settime";
String ACTION_SETMUSICINFO = PREFIX + ".action.setmusicinfo"; String ACTION_SETMUSICINFO = PREFIX + ".action.setmusicinfo";
String ACTION_SETMUSICSTATE = PREFIX + ".action.setmusicstate"; String ACTION_SETMUSICSTATE = PREFIX + ".action.setmusicstate";
String ACTION_SET_PHONE_VOLUME = PREFIX + ".action.set_phone_volume";
String ACTION_REQUEST_DEVICEINFO = PREFIX + ".action.request_deviceinfo"; String ACTION_REQUEST_DEVICEINFO = PREFIX + ".action.request_deviceinfo";
String ACTION_REQUEST_APPINFO = PREFIX + ".action.request_appinfo"; String ACTION_REQUEST_APPINFO = PREFIX + ".action.request_appinfo";
String ACTION_REQUEST_SCREENSHOT = PREFIX + ".action.request_screenshot"; String ACTION_REQUEST_SCREENSHOT = PREFIX + ".action.request_screenshot";
@ -103,6 +104,7 @@ public interface DeviceService extends EventHandler {
String EXTRA_MUSIC_REPEAT = "music_repeat"; String EXTRA_MUSIC_REPEAT = "music_repeat";
String EXTRA_MUSIC_POSITION = "music_position"; String EXTRA_MUSIC_POSITION = "music_position";
String EXTRA_MUSIC_RATE = "music_rate"; String EXTRA_MUSIC_RATE = "music_rate";
String EXTRA_PHONE_VOLUME = "phone_volume";
String EXTRA_APP_UUID = "app_uuid"; String EXTRA_APP_UUID = "app_uuid";
String EXTRA_APP_START = "app_start"; String EXTRA_APP_START = "app_start";
String EXTRA_APP_CONFIG = "app_config"; String EXTRA_APP_CONFIG = "app_config";

View File

@ -120,6 +120,7 @@ import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SE
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SET_FM_FREQUENCY; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SET_FM_FREQUENCY;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SET_HEARTRATE_MEASUREMENT_INTERVAL; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SET_HEARTRATE_MEASUREMENT_INTERVAL;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SET_LED_COLOR; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SET_LED_COLOR;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SET_PHONE_VOLUME;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SET_REMINDERS; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SET_REMINDERS;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SET_WORLD_CLOCKS; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_SET_WORLD_CLOCKS;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_START; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.ACTION_START;
@ -174,6 +175,7 @@ import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOT
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOTIFICATION_SUBJECT; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOTIFICATION_SUBJECT;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOTIFICATION_TITLE; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOTIFICATION_TITLE;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOTIFICATION_TYPE; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_NOTIFICATION_TYPE;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_PHONE_VOLUME;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_RECORDED_DATA_TYPES; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_RECORDED_DATA_TYPES;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_REMINDERS; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_REMINDERS;
import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_RESET_FLAGS; import static nodomain.freeyourgadget.gadgetbridge.model.DeviceService.EXTRA_RESET_FLAGS;
@ -533,6 +535,10 @@ public class DeviceCommunicationService extends Service implements SharedPrefere
musicSpec.trackNr = intent.getIntExtra(EXTRA_MUSIC_TRACKNR, 0); musicSpec.trackNr = intent.getIntExtra(EXTRA_MUSIC_TRACKNR, 0);
mDeviceSupport.onSetMusicInfo(musicSpec); mDeviceSupport.onSetMusicInfo(musicSpec);
break; break;
case ACTION_SET_PHONE_VOLUME:
float phoneVolume = intent.getFloatExtra(EXTRA_PHONE_VOLUME, 0);
mDeviceSupport.onSetPhoneVolume(phoneVolume);
break;
case ACTION_SETMUSICSTATE: case ACTION_SETMUSICSTATE:
MusicStateSpec stateSpec = new MusicStateSpec(); MusicStateSpec stateSpec = new MusicStateSpec();
stateSpec.shuffle = intent.getByteExtra(EXTRA_MUSIC_SHUFFLE, (byte) 0); stateSpec.shuffle = intent.getByteExtra(EXTRA_MUSIC_SHUFFLE, (byte) 0);

View File

@ -207,6 +207,14 @@ public class ServiceDeviceSupport implements DeviceSupport {
delegate.onSetMusicInfo(musicSpec); delegate.onSetMusicInfo(musicSpec);
} }
@Override
public void onSetPhoneVolume(float volume) {
if (checkBusy("set phone volume")) {
return;
}
delegate.onSetPhoneVolume(volume);
}
@Override @Override
public void onInstallApp(Uri uri) { public void onInstallApp(Uri uri) {
if (checkBusy("install app")) { if (checkBusy("install app")) {

View File

@ -374,6 +374,11 @@ public abstract class AbstractBTLEDeviceSupport extends AbstractDeviceSupport im
} }
@Override
public void onSetPhoneVolume(final float volume) {
}
@Override @Override
public void onSetReminders(ArrayList<? extends Reminder> reminders) { public void onSetReminders(ArrayList<? extends Reminder> reminders) {

View File

@ -26,6 +26,7 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo; import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.media.AudioManager;
import android.net.Uri; import android.net.Uri;
import android.widget.Toast; import android.widget.Toast;
@ -1230,11 +1231,40 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport {
} }
} }
private void sendMusicStateToDevice() { private void sendMusicStateToDevice() {
sendMusicStateToDevice(bufferMusicSpec, bufferMusicStateSpec); sendMusicStateToDevice(bufferMusicSpec, bufferMusicStateSpec);
} }
@Override
public void onSetPhoneVolume(final float volume) {
if (characteristicChunked == null) {
return;
}
final byte[] volumeCommand = new byte[]{0x40, (byte) Math.round(volume)};
try {
final TransactionBuilder builder = performInitialized("send volume");
writeToChunked(builder, 3, volumeCommand);
builder.queue(getQueue());
} catch (final IOException e) {
LOG.error("Unable to send volume", e);
}
LOG.info("sendVolumeStateToDevice: {}", volume);
}
private void sendVolumeStateToDevice() {
final AudioManager audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
final int volumeLevel = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
final int volumeMax = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
final int volumePercentage = (byte) Math.round(100 * (volumeLevel / (float) volumeMax));
onSetPhoneVolume(volumePercentage);
}
protected void sendMusicStateToDevice(MusicSpec musicSpec, MusicStateSpec musicStateSpec) { protected void sendMusicStateToDevice(MusicSpec musicSpec, MusicStateSpec musicStateSpec) {
if (characteristicChunked == null) { if (characteristicChunked == null) {
return; return;
@ -1712,6 +1742,7 @@ public class HuamiSupport extends AbstractBTLEDeviceSupport {
LOG.info("Music app started"); LOG.info("Music app started");
isMusicAppStarted = true; isMusicAppStarted = true;
sendMusicStateToDevice(); sendMusicStateToDevice();
sendVolumeStateToDevice();
break; break;
case (byte) 225: case (byte) 225:
LOG.info("Music app terminated"); LOG.info("Music app terminated");

View File

@ -76,6 +76,12 @@ public class GBMusicControlReceiver extends BroadcastReceiver {
case VOLUMEDOWN: case VOLUMEDOWN:
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, volumeAdjust, 0); audioManager.adjustStreamVolume(AudioManager.STREAM_MUSIC, volumeAdjust, 0);
final int volumeLevel = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
final int volumeMax = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
final int volumePercentage = (byte) Math.round(100 * (volumeLevel / (float) volumeMax));
GBApplication.deviceService().onSetPhoneVolume(volumePercentage);
break; break;
default: default:
return; return;

View File

@ -158,6 +158,12 @@ public abstract class AbstractSerialDeviceSupport extends AbstractDeviceSupport
sendToDevice(bytes); sendToDevice(bytes);
} }
@Override
public void onSetPhoneVolume(final float volume) {
byte[] bytes = gbDeviceProtocol.encodeVolume(volume);
sendToDevice(bytes);
}
@Override @Override
public void onAppInfoReq() { public void onAppInfoReq() {
byte[] bytes = gbDeviceProtocol.encodeAppInfoReq(); byte[] bytes = gbDeviceProtocol.encodeAppInfoReq();

View File

@ -64,6 +64,10 @@ public abstract class GBDeviceProtocol {
return null; return null;
} }
public byte[] encodeVolume(float volume) {
return null;
}
public byte[] encodeSetMusicState(byte state, int position, int playRate, byte shuffle, byte repeat) { public byte[] encodeSetMusicState(byte state, int position, int playRate, byte shuffle, byte repeat) {
return null; return null;
} }