| 1 | package ro.ieval.unical; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.text.DateFormat; |
| 5 | import java.text.SimpleDateFormat; |
| 6 | import java.util.Date; |
| 7 | import java.util.Locale; |
| 8 | |
| 9 | import android.app.Activity; |
| 10 | import android.content.Context; |
| 11 | import android.media.AudioManager; |
| 12 | import android.media.MediaPlayer; |
| 13 | import android.media.RingtoneManager; |
| 14 | import android.net.Uri; |
| 15 | import android.os.Bundle; |
| 16 | import android.view.MotionEvent; |
| 17 | import android.view.View; |
| 18 | import android.view.View.OnTouchListener; |
| 19 | import android.view.Window; |
| 20 | import android.view.WindowManager; |
| 21 | import android.widget.Button; |
| 22 | import android.widget.TextView; |
| 23 | |
| 24 | public class AlarmReceiverActivity extends Activity { |
| 25 | public static final String EXTRA_EVENT = "event"; |
| 26 | private MediaPlayer mMediaPlayer; |
| 27 | |
| 28 | @Override |
| 29 | public void onCreate(final Bundle savedInstanceState) { |
| 30 | super.onCreate(savedInstanceState); |
| 31 | this.requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 32 | this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); |
| 33 | Event event=Event.getEventById(this,getIntent().getLongExtra(EXTRA_EVENT,1L)); |
| 34 | setContentView(R.layout.alarm); |
| 35 | |
| 36 | TextView title = (TextView)findViewById(R.id.alarm_title); |
| 37 | title.setText(event.title); |
| 38 | |
| 39 | final TextView startDay= (TextView) findViewById(R.id.startday); |
| 40 | final TextView endDay= (TextView) findViewById(R.id.endday); |
| 41 | final TextView startDate= (TextView) findViewById(R.id.startdate); |
| 42 | final TextView endDate= (TextView) findViewById(R.id.enddate); |
| 43 | final TextView startTime= (TextView) findViewById(R.id.starttime); |
| 44 | final TextView endTime= (TextView) findViewById(R.id.endtime); |
| 45 | final Date start=new Date(event.dtstart); |
| 46 | final Date end=new Date(event.dtend); |
| 47 | final DateFormat dateFormat = DateFormat.getDateInstance(); |
| 48 | final DateFormat timeFormat = DateFormat.getTimeInstance(); |
| 49 | final DateFormat dayOfWeekFormat = new SimpleDateFormat("EEEEEEE", Locale.UK); |
| 50 | |
| 51 | startDay.setText(dayOfWeekFormat.format(start)); |
| 52 | startDate.setText(dateFormat.format(start)); |
| 53 | if(!dateFormat.format(start).equals(dateFormat.format(end))) { |
| 54 | endDay.setText(dayOfWeekFormat.format(end)); |
| 55 | endDate.setText(dateFormat.format(end)); |
| 56 | } |
| 57 | |
| 58 | startTime.setText(timeFormat.format(start)); |
| 59 | if(!start.equals(end)) |
| 60 | endTime.setText(timeFormat.format(end)); |
| 61 | |
| 62 | final Button stopAlarm = (Button) findViewById(R.id.stopAlarm); |
| 63 | stopAlarm.setOnTouchListener(new OnTouchListener() { |
| 64 | @Override |
| 65 | public boolean onTouch(final View arg0, final MotionEvent arg1) { |
| 66 | mMediaPlayer.stop(); |
| 67 | finish(); |
| 68 | return false; |
| 69 | } |
| 70 | }); |
| 71 | |
| 72 | playSound(this, getAlarmUri()); |
| 73 | } |
| 74 | |
| 75 | private void playSound(final Context context, final Uri alert) { |
| 76 | mMediaPlayer = new MediaPlayer(); |
| 77 | try { |
| 78 | mMediaPlayer.setDataSource(context, alert); |
| 79 | final AudioManager audioManager = (AudioManager) context |
| 80 | .getSystemService(Context.AUDIO_SERVICE); |
| 81 | if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) { |
| 82 | mMediaPlayer.setAudioStreamType(AudioManager.STREAM_ALARM); |
| 83 | mMediaPlayer.prepare(); |
| 84 | mMediaPlayer.start(); |
| 85 | } |
| 86 | } catch (IOException e) { |
| 87 | e.printStackTrace(); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | private static Uri getAlarmUri() { |
| 92 | Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); |
| 93 | if (alert == null) |
| 94 | alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); |
| 95 | if (alert == null) |
| 96 | alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); |
| 97 | |
| 98 | return alert; |
| 99 | } |
| 100 | } |