Fix up Looper preparation.

bug: 21473342

Change-Id: I1086938f9fa8c7de482eeb1391a75c668e8b6fe1
diff --git a/tests/tests/media/src/android/media/cts/EnumDevicesTest.java b/tests/tests/media/src/android/media/cts/EnumDevicesTest.java
index 53dd5e6..73a6314 100644
--- a/tests/tests/media/src/android/media/cts/EnumDevicesTest.java
+++ b/tests/tests/media/src/android/media/cts/EnumDevicesTest.java
@@ -37,11 +37,6 @@
     boolean mAddCallbackCalled = false;
     boolean mRemoveCallbackCalled = false;
 
-    static {
-        // We're going to use a Handler
-        Looper.prepare();
-    }
-
     @Override
     protected void setUp() throws Exception {
         super.setUp();
@@ -152,6 +147,23 @@
         }
     }
 
+    /*
+     * tests if the Looper for the current thread has been prepared,
+     * If not, it makes one, prepares it and returns it.
+     * If this returns non-null, the caller is reponsible for calling quit()
+     * on the returned Looper.
+     */
+    private Looper prepareIfNeededLooper() {
+        // non-null Handler
+        Looper myLooper = null;
+        if (Looper.myLooper() == null) {
+            Looper.prepare();
+            myLooper = Looper.myLooper();
+            assertNotNull(myLooper);
+        }
+        return myLooper;
+    }
+    
     public void test_deviceCallback() {
         // null callback?
         mAudioManager.registerAudioDeviceCallback(null,null);
@@ -170,7 +182,8 @@
         // remove twice
         mAudioManager.unregisterAudioDeviceCallback(callback);
 
-        // non-null Handler
+        Looper myLooper = prepareIfNeededLooper();
+        
         mAudioManager.registerAudioDeviceCallback(callback, new Handler());
         // unregister null callback
         mAudioManager.unregisterAudioDeviceCallback(null);
@@ -180,6 +193,10 @@
         mAudioManager.unregisterAudioDeviceCallback(callback);
         // remove twice
         mAudioManager.unregisterAudioDeviceCallback(callback);
+
+        if (myLooper != null) {
+            myLooper.quit();
+        }
     }
 
     //TODO - Need tests for device connect/disconnect callbacks
diff --git a/tests/tests/media/src/android/media/cts/RoutingTest.java b/tests/tests/media/src/android/media/cts/RoutingTest.java
index 76bb68d..d641734 100644
--- a/tests/tests/media/src/android/media/cts/RoutingTest.java
+++ b/tests/tests/media/src/android/media/cts/RoutingTest.java
@@ -41,11 +41,6 @@
 
     private AudioManager mAudioManager;
 
-    static {
-        // We're going to use a Handler
-        Looper.prepare();
-    }
-
     @Override
     protected void setUp() throws Exception {
         super.setUp();
@@ -101,6 +96,23 @@
         audioTrack.release();
     }
 
+    /*
+     * tests if the Looper for the current thread has been prepared,
+     * If not, it makes one, prepares it and returns it.
+     * If this returns non-null, the caller is reponsible for calling quit()
+     * on the returned Looper.
+     */
+    private Looper prepareIfNeededLooper() {
+        // non-null Handler
+        Looper myLooper = null;
+        if (Looper.myLooper() == null) {
+            Looper.prepare();
+            myLooper = Looper.myLooper();
+            assertNotNull(myLooper);
+        }
+        return myLooper;
+    }
+
     private class AudioTrackRoutingListener implements AudioTrack.OnRoutingChangedListener {
         public void onRoutingChanged(AudioTrack audioTrack) {}
     }
@@ -120,11 +132,15 @@
 
         audioTrack.removeOnRoutingChangedListener(listener);
 
+        Looper myLooper = prepareIfNeededLooper();
         audioTrack.addOnRoutingChangedListener(listener, new Handler());
 
         audioTrack.removeOnRoutingChangedListener(listener);
 
         audioTrack.release();
+        if (myLooper != null) {
+            myLooper.quit();
+        }
    }
 
     private AudioRecord allocAudioRecord() {
@@ -151,7 +167,6 @@
             // Can't do it so skip this test
             return;
         }
-
         AudioRecord audioRecord = allocAudioRecord();
 
         audioRecord.addOnRoutingChangedListener(null, null);
@@ -166,11 +181,15 @@
 
         audioRecord.removeOnRoutingChangedListener(listener);
 
+        Looper myLooper = prepareIfNeededLooper();
         audioRecord.addOnRoutingChangedListener(listener, new Handler());
 
         audioRecord.removeOnRoutingChangedListener(listener);
 
         audioRecord.release();
+        if (myLooper != null) {
+            myLooper.quit();
+        }
     }
 
     public void test_audioRecord_preferredDevice() {