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.MediaColumns; 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.datamodel.media.VideoThumbnailRequestDescriptor; 30 import com.android.messaging.util.Assert; 31 import com.android.messaging.util.ContentType; 32 import com.android.messaging.util.UriUtil; 33 34 /** 35 * Provides data for GalleryGridItemView 36 */ 37 public class GalleryGridItemData { 38 public static final String[] MEDIA_PROJECTION = new String[] { 39 MediaColumns._ID, 40 MediaColumns.DATA, 41 MediaColumns.WIDTH, 42 MediaColumns.HEIGHT, 43 MediaColumns.MIME_TYPE, 44 MediaColumns.DATE_MODIFIED, 45 MediaColumns.DISPLAY_NAME}; 46 47 public static final String[] SPECIAL_ITEM_COLUMNS = new String[] { 48 BaseColumns._ID 49 }; 50 51 private static final int INDEX_ID = 0; 52 53 // For local media gallery. 54 private static final int INDEX_DATA_PATH = 1; 55 private static final int INDEX_WIDTH = 2; 56 private static final int INDEX_HEIGHT = 3; 57 private static final int INDEX_MIME_TYPE = 4; 58 private static final int INDEX_DATE_MODIFIED = 5; 59 private static final int INDEX_DISPLAY_NAME = 6; 60 61 /** A special item's id for picking a media from document picker */ 62 public static final String ID_DOCUMENT_PICKER_ITEM = "-1"; 63 64 private UriImageRequestDescriptor mImageData; 65 private String mContentType; 66 private boolean mIsDocumentPickerItem; 67 private long mDateSeconds; 68 private String mFileName; 69 private Uri mAudioUri; 70 GalleryGridItemData()71 public GalleryGridItemData() { 72 } 73 bind(final Cursor cursor, final int desiredWidth, final int desiredHeight)74 public void bind(final Cursor cursor, final int desiredWidth, final int desiredHeight) { 75 mIsDocumentPickerItem = TextUtils.equals(cursor.getString(INDEX_ID), 76 ID_DOCUMENT_PICKER_ITEM); 77 if (mIsDocumentPickerItem) { 78 mImageData = null; 79 mContentType = null; 80 } else { 81 mContentType = cursor.getString(INDEX_MIME_TYPE); 82 final String filePath = cursor.getString(INDEX_DATA_PATH); 83 final String dateModified = cursor.getString(INDEX_DATE_MODIFIED); 84 mDateSeconds = !TextUtils.isEmpty(dateModified) ? Long.parseLong(dateModified) : -1; 85 if (ContentType.isAudioType(mContentType)) { 86 mImageData = null; 87 mAudioUri = UriUtil.getUriForResourceFile(filePath); 88 mFileName = cursor.getString(INDEX_DISPLAY_NAME); 89 } else { // For image and video types 90 int sourceWidth = cursor.getInt(INDEX_WIDTH); 91 int sourceHeight = cursor.getInt(INDEX_HEIGHT); 92 93 // Guard against bad data 94 if (sourceWidth <= 0) { 95 sourceWidth = ImageRequest.UNSPECIFIED_SIZE; 96 } 97 if (sourceHeight <= 0) { 98 sourceHeight = ImageRequest.UNSPECIFIED_SIZE; 99 } 100 101 if (ContentType.isVideoType(mContentType)) { 102 mImageData = new VideoThumbnailRequestDescriptor( 103 cursor.getLong(INDEX_ID), 104 desiredWidth, 105 desiredHeight, 106 sourceWidth, 107 sourceHeight); 108 } else { 109 mImageData = new FileImageRequestDescriptor( 110 filePath, 111 desiredWidth, 112 desiredHeight, 113 sourceWidth, 114 sourceHeight, 115 true /* canUseThumbnail */, 116 true /* allowCompression */, 117 true /* isStatic */); 118 } 119 } 120 } 121 } 122 isDocumentPickerItem()123 public boolean isDocumentPickerItem() { 124 return mIsDocumentPickerItem; 125 } 126 getImageUri()127 public Uri getImageUri() { 128 return ContentType.isAudioType(mContentType) ? mAudioUri : mImageData.uri; 129 } 130 getImageRequestDescriptor()131 public UriImageRequestDescriptor getImageRequestDescriptor() { 132 return mImageData; 133 } 134 constructMessagePartData(final Rect startRect)135 public MessagePartData constructMessagePartData(final Rect startRect) { 136 Assert.isTrue(!mIsDocumentPickerItem); 137 return ContentType.isAudioType(mContentType) 138 ? new MediaPickerMessagePartData(startRect, mContentType, mAudioUri, 0, 0) 139 : new MediaPickerMessagePartData(startRect, mContentType, mImageData.uri, 140 mImageData.sourceWidth, mImageData.sourceHeight); 141 } 142 143 /** 144 * @return The date in seconds. This can be negative if we could not retreive date info 145 */ getDateSeconds()146 public long getDateSeconds() { 147 return mDateSeconds; 148 } 149 getContentType()150 public String getContentType() { 151 return mContentType; 152 } 153 getFileName()154 public String getFileName() { 155 return mFileName; 156 } 157 } 158