package org.bostonandroid.umbrellatoday;

import android.app.IntentService;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;

public class AlarmService extends IntentService {
  public final static String TAG = "AlarmService";

  private NotificationManager notificationManager;

  public AlarmService() {
    super("AlarmService");
  }

  @Override
  public void onCreate() {
    super.onCreate();
    notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
  }

  @Override
  protected void onHandleIntent(Intent intent) {
    Alert.find(this, intent.getExtras().getLong("alarm_id")).perform(new ValueRunner<SavedAlert>() {
      public void run(SavedAlert alert) {
        alert.url(AlarmService.this).
          perform(new ValueRunner<String>() {
            public void run(String url) {
              new ReportRetriever(url).retrieveReport().perform(new ValueRunner<Report>() {
                public void run(Report report) {
                  String answer = report.getAnswer();
                  if (answer.equals("yes"))
                    showNotification("You may need your galoshes!", R.drawable.weather_showers);
                  else if (answer.equals("snow"))
                    showNotification("Bring a shovel!", R.drawable.weather_snow);
                }});}}).
          orElse(new Runnable() {
            public void run() {
              showErrorNotification("Can't look up location data.");
            }});}});
  }

  void showNotification(String message, int icon) {
    String appName = getString(R.string.app_name);
    Notification notification = new Notification(
        R.drawable.weather_showers, appName, System
        .currentTimeMillis());
    notification.setLatestEventInfo(AlarmService.this, appName, message,
        PendingIntent.getActivity(this, 1, new Intent(this, Alerts.class), 0));
    notification.flags |= Notification.FLAG_AUTO_CANCEL;
    notificationManager.notify(1, notification);
  }
  
  private void showErrorNotification(String message) {
    showNotification(message, R.drawable.weather_showers);
  }
}

@@ -683,24 +683,28 @@
     
-Alert.find(this,
+final long alarmId =
  int
@@ -738,16 +738,48 @@
 arm_id%22)
+;%0A %0A    Alert.find(this, alarmId
 ).perfor
@@ -1256,16 +1256,44 @@
 ication(
+R.drawable.weather_showers, 
 %22You may
@@ -1319,34 +1319,15 @@
 !%22, 
-R.drawable.weather_showers
+alarmId
 );%0A 
@@ -1416,26 +1416,8 @@
 ion(
-%22Bring a shovel!%22,
  R.d
@@ -1436,16 +1436,44 @@
 her_snow
+, %22Bring a shovel!%22, alarmId
 );%0A     
@@ -1624,16 +1624,25 @@
 n data.%22
+, alarmId
 );%0A     
@@ -1663,59 +1663,100 @@
 ;%0A  
-%7D%0A%0A  void showNotification(String message, 
+  %0A    new AlarmSetter(this).run();%0A  %7D%0A  %0A  private Notification buildNotification(
 int icon
 ) %7B%0A
@@ -1755,23 +1755,17 @@
 icon
-) %7B%0A   
+,
  String 
 appN
@@ -1764,47 +1764,36 @@
 ing 
-appName = getString(R.string.app_name);
+contentText, long alertId) %7B
 %0A   
@@ -1842,69 +1842,33 @@
 ion(
-%0A        R.drawable.weather_showers, appName, System%0A        
+icon, contentText, System
 .cur
@@ -1926,43 +1926,55 @@
 nfo(
-AlarmService.this, appN
+this, getString(R.string.app_n
 ame
+)
 , 
-message
+contentText
 ,%0A  
@@ -2011,17 +2011,17 @@
 y(this, 
-1
+0
 , new In
@@ -2035,21 +2035,54 @@
 is, 
+Edit
 Alert
-s
 .class)
+.putExtra(%22alert_id%22, alertId)
 , 0)
@@ -2149,50 +2149,294 @@
     
-notificationManager.notify(1, notification
+return notification;%0A  %7D%0A%0A  void showNotification(int icon, String contentText, long alertId) %7B%0A    notificationManager.cancel((int)alertId); // FIXME: cancel and notify take an int, alertId is long%0A    notificationManager.notify((int)icon, buildNotification(icon, contentText, alertId)
 );%0A 
@@ -2485,23 +2485,41 @@
 (String 
-message
+contentText, long alarmId
 ) %7B%0A    
@@ -2539,17 +2539,8 @@
 ion(
-message, 
 R.dr
@@ -2561,16 +2561,38 @@
 _showers
+, contentText, alarmId
 );%0A  %7D%0A%7D

Tidy up notifications