|
|
@@ -31,6 +31,7 @@ import android.support.v4.app.NotificationCompat;
|
|
|
import android.support.v4.app.NotificationCompat.MessagingStyle.Message;
|
|
|
import android.support.v4.media.app.NotificationCompat.MediaStyle;
|
|
|
import android.support.v4.media.session.MediaSessionCompat;
|
|
|
+import android.util.SparseArray;
|
|
|
|
|
|
import java.util.List;
|
|
|
import java.util.Random;
|
|
|
@@ -45,8 +46,12 @@ import static de.appplant.cordova.plugin.notification.Notification.EXTRA_UPDATE;
|
|
|
* Builder class for local notifications. Build fully configured local
|
|
|
* notification specified by JSON object passed from JS side.
|
|
|
*/
|
|
|
+@SuppressWarnings("Convert2Diamond")
|
|
|
public final class Builder {
|
|
|
|
|
|
+ // Cache for the builder instances
|
|
|
+ private static SparseArray<NotificationCompat.Builder> cache = null;
|
|
|
+
|
|
|
// Application context passed by constructor
|
|
|
private final Context context;
|
|
|
|
|
|
@@ -123,7 +128,7 @@ public final class Builder {
|
|
|
extras.putInt(Notification.EXTRA_ID, options.getId());
|
|
|
extras.putString(Options.EXTRA_SOUND, sound.toString());
|
|
|
|
|
|
- builder = new NotificationCompat.Builder(context, Manager.CHANNEL_ID)
|
|
|
+ builder = findOrCreateBuilder()
|
|
|
.setDefaults(options.getDefaults())
|
|
|
.setExtras(extras)
|
|
|
.setOnlyAlertOnce(false)
|
|
|
@@ -148,6 +153,7 @@ public final class Builder {
|
|
|
}
|
|
|
|
|
|
if (options.isWithProgressBar()) {
|
|
|
+ cacheBuilder(builder);
|
|
|
builder.setProgress(
|
|
|
options.getProgressMaxValue(),
|
|
|
options.getProgressValue(),
|
|
|
@@ -406,4 +412,35 @@ public final class Builder {
|
|
|
return extras != null && extras.getBoolean(EXTRA_UPDATE, false);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Returns a cached builder instance or creates a new one.
|
|
|
+ */
|
|
|
+ private NotificationCompat.Builder findOrCreateBuilder() {
|
|
|
+ NotificationCompat.Builder builder = null;
|
|
|
+ int key = options.getId();
|
|
|
+
|
|
|
+ if (cache != null) {
|
|
|
+ builder = cache.get(key);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (builder == null) {
|
|
|
+ builder = new NotificationCompat.Builder(context, Manager.CHANNEL_ID);
|
|
|
+ }
|
|
|
+
|
|
|
+ return builder;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Caches the builder instance so it can be used later.
|
|
|
+ *
|
|
|
+ * @param builder The instance to cache.
|
|
|
+ */
|
|
|
+ private void cacheBuilder (NotificationCompat.Builder builder) {
|
|
|
+ if (cache == null) {
|
|
|
+ cache = new SparseArray<NotificationCompat.Builder>();
|
|
|
+ }
|
|
|
+
|
|
|
+ cache.put(options.getId(), builder);
|
|
|
+ }
|
|
|
+
|
|
|
}
|