Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.JmeAndTest;
- import android.app.Activity;
- import android.app.AlertDialog;
- import android.content.DialogInterface;
- import android.content.SharedPreferences;
- import android.content.pm.ActivityInfo;
- import android.graphics.drawable.Drawable;
- import android.graphics.drawable.NinePatchDrawable;
- import android.os.Bundle;
- import android.view.*;
- import android.widget.FrameLayout;
- import android.widget.ImageView;
- import android.widget.TextView;
- import com.google.android.gms.ads.AdListener;
- import com.google.android.gms.ads.AdRequest;
- import com.google.android.gms.ads.InterstitialAd;
- import com.jme3.app.AndroidHarness;
- import com.jme3.app.Application;
- import com.jme3.app.state.AppStateManager;
- import com.jme3.audio.AudioRenderer;
- import com.jme3.audio.android.AndroidAudioRenderer;
- import com.jme3.input.JoyInput;
- import com.jme3.input.android.AndroidSensorJoyInput;
- import com.jme3.renderer.android.AndroidGLSurfaceView;
- import com.jme3.system.AppSettings;
- import com.jme3.system.SystemListener;
- import com.jme3.system.android.AndroidConfigChooser;
- import com.jme3.system.android.JmeAndroidSystem;
- import com.jme3.system.android.OGLESContext;
- import testgame.TestGameMain;
- import java.io.PrintWriter;
- import java.io.StringWriter;
- import java.util.concurrent.Callable;
- public class MainActivity extends Activity implements DialogInterface.OnClickListener, SystemListener {
- protected String appClass = "jme3test.android.Test";
- protected Application app = null;
- protected String audioRendererType = AppSettings.ANDROID_MEDIAPLAYER;
- protected boolean joystickEventsEnabled = false;
- protected boolean mouseEventsEnabled = true;
- protected boolean mouseEventsInvertX = false;
- protected boolean mouseEventsInvertY = false;
- protected boolean finishOnAppStop = true;
- /**
- * Set the screen window mode. If screenFullSize is true, then the
- * notification bar and title bar are removed and the screen covers the
- * entire display. If screenFullSize is false, then the notification bar
- * remains visible if screenShowTitle is true while screenFullScreen is
- * false, then the title bar is also displayed under the notification bar.
- */
- protected boolean screenFullScreen = true;
- /**
- * if screenShowTitle is true while screenFullScreen is false, then the
- * title bar is also displayed under the notification bar
- */
- protected boolean screenShowTitle = true;
- /**
- * Splash Screen picture Resource ID. If a Splash Screen is desired, set
- * splashPicID to the value of the Resource ID (i.e. R.drawable.picname). If
- * splashPicID = 0, then no splash screen will be displayed.
- */
- protected int splashPicID = 0;
- protected OGLESContext ctx;
- protected AndroidGLSurfaceView view = null;
- protected boolean isGLThreadPaused = true;
- protected ImageView splashImageView = null;
- protected FrameLayout frameLayout = null;
- private boolean firstDrawFrame = true;
- private boolean inConfigChange = false;
- private class DataObject {
- protected Application app = null;
- }
- private static Application appStatic; // remembers reference to applicaiton when calling loadAd()
- public MainActivity(){
- super();
- appClass = "testgame.TestGameMain";
- splashPicID = R.drawable.splash;
- mouseEventsEnabled = true;
- }
- @Override
- public Object onRetainNonConfigurationInstance() {
- //logger.log(Level.FINE, "onRetainNonConfigurationInstance");
- final DataObject data = new DataObject();
- data.app = this.app;
- inConfigChange = true;
- return data;
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- JmeAndroidSystem.setActivity(this);
- if (screenFullScreen) {
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- } else {
- if (!screenShowTitle) {
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- }
- }
- //setRequestedOrientation(screenOrientation); // this is bad. dont do this
- final DataObject data = (DataObject) getLastNonConfigurationInstance();
- if(appStatic != null){
- this.app = appStatic;
- appStatic = null; // null appStatic so when the activity closes for real it wont prevent it from happening
- }else if (data != null) {
- System.out.println("Using Retained App");
- this.app = data.app;
- } else {
- // Discover the screen reolution
- //TODO try to find a better way to get a hand on the resolution
- WindowManager wind = this.getWindowManager();
- Display disp = wind.getDefaultDisplay();
- //Log.d("AndroidHarness", "Resolution from Window, width:" + disp.getWidth() + ", height: " + disp.getHeight());
- // Create Settings
- //logger.log(Level.FINE, "Creating settings");
- AppSettings settings = new AppSettings(true);
- settings.setEmulateMouse(mouseEventsEnabled);
- settings.setEmulateMouseFlipAxis(mouseEventsInvertX, mouseEventsInvertY);
- settings.setUseJoysticks(joystickEventsEnabled);
- settings.setBitsPerPixel(24);
- //settings.setAlphaBits(0);
- settings.setDepthBits(16);
- settings.setStencilBits(0);
- settings.setSamples(0);
- settings.setResolution(disp.getWidth(), disp.getHeight());
- settings.setAudioRenderer(audioRendererType);
- // Create application instance
- try {
- if (app == null) {
- @SuppressWarnings("unchecked")
- Class<? extends Application> clazz = (Class<? extends Application>) Class.forName(appClass);
- app = clazz.newInstance();
- }
- app.setSettings(settings);
- app.start();
- } catch (Exception ex) {
- //handleError("Class " + appClass + " init failed", ex);
- setContentView(new TextView(this));
- }
- }
- ctx = (OGLESContext) app.getContext();
- view = ctx.createView(AndroidConfigChooser.ConfigType.BEST, false);
- //view = ctx.createView();
- // AndroidHarness wraps the app as a SystemListener.
- ctx.setSystemListener(this);
- layoutDisplay();
- //AppStateManager stateManager = app.getStateManager();
- //jackGM = stateManager.getState(JackGM.class);
- //jackGM.setManagedActivity(MyActivity.this);
- }
- private InterstitialAd interstitial;
- public void loadAd() {
- runOnUiThread(new Runnable() {
- public void run() {
- interstitial = new InterstitialAd(MyActivity.this);
- interstitial.setAdUnitId("ca-app-pub-3542905976626572/8727186243");
- interstitial.setAdListener(new AdListener() {
- @Override
- public void onAdLoaded() {
- if(interstitial.isLoaded()){
- appStatic = app; // setAppStatic = app, so in onDestroy() it wont stop app, and so onCreate() it can get the reference
- interstitial.show();
- }
- }
- });
- AdRequest adRequest = new AdRequest.Builder()
- .addTestDevice("90BB3531E9DE22C6EE2CFFD0FBD8CD0E")
- .addTestDevice("EC11ABF15B7CD200ED08B48C56C2A5BA")
- .build();
- try {
- interstitial.loadAd(adRequest);
- } catch (java.lang.NoClassDefFoundError e) {
- // This is here to catch the fucked up error that gets thrown on my tablet
- System.out.println("-------------messed up error was thrown: " + e.getMessage());
- e.printStackTrace();
- }
- }
- });
- }
- public void layoutDisplay() {
- //logger.log(Level.FINE, "Splash Screen Picture Resource ID: {0}", splashPicID);
- if (view == null) {
- //logger.log(Level.FINE, "view is null!");
- }
- if (splashPicID != 0) {
- FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(
- ViewGroup.LayoutParams.FILL_PARENT,
- ViewGroup.LayoutParams.FILL_PARENT,
- Gravity.CENTER);
- frameLayout = new FrameLayout(this);
- splashImageView = new ImageView(this);
- Drawable drawable = this.getResources().getDrawable(splashPicID);
- if (drawable instanceof NinePatchDrawable) {
- splashImageView.setBackgroundDrawable(drawable);
- } else {
- splashImageView.setImageResource(splashPicID);
- }
- if (view.getParent() != null) {
- ((ViewGroup) view.getParent()).removeView(view);
- }
- frameLayout.addView(view);
- if (splashImageView.getParent() != null) {
- ((ViewGroup) splashImageView.getParent()).removeView(splashImageView);
- }
- frameLayout.addView(splashImageView, lp);
- setContentView(frameLayout);
- //logger.log(Level.FINE, "Splash Screen Created");
- } else {
- //logger.log(Level.FINE, "Splash Screen Skipped.");
- setContentView(view);
- }
- }
- public void removeSplashScreen() {
- //logger.log(Level.FINE, "Splash Screen Picture Resource ID: {0}", splashPicID);
- if (splashPicID != 0) {
- if (frameLayout != null) {
- if (splashImageView != null) {
- this.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- splashImageView.setVisibility(View.INVISIBLE);
- frameLayout.removeView(splashImageView);
- }
- });
- } else {
- //logger.log(Level.FINE, "splashImageView is null");
- }
- } else {
- //logger.log(Level.FINE, "frameLayout is null");
- }
- }
- }
- @Override
- protected void onRestart() {
- super.onRestart();
- if (app != null) {
- app.restart();
- }
- }
- @Override
- protected void onStart() {
- super.onStart();
- }
- @Override
- protected void onResume() {
- super.onResume();
- gainFocus();
- }
- @Override
- protected void onPause() {
- loseFocus();
- super.onPause();
- }
- @Override
- protected void onStop() {
- super.onStop();
- }
- @Override
- protected void onDestroy() {
- final DataObject data = (DataObject) getLastNonConfigurationInstance();
- if (data != null || inConfigChange || appStatic != null) {
- //logger.fine("In Config Change, not stopping app.");
- } else {
- if (app != null) {
- app.stop(!isGLThreadPaused);
- }
- }
- setContentView(new TextView(this));
- JmeAndroidSystem.setActivity(null);
- ctx = null;
- app = null;
- view = null;
- super.onDestroy();
- }
- @Override
- public void initialize() {
- app.initialize();
- }
- @Override
- public void reshape(int width, int height) {
- app.reshape(width, height);
- }
- @Override
- 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;
- }
- }
- @Override
- public void requestClose(boolean esc) {
- app.requestClose(esc);
- }
- @Override
- public void destroy() {
- if (app != null) {
- app.destroy();
- }
- if (finishOnAppStop) {
- finish();
- }
- }
- @Override
- public void gainFocus() {
- //logger.fine("gainFocus");
- if (view != null) {
- view.onResume();
- }
- if (app != null) {
- //resume the audio
- AudioRenderer result = app.getAudioRenderer();
- if (result != null) {
- if (result instanceof AndroidAudioRenderer) {
- AndroidAudioRenderer renderer = (AndroidAudioRenderer) result;
- renderer.resumeAll();
- }
- }
- //resume the sensors (aka joysticks)
- if (app.getContext() != null) {
- JoyInput joyInput = app.getContext().getJoyInput();
- if (joyInput != null) {
- if (joyInput instanceof AndroidSensorJoyInput) {
- AndroidSensorJoyInput androidJoyInput = (AndroidSensorJoyInput) joyInput;
- androidJoyInput.resumeSensors();
- }
- }
- }
- }
- isGLThreadPaused = false;
- if (app != null) {
- app.gainFocus();
- }
- }
- @Override
- public void loseFocus() {
- //logger.fine("loseFocus");
- if (app != null) {
- app.loseFocus();
- }
- if (view != null) {
- view.onPause();
- }
- if (app != null) {
- //pause the audio
- AudioRenderer result = app.getAudioRenderer();
- if (result != null) {
- //logger.log(Level.FINE, "pause: {0}", result.getClass().getSimpleName());
- if (result instanceof AndroidAudioRenderer) {
- AndroidAudioRenderer renderer = (AndroidAudioRenderer) result;
- renderer.pauseAll();
- }
- }
- //pause the sensors (aka joysticks)
- if (app.getContext() != null) {
- JoyInput joyInput = app.getContext().getJoyInput();
- if (joyInput != null) {
- if (joyInput instanceof AndroidSensorJoyInput) {
- AndroidSensorJoyInput androidJoyInput = (AndroidSensorJoyInput) joyInput;
- androidJoyInput.pauseSensors();
- }
- }
- }
- }
- isGLThreadPaused = true;
- }
- /**
- * Called when an error has occurred. By default, will show an error message
- * to the user and print the exception/error to the log.
- */
- @Override
- public void handleError(final String errorMsg, final Throwable t) {
- String stackTrace = "";
- String title = "Error";
- if (t != null) {
- // Convert exception to string
- StringWriter sw = new StringWriter(100);
- t.printStackTrace(new PrintWriter(sw));
- stackTrace = sw.toString();
- title = t.toString();
- }
- final String finalTitle = title;
- final String finalMsg = (errorMsg != null ? errorMsg : "Uncaught Exception")
- + "\n" + stackTrace;
- System.err.println(finalMsg);
- runOnUiThread(new Runnable() {
- @Override
- public void run() {
- AlertDialog dialog = new AlertDialog.Builder(MyActivity.this) // .setIcon(R.drawable.alert_dialog_icon)
- .setTitle(finalTitle).setPositiveButton("Kill", MyActivity.this).setMessage(finalMsg).create();
- dialog.show();
- }
- });
- }
- /**
- * Called by the android alert dialog, terminate the activity and OpenGL
- * rendering
- *
- * @param dialog
- * @param whichButton
- */
- public void onClick(DialogInterface dialog, int whichButton) {
- if (whichButton != -2) {
- if (app != null) {
- app.stop(true);
- }
- app = null;
- this.finish();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement