Advertisement
Guest User

Fix to AndroidHarness

a guest
Sep 25th, 2014
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 20.08 KB | None | 0 0
  1. package com.example.JmeAndTest;
  2.  
  3. import android.app.Activity;
  4. import android.app.AlertDialog;
  5. import android.content.DialogInterface;
  6. import android.content.SharedPreferences;
  7. import android.content.pm.ActivityInfo;
  8. import android.graphics.drawable.Drawable;
  9. import android.graphics.drawable.NinePatchDrawable;
  10. import android.os.Bundle;
  11. import android.view.*;
  12. import android.widget.FrameLayout;
  13. import android.widget.ImageView;
  14. import android.widget.TextView;
  15. import com.google.android.gms.ads.AdListener;
  16. import com.google.android.gms.ads.AdRequest;
  17. import com.google.android.gms.ads.InterstitialAd;
  18. import com.jme3.app.AndroidHarness;
  19. import com.jme3.app.Application;
  20. import com.jme3.app.state.AppStateManager;
  21. import com.jme3.audio.AudioRenderer;
  22. import com.jme3.audio.android.AndroidAudioRenderer;
  23. import com.jme3.input.JoyInput;
  24. import com.jme3.input.android.AndroidSensorJoyInput;
  25. import com.jme3.renderer.android.AndroidGLSurfaceView;
  26. import com.jme3.system.AppSettings;
  27. import com.jme3.system.SystemListener;
  28. import com.jme3.system.android.AndroidConfigChooser;
  29. import com.jme3.system.android.JmeAndroidSystem;
  30. import com.jme3.system.android.OGLESContext;
  31. import testgame.TestGameMain;
  32.  
  33. import java.io.PrintWriter;
  34. import java.io.StringWriter;
  35. import java.util.concurrent.Callable;
  36.  
  37. public class MainActivity extends Activity implements DialogInterface.OnClickListener, SystemListener {
  38.  
  39.         protected String appClass = "jme3test.android.Test";
  40.         protected Application app = null;
  41.         protected String audioRendererType = AppSettings.ANDROID_MEDIAPLAYER;
  42.  
  43.         protected boolean joystickEventsEnabled = false;
  44.         protected boolean mouseEventsEnabled = true;
  45.         protected boolean mouseEventsInvertX = false;
  46.         protected boolean mouseEventsInvertY = false;
  47.         protected boolean finishOnAppStop = true;
  48.         /**
  49.          * Set the screen window mode. If screenFullSize is true, then the
  50.          * notification bar and title bar are removed and the screen covers the
  51.          * entire display. If screenFullSize is false, then the notification bar
  52.          * remains visible if screenShowTitle is true while screenFullScreen is
  53.          * false, then the title bar is also displayed under the notification bar.
  54.          */
  55.         protected boolean screenFullScreen = true;
  56.         /**
  57.          * if screenShowTitle is true while screenFullScreen is false, then the
  58.          * title bar is also displayed under the notification bar
  59.          */
  60.         protected boolean screenShowTitle = true;
  61.         /**
  62.          * Splash Screen picture Resource ID. If a Splash Screen is desired, set
  63.          * splashPicID to the value of the Resource ID (i.e. R.drawable.picname). If
  64.          * splashPicID = 0, then no splash screen will be displayed.
  65.          */
  66.         protected int splashPicID = 0;
  67.         protected OGLESContext ctx;
  68.         protected AndroidGLSurfaceView view = null;
  69.         protected boolean isGLThreadPaused = true;
  70.         protected ImageView splashImageView = null;
  71.         protected FrameLayout frameLayout = null;
  72.         private boolean firstDrawFrame = true;
  73.         private boolean inConfigChange = false;
  74.  
  75.  
  76.         private class DataObject {
  77.                 protected Application app = null;
  78.  
  79.         }
  80.  
  81.         private static Application appStatic; // remembers reference to applicaiton when calling loadAd()
  82.  
  83.         public MainActivity(){
  84.                 super();
  85.                 appClass = "testgame.TestGameMain";
  86.                 splashPicID = R.drawable.splash;
  87.                 mouseEventsEnabled = true;
  88.  
  89.         }
  90.  
  91.         @Override
  92.         public Object onRetainNonConfigurationInstance() {
  93.                 //logger.log(Level.FINE, "onRetainNonConfigurationInstance");
  94.                 final DataObject data = new DataObject();
  95.                 data.app = this.app;
  96.                 inConfigChange = true;
  97.  
  98.                 return data;
  99.         }
  100.  
  101.         @Override
  102.         public void onCreate(Bundle savedInstanceState) {
  103.                 super.onCreate(savedInstanceState);
  104.  
  105.                 JmeAndroidSystem.setActivity(this);
  106.                 if (screenFullScreen) {
  107.                         requestWindowFeature(Window.FEATURE_NO_TITLE);
  108.                         getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
  109.                                 WindowManager.LayoutParams.FLAG_FULLSCREEN);
  110.                 } else {
  111.                         if (!screenShowTitle) {
  112.                                 requestWindowFeature(Window.FEATURE_NO_TITLE);
  113.                         }
  114.                 }
  115.  
  116.                 //setRequestedOrientation(screenOrientation); // this is bad. dont do this
  117.  
  118.  
  119.                 final DataObject data = (DataObject) getLastNonConfigurationInstance();
  120.                 if(appStatic != null){
  121.                      this.app = appStatic;
  122.                      appStatic = null; // null appStatic so when the activity closes for real it wont prevent it from happening
  123.                 }else if (data != null) {
  124.                         System.out.println("Using Retained App");
  125.                         this.app = data.app;
  126.                 } else {
  127.                         // Discover the screen reolution
  128.                         //TODO try to find a better way to get a hand on the resolution
  129.                         WindowManager wind = this.getWindowManager();
  130.                         Display disp = wind.getDefaultDisplay();
  131.                         //Log.d("AndroidHarness", "Resolution from Window, width:" + disp.getWidth() + ", height: " + disp.getHeight());
  132.  
  133.                         // Create Settings
  134.                         //logger.log(Level.FINE, "Creating settings");
  135.                         AppSettings settings = new AppSettings(true);
  136.                         settings.setEmulateMouse(mouseEventsEnabled);
  137.                         settings.setEmulateMouseFlipAxis(mouseEventsInvertX, mouseEventsInvertY);
  138.                         settings.setUseJoysticks(joystickEventsEnabled);
  139.                         settings.setBitsPerPixel(24);
  140.                         //settings.setAlphaBits(0);
  141.                         settings.setDepthBits(16);
  142.                         settings.setStencilBits(0);
  143.                         settings.setSamples(0);
  144.                         settings.setResolution(disp.getWidth(), disp.getHeight());
  145.                         settings.setAudioRenderer(audioRendererType);
  146.  
  147.  
  148.                         // Create application instance
  149.                         try {
  150.                                 if (app == null) {
  151.                                         @SuppressWarnings("unchecked")
  152.                                         Class<? extends Application> clazz = (Class<? extends Application>) Class.forName(appClass);
  153.                                         app = clazz.newInstance();
  154.                                 }
  155.  
  156.                                 app.setSettings(settings);
  157.                                 app.start();
  158.                         } catch (Exception ex) {
  159.                                 //handleError("Class " + appClass + " init failed", ex);
  160.                                 setContentView(new TextView(this));
  161.                         }
  162.                 }
  163.  
  164.                 ctx = (OGLESContext) app.getContext();
  165.                 view = ctx.createView(AndroidConfigChooser.ConfigType.BEST, false);
  166.                 //view = ctx.createView();
  167.                 // AndroidHarness wraps the app as a SystemListener.
  168.                 ctx.setSystemListener(this);
  169.                 layoutDisplay();
  170.  
  171.  
  172.  
  173.  
  174.                 //AppStateManager stateManager = app.getStateManager();
  175.                 //jackGM = stateManager.getState(JackGM.class);
  176.                 //jackGM.setManagedActivity(MyActivity.this);
  177.  
  178.         }
  179.  
  180.         private InterstitialAd interstitial;
  181.  
  182.         public void loadAd() {
  183.  
  184.                 runOnUiThread(new Runnable() {
  185.                         public void run() {
  186.                                 interstitial = new InterstitialAd(MyActivity.this);
  187.                                 interstitial.setAdUnitId("ca-app-pub-3542905976626572/8727186243");
  188.                                 interstitial.setAdListener(new AdListener() {
  189.                                         @Override
  190.                                         public void onAdLoaded() {
  191.                                                 if(interstitial.isLoaded()){
  192.                                                         appStatic = app; // setAppStatic = app, so in onDestroy() it wont stop app, and so onCreate() it can get the reference
  193.                                                         interstitial.show();
  194.                                                 }
  195.                                         }
  196.                                 });
  197.  
  198.                                 AdRequest adRequest = new AdRequest.Builder()
  199.                                         .addTestDevice("90BB3531E9DE22C6EE2CFFD0FBD8CD0E")
  200.                                         .addTestDevice("EC11ABF15B7CD200ED08B48C56C2A5BA")
  201.                                         .build();
  202.  
  203.                                 try {
  204.                                         interstitial.loadAd(adRequest);
  205.                                 } catch (java.lang.NoClassDefFoundError e) {
  206.                                         // This is here to catch the fucked up error that gets thrown on my tablet
  207.                                         System.out.println("-------------messed up error was thrown:   " + e.getMessage());
  208.                                         e.printStackTrace();
  209.                                 }
  210.  
  211.  
  212.  
  213.                         }
  214.  
  215.                 });
  216.         }
  217.  
  218.         public void layoutDisplay() {
  219.                 //logger.log(Level.FINE, "Splash Screen Picture Resource ID: {0}", splashPicID);
  220.                 if (view == null) {
  221.                         //logger.log(Level.FINE, "view is null!");
  222.                 }
  223.                 if (splashPicID != 0) {
  224.                         FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
  225.                                 ViewGroup.LayoutParams.FILL_PARENT,
  226.                                 ViewGroup.LayoutParams.FILL_PARENT,
  227.                                 Gravity.CENTER);
  228.  
  229.                         frameLayout = new FrameLayout(this);
  230.                         splashImageView = new ImageView(this);
  231.  
  232.                         Drawable drawable = this.getResources().getDrawable(splashPicID);
  233.                         if (drawable instanceof NinePatchDrawable) {
  234.                                 splashImageView.setBackgroundDrawable(drawable);
  235.                         } else {
  236.                                 splashImageView.setImageResource(splashPicID);
  237.                         }
  238.  
  239.                         if (view.getParent() != null) {
  240.                                 ((ViewGroup) view.getParent()).removeView(view);
  241.                         }
  242.                         frameLayout.addView(view);
  243.  
  244.                         if (splashImageView.getParent() != null) {
  245.                                 ((ViewGroup) splashImageView.getParent()).removeView(splashImageView);
  246.                         }
  247.                         frameLayout.addView(splashImageView, lp);
  248.  
  249.                         setContentView(frameLayout);
  250.                         //logger.log(Level.FINE, "Splash Screen Created");
  251.                 } else {
  252.                         //logger.log(Level.FINE, "Splash Screen Skipped.");
  253.                         setContentView(view);
  254.                 }
  255.         }
  256.  
  257.         public void removeSplashScreen() {
  258.                 //logger.log(Level.FINE, "Splash Screen Picture Resource ID: {0}", splashPicID);
  259.                 if (splashPicID != 0) {
  260.                         if (frameLayout != null) {
  261.                                 if (splashImageView != null) {
  262.                                         this.runOnUiThread(new Runnable() {
  263.                                                 @Override
  264.                                                 public void run() {
  265.                                                         splashImageView.setVisibility(View.INVISIBLE);
  266.                                                         frameLayout.removeView(splashImageView);
  267.                                                 }
  268.                                         });
  269.                                 } else {
  270.                                         //logger.log(Level.FINE, "splashImageView is null");
  271.                                 }
  272.                         } else {
  273.                                 //logger.log(Level.FINE, "frameLayout is null");
  274.                         }
  275.                 }
  276.         }
  277.  
  278.  
  279.         @Override
  280.         protected void onRestart() {
  281.                 super.onRestart();
  282.                 if (app != null) {
  283.                         app.restart();
  284.                 }
  285.         }
  286.  
  287.         @Override
  288.         protected void onStart() {
  289.                 super.onStart();
  290.         }
  291.  
  292.         @Override
  293.         protected void onResume() {
  294.                 super.onResume();
  295.  
  296.                 gainFocus();
  297.         }
  298.  
  299.         @Override
  300.         protected void onPause() {
  301.                 loseFocus();
  302.  
  303.                 super.onPause();
  304.         }
  305.  
  306.         @Override
  307.         protected void onStop() {
  308.                 super.onStop();
  309.         }
  310.  
  311.         @Override
  312.         protected void onDestroy() {
  313.                 final DataObject data = (DataObject) getLastNonConfigurationInstance();
  314.                 if (data != null || inConfigChange || appStatic != null) {
  315.                         //logger.fine("In Config Change, not stopping app.");
  316.                 } else {
  317.                         if (app != null) {
  318.                                 app.stop(!isGLThreadPaused);
  319.                         }
  320.                 }
  321.                 setContentView(new TextView(this));
  322.                 JmeAndroidSystem.setActivity(null);
  323.                 ctx = null;
  324.                 app = null;
  325.                 view = null;
  326.  
  327.                 super.onDestroy();
  328.         }
  329.  
  330.  
  331.  
  332.  
  333.         @Override
  334.         public void initialize() {
  335.                 app.initialize();
  336.         }
  337.  
  338.         @Override
  339.         public void reshape(int width, int height) {
  340.                 app.reshape(width, height);
  341.         }
  342.  
  343.         @Override
  344.         public void update() {
  345.                 app.update();
  346.                 // call to remove the splash screen, if present.
  347.                 // call after app.update() to make sure no gap between
  348.                 // splash screen going away and app display being shown.
  349.                 if (firstDrawFrame) {
  350.                         removeSplashScreen();
  351.                         firstDrawFrame = false;
  352.                 }
  353.         }
  354.  
  355.         @Override
  356.         public void requestClose(boolean esc) {
  357.                 app.requestClose(esc);
  358.         }
  359.  
  360.         @Override
  361.         public void destroy() {
  362.                 if (app != null) {
  363.                         app.destroy();
  364.                 }
  365.                 if (finishOnAppStop) {
  366.                         finish();
  367.                 }
  368.         }
  369.  
  370.         @Override
  371.         public void gainFocus() {
  372.  
  373.                 //logger.fine("gainFocus");
  374.                 if (view != null) {
  375.                         view.onResume();
  376.                 }
  377.  
  378.                 if (app != null) {
  379.                         //resume the audio
  380.                         AudioRenderer result = app.getAudioRenderer();
  381.                         if (result != null) {
  382.                                 if (result instanceof AndroidAudioRenderer) {
  383.                                         AndroidAudioRenderer renderer = (AndroidAudioRenderer) result;
  384.                                         renderer.resumeAll();
  385.                                 }
  386.                         }
  387.                         //resume the sensors (aka joysticks)
  388.                         if (app.getContext() != null) {
  389.                                 JoyInput joyInput = app.getContext().getJoyInput();
  390.                                 if (joyInput != null) {
  391.                                         if (joyInput instanceof AndroidSensorJoyInput) {
  392.                                                 AndroidSensorJoyInput androidJoyInput = (AndroidSensorJoyInput) joyInput;
  393.                                                 androidJoyInput.resumeSensors();
  394.                                         }
  395.                                 }
  396.                         }
  397.                 }
  398.  
  399.                 isGLThreadPaused = false;
  400.  
  401.                 if (app != null) {
  402.                         app.gainFocus();
  403.                 }
  404.  
  405.         }
  406.  
  407.         @Override
  408.         public void loseFocus() {
  409.                 //logger.fine("loseFocus");
  410.                 if (app != null) {
  411.                         app.loseFocus();
  412.                 }
  413.  
  414.                 if (view != null) {
  415.                         view.onPause();
  416.                 }
  417.  
  418.                 if (app != null) {
  419.                         //pause the audio
  420.                         AudioRenderer result = app.getAudioRenderer();
  421.                         if (result != null) {
  422.                                 //logger.log(Level.FINE, "pause: {0}", result.getClass().getSimpleName());
  423.                                 if (result instanceof AndroidAudioRenderer) {
  424.                                         AndroidAudioRenderer renderer = (AndroidAudioRenderer) result;
  425.                                         renderer.pauseAll();
  426.                                 }
  427.                         }
  428.                         //pause the sensors (aka joysticks)
  429.                         if (app.getContext() != null) {
  430.                                 JoyInput joyInput = app.getContext().getJoyInput();
  431.                                 if (joyInput != null) {
  432.                                         if (joyInput instanceof AndroidSensorJoyInput) {
  433.                                                 AndroidSensorJoyInput androidJoyInput = (AndroidSensorJoyInput) joyInput;
  434.                                                 androidJoyInput.pauseSensors();
  435.                                         }
  436.                                 }
  437.                         }
  438.                 }
  439.                 isGLThreadPaused = true;
  440.         }
  441.  
  442.  
  443.  
  444.         /**
  445.          * Called when an error has occurred. By default, will show an error message
  446.          * to the user and print the exception/error to the log.
  447.          */
  448.         @Override
  449.         public void handleError(final String errorMsg, final Throwable t) {
  450.                 String stackTrace = "";
  451.                 String title = "Error";
  452.  
  453.                 if (t != null) {
  454.                         // Convert exception to string
  455.                         StringWriter sw = new StringWriter(100);
  456.                         t.printStackTrace(new PrintWriter(sw));
  457.                         stackTrace = sw.toString();
  458.                         title = t.toString();
  459.                 }
  460.  
  461.                 final String finalTitle = title;
  462.                 final String finalMsg = (errorMsg != null ? errorMsg : "Uncaught Exception")
  463.                         + "\n" + stackTrace;
  464.  
  465.                 System.err.println(finalMsg);
  466.  
  467.                 runOnUiThread(new Runnable() {
  468.                         @Override
  469.                         public void run() {
  470.                                 AlertDialog dialog = new AlertDialog.Builder(MyActivity.this) // .setIcon(R.drawable.alert_dialog_icon)
  471.                                         .setTitle(finalTitle).setPositiveButton("Kill", MyActivity.this).setMessage(finalMsg).create();
  472.                                 dialog.show();
  473.                         }
  474.                 });
  475.         }
  476.  
  477.         /**
  478.          * Called by the android alert dialog, terminate the activity and OpenGL
  479.          * rendering
  480.          *
  481.          * @param dialog
  482.          * @param whichButton
  483.          */
  484.         public void onClick(DialogInterface dialog, int whichButton) {
  485.                 if (whichButton != -2) {
  486.                         if (app != null) {
  487.                                 app.stop(true);
  488.                         }
  489.                         app = null;
  490.                         this.finish();
  491.                 }
  492.         }
  493. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement