Notifications all working.

This commit is contained in:
Timothy Allen 2014-03-25 14:43:42 +02:00
commit 87dda47608
2 changed files with 135 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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));
}
}