在MediaScanner.java文件的MediaScanner类前面有如下对扫描过程的说明。原文和翻译如下:
1.原文
Internal service helper that no-one should use directly.
The way the scan currently works is: - The Java MediaScannerService creates a MediaScanner (this class), and calls MediaScanner.scanDirectories on it. - scanDirectories() calls the native processDirectory() for each of the specified directories. - the processDirectory() JNI method wraps the provided mediascanner client in a native ‘MyMediaScannerClient‘ class, then calls processDirectory() on the native MediaScanner object (which got created when the Java MediaScanner was created). - native MediaScanner.processDirectory() (currently part of opencore) calls doProcessDirectory(), which recurses over the folder, and calls native MyMediaScannerClient.scanFile() for every file whose extension matches. - native MyMediaScannerClient.scanFile() calls back on Java MediaScannerClient.scanFile, which calls doScanFile, which after some setup calls back down to native code, calling MediaScanner.processFile(). - MediaScanner.processFile() calls one of several methods, depending on the type of the file: parseMP3, parseMP4, parseMidi, parseOgg or parseWMA. - each of these methods gets metadata key/value pairs from the file, and repeatedly calls native MyMediaScannerClient.handleStringTag, which calls back up to its Java counterparts in this file. - Java handleStringTag() gathers the key/value pairs that it‘s interested in. - once processFile returns and we‘re back in Java code in doScanFile(), it calls Java MyMediaScannerClient.endFile(), which takes all the data that‘s been gathered and inserts an entry in to the database.
In summary: Java MediaScannerService calls Java MediaScanner scanDirectories, which calls Java MediaScanner processDirectory (native method), which calls native MediaScanner processDirectory, which calls native MyMediaScannerClient scanFile, which calls Java MyMediaScannerClient scanFile, which calls Java MediaScannerClient doScanFile, which calls Java MediaScanner processFile (native method), which calls native MediaScanner processFile, which calls native parseMP3, parseMP4, parseMidi, parseOgg or parseWMA, which calls native MyMediaScanner handleStringTag, which calls Java MyMediaScanner handleStringTag. Once MediaScanner processFile returns, an entry is inserted in to the database.
2.译文
不能直接使用内部服务助手。
目前扫描的工作方法是: - Java类MediaScannerService创建了MediaScanner对象,并且调用此对象的MediaScanner.scanDirectories方法。
- scanDirectories()方法为每一个指定的目录调用native方法processDirectory()。
- processDirectory() JNI方法通过native类MyMediaScannerClient,提供了一个mediascanner 客户端,
然后通过此native MediaScanner对象调用processDirectory()方法(当创建Java对象MediaScanner时创建的)。
- native方法MediaScanner.processDirectory() (目前是opencore的一部分)调用doProcessDirectory()方法递归处理文件夹,
并且为每一个扩展名匹配的文件调用native方法MyMediaScannerClient.scanFile()。
- Java方法MediaScannerClient.scanFile回调native方法MyMediaScannerClient.scanFile()(MyMediaScannerClient类实现了MediaScannerClient接口), MyMediaScannerClient.scanFile()方法又调用了doScanFile方法,doScanFile方法做了一些设置后又调用native方法MediaScanner.processFile()。
- MediaScanner.processFile()依据文件类型调用以下方法中的一个: parseMP3, parseMP4, parseMidi, parseOgg或者parseWMA。
-这些方法从文件中取得metadata键/值对,并且循环调用native方法MyMediaScannerClient.handleStringTag,由文件中的java counterparts决定回调。
- Java方法handleStringTag()收集感兴趣的键/值对。 - 在doScanFile()方法中,一旦processFile方法返回Java代码,它调用Java方法MyMediaScannerClient.endFile(),并且把所有收集到的数据插入到数据库实体中。
总结:
Java类MediaScannerService调用 Java类MediaScanner的scanDirectories方法,它又调用了 Java类MediaScanner的processDirectory 方法(native方法),它又调用了 native类MediaScanner的processDirectory方法,它又调用了 native类MyMediaScannerClient的scanFile方法,它又调用了 (此处不 容易理解,从native调用java层的方法) Java类MyMediaScannerClient的scanFile方法,它又调用了 Java接口MediaScannerClient的doScanFile方法(实际是调用的java类MyMediaScannerClient的doScanFile方法,java接口MediaScannerClient未定义doScanFile方法),它又调用了 Java类MediaScanner的processFile方法(native方法),它又调用了 native类MediaScanner的processFile方法,它又调用了 native方法parseMP3, parseMP4, parseMidi, parseOgg或者parseWMA,它又调用了 native类MyMediaScanner的handleStringTag方法,它又调用了 Java类MyMediaScanner的handleStringTag方法。 一旦从MediaScanner的processFile方法返回,将检索到的一条媒体条目信息插入到数据库中。
Java类MyMediaScannerClient的定义在MediaScanner.java文件中,实现了MediaScannerClient接口。
Native类MyMediaScannerClient的定义在android_media_MediaScanner.cpp文件中,继承了MediaScannerClient类。
调用时序图如下:引自http://www.cnblogs.com/halzhang/archive/2011/03/10/1980319.html
|