Skip to content

Commit 6bcfa72

Browse files
committed
Switch to ConcurrentHashMap
1 parent 42faeab commit 6bcfa72

File tree

1 file changed

+15
-25
lines changed

1 file changed

+15
-25
lines changed

com.unity.mobile.notifications/Runtime/Android/Plugins/com/unity/androidnotifications/UnityNotificationManager.java

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,19 @@
3030

3131
import java.lang.Integer;
3232
import java.util.Calendar;
33-
import java.util.HashMap;
3433
import java.util.Set;
3534
import java.util.HashSet;
3635
import java.util.ArrayList;
3736
import java.util.Arrays;
3837
import java.util.List;
38+
import java.util.concurrent.ConcurrentHashMap;
3939

4040
import com.unity3d.player.UnityPlayer;
4141

4242
public class UnityNotificationManager extends BroadcastReceiver {
4343
protected static NotificationCallback mNotificationCallback;
4444
protected static UnityNotificationManager mUnityNotificationManager;
45-
private static HashMap<Integer, Notification> mScheduledNotifications = new HashMap();
45+
private static ConcurrentHashMap<Integer, Object> mScheduledNotifications = new ConcurrentHashMap();
4646
private static HashSet<Integer> mVisibleNotifications = new HashSet<>();
4747

4848
public Context mContext = null;
@@ -303,7 +303,8 @@ public NotificationChannelWrapper[] getNotificationChannels() {
303303
// This is called from Unity managed code to call AlarmManager to set a broadcast intent for sending a notification.
304304
public void scheduleNotification(Notification.Builder notificationBuilder) {
305305
int id = notificationBuilder.getExtras().getInt(KEY_ID, -1);
306-
putScheduledNotification(id);
306+
// don't replace existing, only put null as placeholder so we properly report status as being scheduled
307+
mScheduledNotifications.putIfAbsent(Integer.valueOf(id), null);
307308
mBackgroundThread.enqueueNotification(id, notificationBuilder);
308309
}
309310

@@ -348,7 +349,7 @@ static Notification scheduleAlarmWithNotification(Context context, Class activit
348349
// fireTime not taken from notification, because we may have adjusted it
349350

350351
Notification notification = buildNotificationForSending(context, activityClass, notificationBuilder);
351-
putScheduledNotification(Integer.valueOf(id), notification);
352+
mScheduledNotifications.put(Integer.valueOf(id), notification);
352353
intent.putExtra(KEY_NOTIFICATION_ID, id);
353354

354355
PendingIntent broadcast = getBroadcastPendingIntent(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
@@ -669,7 +670,7 @@ public void onReceive(Context context, Intent intent) {
669670
id = builder.getExtras().getInt(KEY_NOTIFICATION_ID, -1);
670671
notif = buildNotificationForSending(context, openActivity, builder);
671672
// if notification is not sendable, it wasn't cached
672-
putScheduledNotification(Integer.valueOf(id), notif);
673+
mScheduledNotifications.put(Integer.valueOf(id), notif);
673674
}
674675

675676
if (notif != null) {
@@ -848,7 +849,7 @@ public static Object getNotificationOrBuilderForIntent(Context context, Intent i
848849
if (intent.hasExtra(KEY_NOTIFICATION_ID)) {
849850
int id = intent.getExtras().getInt(KEY_NOTIFICATION_ID);
850851
Integer notificationId = Integer.valueOf(id);
851-
if ((notification = getScheduledNotification(notificationId)) != null) {
852+
if ((notification = mScheduledNotifications.get(notificationId)) != null) {
852853
sendable = true;
853854
} else {
854855
// in case we don't have cached notification, deserialize from storage
@@ -897,27 +898,16 @@ public void showNotificationSettings(String channelId) {
897898
mActivity.startActivity(settingsIntent);
898899
}
899900

900-
private static synchronized void putScheduledNotification(Integer id) {
901-
// don't replace existing, only put null as placeholder so we properly report status as being scheduled
902-
if (!mScheduledNotifications.containsKey(id))
903-
mScheduledNotifications.put(id, null);
904-
}
905-
906-
private static synchronized void putScheduledNotification(Integer id, Notification notification) {
907-
mScheduledNotifications.put(id, notification);
908-
}
909-
910-
private static synchronized boolean haveScheduledNotification(Integer id) {
911-
// have key but notification is null, means scheduling is still happening on other thread
912-
return mScheduledNotifications.containsKey(id) && mScheduledNotifications.get(id) == null;
913-
}
914-
915-
private static synchronized Notification getScheduledNotification(Integer id) {
916-
return mScheduledNotifications.get(id);
901+
private static boolean haveScheduledNotification(Integer id) {
902+
Object value = mScheduledNotifications.get(id);
903+
if (value == null)
904+
return false;
905+
// If map contains builder, in means we're still scheduling
906+
return value instanceof Notification.Builder;
917907
}
918908

919-
protected static synchronized Notification removeScheduledNotification(Integer id) {
920-
return mScheduledNotifications.remove(id);
909+
protected static void removeScheduledNotification(Integer id) {
910+
mScheduledNotifications.remove(id);
921911
}
922912
}
923913

0 commit comments

Comments
 (0)