Merge "Wait for first scan before partitioning is done."
diff --git a/services/core/java/com/android/server/MountService.java b/services/core/java/com/android/server/MountService.java
index 868d3d8..649fcde 100644
--- a/services/core/java/com/android/server/MountService.java
+++ b/services/core/java/com/android/server/MountService.java
@@ -215,7 +215,7 @@
         public static final int DISK_CREATED = 640;
         public static final int DISK_SIZE_CHANGED = 641;
         public static final int DISK_LABEL_CHANGED = 642;
-        public static final int DISK_UNSUPPORTED = 643;
+        public static final int DISK_SCANNED = 643;
         public static final int DISK_DESTROYED = 649;
 
         public static final int VOLUME_CREATED = 650;
@@ -303,6 +303,10 @@
     @GuardedBy("mLock")
     private ArrayMap<String, VolumeMetadata> mMetadata = new ArrayMap<>();
 
+    /** Map from disk ID to latches */
+    @GuardedBy("mLock")
+    private ArrayMap<String, CountDownLatch> mDiskScanLatches = new ArrayMap<>();
+
     private DiskInfo findDiskById(String id) {
         synchronized (mLock) {
             final DiskInfo disk = mDisks.get(id);
@@ -345,6 +349,17 @@
         return meta;
     }
 
+    private CountDownLatch findOrCreateDiskScanLatch(String diskId) {
+        synchronized (mLock) {
+            CountDownLatch latch = mDiskScanLatches.get(diskId);
+            if (latch == null) {
+                latch = new CountDownLatch(1);
+                mDiskScanLatches.put(diskId, latch);
+            }
+            return latch;
+        }
+    }
+
     private static int sNextMtpIndex = 1;
 
     private static int allocateMtpIndex(String volId) {
@@ -620,7 +635,7 @@
     }
 
     private void waitForLatch(CountDownLatch latch, String condition) {
-        for (;;) {
+        while (true) {
             try {
                 if (latch.await(5000, TimeUnit.MILLISECONDS)) {
                     return;
@@ -629,7 +644,7 @@
                             + " still waiting for " + condition + "...");
                 }
             } catch (InterruptedException e) {
-                Slog.w(TAG, "Interrupt while waiting for MountService to be ready.");
+                Slog.w(TAG, "Interrupt while waiting for " + condition);
             }
         }
     }
@@ -831,10 +846,12 @@
                 }
                 break;
             }
-            case VoldResponseCode.DISK_UNSUPPORTED: {
+            case VoldResponseCode.DISK_SCANNED: {
                 if (cooked.length != 2) break;
                 final DiskInfo disk = mDisks.get(cooked[1]);
-                mCallbacks.notifyDiskUnsupported(disk);
+                if (disk != null) {
+                    onDiskScannedLocked(disk);
+                }
                 break;
             }
             case VoldResponseCode.DISK_DESTROYED: {
@@ -922,6 +939,25 @@
         return true;
     }
 
+    private void onDiskScannedLocked(DiskInfo disk) {
+        final CountDownLatch latch = mDiskScanLatches.remove(disk.id);
+        if (latch != null) {
+            latch.countDown();
+        }
+
+        boolean empty = true;
+        for (int i = 0; i < mVolumes.size(); i++) {
+            final VolumeInfo vol = mVolumes.valueAt(i);
+            if (Objects.equals(disk.id, vol.getDiskId())) {
+                empty = false;
+            }
+        }
+
+        if (empty) {
+            mCallbacks.notifyDiskUnsupported(disk);
+        }
+    }
+
     private void onVolumeCreatedLocked(VolumeInfo vol) {
         final boolean primaryPhysical = SystemProperties.getBoolean(
                 StorageManager.PROP_PRIMARY_PHYSICAL, false);
@@ -1291,11 +1327,13 @@
         enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
         waitForReady();
 
+        final CountDownLatch latch = findOrCreateDiskScanLatch(diskId);
         try {
             mConnector.execute("volume", "partition", diskId, "public");
         } catch (NativeDaemonConnectorException e) {
             throw e.rethrowAsParcelableException();
         }
+        waitForLatch(latch, "partitionPublic");
     }
 
     @Override
@@ -1303,11 +1341,13 @@
         enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
         waitForReady();
 
+        final CountDownLatch latch = findOrCreateDiskScanLatch(diskId);
         try {
             mConnector.execute("volume", "partition", diskId, "private");
         } catch (NativeDaemonConnectorException e) {
             throw e.rethrowAsParcelableException();
         }
+        waitForLatch(latch, "partitionPrivate");
     }
 
     @Override
@@ -1315,11 +1355,13 @@
         enforcePermission(android.Manifest.permission.MOUNT_FORMAT_FILESYSTEMS);
         waitForReady();
 
+        final CountDownLatch latch = findOrCreateDiskScanLatch(diskId);
         try {
             mConnector.execute("volume", "partition", diskId, "mixed", ratio);
         } catch (NativeDaemonConnectorException e) {
             throw e.rethrowAsParcelableException();
         }
+        waitForLatch(latch, "partitionMixed");
     }
 
     @Override