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