From 2cb7b0ef13ae48ca86d114e53951011b9c53d197 Mon Sep 17 00:00:00 2001 From: drizea Date: Fri, 10 Aug 2012 20:33:45 +0300 Subject: [PATCH] completed notification infrastructure add an intentService that is fired once a day (at a predefined time or on internet connectivity). This service handles notification scheduling. Some tunnigs still has to be done --- Open-ILS/src/Android/AndroidManifest.xml | 10 +- Open-ILS/src/Android/res/values/resources.xml | 4 +- .../checkout/ItemsCheckOutListView.java | 70 -------- .../accountAccess/checkout/RebootReceiver.java | 57 ------- .../android/database/DatabaseManager.java | 2 +- .../evergreen/android/globals/GlobalConfigs.java | 8 +- .../checkout => services}/NotificationAlert.java | 2 +- .../NotificationReceiver.java | 10 +- .../services/PeriodicServiceBroadcastReceiver.java | 69 ++++++++ .../evergreen/android/services/RebootReceiver.java | 44 +++++ .../android/services/ScheduledIntentService.java | 181 +++++++++++++++++++++ .../android/views/ApplicationPreferences.java | 48 ++++++ 12 files changed, 365 insertions(+), 140 deletions(-) delete mode 100644 Open-ILS/src/Android/src/org/evergreen/android/accountAccess/checkout/RebootReceiver.java rename Open-ILS/src/Android/src/org/evergreen/android/{accountAccess/checkout => services}/NotificationAlert.java (94%) rename Open-ILS/src/Android/src/org/evergreen/android/{accountAccess/checkout => services}/NotificationReceiver.java (86%) create mode 100644 Open-ILS/src/Android/src/org/evergreen/android/services/PeriodicServiceBroadcastReceiver.java create mode 100644 Open-ILS/src/Android/src/org/evergreen/android/services/RebootReceiver.java create mode 100644 Open-ILS/src/Android/src/org/evergreen/android/services/ScheduledIntentService.java diff --git a/Open-ILS/src/Android/AndroidManifest.xml b/Open-ILS/src/Android/AndroidManifest.xml index a64c282a64..43988771bd 100644 --- a/Open-ILS/src/Android/AndroidManifest.xml +++ b/Open-ILS/src/Android/AndroidManifest.xml @@ -25,13 +25,19 @@ > - + - + + + + + + + Matches exactly - + 1 2 3 @@ -22,7 +22,7 @@ 5 6 7 - + 1 Day diff --git a/Open-ILS/src/Android/src/org/evergreen/android/accountAccess/checkout/ItemsCheckOutListView.java b/Open-ILS/src/Android/src/org/evergreen/android/accountAccess/checkout/ItemsCheckOutListView.java index d71b18c0aa..1de096480d 100644 --- a/Open-ILS/src/Android/src/org/evergreen/android/accountAccess/checkout/ItemsCheckOutListView.java +++ b/Open-ILS/src/Android/src/org/evergreen/android/accountAccess/checkout/ItemsCheckOutListView.java @@ -159,9 +159,6 @@ public class ItemsCheckOutListView extends Activity { } overdueItems.setText(" " + overdueNo); - - // set alarms or renew them - setNotificationAlarms(circRecords); progressDialog.dismiss(); @@ -188,73 +185,6 @@ public class ItemsCheckOutListView extends Activity { } - public void setNotificationAlarms(ArrayList records) { - - - DefaultDAO daoNotifications = DatabaseManager.getDAOInstance(context, NotificationAlert.class, NotificationAlert.tableName); - daoNotifications.open(); - - // Fetch all alarms - List alarms = daoNotifications.fetchAll(""); - - System.out.println(" Alarms " + alarms.size()); - - for(int i=0;i daoNotifications = DatabaseManager.getDAOInstance(context, NotificationAlert.class, NotificationAlert.tableName); - daoNotifications.open(); - - // Fetch all alarms - List alarms = daoNotifications.fetchAll(""); - - System.out.println(" Alarms " + alarms.size()); - - for(int i=0;i circRecords = new ArrayList(); + + //get the circ records + try { + circRecords = accountAccess.getItemsCheckedOut(); + } catch (NoNetworkAccessException e) { + //not suppose to happen + } catch (NoAccessToServer e) { + //not suppose to happen + } catch (SessionNotFoundException e) { + //auth just earlier realized, not supose to happen + } + + DefaultDAO daoNotifications = DatabaseManager.getDAOInstance(this, NotificationAlert.class, NotificationAlert.tableName); + daoNotifications.open(); + + // Fetch all alarms from database + List alarms = daoNotifications.fetchAll(""); + + for(int i=0;i= 0) { + + // get a Calendar object with current time + Calendar cal = Calendar.getInstance(); + + cal.setTime(dueDate); + + //just for test + cal.add(Calendar.HOUR, 4); + cal.add(Calendar.MINUTE, 37); + + Log.d(TAG, "Set notification in " + cal.getTime()); + + NotificationAlert notifications = daoNotifications.fetch(checkoutRecord.circ_id); + NotificationAlert newNotificationInf = new NotificationAlert(checkoutRecord.circ_id, NotificationAlert.NOTIFICATION_INTENT + + checkoutRecord.circ_id, cal.getTime(), "Checkout " + checkoutRecord.getAuthor() + " expires on " + checkoutRecord.getDueDate()); + + if(notifications == null){ + daoNotifications.insert(newNotificationInf, false); + } + else{ + //update info in database + daoNotifications.update(newNotificationInf, checkoutRecord.circ_id); + } + + Intent intentNotification = new Intent(this, NotificationReceiver.class); + + Log.d(TAG, "Set due date alarm at" + cal.getTime() + " for " + newNotificationInf.id + " intent_val: "+ newNotificationInf.intent_val); + + intentNotification.putExtra("checkoutMessage", "The item " + checkoutRecord.getAuthor() + " is about to expire on " +checkoutRecord.getDueDate() ); + // update the current intent if it exists + PendingIntent sender = PendingIntent.getBroadcast(this, + NotificationAlert.NOTIFICATION_INTENT + checkoutRecord.circ_id, intentNotification, + PendingIntent.FLAG_UPDATE_CURRENT); + + // Get the AlarmManager service + AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); + am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender); + } + } + daoNotifications.close(); + + + lastUpdateServiceDate = currentDate; + SharedPreferences.Editor editor = sharedPreferences.edit(); + editor.putLong("lastUpdateTime", lastUpdateServiceDate.getTime()); + editor.commit(); + + Log.d(TAG, "set last service update date " + lastUpdateServiceDate); + } + + } + +} diff --git a/Open-ILS/src/Android/src/org/evergreen/android/views/ApplicationPreferences.java b/Open-ILS/src/Android/src/org/evergreen/android/views/ApplicationPreferences.java index 9a1a10dece..51220eaa85 100644 --- a/Open-ILS/src/Android/src/org/evergreen/android/views/ApplicationPreferences.java +++ b/Open-ILS/src/Android/src/org/evergreen/android/views/ApplicationPreferences.java @@ -1,13 +1,21 @@ package org.evergreen.android.views; +import java.util.Calendar; + import org.evergreen.android.R; import org.evergreen.android.accountAccess.AccountAccess; import org.evergreen.android.globals.GlobalConfigs; import org.evergreen.android.globals.NoAccessToServer; import org.evergreen.android.globals.NoNetworkAccessException; import org.evergreen.android.globals.Utils; +import org.evergreen.android.services.NotificationAlert; +import org.evergreen.android.services.NotificationReceiver; +import org.evergreen.android.services.PeriodicServiceBroadcastReceiver; +import org.evergreen.android.services.ScheduledIntentService; +import android.app.AlarmManager; import android.app.AlertDialog; +import android.app.PendingIntent; import android.app.ProgressDialog; import android.content.Context; import android.content.DialogInterface; @@ -210,6 +218,46 @@ public class ApplicationPreferences extends PreferenceActivity implements OnShar //wait for execution } + if(key.equals("notifications_enabled")){ + + if(sharedPreferences.getBoolean("notifications_enabled", false)){ + + Toast.makeText(context, "Set up notification updates", Toast.LENGTH_SHORT).show(); + //if enabled register the update service to run once per day + // get a Calendar object with current time + Calendar cal = Calendar.getInstance(); + + Intent bRecvIntent = new Intent(this,PeriodicServiceBroadcastReceiver.class); + bRecvIntent.setAction(ScheduledIntentService.ACTION); + // update the current intent if it exists + PendingIntent sender = PendingIntent.getBroadcast(this, + NotificationAlert.NOTIFICATION_INTENT + PeriodicServiceBroadcastReceiver.INTENT_ID, bRecvIntent, + PendingIntent.FLAG_UPDATE_CURRENT); + + // Get the AlarmManager service + AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); + am.setRepeating(AlarmManager.RTC, cal.getTimeInMillis(), 10000 * ScheduledIntentService.SCHEDULE_TIME_INTERVAL, sender); + } + else + { + Toast.makeText(context, "Disable notification updates", Toast.LENGTH_SHORT).show(); + //cancel the service + + Intent bRecvIntent = new Intent(this,PeriodicServiceBroadcastReceiver.class); + + // update the current intent if it exists + PendingIntent sender = PendingIntent.getBroadcast(this, + NotificationAlert.NOTIFICATION_INTENT + PeriodicServiceBroadcastReceiver.INTENT_ID, bRecvIntent, + PendingIntent.FLAG_UPDATE_CURRENT); + + // Get the AlarmManager service + AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE); + //cancel the service + am.cancel(sender); + } + //register the + + } //test connection if(!isFinishing() && httpAddressChange == false && checkConnection == true){ -- 2.11.0