Files
Plugin/Source/SenseGloveAndroid/Private/SGAndroid/SGCoreJNI.cpp
T

140 lines
5.1 KiB
C++

/**
* @file
*
* @author Mamadou Babaei <mamadou@senseglove.com>
*
* @section LICENSE
*
* (The MIT License)
*
* Copyright (c) 2020 - 2024 SenseGlove
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @section DESCRIPTION
*
*
*/
#include "SGAndroid/SGCoreJNI.h"
#if PLATFORM_ANDROID
#include "Android/AndroidApplication.h"
#include "Android/AndroidJava.h"
#endif /* PLATFORM_ANDROID */
#include "Misc/Paths.h"
#if PLATFORM_ANDROID
#include "SGCoreImpl/SGExportedFunctions.h"
#include "SGInterop/SGIC_FString.h"
#include "SGInterop/SGIC_jclass.h"
#include "SGInterop/SGIC_jmethodID.h"
#include "SGInterop/SGIC_JNIEnv_Ptr.h"
#endif /* PLATFORM_ANDROID */
#include "SGLog/SGLog.h"
int32 FSGCoreJNI::Initialize()
{
int32 Result = -999999;
#if PLATFORM_ANDROID
static constexpr char SGCONNECT_JAVA_CLASS_NAME[] = "com.senseglove.sgconnect.SGConnect";
SGLOG("FSGCoreJNI: attempting to get the Android JNI environment...");
JNIEnv* Env = FAndroidApplication::GetJavaEnv();
FAndroidApplication::CheckJavaException();
if (!Env)
{
SGLOG_ERROR("FSGCoreJNI: failed to get the Android JNI environmnet!");
return -1999999;
}
FSGIC_JNIEnv_Ptr InEnvContainer{Env};
SGLOG("FSGCoreJNI: attempting to find the Android Java class...");
jclass Class = FAndroidApplication::FindJavaClass(SGCONNECT_JAVA_CLASS_NAME);
FAndroidApplication::CheckJavaException();
if (Env->IsSameObject(Class, nullptr))
{
SGLOG_ERROR("FSGCoreJNI: failed to get the Android Java class!");
return -2999999;
}
Class = (jclass)Env->NewGlobalRef(Class);
FSGIC_jclass InClassContainer{Class};
SGLOG("FSGCoreJNI: attempting to find the Android JNI method 'ActiveDevices'...");
jmethodID ActiveDevicesMethodID = Env->GetStaticMethodID(Class, "ActiveDevices", "()I");
FAndroidApplication::CheckJavaException();
if (!ActiveDevicesMethodID)
{
SGLOG_ERROR("FSGCoreJNI: failed to get the Android JNI method 'ActiveDevices'!");
return -3999999;
}
FSGIC_jmethodID InActiveDevicesMethodIDContainer{ActiveDevicesMethodID};
SGLOG("FSGCoreJNI: attempting to find the Android JNI method 'GetDeviceString'...");
jmethodID GetDeviceStringMethodID = Env->GetStaticMethodID(Class, "GetDeviceString", "(I)Ljava/lang/String;");
FAndroidApplication::CheckJavaException();
if (!GetDeviceStringMethodID)
{
SGLOG_ERROR("FSGCoreJNI: failed to get the Android JNI method 'GetDeviceString'!");
return -4999999;
}
FSGIC_jmethodID InGetDeviceStringMethodIDContainer{GetDeviceStringMethodID};
SGLOG("FSGCoreJNI: attempting to find the Android JNI method 'GetSensorString'...");
jmethodID GetSensorStringMethodID = Env->GetStaticMethodID(Class, "GetSensorString", "(I)Ljava/lang/String;");
FAndroidApplication::CheckJavaException();
if (!GetSensorStringMethodID)
{
SGLOG_ERROR("FSGCoreJNI: failed to get the Android JNI method 'GetSensorString'!");
return -5999999;
}
FSGIC_jmethodID InGetSensorStringMethodIDContainer{GetSensorStringMethodID};
SGLOG("FSGCoreJNI: attempting to find the Android JNI method 'WriteHaptics'...");
jmethodID WriteHapticsMethodID = Env->GetStaticMethodID(Class, "WriteHaptics", "(ILjava/lang/String;)I");
FAndroidApplication::CheckJavaException();
if (!WriteHapticsMethodID)
{
SGLOG_ERROR("FSGCoreJNI: failed to get the Android JNI method 'WriteHaptics'!");
return -6999999;
}
FSGIC_jmethodID InWriteHapticsMethodIDContainer{WriteHapticsMethodID};
FSGIC_FString ProjectSavedDirContainer{FPaths::ProjectSavedDir()};
Result = SGCoreImpl_Android_Initialize(&InEnvContainer, &InClassContainer,
&InActiveDevicesMethodIDContainer,
&InGetDeviceStringMethodIDContainer,
&InGetSensorStringMethodIDContainer,
&InWriteHapticsMethodIDContainer,
&ProjectSavedDirContainer);
#endif /* PLATFORM_ANDROID */
return Result;
}