summaryrefslogtreecommitdiffstats
path: root/src/com/android/mail/browse/MessageFooterView.java
blob: f73253fc6903e1876fa1a20c43f52217e63c1449 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * Copyright (C) 2012 Google Inc.
 * Licensed to The Android Open Source Project.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.mail.browse;

import android.app.FragmentManager;
import android.app.LoaderManager;
import android.content.Context;
import android.content.Loader;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.mail.R;
import com.android.mail.browse.AttachmentLoader.AttachmentCursor;
import com.android.mail.browse.ConversationContainer.DetachListener;
import com.android.mail.browse.ConversationViewAdapter.MessageHeaderItem;
import com.android.mail.providers.Attachment;
import com.android.mail.providers.Message;
import com.android.mail.ui.AttachmentTile;
import com.android.mail.ui.AttachmentTileGrid;
import com.android.mail.utils.LogTag;
import com.android.mail.utils.LogUtils;

import com.google.common.base.Objects;
import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.List;

public class MessageFooterView extends LinearLayout implements DetachListener,
        LoaderManager.LoaderCallbacks<Cursor> {

    private MessageHeaderItem mMessageHeaderItem;
    private LoaderManager mLoaderManager;
    private FragmentManager mFragmentManager;
    private AttachmentCursor mAttachmentsCursor;
    private LinearLayout mAttachmentLoadMore;
    private TextView mTitleText;
    private AttachmentTileGrid mAttachmentGrid;
    private LinearLayout mAttachmentBarList;

    private final LayoutInflater mInflater;

    private static final String LOG_TAG = LogTag.getLogTag();

    private Uri mAccountUri;

    public MessageFooterView(Context context) {
        this(context, null);
    }

    public MessageFooterView(Context context, AttributeSet attrs) {
        super(context, attrs);

        mInflater = LayoutInflater.from(context);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        mAttachmentLoadMore = (LinearLayout) findViewById(R.id.attachment_placeholder_load_more);
        mTitleText = (TextView) findViewById(R.id.attachments_header_text);
        mAttachmentGrid = (AttachmentTileGrid) findViewById(R.id.attachment_tile_grid);
        mAttachmentBarList = (LinearLayout) findViewById(R.id.attachment_bar_list);
    }

    public void initialize(LoaderManager loaderManager, FragmentManager fragmentManager) {
        mLoaderManager = loaderManager;
        mFragmentManager = fragmentManager;
    }

    public void bind(MessageHeaderItem headerItem, Uri accountUri, boolean measureOnly) {
        mAccountUri = accountUri;

        // Resets the footer view. This step is only done if the
        // attachmentsListUri changes so that we don't
        // repeat the work of layout and measure when
        // we're only updating the attachments.
        if (mMessageHeaderItem != null &&
                mMessageHeaderItem.getMessage() != null &&
                mMessageHeaderItem.getMessage().attachmentListUri != null) {
            mAttachmentLoadMore.removeAllViewsInLayout();
            mAttachmentGrid.removeAllViewsInLayout();
            mAttachmentBarList.removeAllViewsInLayout();
            mAttachmentLoadMore.setVisibility(View.GONE);
            mTitleText.setVisibility(View.GONE);
            mAttachmentGrid.setVisibility(View.GONE);
            mAttachmentBarList.setVisibility(View.GONE);
        }

        // If this MessageFooterView is being bound to a new attachment, we need to unbind with the
        // old loader
        final Integer oldAttachmentLoaderId = getAttachmentLoaderId();

        mMessageHeaderItem = headerItem;

        final Integer attachmentLoaderId = getAttachmentLoaderId();
        // Destroy the loader if we are attempting to load a different attachment
        if (oldAttachmentLoaderId != null &&
                !Objects.equal(oldAttachmentLoaderId, attachmentLoaderId)) {
            mLoaderManager.destroyLoader(oldAttachmentLoaderId);
        }

        // kick off load of Attachment objects in background thread
        // but don't do any Loader work if we're only measuring
        if (!measureOnly && attachmentLoaderId != null) {
            LogUtils.i(LOG_TAG, "binding footer view, calling initLoader for message %d",
                    attachmentLoaderId);
            mLoaderManager.initLoader(attachmentLoaderId, Bundle.EMPTY, this);
        }

        // Do an initial render if initLoader didn't already do one
        if (mAttachmentLoadMore.getChildCount() == 0
                && mAttachmentGrid.getChildCount() == 0
                && mAttachmentBarList.getChildCount() == 0) {
            renderAttachments(false);
        }
        setVisibility(mMessageHeaderItem.isExpanded() ? VISIBLE : GONE);
    }

    private void renderAttachments(boolean loaderResult) {
        final List<Attachment> attachments;
        if (mAttachmentsCursor != null && !mAttachmentsCursor.isClosed()) {
            int i = -1;
            attachments = Lists.newArrayList();
            while (mAttachmentsCursor.moveToPosition(++i)) {
                attachments.add(mAttachmentsCursor.get());
            }
        } else {
            // before the attachment loader results are in, we can still render immediately using
            // the basic info in the message's attachmentsJSON
            attachments = mMessageHeaderItem.getMessage().getAttachments();
        }
        renderAttachments(attachments, loaderResult);
    }

    private void renderAttachments(List<Attachment> attachments, boolean loaderResult) {
        if (attachments == null || attachments.isEmpty()) {
            return;
        }

        // filter the attachments into tiled and non-tiled
        final int maxSize = attachments.size();
        Attachment loadMore = null;
        final List<Attachment> tiledAttachments = new ArrayList<Attachment>(maxSize);
        final List<Attachment> barAttachments = new ArrayList<Attachment>(maxSize);

        for (Attachment attachment : attachments) {
            if (attachment.isInlineAttachment()) {
                LogUtils.d(LOG_TAG, "attachment(" + attachment.contentUri
                        + ") is inline attachment. Ignore and do not show it!");
                continue;
            }

            if (attachment.isLoadMore()) {
                loadMore = attachment;
                loadMore.messageLoadMoreUri = mMessageHeaderItem.getMessage().loadMoreUri;
            } else if (AttachmentTile.isTiledAttachment(attachment)) {
                tiledAttachments.add(attachment);
            } else {
                barAttachments.add(attachment);
            }
        }
        mMessageHeaderItem.getMessage().attachmentsJson = Attachment.toJSONArray(attachments);

        if (tiledAttachments.size() > 0 || barAttachments.size() > 0) {
            // If there isn't any tiled attachment or bar attachment,
            // then we needn't to show the title.
            mTitleText.setVisibility(View.VISIBLE);
        }

        renderLoadMore(loadMore, loaderResult);
        renderTiledAttachments(tiledAttachments, loaderResult);
        renderBarAttachments(barAttachments, loaderResult);
    }

    private void renderLoadMore(Attachment loadMore, boolean loaderResult) {
        if (loadMore == null) return;

        mAttachmentLoadMore.setVisibility(View.VISIBLE);
        renderAttachment(mAttachmentLoadMore, loadMore, loaderResult);
    }

    private void renderTiledAttachments(List<Attachment> tiledAttachments, boolean loaderResult) {
        mAttachmentGrid.setVisibility(View.VISIBLE);

        // Setup the tiles.
        mAttachmentGrid.configureGrid(mFragmentManager,
                mMessageHeaderItem.getMessage().attachmentListUri, tiledAttachments, loaderResult);
    }

    private void renderBarAttachments(List<Attachment> barAttachments, boolean loaderResult) {
        mAttachmentBarList.setVisibility(View.VISIBLE);

        for (Attachment attachment : barAttachments) {
            renderAttachment(mAttachmentBarList, attachment, loaderResult);
        }
    }

    private void renderAttachment(LinearLayout parentView, Attachment attachment,
            boolean loaderResult) {
        final Uri id = attachment.getIdentifierUri();
        MessageAttachmentBar barAttachmentView =
                (MessageAttachmentBar) parentView.findViewWithTag(id);

        if (barAttachmentView == null) {
            barAttachmentView = MessageAttachmentBar.inflate(mInflater, this);
            barAttachmentView.setTag(id);
            barAttachmentView.initialize(mFragmentManager);
            parentView.addView(barAttachmentView);
        }

        barAttachmentView.render(attachment, mAccountUri, loaderResult);
    }

    private Integer getAttachmentLoaderId() {
        Integer id = null;
        final Message msg = mMessageHeaderItem == null ? null : mMessageHeaderItem.getMessage();
        if (msg != null && msg.hasAttachments && msg.attachmentListUri != null) {
            id = msg.attachmentListUri.hashCode();
        }
        return id;
    }

    @Override
    public void onDetachedFromParent() {
        // Do nothing
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return new AttachmentLoader(getContext(),
                mMessageHeaderItem.getMessage().attachmentListUri);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        mAttachmentsCursor = (AttachmentCursor) data;

        if (mAttachmentsCursor == null || mAttachmentsCursor.isClosed()) {
            return;
        }

        renderAttachments(true);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        mAttachmentsCursor = null;
    }
}