Android设备扫描机制详解

Android设备扫描机制详解

本文基于Android pie,对Android的设备扫描机制做一个全面的解析,由于本人掌握的知识有限,如有讲错的地方还请大家指出来。
Android提供了一套扫描机制,用以扫描设备内置存储(Internal storage)和外置存储(包括SDCard 和外接U盘等),并将扫描得到的数据存储在数据库中,以供其他应用使用(比如音乐播放器,视频播放器等)。

Android 设备扫描机制结构图

整体结构图

MediaScannerReceiver

代码路径:

packages/providers/MediaProvider/src/com/android/providers/media/MediaScannerReceiver.java

该类用以监听"Intent.ACTION_BOOT_COMPLETED","Intent.ACTION_MEDIA_MOUNTED"和"Intent.ACTION_MEDIA_SCANNER_SCAN_FILE"广播事件,然后启动MediaScannerService处理对应的扫描任务。"BOOT_COMPLETED"是开机启动完毕系统发出的广播,在收到该广播后,会触发扫描内置存储。"MEDIA_MOUNTED"广播是在外置设备挂载成功后,由StorageManagerService发出的,包括SDCard和U盘等,在收到该广播后,会触发扫描外置存储。"MEDIA_SCANNER_SCAN_FILE"广播用以扫描单个文件,需要将该文件的路径通过bundle传递过来。

MediaScannerService

代码路径:

packages/providers/MediaProvider/src/com/android/providers/media/MediaScannerService.java

该类在收到MediaScannerReceiver传递过来的扫描任务后,根据传递过来的参数判断扫描任务的类型,然后
再调用MediaScanner的不同接口开始扫描任务。需要特别注意的是,MediaScannerService会在扫描任务完成后自动销毁,所以该服务并不会一直存在。

...
            String filePath = arguments.getString("filepath");
            
            try {
                if (filePath != null) {
                    IBinder binder = arguments.getIBinder("listener");
                    IMediaScannerListener listener = 
                            (binder == null ? null : IMediaScannerListener.Stub.asInterface(binder));
                    Uri uri = null;
                    try {
                        uri = scanFile(filePath, arguments.getString("mimetype")); **//类型1:扫描单个文件**
                    } catch (Exception e) {
                        Log.e(TAG, "Exception scanning file", e);
                    }
                    if (listener != null) {
                        listener.scanCompleted(filePath, uri);
                    }
                } else {
                    String volume = arguments.getString("volume");
                    String[] directories = null;

                    if (MediaProvider.INTERNAL_VOLUME.equals(volume)) {
                        // scan internal media storage **//类型2:扫描内置存储,并将存储路径赋值给directories**
                        directories = new String[] {
                                Environment.getRootDirectory() + "/media",
                                Environment.getOemDirectory() + "/media",
                        };
                    }
                    else if (MediaProvider.EXTERNAL_VOLUME.equals(volume)) {
                        // scan external storage volumes **//类型3:扫描外置存储,并将存储路径赋值给directories**
                        if (getSystemService(UserManager.class).isDemoUser()) {
                            directories = ArrayUtils.appendElement(String.class,
                                    mExternalStoragePaths,
                                    Environment.getDataPreloadsMediaDirectory().getAbsolutePath());
                        } else {
                            directories = mExternalStoragePaths;
                        }
                    }

                    if (directories != null) {
                        if (false) Log.d(TAG, "start scanning volume " + volume + ": "
                                + Arrays.toString(directories));
                        scan(directories, volume);
                        if (false) Log.d(TAG, "done scanning volume " + volume);
                    }
                }
            } catch (Exception e) {
                Log.e(TAG, "Exception in handleMessage", e);
            }

            stopSelf(msg.arg1); **//PS: 扫描任务完成后,该service会被自动销毁**
...

MediaScanner

该类暴露了两个重要接口供外部调用,scanDirectories(String[] directories)用以扫描目录,scanSingleFile(String path, String mimeType)用以扫描一个具体的文件。

MediaProvider

代码路径:

packages/providers/MediaProvider/src/com/android/providers/media/MediaProvider.java

如前面提到的,设备在扫描后得到的数据会被存储在数据库中,该类便是用于创建并管理该数据库的。该类对外暴露了操作数据库的常用接口:增删查改。
数据库路径:

/data/data/com.android.providers.media/databases/

media_jni

代码路径:

frameworks/base/media/jni/android_media_MediaScanner.cpp

media_jni工作在JNI层,用于java层和native层的通信。

