Receiver.java 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. Copyright 2013-2014 appPlant UG
  3. Licensed to the Apache Software Foundation (ASF) under one
  4. or more contributor license agreements. See the NOTICE file
  5. distributed with this work for additional information
  6. regarding copyright ownership. The ASF licenses this file
  7. to you under the Apache License, Version 2.0 (the
  8. "License"); you may not use this file except in compliance
  9. with the License. You may obtain a copy of the License at
  10. http://www.apache.org/licenses/LICENSE-2.0
  11. Unless required by applicable law or agreed to in writing,
  12. software distributed under the License is distributed on an
  13. "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. KIND, either express or implied. See the License for the
  15. specific language governing permissions and limitations
  16. under the License.
  17. */
  18. package de.appplant.cordova.plugin.localnotification;
  19. import java.util.Calendar;
  20. import org.json.JSONArray;
  21. import org.json.JSONException;
  22. import org.json.JSONObject;
  23. import android.content.BroadcastReceiver;
  24. import android.content.Context;
  25. import android.content.Intent;
  26. import android.os.Bundle;
  27. import android.support.v4.app.NotificationCompat.Builder;
  28. import de.appplant.cordova.plugin.notification.*;
  29. /**
  30. * The alarm receiver is triggered when a scheduled alarm is fired. This class
  31. * reads the information in the intent and displays this information in the
  32. * Android notification bar. The notification uses the default notification
  33. * sound and it vibrates the phone.
  34. */
  35. public class Receiver extends BroadcastReceiver {
  36. public static final String OPTIONS = "LOCAL_NOTIFICATION_OPTIONS";
  37. private Options options;
  38. @Override
  39. public void onReceive (Context context, Intent intent) {
  40. NotificationWrapper nWrapper = new NotificationWrapper(context,
  41. Receiver.class,LocalNotification.PLUGIN_NAME,OPTIONS);
  42. Options options = null;
  43. Bundle bundle = intent.getExtras();
  44. JSONObject args;
  45. try {
  46. args = new JSONObject(bundle.getString(OPTIONS));
  47. options = new Options(context).parse(args);
  48. } catch (JSONException e) {
  49. return;
  50. }
  51. this.options = options;
  52. NotificationBuilder builder = new NotificationBuilder(options,context,OPTIONS,
  53. DeleteIntentReceiver.class,ReceiverActivity.class);
  54. // The context may got lost if the app was not running before
  55. LocalNotification.setContext(context);
  56. fireTriggerEvent();
  57. if (options.getInterval() == 0) {
  58. } else if (isFirstAlarmInFuture()) {
  59. return;
  60. } else {
  61. JSONArray data = new JSONArray().put(options.getJSONObject());
  62. LocalNotification.fireEvent("updateCall", options.getId(), options.getJSON(),data);
  63. nWrapper.schedule(options.moveDate());
  64. }
  65. if (!LocalNotification.isInBackground && options.getForegroundMode()){
  66. if (options.getInterval() == 0) {
  67. LocalNotification.unpersist(options.getId());
  68. }
  69. nWrapper.showNotificationToast(options);
  70. fireTriggerEvent();
  71. } else {
  72. Builder notification = builder.buildNotification();
  73. nWrapper.showNotification(notification, options);
  74. }
  75. }
  76. /*
  77. * If you set a repeating alarm at 11:00 in the morning and it
  78. * should trigger every morning at 08:00 o'clock, it will
  79. * immediately fire. E.g. Android tries to make up for the
  80. * 'forgotten' reminder for that day. Therefore we ignore the event
  81. * if Android tries to 'catch up'.
  82. */
  83. private Boolean isFirstAlarmInFuture () {
  84. if (options.getInterval() > 0) {
  85. Calendar now = Calendar.getInstance();
  86. Calendar alarm = options.getCalendar();
  87. int alarmHour = alarm.get(Calendar.HOUR_OF_DAY);
  88. int alarmMin = alarm.get(Calendar.MINUTE);
  89. int currentHour = now.get(Calendar.HOUR_OF_DAY);
  90. int currentMin = now.get(Calendar.MINUTE);
  91. if (currentHour != alarmHour && currentMin != alarmMin) {
  92. return true;
  93. }
  94. }
  95. return false;
  96. }
  97. /**
  98. * Fires ontrigger event.
  99. */
  100. private void fireTriggerEvent () {
  101. JSONArray data = new JSONArray().put(options.getJSONObject());
  102. LocalNotification.fireEvent("trigger", options.getId(), options.getJSON(),data);
  103. }
  104. }