Browse Source

Removed AndroidHarness specific calls from the OGLESContext.
- The harness is now a system listener and dispatch the system events to the jme app
- Every Harness related code is now in the harness (exit hook dialog, and mapping addition, spash screen hiding,....)
- Added a handleExitHook flag to configure if the Harness should handle exit or not.

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9238 75d07b2b-3a1a-0410-a2c5-0572b91ccdca

rem..om 13 years ago
parent
commit
191bd21b40

+ 55 - 7
engine/src/android/com/jme3/app/AndroidHarness.java

@@ -15,11 +15,14 @@ import android.widget.ImageView;
 import android.widget.TextView;
 import com.jme3.audio.AudioRenderer;
 import com.jme3.audio.android.AndroidAudioRenderer;
+import com.jme3.input.TouchInput;
 import com.jme3.input.android.AndroidInput;
 import com.jme3.input.controls.TouchListener;
+import com.jme3.input.controls.TouchTrigger;
 import com.jme3.input.event.TouchEvent;
 import com.jme3.system.AppSettings;
 import com.jme3.system.JmeSystem;
+import com.jme3.system.SystemListener;
 import com.jme3.system.android.AndroidConfigChooser.ConfigType;
 import com.jme3.system.android.JmeAndroidSystem;
 import com.jme3.system.android.OGLESContext;
@@ -37,7 +40,7 @@ import java.util.logging.Logger;
  * @author Kirill
  * @author larynx
  */
