private void cancelAutoFocus() {
    Log.e(TAG, "cancelAutofocus");
    // Note: CameraController.getFocusMode(); will return
    // 'FOCUS_MODE_AUTO' if actual
    // mode is in fact FOCUS_MODE_CONTINUOUS_PICTURE or
    // FOCUS_MODE_CONTINUOUS_VIDEO
    int fm = CameraController.getFocusMode();
    if (fm != CameraParameters.AF_MODE_UNSUPPORTED) {
      if (fm != preferenceFocusMode && preferenceFocusMode != CameraParameters.MF_MODE) {
        CameraController.cancelAutoFocus();
        CameraController.setCameraFocusMode(preferenceFocusMode);
      }
    }

    // Reset the tap area before calling mListener.cancelAutofocus.
    // Otherwise, focus mode stays at auto and the tap area passed to the
    // driver is not reset.
    CameraController.setCameraFocusAreas(null);
    ApplicationScreen.instance.setCameraMeteringMode(ApplicationScreen.getMeteringMode());
    resetTouchFocus();

    mState = STATE_IDLE;
    CameraController.setFocusState(CameraController.FOCUS_STATE_IDLE);

    updateFocusUI();
    mHandler.removeMessages(RESET_TOUCH_FOCUS);
  }
  public void onTouchAreas(MotionEvent e) {
    // Initialize variables.
    int x = Math.round(e.getX());
    int y = Math.round(e.getY());
    int focusWidth = mFocusIndicatorRotateLayout.getWidth();
    int focusHeight = mFocusIndicatorRotateLayout.getHeight();
    int previewWidth = mPreviewWidth;
    int previewHeight = mPreviewHeight;
    int displayWidth = ApplicationScreen.getAppResources().getDisplayMetrics().widthPixels;
    int diffWidth = displayWidth - previewWidth;

    int paramsLayoutHeight = 0;

    int xOffset = (focusLayout.getWidth() - previewWidth) / 2;
    int yOffset = (focusLayout.getHeight() - previewHeight) / 2;

    if (mFocusArea == null) {
      mFocusArea = new ArrayList<Area>();
      mFocusArea.add(new Area(new Rect(), 1000));
    }

    if (mMeteringArea == null) {
      mMeteringArea = new ArrayList<Area>();
      mMeteringArea.add(new Area(new Rect(), 1000));
    }

    boolean isNexus6 = CameraController.isNexus6;
    // Convert the coordinates to driver format.
    // AE area is bigger because exposure is sensitive and
    // easy to over- or underexposure if area is too small.
    calculateTapArea(
        focusWidth,
        focusHeight,
        1f,
        x,
        y,
        ApplicationScreen.getPreviewSurfaceView().getWidth(),
        ApplicationScreen.getPreviewSurfaceView().getHeight(),
        mFocusArea.get(0).rect);
    if (ApplicationScreen.getMeteringMode() != -1
        && ApplicationScreen.getMeteringMode() == CameraParameters.meteringModeSpot)
      calculateTapArea(
          20 + (isNexus6 ? focusWidth : 0),
          20 + (isNexus6 ? focusHeight : 0),
          1f,
          x,
          y,
          ApplicationScreen.getPreviewSurfaceView().getWidth(),
          ApplicationScreen.getPreviewSurfaceView().getHeight(),
          mMeteringArea.get(0).rect);
    else mMeteringArea = null;

    if (mFocusAreaSupported) {
      // Use margin to set the focus indicator to the touched area.
      RelativeLayout.LayoutParams p =
          (RelativeLayout.LayoutParams) mFocusIndicatorRotateLayout.getLayoutParams();
      int left =
          Util.clamp(
              x - focusWidth / 2 + xOffset,
              diffWidth / 2,
              (previewWidth - focusWidth + xOffset * 2) - diffWidth / 2);
      int top =
          Util.clamp(
              y - focusHeight / 2 + yOffset,
              paramsLayoutHeight / 2,
              (previewHeight - focusHeight + yOffset * 2) - paramsLayoutHeight / 2);
      p.setMargins(left, top, 0, 0);
      // Disable "center" rule because we no longer want to put it in the
      // center.
      int[] rules = p.getRules();
      rules[RelativeLayout.CENTER_IN_PARENT] = 0;
      mFocusIndicatorRotateLayout.requestLayout();
    }

    // Set the focus area and metering area.
    if (mFocusAreaSupported && needAutoFocusCall() && (e.getAction() == MotionEvent.ACTION_UP)) {
      CameraController.cancelAutoFocus();
      if (preferenceFocusMode == CameraParameters.AF_MODE_CONTINUOUS_PICTURE
          || preferenceFocusMode == CameraParameters.AF_MODE_CONTINUOUS_VIDEO) {
        CameraController.setCameraFocusMode(CameraParameters.AF_MODE_AUTO);
      }

      setFocusParameters();
      setMeteringParameters();
      autoFocus();

    } else if (e.getAction() == MotionEvent.ACTION_UP
        && ApplicationScreen.instance.isShotOnTap() == 1
        && !PluginManager.getInstance().getActiveMode().modeID.equals("video")) {

      ApplicationScreen.getGUIManager().onHardwareShutterButtonPressed();
    } else if (e.getAction() == MotionEvent.ACTION_UP
        && ApplicationScreen.instance.isShotOnTap() == 2
        && !PluginManager.getInstance().getActiveMode().modeID.equals("video")) {
      if (isDoubleClick) {
        ApplicationScreen.getGUIManager().onHardwareShutterButtonPressed();
        isDoubleClick = false;
      }
    } else { // Just show the indicator in all other cases.
      autoFocus();
      // Reset the metering area in 3 seconds.
      mHandler.removeMessages(RESET_TOUCH_FOCUS);
      mHandler.sendEmptyMessageDelayed(RESET_TOUCH_FOCUS, RESET_TOUCH_FOCUS_DELAY);
    }
  }
  public void onTouchFocusAndMeteringArea(MotionEvent e) {
    if (!mFocusAreaSupported) return;

    int xRaw = (int) e.getRawX();
    int yRaw = (int) e.getRawY();

    // Initialize variables.
    int focusWidth = mFocusIndicatorRotateLayout.getWidth();
    int focusHeight = mFocusIndicatorRotateLayout.getHeight();
    int previewWidth = mPreviewWidth;
    int previewHeight = mPreviewHeight;
    int displayWidth = ApplicationScreen.getAppResources().getDisplayMetrics().widthPixels;
    int displayHeight = ApplicationScreen.getAppResources().getDisplayMetrics().heightPixels;
    int diffWidth = displayWidth - previewWidth;
    int diffHeight = displayHeight - previewHeight;

    // Initialize variables.
    int xOffset = (focusLayout.getWidth() - previewWidth) / 2;
    int yOffset = (focusLayout.getHeight() - previewHeight) / 2;

    if (mFocusArea == null) {
      mFocusArea = new ArrayList<Area>();
      mFocusArea.add(new Area(new Rect(), 1000));
    }

    if (mMeteringArea == null) {
      mMeteringArea = new ArrayList<Area>();
      mMeteringArea.add(new Area(new Rect(), 1000));
    }

    // Use margin to set the metering indicator to the touched area.
    RelativeLayout.LayoutParams p =
        (RelativeLayout.LayoutParams) mFocusIndicatorRotateLayout.getLayoutParams();
    int left =
        Util.clamp(
            xRaw - focusWidth / 2 + xOffset,
            diffWidth / 2,
            (previewWidth - focusWidth + xOffset * 2) - diffWidth / 2);
    int top =
        Util.clamp(
            yRaw - focusHeight / 2 + yOffset - diffHeight / 2,
            0,
            previewHeight - focusHeight + yOffset * 2);

    p.leftMargin = left;
    p.topMargin = top;

    int[] rules = p.getRules();
    rules[RelativeLayout.CENTER_IN_PARENT] = 0;

    mFocusIndicatorRotateLayout.setLayoutParams(p);

    calculateTapAreaByTopLeft(
        focusWidth,
        focusHeight,
        1f,
        top,
        left,
        ApplicationScreen.getPreviewSurfaceView().getWidth(),
        ApplicationScreen.getPreviewSurfaceView().getHeight(),
        mFocusArea.get(0).rect);

    if (ApplicationScreen.getMeteringMode() != -1
        && ApplicationScreen.getMeteringMode() == CameraParameters.meteringModeSpot)
      calculateTapAreaByTopLeft(
          focusWidth,
          focusHeight,
          1f,
          top,
          left,
          ApplicationScreen.getPreviewSurfaceView().getWidth(),
          ApplicationScreen.getPreviewSurfaceView().getHeight(),
          mMeteringArea.get(0).rect);
    else mMeteringArea = null;

    // Set the focus area and metering area.
    if ((mFocusAreaSupported && needAutoFocusCall() && (e.getAction() == MotionEvent.ACTION_UP))
        || CameraController.isRemoteCamera()) {
      CameraController.cancelAutoFocus();
      if (preferenceFocusMode == CameraParameters.AF_MODE_CONTINUOUS_PICTURE
          || preferenceFocusMode == CameraParameters.AF_MODE_CONTINUOUS_VIDEO) {
        CameraController.setCameraFocusMode(CameraParameters.AF_MODE_AUTO);
      }

      setFocusParameters();
      setMeteringParameters();
      autoFocus();

    } else if (e.getAction() == MotionEvent.ACTION_UP
        && ApplicationScreen.instance.isShotOnTap() == 1
        && !PluginManager.getInstance().getActiveMode().modeID.equals("video")
        && !currentTouch) {

      ApplicationScreen.getGUIManager().onHardwareShutterButtonPressed();
    } else if (e.getAction() == MotionEvent.ACTION_UP
        && ApplicationScreen.instance.isShotOnTap() == 2
        && !PluginManager.getInstance().getActiveMode().modeID.equals("video")
        && !currentTouch) {
      if (isDoubleClick) {
        ApplicationScreen.getGUIManager().onHardwareShutterButtonPressed();
        isDoubleClick = false;
      }
    } else if (e.getAction()
        == MotionEvent.ACTION_UP) { // Just show the indicator in all other cases.
      autoFocus();
      // updateFocusUI();
      // Reset the metering area in 3 seconds.
      mHandler.removeMessages(RESET_TOUCH_FOCUS);
      mHandler.sendEmptyMessageDelayed(RESET_TOUCH_FOCUS, RESET_TOUCH_FOCUS_DELAY);
    }

    mFocusIndicatorRotateLayout.requestLayout();
  }
  @Override
  public boolean onTouch(View view, MotionEvent e) {
    if (CameraController.isRemoteCamera()) {
      if (e.getAction() == MotionEvent.ACTION_UP) {
        onTouchAreas(e);
      }
      return true;
    }

    updateCurrentTouch(e);

    if (e.getPointerCount() > 1) {
      splitMode = true;
      mHandler.removeMessages(START_TOUCH_FOCUS);

      onTouchMeteringArea(e);
      onTouchFocusArea(e);

      return true;
    }

    if (splitMode && e.getAction() == MotionEvent.ACTION_DOWN) {
      mMeteringIndicatorRotateLayout.setVisibility(View.GONE);
      ApplicationScreen.instance.setCameraMeteringMode(ApplicationScreen.getMeteringMode());
      splitMode = false;
    }

    if (splitMode) {
      return true;
    }

    // Check if it's double click
    if (e.getAction() == MotionEvent.ACTION_UP) {
      lastTouchTime1 = lastTouchTime2;
      lastTouchTime2 = System.currentTimeMillis();

      if (lastTouchTime2 - lastTouchTime1 < 1000) {
        isDoubleClick = true;

        // If shot on double click
        if (ApplicationScreen.instance.isShotOnTap() == 2) {
          // Cancel delayed focus, which was created by second click
          mHandler.removeMessages(START_TOUCH_FOCUS);

          // If state is Focused start capture
          if (mState == STATE_SUCCESS) {
            String modeID = PluginManager.getInstance().getActiveMode().modeID;
            if (!modeID.equals("video") && !currentTouch) {
              ApplicationScreen.getGUIManager().onHardwareShutterButtonPressed();
              isDoubleClick = false;
            }
          }
          return true;
        }
      } else {
        isDoubleClick = false;
      }
    }

    // Not handle touch event if no need of autoFocus and refuse 'shot on
    // tap' in video mode.
    if (!mInitialized
        || mState == STATE_FOCUSING_SNAP_ON_FINISH
        || mState == STATE_INACTIVE
        || mFocusDisabled
        || !CameraController.isFocusModeSupported()
        || (!(needAutoFocusCall() || isContinuousFocusMode())
            && !(ApplicationScreen.instance.isShotOnTap() > 0
                && !PluginManager.getInstance().getActiveMode().modeID.equals("video"))))
      return false;

    // Let users be able to cancel previous touch focus.
    if ((mState == STATE_FOCUSING)
        && !delayedFocus
        && ApplicationScreen.instance.isShotOnTap() != 2) {
      focusCanceled = true;
      cancelAutoFocus();
      int fm = CameraController.getFocusMode();
      if ((preferenceFocusMode == CameraParameters.AF_MODE_CONTINUOUS_PICTURE
              || preferenceFocusMode == CameraParameters.AF_MODE_CONTINUOUS_VIDEO)
          && fm != CameraParameters.AF_MODE_UNSUPPORTED
          && preferenceFocusMode != CameraController.getFocusMode()
          && preferenceFocusMode != CameraParameters.MF_MODE) {
        CameraController.setCameraFocusMode(preferenceFocusMode);
      } else if (CameraController.isGalaxyNote3 && preferenceFocusMode != CameraParameters.MF_MODE)
      // Kind of hack to
      // prevent Note 3 of
      // permanent 'auto focus
      // failed' state
      {
        CameraController.setCameraFocusMode(CameraParameters.AF_MODE_CONTINUOUS_PICTURE);
        CameraController.setCameraFocusMode(preferenceFocusMode);
      }
      return true;
    }

    switch (e.getAction()) {
      case MotionEvent.ACTION_DOWN:
        focusCanceled = false;
        delayedFocus = false;
        X = e.getX();
        Y = e.getY();

        lastEvent = MotionEvent.obtain(e);
        mHandler.sendEmptyMessageDelayed(START_TOUCH_FOCUS, START_TOUCH_FOCUS_DELAY);

        return true;
      case MotionEvent.ACTION_MOVE:
        {
          float difX = e.getX();
          float difY = e.getY();

          if ((Math.abs(difX - X) > 50 || Math.abs(difY - Y) > 50) && !focusCanceled) {
            focusCanceled = true;
            cancelAutoFocus();
            mHandler.removeMessages(START_TOUCH_FOCUS);
            return true;
          } else return true;
        }
      case MotionEvent.ACTION_UP:
        mHandler.removeMessages(START_TOUCH_FOCUS);
        if (focusCanceled || delayedFocus) return true;
        break;
      default:
        break;
    }

    onTouchAreas(e);

    return true;
  }