From 87dda476083de9b1f9d9a3577e235520ab7592c7 Mon Sep 17 00:00:00 2001 From: tim Date: Tue, 25 Mar 2014 14:43:42 +0200 Subject: [PATCH] Notifications all working. --- .../org/treehouse/hypoalarm/AlarmNotify.java | 110 ++++++++++++++++++ .../hypoalarm/CancelGraceReceiver.java | 25 ++++ 2 files changed, 135 insertions(+) create mode 100644 HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmNotify.java create mode 100644 HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/CancelGraceReceiver.java diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmNotify.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmNotify.java new file mode 100644 index 0000000..5be4590 --- /dev/null +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/AlarmNotify.java @@ -0,0 +1,110 @@ +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; + +import java.util.Timer; +import java.util.TimerTask; + +// TODO see AlarmNotifications.java +// TODO update every minute to update time till sms is sent + +public class AlarmNotify extends Service { + private final int UPDATE_INTERVAL = 15*1000; // Timer is updated four times a minute + public final int notifyID = 1; + private Timer timer = new Timer(); + + @Override + public IBinder onBind(Intent intent) { + return null; + } + + @Override + public void onDestroy() { + // Remember to remove the notification in the notification bar + NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); + nm.cancel(notifyID); + if (timer != null) { + timer.cancel(); + } + } + + @Override + public int onStartCommand(Intent intent, int flags, int startId) { + SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this); + + final int gracePeriod = sharedPref.getInt(getString(R.string.GracePeriodPref), 60); + final String phoneNumber = sharedPref.getString(getString(R.string.PhoneNumberPref), null); + // 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); + 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)) + + // todo better icon + + + // 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 + // TODO load alert activity (without sound or vibration) on select? This would allow the user to test competence + notification.setContentIntent(cancellerPendingIntent); + + nm.cancel(notifyID); + nm.notify(notifyID, notification.build()); + + timer.scheduleAtFixedRate(new TimerTask() { + // this is called at every UPDATE_INTERVAL + @Override + public void run() { + 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) ); + //Log.d("AlarmNotify", "secondsLeft is "+secondsLeft+" and progress is "+progress+" (gracePeriodSeconds is "+gracePeriodSeconds+")"); + if (secondsLeft >= 0) { + 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()); + } else { + nm.cancel(notifyID); + this.cancel(); // Cancel timer + stopSelf(); // stop service + } + } + }, 0, UPDATE_INTERVAL); + + return super.onStartCommand(intent, flags, startId); + } + +} diff --git a/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/CancelGraceReceiver.java b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/CancelGraceReceiver.java new file mode 100644 index 0000000..e71dfb4 --- /dev/null +++ b/HypoAlarm/src/main/java/za/org/treehouse/hypoalarm/CancelGraceReceiver.java @@ -0,0 +1,25 @@ +package za.org.treehouse.hypoalarm; + +import android.app.AlarmManager; +import android.app.PendingIntent; +import android.content.BroadcastReceiver; +import android.content.Context; +import android.content.Intent; +import android.util.Log; +import android.widget.Toast; + +public class CancelGraceReceiver extends BroadcastReceiver { + @Override + public void onReceive(Context context, Intent intent) { + AlarmManager graceManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); + Intent graceIntent = new Intent(context, GraceReceiver.class); + PendingIntent gracePendingIntent = PendingIntent.getBroadcast(context, MainActivity.GRACE_REQUEST, graceIntent, 0); + graceManager.cancel(gracePendingIntent); + Log.d("CancelGraceReceiver", "Cancelled grace alarm."); + + // Display toast + Toast.makeText(context, context.getString(R.string.alarmCancelToast), Toast.LENGTH_LONG).show(); + + context.stopService(new Intent(context, AlarmNotify.class)); + } +} \ No newline at end of file