Added notification alerts

This commit is contained in:
Juan Miguel Boyero Corral 2012-01-27 00:27:41 +01:00
parent e251e108c7
commit 74c01c93a8
6 changed files with 71 additions and 13 deletions

View File

@ -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;

View File

@ -117,4 +117,6 @@
plataforma de teleformación SWAD.\nObtenlo gratis aquí:
https://market.android.com/details?id=es.ugr.swad.swadroid</string>
<string name="twitterTitle">Twitter</string><string name="facebookTitle">Facebook</string><string name="googlePlusTitle">Google+</string><string name="mailingListTitle">Lista de correo</string><string name="blogTitle">Blog</string><string name="marketTitle">Android Market</string>
<string name="notificationsAlertTitle">Nuevas notificaciones</string>
<string name="notificationsAlertMsg">nuevas notificaciones</string>
</resources>

View File

@ -123,7 +123,7 @@
<string name="facebookTitle">Facebook</string>
<string name="googlePlusTitle">Google+</string>
<string name="mailingListTitle">Mailing list</string>
<string name="blogTitle">Blog</string><string name="marketTitle">Android Market</string>
<string name="blogTitle">Blog</string><string name="marketTitle">Android Market</string><string name="notificationsAlertTitle">New notifications</string><string name="notificationsAlertMsg">new notifications</string>
</resources>

View File

@ -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<Entity> rows = db.getEntityList(Global.DB_TABLE_NOTIFICATIONS, "id = " + prev.getId());
Entity ent = rows.get(0);

View File

@ -26,7 +26,7 @@ import org.ksoap2.serialization.PropertyInfo;
* Class for store a notification
* @author Juan Miguel Boyero Corral <juanmi1982@gmail.com> *
*/
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) {

View File

@ -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();
}
/**