AudioPreview.java revision bc7fa6597f72c1ddeae6b6a2f150140d84319ebf
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.music;
18
19import android.app.Activity;
20import android.content.AsyncQueryHandler;
21import android.content.ContentResolver;
22import android.content.Context;
23import android.content.Intent;
24import android.database.Cursor;
25import android.media.AudioManager;
26import android.media.MediaPlayer;
27import android.media.AudioManager.OnAudioFocusChangeListener;
28import android.media.MediaPlayer.OnCompletionListener;
29import android.media.MediaPlayer.OnErrorListener;
30import android.media.MediaPlayer.OnPreparedListener;
31import android.net.Uri;
32import android.os.Bundle;
33import android.os.Handler;
34import android.provider.MediaStore;
35import android.provider.OpenableColumns;
36import android.text.TextUtils;
37import android.util.Log;
38import android.view.KeyEvent;
39import android.view.Menu;
40import android.view.MenuItem;
41import android.view.View;
42import android.view.Window;
43import android.view.WindowManager;
44import android.widget.ImageButton;
45import android.widget.ProgressBar;
46import android.widget.SeekBar;
47import android.widget.TextView;
48import android.widget.SeekBar.OnSeekBarChangeListener;
49import android.widget.Toast;
50
51import java.io.IOException;
52
53public class AudioPreview extends Activity implements OnPreparedListener, OnErrorListener, OnCompletionListener
54{
55    private final static String TAG = "AudioPreview";
56    private PreviewPlayer mPlayer;
57    private TextView mTextLine1;
58    private TextView mTextLine2;
59    private TextView mLoadingText;
60    private SeekBar mSeekBar;
61    private Handler mProgressRefresher;
62    private boolean mSeeking = false;
63    private int mDuration;
64    private Uri mUri;
65    private long mMediaId = -1;
66    private static final int OPEN_IN_MUSIC = 1;
67    private AudioManager mAudioManager;
68    private boolean mPausedByTransientLossOfFocus;
69
70    @Override
71    public void onCreate(Bundle icicle) {
72        super.onCreate(icicle);
73
74        Intent intent = getIntent();
75        if (intent == null) {
76            finish();
77            return;
78        }
79        mUri = intent.getData();
80        if (mUri == null) {
81            finish();
82            return;
83        }
84        String scheme = mUri.getScheme();
85
86        setVolumeControlStream(AudioManager.STREAM_MUSIC);
87        requestWindowFeature(Window.FEATURE_NO_TITLE);
88        setContentView(R.layout.audiopreview);
89        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
90                WindowManager.LayoutParams.WRAP_CONTENT);
91
92        mTextLine1 = (TextView) findViewById(R.id.line1);
93        mTextLine2 = (TextView) findViewById(R.id.line2);
94        mLoadingText = (TextView) findViewById(R.id.loading);
95        if (scheme.equals("http")) {
96            String msg = getString(R.string.streamloadingtext, mUri.getHost());
97            mLoadingText.setText(msg);
98        } else {
99            mLoadingText.setVisibility(View.GONE);
100        }
101        mSeekBar = (SeekBar) findViewById(R.id.progress);
102        mProgressRefresher = new Handler();
103        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
104
105        PreviewPlayer player = (PreviewPlayer) getLastNonConfigurationInstance();
106        if (player == null) {
107            mPlayer = new PreviewPlayer();
108            mPlayer.setActivity(this);
109            try {
110                mPlayer.setDataSourceAndPrepare(mUri);
111            } catch (IOException ex) {
112                finish();
113                return;
114            }
115        } else {
116            mPlayer = player;
117            mPlayer.setActivity(this);
118            if (mPlayer.isPrepared()) {
119                showPostPrepareUI();
120            }
121        }
122
123        AsyncQueryHandler mAsyncQueryHandler = new AsyncQueryHandler(getContentResolver()) {
124            @Override
125            protected void onQueryComplete(int token, Object cookie, Cursor cursor) {
126                if (cursor != null && cursor.moveToFirst()) {
127
128                    int titleIdx = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE);
129                    int artistIdx = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST);
130                    int idIdx = cursor.getColumnIndex(MediaStore.Audio.Media._ID);
131                    int displaynameIdx = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
132
133                    if (idIdx >=0) {
134                        mMediaId = cursor.getLong(idIdx);
135                    }
136
137                    if (titleIdx >= 0) {
138                        String title = cursor.getString(titleIdx);
139                        mTextLine1.setText(title);
140                        if (artistIdx >= 0) {
141                            String artist = cursor.getString(artistIdx);
142                            mTextLine2.setText(artist);
143                        }
144                    } else if (displaynameIdx >= 0) {
145                        String name = cursor.getString(displaynameIdx);
146                        mTextLine1.setText(name);
147                    } else {
148                        // Couldn't find anything to display, what to do now?
149                        Log.w(TAG, "Cursor had no names for us");
150                    }
151                } else {
152                    Log.w(TAG, "empty cursor");
153                }
154
155                if (cursor != null) {
156                    cursor.close();
157                }
158                setNames();
159            }
160        };
161
162        if (scheme.equals(ContentResolver.SCHEME_CONTENT)) {
163            if (mUri.getAuthority() == MediaStore.AUTHORITY) {
164                // try to get title and artist from the media content provider
165                mAsyncQueryHandler.startQuery(0, null, mUri, new String [] {
166                        MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST},
167                        null, null, null);
168            } else {
169                // Try to get the display name from another content provider.
170                // Don't specifically ask for the display name though, since the
171                // provider might not actually support that column.
172                mAsyncQueryHandler.startQuery(0, null, mUri, null, null, null, null);
173            }
174        } else if (scheme.equals("file")) {
175            // check if this file is in the media database (clicking on a download
176            // in the download manager might follow this path
177            String path = mUri.getPath();
178            mAsyncQueryHandler.startQuery(0, null,  MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
179                    new String [] {MediaStore.Audio.Media._ID,
180                        MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST},
181                    MediaStore.Audio.Media.DATA + "=?", new String [] {path}, null);
182        } else {
183            // We can't get metadata from the file/stream itself yet, because
184            // that API is hidden, so instead we display the URI being played
185            if (mPlayer.isPrepared()) {
186                setNames();
187            }
188        }
189    }
190
191    @Override
192    public Object onRetainNonConfigurationInstance() {
193        PreviewPlayer player = mPlayer;
194        mPlayer = null;
195        return player;
196    }
197
198    @Override
199    public void onDestroy() {
200        stopPlayback();
201        super.onDestroy();
202    }
203
204    private void stopPlayback() {
205        if (mProgressRefresher != null) {
206            mProgressRefresher.removeCallbacksAndMessages(null);
207        }
208        if (mPlayer != null) {
209            mPlayer.release();
210            mPlayer = null;
211            mAudioManager.abandonAudioFocus(mAudioFocusListener);
212        }
213    }
214
215    @Override
216    public void onUserLeaveHint() {
217        stopPlayback();
218        finish();
219        super.onUserLeaveHint();
220    }
221
222    public void onPrepared(MediaPlayer mp) {
223        if (isFinishing()) return;
224        mPlayer = (PreviewPlayer) mp;
225        setNames();
226        showPostPrepareUI();
227    }
228
229    private void showPostPrepareUI() {
230        ProgressBar pb = (ProgressBar) findViewById(R.id.spinner);
231        pb.setVisibility(View.GONE);
232        mDuration = mPlayer.getDuration();
233        if (mDuration != 0) {
234            mSeekBar.setMax(mDuration);
235            mSeekBar.setVisibility(View.VISIBLE);
236        }
237        mSeekBar.setOnSeekBarChangeListener(mSeekListener);
238        mLoadingText.setVisibility(View.GONE);
239        View v = findViewById(R.id.titleandbuttons);
240        v.setVisibility(View.VISIBLE);
241        start(); // because it requests audio focus
242        updatePlayPause();
243    }
244
245    private OnAudioFocusChangeListener mAudioFocusListener = new OnAudioFocusChangeListener() {
246        public void onAudioFocusChange(int focusChange) {
247            if (mPlayer == null) {
248                // this activity has handed its MediaPlayer off to the next activity
249                // (e.g. portrait/landscape switch) and should abandon its focus
250                mAudioManager.abandonAudioFocus(this);
251                return;
252            }
253            switch (focusChange) {
254                case AudioManager.AUDIOFOCUS_LOSS:
255                    mPausedByTransientLossOfFocus = false;
256                    mPlayer.pause();
257                    break;
258                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
259                case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
260                    if (mPlayer.isPlaying()) {
261                        mPausedByTransientLossOfFocus = true;
262                        mPlayer.pause();
263                    }
264                    break;
265                case AudioManager.AUDIOFOCUS_GAIN:
266                    if (mPausedByTransientLossOfFocus) {
267                        mPausedByTransientLossOfFocus = false;
268                        start();
269                    }
270                    break;
271            }
272            updatePlayPause();
273        }
274    };
275
276    private void start() {
277        mAudioManager.requestAudioFocus(mAudioFocusListener, AudioManager.STREAM_MUSIC,
278                AudioManager.AUDIOFOCUS_GAIN_TRANSIENT);
279        mPlayer.start();
280        mProgressRefresher.postDelayed(new ProgressRefresher(), 200);
281    }
282
283    public void setNames() {
284        if (TextUtils.isEmpty(mTextLine1.getText())) {
285            mTextLine1.setText(mUri.getLastPathSegment());
286        }
287        if (TextUtils.isEmpty(mTextLine2.getText())) {
288            mTextLine2.setVisibility(View.GONE);
289        } else {
290            mTextLine2.setVisibility(View.VISIBLE);
291        }
292    }
293
294    class ProgressRefresher implements Runnable {
295
296        public void run() {
297            if (mPlayer != null && !mSeeking && mDuration != 0) {
298                int progress = mPlayer.getCurrentPosition() / mDuration;
299                mSeekBar.setProgress(mPlayer.getCurrentPosition());
300            }
301            mProgressRefresher.removeCallbacksAndMessages(null);
302            mProgressRefresher.postDelayed(new ProgressRefresher(), 200);
303        }
304    }
305
306    private void updatePlayPause() {
307        ImageButton b = (ImageButton) findViewById(R.id.playpause);
308        if (b != null) {
309            if (mPlayer.isPlaying()) {
310                b.setImageResource(R.drawable.btn_playback_ic_pause_small);
311            } else {
312                b.setImageResource(R.drawable.btn_playback_ic_play_small);
313                mProgressRefresher.removeCallbacksAndMessages(null);
314            }
315        }
316    }
317
318    private OnSeekBarChangeListener mSeekListener = new OnSeekBarChangeListener() {
319        public void onStartTrackingTouch(SeekBar bar) {
320            mSeeking = true;
321        }
322        public void onProgressChanged(SeekBar bar, int progress, boolean fromuser) {
323            if (!fromuser) {
324                return;
325            }
326            mPlayer.seekTo(progress);
327        }
328        public void onStopTrackingTouch(SeekBar bar) {
329            mSeeking = false;
330        }
331    };
332
333    public boolean onError(MediaPlayer mp, int what, int extra) {
334        Toast.makeText(this, R.string.playback_failed, Toast.LENGTH_SHORT).show();
335        finish();
336        return true;
337    }
338
339    public void onCompletion(MediaPlayer mp) {
340        mSeekBar.setProgress(mDuration);
341        updatePlayPause();
342    }
343
344    public void playPauseClicked(View v) {
345        if (mPlayer.isPlaying()) {
346            mPlayer.pause();
347        } else {
348            start();
349        }
350        updatePlayPause();
351    }
352
353    @Override
354    public boolean onCreateOptionsMenu(Menu menu) {
355        super.onCreateOptionsMenu(menu);
356        // TODO: if mMediaId != -1, then the playing file has an entry in the media
357        // database, and we could open it in the full music app instead.
358        // Ideally, we would hand off the currently running mediaplayer
359        // to the music UI, which can probably be done via a public static
360        menu.add(0, OPEN_IN_MUSIC, 0, "open in music");
361        return true;
362    }
363
364    @Override
365    public boolean onPrepareOptionsMenu(Menu menu) {
366        MenuItem item = menu.findItem(OPEN_IN_MUSIC);
367        if (mMediaId >= 0) {
368            item.setVisible(true);
369            return true;
370        }
371        item.setVisible(false);
372        return false;
373    }
374
375    @Override
376    public boolean onKeyDown(int keyCode, KeyEvent event) {
377        switch (keyCode) {
378            case KeyEvent.KEYCODE_HEADSETHOOK:
379            case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
380                if (mPlayer.isPlaying()) {
381                    mPlayer.pause();
382                } else {
383                    start();
384                }
385                updatePlayPause();
386                return true;
387            case KeyEvent.KEYCODE_MEDIA_FAST_FORWARD:
388            case KeyEvent.KEYCODE_MEDIA_NEXT:
389            case KeyEvent.KEYCODE_MEDIA_PREVIOUS:
390            case KeyEvent.KEYCODE_MEDIA_REWIND:
391                return true;
392            case KeyEvent.KEYCODE_MEDIA_STOP:
393            case KeyEvent.KEYCODE_BACK:
394                stopPlayback();
395                finish();
396                return true;
397        }
398        return super.onKeyDown(keyCode, event);
399    }
400
401    /*
402     * Wrapper class to help with handing off the MediaPlayer to the next instance
403     * of the activity in case of orientation change, without losing any state.
404     */
405    private static class PreviewPlayer extends MediaPlayer implements OnPreparedListener {
406        AudioPreview mActivity;
407        boolean mIsPrepared = false;
408
409        public void setActivity(AudioPreview activity) {
410            mActivity = activity;
411            setOnPreparedListener(this);
412            setOnErrorListener(mActivity);
413            setOnCompletionListener(mActivity);
414        }
415
416        public void setDataSourceAndPrepare(Uri uri) throws IllegalArgumentException,
417                        SecurityException, IllegalStateException, IOException {
418            setDataSource(mActivity,uri);
419            prepareAsync();
420        }
421
422        /* (non-Javadoc)
423         * @see android.media.MediaPlayer.OnPreparedListener#onPrepared(android.media.MediaPlayer)
424         */
425        @Override
426        public void onPrepared(MediaPlayer mp) {
427            mIsPrepared = true;
428            mActivity.onPrepared(mp);
429        }
430
431        boolean isPrepared() {
432            return mIsPrepared;
433        }
434    }
435
436}
437