Skip to content

Commit

Permalink
Updated Android interface to use 1.3.x SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
seniorquico committed Oct 2, 2018
1 parent a0380c7 commit 69db4a6
Show file tree
Hide file tree
Showing 17 changed files with 1,405 additions and 325 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.

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

using System;
using UnityEngine;

namespace ZapicSDK
{
/// <summary>An implementation of the <c>ZapicPlayerAuthenticationHandler</c> Java interface.</summary>
internal sealed class ZapicAndroidAuthenticationHandler : AndroidJavaProxy
{
/// <summary>
/// Initializes a new instance of the <see cref="ZapicAndroidAuthenticationHandler"/> class.
/// </summary>
internal ZapicAndroidAuthenticationHandler()
: base("com/zapic/sdk/android/ZapicPlayerAuthenticationHandler")
{
}

/// <summary>
/// <para>Gets or sets the callback invoked after the player has been logged in.</para>
/// <para>The player that has been logged in is passed to the callback.</para>
/// </summary>
public Action<ZapicPlayer> OnLogin { get; set; }

/// <summary>
/// <para>Gets or sets the callback invoked after the player has been logged out.</para>
/// <para>The player that has been logged out is passed to the callback.</para>
/// </summary>
public Action<ZapicPlayer> OnLogout { get; set; }

/// <summary>Invoked after the player has logged in.</summary>
/// <param name="playerObject">The current player.</param>
public void onLogin(AndroidJavaObject playerObject)
{
try
{
var handler = 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();
}
}
}

/// <summary>Invoked after the player has logged out.</summary>
/// <param name="playerObject">The previous player.</param>
public void onLogout(AndroidJavaObject playerObject)
{
try
{
var handler = 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.

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

using System;
using UnityEngine;

namespace ZapicSDK
{
/// <summary>A delegate-based implementation of the <c>ZapicCallback</c> Java interface.</summary>
/// <typeparam name="T">The type of the result.</typeparam>
internal sealed class ZapicAndroidFunctionCallback<T> : AndroidJavaProxy
where T : class
{
/// <summary>The callback invoked with the result of the asynchronous operation.</summary>
private readonly Action<T, ZapicException> callback;

/// <summary>The callback invoked to convert the result of the asynchronous operation.</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 operation.</param>
/// <param name="convertResult">
/// The callback invoked to convert the result of the asynchronous operation.
/// </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;
}

/// <summary>Called when the asynchronous operation is complete.</summary>
/// <param name="resultObject">The result of the asynchronous operation.</param>
/// <param name="errorObject">
/// The error thrown by the asynchronous operation. This will be {@code null} if the asynchronous operation
/// completed normally.
/// </param>
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 = e as ZapicException;
if (error == null)
{
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 = e as ZapicException;
if (error == null)
{
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();
}

javaInterface.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 69db4a6

Please sign in to comment.