这里主要是在系统启动系统媒体扫描服务的时候将我们需要内置的文件拷贝到指定位置。
1. Frameworks中在FileUtil中添加一个复制文件的方法:
diff --git a/core/java/android/os/FileUtils.java b/core/java/android/os/FileUtils.java
index 0a724a1..b30735a 100644
--- a/core/java/android/os/FileUtils.java
+++ b/core/java/android/os/FileUtils.java
@@ -204,6 +204,29 @@ public class FileUtils {
}
}
+ /** @hide */
+ public static boolean copyFolder(File srcFile, File destFile) {
+ if (!srcFile.isDirectory()) {
+ return false;
+ }
+ if (!destFile.exists() && !destFile.mkdirs()) {
+ return false;
+ }
+ boolean result = true;
+ File[] list = srcFile.listFiles();
+ if (list == null) {
+ return result;
+ }
+ for (File f:list) {
+ if (f.isDirectory()) {
+ result &= copyFolder (f,new File(destFile,f.getName()));
+ } else {
+ result &= copyFile(f,new File(destFile,f.getName()));
+ }
+ }
+ return result;
+ }
+
/**
* Check if a filename is "safe" (no metacharacters or spaces).
* @param file The file to check
2. 在系统媒体扫描服务(MediaScannerService.java)中添加复制的文件的动作:
diff --git a/src/com/android/providers/media/MediaScannerService.java b/src/com/android/providers/media/MediaScannerService.java
index 7391fac..dd723df 100644
--- a/src/com/android/providers/media/MediaScannerService.java
+++ b/src/com/android/providers/media/MediaScannerService.java
@@ -50,6 +50,8 @@ import java.util.concurrent.atomic.AtomicInteger;
public class MediaScannerService extends Service implements Runnable
{
private static final String TAG = "MediaScannerService";
+ private static String PRELOAD_SRC = "/system/preloadmedia";
+ private static String PRELOAD_FLAG = "mediapreloaded";
private int mIndex = 0;
private int mRecentId;
@@ -322,6 +324,14 @@ public class MediaScannerService extends Service implements Runnable
+ if (!getSharedPreferences(PRELOAD_FLAG, Context.MODE_PRIVATE)
+ .getBoolean(PRELOAD_FLAG, false)) {
+ if (doPreloadMedia(PRELOAD_SRC,
+ Environment.getInternalStoragePath().getPath())) {
+ getSharedPreferences(PRELOAD_FLAG, Context.MODE_PRIVATE).edit()
+ .putBoolean(PRELOAD_FLAG, true).commit();
+ }
+ }
String path = arguments.getString("path");
if (path != null) {
directories = new String[] {arguments.getString("path")};
@@ -347,5 +357,13 @@ public class MediaScannerService extends Service implements Runnable
mMainLooper.post(mStopSelfTask);
}
};
+
+ private boolean doPreloadMedia(String src, String dst) {
+ if (!android.os.FileUtils.copyFolder(new File(src), new File(dst))) {
+ Log.e(TAG, "doPreloadMedia failed");
+ return false;
+ }
+ return true;
+ }
}
3.接下来就是编写个简单的mk文件,将我们需要内置的文件放到第2步骤地目标 (system/preloadmedia/),还有就是要确保这个mk一定要被系统检索到。mk大致内容如下:
LOCAL_PATH:= vendor/sprd/sprd_media
PRODUCT_COPY_FILES += \
$(LOCAL_PATH)/media/1.mp3:system/preloadmedia/xxx.mp3