Builder.java 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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.net.Uri;
  26. import android.support.v4.app.NotificationCompat;
  27. import org.json.JSONObject;
  28. import java.util.Random;
  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 trigger event
  39. private Class<?> triggerReceiver;
  40. // Receiver to handle the clear event
  41. private Class<?> clearReceiver = ClearReceiver.class;
  42. // Activity to handle the click event
  43. private Class<?> clickActivity = ClickActivity.class;
  44. /**
  45. * Constructor
  46. *
  47. * @param context
  48. * Application context
  49. * @param options
  50. * Notification options
  51. */
  52. public Builder(Context context, JSONObject options) {
  53. this.context = context;
  54. this.options = new Options(context).parse(options);
  55. }
  56. /**
  57. * Constructor
  58. *
  59. * @param options
  60. * Notification options
  61. */
  62. public Builder(Options options) {
  63. this.context = options.getContext();
  64. this.options = options;
  65. }
  66. /**
  67. * Set trigger receiver.
  68. *
  69. * @param receiver
  70. * Broadcast receiver
  71. */
  72. public Builder setTriggerReceiver(Class<?> receiver) {
  73. this.triggerReceiver = receiver;
  74. return this;
  75. }
  76. /**
  77. * Set clear receiver.
  78. *
  79. * @param receiver
  80. * Broadcast receiver
  81. */
  82. public Builder setClearReceiver(Class<?> receiver) {
  83. this.clearReceiver = receiver;
  84. return this;
  85. }
  86. /**
  87. * Set click activity.
  88. *
  89. * @param activity
  90. * Activity
  91. */
  92. public Builder setClickActivity(Class<?> activity) {
  93. this.clickActivity = activity;
  94. return this;
  95. }
  96. /**
  97. * Creates the notification with all its options passed through JS.
  98. */
  99. public Notification build() {
  100. Uri sound = options.getSoundUri();
  101. int smallIcon = options.getSmallIcon();
  102. int ledColor = options.getLedColor();
  103. NotificationCompat.Builder builder;
  104. builder = new NotificationCompat.Builder(context)
  105. .setDefaults(0)
  106. .setContentTitle(options.getTitle())
  107. .setContentText(options.getText())
  108. .setNumber(options.getBadgeNumber())
  109. .setTicker(options.getText())
  110. .setAutoCancel(options.isAutoClear())
  111. .setOngoing(options.isOngoing())
  112. .setColor(options.getColor());
  113. if (ledColor != 0) {
  114. builder.setLights(ledColor, 100, 100);
  115. }
  116. if (sound != null) {
  117. builder.setSound(sound);
  118. }
  119. if (smallIcon == 0) {
  120. builder.setSmallIcon(options.getIcon());
  121. } else {
  122. builder.setSmallIcon(options.getSmallIcon());
  123. builder.setLargeIcon(options.getIconBitmap());
  124. }
  125. applyDeleteReceiver(builder);
  126. applyContentReceiver(builder);
  127. return new Notification(context, options, builder, triggerReceiver);
  128. }
  129. /**
  130. * Set intent to handle the delete event. Will clean up some persisted
  131. * preferences.
  132. *
  133. * @param builder
  134. * Local notification builder instance
  135. */
  136. private void applyDeleteReceiver(NotificationCompat.Builder builder) {
  137. if (clearReceiver == null)
  138. return;
  139. Intent intent = new Intent(context, clearReceiver)
  140. .setAction(options.getIdStr())
  141. .putExtra(Options.EXTRA, options.toString());
  142. PendingIntent deleteIntent = PendingIntent.getBroadcast(
  143. context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  144. builder.setDeleteIntent(deleteIntent);
  145. }
  146. /**
  147. * Set intent to handle the click event. Will bring the app to
  148. * foreground.
  149. *
  150. * @param builder
  151. * Local notification builder instance
  152. */
  153. private void applyContentReceiver(NotificationCompat.Builder builder) {
  154. if (clickActivity == null)
  155. return;
  156. Intent intent = new Intent(context, clickActivity)
  157. .putExtra(Options.EXTRA, options.toString())
  158. .setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
  159. int reqCode = new Random().nextInt();
  160. PendingIntent contentIntent = PendingIntent.getActivity(
  161. context, reqCode, intent, PendingIntent.FLAG_UPDATE_CURRENT);
  162. builder.setContentIntent(contentIntent);
  163. }
  164. }