Skip to content

Commit

Permalink
Fixed main thread dispatcher (not used at the moment)
Browse files Browse the repository at this point in the history
  • Loading branch information
yasirkula committed Jan 21, 2019
1 parent 2bc1450 commit 7a69c56
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,35 @@ namespace AndroidRuntimePermissionsNamespace
{
public class PermissionCallbackAsync : AndroidJavaProxy
{
private string result;
private string[] permissions;
private readonly string[] permissions;
private AndroidRuntimePermissions.PermissionResultMultiple callback;
private readonly PermissionCallbackHelper callbackHelper;

public PermissionCallbackAsync( string[] permissions, AndroidRuntimePermissions.PermissionResultMultiple callback ) : base( "com.yasirkula.unity.RuntimePermissionsReceiver" )
{
result = null;

this.permissions = permissions;
this.callback = callback;
callbackHelper = new GameObject( "PermissionCallbackHelper" ).AddComponent<PermissionCallbackHelper>();
}

public void OnPermissionResult( string result )
{
this.result = result;
MainThreadDispatcher.ExecuteOnMainThread( ExecuteCallback );
callbackHelper.CallOnMainThread( () => ExecuteCallback( result ) );
}

private void ExecuteCallback()
private void ExecuteCallback( string result )
{
if( callback != null )
try
{
if( callback != null )
{
callback( permissions, AndroidRuntimePermissions.ProcessPermissionRequest( permissions, result ) );
callback = null;
}
}
finally
{
callback( permissions, AndroidRuntimePermissions.ProcessPermissionRequest( permissions, result ) );
callback = null;
Object.Destroy( callbackHelper );
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#if !UNITY_EDITOR && UNITY_ANDROID
using UnityEngine;

namespace AndroidRuntimePermissionsNamespace
{
public class PermissionCallbackHelper : MonoBehaviour
{
private System.Action mainThreadAction = null;

private void Awake()
{
DontDestroyOnLoad( gameObject );
}

private void Update()
{
if( mainThreadAction != null )
{
System.Action temp = mainThreadAction;
mainThreadAction = null;
temp();
}
}

public void CallOnMainThread( System.Action function )
{
mainThreadAction = function;
}
}
}
#endif
35 changes: 0 additions & 35 deletions Plugins/AndroidRuntimePermissions/MainThreadDispatcher.cs

This file was deleted.

Binary file modified RuntimePermissions.unitypackage
Binary file not shown.

0 comments on commit 7a69c56

Please sign in to comment.