]>
Commit | Line | Data |
---|---|---|
e2332327 PT |
1 | package ro.ieval.unical; |
2 | ||
3 | import java.io.IOException; | |
93afae01 P |
4 | import java.text.DateFormat; |
5 | import java.text.SimpleDateFormat; | |
6 | import java.util.Date; | |
7 | import java.util.Locale; | |
e2332327 PT |
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; | |
93afae01 | 22 | import android.widget.TextView; |
e2332327 PT |
23 | |
24 | public class AlarmReceiverActivity extends Activity { | |
57ca4051 | 25 | public static final String EXTRA_EVENT = "event"; |
e2332327 | 26 | private MediaPlayer mMediaPlayer; |
57ca4051 | 27 | |
e2332327 | 28 | @Override |
ea370609 | 29 | public void onCreate(final Bundle savedInstanceState) { |
e2332327 PT |
30 | super.onCreate(savedInstanceState); |
31 | this.requestWindowFeature(Window.FEATURE_NO_TITLE); | |
32 | this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); | |
93afae01 | 33 | Event event=Event.getEventById(this,getIntent().getLongExtra(EXTRA_EVENT,1L)); |
e2332327 | 34 | setContentView(R.layout.alarm); |
93afae01 P |
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)); | |
e2332327 | 61 | |
ea370609 | 62 | final Button stopAlarm = (Button) findViewById(R.id.stopAlarm); |
e2332327 | 63 | stopAlarm.setOnTouchListener(new OnTouchListener() { |
ea370609 MG |
64 | @Override |
65 | public boolean onTouch(final View arg0, final MotionEvent arg1) { | |
e2332327 PT |
66 | mMediaPlayer.stop(); |
67 | finish(); | |
68 | return false; | |
69 | } | |
70 | }); | |
71 | ||
72 | playSound(this, getAlarmUri()); | |
73 | } | |
74 | ||
ea370609 | 75 | private void playSound(final Context context, final Uri alert) { |
e2332327 PT |
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) { | |
ea370609 | 87 | e.printStackTrace(); |
e2332327 PT |
88 | } |
89 | } | |
ea370609 MG |
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 | ||
e2332327 PT |
98 | return alert; |
99 | } | |
100 | } |