1package com.android.mail.browse;
2
3import android.content.Context;
4import android.util.AttributeSet;
5import android.view.View;
6import android.view.ViewGroup;
7import android.widget.LinearLayout;
8
9import com.android.mail.R;
10import com.android.mail.browse.ConversationViewAdapter.ConversationFooterItem;
11import com.android.mail.browse.ConversationViewAdapter.MessageHeaderItem;
12import com.android.mail.compose.ComposeActivity;
13import com.android.mail.providers.Account;
14import com.android.mail.providers.Message;
15import com.android.mail.utils.LogTag;
16import com.android.mail.utils.LogUtils;
17import com.android.mail.utils.Utils;
18
19/**
20 * A view placed at the bottom of the conversation view that allows the user to
21 * reply/reply all/forward to the last message in the conversation.
22 */
23public class ConversationFooterView extends LinearLayout implements View.OnClickListener {
24
25    public interface ConversationFooterCallbacks {
26        /**
27         * Called when the height of the {@link ConversationFooterView} changes.
28         *
29         * @param newHeight the new height in px
30         */
31        void onConversationFooterHeightChange(int newHeight);
32    }
33    private static final String LOG_TAG = LogTag.getLogTag();
34
35    private ConversationFooterItem mFooterItem;
36    private ConversationAccountController mAccountController;
37    private ConversationFooterCallbacks mCallbacks;
38
39    private View mFooterButtons;
40
41    public ConversationFooterView(Context context) {
42        super(context);
43    }
44
45    public ConversationFooterView(Context context, AttributeSet attrs) {
46        super(context, attrs);
47    }
48
49    public ConversationFooterView(Context context, AttributeSet attrs, int defStyle) {
50        super(context, attrs, defStyle);
51    }
52
53    @Override
54    protected void onFinishInflate() {
55        super.onFinishInflate();
56
57        mFooterButtons = findViewById(R.id.footer_buttons);
58
59        findViewById(R.id.reply_button).setOnClickListener(this);
60        findViewById(R.id.reply_all_button).setOnClickListener(this);
61        findViewById(R.id.forward_button).setOnClickListener(this);
62    }
63
64    @Override
65    public void onClick(View v) {
66        if (mFooterItem == null) {
67            LogUtils.i(LOG_TAG, "ignoring conversation footer tap on unbound view");
68            return;
69        }
70        final MessageHeaderItem headerItem = mFooterItem.getLastMessageHeaderItem();
71        if (headerItem == null) {
72            LogUtils.i(LOG_TAG, "ignoring conversation footer tap on null header item");
73            return;
74        }
75        final Message message = headerItem.getMessage();
76        if (message == null) {
77            LogUtils.i(LOG_TAG, "ignoring conversation footer tap on null message");
78            return;
79        }
80        final int id = v.getId();
81        if (id == R.id.reply_button) {
82            ComposeActivity.reply(getContext(), getAccount(), message);
83        } else if (id == R.id.reply_all_button) {
84            ComposeActivity.replyAll(getContext(), getAccount(), message);
85        } else if (id == R.id.forward_button) {
86            ComposeActivity.forward(getContext(), getAccount(), message);
87        }
88    }
89
90    public void bind(ConversationFooterItem footerItem) {
91        mFooterItem = footerItem;
92
93        if (mFooterItem == null) {
94            LogUtils.i(LOG_TAG, "ignoring conversation footer tap on unbound view");
95            return;
96        }
97        final MessageHeaderItem headerItem = mFooterItem.getLastMessageHeaderItem();
98        if (headerItem == null) {
99            LogUtils.i(LOG_TAG, "ignoring conversation footer tap on null header item");
100            return;
101        }
102        final Message message = headerItem.getMessage();
103        if (message == null) {
104            LogUtils.i(LOG_TAG, "ignoring conversation footer tap on null message");
105            return;
106        }
107
108        // hide the footer icons
109        mFooterButtons.setVisibility(message.isDraft() ? GONE : VISIBLE);
110    }
111
112    public void rebind(ConversationFooterItem footerItem) {
113        bind(footerItem);
114
115        if (mFooterItem != null) {
116            final int h = measureHeight();
117            if (mFooterItem.setHeight(h)) {
118                mCallbacks.onConversationFooterHeightChange(h);
119            }
120        }
121    }
122
123    private int measureHeight() {
124        ViewGroup parent = (ViewGroup) getParent();
125        if (parent == null) {
126            LogUtils.e(LOG_TAG, "Unable to measure height of conversation header");
127            return getHeight();
128        }
129        final int h = Utils.measureViewHeight(this, parent);
130        return h;
131    }
132
133    public void setAccountController(ConversationAccountController accountController) {
134        mAccountController = accountController;
135    }
136
137    public void setConversationFooterCallbacks(ConversationFooterCallbacks callbacks) {
138        mCallbacks = callbacks;
139    }
140
141    private Account getAccount() {
142        return mAccountController != null ? mAccountController.getAccount() : null;
143    }
144}
145