|
|
@@ -2,6 +2,7 @@ package com.ivalentin.margolariak;
|
|
|
|
|
|
//TODO: This doesn't work. Revise once the notification API is ready
|
|
|
|
|
|
+import android.app.Activity;
|
|
|
import android.app.AlarmManager;
|
|
|
import android.app.NotificationManager;
|
|
|
import android.app.PendingIntent;
|
|
|
@@ -19,6 +20,9 @@ import android.support.v4.app.TaskStackBuilder;
|
|
|
import android.content.BroadcastReceiver;
|
|
|
import android.util.Log;
|
|
|
|
|
|
+import org.json.JSONArray;
|
|
|
+import org.json.JSONObject;
|
|
|
+
|
|
|
/**
|
|
|
* Manages alarms to perform actions in the background, such as sync and receive notifications.
|
|
|
*
|
|
|
@@ -55,11 +59,65 @@ public class AlarmReceiver extends BroadcastReceiver {
|
|
|
//Get the file
|
|
|
try{
|
|
|
fu = new FetchURL();
|
|
|
- fu.Run(GM.API.SERVER + "/app/notifications.php");
|
|
|
- Log.d("Alarm", "Fetched notifications");
|
|
|
+ fu.Run(GM.API.SERVER + GM.API.NOTIFICATION.PATH);
|
|
|
+ Log.d("NOTIFICATIONS", "Fetched notifications");
|
|
|
|
|
|
//Parse info
|
|
|
String o = fu.getOutput().toString();
|
|
|
+
|
|
|
+ //Open database
|
|
|
+ SQLiteDatabase db = context.openOrCreateDatabase(GM.DB.NAME, Activity.MODE_PRIVATE, null);
|
|
|
+ if (db.isReadOnly()){
|
|
|
+ Log.e("NOTIFICATIONS", "Database is locked and in read only mode.");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Cursor cursor;
|
|
|
+
|
|
|
+ //Values for parsing
|
|
|
+ String not, title_es, title_en, title_eu, text_es, text_en, text_eu, dtime, action;
|
|
|
+ int id, gm, duration;
|
|
|
+
|
|
|
+ int counter = 0;
|
|
|
+ while (counter < 10 && o.contains("\"}")){
|
|
|
+
|
|
|
+ //Get the notifications
|
|
|
+ not = o.substring(o.indexOf("{\"") + 2, o.indexOf("\"}"));
|
|
|
+
|
|
|
+ //Extract data
|
|
|
+ id = Integer.valueOf(not.substring(not.indexOf("\"id\":") + 6, not.indexOf("\",")));
|
|
|
+ title_es = not.substring(not.indexOf("\"title_es\":") + 12, not.indexOf("\"", not.indexOf("\"title_es\":") + 13));
|
|
|
+ title_en = not.substring(not.indexOf("\"title_en\":") + 12, not.indexOf("\"", not.indexOf("\"title_en\":") + 13));
|
|
|
+ title_eu = not.substring(not.indexOf("\"title_eu\":") + 12, not.indexOf("\"", not.indexOf("\"title_eu\":") + 13));
|
|
|
+ text_es = not.substring(not.indexOf("\"text_es\":") + 11, not.indexOf("\"", not.indexOf("\"text_es\":") + 12));
|
|
|
+ text_en = not.substring(not.indexOf("\"text_en\":") + 11, not.indexOf("\"", not.indexOf("\"text_en\":") + 12));
|
|
|
+ text_eu = not.substring(not.indexOf("\"text_eu\":") + 11, not.indexOf("\"", not.indexOf("\"text_eu\":") + 12));
|
|
|
+ dtime = not.substring(not.indexOf("\"dtime\":") + 9, not.indexOf("\"", not.indexOf("\"dtime\":") + 10));
|
|
|
+ action = not.substring(not.indexOf("\"action\":") + 10, not.indexOf("\"", not.indexOf("\"action\":") + 11));
|
|
|
+ gm = Integer.valueOf(not.substring(not.indexOf("\"gm\":") + 6, not.indexOf("\"", not.indexOf("\"gm\":") + 7)));
|
|
|
+ duration = Integer.valueOf(not.substring(not.indexOf("\"duration\":") + 12, not.indexOf("\"", not.indexOf("\"duration\":") + 13)));
|
|
|
+ duration = 60;
|
|
|
+
|
|
|
+
|
|
|
+ //Compare with the database
|
|
|
+ cursor = db.rawQuery("SELECT id FROM notification WHERE id = " + id + ";", null);
|
|
|
+ if (cursor.getCount() == 0){
|
|
|
+ db.execSQL("INSERT INTO notification (id, title_es, title_en, title_eu, text_es, text_en, text_eu, dtime, internal, duration, action) VALUES " +
|
|
|
+ "(" + id + ", \"" + title_es + "\", \"" + title_en + "\", \"" + title_eu + "\", \"" + text_es + "\", \"" + text_en + "\", \"" + text_eu + "\", \"" + dtime + "\", " + gm + ", " + duration + ", \"" + action + "\");");
|
|
|
+ }
|
|
|
+ cursor.close();
|
|
|
+
|
|
|
+ //TODO: Show notification;
|
|
|
+
|
|
|
+ o = o.substring(o.indexOf("\"}") + 3);
|
|
|
+ counter ++;
|
|
|
+ }
|
|
|
+ db.close();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ //Parse info
|
|
|
+ /*String o = fu.getOutput().toString();
|
|
|
while (o.contains("<notification>")){
|
|
|
//Get non-language-dependant fields
|
|
|
String notification = o.substring(o.indexOf("<notification>") + 14, o.indexOf("</notification>"));
|
|
|
@@ -131,10 +189,13 @@ public class AlarmReceiver extends BroadcastReceiver {
|
|
|
}
|
|
|
}
|
|
|
o = o.substring(o.indexOf("</notification>") + 15);
|
|
|
- }
|
|
|
+ }*/
|
|
|
+ }
|
|
|
+ catch(NumberFormatException ex) {
|
|
|
+ Log.e("NOTIFICATION", "Error parsing remote file: " + ex.toString());
|
|
|
}
|
|
|
- catch(Exception ex) {
|
|
|
- Log.e("Notification error", "Error fetching remote file: " + ex.toString());
|
|
|
+ catch (Exception ex){
|
|
|
+ Log.e("NOTIFICATION", "Error fetching notifications: " + ex.toString());
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -157,7 +218,8 @@ public class AlarmReceiver extends BroadcastReceiver {
|
|
|
PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
|
|
|
|
|
|
//Set the alarm cycle.
|
|
|
- alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, GM.PERIOD_SYNC.FESTIVALS, GM.PERIOD_SYNC.NORMAL, alarmIntent);
|
|
|
+ //alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, GM.PERIOD_SYNC.FESTIVALS, GM.PERIOD_SYNC.NORMAL, alarmIntent);
|
|
|
+ alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, GM.PERIOD_SYNC.FESTIVALS, GM.PERIOD_SYNC.FESTIVALS, alarmIntent);
|
|
|
|
|
|
// Enable SampleBootReceiver to automatically restart the alarm when the device is rebooted.
|
|
|
ComponentName receiver = new ComponentName(context, BootReceiver.class);
|