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