Documentation for the Unity C# Library
Loading...
Searching...
No Matches
SafeJNI.cs
Go to the documentation of this file.
1using System;
2
3using UnityEngine;
4
5namespace PixoVR.Apex
6{
7 public static class SafeJNI
8 {
9 const string TAG = "SafeJNI";
10
18 public static bool SafeCallStatic(this AndroidJavaObject obj, string methodName, params object[] args)
19 {
20 if (obj == null)
21 {
22 return false;
23 }
24
25 try
26 {
27 if (args == null || args.Length == 0)
28 obj.CallStatic(methodName);
29 else
30 obj.CallStatic(methodName, args);
31 return true;
32 }
33 catch (AndroidJavaException e)
34 {
35 Debug.unityLogger.Log(LogType.Exception, TAG, "JNI Exception: " + e);
36 return false;
37 }
38 catch (Exception ex)
39 {
40 Debug.unityLogger.Log(LogType.Exception, TAG, "Unexpected Exception: " + ex);
41 return false;
42 }
43 }
44
53 public static ReturnType SafeCallStatic<ReturnType>(this AndroidJavaObject obj, string methodName, params object[] args)
54 {
55 if (obj == null)
56 {
57 return default;
58 }
59
60 try
61 {
62 if (args == null || args.Length == 0)
63 return obj.CallStatic<ReturnType>(methodName);
64 else
65 return obj.CallStatic<ReturnType>(methodName, args);
66 }
67 catch (AndroidJavaException e)
68 {
69 Debug.unityLogger.Log(LogType.Exception, TAG, "JNI Exception: " + e);
70 return default;
71 }
72 catch (Exception ex)
73 {
74 Debug.unityLogger.Log(LogType.Exception, TAG, "Unexpected Exception: " + ex);
75 return default;
76 }
77 }
78
86 public static bool SafeCall(this AndroidJavaObject obj, string methodName, params object[] args)
87 {
88 if (obj == null)
89 {
90 return false;
91 }
92
93 try
94 {
95 if (args == null || args.Length == 0)
96 obj.Call(methodName);
97 else
98 obj.Call(methodName, args);
99 return true;
100 }
101 catch (AndroidJavaException e)
102 {
103 Debug.unityLogger.Log(LogType.Exception, TAG, "JNI Exception: " + e);
104 return false;
105 }
106 catch (Exception ex)
107 {
108 Debug.unityLogger.Log(LogType.Exception, TAG, "Unexpected Exception: " + ex);
109 return false;
110 }
111 }
112
121 public static ReturnType SafeCall<ReturnType>(this AndroidJavaObject obj, string methodName, params object[] args)
122 {
123 if (obj == null)
124 {
125 return default;
126 }
127
128 try
129 {
130 if (args == null || args.Length == 0)
131 return obj.Call<ReturnType>(methodName);
132 else
133 return obj.Call<ReturnType>(methodName, args);
134 }
135 catch (AndroidJavaException e)
136 {
137 Debug.unityLogger.Log(LogType.Exception, TAG, "JNI Exception: " + e);
138 return default;
139 }
140 catch (Exception ex)
141 {
142 Debug.unityLogger.Log(LogType.Exception, TAG, "Unexpected Exception: " + ex);
143 return default;
144 }
145 }
146
154 public static ReturnType SafeGetStatic<ReturnType>(this AndroidJavaObject obj, string fieldName)
155 {
156 if (obj == null)
157 {
158 return default;
159 }
160
161 try
162 {
163 return obj.GetStatic<ReturnType>(fieldName);
164 }
165 catch (AndroidJavaException e)
166 {
167 Debug.unityLogger.Log(LogType.Exception, TAG, "JNI Exception: " + e);
168 return default;
169 }
170 catch (Exception ex)
171 {
172 Debug.unityLogger.Log(LogType.Exception, TAG, "Unexpected Exception: " + ex);
173 return default;
174 }
175 }
176
184 public static ReturnType SafeGet<ReturnType>(this AndroidJavaObject obj, string fieldName)
185 {
186 if (obj == null)
187 {
188 return default;
189 }
190
191 try
192 {
193 return obj.Get<ReturnType>(fieldName);
194 }
195 catch (AndroidJavaException e)
196 {
197 Debug.unityLogger.Log(LogType.Exception, TAG, "JNI Exception: " + e);
198 return default;
199 }
200 catch (Exception ex)
201 {
202 Debug.unityLogger.Log(LogType.Exception, TAG, "Unexpected Exception: " + ex);
203 return default;
204 }
205 }
206 }
207}
static ReturnType SafeGetStatic< ReturnType >(this AndroidJavaObject obj, string fieldName)
Gets a static field of a native object.
Definition SafeJNI.cs:154
static ReturnType SafeCallStatic< ReturnType >(this AndroidJavaObject obj, string methodName, params object[] args)
Calls a static method on a native object using method name with optional arguments,...
Definition SafeJNI.cs:53
const string TAG
Definition SafeJNI.cs:9
static bool SafeCall(this AndroidJavaObject obj, string methodName, params object[] args)
Calls a void method on a native object using method name with optional arguments.
Definition SafeJNI.cs:86
static bool SafeCallStatic(this AndroidJavaObject obj, string methodName, params object[] args)
Calls a static void method on a native object using method name with optional arguments.
Definition SafeJNI.cs:18
static ReturnType SafeGet< ReturnType >(this AndroidJavaObject obj, string fieldName)
Gets a field of a native object.
Definition SafeJNI.cs:184
static ReturnType SafeCall< ReturnType >(this AndroidJavaObject obj, string methodName, params object[] args)
Calls a method on a native object using method name with optional parameters, and returns the result.
Definition SafeJNI.cs:121