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