summaryrefslogtreecommitdiffstats
path: root/src/com/android/messaging/ui/AttachmentPreview.java
blob: 7eea14b1bf7a3ae984b9795c2616e377975fc67b (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
 * Copyright (C) 2015 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.messaging.ui;

import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Rect;
import android.os.Handler;
import android.os.Looper;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ScrollView;

import com.android.messaging.R;
import com.android.messaging.annotation.VisibleForAnimation;
import com.android.messaging.datamodel.data.DraftMessageData;
import com.android.messaging.datamodel.data.MediaPickerMessagePartData;
import com.android.messaging.datamodel.data.MessagePartData;
import com.android.messaging.datamodel.data.PendingAttachmentData;
import com.android.messaging.ui.MultiAttachmentLayout.OnAttachmentClickListener;
import com.android.messaging.ui.animation.PopupTransitionAnimation;
import com.android.messaging.ui.conversation.ComposeMessageView;
import com.android.messaging.ui.conversation.ConversationFragment;
import com.android.messaging.util.Assert;
import com.android.messaging.util.ThreadUtil;
import com.android.messaging.util.UiUtils;

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

public class AttachmentPreview extends ScrollView implements OnAttachmentClickListener {
    private FrameLayout mAttachmentView;
    private ComposeMessageView mComposeMessageView;
    private ImageButton mCloseButton;
    private int mAnimatedHeight = -1;
    private Animator mCloseGapAnimator;
    private boolean mPendingFirstUpdate;
    private Handler mHandler;
    private Runnable mHideRunnable;
    private boolean mPendingHideCanceled;

    private static final int CLOSE_BUTTON_REVEAL_STAGGER_MILLIS = 300;

    public AttachmentPreview(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        mHandler = new Handler(Looper.getMainLooper());
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mCloseButton = (ImageButton) findViewById(R.id.close_button);
        mCloseButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(final View view) {
                mComposeMessageView.clearAttachments();
            }
        });

        mAttachmentView = (FrameLayout) findViewById(R.id.attachment_view);

        // The attachment preview is a scroll view so that it can show the bottom portion of the
        // attachment whenever the space is tight (e.g. when in landscape mode). Per design
        // request we'd like to make the attachment view always scrolled to the bottom.
        addOnLayoutChangeListener(new OnLayoutChangeListener() {
            @Override
            public void onLayoutChange(final View v, final int left, final int top, final int right,
                    final int bottom, final int oldLeft, final int oldTop, final int oldRight,
                    final int oldBottom) {
                post(new Runnable() {
                    @Override
                    public void run() {
                        final int childCount = getChildCount();
                        if (childCount > 0) {
                            final View lastChild = getChildAt(childCount - 1);
                            scrollTo(getScrollX(), lastChild.getBottom() - getHeight());
                        }
                    }
                });
            }
        });
        mPendingFirstUpdate = true;
    }

    public void setComposeMessageView(final ComposeMessageView composeMessageView) {
        mComposeMessageView = composeMessageView;
    }

    @Override
    protected void onMeasure(final int widthMeasureSpec, final int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (mAnimatedHeight >= 0) {
            setMeasuredDimension(getMeasuredWidth(), mAnimatedHeight);
        }
    }

    private void cancelPendingHide() {
        mPendingHideCanceled = true;
    }

    public void hideAttachmentPreview() {
        if (getVisibility() != GONE) {
            UiUtils.revealOrHideViewWithAnimation(mCloseButton, GONE,
                    null /* onFinishRunnable */);
            startCloseGapAnimationOnAttachmentClear();

            if (mAttachmentView.getChildCount() > 0) {
                mPendingHideCanceled = false;
                final View viewToHide = mAttachmentView.getChildCount() > 1 ?
                        mAttachmentView : mAttachmentView.getChildAt(0);
                UiUtils.revealOrHideViewWithAnimation(viewToHide, INVISIBLE,
                        new Runnable() {
                            @Override
                            public void run() {
                                // Only hide if we are didn't get overruled by showing
                                if (!mPendingHideCanceled) {
                                    mAttachmentView.removeAllViews();
                                    setVisibility(GONE);
                                }
                            }
                        });
            } else {
                mAttachmentView.removeAllViews();
                setVisibility(GONE);
            }
        }
    }

    // returns true if we have attachments
    public boolean onAttachmentsChanged(final DraftMessageData draftMessageData) {
        final boolean isFirstUpdate = mPendingFirstUpdate;
        final List<MessagePartData> attachments = draftMessageData.getReadOnlyAttachments();
        final List<PendingAttachmentData> pendingAttachments =
                draftMessageData.getReadOnlyPendingAttachments();

        // Any change in attachments would invalidate the animated height animation.
        cancelCloseGapAnimation();
        mPendingFirstUpdate = false;

        final int combinedAttachmentCount = attachments.size() + pendingAttachments.size();
        mCloseButton.setContentDescription(getResources()
                .getQuantityString(R.plurals.attachment_preview_close_content_description,
                        combinedAttachmentCount));
        if (combinedAttachmentCount == 0) {
            mHideRunnable = new Runnable() {
                @Override
                public void run() {
                    mHideRunnable = null;
                    // Only start the hiding if there are still no attachments
                    if (attachments.size() + pendingAttachments.size() == 0) {
                        hideAttachmentPreview();
                    }
                }
            };
            if (draftMessageData.isSending()) {
                // Wait to hide until the message is ready to start animating
                // We'll execute immediately when the animation triggers
                mHandler.postDelayed(mHideRunnable,
                        ConversationFragment.MESSAGE_ANIMATION_MAX_WAIT);
            } else {
                // Run immediately when clearing attachments
                mHideRunnable.run();
            }
            return false;
        }

        cancelPendingHide();  // We're showing
        if (getVisibility() != VISIBLE) {
            setVisibility(VISIBLE);
            mAttachmentView.setVisibility(VISIBLE);

            // Don't animate in the close button if this is the first update after view creation.
            // This is the initial draft load from database for pre-existing drafts.
            if (!isFirstUpdate) {
                // Reveal the close button after the view animates in.
                mCloseButton.setVisibility(INVISIBLE);
                ThreadUtil.getMainThreadHandler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        UiUtils.revealOrHideViewWithAnimation(mCloseButton, VISIBLE,
                                null /* onFinishRunnable */);
                    }
                }, UiUtils.MEDIAPICKER_TRANSITION_DURATION + CLOSE_BUTTON_REVEAL_STAGGER_MILLIS);
            }
        }

        // Merge the pending attachment list with real attachment.  Design would prefer these be
        // in LIFO order user can see added images past the 5th one but we also want them to be in
        // order and we want it to be WYSIWYG.
        final List<MessagePartData> combinedAttachments = new ArrayList<>();
        combinedAttachments.addAll(attachments);
        combinedAttachments.addAll(pendingAttachments);

        final LayoutInflater layoutInflater = LayoutInflater.from(getContext());
        if (combinedAttachmentCount > 1) {
            MultiAttachmentLayout multiAttachmentLayout = null;
            Rect transitionRect = null;
            if (mAttachmentView.getChildCount() > 0) {
                final View firstChild = mAttachmentView.getChildAt(0);
                if (firstChild instanceof MultiAttachmentLayout) {
                    Assert.equals(1, mAttachmentView.getChildCount());
                    multiAttachmentLayout = (MultiAttachmentLayout) firstChild;
                    multiAttachmentLayout.bindAttachments(combinedAttachments,
                            null /* transitionRect */, combinedAttachmentCount);
                } else {
                    transitionRect = new Rect(firstChild.getLeft(), firstChild.getTop(),
                            firstChild.getRight(), firstChild.getBottom());
                }
            }
            if (multiAttachmentLayout == null) {
                multiAttachmentLayout = AttachmentPreviewFactory.createMultiplePreview(
                        getContext(), this);
                multiAttachmentLayout.bindAttachments(combinedAttachments, transitionRect,
                        combinedAttachmentCount);
                mAttachmentView.removeAllViews();
                mAttachmentView.addView(multiAttachmentLayout);
            }
        } else {
            final MessagePartData attachment = combinedAttachments.get(0);
            boolean shouldAnimate = true;
            if (mAttachmentView.getChildCount() > 0) {
                // If we are going from N->1 attachments, try to use the current bounds
                // bounds as the starting rect.
                shouldAnimate = false;
                final View firstChild = mAttachmentView.getChildAt(0);
                if (firstChild instanceof MultiAttachmentLayout &&
                        attachment instanceof MediaPickerMessagePartData) {
                    final View leftoverView = ((MultiAttachmentLayout) firstChild)
                            .findViewForAttachment(attachment);
                    if (leftoverView != null) {
                        final Rect currentRect = UiUtils.getMeasuredBoundsOnScreen(leftoverView);
                        if (!currentRect.isEmpty() &&
                                attachment instanceof MediaPickerMessagePartData) {
                            ((MediaPickerMessagePartData) attachment).setStartRect(currentRect);
                            shouldAnimate = true;
                        }
                    }
                }
            }
            mAttachmentView.removeAllViews();
            final View attachmentView = AttachmentPreviewFactory.createAttachmentPreview(
                    layoutInflater, attachment, mAttachmentView,
                    AttachmentPreviewFactory.TYPE_SINGLE, true /* startImageRequest */, this);
            if (attachmentView != null) {
                mAttachmentView.addView(attachmentView);
                if (shouldAnimate) {
                    tryAnimateViewIn(attachment, attachmentView);
                }
            }
        }
        return true;
    }

    public void onMessageAnimationStart() {
        if (mHideRunnable == null) {
            return;
        }

        // Run the hide animation at the same time as the message animation
        mHandler.removeCallbacks(mHideRunnable);
        setVisibility(View.INVISIBLE);
        mHideRunnable.run();
    }

    static void tryAnimateViewIn(final MessagePartData attachmentData, final View view) {
        if (attachmentData instanceof MediaPickerMessagePartData) {
            final Rect startRect = ((MediaPickerMessagePartData) attachmentData).getStartRect();
            new PopupTransitionAnimation(startRect, view).startAfterLayoutComplete();
        }
    }

    @VisibleForAnimation
    public void setAnimatedHeight(final int animatedHeight) {
        if (mAnimatedHeight != animatedHeight) {
            mAnimatedHeight = animatedHeight;
            requestLayout();
        }
    }

    /**
     * Kicks off an animation to animate the layout change for closing the gap between the
     * message list and the compose message box when the attachments are cleared.
     */
    private void startCloseGapAnimationOnAttachmentClear() {
        // Cancel existing animation.
        cancelCloseGapAnimation();
        mCloseGapAnimator = ObjectAnimator.ofInt(this, "animatedHeight", getHeight(), 0);
        mCloseGapAnimator.start();
    }

    private void cancelCloseGapAnimation() {
        if (mCloseGapAnimator != null) {
            mCloseGapAnimator.cancel();
            mCloseGapAnimator = null;
        }
        mAnimatedHeight = -1;
    }

    @Override
    public boolean onAttachmentClick(final MessagePartData attachment,
            final Rect viewBoundsOnScreen, final boolean longPress) {
        if (longPress) {
            mComposeMessageView.onAttachmentPreviewLongClicked();
            return true;
        }

        if (!(attachment instanceof PendingAttachmentData) && attachment.isImage()) {
            mComposeMessageView.displayPhoto(attachment.getContentUri(), viewBoundsOnScreen);
            return true;
        }
        return false;
    }
}