Skip to content

Commit

Permalink
Merge pull request #122 from ultraleap/feature/disconnect
Browse files Browse the repository at this point in the history
Feature/disconnect
  • Loading branch information
YasserNezzari committed Feb 24, 2023
2 parents 672b956 + dfd81ff commit 0d3ba2b
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 6 deletions.
7 changes: 7 additions & 0 deletions Source/ThirdParty/BodyState/Private/BodyStateAnimInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1301,9 +1301,16 @@ int32 UBodyStateAnimInstance::GetActiveDeviceID()
{
return GetDeviceIDFromDeviceSerial(ActiveDeviceSerial);
}

void UBodyStateAnimInstance::OnDeviceAdded(const FString& DeviceSerial, const uint32 DeviceID)
{
UpdateDeviceList();

BodyStateSkeleton = GetCurrentSkeleton();
if (BodyStateSkeleton != nullptr)
{
SetAnimSkeleton(BodyStateSkeleton); // this will sync all the bones
}
}
void UBodyStateAnimInstance::OnDeviceRemoved(const uint32 DeviceID)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
#include "IUltraleapTrackingPlugin.h"
#include "Misc/ConfigCacheIni.h"

#if PLATFORM_ANDROID
#include "Android/AndroidApplication.h"
#include "Android/AndroidJNI.h"
#include <android/log.h>
#endif

FRotator ULeapBlueprintFunctionLibrary::DebugRotator;
ULeapBlueprintFunctionLibrary::ULeapBlueprintFunctionLibrary(const class FObjectInitializer& Initializer) : Super(Initializer)
{
Expand Down Expand Up @@ -93,4 +99,46 @@ float ULeapBlueprintFunctionLibrary::AngleBetweenVectors(const FVector& A, const
float AngleCosine = FVector::DotProduct(A, B) / (A.Size() * B.Size());
float AngleRadians = FMath::Acos(AngleCosine);
return FMath::RadiansToDegrees(AngleRadians);
}

void ULeapBlueprintFunctionLibrary::BindTrackingServiceAndroid()
{
#if PLATFORM_ANDROID
// Binding the tracking service
if (JNIEnv* Env = FAndroidApplication::GetJavaEnv())
{
static jmethodID Method =
FJavaWrapper::FindMethod(Env, FJavaWrapper::GameActivityClassID, "AndroidThunkJava_Bind", "()V", false);
if (Method)
{
UE_LOG(UltraleapTrackingLog, Log, TEXT("UltraleapTracking: calling AndroidThunkJava_Bind"));
FJavaWrapper::CallVoidMethod(Env, FJavaWrapper::GameActivityThis, Method);
}
else
{
UE_LOG(UltraleapTrackingLog, Error, TEXT("UltraleapTracking: could not call AndroidThunkJava_Bind invalid Method"));
}
}
#endif
}

void ULeapBlueprintFunctionLibrary::UnbindTrackingServiceAndroid()
{
#if PLATFORM_ANDROID
// Unbind the tracking service
if (JNIEnv* Env = FAndroidApplication::GetJavaEnv())
{
static jmethodID Method =
FJavaWrapper::FindMethod(Env, FJavaWrapper::GameActivityClassID, "AndroidThunkJava_Unbind", "()V", false);
if (Method)
{
UE_LOG(UltraleapTrackingLog, Log, TEXT("UltraleapTracking: calling AndroidThunkJava_Unbind"));
FJavaWrapper::CallVoidMethod(Env, FJavaWrapper::GameActivityThis, Method);
}
else
{
UE_LOG(UltraleapTrackingLog, Error, TEXT("UltraleapTracking: could not call AndroidThunkJava_Unbind invalid Method"));
}
}
#endif
}
4 changes: 4 additions & 0 deletions Source/UltraleapTrackingCore/Private/LeapComponent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,10 @@ void ULeapComponent::GetMultiDeviceDebugInfo(int32& NumLeftTracked, int32& NumRi
}
bool ULeapComponent::GetDeviceOrigin(FTransform& DeviceOrigin)
{
if (CurrentHandTrackingDevice == nullptr)
{
return false;
}
auto Device = CurrentHandTrackingDevice->GetDevice();
if (Device)
{
Expand Down
30 changes: 30 additions & 0 deletions Source/UltraleapTrackingCore/Private/LeapWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "LeapUtility.h"
#include "Multileap/DeviceCombiner.h"
#include "Runtime/Core/Public/Misc/Timespan.h"
#include "LeapBlueprintFunctionLibrary.h"

#pragma region LeapC Wrapper

Expand All @@ -22,6 +23,22 @@ FLeapWrapper::FLeapWrapper()
, InterpolatedFrameSize(0)
{
UseOpenXR = true;

if (!HasDeactivateHandle.IsValid())
{
HasDeactivateHandle = FCoreDelegates::ApplicationWillDeactivateDelegate.AddRaw(this, &FLeapWrapper::HandleApplicationDeactivate);
}
}

void FLeapWrapper::HandleApplicationDeactivate()
{
// This will reset the tracking service for Android app, when the device goes to sleep
AsyncTask(ENamedThreads::GameThread,
[this]()
{
ULeapBlueprintFunctionLibrary::UnbindTrackingServiceAndroid();
ULeapBlueprintFunctionLibrary::BindTrackingServiceAndroid();
});
}

FLeapWrapper::~FLeapWrapper()
Expand All @@ -46,6 +63,13 @@ FLeapWrapper::~FLeapWrapper()
{
CloseConnection();
}

// Always remove delegate events and reset
if (HasDeactivateHandle.IsValid())
{
FCoreDelegates::ApplicationWillDeactivateDelegate.Remove(HasDeactivateHandle);
HasDeactivateHandle.Reset();
}
}
// to be deprecated
void FLeapWrapper::SetCallbackDelegate(LeapWrapperCallbackInterface* InCallbackDelegate)
Expand Down Expand Up @@ -500,6 +524,12 @@ void FLeapWrapper::AddDevice(const uint32_t DeviceID, const LEAP_DEVICE_INFO& De
}
void FLeapWrapper::RemoveDevice(const uint32_t DeviceID)
{
// This will reset the tracking service for Android app
AsyncTask(ENamedThreads::GameThread, [this]() {
ULeapBlueprintFunctionLibrary::UnbindTrackingServiceAndroid();
ULeapBlueprintFunctionLibrary::BindTrackingServiceAndroid();
});

if (IsInGameThread())
{
return RemoveDeviceDirect(DeviceID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,10 @@ class ULeapBlueprintFunctionLibrary : public UBlueprintFunctionLibrary
// Debug functions, remove completely when no longer needed
static void ShutdownLeap();

/** Used for binding the tracking service */
static void BindTrackingServiceAndroid();
/** Used for unbinding the tracking service */
static void UnbindTrackingServiceAndroid();

static FRotator DebugRotator;
};
7 changes: 7 additions & 0 deletions Source/UltraleapTrackingCore/Public/LeapWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,10 @@ class FLeapWrapper : public IHandTrackingWrapper, public ILeapConnector
virtual void PostEarlyInit() override;
// End of ILeapConnector

// This will handle when an app is deactivated, when system goes to sleep
void HandleApplicationDeactivate();


private:
void CloseConnectionHandle(LEAP_CONNECTION* ConnectionHandle);
void Millisleep(int Milliseconds);
Expand Down Expand Up @@ -308,6 +312,9 @@ class FLeapWrapper : public IHandTrackingWrapper, public ILeapConnector
FGraphEventRef TaskRefConfigChange;
FGraphEventRef TaskRefConfigResponse;

// Delegate to access the deactivation event
FDelegateHandle HasDeactivateHandle;

// void setImage();
void SetFrame(const LEAP_TRACKING_EVENT* Frame);
//void SetDevice(const LEAP_DEVICE_INFO* DeviceProps);
Expand Down
13 changes: 9 additions & 4 deletions Source/UltraleapTrackingCore/UltraleapTracking.Build.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public UltraleapTracking(ReadOnlyTargetRules Target) : base(Target)
//OptimizeCode = CodeOptimization.Never;
PublicIncludePaths.AddRange(
new string[] {

// ... add public include paths required here ...
}
);
Expand Down Expand Up @@ -122,11 +123,11 @@ public UltraleapTracking(ReadOnlyTargetRules Target) : base(Target)
}
);

PrivateDependencyModuleNames.AddRange(
PrivateDependencyModuleNames.AddRange(
new string[]
{
// ... add private dependencies that you statically link with here ...
}
}
);

DynamicallyLoadedModuleNames.AddRange(
Expand All @@ -136,7 +137,9 @@ public UltraleapTracking(ReadOnlyTargetRules Target) : base(Target)
}
);

LoadLeapLib(Target);
PublicIncludePathModuleNames.Add("Launch");

LoadLeapLib(Target);
}

