]> git.basschouten.com Git - openhab-addons.git/commitdiff
[sonos] Fix handling of InterruptedException (Thread.sleep) (#10459)
authorlolodomo <lg.hc@free.fr>
Mon, 5 Apr 2021 13:15:28 +0000 (15:15 +0200)
committerGitHub <noreply@github.com>
Mon, 5 Apr 2021 13:15:28 +0000 (15:15 +0200)
* [sonos] Fix handling of InterruptedException (Thread.sleep)

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
* Avoid catching Exception

Signed-off-by: Laurent Garnier <lg.hc@free.fr>
bundles/org.openhab.binding.sonos/src/main/java/org/openhab/binding/sonos/internal/SonosXMLParser.java
bundles/org.openhab.binding.sonos/src/main/java/org/openhab/binding/sonos/internal/handler/ZonePlayerHandler.java

index c70bb59332a07a11681f495341c075286ce35381..c152c59b5bd3bd556ae9257ec21d63ac2ba552e7 100644 (file)
@@ -371,7 +371,7 @@ public class SonosXMLParser {
                 int trackNumberVal = 0;
                 try {
                     trackNumberVal = Integer.parseInt(trackNumber.toString());
-                } catch (Exception e) {
+                } catch (NumberFormatException e) {
                 }
 
                 SonosResourceMetaData md = null;
index e8731c99a99f1c80a209d950f535aa079e48df50..aca3820ce1aea853e10bebc124bad12c5c823cd4 100644 (file)
@@ -2552,6 +2552,9 @@ public class ZonePlayerHandler extends BaseThingHandler implements UpnpIOPartici
                 coordinator.play();
             } catch (IllegalStateException e) {
                 logger.debug("Cannot play URI ({})", e.getMessage());
+            } catch (InterruptedException e) {
+                logger.debug("Play URI interrupted ({})", e.getMessage());
+                Thread.currentThread().interrupt();
             }
         }
     }
@@ -2592,7 +2595,10 @@ public class ZonePlayerHandler extends BaseThingHandler implements UpnpIOPartici
                     notificationLock.notify();
                 }
             } catch (IllegalStateException e) {
-                logger.debug("Cannot play sound ({})", e.getMessage());
+                logger.debug("Cannot play notification sound ({})", e.getMessage());
+            } catch (InterruptedException e) {
+                logger.debug("Play notification sound interrupted ({})", e.getMessage());
+                Thread.currentThread().interrupt();
             }
         }
     }
@@ -2637,9 +2643,10 @@ public class ZonePlayerHandler extends BaseThingHandler implements UpnpIOPartici
      * @param currentStreamURI - the currently loaded stream's URI
      * @param notificationURL - the notification url in the format of //host/folder/filename.mp3
      * @param coordinator - {@link ZonePlayerHandler} coordinator for the SONOS device(s)
