Skip to content

Commit e68a693

Browse files
marconeAndroid (Google) Code Review
authored andcommitted
Merge "Refactor FileInserter in MediaScanner and adding unit tests for the newly added class."
2 parents 4715402 + 8e2ed8d commit e68a693

File tree

5 files changed

+334
-55
lines changed

5 files changed

+334
-55
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (C) 2011 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 android.media;
18+
19+
import android.content.ContentValues;
20+
import android.content.IContentProvider;
21+
import android.net.Uri;
22+
import android.os.RemoteException;
23+
24+
import java.util.ArrayList;
25+
import java.util.HashMap;
26+
import java.util.List;
27+
28+
/**
29+
* A MediaScanner helper class which enables us to do lazy insertion on the
30+
* given provider. This class manages buffers internally and flushes when they
31+
* are full. Note that you should call flushAll() after using this class.
32+
* {@hide}
33+
*/
34+
public class MediaInserter {
35+
private HashMap<Uri, List<ContentValues>> mRowMap =
36+
new HashMap<Uri, List<ContentValues>>();
37+
38+
private IContentProvider mProvider;
39+
private int mBufferSizePerUri;
40+
41+
public MediaInserter(IContentProvider provider, int bufferSizePerUri) {
42+
mProvider = provider;
43+
mBufferSizePerUri = bufferSizePerUri;
44+
}
45+
46+
public void insert(Uri tableUri, ContentValues values) throws RemoteException {
47+
List<ContentValues> list = mRowMap.get(tableUri);
48+
if (list == null) {
49+
list = new ArrayList<ContentValues>();
50+
mRowMap.put(tableUri, list);
51+
}
52+
list.add(new ContentValues(values));
53+
if (list.size() >= mBufferSizePerUri) {
54+
flush(tableUri);
55+
}
56+
}
57+
58+
public void flushAll() throws RemoteException {
59+
for (Uri tableUri : mRowMap.keySet()){
60+
flush(tableUri);
61+
}
62+
mRowMap.clear();
63+
}
64+
65+
private void flush(Uri tableUri) throws RemoteException {
66+
List<ContentValues> list = mRowMap.get(tableUri);
67+
if (!list.isEmpty()) {
68+
ContentValues[] valuesArray = new ContentValues[list.size()];
69+
valuesArray = list.toArray(valuesArray);
70+
mProvider.bulkInsert(tableUri, valuesArray);
71+
list.clear();
72+
}
73+
}
74+
}

media/java/android/media/MediaScanner.java

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -377,43 +377,7 @@ public String toString() {
377377
}
378378
}
379379

380-
private class FileInserter {
381-
382-
private final Uri mUri;
383-
private final ContentValues[] mValues;
384-
private int mIndex;
385-
386-
public FileInserter(Uri uri, int count) {
387-
mUri = uri;
388-
mValues = new ContentValues[count];
389-
}
390-
391-
public Uri insert(ContentValues values) {
392-
if (mIndex == mValues.length) {
393-
flush();
394-
}
395-
mValues[mIndex++] = values;
396-
// URI not needed when doing bulk inserts
397-
return null;
398-
}
399-
400-
public void flush() {
401-
while (mIndex < mValues.length) {
402-
mValues[mIndex++] = null;
403-
}
404-
try {
405-
mMediaProvider.bulkInsert(mUri, mValues);
406-
} catch (RemoteException e) {
407-
Log.e(TAG, "RemoteException in FileInserter.flush()", e);
408-
}
409-
mIndex = 0;
410-
}
411-
}
412-
413-
private FileInserter mAudioInserter;
414-
private FileInserter mVideoInserter;
415-
private FileInserter mImageInserter;
416-
private FileInserter mFileInserter;
380+
private MediaInserter mMediaInserter;
417381

418382
// hashes file path to FileCacheEntry.
419383
// path should be lower case if mCaseInsensitivePaths is true
@@ -880,17 +844,14 @@ private Uri endFile(FileCacheEntry entry, boolean ringtones, boolean notificatio
880844
}
881845

