completed notification infrastructure
authordrizea <danielrizea27@gmail.com>
Fri, 10 Aug 2012 17:33:45 +0000 (20:33 +0300)
committerdrizea <danielrizea27@gmail.com>
Fri, 10 Aug 2012 17:33:45 +0000 (20:33 +0300)
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

14 files changed:
Open-ILS/src/Android/AndroidManifest.xml
Open-ILS/src/Android/res/values/resources.xml
Open-ILS/src/Android/src/org/evergreen/android/accountAccess/checkout/ItemsCheckOutListView.java
Open-ILS/src/Android/src/org/evergreen/android/accountAccess/checkout/NotificationAlert.java [deleted file]
Open-ILS/src/Android/src/org/evergreen/android/accountAccess/checkout/NotificationReceiver.java [deleted file]
Open-ILS/src/Android/src/org/evergreen/android/accountAccess/checkout/RebootReceiver.java [deleted file]
Open-ILS/src/Android/src/org/evergreen/android/database/DatabaseManager.java
Open-ILS/src/Android/src/org/evergreen/android/globals/GlobalConfigs.java
Open-ILS/src/Android/src/org/evergreen/android/services/NotificationAlert.java [new file with mode: 0644]
Open-ILS/src/Android/src/org/evergreen/android/services/NotificationReceiver.java [new file with mode: 0644]
Open-ILS/src/Android/src/org/evergreen/android/services/PeriodicServiceBroadcastReceiver.java [new file with mode: 0644]
Open-ILS/src/Android/src/org/evergreen/android/services/RebootReceiver.java [new file with mode: 0644]
Open-ILS/src/Android/src/org/evergreen/android/services/ScheduledIntentService.java [new file with mode: 0644]
Open-ILS/src/Android/src/org/evergreen/android/views/ApplicationPreferences.java

index a64c282..4398877 100644 (file)
         >
         
         <!-- Notification receiver -->
-        <receiver  android:process=":remote" android:name=".accountAccess.checkout.NotificationReceiver"></receiver>
+        <receiver  android:process=":remote" android:name=".services.NotificationReceiver"></receiver>
                <!-- Receiver to reinit notifications on reboot -->
-               <receiver android:name=".accountAccess.checkout.RebootReceiver">
+               <receiver android:name=".services.RebootReceiver">
                        <intent-filter>
                        <action android:name="android.intent.action.BOOT_COMPLETED"/>
                        </intent-filter>
                </receiver>
+               <receiver android:name=".services.PeriodicServiceBroadcastReceiver">
+                       <intent-filter>
+                       <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
+                       </intent-filter>
+               </receiver>
+               <service android:name=".services.ScheduledIntentService"></service>
         
         
         <activity
index db6eaf0..3e68722 100644 (file)
@@ -14,7 +14,7 @@
                <item>Matches exactly</item>
     </string-array>
     
-    <string-array name="notification_preferences_list_values">
+    <integer-array name="notification_preferences_list_values">
         <item >1</item>
         <item >2</item>
         <item >3</item>
@@ -22,7 +22,7 @@
         <item >5</item>
         <item >6</item>
         <item >7</item>
-    </string-array>
+    </integer-array>
     
     <string-array name="notification_preferences_list_entries">
         <item >1 Day</item>
