Skip to content

Commit 01036cb

Browse files
committed
Refactor Notification from static class to struct
1 parent 4dd54fe commit 01036cb

File tree

1 file changed

+50
-49
lines changed

1 file changed

+50
-49
lines changed

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

Lines changed: 50 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ public NotificationManagerJni(AndroidJavaClass clazz, AndroidJavaObject obj)
8888
KEY_SMALL_ICON = clazz.GetStatic<AndroidJavaObject>("KEY_SMALL_ICON");
8989

9090
CollectMethods(clazz);
91-
JniApi.Notification.CollectJni();
9291
JniApi.NotificationBuilder.CollectJni();
9392
JniApi.Bundle.CollectJni();
9493
#else
@@ -233,36 +232,20 @@ public AndroidJavaObject CreateNotificationBuilder(String channelId)
233232
}
234233
}
235234

236-
struct JniApi
237-
{
238-
public NotificationManagerJni NotificationManager;
239-
240-
public static JniMethodID FindMethod(AndroidJavaClass clazz, string name, string signature, bool isStatic)
235+
struct NotificationJni
241236
{
242-
#if UNITY_2022_2_OR_NEWER
243-
var method = AndroidJNIHelper.GetMethodID(clazz.GetRawClass(), name, signature, isStatic);
244-
if (method == IntPtr.Zero)
245-
throw new Exception($"Method {name} with signature {signature} not found");
246-
return method;
247-
#else
248-
return name;
249-
#endif
250-
}
251-
252-
public static class Notification
253-
{
254-
public static AndroidJavaObject EXTRA_TITLE;
255-
public static AndroidJavaObject EXTRA_TEXT;
256-
public static AndroidJavaObject EXTRA_SHOW_CHRONOMETER;
257-
public static AndroidJavaObject EXTRA_BIG_TEXT;
258-
public static AndroidJavaObject EXTRA_SHOW_WHEN;
259-
public static int FLAG_AUTO_CANCEL;
260-
public static int FLAG_GROUP_SUMMARY;
237+
public AndroidJavaObject EXTRA_TITLE;
238+
public AndroidJavaObject EXTRA_TEXT;
239+
public AndroidJavaObject EXTRA_SHOW_CHRONOMETER;
240+
public AndroidJavaObject EXTRA_BIG_TEXT;
241+
public AndroidJavaObject EXTRA_SHOW_WHEN;
242+
public int FLAG_AUTO_CANCEL;
243+
public int FLAG_GROUP_SUMMARY;
261244

262-
static JniMethodID getGroup;
263-
static JniMethodID getSortKey;
245+
JniMethodID getGroup;
246+
JniMethodID getSortKey;
264247

265-
public static void CollectJni()
248+
public void CollectJni()
266249
{
267250
using (var notificationClass = new AndroidJavaClass("android.app.Notification"))
268251
{
@@ -271,7 +254,7 @@ public static void CollectJni()
271254
}
272255
}
273256

274-
static void CollectConstants(AndroidJavaClass clazz)
257+
void CollectConstants(AndroidJavaClass clazz)
275258
{
276259
EXTRA_TITLE = clazz.GetStatic<AndroidJavaObject>("EXTRA_TITLE");
277260
EXTRA_TEXT = clazz.GetStatic<AndroidJavaObject>("EXTRA_TEXT");
@@ -282,43 +265,60 @@ static void CollectConstants(AndroidJavaClass clazz)
282265
FLAG_GROUP_SUMMARY = clazz.GetStatic<int>("FLAG_GROUP_SUMMARY");
283266
}
284267

285-
static void CollectMethods(AndroidJavaClass clazz)
268+
void CollectMethods(AndroidJavaClass clazz)
286269
{
287270
getGroup = JniApi.FindMethod(clazz, "getGroup", "()Ljava/lang/String;", false);
288271
getSortKey = JniApi.FindMethod(clazz, "getSortKey", "()Ljava/lang/String;", false);
289272
}
290273

291-
public static AndroidJavaObject Extras(AndroidJavaObject notification)
274+
public AndroidJavaObject Extras(AndroidJavaObject notification)
292275
{
293276
return notification.Get<AndroidJavaObject>("extras");
294277
}
295278

296-
public static int Flags(AndroidJavaObject notification)
279+
public int Flags(AndroidJavaObject notification)
297280
{
298281
return notification.Get<int>("flags");
299282
}
300283

301-
public static int Number(AndroidJavaObject notification)
284+
public int Number(AndroidJavaObject notification)
302285
{
303286
return notification.Get<int>("number");
304287
}
305288

306-
public static string GetGroup(AndroidJavaObject notification)
289+
public string GetGroup(AndroidJavaObject notification)
307290
{
308291
return notification.Call<string>(getGroup);
309292
}
310293

311-
public static string GetSortKey(AndroidJavaObject notification)
294+
public string GetSortKey(AndroidJavaObject notification)
312295
{
313296
return notification.Call<string>(getSortKey);
314297
}
315298

316-
internal static long When(AndroidJavaObject notification)
299+
internal long When(AndroidJavaObject notification)
317300
{
318301
return notification.Get<long>("when");
319302
}
320303
}
321304

305+
struct JniApi
306+
{
307+
public NotificationManagerJni NotificationManager;
308+
public NotificationJni Notification;
309+
310+
public static JniMethodID FindMethod(AndroidJavaClass clazz, string name, string signature, bool isStatic)
311+
{
312+
#if UNITY_2022_2_OR_NEWER
313+
var method = AndroidJNIHelper.GetMethodID(clazz.GetRawClass(), name, signature, isStatic);
314+
if (method == IntPtr.Zero)
315+
throw new Exception($"Method {name} with signature {signature} not found");
316+
return method;
317+
#else
318+
return name;
319+
#endif
320+
}
321+
322322
public static class NotificationBuilder
323323
{
324324
static JniMethodID getExtras;
@@ -521,6 +521,7 @@ public static bool Initialize()
521521
var notificationManager = notificationManagerClass.CallStatic<AndroidJavaObject>("getNotificationManagerImpl", context, s_CurrentActivity);
522522
notificationManager.Call("setNotificationCallback", new NotificationCallback());
523523
s_Jni.NotificationManager = new NotificationManagerJni(notificationManagerClass, notificationManager);
524+
s_Jni.Notification.CollectJni();
524525

525526
s_Initialized = true;
526527
#endif
@@ -871,41 +872,41 @@ public static AndroidJavaObject CreateNotificationBuilder(int id, AndroidNotific
871872

872873
internal static AndroidNotificationIntentData GetNotificationData(AndroidJavaObject notificationObj)
873874
{
874-
using (var extras = JniApi.Notification.Extras(notificationObj))
875+
using (var extras = s_Jni.Notification.Extras(notificationObj))
875876
{
876877
var id = JniApi.Bundle.GetInt(extras, s_Jni.NotificationManager.KEY_ID, -1);
877878
if (id == -1)
878879
return null;
879880

880881
var channelId = s_Jni.NotificationManager.GetNotificationChannelId(notificationObj);
881-
int flags = JniApi.Notification.Flags(notificationObj);
882+
int flags = s_Jni.Notification.Flags(notificationObj);
882883

883884
var notification = new AndroidNotification();
884-
notification.Title = JniApi.Bundle.GetString(extras, JniApi.Notification.EXTRA_TITLE);
885-
notification.Text = JniApi.Bundle.GetString(extras, JniApi.Notification.EXTRA_TEXT);
885+
notification.Title = JniApi.Bundle.GetString(extras, s_Jni.Notification.EXTRA_TITLE);
886+
notification.Text = JniApi.Bundle.GetString(extras, s_Jni.Notification.EXTRA_TEXT);
886887
notification.SmallIcon = JniApi.Bundle.GetString(extras, s_Jni.NotificationManager.KEY_SMALL_ICON);
887888
notification.LargeIcon = JniApi.Bundle.GetString(extras, s_Jni.NotificationManager.KEY_LARGE_ICON);
888-
notification.ShouldAutoCancel = 0 != (flags & JniApi.Notification.FLAG_AUTO_CANCEL);
889-
notification.UsesStopwatch = JniApi.Bundle.GetBoolean(extras, JniApi.Notification.EXTRA_SHOW_CHRONOMETER, false);
889+
notification.ShouldAutoCancel = 0 != (flags & s_Jni.Notification.FLAG_AUTO_CANCEL);
890+
notification.UsesStopwatch = JniApi.Bundle.GetBoolean(extras, s_Jni.Notification.EXTRA_SHOW_CHRONOMETER, false);
890891
notification.FireTime = JniApi.Bundle.GetLong(extras, s_Jni.NotificationManager.KEY_FIRE_TIME, -1L).ToDatetime();
891892
notification.RepeatInterval = JniApi.Bundle.GetLong(extras, s_Jni.NotificationManager.KEY_REPEAT_INTERVAL, -1L).ToTimeSpan();
892893

893-
if (JniApi.Bundle.ContainsKey(extras, JniApi.Notification.EXTRA_BIG_TEXT))
894+
if (JniApi.Bundle.ContainsKey(extras, s_Jni.Notification.EXTRA_BIG_TEXT))
894895
notification.Style = NotificationStyle.BigTextStyle;
895896
else
896897
notification.Style = NotificationStyle.None;
897898

898899
notification.Color = s_Jni.NotificationManager.GetNotificationColor(notificationObj);
899-
notification.Number = JniApi.Notification.Number(notificationObj);
900+
notification.Number = s_Jni.Notification.Number(notificationObj);
900901
notification.IntentData = JniApi.Bundle.GetString(extras, s_Jni.NotificationManager.KEY_INTENT_DATA);
901-
notification.Group = JniApi.Notification.GetGroup(notificationObj);
902-
notification.GroupSummary = 0 != (flags & JniApi.Notification.FLAG_GROUP_SUMMARY);
903-
notification.SortKey = JniApi.Notification.GetSortKey(notificationObj);
902+
notification.Group = s_Jni.Notification.GetGroup(notificationObj);
903+
notification.GroupSummary = 0 != (flags & s_Jni.Notification.FLAG_GROUP_SUMMARY);
904+
notification.SortKey = s_Jni.Notification.GetSortKey(notificationObj);
904905
notification.GroupAlertBehaviour = s_Jni.NotificationManager.GetNotificationGroupAlertBehavior(notificationObj).ToGroupAlertBehaviours();
905-
var showTimestamp = JniApi.Bundle.GetBoolean(extras, JniApi.Notification.EXTRA_SHOW_WHEN, false);
906+
var showTimestamp = JniApi.Bundle.GetBoolean(extras, s_Jni.Notification.EXTRA_SHOW_WHEN, false);
906907
notification.ShowTimestamp = showTimestamp;
907908
if (showTimestamp)
908-
notification.CustomTimestamp = JniApi.Notification.When(notificationObj).ToDatetime();
909+
notification.CustomTimestamp = s_Jni.Notification.When(notificationObj).ToDatetime();
909910

910911
var data = new AndroidNotificationIntentData(id, channelId, notification);
911912
data.NativeNotification = notificationObj;

0 commit comments

Comments
 (0)