07 Gallery 源码-MediaSource 数据源

1. 图库数据源-基类 MediaSource

package com.android.gallery3d.data;

public abstract class MediaSource {
    private String mPrefix;
}

2. 图库数据源-子类

ytw012@rom:~/Android_Build_CS/android$ grep -irn "extends MediaSource" packages/apps/Gallery/
packages/apps/Gallery/src_pd/com/android/gallery3d/picasasource/PicasaSource.java:34:public class PicasaSource extends MediaSource {
packages/apps/Gallery/src/com/android/gallery3d/data/ComboSource.java:21:class ComboSource extends MediaSource {
packages/apps/Gallery/src/com/android/gallery3d/data/SnailSource.java:20:public class SnailSource extends MediaSource {
packages/apps/Gallery/src/com/android/gallery3d/data/FilterSource.java:21:public class FilterSource extends MediaSource {
packages/apps/Gallery/src/com/android/gallery3d/data/ClusterSource.java:21:class ClusterSource extends MediaSource {
packages/apps/Gallery/src/com/android/gallery3d/data/LocalSource.java:33:class LocalSource extends MediaSource {
packages/apps/Gallery/src/com/android/gallery3d/data/SecureSource.java:21:public class SecureSource extends MediaSource {
packages/apps/Gallery/src/com/android/gallery3d/data/UriSource.java:31:class UriSource extends MediaSource {

图库中支持的数据源有:

  1. LocalSource(本地数据)
  2. PicasaSource(Gmail同步)
  3. ComboSource(混合类型数据)
  4. FilterSource(过滤后的数据)
  5. SecureSource(安全数据)
  6. UriSource(Uri数据)

3. 加载图库数据源

  • getDataManager().getTopSetPath(DataManager.INCLUDE_ALL)
    从单例模式的命名规则看,实例化的文件位于 DataManager.java
package com.android.gallery3d.app;

// DataManager manages all media sets and media items in the system.
//
// Each MediaSet and MediaItem has a unique 64 bits id. The most significant
// 32 bits represents its parent, and the least significant 32 bits represents
// the self id. For MediaSet the self id is is globally unique, but for
// MediaItem it's unique only relative to its parent.
//
// To make sure the id is the same when the MediaSet is re-created, a child key
// is provided to obtainSetId() to make sure the same self id will be used as
// when the parent and key are the same. A sequence of child keys is called a
// path. And it's used to identify a specific media set even if the process is
// killed and re-created, so child keys should be stable identifiers.

public class DataManager implements StitchingChangeListener {

    public static DataManager from(Context context) {
        GalleryApp app = (GalleryApp) context.getApplicationContext();
        return app.getDataManager();
    }

}

上述查看 GalleryApp 为一个接口

package com.android.gallery3d.app;

public interface GalleryApp {
    public DataManager getDataManager();
    ...
}

查看接口实现位置,即为单例模式实例化位置

package com.android.gallery3d.app;

public class GalleryAppImpl extends Application implements GalleryApp {

    @Override
    public synchronized DataManager getDataManager() {
        if (mDataManager == null) {
            mDataManager = new DataManager(this);
            mDataManager.initializeSourceMap();
        }
        return mDataManager;
    }
}

3.1 查看DataManager实例化
package com.android.gallery3d.data;

public class DataManager implements StitchingChangeListener {

    public DataManager(GalleryApp application) {
        mApplication = application;
        mDefaultMainHandler = new Handler(application.getMainLooper());
    }
3.2 查看 initializeSourceMap
package com.android.gallery3d.data;

public class DataManager implements StitchingChangeListener {

    public synchronized void initializeSourceMap() {
        if (!mSourceMap.isEmpty()) return;

        // the order matters, the UriSource must come last
        // 所有数据源
        addSource(new LocalSource(mApplication)); // 本地数据
        addSource(new PicasaSource(mApplication)); // Gmail同步
        addSource(new ComboSource(mApplication)); // 混合类型数据
        addSource(new ClusterSource(mApplication));
        addSource(new FilterSource(mApplication)); // 过滤后的数据
        addSource(new SecureSource(mApplication)); // 安全数据
        addSource(new UriSource(mApplication)); // Uri数据
        addSource(new SnailSource(mApplication));

        if (mActiveCount > 0) {
            for (MediaSource source : mSourceMap.values()) {
                source.resume();
            }
        }
    }
    
3.3 根据路径path的前缀匹配相应的数据及加载数据
    private HashMap<String, MediaSource> mSourceMap =
            new LinkedHashMap<String, MediaSource>();

    void addSource(MediaSource source) {
        if (source == null) return;
        mSourceMap.put(source.getPrefix(), source);
    }

例如

package com.android.gallery3d.data;

public abstract class MediaSource {

    protected MediaSource(String prefix) {
        mPrefix = prefix;
    }

}

package com.android.gallery3d.data;

class LocalSource extends MediaSource {

    public LocalSource(GalleryApp context) {
        super("local");
    }
}

public class PicasaSource extends MediaSource {

    public PicasaSource(GalleryApp application) {
        super("picasa");
        ...
    }
}


class ComboSource extends MediaSource {

    public ComboSource(GalleryApp application) {
        super("combo");
        ...
    }

    ...
3.4 每类数据源有包含有多个数据集

数据集的基类

package com.android.gallery3d.data;

// MediaSet is a directory-like data structure.
// It contains MediaItems and sub-MediaSets.
//
// The primary interface are:
// getMediaItemCount(), getMediaItem() and
// getSubMediaSetCount(), getSubMediaSet().
//
// getTotalMediaItemCount() returns the number of all MediaItems, including
// those in sub-MediaSets.
public abstract class MediaSet extends MediaObject {

    
}

根据继承关系的搜索结果

ytw012@rom:~/Android_Build_CS/android$ grep -irn "extends MediaSet" packages/apps/Gallery/
packages/apps/Gallery/src_pd/com/android/gallery3d/picasasource/PicasaSource.java:57:    private static class EmptyAlbumSet extends MediaSet {
packages/apps/Gallery/src/com/android/gallery3d/data/FilterDeleteSet.java:30:public class FilterDeleteSet extends MediaSet implements ContentListener {
packages/apps/Gallery/src/com/android/gallery3d/data/ClusterAlbumSet.java:28:public class ClusterAlbumSet extends MediaSet implements ContentListener {
packages/apps/Gallery/src/com/android/gallery3d/data/ClusterAlbum.java:22:public class ClusterAlbum extends MediaSet implements ContentListener {
packages/apps/Gallery/src/com/android/gallery3d/data/LocalMergeAlbum.java:36:public class LocalMergeAlbum extends MediaSet implements ContentListener {
packages/apps/Gallery/src/com/android/gallery3d/data/FilterEmptyPromptSet.java:21:public class FilterEmptyPromptSet extends MediaSet implements ContentListener {
packages/apps/Gallery/src/com/android/gallery3d/data/FilterTypeSet.java:22:public class FilterTypeSet extends MediaSet implements ContentListener {
packages/apps/Gallery/src/com/android/gallery3d/data/ComboAlbum.java:26:public class ComboAlbum extends MediaSet implements ContentListener {
packages/apps/Gallery/src/com/android/gallery3d/data/SingleItemAlbum.java:21:public class SingleItemAlbum extends MediaSet {
packages/apps/Gallery/src/com/android/gallery3d/data/LocalAlbum.java:42:public class LocalAlbum extends MediaSet {
packages/apps/Gallery/src/com/android/gallery3d/data/LocalAlbumSet.java:38:public class LocalAlbumSet extends MediaSet
packages/apps/Gallery/src/com/android/gallery3d/data/ComboAlbumSet.java:26:public class ComboAlbumSet extends MediaSet implements ContentListener {
packages/apps/Gallery/src/com/android/gallery3d/data/SecureAlbum.java:33:public class SecureAlbum extends MediaSet implements StitchingChangeListener {

例如 LocalSource 数据源含有如下数据集

package com.android.gallery3d.data;

class LocalSource extends MediaSource {

    public LocalSource(GalleryApp context) {
        super("local");
        mApplication = context;
        mMatcher = new PathMatcher();
        mMatcher.add("/local/image", LOCAL_IMAGE_ALBUMSET);
        mMatcher.add("/local/video", LOCAL_VIDEO_ALBUMSET);
        mMatcher.add("/local/all", LOCAL_ALL_ALBUMSET);

        mMatcher.add("/local/image/*", LOCAL_IMAGE_ALBUM);
        mMatcher.add("/local/video/*", LOCAL_VIDEO_ALBUM);
        mMatcher.add("/local/all/*", LOCAL_ALL_ALBUM);
        mMatcher.add("/local/image/item/*", LOCAL_IMAGE_ITEM);
        mMatcher.add("/local/video/item/*", LOCAL_VIDEO_ITEM);

        mUriMatcher.addURI(MediaStore.AUTHORITY,
                "external/images/media/#", LOCAL_IMAGE_ITEM);
        mUriMatcher.addURI(MediaStore.AUTHORITY,
                "external/video/media/#", LOCAL_VIDEO_ITEM);
        mUriMatcher.addURI(MediaStore.AUTHORITY,
                "external/images/media", LOCAL_IMAGE_ALBUM);
        mUriMatcher.addURI(MediaStore.AUTHORITY,
                "external/video/media", LOCAL_VIDEO_ALBUM);
        mUriMatcher.addURI(MediaStore.AUTHORITY,
                "external/file", LOCAL_ALL_ALBUM);
    }

    public MediaObject createMediaObject(Path path) {
        GalleryApp app = mApplication;
        switch (mMatcher.match(path)) {
            case LOCAL_ALL_ALBUMSET:
            case LOCAL_IMAGE_ALBUMSET:
            case LOCAL_VIDEO_ALBUMSET:
                return new LocalAlbumSet(path, mApplication);
            case LOCAL_IMAGE_ALBUM:
                return new LocalAlbum(path, app, mMatcher.getIntVar(0), true);
            case LOCAL_VIDEO_ALBUM:
                return new LocalAlbum(path, app, mMatcher.getIntVar(0), false);
            case LOCAL_ALL_ALBUM: {
                int bucketId = mMatcher.getIntVar(0);
                DataManager dataManager = app.getDataManager();
                MediaSet imageSet = (MediaSet) dataManager.getMediaObject(
                        LocalAlbumSet.PATH_IMAGE.getChild(bucketId));
                MediaSet videoSet = (MediaSet) dataManager.getMediaObject(
                        LocalAlbumSet.PATH_VIDEO.getChild(bucketId));
                Comparator<MediaItem> comp = DataManager.sDateTakenComparator;

                //TW_FWK_GALLERY dqzeng 2019/1/15
                // 增加传参mediaType = MEDIA_TYPE_ALL
                // 增加传参mApplication
                return new LocalMergeAlbum(
                        path,mApplication, comp, new MediaSet[] {imageSet, videoSet}, bucketId, false);
            }
            case LOCAL_IMAGE_ITEM:
                return new LocalImage(path, mApplication, mMatcher.getIntVar(0));
            case LOCAL_VIDEO_ITEM:
                return new LocalVideo(path, mApplication, mMatcher.getIntVar(0));
            default:
                throw new RuntimeException("bad path: " + path);
        }
    }

}

上述中LocalSource,含有的数据集有: LocalAlbumSet ,LocalAlbum, LocalMergeAlbum ,LocalImage, LocalVideo

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值