mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-01-10 09:01:55 +01:00
Volume changes receiver added
This commit is contained in:
parent
4deb5213f9
commit
ae6e387ca6
@ -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<VolumeChangeReceiver> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -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<CalendarReceiver> 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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
Loading…
Reference in New Issue
Block a user