MessageAttachmentTile.java revision 8081df46ef5a7794374e41cd1836e778a2da9b31
1/*
2 * Copyright (C) 2012 Google Inc.
3 * Licensed to The Android Open Source Project.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.mail.browse;
19
20import android.app.FragmentManager;;
21import android.content.ActivityNotFoundException;
22import android.content.Context;
23import android.content.Intent;
24import android.graphics.Bitmap;
25import android.net.Uri;
26import android.util.AttributeSet;
27import android.view.LayoutInflater;
28import android.view.View;
29import android.view.View.OnClickListener;
30import android.view.ViewGroup;
31
32import com.android.ex.photo.Intents;
33import com.android.ex.photo.Intents.PhotoViewIntentBuilder;
34import com.android.ex.photo.util.ImageUtils;
35import com.android.mail.R;
36import com.android.mail.photo.MailPhotoViewActivity;
37import com.android.mail.providers.Attachment;
38import com.android.mail.providers.UIProvider;
39import com.android.mail.ui.AttachmentTile;
40import com.android.mail.ui.AttachmentTileGrid;
41import com.android.mail.ui.AttachmentTile.AttachmentPreviewCache;
42import com.android.mail.utils.LogTag;
43import com.android.mail.utils.LogUtils;
44import com.android.mail.utils.Utils;
45
46import java.util.List;
47
48/**
49 * View for a single attachment in conversation view. Shows download status and allows launching
50 * intents to act on an attachment.
51 *
52 */
53public class MessageAttachmentTile extends AttachmentTile implements OnClickListener,
54        AttachmentViewInterface {
55
56    private int mPhotoIndex;
57    private Uri mAttachmentsListUri;
58    private View mTextContainer;
59
60    private final AttachmentActionHandler mActionHandler;
61
62    private static final String LOG_TAG = LogTag.getLogTag();
63
64    public MessageAttachmentTile(Context context) {
65        this(context, null);
66    }
67
68    public MessageAttachmentTile(Context context, AttributeSet attrs) {
69        super(context, attrs);
70
71        mActionHandler = new AttachmentActionHandler(context, this);
72    }
73
74    public void initialize(FragmentManager fragmentManager) {
75        mActionHandler.initialize(fragmentManager);
76    }
77
78    /**
79     * Render or update an attachment's view. This happens immediately upon instantiation, and
80     * repeatedly as status updates stream in, so only properties with new or changed values will
81     * cause sub-views to update.
82     */
83    @Override
84    public void render(Attachment attachment, Uri attachmentsListUri, int index,
85            AttachmentPreviewCache attachmentPreviewCache, boolean loaderResult) {
86        super.render(attachment, attachmentsListUri, index, attachmentPreviewCache, loaderResult);
87
88        mAttachmentsListUri = attachmentsListUri;
89        mPhotoIndex = index;
90
91        mActionHandler.setAttachment(mAttachment);
92        mActionHandler.updateStatus(loaderResult);
93    }
94
95    public static MessageAttachmentTile inflate(LayoutInflater inflater, ViewGroup parent) {
96        MessageAttachmentTile view = (MessageAttachmentTile) inflater.inflate(
97                R.layout.conversation_message_attachment_tile, parent, false);
98        return view;
99    }
100
101
102    @Override
103    protected void onFinishInflate() {
104        super.onFinishInflate();
105
106        mTextContainer = findViewById(R.id.attachment_tile_text_container);
107
108        setOnClickListener(this);
109    }
110
111    @Override
112    public void onClick(View v) {
113        onClick(v.getId(), v);
114    }
115
116    private boolean onClick(int res, View v) {
117        mActionHandler.showAndDownloadAttachments();
118
119        return true;
120    }
121
122    public void viewAttachment() {
123        if (ImageUtils.isImageMimeType(Utils.normalizeMimeType(mAttachment.contentType))) {
124            final PhotoViewIntentBuilder builder =
125                    Intents.newPhotoViewIntentBuilder(getContext(), MailPhotoViewActivity.class);
126            builder
127                .setPhotosUri(mAttachmentsListUri.toString())
128                .setProjection(UIProvider.ATTACHMENT_PROJECTION)
129                .setPhotoIndex(mPhotoIndex);
130
131            getContext().startActivity(builder.build());
132            return;
133        }
134
135        Intent intent = new Intent(Intent.ACTION_VIEW);
136        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
137                | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
138        Utils.setIntentDataAndTypeAndNormalize(intent, mAttachment.contentUri,
139                mAttachment.contentType);
140        try {
141            getContext().startActivity(intent);
142        } catch (ActivityNotFoundException e) {
143            // couldn't find activity for View intent
144            LogUtils.e(LOG_TAG, "Coun't find Activity for intent", e);
145        }
146    }
147
148    public void updateProgress(boolean showDeterminateProgress) {
149    }
150
151    public void onUpdateStatus() {
152    }
153
154    @Override
155    public void setThumbnailToDefault() {
156        super.setThumbnailToDefault();
157        mTextContainer.setVisibility(VISIBLE);
158    }
159
160    @Override
161    public void setThumbnail(Bitmap result) {
162        super.setThumbnail(result);
163        mTextContainer.setVisibility(GONE);
164    }
165
166    @Override
167    public List<Attachment> getAttachments() {
168        return ((AttachmentTileGrid) getParent()).getAttachments();
169    }
170}
171