1d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/*
2d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Copyright (C) 2015 The Android Open Source Project
3d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
4d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Licensed under the Apache License, Version 2.0 (the "License");
5d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * you may not use this file except in compliance with the License.
6d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * You may obtain a copy of the License at
7d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
8d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *      http://www.apache.org/licenses/LICENSE-2.0
9d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd *
10d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * Unless required by applicable law or agreed to in writing, software
11d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * distributed under the License is distributed on an "AS IS" BASIS,
12d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * See the License for the specific language governing permissions and
14d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * limitations under the License.
15d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
16d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpackage com.android.messaging.ui;
17d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
18d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.Context;
19d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.content.res.Resources;
20d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.graphics.Rect;
21d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.net.Uri;
22d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.support.annotation.Nullable;
23d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.text.TextUtils;
24d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.view.LayoutInflater;
25d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.view.View;
26d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.view.View.OnClickListener;
27d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.view.View.OnLongClickListener;
28d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.view.ViewGroup;
29d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.widget.FrameLayout.LayoutParams;
30d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.widget.ImageView;
31d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport android.widget.TextView;
32d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
33d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.R;
34d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.DataModel;
35d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.MessagePartData;
36d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.PendingAttachmentData;
37d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.PersonItemData;
38d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.data.VCardContactItemData;
39d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.media.FileImageRequestDescriptor;
40d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.media.ImageRequest;
41d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.media.ImageRequestDescriptor;
42d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.datamodel.media.UriImageRequestDescriptor;
43d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.ui.MultiAttachmentLayout.OnAttachmentClickListener;
44d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.ui.PersonItemView.PersonItemViewListener;
45d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.Assert;
46d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.ContentType;
47d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.ImageUtils;
48d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.UiUtils;
49d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddimport com.android.messaging.util.UriUtil;
50d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
51d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd/**
52d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd * A view factory that creates previews for single/multiple attachments.
53d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd */
54d3b009ae55651f1e60950342468e3c37fdeb0796Mike Doddpublic class AttachmentPreviewFactory {
55d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /** Standalone attachment preview */
56d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int TYPE_SINGLE = 1;
57d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
58d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /** Attachment preview displayed in a multi-attachment layout */
59d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int TYPE_MULTIPLE = 2;
60d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
61d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    /** Attachment preview displayed in the attachment chooser grid view */
62d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static final int TYPE_CHOOSER_GRID = 3;
63d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
64d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static View createAttachmentPreview(final LayoutInflater layoutInflater,
65d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final MessagePartData attachmentData, final ViewGroup parent,
66d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final int viewType, final boolean startImageRequest,
67d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            @Nullable final OnAttachmentClickListener clickListener) {
68d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final String contentType = attachmentData.getContentType();
69d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        View attachmentView = null;
70d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (attachmentData instanceof PendingAttachmentData) {
71d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            attachmentView = createPendingAttachmentPreview(layoutInflater, parent,
72d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    (PendingAttachmentData) attachmentData);
73d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        } else if (ContentType.isImageType(contentType)) {
74d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            attachmentView = createImagePreview(layoutInflater, attachmentData, parent, viewType,
75d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    startImageRequest);
76d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        } else if (ContentType.isAudioType(contentType)) {
77d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            attachmentView = createAudioPreview(layoutInflater, attachmentData, parent, viewType);
78d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        } else if (ContentType.isVideoType(contentType)) {
79d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            attachmentView = createVideoPreview(layoutInflater, attachmentData, parent, viewType);
80d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        } else if (ContentType.isVCardType(contentType)) {
81d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            attachmentView = createVCardPreview(layoutInflater, attachmentData, parent, viewType);
82d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        } else {
83d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            Assert.fail("unsupported attachment type: " + contentType);
84d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            return null;
85d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
86d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
87d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        // Some views have a caption, set the text/visibility if one exists
88d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final TextView captionView = (TextView) attachmentView.findViewById(R.id.caption);
89d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (captionView != null) {
90d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final String caption = attachmentData.getText();
91d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            captionView.setVisibility(TextUtils.isEmpty(caption) ? View.GONE : View.VISIBLE);
92d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            captionView.setText(caption);
93d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
94d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
95d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (attachmentView != null && clickListener != null) {
96d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            attachmentView.setOnClickListener(new OnClickListener() {
97d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    @Override
98d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    public void onClick(final View view) {
99d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        final Rect bounds = UiUtils.getMeasuredBoundsOnScreen(view);
100d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        clickListener.onAttachmentClick(attachmentData, bounds,
101d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                                false /* longPress */);
102d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    }
103d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                });
104d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            attachmentView.setOnLongClickListener(new OnLongClickListener() {
105d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    @Override
106d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    public boolean onLongClick(final View view) {
107d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        final Rect bounds = UiUtils.getMeasuredBoundsOnScreen(view);
108d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        return clickListener.onAttachmentClick(attachmentData, bounds,
109d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                                true /* longPress */);
110d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    }
111d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                });
112d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
113d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return attachmentView;
114d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
115d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
116d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static MultiAttachmentLayout createMultiplePreview(final Context context,
117d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final OnAttachmentClickListener listener) {
118d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final MultiAttachmentLayout multiAttachmentLayout =
119d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                new MultiAttachmentLayout(context, null);
120d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
121d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
122d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        multiAttachmentLayout.setLayoutParams(layoutParams);
123d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        multiAttachmentLayout.setOnAttachmentClickListener(listener);
124d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return multiAttachmentLayout;
125d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
126d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
127d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    public static ImageRequestDescriptor getImageRequestDescriptorForAttachment(
128d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final MessagePartData attachmentData, final int desiredWidth, final int desiredHeight) {
129d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final Uri uri = attachmentData.getContentUri();
130d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final String contentType = attachmentData.getContentType();
131d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (ContentType.isImageType(contentType)) {
132d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final String filePath = UriUtil.getFilePathFromUri(uri);
133d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            if (filePath != null) {
134d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return new FileImageRequestDescriptor(filePath, desiredWidth, desiredHeight,
135d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        attachmentData.getWidth(), attachmentData.getHeight(),
136d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        false /* canUseThumbnail */, true /* allowCompression */,
137d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        false /* isStatic */);
138d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            } else {
139d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return new UriImageRequestDescriptor(uri, desiredWidth, desiredHeight,
140d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        attachmentData.getWidth(), attachmentData.getHeight(),
141d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        true /* allowCompression */, false /* isStatic */, false /*cropToCircle*/,
142d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        ImageUtils.DEFAULT_CIRCLE_BACKGROUND_COLOR /* circleBackgroundColor */,
143d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                        ImageUtils.DEFAULT_CIRCLE_STROKE_COLOR /* circleStrokeColor */);
144d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
145d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
146d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return null;
147d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
148d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
149d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static View createImagePreview(final LayoutInflater layoutInflater,
150d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final MessagePartData attachmentData, final ViewGroup parent,
151d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final int viewType, final boolean startImageRequest) {
152d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        int layoutId = R.layout.attachment_single_image;
153d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        switch (viewType) {
154d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case AttachmentPreviewFactory.TYPE_SINGLE:
155d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                layoutId = R.layout.attachment_single_image;
156d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                break;
157d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case AttachmentPreviewFactory.TYPE_MULTIPLE:
158d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                layoutId = R.layout.attachment_multiple_image;
159d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                break;
160d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case AttachmentPreviewFactory.TYPE_CHOOSER_GRID:
161d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                layoutId = R.layout.attachment_chooser_image;
162d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                break;
163d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            default:
164d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                Assert.fail("unsupported attachment view type!");
165d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                break;
166d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
167d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final View view = layoutInflater.inflate(layoutId, parent, false /* attachToRoot */);
168d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final AsyncImageView imageView = (AsyncImageView) view.findViewById(
169d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                R.id.attachment_image_view);
170d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        int maxWidth = imageView.getMaxWidth();
171d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        int maxHeight = imageView.getMaxHeight();
172d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (viewType == TYPE_CHOOSER_GRID) {
173d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final Resources resources = layoutInflater.getContext().getResources();
174d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            maxWidth = maxHeight = resources.getDimensionPixelSize(
175d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    R.dimen.attachment_grid_image_cell_size);
176d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
177d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (maxWidth <= 0 || maxWidth == Integer.MAX_VALUE) {
178d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            maxWidth = ImageRequest.UNSPECIFIED_SIZE;
179d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
180d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (maxHeight <= 0 || maxHeight == Integer.MAX_VALUE) {
181d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            maxHeight = ImageRequest.UNSPECIFIED_SIZE;
182d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
183d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        if (startImageRequest) {
184d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            imageView.setImageResourceId(getImageRequestDescriptorForAttachment(attachmentData,
185d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    maxWidth, maxHeight));
186d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
187d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        imageView.setContentDescription(
188d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                parent.getResources().getString(R.string.message_image_content_description));
189d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return view;
190d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
191d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
192d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static View createPendingAttachmentPreview(final LayoutInflater layoutInflater,
193d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final ViewGroup parent, final PendingAttachmentData attachmentData) {
194d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final View pendingItemView = layoutInflater.inflate(R.layout.attachment_pending_item,
195d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                parent, false);
196d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final ImageView imageView = (ImageView)
197d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                pendingItemView.findViewById(R.id.pending_item_view);
198d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final ViewGroup.LayoutParams layoutParams = imageView.getLayoutParams();
199d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final int defaultSize = layoutInflater.getContext().getResources().getDimensionPixelSize(
200d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                R.dimen.pending_attachment_size);
201d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        layoutParams.width = attachmentData.getWidth() == MessagePartData.UNSPECIFIED_SIZE ?
202d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                defaultSize : attachmentData.getWidth();
203d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        layoutParams.height = attachmentData.getHeight() == MessagePartData.UNSPECIFIED_SIZE ?
204d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                defaultSize : attachmentData.getHeight();
205d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return pendingItemView;
206d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
207d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
208d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static View createVCardPreview(final LayoutInflater layoutInflater,
209d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final MessagePartData attachmentData, final ViewGroup parent,
210d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final int viewType) {
211d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        int layoutId = R.layout.attachment_single_vcard;
212d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        switch (viewType) {
213d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case AttachmentPreviewFactory.TYPE_SINGLE:
214d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                layoutId = R.layout.attachment_single_vcard;
215d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                break;
216d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case AttachmentPreviewFactory.TYPE_MULTIPLE:
217d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                layoutId = R.layout.attachment_multiple_vcard;
218d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                break;
219d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case AttachmentPreviewFactory.TYPE_CHOOSER_GRID:
220d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                layoutId = R.layout.attachment_chooser_vcard;
221d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                break;
222d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            default:
223d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                Assert.fail("unsupported attachment view type!");
224d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                break;
225d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
226d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final View view = layoutInflater.inflate(layoutId, parent, false /* attachToRoot */);
227d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final PersonItemView vcardPreview = (PersonItemView) view.findViewById(
228d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                R.id.vcard_attachment_view);
229d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        vcardPreview.setAvatarOnly(viewType != AttachmentPreviewFactory.TYPE_SINGLE);
230d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        vcardPreview.bind(DataModel.get().createVCardContactItemData(layoutInflater.getContext(),
231d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                attachmentData));
232d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        vcardPreview.setListener(new PersonItemViewListener() {
233d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            @Override
234d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            public void onPersonClicked(final PersonItemData data) {
235d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                Assert.isTrue(data instanceof VCardContactItemData);
236d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                final VCardContactItemData vCardData = (VCardContactItemData) data;
237d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                if (vCardData.hasValidVCard()) {
238d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    final Uri vCardUri = vCardData.getVCardUri();
239d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                    UIIntents.get().launchVCardDetailActivity(vcardPreview.getContext(), vCardUri);
240d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                }
241d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
242d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
243d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            @Override
244d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            public boolean onPersonLongClicked(final PersonItemData data) {
245d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                return false;
246d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            }
247d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        });
248d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return view;
249d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
250d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
251d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static View createAudioPreview(final LayoutInflater layoutInflater,
252d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                final MessagePartData attachmentData, final ViewGroup parent,
253d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                final int viewType) {
254d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        int layoutId = R.layout.attachment_single_audio;
255d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        switch (viewType) {
256d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case AttachmentPreviewFactory.TYPE_SINGLE:
257d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                layoutId = R.layout.attachment_single_audio;
258d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                break;
259d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case AttachmentPreviewFactory.TYPE_MULTIPLE:
260d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                layoutId = R.layout.attachment_multiple_audio;
261d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                break;
262d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case AttachmentPreviewFactory.TYPE_CHOOSER_GRID:
263d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                layoutId = R.layout.attachment_chooser_audio;
264d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                break;
265d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            default:
266d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                Assert.fail("unsupported attachment view type!");
267d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                break;
268d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
269d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final View view = layoutInflater.inflate(layoutId, parent, false /* attachToRoot */);
270d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final AudioAttachmentView audioView = (AudioAttachmentView)
271d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                view.findViewById(R.id.audio_attachment_view);
272eba0949bc60e9002adf0c96044677b4c281b2819Shri Borde        audioView.bindMessagePartData(
273eba0949bc60e9002adf0c96044677b4c281b2819Shri Borde                attachmentData, false /* incoming */, false /* showAsSelected */);
274d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return view;
275d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
276d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd
277d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    private static View createVideoPreview(final LayoutInflater layoutInflater,
278d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final MessagePartData attachmentData, final ViewGroup parent,
279d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            final int viewType) {
280d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        int layoutId = R.layout.attachment_single_video;
281d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        switch (viewType) {
282d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case AttachmentPreviewFactory.TYPE_SINGLE:
283d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                layoutId = R.layout.attachment_single_video;
284d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                break;
285d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case AttachmentPreviewFactory.TYPE_MULTIPLE:
286d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                layoutId = R.layout.attachment_multiple_video;
287d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                break;
288d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            case AttachmentPreviewFactory.TYPE_CHOOSER_GRID:
289d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                layoutId = R.layout.attachment_chooser_video;
290d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                break;
291d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd            default:
292d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                Assert.fail("unsupported attachment view type!");
293d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                break;
294d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        }
295d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        final VideoThumbnailView videoThumbnail = (VideoThumbnailView) layoutInflater.inflate(
296d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd                layoutId, parent, false /* attachToRoot */);
297d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        videoThumbnail.setSource(attachmentData, false /* incomingMessage */);
298d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd        return videoThumbnail;
299d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd    }
300d3b009ae55651f1e60950342468e3c37fdeb0796Mike Dodd}
301