add bootreceiver to reinitialize the notifications + notification code
authordrizea <danielrizea27@gmail.com>
Thu, 9 Aug 2012 14:55:44 +0000 (17:55 +0300)
committerdrizea <danielrizea27@gmail.com>
Thu, 9 Aug 2012 14:55:44 +0000 (17:55 +0300)
Open-ILS/src/Android/.classpath
Open-ILS/src/Android/AndroidManifest.xml
Open-ILS/src/Android/libs/androwrapee-1.1.0.jar [new file with mode: 0644]
Open-ILS/src/Android/src/org/evergreen/android/accountAccess/checkout/ItemsCheckOutListView.java
Open-ILS/src/Android/src/org/evergreen/android/accountAccess/checkout/NotificationAlert.java [new file with mode: 0644]
Open-ILS/src/Android/src/org/evergreen/android/accountAccess/checkout/NotificationReceiver.java
Open-ILS/src/Android/src/org/evergreen/android/accountAccess/checkout/RebootReceiver.java [new file with mode: 0644]
Open-ILS/src/Android/src/org/evergreen/android/database/DatabaseManager.java [new file with mode: 0644]

index 3c52578..ace0184 100644 (file)
@@ -9,5 +9,6 @@
        <classpathentry kind="lib" path="libs/org.opensrf2_serialized_reg.jar"/>
        <classpathentry kind="lib" path="libs/simple-xml-2.6.4.jar"/>
        <classpathentry kind="lib" path="libs/zxing_barcode.jar"/>
+       <classpathentry kind="lib" path="libs/androwrapee-1.1.0.jar"/>
        <classpathentry kind="output" path="bin/classes"/>
 </classpath>
index 1558a74..a64c282 100644 (file)
@@ -16,7 +16,7 @@
        <uses-feature android:name="android.hardware.camera.autofocus" />
        <uses-permission android:name="android.permission.READ_CALENDAR" />
     <uses-permission android:name="android.permission.WRITE_CALENDAR" />
-       
+       <uses-permission android:name="android.permission.REBOOT"/>
        
     <application
         android:icon="@drawable/evergreen_launcher_icon"
         
         <!-- Notification receiver -->
         <receiver  android:process=":remote" android:name=".accountAccess.checkout.NotificationReceiver"></receiver>
-
+               <!-- Receiver to reinit notifications on reboot -->
+               <receiver android:name=".accountAccess.checkout.RebootReceiver">
+                       <intent-filter>
+                       <action android:name="android.intent.action.BOOT_COMPLETED"/>
+                       </intent-filter>
+               </receiver>
+        
+        
         <activity
             android:name=".views.splashscreen.SplashActivity"
             android:label="@string/app_name"
diff --git a/Open-ILS/src/Android/libs/androwrapee-1.1.0.jar b/Open-ILS/src/Android/libs/androwrapee-1.1.0.jar
new file mode 100644 (file)
index 0000000..0f8977e
Binary files /dev/null and b/Open-ILS/src/Android/libs/androwrapee-1.1.0.jar differ
index a22054c..5da4696 100644 (file)
@@ -5,10 +5,12 @@ import java.util.Calendar;
 import java.util.Date;
 import java.util.List;
 
+import org.androwrapee.db.DefaultDAO;
 import org.evergreen.android.R;
 import org.evergreen.android.accountAccess.AccountAccess;
 import org.evergreen.android.accountAccess.MaxRenewalsException;
 import org.evergreen.android.accountAccess.SessionNotFoundException;
+import org.evergreen.android.database.DatabaseManager;
 import org.evergreen.android.globals.NoAccessToServer;
 import org.evergreen.android.globals.NoNetworkAccessException;
 import org.evergreen.android.globals.Utils;
@@ -61,8 +63,6 @@ public class ItemsCheckOutListView extends Activity {
        private TextView itemsNo;
 
        private Activity thisActivity;
-
-       private static int NOTIFICATION_INTENT = 123456;
        
        private TextView overdueItems;
        
@@ -190,18 +190,29 @@ 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();
-                               // add 5 minutes to the calendar object
-                               //cal.add(Calendar.MINUTE, 5);
 
                                cal.set(dueDate.getYear(), dueDate.getMonth(), dueDate.getDay(), dueDate.getHours(), dueDate.getMinutes());
 
@@ -209,18 +220,22 @@ public class ItemsCheckOutListView extends Activity {
                                cal.add(Calendar.HOUR, 4);
                                cal.add(Calendar.MINUTE, 37);
                                
+                               NotificationAlert notifications = daoNotifications.fetch(checkoutRecord.circ_id);
+                               
+                               if(notifications == null)
+                                       daoNotifications.insert(new NotificationAlert(checkoutRecord.circ_id, NotificationAlert.NOTIFICATION_INTENT
+                                                       + checkoutRecord.circ_id, cal.getTime(), "Checkout " + checkoutRecord.getAuthor() + " expires on " + checkoutRecord.getDueDate()), false);
+                               
                                Intent intent = new Intent(context, NotificationReceiver.class);
 
-                               System.out.println("Set due date " + cal
-                                               + " with intent val " + NOTIFICATION_INTENT
+                               System.out.println("Set due date " + cal.getTime()
+                                               + " with intent val " + NotificationAlert.NOTIFICATION_INTENT
                                                + checkoutRecord.circ_id);
-                               intent.putExtra("checkoutName", checkoutRecord.getAuthor());
+                               intent.putExtra("checkoutMessage", "The item " + checkoutRecord.getAuthor() + " is about to expire on " +checkoutRecord.getDueDate() );
                                
-                               // In reality, you would want to have a static variable for the
-                               // request code instead of 192837
                                // update the current intent if it exists
                                PendingIntent sender = PendingIntent.getBroadcast(this,
-                                               NOTIFICATION_INTENT + checkoutRecord.circ_id, intent,
+                                               NotificationAlert.NOTIFICATION_INTENT + checkoutRecord.circ_id, intent,
                                                PendingIntent.FLAG_UPDATE_CURRENT);
 
                                // Get the AlarmManager service
@@ -228,6 +243,7 @@ public class ItemsCheckOutListView extends Activity {
                                am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), sender);
                        }
                }
