Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/main/java/de/labystudio/spotifyapi/SpotifyAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ default SpotifyAPI initialize() {
*/
SpotifyAPI initialize(SpotifyConfiguration configuration);


/**
* Initialize the SpotifyAPI and connect to the Spotify process asynchronously.
*
Expand All @@ -47,7 +46,6 @@ default CompletableFuture<SpotifyAPI> initializeAsync(SpotifyConfiguration confi
return CompletableFuture.supplyAsync(() -> this.initialize(configuration));
}


/**
* Initialize the SpotifyAPI and connect to the Spotify process asynchronously.
* It will use a default configuration.
Expand Down Expand Up @@ -119,7 +117,6 @@ default boolean hasTrack() {
*/
boolean isConnected();


/**
* Returns true if the background process is running.
*
Expand Down Expand Up @@ -154,4 +151,11 @@ default boolean hasTrack() {
* Disconnect from the Spotify application and stop all background tasks.
*/
void stop();

/**
* Similar to {@link SpotifyAPI#stop()} but releases any held resources.
* <p>
* Should only be called if there is no intent to reuse this instance.
*/
void shutdown();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

Expand All @@ -27,33 +28,34 @@ public abstract class AbstractTickSpotifyAPI implements SpotifyAPI {

private SpotifyConfiguration configuration;

private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();

private ScheduledFuture<?> task;

private long timeLastException = -1;

/**
* Initialize the SpotifyAPI abstract tick implementation.
* It will create a task that will update the current track and position every second.
*
* @return the initialized SpotifyAPI
* @throws IllegalStateException if the API is already initialized
* @throws IllegalStateException if the API is already initialized or has been shutdown
*/
@Override
public SpotifyAPI initialize(SpotifyConfiguration configuration) {
synchronized (this) {
this.configuration = configuration;

if (this.executor.isShutdown()) {
throw new IllegalStateException("This SpotifyAPI has been shutdown and cannot be reused");
}

if (this.isInitialized()) {
throw new IllegalStateException("The SpotifyAPI is already initialized");
throw new IllegalStateException("This SpotifyAPI is already initialized");
}

// Start task to update every second
this.task = Executors.newScheduledThreadPool(1)
.scheduleWithFixedDelay(
this::onInternalTick,
0,
1,
TimeUnit.SECONDS
);
this.task = this.executor.scheduleWithFixedDelay(this::onInternalTick, 0L, 1L, TimeUnit.SECONDS);
}
return this;
}
Expand Down Expand Up @@ -120,4 +122,10 @@ public void stop() {
}
}
}

@Override
public void shutdown() {
this.stop();
this.executor.shutdownNow();
}
}