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 
17 package com.android.messaging.datamodel.data;
18 
19 import android.database.Cursor;
20 import android.graphics.Rect;
21 import android.net.Uri;
22 import android.provider.BaseColumns;
23 import android.provider.MediaStore.Images.Media;
24 import android.text.TextUtils;
25 
26 import com.android.messaging.datamodel.media.FileImageRequestDescriptor;
27 import com.android.messaging.datamodel.media.ImageRequest;
28 import com.android.messaging.datamodel.media.UriImageRequestDescriptor;
29 import com.android.messaging.util.Assert;
30 
31 /**
32  * Provides data for GalleryGridItemView
33  */
34 public class GalleryGridItemData {
35     public static final String[] IMAGE_PROJECTION = new String[] {
36         Media._ID,
37         Media.DATA,
38         Media.WIDTH,
39         Media.HEIGHT,
40         Media.MIME_TYPE,
41         Media.DATE_MODIFIED};
42 
43     public static final String[] SPECIAL_ITEM_COLUMNS = new String[] {
44         BaseColumns._ID
45     };
46 
47     private static final int INDEX_ID = 0;
48 
49     // For local image gallery.
50     private static final int INDEX_DATA_PATH = 1;
51     private static final int INDEX_WIDTH = 2;
52     private static final int INDEX_HEIGHT = 3;
53     private static final int INDEX_MIME_TYPE = 4;
54     private static final int INDEX_DATE_MODIFIED = 5;
55 
56     /** A special item's id for picking images from document picker */
57     public static final String ID_DOCUMENT_PICKER_ITEM = "-1";
58 
59     private UriImageRequestDescriptor mImageData;
60     private String mContentType;
61     private boolean mIsDocumentPickerItem;
62     private long mDateSeconds;
63 
GalleryGridItemData()64     public GalleryGridItemData() {
65     }
66 
bind(final Cursor cursor, final int desiredWidth, final int desiredHeight)67     public void bind(final Cursor cursor, final int desiredWidth, final int desiredHeight) {
68         mIsDocumentPickerItem = TextUtils.equals(cursor.getString(INDEX_ID),
69                 ID_DOCUMENT_PICKER_ITEM);
70         if (mIsDocumentPickerItem) {
71             mImageData = null;
72             mContentType = null;
73         } else {
74             int sourceWidth = cursor.getInt(INDEX_WIDTH);
75             int sourceHeight = cursor.getInt(INDEX_HEIGHT);
76 
77             // Guard against bad data
78             if (sourceWidth <= 0) {
79                 sourceWidth = ImageRequest.UNSPECIFIED_SIZE;
80             }
81             if (sourceHeight <= 0) {
82                 sourceHeight = ImageRequest.UNSPECIFIED_SIZE;
83             }
84 
85             mContentType = cursor.getString(INDEX_MIME_TYPE);
86             final String dateModified = cursor.getString(INDEX_DATE_MODIFIED);
87             mDateSeconds = !TextUtils.isEmpty(dateModified) ? Long.parseLong(dateModified) : -1;
88             mImageData = new FileImageRequestDescriptor(
89                     cursor.getString(INDEX_DATA_PATH),
90                     desiredWidth,
91                     desiredHeight,
92                     sourceWidth,
93                     sourceHeight,
94                     true /* canUseThumbnail */,
95                     true /* allowCompression */,
96                     true /* isStatic */);
97         }
98     }
99 
isDocumentPickerItem()100     public boolean isDocumentPickerItem() {
101         return mIsDocumentPickerItem;
102     }
103 
getImageUri()104     public Uri getImageUri() {
105         return mImageData.uri;
106     }
107 
getImageRequestDescriptor()108     public UriImageRequestDescriptor getImageRequestDescriptor() {
109         return mImageData;
110     }
111 
constructMessagePartData(final Rect startRect)112     public MessagePartData constructMessagePartData(final Rect startRect) {
113         Assert.isTrue(!mIsDocumentPickerItem);
114         return new MediaPickerMessagePartData(startRect, mContentType,
115                 mImageData.uri, mImageData.sourceWidth, mImageData.sourceHeight);
116     }
117 
118     /**
119      * @return The date in seconds. This can be negative if we could not retreive date info
120      */
getDateSeconds()121     public long getDateSeconds() {
122         return mDateSeconds;
123     }
124 
getContentType()125     public String getContentType() {
126         return mContentType;
127     }
128 }
129