|
|
@@ -1,7 +1,5 @@
|
|
|
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;
|
|
|
@@ -20,9 +18,6 @@ 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.
|
|
|
*
|
|
|
@@ -45,9 +40,7 @@ public class AlarmReceiver extends BroadcastReceiver {
|
|
|
@Override
|
|
|
@SuppressWarnings("deprecation")
|
|
|
public void onReceive(Context context, Intent intent) {
|
|
|
-
|
|
|
- Log.d("Alarm", "Received");
|
|
|
-
|
|
|
+
|
|
|
//Open the preferences to be available several times later.
|
|
|
SharedPreferences preferences = context.getSharedPreferences(GM.PREFERENCES.PREFERNCES, Context.MODE_PRIVATE);
|
|
|
|
|
|
@@ -73,10 +66,14 @@ public class AlarmReceiver extends BroadcastReceiver {
|
|
|
}
|
|
|
Cursor cursor;
|
|
|
|
|
|
- //Values for parsing
|
|
|
+ //Variables for parsing
|
|
|
String not, title_es, title_en, title_eu, text_es, text_en, text_eu, dtime, action;
|
|
|
int id, gm, duration;
|
|
|
|
|
|
+ //Variables for showinfg the notification
|
|
|
+ String lang = GM.getLang();
|
|
|
+ String title, text;
|
|
|
+
|
|
|
int counter = 0;
|
|
|
while (counter < 10 && o.contains("\"}")){
|
|
|
|
|
|
@@ -95,18 +92,73 @@ public class AlarmReceiver extends BroadcastReceiver {
|
|
|
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){
|
|
|
+ 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 + "\");");
|
|
|
+
|
|
|
+ //TODO: Show notification;
|
|
|
+ switch (lang) {
|
|
|
+ case "en":
|
|
|
+ title = title_en;
|
|
|
+ text = text_en;
|
|
|
+ break;
|
|
|
+ case "eu":
|
|
|
+ title = title_eu;
|
|
|
+ text = text_eu;
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ title = title_es;
|
|
|
+ text = text_es;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+
|
|
|
+ //Get the notification manager ready
|
|
|
+ NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
|
|
|
+
|
|
|
+ //Variables to create intents for each notification
|
|
|
+ Intent resultIntent;
|
|
|
+ TaskStackBuilder stackBuilder;
|
|
|
+
|
|
|
+ //Send the notification.
|
|
|
+ NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
|
|
|
+ .setSmallIcon(R.drawable.ic_notification)
|
|
|
+ .setContentTitle(title)
|
|
|
+ .setAutoCancel(true)
|
|
|
+ .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher))
|
|
|
+ .setVibrate((new long[]{400, 400, 400}))
|
|
|
+ .setColor(context.getResources().getColor(R.color.background_notification))
|
|
|
+ .setSubText(context.getString(R.string.app_name))
|
|
|
+ .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
|
|
|
+ .setContentText(text);
|
|
|
+
|
|
|
+ // Creates an intent for an Activity to be launched from the notification.
|
|
|
+ resultIntent = new Intent(context, MainActivity.class);
|
|
|
+ resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
|
|
|
+
|
|
|
+ //Set extras depending on type.
|
|
|
+ resultIntent.putExtra(GM.EXTRA.TEXT, text);
|
|
|
+ resultIntent.putExtra(GM.EXTRA.TITLE, title);
|
|
|
+ resultIntent.putExtra(GM.EXTRA.ACTION, action);
|
|
|
+
|
|
|
+ //Add the intent to the notification.
|
|
|
+ stackBuilder = TaskStackBuilder.create(context);
|
|
|
+ stackBuilder.addParentStack(MainActivity.class);
|
|
|
+
|
|
|
+ // Adds the Intent that starts the Activity to the top of the stack.
|
|
|
+ stackBuilder.addNextIntent(resultIntent);
|
|
|
+ PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
|
|
|
+ mBuilder.setContentIntent(resultPendingIntent);
|
|
|
+
|
|
|
+ //Actually send the notification.
|
|
|
+ mNotificationManager.notify(id, mBuilder.build());
|
|
|
+
|
|
|
}
|
|
|
cursor.close();
|
|
|
|
|
|
- //TODO: Show notification;
|
|
|
|
|
|
o = o.substring(o.indexOf("\"}") + 3);
|
|
|
counter ++;
|
|
|
@@ -218,7 +270,7 @@ 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);
|
|
|
+ //TODO: Set the most appropiate interval
|
|
|
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.
|