media: move timeout timer to the worker thread to unblock onCreate.

plus some other minor tweaks on test.

Bug: 22528409

Change-Id: I4ef10078ea446aab13e8e543ad29efc07d5c2ea0
diff --git a/tests/tests/media/src/android/media/cts/ResourceManagerStubActivity.java b/tests/tests/media/src/android/media/cts/ResourceManagerStubActivity.java
index f77b245..5595800 100644
--- a/tests/tests/media/src/android/media/cts/ResourceManagerStubActivity.java
+++ b/tests/tests/media/src/android/media/cts/ResourceManagerStubActivity.java
@@ -100,6 +100,7 @@
         };
         thread.start();
         thread.join(20000 /* millis */);
+        System.gc();
         Thread.sleep(5000);  // give the gc a chance to release test activities.
 
         boolean result = true;
diff --git a/tests/tests/media/src/android/media/cts/ResourceManagerTestActivity1.java b/tests/tests/media/src/android/media/cts/ResourceManagerTestActivity1.java
index 938324c..a11d773 100644
--- a/tests/tests/media/src/android/media/cts/ResourceManagerTestActivity1.java
+++ b/tests/tests/media/src/android/media/cts/ResourceManagerTestActivity1.java
@@ -31,26 +31,16 @@
         super.onCreate(savedInstanceState);
         moveTaskToBack(true);
 
-        if (allocateCodecs(MAX_INSTANCES) == MAX_INSTANCES) {
-            // haven't reached the limit with MAX_INSTANCES, report RESULT_OK directly and
-            // skip additional test.
-            finishWithResult(RESULT_OK);
-        }
-        useCodecs();
-
-        boolean waitForReclaim = true;
         Bundle extras = getIntent().getExtras();
         if (extras != null) {
-            waitForReclaim = extras.getBoolean("wait-for-reclaim", waitForReclaim);
+            mWaitForReclaim = extras.getBoolean("wait-for-reclaim", mWaitForReclaim);
         }
 
-        try {
-            Thread.sleep(15000);  // timeout to ensure the activity is finished.
-        } catch (InterruptedException e) {
+        if (allocateCodecs(MAX_INSTANCES) == MAX_INSTANCES) {
+            // haven't reached the limit with MAX_INSTANCES, no need to wait for reclaim exception.
+            mWaitForReclaim = false;
         }
-        stopUsingCodecs();
-        // if the test is supposed to wait for reclaim event then this is a failure, otherwise
-        // this is a pass.
-        finishWithResult(waitForReclaim ? RESULT_CANCELED : RESULT_OK);
+
+        useCodecs();
     }
 }
diff --git a/tests/tests/media/src/android/media/cts/ResourceManagerTestActivityBase.java b/tests/tests/media/src/android/media/cts/ResourceManagerTestActivityBase.java
index 38d1a92..ace8f82 100644
--- a/tests/tests/media/src/android/media/cts/ResourceManagerTestActivityBase.java
+++ b/tests/tests/media/src/android/media/cts/ResourceManagerTestActivityBase.java
@@ -64,7 +64,8 @@
 
     private MediaCodec.Callback mCallback = new TestCodecCallback();
 
-    private static MediaFormat getTestFormat(VideoCapabilities vcaps, boolean securePlayback) {
+    private MediaFormat getTestFormat(CodecCapabilities caps, boolean securePlayback) {
+        VideoCapabilities vcaps = caps.getVideoCapabilities();
         int maxWidth = vcaps.getSupportedWidths().getUpper();
         int maxHeight = vcaps.getSupportedHeightsFor(maxWidth).getUpper();
         int maxBitrate = vcaps.getBitrateRange().getUpper();
@@ -72,8 +73,7 @@
                 .getUpper().intValue();
 
         MediaFormat format = MediaFormat.createVideoFormat(MIME, maxWidth, maxHeight);
-        format.setInteger(MediaFormat.KEY_COLOR_FORMAT,
-                CodecCapabilities.COLOR_FormatYUV420Flexible);
+        format.setInteger(MediaFormat.KEY_COLOR_FORMAT, caps.colorFormats[0]);
         format.setInteger(MediaFormat.KEY_BIT_RATE, maxBitrate);
         format.setInteger(MediaFormat.KEY_FRAME_RATE, maxFramerate);
         format.setInteger(MediaFormat.KEY_I_FRAME_INTERVAL, IFRAME_INTERVAL);
@@ -154,8 +154,7 @@
     protected void allocateCodecs(int max, MediaCodecInfo info, boolean securePlayback) {
         String name = info.getName();
         CodecCapabilities caps = info.getCapabilitiesForType(MIME);
-        VideoCapabilities vcaps = caps.getVideoCapabilities();
-        MediaFormat format = getTestFormat(vcaps, securePlayback);
+        MediaFormat format = getTestFormat(caps, securePlayback);
         MediaCodec codec = null;
         for (int i = mCodecs.size(); i < max; ++i) {
             try {
@@ -214,6 +213,7 @@
         }
     }
 
+    protected boolean mWaitForReclaim = true;
     private Thread mWorkerThread;
     private volatile boolean mUseCodecs = true;
     private volatile boolean mGotReclaimedException = false;
@@ -221,28 +221,29 @@
         mWorkerThread = new Thread(new Runnable() {
             @Override
             public void run() {
-                while (mUseCodecs) {
+                long start = System.currentTimeMillis();
+                long timeSinceStartedMs = 0;
+                while (mUseCodecs && (timeSinceStartedMs < 15000)) {  // timeout in 15s
                     doUseCodecs();
                     try {
                         Thread.sleep(50 /* millis */);
                     } catch (InterruptedException e) {}
+                    timeSinceStartedMs = System.currentTimeMillis() - start;
                 }
                 if (mGotReclaimedException) {
+                    Log.d(TAG, "Got expected reclaim exception.");
                     finishWithResult(RESULT_OK);
+                } else {
+                    Log.d(TAG, "Stopped without getting reclaim exception.");
+                    // if the test is supposed to wait for reclaim event then this is a failure,
+                    // otherwise this is a pass.
+                    finishWithResult(mWaitForReclaim ? RESULT_CANCELED : RESULT_OK);
                 }
             }
         });
         mWorkerThread.start();
     }
 
-    protected void stopUsingCodecs() {
-        mUseCodecs = false;
-        try {
-            mWorkerThread.join(1000);
-        } catch (InterruptedException e) {
-        }
-    }
-
     @Override
     protected void onDestroy() {
         Log.d(TAG, "onDestroy called.");