mirror of
https://codeberg.org/Freeyourgadget/Gadgetbridge.git
synced 2025-01-25 16:15:55 +01:00
Garmin: Remove music state timer
This commit is contained in:
parent
4f7fb8117e
commit
d4e34c5982
@ -1,7 +1,9 @@
|
|||||||
package nodomain.freeyourgadget.gadgetbridge.service.devices.garmin;
|
package nodomain.freeyourgadget.gadgetbridge.service.devices.garmin;
|
||||||
|
|
||||||
|
import android.bluetooth.BluetoothAdapter;
|
||||||
import android.bluetooth.BluetoothGatt;
|
import android.bluetooth.BluetoothGatt;
|
||||||
import android.bluetooth.BluetoothGattCharacteristic;
|
import android.bluetooth.BluetoothGattCharacteristic;
|
||||||
|
import android.content.Context;
|
||||||
import android.location.Location;
|
import android.location.Location;
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
@ -14,7 +16,6 @@ import org.slf4j.LoggerFactory;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.text.DecimalFormat;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@ -26,8 +27,6 @@ import java.util.Map;
|
|||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import java.util.Queue;
|
import java.util.Queue;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.Timer;
|
|
||||||
import java.util.TimerTask;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
@ -81,8 +80,8 @@ import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages.Supp
|
|||||||
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages.SystemEventMessage;
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages.SystemEventMessage;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages.status.NotificationSubscriptionStatusMessage;
|
import nodomain.freeyourgadget.gadgetbridge.service.devices.garmin.messages.status.NotificationSubscriptionStatusMessage;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
import nodomain.freeyourgadget.gadgetbridge.util.GB;
|
||||||
|
import nodomain.freeyourgadget.gadgetbridge.util.MediaManager;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
import nodomain.freeyourgadget.gadgetbridge.util.Prefs;
|
||||||
import nodomain.freeyourgadget.gadgetbridge.util.StringUtils;
|
|
||||||
|
|
||||||
import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_ALLOW_HIGH_MTU;
|
import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_ALLOW_HIGH_MTU;
|
||||||
import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_SEND_APP_NOTIFICATIONS;
|
import static nodomain.freeyourgadget.gadgetbridge.activities.devicesettings.DeviceSettingsPreferenceConst.PREF_SEND_APP_NOTIFICATIONS;
|
||||||
@ -97,8 +96,7 @@ public class GarminSupport extends AbstractBTLEDeviceSupport implements ICommuni
|
|||||||
private final List<MessageHandler> messageHandlers;
|
private final List<MessageHandler> messageHandlers;
|
||||||
private final List<FileType> supportedFileTypeList = new ArrayList<>();
|
private final List<FileType> supportedFileTypeList = new ArrayList<>();
|
||||||
private ICommunicator communicator;
|
private ICommunicator communicator;
|
||||||
private MusicStateSpec musicStateSpec;
|
private MediaManager mediaManager;
|
||||||
private Timer musicStateTimer;
|
|
||||||
private boolean mFirstConnect = false;
|
private boolean mFirstConnect = false;
|
||||||
private boolean isBusyFetching;
|
private boolean isBusyFetching;
|
||||||
|
|
||||||
@ -116,22 +114,19 @@ public class GarminSupport extends AbstractBTLEDeviceSupport implements ICommuni
|
|||||||
messageHandlers.add(notificationsHandler);
|
messageHandlers.add(notificationsHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setContext(final GBDevice gbDevice, final BluetoothAdapter btAdapter, final Context context) {
|
||||||
|
super.setContext(gbDevice, btAdapter, context);
|
||||||
|
this.mediaManager = new MediaManager(context);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void dispose() {
|
public void dispose() {
|
||||||
LOG.info("Garmin dispose()");
|
LOG.info("Garmin dispose()");
|
||||||
GBLocationService.stop(getContext(), getDevice());
|
GBLocationService.stop(getContext(), getDevice());
|
||||||
stopMusicTimer();
|
|
||||||
super.dispose();
|
super.dispose();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void stopMusicTimer() {
|
|
||||||
if (musicStateTimer != null) {
|
|
||||||
musicStateTimer.cancel();
|
|
||||||
musicStateTimer.purge();
|
|
||||||
musicStateTimer = null;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void addFileToDownloadList(FileTransferHandler.DirectoryEntry directoryEntry) {
|
public void addFileToDownloadList(FileTransferHandler.DirectoryEntry directoryEntry) {
|
||||||
filesToDownload.add(directoryEntry);
|
filesToDownload.add(directoryEntry);
|
||||||
}
|
}
|
||||||
@ -632,6 +627,11 @@ public class GarminSupport extends AbstractBTLEDeviceSupport implements ICommuni
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSetMusicInfo(MusicSpec musicSpec) {
|
public void onSetMusicInfo(MusicSpec musicSpec) {
|
||||||
|
if (!mediaManager.onSetMusicInfo(musicSpec)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
LOG.debug("onSetMusicInfo: {}", musicSpec.toString());
|
||||||
|
|
||||||
Map<MusicControlEntityUpdateMessage.MusicEntity, String> attributes = new HashMap<>();
|
Map<MusicControlEntityUpdateMessage.MusicEntity, String> attributes = new HashMap<>();
|
||||||
|
|
||||||
@ -641,42 +641,41 @@ public class GarminSupport extends AbstractBTLEDeviceSupport implements ICommuni
|
|||||||
attributes.put(MusicControlEntityUpdateMessage.TRACK.DURATION, String.valueOf(musicSpec.duration));
|
attributes.put(MusicControlEntityUpdateMessage.TRACK.DURATION, String.valueOf(musicSpec.duration));
|
||||||
|
|
||||||
sendOutgoingMessage("set music info", new MusicControlEntityUpdateMessage(attributes));
|
sendOutgoingMessage("set music info", new MusicControlEntityUpdateMessage(attributes));
|
||||||
|
|
||||||
|
// Update the music state spec as well
|
||||||
|
final MusicStateSpec bufferMusicStateSpec = mediaManager.getBufferMusicStateSpec();
|
||||||
|
if (bufferMusicStateSpec != null) {
|
||||||
|
sendMusicState(bufferMusicStateSpec, bufferMusicStateSpec.position);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onSetMusicState(MusicStateSpec stateSpec) {
|
public void onSetMusicState(MusicStateSpec stateSpec) {
|
||||||
musicStateSpec = stateSpec;
|
if (!mediaManager.onSetMusicState(stateSpec)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
stopMusicTimer();
|
|
||||||
|
|
||||||
musicStateTimer = new Timer();
|
|
||||||
int updatePeriod = 29000; //milliseconds
|
|
||||||
LOG.debug("onSetMusicState: {}", stateSpec.toString());
|
LOG.debug("onSetMusicState: {}", stateSpec.toString());
|
||||||
|
|
||||||
|
sendMusicState(stateSpec, stateSpec.position);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendMusicState(final MusicStateSpec stateSpec, final int progress) {
|
||||||
|
final int playing;
|
||||||
|
final float playRate;
|
||||||
if (stateSpec.state == MusicStateSpec.STATE_PLAYING) {
|
if (stateSpec.state == MusicStateSpec.STATE_PLAYING) {
|
||||||
musicStateTimer.schedule(new TimerTask() {
|
playing = 1;
|
||||||
@Override
|
playRate = stateSpec.playRate > 0 ? stateSpec.playRate / 100f : 1.0f;
|
||||||
public void run() {
|
|
||||||
String playing = "1";
|
|
||||||
String playRate = "1.0";
|
|
||||||
String position = new DecimalFormat("#.000").format(musicStateSpec.position);
|
|
||||||
musicStateSpec.position += updatePeriod / 1000;
|
|
||||||
|
|
||||||
Map<MusicControlEntityUpdateMessage.MusicEntity, String> attributes = new HashMap<>();
|
|
||||||
attributes.put(MusicControlEntityUpdateMessage.PLAYER.PLAYBACK_INFO, StringUtils.join(",", playing, playRate, position).toString());
|
|
||||||
sendOutgoingMessage("music state timer", new MusicControlEntityUpdateMessage(attributes));
|
|
||||||
|
|
||||||
}
|
|
||||||
}, 0, updatePeriod);
|
|
||||||
} else {
|
} else {
|
||||||
String playing = "0";
|
playing = 0;
|
||||||
String playRate = "0.0";
|
playRate = 0;
|
||||||
String position = new DecimalFormat("#.###").format(stateSpec.position);
|
|
||||||
|
|
||||||
Map<MusicControlEntityUpdateMessage.MusicEntity, String> attributes = new HashMap<>();
|
|
||||||
attributes.put(MusicControlEntityUpdateMessage.PLAYER.PLAYBACK_INFO, StringUtils.join(",", playing, playRate, position).toString());
|
|
||||||
sendOutgoingMessage("music stopped", new MusicControlEntityUpdateMessage(attributes));
|
|
||||||
}
|
}
|
||||||
|
final Map<MusicControlEntityUpdateMessage.MusicEntity, String> attributes = new HashMap<>();
|
||||||
|
attributes.put(
|
||||||
|
MusicControlEntityUpdateMessage.PLAYER.PLAYBACK_INFO,
|
||||||
|
String.format(Locale.ROOT, "%d,%.1f,%.3f", playing, playRate, (float) progress)
|
||||||
|
);
|
||||||
|
sendOutgoingMessage("set music state", new MusicControlEntityUpdateMessage(attributes));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
Loading…
Reference in New Issue
Block a user