MessageAttachmentTile.java revision 62066e47fe69c33b60ed970e46809ba81d044064
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.content.ActivityNotFoundException;
21import android.content.Context;
22import android.content.Intent;
23import android.graphics.Bitmap;
24import android.net.Uri;
25import android.util.AttributeSet;
26import android.view.LayoutInflater;
27import android.view.View;
28import android.view.View.OnClickListener;
29import android.view.ViewGroup;
30
31import com.android.ex.photo.Intents;
32import com.android.ex.photo.Intents.PhotoViewIntentBuilder;
33import com.android.ex.photo.util.ImageUtils;
34import com.android.mail.R;
35import com.android.mail.photo.MailPhotoViewActivity;
36import com.android.mail.providers.Attachment;
37import com.android.mail.providers.UIProvider;
38import com.android.mail.ui.AttachmentTile;
39import com.android.mail.ui.AttachmentTileGrid;
40import com.android.mail.ui.AttachmentTile.AttachmentPreviewCache;
41import com.android.mail.utils.LogTag;
42import com.android.mail.utils.LogUtils;
43import com.android.mail.utils.Utils;
44
45import java.util.List;
46
47/**
48 * View for a single attachment in conversation view. Shows download status and allows launching
49 * intents to act on an attachment.
50 *
51 */
52public class MessageAttachmentTile extends AttachmentTile implements OnClickListener,
53        AttachmentViewInterface {
54
55    private int mPhotoIndex;
56    private Uri mAttachmentsListUri;
57    private View mTextContainer;
58
59    private final AttachmentActionHandler mActionHandler;
60
61    private static final String LOG_TAG = LogTag.getLogTag();
62
63    public MessageAttachmentTile(Context context) {
64        this(context, null);
65    }
66
67    public MessageAttachmentTile(Context context, AttributeSet attrs) {
68        super(context, attrs);
69
70        mActionHandler = new AttachmentActionHandler(context, this);
71    }
72
73    /**
74     * Render or update an attachment's view. This happens immediately upon instantiation, and
75     * repeatedly as status updates stream in, so only properties with new or changed values will
76     * cause sub-views to update.
77     */
78    @Override
79    public void render(Attachment attachment, Uri attachmentsListUri, int index,
80            AttachmentPreviewCache attachmentPreviewCache) {
81        super.render(attachment, attachmentsListUri, index, attachmentPreviewCache);
82
83        mAttachmentsListUri = attachmentsListUri;
84        mPhotoIndex = index;
85
86        mActionHandler.setAttachment(mAttachment);
87        mActionHandler.updateStatus();
88    }
89
90    public static MessageAttachmentTile inflate(LayoutInflater inflater, ViewGroup parent) {
91        MessageAttachmentTile view = (MessageAttachmentTile) inflater.inflate(
92                R.layout.conversation_message_attachment_tile, parent, false);
93        return view;
94    }
95
96
97    @Override
98    protected void onFinishInflate() {
99        super.onFinishInflate();
100
101        mTextContainer = findViewById(R.id.attachment_tile_text_container);
102
103        setOnClickListener(this);
104    }
105
106    @Override
107    public void onClick(View v) {
108        onClick(v.getId(), v);
109    }
110
111    private boolean onClick(int res, View v) {
112        mActionHandler.showAndDownloadAttachments();
113
114        return true;
115    }
116
117    public void viewAttachment() {
118        if (ImageUtils.isImageMimeType(Utils.normalizeMimeType(mAttachment.contentType))) {
119            final PhotoViewIntentBuilder builder =
120                    Intents.newPhotoViewIntentBuilder(getContext(), MailPhotoViewActivity.class);
121            builder
122                .setPhotosUri(mAttachmentsListUri.toString())
123                .setProjection(UIProvider.ATTACHMENT_PROJECTION)
124                .setPhotoIndex(mPhotoIndex);
125
126            getContext().startActivity(builder.build());
127            return;
128        }
129
130        Intent intent = new Intent(Intent.ACTION_VIEW);
131        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION
132                | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
133        Utils.setIntentDataAndTypeAndNormalize(intent, mAttachment.contentUri,
134                mAttachment.contentType);
135        try {
136            getContext().startActivity(intent);
137        } catch (ActivityNotFoundException e) {
138            // couldn't find activity for View intent
139            LogUtils.e(LOG_TAG, "Coun't find Activity for intent", e);
140        }
141    }
142
143    public void updateProgress(boolean showDeterminateProgress) {
144    }
145
146    public void onUpdateStatus() {
147    }
148
149    @Override
150    public void setThumbnailToDefault() {
151        super.setThumbnailToDefault();
152        mTextContainer.setVisibility(VISIBLE);
153    }
154
155    @Override
156    public void setThumbnail(Bitmap result) {
157        super.setThumbnail(result);
158        mTextContainer.setVisibility(GONE);
159    }
160
161    @Override
162    public List<Attachment> getAttachments() {
163        return ((AttachmentTileGrid) getParent()).getAttachments();
164    }
165}
166