-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlarmNotification.java
191 lines (156 loc) · 5.46 KB
/
AlarmNotification.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**************************************************************************
*
* Copyright (C) 2012-2015 Alex Taradov <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*************************************************************************/
package com.application.brainfit;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import java.util.Timer;
import java.util.TimerTask;
public class AlarmNotification extends Activity
{
private final String TAG = "AlarmMe";
private MediaPlayer mRingtone;
private Vibrator mVibrator;
private final long[] mVibratePattern = { 0, 500, 500 };
private boolean mVibrate;
private Uri mAlarmSound;
private long mPlayTime;
private Timer mTimer = null;
private Alarm mAlarm;
private DateTime mDateTime;
private TextView mTextView;
private PlayTimerTask mTimerTask;
@Override
protected void onCreate(Bundle bundle)
{
super.onCreate(bundle);
getWindow().addFlags(
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON |
WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
setContentView(R.layout.notification);
mDateTime = new DateTime(this);
mTextView = (TextView)findViewById(R.id.alarm_title_text);
readPreferences();
mRingtone = MediaPlayer.create(getApplicationContext(), R.raw.tone2);
if (mVibrate)
mVibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
start(getIntent());
}
@Override
protected void onDestroy()
{
super.onDestroy();
Log.i(TAG, "AlarmNotification.onDestroy()");
stop();
}
@Override
protected void onNewIntent(Intent intent)
{
super.onNewIntent(intent);
Log.i(TAG, "AlarmNotification.onNewIntent()");
addNotification(mAlarm);
stop();
start(intent);
}
private void start(Intent intent)
{
mAlarm = new Alarm(this);
mAlarm.fromIntent(intent);
Log.i(TAG, "AlarmNotification.start('" + mAlarm.getTitle() + "')");
mTextView.setText(mAlarm.getTitle());
mTimerTask = new PlayTimerTask();
mTimer = new Timer();
mTimer.schedule(mTimerTask, mPlayTime);
mRingtone.start();
if (mVibrate)
mVibrator.vibrate(mVibratePattern, 0);
}
private void stop()
{
Log.i(TAG, "AlarmNotification.stop()");
mTimer.cancel();
mRingtone.stop();
if (mVibrate)
mVibrator.cancel();
}
public void onDismissClick(View view)
{
finish();
}
private void readPreferences()
{
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
mAlarmSound = Uri.parse(prefs.getString("alarm_sound_pref", "DEFAULT_RINGTONE_URI"));
mVibrate = prefs.getBoolean("vibrate_pref", true);
mPlayTime = (long) Integer.parseInt(prefs.getString("alarm_play_time_pref", "30")) * 1000;
}
private void addNotification(Alarm alarm)
{
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification;
PendingIntent activity;
Intent intent;
Log.i(TAG, "AlarmNotification.addNotification(" + alarm.getId() + ", '" + alarm.getTitle() + "', '" + mDateTime.formatDetails(alarm) + "')");
intent = new Intent(this.getApplicationContext(), AlarmMe.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
activity = PendingIntent.getActivity(this, (int)alarm.getId(), intent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
notification = builder
.setContentIntent(activity)
.setSmallIcon(R.drawable.ic_notification)
.setAutoCancel(true)
.setContentTitle("Missed alarm: " + alarm.getTitle())
.setContentText(mDateTime.formatDetails(alarm))
.build();
notificationManager.notify((int)alarm.getId(), notification);
}
@Override
public void onBackPressed()
{
finish();
}
private class PlayTimerTask extends TimerTask
{
@Override
public void run()
{
Log.i(TAG, "AlarmNotification.PalyTimerTask.run()");
addNotification(mAlarm);
finish();
}
}
}