Unity SDK Docs 1.5.0-beta.6
Loading...
Searching...
No Matches
SystemControl.cs
1/*
2 * Copyright (C) 2020-2023 Tilt Five, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16using UnityEngine;
18using System;
19
20namespace TiltFive
21{
22 public static class SystemControl
23 {
24 public static bool IsTiltFiveUIRequestingAttention()
25 {
26 T5_Bool attentionRequested = false;
27 try
28 {
29 var result = NativePlugin.IsTiltFiveUIRequestingAttention(ref attentionRequested);
30
31 // Return false if the native client reports an internal error while running this query.
32 if (result != NativePlugin.T5_RESULT_SUCCESS)
33 {
34 return false;
35 }
36 }
37 catch (Exception e)
38 {
39 Log.Error(e.Message);
40 return false;
41 }
42
43 return attentionRequested;
44 }
45
46 internal static ServiceCompatibility GetServiceCompatibility()
47 {
48 var compatibility = ServiceCompatibility.Unknown;
49
50 try
51 {
52 compatibility = NativePlugin.GetServiceCompatibility();
53 }
54 catch(Exception e)
55 {
56 Log.Error(e.Message);
57 }
58
59 return compatibility;
60 }
61
62 internal static bool SetApplicationInfo()
63 {
64 string applicationName = Application.productName;
65#if UNITY_EDITOR
66 // TODO: Localize
67 applicationName = $"Unity Editor: {applicationName}";
68#endif
69 string applicationId = Application.identifier;
70 string productVersion = Application.version;
71 string engineVersion = Application.unityVersion;
72 TextAsset pluginVersionAsset = (TextAsset)Resources.Load("pluginversion");
73 string applicationVersionInfo = $"App: {productVersion}, Engine: {engineVersion}, T5 SDK: {pluginVersionAsset.text}";
74
75 int result = NativePlugin.T5_RESULT_UNKNOWN_ERROR;
76
77 try
78 {
79 using (T5_StringUTF8 appName = applicationName)
80 using (T5_StringUTF8 appId = applicationId)
81 using (T5_StringUTF8 appVersion = applicationVersionInfo)
82 {
83 result = NativePlugin.SetApplicationInfo(appName, appId, appVersion);
84 }
85 }
86 catch (System.DllNotFoundException e)
87 {
88 Log.Info("Could not connect to Tilt Five plugin to register project info: {0}", e);
89 }
90 catch (Exception)
91 {
92 Log.Error("Failed to register project info with the Tilt Five service.");
93 }
94
95 return result == NativePlugin.T5_RESULT_SUCCESS;
96 }
97
98 internal static bool SetPlatformContext()
99 {
100#if UNITY_ANDROID
101 if (Application.platform == RuntimePlatform.Android)
102 {
103 // Ensure the current thread is attached to the JVM
104 AndroidJNI.AttachCurrentThread();
105
106 IntPtr unityPlayerClazz = AndroidJNI.FindClass("com/unity3d/player/UnityPlayer");
107 if (unityPlayerClazz == IntPtr.Zero)
108 {
109 Log.Error("Failed to obtain UnityPlayer class via JNI");
110 return false;
111 }
112
113 IntPtr currentActivityFieldId = AndroidJNI.GetStaticFieldID(unityPlayerClazz, "currentActivity", "Landroid/app/Activity;");
114 if (currentActivityFieldId == IntPtr.Zero)
115 {
116 Log.Error("Failed to obtain UnityPlayer/currentActivity field via JNI");
117 return false;
118 }
119
120 IntPtr currentActivity = AndroidJNI.GetStaticObjectField(unityPlayerClazz, currentActivityFieldId);
121 if (currentActivity == IntPtr.Zero)
122 {
123 Log.Error("Failed to obtain UnityPlayer/currentActivity instance via JNI");
124 return false;
125 }
126
127 IntPtr t5ActivityClazz = AndroidJNI.FindClass("com/tiltfive/client/TiltFiveActivity");
128 if (t5ActivityClazz == IntPtr.Zero)
129 {
130 Log.Error("Failed to obtain TiltFive activity class via JNI");
131 return false;
132 }
133
134 IntPtr getPlatformContextMethodId = AndroidJNI.GetMethodID(t5ActivityClazz, "getT5PlatformContext", "()J");
135 if (getPlatformContextMethodId == IntPtr.Zero)
136 {
137 Log.Error("Failed to obtain TiltFive getT5PlatformContext() method via JNI");
138 return false;
139 }
140
141 var context = AndroidJNI.CallLongMethod(currentActivity, getPlatformContextMethodId, new jvalue[] { });
142 if (context == 0)
143 {
144 Log.Error("Failed to obtain TiltFive platform context via JNI");
145 return false;
146 }
147
148 // If we obtained a context from Java, send it to native
149 try
150 {
151 int result = NativePlugin.SetPlatformContext(new IntPtr(context));
152 if (result != NativePlugin.T5_RESULT_SUCCESS)
153 {
154 Log.Error("Tilt Five platform context set returned error: {0}", result);
155 }
156 }
157 catch (System.DllNotFoundException e)
158 {
159 Log.Info("Tilt Five plugin unavailable for set platform context: {0}", e);
160 return false;
161 }
162 catch (Exception e)
163 {
164 Log.Error("Failed to set Tilt Five platform context: {0}", e);
165 return false;
166 }
167 }
168#endif // UNITY_ANDROID
169
170 return true;
171 }
172 }
173}
ServiceCompatibility
Whether the running service is compatible.