Skip to content

Commit 912b96e

Browse files
committed
Refactor NotificationBuilder to be struct
1 parent ce868f3 commit 912b96e

File tree

1 file changed

+102
-100
lines changed

1 file changed

+102
-100
lines changed

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

Lines changed: 102 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -300,17 +300,107 @@ internal long When(AndroidJavaObject notification)
300300
}
301301
}
302302

303+
struct NotificationBuilderJni
304+
{
305+
JniMethodID getExtras;
306+
JniMethodID setContentTitle;
307+
JniMethodID setContentText;
308+
JniMethodID setAutoCancel;
309+
JniMethodID setNumber;
310+
JniMethodID setStyle;
311+
JniMethodID setWhen;
312+
JniMethodID setGroup;
313+
JniMethodID setGroupSummary;
314+
JniMethodID setSortKey;
315+
JniMethodID setShowWhen;
316+
317+
public void CollectJni()
318+
{
319+
using (var clazz = new AndroidJavaClass("android.app.Notification$Builder"))
320+
{
321+
getExtras = JniApi.FindMethod(clazz, "getExtras", "()Landroid/os/Bundle;", false);
322+
setContentTitle = JniApi.FindMethod(clazz, "setContentTitle", "(Ljava/lang/CharSequence;)Landroid/app/Notification$Builder;", false);
323+
setContentText = JniApi.FindMethod(clazz, "setContentText", "(Ljava/lang/CharSequence;)Landroid/app/Notification$Builder;", false);
324+
setAutoCancel = JniApi.FindMethod(clazz, "setAutoCancel", "(Z)Landroid/app/Notification$Builder;", false);
325+
setNumber = JniApi.FindMethod(clazz, "setNumber", "(I)Landroid/app/Notification$Builder;", false);
326+
setStyle = JniApi.FindMethod(clazz, "setStyle", "(Landroid/app/Notification$Style;)Landroid/app/Notification$Builder;", false);
327+
setWhen = JniApi.FindMethod(clazz, "setWhen", "(J)Landroid/app/Notification$Builder;", false);
328+
setGroup = JniApi.FindMethod(clazz, "setGroup", "(Ljava/lang/String;)Landroid/app/Notification$Builder;", false);
329+
setGroupSummary = JniApi.FindMethod(clazz, "setGroupSummary", "(Z)Landroid/app/Notification$Builder;", false);
330+
setSortKey = JniApi.FindMethod(clazz, "setSortKey", "(Ljava/lang/String;)Landroid/app/Notification$Builder;", false);
331+
setShowWhen = JniApi.FindMethod(clazz, "setShowWhen", "(Z)Landroid/app/Notification$Builder;", false);
332+
}
333+
}
334+
335+
public AndroidJavaObject GetExtras(AndroidJavaObject builder)
336+
{
337+
return builder.Call<AndroidJavaObject>(getExtras);
338+
}
339+
340+
public void SetContentTitle(AndroidJavaObject builder, string title)
341+
{
342+
builder.Call<AndroidJavaObject>(setContentTitle, title).Dispose();
343+
}
344+
345+
public void SetContentText(AndroidJavaObject builder, string text)
346+
{
347+
builder.Call<AndroidJavaObject>(setContentText, text).Dispose();
348+
}
349+
350+
public void SetAutoCancel(AndroidJavaObject builder, bool shouldAutoCancel)
351+
{
352+
builder.Call<AndroidJavaObject>(setAutoCancel, shouldAutoCancel).Dispose();
353+
}
354+
355+
public void SetNumber(AndroidJavaObject builder, int number)
356+
{
357+
builder.Call<AndroidJavaObject>(setNumber, number).Dispose();
358+
}
359+
360+
public void SetStyle(AndroidJavaObject builder, AndroidJavaObject style)
361+
{
362+
builder.Call<AndroidJavaObject>(setStyle, style).Dispose();
363+
}
364+
365+
public void SetWhen(AndroidJavaObject builder, long timestamp)
366+
{
367+
builder.Call<AndroidJavaObject>(setWhen, timestamp).Dispose();
368+
}
369+
370+
public void SetGroup(AndroidJavaObject builder, string group)
371+
{
372+
builder.Call<AndroidJavaObject>(setGroup, group).Dispose();
373+
}
374+
375+
public void SetGroupSummary(AndroidJavaObject builder, bool groupSummary)
376+
{
377+
builder.Call<AndroidJavaObject>(setGroupSummary, groupSummary).Dispose();
378+
}
379+
380+
public void SetSortKey(AndroidJavaObject builder, string sortKey)
381+
{
382+
builder.Call<AndroidJavaObject>(setSortKey, sortKey).Dispose();
383+
}
384+
385+
public void SetShowWhen(AndroidJavaObject builder, bool showTimestamp)
386+
{
387+
builder.Call<AndroidJavaObject>(setShowWhen, showTimestamp).Dispose();
388+
}
389+
}
390+
303391
struct JniApi
304392
{
305393
public NotificationManagerJni NotificationManager;
306394
public NotificationJni Notification;
395+
public NotificationBuilderJni NotificationBuilder;
307396

308397
public JniApi(AndroidJavaClass notificationManagerClass, AndroidJavaObject notificationManager)
309398
{
310399
NotificationManager = new NotificationManagerJni(notificationManagerClass, notificationManager);
311400
Notification = default;
312401
Notification.CollectJni();
313-
JniApi.NotificationBuilder.CollectJni();
402+
NotificationBuilder = default;
403+
NotificationBuilder.CollectJni();
314404
JniApi.Bundle.CollectJni();
315405
}
316406

@@ -326,94 +416,6 @@ public static JniMethodID FindMethod(AndroidJavaClass clazz, string name, string
326416
#endif
327417
}
328418

329-
public static class NotificationBuilder
330-
{
331-
static JniMethodID getExtras;
332-
static JniMethodID setContentTitle;
333-
static JniMethodID setContentText;
334-
static JniMethodID setAutoCancel;
335-
static JniMethodID setNumber;
336-
static JniMethodID setStyle;
337-
static JniMethodID setWhen;
338-
static JniMethodID setGroup;
339-
static JniMethodID setGroupSummary;
340-
static JniMethodID setSortKey;
341-
static JniMethodID setShowWhen;
342-
343-
public static void CollectJni()
344-
{
345-
using (var clazz = new AndroidJavaClass("android.app.Notification$Builder"))
346-
{
347-
getExtras = JniApi.FindMethod(clazz, "getExtras", "()Landroid/os/Bundle;", false);
348-
setContentTitle = JniApi.FindMethod(clazz, "setContentTitle", "(Ljava/lang/CharSequence;)Landroid/app/Notification$Builder;", false);
349-
setContentText = JniApi.FindMethod(clazz, "setContentText", "(Ljava/lang/CharSequence;)Landroid/app/Notification$Builder;", false);
350-
setAutoCancel = JniApi.FindMethod(clazz, "setAutoCancel", "(Z)Landroid/app/Notification$Builder;", false);
351-
setNumber = JniApi.FindMethod(clazz, "setNumber", "(I)Landroid/app/Notification$Builder;", false);
352-
setStyle = JniApi.FindMethod(clazz, "setStyle", "(Landroid/app/Notification$Style;)Landroid/app/Notification$Builder;", false);
353-
setWhen = JniApi.FindMethod(clazz, "setWhen", "(J)Landroid/app/Notification$Builder;", false);
354-
setGroup = JniApi.FindMethod(clazz, "setGroup", "(Ljava/lang/String;)Landroid/app/Notification$Builder;", false);
355-
setGroupSummary = JniApi.FindMethod(clazz, "setGroupSummary", "(Z)Landroid/app/Notification$Builder;", false);
356-
setSortKey = JniApi.FindMethod(clazz, "setSortKey", "(Ljava/lang/String;)Landroid/app/Notification$Builder;", false);
357-
setShowWhen = JniApi.FindMethod(clazz, "setShowWhen", "(Z)Landroid/app/Notification$Builder;", false);
358-
}
359-
}
360-
361-
public static AndroidJavaObject GetExtras(AndroidJavaObject builder)
362-
{
363-
return builder.Call<AndroidJavaObject>(getExtras);
364-
}
365-
366-
public static void SetContentTitle(AndroidJavaObject builder, string title)
367-
{
368-
builder.Call<AndroidJavaObject>(setContentTitle, title).Dispose();
369-
}
370-
371-
public static void SetContentText(AndroidJavaObject builder, string text)
372-
{
373-
builder.Call<AndroidJavaObject>(setContentText, text).Dispose();
374-
}
375-
376-
public static void SetAutoCancel(AndroidJavaObject builder, bool shouldAutoCancel)
377-
{
378-
builder.Call<AndroidJavaObject>(setAutoCancel, shouldAutoCancel).Dispose();
379-
}
380-
381-
public static void SetNumber(AndroidJavaObject builder, int number)
382-
{
383-
builder.Call<AndroidJavaObject>(setNumber, number).Dispose();
384-
}
385-
386-
public static void SetStyle(AndroidJavaObject builder, AndroidJavaObject style)
387-
{
388-
builder.Call<AndroidJavaObject>(setStyle, style).Dispose();
389-
}
390-
391-
public static void SetWhen(AndroidJavaObject builder, long timestamp)
392-
{
393-
builder.Call<AndroidJavaObject>(setWhen, timestamp).Dispose();
394-
}
395-
396-
public static void SetGroup(AndroidJavaObject builder, string group)
397-
{
398-
builder.Call<AndroidJavaObject>(setGroup, group).Dispose();
399-
}
400-
401-
public static void SetGroupSummary(AndroidJavaObject builder, bool groupSummary)
402-
{
403-
builder.Call<AndroidJavaObject>(setGroupSummary, groupSummary).Dispose();
404-
}
405-
406-
public static void SetSortKey(AndroidJavaObject builder, string sortKey)
407-
{
408-
builder.Call<AndroidJavaObject>(setSortKey, sortKey).Dispose();
409-
}
410-
411-
public static void SetShowWhen(AndroidJavaObject builder, bool showTimestamp)
412-
{
413-
builder.Call<AndroidJavaObject>(setShowWhen, showTimestamp).Dispose();
414-
}
415-
}
416-
417419
public static class Bundle
418420
{
419421
static JniMethodID containsKey;
@@ -836,35 +838,35 @@ public static AndroidJavaObject CreateNotificationBuilder(int id, AndroidNotific
836838
s_Jni.NotificationManager.SetNotificationIcon(notificationBuilder, s_Jni.NotificationManager.KEY_SMALL_ICON, notification.SmallIcon);
837839
if (!string.IsNullOrEmpty(notification.LargeIcon))
838840
s_Jni.NotificationManager.SetNotificationIcon(notificationBuilder, s_Jni.NotificationManager.KEY_LARGE_ICON, notification.LargeIcon);
839-
JniApi.NotificationBuilder.SetContentTitle(notificationBuilder, notification.Title);
840-
JniApi.NotificationBuilder.SetContentText(notificationBuilder, notification.Text);
841-
JniApi.NotificationBuilder.SetAutoCancel(notificationBuilder, notification.ShouldAutoCancel);
841+
s_Jni.NotificationBuilder.SetContentTitle(notificationBuilder, notification.Title);
842+
s_Jni.NotificationBuilder.SetContentText(notificationBuilder, notification.Text);
843+
s_Jni.NotificationBuilder.SetAutoCancel(notificationBuilder, notification.ShouldAutoCancel);
842844
if (notification.Number >= 0)
843-
JniApi.NotificationBuilder.SetNumber(notificationBuilder, notification.Number);
845+
s_Jni.NotificationBuilder.SetNumber(notificationBuilder, notification.Number);
844846
if (notification.Style == NotificationStyle.BigTextStyle)
845847
{
846848
using (var style = new AndroidJavaObject("android.app.Notification$BigTextStyle"))
847849
{
848850
style.Call<AndroidJavaObject>("bigText", notification.Text).Dispose();
849-
JniApi.NotificationBuilder.SetStyle(notificationBuilder, style);
851+
s_Jni.NotificationBuilder.SetStyle(notificationBuilder, style);
850852
}
851853
}
852854
long timestampValue = notification.ShowCustomTimestamp ? notification.CustomTimestamp.ToLong() : fireTime;
853-
JniApi.NotificationBuilder.SetWhen(notificationBuilder, timestampValue);
855+
s_Jni.NotificationBuilder.SetWhen(notificationBuilder, timestampValue);
854856
if (!string.IsNullOrEmpty(notification.Group))
855-
JniApi.NotificationBuilder.SetGroup(notificationBuilder, notification.Group);
857+
s_Jni.NotificationBuilder.SetGroup(notificationBuilder, notification.Group);
856858
if (notification.GroupSummary)
857-
JniApi.NotificationBuilder.SetGroupSummary(notificationBuilder, notification.GroupSummary);
859+
s_Jni.NotificationBuilder.SetGroupSummary(notificationBuilder, notification.GroupSummary);
858860
if (!string.IsNullOrEmpty(notification.SortKey))
859-
JniApi.NotificationBuilder.SetSortKey(notificationBuilder, notification.SortKey);
860-
JniApi.NotificationBuilder.SetShowWhen(notificationBuilder, notification.ShowTimestamp);
861+
s_Jni.NotificationBuilder.SetSortKey(notificationBuilder, notification.SortKey);
862+
s_Jni.NotificationBuilder.SetShowWhen(notificationBuilder, notification.ShowTimestamp);
861863
int color = notification.Color.ToInt();
862864
if (color != 0)
863865
s_Jni.NotificationManager.SetNotificationColor(notificationBuilder, color);
864866
s_Jni.NotificationManager.SetNotificationUsesChronometer(notificationBuilder, notification.UsesStopwatch);
865867
s_Jni.NotificationManager.SetNotificationGroupAlertBehavior(notificationBuilder, (int)notification.GroupAlertBehaviour);
866868

867-
using (var extras = JniApi.NotificationBuilder.GetExtras(notificationBuilder))
869+
using (var extras = s_Jni.NotificationBuilder.GetExtras(notificationBuilder))
868870
{
869871
JniApi.Bundle.PutInt(extras, s_Jni.NotificationManager.KEY_ID, id);
870872
JniApi.Bundle.PutLong(extras, s_Jni.NotificationManager.KEY_REPEAT_INTERVAL, notification.RepeatInterval.ToLong());

0 commit comments

Comments
 (0)