index d71b18c..1de0964 100644 (file)
@@ -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<CircRecord> records) {
-
-               
-               DefaultDAO<NotificationAlert> daoNotifications = DatabaseManager.getDAOInstance(context, NotificationAlert.class, NotificationAlert.tableName);
-               daoNotifications.open();
-
-               // Fetch all alarms
-               List<NotificationAlert> alarms = daoNotifications.fetchAll("");
-               
-               System.out.println(" Alarms " + alarms.size());
-
-               for(int i=0;i<alarms.size();i++){
-                       System.out.println("notification " + alarms.get(i));
-               }
-               for (int i = 0; i < records.size(); i++) {
-
-                       CircRecord checkoutRecord = records.get(i);
-
-                       Date dueDate = checkoutRecord.getDueDateObject();
-       
-                       // if due date in the future
-                       if (currentDate.compareTo(dueDate) <= 0) {
-
-                               // get a Calendar object with current time
-                               Calendar cal = Calendar.getInstance();
-
-                               
-                               cal.setTime(dueDate);
-
-                               System.out.println("Cal :" + cal.get(Calendar.DAY_OF_MONTH) + " " + cal.get(Calendar.YEAR) + " " + cal.get(Calendar.MONTH));
-                               System.out.println("Date " + new Date(cal.getTimeInMillis()) );
-                               //just for test
-                               cal.add(Calendar.HOUR, 4);
-                               cal.add(Calendar.MINUTE, 37);
-                               
-                               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 intent = new Intent(context, NotificationReceiver.class);
-
-                               System.out.println("Set due date " + cal.getTime()
-                                               + " with intent val " + NotificationAlert.NOTIFICATION_INTENT
-                                               + checkoutRecord.circ_id);
-                               intent.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, intent,
-                                               PendingIntent.FLAG_UPDATE_CURRENT);
-
-                               // Get the AlarmManager service
-                               AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
-                               am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
-                       }
-               }
-               daoNotifications.close();
-
-       }
 
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
diff --git a/Open-ILS/src/Android/src/org/evergreen/android/accountAccess/checkout/NotificationAlert.java b/Open-ILS/src/Android/src/org/evergreen/android/accountAccess/checkout/NotificationAlert.java
deleted file mode 100644 (file)
index 8e4ca59..0000000
+++ /dev/null
@@ -1,47 +0,0 @@
-package org.evergreen.android.accountAccess.checkout;
-
-import java.util.Date;
-
-import org.androwrapee.db.DatabaseClass;
-import org.androwrapee.db.DatabaseField;
-import org.androwrapee.db.IdField;
-
-@DatabaseClass
-public class NotificationAlert {
-       
-       public static final String tableName = "notifications";
-       
-       public static final int NOTIFICATION_INTENT = 123456;
-       
-       @IdField
-       public long id;
-       
-       @DatabaseField
-       public int intent_val;
-       
-       @DatabaseField
-       public Date triggerDate; 
-       
-       @DatabaseField
-       public String message;
-       
-       //required constructor for DAO
-       public NotificationAlert(){
-               
-       }
-       
-       public NotificationAlert(int id, int intent_val, Date triggerDate, String message ){
-               
-               this.id = id;
-               this.intent_val = intent_val;
-               this.triggerDate = triggerDate;
-               this.message = message;
-       }
-       
-       
-       @Override
-       public String toString() {
-               
-               return " Notification:[ id: " + id+ "; intent_val: "+intent_val+"; triggerDate : "+triggerDate+"; message: "+message+"]";
-       }
-}
diff --git a/Open-ILS/src/Android/src/org/evergreen/android/accountAccess/checkout/NotificationReceiver.java b/Open-ILS/src/Android/src/org/evergreen/android/accountAccess/checkout/NotificationReceiver.java
deleted file mode 100644 (file)
index 0e80cea..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-package org.evergreen.android.accountAccess.checkout;
-
-import org.evergreen.android.views.splashscreen.SplashActivity;
-
-import android.app.Notification;
-import android.app.NotificationManager;
-import android.app.PendingIntent;
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-import android.os.Bundle;
-import android.util.Log;
-
-public class NotificationReceiver extends BroadcastReceiver {
-
-       private String TAG = "NotificationManager";
-       public static final int NOTIFICATION_ID = 1;
-
-       @Override
-       public void onReceive(Context context, Intent intent) {
-
-               String ns = Context.NOTIFICATION_SERVICE;
-               NotificationManager mNotificationManager = (NotificationManager) context
-                               .getSystemService(ns);
-
-               Bundle bundle = intent.getExtras();
-               String checkoutMessage = bundle.getString("checkoutMessage");
-
-               Log.d(TAG, "The " + checkoutMessage + " is about to expire");
-               // send notification
-
-               int icon = android.R.drawable.ic_dialog_alert;
-               CharSequence tickerText = "Checkout item due date";
-               long when = System.currentTimeMillis();
-
-               Notification notification = new Notification(icon, tickerText, when);
-
-               CharSequence contentTitle = "EG - checkout item due date";
-               CharSequence contentText = checkoutMessage;
-               // start evergreen
-               Intent notificationIntent = new Intent(context, SplashActivity.class);
-               notificationIntent.putExtra("jump", "checkout_items");
-
-               PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
-                               notificationIntent, 0);
-
-               notification.setLatestEventInfo(context, contentTitle, contentText,
-                               contentIntent);
-
-               mNotificationManager.notify(NOTIFICATION_ID, notification);
-
-       }
-
-}
diff --git a/Open-ILS/src/Android/src/org/evergreen/android/accountAccess/checkout/RebootReceiver.java b/Open-ILS/src/Android/src/org/evergreen/android/accountAccess/checkout/RebootReceiver.java
deleted file mode 100644 (file)
index 5932147..0000000
+++ /dev/null
@@ -1,57 +0,0 @@
-package org.evergreen.android.accountAccess.checkout;
-
-import java.util.Calendar;
-import java.util.List;
-
-import org.androwrapee.db.DefaultDAO;
-import org.evergreen.android.database.DatabaseManager;
-
-import android.app.Activity;
-import android.app.AlarmManager;
-import android.app.PendingIntent;
-import android.content.BroadcastReceiver;
-import android.content.Context;
-import android.content.Intent;
-
-
-public class RebootReceiver extends BroadcastReceiver{
-
-       @Override
-       public void onReceive(Context context, Intent arg1) {
-               //reinitialize notifications
-               
-               DefaultDAO<NotificationAlert> daoNotifications = DatabaseManager.getDAOInstance(context, NotificationAlert.class, NotificationAlert.tableName);
-               daoNotifications.open();
-
-               // Fetch all alarms
-               List<NotificationAlert> alarms = daoNotifications.fetchAll("");
-               
-               System.out.println(" Alarms " + alarms.size());
-
-               for(int i=0;i<alarms.size();i++){
-                       System.out.println("notification " + alarms.get(i));
-               
-                       Calendar cal = Calendar.getInstance();
-                       cal.setTime(alarms.get(i).triggerDate);
-
-                       Intent intent = new Intent(context, NotificationReceiver.class);
-
-                       System.out.println("Set Notification with message " + alarms.get(i).message + " on time :" +  cal.getTime());
-                       intent.putExtra("checkoutMessage", alarms.get(i).message);
-                       
-                       // In reality, you would want to have a static variable for the
-
-                       PendingIntent sender = PendingIntent.getBroadcast(context, alarms.get(i).intent_val, intent,
-                                       PendingIntent.FLAG_UPDATE_CURRENT);
-
-                       // Get the AlarmManager service
-                       AlarmManager am = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);
-                       am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
-               
-               }
-               
-               daoNotifications.close();
-
-       }
-
-}
index 8844877..b5ea337 100644 (file)
@@ -8,7 +8,7 @@ import org.androwrapee.db.DefaultDAO;
 import org.androwrapee.db.DefaultDatabaseHelper;
 import org.androwrapee.db.IllegalClassStructureException;
 import org.androwrapee.db.ReflectionManager;
