diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/VolumeChangeReceiver.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/VolumeChangeReceiver.java new file mode 100644 index 000000000..6f91a05bd --- /dev/null +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/externalevents/VolumeChangeReceiver.java @@ -0,0 +1,83 @@ +package nodomain.freeyourgadget.gadgetbridge.externalevents; + +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.content.IntentFilter; +import android.media.AudioManager; + +import androidx.core.content.ContextCompat; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.ref.WeakReference; + +import nodomain.freeyourgadget.gadgetbridge.GBApplication; + +public class VolumeChangeReceiver { + private final Logger LOG = LoggerFactory.getLogger(VolumeChangeReceiver.class); + + private static final String EXTRA_VOLUME_STREAM_TYPE = "android.media.EXTRA_VOLUME_STREAM_TYPE"; + private static final String VOLUME_CHANGED_ACTION = "android.media.VOLUME_CHANGED_ACTION"; + private AudioManager audioManager; + private Context context; + private boolean registered = false; + private VolumeChangeReceiver.VolumeBroadcastReceiver volumeBroadcastReceiver; + + public VolumeChangeReceiver() {} + + public int getCurrentMusicVolume() { + AudioManager audioManager = this.audioManager; + if (audioManager != null) { + return audioManager.getStreamVolume(AudioManager.STREAM_MUSIC); + } + return -1; + } + + public int getMaxMusicVolume() { + AudioManager audioManager = this.audioManager; + if (audioManager != null) { + return audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC); + } + return 15; + } + + public void registerReceiver(Context context) { + this.context = context; + this.audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); + this.volumeBroadcastReceiver = new VolumeBroadcastReceiver(this); + IntentFilter intentFilter = new IntentFilter(); + intentFilter.addAction(VOLUME_CHANGED_ACTION); + ContextCompat.registerReceiver(this.context, this.volumeBroadcastReceiver, intentFilter, ContextCompat.RECEIVER_EXPORTED); + this.registered = true; + } + + public void unregisterReceiver() { + if (this.registered) { + try { + this.context.unregisterReceiver(this.volumeBroadcastReceiver); + this.registered = false; + } catch (Exception e) { + LOG.error("Error unregister volume receiver", e); + } + } + } + + private static class VolumeBroadcastReceiver extends BroadcastReceiver { + private final WeakReference observerWeakReference; + + public VolumeBroadcastReceiver(VolumeChangeReceiver volumeChangeObserver) { + this.observerWeakReference = new WeakReference<>(volumeChangeObserver); + } + + public void onReceive(Context context, Intent intent) { + VolumeChangeReceiver volumeChangeObserver; + int currentMusicVolume; + if (VolumeChangeReceiver.VOLUME_CHANGED_ACTION.equals(intent.getAction()) && intent.getIntExtra(VolumeChangeReceiver.EXTRA_VOLUME_STREAM_TYPE, -1) == AudioManager.STREAM_MUSIC && (volumeChangeObserver = this.observerWeakReference.get()) != null && (currentMusicVolume = volumeChangeObserver.getCurrentMusicVolume()) >= 0) { + final int volumePercentage = (byte) Math.round(100 * (currentMusicVolume / (float) volumeChangeObserver.getMaxMusicVolume())); + GBApplication.deviceService().onSetPhoneVolume(volumePercentage); + } + } + } +} diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java index bcdf3c7db..f30b2c2e4 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/DeviceCommunicationService.java @@ -84,6 +84,7 @@ import nodomain.freeyourgadget.gadgetbridge.externalevents.SMSReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.SilentModeReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.TimeChangeReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.TinyWeatherForecastGermanyReceiver; +import nodomain.freeyourgadget.gadgetbridge.externalevents.VolumeChangeReceiver; import nodomain.freeyourgadget.gadgetbridge.externalevents.gps.GBLocationService; import nodomain.freeyourgadget.gadgetbridge.externalevents.sleepasandroid.SleepAsAndroidReceiver; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; @@ -259,6 +260,8 @@ public class DeviceCommunicationService extends Service implements SharedPrefere private GBAutoFetchReceiver mGBAutoFetchReceiver = null; private AutoConnectIntervalReceiver mAutoConnectInvervalReceiver = null; + private VolumeChangeReceiver mVolumeChangeReceiver = null; + private AlarmReceiver mAlarmReceiver = null; private final List mCalendarReceiver = new ArrayList<>(); private CMWeatherReceiver mCMWeatherReceiver = null; @@ -1359,6 +1362,10 @@ public class DeviceCommunicationService extends Service implements SharedPrefere } ContextCompat.registerReceiver(this, mMusicPlaybackReceiver, filter, ContextCompat.RECEIVER_EXPORTED); } + if (mVolumeChangeReceiver == null && features.supportsMusicInfo()) { + mVolumeChangeReceiver = new VolumeChangeReceiver(); + mVolumeChangeReceiver.registerReceiver(this); + } if (mTimeChangeReceiver == null) { mTimeChangeReceiver = new TimeChangeReceiver(); IntentFilter filter = new IntentFilter(); @@ -1461,6 +1468,10 @@ public class DeviceCommunicationService extends Service implements SharedPrefere unregisterReceiver(mMusicPlaybackReceiver); mMusicPlaybackReceiver = null; } + if (mVolumeChangeReceiver != null) { + mVolumeChangeReceiver.unregisterReceiver(); + mVolumeChangeReceiver = null; + } if (mTimeChangeReceiver != null) { unregisterReceiver(mTimeChangeReceiver); mTimeChangeReceiver = null; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiBRSupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiBRSupport.java index 685eb6163..bdc37f94d 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiBRSupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiBRSupport.java @@ -24,13 +24,11 @@ import android.net.Uri; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java.util.ArrayList; -import java.util.List; import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCameraRemote; import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiConstants; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; -import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceMusic; import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec; import nodomain.freeyourgadget.gadgetbridge.model.CallSpec; import nodomain.freeyourgadget.gadgetbridge.model.Contact; diff --git a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiLESupport.java b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiLESupport.java index 15868167a..7f239194c 100644 --- a/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiLESupport.java +++ b/app/src/main/java/nodomain/freeyourgadget/gadgetbridge/service/devices/huawei/HuaweiLESupport.java @@ -33,7 +33,6 @@ import java.util.UUID; import nodomain.freeyourgadget.gadgetbridge.deviceevents.GBDeviceEventCameraRemote; import nodomain.freeyourgadget.gadgetbridge.devices.huawei.HuaweiConstants; import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice; -import nodomain.freeyourgadget.gadgetbridge.impl.GBDeviceMusic; import nodomain.freeyourgadget.gadgetbridge.model.CalendarEventSpec; import nodomain.freeyourgadget.gadgetbridge.model.CallSpec; import nodomain.freeyourgadget.gadgetbridge.model.Contact;