Fix Android/media detection bug
When cross-user is enabled, FileUtils#isExternalMediaDirectory() returns
true only for "/storage/emulated/0/AppClone/Android/media/". However it
should return true for "/storage/emulated/0/Android/media/" as well.
Bug: 211849800
Test: atest MediaProviderTests
Change-Id: I29ed9e306a3431d6a55aa24c4304dcae666a2742
(cherry picked from commit a6b1870857ffc8aece4e431f33e517c2f8585003)
diff --git a/src/com/android/providers/media/util/FileUtils.java b/src/com/android/providers/media/util/FileUtils.java
index 8e7e75f..835efbc 100644
--- a/src/com/android/providers/media/util/FileUtils.java
+++ b/src/com/android/providers/media/util/FileUtils.java
@@ -1182,11 +1182,17 @@
@VisibleForTesting
static boolean isExternalMediaDirectory(@NonNull String path, String crossUserRoot) {
final String relativePath = extractRelativePath(path);
- if (relativePath != null) {
- final String externalMediaDir = (crossUserRoot == null || crossUserRoot.isEmpty())
- ? "Android/media" : crossUserRoot + "/Android/media";
- return StringUtils.startsWithIgnoreCase(relativePath, externalMediaDir);
+ if (relativePath == null) {
+ return false;
}
+
+ if (StringUtils.startsWithIgnoreCase(relativePath, "Android/media")) {
+ return true;
+ }
+ if (!TextUtils.isEmpty(crossUserRoot)) {
+ return StringUtils.startsWithIgnoreCase(relativePath, crossUserRoot + "/Android/media");
+ }
+
return false;
}
diff --git a/tests/src/com/android/providers/media/util/FileUtilsTest.java b/tests/src/com/android/providers/media/util/FileUtilsTest.java
index f241be3..96a9d1b 100644
--- a/tests/src/com/android/providers/media/util/FileUtilsTest.java
+++ b/tests/src/com/android/providers/media/util/FileUtilsTest.java
@@ -1034,12 +1034,22 @@
@Test
public void testIsExternalMediaDirectory() throws Exception {
for (String prefix : new String[] {
- "/storage/emulated/0/AppClone/",
- "/storage/0000-0000/AppClone/"
+ "/storage/emulated/0/",
+ "/storage/0000-0000/",
}) {
+ assertTrue(isExternalMediaDirectory(prefix + "Android/media/foo.jpg", null));
+ assertTrue(isExternalMediaDirectory(prefix + "Android/media/foo.jpg", ""));
+ assertTrue(isExternalMediaDirectory(prefix + "Android/mEdia/foo.jpg", ""));
+ assertFalse(isExternalMediaDirectory(prefix + "Android/data/foo.jpg", ""));
assertTrue(isExternalMediaDirectory(prefix + "Android/media/foo.jpg", "AppClone"));
assertTrue(isExternalMediaDirectory(prefix + "android/mEdia/foo.jpg", "AppClone"));
- assertFalse(isExternalMediaDirectory(prefix + "Android/media/foo.jpg", "NotAppClone"));
+ assertTrue(isExternalMediaDirectory(prefix + "AppClone/Android/media/foo.jpg", "AppClone"));
+ assertTrue(isExternalMediaDirectory(prefix + "AppClone/Android/mEdia/foo.jpg", "AppClone"));
+ assertTrue(isExternalMediaDirectory(prefix + "Appclone/Android/mEdia/foo.jpg", "AppClone"));
+ assertFalse(isExternalMediaDirectory(prefix + "AppClone/Android/media/foo.jpg", null));
+ assertFalse(isExternalMediaDirectory(prefix + "AppClone/Android/mEdia/foo.jpg", null));
+ assertFalse(isExternalMediaDirectory(prefix + "AppClone/Android/media/foo.jpg", ""));
+ assertFalse(isExternalMediaDirectory(prefix + "AppClone/Android/media/foo.jpg", "NotAppClone"));
}
}