public string GetUProjectPath()
Expand Down Expand Up @@ -203,7 +206,9 @@ public bool LoadLeapLib(ReadOnlyTargetRules Target)
PublicAdditionalLibraries.Add(Path.Combine(LibraryPath, PlatformString, "arm64-v8a", "libLeapC.so"));

AdditionalPropertiesForReceipt.Add("AndroidPlugin", Path.Combine(ModulePath, "UltraleapTracking_APL.xml"));
}

PrivateDependencyModuleNames.AddRange(new string[] { "Launch" });
}
else if (Target.Platform == UnrealTargetPlatform.Linux)
{
IsLibrarySupported = true;
Expand Down
24 changes: 22 additions & 2 deletions Source/UltraleapTrackingCore/UltraleapTracking_APL.xml
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,30 @@

public void onUnbound()
{
Log.debug("UltraleapTracking OnUnBound() called");
Log.debug("UltraleapTracking OnUnBound() called");
}
};
private ServiceBinder serviceBinder;


public void AndroidThunkJava_Bind()
{
if (ServiceBinder.isServiceInstalled(getApplicationContext()))
{
Log.debug("UltraleapTracking calling bind() on the service binder");
serviceBinder.bind();
}
}

public void AndroidThunkJava_Unbind()
{
if (ServiceBinder.isServiceInstalled(getApplicationContext()))
{
Log.debug("UltraleapTracking calling unbind() on the service binder");
serviceBinder.unbind();
}
}

</insert>

</gameActivityClassAdditions>
Expand All @@ -108,7 +128,7 @@

<gameActivityOnResumeAdditions>
</gameActivityOnResumeAdditions>

<gameActivityOnCreateAdditions>
<insert>
// The service binder handles automatically binding to the hand tracking service, it provides a callback interface for checking the bind status.
Expand Down

0 comments on commit 0d3ba2b

Please sign in to comment.