+               daoNotifications.close();
 
        }
 
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
new file mode 100644 (file)
index 0000000..8e4ca59
--- /dev/null
@@ -0,0 +1,47 @@
+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+"]";
+       }
+}
index d05aeab..0e80cea 100644 (file)
@@ -1,26 +1,54 @@
 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;
-import android.widget.Toast;
 
-public class NotificationReceiver extends BroadcastReceiver{
+public class NotificationReceiver extends BroadcastReceiver {
 
        private String TAG = "NotificationManager";
+       public static final int NOTIFICATION_ID = 1;
+
        @Override
        public void onReceive(Context context, Intent intent) {
 
-                Bundle bundle = intent.getExtras();
-            String checkoutItemName = bundle.getString("checkoutName");
-            Toast.makeText(context, "Notification received", Toast.LENGTH_SHORT).show();
-            
-            Log.d(TAG, "The " + checkoutItemName + " is about to expire");
-            
-            //send notification
+               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
new file mode 100644 (file)
index 0000000..5932147
--- /dev/null
@@ -0,0 +1,57 @@
+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();
+
+       }
+
+}
diff --git a/Open-ILS/src/Android/src/org/evergreen/android/database/DatabaseManager.java b/Open-ILS/src/Android/src/org/evergreen/android/database/DatabaseManager.java
new file mode 100644 (file)
index 0000000..8844877
--- /dev/null
@@ -0,0 +1,88 @@
+
+package org.evergreen.android.database;
+
+import java.util.HashMap;
+import java.util.logging.Logger;
+
+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 android.content.Context;
+import android.util.Log;
+
+/**
+ * The Class DatabaseDefaults.
+ */
+public class DatabaseManager {
+
+       public static String TAG = "DatabaseManager";
+       
+       /** The DATABASE NAME. */
+       public static final String DATABASE_NAME = "evergreen.db";
+
+       /** The DATABASE VERSION. */
+       public static final int DATABASE_VERSION = 1;
+
+       /** The db helper. */
+       private static DefaultDatabaseHelper dbHelper = null;
+
+       /** The singleton reflection managers map. */
+       @SuppressWarnings("rawtypes")
+       private static HashMap<Class, ReflectionManager> rmMap = new HashMap<Class, ReflectionManager>();
+
+       @SuppressWarnings("rawtypes")
+       private static HashMap<Class, DefaultDAO> daoMap = new HashMap<Class, DefaultDAO>();
+
+       /**
+        * Gets the Singleton database helper.
+        * 
+        * @return the dB helper
+        */
+       public static DefaultDatabaseHelper getDBHelper(Context context) {
+               if (dbHelper == null)
+                       dbHelper = new DefaultDatabaseHelper(context, DATABASE_NAME, DATABASE_VERSION, new Class[] { NotificationAlert.class}, new String[] {
+                                       NotificationAlert.tableName});
+               return dbHelper;
+       }
+
+       /**
+        * Gets a singleton instance of a reflection manager corresponding to a class.
+        * 
+        * @param cls the class
+        * @return the reflection manager instance
+        */
+       public static <T> ReflectionManager getReflectionManagerInstance(Class<T> cls) {
+               if (rmMap.containsKey(cls))
+                       return rmMap.get(cls);
+               try {
+                       ReflectionManager rm = new ReflectionManager(cls);
+                       rmMap.put(cls, rm);
+                       return rm;
+               } catch (IllegalClassStructureException ex) {
+                       ex.printStackTrace();
+                       Log.d(TAG, "Illegal Class Structure for class " + cls + ": " + ex.getMessage());
+                       return null;
+               }
+       }
+
+       /**
+        * Gets a singleton instance of a DefaultDAO object corresponding to a class.
+        * 
+        * @param <T> the generic type
+        * @param cls the class
+        * @param tableName the table name
+        * @return the DAO instance
+        */
+       @SuppressWarnings("unchecked")
+       public static synchronized <T> DefaultDAO<T> getDAOInstance(Context context, Class<T> cls, String tableName) {
+               if (daoMap.containsKey(cls))
+                       return daoMap.get(cls);
+               DefaultDAO<T> dao = new DefaultDAO<T>(cls, DatabaseManager.getDBHelper(context),
+                               DatabaseManager.getReflectionManagerInstance(cls), tableName);
+               daoMap.put(cls, dao);
+               return dao;
+       }
+}