5555 * @author Aleksandar Gyorev, Anna Orlova
5656 */
5757public class LeanplumInbox {
58- static boolean isInboxImagePrefetchingEnabled = true ;
59- /**
60- * Should be like this until Newsfeed is removed for backward compatibility.
61- */
62- static Newsfeed instance = new Newsfeed ();
58+ private static LeanplumInbox instance = new LeanplumInbox ();
59+
6360 static Set <String > downloadedImageUrls ;
61+ static boolean isInboxImagePrefetchingEnabled = true ;
6462
65- // Inbox properties.
6663 private int unreadCount ;
6764 private Map <String , LeanplumInboxMessage > messages ;
6865 private boolean didLoad = false ;
69- private List <InboxChangedCallback > changedCallbacks ;
70- private Object updatingLock = new Object ();
7166
72- LeanplumInbox () {
67+ private final List <InboxChangedCallback > changedCallbacks ;
68+ private final Object updatingLock = new Object ();
69+
70+ private LeanplumInbox () {
7371 this .unreadCount = 0 ;
7472 this .messages = new HashMap <>();
7573 this .didLoad = false ;
@@ -78,17 +76,113 @@ public class LeanplumInbox {
7876 }
7977
8078 /**
81- * Static 'getInstance' method .
79+ * Disable prefetching images .
8280 */
83- static LeanplumInbox getInstance () {
84- return instance ;
81+ public static void disableImagePrefetching () {
82+ isInboxImagePrefetchingEnabled = false ;
8583 }
8684
8785 /**
88- * Disable prefetching images .
86+ * Returns the number of all inbox messages on the device .
8987 */
90- public static void disableImagePrefetching () {
91- isInboxImagePrefetchingEnabled = false ;
88+ public int count () {
89+ return messages .size ();
90+ }
91+
92+ /**
93+ * Returns the number of the unread inbox messages on the device.
94+ */
95+ public int unreadCount () {
96+ return unreadCount ;
97+ }
98+
99+ /**
100+ * Returns the identifiers of all inbox messages on the device sorted in ascending
101+ * chronological order, i.e. the id of the oldest message is the first one, and the most recent
102+ * one is the last one in the array.
103+ */
104+ public List <String > messagesIds () {
105+ List <String > messageIds = new ArrayList <>(messages .keySet ());
106+ try {
107+ Collections .sort (messageIds , new Comparator <String >() {
108+ @ Override
109+ public int compare (String firstMessageId , String secondMessageId ) {
110+ // Message that is null will be moved to the back of the list.
111+ LeanplumInboxMessage firstMessage = messageForId (firstMessageId );
112+ if (firstMessage == null ) {
113+ return -1 ;
114+ }
115+ LeanplumInboxMessage secondMessage = messageForId (secondMessageId );
116+ if (secondMessage == null ) {
117+ return 1 ;
118+ }
119+ // Message with null date will be moved to the back of the list.
120+ Date firstDate = firstMessage .getDeliveryTimestamp ();
121+ if (firstDate == null ) {
122+ return -1 ;
123+ }
124+ Date secondDate = secondMessage .getDeliveryTimestamp ();
125+ if (secondDate == null ) {
126+ return 1 ;
127+ }
128+ return firstDate .compareTo (secondDate );
129+ }
130+ });
131+ } catch (Throwable t ) {
132+ Util .handleException (t );
133+ }
134+ return messageIds ;
135+ }
136+
137+ /**
138+ * Returns a List containing all of the inbox messages sorted chronologically ascending (i.e.
139+ * the oldest first and the newest last).
140+ */
141+ public List <LeanplumInboxMessage > allMessages () {
142+ return allMessages (new ArrayList <LeanplumInboxMessage >());
143+ }
144+
145+ /**
146+ * Returns a List containing all of the unread inbox messages sorted chronologically ascending
147+ * (i.e. the oldest first and the newest last).
148+ */
149+ public List <LeanplumInboxMessage > unreadMessages () {
150+ return unreadMessages (new ArrayList <LeanplumInboxMessage >());
151+ }
152+
153+ /**
154+ * Returns the inbox messages associated with the given getMessageId identifier.
155+ */
156+ public LeanplumInboxMessage messageForId (String messageId ) {
157+ return messages .get (messageId );
158+ }
159+
160+ /**
161+ * Add a callback for when the inbox receives new values from the server.
162+ */
163+ public void addChangedHandler (InboxChangedCallback handler ) {
164+ synchronized (changedCallbacks ) {
165+ changedCallbacks .add (handler );
166+ }
167+ if (this .didLoad ) {
168+ handler .inboxChanged ();
169+ }
170+ }
171+
172+ /**
173+ * Removes a inbox changed callback.
174+ */
175+ public void removeChangedHandler (InboxChangedCallback handler ) {
176+ synchronized (changedCallbacks ) {
177+ changedCallbacks .remove (handler );
178+ }
179+ }
180+
181+ /**
182+ * Static 'getInstance' method.
183+ */
184+ static LeanplumInbox getInstance () {
185+ return instance ;
92186 }
93187
94188 boolean isInboxImagePrefetchingEnabled () {
@@ -199,8 +293,8 @@ void save() {
199293 Map <String , Object > messages = new HashMap <>();
200294 for (Map .Entry <String , LeanplumInboxMessage > entry : this .messages .entrySet ()) {
201295 String messageId = entry .getKey ();
202- NewsfeedMessage newsfeedMessage = entry .getValue ();
203- Map <String , Object > data = newsfeedMessage .toJsonMap ();
296+ LeanplumInboxMessage inboxMessage = entry .getValue ();
297+ Map <String , Object > data = inboxMessage .toJsonMap ();
204298 messages .put (messageId , data );
205299 }
206300 String messagesJson = JsonConverter .toJson (messages );
@@ -279,90 +373,16 @@ public void variablesChanged() {
279373 }
280374
281375 /**
282- * Returns the number of all inbox messages on the device.
283- */
284- public int count () {
285- return messages .size ();
286- }
287-
288- /**
289- * Returns the number of the unread inbox messages on the device.
290- */
291- public int unreadCount () {
292- return unreadCount ;
293- }
294-
295- /**
296- * Returns the identifiers of all inbox messages on the device sorted in ascending
297- * chronological order, i.e. the id of the oldest message is the first one, and the most recent
298- * one is the last one in the array.
299- */
300- public List <String > messagesIds () {
301- List <String > messageIds = new ArrayList <>(messages .keySet ());
302- try {
303- Collections .sort (messageIds , new Comparator <String >() {
304- @ Override
305- public int compare (String firstMessageId , String secondMessageId ) {
306- // Message that is null will be moved to the back of the list.
307- LeanplumInboxMessage firstMessage = messageForId (firstMessageId );
308- if (firstMessage == null ) {
309- return -1 ;
310- }
311- LeanplumInboxMessage secondMessage = messageForId (secondMessageId );
312- if (secondMessage == null ) {
313- return 1 ;
314- }
315- // Message with null date will be moved to the back of the list.
316- Date firstDate = firstMessage .getDeliveryTimestamp ();
317- if (firstDate == null ) {
318- return -1 ;
319- }
320- Date secondDate = secondMessage .getDeliveryTimestamp ();
321- if (secondDate == null ) {
322- return 1 ;
323- }
324- return firstDate .compareTo (secondDate );
325- }
326- });
327- } catch (Throwable t ) {
328- Util .handleException (t );
329- }
330- return messageIds ;
331- }
332-
333- /**
334- * Have to stay as is because of backward compatibility + generics super-sub incompatibility
335- * (http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html#Topic2).
336- * <p>
337- * Returns a List containing all of the newsfeed messages sorted chronologically ascending (i.e.
376+ * Returns a List containing all of the inbox messages sorted chronologically ascending (i.e.
338377 * the oldest first and the newest last).
339378 */
340- public List <NewsfeedMessage > allMessages () {
341- return allMessages (new ArrayList <NewsfeedMessage >());
342- }
343-
344- /**
345- * Have to stay as is because of backward compatibility + generics super-sub incompatibility
346- * (http://www.angelikalanger.com/GenericsFAQ/FAQSections/ParameterizedTypes.html#Topic2).
347- * <p>
348- * Returns a List containing all of the unread newsfeed messages sorted chronologically ascending
349- * (i.e. the oldest first and the newest last).
350- */
351- public List <NewsfeedMessage > unreadMessages () {
352- return unreadMessages (new ArrayList <NewsfeedMessage >());
353- }
354-
355- /**
356- * Suggested workaround for generics to be used with {@link LeanplumInbox#getInstance()} although
357- * only LeanplumInboxMessage could be an instance of NewsfeedMessage.
358- */
359- private <T extends NewsfeedMessage > List <T > allMessages (List <T > messages ) {
379+ private List <LeanplumInboxMessage > allMessages (List <LeanplumInboxMessage > messages ) {
360380 if (messages == null ) {
361381 messages = new ArrayList <>();
362382 }
363383 try {
364384 for (String messageId : messagesIds ()) {
365- messages .add (( T ) messageForId (messageId ));
385+ messages .add (messageForId (messageId ));
366386 }
367387 } catch (Throwable t ) {
368388 Util .handleException (t );
@@ -371,47 +391,19 @@ private <T extends NewsfeedMessage> List<T> allMessages(List<T> messages) {
371391 }
372392
373393 /**
374- * Suggested workaround for generics to be used with {@link LeanplumInbox#getInstance()} although
375- * only LeanplumInboxMessage could be an instance of NewsfeedMessage .
394+ * Returns a List containing all of the unread inbox messages sorted chronologically ascending
395+ * (i.e. the oldest first and the newest last) .
376396 */
377- private < T extends NewsfeedMessage > List <T > unreadMessages (List <T > unreadMessages ) {
397+ private List <LeanplumInboxMessage > unreadMessages (List <LeanplumInboxMessage > unreadMessages ) {
378398 if (unreadMessages == null ) {
379399 unreadMessages = new ArrayList <>();
380400 }
381401 List <LeanplumInboxMessage > messages = allMessages (null );
382402 for (LeanplumInboxMessage message : messages ) {
383403 if (!message .isRead ()) {
384- unreadMessages .add (( T ) message );
404+ unreadMessages .add (message );
385405 }
386406 }
387407 return unreadMessages ;
388408 }
389-
390- /**
391- * Returns the inbox messages associated with the given getMessageId identifier.
392- */
393- public LeanplumInboxMessage messageForId (String messageId ) {
394- return messages .get (messageId );
395- }
396-
397- /**
398- * Add a callback for when the inbox receives new values from the server.
399- */
400- public void addChangedHandler (InboxChangedCallback handler ) {
401- synchronized (changedCallbacks ) {
402- changedCallbacks .add (handler );
403- }
404- if (this .didLoad ) {
405- handler .inboxChanged ();
406- }
407- }
408-
409- /**
410- * Removes a inbox changed callback.
411- */
412- public void removeChangedHandler (InboxChangedCallback handler ) {
413- synchronized (changedCallbacks ) {
414- changedCallbacks .remove (handler );
415- }
416- }
417409}
0 commit comments