StagefrightMediaScanner

代码路径:

frameworks/av/media/libstagefright/StagefrightMediaScanner.cpp

StagefrightMediaScanner工作在Native层,该类继承自MediaScanner, 其主要作用是遍历从java层传下来的目录,在遍历的过程中会跳过需要忽略的目录。

...
 MediaScanResult MediaScanner::processDirectory(
        const char *path, MediaScannerClient &client) {
    int pathLength = strlen(path);
    ...
    MediaScanResult result = doProcessDirectory(pathBuffer, pathRemaining, client, false);

    free(pathBuffer);

    return result;
}

MediaScanResult MediaScanner::doProcessDirectory(
        char *path, int pathRemaining, MediaScannerClient &client, bool noMedia) {
    // place to copy file or directory name
    char* fileSpot = path + strlen(path);
    struct dirent* entry;

    if (shouldSkipDirectory(path)) {
        ALOGD("Skipping: %s", path); **// 跳过需要忽略的目录**
        return MEDIA_SCAN_RESULT_OK;
    }
    ...
    MediaScanResult result = MEDIA_SCAN_RESULT_OK;
    while ((entry = readdir(dir))) { **//遍历整个目录**
        if (doProcessDirectoryEntry(path, pathRemaining, client, noMedia, entry, fileSpot)
                == MEDIA_SCAN_RESULT_ERROR) {
            result = MEDIA_SCAN_RESULT_ERROR;
            break;
        }
    }
    closedir(dir);
    return result;
}
...

MediaMetadataRetriever

MediaMetadataRetriever的作用是解析媒体文件的属性,比如一个音频文件的作者,专辑,流派等属性信息。

目录扫描流程

扫描单个文件其实也是扫描目录的一部分,其流程包含在扫描目录的流程中,所以接下来将以scanDirectories接口为入口,介绍整个扫描流程。
进入scanDirectories后,首先会调用prescan方法,该方法的目的是检查数据库中对应的数据是否存在,如果不存在则会将其从数据库中清除。这里对应的意思是由prescan方法的第一个参数决定的,如果传递的是null,则会将数据库中不存在的数据全编清除,如果传递的是一个路径,则只会将该路径下不存在的数据清除,具体的说明请查看我的另一篇博客如何禁止MediaScanner自动清除MediaProvider数据库中已经拔掉设备的数据。然后调用natvice接口processDirectory。

