Files
test/Assets/Tech3C/Runtime/Tech3CAndroidBridge.cs
2026-02-02 19:21:45 +07:00

453 lines
15 KiB
C#

using UnityEngine;
using System;
namespace Tech3C
{
/// <summary>
/// Bridge class for Android platform to communicate with Tech3C native SDK
/// </summary>
public class Tech3CAndroidBridge : ITech3CNativeBridge
{
private const string CLASS_NAME = "vn.tech3c.sdk.auth.controller.Tech3CIdController";
private const string BRIDGE_CLASS = "vn.tech3c.sdk.unity.Tech3CUnityBridge";
private AndroidJavaObject controller;
private AndroidJavaObject unityBridge;
/// <summary>
/// Initialize the Tech3C SDK
/// </summary>
public void Initialize(string clientId, string clientSecret, Tech3CConfig config)
{
try
{
Debug.Log("[Tech3C] Bridge GameObject initialized");
// Initialize Unity Bridge for callbacks first
using (AndroidJavaClass bridgeClass = new AndroidJavaClass(BRIDGE_CLASS))
{
unityBridge = bridgeClass.CallStatic<AndroidJavaObject>("getInstance");
}
// Initialize controller with method chaining to set callback
if (controller == null)
{
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
using (AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
using (AndroidJavaClass controllerClass = new AndroidJavaClass(CLASS_NAME))
{
// Chain methods like MainActivity: initialize() -> setDebug() -> setOnAuthCallback()
controller = controllerClass.CallStatic<AndroidJavaObject>(
"initialize",
activity,
clientId,
clientSecret
)
// .Call<AndroidJavaObject>("setDebug", true)
// .Call<AndroidJavaObject>("setDisableExitLogin", false)
// .Call<AndroidJavaObject>("setEnableMaintenanceCheck", true)
.Call<AndroidJavaObject>("setOnAuthCallback", unityBridge);
Debug.Log("[Tech3C] Unity Bridge registered with native SDK");
}
}
if (controller != null)
{
// Apply configuration
ApplyConfig(config);
Debug.Log("[Tech3C] Initialized successfully");
}
else
{
Debug.LogError("[Tech3C] Failed to initialize controller");
}
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] Initialize error: {e.Message}");
}
}
/// <summary>
/// Apply configuration settings to the SDK
/// </summary>
private void ApplyConfig(Tech3CConfig config)
{
if (controller == null || config == null) return;
try
{
// Set debug mode (returns Tech3CIdController for chaining)
controller = controller.Call<AndroidJavaObject>("setDebug", config.debugMode);
// Set language - pass Java Language enum
using (AndroidJavaClass languageEnumClass = new AndroidJavaClass("vn.tech3c.sdk.auth.entities.enums.Language"))
{
string javaEnumValue = config.language == Language.Vietnamese ? "VIETNAMESE" : "ENGLISH";
AndroidJavaObject javaLanguage = languageEnumClass.GetStatic<AndroidJavaObject>(javaEnumValue);
controller = controller.Call<AndroidJavaObject>("setLanguageDisplay", javaLanguage);
}
// Set UI mode - pass Java UiMode enum
using (AndroidJavaClass uiModeEnumClass = new AndroidJavaClass("vn.tech3c.sdk.auth.entities.enums.UiMode"))
{
string javaEnumValue = config.uiMode == UiMode.Fullscreen ? "FULLSCREEN" : "DIALOG";
AndroidJavaObject javaUiMode = uiModeEnumClass.GetStatic<AndroidJavaObject>(javaEnumValue);
controller = controller.Call<AndroidJavaObject>("setUiMode", javaUiMode);
}
// Set orientation - pass Java OrientationMode enum
using (AndroidJavaClass orientationEnumClass = new AndroidJavaClass("vn.tech3c.sdk.auth.entities.enums.OrientationMode"))
{
string javaEnumValue = config.screenOrientation.ToString().ToUpper();
AndroidJavaObject javaOrientation = orientationEnumClass.GetStatic<AndroidJavaObject>(javaEnumValue);
controller = controller.Call<AndroidJavaObject>("setOrientation", javaOrientation);
}
// Set guest login
controller = controller.Call<AndroidJavaObject>("setEnableGuestLogin", config.enableGuestLogin);
// Set OTP requirement
controller = controller.Call<AndroidJavaObject>("setRequireOtp", config.requireOtp);
// Set disable exit login
controller = controller.Call<AndroidJavaObject>("setDisableExitLogin", config.disableExitLogin);
// Set maintenance check
controller = controller.Call<AndroidJavaObject>("setEnableMaintenanceCheck", config.enableMaintenanceCheck);
if (!string.IsNullOrEmpty(config.maintenanceCheckUrl))
{
controller = controller.Call<AndroidJavaObject>("setMaintenanceCheckUrl", config.maintenanceCheckUrl);
}
if (!string.IsNullOrEmpty(config.serverIp))
{
controller = controller.Call<AndroidJavaObject>("setIpMaintenanceCheck", config.serverIp);
}
Debug.Log("[Tech3C] Configuration applied successfully");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] ApplyConfig error: {e.Message}");
}
}
/// <summary>
/// Show authentication screen
/// </summary>
public void ShowAuth(IAuthCallback callback)
{
try
{
if (controller == null)
{
Debug.LogError("[Tech3C] Controller not initialized");
callback?.OnAuthError(-1, "SDK not initialized");
return;
}
// Register callback with bridge
Tech3CSDKBridge.SetAuthCallback(callback);
using (AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer"))
using (AndroidJavaObject activity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity"))
{
controller.Call("showAuth", activity);
Debug.Log("[Tech3C] Auth screen shown");
}
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] ShowAuth error: {e.Message}");
callback?.OnAuthError(-1, e.Message);
}
}
/// <summary>
/// Logout current user
/// </summary>
public void Logout(ILogoutCallback callback)
{
try
{
if (controller == null)
{
Debug.LogError("[Tech3C] Controller not initialized");
callback?.OnLogoutError(-1, "SDK not initialized");
return;
}
// Register callback with bridge
Tech3CSDKBridge.SetLogoutCallback(callback);
controller.Call("logout");
Debug.Log("[Tech3C] Logout initiated");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] Logout error: {e.Message}");
callback?.OnLogoutError(-1, e.Message);
}
}
/// <summary>
/// Get user information
/// </summary>
public void GetUserInfo(IUserInfoCallback callback)
{
try
{
if (controller == null)
{
Debug.LogError("[Tech3C] Controller not initialized");
callback?.OnUserInfoError(-1, "SDK not initialized");
return;
}
// Register callback with bridge
Tech3CSDKBridge.SetUserInfoCallback(callback);
controller.Call("getUserInfo");
Debug.Log("[Tech3C] GetUserInfo called");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] GetUserInfo error: {e.Message}");
callback?.OnUserInfoError(-1, e.Message);
}
}
/// <summary>
/// Check if user is logged in
/// </summary>
public bool IsLoggedIn()
{
try
{
if (controller == null) return false;
return controller.Call<bool>("isLogin");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] IsLoggedIn error: {e.Message}");
return false;
}
}
/// <summary>
/// Get access token
/// </summary>
public string GetAccessToken()
{
try
{
if (controller == null) return null;
return controller.Call<string>("getAccessToken");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] GetAccessToken error: {e.Message}");
return null;
}
}
/// <summary>
/// Get refresh token
/// </summary>
public string GetRefreshToken()
{
try
{
if (controller == null) return null;
return controller.Call<string>("getRefreshToken");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] GetRefreshToken error: {e.Message}");
return null;
}
}
/// <summary>
/// Get user ID
/// </summary>
public string GetUserId()
{
try
{
if (controller == null) return null;
return controller.Call<string>("getUserId");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] GetUserId error: {e.Message}");
return null;
}
}
/// <summary>
/// Get device ID
/// </summary>
public string GetDeviceId()
{
try
{
if (controller == null) return null;
return controller.Call<string>("getDeviceId");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] GetDeviceId error: {e.Message}");
return null;
}
}
/// <summary>
/// Get login time
/// </summary>
public long GetLoginTime()
{
try
{
if (controller == null) return 0;
return controller.Call<long>("getLoginTime");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] GetLoginTime error: {e.Message}");
return 0;
}
}
/// <summary>
/// Get token expiry time
/// </summary>
public long GetTokenExpiry()
{
try
{
if (controller == null) return 0;
return controller.Call<long>("getTokenExpiry");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] GetTokenExpiry error: {e.Message}");
return 0;
}
}
/// <summary>
/// Check if token is expired
/// </summary>
public bool IsTokenExpired()
{
try
{
if (controller == null) return false;
return controller.Call<bool>("isTokenExpired");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] IsTokenExpired error: {e.Message}");
return false;
}
}
/// <summary>
/// Set language
/// </summary>
public void SetLanguage(Language language)
{
try
{
if (controller == null) return;
// Set language - pass Java Language enum
using (AndroidJavaClass languageEnumClass = new AndroidJavaClass("vn.tech3c.sdk.auth.entities.enums.Language"))
{
string javaEnumValue = language == Language.Vietnamese ? "VIETNAMESE" : "ENGLISH";
AndroidJavaObject javaLanguage = languageEnumClass.GetStatic<AndroidJavaObject>(javaEnumValue);
controller = controller.Call<AndroidJavaObject>("setLanguageDisplay", javaLanguage);
}
Debug.Log($"[Tech3C] Language set to {language}");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] SetLanguage error: {e.Message}");
}
}
/// <summary>
/// Set debug mode
/// </summary>
public void SetDebug(bool debug)
{
try
{
if (controller == null) return;
controller = controller.Call<AndroidJavaObject>("setDebug", debug);
Debug.Log($"[Tech3C] Debug mode set to {debug}");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] SetDebug error: {e.Message}");
}
}
/// <summary>
/// Cleanup resources
/// </summary>
public void Cleanup()
{
try
{
if (controller != null)
{
controller.Call("cleanup");
controller.Dispose();
controller = null;
Debug.Log("[Tech3C] Cleanup completed");
}
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] Cleanup error: {e.Message}");
}
}
}
/// <summary>
/// Interface for native bridge implementations
/// </summary>
public interface ITech3CNativeBridge
{
void Initialize(string clientId, string clientSecret, Tech3CConfig config);
void ShowAuth(IAuthCallback callback);
void Logout(ILogoutCallback callback);
void GetUserInfo(IUserInfoCallback callback);
bool IsLoggedIn();
string GetAccessToken();
string GetRefreshToken();
string GetUserId();
string GetDeviceId();
long GetLoginTime();
long GetTokenExpiry();
bool IsTokenExpired();
void SetLanguage(Language language);
void SetDebug(bool debug);
void Cleanup();
}
}