transcoding: Use background thread for copy file.

Bug:186853525
Bug: 183751018
Test: adb shell am instrument -w -r --no-isolated-storage -e newRunListenerMode true  -e listener android.device.stressmodes.IOContentionStressTestMode -e jank-listener:jank-package-names com.android.test.uibench -e include-ui-xml true -e class 'com.android.uibench.microbenchmark.UiBenchDialogListFlingMicrobenchmark,com.android.uibench.microbenchmark.UiBenchInflatingListViewFlingMicrobenchmark' -e timeout_msec 300000 -e iterations 25 com.android.uibench.microbenchmark/androidx.test.runner.AndroidJUnitRunner

Change-Id: I2c03e1991b29acf01bf1ebc3df1733ba8b7b8285
diff --git a/libraries/device-collectors/src/main/java/android/device/stressmodes/IOContentionStressTestMode.java b/libraries/device-collectors/src/main/java/android/device/stressmodes/IOContentionStressTestMode.java
index 84e0ce8..e948713 100644
--- a/libraries/device-collectors/src/main/java/android/device/stressmodes/IOContentionStressTestMode.java
+++ b/libraries/device-collectors/src/main/java/android/device/stressmodes/IOContentionStressTestMode.java
@@ -34,18 +34,20 @@
 import java.io.File;
 import java.nio.ByteBuffer;
 import java.util.HashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
 /**
  * This stressor test read file from a mp4 file and write to a new file to simulate the IO
  * contention.
  */
 public class IOContentionStressTestMode extends InstrumentationRunListener {
-    private static final String TAG = BackgroundTranscodingStressTestMode.class.getSimpleName();
+    private static final String TAG = IOContentionStressTestMode.class.getSimpleName();
     private Context mContext;
     private static final int MAX_SAMPLE_SIZE = 256 * 1024;
-
     private ContentResolver mContentResolver;
     private boolean mStop = false;
+    private ExecutorService mExecutorService = null;
 
     public IOContentionStressTestMode() {
         mContext = InstrumentationRegistry.getInstrumentation().getContext();
@@ -53,6 +55,7 @@
                 .getUiAutomation()
                 .adoptShellPermissionIdentity("android.permission.WRITE_MEDIA_STORAGE");
         mContentResolver = mContext.getContentResolver();
+        mExecutorService = Executors.newSingleThreadExecutor();
     }
 
     public final void doCopyFile() throws Exception {
@@ -60,7 +63,7 @@
         String path = "/data/local/tmp/testHevc.mp4";
         final File file = new File(path);
 
-        Log.i(TAG, "Transcoding file " + file);
+        Log.i(TAG, "Copying file " + file);
 
         // Create a file Uri: file:///data/user/0/android.media.cts/cache/HevcTranscode.mp4
         Uri destinationUri =
@@ -142,15 +145,29 @@
 
     @Override
     public final void testStarted(Description description) throws Exception {
+        mExecutorService = Executors.newSingleThreadExecutor();
         Log.i(TAG, "IOContentionStressTestMode stress started");
-        while (!mStop) {
-            doCopyFile();
-        }
+        mExecutorService.submit(
+                new Runnable() {
+                    @Override
+                    public void run() {
+                        while (!mStop) {
+                            try {
+                                doCopyFile();
+                            } catch (Exception ex) {
+                                Log.e(TAG, "Copy file get exception: " + ex);
+                            }
+                        }
+                    }
+                });
     }
 
     @Override
     public final void testFinished(Description description) throws Exception {
         Log.i(TAG, "IOContentionStressTestMode stress finished");
         mStop = true;
+        if (mExecutorService != null) {
+            mExecutorService.shutdown();
+        }
     }
 }