@@ -68,6 +68,7 @@ public final class DeviceStateHandler implements Closeable, DealerClient.Message
6868 private final AsyncWorker <Connect .PutStateRequest > putStateWorker ;
6969 private volatile String connectionId = null ;
7070 private volatile boolean closing = false ;
71+ private String lastCommandSentByDeviceId ;
7172
7273 public DeviceStateHandler (@ NotNull Session session , @ NotNull PlayerConfiguration conf ) {
7374 this .session = session ;
@@ -162,13 +163,6 @@ public void onMessage(@NotNull String uri, @NotNull Map<String, String> headers,
162163 updateConnectionId (headers .get ("Spotify-Connection-Id" ));
163164 } else if (Objects .equals (uri , "hm://connect-state/v1/connect/volume" )) {
164165 Connect .SetVolumeCommand cmd = Connect .SetVolumeCommand .parseFrom (payload );
165- synchronized (this ) {
166- if (cmd .hasCommandOptions ()) {
167- putState .setLastCommandMessageId (cmd .getCommandOptions ().getMessageId ())
168- .clearLastCommandSentByDeviceId ();
169- }
170- }
171-
172166 setVolume (cmd .getVolume ());
173167 } else if (Objects .equals (uri , "hm://connect-state/v1/cluster" )) {
174168 Connect .ClusterUpdate update = Connect .ClusterUpdate .parseFrom (payload );
@@ -188,7 +182,7 @@ public void onMessage(@NotNull String uri, @NotNull Map<String, String> headers,
188182 @ NotNull
189183 @ Override
190184 public RequestResult onRequest (@ NotNull String mid , int pid , @ NotNull String sender , @ NotNull JsonObject command ) {
191- putState . setLastCommandMessageId ( pid ). setLastCommandSentByDeviceId ( sender ) ;
185+ lastCommandSentByDeviceId = sender ;
192186
193187 Endpoint endpoint = Endpoint .parse (command .get ("endpoint" ).getAsString ());
194188 notifyCommand (endpoint , new CommandBody (command ));
@@ -197,7 +191,7 @@ public RequestResult onRequest(@NotNull String mid, int pid, @NotNull String sen
197191
198192 @ Nullable
199193 public synchronized String getLastCommandSentByDeviceId () {
200- return putState . getLastCommandSentByDeviceId () ;
194+ return lastCommandSentByDeviceId ;
201195 }
202196
203197 private synchronized long startedPlayingAt () {
@@ -223,12 +217,18 @@ public synchronized void setIsActive(boolean active) {
223217 public synchronized void updateState (@ NotNull Connect .PutStateReason reason , int playerTime , @ NotNull Player .PlayerState state ) {
224218 if (connectionId == null ) throw new IllegalStateException ();
225219
226- if (playerTime == -1 ) putState .clearHasBeenPlayingForMs ();
227- else putState .setHasBeenPlayingForMs (playerTime );
220+ long timestamp = TimeProvider .currentTimeMillis ();
221+
222+ if (playerTime == -1 )
223+ putState .clearHasBeenPlayingForMs ();
224+ else
225+ putState .setHasBeenPlayingForMs (Math .min (playerTime , timestamp - putState .getStartedPlayingAt ()));
228226
229227 putState .setPutStateReason (reason )
230- .setClientSideTimestamp (TimeProvider .currentTimeMillis ())
231- .getDeviceBuilder ().setDeviceInfo (deviceInfo ).setPlayerState (state );
228+ .setClientSideTimestamp (timestamp )
229+ .getDeviceBuilder ()
230+ .setDeviceInfo (deviceInfo )
231+ .setPlayerState (state );
232232
233233 try {
234234 putStateWorker .submit (putState .build ());
@@ -267,6 +267,8 @@ public void close() {
267267 * @param req The {@link Connect.PutStateRequest}
268268 */
269269 private void putConnectState (@ NotNull Connect .PutStateRequest req ) {
270+ System .out .println (req );
271+
270272 try {
271273 session .api ().putConnectState (connectionId , req );
272274 if (LOGGER .isTraceEnabled ()) {
0 commit comments