AlarmReceiver.java 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. package com.ivalentin.margolariak;
  2. import android.app.AlarmManager;
  3. import android.app.NotificationManager;
  4. import android.app.PendingIntent;
  5. import android.content.ComponentName;
  6. import android.content.Context;
  7. import android.content.Intent;
  8. import android.content.SharedPreferences;
  9. import android.content.pm.PackageManager;
  10. import android.graphics.BitmapFactory;
  11. import android.media.RingtoneManager;
  12. import android.support.v4.app.NotificationCompat;
  13. import android.support.v4.app.TaskStackBuilder;
  14. import android.content.BroadcastReceiver;
  15. import android.util.Log;
  16. /**
  17. * Manages alarms to perform actions in the background, such as sync and receive notifications.
  18. *
  19. * @see BroadcastReceiver
  20. *
  21. * @author Iñigo Valentin
  22. *
  23. */
  24. public class AlarmReceiver extends BroadcastReceiver {
  25. /**
  26. * Actions to be performed when an alarm is received.
  27. * Performs a full sync, and gets the pending notifications.
  28. *
  29. * @param context Context of the activity or application
  30. * @param intent Receiver intent
  31. *
  32. * @see android.content.BroadcastReceiver#onReceive(android.content.Context, android.content.Intent)
  33. */
  34. @Override
  35. @SuppressWarnings("deprecation")
  36. public void onReceive(Context context, Intent intent) {
  37. Log.d("Alarm", "Received");
  38. //Open the preferences to be available several times later.
  39. SharedPreferences settings = context.getSharedPreferences(GM.PREF, Context.MODE_PRIVATE);
  40. FetchURL fu;
  41. //Check if the user wants to receive notifications
  42. if (settings.getInt(GM.PREF_NOTIFICATION , GM.DEFAULT_PREF_NOTIFICATION) == 1){
  43. //Get the file
  44. try{
  45. fu = new FetchURL();
  46. fu.Run(GM.SERVER + "/app/notifications.php");
  47. Log.d("Alarm", "Fetched notifications");
  48. //Parse info
  49. String o = fu.getOutput().toString();
  50. while (o.contains("<notification>")){
  51. //Get non-language-dependant fields
  52. String notification = o.substring(o.indexOf("<notification>") + 14, o.indexOf("</notification>"));
  53. String id = notification.substring(notification.indexOf("<id>") + 4, notification.indexOf("</id>"));
  54. String action = notification.substring(notification.indexOf("<action>") + 8, notification.indexOf("</action>"));
  55. //Is the notification seen already?
  56. if(!settings.getBoolean(GM.NOTIFICATION_SEEN_ + id, false)){
  57. String lang = GM.getLang();
  58. String title = notification.substring(notification.indexOf("<title_" + lang + ">") + 10, notification.indexOf("</title_" + lang + ">"));
  59. String text = notification.substring(notification.indexOf("<text_" + lang + ">") + 9, notification.indexOf("</text_" + lang + ">"));
  60. //Get the notification manager ready
  61. NotificationManager mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
  62. //Variables to create intents for each notification
  63. Intent resultIntent;
  64. TaskStackBuilder stackBuilder;
  65. //Send the notification.
  66. NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
  67. .setSmallIcon(R.drawable.ic_notification)
  68. .setContentTitle(title)
  69. .setAutoCancel(true)
  70. .setLargeIcon(BitmapFactory.decodeResource(context.getResources(), R.drawable.ic_launcher))
  71. .setVibrate((new long[] { 400, 400, 400}))
  72. .setColor(context.getResources().getColor(R.color.background_notification))
  73. .setSubText(context.getString(R.string.app_name))
  74. .setSound(RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION))
  75. .setContentText(text);
  76. // Creates an intent for an Activity to be launched from the notification.
  77. resultIntent = new Intent(context, MainActivity.class);
  78. resultIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
  79. //Set extras depending on type.
  80. resultIntent.putExtra(GM.EXTRA_TEXT, text);
  81. resultIntent.putExtra(GM.EXTRA_TITLE, title);
  82. resultIntent.putExtra(GM.EXTRA_ACTION, action);
  83. //Add the intent to the notification.
  84. stackBuilder = TaskStackBuilder.create(context);
  85. stackBuilder.addParentStack(MainActivity.class);
  86. // Adds the Intent that starts the Activity to the top of the stack.
  87. stackBuilder.addNextIntent(resultIntent);
  88. PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
  89. mBuilder.setContentIntent(resultPendingIntent);
  90. //Actually send the notification.
  91. mNotificationManager.notify(Integer.parseInt(id), mBuilder.build());
  92. //Mark as notified
  93. SharedPreferences.Editor editor = settings.edit();
  94. editor.putBoolean(GM.NOTIFICATION_SEEN_ + id, true);
  95. editor.apply();
  96. }
  97. o = o.substring(o.indexOf("</notification>") + 15);
  98. }
  99. }
  100. catch(Exception ex) {
  101. Log.e("Notification error", "Error fetching remote file: " + ex.toString());
  102. }
  103. }
  104. //Perform a background sync
  105. new Sync(context).execute();
  106. }
  107. /**
  108. * Sets a repeating alarm.
  109. * When the alarm fires, the app broadcasts an Intent to this WakefulBroadcastReceiver.
  110. * @param context The context of the app
  111. */
  112. public void setAlarm(Context context) {
  113. AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
  114. Intent intent = new Intent(context, AlarmReceiver.class);
  115. PendingIntent alarmIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
  116. //Set the alarm cycle.
  117. alarmMgr.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, GM.PERIOD_SYNC, GM.PERIOD_SYNC, alarmIntent);
  118. // Enable SampleBootReceiver to automatically restart the alarm when the device is rebooted.
  119. ComponentName receiver = new ComponentName(context, BootReceiver.class);
  120. PackageManager pm = context.getPackageManager();
  121. pm.setComponentEnabledSetting(receiver, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
  122. }
  123. }