Fix MediaRandomTest watchdog affecting other tests

Bug 6971082

MediaRandomTest fails when the media server crashes, but the watchdog
kicks in later during another test and causes that test to fail.
Thus, make sure to end the watchdog after each test.

10-08 14:41:56.629  2274  2292 I TestRunner: started:
testOnInfoListener(android.media.cts.MediaRecorderTest)
10-08 14:41:59.182  2274  8725 E AndroidRuntime:
junit.framework.AssertionFailedError: Action 11 Param 581451 waited over
5000 ms
10-08 14:41:59.182  2274  8725 E AndroidRuntime:    at
junit.framework.Assert.fail(Assert.java:50)
10-08 14:41:59.182  2274  8725 E AndroidRuntime:    at
android.media.cts.MediaRandomTest$Watchdog.run(MediaRandomTest.java:141)

Change-Id: I1f956f2c0ab71ee3790b42ff2931b9119878829d
diff --git a/tests/tests/media/src/android/media/cts/MediaRandomTest.java b/tests/tests/media/src/android/media/cts/MediaRandomTest.java
index adee09d..f99c927 100644
--- a/tests/tests/media/src/android/media/cts/MediaRandomTest.java
+++ b/tests/tests/media/src/android/media/cts/MediaRandomTest.java
@@ -172,6 +172,7 @@
     }
 
     public void testPlayerRandomAction() throws Exception {
+        Watchdog watchdog = new Watchdog(5000);
         try {
             mPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                 @Override
@@ -193,7 +194,6 @@
             Log.v(TAG, "seed = " + seed);
             Random r = new Random(seed);
 
-            Watchdog watchdog = new Watchdog(5000);
             watchdog.start();
             for (int i = 0; i < NUMBER_OF_PLAYER_RANDOM_ACTIONS; i++){
                 watchdog.ping();
@@ -248,14 +248,16 @@
                 }
             }
             mPlayer.stop();
-            watchdog.end();
-            watchdog.join();
         } catch (Exception e) {
             Log.v(TAG, e.toString());
+        } finally {
+            watchdog.end();
+            watchdog.join();
         }
     }
 
     public void testRecorderRandomAction() throws Exception {
+        Watchdog watchdog = new Watchdog(5000);
         try {
             long seed = System.currentTimeMillis();
             Log.v(TAG, "seed = " + seed);
@@ -276,7 +278,6 @@
             final int[] width  = {176, 352, 320, 640, 1280, 1920};
             final int[] height = {144, 288, 240, 480,  720, 1080};
 
-            Watchdog watchdog = new Watchdog(5000);
             watchdog.start();
             for (int i = 0; i < NUMBER_OF_RECORDER_RANDOM_ACTIONS; i++) {
                 watchdog.ping();
@@ -338,10 +339,11 @@
                 } catch (Exception e) {
                 }
             }
-            watchdog.end();
-            watchdog.join();
         } catch (Exception e) {
             Log.v(TAG, e.toString());
+        } finally {
+            watchdog.end();
+            watchdog.join();
         }
     }
 }