Skip to content

Commit cdf317e

Browse files
committed
Refactor Bundle into struct
1 parent 912b96e commit cdf317e

File tree

1 file changed

+85
-83
lines changed

1 file changed

+85
-83
lines changed

com.unity.mobile.notifications/Runtime/Android/AndroidNotificationCenter.cs

Lines changed: 85 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,79 @@ public void SetShowWhen(AndroidJavaObject builder, bool showTimestamp)
388388
}
389389
}
390390

391+
struct BundleJni
392+
{
393+
JniMethodID containsKey;
394+
JniMethodID getBoolean;
395+
JniMethodID getInt;
396+
JniMethodID getLong;
397+
JniMethodID getString;
398+
JniMethodID putInt;
399+
JniMethodID putLong;
400+
JniMethodID putString;
401+
402+
public void CollectJni()
403+
{
404+
using (var clazz = new AndroidJavaClass("android/os/Bundle"))
405+
{
406+
containsKey = JniApi.FindMethod(clazz, "containsKey", "(Ljava/lang/String;)Z", false);
407+
getBoolean = JniApi.FindMethod(clazz, "getBoolean", "(Ljava/lang/String;Z)Z", false);
408+
getInt = JniApi.FindMethod(clazz, "getInt", "(Ljava/lang/String;I)I", false);
409+
getLong = JniApi.FindMethod(clazz, "getLong", "(Ljava/lang/String;J)J", false);
410+
getString = JniApi.FindMethod(clazz, "getString", "(Ljava/lang/String;)Ljava/lang/String;", false);
411+
putInt = JniApi.FindMethod(clazz, "putInt", "(Ljava/lang/String;I)V", false);
412+
putLong = JniApi.FindMethod(clazz, "putLong", "(Ljava/lang/String;J)V", false);
413+
putString = JniApi.FindMethod(clazz, "putString", "(Ljava/lang/String;Ljava/lang/String;)V", false);
414+
}
415+
}
416+
417+
public bool ContainsKey(AndroidJavaObject bundle, AndroidJavaObject key)
418+
{
419+
return bundle.Call<bool>(containsKey, key);
420+
}
421+
422+
public bool GetBoolean(AndroidJavaObject bundle, AndroidJavaObject key, bool defaultValue)
423+
{
424+
return bundle.Call<bool>(getBoolean, key, defaultValue);
425+
}
426+
427+
public int GetInt(AndroidJavaObject bundle, AndroidJavaObject key, int defaultValue)
428+
{
429+
return bundle.Call<int>(getInt, key, defaultValue);
430+
}
431+
432+
public long GetLong(AndroidJavaObject bundle, AndroidJavaObject key, long defaultValue)
433+
{
434+
return bundle.Call<long>(getLong, key, defaultValue);
435+
}
436+
437+
public string GetString(AndroidJavaObject bundle, AndroidJavaObject key)
438+
{
439+
return bundle.Call<string>(getString, key);
440+
}
441+
442+
public void PutInt(AndroidJavaObject bundle, AndroidJavaObject key, int value)
443+
{
444+
bundle.Call(putInt, key, value);
445+
}
446+
447+
public void PutLong(AndroidJavaObject bundle, AndroidJavaObject key, long value)
448+
{
449+
bundle.Call(putLong, key, value);
450+
}
451+
452+
public void PutString(AndroidJavaObject bundle, AndroidJavaObject key, string value)
453+
{
454+
bundle.Call(putString, key, value);
455+
}
456+
}
457+
391458
struct JniApi
392459
{
393460
public NotificationManagerJni NotificationManager;
394461
public NotificationJni Notification;
395462
public NotificationBuilderJni NotificationBuilder;
463+
public BundleJni Bundle;
396464

397465
public JniApi(AndroidJavaClass notificationManagerClass, AndroidJavaObject notificationManager)
398466
{
@@ -401,7 +469,8 @@ public JniApi(AndroidJavaClass notificationManagerClass, AndroidJavaObject notif
401469
Notification.CollectJni();
402470
NotificationBuilder = default;
403471
NotificationBuilder.CollectJni();
404-
JniApi.Bundle.CollectJni();
472+
Bundle = default;
473+
Bundle.CollectJni();
405474
}
406475

407476
public static JniMethodID FindMethod(AndroidJavaClass clazz, string name, string signature, bool isStatic)
@@ -415,73 +484,6 @@ public static JniMethodID FindMethod(AndroidJavaClass clazz, string name, string
415484
return name;
416485
#endif
417486
}
418-
419-
public static class Bundle
420-
{
421-
static JniMethodID containsKey;
422-
static JniMethodID getBoolean;
423-
static JniMethodID getInt;
424-
static JniMethodID getLong;
425-
static JniMethodID getString;
426-
static JniMethodID putInt;
427-
static JniMethodID putLong;
428-
static JniMethodID putString;
429-
430-
public static void CollectJni()
431-
{
432-
using (var clazz = new AndroidJavaClass("android/os/Bundle"))
433-
{
434-
containsKey = JniApi.FindMethod(clazz, "containsKey", "(Ljava/lang/String;)Z", false);
435-
getBoolean = JniApi.FindMethod(clazz, "getBoolean", "(Ljava/lang/String;Z)Z", false);
436-
getInt = JniApi.FindMethod(clazz, "getInt", "(Ljava/lang/String;I)I", false);
437-
getLong = JniApi.FindMethod(clazz, "getLong", "(Ljava/lang/String;J)J", false);
438-
getString = JniApi.FindMethod(clazz, "getString", "(Ljava/lang/String;)Ljava/lang/String;", false);
439-
putInt = JniApi.FindMethod(clazz, "putInt", "(Ljava/lang/String;I)V", false);
440-
putLong = JniApi.FindMethod(clazz, "putLong", "(Ljava/lang/String;J)V", false);
441-
putString = JniApi.FindMethod(clazz, "putString", "(Ljava/lang/String;Ljava/lang/String;)V", false);
442-
}
443-
}
444-
445-
public static bool ContainsKey(AndroidJavaObject bundle, AndroidJavaObject key)
446-
{
447-
return bundle.Call<bool>(containsKey, key);
448-
}
449-
450-
public static bool GetBoolean(AndroidJavaObject bundle, AndroidJavaObject key, bool defaultValue)
451-
{
452-
return bundle.Call<bool>(getBoolean, key, defaultValue);
453-
}
454-
455-
public static int GetInt(AndroidJavaObject bundle, AndroidJavaObject key, int defaultValue)
456-
{
457-
return bundle.Call<int>(getInt, key, defaultValue);
458-
}
459-
460-
public static long GetLong(AndroidJavaObject bundle, AndroidJavaObject key, long defaultValue)
461-
{
462-
return bundle.Call<long>(getLong, key, defaultValue);
463-
}
464-
465-
public static string GetString(AndroidJavaObject bundle, AndroidJavaObject key)
466-
{
467-
return bundle.Call<string>(getString, key);
468-
}
469-
470-
public static void PutInt(AndroidJavaObject bundle, AndroidJavaObject key, int value)
471-
{
472-
bundle.Call(putInt, key, value);
473-
}
474-
475-
public static void PutLong(AndroidJavaObject bundle, AndroidJavaObject key, long value)
476-
{
477-
bundle.Call(putLong, key, value);
478-
}
479-
480-
public static void PutString(AndroidJavaObject bundle, AndroidJavaObject key, string value)
481-
{
482-
bundle.Call(putString, key, value);
483-
}
484-
}
485487
}
486488

487489
/// <summary>
@@ -868,11 +870,11 @@ public static AndroidJavaObject CreateNotificationBuilder(int id, AndroidNotific
868870

869871
using (var extras = s_Jni.NotificationBuilder.GetExtras(notificationBuilder))
870872
{
871-
JniApi.Bundle.PutInt(extras, s_Jni.NotificationManager.KEY_ID, id);
872-
JniApi.Bundle.PutLong(extras, s_Jni.NotificationManager.KEY_REPEAT_INTERVAL, notification.RepeatInterval.ToLong());
873-
JniApi.Bundle.PutLong(extras, s_Jni.NotificationManager.KEY_FIRE_TIME, fireTime);
873+
s_Jni.Bundle.PutInt(extras, s_Jni.NotificationManager.KEY_ID, id);
874+
s_Jni.Bundle.PutLong(extras, s_Jni.NotificationManager.KEY_REPEAT_INTERVAL, notification.RepeatInterval.ToLong());
875+
s_Jni.Bundle.PutLong(extras, s_Jni.NotificationManager.KEY_FIRE_TIME, fireTime);
874876
if (!string.IsNullOrEmpty(notification.IntentData))
875-
JniApi.Bundle.PutString(extras, s_Jni.NotificationManager.KEY_INTENT_DATA, notification.IntentData);
877+
s_Jni.Bundle.PutString(extras, s_Jni.NotificationManager.KEY_INTENT_DATA, notification.IntentData);
876878
}
877879

878880
return notificationBuilder;
@@ -882,36 +884,36 @@ internal static AndroidNotificationIntentData GetNotificationData(AndroidJavaObj
882884
{
883885
using (var extras = s_Jni.Notification.Extras(notificationObj))
884886
{
885-
var id = JniApi.Bundle.GetInt(extras, s_Jni.NotificationManager.KEY_ID, -1);
887+
var id = s_Jni.Bundle.GetInt(extras, s_Jni.NotificationManager.KEY_ID, -1);
886888
if (id == -1)
887889
return null;
888890

889891
var channelId = s_Jni.NotificationManager.GetNotificationChannelId(notificationObj);
890892
int flags = s_Jni.Notification.Flags(notificationObj);
891893

892894
var notification = new AndroidNotification();
893-
notification.Title = JniApi.Bundle.GetString(extras, s_Jni.Notification.EXTRA_TITLE);
894-
notification.Text = JniApi.Bundle.GetString(extras, s_Jni.Notification.EXTRA_TEXT);
895-
notification.SmallIcon = JniApi.Bundle.GetString(extras, s_Jni.NotificationManager.KEY_SMALL_ICON);
896-
notification.LargeIcon = JniApi.Bundle.GetString(extras, s_Jni.NotificationManager.KEY_LARGE_ICON);
895+
notification.Title = s_Jni.Bundle.GetString(extras, s_Jni.Notification.EXTRA_TITLE);
896+
notification.Text = s_Jni.Bundle.GetString(extras, s_Jni.Notification.EXTRA_TEXT);
897+
notification.SmallIcon = s_Jni.Bundle.GetString(extras, s_Jni.NotificationManager.KEY_SMALL_ICON);
898+
notification.LargeIcon = s_Jni.Bundle.GetString(extras, s_Jni.NotificationManager.KEY_LARGE_ICON);
897899
notification.ShouldAutoCancel = 0 != (flags & s_Jni.Notification.FLAG_AUTO_CANCEL);
898-
notification.UsesStopwatch = JniApi.Bundle.GetBoolean(extras, s_Jni.Notification.EXTRA_SHOW_CHRONOMETER, false);
899-
notification.FireTime = JniApi.Bundle.GetLong(extras, s_Jni.NotificationManager.KEY_FIRE_TIME, -1L).ToDatetime();
900-
notification.RepeatInterval = JniApi.Bundle.GetLong(extras, s_Jni.NotificationManager.KEY_REPEAT_INTERVAL, -1L).ToTimeSpan();
900+
notification.UsesStopwatch = s_Jni.Bundle.GetBoolean(extras, s_Jni.Notification.EXTRA_SHOW_CHRONOMETER, false);
901+
notification.FireTime = s_Jni.Bundle.GetLong(extras, s_Jni.NotificationManager.KEY_FIRE_TIME, -1L).ToDatetime();
902+
notification.RepeatInterval = s_Jni.Bundle.GetLong(extras, s_Jni.NotificationManager.KEY_REPEAT_INTERVAL, -1L).ToTimeSpan();
901903

902-
if (JniApi.Bundle.ContainsKey(extras, s_Jni.Notification.EXTRA_BIG_TEXT))
904+
if (s_Jni.Bundle.ContainsKey(extras, s_Jni.Notification.EXTRA_BIG_TEXT))
903905
notification.Style = NotificationStyle.BigTextStyle;
904906
else
905907
notification.Style = NotificationStyle.None;
906908

907909
notification.Color = s_Jni.NotificationManager.GetNotificationColor(notificationObj);
908910
notification.Number = s_Jni.Notification.Number(notificationObj);
909-
notification.IntentData = JniApi.Bundle.GetString(extras, s_Jni.NotificationManager.KEY_INTENT_DATA);
911+
notification.IntentData = s_Jni.Bundle.GetString(extras, s_Jni.NotificationManager.KEY_INTENT_DATA);
910912
notification.Group = s_Jni.Notification.GetGroup(notificationObj);
911913
notification.GroupSummary = 0 != (flags & s_Jni.Notification.FLAG_GROUP_SUMMARY);
912914
notification.SortKey = s_Jni.Notification.GetSortKey(notificationObj);
913915
notification.GroupAlertBehaviour = s_Jni.NotificationManager.GetNotificationGroupAlertBehavior(notificationObj).ToGroupAlertBehaviours();
914-
var showTimestamp = JniApi.Bundle.GetBoolean(extras, s_Jni.Notification.EXTRA_SHOW_WHEN, false);
916+
var showTimestamp = s_Jni.Bundle.GetBoolean(extras, s_Jni.Notification.EXTRA_SHOW_WHEN, false);
915917
notification.ShowTimestamp = showTimestamp;
916918
if (showTimestamp)
917919
notification.CustomTimestamp = s_Jni.Notification.When(notificationObj).ToDatetime();

0 commit comments

Comments
 (0)