How to handle Android Plugin's Callback in unity3D without SendUnityMessage

Hello,

I am using some 3rd party plugin for authentication which was developed for Android. But we cannot change the code. After authentication, it calls callback function for the result. But we don’t know how to handle that Callback method in Unity3D to get the authentication result, keeping Plugin source as it is.

Here I have write some sample code, both for Android Plugin part and Unity3D part as understandable level as possible to get the idea.

I have tried lots of Google search, but still unable to resolve this issue. As we cannot update the plugin code, I am not sure yet whether it is possible to handle the situation in unity3D. Could you please give your opinion or suggestion?

Help will be greatly appreciated.

Android Plugin Side:

// -------

package com.myapp.httpauthplugin.http;

public class HttpPostTask extends AbstractHttpTask<String, Void, Integer> {
	// ...
    public HttpPostTask(Activity activity, Handler handler) {
        super(activity);
        this.handler = handler;
        
        this.parameters = new ArrayList<NameValuePair>();
        this.headers = new ArrayList<Header>();
    }
    @Override
    protected Integer doInBackground(String... params) {
		//....
	}
    @Override
    protected void onPostExecute(Integer status) {
		// ...
	}	
}

// -------


// -------

package com.myapp.httpauthplugin;

import com.myapp.httpauthplugin.http.HttpPostTask;

public class MyAppStartActivity extends Activity
{

	// ....

	public void contractAuthentication(Activity activity) {
		HttpPostTask httpPostTask = new HttpPostTask(this, new UIHandler(this));

		httpPostTask.addHeader("Content-Type", CONTENT_TYPE);
		httpPostTask.addHeader("x-Content-id", getMetadata(METADATA_KEY_APP_CONT_ID));
		httpPostTask.addParameter("csAppId", getMetadata(METADATA_KEY_APP_IDENTIFIER));
		httpPostTask.execute(HTTPS + getMetadata(METADATA_KEY_APP_HOST) + URL_NEW_NATIVE_APP_CONTRACT_STATUS_AUTH);
	}

	private void launchBrowser(String url) {
		Uri uri = Uri.parse(url);
		Intent intent = new Intent(Intent.ACTION_VIEW, uri);
		startActivity(intent);
	}

	public static class UIHandler extends Handler {
		private WeakReference<MyAppStartActivity> weakReference;

		public UIHandler(MyAppStartActivity activity) {
			weakReference = new WeakReference<MyAppStartActivity>(activity);
		}

		@Override
		public void handleMessage(Message msg) {
			MyAppStartActivity activity = weakReference.get();

			switch (msg.what) {
			case HttpPostTask.REQUEST_TO_REDIRECT:
				String url = (String) msg.obj;
				activity.launchBrowser(url);
				activity.finish();
				break;
			case HttpPostTask.REQUEST_TO_ERROR:
				  int status = (Integer) msg.obj;
				  Toast
						  .makeText(
								  activity,
								  activity.getString(R.string.msg_unexpected_status, status),
								  Toast.LENGTH_LONG)
						  .show();
				activity.authErrorDialog();
				break;
			}
		}
	}
}

Unity3D C# Code:

void Start()
{
	validateHttpAuth();
}

private void validateHttpAuth()
{
#if UNITY_ANDROID
        AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
        AndroidJavaClass _plugin = new AndroidJavaClass("com.myapp.httpauthplugin.MyAppStartActivity");
        if(Application.platform == RuntimePlatform.Android)
        {
            try
            {
                _plugin.Call("runOnUiThread", new AndroidJavaRunnable(() =>
                {
                    _plugin.Call("contractAuthentication", unityActivity);
                }));
            }
            catch (AndroidJavaException ae)
            {
                throw;
            }
            catch (Exception e)
            {
                throw;
            }
        }
#endif
}

@ahsanmostafa
Did this ever get resolved? I have a similar issue.

have you tried to export to Android Studio project? and find where to insert that script?