Advertisement
Guest User

QtApplication.java

a guest
Jul 24th, 2015
181
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.72 KB | None | 0 0
  1. /*
  2.     Copyright (c) 2012-2013, BogDan Vatra <bogdan@kde.org>
  3.     Contact: http://www.qt-project.org/legal
  4.  
  5.     Commercial License Usage
  6.     Licensees holding valid commercial Qt licenses may use this file in
  7.     accordance with the commercial license agreement provided with the
  8.     Software or, alternatively, in accordance with the terms contained in
  9.     a written agreement between you and Digia.  For licensing terms and
  10.     conditions see http://qt.digia.com/licensing.  For further information
  11.     use the contact form at http://qt.digia.com/contact-us.
  12.  
  13.     BSD License Usage
  14.     Alternatively, this file may be used under the BSD license as follows:
  15.     Redistribution and use in source and binary forms, with or without
  16.     modification, are permitted provided that the following conditions
  17.     are met:
  18.  
  19.     1. Redistributions of source code must retain the above copyright
  20.     notice, this list of conditions and the following disclaimer.
  21.     2. Redistributions in binary form must reproduce the above copyright
  22.     notice, this list of conditions and the following disclaimer in the
  23.     documentation and/or other materials provided with the distribution.
  24.  
  25.     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  26.     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  27.     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  28.     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  29.     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  30.     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  31.     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  32.     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33.     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  34.     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36.  
  37. package org.qtproject.qt5.android.bindings;
  38.  
  39. import java.lang.reflect.Field;
  40. import java.lang.reflect.Method;
  41. import java.util.ArrayList;
  42. import java.util.HashMap;
  43.  
  44. import android.app.Application;
  45.  
  46. public class QtApplication extends Application
  47. {
  48.     public final static String QtTAG = "Qt";
  49.     public static Object m_delegateObject = null;
  50.     public static HashMap<String, ArrayList<Method>> m_delegateMethods= new HashMap<String, ArrayList<Method>>();
  51.     public static Method dispatchKeyEvent = null;
  52.     public static Method dispatchPopulateAccessibilityEvent = null;
  53.     public static Method dispatchTouchEvent = null;
  54.     public static Method dispatchTrackballEvent = null;
  55.     public static Method onKeyDown = null;
  56.     public static Method onKeyMultiple = null;
  57.     public static Method onKeyUp = null;
  58.     public static Method onTouchEvent = null;
  59.     public static Method onTrackballEvent = null;
  60.     public static Method onActivityResult = null;
  61.     public static Method onCreate = null;
  62.     public static Method onKeyLongPress = null;
  63.     public static Method dispatchKeyShortcutEvent = null;
  64.     public static Method onKeyShortcut = null;
  65.     public static Method dispatchGenericMotionEvent = null;
  66.     public static Method onGenericMotionEvent = null;
  67.  
  68.     public static void setQtActivityDelegate(Object listener)
  69.     {
  70.         QtApplication.m_delegateObject = listener;
  71.  
  72.         ArrayList<Method> delegateMethods = new ArrayList<Method>();
  73.         for (Method m : listener.getClass().getMethods()) {
  74.             if (m.getDeclaringClass().getName().startsWith("org.qtproject.qt5.android"))
  75.                 delegateMethods.add(m);
  76.         }
  77.  
  78.         ArrayList<Field> applicationFields = new ArrayList<Field>();
  79.         for (Field f : QtApplication.class.getFields()) {
  80.             if (f.getDeclaringClass().getName().equals(QtApplication.class.getName()))
  81.                 applicationFields.add(f);
  82.         }
  83.  
  84.         for (Method delegateMethod : delegateMethods) {
  85.             try {
  86.                 QtActivity.class.getDeclaredMethod(delegateMethod.getName(), delegateMethod.getParameterTypes());
  87.                 if (QtApplication.m_delegateMethods.containsKey(delegateMethod.getName())) {
  88.                     QtApplication.m_delegateMethods.get(delegateMethod.getName()).add(delegateMethod);
  89.                 } else {
  90.                     ArrayList<Method> delegateSet = new ArrayList<Method>();
  91.                     delegateSet.add(delegateMethod);
  92.                     QtApplication.m_delegateMethods.put(delegateMethod.getName(), delegateSet);
  93.                 }
  94.                 for (Field applicationField:applicationFields) {
  95.                     if (applicationField.getName().equals(delegateMethod.getName())) {
  96.                         try {
  97.                             applicationField.set(null, delegateMethod);
  98.                         } catch (Exception e) {
  99.                             e.printStackTrace();
  100.                         }
  101.                     }
  102.                 }
  103.             } catch (Exception e) {
  104.             }
  105.         }
  106.     }
  107.  
  108.     @Override
  109.     public void onTerminate() {
  110.         if (m_delegateObject != null && m_delegateMethods.containsKey("onTerminate"))
  111.             invokeDelegateMethod(m_delegateMethods.get("onTerminate").get(0));
  112.         super.onTerminate();
  113.     }
  114.  
  115.     public static class InvokeResult
  116.     {
  117.         public boolean invoked = false;
  118.         public Object methodReturns = null;
  119.     }
  120.  
  121.     private static int stackDeep=-1;
  122.     public static InvokeResult invokeDelegate(Object... args)
  123.     {
  124.         InvokeResult result = new InvokeResult();
  125.         if (m_delegateObject == null)
  126.             return result;
  127.         StackTraceElement[] elements = Thread.currentThread().getStackTrace();
  128.         if (-1 == stackDeep) {
  129.             String activityClassName = QtActivity.class.getCanonicalName();
  130.             for (int it=0;it<elements.length;it++)
  131.                 if (elements[it].getClassName().equals(activityClassName)) {
  132.                     stackDeep = it;
  133.                     break;
  134.                 }
  135.         }
  136.         final String methodName=elements[stackDeep].getMethodName();
  137.         if (-1 == stackDeep || !m_delegateMethods.containsKey(methodName))
  138.             return result;
  139.  
  140.         for (Method m : m_delegateMethods.get(methodName)) {
  141.             if (m.getParameterTypes().length == args.length) {
  142.                 result.methodReturns = invokeDelegateMethod(m, args);
  143.                 result.invoked = true;
  144.                 return result;
  145.             }
  146.         }
  147.         return result;
  148.     }
  149.  
  150.     public static Object invokeDelegateMethod(Method m, Object... args)
  151.     {
  152.         try {
  153.             return m.invoke(m_delegateObject, args);
  154.         } catch (Exception e) {
  155.             e.printStackTrace();
  156.         }
  157.         return null;
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement