1/*
2 * Copyright (C) 2015 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.messaging.datamodel;
18
19import android.content.Context;
20import android.net.Uri;
21import android.provider.MediaStore.Files;
22import android.provider.MediaStore.Files.FileColumns;
23import android.provider.MediaStore.Images.Media;
24
25import com.android.messaging.datamodel.data.GalleryGridItemData;
26import com.android.messaging.datamodel.data.MessagePartData;
27import com.google.common.base.Joiner;
28
29/**
30 * A BoundCursorLoader that reads local media on the device.
31 */
32public class GalleryBoundCursorLoader extends BoundCursorLoader {
33    public static final String MEDIA_SCANNER_VOLUME_EXTERNAL = "external";
34    private static final Uri STORAGE_URI = Files.getContentUri(MEDIA_SCANNER_VOLUME_EXTERNAL);
35    private static final String SORT_ORDER = Media.DATE_MODIFIED + " DESC";
36    private static final String IMAGE_SELECTION = createSelection(
37            MessagePartData.ACCEPTABLE_IMAGE_TYPES,
38            new Integer[] { FileColumns.MEDIA_TYPE_IMAGE });
39
40    public GalleryBoundCursorLoader(final String bindingId, final Context context) {
41        super(bindingId, context, STORAGE_URI, GalleryGridItemData.IMAGE_PROJECTION,
42                IMAGE_SELECTION, null, SORT_ORDER);
43    }
44
45    private static String createSelection(final String[] mimeTypes, Integer[] mediaTypes) {
46        return Media.MIME_TYPE + " IN ('" + Joiner.on("','").join(mimeTypes) + "') AND "
47                + FileColumns.MEDIA_TYPE + " IN (" + Joiner.on(',').join(mediaTypes) + ")";
48    }
49}
50