Remove db rows after unlinking the file from ROOT_UID

File path operations from ROOT_UID will not insert/update/delete
database rows. Hence, in this CL, after unlinking the file, we scan the
file to remove db row of the deleted file.

Bug: 159565500
Test: atest CtsScopedStorageHostTest
Tets: atest CtsScopedStoragePublicVolumeHostTest
Change-Id: Ibb7f2ecf253ac6625211f78b0a125f59a71198eb
Merged-In: Ibb7f2ecf253ac6625211f78b0a125f59a71198eb
(cherry picked from commit 037cc38f9addfaea300edb14f0e466da17b833d2)
diff --git a/hostsidetests/scopedstorage/legacy/src/android/scopedstorage/cts/legacy/LegacyStorageTest.java b/hostsidetests/scopedstorage/legacy/src/android/scopedstorage/cts/legacy/LegacyStorageTest.java
index 2422f3b..4596cab 100644
--- a/hostsidetests/scopedstorage/legacy/src/android/scopedstorage/cts/legacy/LegacyStorageTest.java
+++ b/hostsidetests/scopedstorage/legacy/src/android/scopedstorage/cts/legacy/LegacyStorageTest.java
@@ -133,6 +133,7 @@
     @After
     public void teardown() throws Exception {
         executeShellCommand("rm " + getShellFile());
+        MediaStore.scanFile(getContentResolver(), getShellFile());
     }
 
     /**
@@ -221,6 +222,7 @@
 
         try {
             executeShellCommand("touch " + existingFile);
+            MediaStore.scanFile(getContentResolver(), existingFile);
             Os.open(existingFile.getPath(), OsConstants.O_RDONLY, /*mode*/ 0);
             fail("Opening file for read expected to fail: " + existingFile);
         } catch (ErrnoException expected) {
@@ -273,6 +275,7 @@
         FileDescriptor fd = null;
         try {
             executeShellCommand("touch " + existingFile);
+            MediaStore.scanFile(getContentResolver(), existingFile);
             fd = Os.open(existingFile.getPath(), OsConstants.O_RDONLY, /*mode*/ 0);
         } finally {
             if (fd != null) {
@@ -314,6 +317,7 @@
         final File shellFile = getShellFile();
 
         executeShellCommand("touch " + getShellFile());
+        MediaStore.scanFile(getContentResolver(), getShellFile());
         // can list a non-media file created by other package.
         assertThat(Arrays.asList(shellFile.getParentFile().list()))
                 .contains(shellFile.getName());
@@ -381,6 +385,7 @@
                         "LegacyFileAccessTest2");
         try {
             executeShellCommand("touch " + shellFile1);
+            MediaStore.scanFile(getContentResolver(), shellFile1);
             // app can't rename shell file.
             assertCantRenameFile(shellFile1, shellFile2);
             // app can't move shell file to its media directory.
@@ -415,6 +420,7 @@
                         "LegacyFileAccessTest2");
         try {
             executeShellCommand("touch " + shellFile1);
+            MediaStore.scanFile(getContentResolver(), shellFile1);
             // app can't rename shell file.
             assertCantRenameFile(shellFile1, shellFile2);
             // app can't move shell file to its media directory.
diff --git a/hostsidetests/scopedstorage/src/android/scopedstorage/cts/ScopedStorageTest.java b/hostsidetests/scopedstorage/src/android/scopedstorage/cts/ScopedStorageTest.java
index 81b55b8..e8bd0c5 100644
--- a/hostsidetests/scopedstorage/src/android/scopedstorage/cts/ScopedStorageTest.java
+++ b/hostsidetests/scopedstorage/src/android/scopedstorage/cts/ScopedStorageTest.java
@@ -778,6 +778,8 @@
         } finally {
             executeShellCommand("rm " + pdfFile.getAbsolutePath());
             executeShellCommand("rm " + videoFile.getAbsolutePath());
+            MediaStore.scanFile(getContentResolver(), pdfFile);
+            MediaStore.scanFile(getContentResolver(), videoFile);
             uninstallAppNoThrow(TEST_APP_A);
         }
     }
@@ -1384,6 +1386,7 @@
             videoFile.delete();
             topLevelVideoFile.delete();
             executeShellCommand("rm  " + musicFile.getAbsolutePath());
+            MediaStore.scanFile(getContentResolver(), musicFile);
             denyAppOpsToUid(Process.myUid(), SYSTEM_GALERY_APPOPS);
         }
     }
@@ -2094,11 +2097,13 @@
             // Use shell to create root file because TEST_APP_A is in
             // scoped storage.
             executeShellCommand("touch " + shellPdfAtRoot.getAbsolutePath());
+            MediaStore.scanFile(getContentResolver(), shellPdfAtRoot);
             assertFileAccess_existsOnly(shellPdfAtRoot);
         } finally {
             deleteFileAsNoThrow(TEST_APP_A, otherAppPdf.getAbsolutePath());
             deleteFileAsNoThrow(TEST_APP_A, otherAppImage.getAbsolutePath());
             executeShellCommand("rm " + shellPdfAtRoot.getAbsolutePath());
+            MediaStore.scanFile(getContentResolver(), shellPdfAtRoot);
             myAppPdf.delete();
             uninstallApp(TEST_APP_A);
         }
@@ -2222,6 +2227,7 @@
             installApp(TEST_APP_A);
             assertCreateFilesAs(TEST_APP_A, otherAppImg, otherAppMusic, otherAppPdf);
             executeShellCommand("touch " + otherTopLevelFile);
+            MediaStore.scanFile(getContentResolver(), otherTopLevelFile);
 
             allowAppOpsToUid(Process.myUid(), OPSTR_MANAGE_EXTERNAL_STORAGE);
 
@@ -2237,6 +2243,7 @@
         } finally {
             denyAppOpsToUid(Process.myUid(), OPSTR_MANAGE_EXTERNAL_STORAGE);
             executeShellCommand("rm " + otherTopLevelFile);
+            MediaStore.scanFile(getContentResolver(), otherTopLevelFile);
             deleteFilesAs(TEST_APP_A, otherAppImg, otherAppMusic, otherAppPdf);
             uninstallApp(TEST_APP_A);
         }
@@ -2341,6 +2348,7 @@
             assertThat(dirInDcim.exists() || dirInDcim.mkdir()).isTrue();
 
             executeShellCommand("touch " + otherAppPdfFile1);
+            MediaStore.scanFile(getContentResolver(), otherAppPdfFile1);
 
             installAppWithStoragePermissions(TEST_APP_A);
             allowAppOpsToUid(Process.myUid(), SYSTEM_GALERY_APPOPS);
@@ -2359,6 +2367,8 @@
         } finally {
             executeShellCommand("rm " + otherAppPdfFile1);
             executeShellCommand("rm " + otherAppPdfFile2);
+            MediaStore.scanFile(getContentResolver(), otherAppPdfFile1);
+            MediaStore.scanFile(getContentResolver(), otherAppPdfFile2);
             otherAppImageFile1.delete();
             otherAppImageFile2.delete();
             otherAppVideoFile1.delete();