MediaItemsLoader.java revision d228222e5efca7df8c7f2fe49e5ec76696cbbc05
1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.photos.shims;
18
19import android.content.AsyncTaskLoader;
20import android.content.Context;
21import android.database.Cursor;
22import android.database.MatrixCursor;
23import android.graphics.drawable.Drawable;
24import android.provider.MediaStore.Files.FileColumns;
25
26import com.android.gallery3d.data.ContentListener;
27import com.android.gallery3d.data.DataManager;
28import com.android.gallery3d.data.MediaItem;
29import com.android.gallery3d.data.MediaSet;
30import com.android.gallery3d.data.MediaSet.ItemConsumer;
31import com.android.gallery3d.data.MediaSet.SyncListener;
32import com.android.gallery3d.util.Future;
33import com.android.photos.data.PhotoSetLoader;
34import com.android.photos.drawables.DrawableFactory;
35
36import java.util.ArrayList;
37
38/**
39 * Returns all MediaItems in a MediaSet, wrapping them in a cursor to appear
40 * like a PhotoSetLoader
41 */
42public class MediaItemsLoader extends AsyncTaskLoader<Cursor> implements DrawableFactory<Cursor> {
43
44    private static final SyncListener sNullListener = new SyncListener() {
45        @Override
46        public void onSyncDone(MediaSet mediaSet, int resultCode) {
47        }
48    };
49
50    private MediaSet mMediaSet;
51    private Future<Integer> mSyncTask = null;
52    private ContentListener mObserver = new ContentListener() {
53        @Override
54        public void onContentDirty() {
55            onContentChanged();
56        }
57    };
58    private ArrayList<MediaItem> mMediaItems = new ArrayList<MediaItem>();
59
60    public MediaItemsLoader(Context context) {
61        super(context);
62        DataManager dm = DataManager.from(context);
63        String path = dm.getTopSetPath(DataManager.INCLUDE_ALL);
64        mMediaSet = dm.getMediaSet(path);
65    }
66
67    public MediaItemsLoader(Context context, String parentPath) {
68        super(context);
69        mMediaSet = DataManager.from(getContext()).getMediaSet(parentPath);
70    }
71
72    @Override
73    protected void onStartLoading() {
74        super.onStartLoading();
75        mMediaSet.addContentListener(mObserver);
76        mSyncTask = mMediaSet.requestSync(sNullListener);
77        forceLoad();
78    }
79
80    @Override
81    protected boolean onCancelLoad() {
82        if (mSyncTask != null) {
83            mSyncTask.cancel();
84            mSyncTask = null;
85        }
86        return super.onCancelLoad();
87    }
88
89    @Override
90    protected void onStopLoading() {
91        super.onStopLoading();
92        cancelLoad();
93        mMediaSet.removeContentListener(mObserver);
94    }
95
96    @Override
97    protected void onReset() {
98        super.onReset();
99        onStopLoading();
100    }
101
102    @Override
103    public Cursor loadInBackground() {
104        mMediaSet.loadIfDirty();
105        final MatrixCursor cursor = new MatrixCursor(PhotoSetLoader.PROJECTION);
106        final Object[] row = new Object[PhotoSetLoader.PROJECTION.length];
107        mMediaSet.enumerateTotalMediaItems(new ItemConsumer() {
108            @Override
109            public void consume(int index, MediaItem item) {
110                row[PhotoSetLoader.INDEX_ID] = index;
111                row[PhotoSetLoader.INDEX_DATA] = item.getContentUri().toString();
112                row[PhotoSetLoader.INDEX_DATE_ADDED] = item.getDateInMs();
113                row[PhotoSetLoader.INDEX_HEIGHT] = item.getHeight();
114                row[PhotoSetLoader.INDEX_WIDTH] = item.getWidth();
115                row[PhotoSetLoader.INDEX_WIDTH] = item.getWidth();
116                int rawMediaType = item.getMediaType();
117                int mappedMediaType = FileColumns.MEDIA_TYPE_NONE;
118                if (rawMediaType == MediaItem.MEDIA_TYPE_IMAGE) {
119                    mappedMediaType = FileColumns.MEDIA_TYPE_IMAGE;
120                } else if (rawMediaType == MediaItem.MEDIA_TYPE_VIDEO) {
121                    mappedMediaType = FileColumns.MEDIA_TYPE_VIDEO;
122                }
123                row[PhotoSetLoader.INDEX_MEDIA_TYPE] = mappedMediaType;
124                cursor.addRow(row);
125                mMediaItems.add(item);
126            }
127        });
128        return cursor;
129    }
130
131    @Override
132    public Drawable drawableForItem(Cursor item, Drawable recycle) {
133        BitmapJobDrawable drawable = null;
134        if (recycle == null || !(recycle instanceof BitmapJobDrawable)) {
135            drawable = new BitmapJobDrawable();
136        } else {
137            drawable = (BitmapJobDrawable) recycle;
138        }
139        int index = item.getInt(PhotoSetLoader.INDEX_ID);
140        drawable.setMediaItem(mMediaItems.get(index));
141        return drawable;
142    }
143
144    public static int getThumbnailSize() {
145        return MediaItem.getTargetSize(MediaItem.TYPE_MICROTHUMBNAIL);
146    }
147
148}
149