Beispiel #1
0
        void TestJniInvocation(TextView textview)
        {
            IntPtr Adder = JNIEnv.FindClass("mono/android/test/Adder");

            if (Adder == IntPtr.Zero)
            {
                throw new InvalidOperationException("Couldn't find mono.android.test.Adder");
            }
            IntPtr Adder_ctor = JNIEnv.GetMethodID(Adder, "<init>", "()V");

            if (Adder_ctor == IntPtr.Zero)
            {
                throw new InvalidOperationException("Couldn't find mono.android.test.Adder.#ctor()");
            }
            IntPtr Adder_add = JNIEnv.GetMethodID(Adder, "add", "(II)I");

            if (Adder_add == IntPtr.Zero)
            {
                throw new InvalidOperationException("Couldn't find mono.android.test.Adder.add(int,int)");
            }
            IntPtr instance = JNIEnv.NewObject(Adder, Adder_ctor);
            int    result   = JNIEnv.CallIntMethod(instance, Adder_add, new JValue(2), new JValue(3));

            textview.Text += "\n\nnew Adder().add(2,3)=" + result;

            var boundAdder = new Adder(instance, JniHandleOwnership.DoNotTransfer);

            if (boundAdder.Add(3, 4) != 7)
            {
                throw new InvalidOperationException("Add(3,4) != 7!");
            }
            JNIEnv.DeleteLocalRef(instance);
        }
Beispiel #2
0
 public static int Add(Adder self, int a, int b)
 {
     if (id_sadd == IntPtr.Zero)
     {
         id_sadd = JNIEnv.GetStaticMethodID(class_ref, "add", "(Lmono/android/test/Adder;II)I");
     }
     return(JNIEnv.CallStaticIntMethod(class_ref, id_sadd,
                                       new JValue(JNIEnv.ToJniHandle(self)), new JValue(a), new JValue(b)));
 }
Beispiel #3
0
        public static int Sum(Adder self, IAdderProgress progress, params int[] values)
        {
            if (id_ssum == IntPtr.Zero)
            {
                id_ssum = JNIEnv.GetStaticMethodID(class_ref, "sum", "(Lmono/android/test/Adder;Lmono/android/test/Adder$Progress;[I)I");
            }
            IntPtr native_values = JNIEnv.NewArray(values);

            try {
                return(JNIEnv.CallStaticIntMethod(class_ref, id_ssum,
                                                  new JValue(JNIEnv.ToJniHandle(self)), new JValue(JNIEnv.ToJniHandle(progress)), new JValue(native_values)));
            } finally {
                JNIEnv.DeleteLocalRef(native_values);
            }
        }
Beispiel #4
0
        void TestManualOverrides()
        {
            var adder = new Adder();

            Console.WriteLine("Adder Class: {0}", adder.Class);

            var managedAdder = new ManagedAdder();
            int result       = Adder.Add(managedAdder, 3, 4);

            if (result != 14)
            {
                throw new InvalidOperationException("ManagedAdder.Add(3, 4) != 14!");
            }

            var progress = new AdderProgress();
            int sum      = Adder.Sum(adder, progress, 1, 2, 3);

            if (sum != 6)
            {
                throw new InvalidOperationException("Adder.Sum(adder, 1, 2, 3) != 6! Was: " + sum + ".");
            }
            if (progress.AddInvocations != 3)
            {
                throw new InvalidOperationException("Adder.Sum(adder, 1, 2, 3) didn't invoke progress 3 times! Was: " + progress.AddInvocations + ".");
            }

            progress.AddInvocations = 0;
            sum = Adder.Sum(managedAdder, progress, 6, 7);
            if (sum != 38)
            {
                throw new InvalidOperationException("Adder.Sum(managedAdder, 6, 7) != 38! Was: " + sum + ".");
            }
            if (progress.AddInvocations != 2)
            {
                throw new InvalidOperationException("Adder.Sum(adder, 6, 7) didn't invoke progress 2 times! Was: " + progress.AddInvocations + ".");
            }

            IntPtr javaDefaultProgress = JNIEnv.CreateInstance("mono/android/test/Adder$DefaultProgress", "()V");
            var    progress2           = Java.Lang.Object.GetObject <IAdderProgress>(javaDefaultProgress, JniHandleOwnership.TransferLocalRef);

            Console.WriteLine("progress2 MCW: {0}", progress2.GetType().FullName);

            javaDefaultProgress = JNIEnv.CreateInstance("mono/android/test/Adder$DefaultProgress", "()V");
            var progress3 = new Java.Lang.Object(javaDefaultProgress, JniHandleOwnership.TransferLocalRef)
                            .JavaCast <IAdderProgress> ();

            Console.WriteLine("progress3 MCW: {0}", progress3.GetType().FullName);
        }
Beispiel #5
0
        static int n_Add(IntPtr jnienv, IntPtr lrefThis, int a, int b)
        {
            Adder __this = Java.Lang.Object.GetObject <Adder>(lrefThis, JniHandleOwnership.DoNotTransfer);

            return(__this.Add(a, b));
        }