+     * @throws InterruptedException
      */
     private void handleRadioStream(@Nullable String currentStreamURI, Command notificationURL,
-            ZonePlayerHandler coordinator) {
+            ZonePlayerHandler coordinator) throws InterruptedException {
         String nextAction = coordinator.getTransportState();
         SonosMetaData track = coordinator.getTrackMetadata();
         SonosMetaData currentUriMetaData = coordinator.getCurrentURIMetadata();
@@ -2660,9 +2667,10 @@ public class ZonePlayerHandler extends BaseThingHandler implements UpnpIOPartici
      * @param currentLineInURI - the currently loaded line-in URI
      * @param notificationURL - the notification url in the format of //host/folder/filename.mp3
      * @param coordinator - {@link ZonePlayerHandler} coordinator for the SONOS device(s)
+     * @throws InterruptedException
      */
-    private void handleLineIn(@Nullable String currentLineInURI, Command notificationURL,
-            ZonePlayerHandler coordinator) {
+    private void handleLineIn(@Nullable String currentLineInURI, Command notificationURL, ZonePlayerHandler coordinator)
+            throws InterruptedException {
         logger.debug("Handling notification while sound from line-in was being played");
         String nextAction = coordinator.getTransportState();
 
@@ -2682,9 +2690,10 @@ public class ZonePlayerHandler extends BaseThingHandler implements UpnpIOPartici
      * @param currentQueueURI - the currently loaded queue URI
      * @param notificationURL - the notification url in the format of //host/folder/filename.mp3
      * @param coordinator - {@link ZonePlayerHandler} coordinator for the SONOS device(s)
+     * @throws InterruptedException
      */
     private void handleSharedQueue(@Nullable String currentQueueURI, Command notificationURL,
-            ZonePlayerHandler coordinator) {
+            ZonePlayerHandler coordinator) throws InterruptedException {
         String nextAction = coordinator.getTransportState();
         String trackPosition = coordinator.getRefreshedPosition();
         long currentTrackNumber = coordinator.getRefreshedCurrenTrackNr();
@@ -2705,8 +2714,10 @@ public class ZonePlayerHandler extends BaseThingHandler implements UpnpIOPartici
      *
      * @param notificationURL - the notification url in the format of //host/folder/filename.mp3
      * @param coordinator - {@link ZonePlayerHandler} coordinator for the SONOS device(s)
+     * @throws InterruptedException
      */
-    private void handleNotificationSound(Command notificationURL, ZonePlayerHandler coordinator) {
+    private void handleNotificationSound(Command notificationURL, ZonePlayerHandler coordinator)
+            throws InterruptedException {
         boolean sourceStoppable = !isPlayingOpticalLineIn(coordinator.getCurrentURI());
         String originalVolume = (isAdHocGroup() || isStandalonePlayer()) ? getVolume() : coordinator.getVolume();
         if (sourceStoppable) {
@@ -2731,7 +2742,8 @@ public class ZonePlayerHandler extends BaseThingHandler implements UpnpIOPartici
         coordinator.removeRangeOfTracksFromQueue(new StringType(Long.toString(notificationPosition) + ",1"));
     }
 
-    private void restoreLastTransportState(ZonePlayerHandler coordinator, @Nullable String nextAction) {
+    private void restoreLastTransportState(ZonePlayerHandler coordinator, @Nullable String nextAction)
+            throws InterruptedException {
         if (nextAction != null) {
             switch (nextAction) {
                 case STATE_PLAYING:
@@ -2752,8 +2764,9 @@ public class ZonePlayerHandler extends BaseThingHandler implements UpnpIOPartici
      *
      * @param notificationURL - the notification url in the format of //host/folder/filename.mp3
      * @param coordinator - {@link ZonePlayerHandler} coordinator for the SONOS device(s)
+     * @throws InterruptedException
      */
-    private void handleEmptyQueue(Command notificationURL, ZonePlayerHandler coordinator) {
+    private void handleEmptyQueue(Command notificationURL, ZonePlayerHandler coordinator) throws InterruptedException {
         String originalVolume = coordinator.getVolume();
         coordinator.applyNotificationSoundVolume();
         coordinator.playURI(notificationURL);
@@ -2773,54 +2786,42 @@ public class ZonePlayerHandler extends BaseThingHandler implements UpnpIOPartici
         setNotificationSoundVolume(getNotificationSoundVolume());
     }
 
-    private void waitForFinishedNotification() {
+    private void waitForFinishedNotification() throws InterruptedException {
         waitForTransportState(STATE_PLAYING);
 
         // check Sonos state events to determine the end of the notification sound
         String notificationTitle = getCurrentTitle();
         long playstart = System.currentTimeMillis();
         while (System.currentTimeMillis() - playstart < (long) configuration.notificationTimeout * 1000) {
-            try {
-                Thread.sleep(50);
-                String currentTitle = getCurrentTitle();
-                if ((notificationTitle == null && currentTitle != null)
-                        || (notificationTitle != null && !notificationTitle.equals(currentTitle))
-                        || !STATE_PLAYING.equals(getTransportState())) {
-                    break;
-                }
-            } catch (InterruptedException e) {
-                logger.debug("InterruptedException during playing a notification sound");
+            Thread.sleep(50);
+            String currentTitle = getCurrentTitle();
+            if ((notificationTitle == null && currentTitle != null)
+                    || (notificationTitle != null && !notificationTitle.equals(currentTitle))
+                    || !STATE_PLAYING.equals(getTransportState())) {
+                break;
             }
         }
     }
 
-    private void waitForTransportState(String state) {
+    private void waitForTransportState(String state) throws InterruptedException {
         if (getTransportState() != null) {
             long start = System.currentTimeMillis();
             while (!state.equals(getTransportState())) {
-                try {
-                    Thread.sleep(50);
-                    if (System.currentTimeMillis() - start > (long) configuration.notificationTimeout * 1000) {
-                        break;
-                    }
-                } catch (InterruptedException e) {
-                    logger.debug("InterruptedException during playing a notification sound");
+                Thread.sleep(50);
+                if (System.currentTimeMillis() - start > (long) configuration.notificationTimeout * 1000) {
+                    break;
                 }
             }
         }
     }
 
-    private void waitForNotTransportState(String state) {
+    private void waitForNotTransportState(String state) throws InterruptedException {
         if (getTransportState() != null) {
             long start = System.currentTimeMillis();
             while (state.equals(getTransportState())) {
-                try {
-                    Thread.sleep(50);
-                    if (System.currentTimeMillis() - start > (long) configuration.notificationTimeout * 1000) {
-                        break;
-                    }
-                } catch (InterruptedException e) {
-                    logger.debug("InterruptedException during playing a notification sound");
+                Thread.sleep(50);
+                if (System.currentTimeMillis() - start > (long) configuration.notificationTimeout * 1000) {
+                    break;
                 }
             }
         }