1/*
2 * Copyright (C) 2008 Esmertec AG.
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 *      http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18package com.android.mms.ui;
19
20import java.util.Map;
21
22import android.content.Context;
23import android.content.res.Resources;
24import android.graphics.Bitmap;
25import android.media.AudioManager;
26import android.media.MediaPlayer;
27import android.media.MediaPlayer.OnCompletionListener;
28import android.media.MediaPlayer.OnErrorListener;
29import android.net.Uri;
30import android.util.AttributeSet;
31import android.util.Log;
32import android.widget.LinearLayout;
33import android.widget.TextView;
34
35import com.android.mms.LogTag;
36import com.android.mms.R;
37
38/**
39 * This class provides an embedded editor/viewer of audio attachment.
40 */
41public class AudioAttachmentView extends LinearLayout implements
42        SlideViewInterface {
43    private static final String TAG = LogTag.TAG;
44
45    private final Resources mRes;
46    private TextView mNameView;
47    private TextView mAlbumView;
48    private TextView mArtistView;
49    private TextView mErrorMsgView;
50    private Uri mAudioUri;
51    private MediaPlayer mMediaPlayer;
52    private boolean mIsPlaying;
53
54    public AudioAttachmentView(Context context) {
55        super(context);
56        mRes = context.getResources();
57    }
58
59    public AudioAttachmentView(Context context, AttributeSet attrs) {
60        super(context, attrs);
61        mRes = context.getResources();
62    }
63
64    @Override
65    protected void onFinishInflate() {
66        mNameView = (TextView) findViewById(R.id.audio_name);
67        mAlbumView = (TextView) findViewById(R.id.album_name);
68        mArtistView = (TextView) findViewById(R.id.artist_name);
69        mErrorMsgView = (TextView) findViewById(R.id.audio_error_msg);
70    }
71
72    private void onPlaybackError() {
73        Log.e(TAG, "Error occurred while playing audio.");
74        showErrorMessage(mRes.getString(R.string.cannot_play_audio));
75        stopAudio();
76    }
77
78    private void cleanupMediaPlayer() {
79        if (mMediaPlayer != null) {
80            try {
81                mMediaPlayer.stop();
82                mMediaPlayer.release();
83            } finally {
84                mMediaPlayer = null;
85            }
86        }
87    }
88
89    synchronized public void startAudio() {
90        if (!mIsPlaying && (mAudioUri != null)) {
91            mMediaPlayer = MediaPlayer.create(mContext, mAudioUri);
92            if (mMediaPlayer != null) {
93                mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
94                mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
95                    public void onCompletion(MediaPlayer mp) {
96                        stopAudio();
97                    }
98                });
99                mMediaPlayer.setOnErrorListener(new OnErrorListener() {
100                    public boolean onError(MediaPlayer mp, int what, int extra) {
101                        onPlaybackError();
102                        return true;
103                    }
104                });
105
106                mIsPlaying = true;
107                mMediaPlayer.start();
108            }
109        }
110    }
111
112    public void startVideo() {
113        // TODO Auto-generated method stub
114
115    }
116
117    public void setAudio(Uri audio, String name, Map<String, ?> extras) {
118        synchronized (this) {
119            mAudioUri = audio;
120        }
121
122        mNameView.setText(name);
123        mAlbumView.setText((String) extras.get("album"));
124        mArtistView.setText((String) extras.get("artist"));
125    }
126
127    public void setImage(String name, Bitmap bitmap) {
128        // TODO Auto-generated method stub
129
130    }
131
132    public void setImageRegionFit(String fit) {
133        // TODO Auto-generated method stub
134
135    }
136
137    public void setImageVisibility(boolean visible) {
138        // TODO Auto-generated method stub
139
140    }
141
142    public void setText(String name, String text) {
143        // TODO Auto-generated method stub
144
145    }
146
147    public void setTextVisibility(boolean visible) {
148        // TODO Auto-generated method stub
149
150    }
151
152    public void setVideo(String name, Uri video) {
153        // TODO Auto-generated method stub
154
155    }
156
157    public void setVideoThumbnail(String name, Bitmap bitmap) {
158    }
159
160    public void setVideoVisibility(boolean visible) {
161        // TODO Auto-generated method stub
162
163    }
164
165    synchronized public void stopAudio() {
166        try {
167            cleanupMediaPlayer();
168        } finally {
169            mIsPlaying = false;
170        }
171    }
172
173    public void stopVideo() {
174        // TODO Auto-generated method stub
175
176    }
177
178    public void reset() {
179        synchronized (this) {
180            if (mIsPlaying) {
181                stopAudio();
182            }
183        }
184        mErrorMsgView.setVisibility(GONE);
185    }
186
187    public void setVisibility(boolean visible) {
188        // TODO Auto-generated method stub
189
190    }
191
192    private void showErrorMessage(String msg) {
193        mErrorMsgView.setText(msg);
194        mErrorMsgView.setVisibility(VISIBLE);
195    }
196
197    public void pauseAudio() {
198        // TODO Auto-generated method stub
199
200    }
201
202    public void pauseVideo() {
203        // TODO Auto-generated method stub
204
205    }
206
207    public void seekAudio(int seekTo) {
208        // TODO Auto-generated method stub
209
210    }
211
212    public void seekVideo(int seekTo) {
213        // TODO Auto-generated method stub
214
215    }
216}
217