public void onDestroy() {
    renderer.clearSprites();

    if (canvasView != null) {
      canvasView.onStop();
    }
  }
  public void onResume() {
    if (glView != null) {
      glView.onResume();
    }

    if (canvasView != null) {
      canvasView.onStart();
    }
  }
  public void onPause() {
    if (glView != null) {
      glView.onPause();
    }

    if (canvasView != null) {
      canvasView.onStop();
    }
  }
 private void initCanvasSurfaceView() {
   if (canvasView != null) {
     canvasView.setRenderer(renderer);
     canvasView.setOnTouchListener(this);
   }
 }
  public HudViewController(Activity context, boolean useSoftwareRendering) {
    joypadOpacity = 1f;
    this.context = context;
    this.useSoftwareRendering = useSoftwareRendering;
    gestureDetector = new EnhancedGestureDetector(context, this);

    canvasView = null;
    joysticks = new JoystickBase[2];

    glView = new GLSurfaceView(context);
    glView.setEGLContextClientVersion(2);

    context.setContentView(glView);

    renderer = new VideoStageRenderer(context, null);

    if (useSoftwareRendering) {
      // Replacing OpneGl based view with Canvas based one
      //			RelativeLayout root = (RelativeLayout) context.findViewById(R.id.controllerRootLayout);
      //			root.removeView(glView);
      glView = null;

      canvasView = new VideoStageView(context);
      canvasView.setLayoutParams(
          new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
      //			root.addView(canvasView, 0);
    }

    initNavdataStrings();
    initCanvasSurfaceView();
    initGLSurfaceView();

    Resources res = context.getResources();

    btnSettings =
        new Button(res, R.drawable.btn_settings, R.drawable.btn_settings_pressed, Align.TOP_LEFT);
    btnSettings.setMargin(0, 0, 0, (int) res.getDimension(R.dimen.hud_btn_settings_margin_left));

    btnBack = new Button(res, R.drawable.btn_back, R.drawable.btn_back_pressed, Align.TOP_LEFT);
    btnBack.setMargin(0, 0, 0, res.getDimensionPixelOffset(R.dimen.hud_btn_back_margin_left));

    btnEmergency =
        new Button(
            res,
            R.drawable.btn_emergency_normal,
            R.drawable.btn_emergency_pressed,
            Align.TOP_CENTER);
    btnTakeOff =
        new Button(
            res,
            R.drawable.btn_take_off_normal,
            R.drawable.btn_take_off_pressed,
            Align.BOTTOM_CENTER);
    btnLand =
        new Button(
            res, R.drawable.btn_landing, R.drawable.btn_landing_pressed, Align.BOTTOM_CENTER);
    btnLand.setVisible(false);

    Image topBarBg = new Image(res, R.drawable.barre_haut, Align.TOP_CENTER);
    topBarBg.setSizeParams(SizeParams.FILL_SCREEN, SizeParams.NONE);
    topBarBg.setAlphaEnabled(false);

    bottomBarBg = new Image(res, R.drawable.barre_bas, Align.BOTTOM_CENTER);
    bottomBarBg.setSizeParams(SizeParams.FILL_SCREEN, SizeParams.NONE);
    bottomBarBg.setAlphaEnabled(false);

    btnPhoto = new Button(res, R.drawable.btn_photo, R.drawable.btn_photo_pressed, Align.TOP_RIGHT);
    btnRecord =
        new ToggleButton(
            res,
            R.drawable.btn_record,
            R.drawable.btn_record_pressed,
            R.drawable.btn_record1,
            R.drawable.btn_record1_pressed,
            R.drawable.btn_record2,
            Align.TOP_RIGHT);
    btnRecord.setMargin(0, res.getDimensionPixelOffset(R.dimen.hud_btn_rec_margin_right), 0, 0);

    txtRecord = new Text(context, "REC", Align.TOP_RIGHT);
    txtRecord.setMargin(
        (int) res.getDimension(R.dimen.hud_rec_text_margin_top),
        (int) res.getDimension(R.dimen.hud_rec_text_margin_right),
        0,
        0);
    txtRecord.setTextColor(Color.WHITE);
    txtRecord.setTypeface(TYPEFACE.Helvetica(context));
    txtRecord.setTextSize(res.getDimensionPixelSize(R.dimen.hud_rec_text_size));

    usbIndicator = new Image(res, R.drawable.picto_usb_actif, Align.TOP_RIGHT);
    usbIndicator.setMargin(
        0, res.getDimensionPixelOffset(R.dimen.hud_usb_indicator_margin_right), 0, 0);

    prevRemainingTime = -1;
    txtUsbRemaining = new Text(context, "KO", Align.TOP_RIGHT);
    txtUsbRemaining.setMargin(
        res.getDimensionPixelOffset(R.dimen.hud_usb_indicator_text_margin_top),
        res.getDimensionPixelOffset(R.dimen.hud_usb_indicator_text_margin_right),
        0,
        0);
    txtUsbRemaining.setTypeface(TYPEFACE.Helvetica(context));
    txtUsbRemaining.setTextSize(res.getDimensionPixelSize(R.dimen.hud_usb_indicator_text_size));

    btnCameraSwitch =
        new Button(res, R.drawable.btn_camera, R.drawable.btn_camera_pressed, Align.TOP_RIGHT);
    btnCameraSwitch.setMargin(
        0, res.getDimensionPixelOffset(R.dimen.hud_btn_camera_switch_margin_right), 0, 0);

    int batteryIndicatorRes[] = {
      R.drawable.btn_battery_0,
      R.drawable.btn_battery_1,
      R.drawable.btn_battery_2,
      R.drawable.btn_battery_3
    };

    batteryIndicator = new Indicator(res, batteryIndicatorRes, Align.TOP_LEFT);
    batteryIndicator.setMargin(
        0, 0, 0, (int) res.getDimension(R.dimen.hud_battery_indicator_margin_left));

    txtBatteryStatus = new Text(context, "0%", Align.TOP_LEFT);
    txtBatteryStatus.setMargin(
        (int) res.getDimension(R.dimen.hud_battery_text_margin_top),
        0,
        0,
        (int) res.getDimension(R.dimen.hud_battery_indicator_margin_left)
            + batteryIndicator.getWidth());
    txtBatteryStatus.setTextColor(Color.WHITE);
    txtBatteryStatus.setTypeface(TYPEFACE.Helvetica(context));
    txtBatteryStatus.setTextSize((int) res.getDimension(R.dimen.hud_battery_text_size));

    int margin = res.getDimensionPixelOffset(R.dimen.hud_sphero_mode_margin);
    btnCombinedYawOn =
        new Button(res, R.drawable.btn_sphero1, R.drawable.btn_sphero1, Align.BOTTOM_RIGHT);
    btnCombinedYawOn.setMargin(
        0,
        res.getDimensionPixelOffset(R.dimen.hud_joy_margin),
        bottomBarBg.getHeight() + margin,
        0);

    btnCombinedYawOff =
        new Button(res, R.drawable.btn_sphero2, R.drawable.btn_sphero2, Align.BOTTOM_RIGHT);
    btnCombinedYawOff.setMargin(
        0,
        res.getDimensionPixelOffset(R.dimen.hud_joy_margin),
        bottomBarBg.getHeight() + margin,
        0);
    btnCombinedYawOff.setVisible(false);

    int wifiIndicatorRes[] = {
      R.drawable.btn_wifi_0, R.drawable.btn_wifi_1, R.drawable.btn_wifi_2, R.drawable.btn_wifi_3
    };

    wifiIndicator = new Indicator(res, wifiIndicatorRes, Align.TOP_LEFT);
    wifiIndicator.setMargin(
        0, 0, 0, (int) res.getDimension(R.dimen.hud_wifi_indicator_margin_left));

    buttons = new Button[10];
    buttons[0] = btnSettings;
    buttons[1] = btnEmergency;
    buttons[2] = btnTakeOff;
    buttons[3] = btnLand;
    buttons[4] = btnPhoto;
    buttons[5] = btnRecord;
    buttons[6] = btnCameraSwitch;
    buttons[7] = btnBack;
    buttons[8] = btnCombinedYawOn;
    buttons[9] = btnCombinedYawOff;

    txtAlert = new Text(context, "", Align.TOP_CENTER);
    txtAlert.setMargin((int) res.getDimension(R.dimen.hud_alert_text_margin_top), 0, 0, 0);
    txtAlert.setTextColor(Color.RED);
    txtAlert.setTextSize((int) res.getDimension(R.dimen.hud_alert_text_size));
    txtAlert.setBold(true);
    txtAlert.blink(true);

    //		txtUsbRemaining = new Text(context, "KO", Align.TOP_RIGHT);
    //
    //	txtUsbRemaining.setMargin(res.getDimensionPixelOffset(R.dimen.hud_usb_indicator_text_margin_top),
    //									res.getDimensionPixelOffset(R.dimen.hud_usb_indicator_text_margin_right), 0, 0);
    //		txtUsbRemaining.setTypeface(TYPEFACE.Helvetica(context));
    //		txtUsbRemaining.setTextSize(res.getDimensionPixelSize(R.dimen.hud_usb_indicator_text_size));

    //		txtSphPitch = new Text(context, "sphero 1 - pitch : ", Align.TOP_LEFT);
    //		txtSphPitch.setMargin(60, 0, 0, 30);
    //		txtSphPitch.setTextColor(Color.GREEN);
    //		txtSphRoll = new Text(context, "sphero 1 - roll : ", Align.TOP_LEFT);
    //		txtSphRoll.setMargin(85, 0, 0, 30);
    //		txtSphRoll.setTextColor(Color.GREEN);
    //		txtDronePitch = new Text(context, "drone - pitch : ", Align.TOP_LEFT);
    //		txtDronePitch.setMargin(110, 0, 0, 30);
    //		txtDronePitch.setTextColor(Color.GREEN);
    //		txtDroneRoll = new Text(context, "drone - roll : ", Align.TOP_LEFT);
    //		txtDroneRoll.setMargin(135, 0, 0, 30);
    //		txtDroneRoll.setTextColor(Color.GREEN);
    //
    //		txtSph2Pitch = new Text(context, "sphero 2 - pitch : ", Align.TOP_LEFT);
    //		txtSph2Pitch.setMargin(60, 0, 0, 300);
    //		txtSph2Pitch.setTextColor(Color.GREEN);
    //		txtSph2Roll = new Text(context, "sphero 2 - roll : ", Align.TOP_LEFT);
    //		txtSph2Roll.setMargin(85, 0, 0, 300);
    //		txtSph2Roll.setTextColor(Color.GREEN);
    //		txtDroneGaz = new Text(context, "drone - gaz : ", Align.TOP_LEFT);
    //		txtDroneGaz.setMargin(110, 0, 0, 300);
    //		txtDroneGaz.setTextColor(Color.GREEN);
    //		txtDroneYaw = new Text(context, "drone - yaw : ", Align.TOP_LEFT);
    //		txtDroneYaw.setMargin(135, 0, 0, 300);
    //		txtDroneYaw.setTextColor(Color.GREEN);

    renderer.addSprite(TOP_BAR_ID, topBarBg);
    renderer.addSprite(BOTTOM_BAR_ID, bottomBarBg);
    renderer.addSprite(SETTINGS_ID, btnSettings);
    renderer.addSprite(BACK_BTN_ID, btnBack);
    renderer.addSprite(PHOTO_ID, btnPhoto);
    renderer.addSprite(RECORD_ID, btnRecord);
    renderer.addSprite(CAMERA_ID, btnCameraSwitch);
    renderer.addSprite(ALERT_ID, btnEmergency);
    renderer.addSprite(TAKE_OFF_ID, btnTakeOff);
    renderer.addSprite(LAND_ID, btnLand);
    renderer.addSprite(BATTERY_INDICATOR_ID, batteryIndicator);
    renderer.addSprite(WIFI_INDICATOR_ID, wifiIndicator);
    renderer.addSprite(EMERGENCY_LABEL_ID, txtAlert);
    renderer.addSprite(BATTERY_STATUS_LABEL_ID, txtBatteryStatus);
    renderer.addSprite(RECORD_LABEL_ID, txtRecord);
    renderer.addSprite(USB_INDICATOR_ID, usbIndicator);
    renderer.addSprite(USB_INDICATOR_TEXT_ID, txtUsbRemaining);
    renderer.addSprite(SPHERO_COMBINED_YAW_ON, btnCombinedYawOn);
    renderer.addSprite(SPHERO_COMBINED_YAW_OFF, btnCombinedYawOff);

    //		renderer.addSprite(20, txtSphPitch);
    //		renderer.addSprite(21, txtSphRoll);
    //		renderer.addSprite(22, txtDronePitch);
    //		renderer.addSprite(23, txtDroneRoll);
    //		renderer.addSprite(24, txtSph2Pitch);
    //		renderer.addSprite(25, txtSph2Roll);
    //		renderer.addSprite(26, txtDroneGaz);
    //		renderer.addSprite(27, txtDroneYaw);
  }