2024-01-10 18:54:00 +01:00
|
|
|
/* Copyright (C) 2015-2024 andre, Andreas Shimokawa, Avamander, Carsten
|
2017-03-10 14:53:19 +01:00
|
|
|
Pfeiffer, Daniele Gobbetti
|
|
|
|
|
|
|
|
This file is part of Gadgetbridge.
|
|
|
|
|
|
|
|
Gadgetbridge is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Gadgetbridge is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
2024-01-10 18:54:00 +01:00
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>. */
|
2015-04-20 22:39:35 +02:00
|
|
|
package nodomain.freeyourgadget.gadgetbridge.externalevents;
|
2015-02-08 23:53:40 +01:00
|
|
|
|
|
|
|
import android.content.BroadcastReceiver;
|
|
|
|
import android.content.Context;
|
|
|
|
import android.content.Intent;
|
2017-02-01 22:55:40 +01:00
|
|
|
import android.os.Bundle;
|
2015-05-12 06:28:11 +02:00
|
|
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
2015-02-08 23:53:40 +01:00
|
|
|
|
2015-08-21 00:58:18 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.GBApplication;
|
2016-04-04 20:08:34 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.MusicSpec;
|
2016-06-11 23:37:03 +02:00
|
|
|
import nodomain.freeyourgadget.gadgetbridge.model.MusicStateSpec;
|
2015-04-20 22:39:35 +02:00
|
|
|
|
2015-02-08 23:53:40 +01:00
|
|
|
public class MusicPlaybackReceiver extends BroadcastReceiver {
|
2015-05-12 06:28:11 +02:00
|
|
|
private static final Logger LOG = LoggerFactory.getLogger(MusicPlaybackReceiver.class);
|
2016-06-10 23:13:33 +02:00
|
|
|
private static MusicSpec lastMusicSpec = new MusicSpec();
|
2017-02-11 09:49:01 +01:00
|
|
|
private static MusicStateSpec lastStateSpec = new MusicStateSpec();
|
2015-02-08 23:53:40 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onReceive(Context context, Intent intent) {
|
2016-06-11 23:37:03 +02:00
|
|
|
/*
|
|
|
|
Bundle bundle = intent.getExtras();
|
|
|
|
for (String key : bundle.keySet()) {
|
|
|
|
Object value = bundle.get(key);
|
|
|
|
LOG.info(String.format("%s %s (%s)", key,
|
|
|
|
value != null ? value.toString() : "null", value != null ? value.getClass().getName() : "no class"));
|
|
|
|
}
|
|
|
|
*/
|
2017-02-01 22:55:40 +01:00
|
|
|
MusicSpec musicSpec = new MusicSpec(lastMusicSpec);
|
2017-02-11 09:49:01 +01:00
|
|
|
MusicStateSpec stateSpec = new MusicStateSpec(lastStateSpec);
|
2016-12-26 23:53:24 +01:00
|
|
|
|
2017-02-01 22:55:40 +01:00
|
|
|
Bundle incomingBundle = intent.getExtras();
|
2017-08-25 22:01:21 +02:00
|
|
|
|
|
|
|
if (incomingBundle == null) {
|
|
|
|
LOG.warn("Not processing incoming null bundle.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-02-01 22:55:40 +01:00
|
|
|
for (String key : incomingBundle.keySet()) {
|
|
|
|
Object incoming = incomingBundle.get(key);
|
|
|
|
if (incoming instanceof String && "artist".equals(key)) {
|
|
|
|
musicSpec.artist = (String) incoming;
|
|
|
|
} else if (incoming instanceof String && "album".equals(key)) {
|
|
|
|
musicSpec.album = (String) incoming;
|
|
|
|
} else if (incoming instanceof String && "track".equals(key)) {
|
|
|
|
musicSpec.track = (String) incoming;
|
|
|
|
} else if (incoming instanceof String && "title".equals(key) && musicSpec.track == null) {
|
|
|
|
musicSpec.track = (String) incoming;
|
|
|
|
} else if (incoming instanceof Integer && "duration".equals(key)) {
|
|
|
|
musicSpec.duration = (Integer) incoming / 1000;
|
|
|
|
} else if (incoming instanceof Long && "duration".equals(key)) {
|
|
|
|
musicSpec.duration = ((Long) incoming).intValue() / 1000;
|
|
|
|
} else if (incoming instanceof Integer && "position".equals(key)) {
|
|
|
|
stateSpec.position = (Integer) incoming / 1000;
|
|
|
|
} else if (incoming instanceof Long && "position".equals(key)) {
|
|
|
|
stateSpec.position = ((Long) incoming).intValue() / 1000;
|
|
|
|
} else if (incoming instanceof Boolean && "playing".equals(key)) {
|
|
|
|
stateSpec.state = (byte) (((Boolean) incoming) ? MusicStateSpec.STATE_PLAYING : MusicStateSpec.STATE_PAUSED);
|
2017-02-17 09:01:37 +01:00
|
|
|
stateSpec.playRate = (byte) (((Boolean) incoming) ? 100 : 0);
|
2017-02-11 09:49:01 +01:00
|
|
|
} else if (incoming instanceof String && "duration".equals(key)) {
|
2018-08-17 00:25:48 +02:00
|
|
|
musicSpec.duration = Integer.parseInt((String) incoming) / 1000;
|
2017-02-11 09:49:01 +01:00
|
|
|
} else if (incoming instanceof String && "trackno".equals(key)) {
|
2018-08-17 00:25:48 +02:00
|
|
|
musicSpec.trackNr = Integer.parseInt((String) incoming);
|
2017-02-11 09:49:01 +01:00
|
|
|
} else if (incoming instanceof String && "totaltrack".equals(key)) {
|
2018-08-17 00:25:48 +02:00
|
|
|
musicSpec.trackCount = Integer.parseInt((String) incoming);
|
2017-02-11 09:49:01 +01:00
|
|
|
} else if (incoming instanceof Integer && "pos".equals(key)) {
|
|
|
|
stateSpec.position = (Integer) incoming;
|
2017-02-17 09:01:37 +01:00
|
|
|
} else if (incoming instanceof Integer && "repeat".equals(key)) {
|
|
|
|
if ((Integer) incoming > 0) {
|
|
|
|
stateSpec.repeat = 1;
|
|
|
|
} else {
|
|
|
|
stateSpec.repeat = 0;
|
|
|
|
}
|
|
|
|
} else if (incoming instanceof Integer && "shuffle".equals(key)) {
|
|
|
|
if ((Integer) incoming > 0) {
|
|
|
|
stateSpec.shuffle = 1;
|
|
|
|
} else {
|
|
|
|
stateSpec.shuffle = 0;
|
|
|
|
}
|
2017-02-01 22:55:40 +01:00
|
|
|
}
|
|
|
|
}
|
2016-06-11 23:37:03 +02:00
|
|
|
|
2016-06-10 23:13:33 +02:00
|
|
|
if (!lastMusicSpec.equals(musicSpec)) {
|
|
|
|
lastMusicSpec = musicSpec;
|
|
|
|
LOG.info("Update Music Info: " + musicSpec.artist + " / " + musicSpec.album + " / " + musicSpec.track);
|
|
|
|
GBApplication.deviceService().onSetMusicInfo(musicSpec);
|
|
|
|
} else {
|
2017-02-11 09:49:01 +01:00
|
|
|
LOG.info("Got metadata changed intent, but nothing changed, ignoring.");
|
2016-06-10 23:13:33 +02:00
|
|
|
}
|
2016-06-11 23:37:03 +02:00
|
|
|
|
2017-02-11 09:49:01 +01:00
|
|
|
if (!lastStateSpec.equals(stateSpec)) {
|
|
|
|
lastStateSpec = stateSpec;
|
|
|
|
LOG.info("Update Music State: state=" + stateSpec.state + ", position= " + stateSpec.position);
|
|
|
|
GBApplication.deviceService().onSetMusicState(stateSpec);
|
|
|
|
} else {
|
|
|
|
LOG.info("Got state changed intent, but not enough has changed, ignoring.");
|
2016-06-11 23:37:03 +02:00
|
|
|
}
|
2015-02-08 23:53:40 +01:00
|
|
|
}
|
|
|
|
}
|