1/*
2 * Copyright (C) 2009 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.camera.gallery;
18
19import android.content.ContentResolver;
20import android.database.Cursor;
21import android.net.Uri;
22import android.provider.MediaStore.Images;
23import android.provider.MediaStore.Video.Media;
24
25import java.util.HashMap;
26
27/**
28 * A collection of all the <code>VideoObject</code> in gallery.
29 */
30public class VideoList extends BaseImageList {
31
32    @SuppressWarnings("unused")
33    private static final String TAG = "BaseImageList";
34
35    private static final String[] VIDEO_PROJECTION = new String[] {
36            Media._ID,
37            Media.DATA,
38            Media.DATE_TAKEN,
39            Media.TITLE,
40            Media.MINI_THUMB_MAGIC,
41            Media.MIME_TYPE,
42            Media.DATE_MODIFIED};
43
44    private static final int INDEX_ID = 0;
45    private static final int INDEX_DATA_PATH = 1;
46    private static final int INDEX_DATE_TAKEN = 2;
47    private static final int INDEX_TITLE = 3;
48    private static final int INDEX_MIMI_THUMB_MAGIC = 4;
49    private static final int INDEX_MIME_TYPE = 5;
50    private static final int INDEX_DATE_MODIFIED = 6;
51
52    @Override
53    protected long getImageId(Cursor cursor) {
54        return cursor.getLong(INDEX_ID);
55    }
56
57    @Override
58    protected BaseImage loadImageFromCursor(Cursor cursor) {
59        long id = cursor.getLong(INDEX_ID);
60        String dataPath = cursor.getString(INDEX_DATA_PATH);
61        long dateTaken = cursor.getLong(INDEX_DATE_TAKEN);
62        if (dateTaken == 0) {
63            dateTaken = cursor.getLong(INDEX_DATE_MODIFIED) * 1000;
64        }
65        long miniThumbMagic = cursor.getLong(INDEX_MIMI_THUMB_MAGIC);
66        String title = cursor.getString(INDEX_TITLE);
67        String mimeType = cursor.getString(INDEX_MIME_TYPE);
68        if (title == null || title.length() == 0) {
69            title = dataPath;
70        }
71        return new VideoObject(this, mContentResolver,
72                id, cursor.getPosition(), contentUri(id), dataPath,
73                mimeType, dateTaken, title);
74    }
75
76    public VideoList(ContentResolver resolver, Uri uri, int sort,
77            String bucketId) {
78        super(resolver, uri, sort, bucketId);
79    }
80
81    public HashMap<String, String> getBucketIds() {
82        Uri uri = mBaseUri.buildUpon()
83                .appendQueryParameter("distinct", "true").build();
84        Cursor c = Images.Media.query(
85                mContentResolver, uri,
86                new String[] {
87                    Media.BUCKET_DISPLAY_NAME,
88                    Media.BUCKET_ID
89                },
90                whereClause(), whereClauseArgs(), sortOrder());
91        try {
92            HashMap<String, String> hash = new HashMap<String, String>();
93            while (c.moveToNext()) {
94                hash.put(c.getString(1), c.getString(0));
95            }
96            return hash;
97        } finally {
98            c.close();
99        }
100    }
101
102    protected String whereClause() {
103        return mBucketId != null
104                ? Images.Media.BUCKET_ID + " = '" + mBucketId + "'"
105                : null;
106    }
107
108    protected String[] whereClauseArgs() {
109        return null;
110    }
111
112    @Override
113    protected Cursor createCursor() {
114        Cursor c = Images.Media.query(
115                mContentResolver, mBaseUri, VIDEO_PROJECTION,
116                whereClause(), whereClauseArgs(), sortOrder());
117        return c;
118    }
119}
120