-import org.evergreen.android.accountAccess.checkout.NotificationAlert;
+import org.evergreen.android.services.NotificationAlert;
 
 import android.content.Context;
 import android.util.Log;
index 26fd355..5d4e4ed 100644 (file)
@@ -23,6 +23,8 @@ import android.util.Log;
 
 public class GlobalConfigs {
 
+       public static String IDL_FILE_FROM_ROOT = "/reports/fm_IDL.xml";
+       
        public static String httpAddress = "";
 
        private boolean init = false;
@@ -110,14 +112,14 @@ public class GlobalConfigs {
        
        public void loadIDLFile(){
                
-                        String idlFile = "/reports/fm_IDL.xml";
+                        
                         try{
                                Log.d("debug","Read fm");
-                               InputStream in_IDL = Utils.getNetInputStream(httpAddress + idlFile);
+                               InputStream in_IDL = Utils.getNetInputStream(httpAddress + IDL_FILE_FROM_ROOT);
                                IDLParser parser = new IDLParser(in_IDL);
                                parser.parse();
                        }catch(Exception e){
-                               System.err.println("Error in parsing IDL file " + idlFile + " " + e.getMessage());
+                               System.err.println("Error in parsing IDL file " + IDL_FILE_FROM_ROOT + " " + e.getMessage());
                        };
                
                        loadedIDL = true; 
diff --git a/Open-ILS/src/Android/src/org/evergreen/android/services/NotificationAlert.java b/Open-ILS/src/Android/src/org/evergreen/android/services/NotificationAlert.java
new file mode 100644 (file)
index 0000000..77c64e5
--- /dev/null
@@ -0,0 +1,47 @@
+package org.evergreen.android.services;
+
+import java.util.Date;
+
+import org.androwrapee.db.DatabaseClass;
+import org.androwrapee.db.DatabaseField;
+import org.androwrapee.db.IdField;
+
+@DatabaseClass
+public class NotificationAlert {
+       
+       public static final String tableName = "notifications";
+       
+       public static final int NOTIFICATION_INTENT = 123456;
+       
+       @IdField
+       public long id;
+       
+       @DatabaseField
+       public int intent_val;
+       
+       @DatabaseField
+       public Date triggerDate; 
+       
+       @DatabaseField
+       public String message;
+       
+       //required constructor for DAO
+       public NotificationAlert(){
+               
+       }
+       
+       public NotificationAlert(int id, int intent_val, Date triggerDate, String message ){
+               
+               this.id = id;
+               this.intent_val = intent_val;
+               this.triggerDate = triggerDate;
+               this.message = message;
+       }
+       
+       
+       @Override
+       public String toString() {
+               
+               return " Notification:[ id: " + id+ "; intent_val: "+intent_val+"; triggerDate : "+triggerDate+"; message: "+message+"]";
+       }
+}
diff --git a/Open-ILS/src/Android/src/org/evergreen/android/services/NotificationReceiver.java b/Open-ILS/src/Android/src/org/evergreen/android/services/NotificationReceiver.java
new file mode 100644 (file)
index 0000000..af0c221
--- /dev/null
@@ -0,0 +1,56 @@
+package org.evergreen.android.services;
+
+import org.evergreen.android.views.splashscreen.SplashActivity;
+
+import android.app.Notification;
+import android.app.NotificationManager;
+import android.app.PendingIntent;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.os.Bundle;
+import android.util.Log;
+
+public class NotificationReceiver extends BroadcastReceiver {
+
+       private String TAG = "NotificationManager";
+       public static final int NOTIFICATION_ID = 1;
+
+       @Override
+       public void onReceive(Context context, Intent intent) {
+
+               String ns = Context.NOTIFICATION_SERVICE;
+               NotificationManager mNotificationManager = (NotificationManager) context
+                               .getSystemService(ns);
+
+               
+               String checkoutMessage = intent.getStringExtra("checkoutMesage");
+
+               Log.d(TAG, "Message " + checkoutMessage);
+               // send notification
+
+               int icon = android.R.drawable.ic_dialog_alert;
+               CharSequence tickerText = "Checkout item due date";
+               long when = System.currentTimeMillis();
+
+               Notification notification = new Notification(icon, tickerText, when);
+
+               notification.defaults |= Notification.FLAG_AUTO_CANCEL;
+               
+               CharSequence contentTitle = "EG - checkout item due date";
+               CharSequence contentText = checkoutMessage;
+               // start evergreen
+               Intent notificationIntent = new Intent(context, SplashActivity.class);
+               notificationIntent.putExtra("jump", "checkout_items");
+
+               PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
+                               notificationIntent, 0);
+
+               notification.setLatestEventInfo(context, contentTitle, contentText,
+                               contentIntent);
+
+               mNotificationManager.notify(NOTIFICATION_ID, notification);
+
+       }
+
+}
diff --git a/Open-ILS/src/Android/src/org/evergreen/android/services/PeriodicServiceBroadcastReceiver.java b/Open-ILS/src/Android/src/org/evergreen/android/services/PeriodicServiceBroadcastReceiver.java
new file mode 100644 (file)
index 0000000..a341342
--- /dev/null
@@ -0,0 +1,69 @@
+package org.evergreen.android.services;
+
+import java.util.Calendar;
+
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.net.ConnectivityManager;
+import android.net.NetworkInfo;
+import android.preference.PreferenceManager;
+import android.util.Log;
+
+public class PeriodicServiceBroadcastReceiver extends BroadcastReceiver {
+
+       public static final int INTENT_ID = 123;
+
+       @Override
+       public void onReceive(Context context, Intent intent) {
+
+               // do update logic
+               boolean mustDoUpdate = false;
+
+               SharedPreferences prefs = PreferenceManager
+                               .getDefaultSharedPreferences(context);
+
+               // determine if to use alerts or not
+               boolean use_notifications = prefs.getBoolean("notifications_enabled",
+                               false);
+
+               if (use_notifications) {
+                       // if no updates have been made
+                               Calendar current = Calendar.getInstance();
+                               Calendar lastUpdateTime = Calendar.getInstance();
+                               lastUpdateTime
+                                               .setTimeInMillis(prefs.getLong("lastUpdateTime", 0));
+
+                               lastUpdateTime.add(Calendar.DAY_OF_MONTH,
+                                               ScheduledIntentService.SCHEDULE_TIME_INTERVAL);
+
+                               // if the last update time + elapsed scheduled time < current
+                               // time we must do an update
+                               if (lastUpdateTime.compareTo(current) == -1) {
+                                       mustDoUpdate = true;
+                               }
+
+
+                       Log.d("app", "Network connectivity change or alarm must do update "
+                                       + mustDoUpdate);
+
+                       ConnectivityManager connMgr = (ConnectivityManager) context
+                                       .getSystemService(Context.CONNECTIVITY_SERVICE);
+
+                       NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
+                       // if internet available
+                       if (networkInfo != null && networkInfo.isConnected()) {
+                               
+                               if (mustDoUpdate == true) {
+                                       Intent intentService = new Intent(context,
+                                                       ScheduledIntentService.class);
+                                       context.startService(intentService);
+                               }
+                       }
+
+               }
+
+       }
+
+}
diff --git a/Open-ILS/src/Android/src/org/evergreen/android/services/RebootReceiver.java b/Open-ILS/src/Android/src/org/evergreen/android/services/RebootReceiver.java
new file mode 100644 (file)
index 0000000..70cbd88
--- /dev/null
@@ -0,0 +1,44 @@
+package org.evergreen.android.services;
+
+import java.util.Calendar;
+
+import android.app.Activity;
+import android.app.AlarmManager;
+import android.app.PendingIntent;
+import android.content.BroadcastReceiver;
+import android.content.Context;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.preference.PreferenceManager;
+import android.widget.Toast;
+
+public class RebootReceiver extends BroadcastReceiver{
+
+       @Override
+       public void onReceive(Context context, Intent arg1) {
+               //reinitialize notifications
+       
+               SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
+               
+               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(context,PeriodicServiceBroadcastReceiver.class);
+                       bRecvIntent.setAction(ScheduledIntentService.ACTION);
+                       // update the current intent if it exists
+                       PendingIntent sender = PendingIntent.getBroadcast(context,
+                                       NotificationAlert.NOTIFICATION_INTENT + PeriodicServiceBroadcastReceiver.INTENT_ID, bRecvIntent,
+                                       PendingIntent.FLAG_UPDATE_CURRENT);
+
+                       // Get the AlarmManager service
+                       AlarmManager am = (AlarmManager) context.getSystemService(Activity.ALARM_SERVICE);
+                       am.setRepeating(AlarmManager.RTC, cal.getTimeInMillis(), 10000 * ScheduledIntentService.SCHEDULE_TIME_INTERVAL, sender);
+               }
+
+       }
+
+}
diff --git a/Open-ILS/src/Android/src/org/evergreen/android/services/ScheduledIntentService.java b/Open-ILS/src/Android/src/org/evergreen/android/services/ScheduledIntentService.java
new file mode 100644 (file)
index 0000000..471087a
--- /dev/null
@@ -0,0 +1,181 @@
+package org.evergreen.android.services;
+
+import java.io.InputStream;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.List;
+
+import org.androwrapee.db.DefaultDAO;
+import org.evergreen.android.accountAccess.AccountAccess;
+import org.evergreen.android.accountAccess.SessionNotFoundException;
+import org.evergreen.android.accountAccess.checkout.CircRecord;
+import org.evergreen.android.database.DatabaseManager;
+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.open_ils.idl.IDLParser;
+
+import android.app.AlarmManager;
+import android.app.IntentService;
+import android.app.PendingIntent;
+import android.content.Intent;
+import android.content.SharedPreferences;
+import android.net.ConnectivityManager;
+import android.preference.PreferenceManager;
+import android.util.Log;
+
+public class ScheduledIntentService extends IntentService{
+
+       public static Date lastUpdateServiceDate;
+       
+       public static String TAG = "ScheduledIntentService";
+       
+       public static String ACTION = "org.evergreen.updateservice";
+       
+       //fire up once a day
+       public static int SCHEDULE_TIME_INTERVAL = 1;
+       
+       public ScheduledIntentService(){
+               super("EvergreenIntentService");
+       }
+       
+       @Override
+       public int onStartCommand(Intent intent, int flags, int startId) {
+               Log.d(TAG, "Start service");
+               
+               return super.onStartCommand(intent, flags, startId);
+       }
+       
+       @Override
+       protected void onHandleIntent(Intent intent) {
+               
+                Date currentDate = new Date(System.currentTimeMillis());
+               
+                Log.d(TAG, "Notifications service started");
+                
+                /* Download the necessary IDL files for checkout items operations like au (for auth), circ, mvr and acp
+                 */
+                String idlFile = GlobalConfigs.IDL_FILE_FROM_ROOT + "?class=circ&class=au&class=mvr&class=acp";
+                try{
+                       Log.d("debug","Read fm");
+                       InputStream in_IDL = Utils.getNetInputStream(GlobalConfigs.httpAddress + idlFile);
+                       IDLParser parser = new IDLParser(in_IDL);
+                       parser.parse();
+               }catch(Exception e){
+                       System.err.println("Error in parsing IDL file " + idlFile + " " + e.getMessage());
+               };
+               
+               //login with the user credentials
+               AccountAccess accountAccess = AccountAccess.getAccountAccess(GlobalConfigs.httpAddress, (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE));
+               boolean auth = true;
+               try {
+                       accountAccess.authenticate();
+               } catch (NoNetworkAccessException e) {
+                       auth=false;
+                       e.printStackTrace();
+               } catch (NoAccessToServer e) {
+                       auth = false;
+                       e.printStackTrace();
+               }
+               
+               //if we managed to authenticate we start
+               if(auth){
+                       
+                       SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
+                       //int daysBeforeDueDate = sharedPreferences.getInt("notifications_days_before_expiration", 2);
+                       //TODO
+                       int daysBeforeDueDate = 2;
+                       ArrayList<CircRecord> circRecords = new ArrayList<CircRecord>();
+                       
+                       //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<NotificationAlert> daoNotifications = DatabaseManager.getDAOInstance(this, NotificationAlert.class, NotificationAlert.tableName);
+                       daoNotifications.open();
+
+                       // Fetch all alarms from database
+                       List<NotificationAlert> alarms = daoNotifications.fetchAll("");
+
+                       for(int i=0;i<alarms.size();i++){
+                               System.out.println("notification " + alarms.get(i));
+                               Log.d(TAG, "Notification " + alarms.get(i));
+                       }
+                       for (int i = 0; i < circRecords.size(); i++) {
+
+                               CircRecord checkoutRecord = circRecords.get(i);
+
+                               Date dueDate = checkoutRecord.getDueDateObject();
+               
+                               Calendar notificationDate = Calendar.getInstance();
+                               notificationDate.setTime(dueDate);
+                               
+                               
+                               notificationDate.add(Calendar.DAY_OF_MONTH, -daysBeforeDueDate);
+                               Log.d(TAG, " notification time start "+notificationDate.getTime() + " current date " + currentDate  + " date between " + currentDate);
+                               
+                               
+                               // if due date in the future
+                               if (currentDate.compareTo(notificationDate.getTime()) >= 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);
+               }
+               
+       }
+
+}
index 9a1a10d..51220ea 100644 (file)
@@ -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){