using UnityEngine;
using System;
namespace Tech3C
{
///
/// Bridge class for Android platform to communicate with Tech3C native SDK
///
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;
///
/// Initialize the Tech3C SDK
///
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("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("currentActivity"))
using (AndroidJavaClass controllerClass = new AndroidJavaClass(CLASS_NAME))
{
// Chain methods like MainActivity: initialize() -> setDebug() -> setOnAuthCallback()
controller = controllerClass.CallStatic(
"initialize",
activity,
clientId,
clientSecret
)
// .Call("setDebug", true)
// .Call("setDisableExitLogin", false)
// .Call("setEnableMaintenanceCheck", true)
.Call("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}");
}
}
///
/// Apply configuration settings to the SDK
///
private void ApplyConfig(Tech3CConfig config)
{
if (controller == null || config == null) return;
try
{
// Set debug mode (returns Tech3CIdController for chaining)
controller = controller.Call("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(javaEnumValue);
controller = controller.Call("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(javaEnumValue);
controller = controller.Call("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(javaEnumValue);
controller = controller.Call("setOrientation", javaOrientation);
}
// Set guest login
controller = controller.Call("setEnableGuestLogin", config.enableGuestLogin);
// Set OTP requirement
controller = controller.Call("setRequireOtp", config.requireOtp);
// Set disable exit login
controller = controller.Call("setDisableExitLogin", config.disableExitLogin);
// Set maintenance check
controller = controller.Call("setEnableMaintenanceCheck", config.enableMaintenanceCheck);
if (!string.IsNullOrEmpty(config.maintenanceCheckUrl))
{
controller = controller.Call("setMaintenanceCheckUrl", config.maintenanceCheckUrl);
}
if (!string.IsNullOrEmpty(config.serverIp))
{
controller = controller.Call("setIpMaintenanceCheck", config.serverIp);
}
Debug.Log("[Tech3C] Configuration applied successfully");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] ApplyConfig error: {e.Message}");
}
}
///
/// Show authentication screen
///
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("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);
}
}
///
/// Logout current user
///
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);
}
}
///
/// Get user information
///
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);
}
}
///
/// Check if user is logged in
///
public bool IsLoggedIn()
{
try
{
if (controller == null) return false;
return controller.Call("isLogin");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] IsLoggedIn error: {e.Message}");
return false;
}
}
///
/// Get access token
///
public string GetAccessToken()
{
try
{
if (controller == null) return null;
return controller.Call("getAccessToken");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] GetAccessToken error: {e.Message}");
return null;
}
}
///
/// Get refresh token
///
public string GetRefreshToken()
{
try
{
if (controller == null) return null;
return controller.Call("getRefreshToken");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] GetRefreshToken error: {e.Message}");
return null;
}
}
///
/// Get user ID
///
public string GetUserId()
{
try
{
if (controller == null) return null;
return controller.Call("getUserId");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] GetUserId error: {e.Message}");
return null;
}
}
///
/// Get device ID
///
public string GetDeviceId()
{
try
{
if (controller == null) return null;
return controller.Call("getDeviceId");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] GetDeviceId error: {e.Message}");
return null;
}
}
///
/// Get login time
///
public long GetLoginTime()
{
try
{
if (controller == null) return 0;
return controller.Call("getLoginTime");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] GetLoginTime error: {e.Message}");
return 0;
}
}
///
/// Get token expiry time
///
public long GetTokenExpiry()
{
try
{
if (controller == null) return 0;
return controller.Call("getTokenExpiry");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] GetTokenExpiry error: {e.Message}");
return 0;
}
}
///
/// Check if token is expired
///
public bool IsTokenExpired()
{
try
{
if (controller == null) return false;
return controller.Call("isTokenExpired");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] IsTokenExpired error: {e.Message}");
return false;
}
}
///
/// Set language
///
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(javaEnumValue);
controller = controller.Call("setLanguageDisplay", javaLanguage);
}
Debug.Log($"[Tech3C] Language set to {language}");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] SetLanguage error: {e.Message}");
}
}
///
/// Set debug mode
///
public void SetDebug(bool debug)
{
try
{
if (controller == null) return;
controller = controller.Call("setDebug", debug);
Debug.Log($"[Tech3C] Debug mode set to {debug}");
}
catch (Exception e)
{
Debug.LogError($"[Tech3C] SetDebug error: {e.Message}");
}
}
///
/// Cleanup resources
///
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}");
}
}
}
///
/// Interface for native bridge implementations
///
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();
}
}