Builder.java 5.7 KB

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