From 74c01c93a80899f61d137e3ed1b5740020c3ad71 Mon Sep 17 00:00:00 2001 From: Juan Miguel Boyero Corral Date: Fri, 27 Jan 2012 00:27:41 +0100 Subject: [PATCH] Added notification alerts --- SWADroid/gen/es/ugr/swad/swadroid/R.java | 2 + SWADroid/res/values-es/strings.xml | 2 + SWADroid/res/values/strings.xml | 2 +- .../swad/swadroid/model/DataBaseHelper.java | 6 +- ...otification.java => SWADNotification.java} | 4 +- .../modules/notifications/Notifications.java | 68 +++++++++++++++++-- 6 files changed, 71 insertions(+), 13 deletions(-) rename SWADroid/src/es/ugr/swad/swadroid/model/{Notification.java => SWADNotification.java} (98%) diff --git a/SWADroid/gen/es/ugr/swad/swadroid/R.java b/SWADroid/gen/es/ugr/swad/swadroid/R.java index 0f9359a5..b69c82e6 100644 --- a/SWADroid/gen/es/ugr/swad/swadroid/R.java +++ b/SWADroid/gen/es/ugr/swad/swadroid/R.java @@ -210,6 +210,8 @@ public final class R { public static final int noQuestionsPluggableTestsDownloadMsg=0x7f070045; public static final int noSubjectMsg=0x7f070039; public static final int notice=0x7f07002e; + public static final int notificationsAlertMsg=0x7f070073; + public static final int notificationsAlertTitle=0x7f070072; public static final int notificationsDownloadedMsg=0x7f070023; public static final int notificationsEmptyListMsg=0x7f070022; public static final int notificationsModuleLabel=0x7f070002; diff --git a/SWADroid/res/values-es/strings.xml b/SWADroid/res/values-es/strings.xml index 01673323..6401e7f0 100644 --- a/SWADroid/res/values-es/strings.xml +++ b/SWADroid/res/values-es/strings.xml @@ -117,4 +117,6 @@ plataforma de teleformaciĆ³n SWAD.\nObtenlo gratis aquĆ­: https://market.android.com/details?id=es.ugr.swad.swadroid TwitterFacebookGoogle+Lista de correoBlogAndroid Market + Nuevas notificaciones + nuevas notificaciones \ No newline at end of file diff --git a/SWADroid/res/values/strings.xml b/SWADroid/res/values/strings.xml index 87c66ed3..b8423ab9 100644 --- a/SWADroid/res/values/strings.xml +++ b/SWADroid/res/values/strings.xml @@ -123,7 +123,7 @@ Facebook Google+ Mailing list - BlogAndroid Market + BlogAndroid MarketNew notificationsnew notifications \ No newline at end of file diff --git a/SWADroid/src/es/ugr/swad/swadroid/model/DataBaseHelper.java b/SWADroid/src/es/ugr/swad/swadroid/model/DataBaseHelper.java index aa69a0dc..dece0d0a 100644 --- a/SWADroid/src/es/ugr/swad/swadroid/model/DataBaseHelper.java +++ b/SWADroid/src/es/ugr/swad/swadroid/model/DataBaseHelper.java @@ -122,7 +122,7 @@ public class DataBaseHelper { ent.getInt(params.getFirst()), ent.getInt(params.getSecond())); } else if(table.equals(Global.DB_TABLE_NOTIFICATIONS)) { - o = new Notification(ent.getInt("id"), + o = new SWADNotification(ent.getInt("id"), ent.getString("eventType"), ent.getLong("eventTime"), ent.getString("userSurname1"), @@ -272,7 +272,7 @@ public class DataBaseHelper { * Inserts a notification in database * @param n Notification to be inserted */ - public void insertNotification(Notification n) + public void insertNotification(SWADNotification n) { Entity ent = new Entity(Global.DB_TABLE_NOTIFICATIONS); @@ -413,7 +413,7 @@ public class DataBaseHelper { * @param prev Notification to be updated * @param actual Updated notification */ - public void updateNotification(Notification prev, Notification actual) + public void updateNotification(SWADNotification prev, SWADNotification actual) { List rows = db.getEntityList(Global.DB_TABLE_NOTIFICATIONS, "id = " + prev.getId()); Entity ent = rows.get(0); diff --git a/SWADroid/src/es/ugr/swad/swadroid/model/Notification.java b/SWADroid/src/es/ugr/swad/swadroid/model/SWADNotification.java similarity index 98% rename from SWADroid/src/es/ugr/swad/swadroid/model/Notification.java rename to SWADroid/src/es/ugr/swad/swadroid/model/SWADNotification.java index 7ac22819..37630b54 100644 --- a/SWADroid/src/es/ugr/swad/swadroid/model/Notification.java +++ b/SWADroid/src/es/ugr/swad/swadroid/model/SWADNotification.java @@ -26,7 +26,7 @@ import org.ksoap2.serialization.PropertyInfo; * Class for store a notification * @author Juan Miguel Boyero Corral * */ -public class Notification extends Model { +public class SWADNotification extends Model { /** * Notification type */ @@ -99,7 +99,7 @@ public class Notification extends Model { * @param summary Notification summary * @param status Notification summary */ - public Notification(long id, String eventType, long eventTime, + public SWADNotification(long id, String eventType, long eventTime, String userSurname1, String userSurname2, String userFirstName, String location, String summary, int status, String content) { diff --git a/SWADroid/src/es/ugr/swad/swadroid/modules/notifications/Notifications.java b/SWADroid/src/es/ugr/swad/swadroid/modules/notifications/Notifications.java index bc80bc99..6190943d 100644 --- a/SWADroid/src/es/ugr/swad/swadroid/modules/notifications/Notifications.java +++ b/SWADroid/src/es/ugr/swad/swadroid/modules/notifications/Notifications.java @@ -32,9 +32,13 @@ import es.ugr.swad.swadroid.Global; import es.ugr.swad.swadroid.R; import es.ugr.swad.swadroid.model.DataBaseHelper; import es.ugr.swad.swadroid.model.User; -import es.ugr.swad.swadroid.model.Notification; +import es.ugr.swad.swadroid.model.SWADNotification; import es.ugr.swad.swadroid.modules.Module; +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; import android.content.Context; +import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.util.Log; @@ -73,6 +77,14 @@ public class Notifications extends Module { * Cursor orderby parameter */ private String orderby = "eventTime DESC"; + /** + * Notifications counter + */ + private int notifCount; + /** + * Unique identifier for notification alerts + */ + private int NOTIF_ALERT_ID = 1982; /** * Notifications tag name for Logcat */ @@ -173,7 +185,7 @@ public class Notifications extends Module { protected void requestService() throws NoSuchAlgorithmException, IOException, XmlPullParserException, SoapFault, IllegalAccessException, InstantiationException { - + //Calculates next timestamp to be requested Long timestamp = new Long(dbHelper.getFieldOfLastNotification("eventTime")); timestamp++; @@ -182,7 +194,7 @@ public class Notifications extends Module { createRequest(); addParam("wsKey", User.getWsKey()); addParam("beginTime", timestamp); - sendRequest(Notification.class, false); + sendRequest(SWADNotification.class, false); if (result != null) { dbHelper.beginTransaction(); @@ -190,8 +202,8 @@ public class Notifications extends Module { //Stores notifications data returned by webservice response Vector res = (Vector) result; SoapObject soap = (SoapObject) res.get(1); - int csSize = soap.getPropertyCount(); - for (int i = 0; i < csSize; i++) { + notifCount = soap.getPropertyCount(); + for (int i = 0; i < notifCount; i++) { SoapObject pii = (SoapObject)soap.getProperty(i); Long notificationCode = new Long(pii.getProperty("notificationCode").toString()); String eventType = pii.getProperty("eventType").toString(); @@ -203,7 +215,7 @@ public class Notifications extends Module { String summary = pii.getProperty("summary").toString(); Integer status = new Integer(pii.getProperty("status").toString()); String content = pii.getProperty("content").toString(); - Notification n = new Notification(notificationCode, eventType, eventTime, userSurname1, userSurname2, userFirstName, location, summary, status, content); + SWADNotification n = new SWADNotification(notificationCode, eventType, eventTime, userSurname1, userSurname2, userFirstName, location, summary, status, content); dbHelper.insertNotification(n); if(isDebuggable) @@ -211,7 +223,7 @@ public class Notifications extends Module { } //Request finalized without errors - Log.i(TAG, "Retrieved " + csSize + " notifications"); + Log.i(TAG, "Retrieved " + notifCount + " notifications"); //Clear old notifications to control database size dbHelper.clearOldNotifications(SIZE_LIMIT); @@ -219,6 +231,46 @@ public class Notifications extends Module { dbHelper.endTransaction(); } } + + protected void alertNotif() { + if(notifCount > 0) { + //Obtain a reference to the notification service + String ns = Context.NOTIFICATION_SERVICE; + NotificationManager notManager = + (NotificationManager) getSystemService(ns); + + //Configure the alert + int icon = R.drawable.ic_launcher_swadroid; + long hour = System.currentTimeMillis(); + + Notification notif = + new Notification(icon, getString(R.string.notificationsAlertTitle), hour); + + //Configure the Intent + Context context = getApplicationContext(); + + Intent notIntent = new Intent(context, + Notifications.class); + + PendingIntent contIntent = PendingIntent.getActivity( + context, 0, notIntent, 0); + + notif.setLatestEventInfo( + context, getString(R.string.notificationsAlertTitle), notifCount + " " + + getString(R.string.notificationsAlertMsg), contIntent); + + //AutoCancel: alert disappears when pushed + notif.flags |= Notification.FLAG_AUTO_CANCEL; + + //Add sound, vibration and lights + notif.defaults |= Notification.DEFAULT_SOUND; + //notif.defaults |= Notification.DEFAULT_VIBRATE; + notif.defaults |= Notification.DEFAULT_LIGHTS; + + //Send alert + notManager.notify(NOTIF_ALERT_ID, notif); + } + } /* (non-Javadoc) * @see es.ugr.swad.swadroid.modules.Module#connect() @@ -238,6 +290,8 @@ public class Notifications extends Module { protected void postConnect() { refreshScreen(); Toast.makeText(this, R.string.notificationsDownloadedMsg, Toast.LENGTH_SHORT).show(); + + alertNotif(); } /**