TriggerReceiver.java 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Apache 2.0 License
  3. *
  4. * Copyright (c) Sebastian Katzer 2017
  5. *
  6. * This file contains Original Code and/or Modifications of Original Code
  7. * as defined in and that are subject to the Apache License
  8. * Version 2.0 (the 'License'). You may not use this file except in
  9. * compliance with the License. Please obtain a copy of the License at
  10. * http://opensource.org/licenses/Apache-2.0/ and read it before using this
  11. * file.
  12. *
  13. * The Original Code and all software distributed under the License are
  14. * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
  15. * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
  16. * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
  17. * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
  18. * Please see the License for the specific language governing rights and
  19. * limitations under the License.
  20. */
  21. package de.appplant.cordova.plugin.localnotification;
  22. import android.content.Context;
  23. import android.os.Bundle;
  24. import android.os.PowerManager;
  25. import de.appplant.cordova.plugin.notification.Builder;
  26. import de.appplant.cordova.plugin.notification.Manager;
  27. import de.appplant.cordova.plugin.notification.Notification;
  28. import de.appplant.cordova.plugin.notification.Options;
  29. import de.appplant.cordova.plugin.notification.Request;
  30. import de.appplant.cordova.plugin.notification.receiver.AbstractTriggerReceiver;
  31. import static android.content.Context.POWER_SERVICE;
  32. import static android.os.Build.VERSION.SDK_INT;
  33. import static android.os.Build.VERSION_CODES.LOLLIPOP;
  34. /**
  35. * The alarm receiver is triggered when a scheduled alarm is fired. This class
  36. * reads the information in the intent and displays this information in the
  37. * Android notification bar. The notification uses the default notification
  38. * sound and it vibrates the phone.
  39. */
  40. public class TriggerReceiver extends AbstractTriggerReceiver {
  41. /**
  42. * Called when a local notification was triggered. Does present the local
  43. * notification, re-schedule the alarm if necessary and fire trigger event.
  44. *
  45. * @param notification Wrapper around the local notification.
  46. * @param bundle The bundled extras.
  47. */
  48. @Override
  49. public void onTrigger (Notification notification, Bundle bundle) {
  50. boolean isUpdate = bundle.getBoolean(Notification.EXTRA_UPDATE, false);
  51. Context context = notification.getContext();
  52. Options options = notification.getOptions();
  53. Manager manager = Manager.getInstance(context);
  54. int badge = options.getBadgeNumber();
  55. if (badge > 0) {
  56. manager.setBadge(badge);
  57. }
  58. if (options.shallWakeUp()) {
  59. wakeUp(context);
  60. }
  61. notification.show();
  62. if (options.isInfiniteTrigger()) {
  63. manager.schedule(new Request(options), this.getClass());
  64. }
  65. if (!isUpdate) {
  66. LocalNotification.fireEvent("trigger", notification);
  67. }
  68. }
  69. /**
  70. * Wakeup the device.
  71. *
  72. * @param context The application context.
  73. */
  74. private void wakeUp (Context context) {
  75. PowerManager pm = (PowerManager) context.getSystemService(POWER_SERVICE);
  76. if (pm == null)
  77. return;
  78. int level = PowerManager.SCREEN_DIM_WAKE_LOCK
  79. | PowerManager.ACQUIRE_CAUSES_WAKEUP;
  80. PowerManager.WakeLock wakeLock = pm.newWakeLock(
  81. level, "LocalNotification");
  82. wakeLock.setReferenceCounted(false);
  83. wakeLock.acquire(1000);
  84. if (SDK_INT >= LOLLIPOP) {
  85. wakeLock.release(PowerManager.RELEASE_FLAG_WAIT_FOR_NO_PROXIMITY);
  86. } else {
  87. wakeLock.release();
  88. }
  89. }
  90. /**
  91. * Build notification specified by options.
  92. *
  93. * @param builder Notification builder.
  94. * @param bundle The bundled extras.
  95. */
  96. @Override
  97. public Notification buildNotification (Builder builder, Bundle bundle) {
  98. return builder
  99. .setClickActivity(ClickReceiver.class)
  100. .setClearReceiver(ClearReceiver.class)
  101. .setExtras(bundle)
  102. .build();
  103. }
  104. }