882846
Uri tableUri = mFilesUri;
883-
FileInserter inserter = mFileInserter;
847+
MediaInserter inserter = mMediaInserter;
884848
if (!mNoMedia) {
885849
if (MediaFile.isVideoFileType(mFileType)) {
886850
tableUri = mVideoUri;
887-
inserter = mVideoInserter;
888851
} else if (MediaFile.isImageFileType(mFileType)) {
889852
tableUri = mImagesUri;
890-
inserter = mImageInserter;
891853
} else if (MediaFile.isAudioFileType(mFileType)) {
892854
tableUri = mAudioUri;
893-
inserter = mAudioInserter;
894855
}
895856
}
896857
Uri result = null;
@@ -913,7 +874,7 @@ private Uri endFile(FileCacheEntry entry, boolean ringtones, boolean notificatio
913874
if (inserter == null || entry.mFormat == MtpConstants.FORMAT_ASSOCIATION) {
914875
result = mMediaProvider.insert(tableUri, values);
915876
} else {
916-
result = inserter.insert(values);
877+
inserter.insert(tableUri, values);
917878
}
918879

919880
if (result != null) {
@@ -1212,11 +1173,8 @@ public void scanDirectories(String[] directories, String volumeName) {
12121173
long prescan = System.currentTimeMillis();
12131174

12141175
if (ENABLE_BULK_INSERTS) {
1215-
// create FileInserters for bulk inserts
1216-
mAudioInserter = new FileInserter(mAudioUri, 500);
1217-
mVideoInserter = new FileInserter(mVideoUri, 500);
1218-
mImageInserter = new FileInserter(mImagesUri, 500);
1219-
mFileInserter = new FileInserter(mFilesUri, 500);
1176+
// create MediaInserter for bulk inserts
1177+
mMediaInserter = new MediaInserter(mMediaProvider, 500);
12201178
}
12211179

12221180
for (int i = 0; i < directories.length; i++) {
@@ -1225,14 +1183,8 @@ public void scanDirectories(String[] directories, String volumeName) {
12251183

12261184
if (ENABLE_BULK_INSERTS) {
12271185
// flush remaining inserts
1228-
mAudioInserter.flush();
1229-
mVideoInserter.flush();
1230-
mImageInserter.flush();
1231-
mFileInserter.flush();
1232-
mAudioInserter = null;
1233-
mVideoInserter = null;
1234-
mImageInserter = null;
1235-
mFileInserter = null;
1186+
mMediaInserter.flushAll();
1187+
mMediaInserter = null;
12361188
}
12371189

12381190
long scan = System.currentTimeMillis();

media/tests/MediaFrameworkTest/Android.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ LOCAL_SRC_FILES := $(call all-subdir-java-files)
77

88
LOCAL_JAVA_LIBRARIES := android.test.runner
99

10+
LOCAL_STATIC_JAVA_LIBRARIES := easymocklib
11+
1012
LOCAL_PACKAGE_NAME := mediaframeworktest
1113

1214
include $(BUILD_PACKAGE)

media/tests/MediaFrameworkTest/src/com/android/mediaframeworktest/MediaFrameworkUnitTestRunner.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public TestSuite getAllTests() {
4747
addMediaMetadataRetrieverStateUnitTests(suite);
4848
addMediaRecorderStateUnitTests(suite);
4949
addMediaPlayerStateUnitTests(suite);
50+
addMediaScannerUnitTests(suite);
5051
return suite;
5152
}
5253

@@ -89,4 +90,8 @@ private void addMediaPlayerStateUnitTests(TestSuite suite) {
8990
suite.addTestSuite(MediaPlayerSetVolumeStateUnitTest.class);
9091
suite.addTestSuite(MediaPlayerMetadataParserTest.class);
9192
}
93+
94+
private void addMediaScannerUnitTests(TestSuite suite) {
95+
suite.addTestSuite(MediaInserterTest.class);
96+
}
9297
}

0 commit comments

Comments
 (0)