/***
	Copyright (c) 2008 CommonsWare, LLC
	
	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.commonsware.android.audio;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

public class AudioDemo extends Activity {
	private static final int CLOSE_ID = Menu.FIRST+2;
	
	private ImageButton play;
	private ImageButton pause;
	private ImageButton stop;
	private MediaPlayer mp;

	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.main);
		
		play=(ImageButton)findViewById(R.id.play);
		pause=(ImageButton)findViewById(R.id.pause);
		stop=(ImageButton)findViewById(R.id.stop);
		
		play.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				mp.start();
				play.setEnabled(false);
				pause.setEnabled(true);
				stop.setEnabled(true);
			}
		});
		
		pause.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				mp.pause();
				play.setEnabled(true);
				pause.setEnabled(false);
				stop.setEnabled(true);
			}
		});
		
		stop.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				stop();
			}
		});
		
		try {
			mp=new MediaPlayer();
			mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
				public void onPrepared(MediaPlayer mp) {
					play.setEnabled(true);
				}
			});
			mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
				public void onCompletion(MediaPlayer mp) {
					stop();
				}
			});
			
			setup();
		}
		catch (Throwable t) {
			android.util.Log.e("AudioDemo", "Exception playing audio", t);
			Toast.makeText(this, "Ick!", 2000).show();
		}
		}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, CLOSE_ID, "Close", R.drawable.eject)
				.setAlphabeticShortcut('c');

		return(super.onCreateOptionsMenu(menu));
	}

	@Override
	public boolean onOptionsItemSelected(Menu.Item item) {
		switch (item.getId()) {
			case CLOSE_ID:
				finish();
				return(true);
		}

		return(super.onOptionsItemSelected(item));
	}
	
	private void stop() {
		mp.reset();
		setup();
	}
	
	private void setup() {
		play.setEnabled(false);
		pause.setEnabled(false);
		stop.setEnabled(false);
		
		try {
			mp.setDataSource("/system/media/audio/ringtones/ringer.mp3");
		}
		catch (Throwable t) {
			android.util.Log.e("AudioDemo", "Exception playing audio", t);
			Toast.makeText(this, "Ick!", 2000).show();
		}
		
		mp.prepareAsync();
	}
}