OSDN Git Service

Android: Fixed bug in AndroidHarness if appClass can not be found
[mikumikustudio/MikuMikuStudio.git] / engine / src / android / com / jme3 / app / AndroidHarness.java
1 package com.jme3.app;\r
2 \r
3 import java.util.logging.Logger;\r
4 \r
5 import android.app.Activity;\r
6 import android.app.AlertDialog;\r
7 import android.content.DialogInterface;\r
8 import android.opengl.GLSurfaceView;\r
9 import android.os.Bundle;\r
10 import android.view.SurfaceView;\r
11 import android.view.View;\r
12 import android.view.Window;\r
13 import android.view.WindowManager;\r
14 import android.widget.TextView;\r
15 \r
16 import com.jme3.app.Application;\r
17 import com.jme3.input.TouchInput;\r
18 import com.jme3.input.android.AndroidInput;\r
19 import com.jme3.input.controls.TouchListener;\r
20 import com.jme3.input.controls.TouchTrigger;\r
21 import com.jme3.input.event.TouchEvent;\r
22 import com.jme3.system.AppSettings;\r
23 import com.jme3.system.JmeSystem;\r
24 import com.jme3.system.android.OGLESContext;\r
25 import com.jme3.system.android.AndroidConfigChooser.ConfigType;\r
26 \r
27 \r
28 /**\r
29  * <code>AndroidHarness</code> wraps a jme application object and runs it on Android\r
30  * @author Kirill\r
31  * @author larynx\r
32  */\r
33 public class AndroidHarness extends Activity implements TouchListener, DialogInterface.OnClickListener\r
34 {\r
35     protected final static Logger logger = Logger.getLogger(AndroidHarness.class.getName());\r
36     \r
37     /**\r
38      * The application class to start\r
39      */\r
40     protected String appClass = "jme3test.android.Test";\r
41     \r
42     /**\r
43      * The jme3 application object\r
44      */\r
45     protected Application app = null;\r
46     \r
47     /**\r
48      * ConfigType.FASTEST is RGB565, GLSurfaceView default\r
49      * ConfigType.BEST is RGBA8888 or better if supported by the hardware\r
50      */\r
51     protected ConfigType eglConfigType = ConfigType.FASTEST;\r
52     \r
53     /**\r
54      * If true all valid and not valid egl configs are logged\r
55      */\r
56     protected boolean eglConfigVerboseLogging = false;\r
57 \r
58     /**\r
59      * Title of the exit dialog, default is "Do you want to exit?"\r
60      */\r
61     protected String exitDialogTitle = "Do you want to exit?";\r
62     /**\r
63      * Message of the exit dialog, default is "Use your home key to bring this app into the background or exit to terminate it."\r
64      */\r
65     protected String exitDialogMessage = "Use your home key to bring this app into the background or exit to terminate it.";\r
66 \r
67           \r
68     protected OGLESContext ctx;\r
69     protected GLSurfaceView view;\r
70     \r
71     final private String ESCAPE_EVENT = "TouchEscape"; \r
72 \r
73     @Override\r
74     public void onCreate(Bundle savedInstanceState) \r
75     {\r
76         super.onCreate(savedInstanceState);\r
77 \r
78         JmeSystem.setResources(getResources());\r
79         JmeSystem.setActivity(this);\r
80 \r
81         requestWindowFeature(Window.FEATURE_NO_TITLE);\r
82         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,\r
83         WindowManager.LayoutParams.FLAG_FULLSCREEN);\r
84 \r
85         AppSettings settings = new AppSettings(true);\r
86         AndroidInput input = new AndroidInput(this);\r
87                 \r
88         // Create application instance\r
89         try\r
90         {\r
91             app = null;\r
92             view = null;\r
93             \r
94             @SuppressWarnings("unchecked")\r
95             Class<? extends Application> clazz = (Class<? extends Application>) Class.forName(appClass);\r
96             app = clazz.newInstance();\r
97             \r
98             app.setSettings(settings);\r
99             app.start();    \r
100             ctx = (OGLESContext) app.getContext();\r
101             view = ctx.createView(input, eglConfigType, eglConfigVerboseLogging);\r
102             setContentView(view);               \r
103         }\r
104         catch (Exception ex)\r
105         {\r
106             handleError("Class " + appClass + " init failed", ex);\r
107             setContentView(new TextView(this));\r
108         }\r
109     }\r
110 \r
111 \r
112     @Override\r
113     protected void onRestart(){\r
114         super.onRestart(); \r
115         if (app != null)\r
116             app.restart();\r
117         logger.info("onRestart");\r
118     }\r
119     \r
120 \r
121     @Override\r
122     protected void onStart(){\r
123         super.onStart();\r
124         logger.info("onStart");\r
125     }\r
126     \r
127     @Override\r
128     protected void onResume() {\r
129         super.onResume();\r
130         if (view != null)\r
131             view.onResume();\r
132         logger.info("onResume");\r
133     }\r
134 \r
135     @Override\r
136     protected void onPause() {\r
137         super.onPause();\r
138         if (view != null)\r
139             view.onPause();\r
140         logger.info("onPause");\r
141     }\r
142     \r
143     @Override\r
144     protected void onStop(){\r
145         super.onStop();\r
146         logger.info("onStop");\r
147     }\r
148 \r
149     @Override\r
150     protected void onDestroy(){\r
151         if (app != null)\r
152             app.stop(true);\r
153         super.onDestroy();                \r
154         logger.info("onDestroy");\r
155     }\r
156 \r
157     public Application getJmeApplication()\r
158     {\r
159         return app;\r
160     }\r
161     \r
162     /**\r
163      * Called when an error has occured. This is typically\r
164      * invoked when an uncought exception is thrown in the render thread.\r
165      * @param errorMsg The error message, if any, or null.\r
166      * @param t Throwable object, or null.\r
167      */\r
168     public void handleError(final String errorMsg, final Throwable t)\r
169     {\r
170         \r
171         String s = "";\r
172         if (t != null && t.getStackTrace() != null)\r
173         {\r
174             for (StackTraceElement ste: t.getStackTrace())\r
175             {\r
176                 s +=  ste.getClassName() + "." + ste.getMethodName() + "(" + + ste.getLineNumber() + ") ";\r
177             }\r
178         }                \r
179         \r
180         final String sTrace = s;\r
181         \r
182         logger.severe(t != null ? t.toString() : "OpenGL Exception");\r
183         logger.severe((errorMsg != null ? errorMsg + ": " : "") + sTrace);\r
184         \r
185         this.runOnUiThread(new Runnable() {\r
186             @Override\r
187             public void run() \r
188             {                                \r
189                 AlertDialog dialog = new AlertDialog.Builder(AndroidHarness.this)\r
190                // .setIcon(R.drawable.alert_dialog_icon)\r
191                 .setTitle(t != null ? (t.getMessage() != null ? (t.getMessage() + ": " + t.getClass().getName()) : t.getClass().getName()) : "OpenGL Exception")\r
192                 .setPositiveButton("Kill", AndroidHarness.this)\r
193                 .setMessage((errorMsg != null ? errorMsg + ": " : "") + sTrace)\r
194                 .create();    \r
195                 dialog.show();                \r
196             }\r
197         });\r
198         \r
199     }\r
200     \r
201     /**\r
202      * Called by the android alert dialog, terminate the activity and OpenGL rendering\r
203      * @param dialog\r
204      * @param whichButton\r
205      */\r
206     public void onClick(DialogInterface dialog, int whichButton) \r
207     {        \r
208         if (whichButton != -2)\r
209         {\r
210             if (app != null)\r
211                 app.stop(true);\r
212             this.finish();\r
213         }\r
214     }\r
215     \r
216     /**\r
217      * Gets called by the InputManager on all touch/drag/scale events\r
218      */    \r
219     @Override    \r
220     public void onTouch(String name, TouchEvent evt, float tpf)  \r
221     {\r
222         if (name.equals(ESCAPE_EVENT))\r
223         {\r
224             switch(evt.getType())\r
225             {                \r
226                 case KEY_UP:\r
227                     this.runOnUiThread(new Runnable() \r
228                     {\r
229                         @Override\r
230                         public void run() \r
231                         {                                                \r
232                             AlertDialog dialog = new AlertDialog.Builder(AndroidHarness.this)\r
233                            // .setIcon(R.drawable.alert_dialog_icon)\r
234                             .setTitle(exitDialogTitle)\r
235                             .setPositiveButton("Yes", AndroidHarness.this)\r
236                             .setNegativeButton("No", AndroidHarness.this)\r
237                             .setMessage(exitDialogMessage)\r
238                             .create();    \r
239                             dialog.show();                \r
240                         }\r
241                     });\r
242                             \r
243                             \r
244                     break;\r
245                     \r
246                default:\r
247                    break;\r
248             }\r
249         }\r
250                         \r
251     }    \r
252     \r
253 }\r