Skip to content

Commit

Permalink
Added query methods to support Android SDK 1.3.x
Browse files Browse the repository at this point in the history
  • Loading branch information
seniorquico committed Oct 1, 2018
1 parent 40436fd commit 01b306a
Show file tree
Hide file tree
Showing 36 changed files with 2,721 additions and 908 deletions.
8 changes: 8 additions & 0 deletions Assets/Zapic/Android.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

125 changes: 125 additions & 0 deletions Assets/Zapic/Android/ZapicAndroidAuthenticationHandler.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#if !UNITY_EDITOR && UNITY_ANDROID

using System;
using UnityEngine;

namespace ZapicSDK
{
internal sealed class ZapicAndroidAuthenticationHandler : AndroidJavaProxy
{
/// <summary>The <see cref="IZapicInterface"/> that communicates with the Zapic Android SDK.</summary>
private readonly ZapicAndroidInterface platform;

/// <summary>
/// Initializes a new instance of the <see cref="ZapicAndroidAuthenticationHandler"/> class.
/// </summary>
/// <param name="platform">
/// The <see cref="IZapicInterface"/> that communicates with the Zapic Android SDK.
/// </param>
internal ZapicAndroidAuthenticationHandler(ZapicAndroidInterface platform)
: base("com/zapic/sdk/android/ZapicPlayerAuthenticationHandler")
{
this.platform = platform;
}

public void onLogin(AndroidJavaObject playerObject)
{
try
{
var handler = platform.OnLogin;
if (handler == null)
{
return;
}

ZapicPlayer player;
try
{
player = ZapicAndroidUtilities.ConvertPlayer(playerObject);
}
catch (Exception e)
{
Debug.LogError("Zapic: An error occurred converting the Java object to ZapicPlayer");
Debug.LogException(e);
return;
}
finally
{
if (playerObject != null)
{
playerObject.Dispose();
playerObject = null;
}
}

try
{
handler(player);
}
catch (Exception e)
{
Debug.LogError("Zapic: An error occurred invoking the application callback");
Debug.LogException(e);
}
}
finally
{
if (playerObject != null)
{
playerObject.Dispose();
}
}
}

public void onLogout(AndroidJavaObject playerObject)
{
try
{
var handler = platform.OnLogout;
if (handler == null)
{
return;
}

ZapicPlayer player;
try
{
player = ZapicAndroidUtilities.ConvertPlayer(playerObject);
}
catch (Exception e)
{
Debug.LogError("Zapic: An error occurred converting the Java object to ZapicPlayer");
Debug.LogException(e);
return;
}
finally
{
if (playerObject != null)
{
playerObject.Dispose();
playerObject = null;
}
}

try
{
handler(player);
}
catch (Exception e)
{
Debug.LogError("Zapic: An error occurred invoking the application callback");
Debug.LogException(e);
}
}
finally
{
if (playerObject != null)
{
playerObject.Dispose();
}
}
}
}
}

#endif

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

149 changes: 149 additions & 0 deletions Assets/Zapic/Android/ZapicAndroidFunctionCallback{T}.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
#if !UNITY_EDITOR && UNITY_ANDROID

using System;
using UnityEngine;

namespace ZapicSDK
{
internal sealed class ZapicAndroidFunctionCallback<T> : AndroidJavaProxy
where T : class
{
/// <summary>The callback invoked with the result of the asynchronous query.</summary>
private readonly Action<T, ZapicException> callback;

/// <summary>The callback invoked to convert the result of the asynchronous query.</summary>
private readonly Func<AndroidJavaObject, T> convertResult;

/// <summary>Initializes a new instance of the <see cref="ZapicAndroidFunctionCallback{T}"/> class.</summary>
/// <param name="callback">The callback invoked with the result of the asynchronous query.</param>
/// <param name="convertResult">The callback invoked to convert the result of the asynchronous query.</param>
/// <exception cref="ArgumentNullException">
/// If <paramref name="callback"/> or <paramref name="convertResult"/> are <c>null</c>.
/// </exception>
internal ZapicAndroidFunctionCallback(
Action<T, ZapicException> callback,
Func<AndroidJavaObject, T> convertResult)
: base("com/zapic/sdk/android/ZapicCallback")
{
if (callback == null)
{
throw new ArgumentNullException("callback");
}

if (convertResult == null)
{
throw new ArgumentNullException("convertResult");
}

this.callback = callback;
this.convertResult = convertResult;
}

public void onComplete(AndroidJavaObject resultObject, AndroidJavaObject errorObject)
{
try
{
ZapicException error;
try
{
error = ZapicAndroidUtilities.ConvertError(errorObject);
}
catch (Exception e)
{
Debug.LogError("Zapic: An error occurred converting the Java object to ZapicException");
Debug.LogException(e);

error = new ZapicException(
ZapicErrorCode.INVALID_RESPONSE,
"An error occurred converting the Java object to ZapicException",
e);
}
finally
{
if (errorObject != null)
{
errorObject.Dispose();
errorObject = null;
}
}

if (error != null)
{
if (resultObject != null)
{
resultObject.Dispose();
resultObject = null;
}

try
{
callback(null, error);
}
catch (Exception e)
{
Debug.LogError("Zapic: An error occurred invoking the application callback");
Debug.LogException(e);
}

return;
}

T result;
try
{
result = convertResult(resultObject);
}
catch (Exception e)
{
Debug.LogError("Zapic: An error occurred converting the Java object to " + typeof(T).Name);
Debug.LogException(e);

error = new ZapicException(
ZapicErrorCode.INVALID_RESPONSE,
"An error occurred converting the Java object to " + typeof(T).Name,
e);
result = null;
}
finally
{
if (resultObject != null)
{
resultObject.Dispose();
resultObject = null;
}
}

try
{
if (error != null)
{
callback(null, error);
}
else
{
callback(result, null);
}
}
catch (Exception e)
{
Debug.LogError("Zapic: An error occurred invoking the application callback");
Debug.LogException(e);
}
}
finally
{
if (errorObject != null)
{
errorObject.Dispose();
}

if (resultObject != null)
{
resultObject.Dispose();
}
}
}
}
}

#endif
11 changes: 11 additions & 0 deletions Assets/Zapic/Android/ZapicAndroidFunctionCallback{T}.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 01b306a

Please sign in to comment.