Skip to content

Commit

Permalink
fix exception handling for mac, version 1.1.11
Browse files Browse the repository at this point in the history
  • Loading branch information
LabyStudio committed Feb 13, 2023
1 parent 0e3e902 commit d1158ae
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 45 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'de.labystudio'
version '1.1.10'
version '1.1.11'

compileJava {
sourceCompatibility = '1.8'
Expand Down
Expand Up @@ -55,7 +55,7 @@ public SpotifyAPI initialize(SpotifyConfiguration configuration) {
return this;
}

private void onInternalTick() {
protected void onInternalTick() {
try {
// Check if we passed the exception timeout
long timeSinceLastException = System.currentTimeMillis() - this.timeLastException;
Expand All @@ -78,7 +78,7 @@ private void onInternalTick() {
}
}

protected abstract void onTick();
protected abstract void onTick() throws Exception;

@Override
public void registerListener(SpotifyListener listener) {
Expand Down
Expand Up @@ -27,57 +27,58 @@ public class OSXSpotifyApi extends AbstractTickSpotifyAPI {
private long lastTimePositionUpdated;

@Override
protected void onTick() {
try {
String trackId = this.appleScript.getTrackId();
protected void onTick() throws Exception {
String trackId = this.appleScript.getTrackId();

// Handle on connect
if (!this.connected && !trackId.isEmpty()) {
this.connected = true;
this.listeners.forEach(SpotifyListener::onConnect);
}
// Handle on connect
if (!this.connected && !trackId.isEmpty()) {
this.connected = true;
this.listeners.forEach(SpotifyListener::onConnect);
}

// Handle track changes
if (!Objects.equals(trackId, this.currentTrack == null ? null : this.currentTrack.getId())) {
String trackName = this.appleScript.getTrackName();
String trackArtist = this.appleScript.getTrackArtist();
int trackLength = this.appleScript.getTrackLength();
// Handle track changes
if (!Objects.equals(trackId, this.currentTrack == null ? null : this.currentTrack.getId())) {
String trackName = this.appleScript.getTrackName();
String trackArtist = this.appleScript.getTrackArtist();
int trackLength = this.appleScript.getTrackLength();

boolean isFirstTrack = !this.hasTrack();
boolean isFirstTrack = !this.hasTrack();

Track track = new Track(trackId, trackName, trackArtist, trackLength);
this.currentTrack = track;
Track track = new Track(trackId, trackName, trackArtist, trackLength);
this.currentTrack = track;

// Fire on track changed
this.listeners.forEach(listener -> listener.onTrackChanged(track));
// Fire on track changed
this.listeners.forEach(listener -> listener.onTrackChanged(track));

// Reset position on song change
if (!isFirstTrack) {
this.updatePosition(0);
}
// Reset position on song change
if (!isFirstTrack) {
this.updatePosition(0);
}
}

// Handle is playing changes
boolean isPlaying = this.appleScript.getPlayerState();
if (isPlaying != this.isPlaying) {
this.isPlaying = isPlaying;

// Fire on play back changed
this.listeners.forEach(listener -> listener.onPlayBackChanged(isPlaying));
}
// Handle is playing changes
boolean isPlaying = this.appleScript.getPlayerState();
if (isPlaying != this.isPlaying) {
this.isPlaying = isPlaying;

// Handle position changes
int position = this.appleScript.getPlayerPosition();
if (!this.hasPosition() || Math.abs(position - this.getPosition()) > 1000) {
this.updatePosition(position);
}
// Fire on play back changed
this.listeners.forEach(listener -> listener.onPlayBackChanged(isPlaying));
}

// Fire keep alive
this.listeners.forEach(SpotifyListener::onSync);
} catch (Exception e) {
this.listeners.forEach(listener -> listener.onDisconnect(e));
this.connected = false;
// Handle position changes
int position = this.appleScript.getPlayerPosition();
if (!this.hasPosition() || Math.abs(position - this.getPosition()) > 1000) {
this.updatePosition(position);
}

// Fire keep alive
this.listeners.forEach(SpotifyListener::onSync);
}

@Override
public void stop() {
super.stop();
this.connected = false;
}

private void updatePosition(int position) {
Expand Down
Expand Up @@ -36,7 +36,7 @@ public class WinSpotifyAPI extends AbstractTickSpotifyAPI {
* Updates the current track, position and playback state.
* If the process is not connected, it will try to connect to the Spotify process.
*/
protected void onTick() {
protected void onTick() throws Exception {
if (!this.isConnected()) {
// Connect
this.process = new SpotifyProcess();
Expand Down Expand Up @@ -173,7 +173,7 @@ public void pressMediaKey(MediaKey mediaKey) {
}

// Update state immediately
this.onTick();
this.onInternalTick();
}

@Override
Expand Down

0 comments on commit d1158ae

Please sign in to comment.