@@ -76,6 +76,7 @@ public final class Session implements Closeable {
7676 private final AtomicBoolean authLock = new AtomicBoolean (false );
7777 private final OkHttpClient client ;
7878 private final List <CloseListener > closeListeners = Collections .synchronizedList (new ArrayList <>());
79+ private final List <ReconnectionListener > reconnectionListeners = Collections .synchronizedList (new ArrayList <>());
7980 private ConnectionHolder conn ;
8081 private CipherPair cipherPair ;
8182 private Receiver receiver ;
@@ -528,6 +529,10 @@ public boolean valid() {
528529 return apWelcome != null && conn != null && !conn .socket .isClosed ();
529530 }
530531
532+ public boolean reconnecting () {
533+ return !closed && conn == null ;
534+ }
535+
531536 @ NotNull
532537 public String deviceId () {
533538 return inner .deviceId ;
@@ -554,6 +559,10 @@ public Random random() {
554559 }
555560
556561 private void reconnect () {
562+ synchronized (reconnectionListeners ) {
563+ reconnectionListeners .forEach (ReconnectionListener ::onConnectionDropped );
564+ }
565+
557566 try {
558567 if (conn != null ) {
559568 conn .socket .close ();
@@ -569,7 +578,12 @@ private void reconnect() {
569578 .build (), true );
570579
571580 LOGGER .info (String .format ("Re-authenticated as %s!" , apWelcome .getCanonicalUsername ()));
581+
582+ synchronized (reconnectionListeners ) {
583+ reconnectionListeners .forEach (ReconnectionListener ::onConnectionEstablished );
584+ }
572585 } catch (IOException | GeneralSecurityException | SpotifyAuthenticationException ex ) {
586+ conn = null ;
573587 LOGGER .error ("Failed reconnecting, retrying in 10 seconds..." , ex );
574588 scheduler .schedule (this ::reconnect , 10 , TimeUnit .SECONDS );
575589 }
@@ -589,6 +603,16 @@ public void addCloseListener(@NotNull CloseListener listener) {
589603 if (!closeListeners .contains (listener )) closeListeners .add (listener );
590604 }
591605
606+ public void addReconnectionListener (@ NotNull ReconnectionListener listener ) {
607+ if (!reconnectionListeners .contains (listener )) reconnectionListeners .add (listener );
608+ }
609+
610+ public interface ReconnectionListener {
611+ void onConnectionDropped ();
612+
613+ void onConnectionEstablished ();
614+ }
615+
592616 public interface ProxyConfiguration {
593617 boolean proxyEnabled ();
594618
0 commit comments