@Override
  public boolean onKeyDown(int keyCode, KeyEvent keyEvent) {
    // Log.i(TAG, "onKeyDown keyCode=" + DebugInput.debugGetButtonName(keyCode));

    int playerNum = OuyaController.getPlayerNumByDeviceId(keyEvent.getDeviceId());
    if (playerNum < 0) {
      Log.e(TAG, "Failed to find playerId for Controller=" + keyEvent.getDevice().getName());
      return true;
    }

    int action = keyEvent.getAction();
    dispatchKeyEventNative(playerNum, keyCode, action);
    return true;
  }
  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);
  }
  @Override
  public boolean onGenericMotionEvent(MotionEvent motionEvent) {
    // DebugInput.debugMotionEvent(motionEvent);

    int playerNum = OuyaController.getPlayerNumByDeviceId(motionEvent.getDeviceId());
    if (playerNum < 0) {
      Log.e(TAG, "Failed to find playerId for Controller=" + motionEvent.getDevice().getName());
      return true;
    }

    dispatchGenericMotionEventNative(
        playerNum, OuyaController.AXIS_LS_X, motionEvent.getAxisValue(OuyaController.AXIS_LS_X));
    dispatchGenericMotionEventNative(
        playerNum, OuyaController.AXIS_LS_Y, motionEvent.getAxisValue(OuyaController.AXIS_LS_Y));
    dispatchGenericMotionEventNative(
        playerNum, OuyaController.AXIS_RS_X, motionEvent.getAxisValue(OuyaController.AXIS_RS_X));
    dispatchGenericMotionEventNative(
        playerNum, OuyaController.AXIS_RS_Y, motionEvent.getAxisValue(OuyaController.AXIS_RS_Y));
    dispatchGenericMotionEventNative(
        playerNum, OuyaController.AXIS_L2, motionEvent.getAxisValue(OuyaController.AXIS_L2));
    dispatchGenericMotionEventNative(
        playerNum, OuyaController.AXIS_R2, motionEvent.getAxisValue(OuyaController.AXIS_R2));
    return true;
  }
 @Override
 public boolean onKeyUp(int keyCode, KeyEvent event) {
   boolean handled = OuyaController.onKeyUp(keyCode, event);
   return handled || super.onKeyUp(keyCode, event);
 }
 @Override
 public boolean onGenericMotionEvent(MotionEvent event) {
   boolean handled = OuyaController.onGenericMotionEvent(event);
   return handled || super.onGenericMotionEvent(event);
 }