Android5.1图库Gallery2代码分析数据加载流程

时间:2022-07-26 16:25:14

图片数据加载流程。

Gallery---->GalleryActivity------>AlbumSetPage------->AlbumPage--------->PhotoPage

相册集                        照片集                 某张图片

1,AlbumSetPage.java

private void initializeData(Bundle data) {

String mediaPath = data.getString(AlbumSetPage.KEY_MEDIA_PATH);

mMediaSet = mActivity.getDataManager().getMediaSet(mediaPath);

mSelectionManager.setSourceMediaSet(mMediaSet);

mAlbumSetDataAdapter = new AlbumSetDataLoader(

mActivity, mMediaSet, DATA_CACHE_SIZE);

mAlbumSetDataAdapter.setLoadingListener(new MyLoadingListener());

}

增加回掉函数,当AlbumSetDataLoader.java发生变化时,更新界面

2,AlbumSetDataloader.java

public void resume() {

mSource.addContentListener(mSourceListener);

mReloadTask = new ReloadTask();

mReloadTask.start();

增加回调函数,当数据库发生变化时,加载数据。

 

3,LocalAlbumSet.java (extends MediaSet)

public void addContentListener(ContentListener listener) {

/// M: [BUG.MODIFY] avoid ConcurrentModificationException @{

/*mListeners.put(listener, null);*/

synchronized(WEAK_HASH_MAP_LOCK) {

mListeners.put(listener, null);

}

  增加回调直接和数据库打交道。

4,DataManager.java

public synchronized void initializeSourceMap() {

if (!mSourceMap.isEmpty()) return;

// the order matters, the UriSource must come last

addSource(new LocalSource(mApplication));

新建LocalSource.java

 

5,LocalSource.java(extends MediaSource)

@Override

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);

新建LocalAlbumSet

 

6,LocalAlbumSet.java (extends MediaSet)

public LocalAlbumSet(Path path, GalleryApp application) {

super(path, nextVersionNumber());

mApplication = application;

mHandler = new Handler(application.getMainLooper());

mType = getTypeFromPath(path);

mNotifier = new ChangeNotifier(this, mWatchUris, application);

mName = application.getResources().getString(

R.string.set_label_local_albums);

}

private static final Uri[] mWatchUris =

{Images.Media.EXTERNAL_CONTENT_URI, Video.Media.EXTERNAL_CONTENT_URI};

将监控要监控的图片和视频url添加到ChangeNotifier中。

 

7,ChangeNotifier.java

public ChangeNotifier(MediaSet set, Uri[] uris, GalleryApp application) {

mMediaSet = set;

for (int i = 0; i < uris.length; i++) {

application.getDataManager().registerChangeNotifier(uris[i], this);

  注册url

 

8,DataManager.java

public void registerChangeNotifier(Uri uri, ChangeNotifier notifier) {

NotifyBroker broker = null;

synchronized (mNotifierMap) {

broker = mNotifierMap.get(uri);

if (broker == null) {

broker = new NotifyBroker(mDefaultMainHandler);

mApplication.getContentResolver()

.registerContentObserver(uri, true, broker);

mNotifierMap.put(uri, broker);

}

}

broker.registerNotifier(notifier);

}

将url和NotifyBroker进行关联,当url发生变化时,就调用回调类broker

9,DataManager.java

private static class NotifyBroker extends ContentObserver {

@Override

public synchronized void onChange(boolean selfChange) {

for(ChangeNotifier notifier : mNotifiers.keySet()) {

notifier.onChange(selfChange);

}

}

当url发生变化时,就调用。notifier.onChange(selfChange)

-----àmMediaSet.notifyContentChanged();(ChangeNotifier.java)

--à listener.onContentDirty();(LocalAlbumSet.java (extends MediaSet))