1 /*
2  * Copyright (C) 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.providers.media.scan;
18 
19 import static org.junit.Assert.assertNotNull;
20 import static org.junit.Assert.fail;
21 
22 import android.provider.MediaStore;
23 
24 import androidx.test.InstrumentationRegistry;
25 import androidx.test.runner.AndroidJUnit4;
26 
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 
30 import java.io.File;
31 
32 @RunWith(AndroidJUnit4.class)
33 public class LegacyMediaScannerTest {
34     @Test
testSimple()35     public void testSimple() throws Exception {
36         final LegacyMediaScanner scanner = new LegacyMediaScanner(
37                 InstrumentationRegistry.getTargetContext());
38         assertNotNull(scanner.getContext());
39 
40         try {
41             scanner.scanDirectory(new File("/dev/null"), MediaScanner.REASON_UNKNOWN);
42             fail();
43         } catch (UnsupportedOperationException expected) {
44         }
45         try {
46             scanner.scanFile(new File("/dev/null"), MediaScanner.REASON_UNKNOWN);
47             fail();
48         } catch (UnsupportedOperationException expected) {
49         }
50         try {
51             scanner.scanFile(new File("/dev/null"), MediaScanner.REASON_UNKNOWN,
52                     InstrumentationRegistry.getContext().getPackageName());
53             fail();
54         } catch (UnsupportedOperationException expected) {
55         }
56         try {
57             scanner.onDetachVolume(MediaStore.VOLUME_EXTERNAL_PRIMARY);
58             fail();
59         } catch (UnsupportedOperationException expected) {
60         }
61     }
62 
63     /**
64       * This implementation was copied verbatim from the legacy
65       * {@code frameworks/base/media/java/android/media/MediaScanner.java}.
66       */
isNonMediaFile(String path)67     static boolean isNonMediaFile(String path) {
68         // special case certain file names
69         // I use regionMatches() instead of substring() below
70         // to avoid memory allocation
71         final int lastSlash = path.lastIndexOf('/');
72         if (lastSlash >= 0 && lastSlash + 2 < path.length()) {
73             // ignore those ._* files created by MacOS
74             if (path.regionMatches(lastSlash + 1, "._", 0, 2)) {
75                 return true;
76             }
77 
78             // ignore album art files created by Windows Media Player:
79             // Folder.jpg, AlbumArtSmall.jpg, AlbumArt_{...}_Large.jpg
80             // and AlbumArt_{...}_Small.jpg
81             if (path.regionMatches(true, path.length() - 4, ".jpg", 0, 4)) {
82                 if (path.regionMatches(true, lastSlash + 1, "AlbumArt_{", 0, 10) ||
83                         path.regionMatches(true, lastSlash + 1, "AlbumArt.", 0, 9)) {
84                     return true;
85                 }
86                 int length = path.length() - lastSlash - 1;
87                 if ((length == 17 && path.regionMatches(
88                         true, lastSlash + 1, "AlbumArtSmall", 0, 13)) ||
89                         (length == 10
90                          && path.regionMatches(true, lastSlash + 1, "Folder", 0, 6))) {
91                     return true;
92                 }
93             }
94         }
95         return false;
96     }
97 }
98