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