Builder.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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.notification;
  22. import android.app.PendingIntent;
  23. import android.content.Context;
  24. import android.content.Intent;
  25. import android.support.v4.app.NotificationCompat;
  26. import java.util.Random;
  27. import de.appplant.cordova.plugin.notification.activity.ClickActivity;
  28. import de.appplant.cordova.plugin.notification.receiver.ClearReceiver;
  29. /**
  30. * Builder class for local notifications. Build fully configured local
  31. * notification specified by JSON object passed from JS side.
  32. */
  33. public class Builder {
  34. // Application context passed by constructor
  35. private final Context context;
  36. // Notification options passed by JS
  37. private final Options options;
  38. // Receiver to handle the clear event
  39. private Class<?> clearReceiver = ClearReceiver.class;
  40. // Activity to handle the click event
  41. private Class<?> clickActivity = ClickActivity.class;
  42. /**
  43. * Constructor
  44. *
  45. * @param options Notification options
  46. */
  47. public Builder(Options options) {
  48. this.context = options.getContext();
  49. this.options = options;
  50. }
  51. /**
  52. * Set clear receiver.
  53. *
  54. * @param receiver Broadcast receiver for the clear event.
  55. */
  56. public Builder setClearReceiver(Class<?> receiver) {
  57. this.clearReceiver = receiver;
  58. return this;
  59. }
  60. /**
  61. * Set click activity.
  62. *
  63. * @param activity The activity to handler the click event.
  64. */
  65. public Builder setClickActivity(Class<?> activity) {
  66. this.clickActivity = activity;
  67. return this;
  68. }
  69. /**
  70. * Creates the notification with all its options passed through JS.
  71. *
  72. * @return The final notification to display.
  73. */
  74. public Notification build() {
  75. int smallIcon = options.getSmallIcon();
  76. NotificationCompat.Builder builder;
  77. builder = new NotificationCompat.Builder(context, "group")
  78. .setDefaults(options.getDefaults())
  79. .setContentTitle(options.getTitle())
  80. .setContentText(options.getText())
  81. .setNumber(options.getBadgeNumber())
  82. .setAutoCancel(options.isAutoClear())
  83. .setOngoing(options.isSticky())
  84. .setColor(options.getColor())
  85. .setSound(options.getSound())
  86. .setVisibility(options.getVisibility())
  87. .setPriority(options.getPriority())
  88. .setShowWhen(options.getShowWhen())
  89. .setLights(options.getLedColor(), options.getLedOn(), options.getLedOff());
  90. if (smallIcon != 0) {
  91. builder.setSmallIcon(smallIcon);
  92. builder.setLargeIcon(options.getLargeIcon());
  93. } else {
  94. builder.setSmallIcon(options.getIcon());
  95. }
  96. applyDeleteReceiver(builder);
  97. applyContentReceiver(builder);
  98. return new Notification(context, options, builder);
  99. }
  100. /**
  101. * Set intent to handle the delete event. Will clean up some persisted
  102. * preferences.
  103. *
  104. * @param builder Local notification builder instance
  105. */
  106. private void applyDeleteReceiver(NotificationCompat.Builder builder) {
  107. if (clearReceiver == null)
  108. return;
  109. Intent intent = new Intent(context, clearReceiver)
  110. .setAction(options.getIdentifier())
  111. .putExtra(Options.EXTRA, options.toString());
  112. PendingIntent deleteIntent = PendingIntent.getBroadcast(
  113. context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  114. builder.setDeleteIntent(deleteIntent);
  115. }
  116. /**
  117. * Set intent to handle the click event. Will bring the app to
  118. * foreground.
  119. *
  120. * @param builder Local notification builder instance
  121. */
  122. private void applyContentReceiver(NotificationCompat.Builder builder) {
  123. if (clickActivity == null)
  124. return;
  125. Intent intent = new Intent(context, clickActivity)
  126. .putExtra(Options.EXTRA, options.toString())
  127. .setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
  128. int reqCode = new Random().nextInt();
  129. PendingIntent contentIntent = PendingIntent.getActivity(
  130. context, reqCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  131. builder.setContentIntent(contentIntent);
  132. }
  133. }