2
Copyright (c) 2009-2011, BogDan Vatra <bog_dan_ro@yahoo.com>
5
Redistribution and use in source and binary forms, with or without
6
modification, are permitted provided that the following conditions are met:
7
* Redistributions of source code must retain the above copyright
8
notice, this list of conditions and the following disclaimer.
9
* Redistributions in binary form must reproduce the above copyright
10
notice, this list of conditions and the following disclaimer in the
11
documentation and/or other materials provided with the distribution.
12
* Neither the name of the BogDan Vatra <bog_dan_ro@yahoo.com> nor the
13
names of its contributors may be used to endorse or promote products
14
derived from this software without specific prior written permission.
16
THIS SOFTWARE IS PROVIDED BY BogDan Vatra <bog_dan_ro@yahoo.com> ''AS IS'' AND ANY
17
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
DISCLAIMED. IN NO EVENT SHALL BogDan Vatra <bog_dan_ro@yahoo.com> BE LIABLE FOR ANY
20
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
package org.kde.necessitas.origo;
30
import java.lang.reflect.Field;
31
import java.lang.reflect.Method;
32
import java.util.ArrayList;
33
import java.util.HashMap;
35
import android.app.Application;
37
public class QtApplication extends Application
39
public final static String QtTAG="Qt";
40
public static Object m_delegateObject = null;
41
public static HashMap<String, ArrayList<Method>> m_delegateMethods= new HashMap<String, ArrayList<Method>>();
42
public static Method dispatchKeyEvent = null;
43
public static Method dispatchPopulateAccessibilityEvent = null;
44
public static Method dispatchTouchEvent = null;
45
public static Method dispatchTrackballEvent = null;
46
public static Method onKeyDown = null;
47
public static Method onKeyMultiple = null;
48
public static Method onKeyUp = null;
49
public static Method onTouchEvent = null;
50
public static Method onTrackballEvent = null;
51
public static Method onActivityResult = null;
52
public static Method onCreate = null;
53
public static Method onKeyLongPress = null;
54
public static Method dispatchKeyShortcutEvent = null;
55
public static Method onKeyShortcut = null;
56
public static Method dispatchGenericMotionEvent = null;
57
public static Method onGenericMotionEvent = null;
59
public static Method onTerminate = null;
60
public static Method onApplyThemeResource = null;
61
public static Method onChildTitleChanged = null;
62
public static Method onConfigurationChanged = null;
63
public static Method onContentChanged = null;
64
public static Method onContextItemSelected = null;
65
public static Method onContextMenuClosed = null;
66
public static Method onCreateContextMenu = null;
67
public static Method onCreateDescription = null;
68
public static Method onCreateDialog = null;
69
public static Method onCreateOptionsMenu = null;
70
public static Method onCreatePanelMenu = null;
71
public static Method onCreatePanelView = null;
72
public static Method onCreateThumbnail = null;
73
public static Method onCreateView = null;
74
public static Method onDestroy = null;
75
public static Method onLowMemory = null;
76
public static Method onMenuItemSelected = null;
77
public static Method onMenuOpened = null;
78
public static Method onNewIntent = null;
79
public static Method onOptionsItemSelected = null;
80
public static Method onOptionsMenuClosed = null;
81
public static Method onPanelClosed = null;
82
public static Method onPause = null;
83
public static Method onPostCreate = null;
84
public static Method onPostResume = null;
85
public static Method onPrepareDialog = null;
86
public static Method onPrepareOptionsMenu = null;
87
public static Method onPreparePanel = null;
88
public static Method onRestart = null;
89
public static Method onRestoreInstanceState = null;
90
public static Method onResume = null;
91
public static Method onRetainNonConfigurationInstance = null;
92
public static Method onSaveInstanceState = null;
93
public static Method onSearchRequested = null;
94
public static Method onStart = null;
95
public static Method onStop = null;
96
public static Method onTitleChanged = null;
97
public static Method onUserInteraction = null;
98
public static Method onUserLeaveHint = null;
99
public static Method onWindowAttributesChanged = null;
100
public static Method onWindowFocusChanged = null;
101
public static Method onAttachedToWindow = null;
102
public static Method onBackPressed = null;
103
public static Method onDetachedFromWindow = null;
104
public static Method onCreateDialog8 = null;
105
public static Method onPrepareDialog8 = null;
106
public static Method onActionModeFinished = null;
107
public static Method onActionModeStarted = null;
108
public static Method onAttachFragment = null;
109
public static Method onCreateView11 = null;
110
public static Method onWindowStartingActionMode = null;
113
public static void setQtActivityDelegate(Object listener)
115
QtApplication.m_delegateObject = listener;
117
ArrayList<Method> delegateMethods = new ArrayList<Method>();
118
for (Method m: listener.getClass().getMethods())
119
if (m.getDeclaringClass().getName().startsWith("org.kde.necessitas"))
120
delegateMethods.add(m);
122
ArrayList<Field> applicationFields = new ArrayList<Field>();
123
for (Field f: QtApplication.class.getFields())
124
if (f.getDeclaringClass().getName().equals(QtApplication.class.getName()))
125
applicationFields.add(f);
127
for (Method delegateMethod:delegateMethods)
130
QtActivity.class.getDeclaredMethod(delegateMethod.getName(), delegateMethod.getParameterTypes());
131
if (QtApplication.m_delegateMethods.containsKey(delegateMethod.getName()))
132
QtApplication.m_delegateMethods.get(delegateMethod.getName()).add(delegateMethod);
135
ArrayList<Method> delegateSet = new ArrayList<Method>();
136
delegateSet.add(delegateMethod);
137
QtApplication.m_delegateMethods.put(delegateMethod.getName(), delegateSet);
139
for(Field applicationField:applicationFields)
141
if (applicationField.getName().equals(delegateMethod.getName()))
144
applicationField.set(null, delegateMethod);
145
} catch (Exception e) {
158
public void onTerminate() {
159
if (m_delegateObject != null && m_delegateMethods.containsKey("onTerminate"))
160
invokeDelegateMethod(m_delegateMethods.get("onTerminate").get(0));
164
public static class InvokeResult
166
public boolean invoked = false;
167
public Object methodReturns = null;
170
private static int stackDeep=-1;
171
public static InvokeResult invokeDelegate(Object... args)
173
InvokeResult result = new InvokeResult();
174
if (m_delegateObject==null)
176
StackTraceElement[] elements=Thread.currentThread().getStackTrace();
179
String activityClassName=QtActivity.class.getCanonicalName();
180
for(int it=0;it<elements.length;it++)
181
if (elements[it].getClassName().equals(activityClassName))
187
final String methodName=elements[stackDeep].getMethodName();
188
if (-1 == stackDeep || !m_delegateMethods.containsKey(methodName))
191
for (Method m:m_delegateMethods.get(methodName))
192
if (m.getParameterTypes().length == args.length)
194
result.methodReturns=invokeDelegateMethod(m, args);
201
public static Object invokeDelegateMethod(Method m, Object... args)
204
return m.invoke(m_delegateObject, args);
205
} catch (Exception e) {