summaryrefslogtreecommitdiff
path: root/src/com/android/messaging/ui/AudioAttachmentView.java
blob: ad91ed26286ee86f90a1c3e90db95d1a212bb4a1 (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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
 * 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.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.RectF;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.android.messaging.Factory;
import com.android.messaging.R;
import com.android.messaging.datamodel.data.MessagePartData;
import com.android.messaging.ui.mediapicker.PausableChronometer;
import com.android.messaging.util.Assert;
import com.android.messaging.util.ContentType;
import com.android.messaging.util.LogUtil;
import com.android.messaging.util.MediaUtil;
import com.android.messaging.util.UiUtils;

/**
 * A reusable widget that hosts an audio player for audio attachment playback. This widget is used
 * by both the media picker and the conversation message view to show audio attachments.
 */
public class AudioAttachmentView extends LinearLayout {
    /** The normal layout mode where we have the play button, timer and progress bar */
    private static final int LAYOUT_MODE_NORMAL = 0;

    /** The compact layout mode with only the play button and the timer beneath it. Suitable
     *  for displaying in limited space such as multi-attachment layout */
    private static final int LAYOUT_MODE_COMPACT = 1;

    /** The sub-compact layout mode with only the play button. */
    private static final int LAYOUT_MODE_SUB_COMPACT = 2;

    private static final int PLAY_BUTTON = 0;
    private static final int PAUSE_BUTTON = 1;

    private AudioAttachmentPlayPauseButton mPlayPauseButton;
    private PausableChronometer mChronometer;
    private AudioPlaybackProgressBar mProgressBar;
    private MediaPlayer mMediaPlayer;

    private Uri mDataSourceUri;

    // The corner radius for drawing rounded corners. The default value is zero (no rounded corners)
    private final int mCornerRadius;
    private final Path mRoundedCornerClipPath;
    private int mClipPathWidth;
    private int mClipPathHeight;

    private boolean mUseIncomingStyle;
    private int mThemeColor;

    private boolean mStartPlayAfterPrepare;
    // should the MediaPlayer be prepared lazily when the user chooses to play the audio (as
    // opposed to preparing it early, on bind)
    private boolean mPrepareOnPlayback;
    private boolean mPrepared;
    private boolean mPlaybackFinished; // Was the audio played all the way to the end
    private final int mMode;

    public AudioAttachmentView(final Context context, final AttributeSet attrs) {
        super(context, attrs);
        final TypedArray typedAttributes =
                context.obtainStyledAttributes(attrs, R.styleable.AudioAttachmentView);
        mMode = typedAttributes.getInt(R.styleable.AudioAttachmentView_layoutMode,
                LAYOUT_MODE_NORMAL);
        final LayoutInflater inflater = LayoutInflater.from(getContext());
        inflater.inflate(R.layout.audio_attachment_view, this, true);
        typedAttributes.recycle();

        setWillNotDraw(mMode != LAYOUT_MODE_SUB_COMPACT);
        mRoundedCornerClipPath = new Path();
        mCornerRadius = context.getResources().getDimensionPixelSize(
                R.dimen.conversation_list_image_preview_corner_radius);
        setContentDescription(context.getString(R.string.audio_attachment_content_description));
    }

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

        mPlayPauseButton = (AudioAttachmentPlayPauseButton) findViewById(R.id.play_pause_button);
        mChronometer = (PausableChronometer) findViewById(R.id.timer);
        mProgressBar = (AudioPlaybackProgressBar) findViewById(R.id.progress);
        mPlayPauseButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(final View v) {
                // Has the MediaPlayer already been prepared?
                if (mMediaPlayer != null && mPrepared) {
                    if (mMediaPlayer.isPlaying()) {
                        mMediaPlayer.pause();
                        mChronometer.pause();
                        mProgressBar.pause();
                    } else {
                        playAudio();
                    }
                } else {
                    // Either eager preparation is still going on (the user must have clicked
                    // the Play button immediately after the view is bound) or this is lazy
                    // preparation.
                    if (mStartPlayAfterPrepare) {
                        // The user is (starting and) pausing before the MediaPlayer is prepared
                        mStartPlayAfterPrepare = false;
                    } else {
                        mStartPlayAfterPrepare = true;
                        setupMediaPlayer();
                    }
                }
                updatePlayPauseButtonState();
            }
        });
        updatePlayPauseButtonState();
        initializeViewsForMode();
    }

    private void updateChronometerVisibility(final boolean playing) {
        if (mChronometer.getVisibility() == View.GONE) {
            // The chronometer is always GONE for LAYOUT_MODE_SUB_COMPACT
            Assert.equals(LAYOUT_MODE_SUB_COMPACT, mMode);
            return;
        }

        if (mPrepareOnPlayback) {
            // For lazy preparation, the chronometer will only be shown during playback
            mChronometer.setVisibility(playing ? View.VISIBLE : View.INVISIBLE);
        } else {
            mChronometer.setVisibility(View.VISIBLE);
        }
    }

    /**
     * Bind the audio attachment view with a MessagePartData.
     * @param incoming indicates whether the attachment view is to be styled as a part of an
     *        incoming message.
     */
    public void bindMessagePartData(final MessagePartData messagePartData,
            final boolean incoming, final boolean showAsSelected) {
        Assert.isTrue(messagePartData == null ||
                ContentType.isAudioType(messagePartData.getContentType()));
        final Uri contentUri = (messagePartData == null) ? null : messagePartData.getContentUri();
        bind(contentUri, incoming, showAsSelected);
    }

    public void bind(
            final Uri dataSourceUri, final boolean incoming, final boolean showAsSelected) {
        final String currentUriString = (mDataSourceUri == null) ? "" : mDataSourceUri.toString();
        final String newUriString = (dataSourceUri == null) ? "" : dataSourceUri.toString();
        final int themeColor = ConversationDrawables.get().getConversationThemeColor();
        final boolean useIncomingStyle = incoming || showAsSelected;
        final boolean visualStyleChanged = mThemeColor != themeColor ||
                mUseIncomingStyle != useIncomingStyle;

        mUseIncomingStyle = useIncomingStyle;
        mThemeColor = themeColor;
        mPrepareOnPlayback = incoming && !MediaUtil.canAutoAccessIncomingMedia();

        if (!TextUtils.equals(currentUriString, newUriString)) {
            mDataSourceUri = dataSourceUri;
            resetToZeroState();
        } else if (visualStyleChanged) {
            updateVisualStyle();
        }
    }

    private void playAudio() {
        Assert.notNull(mMediaPlayer);
        if (mPlaybackFinished) {
            mMediaPlayer.seekTo(0);
            mChronometer.restart();
            mProgressBar.restart();
            mPlaybackFinished = false;
        } else {
            mChronometer.resume();
            mProgressBar.resume();
        }
        mMediaPlayer.start();
    }

    private void onAudioReplayError(final int what, final int extra, final Exception exception) {
        if (exception == null) {
            LogUtil.e(LogUtil.BUGLE_TAG, "audio replay failed, what=" + what +
                    ", extra=" + extra);
        } else {
            LogUtil.e(LogUtil.BUGLE_TAG, "audio replay failed, exception=" + exception);
        }
        UiUtils.showToastAtBottom(R.string.audio_recording_replay_failed);
        releaseMediaPlayer();
    }

    /**
     * Prepare the MediaPlayer, and if mPrepareOnPlayback, start playing the audio
     */
    private void setupMediaPlayer() {
        Assert.notNull(mDataSourceUri);
        if (mMediaPlayer == null) {
            Assert.isTrue(!mPrepared);
            mMediaPlayer = new MediaPlayer();

            try {
                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                mMediaPlayer.setDataSource(Factory.get().getApplicationContext(), mDataSourceUri);
                mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
                    @Override
                    public void onCompletion(final MediaPlayer mp) {
                        updatePlayPauseButtonState();
                        mChronometer.reset();
                        mChronometer.setBase(SystemClock.elapsedRealtime() -
                                mMediaPlayer.getDuration());
                        updateChronometerVisibility(false /* playing */);
                        mProgressBar.reset();

                        mPlaybackFinished = true;
                    }
                });

                mMediaPlayer.setOnPreparedListener(new OnPreparedListener() {
                    @Override
                    public void onPrepared(final MediaPlayer mp) {
                        // Set base on the chronometer so we can show the full length of the audio.
                        mChronometer.setBase(SystemClock.elapsedRealtime() -
                                mMediaPlayer.getDuration());
                        mProgressBar.setDuration(mMediaPlayer.getDuration());
                        mMediaPlayer.seekTo(0);
                        mPrepared = true;

                        if (mStartPlayAfterPrepare) {
                            mStartPlayAfterPrepare = false;
                            playAudio();
                            updatePlayPauseButtonState();
                        }
                    }
                });

                mMediaPlayer.setOnErrorListener(new OnErrorListener() {
                    @Override
                    public boolean onError(final MediaPlayer mp, final int what, final int extra) {
                        mStartPlayAfterPrepare = false;
                        onAudioReplayError(what, extra, null);
                        return true;
                    }
                });

                mMediaPlayer.prepareAsync();
            } catch (final Exception exception) {
                onAudioReplayError(0, 0, exception);
                releaseMediaPlayer();
            }
        }
    }

    private void releaseMediaPlayer() {
        if (mMediaPlayer != null) {
            mMediaPlayer.release();
            mMediaPlayer = null;
            mPrepared = false;
            mStartPlayAfterPrepare = false;
            mPlaybackFinished = false;
            mChronometer.reset();
            mProgressBar.reset();
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        // The view must have scrolled off. Stop playback.
        releaseMediaPlayer();
    }

    @Override
    protected void onDraw(final Canvas canvas) {
        if (mMode != LAYOUT_MODE_SUB_COMPACT) {
            return;
        }

        final int currentWidth = this.getWidth();
        final int currentHeight = this.getHeight();
        if (mClipPathWidth != currentWidth || mClipPathHeight != currentHeight) {
            final RectF rect = new RectF(0, 0, currentWidth, currentHeight);
            mRoundedCornerClipPath.reset();
            mRoundedCornerClipPath.addRoundRect(rect, mCornerRadius, mCornerRadius,
                    Path.Direction.CW);
            mClipPathWidth = currentWidth;
            mClipPathHeight = currentHeight;
        }

        canvas.clipPath(mRoundedCornerClipPath);
        super.onDraw(canvas);
    }

    private void updatePlayPauseButtonState() {
        final boolean playing = mMediaPlayer != null && mMediaPlayer.isPlaying();
        updateChronometerVisibility(playing);
        if (mStartPlayAfterPrepare || playing) {
            mPlayPauseButton.setDisplayedChild(PAUSE_BUTTON);
        } else {
            mPlayPauseButton.setDisplayedChild(PLAY_BUTTON);
        }
    }

    private void resetToZeroState() {
        // Release the media player so it may be set up with the new audio source.
        releaseMediaPlayer();
        updateVisualStyle();
        updateChronometerVisibility(false /* playing */);

        if (mDataSourceUri != null && !mPrepareOnPlayback) {
            // Prepare the media player, so we can read the duration of the audio.
            setupMediaPlayer();
        }
    }

    private void updateVisualStyle() {
        if (mMode == LAYOUT_MODE_SUB_COMPACT) {
            // Sub-compact mode has static visual appearance already set up during initialization.
            return;
        }

        if (mUseIncomingStyle) {
            mChronometer.setTextColor(getResources().getColor(R.color.message_text_color_incoming));
        } else {
            mChronometer.setTextColor(getResources().getColor(R.color.message_text_color_outgoing));
        }
        mProgressBar.setVisualStyle(mUseIncomingStyle);
        mPlayPauseButton.setVisualStyle(mUseIncomingStyle);
        updatePlayPauseButtonState();
    }

    private void initializeViewsForMode() {
        switch (mMode) {
            case LAYOUT_MODE_NORMAL:
                setOrientation(HORIZONTAL);
                mProgressBar.setVisibility(VISIBLE);
                break;

            case LAYOUT_MODE_COMPACT:
                setOrientation(VERTICAL);
                mProgressBar.setVisibility(GONE);
                ((MarginLayoutParams) mPlayPauseButton.getLayoutParams()).setMargins(0, 0, 0, 0);
                ((MarginLayoutParams) mChronometer.getLayoutParams()).setMargins(0, 0, 0, 0);
                break;

            case LAYOUT_MODE_SUB_COMPACT:
                setOrientation(VERTICAL);
                mProgressBar.setVisibility(GONE);
                mChronometer.setVisibility(GONE);
                ((MarginLayoutParams) mPlayPauseButton.getLayoutParams()).setMargins(0, 0, 0, 0);
                final ImageView playButton = (ImageView) findViewById(R.id.play_button);
                playButton.setImageDrawable(
                        getResources().getDrawable(R.drawable.ic_preview_play));
                final ImageView pauseButton = (ImageView) findViewById(R.id.pause_button);
                pauseButton.setImageDrawable(
                        getResources().getDrawable(R.drawable.ic_preview_pause));
                break;

            default:
                Assert.fail("Unsupported mode for AudioAttachmentView!");
                break;
        }
    }
}