Merge "Create default dirs before running FuseDaemonHostTest" into rvc-dev
diff --git a/src/com/android/providers/media/MediaProvider.java b/src/com/android/providers/media/MediaProvider.java
index 2102a91..9259e94 100644
--- a/src/com/android/providers/media/MediaProvider.java
+++ b/src/com/android/providers/media/MediaProvider.java
@@ -288,7 +288,7 @@
     @GuardedBy("sCacheLock")
     private static final Map<String, Collection<File>> sCachedVolumeScanPaths = new ArrayMap<>();
 
-    private void updateVolumes() {
+    public void updateVolumes() {
         synchronized (sCacheLock) {
             sCachedExternalVolumeNames.clear();
             sCachedExternalVolumeNames.addAll(MediaStore.getExternalVolumeNames(getContext()));
@@ -442,40 +442,6 @@
 
     private static final String CANONICAL = "canonical";
 
-    private BroadcastReceiver mMediaReceiver = new BroadcastReceiver() {
-        @Override
-        public void onReceive(Context context, Intent intent) {
-            final StorageVolume sv = intent.getParcelableExtra(StorageVolume.EXTRA_STORAGE_VOLUME);
-            try {
-                final String volumeName;
-                if (sv.isPrimary()) {
-                    volumeName = MediaStore.VOLUME_EXTERNAL_PRIMARY;
-                } else {
-                    try {
-                        volumeName = MediaStore
-                                .checkArgumentVolumeName(sv.getMediaStoreVolumeName());
-                    } catch (IllegalArgumentException ignored) {
-                        return;
-                    }
-                }
-
-                switch (intent.getAction()) {
-                    case Intent.ACTION_MEDIA_MOUNTED:
-                        attachVolume(volumeName);
-                        break;
-                    case Intent.ACTION_MEDIA_UNMOUNTED:
-                    case Intent.ACTION_MEDIA_EJECT:
-                    case Intent.ACTION_MEDIA_REMOVED:
-                    case Intent.ACTION_MEDIA_BAD_REMOVAL:
-                        detachVolume(volumeName);
-                        break;
-                }
-            } catch (Exception e) {
-                Log.w(TAG, "Failed to handle broadcast " + intent, e);
-            }
-        }
-    };
-
     private BroadcastReceiver mPackageReceiver = new BroadcastReceiver() {
         @Override
         public void onReceive(Context context, Intent intent) {
@@ -826,32 +792,13 @@
                 false, false, mLegacyProvider, Column.class,
                 Metrics::logSchemaChange, mFilesListener, mMigrationListener);
 
-        final IntentFilter filter = new IntentFilter();
-        filter.setPriority(10);
-        filter.addDataScheme("file");
-        filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
-        filter.addAction(Intent.ACTION_MEDIA_MOUNTED);
-        filter.addAction(Intent.ACTION_MEDIA_EJECT);
-        filter.addAction(Intent.ACTION_MEDIA_REMOVED);
-        filter.addAction(Intent.ACTION_MEDIA_BAD_REMOVAL);
-        context.registerReceiver(mMediaReceiver, filter);
-
         final IntentFilter packageFilter = new IntentFilter();
         packageFilter.setPriority(10);
-        filter.addDataScheme("package");
+        packageFilter.addDataScheme("package");
         packageFilter.addAction(Intent.ACTION_PACKAGE_ADDED);
         packageFilter.addAction(Intent.ACTION_PACKAGE_REMOVED);
         context.registerReceiver(mPackageReceiver, packageFilter);
 
-        // Watch for invalidation of cached volumes
-        mStorageManager.registerStorageVolumeCallback(context.getMainExecutor(),
-                new StorageVolumeCallback() {
-                    @Override
-                    public void onStateChanged(@NonNull StorageVolume volume) {
-                        updateVolumes();
-                    }
-                });
-
         updateVolumes();
         attachVolume(MediaStore.VOLUME_INTERNAL);
         for (String volumeName : getExternalVolumeNames()) {
@@ -5248,7 +5195,7 @@
     private void invalidateFuseDentry(@NonNull String path) {
         FuseDaemon daemon = getFuseDaemonForFile(new File(path));
         if (daemon != null) {
-            if (FuseDaemon.native_is_fuse_thread()) {
+            if (isFuseThread()) {
                 // If we are on a FUSE thread, we don't need to invalidate,
                 // (and *must* not, otherwise we'd crash) because the invalidation
                 // is already reflected in the lower filesystem
@@ -6174,6 +6121,10 @@
         return false;
     }
 
+    private static boolean isFuseThread() {
+        return FuseDaemon.native_is_fuse_thread();
+    }
+
     @Deprecated
     private boolean checkCallingPermissionAudio(boolean forWrite, String callingPackage) {
         if (forWrite) {
@@ -6448,10 +6399,6 @@
         final String volumeName = resolveVolumeName(uri);
         synchronized (mAttachedVolumeNames) {
             if (!mAttachedVolumeNames.contains(volumeName)) {
-                // Maybe we are racing onVolumeStateChanged, update our cache and try again
-                updateVolumes();
-            }
-            if (!mAttachedVolumeNames.contains(volumeName)) {
                 throw new VolumeNotFoundException(volumeName);
             }
         }
diff --git a/src/com/android/providers/media/MediaService.java b/src/com/android/providers/media/MediaService.java
index 4c31c9f..385c9ab 100644
--- a/src/com/android/providers/media/MediaService.java
+++ b/src/com/android/providers/media/MediaService.java
@@ -63,10 +63,6 @@
                     onPackageOrphaned(packageName);
                     break;
                 }
-                case Intent.ACTION_MEDIA_MOUNTED: {
-                    onScanVolume(this, intent.getData(), REASON_MOUNTED);
-                    break;
-                }
                 case Intent.ACTION_MEDIA_SCANNER_SCAN_FILE: {
                     onScanFile(this, intent.getData());
                     break;
diff --git a/src/com/android/providers/media/fuse/ExternalStorageServiceImpl.java b/src/com/android/providers/media/fuse/ExternalStorageServiceImpl.java
index 34c828c..8cdcba7 100644
--- a/src/com/android/providers/media/fuse/ExternalStorageServiceImpl.java
+++ b/src/com/android/providers/media/fuse/ExternalStorageServiceImpl.java
@@ -96,6 +96,8 @@
                 Log.i(TAG, "Ignoring volume state for vol:" + volumeName
                         + ". State: " + vol.getState());
         }
+        // Check for invalidation of cached volumes
+        mediaProvider.updateVolumes();
     }
 
     @Override
diff --git a/tests/Android.bp b/tests/Android.bp
index 3d956ef..ba71320 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -28,6 +28,10 @@
         "unsupportedappusage",
     ],
 
+    jni_libs: [
+        "libfuse_jni",
+    ],
+
     static_libs: [
         "androidx.appcompat_appcompat",
         "androidx.core_core",
diff --git a/tests/src/com/android/providers/media/scan/MediaScannerTest.java b/tests/src/com/android/providers/media/scan/MediaScannerTest.java
index 44d5cfb..bc64a62 100644
--- a/tests/src/com/android/providers/media/scan/MediaScannerTest.java
+++ b/tests/src/com/android/providers/media/scan/MediaScannerTest.java
@@ -65,6 +65,10 @@
     private static final String TAG = "MediaScannerTest";
 
     public static class IsolatedContext extends ContextWrapper {
+        static {
+            System.loadLibrary("fuse_jni");
+        }
+
         private final File mDir;
         private final MockContentResolver mResolver;
         private final MediaProvider mProvider;