-public class AndroidHarness extends Activity implements TouchListener, DialogInterface.OnClickListener {
+public class AndroidHarness extends Activity implements TouchListener, DialogInterface.OnClickListener, SystemListener {
 
     protected final static Logger logger = Logger.getLogger(AndroidHarness.class.getName());
     /**
@@ -73,6 +76,10 @@ public class AndroidHarness extends Activity implements TouchListener, DialogInt
      * if true finish this activity when the jme app is stopped
      */
     protected boolean finishOnAppStop = true;
+    /**
+     * set to false if you don't want the harness to handle the exit hook
+     */
+    protected boolean handleExitHook = true;
     /**
      * Title of the exit dialog, default is "Do you want to exit?"
      */
@@ -118,6 +125,7 @@ public class AndroidHarness extends Activity implements TouchListener, DialogInt
     private ImageView splashImageView = null;
     private FrameLayout frameLayout = null;
     final private String ESCAPE_EVENT = "TouchEscape";
+    private boolean firstDrawFrame = true;
 
     static {
         try {
@@ -190,7 +198,8 @@ public class AndroidHarness extends Activity implements TouchListener, DialogInt
 
             AppSettings s = ctx.getSettings();
             logger.log(Level.INFO, "Settings: Width {0} Height {1}", new Object[]{s.getWidth(), s.getHeight()});
-
+            //setting the Harness as the system listener
+            ctx.setSystemListener(this);
             layoutDisplay();
         } catch (Exception ex) {
             handleError("Class " + appClass + " init failed", ex);
@@ -244,7 +253,7 @@ public class AndroidHarness extends Activity implements TouchListener, DialogInt
         //pause the audio
         AudioRenderer result = app.getAudioRenderer();
         if (result != null) {
-            logger.info("pause: " + result.getClass().getSimpleName());
+            logger.log(Level.INFO, "pause: {0}", result.getClass().getSimpleName());
             if (result instanceof AndroidAudioRenderer) {
                 AndroidAudioRenderer renderer = (AndroidAudioRenderer) result;
                 renderer.pauseAll();
@@ -321,6 +330,7 @@ public class AndroidHarness extends Activity implements TouchListener, DialogInt
             if (app != null) {
                 app.stop(true);
             }
+            app = null;
             this.finish();
         }
     }
@@ -400,9 +410,47 @@ public class AndroidHarness extends Activity implements TouchListener, DialogInt
         }
     }
 
-    public boolean isFinishOnAppStop() {
-        return finishOnAppStop;
+    public void initialize() {
+        app.initialize();
+        if (handleExitHook) {
+            app.getInputManager().addMapping(ESCAPE_EVENT, new TouchTrigger(TouchInput.KEYCODE_BACK));
+            app.getInputManager().addListener(this, new String[]{ESCAPE_EVENT});
+        }
+    }
+
+    public void reshape(int width, int height) {
+        app.reshape(width, height);
+    }
+
+    public void update() {
+        app.update();
+        // call to remove the splash screen, if present.
+        // call after app.update() to make sure no gap between
+        // splash screen going away and app display being shown.
+        if (firstDrawFrame) {
+            removeSplashScreen();
+            firstDrawFrame = false;
+        }
+    }
+
+    public void requestClose(boolean esc) {
+        app.requestClose(esc);
+    }
+
+    public void gainFocus() {
+        app.gainFocus();
+    }
+
+    public void loseFocus() {
+        app.loseFocus();
+    }
+
+    public void destroy() {
+        if (app != null) {
+            app.destroy();
+        }       
+        if (finishOnAppStop) {
+            finish();
+        }
     }
-    
-    
 }

+ 7 - 41
engine/src/android/com/jme3/system/android/OGLESContext.java

@@ -86,7 +86,6 @@ public class OGLESContext implements JmeContext, GLSurfaceView.Renderer, SoftTex
     protected SystemListener listener;
     protected boolean autoFlush = true;
     protected AndroidInput view;
-    private boolean firstDrawFrame = true;
     //protected int minFrameDuration = 1000 / frameRate;  // Set a max FPS of 33
     protected int minFrameDuration = 0;                   // No FPS cap
     /**
@@ -95,7 +94,6 @@ public class OGLESContext implements JmeContext, GLSurfaceView.Renderer, SoftTex
      */
     protected int clientOpenGLESVersion = 1;
     protected boolean verboseLogging = false;
-    final private String ESCAPE_EVENT = "TouchEscape";
 
     public OGLESContext() {
     }
@@ -215,24 +213,15 @@ public class OGLESContext implements JmeContext, GLSurfaceView.Renderer, SoftTex
         logger.info("OGLESContext create");
         logger.info("Running on thread: " + Thread.currentThread().getName());
 
-        final Context ctx = this.view.getContext();
 
         // Setup unhandled Exception Handler
-        if (ctx instanceof AndroidHarness) {
-            Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
 
-                public void uncaughtException(Thread thread, Throwable thrown) {
-                    ((AndroidHarness) ctx).handleError("Exception thrown in " + thread.toString(), thrown);
-                }
-            });
-        } else {
-            Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
+        Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
 
-                public void uncaughtException(Thread thread, Throwable thrown) {
-                    listener.handleError("Exception thrown in " + thread.toString(), thrown);
-                }
-            });
-        }
+            public void uncaughtException(Thread thread, Throwable thrown) {
+                listener.handleError("Exception thrown in " + thread.toString(), thrown);
+            }
+        });
 
         if (clientOpenGLESVersion < 2) {
             throw new UnsupportedOperationException("OpenGL ES 2.0 is not supported on this device");
@@ -247,14 +236,6 @@ public class OGLESContext implements JmeContext, GLSurfaceView.Renderer, SoftTex
         renderer.initialize();
         listener.initialize();
 
-        // Setup exit hook
-        if (ctx instanceof AndroidHarness) {
-            Application app = ((AndroidHarness) ctx).getJmeApplication();
-            if (app.getInputManager() != null) {
-                app.getInputManager().addMapping(ESCAPE_EVENT, new TouchTrigger(TouchInput.KEYCODE_BACK));
-                app.getInputManager().addListener((AndroidHarness) ctx, new String[]{ESCAPE_EVENT});
-            }
-        }
 
         JmeSystem.setSoftTextDialogInput(this);
 
@@ -282,13 +263,7 @@ public class OGLESContext implements JmeContext, GLSurfaceView.Renderer, SoftTex
             logger.info("Display destroyed.");
 
             renderable.set(false);
-            final Context ctx = this.view.getContext();
-            if (ctx instanceof AndroidHarness) {
-                AndroidHarness harness = (AndroidHarness) ctx;
-                if (harness.isFinishOnAppStop()) {
-                    harness.finish();
-                }
-            }
+
         }
     }
 
@@ -371,6 +346,7 @@ public class OGLESContext implements JmeContext, GLSurfaceView.Renderer, SoftTex
         logger.info("GL Surface changed, width: " + width + " height: " + height);
         settings.setResolution(width, height);
         listener.reshape(width, height);
+        //     androidListener.reshape(width, height);
     }
 
     // SystemListener:update
@@ -390,16 +366,6 @@ public class OGLESContext implements JmeContext, GLSurfaceView.Renderer, SoftTex
 
             listener.update();
 
-            // call to AndroidHarness to remove the splash screen, if present.
-            // call after listener.update() to make sure no gap between
-            //   splash screen going away and app display being shown.
-            if (firstDrawFrame) {
-                final Context ctx = this.view.getContext();
-                if (ctx instanceof AndroidHarness) {
-                    ((AndroidHarness) ctx).removeSplashScreen();
-                }
-                firstDrawFrame = false;
-            }
 
             if (autoFlush) {
                 renderer.onFrame();