Qt 6.x
The Qt SDK
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
QtApplication.java
Go to the documentation of this file.
1// Copyright (C) 2022 The Qt Company Ltd.
2// Copyright (c) 2016, BogDan Vatra <bogdan@kde.org>
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR BSD-3-Clause
4
5package org.qtproject.qt.android.bindings;
6
7import android.app.Application;
8
9import java.lang.reflect.Field;
10import java.lang.reflect.Method;
11import java.util.ArrayList;
12import java.util.HashMap;
13
14public class QtApplication extends Application
15{
16 public final static String QtTAG = "Qt";
17 public static Object m_delegateObject = null;
18 public static HashMap<String, ArrayList<Method>> m_delegateMethods= new HashMap<String, ArrayList<Method>>();
19 public static Method dispatchKeyEvent = null;
20 public static Method dispatchPopulateAccessibilityEvent = null;
21 public static Method dispatchTouchEvent = null;
22 public static Method dispatchTrackballEvent = null;
23 public static Method onKeyDown = null;
24 public static Method onKeyMultiple = null;
25 public static Method onKeyUp = null;
26 public static Method onTouchEvent = null;
27 public static Method onTrackballEvent = null;
28 public static Method onActivityResult = null;
29 public static Method onCreate = null;
30 public static Method onKeyLongPress = null;
31 public static Method dispatchKeyShortcutEvent = null;
32 public static Method onKeyShortcut = null;
33 public static Method dispatchGenericMotionEvent = null;
34 public static Method onGenericMotionEvent = null;
35 public static Method onRequestPermissionsResult = null;
36 private static String activityClassName;
37 public static void setQtContextDelegate(Class<?> clazz, Object listener)
38 {
39 m_delegateObject = listener;
40 activityClassName = clazz.getCanonicalName();
41
42 ArrayList<Method> delegateMethods = new ArrayList<Method>();
43 for (Method m : listener.getClass().getMethods()) {
44 if (m.getDeclaringClass().getName().startsWith("org.qtproject.qt.android"))
45 delegateMethods.add(m);
46 }
47
48 ArrayList<Field> applicationFields = new ArrayList<Field>();
49 for (Field f : QtApplication.class.getFields()) {
50 if (f.getDeclaringClass().getName().equals(QtApplication.class.getName()))
51 applicationFields.add(f);
52 }
53
54 for (Method delegateMethod : delegateMethods) {
55 try {
56 clazz.getDeclaredMethod(delegateMethod.getName(), delegateMethod.getParameterTypes());
57 if (QtApplication.m_delegateMethods.containsKey(delegateMethod.getName())) {
58 QtApplication.m_delegateMethods.get(delegateMethod.getName()).add(delegateMethod);
59 } else {
60 ArrayList<Method> delegateSet = new ArrayList<Method>();
61 delegateSet.add(delegateMethod);
62 QtApplication.m_delegateMethods.put(delegateMethod.getName(), delegateSet);
63 }
64 for (Field applicationField:applicationFields) {
65 if (applicationField.getName().equals(delegateMethod.getName())) {
66 try {
67 applicationField.set(null, delegateMethod);
68 } catch (Exception e) {
69 e.printStackTrace();
70 }
71 }
72 }
73 } catch (Exception e) { }
74 }
75 }
76
77 @Override
78 public void onTerminate() {
79 if (m_delegateObject != null && m_delegateMethods.containsKey("onTerminate"))
80 invokeDelegateMethod(m_delegateMethods.get("onTerminate").get(0));
81 super.onTerminate();
82 }
83
84 public static class InvokeResult
85 {
86 public boolean invoked = false;
87 public Object methodReturns = null;
88 }
89
90 private static int stackDeep=-1;
92 {
94 if (m_delegateObject == null)
95 return result;
96 StackTraceElement[] elements = Thread.currentThread().getStackTrace();
97 if (-1 == stackDeep) {
98 for (int it=0;it<elements.length;it++)
99 if (elements[it].getClassName().equals(activityClassName)) {
100 stackDeep = it;
101 break;
102 }
103 }
104 if (-1 == stackDeep)
105 return result;
106
107 final String methodName=elements[stackDeep].getMethodName();
108 if (!m_delegateMethods.containsKey(methodName))
109 return result;
110
111 for (Method m : m_delegateMethods.get(methodName)) {
112 if (m.getParameterTypes().length == args.length) {
113 result.methodReturns = invokeDelegateMethod(m, args);
114 result.invoked = true;
115 return result;
116 }
117 }
118 return result;
119 }
120
121 public static Object invokeDelegateMethod(Method m, Object... args)
122 {
123 try {
124 return m.invoke(m_delegateObject, args);
125 } catch (Exception e) {
126 e.printStackTrace();
127 }
128 return null;
129 }
130}
Definition main.cpp:8
qsizetype length() const noexcept
Definition qlist.h:388
static HashMap< String, ArrayList< Method > > m_delegateMethods
static void setQtContextDelegate(Class<?> clazz, Object listener)
static InvokeResult invokeDelegate(Object... args)
static Object invokeDelegateMethod(Method m, Object... args)
double e
QSet< QString >::iterator it
static QString methodName(const QDBusIntrospection::Method &method)
const GLfloat * m
GLfloat GLfloat f
GLuint64EXT * result
[6]
static void add(QPainterPath &path, const QWingedEdge &list, int edge, QPathEdge::Traversal traversal)
static const QTextHtmlElement elements[Html_NumElements]
QJSValueList args