...
    public void scanDirectories(String[] directories) {
        try {
            long start = System.currentTimeMillis();
            prescan(null, true); **//默认传递null**
            long prescan = System.currentTimeMillis();

            if (ENABLE_BULK_INSERTS) {
                // create MediaInserter for bulk inserts 
                mMediaInserter = new MediaInserter(mMediaProvider, 500); **//创建一个MediaInserter,用以批量插入数据到数据库**
            }

            for (int i = 0; i < directories.length; i++) {
                processDirectory(directories[i], mClient);  **//访问native接口**
            }
           ...
    }
...

进入processDirectory后,如前面提到的,将会依次遍历整个目录,并将遍历到的文件夹/文件信息通过JNI层传下来的client带回到java层。

...
MediaScanResult MediaScanner::doProcessDirectoryEntry(
        char *path, int pathRemaining, MediaScannerClient &client, bool noMedia,
        struct dirent* entry, char* fileSpot) {
    ...
    if (type == DT_DIR) {  **//遍历得到一个文件夹**
        bool childNoMedia = noMedia;
        // set noMedia flag on directories with a name that starts with '.'
        // for example, the Mac ".Trashes" directory
        if (name[0] == '.')
            childNoMedia = true;

        // report the directory to the client
        if (stat(path, &statbuf) == 0) {
            status_t status = client.scanFile(path, statbuf.st_mtime, 0,
                    true /*isDirectory*/, childNoMedia); **//访问JNI的client**
            if (status) {
                return MEDIA_SCAN_RESULT_ERROR;
            }
        }

        // and now process its contents
        strcat(fileSpot, "/");
        MediaScanResult result = doProcessDirectory(path, pathRemaining - nameLength - 1,
                client, childNoMedia); **//如果是一个文件夹,会继续递归遍历下去**
        if (result == MEDIA_SCAN_RESULT_ERROR) {
            return MEDIA_SCAN_RESULT_ERROR;
        }
    } else if (type == DT_REG) { **//遍历得到一个文件**
        stat(path, &statbuf);
        status_t status = client.scanFile(path, statbuf.st_mtime, statbuf.st_size,
                false /*isDirectory*/, noMedia); **//访问JNI的client**
        if (status) {
            return MEDIA_SCAN_RESULT_ERROR;
        }
    }

    return MEDIA_SCAN_RESULT_OK;
}
...

回到java层,继续调用doScanFile,接着继续调用beginFile,可以看到该方法会返回一个FileEntry对象,在beginFile方法里面,会去查一次数据库,检查当前文件的数据在数据库中是否已经存在,如果存在,再通过比较"lastModified"属性判断该文件是否已经被修改。如果该文件是一个新的文件(数据库中不存在),或者该文件已经被修改(与数据库中数据有更新),则需要重新解析该文件。

        @Override
        public void scanFile(String path, long lastModified, long fileSize,
                boolean isDirectory, boolean noMedia) {
            // This is the callback funtion from native codes.
            // Log.v(TAG, "scanFile: "+path);
            doScanFile(path, null, lastModified, fileSize, isDirectory, false, noMedia);
        }

        public Uri doScanFile(String path, String mimeType, long lastModified,
                long fileSize, boolean isDirectory, boolean scanAlways, boolean noMedia) {
            Uri result = null;
//            long t1 = System.currentTimeMillis();
            try {
                FileEntry entry = beginFile(path, mimeType, lastModified,
                        fileSize, isDirectory, noMedia); **//调用beginFile创建FileEntry对象**

                if (entry == null) {
                    return null;
                }
                ...
                // rescan for metadata if file was modified since last scan **//如果该文件是新文件,或者已被修改,则重新解析**
                if (entry != null && (entry.mLastModifiedChanged || scanAlways)) {
                    if (noMedia) {
                        result = endFile(entry, false, false, false, false, false); 
                    } else {
                        ...
                        if (isaudio || isvideo || isimage) {
                            path = Environment.maybeTranslateEmulatedPathToInternal(new File(path))
                                    .getAbsolutePath();
                        }

                        // we only extract metadata for audio and video files
                        if (isaudio || isvideo) {
                            processFile(path, mimeType, this); **//如果是音频或者视频文件,则继续调用native接口processFile解析metadata信息**
                        }

                        if (isimage) {
                            processImageFile(path); **//如果是图片文件,则调用processImageFile方法,并利用BitmapFactory解析该文件**
                        }

                        result = endFile(entry, ringtones, notifications, alarms, music, podcasts);
                    }
                }
            } catch (RemoteException e) {
                Log.e(TAG, "RemoteException in MediaScanner.scanFile()", e);
            }
            ...
            return result;
        }

图片文件的解析流程比较简单,这里就不详细描述,重点描述视频和音频文件的解析过程。调用natvice接口processFile,又回到native层。接着继续调用processFileInternal,在processFileInternal中创建一个MediaMetadataRetriever实例,利用该对象解析文件的metadata信息,并将解析得到的信息通过JNI的client传回到java层。

MediaScanResult StagefrightMediaScanner::processFile(
        const char *path, const char *mimeType,
        MediaScannerClient &client) {
  ...
  MediaScanResult result = processFileInternal(path, mimeType, client);
  ...
  return result;
}

MediaScanResult StagefrightMediaScanner::processFileInternal(
        const char *path, const char * /* mimeType */,
        MediaScannerClient &client) {
  ...
  sp<MediaMetadataRetriever> mRetriever(new MediaMetadataRetriever); //创建MediaMetadataRetriever实例
  ...
  static const KeyMap kKeyMap[] = { **//Metadata常见属性**
      {"tracknumber", METADATA_KEY_CD_TRACK_NUMBER},
      {"discnumber", METADATA_KEY_DISC_NUMBER},
      {"album", METADATA_KEY_ALBUM},
      {"artist", METADATA_KEY_ARTIST},
      {"albumartist", METADATA_KEY_ALBUMARTIST},
      {"composer", METADATA_KEY_COMPOSER},
      {"genre", METADATA_KEY_GENRE},
      {"title", METADATA_KEY_TITLE},
      {"year", METADATA_KEY_YEAR},
      {"duration", METADATA_KEY_DURATION},
      {"writer", METADATA_KEY_WRITER},
      {"compilation", METADATA_KEY_COMPILATION},
      {"isdrm", METADATA_KEY_IS_DRM},
      {"date", METADATA_KEY_DATE},
      {"width", METADATA_KEY_VIDEO_WIDTH},
      {"height", METADATA_KEY_VIDEO_HEIGHT},
  };
  static const size_t kNumEntries = sizeof(kKeyMap) / sizeof(kKeyMap[0]);

  for (size_t i = 0; i < kNumEntries; ++i) { **//循环解析文件的metadata属性**
    const char *value;
    if ((value = mRetriever->extractMetadata(kKeyMap[i].key)) != NULL) {
      status = client.addStringTag(kKeyMap[i].tag, value); //通过调用JNI的client对象将解析到的属性传回java层
      if (status != OK) {
        return MEDIA_SCAN_RESULT_ERROR;
      }
    }
  }

  return MEDIA_SCAN_RESULT_OK;
}

通过调用MediaScanner的handleStringTag接口,将解析到的metadata信息全部传回到java层。

        public void handleStringTag(String name, String value) {
            if (name.equalsIgnoreCase("title") || name.startsWith("title;")) {
                // Don't trim() here, to preserve the special \001 character
                // used to force sorting. The media provider will trim() before
                // inserting the title in to the database.
                mTitle = value;
            } else if (name.equalsIgnoreCase("artist") || name.startsWith("artist;")) {
                mArtist = value.trim();
            } else if (name.equalsIgnoreCase("albumartist") || name.startsWith("albumartist;")
                    || name.equalsIgnoreCase("band") || name.startsWith("band;")) {
                mAlbumArtist = value.trim();
            } else if (name.equalsIgnoreCase("album") || name.startsWith("album;")) {
                mAlbum = value.trim();
            } else if (name.equalsIgnoreCase("composer") || name.startsWith("composer;")) {
                mComposer = value.trim();
            } else if (mProcessGenres &&
                    (name.equalsIgnoreCase("genre") || name.startsWith("genre;"))) {
                mGenre = getGenreName(value);
            } else if (name.equalsIgnoreCase("year") || name.startsWith("year;")) {
                mYear = parseSubstring(value, 0, 0);
            } else if (name.equalsIgnoreCase("tracknumber") || name.startsWith("tracknumber;")) {
                // track number might be of the form "2/12"
                // we just read the number before the slash
                int num = parseSubstring(value, 0, 0);
                mTrack = (mTrack / 1000) * 1000 + num;
            } else if (name.equalsIgnoreCase("discnumber") ||
                    name.equals("set") || name.startsWith("set;")) {
                // set number might be of the form "1/3"
                // we just read the number before the slash
                int num = parseSubstring(value, 0, 0);
                mTrack = (num * 1000) + (mTrack % 1000);
            } else if (name.equalsIgnoreCase("duration")) {
                mDuration = parseSubstring(value, 0, 0);
            } else if (name.equalsIgnoreCase("writer") || name.startsWith("writer;")) {
                mWriter = value.trim();
            } else if (name.equalsIgnoreCase("compilation")) {
                mCompilation = parseSubstring(value, 0, 0);
            } else if (name.equalsIgnoreCase("isdrm")) {
                mIsDrm = (parseSubstring(value, 0, 0) == 1);
            } else if (name.equalsIgnoreCase("date")) {
                mDate = parseDate(value);
            } else if (name.equalsIgnoreCase("width")) {
                mWidth = parseSubstring(value, 0, 0);
            } else if (name.equalsIgnoreCase("height")) {
                mHeight = parseSubstring(value, 0, 0);
            } else {
                //Log.v(TAG, "unknown tag: " + name + " (" + mProcessGenres + ")");
            }
        }

解析完毕后,调用endFile,在该方法中就会将该文件的所有信息保存到数据库中。

...
        private Uri endFile(FileEntry entry, boolean ringtones, boolean notifications,
                boolean alarms, boolean music, boolean podcasts)
                throws RemoteException {
            ...
            ContentValues values = toValues(); **//将文件信息填充到ContentValues**
            ...
            if (rowId == 0) { **//新文件,插入数据到数据库**
               ...
                if (inserter == null || needToSetSettings) {
                    if (inserter != null) {
                        inserter.flushAll();
                    }
                    result = mMediaProvider.insert(tableUri, values); 
                } else if (entry.mFormat == MtpConstants.FORMAT_ASSOCIATION) {
                    inserter.insertwithPriority(tableUri, values);
                } else {
                    inserter.insert(tableUri, values);
                }

                if (result != null) {
                    rowId = ContentUris.parseId(result);
                    entry.mRowId = rowId;
                }
            } else { //文件有更新,将数据更新到数据库
                ...
                mMediaProvider.update(result, values, null, null);
            }
...

时序图:
时序图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值