public void onWindowFocusChanged(boolean hasFocus) {
   super.onWindowFocusChanged(hasFocus);
   if (null == IOuyaActivity.GetUnityPlayer()) {
     Log.i("Unity", "IOuyaActivity.GetUnityPlayer() is null");
     return;
   }
   IOuyaActivity.GetUnityPlayer().windowFocusChanged(hasFocus);
 }
 // Pass any keys not handled by (unfocused) views straight to UnityPlayer
 public boolean onKeyMultiple(int keyCode, int count, KeyEvent event) {
   if (null == IOuyaActivity.GetUnityPlayer()) {
     Log.i("Unity", "IOuyaActivity.GetUnityPlayer() is null");
     return false;
   }
   return IOuyaActivity.GetUnityPlayer().onKeyMultiple(keyCode, count, event);
 }
 public void onConfigurationChanged(Configuration newConfig) {
   super.onConfigurationChanged(newConfig);
   if (null == IOuyaActivity.GetUnityPlayer()) {
     Log.i("Unity", "IOuyaActivity.GetUnityPlayer() is null");
     return;
   }
   IOuyaActivity.GetUnityPlayer().configurationChanged(newConfig);
 }
  @Override
  public boolean onTrackballEvent(MotionEvent event) {
    if (null == IOuyaActivity.GetUnityPlayer()) {
      Log.i("Unity", "IOuyaActivity.GetUnityPlayer() is null");
      return false;
    }

    return IOuyaActivity.GetUnityPlayer().onTrackballEvent(event);
  }
  @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (null == IOuyaActivity.GetUnityPlayer()) {
      Log.i("Unity", "IOuyaActivity.GetUnityPlayer() is null");
      return false;
    }

    return IOuyaActivity.GetUnityPlayer().onKeyDown(keyCode, event);
  }
  @Override
  public boolean onGenericMotionEvent(MotionEvent event) {
    if (null == IOuyaActivity.GetUnityPlayer()) {
      Log.i("Unity", "IOuyaActivity.GetUnityPlayer() is null");
      return false;
    }

    // return IOuyaActivity.GetUnityPlayer().onGenericMotionEvent(event);

    // rupert is awesome!!! workaround to not detecting axis input (3.5.7)
    return IOuyaActivity.GetUnityPlayer().onTouchEvent(event);
  }
  @Override
  protected void onDestroy() {
    TestOuyaFacade test = IOuyaActivity.GetTestOuyaFacade();
    if (null != test) {
      test.onDestroy();
    }

    if (null != IOuyaActivity.GetUnityPlayer()) {
      IOuyaActivity.GetUnityPlayer().quit();
    }

    super.onDestroy();
  }
  @Override
  public boolean onKeyUp(int keyCode, KeyEvent event) {
    // Log.i("Unity", "onKeyUp keyCode=" + keyCode);
    if (keyCode == OuyaController.BUTTON_MENU) {
      Log.i("Unity", "BroadcastReceiver tell Unity we see the menu button up");
      UnityPlayer.UnitySendMessage("OuyaGameObject", "onMenuButtonUp", "");
      Log.i("Unity", "BroadcastReceiver notified Unity onMenuButtonUp");
    }

    if (null == IOuyaActivity.GetUnityPlayer()) {
      Log.i("Unity", "IOuyaActivity.GetUnityPlayer() is null");
      return false;
    }

    return IOuyaActivity.GetUnityPlayer().onKeyUp(keyCode, event);
  }
 @Override
 protected void onSaveInstanceState(final Bundle outState) {
   TestOuyaFacade test = IOuyaActivity.GetTestOuyaFacade();
   if (null != test) {
     test.onSaveInstanceState(outState);
   }
 }
 @Override
 public void onReceive(Context context, Intent intent) {
   TestOuyaFacade test = IOuyaActivity.GetTestOuyaFacade();
   if (null != test) {
     test.requestReceipts();
   }
 }
  @Override
  public void onPause() {
    if (m_enableUnity) {
      UnityPlayer.UnitySendMessage("OuyaGameObject", "onPause", "");
    }

    Boolean isFinishing = isFinishing();
    if (m_enableLogging) {
      Log.i("Unity", "isFinishing=" + isFinishing);
    }
    if (isFinishing) {
      IOuyaActivity.GetUnityPlayer().quit();
    }

    IOuyaActivity.GetUnityPlayer().pause();

    super.onPause();
  }
  @Override
  public void onResume() {
    if (m_enableUnity) {
      UnityPlayer.UnitySendMessage("OuyaGameObject", "onResume", "");
    }

    super.onResume();

    IOuyaActivity.GetUnityPlayer().resume();
  }
 /**
  * Check for the result from a call through to the authentication intent. If the authentication
  * was successful then re-try the purchase.
  */
 @Override
 protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
   if (resultCode == RESULT_OK) {
     TestOuyaFacade test = IOuyaActivity.GetTestOuyaFacade();
     if (null != test) {
       switch (requestCode) {
         case TestOuyaFacade.GAMER_UUID_AUTHENTICATION_ACTIVITY_ID:
           test.fetchGamerUUID();
           break;
         case TestOuyaFacade.PURCHASE_AUTHENTICATION_ACTIVITY_ID:
           test.restartInterruptedPurchase();
           break;
       }
     }
   }
 }
  protected void onCreate(Bundle savedInstanceState) {
    // make activity accessible to Unity
    IOuyaActivity.SetActivity(this);

    // make bundle accessible to Unity
    IOuyaActivity.SetSavedInstanceState(savedInstanceState);

    super.onCreate(savedInstanceState);

    // load the raw resource for the application key
    try {
      InputStream inputStream = getResources().openRawResource(R.raw.key);
      byte[] applicationKey = new byte[inputStream.available()];
      inputStream.read(applicationKey);
      inputStream.close();
      IOuyaActivity.SetApplicationKey(applicationKey);
    } catch (IOException e) {
      e.printStackTrace();
    }

    // Create the UnityPlayer
    IOuyaActivity.SetUnityPlayer(new UnityPlayer(this));
    int glesMode = IOuyaActivity.GetUnityPlayer().getSettings().getInt("gles_mode", 1);
    boolean trueColor8888 = false;
    IOuyaActivity.GetUnityPlayer().init(glesMode, trueColor8888);
    setContentView(R.layout.main);

    // Add the Unity view
    FrameLayout layout = (FrameLayout) findViewById(R.id.unityLayout);
    LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    layout.addView(IOuyaActivity.GetUnityPlayer().getView(), 0, lp);
    IOuyaActivity.SetLayout(layout);

    // Set the focus
    RelativeLayout mainLayout = (RelativeLayout) findViewById(R.id.mainLayout);
    mainLayout.setFocusableInTouchMode(true);

    Context context = getBaseContext();

    // Init the controller
    OuyaController.init(context);
  }