~ubuntu-app-review-contributors/ubuntu-app-reviews/limoo

« back to all changes in this revision

Viewing changes to android/src/org/kde/necessitas/origo/QtApplication.java

  • Committer: App Bot
  • Date: 2012-06-12 12:26:24 UTC
  • Revision ID: appbot@holba.ch-20120612122624-yq0ueol26sxtovpm
initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 2009-2011, BogDan Vatra <bog_dan_ro@yahoo.com>
 
3
    All rights reserved.
 
4
 
 
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.
 
15
 
 
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.
 
26
*/
 
27
 
 
28
package org.kde.necessitas.origo;
 
29
 
 
30
import java.lang.reflect.Field;
 
31
import java.lang.reflect.Method;
 
32
import java.util.ArrayList;
 
33
import java.util.HashMap;
 
34
 
 
35
import android.app.Application;
 
36
 
 
37
public class QtApplication extends Application
 
38
{
 
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;
 
58
/*
 
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;
 
111
*/
 
112
 
 
113
    public static void setQtActivityDelegate(Object listener)
 
114
    {
 
115
        QtApplication.m_delegateObject = listener;
 
116
 
 
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);
 
121
 
 
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);
 
126
 
 
127
        for (Method delegateMethod:delegateMethods)
 
128
        {
 
129
            try {
 
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);
 
133
                else
 
134
                {
 
135
                    ArrayList<Method> delegateSet = new ArrayList<Method>();
 
136
                    delegateSet.add(delegateMethod);
 
137
                    QtApplication.m_delegateMethods.put(delegateMethod.getName(), delegateSet);
 
138
                }
 
139
                for(Field applicationField:applicationFields)
 
140
                {
 
141
                    if (applicationField.getName().equals(delegateMethod.getName()))
 
142
                    {
 
143
                        try {
 
144
                            applicationField.set(null, delegateMethod);
 
145
                        } catch (Exception e) {
 
146
                            e.printStackTrace();
 
147
                        }
 
148
                    }
 
149
                }
 
150
            }
 
151
            catch (Exception e)
 
152
            {
 
153
            }
 
154
        }
 
155
    }
 
156
 
 
157
    @Override
 
158
    public void onTerminate() {
 
159
        if (m_delegateObject != null && m_delegateMethods.containsKey("onTerminate"))
 
160
            invokeDelegateMethod(m_delegateMethods.get("onTerminate").get(0));
 
161
        super.onTerminate();
 
162
    }
 
163
 
 
164
    public static class InvokeResult
 
165
    {
 
166
        public boolean invoked = false;
 
167
        public Object methodReturns = null;
 
168
    }
 
169
 
 
170
    private static int stackDeep=-1;
 
171
    public static InvokeResult invokeDelegate(Object... args)
 
172
    {
 
173
        InvokeResult result = new InvokeResult();
 
174
        if (m_delegateObject==null)
 
175
            return result;
 
176
        StackTraceElement[] elements=Thread.currentThread().getStackTrace();
 
177
        if (-1 == stackDeep)
 
178
        {
 
179
            String activityClassName=QtActivity.class.getCanonicalName();
 
180
            for(int it=0;it<elements.length;it++)
 
181
                if (elements[it].getClassName().equals(activityClassName))
 
182
                {
 
183
                    stackDeep=it;
 
184
                    break;
 
185
                }
 
186
        }
 
187
        final String methodName=elements[stackDeep].getMethodName();
 
188
        if (-1 == stackDeep || !m_delegateMethods.containsKey(methodName))
 
189
            return result;
 
190
 
 
191
        for (Method m:m_delegateMethods.get(methodName))
 
192
            if (m.getParameterTypes().length == args.length)
 
193
            {
 
194
                result.methodReturns=invokeDelegateMethod(m, args);
 
195
                result.invoked=true;
 
196
                return result;
 
197
            }
 
198
        return result;
 
199
    }
 
200
 
 
201
    public static Object invokeDelegateMethod(Method m, Object... args)
 
202
    {
 
203
        try {
 
204
            return m.invoke(m_delegateObject, args);
 
205
        } catch (Exception e) {
 
206
            e.printStackTrace();
 
207
        }
 
208
        return null;
 
209
    }
 
210
}