-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplement media scanner in managed code.
The platform media scanner has served faithfully for many years, but it's become tedious to test and maintain, mainly due to the way it weaves obscurely between managed and native code. This modern reimplementation is bug-faithful to the legacy scanner, with tests to confirm that media is scanned identically. Future CLs will flesh out the remaining features and add additional tests to confirm behaviors around hiding/showing directories. Eventually this will also extract XMP metadata from images. Current benchmarks show legacy performance: Scan Initial: 5175ms [220, 1, 94, 94] Scan No-op: 333ms [0, 0, 0, 0] Scan Clean: 111ms [-191, 0, -93, -93] Compared with similar modern performance: Scan Initial: 5822ms [207, 1, 94, 94] Scan No-op: 331ms [0, 0, 0, 0] Scan Clean: 170ms [-191, 0, -93, -93] Bug: 120791890, 122263824, 120862852 Test: atest com.android.providers.media.MediaScannerTest Change-Id: I938b18f7c6cd5309a6b9c4eb97e635d6151b4ead
- Loading branch information
Showing
12 changed files
with
970 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
-keep class com.android.providers.media.scan.MediaScanner { | ||
*; | ||
} | ||
|
||
-keep class * implements com.android.providers.media.scan.MediaScanner { | ||
*; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/com/android/providers/media/scan/LegacyMediaScanner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Copyright (C) 2019 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.android.providers.media.scan; | ||
|
||
import android.content.Context; | ||
import android.net.Uri; | ||
import android.os.Trace; | ||
import android.provider.MediaStore; | ||
|
||
import libcore.net.MimeUtils; | ||
|
||
import java.io.File; | ||
|
||
public class LegacyMediaScanner implements MediaScanner { | ||
private final Context mContext; | ||
|
||
public LegacyMediaScanner(Context context) { | ||
mContext = context; | ||
} | ||
|
||
@Override | ||
public Context getContext() { | ||
return mContext; | ||
} | ||
|
||
@Override | ||
public void scanDirectory(File file) { | ||
final String path = file.getAbsolutePath(); | ||
final String volumeName = MediaStore.getVolumeName(file); | ||
|
||
Trace.traceBegin(Trace.TRACE_TAG_DATABASE, "scanDirectory"); | ||
try (android.media.MediaScanner scanner = | ||
new android.media.MediaScanner(mContext, volumeName)) { | ||
scanner.scanDirectories(new String[] { path }); | ||
} finally { | ||
Trace.traceEnd(Trace.TRACE_TAG_DATABASE); | ||
} | ||
} | ||
|
||
@Override | ||
public Uri scanFile(File file) { | ||
final String path = file.getAbsolutePath(); | ||
final String volumeName = MediaStore.getVolumeName(file); | ||
|
||
Trace.traceBegin(Trace.TRACE_TAG_DATABASE, "scanFile"); | ||
try (android.media.MediaScanner scanner = | ||
new android.media.MediaScanner(mContext, volumeName)) { | ||
final String ext = path.substring(path.lastIndexOf('.') + 1); | ||
return scanner.scanSingleFile(path, | ||
MimeUtils.guessMimeTypeFromExtension(ext)); | ||
} finally { | ||
Trace.traceEnd(Trace.TRACE_TAG_DATABASE); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright (C) 2019 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.android.providers.media.scan; | ||
|
||
import android.content.Context; | ||
import android.net.Uri; | ||
|
||
import java.io.File; | ||
|
||
public interface MediaScanner { | ||
public Context getContext(); | ||
public void scanDirectory(File file); | ||
public Uri scanFile(File file); | ||
|
||
public static MediaScanner instance(Context context) { | ||
return new LegacyMediaScanner(context); | ||
} | ||
} |
Oops, something went wrong.