Synchronize MediaPreparer file download/extraction

bug: 74124491
Test: run unit tests
merged-in: If42d2396062c9242d60463f06322a776b3c4db64
Change-Id: If42d2396062c9242d60463f06322a776b3c4db64
diff --git a/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/targetprep/MediaPreparer.java b/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/targetprep/MediaPreparer.java
index 1c74af5..b71882a 100644
--- a/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/targetprep/MediaPreparer.java
+++ b/common/host-side/tradefed/src/com/android/compatibility/common/tradefed/targetprep/MediaPreparer.java
@@ -324,12 +324,17 @@
             // Option 'local-media-path' has not been defined
             // Get directory to store media files on this host
             File mediaFolder = getDefaultMediaDir();
-            if(!mediaFolder.exists() || mediaFolder.list().length == 0){
-                // If directory already exists and contains files, it has been created by previous
-                // runs of MediaPreparer. Assume media files exist inside.
-                // Else, create directory if needed and download/extract media files inside.
-                mediaFolder.mkdirs();
-                downloadMediaToHost(device, buildInfo, mediaFolder);
+            synchronized (MediaPreparer.class) {
+                // Synchronize this block so that multiple shards won't download/extract
+                // this file to the same location on the host. Only an issue in Android O and above,
+                // where MediaPreparer is used for multiple, shardable modules.
+                if(!mediaFolder.exists() || mediaFolder.list().length == 0){
+                    // If directory already exists and contains files, it has been created by
+                    // previous runs of MediaPreparer. Assume media files exist inside.
+                    // Else, create directory if needed and download/extract media files inside.
+                    mediaFolder.mkdirs();
+                    downloadMediaToHost(device, buildInfo, mediaFolder);
+                }
             }
             // set mLocalMediaPath to where the CTS media files have been extracted
             updateLocalMediaPath(device, mediaFolder);