49
49
import java .util .concurrent .Future ;
50
50
import java .util .concurrent .TimeUnit ;
51
51
52
+ import com .expensify .chat .customairshipextender .NotificationCache .NotificationData ;
53
+ import com .expensify .chat .customairshipextender .NotificationCache .NotificationMessage ;
54
+
52
55
public class CustomNotificationProvider extends ReactNotificationProvider {
53
56
// Resize icons to 100 dp x 100 dp
54
57
private static final int MAX_ICON_SIZE_DPS = 100 ;
@@ -71,7 +74,6 @@ public class CustomNotificationProvider extends ReactNotificationProvider {
71
74
private static final String ONYX_DATA_KEY = "onyxData" ;
72
75
73
76
private final ExecutorService executorService = Executors .newCachedThreadPool ();
74
- public final HashMap <Long , NotificationCache > cache = new HashMap <>();
75
77
76
78
public CustomNotificationProvider (@ NonNull Context context , @ NonNull AirshipConfigOptions configOptions ) {
77
79
super (context , configOptions );
@@ -168,8 +170,8 @@ private void applyMessageStyle(@NonNull Context context, NotificationCompat.Buil
168
170
}
169
171
170
172
// Retrieve and check for cached notifications
171
- NotificationCache notificationCache = findOrCreateNotificationCache (reportID );
172
- boolean hasExistingNotification = notificationCache .messages .size () >= 1 ;
173
+ NotificationData notificationData = NotificationCache . getNotificationData (reportID );
174
+ boolean hasExistingNotification = notificationData .messages .size () >= 1 ;
173
175
174
176
try {
175
177
JsonMap reportMap = payload .get (ONYX_DATA_KEY ).getList ().get (1 ).getMap ().get ("value" ).getMap ();
@@ -183,8 +185,8 @@ private void applyMessageStyle(@NonNull Context context, NotificationCompat.Buil
183
185
String conversationName = payload .get ("roomName" ) == null ? "" : payload .get ("roomName" ).getString ("" );
184
186
185
187
// Retrieve or create the Person object who sent the latest report comment
186
- Person person = notificationCache . people . get (accountID );
187
- Bitmap personIcon = notificationCache . bitmapIcons . get (accountID );
188
+ Person person = notificationData . getPerson (accountID );
189
+ Bitmap personIcon = notificationData . getIcon (accountID );
188
190
189
191
if (personIcon == null ) {
190
192
personIcon = fetchIcon (context , avatar );
@@ -200,13 +202,12 @@ private void applyMessageStyle(@NonNull Context context, NotificationCompat.Buil
200
202
.setName (name )
201
203
.build ();
202
204
203
- notificationCache .people .put (accountID , person );
204
- notificationCache .bitmapIcons .put (accountID , personIcon );
205
+ notificationData .putPerson (accountID , name , personIcon );
205
206
}
206
207
207
208
// Despite not using conversation style for the initial notification from each chat, we need to cache it to enable conversation style for future notifications
208
209
long createdTimeInMillis = getMessageTimeInMillis (messageData .get ("created" ).getString ("" ));
209
- notificationCache .messages .add (new NotificationCache . Message ( person , message , createdTimeInMillis ));
210
+ notificationData .messages .add (new NotificationMessage ( accountID , message , createdTimeInMillis ));
210
211
211
212
212
213
// Conversational styling should be applied to groups chats, rooms, and any 1:1 chats with more than one notification (ensuring the large profile image is always shown)
@@ -217,16 +218,16 @@ private void applyMessageStyle(@NonNull Context context, NotificationCompat.Buil
217
218
.setConversationTitle (conversationName );
218
219
219
220
// Add all conversation messages to the notification, including the last one we just received.
220
- for (NotificationCache . Message cachedMessage : notificationCache .messages ) {
221
- messagingStyle .addMessage (cachedMessage .text , cachedMessage .time , cachedMessage .person );
221
+ for (NotificationMessage cachedMessage : notificationData .messages ) {
222
+ messagingStyle .addMessage (cachedMessage .text , cachedMessage .time , notificationData . getPerson ( cachedMessage .accountID ) );
222
223
}
223
224
builder .setStyle (messagingStyle );
224
225
}
225
226
226
227
// Clear the previous notification associated to this conversation so it looks like we are
227
228
// replacing them with this new one we just built.
228
- if (notificationCache .prevNotificationID != -1 ) {
229
- NotificationManagerCompat .from (context ).cancel (notificationCache .prevNotificationID );
229
+ if (notificationData .prevNotificationID != -1 ) {
230
+ NotificationManagerCompat .from (context ).cancel (notificationData .prevNotificationID );
230
231
}
231
232
232
233
} catch (Exception e ) {
@@ -235,7 +236,9 @@ private void applyMessageStyle(@NonNull Context context, NotificationCompat.Buil
235
236
236
237
// Store the new notification ID so we can replace the notification if this conversation
237
238
// receives more messages
238
- notificationCache .prevNotificationID = notificationID ;
239
+ notificationData .prevNotificationID = notificationID ;
240
+
241
+ NotificationCache .setNotificationData (reportID , notificationData );
239
242
}
240
243
241
244
/**
@@ -254,25 +257,6 @@ private long getMessageTimeInMillis(String createdTime) {
254
257
return Calendar .getInstance ().getTimeInMillis ();
255
258
}
256
259
257
- /**
258
- * Check if we are showing a notification related to a reportID.
259
- * If not, create a new NotificationCache so we can build a conversation notification
260
- * as the messages come.
261
- *
262
- * @param reportID Report ID.
263
- * @return Notification Cache.
264
- */
265
- private NotificationCache findOrCreateNotificationCache (long reportID ) {
266
- NotificationCache notificationCache = cache .get (reportID );
267
-
268
- if (notificationCache == null ) {
269
- notificationCache = new NotificationCache ();
270
- cache .put (reportID , notificationCache );
271
- }
272
-
273
- return notificationCache ;
274
- }
275
-
276
260
/**
277
261
* Remove the notification data from the cache when the user dismisses the notification.
278
262
*
@@ -287,7 +271,7 @@ public void onDismissNotification(PushMessage message) {
287
271
return ;
288
272
}
289
273
290
- cache . remove (reportID );
274
+ NotificationCache . setNotificationData (reportID , null );
291
275
} catch (Exception e ) {
292
276
Log .e (TAG , "Failed to delete conversation cache. SendID=" + message .getSendId (), e );
293
277
}
@@ -328,24 +312,4 @@ private Bitmap fetchIcon(@NonNull Context context, String urlString) {
328
312
329
313
return null ;
330
314
}
331
-
332
- private static class NotificationCache {
333
- public Map <String , Person > people = new HashMap <>();
334
- public ArrayList <Message > messages = new ArrayList <>();
335
-
336
- public Map <String , Bitmap > bitmapIcons = new HashMap <>();
337
- public int prevNotificationID = -1 ;
338
-
339
- public static class Message {
340
- public Person person ;
341
- public String text ;
342
- public long time ;
343
-
344
- Message (Person person , String text , long time ) {
345
- this .person = person ;
346
- this .text = text ;
347
- this .time = time ;
348
- }
349
- }
350
- }
351
315
}
0 commit comments