HypoAlarm/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmNotify.java

118 lines
5.9 KiB
Java

package za.org.treehouse.hypoalarm;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
public class AlarmNotify extends Service {
public static final int notifyID = 1;
private volatile boolean threadRunning = false;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onDestroy() {
// If the notification is cancelled, stop updating.
threadRunning = false;
// Remove the notification in the notification bar
NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
nm.cancel(notifyID);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
final int UPDATE_INTERVAL = 10*1000; // Timer is updated six times a minute
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
//final String phoneNumber = sharedPref.getString(getString(R.string.PhoneNumberPref), null);
final int gracePeriod = sharedPref.getInt(getString(R.string.GracePeriodPref), 60);
// convert gracePeriod to milliseconds and calculate when it'll fire
final long endTime = System.currentTimeMillis() + (gracePeriod * 60 * 1000);
//Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.alarm_notification);
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher_grey);
final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
final Notification.Builder notification = new Notification.Builder(this)
.setContentTitle(getString(R.string.app_name))
.setContentText(String.format(getString(R.string.notificationText), MainActivity.MinutesToGracePeriodStr(gracePeriod)))
.setSmallIcon(R.drawable.alarm_notification)
.setLargeIcon(bm)
.setOnlyAlertOnce(true)
.setAutoCancel(true)
.setPriority(Notification.PRIORITY_HIGH);
//.setContentText(String.format(getString(R.string.notificationText), phoneNumber) + MainActivity.MinutesToGracePeriodStr(gracePeriod))
// Set up dismiss action
Intent cancellerIntent = new Intent(getBaseContext(), CancelGraceReceiver.class);
PendingIntent cancellerPendingIntent = PendingIntent.getBroadcast(getBaseContext(), MainActivity.CANCEL_GRACE_REQUEST, cancellerIntent, 0);
// Cancel the grace period if the user clears the notification
notification.setDeleteIntent(cancellerPendingIntent);
// Allow the user to cancel by clicking a "Cancel" button
notification.addAction(android.R.drawable.ic_menu_close_clear_cancel, getString(R.string.notificationCancellation), cancellerPendingIntent);
// Allow the user to cancel by selecting the ContentText or ContentTitle
notification.setContentIntent(cancellerPendingIntent);
// TODO load alert activity (without sound or vibration) on select? This would allow the user to test competence
Intent alertActivityIntent = new Intent(this, AlarmAlertActivity.class);
alertActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
//notification.setContentIntent(alertActivityIntent);
nm.cancel(notifyID);
nm.notify(notifyID, notification.build());
new Thread(new Runnable() {
@Override
public void run() {
threadRunning = true;
int max = 1000;
// Convert endTime from milliseconds to seconds, and translate to time remaining
int secondsLeft = (int) ((endTime - System.currentTimeMillis())) / (1000);
int gracePeriodSeconds = gracePeriod * 60;
// Multiply each int by 1000 for greater progress resolution
int progress = (((gracePeriodSeconds * 1000) - (secondsLeft * 1000)) * max) / (gracePeriodSeconds * 1000);
while (progress < max) {
// Stop the thread if cancelled elsewhere
if (!threadRunning) {
return;
}
int minutesLeft = secondsLeft / 60;
notification.setContentText(String.format(getString(R.string.notificationText), MainActivity.MinutesToGracePeriodStr(minutesLeft)));
notification.setProgress(max, progress, false);
// Update the notification
nm.notify(notifyID, notification.build());
// Prepare secondsLeft and progress for the next loop
secondsLeft = secondsLeft - (UPDATE_INTERVAL / 1000);
// Multiply each int by 1000 for greater progress resolution
progress = (((gracePeriodSeconds * 1000) - (secondsLeft * 1000)) * max) / (gracePeriodSeconds * 1000);
Log.d("AlarmNotify", "secondsLeft is "+secondsLeft+" and progress is "+progress+" (gracePeriodSeconds is "+gracePeriodSeconds+")");
// Sleeps the thread, simulating an operation
// that takes time
try {
Thread.sleep(UPDATE_INTERVAL);
} catch (InterruptedException e) {
Log.d("AlarmNotify", "sleep failure");
}
}
stopSelf(); // stop notification service
}
}).start();
return super.onStartCommand(intent, flags, startId);
}
}