Hi,

I'm trying to port over some Java to M4A to enable me to produce modal windows. Most of the work has been done, but I'm now stuck on using JNIEnv.GetMethodID/JNIEnv.GetFieldID and a number methods that don't seem to be listed anywhere (GetDeclaredMethod and GetDeclaredField) or aren't available when using JNIEnv.

JNIEnv.Get*ID requires the type of the variable to be IntPtr, but then requires IntPtr kls, string name, string signature for the parameters. Unfortunately, the documentation on the API area gives the "documentation to be added" line for kls which isn't much help.

My current code looks like this - I've commented parts for ease...

8-->
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace oqhra_android
{
    [Activity(Theme = "@android:style/Theme.NoTitleBar")]
    public class modal : Activity
    {
        public class ModalDialog
        {
            private bool mChoice = false;
            private bool mQuitModal = false;

            // Both JNIEnv require IntPtr kls, string name and
            // string signature - below is therefore wrong
            private IntPtr mMsgQueueNextMethod = JNIEnv.GetMethodID();
            private IntPtr mMsgTargetFiled = JNIEnv.GetFieldID();

            public ModalDialog()
            { }

            public void showAlertDialog(Context context, string info)
            {
                if (!prepareModal())
                    return;

AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.SetMessage(info);
                builder.SetCancelable(false);
builder.SetPositiveButton("Yes", (object o, Android.Content.DialogClickEventArgs e) =>
                {
                    this.mQuitModal = true;
                    builder.Dispose(); // was dialog.dismiss()
                });

                AlertDialog alert = builder.Create();
                alert.Show();

                doModal();
            }

            public bool showConfirmDialog(Context context, String info)
            {
                if (!prepareModal())
                    return false;
                // reset choice
                mChoice = false;

AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.SetMessage(info);
                builder.SetCancelable(false);
builder.SetPositiveButton("Yes", (object o, Android.Content.DialogClickEventArgs e) =>
                    {
                        this.mQuitModal = true;
                        this.mChoice = true;
                        builder.Dispose();
                    });

builder.SetNegativeButton("Cancel", (object o, Android.Content.DialogClickEventArgs e) =>
                    {
                        mQuitModal = true;
                        mChoice = false;
                        builder.Dispose(); // probably wrong
                    });

                AlertDialog alert = builder.Create();
                alert.Show();

                doModal();
                return mChoice;
            }

            private bool prepareModal()
            {
                Java.Lang.Class clsMsgQueue = null;
                Java.Lang.Class clsMessage = null;

                try
                {
clsMsgQueue = Java.Lang.Class.ForName("android.os.MessageQueue");
                }
                catch (Java.Lang.ClassNotFoundException e)
                {
                    e.PrintStackTrace();
                    return false;
                }

                try
                {
clsMessage = Java.Lang.Class.ForName("android.os.Message");
                }
                catch (Java.Lang.ClassNotFoundException e)
                {
                    e.PrintStackTrace();
                    return false;
                }

                try
                {
                    //GetDeclaredMethod is not available
mMsgQueueNextMethod = clsMsgQueue.GetDeclaredMethod("next", new Java.Lang.Class[] { });
                }
                catch (Java.Lang.SecurityException e)
                {
                    e.PrintStackTrace();
                    return false;
                }
                catch (Java.Lang.NoSuchMethodException e)
                {
                    e.PrintStackTrace();
                    return false;
                }

                mMsgQueueNextMethod.setAccessible(true);
                // setAccessible - not available

                try
                {
                    // GetDeclaredField not available
mMsgTargetFiled = clsMessage.GetDeclaredField("target");
                }
                catch (Java.Lang.SecurityException e)
                {
                    e.PrintStackTrace();
                    return false;
                }
                catch (Java.Lang.NoSuchFieldException e)
                {
                    e.PrintStackTrace();
                    return false;
                }

                mMsgTargetFiled.SetAccessible(true);
                return true;
            }

            private void doModal()
            {
                mQuitModal = false;

                // get message queue associated with main UI thread
                MessageQueue queue = Looper.MyQueue();
                while (!mQuitModal)
                {
                    // call queue.next(), might block
                    Message msg = null;
                    try
                    {
                        // invoke not available
msg = (Message)mMsgQueueNextMethod.invoke(queue, new Object[] { });
                    }
                    catch (Java.Lang.IllegalArgumentException e)
                    {
                        e.PrintStackTrace();
                    }
                    catch (Java.Lang.IllegalAccessException e)
                    {
                        e.PrintStackTrace();
                    }
                    /*catch (InvocationTargetException e)
                    {
                        e.PrintStackTrace();
                    }*/

                    if (null != msg)
                    {
                        Handler target = null;
                        try
                        {
                            target = (Handler)mMsgTargetFiled.Get(msg);
                        }
                        catch (Java.Lang.IllegalArgumentException e)
                        {
                            e.PrintStackTrace();
                        }
                        catch (Java.Lang.IllegalAccessException e)
                        {
                            e.PrintStackTrace();
                        }

                        if (target == null)
                        {
// No target is a magic identifier for the quit message.
                            mQuitModal = true;
                        }

                        target.DispatchMessage(msg);
                        msg.Recycle();
                    }
                }
            }
        }
    }
}

<--8

To be honest, there isn't that many issues with this code, but I will hold my hand up and admit my knowledge of Java is close to zero...

Paul
_______________________________________________
Monodroid mailing list
[email protected]

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to