@Override
 protected void onPause() {
   super.onPause();
   Log.i(TAG, "onPause");
   loseAudioFocus(this.audioManager, this.audioFocusChangeListener);
   NativeApp.pause();
   mSurfaceView.onPause();
 }
 @Override
 protected void onDestroy() {
   super.onDestroy();
   Log.i(TAG, "onDestroy");
   mSurfaceView.onDestroy();
   NativeApp.audioShutdown();
   // Probably vain attempt to help the garbage collector...
   mSurfaceView = null;
   audioFocusChangeListener = null;
   audioManager = null;
   unregisterCallbacks();
 }
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    registerCallbacks();

    updateDisplayMetrics(null);

    if (!initialized) {
      Initialize();
      initialized = true;
    }

    // OK, config should be initialized, we can query for screen rotation.
    updateScreenRotation();

    // Keep the screen bright - very annoying if it goes dark when tilting away
    Window window = this.getWindow();
    window.addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setVolumeControlStream(AudioManager.STREAM_MUSIC);

    gainAudioFocus(this.audioManager, this.audioFocusChangeListener);
    NativeApp.audioInit();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
      updateSystemUiVisibility();
      if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
        setupSystemUiCallback();
      }
    }
    updateDisplayMetrics(null);

    NativeApp.computeDesiredBackbufferDimensions();
    int bbW = NativeApp.getDesiredBackbufferWidth();
    int bbH = NativeApp.getDesiredBackbufferHeight();

    mSurfaceView = new NativeSurfaceView(NativeActivity.this, bbW, bbH);
    mSurfaceView.getHolder().addCallback(NativeActivity.this);
    Log.i(TAG, "setcontentview before");
    setContentView(mSurfaceView);
    Log.i(TAG, "setcontentview after");

    mRenderLoopThread = new Thread(mEmulationRunner);
    mRenderLoopThread.start();
  }
  @Override
  protected void onResume() {
    super.onResume();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
      updateSystemUiVisibility();
    }
    // OK, config should be initialized, we can query for screen rotation.
    updateScreenRotation();

    Log.i(TAG, "onResume");
    if (mSurfaceView != null) {
      mSurfaceView.onResume();
    } else {
      Log.e(TAG, "mGLSurfaceView really shouldn't be null in onResume");
    }

    gainAudioFocus(this.audioManager, this.audioFocusChangeListener);
    NativeApp.resume();
  }
 public boolean processCommand(String command, String params) {
   if (command.equals("launchBrowser")) {
     try {
       Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(params));
       startActivity(i);
       return true;
     } catch (Exception e) {
       // No browser?
       Log.e(TAG, e.toString());
       return false;
     }
   } else if (command.equals("launchEmail")) {
     try {
       Intent send = new Intent(Intent.ACTION_SENDTO);
       String uriText;
       uriText = "mailto:[email protected]" + "?subject=Your app is..." + "&body=great! Or?";
       uriText = uriText.replace(" ", "%20");
       Uri uri = Uri.parse(uriText);
       send.setData(uri);
       startActivity(Intent.createChooser(send, "E-mail the app author!"));
       return true;
     } catch (Exception e) { // For example, android.content.ActivityNotFoundException
       Log.e(TAG, e.toString());
       return false;
     }
   } else if (command.equals("sharejpeg")) {
     try {
       Intent share = new Intent(Intent.ACTION_SEND);
       share.setType("image/jpeg");
       share.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + params));
       startActivity(Intent.createChooser(share, "Share Picture"));
       return true;
     } catch (Exception e) { // For example, android.content.ActivityNotFoundException
       Log.e(TAG, e.toString());
       return false;
     }
   } else if (command.equals("sharetext")) {
     try {
       Intent sendIntent = new Intent();
       sendIntent.setType("text/plain");
       sendIntent.putExtra(Intent.EXTRA_TEXT, params);
       sendIntent.setAction(Intent.ACTION_SEND);
       startActivity(sendIntent);
       return true;
     } catch (Exception e) { // For example, android.content.ActivityNotFoundException
       Log.e(TAG, e.toString());
       return false;
     }
   } else if (command.equals("showTwitter")) {
     try {
       String twitter_user_name = params;
       try {
         startActivity(
             new Intent(
                 Intent.ACTION_VIEW,
                 Uri.parse("twitter://user?screen_name=" + twitter_user_name)));
       } catch (Exception e) {
         startActivity(
             new Intent(
                 Intent.ACTION_VIEW, Uri.parse("https://twitter.com/#!/" + twitter_user_name)));
       }
       return true;
     } catch (Exception e) { // For example, android.content.ActivityNotFoundException
       Log.e(TAG, e.toString());
       return false;
     }
   } else if (command.equals("launchMarket")) {
     // Don't need this, can just use launchBrowser with a market:
     // http://stackoverflow.com/questions/3442366/android-link-to-market-from-inside-another-app
     // http://developer.android.com/guide/publishing/publishing.html#marketintent
     return false;
   } else if (command.equals("toast")) {
     Toast toast = Toast.makeText(this, params, Toast.LENGTH_SHORT);
     toast.show();
     Log.i(TAG, params);
     return true;
   } else if (command.equals("showKeyboard") && mSurfaceView != null) {
     InputMethodManager inputMethodManager =
         (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
     // No idea what the point of the ApplicationWindowToken is or if it
     // matters where we get it from...
     inputMethodManager.toggleSoftInputFromWindow(
         mSurfaceView.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
     return true;
   } else if (command.equals("hideKeyboard") && mSurfaceView != null) {
     InputMethodManager inputMethodManager =
         (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
     inputMethodManager.toggleSoftInputFromWindow(
         mSurfaceView.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
     return true;
   } else if (command.equals("inputbox")) {
     String title = "Input";
     String defString = "";
     String[] param = params.split(":");
     if (param[0].length() > 0) title = param[0];
     if (param.length > 1) defString = param[1];
     Log.i(TAG, "Launching inputbox: " + title + " " + defString);
     inputBox(title, defString, "OK");
     return true;
   } else if (command.equals("vibrate") && mSurfaceView != null) {
     int milliseconds = -1;
     if (params != "") {
       try {
         milliseconds = Integer.parseInt(params);
       } catch (NumberFormatException e) {
       }
     }
     // Special parameters to perform standard haptic feedback
     // operations
     // -1 = Standard keyboard press feedback
     // -2 = Virtual key press
     // -3 = Long press feedback
     // Note that these three do not require the VIBRATE Android
     // permission.
     switch (milliseconds) {
       case -1:
         mSurfaceView.performHapticFeedback(HapticFeedbackConstants.KEYBOARD_TAP);
         break;
       case -2:
         mSurfaceView.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
         break;
       case -3:
         mSurfaceView.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
         break;
       default:
         if (vibrator != null) {
           vibrator.vibrate(milliseconds);
         }
         break;
     }
     return true;
   } else if (command.equals("finish")) {
     finish();
   } else if (command.equals("rotate")) {
     updateScreenRotation();
     if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
       Log.i(TAG, "Must recreate activity on rotation");
     }
   } else if (command.equals("immersive")) {
     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
       updateSystemUiVisibility();
     }
   } else if (command.equals("recreate")) {
     exitEGLRenderLoop();
     recreate();
   } else if (command.equals("ask_permission") && params.equals("storage")) {
     askForStoragePermission();
   }
   return false;
 }