1/**
2 * Copyright (c) 2007, Google Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.android.mail.compose;
17
18import android.content.Context;
19import android.view.LayoutInflater;
20import android.view.View;
21import android.widget.ImageButton;
22import android.widget.LinearLayout;
23import android.widget.TextView;
24
25import com.android.mail.providers.Attachment;
26import com.android.mail.R;
27import com.android.mail.utils.AttachmentUtils;
28import com.android.mail.utils.LogTag;
29import com.android.mail.utils.LogUtils;
30
31import org.json.JSONException;
32
33/**
34 * This view is used in the ComposeActivity to display an attachment along with its name/size
35 * and a Remove button.
36 */
37class AttachmentComposeView extends LinearLayout implements AttachmentDeletionInterface {
38    private final Attachment mAttachment;
39    private final static String LOG_TAG = LogTag.getLogTag();
40
41    public AttachmentComposeView(Context c, Attachment attachment) {
42        super(c);
43        mAttachment = attachment;
44
45        if (LogUtils.isLoggable(LOG_TAG, LogUtils.DEBUG)) {
46            String attachStr = null;
47            try {
48                attachStr = attachment.toJSON().toString(2);
49            } catch (JSONException e) {
50                attachStr = attachment.toString();
51            }
52            LogUtils.d(LOG_TAG, "attachment view: %s", attachStr);
53        }
54
55        LayoutInflater factory = LayoutInflater.from(getContext());
56
57        factory.inflate(R.layout.attachment, this);
58        populateAttachmentData(c);
59    }
60
61    @Override
62    public void addDeleteListener(OnClickListener clickListener) {
63        final ImageButton deleteButton = (ImageButton) findViewById(R.id.remove_attachment);
64        deleteButton.setOnClickListener(clickListener);
65        deleteButton.setContentDescription(getResources().getString(R.string.remove_attachment_desc,
66                mAttachment.getName()));
67    }
68
69    private void populateAttachmentData(Context context) {
70        ((TextView) findViewById(R.id.attachment_name)).setText(mAttachment.getName());
71
72        if (mAttachment.size > 0) {
73            ((TextView) findViewById(R.id.attachment_size)).
74                    setText(AttachmentUtils.convertToHumanReadableSize(context, mAttachment.size));
75        } else {
76            ((TextView) findViewById(R.id.attachment_size)).setVisibility(View.GONE);
77        }
78    }
79}
80